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 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <sys/ioctl.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
42 * We duplicate the following Linux kernel header defines here so we can still
43 * run at full speed on modern kernels in cases where an old toolchain was used
44 * to build GLib. This is often done deliberately to allow shipping binaries
45 * that need to run on a wide range of systems.
48 #define F_SETPIPE_SZ 1031
51 #define F_GETPIPE_SZ 1032
57 #include <sys/types.h>
60 #include "glib/gstdio.h"
62 #include "glib-unix.h"
66 #include "gfileattribute-priv.h"
67 #include "gfiledescriptorbased.h"
68 #include "gpollfilemonitor.h"
70 #include "gfileinputstream.h"
71 #include "gfileoutputstream.h"
72 #include "glocalfileoutputstream.h"
73 #include "glocalfileiostream.h"
74 #include "glocalfile.h"
75 #include "gcancellable.h"
76 #include "gasyncresult.h"
80 /* Linux defines loff_t as a way to simplify the offset types for calls like
81 * splice() and copy_file_range(). BSD has copy_file_range() but doesn’t define
82 * loff_t. Abstract that. */
90 * @short_description: File and Directory Handling
92 * @see_also: #GFileInfo, #GFileEnumerator
94 * #GFile is a high level abstraction for manipulating files on a
95 * virtual file system. #GFiles are lightweight, immutable objects
96 * that do no I/O upon creation. It is necessary to understand that
97 * #GFile objects do not represent files, merely an identifier for a
98 * file. All file content I/O is implemented as streaming operations
99 * (see #GInputStream and #GOutputStream).
101 * To construct a #GFile, you can use:
102 * - g_file_new_for_path() if you have a path.
103 * - g_file_new_for_uri() if you have a URI.
104 * - g_file_new_for_commandline_arg() for a command line argument.
105 * - g_file_new_tmp() to create a temporary file from a template.
106 * - g_file_new_tmp_async() to asynchronously create a temporary file.
107 * - g_file_new_tmp_dir_async() to asynchronously create a temporary directory.
108 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
109 * - g_file_new_build_filename() or g_file_new_build_filenamev() to create a file from path elements.
111 * One way to think of a #GFile is as an abstraction of a pathname. For
112 * normal files the system pathname is what is stored internally, but as
113 * #GFiles are extensible it could also be something else that corresponds
114 * to a pathname in a userspace implementation of a filesystem.
116 * #GFiles make up hierarchies of directories and files that correspond to
117 * the files on a filesystem. You can move through the file system with
118 * #GFile using g_file_get_parent() to get an identifier for the parent
119 * directory, g_file_get_child() to get a child within a directory,
120 * g_file_resolve_relative_path() to resolve a relative path between two
121 * #GFiles. There can be multiple hierarchies, so you may not end up at
122 * the same root if you repeatedly call g_file_get_parent() on two different
125 * All #GFiles have a basename (get with g_file_get_basename()). These names
126 * are byte strings that are used to identify the file on the filesystem
127 * (relative to its parent directory) and there is no guarantees that they
128 * have any particular charset encoding or even make any sense at all. If
129 * you want to use filenames in a user interface you should use the display
130 * name that you can get by requesting the
131 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
132 * This is guaranteed to be in UTF-8 and can be used in a user interface.
133 * But always store the real basename or the #GFile to use to actually
134 * access the file, because there is no way to go from a display name to
137 * Using #GFile as an identifier has the same weaknesses as using a path
138 * in that there may be multiple aliases for the same file. For instance,
139 * hard or soft links may cause two different #GFiles to refer to the same
140 * file. Other possible causes for aliases are: case insensitive filesystems,
141 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
142 * check if two #GFiles point to the same file you can query for the
143 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
144 * canonicalization of pathnames passed in, so that trivial differences in
145 * the path string used at creation (duplicated slashes, slash at end of
146 * path, "." or ".." path segments, etc) does not create different #GFiles.
148 * Many #GFile operations have both synchronous and asynchronous versions
149 * to suit your application. Asynchronous versions of synchronous functions
150 * simply have _async() appended to their function names. The asynchronous
151 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
152 * the operation, producing a GAsyncResult which is then passed to the
153 * function's matching _finish() operation.
155 * It is highly recommended to use asynchronous calls when running within a
156 * shared main loop, such as in the main thread of an application. This avoids
157 * I/O operations blocking other sources on the main loop from being dispatched.
158 * Synchronous I/O operations should be performed from worker threads. See the
159 * [introduction to asynchronous programming section][async-programming] for
162 * Some #GFile operations almost always take a noticeable amount of time, and
163 * so do not have synchronous analogs. Notable cases include:
164 * - g_file_mount_mountable() to mount a mountable file.
165 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
166 * - g_file_eject_mountable_with_operation() to eject a mountable file.
168 * ## Entity Tags # {#gfile-etag}
170 * One notable feature of #GFiles are entity tags, or "etags" for
171 * short. Entity tags are somewhat like a more abstract version of the
172 * traditional mtime, and can be used to quickly determine if the file
173 * has been modified from the version on the file system. See the
175 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
176 * for HTTP Etag headers, which are a very similar concept.
179 static void g_file_real_query_info_async (GFile *file,
180 const char *attributes,
181 GFileQueryInfoFlags flags,
183 GCancellable *cancellable,
184 GAsyncReadyCallback callback,
186 static GFileInfo * g_file_real_query_info_finish (GFile *file,
189 static void g_file_real_query_filesystem_info_async (GFile *file,
190 const char *attributes,
192 GCancellable *cancellable,
193 GAsyncReadyCallback callback,
195 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
198 static void g_file_real_enumerate_children_async (GFile *file,
199 const char *attributes,
200 GFileQueryInfoFlags flags,
202 GCancellable *cancellable,
203 GAsyncReadyCallback callback,
205 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
208 static void g_file_real_read_async (GFile *file,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
213 static GFileInputStream * g_file_real_read_finish (GFile *file,
216 static void g_file_real_append_to_async (GFile *file,
217 GFileCreateFlags flags,
219 GCancellable *cancellable,
220 GAsyncReadyCallback callback,
222 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
225 static void g_file_real_create_async (GFile *file,
226 GFileCreateFlags flags,
228 GCancellable *cancellable,
229 GAsyncReadyCallback callback,
231 static GFileOutputStream *g_file_real_create_finish (GFile *file,
234 static void g_file_real_replace_async (GFile *file,
236 gboolean make_backup,
237 GFileCreateFlags flags,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
245 static void g_file_real_delete_async (GFile *file,
247 GCancellable *cancellable,
248 GAsyncReadyCallback callback,
250 static gboolean g_file_real_delete_finish (GFile *file,
253 static void g_file_real_trash_async (GFile *file,
255 GCancellable *cancellable,
256 GAsyncReadyCallback callback,
258 static gboolean g_file_real_trash_finish (GFile *file,
261 static void g_file_real_move_async (GFile *source,
263 GFileCopyFlags flags,
265 GCancellable *cancellable,
266 GFileProgressCallback progress_callback,
267 gpointer progress_callback_data,
268 GAsyncReadyCallback callback,
270 static gboolean g_file_real_move_finish (GFile *file,
271 GAsyncResult *result,
273 static void g_file_real_make_directory_async (GFile *file,
275 GCancellable *cancellable,
276 GAsyncReadyCallback callback,
278 static gboolean g_file_real_make_directory_finish (GFile *file,
281 static void g_file_real_make_symbolic_link_async (GFile *file,
282 const char *symlink_value,
284 GCancellable *cancellable,
285 GAsyncReadyCallback callback,
287 static gboolean g_file_real_make_symbolic_link_finish (GFile *file,
288 GAsyncResult *result,
290 static void g_file_real_open_readwrite_async (GFile *file,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
295 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
298 static void g_file_real_create_readwrite_async (GFile *file,
299 GFileCreateFlags flags,
301 GCancellable *cancellable,
302 GAsyncReadyCallback callback,
304 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
307 static void g_file_real_replace_readwrite_async (GFile *file,
309 gboolean make_backup,
310 GFileCreateFlags flags,
312 GCancellable *cancellable,
313 GAsyncReadyCallback callback,
315 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
318 static gboolean g_file_real_set_attributes_from_info (GFile *file,
320 GFileQueryInfoFlags flags,
321 GCancellable *cancellable,
323 static void g_file_real_set_display_name_async (GFile *file,
324 const char *display_name,
326 GCancellable *cancellable,
327 GAsyncReadyCallback callback,
329 static GFile * g_file_real_set_display_name_finish (GFile *file,
332 static void g_file_real_set_attributes_async (GFile *file,
334 GFileQueryInfoFlags flags,
336 GCancellable *cancellable,
337 GAsyncReadyCallback callback,
339 static gboolean g_file_real_set_attributes_finish (GFile *file,
343 static void g_file_real_find_enclosing_mount_async (GFile *file,
345 GCancellable *cancellable,
346 GAsyncReadyCallback callback,
348 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
351 static void g_file_real_copy_async (GFile *source,
353 GFileCopyFlags flags,
355 GCancellable *cancellable,
356 GFileProgressCallback progress_callback,
357 gpointer progress_callback_data,
358 GAsyncReadyCallback callback,
360 static gboolean g_file_real_copy_finish (GFile *file,
364 static gboolean g_file_real_measure_disk_usage (GFile *file,
365 GFileMeasureFlags flags,
366 GCancellable *cancellable,
367 GFileMeasureProgressCallback progress_callback,
368 gpointer progress_data,
373 static void g_file_real_measure_disk_usage_async (GFile *file,
374 GFileMeasureFlags flags,
376 GCancellable *cancellable,
377 GFileMeasureProgressCallback progress_callback,
378 gpointer progress_data,
379 GAsyncReadyCallback callback,
381 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
382 GAsyncResult *result,
388 typedef GFileIface GFileInterface;
389 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
392 g_file_default_init (GFileIface *iface)
394 iface->enumerate_children_async = g_file_real_enumerate_children_async;
395 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
396 iface->set_display_name_async = g_file_real_set_display_name_async;
397 iface->set_display_name_finish = g_file_real_set_display_name_finish;
398 iface->query_info_async = g_file_real_query_info_async;
399 iface->query_info_finish = g_file_real_query_info_finish;
400 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
401 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
402 iface->set_attributes_async = g_file_real_set_attributes_async;
403 iface->set_attributes_finish = g_file_real_set_attributes_finish;
404 iface->read_async = g_file_real_read_async;
405 iface->read_finish = g_file_real_read_finish;
406 iface->append_to_async = g_file_real_append_to_async;
407 iface->append_to_finish = g_file_real_append_to_finish;
408 iface->create_async = g_file_real_create_async;
409 iface->create_finish = g_file_real_create_finish;
410 iface->replace_async = g_file_real_replace_async;
411 iface->replace_finish = g_file_real_replace_finish;
412 iface->delete_file_async = g_file_real_delete_async;
413 iface->delete_file_finish = g_file_real_delete_finish;
414 iface->trash_async = g_file_real_trash_async;
415 iface->trash_finish = g_file_real_trash_finish;
416 iface->move_async = g_file_real_move_async;
417 iface->move_finish = g_file_real_move_finish;
418 iface->make_directory_async = g_file_real_make_directory_async;
419 iface->make_directory_finish = g_file_real_make_directory_finish;
420 iface->make_symbolic_link_async = g_file_real_make_symbolic_link_async;
421 iface->make_symbolic_link_finish = g_file_real_make_symbolic_link_finish;
422 iface->open_readwrite_async = g_file_real_open_readwrite_async;
423 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
424 iface->create_readwrite_async = g_file_real_create_readwrite_async;
425 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
426 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
427 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
428 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
429 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
430 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
431 iface->copy_async = g_file_real_copy_async;
432 iface->copy_finish = g_file_real_copy_finish;
433 iface->measure_disk_usage = g_file_real_measure_disk_usage;
434 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
435 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
441 * @file: input #GFile
443 * Checks to see if a file is native to the platform.
445 * A native file is one expressed in the platform-native filename format,
446 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
447 * as it might be on a locally mounted remote filesystem.
449 * On some systems non-native files may be available using the native
450 * filesystem via a userspace filesystem (FUSE), in these cases this call
451 * will return %FALSE, but g_file_get_path() will still return a native path.
453 * This call does no blocking I/O.
455 * Returns: %TRUE if @file is native
458 g_file_is_native (GFile *file)
462 g_return_val_if_fail (G_IS_FILE (file), FALSE);
464 iface = G_FILE_GET_IFACE (file);
466 return (* iface->is_native) (file);
471 * g_file_has_uri_scheme:
472 * @file: input #GFile
473 * @uri_scheme: a string containing a URI scheme
475 * Checks to see if a #GFile has a given URI scheme.
477 * This call does no blocking I/O.
479 * Returns: %TRUE if #GFile's backend supports the
480 * given URI scheme, %FALSE if URI scheme is %NULL,
481 * not supported, or #GFile is invalid.
484 g_file_has_uri_scheme (GFile *file,
485 const char *uri_scheme)
489 g_return_val_if_fail (G_IS_FILE (file), FALSE);
490 g_return_val_if_fail (uri_scheme != NULL, FALSE);
492 iface = G_FILE_GET_IFACE (file);
494 return (* iface->has_uri_scheme) (file, uri_scheme);
499 * g_file_get_uri_scheme:
500 * @file: input #GFile
502 * Gets the URI scheme for a #GFile.
503 * RFC 3986 decodes the scheme as:
505 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
507 * Common schemes include "file", "http", "ftp", etc.
509 * The scheme can be different from the one used to construct the #GFile,
510 * in that it might be replaced with one that is logically equivalent to the #GFile.
512 * This call does no blocking I/O.
514 * Returns: (nullable): a string containing the URI scheme for the given
515 * #GFile or %NULL if the #GFile was constructed with an invalid URI. The
516 * returned string should be freed with g_free() when no longer needed.
519 g_file_get_uri_scheme (GFile *file)
523 g_return_val_if_fail (G_IS_FILE (file), NULL);
525 iface = G_FILE_GET_IFACE (file);
527 return (* iface->get_uri_scheme) (file);
532 * g_file_get_basename: (virtual get_basename)
533 * @file: input #GFile
535 * Gets the base name (the last component of the path) for a given #GFile.
537 * If called for the top level of a system (such as the filesystem root
538 * or a uri like sftp://host/) it will return a single directory separator
539 * (and on Windows, possibly a drive letter).
541 * The base name is a byte string (not UTF-8). It has no defined encoding
542 * or rules other than it may not contain zero bytes. If you want to use
543 * filenames in a user interface you should use the display name that you
544 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
545 * attribute with g_file_query_info().
547 * This call does no blocking I/O.
549 * Returns: (type filename) (nullable): string containing the #GFile's
550 * base name, or %NULL if given #GFile is invalid. The returned string
551 * should be freed with g_free() when no longer needed.
554 g_file_get_basename (GFile *file)
558 g_return_val_if_fail (G_IS_FILE (file), NULL);
560 iface = G_FILE_GET_IFACE (file);
562 return (* iface->get_basename) (file);
566 * g_file_get_path: (virtual get_path)
567 * @file: input #GFile
569 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
570 * guaranteed to be an absolute, canonical path. It might contain symlinks.
572 * This call does no blocking I/O.
574 * Returns: (type filename) (nullable): string containing the #GFile's path,
575 * or %NULL if no such path exists. The returned string should be freed
576 * with g_free() when no longer needed.
579 g_file_get_path (GFile *file)
583 g_return_val_if_fail (G_IS_FILE (file), NULL);
585 iface = G_FILE_GET_IFACE (file);
587 return (* iface->get_path) (file);
591 file_peek_path_generic (GFile *file)
594 static GQuark _file_path_quark = 0;
596 if (G_UNLIKELY (_file_path_quark) == 0)
597 _file_path_quark = g_quark_from_static_string ("gio-file-path");
599 /* We need to be careful about threading, as two threads calling
600 * g_file_peek_path() on the same file could race: both would see
601 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
602 * the path, but the second thread to add it would end up freeing the path
603 * set by the first thread. The first thread would still return the pointer
604 * to that freed path, though, resulting an a read-after-free. Handle that
605 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
609 gchar *new_path = NULL;
611 path = g_object_get_qdata ((GObject*)file, _file_path_quark);
616 new_path = g_file_get_path (file);
617 if (new_path == NULL)
620 /* By passing NULL here, we ensure we never replace existing data: */
621 if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
622 NULL, (gpointer) new_path,
623 (GDestroyNotify) g_free, NULL))
637 * @file: input #GFile
639 * Exactly like g_file_get_path(), but caches the result via
640 * g_object_set_qdata_full(). This is useful for example in C
641 * applications which mix `g_file_*` APIs with native ones. It
642 * also avoids an extra duplicated string when possible, so will be
643 * generally more efficient.
645 * This call does no blocking I/O.
647 * Returns: (type filename) (nullable): string containing the #GFile's path,
648 * or %NULL if no such path exists. The returned string is owned by @file.
652 g_file_peek_path (GFile *file)
654 if (G_IS_LOCAL_FILE (file))
655 return _g_local_file_get_filename ((GLocalFile *) file);
656 return file_peek_path_generic (file);
661 * @file: input #GFile
663 * Gets the URI for the @file.
665 * This call does no blocking I/O.
667 * Returns: a string containing the #GFile's URI. If the #GFile was constructed
668 * with an invalid URI, an invalid URI is returned.
669 * The returned string should be freed with g_free()
670 * when no longer needed.
673 g_file_get_uri (GFile *file)
677 g_return_val_if_fail (G_IS_FILE (file), NULL);
679 iface = G_FILE_GET_IFACE (file);
681 return (* iface->get_uri) (file);
685 * g_file_get_parse_name:
686 * @file: input #GFile
688 * Gets the parse name of the @file.
689 * A parse name is a UTF-8 string that describes the
690 * file such that one can get the #GFile back using
691 * g_file_parse_name().
693 * This is generally used to show the #GFile as a nice
694 * full-pathname kind of string in a user interface,
695 * like in a location entry.
697 * For local files with names that can safely be converted
698 * to UTF-8 the pathname is used, otherwise the IRI is used
699 * (a form of URI that allows UTF-8 characters unescaped).
701 * This call does no blocking I/O.
703 * Returns: a string containing the #GFile's parse name.
704 * The returned string should be freed with g_free()
705 * when no longer needed.
708 g_file_get_parse_name (GFile *file)
712 g_return_val_if_fail (G_IS_FILE (file), NULL);
714 iface = G_FILE_GET_IFACE (file);
716 return (* iface->get_parse_name) (file);
721 * @file: input #GFile
723 * Duplicates a #GFile handle. This operation does not duplicate
724 * the actual file or directory represented by the #GFile; see
725 * g_file_copy() if attempting to copy a file.
727 * g_file_dup() is useful when a second handle is needed to the same underlying
728 * file, for use in a separate thread (#GFile is not thread-safe). For use
729 * within the same thread, use g_object_ref() to increment the existing object’s
732 * This call does no blocking I/O.
734 * Returns: (transfer full): a new #GFile that is a duplicate
735 * of the given #GFile.
738 g_file_dup (GFile *file)
742 g_return_val_if_fail (G_IS_FILE (file), NULL);
744 iface = G_FILE_GET_IFACE (file);
746 return (* iface->dup) (file);
751 * @file: (type GFile): #gconstpointer to a #GFile
753 * Creates a hash value for a #GFile.
755 * This call does no blocking I/O.
758 * Returns: 0 if @file is not a valid #GFile, otherwise an
759 * integer that can be used as hash value for the #GFile.
760 * This function is intended for easily hashing a #GFile to
761 * add to a #GHashTable or similar data structure.
764 g_file_hash (gconstpointer file)
768 g_return_val_if_fail (G_IS_FILE (file), 0);
770 iface = G_FILE_GET_IFACE (file);
772 return (* iface->hash) ((GFile *)file);
777 * @file1: the first #GFile
778 * @file2: the second #GFile
780 * Checks if the two given #GFiles refer to the same file.
782 * Note that two #GFiles that differ can still refer to the same
783 * file on the filesystem due to various forms of filename
786 * This call does no blocking I/O.
788 * Returns: %TRUE if @file1 and @file2 are equal.
791 g_file_equal (GFile *file1,
796 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
797 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
802 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
805 iface = G_FILE_GET_IFACE (file1);
807 return (* iface->equal) (file1, file2);
813 * @file: input #GFile
815 * Gets the parent directory for the @file.
816 * If the @file represents the root directory of the
817 * file system, then %NULL will be returned.
819 * This call does no blocking I/O.
821 * Returns: (nullable) (transfer full): a #GFile structure to the
822 * parent of the given #GFile or %NULL if there is no parent. Free
823 * the returned object with g_object_unref().
826 g_file_get_parent (GFile *file)
830 g_return_val_if_fail (G_IS_FILE (file), NULL);
832 iface = G_FILE_GET_IFACE (file);
834 return (* iface->get_parent) (file);
839 * @file: input #GFile
840 * @parent: (nullable): the parent to check for, or %NULL
842 * Checks if @file has a parent, and optionally, if it is @parent.
844 * If @parent is %NULL then this function returns %TRUE if @file has any
845 * parent at all. If @parent is non-%NULL then %TRUE is only returned
846 * if @file is an immediate child of @parent.
848 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
849 * the case that @parent is %NULL).
854 g_file_has_parent (GFile *file,
857 GFile *actual_parent;
860 g_return_val_if_fail (G_IS_FILE (file), FALSE);
861 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
863 actual_parent = g_file_get_parent (file);
865 if (actual_parent != NULL)
868 result = g_file_equal (parent, actual_parent);
872 g_object_unref (actual_parent);
882 * @file: input #GFile
883 * @name: (type filename): string containing the child's basename
885 * Gets a child of @file with basename equal to @name.
887 * Note that the file with that specific name might not exist, but
888 * you can still have a #GFile that points to it. You can use this
889 * for instance to create that file.
891 * This call does no blocking I/O.
893 * Returns: (transfer full): a #GFile to a child specified by @name.
894 * Free the returned object with g_object_unref().
897 g_file_get_child (GFile *file,
900 g_return_val_if_fail (G_IS_FILE (file), NULL);
901 g_return_val_if_fail (name != NULL, NULL);
902 g_return_val_if_fail (!g_path_is_absolute (name), NULL);
904 return g_file_resolve_relative_path (file, name);
908 * g_file_get_child_for_display_name:
909 * @file: input #GFile
910 * @display_name: string to a possible child
911 * @error: return location for an error
913 * Gets the child of @file for a given @display_name (i.e. a UTF-8
914 * version of the name). If this function fails, it returns %NULL
915 * and @error will be set. This is very useful when constructing a
916 * #GFile for a new file and the user entered the filename in the
917 * user interface, for instance when you select a directory and
918 * type a filename in the file selector.
920 * This call does no blocking I/O.
922 * Returns: (transfer full): a #GFile to the specified child, or
923 * %NULL if the display name couldn't be converted.
924 * Free the returned object with g_object_unref().
927 g_file_get_child_for_display_name (GFile *file,
928 const char *display_name,
933 g_return_val_if_fail (G_IS_FILE (file), NULL);
934 g_return_val_if_fail (display_name != NULL, NULL);
936 iface = G_FILE_GET_IFACE (file);
938 return (* iface->get_child_for_display_name) (file, display_name, error);
943 * @file: input #GFile
944 * @prefix: input #GFile
946 * Checks whether @file has the prefix specified by @prefix.
948 * In other words, if the names of initial elements of @file's
949 * pathname match @prefix. Only full pathname elements are matched,
950 * so a path like /foo is not considered a prefix of /foobar, only
953 * A #GFile is not a prefix of itself. If you want to check for
954 * equality, use g_file_equal().
956 * This call does no I/O, as it works purely on names. As such it can
957 * sometimes return %FALSE even if @file is inside a @prefix (from a
958 * filesystem point of view), because the prefix of @file is an alias
961 * Virtual: prefix_matches
962 * Returns: %TRUE if the @file's parent, grandparent, etc is @prefix,
966 g_file_has_prefix (GFile *file,
971 g_return_val_if_fail (G_IS_FILE (file), FALSE);
972 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
974 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
977 iface = G_FILE_GET_IFACE (file);
979 /* The vtable function differs in arg order since
980 * we're using the old contains_file call
982 return (* iface->prefix_matches) (prefix, file);
986 * g_file_get_relative_path: (virtual get_relative_path)
987 * @parent: input #GFile
988 * @descendant: input #GFile
990 * Gets the path for @descendant relative to @parent.
992 * This call does no blocking I/O.
994 * Returns: (type filename) (nullable): string with the relative path from
995 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
996 * prefix. The returned string should be freed with g_free() when
1000 g_file_get_relative_path (GFile *parent,
1005 g_return_val_if_fail (G_IS_FILE (parent), NULL);
1006 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
1008 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
1011 iface = G_FILE_GET_IFACE (parent);
1013 return (* iface->get_relative_path) (parent, descendant);
1017 * g_file_resolve_relative_path:
1018 * @file: input #GFile
1019 * @relative_path: (type filename): a given relative path string
1021 * Resolves a relative path for @file to an absolute path.
1023 * This call does no blocking I/O.
1025 * If the @relative_path is an absolute path name, the resolution
1026 * is done absolutely (without taking @file path as base).
1028 * Returns: (transfer full): a #GFile for the resolved path.
1031 g_file_resolve_relative_path (GFile *file,
1032 const char *relative_path)
1036 g_return_val_if_fail (G_IS_FILE (file), NULL);
1037 g_return_val_if_fail (relative_path != NULL, NULL);
1039 iface = G_FILE_GET_IFACE (file);
1041 return (* iface->resolve_relative_path) (file, relative_path);
1045 * g_file_enumerate_children:
1046 * @file: input #GFile
1047 * @attributes: an attribute query string
1048 * @flags: a set of #GFileQueryInfoFlags
1049 * @cancellable: (nullable): optional #GCancellable object,
1051 * @error: #GError for error reporting
1053 * Gets the requested information about the files in a directory.
1054 * The result is a #GFileEnumerator object that will give out
1055 * #GFileInfo objects for all the files in the directory.
1057 * The @attributes value is a string that specifies the file
1058 * attributes that should be gathered. It is not an error if
1059 * it's not possible to read a particular requested attribute
1060 * from a file - it just won't be set. @attributes should
1061 * be a comma-separated list of attributes or attribute wildcards.
1062 * The wildcard "*" means all attributes, and a wildcard like
1063 * "standard::*" means all attributes in the standard namespace.
1064 * An example attribute query be "standard::*,owner::user".
1065 * The standard attributes are available as defines, like
1066 * %G_FILE_ATTRIBUTE_STANDARD_NAME. %G_FILE_ATTRIBUTE_STANDARD_NAME should
1067 * always be specified if you plan to call g_file_enumerator_get_child() or
1068 * g_file_enumerator_iterate() on the returned enumerator.
1070 * If @cancellable is not %NULL, then the operation can be cancelled
1071 * by triggering the cancellable object from another thread. If the
1072 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1075 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1076 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1077 * error will be returned. Other errors are possible too.
1079 * Returns: (transfer full): A #GFileEnumerator if successful,
1080 * %NULL on error. Free the returned object with g_object_unref().
1083 g_file_enumerate_children (GFile *file,
1084 const char *attributes,
1085 GFileQueryInfoFlags flags,
1086 GCancellable *cancellable,
1091 g_return_val_if_fail (G_IS_FILE (file), NULL);
1093 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1096 iface = G_FILE_GET_IFACE (file);
1098 if (iface->enumerate_children == NULL)
1100 g_set_error_literal (error, G_IO_ERROR,
1101 G_IO_ERROR_NOT_SUPPORTED,
1102 _("Operation not supported"));
1106 return (* iface->enumerate_children) (file, attributes, flags,
1107 cancellable, error);
1111 * g_file_enumerate_children_async:
1112 * @file: input #GFile
1113 * @attributes: an attribute query string
1114 * @flags: a set of #GFileQueryInfoFlags
1115 * @io_priority: the [I/O priority][io-priority] of the request
1116 * @cancellable: (nullable): optional #GCancellable object,
1118 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1119 * to call when the request is satisfied
1120 * @user_data: the data to pass to callback function
1122 * Asynchronously gets the requested information about the files
1123 * in a directory. The result is a #GFileEnumerator object that will
1124 * give out #GFileInfo objects for all the files in the directory.
1126 * For more details, see g_file_enumerate_children() which is
1127 * the synchronous version of this call.
1129 * When the operation is finished, @callback will be called. You can
1130 * then call g_file_enumerate_children_finish() to get the result of
1134 g_file_enumerate_children_async (GFile *file,
1135 const char *attributes,
1136 GFileQueryInfoFlags flags,
1138 GCancellable *cancellable,
1139 GAsyncReadyCallback callback,
1144 g_return_if_fail (G_IS_FILE (file));
1146 iface = G_FILE_GET_IFACE (file);
1147 (* iface->enumerate_children_async) (file,
1157 * g_file_enumerate_children_finish:
1158 * @file: input #GFile
1159 * @res: a #GAsyncResult
1162 * Finishes an async enumerate children operation.
1163 * See g_file_enumerate_children_async().
1165 * Returns: (transfer full): a #GFileEnumerator or %NULL
1166 * if an error occurred.
1167 * Free the returned object with g_object_unref().
1170 g_file_enumerate_children_finish (GFile *file,
1176 g_return_val_if_fail (G_IS_FILE (file), NULL);
1177 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1179 if (g_async_result_legacy_propagate_error (res, error))
1182 iface = G_FILE_GET_IFACE (file);
1183 return (* iface->enumerate_children_finish) (file, res, error);
1187 * g_file_query_exists:
1188 * @file: input #GFile
1189 * @cancellable: (nullable): optional #GCancellable object,
1192 * Utility function to check if a particular file exists. This is
1193 * implemented using g_file_query_info() and as such does blocking I/O.
1195 * 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)
1196 * and then execute something based on the outcome of that, because the
1197 * file might have been created or removed in between the operations. The
1198 * general approach to handling that is to not check, but just do the
1199 * operation and handle the errors as they come.
1201 * As an example of race-free checking, take the case of reading a file,
1202 * and if it doesn't exist, creating it. There are two racy versions: read
1203 * it, and on error create it; and: check if it exists, if not create it.
1204 * These can both result in two processes creating the file (with perhaps
1205 * a partially written file as the result). The correct approach is to
1206 * always try to create the file with g_file_create() which will either
1207 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1209 * However, in many cases an existence check is useful in a user interface,
1210 * for instance to make a menu item sensitive/insensitive, so that you don't
1211 * have to fool users that something is possible and then just show an error
1212 * dialog. If you do this, you should make sure to also handle the errors
1213 * that can happen due to races when you execute the operation.
1215 * Returns: %TRUE if the file exists (and can be detected without error),
1216 * %FALSE otherwise (or if cancelled).
1219 g_file_query_exists (GFile *file,
1220 GCancellable *cancellable)
1224 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1226 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1227 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1230 g_object_unref (info);
1238 * g_file_query_file_type:
1239 * @file: input #GFile
1240 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1241 * @cancellable: (nullable): optional #GCancellable object,
1244 * Utility function to inspect the #GFileType of a file. This is
1245 * implemented using g_file_query_info() and as such does blocking I/O.
1247 * The primary use case of this method is to check if a file is
1248 * a regular file, directory, or symlink.
1250 * Returns: The #GFileType of the file and %G_FILE_TYPE_UNKNOWN
1251 * if the file does not exist
1256 g_file_query_file_type (GFile *file,
1257 GFileQueryInfoFlags flags,
1258 GCancellable *cancellable)
1261 GFileType file_type;
1263 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1264 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1268 file_type = g_file_info_get_file_type (info);
1269 g_object_unref (info);
1272 file_type = G_FILE_TYPE_UNKNOWN;
1278 * g_file_query_info:
1279 * @file: input #GFile
1280 * @attributes: an attribute query string
1281 * @flags: a set of #GFileQueryInfoFlags
1282 * @cancellable: (nullable): optional #GCancellable object,
1286 * Gets the requested information about specified @file.
1287 * The result is a #GFileInfo object that contains key-value
1288 * attributes (such as the type or size of the file).
1290 * The @attributes value is a string that specifies the file
1291 * attributes that should be gathered. It is not an error if
1292 * it's not possible to read a particular requested attribute
1293 * from a file - it just won't be set. @attributes should be a
1294 * comma-separated list of attributes or attribute wildcards.
1295 * The wildcard "*" means all attributes, and a wildcard like
1296 * "standard::*" means all attributes in the standard namespace.
1297 * An example attribute query be "standard::*,owner::user".
1298 * The standard attributes are available as defines, like
1299 * %G_FILE_ATTRIBUTE_STANDARD_NAME.
1301 * If @cancellable is not %NULL, then the operation can be cancelled
1302 * by triggering the cancellable object from another thread. If the
1303 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1306 * For symlinks, normally the information about the target of the
1307 * symlink is returned, rather than information about the symlink
1308 * itself. However if you pass %G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1309 * in @flags the information about the symlink itself will be returned.
1310 * Also, for symlinks that point to non-existing files the information
1311 * about the symlink itself will be returned.
1313 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1314 * returned. Other errors are possible too, and depend on what kind of
1315 * filesystem the file is on.
1317 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1318 * on error. Free the returned object with g_object_unref().
1321 g_file_query_info (GFile *file,
1322 const char *attributes,
1323 GFileQueryInfoFlags flags,
1324 GCancellable *cancellable,
1329 g_return_val_if_fail (G_IS_FILE (file), NULL);
1331 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1334 iface = G_FILE_GET_IFACE (file);
1336 if (iface->query_info == NULL)
1338 g_set_error_literal (error, G_IO_ERROR,
1339 G_IO_ERROR_NOT_SUPPORTED,
1340 _("Operation not supported"));
1344 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1348 * g_file_query_info_async:
1349 * @file: input #GFile
1350 * @attributes: an attribute query string
1351 * @flags: a set of #GFileQueryInfoFlags
1352 * @io_priority: the [I/O priority][io-priority] of the request
1353 * @cancellable: (nullable): optional #GCancellable object,
1355 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1356 * to call when the request is satisfied
1357 * @user_data: the data to pass to callback function
1359 * Asynchronously gets the requested information about specified @file.
1360 * The result is a #GFileInfo object that contains key-value attributes
1361 * (such as type or size for the file).
1363 * For more details, see g_file_query_info() which is the synchronous
1364 * version of this call.
1366 * When the operation is finished, @callback will be called. You can
1367 * then call g_file_query_info_finish() to get the result of the operation.
1370 g_file_query_info_async (GFile *file,
1371 const char *attributes,
1372 GFileQueryInfoFlags flags,
1374 GCancellable *cancellable,
1375 GAsyncReadyCallback callback,
1380 g_return_if_fail (G_IS_FILE (file));
1382 iface = G_FILE_GET_IFACE (file);
1383 (* iface->query_info_async) (file,
1393 * g_file_query_info_finish:
1394 * @file: input #GFile
1395 * @res: a #GAsyncResult
1398 * Finishes an asynchronous file info query.
1399 * See g_file_query_info_async().
1401 * Returns: (transfer full): #GFileInfo for given @file
1402 * or %NULL on error. Free the returned object with
1406 g_file_query_info_finish (GFile *file,
1412 g_return_val_if_fail (G_IS_FILE (file), NULL);
1413 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1415 if (g_async_result_legacy_propagate_error (res, error))
1418 iface = G_FILE_GET_IFACE (file);
1419 return (* iface->query_info_finish) (file, res, error);
1423 * g_file_query_filesystem_info:
1424 * @file: input #GFile
1425 * @attributes: an attribute query string
1426 * @cancellable: (nullable): optional #GCancellable object,
1430 * Similar to g_file_query_info(), but obtains information
1431 * about the filesystem the @file is on, rather than the file itself.
1432 * For instance the amount of space available and the type of
1435 * The @attributes value is a string that specifies the attributes
1436 * that should be gathered. It is not an error if it's not possible
1437 * to read a particular requested attribute from a file - it just
1438 * won't be set. @attributes should be a comma-separated list of
1439 * attributes or attribute wildcards. The wildcard "*" means all
1440 * attributes, and a wildcard like "filesystem::*" means all attributes
1441 * in the filesystem namespace. The standard namespace for filesystem
1442 * attributes is "filesystem". Common attributes of interest are
1443 * %G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1444 * in bytes), %G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1445 * and %G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1447 * If @cancellable is not %NULL, then the operation can be cancelled
1448 * by triggering the cancellable object from another thread. If the
1449 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1452 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1453 * be returned. Other errors are possible too, and depend on what
1454 * kind of filesystem the file is on.
1456 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1457 * Free the returned object with g_object_unref().
1460 g_file_query_filesystem_info (GFile *file,
1461 const char *attributes,
1462 GCancellable *cancellable,
1467 g_return_val_if_fail (G_IS_FILE (file), NULL);
1469 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1472 iface = G_FILE_GET_IFACE (file);
1474 if (iface->query_filesystem_info == NULL)
1476 g_set_error_literal (error, G_IO_ERROR,
1477 G_IO_ERROR_NOT_SUPPORTED,
1478 _("Operation not supported"));
1482 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1486 * g_file_query_filesystem_info_async:
1487 * @file: input #GFile
1488 * @attributes: an attribute query string
1489 * @io_priority: the [I/O priority][io-priority] of the request
1490 * @cancellable: (nullable): optional #GCancellable object,
1492 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1493 * to call when the request is satisfied
1494 * @user_data: the data to pass to callback function
1496 * Asynchronously gets the requested information about the filesystem
1497 * that the specified @file is on. The result is a #GFileInfo object
1498 * that contains key-value attributes (such as type or size for the
1501 * For more details, see g_file_query_filesystem_info() which is the
1502 * synchronous version of this call.
1504 * When the operation is finished, @callback will be called. You can
1505 * then call g_file_query_info_finish() to get the result of the
1509 g_file_query_filesystem_info_async (GFile *file,
1510 const char *attributes,
1512 GCancellable *cancellable,
1513 GAsyncReadyCallback callback,
1518 g_return_if_fail (G_IS_FILE (file));
1520 iface = G_FILE_GET_IFACE (file);
1521 (* iface->query_filesystem_info_async) (file,
1530 * g_file_query_filesystem_info_finish:
1531 * @file: input #GFile
1532 * @res: a #GAsyncResult
1535 * Finishes an asynchronous filesystem info query.
1536 * See g_file_query_filesystem_info_async().
1538 * Returns: (transfer full): #GFileInfo for given @file
1539 * or %NULL on error.
1540 * Free the returned object with g_object_unref().
1543 g_file_query_filesystem_info_finish (GFile *file,
1549 g_return_val_if_fail (G_IS_FILE (file), NULL);
1550 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1552 if (g_async_result_legacy_propagate_error (res, error))
1555 iface = G_FILE_GET_IFACE (file);
1556 return (* iface->query_filesystem_info_finish) (file, res, error);
1560 * g_file_find_enclosing_mount:
1561 * @file: input #GFile
1562 * @cancellable: (nullable): optional #GCancellable object,
1566 * Gets a #GMount for the #GFile.
1568 * #GMount is returned only for user interesting locations, see
1569 * #GVolumeMonitor. If the #GFileIface for @file does not have a #mount,
1570 * @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL #will be returned.
1572 * If @cancellable is not %NULL, then the operation can be cancelled by
1573 * triggering the cancellable object from another thread. If the operation
1574 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1576 * Returns: (transfer full): a #GMount where the @file is located
1577 * or %NULL on error.
1578 * Free the returned object with g_object_unref().
1581 g_file_find_enclosing_mount (GFile *file,
1582 GCancellable *cancellable,
1587 g_return_val_if_fail (G_IS_FILE (file), NULL);
1589 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1592 iface = G_FILE_GET_IFACE (file);
1593 if (iface->find_enclosing_mount == NULL)
1596 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1597 /* Translators: This is an error message when
1598 * trying to find the enclosing (user visible)
1599 * mount of a file, but none exists.
1601 _("Containing mount does not exist"));
1605 return (* iface->find_enclosing_mount) (file, cancellable, error);
1609 * g_file_find_enclosing_mount_async:
1611 * @io_priority: the [I/O priority][io-priority] of the request
1612 * @cancellable: (nullable): optional #GCancellable object,
1614 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1615 * to call when the request is satisfied
1616 * @user_data: the data to pass to callback function
1618 * Asynchronously gets the mount for the file.
1620 * For more details, see g_file_find_enclosing_mount() which is
1621 * the synchronous version of this call.
1623 * When the operation is finished, @callback will be called.
1624 * You can then call g_file_find_enclosing_mount_finish() to
1625 * get the result of the operation.
1628 g_file_find_enclosing_mount_async (GFile *file,
1630 GCancellable *cancellable,
1631 GAsyncReadyCallback callback,
1636 g_return_if_fail (G_IS_FILE (file));
1638 iface = G_FILE_GET_IFACE (file);
1639 (* iface->find_enclosing_mount_async) (file,
1647 * g_file_find_enclosing_mount_finish:
1649 * @res: a #GAsyncResult
1652 * Finishes an asynchronous find mount request.
1653 * See g_file_find_enclosing_mount_async().
1655 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1656 * Free the returned object with g_object_unref().
1659 g_file_find_enclosing_mount_finish (GFile *file,
1665 g_return_val_if_fail (G_IS_FILE (file), NULL);
1666 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1668 if (g_async_result_legacy_propagate_error (res, error))
1671 iface = G_FILE_GET_IFACE (file);
1672 return (* iface->find_enclosing_mount_finish) (file, res, error);
1678 * @file: #GFile to read
1679 * @cancellable: (nullable): a #GCancellable
1680 * @error: a #GError, or %NULL
1682 * Opens a file for reading. The result is a #GFileInputStream that
1683 * can be used to read the contents of the file.
1685 * If @cancellable is not %NULL, then the operation can be cancelled by
1686 * triggering the cancellable object from another thread. If the operation
1687 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1689 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1690 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1691 * error will be returned. Other errors are possible too, and depend
1692 * on what kind of filesystem the file is on.
1695 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1696 * Free the returned object with g_object_unref().
1699 g_file_read (GFile *file,
1700 GCancellable *cancellable,
1705 g_return_val_if_fail (G_IS_FILE (file), NULL);
1707 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1710 iface = G_FILE_GET_IFACE (file);
1712 if (iface->read_fn == NULL)
1714 g_set_error_literal (error, G_IO_ERROR,
1715 G_IO_ERROR_NOT_SUPPORTED,
1716 _("Operation not supported"));
1720 return (* iface->read_fn) (file, cancellable, error);
1725 * @file: input #GFile
1726 * @flags: a set of #GFileCreateFlags
1727 * @cancellable: (nullable): optional #GCancellable object,
1729 * @error: a #GError, or %NULL
1731 * Gets an output stream for appending data to the file.
1732 * If the file doesn't already exist it is created.
1734 * By default files created are generally readable by everyone,
1735 * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1736 * will be made readable only to the current user, to the level that
1737 * is supported on the target filesystem.
1739 * If @cancellable is not %NULL, then the operation can be cancelled
1740 * by triggering the cancellable object from another thread. If the
1741 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1744 * Some file systems don't allow all file names, and may return an
1745 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1746 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1747 * possible too, and depend on what kind of filesystem the file is on.
1749 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1750 * Free the returned object with g_object_unref().
1753 g_file_append_to (GFile *file,
1754 GFileCreateFlags flags,
1755 GCancellable *cancellable,
1760 g_return_val_if_fail (G_IS_FILE (file), NULL);
1762 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1765 iface = G_FILE_GET_IFACE (file);
1767 if (iface->append_to == NULL)
1769 g_set_error_literal (error, G_IO_ERROR,
1770 G_IO_ERROR_NOT_SUPPORTED,
1771 _("Operation not supported"));
1775 return (* iface->append_to) (file, flags, cancellable, error);
1780 * @file: input #GFile
1781 * @flags: a set of #GFileCreateFlags
1782 * @cancellable: (nullable): optional #GCancellable object,
1784 * @error: a #GError, or %NULL
1786 * Creates a new file and returns an output stream for writing to it.
1787 * The file must not already exist.
1789 * By default files created are generally readable by everyone,
1790 * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1791 * will be made readable only to the current user, to the level
1792 * that is supported on the target filesystem.
1794 * If @cancellable is not %NULL, then the operation can be cancelled
1795 * by triggering the cancellable object from another thread. If the
1796 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1799 * If a file or directory with this name already exists the
1800 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1801 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1802 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1803 * be returned. Other errors are possible too, and depend on what kind
1804 * of filesystem the file is on.
1806 * Returns: (transfer full): a #GFileOutputStream for the newly created
1807 * file, or %NULL on error.
1808 * Free the returned object with g_object_unref().
1811 g_file_create (GFile *file,
1812 GFileCreateFlags flags,
1813 GCancellable *cancellable,
1818 g_return_val_if_fail (G_IS_FILE (file), NULL);
1820 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1823 iface = G_FILE_GET_IFACE (file);
1825 if (iface->create == NULL)
1827 g_set_error_literal (error, G_IO_ERROR,
1828 G_IO_ERROR_NOT_SUPPORTED,
1829 _("Operation not supported"));
1833 return (* iface->create) (file, flags, cancellable, error);
1838 * @file: input #GFile
1839 * @etag: (nullable): an optional [entity tag][gfile-etag]
1840 * for the current #GFile, or #NULL to ignore
1841 * @make_backup: %TRUE if a backup should be created
1842 * @flags: a set of #GFileCreateFlags
1843 * @cancellable: (nullable): optional #GCancellable object,
1845 * @error: a #GError, or %NULL
1847 * Returns an output stream for overwriting the file, possibly
1848 * creating a backup copy of the file first. If the file doesn't exist,
1849 * it will be created.
1851 * This will try to replace the file in the safest way possible so
1852 * that any errors during the writing will not affect an already
1853 * existing copy of the file. For instance, for local files it
1854 * may write to a temporary file and then atomically rename over
1855 * the destination when the stream is closed.
1857 * By default files created are generally readable by everyone,
1858 * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1859 * will be made readable only to the current user, to the level that
1860 * is supported on the target filesystem.
1862 * If @cancellable is not %NULL, then the operation can be cancelled
1863 * by triggering the cancellable object from another thread. If the
1864 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1867 * If you pass in a non-%NULL @etag value and @file already exists, then
1868 * this value is compared to the current entity tag of the file, and if
1869 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1870 * generally means that the file has been changed since you last read
1871 * it. You can get the new etag from g_file_output_stream_get_etag()
1872 * after you've finished writing and closed the #GFileOutputStream. When
1873 * you load a new file you can use g_file_input_stream_query_info() to
1874 * get the etag of the file.
1876 * If @make_backup is %TRUE, this function will attempt to make a
1877 * backup of the current file before overwriting it. If this fails
1878 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1879 * want to replace anyway, try again with @make_backup set to %FALSE.
1881 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1882 * be returned, and if the file is some other form of non-regular file
1883 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1884 * file systems don't allow all file names, and may return an
1885 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1886 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1887 * possible too, and depend on what kind of filesystem the file is on.
1889 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1890 * Free the returned object with g_object_unref().
1893 g_file_replace (GFile *file,
1895 gboolean make_backup,
1896 GFileCreateFlags flags,
1897 GCancellable *cancellable,
1902 g_return_val_if_fail (G_IS_FILE (file), NULL);
1904 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1907 iface = G_FILE_GET_IFACE (file);
1909 if (iface->replace == NULL)
1911 g_set_error_literal (error, G_IO_ERROR,
1912 G_IO_ERROR_NOT_SUPPORTED,
1913 _("Operation not supported"));
1917 /* Handle empty tag string as NULL in consistent way. */
1918 if (etag && *etag == 0)
1921 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1925 * g_file_open_readwrite:
1926 * @file: #GFile to open
1927 * @cancellable: (nullable): a #GCancellable
1928 * @error: a #GError, or %NULL
1930 * Opens an existing file for reading and writing. The result is
1931 * a #GFileIOStream that can be used to read and write the contents
1934 * If @cancellable is not %NULL, then the operation can be cancelled
1935 * by triggering the cancellable object from another thread. If the
1936 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1939 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1940 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1941 * error will be returned. Other errors are possible too, and depend on
1942 * what kind of filesystem the file is on. Note that in many non-local
1943 * file cases read and write streams are not supported, so make sure you
1944 * really need to do read and write streaming, rather than just opening
1945 * for reading or writing.
1947 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1948 * Free the returned object with g_object_unref().
1953 g_file_open_readwrite (GFile *file,
1954 GCancellable *cancellable,
1959 g_return_val_if_fail (G_IS_FILE (file), NULL);
1961 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1964 iface = G_FILE_GET_IFACE (file);
1966 if (iface->open_readwrite == NULL)
1968 g_set_error_literal (error, G_IO_ERROR,
1969 G_IO_ERROR_NOT_SUPPORTED,
1970 _("Operation not supported"));
1974 return (* iface->open_readwrite) (file, cancellable, error);
1978 * g_file_create_readwrite:
1980 * @flags: a set of #GFileCreateFlags
1981 * @cancellable: (nullable): optional #GCancellable object,
1983 * @error: return location for a #GError, or %NULL
1985 * Creates a new file and returns a stream for reading and
1986 * writing to it. The file must not already exist.
1988 * By default files created are generally readable by everyone,
1989 * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1990 * will be made readable only to the current user, to the level
1991 * that is supported on the target filesystem.
1993 * If @cancellable is not %NULL, then the operation can be cancelled
1994 * by triggering the cancellable object from another thread. If the
1995 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1998 * If a file or directory with this name already exists, the
1999 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
2000 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
2001 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
2002 * will be returned. Other errors are possible too, and depend on what
2003 * kind of filesystem the file is on.
2005 * Note that in many non-local file cases read and write streams are
2006 * not supported, so make sure you really need to do read and write
2007 * streaming, rather than just opening for reading or writing.
2009 * Returns: (transfer full): a #GFileIOStream for the newly created
2010 * file, or %NULL on error.
2011 * Free the returned object with g_object_unref().
2016 g_file_create_readwrite (GFile *file,
2017 GFileCreateFlags flags,
2018 GCancellable *cancellable,
2023 g_return_val_if_fail (G_IS_FILE (file), NULL);
2025 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2028 iface = G_FILE_GET_IFACE (file);
2030 if (iface->create_readwrite == NULL)
2032 g_set_error_literal (error, G_IO_ERROR,
2033 G_IO_ERROR_NOT_SUPPORTED,
2034 _("Operation not supported"));
2038 return (* iface->create_readwrite) (file, flags, cancellable, error);
2042 * g_file_replace_readwrite:
2044 * @etag: (nullable): an optional [entity tag][gfile-etag]
2045 * for the current #GFile, or #NULL to ignore
2046 * @make_backup: %TRUE if a backup should be created
2047 * @flags: a set of #GFileCreateFlags
2048 * @cancellable: (nullable): optional #GCancellable object,
2050 * @error: return location for a #GError, or %NULL
2052 * Returns an output stream for overwriting the file in readwrite mode,
2053 * possibly creating a backup copy of the file first. If the file doesn't
2054 * exist, it will be created.
2056 * For details about the behaviour, see g_file_replace() which does the
2057 * same thing but returns an output stream only.
2059 * Note that in many non-local file cases read and write streams are not
2060 * supported, so make sure you really need to do read and write streaming,
2061 * rather than just opening for reading or writing.
2063 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2064 * Free the returned object with g_object_unref().
2069 g_file_replace_readwrite (GFile *file,
2071 gboolean make_backup,
2072 GFileCreateFlags flags,
2073 GCancellable *cancellable,
2078 g_return_val_if_fail (G_IS_FILE (file), NULL);
2080 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2083 iface = G_FILE_GET_IFACE (file);
2085 if (iface->replace_readwrite == NULL)
2087 g_set_error_literal (error, G_IO_ERROR,
2088 G_IO_ERROR_NOT_SUPPORTED,
2089 _("Operation not supported"));
2093 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2097 * g_file_read_async:
2098 * @file: input #GFile
2099 * @io_priority: the [I/O priority][io-priority] of the request
2100 * @cancellable: (nullable): optional #GCancellable object,
2102 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2103 * to call when the request is satisfied
2104 * @user_data: the data to pass to callback function
2106 * Asynchronously opens @file for reading.
2108 * For more details, see g_file_read() which is
2109 * the synchronous version of this call.
2111 * When the operation is finished, @callback will be called.
2112 * You can then call g_file_read_finish() to get the result
2116 g_file_read_async (GFile *file,
2118 GCancellable *cancellable,
2119 GAsyncReadyCallback callback,
2124 g_return_if_fail (G_IS_FILE (file));
2126 iface = G_FILE_GET_IFACE (file);
2127 (* iface->read_async) (file,
2135 * g_file_read_finish:
2136 * @file: input #GFile
2137 * @res: a #GAsyncResult
2138 * @error: a #GError, or %NULL
2140 * Finishes an asynchronous file read operation started with
2141 * g_file_read_async().
2143 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2144 * Free the returned object with g_object_unref().
2147 g_file_read_finish (GFile *file,
2153 g_return_val_if_fail (G_IS_FILE (file), NULL);
2154 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2156 if (g_async_result_legacy_propagate_error (res, error))
2159 iface = G_FILE_GET_IFACE (file);
2160 return (* iface->read_finish) (file, res, error);
2164 * g_file_append_to_async:
2165 * @file: input #GFile
2166 * @flags: a set of #GFileCreateFlags
2167 * @io_priority: the [I/O priority][io-priority] of the request
2168 * @cancellable: (nullable): optional #GCancellable object,
2170 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2171 * to call when the request is satisfied
2172 * @user_data: the data to pass to callback function
2174 * Asynchronously opens @file for appending.
2176 * For more details, see g_file_append_to() which is
2177 * the synchronous version of this call.
2179 * When the operation is finished, @callback will be called.
2180 * You can then call g_file_append_to_finish() to get the result
2184 g_file_append_to_async (GFile *file,
2185 GFileCreateFlags flags,
2187 GCancellable *cancellable,
2188 GAsyncReadyCallback callback,
2193 g_return_if_fail (G_IS_FILE (file));
2195 iface = G_FILE_GET_IFACE (file);
2196 (* iface->append_to_async) (file,
2205 * g_file_append_to_finish:
2206 * @file: input #GFile
2207 * @res: #GAsyncResult
2208 * @error: a #GError, or %NULL
2210 * Finishes an asynchronous file append operation started with
2211 * g_file_append_to_async().
2213 * Returns: (transfer full): a valid #GFileOutputStream
2214 * or %NULL on error.
2215 * Free the returned object with g_object_unref().
2218 g_file_append_to_finish (GFile *file,
2224 g_return_val_if_fail (G_IS_FILE (file), NULL);
2225 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2227 if (g_async_result_legacy_propagate_error (res, error))
2230 iface = G_FILE_GET_IFACE (file);
2231 return (* iface->append_to_finish) (file, res, error);
2235 * g_file_create_async:
2236 * @file: input #GFile
2237 * @flags: a set of #GFileCreateFlags
2238 * @io_priority: the [I/O priority][io-priority] of the request
2239 * @cancellable: (nullable): optional #GCancellable object,
2241 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2242 * to call when the request is satisfied
2243 * @user_data: the data to pass to callback function
2245 * Asynchronously creates a new file and returns an output stream
2246 * for writing to it. The file must not already exist.
2248 * For more details, see g_file_create() which is
2249 * the synchronous version of this call.
2251 * When the operation is finished, @callback will be called.
2252 * You can then call g_file_create_finish() to get the result
2256 g_file_create_async (GFile *file,
2257 GFileCreateFlags flags,
2259 GCancellable *cancellable,
2260 GAsyncReadyCallback callback,
2265 g_return_if_fail (G_IS_FILE (file));
2267 iface = G_FILE_GET_IFACE (file);
2268 (* iface->create_async) (file,
2277 * g_file_create_finish:
2278 * @file: input #GFile
2279 * @res: a #GAsyncResult
2280 * @error: a #GError, or %NULL
2282 * Finishes an asynchronous file create operation started with
2283 * g_file_create_async().
2285 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2286 * Free the returned object with g_object_unref().
2289 g_file_create_finish (GFile *file,
2295 g_return_val_if_fail (G_IS_FILE (file), NULL);
2296 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2298 if (g_async_result_legacy_propagate_error (res, error))
2301 iface = G_FILE_GET_IFACE (file);
2302 return (* iface->create_finish) (file, res, error);
2306 * g_file_replace_async:
2307 * @file: input #GFile
2308 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2309 * or %NULL to ignore
2310 * @make_backup: %TRUE if a backup should be created
2311 * @flags: a set of #GFileCreateFlags
2312 * @io_priority: the [I/O priority][io-priority] of the request
2313 * @cancellable: (nullable): optional #GCancellable object,
2315 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2316 * to call when the request is satisfied
2317 * @user_data: the data to pass to callback function
2319 * Asynchronously overwrites the file, replacing the contents,
2320 * possibly creating a backup copy of the file first.
2322 * For more details, see g_file_replace() which is
2323 * the synchronous version of this call.
2325 * When the operation is finished, @callback will be called.
2326 * You can then call g_file_replace_finish() to get the result
2330 g_file_replace_async (GFile *file,
2332 gboolean make_backup,
2333 GFileCreateFlags flags,
2335 GCancellable *cancellable,
2336 GAsyncReadyCallback callback,
2341 g_return_if_fail (G_IS_FILE (file));
2343 iface = G_FILE_GET_IFACE (file);
2344 (* iface->replace_async) (file,
2355 * g_file_replace_finish:
2356 * @file: input #GFile
2357 * @res: a #GAsyncResult
2358 * @error: a #GError, or %NULL
2360 * Finishes an asynchronous file replace operation started with
2361 * g_file_replace_async().
2363 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2364 * Free the returned object with g_object_unref().
2367 g_file_replace_finish (GFile *file,
2373 g_return_val_if_fail (G_IS_FILE (file), NULL);
2374 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2376 if (g_async_result_legacy_propagate_error (res, error))
2379 iface = G_FILE_GET_IFACE (file);
2380 return (* iface->replace_finish) (file, res, error);
2384 * g_file_open_readwrite_async
2385 * @file: input #GFile
2386 * @io_priority: the [I/O priority][io-priority] of the request
2387 * @cancellable: (nullable): optional #GCancellable object,
2389 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2390 * to call when the request is satisfied
2391 * @user_data: the data to pass to callback function
2393 * Asynchronously opens @file for reading and writing.
2395 * For more details, see g_file_open_readwrite() which is
2396 * the synchronous version of this call.
2398 * When the operation is finished, @callback will be called.
2399 * You can then call g_file_open_readwrite_finish() to get
2400 * the result of the operation.
2405 g_file_open_readwrite_async (GFile *file,
2407 GCancellable *cancellable,
2408 GAsyncReadyCallback callback,
2413 g_return_if_fail (G_IS_FILE (file));
2415 iface = G_FILE_GET_IFACE (file);
2416 (* iface->open_readwrite_async) (file,
2424 * g_file_open_readwrite_finish:
2425 * @file: input #GFile
2426 * @res: a #GAsyncResult
2427 * @error: a #GError, or %NULL
2429 * Finishes an asynchronous file read operation started with
2430 * g_file_open_readwrite_async().
2432 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2433 * Free the returned object with g_object_unref().
2438 g_file_open_readwrite_finish (GFile *file,
2444 g_return_val_if_fail (G_IS_FILE (file), NULL);
2445 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2447 if (g_async_result_legacy_propagate_error (res, error))
2450 iface = G_FILE_GET_IFACE (file);
2451 return (* iface->open_readwrite_finish) (file, res, error);
2455 * g_file_create_readwrite_async:
2456 * @file: input #GFile
2457 * @flags: a set of #GFileCreateFlags
2458 * @io_priority: the [I/O priority][io-priority] of the request
2459 * @cancellable: (nullable): optional #GCancellable object,
2461 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2462 * to call when the request is satisfied
2463 * @user_data: the data to pass to callback function
2465 * Asynchronously creates a new file and returns a stream
2466 * for reading and writing to it. The file must not already exist.
2468 * For more details, see g_file_create_readwrite() which is
2469 * the synchronous version of this call.
2471 * When the operation is finished, @callback will be called.
2472 * You can then call g_file_create_readwrite_finish() to get
2473 * the result of the operation.
2478 g_file_create_readwrite_async (GFile *file,
2479 GFileCreateFlags flags,
2481 GCancellable *cancellable,
2482 GAsyncReadyCallback callback,
2487 g_return_if_fail (G_IS_FILE (file));
2489 iface = G_FILE_GET_IFACE (file);
2490 (* iface->create_readwrite_async) (file,
2499 * g_file_create_readwrite_finish:
2500 * @file: input #GFile
2501 * @res: a #GAsyncResult
2502 * @error: a #GError, or %NULL
2504 * Finishes an asynchronous file create operation started with
2505 * g_file_create_readwrite_async().
2507 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2508 * Free the returned object with g_object_unref().
2513 g_file_create_readwrite_finish (GFile *file,
2519 g_return_val_if_fail (G_IS_FILE (file), NULL);
2520 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2522 if (g_async_result_legacy_propagate_error (res, error))
2525 iface = G_FILE_GET_IFACE (file);
2526 return (* iface->create_readwrite_finish) (file, res, error);
2530 * g_file_replace_readwrite_async:
2531 * @file: input #GFile
2532 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2533 * or %NULL to ignore
2534 * @make_backup: %TRUE if a backup should be created
2535 * @flags: a set of #GFileCreateFlags
2536 * @io_priority: the [I/O priority][io-priority] of the request
2537 * @cancellable: (nullable): optional #GCancellable object,
2539 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2540 * to call when the request is satisfied
2541 * @user_data: the data to pass to callback function
2543 * Asynchronously overwrites the file in read-write mode,
2544 * replacing the contents, possibly creating a backup copy
2545 * of the file first.
2547 * For more details, see g_file_replace_readwrite() which is
2548 * the synchronous version of this call.
2550 * When the operation is finished, @callback will be called.
2551 * You can then call g_file_replace_readwrite_finish() to get
2552 * the result of the operation.
2557 g_file_replace_readwrite_async (GFile *file,
2559 gboolean make_backup,
2560 GFileCreateFlags flags,
2562 GCancellable *cancellable,
2563 GAsyncReadyCallback callback,
2568 g_return_if_fail (G_IS_FILE (file));
2570 iface = G_FILE_GET_IFACE (file);
2571 (* iface->replace_readwrite_async) (file,
2582 * g_file_replace_readwrite_finish:
2583 * @file: input #GFile
2584 * @res: a #GAsyncResult
2585 * @error: a #GError, or %NULL
2587 * Finishes an asynchronous file replace operation started with
2588 * g_file_replace_readwrite_async().
2590 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2591 * Free the returned object with g_object_unref().
2596 g_file_replace_readwrite_finish (GFile *file,
2602 g_return_val_if_fail (G_IS_FILE (file), NULL);
2603 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2605 if (g_async_result_legacy_propagate_error (res, error))
2608 iface = G_FILE_GET_IFACE (file);
2609 return (* iface->replace_readwrite_finish) (file, res, error);
2613 copy_symlink (GFile *destination,
2614 GFileCopyFlags flags,
2615 GCancellable *cancellable,
2620 gboolean tried_delete;
2622 GFileType file_type;
2624 tried_delete = FALSE;
2628 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2630 /* Maybe it already existed, and we want to overwrite? */
2631 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2632 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2634 g_clear_error (&my_error);
2636 /* Don't overwrite if the destination is a directory */
2637 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2638 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2639 cancellable, &my_error);
2642 file_type = g_file_info_get_file_type (info);
2643 g_object_unref (info);
2645 if (file_type == G_FILE_TYPE_DIRECTORY)
2647 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2648 _("Can’t copy over directory"));
2653 if (!g_file_delete (destination, cancellable, error))
2656 tried_delete = TRUE;
2660 g_propagate_error (error, my_error);
2667 static GFileInputStream *
2668 open_source_for_copy (GFile *source,
2670 GFileCopyFlags flags,
2671 GCancellable *cancellable,
2675 GFileInputStream *ret;
2677 GFileType file_type;
2680 ret = g_file_read (source, cancellable, &my_error);
2684 /* There was an error opening the source, try to set a good error for it: */
2685 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2687 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2688 * as that is less useful to the app. Better check for errors on the
2691 g_error_free (my_error);
2694 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2695 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2696 cancellable, &my_error);
2698 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2700 file_type = g_file_info_get_file_type (info);
2701 g_object_unref (info);
2703 if (flags & G_FILE_COPY_OVERWRITE)
2705 if (file_type == G_FILE_TYPE_DIRECTORY)
2707 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2708 _("Can’t copy directory over directory"));
2711 /* continue to would_recurse error */
2715 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2716 _("Target file exists"));
2722 /* Error getting info from target, return that error
2723 * (except for NOT_FOUND, which is no error here)
2725 g_clear_object (&info);
2726 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2728 g_propagate_error (error, my_error);
2731 g_clear_error (&my_error);
2734 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2735 _("Can’t recursively copy directory"));
2739 g_propagate_error (error, my_error);
2744 should_copy (GFileAttributeInfo *info,
2745 gboolean copy_all_attributes,
2746 gboolean skip_perms)
2748 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2751 if (copy_all_attributes)
2752 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2753 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2757 * g_file_build_attribute_list_for_copy:
2758 * @file: a #GFile to copy attributes to
2759 * @flags: a set of #GFileCopyFlags
2760 * @cancellable: (nullable): optional #GCancellable object,
2762 * @error: a #GError, %NULL to ignore
2764 * Prepares the file attribute query string for copying to @file.
2766 * This function prepares an attribute query string to be
2767 * passed to g_file_query_info() to get a list of attributes
2768 * normally copied with the file (see g_file_copy_attributes()
2769 * for the detailed description). This function is used by the
2770 * implementation of g_file_copy_attributes() and is useful
2771 * when one needs to query and set the attributes in two
2772 * stages (e.g., for recursive move of a directory).
2774 * Returns: an attribute query string for g_file_query_info(),
2775 * or %NULL if an error occurs.
2780 g_file_build_attribute_list_for_copy (GFile *file,
2781 GFileCopyFlags flags,
2782 GCancellable *cancellable,
2786 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2790 gboolean copy_all_attributes;
2791 gboolean skip_perms;
2793 g_return_val_if_fail (G_IS_FILE (file), NULL);
2794 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
2795 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2797 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2798 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2800 /* Ignore errors here, if the target supports no attributes there is
2801 * nothing to copy. We still honor the cancellable though.
2803 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2804 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2807 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2808 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2811 if (attributes == NULL && namespaces == NULL)
2815 s = g_string_new ("");
2817 /* Always query the source file size, even though we can’t set that on the
2818 * destination. This is useful for the copy functions. */
2820 g_string_append (s, G_FILE_ATTRIBUTE_STANDARD_SIZE);
2824 for (i = 0; i < attributes->n_infos; i++)
2826 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2831 g_string_append_c (s, ',');
2833 g_string_append (s, attributes->infos[i].name);
2840 for (i = 0; i < namespaces->n_infos; i++)
2842 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2847 g_string_append_c (s, ',');
2849 g_string_append (s, namespaces->infos[i].name);
2850 g_string_append (s, "::*");
2855 ret = g_string_free (s, FALSE);
2859 g_string_free (s, TRUE);
2861 g_file_attribute_info_list_unref (attributes);
2863 g_file_attribute_info_list_unref (namespaces);
2869 * g_file_copy_attributes:
2870 * @source: a #GFile with attributes
2871 * @destination: a #GFile to copy attributes to
2872 * @flags: a set of #GFileCopyFlags
2873 * @cancellable: (nullable): optional #GCancellable object,
2875 * @error: a #GError, %NULL to ignore
2877 * Copies the file attributes from @source to @destination.
2879 * Normally only a subset of the file attributes are copied,
2880 * those that are copies in a normal file copy operation
2881 * (which for instance does not include e.g. owner). However
2882 * if %G_FILE_COPY_ALL_METADATA is specified in @flags, then
2883 * all the metadata that is possible to copy is copied. This
2884 * is useful when implementing move by copy + delete source.
2886 * Returns: %TRUE if the attributes were copied successfully,
2890 g_file_copy_attributes (GFile *source,
2892 GFileCopyFlags flags,
2893 GCancellable *cancellable,
2896 char *attrs_to_read;
2899 gboolean source_nofollow_symlinks;
2901 attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
2902 cancellable, error);
2906 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2908 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2909 * we just don't copy it.
2911 info = g_file_query_info (source, attrs_to_read,
2912 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2916 g_free (attrs_to_read);
2921 res = g_file_set_attributes_from_info (destination,
2923 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2926 g_object_unref (info);
2932 /* 256k minus malloc overhead */
2933 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2936 copy_stream_with_progress (GInputStream *in,
2939 GCancellable *cancellable,
2940 GFileProgressCallback progress_callback,
2941 gpointer progress_callback_data,
2946 goffset current_size;
2953 /* avoid performance impact of querying total size when it's not needed */
2954 if (progress_callback)
2956 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2957 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2961 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2962 total_size = g_file_info_get_size (info);
2963 g_object_unref (info);
2966 if (total_size == -1)
2968 info = g_file_query_info (source,
2969 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2970 G_FILE_QUERY_INFO_NONE,
2974 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2975 total_size = g_file_info_get_size (info);
2976 g_object_unref (info);
2981 if (total_size == -1)
2984 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2989 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2999 current_size += n_read;
3001 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
3005 if (progress_callback)
3006 progress_callback (current_size, total_size, progress_callback_data);
3010 /* Make sure we send full copied size */
3011 if (progress_callback)
3012 progress_callback (current_size, total_size, progress_callback_data);
3017 #ifdef HAVE_COPY_FILE_RANGE
3019 do_copy_file_range (int fd_in,
3024 size_t *bytes_transferred,
3031 result = copy_file_range (fd_in, off_in, fd_out, off_out, len, 0);
3041 else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP || errsv == EXDEV)
3043 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3044 _("Copy file range not supported"));
3048 g_set_error (error, G_IO_ERROR,
3049 g_io_error_from_errno (errsv),
3050 _("Error splicing file: %s"),
3051 g_strerror (errsv));
3056 } while (result == -1);
3058 g_assert (result >= 0);
3059 *bytes_transferred = result;
3065 copy_file_range_with_progress (GInputStream *in,
3068 GCancellable *cancellable,
3069 GFileProgressCallback progress_callback,
3070 gpointer progress_callback_data,
3073 goffset total_size, last_notified_size;
3079 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3080 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3082 g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3083 total_size = g_file_info_get_size (in_info);
3085 /* Bail out if the reported size of the file is zero. It might be zero, but it
3086 * might also just be a kernel file in /proc. They report their file size as
3087 * zero, but then have data when you start reading. Go to the fallback code
3088 * path for those. */
3089 if (total_size == 0)
3091 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3092 _("Copy file range not supported"));
3096 offset_in = offset_out = 0;
3097 copy_len = total_size;
3098 last_notified_size = 0;
3100 /* Call copy_file_range() in a loop until the whole contents are copied. For
3101 * smaller files, this loop will iterate only once. For larger files, the
3102 * kernel (at least, kernel 6.1.6) will return after 2GB anyway, so that gives
3103 * us more loop iterations and more progress reporting. */
3104 while (copy_len > 0)
3108 if (g_cancellable_set_error_if_cancelled (cancellable, error) ||
3109 !do_copy_file_range (fd_in, &offset_in, fd_out, &offset_out, copy_len, &n_copied, error))
3115 g_assert (n_copied <= copy_len);
3116 copy_len -= n_copied;
3118 if (progress_callback)
3120 progress_callback (offset_in, total_size, progress_callback_data);
3121 last_notified_size = total_size;
3125 /* Make sure we send full copied size */
3126 if (progress_callback && last_notified_size != total_size)
3127 progress_callback (offset_in, total_size, progress_callback_data);
3131 #endif /* HAVE_COPY_FILE_RANGE */
3136 do_splice (int fd_in,
3141 long *bytes_transferd,
3147 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
3155 else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
3156 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3157 _("Splice not supported"));
3159 g_set_error (error, G_IO_ERROR,
3160 g_io_error_from_errno (errsv),
3161 _("Error splicing file: %s"),
3162 g_strerror (errsv));
3167 *bytes_transferd = result;
3172 splice_stream_with_progress (GInputStream *in,
3175 GCancellable *cancellable,
3176 GFileProgressCallback progress_callback,
3177 gpointer progress_callback_data,
3180 int buffer[2] = { -1, -1 };
3188 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3189 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3191 if (!g_unix_open_pipe (buffer, O_CLOEXEC, error))
3194 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3195 * pipe size. See: https://bugzilla.gnome.org/791457 */
3196 buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3197 if (buffer_size <= 0)
3199 buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3200 if (buffer_size <= 0)
3202 /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3203 * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the
3204 * possibility of running on Linux < 2.6.11 (where the capacity was
3205 * the system page size, typically 4KiB) because it’s ancient.
3207 buffer_size = 1024 * 64;
3211 g_assert (buffer_size > 0);
3214 /* avoid performance impact of querying total size when it's not needed */
3215 if (progress_callback)
3217 g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3218 total_size = g_file_info_get_size (in_info);
3221 if (total_size == -1)
3224 offset_in = offset_out = 0;
3231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3234 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3245 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3248 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3251 n_read -= n_written;
3254 if (progress_callback)
3255 progress_callback (offset_in, total_size, progress_callback_data);
3258 /* Make sure we send full copied size */
3259 if (progress_callback)
3260 progress_callback (offset_in, total_size, progress_callback_data);
3262 if (!g_close (buffer[0], error))
3265 if (!g_close (buffer[1], error))
3269 if (buffer[0] != -1)
3270 (void) g_close (buffer[0], NULL);
3271 if (buffer[1] != -1)
3272 (void) g_close (buffer[1], NULL);
3280 btrfs_reflink_with_progress (GInputStream *in,
3284 GCancellable *cancellable,
3285 GFileProgressCallback progress_callback,
3286 gpointer progress_callback_data,
3293 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3294 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3297 /* avoid performance impact of querying total size when it's not needed */
3298 if (progress_callback)
3300 g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3301 total_size = g_file_info_get_size (in_info);
3304 if (total_size == -1)
3307 /* Btrfs clone ioctl properties:
3308 * - Works at the inode level
3309 * - Doesn't work with directories
3310 * - Always follows symlinks (source and destination)
3312 * By the time we get here, *in and *out are both regular files */
3313 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3319 g_set_error_literal (error, G_IO_ERROR,
3320 G_IO_ERROR_NOT_SUPPORTED,
3321 _("Copy (reflink/clone) between mounts is not supported"));
3322 else if (errsv == EINVAL)
3323 g_set_error_literal (error, G_IO_ERROR,
3324 G_IO_ERROR_NOT_SUPPORTED,
3325 _("Copy (reflink/clone) is not supported or invalid"));
3327 /* Most probably something odd happened; retry with fallback */
3328 g_set_error_literal (error, G_IO_ERROR,
3329 G_IO_ERROR_NOT_SUPPORTED,
3330 _("Copy (reflink/clone) is not supported or didn’t work"));
3331 /* We retry with fallback for all error cases because Btrfs is currently
3332 * unstable, and so we can't trust it to do clone properly.
3333 * In addition, any hard errors here would cause the same failure in the
3334 * fallback manual copy as well. */
3338 /* Make sure we send full copied size */
3339 if (progress_callback)
3340 progress_callback (total_size, total_size, progress_callback_data);
3347 file_copy_fallback (GFile *source,
3349 GFileCopyFlags flags,
3350 GCancellable *cancellable,
3351 GFileProgressCallback progress_callback,
3352 gpointer progress_callback_data,
3355 gboolean ret = FALSE;
3356 GFileInputStream *file_in = NULL;
3357 GInputStream *in = NULL;
3358 GOutputStream *out = NULL;
3359 GFileInfo *info = NULL;
3361 char *attrs_to_read;
3362 gboolean do_set_attributes = FALSE;
3363 GFileCreateFlags create_flags;
3364 GError *tmp_error = NULL;
3366 /* need to know the file type */
3367 info = g_file_query_info (source,
3368 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3369 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3375 /* Maybe copy the symlink? */
3376 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3377 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3379 target = g_file_info_get_symlink_target (info);
3382 if (!copy_symlink (destination, flags, cancellable, target, error))
3388 /* ... else fall back on a regular file copy */
3390 /* Handle "special" files (pipes, device nodes, ...)? */
3391 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3393 /* FIXME: could try to recreate device nodes and others? */
3394 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3395 _("Can’t copy special file"));
3399 /* Everything else should just fall back on a regular copy. */
3401 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3404 in = G_INPUT_STREAM (file_in);
3406 attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
3407 cancellable, error);
3411 /* Ok, ditch the previous lightweight info (on Unix we just
3412 * called lstat()); at this point we gather all the information
3413 * we need about the source from the opened file descriptor.
3415 g_object_unref (info);
3417 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3418 cancellable, &tmp_error);
3421 /* Not all gvfs backends implement query_info_on_read(), we
3422 * can just fall back to the pathname again.
3423 * https://bugzilla.gnome.org/706254
3425 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3427 g_clear_error (&tmp_error);
3428 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3429 cancellable, error);
3433 g_free (attrs_to_read);
3434 g_propagate_error (error, tmp_error);
3438 g_free (attrs_to_read);
3442 do_set_attributes = TRUE;
3444 /* In the local file path, we pass down the source info which
3445 * includes things like unix::mode, to ensure that the target file
3446 * is not created with different permissions from the source file.
3448 * If a future API like g_file_replace_with_info() is added, switch
3449 * this code to use that.
3451 * Use %G_FILE_CREATE_PRIVATE unless
3452 * - we were told to create the file with default permissions (i.e. the
3453 * process’ umask),
3454 * - or if the source file is on a file system which doesn’t support
3455 * `unix::mode` (in which case it probably also makes sense to create the
3456 * destination with default permissions because the source cannot be
3458 * - or if the destination file is a `GLocalFile`, in which case we can
3459 * directly open() it with the permissions from the source file.
3461 create_flags = G_FILE_CREATE_NONE;
3462 if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3463 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3464 !G_IS_LOCAL_FILE (destination))
3465 create_flags |= G_FILE_CREATE_PRIVATE;
3466 if (flags & G_FILE_COPY_OVERWRITE)
3467 create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3469 if (G_IS_LOCAL_FILE (destination))
3471 if (flags & G_FILE_COPY_OVERWRITE)
3472 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3474 flags & G_FILE_COPY_BACKUP,
3476 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3477 cancellable, error);
3479 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3480 FALSE, create_flags,
3481 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3482 cancellable, error);
3484 else if (flags & G_FILE_COPY_OVERWRITE)
3486 out = (GOutputStream *)g_file_replace (destination,
3488 flags & G_FILE_COPY_BACKUP,
3490 cancellable, error);
3494 out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3501 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3503 GError *reflink_err = NULL;
3505 if (!btrfs_reflink_with_progress (in, info, out, info, cancellable,
3506 progress_callback, progress_callback_data,
3509 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3511 g_clear_error (&reflink_err);
3515 g_propagate_error (error, reflink_err);
3527 #ifdef HAVE_COPY_FILE_RANGE
3528 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3530 GError *copy_file_range_error = NULL;
3532 if (copy_file_range_with_progress (in, info, out, cancellable,
3533 progress_callback, progress_callback_data,
3534 ©_file_range_error))
3539 else if (!g_error_matches (copy_file_range_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3541 g_propagate_error (error, g_steal_pointer (©_file_range_error));
3546 g_clear_error (©_file_range_error);
3549 #endif /* HAVE_COPY_FILE_RANGE */
3552 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3554 GError *splice_err = NULL;
3556 if (!splice_stream_with_progress (in, info, out, cancellable,
3557 progress_callback, progress_callback_data,
3560 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3562 g_clear_error (&splice_err);
3566 g_propagate_error (error, splice_err);
3579 /* A plain read/write loop */
3580 if (!copy_stream_with_progress (in, out, source, cancellable,
3581 progress_callback, progress_callback_data,
3589 /* Don't care about errors in source here */
3590 (void) g_input_stream_close (in, cancellable, NULL);
3591 g_object_unref (in);
3596 /* But write errors on close are bad! */
3597 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3599 g_object_unref (out);
3602 /* Ignore errors here. Failure to copy metadata is not a hard error */
3603 /* TODO: set these attributes /before/ we do the rename() on Unix */
3604 if (ret && do_set_attributes)
3606 g_file_set_attributes_from_info (destination,
3608 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3613 g_clear_object (&info);
3620 * @source: input #GFile
3621 * @destination: destination #GFile
3622 * @flags: set of #GFileCopyFlags
3623 * @cancellable: (nullable): optional #GCancellable object,
3625 * @progress_callback: (nullable) (scope call) (closure progress_callback_data): function to callback with
3626 * progress information, or %NULL if progress information is not needed
3627 * @progress_callback_data: user data to pass to @progress_callback
3628 * @error: #GError to set on error, or %NULL
3630 * Copies the file @source to the location specified by @destination.
3631 * Can not handle recursive copies of directories.
3633 * If the flag %G_FILE_COPY_OVERWRITE is specified an already
3634 * existing @destination file is overwritten.
3636 * If the flag %G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3637 * will be copied as symlinks, otherwise the target of the
3638 * @source symlink will be copied.
3640 * If the flag %G_FILE_COPY_ALL_METADATA is specified then all the metadata
3641 * that is possible to copy is copied, not just the default subset (which,
3642 * for instance, does not include the owner, see #GFileInfo).
3644 * If @cancellable is not %NULL, then the operation can be cancelled by
3645 * triggering the cancellable object from another thread. If the operation
3646 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3648 * If @progress_callback is not %NULL, then the operation can be monitored
3649 * by setting this to a #GFileProgressCallback function.
3650 * @progress_callback_data will be passed to this function. It is guaranteed
3651 * that this callback will be called after all data has been transferred with
3652 * the total number of bytes copied during the operation.
3654 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3655 * is returned, independent on the status of the @destination.
3657 * If %G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3658 * the error %G_IO_ERROR_EXISTS is returned.
3660 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3661 * error is returned. If trying to overwrite a directory with a directory the
3662 * %G_IO_ERROR_WOULD_MERGE error is returned.
3664 * If the source is a directory and the target does not exist, or
3665 * %G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3666 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3668 * If you are interested in copying the #GFile object itself (not the on-disk
3669 * file), see g_file_dup().
3671 * Returns: %TRUE on success, %FALSE otherwise.
3674 g_file_copy (GFile *source,
3676 GFileCopyFlags flags,
3677 GCancellable *cancellable,
3678 GFileProgressCallback progress_callback,
3679 gpointer progress_callback_data,
3686 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3687 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3689 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3692 iface = G_FILE_GET_IFACE (destination);
3696 res = (* iface->copy) (source, destination,
3698 progress_callback, progress_callback_data,
3704 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3706 g_propagate_error (error, my_error);
3710 g_clear_error (&my_error);
3713 /* If the types are different, and the destination method failed
3714 * also try the source method
3716 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3718 iface = G_FILE_GET_IFACE (source);
3723 res = (* iface->copy) (source, destination,
3725 progress_callback, progress_callback_data,
3731 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3733 g_propagate_error (error, my_error);
3737 g_clear_error (&my_error);
3741 return file_copy_fallback (source, destination, flags, cancellable,
3742 progress_callback, progress_callback_data,
3747 * g_file_copy_async:
3748 * @source: input #GFile
3749 * @destination: destination #GFile
3750 * @flags: set of #GFileCopyFlags
3751 * @io_priority: the [I/O priority][io-priority] of the request
3752 * @cancellable: (nullable): optional #GCancellable object,
3754 * @progress_callback: (nullable) (scope notified) (closure progress_callback_data):
3755 * function to callback with progress information, or %NULL if
3756 * progress information is not needed
3757 * @progress_callback_data: user data to pass to @progress_callback
3758 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
3759 * to call when the request is satisfied
3760 * @user_data: the data to pass to callback
3762 * Copies the file @source to the location specified by @destination
3763 * asynchronously. For details of the behaviour, see g_file_copy().
3765 * If @progress_callback is not %NULL, then that function that will be called
3766 * just like in g_file_copy(). The callback will run in the default main context
3767 * of the thread calling g_file_copy_async() — the same context as @callback is
3770 * When the operation is finished, @callback will be called. You can then call
3771 * g_file_copy_finish() to get the result of the operation.
3774 g_file_copy_async (GFile *source,
3776 GFileCopyFlags flags,
3778 GCancellable *cancellable,
3779 GFileProgressCallback progress_callback,
3780 gpointer progress_callback_data,
3781 GAsyncReadyCallback callback,
3786 g_return_if_fail (G_IS_FILE (source));
3787 g_return_if_fail (G_IS_FILE (destination));
3789 iface = G_FILE_GET_IFACE (source);
3790 (* iface->copy_async) (source,
3796 progress_callback_data,
3802 * g_file_copy_finish:
3803 * @file: input #GFile
3804 * @res: a #GAsyncResult
3805 * @error: a #GError, or %NULL
3807 * Finishes copying the file started with g_file_copy_async().
3809 * Returns: a %TRUE on success, %FALSE on error.
3812 g_file_copy_finish (GFile *file,
3818 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3819 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3821 if (g_async_result_legacy_propagate_error (res, error))
3824 iface = G_FILE_GET_IFACE (file);
3825 return (* iface->copy_finish) (file, res, error);
3830 * @source: #GFile pointing to the source location
3831 * @destination: #GFile pointing to the destination location
3832 * @flags: set of #GFileCopyFlags
3833 * @cancellable: (nullable): optional #GCancellable object,
3835 * @progress_callback: (nullable) (scope call) (closure progress_callback_data): #GFileProgressCallback
3836 * function for updates
3837 * @progress_callback_data: gpointer to user data for
3838 * the callback function
3839 * @error: #GError for returning error conditions, or %NULL
3841 * Tries to move the file or directory @source to the location specified
3842 * by @destination. If native move operations are supported then this is
3843 * used, otherwise a copy + delete fallback is used. The native
3844 * implementation may support moving directories (for instance on moves
3845 * inside the same filesystem), but the fallback code does not.
3847 * If the flag %G_FILE_COPY_OVERWRITE is specified an already
3848 * existing @destination file is overwritten.
3850 * If @cancellable is not %NULL, then the operation can be cancelled by
3851 * triggering the cancellable object from another thread. If the operation
3852 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3854 * If @progress_callback is not %NULL, then the operation can be monitored
3855 * by setting this to a #GFileProgressCallback function.
3856 * @progress_callback_data will be passed to this function. It is
3857 * guaranteed that this callback will be called after all data has been
3858 * transferred with the total number of bytes copied during the operation.
3860 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3861 * error is returned, independent on the status of the @destination.
3863 * If %G_FILE_COPY_OVERWRITE is not specified and the target exists,
3864 * then the error %G_IO_ERROR_EXISTS is returned.
3866 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3867 * error is returned. If trying to overwrite a directory with a directory the
3868 * %G_IO_ERROR_WOULD_MERGE error is returned.
3870 * If the source is a directory and the target does not exist, or
3871 * %G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3872 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3873 * move operation isn't available).
3875 * Returns: %TRUE on successful move, %FALSE otherwise.
3878 g_file_move (GFile *source,
3880 GFileCopyFlags flags,
3881 GCancellable *cancellable,
3882 GFileProgressCallback progress_callback,
3883 gpointer progress_callback_data,
3890 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3891 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3896 iface = G_FILE_GET_IFACE (destination);
3900 res = (* iface->move) (source, destination,
3902 progress_callback, progress_callback_data,
3908 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3910 g_propagate_error (error, my_error);
3914 g_clear_error (&my_error);
3917 /* If the types are different, and the destination method failed
3918 * also try the source method
3920 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3922 iface = G_FILE_GET_IFACE (source);
3927 res = (* iface->move) (source, destination,
3929 progress_callback, progress_callback_data,
3935 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3937 g_propagate_error (error, my_error);
3941 g_clear_error (&my_error);
3945 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3947 g_set_error_literal (error, G_IO_ERROR,
3948 G_IO_ERROR_NOT_SUPPORTED,
3949 _("Operation not supported"));
3953 flags |= G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS;
3954 if (!g_file_copy (source, destination, flags, cancellable,
3955 progress_callback, progress_callback_data,
3959 return g_file_delete (source, cancellable, error);
3963 * g_file_move_async:
3964 * @source: #GFile pointing to the source location
3965 * @destination: #GFile pointing to the destination location
3966 * @flags: set of #GFileCopyFlags
3967 * @io_priority: the [I/O priority][io-priority] of the request
3968 * @cancellable: (nullable): optional #GCancellable object,
3970 * @progress_callback: (nullable) (scope call) (closure progress_callback_data):
3971 * #GFileProgressCallback function for updates
3972 * @progress_callback_data: gpointer to user data for the callback function
3973 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
3974 * to call when the request is satisfied
3975 * @user_data: the data to pass to callback function
3977 * Asynchronously moves a file @source to the location of @destination. For details of the behaviour, see g_file_move().
3979 * If @progress_callback is not %NULL, then that function that will be called
3980 * just like in g_file_move(). The callback will run in the default main context
3981 * of the thread calling g_file_move_async() — the same context as @callback is
3984 * When the operation is finished, @callback will be called. You can then call
3985 * g_file_move_finish() to get the result of the operation.
3990 g_file_move_async (GFile *source,
3992 GFileCopyFlags flags,
3994 GCancellable *cancellable,
3995 GFileProgressCallback progress_callback,
3996 gpointer progress_callback_data,
3997 GAsyncReadyCallback callback,
4002 g_return_if_fail (G_IS_FILE (source));
4003 g_return_if_fail (G_IS_FILE (destination));
4004 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4006 iface = G_FILE_GET_IFACE (source);
4007 (* iface->move_async) (source,
4013 progress_callback_data,
4019 * g_file_move_finish:
4020 * @file: input source #GFile
4021 * @result: a #GAsyncResult
4022 * @error: a #GError, or %NULL
4024 * Finishes an asynchronous file movement, started with
4025 * g_file_move_async().
4027 * Returns: %TRUE on successful file move, %FALSE otherwise.
4032 g_file_move_finish (GFile *file,
4033 GAsyncResult *result,
4038 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4039 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4040 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4042 iface = G_FILE_GET_IFACE (file);
4043 return (* iface->move_finish) (file, result, error);
4047 * g_file_make_directory:
4048 * @file: input #GFile
4049 * @cancellable: (nullable): optional #GCancellable object,
4051 * @error: a #GError, or %NULL
4053 * Creates a directory. Note that this will only create a child directory
4054 * of the immediate parent directory of the path or URI given by the #GFile.
4055 * To recursively create directories, see g_file_make_directory_with_parents().
4056 * This function will fail if the parent directory does not exist, setting
4057 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
4058 * creating directories, this function will fail, setting @error to
4059 * %G_IO_ERROR_NOT_SUPPORTED.
4061 * For a local #GFile the newly created directory will have the default
4062 * (current) ownership and permissions of the current process.
4064 * If @cancellable is not %NULL, then the operation can be cancelled by
4065 * triggering the cancellable object from another thread. If the operation
4066 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4068 * Returns: %TRUE on successful creation, %FALSE otherwise.
4071 g_file_make_directory (GFile *file,
4072 GCancellable *cancellable,
4077 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4079 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4082 iface = G_FILE_GET_IFACE (file);
4084 if (iface->make_directory == NULL)
4086 g_set_error_literal (error, G_IO_ERROR,
4087 G_IO_ERROR_NOT_SUPPORTED,
4088 _("Operation not supported"));
4092 return (* iface->make_directory) (file, cancellable, error);
4096 * g_file_make_directory_async:
4097 * @file: input #GFile
4098 * @io_priority: the [I/O priority][io-priority] of the request
4099 * @cancellable: (nullable): optional #GCancellable object,
4101 * @callback: a #GAsyncReadyCallback to call
4102 * when the request is satisfied
4103 * @user_data: the data to pass to callback function
4105 * Asynchronously creates a directory.
4107 * Virtual: make_directory_async
4111 g_file_make_directory_async (GFile *file,
4113 GCancellable *cancellable,
4114 GAsyncReadyCallback callback,
4119 g_return_if_fail (G_IS_FILE (file));
4121 iface = G_FILE_GET_IFACE (file);
4122 (* iface->make_directory_async) (file,
4130 * g_file_make_directory_finish:
4131 * @file: input #GFile
4132 * @result: a #GAsyncResult
4133 * @error: a #GError, or %NULL
4135 * Finishes an asynchronous directory creation, started with
4136 * g_file_make_directory_async().
4138 * Virtual: make_directory_finish
4139 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
4143 g_file_make_directory_finish (GFile *file,
4144 GAsyncResult *result,
4149 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4150 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4152 iface = G_FILE_GET_IFACE (file);
4153 return (* iface->make_directory_finish) (file, result, error);
4157 * g_file_make_directory_with_parents:
4158 * @file: input #GFile
4159 * @cancellable: (nullable): optional #GCancellable object,
4161 * @error: a #GError, or %NULL
4163 * Creates a directory and any parent directories that may not
4164 * exist similar to 'mkdir -p'. If the file system does not support
4165 * creating directories, this function will fail, setting @error to
4166 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
4167 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
4168 * the similar g_mkdir_with_parents().
4170 * For a local #GFile the newly created directories will have the default
4171 * (current) ownership and permissions of the current process.
4173 * If @cancellable is not %NULL, then the operation can be cancelled by
4174 * triggering the cancellable object from another thread. If the operation
4175 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4177 * Returns: %TRUE if all directories have been successfully created, %FALSE
4183 g_file_make_directory_with_parents (GFile *file,
4184 GCancellable *cancellable,
4187 GFile *work_file = NULL;
4188 GList *list = NULL, *l;
4189 GError *my_error = NULL;
4191 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4193 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4196 /* Try for the simple case of not having to create any parent
4197 * directories. If any parent directory needs to be created, this
4198 * call will fail with NOT_FOUND. If that happens, then that value of
4199 * my_error persists into the while loop below.
4201 g_file_make_directory (file, cancellable, &my_error);
4202 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4205 g_propagate_error (error, my_error);
4206 return my_error == NULL;
4209 work_file = g_object_ref (file);
4211 /* Creates the parent directories as needed. In case any particular
4212 * creation operation fails for lack of other parent directories
4213 * (NOT_FOUND), the directory is added to a list of directories to
4214 * create later, and the value of my_error is retained until the next
4215 * iteration of the loop. After the loop my_error should either be
4216 * empty or contain a real failure condition.
4218 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4222 parent_file = g_file_get_parent (work_file);
4223 if (parent_file == NULL)
4226 g_clear_error (&my_error);
4227 g_file_make_directory (parent_file, cancellable, &my_error);
4228 /* Another process may have created the directory in between the
4229 * G_IO_ERROR_NOT_FOUND and now
4231 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4232 g_clear_error (&my_error);
4234 g_object_unref (work_file);
4235 work_file = g_object_ref (parent_file);
4237 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4238 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
4240 g_object_unref (parent_file);
4243 /* All directories should be able to be created now, so an error at
4244 * this point means the whole operation must fail -- except an EXISTS
4245 * error, which means that another process already created the
4246 * directory in between the previous failure and now.
4248 for (l = list; my_error == NULL && l; l = l->next)
4250 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
4251 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4252 g_clear_error (&my_error);
4256 g_object_unref (work_file);
4259 while (list != NULL)
4261 g_object_unref ((GFile *) list->data);
4262 list = g_list_remove (list, list->data);
4265 /* At this point an error in my_error means a that something
4266 * unexpected failed in either of the loops above, so the whole
4267 * operation must fail.
4269 if (my_error != NULL)
4271 g_propagate_error (error, my_error);
4275 return g_file_make_directory (file, cancellable, error);
4279 * g_file_make_symbolic_link:
4280 * @file: a #GFile with the name of the symlink to create
4281 * @symlink_value: (type filename): a string with the path for the target
4282 * of the new symlink
4283 * @cancellable: (nullable): optional #GCancellable object,
4287 * Creates a symbolic link named @file which contains the string
4290 * If @cancellable is not %NULL, then the operation can be cancelled by
4291 * triggering the cancellable object from another thread. If the operation
4292 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4294 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
4297 g_file_make_symbolic_link (GFile *file,
4298 const char *symlink_value,
4299 GCancellable *cancellable,
4304 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4305 g_return_val_if_fail (symlink_value != NULL, FALSE);
4307 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4310 if (*symlink_value == '\0')
4312 g_set_error_literal (error, G_IO_ERROR,
4313 G_IO_ERROR_INVALID_ARGUMENT,
4314 _("Invalid symlink value given"));
4318 iface = G_FILE_GET_IFACE (file);
4320 if (iface->make_symbolic_link == NULL)
4322 g_set_error_literal (error, G_IO_ERROR,
4323 G_IO_ERROR_NOT_SUPPORTED,
4324 _("Symbolic links not supported"));
4328 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4332 make_symbolic_link_async_thread (GTask *task,
4335 GCancellable *cancellable)
4337 const char *symlink_value = task_data;
4338 GError *error = NULL;
4340 if (g_file_make_symbolic_link (G_FILE (object), symlink_value, cancellable, &error))
4341 g_task_return_boolean (task, TRUE);
4343 g_task_return_error (task, g_steal_pointer (&error));
4347 g_file_real_make_symbolic_link_async (GFile *file,
4348 const char *symlink_value,
4350 GCancellable *cancellable,
4351 GAsyncReadyCallback callback,
4356 g_return_if_fail (G_IS_FILE (file));
4357 g_return_if_fail (symlink_value != NULL);
4358 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4360 task = g_task_new (file, cancellable, callback, user_data);
4361 g_task_set_source_tag (task, g_file_real_make_symbolic_link_async);
4362 g_task_set_task_data (task, g_strdup (symlink_value), g_free);
4363 g_task_set_priority (task, io_priority);
4365 g_task_run_in_thread (task, make_symbolic_link_async_thread);
4366 g_object_unref (task);
4370 * g_file_make_symbolic_link_async:
4371 * @file: a #GFile with the name of the symlink to create
4372 * @symlink_value: (type filename): a string with the path for the target
4373 * of the new symlink
4374 * @io_priority: the [I/O priority][io-priority] of the request
4375 * @cancellable: (nullable): optional #GCancellable object,
4377 * @callback: a #GAsyncReadyCallback to call
4378 * when the request is satisfied
4379 * @user_data: the data to pass to callback function
4381 * Asynchronously creates a symbolic link named @file which contains the
4382 * string @symlink_value.
4384 * Virtual: make_symbolic_link_async
4388 g_file_make_symbolic_link_async (GFile *file,
4389 const char *symlink_value,
4391 GCancellable *cancellable,
4392 GAsyncReadyCallback callback,
4397 g_return_if_fail (G_IS_FILE (file));
4398 g_return_if_fail (symlink_value != NULL);
4399 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4401 iface = G_FILE_GET_IFACE (file);
4403 /* Default implementation should always be provided by GFileIface */
4404 g_assert (iface->make_symbolic_link_async != NULL);
4406 (* iface->make_symbolic_link_async) (file, symlink_value, io_priority,
4407 cancellable, callback, user_data);
4411 g_file_real_make_symbolic_link_finish (GFile *file,
4412 GAsyncResult *result,
4415 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
4417 return g_task_propagate_boolean (G_TASK (result), error);
4421 * g_file_make_symbolic_link_finish:
4422 * @file: input #GFile
4423 * @result: a #GAsyncResult
4424 * @error: a #GError, or %NULL
4426 * Finishes an asynchronous symbolic link creation, started with
4427 * g_file_make_symbolic_link_async().
4429 * Virtual: make_symbolic_link_finish
4430 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
4434 g_file_make_symbolic_link_finish (GFile *file,
4435 GAsyncResult *result,
4440 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4441 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4443 iface = G_FILE_GET_IFACE (file);
4444 /* Default implementation should always be provided by GFileIface */
4445 g_assert (iface->make_symbolic_link_finish != NULL);
4447 return (* iface->make_symbolic_link_finish) (file, result, error);
4452 * @file: input #GFile
4453 * @cancellable: (nullable): optional #GCancellable object,
4455 * @error: a #GError, or %NULL
4457 * Deletes a file. If the @file is a directory, it will only be
4458 * deleted if it is empty. This has the same semantics as g_unlink().
4460 * If @file doesn’t exist, %G_IO_ERROR_NOT_FOUND will be returned. This allows
4461 * for deletion to be implemented avoiding
4462 * [time-of-check to time-of-use races](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use):
4464 * g_autoptr(GError) local_error = NULL;
4465 * if (!g_file_delete (my_file, my_cancellable, &local_error) &&
4466 * !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4468 * // deletion failed for some reason other than the file not existing:
4469 * // so report the error
4470 * g_warning ("Failed to delete %s: %s",
4471 * g_file_peek_path (my_file), local_error->message);
4475 * If @cancellable is not %NULL, then the operation can be cancelled by
4476 * triggering the cancellable object from another thread. If the operation
4477 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4479 * Virtual: delete_file
4480 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4483 g_file_delete (GFile *file,
4484 GCancellable *cancellable,
4489 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4491 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4494 iface = G_FILE_GET_IFACE (file);
4496 if (iface->delete_file == NULL)
4498 g_set_error_literal (error, G_IO_ERROR,
4499 G_IO_ERROR_NOT_SUPPORTED,
4500 _("Operation not supported"));
4504 return (* iface->delete_file) (file, cancellable, error);
4508 * g_file_delete_async:
4509 * @file: input #GFile
4510 * @io_priority: the [I/O priority][io-priority] of the request
4511 * @cancellable: (nullable): optional #GCancellable object,
4513 * @callback: a #GAsyncReadyCallback to call
4514 * when the request is satisfied
4515 * @user_data: the data to pass to callback function
4517 * Asynchronously delete a file. If the @file is a directory, it will
4518 * only be deleted if it is empty. This has the same semantics as
4521 * Virtual: delete_file_async
4525 g_file_delete_async (GFile *file,
4527 GCancellable *cancellable,
4528 GAsyncReadyCallback callback,
4533 g_return_if_fail (G_IS_FILE (file));
4535 iface = G_FILE_GET_IFACE (file);
4536 (* iface->delete_file_async) (file,
4544 * g_file_delete_finish:
4545 * @file: input #GFile
4546 * @result: a #GAsyncResult
4547 * @error: a #GError, or %NULL
4549 * Finishes deleting a file started with g_file_delete_async().
4551 * Virtual: delete_file_finish
4552 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4556 g_file_delete_finish (GFile *file,
4557 GAsyncResult *result,
4562 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4563 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4565 if (g_async_result_legacy_propagate_error (result, error))
4568 iface = G_FILE_GET_IFACE (file);
4569 return (* iface->delete_file_finish) (file, result, error);
4574 * @file: #GFile to send to trash
4575 * @cancellable: (nullable): optional #GCancellable object,
4577 * @error: a #GError, or %NULL
4579 * Sends @file to the "Trashcan", if possible. This is similar to
4580 * deleting it, but the user can recover it before emptying the trashcan.
4581 * Not all file systems support trashing, so this call can return the
4582 * %G_IO_ERROR_NOT_SUPPORTED error. Since GLib 2.66, the `x-gvfs-notrash` unix
4583 * mount option can be used to disable g_file_trash() support for certain
4584 * mounts, the %G_IO_ERROR_NOT_SUPPORTED error will be returned in that case.
4586 * If @cancellable is not %NULL, then the operation can be cancelled by
4587 * triggering the cancellable object from another thread. If the operation
4588 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4591 * Returns: %TRUE on successful trash, %FALSE otherwise.
4594 g_file_trash (GFile *file,
4595 GCancellable *cancellable,
4600 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4602 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4605 iface = G_FILE_GET_IFACE (file);
4607 if (iface->trash == NULL)
4609 g_set_error_literal (error,
4610 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4611 _("Trash not supported"));
4615 return (* iface->trash) (file, cancellable, error);
4619 * g_file_trash_async:
4620 * @file: input #GFile
4621 * @io_priority: the [I/O priority][io-priority] of the request
4622 * @cancellable: (nullable): optional #GCancellable object,
4624 * @callback: a #GAsyncReadyCallback to call
4625 * when the request is satisfied
4626 * @user_data: the data to pass to callback function
4628 * Asynchronously sends @file to the Trash location, if possible.
4630 * Virtual: trash_async
4634 g_file_trash_async (GFile *file,
4636 GCancellable *cancellable,
4637 GAsyncReadyCallback callback,
4642 g_return_if_fail (G_IS_FILE (file));
4644 iface = G_FILE_GET_IFACE (file);
4645 (* iface->trash_async) (file,
4653 * g_file_trash_finish:
4654 * @file: input #GFile
4655 * @result: a #GAsyncResult
4656 * @error: a #GError, or %NULL
4658 * Finishes an asynchronous file trashing operation, started with
4659 * g_file_trash_async().
4661 * Virtual: trash_finish
4662 * Returns: %TRUE on successful trash, %FALSE otherwise.
4666 g_file_trash_finish (GFile *file,
4667 GAsyncResult *result,
4672 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4673 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4675 iface = G_FILE_GET_IFACE (file);
4676 return (* iface->trash_finish) (file, result, error);
4680 * g_file_set_display_name:
4681 * @file: input #GFile
4682 * @display_name: a string
4683 * @cancellable: (nullable): optional #GCancellable object,
4685 * @error: a #GError, or %NULL
4687 * Renames @file to the specified display name.
4689 * The display name is converted from UTF-8 to the correct encoding
4690 * for the target filesystem if possible and the @file is renamed to this.
4692 * If you want to implement a rename operation in the user interface the
4693 * edit name (%G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4694 * initial value in the rename widget, and then the result after editing
4695 * should be passed to g_file_set_display_name().
4697 * On success the resulting converted filename is returned.
4699 * If @cancellable is not %NULL, then the operation can be cancelled by
4700 * triggering the cancellable object from another thread. If the operation
4701 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4703 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4704 * or %NULL if there was an error.
4705 * Free the returned object with g_object_unref().
4708 g_file_set_display_name (GFile *file,
4709 const gchar *display_name,
4710 GCancellable *cancellable,
4715 g_return_val_if_fail (G_IS_FILE (file), NULL);
4716 g_return_val_if_fail (display_name != NULL, NULL);
4718 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4722 G_IO_ERROR_INVALID_ARGUMENT,
4723 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4727 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4730 iface = G_FILE_GET_IFACE (file);
4732 return (* iface->set_display_name) (file, display_name, cancellable, error);
4736 * g_file_set_display_name_async:
4737 * @file: input #GFile
4738 * @display_name: a string
4739 * @io_priority: the [I/O priority][io-priority] of the request
4740 * @cancellable: (nullable): optional #GCancellable object,
4742 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
4743 * to call when the request is satisfied
4744 * @user_data: the data to pass to callback function
4746 * Asynchronously sets the display name for a given #GFile.
4748 * For more details, see g_file_set_display_name() which is
4749 * the synchronous version of this call.
4751 * When the operation is finished, @callback will be called.
4752 * You can then call g_file_set_display_name_finish() to get
4753 * the result of the operation.
4756 g_file_set_display_name_async (GFile *file,
4757 const gchar *display_name,
4759 GCancellable *cancellable,
4760 GAsyncReadyCallback callback,
4765 g_return_if_fail (G_IS_FILE (file));
4766 g_return_if_fail (display_name != NULL);
4768 iface = G_FILE_GET_IFACE (file);
4769 (* iface->set_display_name_async) (file,
4778 * g_file_set_display_name_finish:
4779 * @file: input #GFile
4780 * @res: a #GAsyncResult
4781 * @error: a #GError, or %NULL
4783 * Finishes setting a display name started with
4784 * g_file_set_display_name_async().
4786 * Returns: (transfer full): a #GFile or %NULL on error.
4787 * Free the returned object with g_object_unref().
4790 g_file_set_display_name_finish (GFile *file,
4796 g_return_val_if_fail (G_IS_FILE (file), NULL);
4797 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4799 if (g_async_result_legacy_propagate_error (res, error))
4802 iface = G_FILE_GET_IFACE (file);
4803 return (* iface->set_display_name_finish) (file, res, error);
4807 * g_file_query_settable_attributes:
4808 * @file: input #GFile
4809 * @cancellable: (nullable): optional #GCancellable object,
4811 * @error: a #GError, or %NULL
4813 * Obtain the list of settable attributes for the file.
4815 * Returns the type and full attribute name of all the attributes
4816 * that can be set on this file. This doesn't mean setting it will
4817 * always succeed though, you might get an access failure, or some
4818 * specific file may not support a specific attribute.
4820 * If @cancellable is not %NULL, then the operation can be cancelled by
4821 * triggering the cancellable object from another thread. If the operation
4822 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4824 * Returns: (transfer full): a #GFileAttributeInfoList describing the settable attributes.
4825 * When you are done with it, release it with
4826 * g_file_attribute_info_list_unref()
4828 GFileAttributeInfoList *
4829 g_file_query_settable_attributes (GFile *file,
4830 GCancellable *cancellable,
4835 GFileAttributeInfoList *list;
4837 g_return_val_if_fail (G_IS_FILE (file), NULL);
4839 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4842 iface = G_FILE_GET_IFACE (file);
4844 if (iface->query_settable_attributes == NULL)
4845 return g_file_attribute_info_list_new ();
4848 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4852 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4854 list = g_file_attribute_info_list_new ();
4855 g_error_free (my_error);
4858 g_propagate_error (error, my_error);
4865 * g_file_query_writable_namespaces:
4866 * @file: input #GFile
4867 * @cancellable: (nullable): optional #GCancellable object,
4869 * @error: a #GError, or %NULL
4871 * Obtain the list of attribute namespaces where new attributes
4872 * can be created by a user. An example of this is extended
4873 * attributes (in the "xattr" namespace).
4875 * If @cancellable is not %NULL, then the operation can be cancelled by
4876 * triggering the cancellable object from another thread. If the operation
4877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4879 * Returns: (transfer full): a #GFileAttributeInfoList describing the writable namespaces.
4880 * When you are done with it, release it with
4881 * g_file_attribute_info_list_unref()
4883 GFileAttributeInfoList *
4884 g_file_query_writable_namespaces (GFile *file,
4885 GCancellable *cancellable,
4890 GFileAttributeInfoList *list;
4892 g_return_val_if_fail (G_IS_FILE (file), NULL);
4894 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4897 iface = G_FILE_GET_IFACE (file);
4899 if (iface->query_writable_namespaces == NULL)
4900 return g_file_attribute_info_list_new ();
4903 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4907 g_warn_if_reached();
4908 list = g_file_attribute_info_list_new ();
4911 if (my_error != NULL)
4913 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4915 g_error_free (my_error);
4918 g_propagate_error (error, my_error);
4925 * g_file_set_attribute:
4926 * @file: input #GFile
4927 * @attribute: a string containing the attribute's name
4928 * @type: The type of the attribute
4929 * @value_p: (nullable): a pointer to the value (or the pointer
4930 * itself if the type is a pointer type)
4931 * @flags: a set of #GFileQueryInfoFlags
4932 * @cancellable: (nullable): optional #GCancellable object,
4934 * @error: a #GError, or %NULL
4936 * Sets an attribute in the file with attribute name @attribute to @value_p.
4938 * Some attributes can be unset by setting @type to
4939 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4941 * If @cancellable is not %NULL, then the operation can be cancelled by
4942 * triggering the cancellable object from another thread. If the operation
4943 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4945 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4948 g_file_set_attribute (GFile *file,
4949 const gchar *attribute,
4950 GFileAttributeType type,
4952 GFileQueryInfoFlags flags,
4953 GCancellable *cancellable,
4958 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4959 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4961 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4964 iface = G_FILE_GET_IFACE (file);
4966 if (iface->set_attribute == NULL)
4968 g_set_error_literal (error, G_IO_ERROR,
4969 G_IO_ERROR_NOT_SUPPORTED,
4970 _("Operation not supported"));
4974 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4978 * g_file_set_attributes_from_info:
4979 * @file: input #GFile
4980 * @info: a #GFileInfo
4981 * @flags: #GFileQueryInfoFlags
4982 * @cancellable: (nullable): optional #GCancellable object,
4984 * @error: a #GError, or %NULL
4986 * Tries to set all attributes in the #GFileInfo on the target
4987 * values, not stopping on the first error.
4989 * If there is any error during this operation then @error will
4990 * be set to the first error. Error on particular fields are flagged
4991 * by setting the "status" field in the attribute value to
4992 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4993 * also detect further errors.
4995 * If @cancellable is not %NULL, then the operation can be cancelled by
4996 * triggering the cancellable object from another thread. If the operation
4997 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4999 * Returns: %FALSE if there was any error, %TRUE otherwise.
5002 g_file_set_attributes_from_info (GFile *file,
5004 GFileQueryInfoFlags flags,
5005 GCancellable *cancellable,
5010 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5011 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
5013 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5016 g_file_info_clear_status (info);
5018 iface = G_FILE_GET_IFACE (file);
5020 return (* iface->set_attributes_from_info) (file,
5028 g_file_real_set_attributes_from_info (GFile *file,
5030 GFileQueryInfoFlags flags,
5031 GCancellable *cancellable,
5037 GFileAttributeValue *value;
5041 attributes = g_file_info_list_attributes (info, NULL);
5043 for (i = 0; attributes[i] != NULL; i++)
5045 value = _g_file_info_get_attribute_value (info, attributes[i]);
5047 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
5050 if (!g_file_set_attribute (file, attributes[i],
5051 value->type, _g_file_attribute_value_peek_as_pointer (value),
5052 flags, cancellable, error))
5054 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
5056 /* Don't set error multiple times */
5060 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
5063 g_strfreev (attributes);
5069 * g_file_set_attributes_async:
5070 * @file: input #GFile
5071 * @info: a #GFileInfo
5072 * @flags: a #GFileQueryInfoFlags
5073 * @io_priority: the [I/O priority][io-priority] of the request
5074 * @cancellable: (nullable): optional #GCancellable object,
5076 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
5077 * to call when the request is satisfied
5078 * @user_data: the data to pass to callback function
5080 * Asynchronously sets the attributes of @file with @info.
5082 * For more details, see g_file_set_attributes_from_info(),
5083 * which is the synchronous version of this call.
5085 * When the operation is finished, @callback will be called.
5086 * You can then call g_file_set_attributes_finish() to get
5087 * the result of the operation.
5090 g_file_set_attributes_async (GFile *file,
5092 GFileQueryInfoFlags flags,
5094 GCancellable *cancellable,
5095 GAsyncReadyCallback callback,
5100 g_return_if_fail (G_IS_FILE (file));
5101 g_return_if_fail (G_IS_FILE_INFO (info));
5103 iface = G_FILE_GET_IFACE (file);
5104 (* iface->set_attributes_async) (file,
5114 * g_file_set_attributes_finish:
5115 * @file: input #GFile
5116 * @result: a #GAsyncResult
5117 * @info: (out) (transfer full): a #GFileInfo
5118 * @error: a #GError, or %NULL
5120 * Finishes setting an attribute started in g_file_set_attributes_async().
5122 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
5125 g_file_set_attributes_finish (GFile *file,
5126 GAsyncResult *result,
5132 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5133 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5135 /* No standard handling of errors here, as we must set info even
5138 iface = G_FILE_GET_IFACE (file);
5139 return (* iface->set_attributes_finish) (file, result, info, error);
5143 * g_file_set_attribute_string:
5144 * @file: input #GFile
5145 * @attribute: a string containing the attribute's name
5146 * @value: a string containing the attribute's value
5147 * @flags: #GFileQueryInfoFlags
5148 * @cancellable: (nullable): optional #GCancellable object,
5150 * @error: a #GError, or %NULL
5152 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
5153 * If @attribute is of a different type, this operation will fail.
5155 * If @cancellable is not %NULL, then the operation can be cancelled by
5156 * triggering the cancellable object from another thread. If the operation
5157 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5159 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
5162 g_file_set_attribute_string (GFile *file,
5163 const char *attribute,
5165 GFileQueryInfoFlags flags,
5166 GCancellable *cancellable,
5169 return g_file_set_attribute (file, attribute,
5170 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
5171 flags, cancellable, error);
5175 * g_file_set_attribute_byte_string:
5176 * @file: input #GFile
5177 * @attribute: a string containing the attribute's name
5178 * @value: a string containing the attribute's new value
5179 * @flags: a #GFileQueryInfoFlags
5180 * @cancellable: (nullable): optional #GCancellable object,
5182 * @error: a #GError, or %NULL
5184 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
5185 * If @attribute is of a different type, this operation will fail,
5188 * If @cancellable is not %NULL, then the operation can be cancelled by
5189 * triggering the cancellable object from another thread. If the operation
5190 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5192 * Returns: %TRUE if the @attribute was successfully set to @value
5193 * in the @file, %FALSE otherwise.
5196 g_file_set_attribute_byte_string (GFile *file,
5197 const gchar *attribute,
5199 GFileQueryInfoFlags flags,
5200 GCancellable *cancellable,
5203 return g_file_set_attribute (file, attribute,
5204 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
5205 flags, cancellable, error);
5209 * g_file_set_attribute_uint32:
5210 * @file: input #GFile
5211 * @attribute: a string containing the attribute's name
5212 * @value: a #guint32 containing the attribute's new value
5213 * @flags: a #GFileQueryInfoFlags
5214 * @cancellable: (nullable): optional #GCancellable object,
5216 * @error: a #GError, or %NULL
5218 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
5219 * If @attribute is of a different type, this operation will fail.
5221 * If @cancellable is not %NULL, then the operation can be cancelled by
5222 * triggering the cancellable object from another thread. If the operation
5223 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5225 * Returns: %TRUE if the @attribute was successfully set to @value
5226 * in the @file, %FALSE otherwise.
5229 g_file_set_attribute_uint32 (GFile *file,
5230 const gchar *attribute,
5232 GFileQueryInfoFlags flags,
5233 GCancellable *cancellable,
5236 return g_file_set_attribute (file, attribute,
5237 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
5238 flags, cancellable, error);
5242 * g_file_set_attribute_int32:
5243 * @file: input #GFile
5244 * @attribute: a string containing the attribute's name
5245 * @value: a #gint32 containing the attribute's new value
5246 * @flags: a #GFileQueryInfoFlags
5247 * @cancellable: (nullable): optional #GCancellable object,
5249 * @error: a #GError, or %NULL
5251 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
5252 * If @attribute is of a different type, this operation will fail.
5254 * If @cancellable is not %NULL, then the operation can be cancelled by
5255 * triggering the cancellable object from another thread. If the operation
5256 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5258 * Returns: %TRUE if the @attribute was successfully set to @value
5259 * in the @file, %FALSE otherwise.
5262 g_file_set_attribute_int32 (GFile *file,
5263 const gchar *attribute,
5265 GFileQueryInfoFlags flags,
5266 GCancellable *cancellable,
5269 return g_file_set_attribute (file, attribute,
5270 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
5271 flags, cancellable, error);
5275 * g_file_set_attribute_uint64:
5276 * @file: input #GFile
5277 * @attribute: a string containing the attribute's name
5278 * @value: a #guint64 containing the attribute's new value
5279 * @flags: a #GFileQueryInfoFlags
5280 * @cancellable: (nullable): optional #GCancellable object,
5282 * @error: a #GError, or %NULL
5284 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
5285 * If @attribute is of a different type, this operation will fail.
5287 * If @cancellable is not %NULL, then the operation can be cancelled by
5288 * triggering the cancellable object from another thread. If the operation
5289 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5291 * Returns: %TRUE if the @attribute was successfully set to @value
5292 * in the @file, %FALSE otherwise.
5295 g_file_set_attribute_uint64 (GFile *file,
5296 const gchar *attribute,
5298 GFileQueryInfoFlags flags,
5299 GCancellable *cancellable,
5302 return g_file_set_attribute (file, attribute,
5303 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
5304 flags, cancellable, error);
5308 * g_file_set_attribute_int64:
5309 * @file: input #GFile
5310 * @attribute: a string containing the attribute's name
5311 * @value: a #guint64 containing the attribute's new value
5312 * @flags: a #GFileQueryInfoFlags
5313 * @cancellable: (nullable): optional #GCancellable object,
5315 * @error: a #GError, or %NULL
5317 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
5318 * If @attribute is of a different type, this operation will fail.
5320 * If @cancellable is not %NULL, then the operation can be cancelled by
5321 * triggering the cancellable object from another thread. If the operation
5322 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5324 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
5327 g_file_set_attribute_int64 (GFile *file,
5328 const gchar *attribute,
5330 GFileQueryInfoFlags flags,
5331 GCancellable *cancellable,
5334 return g_file_set_attribute (file, attribute,
5335 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
5336 flags, cancellable, error);
5340 * g_file_mount_mountable:
5341 * @file: input #GFile
5342 * @flags: flags affecting the operation
5343 * @mount_operation: (nullable): a #GMountOperation,
5344 * or %NULL to avoid user interaction
5345 * @cancellable: (nullable): optional #GCancellable object,
5347 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
5348 * to call when the request is satisfied
5349 * @user_data: the data to pass to callback function
5351 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
5352 * Using @mount_operation, you can request callbacks when, for instance,
5353 * passwords are needed during authentication.
5355 * If @cancellable is not %NULL, then the operation can be cancelled by
5356 * triggering the cancellable object from another thread. If the operation
5357 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5359 * When the operation is finished, @callback will be called.
5360 * You can then call g_file_mount_mountable_finish() to get
5361 * the result of the operation.
5364 g_file_mount_mountable (GFile *file,
5365 GMountMountFlags flags,
5366 GMountOperation *mount_operation,
5367 GCancellable *cancellable,
5368 GAsyncReadyCallback callback,
5373 g_return_if_fail (G_IS_FILE (file));
5375 iface = G_FILE_GET_IFACE (file);
5377 if (iface->mount_mountable == NULL)
5379 g_task_report_new_error (file, callback, user_data,
5380 g_file_mount_mountable,
5381 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5382 _("Operation not supported"));
5386 (* iface->mount_mountable) (file,
5395 * g_file_mount_mountable_finish:
5396 * @file: input #GFile
5397 * @result: a #GAsyncResult
5398 * @error: a #GError, or %NULL
5400 * Finishes a mount operation. See g_file_mount_mountable() for details.
5402 * Finish an asynchronous mount operation that was started
5403 * with g_file_mount_mountable().
5405 * Returns: (transfer full): a #GFile or %NULL on error.
5406 * Free the returned object with g_object_unref().
5409 g_file_mount_mountable_finish (GFile *file,
5410 GAsyncResult *result,
5415 g_return_val_if_fail (G_IS_FILE (file), NULL);
5416 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
5418 if (g_async_result_legacy_propagate_error (result, error))
5420 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
5421 return g_task_propagate_pointer (G_TASK (result), error);
5423 iface = G_FILE_GET_IFACE (file);
5424 return (* iface->mount_mountable_finish) (file, result, error);
5428 * g_file_unmount_mountable:
5429 * @file: input #GFile
5430 * @flags: flags affecting the operation
5431 * @cancellable: (nullable): optional #GCancellable object,
5433 * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5434 * to call when the request is satisfied
5435 * @user_data: the data to pass to callback function
5437 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5439 * If @cancellable is not %NULL, then the operation can be cancelled by
5440 * triggering the cancellable object from another thread. If the operation
5441 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5443 * When the operation is finished, @callback will be called.
5444 * You can then call g_file_unmount_mountable_finish() to get
5445 * the result of the operation.
5447 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5450 g_file_unmount_mountable (GFile *file,
5451 GMountUnmountFlags flags,
5452 GCancellable *cancellable,
5453 GAsyncReadyCallback callback,
5458 g_return_if_fail (G_IS_FILE (file));
5460 iface = G_FILE_GET_IFACE (file);
5462 if (iface->unmount_mountable == NULL)
5464 g_task_report_new_error (file, callback, user_data,
5465 g_file_unmount_mountable_with_operation,
5466 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5467 _("Operation not supported"));
5471 (* iface->unmount_mountable) (file,
5479 * g_file_unmount_mountable_finish:
5480 * @file: input #GFile
5481 * @result: a #GAsyncResult
5482 * @error: a #GError, or %NULL
5484 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5486 * Finish an asynchronous unmount operation that was started
5487 * with g_file_unmount_mountable().
5489 * Returns: %TRUE if the operation finished successfully.
5492 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5496 g_file_unmount_mountable_finish (GFile *file,
5497 GAsyncResult *result,
5502 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5503 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5505 if (g_async_result_legacy_propagate_error (result, error))
5507 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5508 return g_task_propagate_boolean (G_TASK (result), error);
5510 iface = G_FILE_GET_IFACE (file);
5511 return (* iface->unmount_mountable_finish) (file, result, error);
5515 * g_file_unmount_mountable_with_operation:
5516 * @file: input #GFile
5517 * @flags: flags affecting the operation
5518 * @mount_operation: (nullable): a #GMountOperation,
5519 * or %NULL to avoid user interaction
5520 * @cancellable: (nullable): optional #GCancellable object,
5522 * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5523 * to call when the request is satisfied
5524 * @user_data: the data to pass to callback function
5526 * Unmounts a file of type %G_FILE_TYPE_MOUNTABLE.
5528 * If @cancellable is not %NULL, then the operation can be cancelled by
5529 * triggering the cancellable object from another thread. If the operation
5530 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5532 * When the operation is finished, @callback will be called.
5533 * You can then call g_file_unmount_mountable_finish() to get
5534 * the result of the operation.
5539 g_file_unmount_mountable_with_operation (GFile *file,
5540 GMountUnmountFlags flags,
5541 GMountOperation *mount_operation,
5542 GCancellable *cancellable,
5543 GAsyncReadyCallback callback,
5548 g_return_if_fail (G_IS_FILE (file));
5550 iface = G_FILE_GET_IFACE (file);
5552 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5554 g_task_report_new_error (file, callback, user_data,
5555 g_file_unmount_mountable_with_operation,
5556 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5557 _("Operation not supported"));
5561 if (iface->unmount_mountable_with_operation != NULL)
5562 (* iface->unmount_mountable_with_operation) (file,
5569 (* iface->unmount_mountable) (file,
5577 * g_file_unmount_mountable_with_operation_finish:
5578 * @file: input #GFile
5579 * @result: a #GAsyncResult
5580 * @error: a #GError, or %NULL
5582 * Finishes an unmount operation,
5583 * see g_file_unmount_mountable_with_operation() for details.
5585 * Finish an asynchronous unmount operation that was started
5586 * with g_file_unmount_mountable_with_operation().
5588 * Returns: %TRUE if the operation finished successfully.
5594 g_file_unmount_mountable_with_operation_finish (GFile *file,
5595 GAsyncResult *result,
5600 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5601 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5603 if (g_async_result_legacy_propagate_error (result, error))
5605 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5606 return g_task_propagate_boolean (G_TASK (result), error);
5608 iface = G_FILE_GET_IFACE (file);
5609 if (iface->unmount_mountable_with_operation_finish != NULL)
5610 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5612 return (* iface->unmount_mountable_finish) (file, result, error);
5616 * g_file_eject_mountable:
5617 * @file: input #GFile
5618 * @flags: flags affecting the operation
5619 * @cancellable: (nullable): optional #GCancellable object,
5621 * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5622 * to call when the request is satisfied
5623 * @user_data: the data to pass to callback function
5625 * Starts an asynchronous eject on a mountable.
5626 * When this operation has completed, @callback will be called with
5627 * @user_user data, and the operation can be finalized with
5628 * g_file_eject_mountable_finish().
5630 * If @cancellable is not %NULL, then the operation can be cancelled by
5631 * triggering the cancellable object from another thread. If the operation
5632 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5634 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5637 g_file_eject_mountable (GFile *file,
5638 GMountUnmountFlags flags,
5639 GCancellable *cancellable,
5640 GAsyncReadyCallback callback,
5645 g_return_if_fail (G_IS_FILE (file));
5647 iface = G_FILE_GET_IFACE (file);
5649 if (iface->eject_mountable == NULL)
5651 g_task_report_new_error (file, callback, user_data,
5652 g_file_eject_mountable_with_operation,
5653 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5654 _("Operation not supported"));
5658 (* iface->eject_mountable) (file,
5666 * g_file_eject_mountable_finish:
5667 * @file: input #GFile
5668 * @result: a #GAsyncResult
5669 * @error: a #GError, or %NULL
5671 * Finishes an asynchronous eject operation started by
5672 * g_file_eject_mountable().
5674 * Returns: %TRUE if the @file was ejected successfully.
5677 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5681 g_file_eject_mountable_finish (GFile *file,
5682 GAsyncResult *result,
5687 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5688 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5690 if (g_async_result_legacy_propagate_error (result, error))
5692 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5693 return g_task_propagate_boolean (G_TASK (result), error);
5695 iface = G_FILE_GET_IFACE (file);
5696 return (* iface->eject_mountable_finish) (file, result, error);
5700 * g_file_eject_mountable_with_operation:
5701 * @file: input #GFile
5702 * @flags: flags affecting the operation
5703 * @mount_operation: (nullable): a #GMountOperation,
5704 * or %NULL to avoid user interaction
5705 * @cancellable: (nullable): optional #GCancellable object,
5707 * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5708 * to call when the request is satisfied
5709 * @user_data: the data to pass to callback function
5711 * Starts an asynchronous eject on a mountable.
5712 * When this operation has completed, @callback will be called with
5713 * @user_user data, and the operation can be finalized with
5714 * g_file_eject_mountable_with_operation_finish().
5716 * If @cancellable is not %NULL, then the operation can be cancelled by
5717 * triggering the cancellable object from another thread. If the operation
5718 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5723 g_file_eject_mountable_with_operation (GFile *file,
5724 GMountUnmountFlags flags,
5725 GMountOperation *mount_operation,
5726 GCancellable *cancellable,
5727 GAsyncReadyCallback callback,
5732 g_return_if_fail (G_IS_FILE (file));
5734 iface = G_FILE_GET_IFACE (file);
5736 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5738 g_task_report_new_error (file, callback, user_data,
5739 g_file_eject_mountable_with_operation,
5740 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5741 _("Operation not supported"));
5745 if (iface->eject_mountable_with_operation != NULL)
5746 (* iface->eject_mountable_with_operation) (file,
5753 (* iface->eject_mountable) (file,
5761 * g_file_eject_mountable_with_operation_finish:
5762 * @file: input #GFile
5763 * @result: a #GAsyncResult
5764 * @error: a #GError, or %NULL
5766 * Finishes an asynchronous eject operation started by
5767 * g_file_eject_mountable_with_operation().
5769 * Returns: %TRUE if the @file was ejected successfully.
5775 g_file_eject_mountable_with_operation_finish (GFile *file,
5776 GAsyncResult *result,
5781 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5782 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5784 if (g_async_result_legacy_propagate_error (result, error))
5786 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5787 return g_task_propagate_boolean (G_TASK (result), error);
5789 iface = G_FILE_GET_IFACE (file);
5790 if (iface->eject_mountable_with_operation_finish != NULL)
5791 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5793 return (* iface->eject_mountable_finish) (file, result, error);
5797 * g_file_monitor_directory:
5798 * @file: input #GFile
5799 * @flags: a set of #GFileMonitorFlags
5800 * @cancellable: (nullable): optional #GCancellable object,
5802 * @error: a #GError, or %NULL
5804 * Obtains a directory monitor for the given file.
5805 * This may fail if directory monitoring is not supported.
5807 * If @cancellable is not %NULL, then the operation can be cancelled by
5808 * triggering the cancellable object from another thread. If the operation
5809 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5811 * It does not make sense for @flags to contain
5812 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5813 * directories. It is not possible to monitor all the files in a
5814 * directory for changes made via hard links; if you want to do this then
5815 * you must register individual watches with g_file_monitor().
5817 * Virtual: monitor_dir
5818 * Returns: (transfer full): a #GFileMonitor for the given @file,
5819 * or %NULL on error.
5820 * Free the returned object with g_object_unref().
5823 g_file_monitor_directory (GFile *file,
5824 GFileMonitorFlags flags,
5825 GCancellable *cancellable,
5830 g_return_val_if_fail (G_IS_FILE (file), NULL);
5831 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5833 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5836 iface = G_FILE_GET_IFACE (file);
5838 if (iface->monitor_dir == NULL)
5840 g_set_error_literal (error, G_IO_ERROR,
5841 G_IO_ERROR_NOT_SUPPORTED,
5842 _("Operation not supported"));
5846 return (* iface->monitor_dir) (file, flags, cancellable, error);
5850 * g_file_monitor_file:
5851 * @file: input #GFile
5852 * @flags: a set of #GFileMonitorFlags
5853 * @cancellable: (nullable): optional #GCancellable object,
5855 * @error: a #GError, or %NULL
5857 * Obtains a file monitor for the given file. If no file notification
5858 * mechanism exists, then regular polling of the file is used.
5860 * If @cancellable is not %NULL, then the operation can be cancelled by
5861 * triggering the cancellable object from another thread. If the operation
5862 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5864 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5865 * will also attempt to report changes made to the file via another
5866 * filename (ie, a hard link). Without this flag, you can only rely on
5867 * changes made through the filename contained in @file to be
5868 * reported. Using this flag may result in an increase in resource
5869 * usage, and may not have any effect depending on the #GFileMonitor
5870 * backend and/or filesystem type.
5872 * Returns: (transfer full): a #GFileMonitor for the given @file,
5873 * or %NULL on error.
5874 * Free the returned object with g_object_unref().
5877 g_file_monitor_file (GFile *file,
5878 GFileMonitorFlags flags,
5879 GCancellable *cancellable,
5883 GFileMonitor *monitor;
5885 g_return_val_if_fail (G_IS_FILE (file), NULL);
5887 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5890 iface = G_FILE_GET_IFACE (file);
5894 if (iface->monitor_file)
5895 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5897 /* Fallback to polling */
5898 if (monitor == NULL)
5899 monitor = _g_poll_file_monitor_new (file);
5906 * @file: input #GFile
5907 * @flags: a set of #GFileMonitorFlags
5908 * @cancellable: (nullable): optional #GCancellable object,
5910 * @error: a #GError, or %NULL
5912 * Obtains a file or directory monitor for the given file,
5913 * depending on the type of the file.
5915 * If @cancellable is not %NULL, then the operation can be cancelled by
5916 * triggering the cancellable object from another thread. If the operation
5917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5919 * Returns: (transfer full): a #GFileMonitor for the given @file,
5920 * or %NULL on error.
5921 * Free the returned object with g_object_unref().
5926 g_file_monitor (GFile *file,
5927 GFileMonitorFlags flags,
5928 GCancellable *cancellable,
5931 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5932 return g_file_monitor_directory (file,
5933 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5934 cancellable, error);
5936 return g_file_monitor_file (file, flags, cancellable, error);
5939 /********************************************
5940 * Default implementation of async ops *
5941 ********************************************/
5945 GFileQueryInfoFlags flags;
5946 } QueryInfoAsyncData;
5949 query_info_data_free (QueryInfoAsyncData *data)
5951 g_free (data->attributes);
5956 query_info_async_thread (GTask *task,
5959 GCancellable *cancellable)
5961 QueryInfoAsyncData *data = task_data;
5963 GError *error = NULL;
5965 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5967 g_task_return_pointer (task, info, g_object_unref);
5969 g_task_return_error (task, error);
5973 g_file_real_query_info_async (GFile *file,
5974 const char *attributes,
5975 GFileQueryInfoFlags flags,
5977 GCancellable *cancellable,
5978 GAsyncReadyCallback callback,
5982 QueryInfoAsyncData *data;
5984 data = g_new0 (QueryInfoAsyncData, 1);
5985 data->attributes = g_strdup (attributes);
5986 data->flags = flags;
5988 task = g_task_new (file, cancellable, callback, user_data);
5989 g_task_set_source_tag (task, g_file_real_query_info_async);
5990 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5991 g_task_set_priority (task, io_priority);
5992 g_task_run_in_thread (task, query_info_async_thread);
5993 g_object_unref (task);
5997 g_file_real_query_info_finish (GFile *file,
6001 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6003 return g_task_propagate_pointer (G_TASK (res), error);
6007 query_filesystem_info_async_thread (GTask *task,
6010 GCancellable *cancellable)
6012 const char *attributes = task_data;
6014 GError *error = NULL;
6016 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
6018 g_task_return_pointer (task, info, g_object_unref);
6020 g_task_return_error (task, error);
6024 g_file_real_query_filesystem_info_async (GFile *file,
6025 const char *attributes,
6027 GCancellable *cancellable,
6028 GAsyncReadyCallback callback,
6033 task = g_task_new (file, cancellable, callback, user_data);
6034 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
6035 g_task_set_task_data (task, g_strdup (attributes), g_free);
6036 g_task_set_priority (task, io_priority);
6037 g_task_run_in_thread (task, query_filesystem_info_async_thread);
6038 g_object_unref (task);
6042 g_file_real_query_filesystem_info_finish (GFile *file,
6046 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6048 return g_task_propagate_pointer (G_TASK (res), error);
6052 enumerate_children_async_thread (GTask *task,
6055 GCancellable *cancellable)
6057 QueryInfoAsyncData *data = task_data;
6058 GFileEnumerator *enumerator;
6059 GError *error = NULL;
6061 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
6063 g_task_return_error (task, error);
6065 g_task_return_pointer (task, enumerator, g_object_unref);
6069 g_file_real_enumerate_children_async (GFile *file,
6070 const char *attributes,
6071 GFileQueryInfoFlags flags,
6073 GCancellable *cancellable,
6074 GAsyncReadyCallback callback,
6078 QueryInfoAsyncData *data;
6080 data = g_new0 (QueryInfoAsyncData, 1);
6081 data->attributes = g_strdup (attributes);
6082 data->flags = flags;
6084 task = g_task_new (file, cancellable, callback, user_data);
6085 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
6086 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
6087 g_task_set_priority (task, io_priority);
6088 g_task_run_in_thread (task, enumerate_children_async_thread);
6089 g_object_unref (task);
6092 static GFileEnumerator *
6093 g_file_real_enumerate_children_finish (GFile *file,
6097 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6099 return g_task_propagate_pointer (G_TASK (res), error);
6103 open_read_async_thread (GTask *task,
6106 GCancellable *cancellable)
6108 GFileInputStream *stream;
6109 GError *error = NULL;
6111 stream = g_file_read (G_FILE (object), cancellable, &error);
6113 g_task_return_pointer (task, stream, g_object_unref);
6115 g_task_return_error (task, error);
6119 g_file_real_read_async (GFile *file,
6121 GCancellable *cancellable,
6122 GAsyncReadyCallback callback,
6127 task = g_task_new (file, cancellable, callback, user_data);
6128 g_task_set_source_tag (task, g_file_real_read_async);
6129 g_task_set_priority (task, io_priority);
6130 g_task_run_in_thread (task, open_read_async_thread);
6131 g_object_unref (task);
6134 static GFileInputStream *
6135 g_file_real_read_finish (GFile *file,
6139 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6141 return g_task_propagate_pointer (G_TASK (res), error);
6145 append_to_async_thread (GTask *task,
6146 gpointer source_object,
6148 GCancellable *cancellable)
6150 GFileCreateFlags *data = task_data;
6151 GFileOutputStream *stream;
6152 GError *error = NULL;
6154 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
6156 g_task_return_pointer (task, stream, g_object_unref);
6158 g_task_return_error (task, error);
6162 g_file_real_append_to_async (GFile *file,
6163 GFileCreateFlags flags,
6165 GCancellable *cancellable,
6166 GAsyncReadyCallback callback,
6169 GFileCreateFlags *data;
6172 data = g_new0 (GFileCreateFlags, 1);
6175 task = g_task_new (file, cancellable, callback, user_data);
6176 g_task_set_source_tag (task, g_file_real_append_to_async);
6177 g_task_set_task_data (task, data, g_free);
6178 g_task_set_priority (task, io_priority);
6180 g_task_run_in_thread (task, append_to_async_thread);
6181 g_object_unref (task);
6184 static GFileOutputStream *
6185 g_file_real_append_to_finish (GFile *file,
6189 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6191 return g_task_propagate_pointer (G_TASK (res), error);
6195 create_async_thread (GTask *task,
6196 gpointer source_object,
6198 GCancellable *cancellable)
6200 GFileCreateFlags *data = task_data;
6201 GFileOutputStream *stream;
6202 GError *error = NULL;
6204 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
6206 g_task_return_pointer (task, stream, g_object_unref);
6208 g_task_return_error (task, error);
6212 g_file_real_create_async (GFile *file,
6213 GFileCreateFlags flags,
6215 GCancellable *cancellable,
6216 GAsyncReadyCallback callback,
6219 GFileCreateFlags *data;
6222 data = g_new0 (GFileCreateFlags, 1);
6225 task = g_task_new (file, cancellable, callback, user_data);
6226 g_task_set_source_tag (task, g_file_real_create_async);
6227 g_task_set_task_data (task, data, g_free);
6228 g_task_set_priority (task, io_priority);
6230 g_task_run_in_thread (task, create_async_thread);
6231 g_object_unref (task);
6234 static GFileOutputStream *
6235 g_file_real_create_finish (GFile *file,
6239 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6241 return g_task_propagate_pointer (G_TASK (res), error);
6245 GFileOutputStream *stream;
6247 gboolean make_backup;
6248 GFileCreateFlags flags;
6252 replace_async_data_free (ReplaceAsyncData *data)
6255 g_object_unref (data->stream);
6256 g_free (data->etag);
6261 replace_async_thread (GTask *task,
6262 gpointer source_object,
6264 GCancellable *cancellable)
6266 GFileOutputStream *stream;
6267 ReplaceAsyncData *data = task_data;
6268 GError *error = NULL;
6270 stream = g_file_replace (G_FILE (source_object),
6278 g_task_return_pointer (task, stream, g_object_unref);
6280 g_task_return_error (task, error);
6284 g_file_real_replace_async (GFile *file,
6286 gboolean make_backup,
6287 GFileCreateFlags flags,
6289 GCancellable *cancellable,
6290 GAsyncReadyCallback callback,
6294 ReplaceAsyncData *data;
6296 data = g_new0 (ReplaceAsyncData, 1);
6297 data->etag = g_strdup (etag);
6298 data->make_backup = make_backup;
6299 data->flags = flags;
6301 task = g_task_new (file, cancellable, callback, user_data);
6302 g_task_set_source_tag (task, g_file_real_replace_async);
6303 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
6304 g_task_set_priority (task, io_priority);
6306 g_task_run_in_thread (task, replace_async_thread);
6307 g_object_unref (task);
6310 static GFileOutputStream *
6311 g_file_real_replace_finish (GFile *file,
6315 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6317 return g_task_propagate_pointer (G_TASK (res), error);
6321 delete_async_thread (GTask *task,
6324 GCancellable *cancellable)
6326 GError *error = NULL;
6328 if (g_file_delete (G_FILE (object), cancellable, &error))
6329 g_task_return_boolean (task, TRUE);
6331 g_task_return_error (task, error);
6335 g_file_real_delete_async (GFile *file,
6337 GCancellable *cancellable,
6338 GAsyncReadyCallback callback,
6343 task = g_task_new (file, cancellable, callback, user_data);
6344 g_task_set_source_tag (task, g_file_real_delete_async);
6345 g_task_set_priority (task, io_priority);
6346 g_task_run_in_thread (task, delete_async_thread);
6347 g_object_unref (task);
6351 g_file_real_delete_finish (GFile *file,
6355 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6357 return g_task_propagate_boolean (G_TASK (res), error);
6361 trash_async_thread (GTask *task,
6364 GCancellable *cancellable)
6366 GError *error = NULL;
6368 if (g_file_trash (G_FILE (object), cancellable, &error))
6369 g_task_return_boolean (task, TRUE);
6371 g_task_return_error (task, error);
6375 g_file_real_trash_async (GFile *file,
6377 GCancellable *cancellable,
6378 GAsyncReadyCallback callback,
6383 task = g_task_new (file, cancellable, callback, user_data);
6384 g_task_set_source_tag (task, g_file_real_trash_async);
6385 g_task_set_priority (task, io_priority);
6386 g_task_run_in_thread (task, trash_async_thread);
6387 g_object_unref (task);
6391 g_file_real_trash_finish (GFile *file,
6395 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6397 return g_task_propagate_boolean (G_TASK (res), error);
6402 GFile *source; /* (owned) */
6403 GFile *destination; /* (owned) */
6404 GFileCopyFlags flags;
6405 GFileProgressCallback progress_cb;
6406 gpointer progress_cb_data;
6410 move_async_data_free (MoveAsyncData *data)
6412 g_object_unref (data->source);
6413 g_object_unref (data->destination);
6414 g_slice_free (MoveAsyncData, data);
6418 MoveAsyncData *data; /* (unowned) */
6419 goffset current_num_bytes;
6420 goffset total_num_bytes;
6424 move_async_progress_in_main (gpointer user_data)
6426 MoveProgressData *progress = user_data;
6427 MoveAsyncData *data = progress->data;
6429 data->progress_cb (progress->current_num_bytes,
6430 progress->total_num_bytes,
6431 data->progress_cb_data);
6433 return G_SOURCE_REMOVE;
6437 move_async_progress_callback (goffset current_num_bytes,
6438 goffset total_num_bytes,
6441 GTask *task = user_data;
6442 MoveAsyncData *data = g_task_get_task_data (task);
6443 MoveProgressData *progress;
6445 progress = g_new0 (MoveProgressData, 1);
6446 progress->data = data;
6447 progress->current_num_bytes = current_num_bytes;
6448 progress->total_num_bytes = total_num_bytes;
6450 g_main_context_invoke_full (g_task_get_context (task),
6451 g_task_get_priority (task),
6452 move_async_progress_in_main,
6453 g_steal_pointer (&progress),
6458 move_async_thread (GTask *task,
6461 GCancellable *cancellable)
6463 MoveAsyncData *data = task_data;
6465 GError *error = NULL;
6467 result = g_file_move (data->source,
6471 (data->progress_cb != NULL) ? move_async_progress_callback : NULL,
6475 g_task_return_boolean (task, TRUE);
6477 g_task_return_error (task, g_steal_pointer (&error));
6481 g_file_real_move_async (GFile *source,
6483 GFileCopyFlags flags,
6485 GCancellable *cancellable,
6486 GFileProgressCallback progress_callback,
6487 gpointer progress_callback_data,
6488 GAsyncReadyCallback callback,
6492 MoveAsyncData *data;
6494 data = g_slice_new0 (MoveAsyncData);
6495 data->source = g_object_ref (source);
6496 data->destination = g_object_ref (destination);
6497 data->flags = flags;
6498 data->progress_cb = progress_callback;
6499 data->progress_cb_data = progress_callback_data;
6501 task = g_task_new (source, cancellable, callback, user_data);
6502 g_task_set_source_tag (task, g_file_real_move_async);
6503 g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) move_async_data_free);
6504 g_task_set_priority (task, io_priority);
6505 g_task_run_in_thread (task, move_async_thread);
6506 g_object_unref (task);
6510 g_file_real_move_finish (GFile *file,
6511 GAsyncResult *result,
6514 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
6516 return g_task_propagate_boolean (G_TASK (result), error);
6520 make_directory_async_thread (GTask *task,
6523 GCancellable *cancellable)
6525 GError *error = NULL;
6527 if (g_file_make_directory (G_FILE (object), cancellable, &error))
6528 g_task_return_boolean (task, TRUE);
6530 g_task_return_error (task, error);
6534 g_file_real_make_directory_async (GFile *file,
6536 GCancellable *cancellable,
6537 GAsyncReadyCallback callback,
6542 task = g_task_new (file, cancellable, callback, user_data);
6543 g_task_set_source_tag (task, g_file_real_make_directory_async);
6544 g_task_set_priority (task, io_priority);
6545 g_task_run_in_thread (task, make_directory_async_thread);
6546 g_object_unref (task);
6550 g_file_real_make_directory_finish (GFile *file,
6554 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6556 return g_task_propagate_boolean (G_TASK (res), error);
6560 open_readwrite_async_thread (GTask *task,
6563 GCancellable *cancellable)
6565 GFileIOStream *stream;
6566 GError *error = NULL;
6568 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6571 g_task_return_error (task, error);
6573 g_task_return_pointer (task, stream, g_object_unref);
6577 g_file_real_open_readwrite_async (GFile *file,
6579 GCancellable *cancellable,
6580 GAsyncReadyCallback callback,
6585 task = g_task_new (file, cancellable, callback, user_data);
6586 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6587 g_task_set_priority (task, io_priority);
6589 g_task_run_in_thread (task, open_readwrite_async_thread);
6590 g_object_unref (task);
6593 static GFileIOStream *
6594 g_file_real_open_readwrite_finish (GFile *file,
6598 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6600 return g_task_propagate_pointer (G_TASK (res), error);
6604 create_readwrite_async_thread (GTask *task,
6607 GCancellable *cancellable)
6609 GFileCreateFlags *data = task_data;
6610 GFileIOStream *stream;
6611 GError *error = NULL;
6613 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6616 g_task_return_error (task, error);
6618 g_task_return_pointer (task, stream, g_object_unref);
6622 g_file_real_create_readwrite_async (GFile *file,
6623 GFileCreateFlags flags,
6625 GCancellable *cancellable,
6626 GAsyncReadyCallback callback,
6629 GFileCreateFlags *data;
6632 data = g_new0 (GFileCreateFlags, 1);
6635 task = g_task_new (file, cancellable, callback, user_data);
6636 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6637 g_task_set_task_data (task, data, g_free);
6638 g_task_set_priority (task, io_priority);
6640 g_task_run_in_thread (task, create_readwrite_async_thread);
6641 g_object_unref (task);
6644 static GFileIOStream *
6645 g_file_real_create_readwrite_finish (GFile *file,
6649 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6651 return g_task_propagate_pointer (G_TASK (res), error);
6656 gboolean make_backup;
6657 GFileCreateFlags flags;
6658 } ReplaceRWAsyncData;
6661 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6663 g_free (data->etag);
6668 replace_readwrite_async_thread (GTask *task,
6671 GCancellable *cancellable)
6673 GFileIOStream *stream;
6674 GError *error = NULL;
6675 ReplaceRWAsyncData *data = task_data;
6677 stream = g_file_replace_readwrite (G_FILE (object),
6685 g_task_return_error (task, error);
6687 g_task_return_pointer (task, stream, g_object_unref);
6691 g_file_real_replace_readwrite_async (GFile *file,
6693 gboolean make_backup,
6694 GFileCreateFlags flags,
6696 GCancellable *cancellable,
6697 GAsyncReadyCallback callback,
6701 ReplaceRWAsyncData *data;
6703 data = g_new0 (ReplaceRWAsyncData, 1);
6704 data->etag = g_strdup (etag);
6705 data->make_backup = make_backup;
6706 data->flags = flags;
6708 task = g_task_new (file, cancellable, callback, user_data);
6709 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6710 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6711 g_task_set_priority (task, io_priority);
6713 g_task_run_in_thread (task, replace_readwrite_async_thread);
6714 g_object_unref (task);
6717 static GFileIOStream *
6718 g_file_real_replace_readwrite_finish (GFile *file,
6722 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6724 return g_task_propagate_pointer (G_TASK (res), error);
6728 set_display_name_async_thread (GTask *task,
6731 GCancellable *cancellable)
6733 GError *error = NULL;
6734 char *name = task_data;
6737 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6740 g_task_return_error (task, error);
6742 g_task_return_pointer (task, file, g_object_unref);
6746 g_file_real_set_display_name_async (GFile *file,
6747 const char *display_name,
6749 GCancellable *cancellable,
6750 GAsyncReadyCallback callback,
6755 task = g_task_new (file, cancellable, callback, user_data);
6756 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6757 g_task_set_task_data (task, g_strdup (display_name), g_free);
6758 g_task_set_priority (task, io_priority);
6760 g_task_run_in_thread (task, set_display_name_async_thread);
6761 g_object_unref (task);
6765 g_file_real_set_display_name_finish (GFile *file,
6769 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6771 return g_task_propagate_pointer (G_TASK (res), error);
6775 GFileQueryInfoFlags flags;
6780 set_info_data_free (SetInfoAsyncData *data)
6783 g_object_unref (data->info);
6788 set_info_async_thread (GTask *task,
6791 GCancellable *cancellable)
6793 SetInfoAsyncData *data = task_data;
6794 GError *error = NULL;
6796 if (g_file_set_attributes_from_info (G_FILE (object),
6801 g_task_return_boolean (task, TRUE);
6803 g_task_return_error (task, error);
6807 g_file_real_set_attributes_async (GFile *file,
6809 GFileQueryInfoFlags flags,
6811 GCancellable *cancellable,
6812 GAsyncReadyCallback callback,
6816 SetInfoAsyncData *data;
6818 data = g_new0 (SetInfoAsyncData, 1);
6819 data->info = g_file_info_dup (info);
6820 data->flags = flags;
6822 task = g_task_new (file, cancellable, callback, user_data);
6823 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6824 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6825 g_task_set_priority (task, io_priority);
6827 g_task_run_in_thread (task, set_info_async_thread);
6828 g_object_unref (task);
6832 g_file_real_set_attributes_finish (GFile *file,
6837 SetInfoAsyncData *data;
6839 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6841 data = g_task_get_task_data (G_TASK (res));
6844 *info = g_object_ref (data->info);
6846 return g_task_propagate_boolean (G_TASK (res), error);
6850 find_enclosing_mount_async_thread (GTask *task,
6853 GCancellable *cancellable)
6855 GError *error = NULL;
6858 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6861 g_task_return_error (task, error);
6863 g_task_return_pointer (task, mount, g_object_unref);
6867 g_file_real_find_enclosing_mount_async (GFile *file,
6869 GCancellable *cancellable,
6870 GAsyncReadyCallback callback,
6875 task = g_task_new (file, cancellable, callback, user_data);
6876 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6877 g_task_set_priority (task, io_priority);
6879 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6880 g_object_unref (task);
6884 g_file_real_find_enclosing_mount_finish (GFile *file,
6888 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6890 return g_task_propagate_pointer (G_TASK (res), error);
6897 GFileCopyFlags flags;
6898 GFileProgressCallback progress_cb;
6899 gpointer progress_cb_data;
6903 copy_async_data_free (CopyAsyncData *data)
6905 g_object_unref (data->source);
6906 g_object_unref (data->destination);
6907 g_slice_free (CopyAsyncData, data);
6911 CopyAsyncData *data;
6912 goffset current_num_bytes;
6913 goffset total_num_bytes;
6917 copy_async_progress_in_main (gpointer user_data)
6919 CopyProgressData *progress = user_data;
6920 CopyAsyncData *data = progress->data;
6922 data->progress_cb (progress->current_num_bytes,
6923 progress->total_num_bytes,
6924 data->progress_cb_data);
6930 copy_async_progress_callback (goffset current_num_bytes,
6931 goffset total_num_bytes,
6934 GTask *task = user_data;
6935 CopyAsyncData *data = g_task_get_task_data (task);
6936 CopyProgressData *progress;
6938 progress = g_new (CopyProgressData, 1);
6939 progress->data = data;
6940 progress->current_num_bytes = current_num_bytes;
6941 progress->total_num_bytes = total_num_bytes;
6943 g_main_context_invoke_full (g_task_get_context (task),
6944 g_task_get_priority (task),
6945 copy_async_progress_in_main,
6951 copy_async_thread (GTask *task,
6954 GCancellable *cancellable)
6956 CopyAsyncData *data = task_data;
6958 GError *error = NULL;
6960 result = g_file_copy (data->source,
6964 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6968 g_task_return_boolean (task, TRUE);
6970 g_task_return_error (task, error);
6974 g_file_real_copy_async (GFile *source,
6976 GFileCopyFlags flags,
6978 GCancellable *cancellable,
6979 GFileProgressCallback progress_callback,
6980 gpointer progress_callback_data,
6981 GAsyncReadyCallback callback,
6985 CopyAsyncData *data;
6987 data = g_slice_new (CopyAsyncData);
6988 data->source = g_object_ref (source);
6989 data->destination = g_object_ref (destination);
6990 data->flags = flags;
6991 data->progress_cb = progress_callback;
6992 data->progress_cb_data = progress_callback_data;
6994 task = g_task_new (source, cancellable, callback, user_data);
6995 g_task_set_source_tag (task, g_file_real_copy_async);
6996 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6997 g_task_set_priority (task, io_priority);
6998 g_task_run_in_thread (task, copy_async_thread);
6999 g_object_unref (task);
7003 g_file_real_copy_finish (GFile *file,
7007 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7009 return g_task_propagate_boolean (G_TASK (res), error);
7013 /********************************************
7014 * Default VFS operations *
7015 ********************************************/
7018 * g_file_new_for_path:
7019 * @path: (type filename): a string containing a relative or absolute path.
7020 * The string must be encoded in the glib filename encoding.
7022 * Constructs a #GFile for a given path. This operation never
7023 * fails, but the returned object might not support any I/O
7024 * operation if @path is malformed.
7026 * Returns: (transfer full): a new #GFile for the given @path.
7027 * Free the returned object with g_object_unref().
7030 g_file_new_for_path (const char *path)
7032 g_return_val_if_fail (path != NULL, NULL);
7034 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
7038 * g_file_new_for_uri:
7039 * @uri: a UTF-8 string containing a URI
7041 * Constructs a #GFile for a given URI. This operation never
7042 * fails, but the returned object might not support any I/O
7043 * operation if @uri is malformed or if the uri type is
7046 * Returns: (transfer full): a new #GFile for the given @uri.
7047 * Free the returned object with g_object_unref().
7050 g_file_new_for_uri (const char *uri)
7052 g_return_val_if_fail (uri != NULL, NULL);
7054 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
7059 * @tmpl: (type filename) (nullable): Template for the file
7060 * name, as in g_file_open_tmp(), or %NULL for a default template
7061 * @iostream: (out): on return, a #GFileIOStream for the created file
7062 * @error: a #GError, or %NULL
7064 * Opens a file in the preferred directory for temporary files (as
7065 * returned by g_get_tmp_dir()) and returns a #GFile and
7066 * #GFileIOStream pointing to it.
7068 * @tmpl should be a string in the GLib file name encoding
7069 * containing a sequence of six 'X' characters, and containing no
7070 * directory components. If it is %NULL, a default template is used.
7072 * Unlike the other #GFile constructors, this will return %NULL if
7073 * a temporary file could not be created.
7075 * Returns: (transfer full): a new #GFile.
7076 * Free the returned object with g_object_unref().
7081 g_file_new_tmp (const char *tmpl,
7082 GFileIOStream **iostream,
7088 GFileOutputStream *output;
7090 g_return_val_if_fail (iostream != NULL, NULL);
7092 fd = g_file_open_tmp (tmpl, &path, error);
7096 file = g_file_new_for_path (path);
7098 output = _g_local_file_output_stream_new (fd);
7099 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
7101 g_object_unref (output);
7109 GFileIOStream *iostream;
7113 new_tmp_data_free (NewTmpAsyncData *data)
7115 g_clear_object (&data->file);
7116 g_clear_object (&data->iostream);
7121 new_tmp_async_thread (GTask *task,
7124 GCancellable *cancellable)
7127 const char *tmpl = task_data;
7128 GFileIOStream *iostream = NULL;
7129 GError *error = NULL;
7130 NewTmpAsyncData *return_data;
7132 if (g_task_return_error_if_cancelled (task))
7135 file = g_file_new_tmp (tmpl, &iostream, &error);
7139 int error_code = G_IO_ERROR_FAILED;
7141 if (error->domain == G_IO_ERROR)
7143 g_task_return_error (task, g_steal_pointer (&error));
7147 if (error->domain == G_FILE_ERROR)
7148 error_code = g_io_error_from_file_error (error->code);
7150 g_task_return_new_error (task, G_IO_ERROR, error_code,
7151 _("Failed to create a temporary directory for "
7152 "template “%s”: %s"),
7153 tmpl, error->message);
7155 g_clear_error (&error);
7159 return_data = g_new0 (NewTmpAsyncData, 1);
7160 return_data->file = g_steal_pointer (&file);
7161 return_data->iostream = g_steal_pointer (&iostream);
7163 g_task_return_pointer (task, g_steal_pointer (&return_data),
7164 (GDestroyNotify) new_tmp_data_free);
7168 * g_file_new_tmp_async:
7169 * @tmpl: (type filename) (nullable): Template for the file
7170 * name, as in g_file_open_tmp(), or %NULL for a default template
7171 * @io_priority: the [I/O priority][io-priority] of the request
7172 * @cancellable: optional #GCancellable object, %NULL to ignore
7173 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
7174 * @user_data: (nullable): data to pass to @callback
7176 * Asynchronously opens a file in the preferred directory for temporary files
7177 * (as returned by g_get_tmp_dir()) as g_file_new_tmp().
7179 * @tmpl should be a string in the GLib file name encoding
7180 * containing a sequence of six 'X' characters, and containing no
7181 * directory components. If it is %NULL, a default template is used.
7186 g_file_new_tmp_async (const char *tmpl,
7188 GCancellable *cancellable,
7189 GAsyncReadyCallback callback,
7194 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7196 task = g_task_new (NULL, cancellable, callback, user_data);
7197 g_task_set_source_tag (task, g_file_new_tmp_async);
7198 g_task_set_task_data (task, g_strdup (tmpl), g_free);
7199 g_task_set_priority (task, io_priority);
7200 g_task_set_check_cancellable (task, TRUE);
7201 g_task_run_in_thread (task, new_tmp_async_thread);
7202 g_object_unref (task);
7206 * g_file_new_tmp_finish:
7207 * @result: a #GAsyncResult
7208 * @iostream: (out) (not optional) (not nullable) (transfer full): on return, a #GFileIOStream for the created file
7209 * @error: a #GError, or %NULL
7211 * Finishes a temporary file creation started by g_file_new_tmp_async().
7213 * Returns: (transfer full): a new #GFile.
7214 * Free the returned object with g_object_unref().
7219 g_file_new_tmp_finish (GAsyncResult *result,
7220 GFileIOStream **iostream,
7224 NewTmpAsyncData *data;
7226 g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7227 g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7228 g_file_new_tmp_async, NULL);
7229 g_return_val_if_fail (iostream != NULL, NULL);
7230 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7232 data = g_task_propagate_pointer (G_TASK (result), error);
7240 file = g_steal_pointer (&data->file);
7241 *iostream = g_steal_pointer (&data->iostream);
7243 new_tmp_data_free (data);
7249 new_tmp_dir_async_thread (GTask *task,
7252 GCancellable *cancellable)
7255 const char *tmpl = task_data;
7256 GError *error = NULL;
7258 if (g_task_return_error_if_cancelled (task))
7261 path = g_dir_make_tmp (tmpl, &error);
7265 int error_code = G_IO_ERROR_FAILED;
7267 if (error->domain == G_IO_ERROR)
7269 g_task_return_error (task, g_steal_pointer (&error));
7273 if (error->domain == G_FILE_ERROR)
7274 error_code = g_io_error_from_file_error (error->code);
7276 g_task_return_new_error (task, G_IO_ERROR, error_code,
7277 _("Failed to create a temporary directory for "
7278 "template “%s”: %s"),
7279 tmpl, error->message);
7281 g_clear_error (&error);
7285 g_task_return_pointer (task, g_file_new_for_path (path), g_object_unref);
7291 * g_file_new_tmp_dir_async:
7292 * @tmpl: (type filename) (nullable): Template for the file
7293 * name, as in g_dir_make_tmp(), or %NULL for a default template
7294 * @io_priority: the [I/O priority][io-priority] of the request
7295 * @cancellable: optional #GCancellable object, %NULL to ignore
7296 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
7297 * @user_data: (nullable): data to pass to @callback
7299 * Asynchronously creates a directory in the preferred directory for
7300 * temporary files (as returned by g_get_tmp_dir()) as g_dir_make_tmp().
7302 * @tmpl should be a string in the GLib file name encoding
7303 * containing a sequence of six 'X' characters, and containing no
7304 * directory components. If it is %NULL, a default template is used.
7309 g_file_new_tmp_dir_async (const char *tmpl,
7311 GCancellable *cancellable,
7312 GAsyncReadyCallback callback,
7317 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7319 task = g_task_new (NULL, cancellable, callback, user_data);
7320 g_task_set_source_tag (task, g_file_new_tmp_dir_async);
7321 g_task_set_task_data (task, g_strdup (tmpl), g_free);
7322 g_task_set_priority (task, io_priority);
7323 g_task_set_check_cancellable (task, TRUE);
7324 g_task_run_in_thread (task, new_tmp_dir_async_thread);
7325 g_object_unref (task);
7329 * g_file_new_tmp_dir_finish:
7330 * @result: a #GAsyncResult
7331 * @error: a #GError, or %NULL
7333 * Finishes a temporary directory creation started by
7334 * g_file_new_tmp_dir_async().
7336 * Returns: (transfer full): a new #GFile.
7337 * Free the returned object with g_object_unref().
7342 g_file_new_tmp_dir_finish (GAsyncResult *result,
7345 g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7346 g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7347 g_file_new_tmp_dir_async, NULL);
7348 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7350 return g_task_propagate_pointer (G_TASK (result), error);
7354 * g_file_parse_name:
7355 * @parse_name: a file name or path to be parsed
7357 * Constructs a #GFile with the given @parse_name (i.e. something
7358 * given by g_file_get_parse_name()). This operation never fails,
7359 * but the returned object might not support any I/O operation if
7360 * the @parse_name cannot be parsed.
7362 * Returns: (transfer full): a new #GFile.
7365 g_file_parse_name (const char *parse_name)
7367 g_return_val_if_fail (parse_name != NULL, NULL);
7369 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
7373 * g_file_new_build_filename:
7374 * @first_element: (type filename): the first element in the path
7375 * @...: remaining elements in path, terminated by %NULL
7377 * Constructs a #GFile from a series of elements using the correct
7378 * separator for filenames.
7380 * Using this function is equivalent to calling g_build_filename(),
7381 * followed by g_file_new_for_path() on the result.
7383 * Returns: (transfer full): a new #GFile
7388 g_file_new_build_filename (const gchar *first_element,
7395 g_return_val_if_fail (first_element != NULL, NULL);
7397 va_start (args, first_element);
7398 str = g_build_filename_valist (first_element, &args);
7401 file = g_file_new_for_path (str);
7409 * g_file_new_build_filenamev:
7410 * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
7411 * array of strings containing the path elements.
7413 * Constructs a #GFile from a vector of elements using the correct
7414 * separator for filenames.
7416 * Using this function is equivalent to calling g_build_filenamev(),
7417 * followed by g_file_new_for_path() on the result.
7419 * Returns: (transfer full): a new #GFile
7424 g_file_new_build_filenamev (const gchar * const *args)
7429 str = g_build_filenamev ((gchar **) args);
7430 file = g_file_new_for_path (str);
7437 is_valid_scheme_character (char c)
7439 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
7442 /* Following RFC 2396, valid schemes are built like:
7443 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
7446 has_valid_scheme (const char *uri)
7452 if (!g_ascii_isalpha (*p))
7457 } while (is_valid_scheme_character (*p));
7463 new_for_cmdline_arg (const gchar *arg,
7469 if (g_path_is_absolute (arg))
7470 return g_file_new_for_path (arg);
7472 if (has_valid_scheme (arg))
7473 return g_file_new_for_uri (arg);
7479 current_dir = g_get_current_dir ();
7480 filename = g_build_filename (current_dir, arg, NULL);
7481 g_free (current_dir);
7484 filename = g_build_filename (cwd, arg, NULL);
7486 file = g_file_new_for_path (filename);
7493 * g_file_new_for_commandline_arg:
7494 * @arg: (type filename): a command line string
7496 * Creates a #GFile with the given argument from the command line.
7497 * The value of @arg can be either a URI, an absolute path or a
7498 * relative path resolved relative to the current working directory.
7499 * This operation never fails, but the returned object might not
7500 * support any I/O operation if @arg points to a malformed path.
7502 * Note that on Windows, this function expects its argument to be in
7503 * UTF-8 -- not the system code page. This means that you
7504 * should not use this function with string from argv as it is passed
7505 * to main(). g_win32_get_command_line() will return a UTF-8 version of
7506 * the commandline. #GApplication also uses UTF-8 but
7507 * g_application_command_line_create_file_for_arg() may be more useful
7508 * for you there. It is also always possible to use this function with
7509 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
7511 * Returns: (transfer full): a new #GFile.
7512 * Free the returned object with g_object_unref().
7515 g_file_new_for_commandline_arg (const char *arg)
7517 g_return_val_if_fail (arg != NULL, NULL);
7519 return new_for_cmdline_arg (arg, NULL);
7523 * g_file_new_for_commandline_arg_and_cwd:
7524 * @arg: (type filename): a command line string
7525 * @cwd: (type filename): the current working directory of the commandline
7527 * Creates a #GFile with the given argument from the command line.
7529 * This function is similar to g_file_new_for_commandline_arg() except
7530 * that it allows for passing the current working directory as an
7531 * argument instead of using the current working directory of the
7534 * This is useful if the commandline argument was given in a context
7535 * other than the invocation of the current process.
7537 * See also g_application_command_line_create_file_for_arg().
7539 * Returns: (transfer full): a new #GFile
7544 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
7547 g_return_val_if_fail (arg != NULL, NULL);
7548 g_return_val_if_fail (cwd != NULL, NULL);
7550 return new_for_cmdline_arg (arg, cwd);
7554 * g_file_mount_enclosing_volume:
7555 * @location: input #GFile
7556 * @flags: flags affecting the operation
7557 * @mount_operation: (nullable): a #GMountOperation
7558 * or %NULL to avoid user interaction
7559 * @cancellable: (nullable): optional #GCancellable object,
7561 * @callback: (nullable): a #GAsyncReadyCallback to call
7562 * when the request is satisfied, or %NULL
7563 * @user_data: the data to pass to callback function
7565 * Starts a @mount_operation, mounting the volume that contains
7566 * the file @location.
7568 * When this operation has completed, @callback will be called with
7569 * @user_user data, and the operation can be finalized with
7570 * g_file_mount_enclosing_volume_finish().
7572 * If @cancellable is not %NULL, then the operation can be cancelled by
7573 * triggering the cancellable object from another thread. If the operation
7574 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7577 g_file_mount_enclosing_volume (GFile *location,
7578 GMountMountFlags flags,
7579 GMountOperation *mount_operation,
7580 GCancellable *cancellable,
7581 GAsyncReadyCallback callback,
7586 g_return_if_fail (G_IS_FILE (location));
7588 iface = G_FILE_GET_IFACE (location);
7590 if (iface->mount_enclosing_volume == NULL)
7592 g_task_report_new_error (location, callback, user_data,
7593 g_file_mount_enclosing_volume,
7594 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7595 _("volume doesn’t implement mount"));
7599 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
7604 * g_file_mount_enclosing_volume_finish:
7605 * @location: input #GFile
7606 * @result: a #GAsyncResult
7607 * @error: a #GError, or %NULL
7609 * Finishes a mount operation started by g_file_mount_enclosing_volume().
7611 * Returns: %TRUE if successful. If an error has occurred,
7612 * this function will return %FALSE and set @error
7613 * appropriately if present.
7616 g_file_mount_enclosing_volume_finish (GFile *location,
7617 GAsyncResult *result,
7622 g_return_val_if_fail (G_IS_FILE (location), FALSE);
7623 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7625 if (g_async_result_legacy_propagate_error (result, error))
7627 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
7628 return g_task_propagate_boolean (G_TASK (result), error);
7630 iface = G_FILE_GET_IFACE (location);
7632 return (* iface->mount_enclosing_volume_finish) (location, result, error);
7635 /********************************************
7636 * Utility functions *
7637 ********************************************/
7640 * g_file_query_default_handler:
7641 * @file: a #GFile to open
7642 * @cancellable: optional #GCancellable object, %NULL to ignore
7643 * @error: a #GError, or %NULL
7645 * Returns the #GAppInfo that is registered as the default
7646 * application to handle the file specified by @file.
7648 * If @cancellable is not %NULL, then the operation can be cancelled by
7649 * triggering the cancellable object from another thread. If the operation
7650 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7652 * Returns: (transfer full): a #GAppInfo if the handle was found,
7653 * %NULL if there were errors.
7654 * When you are done with it, release it with g_object_unref()
7657 g_file_query_default_handler (GFile *file,
7658 GCancellable *cancellable,
7662 const char *content_type;
7667 uri_scheme = g_file_get_uri_scheme (file);
7668 if (uri_scheme && uri_scheme[0] != '\0')
7670 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
7671 g_free (uri_scheme);
7673 if (appinfo != NULL)
7677 g_free (uri_scheme);
7679 info = g_file_query_info (file,
7680 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
7681 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
7690 content_type = g_file_info_get_content_type (info);
7691 if (content_type == NULL)
7692 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7695 /* Don't use is_native(), as we want to support fuse paths if available */
7696 path = g_file_get_path (file);
7697 appinfo = g_app_info_get_default_for_type (content_type,
7702 g_object_unref (info);
7704 if (appinfo != NULL)
7707 g_set_error_literal (error, G_IO_ERROR,
7708 G_IO_ERROR_NOT_SUPPORTED,
7709 _("No application is registered as handling this file"));
7714 query_default_handler_query_app_info_for_type_cb (GObject *object,
7715 GAsyncResult *result,
7718 GTask *task = G_TASK (user_data);
7720 GError *error = NULL;
7722 appinfo = g_app_info_get_default_for_type_finish (result, &error);
7724 if (appinfo != NULL)
7726 g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
7728 else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7730 g_task_return_new_error (task,
7731 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7732 "%s", error->message);
7736 g_task_return_error (task, g_steal_pointer (&error));
7739 g_clear_error (&error);
7740 g_object_unref (task);
7744 query_default_handler_query_info_cb (GObject *object,
7745 GAsyncResult *result,
7748 GFile *file = G_FILE (object);
7749 GTask *task = G_TASK (user_data);
7750 GError *error = NULL;
7752 const char *content_type;
7754 info = g_file_query_info_finish (file, result, &error);
7757 g_task_return_error (task, g_steal_pointer (&error));
7758 g_object_unref (task);
7762 content_type = g_file_info_get_content_type (info);
7763 if (content_type == NULL)
7764 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7767 GCancellable *cancellable = g_task_get_cancellable (task);
7770 /* Don't use is_native(), as we want to support fuse paths if available */
7771 path = g_file_get_path (file);
7773 g_app_info_get_default_for_type_async (content_type,
7776 query_default_handler_query_app_info_for_type_cb,
7777 g_steal_pointer (&task));
7783 g_task_return_new_error (task,
7785 G_IO_ERROR_NOT_SUPPORTED,
7786 _("No application is registered as handling this file"));
7789 g_object_unref (info);
7790 g_clear_object (&task);
7794 on_query_default_handler_for_uri_cb (GObject *object,
7795 GAsyncResult *result,
7798 GTask *task = user_data;
7801 app_info = g_app_info_get_default_for_uri_scheme_finish (result, NULL);
7805 g_task_return_pointer (task, g_steal_pointer (&app_info), g_object_unref);
7806 g_object_unref (task);
7810 g_file_query_info_async (g_task_get_source_object (task),
7811 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
7812 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
7814 g_task_get_priority (task),
7815 g_task_get_cancellable (task),
7816 query_default_handler_query_info_cb,
7822 * g_file_query_default_handler_async:
7823 * @file: a #GFile to open
7824 * @io_priority: the [I/O priority][io-priority] of the request
7825 * @cancellable: optional #GCancellable object, %NULL to ignore
7826 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
7827 * @user_data: (nullable): data to pass to @callback
7829 * Async version of g_file_query_default_handler().
7834 g_file_query_default_handler_async (GFile *file,
7836 GCancellable *cancellable,
7837 GAsyncReadyCallback callback,
7843 task = g_task_new (file, cancellable, callback, user_data);
7844 g_task_set_source_tag (task, g_file_query_default_handler_async);
7846 uri_scheme = g_file_get_uri_scheme (file);
7847 if (uri_scheme && uri_scheme[0] != '\0')
7849 g_app_info_get_default_for_uri_scheme_async (uri_scheme,
7851 on_query_default_handler_for_uri_cb,
7852 g_steal_pointer (&task));
7853 g_free (uri_scheme);
7857 g_file_query_info_async (file,
7858 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
7859 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
7863 query_default_handler_query_info_cb,
7864 g_steal_pointer (&task));
7866 g_free (uri_scheme);
7870 * g_file_query_default_handler_finish:
7871 * @file: a #GFile to open
7872 * @result: a #GAsyncResult
7873 * @error: (nullable): a #GError
7875 * Finishes a g_file_query_default_handler_async() operation.
7877 * Returns: (transfer full): a #GAppInfo if the handle was found,
7878 * %NULL if there were errors.
7879 * When you are done with it, release it with g_object_unref()
7884 g_file_query_default_handler_finish (GFile *file,
7885 GAsyncResult *result,
7888 g_return_val_if_fail (G_IS_FILE (file), NULL);
7889 g_return_val_if_fail (g_task_is_valid (result, file), NULL);
7891 return g_task_propagate_pointer (G_TASK (result), error);
7894 #define GET_CONTENT_BLOCK_SIZE 8192
7897 * g_file_load_contents:
7898 * @file: input #GFile
7899 * @cancellable: optional #GCancellable object, %NULL to ignore
7900 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7901 * @length: (out) (optional): a location to place the length of the contents of the file,
7902 * or %NULL if the length is not needed
7903 * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
7904 * or %NULL if the entity tag is not needed
7905 * @error: a #GError, or %NULL
7907 * Loads the content of the file into memory. The data is always
7908 * zero-terminated, but this is not included in the resultant @length.
7909 * The returned @contents should be freed with g_free() when no longer
7912 * If @cancellable is not %NULL, then the operation can be cancelled by
7913 * triggering the cancellable object from another thread. If the operation
7914 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7916 * Returns: %TRUE if the @file's contents were successfully loaded.
7917 * %FALSE if there were errors.
7920 g_file_load_contents (GFile *file,
7921 GCancellable *cancellable,
7927 GFileInputStream *in;
7928 GByteArray *content;
7933 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7934 g_return_val_if_fail (contents != NULL, FALSE);
7936 in = g_file_read (file, cancellable, error);
7940 content = g_byte_array_new ();
7943 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7944 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
7945 content->data + pos,
7946 GET_CONTENT_BLOCK_SIZE,
7947 cancellable, error)) > 0)
7950 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7957 info = g_file_input_stream_query_info (in,
7958 G_FILE_ATTRIBUTE_ETAG_VALUE,
7963 *etag_out = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ETAG_VALUE) ? g_strdup (g_file_info_get_etag (info)) : NULL;
7964 g_object_unref (info);
7968 /* Ignore errors on close */
7969 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
7970 g_object_unref (in);
7974 /* error is set already */
7975 g_byte_array_free (content, TRUE);
7982 /* Zero terminate (we got an extra byte allocated for this */
7983 content->data[pos] = 0;
7985 *contents = (char *)g_byte_array_free (content, FALSE);
7992 GFileReadMoreCallback read_more_callback;
7993 GByteArray *content;
8000 load_contents_data_free (LoadContentsData *data)
8003 g_byte_array_free (data->content, TRUE);
8004 g_free (data->etag);
8009 load_contents_close_callback (GObject *obj,
8010 GAsyncResult *close_res,
8013 GInputStream *stream = G_INPUT_STREAM (obj);
8014 LoadContentsData *data = user_data;
8016 /* Ignore errors here, we're only reading anyway */
8017 g_input_stream_close_finish (stream, close_res, NULL);
8018 g_object_unref (stream);
8020 g_task_return_boolean (data->task, TRUE);
8021 g_object_unref (data->task);
8025 load_contents_fstat_callback (GObject *obj,
8026 GAsyncResult *stat_res,
8029 GInputStream *stream = G_INPUT_STREAM (obj);
8030 LoadContentsData *data = user_data;
8033 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
8037 data->etag = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ETAG_VALUE) ? g_strdup (g_file_info_get_etag (info)) : NULL;
8038 g_object_unref (info);
8041 g_input_stream_close_async (stream, 0,
8042 g_task_get_cancellable (data->task),
8043 load_contents_close_callback, data);
8047 load_contents_read_callback (GObject *obj,
8048 GAsyncResult *read_res,
8051 GInputStream *stream = G_INPUT_STREAM (obj);
8052 LoadContentsData *data = user_data;
8053 GError *error = NULL;
8056 read_size = g_input_stream_read_finish (stream, read_res, &error);
8060 g_task_return_error (data->task, error);
8061 g_object_unref (data->task);
8063 /* Close the file ignoring any error */
8064 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
8065 g_object_unref (stream);
8067 else if (read_size == 0)
8069 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
8070 G_FILE_ATTRIBUTE_ETAG_VALUE,
8072 g_task_get_cancellable (data->task),
8073 load_contents_fstat_callback,
8076 else if (read_size > 0)
8078 data->pos += read_size;
8080 g_byte_array_set_size (data->content,
8081 data->pos + GET_CONTENT_BLOCK_SIZE);
8084 if (data->read_more_callback &&
8085 !data->read_more_callback ((char *)data->content->data, data->pos,
8086 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
8087 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
8088 G_FILE_ATTRIBUTE_ETAG_VALUE,
8090 g_task_get_cancellable (data->task),
8091 load_contents_fstat_callback,
8094 g_input_stream_read_async (stream,
8095 data->content->data + data->pos,
8096 GET_CONTENT_BLOCK_SIZE,
8098 g_task_get_cancellable (data->task),
8099 load_contents_read_callback,
8105 load_contents_open_callback (GObject *obj,
8106 GAsyncResult *open_res,
8109 GFile *file = G_FILE (obj);
8110 GFileInputStream *stream;
8111 LoadContentsData *data = user_data;
8112 GError *error = NULL;
8114 stream = g_file_read_finish (file, open_res, &error);
8118 g_byte_array_set_size (data->content,
8119 data->pos + GET_CONTENT_BLOCK_SIZE);
8120 g_input_stream_read_async (G_INPUT_STREAM (stream),
8121 data->content->data + data->pos,
8122 GET_CONTENT_BLOCK_SIZE,
8124 g_task_get_cancellable (data->task),
8125 load_contents_read_callback,
8130 g_task_return_error (data->task, error);
8131 g_object_unref (data->task);
8136 * g_file_load_partial_contents_async: (skip)
8137 * @file: input #GFile
8138 * @cancellable: optional #GCancellable object, %NULL to ignore
8139 * @read_more_callback: (scope call) (closure user_data): a
8140 * #GFileReadMoreCallback to receive partial data
8141 * and to specify whether further data should be read
8142 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
8143 * when the request is satisfied
8144 * @user_data: the data to pass to the callback functions
8146 * Reads the partial contents of a file. A #GFileReadMoreCallback should
8147 * be used to stop reading from the file when appropriate, else this
8148 * function will behave exactly as g_file_load_contents_async(). This
8149 * operation can be finished by g_file_load_partial_contents_finish().
8151 * Users of this function should be aware that @user_data is passed to
8152 * both the @read_more_callback and the @callback.
8154 * If @cancellable is not %NULL, then the operation can be cancelled by
8155 * triggering the cancellable object from another thread. If the operation
8156 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8159 g_file_load_partial_contents_async (GFile *file,
8160 GCancellable *cancellable,
8161 GFileReadMoreCallback read_more_callback,
8162 GAsyncReadyCallback callback,
8165 LoadContentsData *data;
8167 g_return_if_fail (G_IS_FILE (file));
8169 data = g_new0 (LoadContentsData, 1);
8170 data->read_more_callback = read_more_callback;
8171 data->content = g_byte_array_new ();
8173 data->task = g_task_new (file, cancellable, callback, user_data);
8174 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
8175 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
8177 g_file_read_async (file,
8179 g_task_get_cancellable (data->task),
8180 load_contents_open_callback,
8185 * g_file_load_partial_contents_finish:
8186 * @file: input #GFile
8187 * @res: a #GAsyncResult
8188 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
8189 * @length: (out) (optional): a location to place the length of the contents of the file,
8190 * or %NULL if the length is not needed
8191 * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
8192 * or %NULL if the entity tag is not needed
8193 * @error: a #GError, or %NULL
8195 * Finishes an asynchronous partial load operation that was started
8196 * with g_file_load_partial_contents_async(). The data is always
8197 * zero-terminated, but this is not included in the resultant @length.
8198 * The returned @contents should be freed with g_free() when no longer
8201 * Returns: %TRUE if the load was successful. If %FALSE and @error is
8202 * present, it will be set appropriately.
8205 g_file_load_partial_contents_finish (GFile *file,
8213 LoadContentsData *data;
8215 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8216 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8217 g_return_val_if_fail (contents != NULL, FALSE);
8219 task = G_TASK (res);
8221 if (!g_task_propagate_boolean (task, error))
8228 data = g_task_get_task_data (task);
8231 *length = data->pos;
8235 *etag_out = data->etag;
8239 /* Zero terminate */
8240 g_byte_array_set_size (data->content, data->pos + 1);
8241 data->content->data[data->pos] = 0;
8243 *contents = (char *)g_byte_array_free (data->content, FALSE);
8244 data->content = NULL;
8250 * g_file_load_contents_async:
8251 * @file: input #GFile
8252 * @cancellable: optional #GCancellable object, %NULL to ignore
8253 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8254 * @user_data: the data to pass to callback function
8256 * Starts an asynchronous load of the @file's contents.
8258 * For more details, see g_file_load_contents() which is
8259 * the synchronous version of this call.
8261 * When the load operation has completed, @callback will be called
8262 * with @user data. To finish the operation, call
8263 * g_file_load_contents_finish() with the #GAsyncResult returned by
8266 * If @cancellable is not %NULL, then the operation can be cancelled by
8267 * triggering the cancellable object from another thread. If the operation
8268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8271 g_file_load_contents_async (GFile *file,
8272 GCancellable *cancellable,
8273 GAsyncReadyCallback callback,
8276 g_file_load_partial_contents_async (file,
8279 callback, user_data);
8283 * g_file_load_contents_finish:
8284 * @file: input #GFile
8285 * @res: a #GAsyncResult
8286 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
8287 * @length: (out) (optional): a location to place the length of the contents of the file,
8288 * or %NULL if the length is not needed
8289 * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
8290 * or %NULL if the entity tag is not needed
8291 * @error: a #GError, or %NULL
8293 * Finishes an asynchronous load of the @file's contents.
8294 * The contents are placed in @contents, and @length is set to the
8295 * size of the @contents string. The @contents should be freed with
8296 * g_free() when no longer needed. If @etag_out is present, it will be
8297 * set to the new entity tag for the @file.
8299 * Returns: %TRUE if the load was successful. If %FALSE and @error is
8300 * present, it will be set appropriately.
8303 g_file_load_contents_finish (GFile *file,
8310 return g_file_load_partial_contents_finish (file,
8319 * g_file_replace_contents:
8320 * @file: input #GFile
8321 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
8322 * @length: the length of @contents in bytes
8323 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
8325 * @make_backup: %TRUE if a backup should be created
8326 * @flags: a set of #GFileCreateFlags
8327 * @new_etag: (out) (optional) (nullable): a location to a new [entity tag][gfile-etag]
8328 * for the document. This should be freed with g_free() when no longer
8330 * @cancellable: optional #GCancellable object, %NULL to ignore
8331 * @error: a #GError, or %NULL
8333 * Replaces the contents of @file with @contents of @length bytes.
8335 * If @etag is specified (not %NULL), any existing file must have that etag,
8336 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
8338 * If @make_backup is %TRUE, this function will attempt to make a backup
8339 * of @file. Internally, it uses g_file_replace(), so will try to replace the
8340 * file contents in the safest way possible. For example, atomic renames are
8341 * used when replacing local files’ contents.
8343 * If @cancellable is not %NULL, then the operation can be cancelled by
8344 * triggering the cancellable object from another thread. If the operation
8345 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8347 * The returned @new_etag can be used to verify that the file hasn't
8348 * changed the next time it is saved over.
8350 * Returns: %TRUE if successful. If an error has occurred, this function
8351 * will return %FALSE and set @error appropriately if present.
8354 g_file_replace_contents (GFile *file,
8355 const char *contents,
8358 gboolean make_backup,
8359 GFileCreateFlags flags,
8361 GCancellable *cancellable,
8364 GFileOutputStream *out;
8365 gsize pos, remainder;
8369 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8370 g_return_val_if_fail (contents != NULL, FALSE);
8372 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
8378 while (remainder > 0 &&
8379 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
8381 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
8389 if (remainder > 0 && res < 0)
8391 /* Ignore errors on close */
8392 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
8393 g_object_unref (out);
8395 /* error is set already */
8399 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
8402 *new_etag = g_file_output_stream_get_etag (out);
8404 g_object_unref (out);
8415 } ReplaceContentsData;
8418 replace_contents_data_free (ReplaceContentsData *data)
8420 g_bytes_unref (data->content);
8421 g_free (data->etag);
8426 replace_contents_close_callback (GObject *obj,
8427 GAsyncResult *close_res,
8430 GOutputStream *stream = G_OUTPUT_STREAM (obj);
8431 ReplaceContentsData *data = user_data;
8433 /* Ignore errors here, we're only reading anyway */
8434 g_output_stream_close_finish (stream, close_res, NULL);
8438 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
8439 g_task_return_boolean (data->task, TRUE);
8441 g_object_unref (data->task);
8445 replace_contents_write_callback (GObject *obj,
8446 GAsyncResult *read_res,
8449 GOutputStream *stream = G_OUTPUT_STREAM (obj);
8450 ReplaceContentsData *data = user_data;
8451 GError *error = NULL;
8454 write_size = g_output_stream_write_finish (stream, read_res, &error);
8456 if (write_size <= 0)
8458 /* Error or EOF, close the file */
8461 data->failed = TRUE;
8462 g_task_return_error (data->task, error);
8464 g_output_stream_close_async (stream, 0,
8465 g_task_get_cancellable (data->task),
8466 replace_contents_close_callback, data);
8468 else if (write_size > 0)
8470 const gchar *content;
8473 content = g_bytes_get_data (data->content, &length);
8474 data->pos += write_size;
8476 if (data->pos >= length)
8477 g_output_stream_close_async (stream, 0,
8478 g_task_get_cancellable (data->task),
8479 replace_contents_close_callback, data);
8481 g_output_stream_write_async (stream,
8482 content + data->pos,
8485 g_task_get_cancellable (data->task),
8486 replace_contents_write_callback,
8492 replace_contents_open_callback (GObject *obj,
8493 GAsyncResult *open_res,
8496 GFile *file = G_FILE (obj);
8497 GFileOutputStream *stream;
8498 ReplaceContentsData *data = user_data;
8499 GError *error = NULL;
8501 stream = g_file_replace_finish (file, open_res, &error);
8505 const gchar *content;
8508 content = g_bytes_get_data (data->content, &length);
8509 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
8510 content + data->pos,
8513 g_task_get_cancellable (data->task),
8514 replace_contents_write_callback,
8516 g_object_unref (stream); /* ownership is transferred to the write_async() call above */
8520 g_task_return_error (data->task, error);
8521 g_object_unref (data->task);
8526 * g_file_replace_contents_async:
8527 * @file: input #GFile
8528 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
8529 * @length: the length of @contents in bytes
8530 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
8531 * @make_backup: %TRUE if a backup should be created
8532 * @flags: a set of #GFileCreateFlags
8533 * @cancellable: optional #GCancellable object, %NULL to ignore
8534 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8535 * @user_data: the data to pass to callback function
8537 * Starts an asynchronous replacement of @file with the given
8538 * @contents of @length bytes. @etag will replace the document's
8539 * current entity tag.
8541 * When this operation has completed, @callback will be called with
8542 * @user_user data, and the operation can be finalized with
8543 * g_file_replace_contents_finish().
8545 * If @cancellable is not %NULL, then the operation can be cancelled by
8546 * triggering the cancellable object from another thread. If the operation
8547 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8549 * If @make_backup is %TRUE, this function will attempt to
8550 * make a backup of @file.
8552 * Note that no copy of @contents will be made, so it must stay valid
8553 * until @callback is called. See g_file_replace_contents_bytes_async()
8554 * for a #GBytes version that will automatically hold a reference to the
8555 * contents (without copying) for the duration of the call.
8558 g_file_replace_contents_async (GFile *file,
8559 const char *contents,
8562 gboolean make_backup,
8563 GFileCreateFlags flags,
8564 GCancellable *cancellable,
8565 GAsyncReadyCallback callback,
8570 bytes = g_bytes_new_static (contents, length);
8571 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
8572 cancellable, callback, user_data);
8573 g_bytes_unref (bytes);
8577 * g_file_replace_contents_bytes_async:
8578 * @file: input #GFile
8579 * @contents: a #GBytes
8580 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
8581 * @make_backup: %TRUE if a backup should be created
8582 * @flags: a set of #GFileCreateFlags
8583 * @cancellable: optional #GCancellable object, %NULL to ignore
8584 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8585 * @user_data: the data to pass to callback function
8587 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
8588 * This function will keep a ref on @contents until the operation is done.
8589 * Unlike g_file_replace_contents_async() this allows forgetting about the
8590 * content without waiting for the callback.
8592 * When this operation has completed, @callback will be called with
8593 * @user_user data, and the operation can be finalized with
8594 * g_file_replace_contents_finish().
8599 g_file_replace_contents_bytes_async (GFile *file,
8602 gboolean make_backup,
8603 GFileCreateFlags flags,
8604 GCancellable *cancellable,
8605 GAsyncReadyCallback callback,
8608 ReplaceContentsData *data;
8610 g_return_if_fail (G_IS_FILE (file));
8611 g_return_if_fail (contents != NULL);
8613 data = g_new0 (ReplaceContentsData, 1);
8615 data->content = g_bytes_ref (contents);
8617 data->task = g_task_new (file, cancellable, callback, user_data);
8618 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
8619 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
8621 g_file_replace_async (file,
8626 g_task_get_cancellable (data->task),
8627 replace_contents_open_callback,
8632 * g_file_replace_contents_finish:
8633 * @file: input #GFile
8634 * @res: a #GAsyncResult
8635 * @new_etag: (out) (optional) (nullable): a location of a new [entity tag][gfile-etag]
8636 * for the document. This should be freed with g_free() when it is no
8637 * longer needed, or %NULL
8638 * @error: a #GError, or %NULL
8640 * Finishes an asynchronous replace of the given @file. See
8641 * g_file_replace_contents_async(). Sets @new_etag to the new entity
8642 * tag for the document, if present.
8644 * Returns: %TRUE on success, %FALSE on failure.
8647 g_file_replace_contents_finish (GFile *file,
8653 ReplaceContentsData *data;
8655 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8656 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8658 task = G_TASK (res);
8660 if (!g_task_propagate_boolean (task, error))
8663 data = g_task_get_task_data (task);
8667 *new_etag = data->etag;
8668 data->etag = NULL; /* Take ownership */
8675 g_file_real_measure_disk_usage (GFile *file,
8676 GFileMeasureFlags flags,
8677 GCancellable *cancellable,
8678 GFileMeasureProgressCallback progress_callback,
8679 gpointer progress_data,
8680 guint64 *disk_usage,
8685 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8686 "Operation not supported for the current backend.");
8692 GFileMeasureFlags flags;
8693 GFileMeasureProgressCallback progress_callback;
8694 gpointer progress_data;
8706 GFileMeasureProgressCallback callback;
8709 guint64 current_size;
8715 measure_disk_usage_invoke_progress (gpointer user_data)
8717 MeasureProgress *progress = user_data;
8719 (* progress->callback) (progress->reporting,
8720 progress->current_size, progress->num_dirs, progress->num_files,
8721 progress->user_data);
8727 measure_disk_usage_progress (gboolean reporting,
8728 guint64 current_size,
8733 MeasureProgress progress;
8734 GTask *task = user_data;
8735 MeasureTaskData *data;
8737 data = g_task_get_task_data (task);
8739 progress.callback = data->progress_callback;
8740 progress.user_data = data->progress_data;
8741 progress.reporting = reporting;
8742 progress.current_size = current_size;
8743 progress.num_dirs = num_dirs;
8744 progress.num_files = num_files;
8746 g_main_context_invoke_full (g_task_get_context (task),
8747 g_task_get_priority (task),
8748 measure_disk_usage_invoke_progress,
8749 g_memdup2 (&progress, sizeof progress),
8754 measure_disk_usage_thread (GTask *task,
8755 gpointer source_object,
8757 GCancellable *cancellable)
8759 MeasureTaskData *data = task_data;
8760 GError *error = NULL;
8761 MeasureResult result = { 0, };
8763 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
8764 data->progress_callback ? measure_disk_usage_progress : NULL, task,
8765 &result.disk_usage, &result.num_dirs, &result.num_files,
8767 g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
8769 g_task_return_error (task, error);
8773 g_file_real_measure_disk_usage_async (GFile *file,
8774 GFileMeasureFlags flags,
8776 GCancellable *cancellable,
8777 GFileMeasureProgressCallback progress_callback,
8778 gpointer progress_data,
8779 GAsyncReadyCallback callback,
8782 MeasureTaskData data;
8786 data.progress_callback = progress_callback;
8787 data.progress_data = progress_data;
8789 task = g_task_new (file, cancellable, callback, user_data);
8790 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
8791 g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
8792 g_task_set_priority (task, io_priority);
8794 g_task_run_in_thread (task, measure_disk_usage_thread);
8795 g_object_unref (task);
8799 g_file_real_measure_disk_usage_finish (GFile *file,
8800 GAsyncResult *result,
8801 guint64 *disk_usage,
8806 MeasureResult *measure_result;
8808 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
8810 measure_result = g_task_propagate_pointer (G_TASK (result), error);
8812 if (measure_result == NULL)
8816 *disk_usage = measure_result->disk_usage;
8819 *num_dirs = measure_result->num_dirs;
8822 *num_files = measure_result->num_files;
8824 g_free (measure_result);
8830 * g_file_measure_disk_usage:
8832 * @flags: #GFileMeasureFlags
8833 * @cancellable: (nullable): optional #GCancellable
8834 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
8835 * @progress_data: user_data for @progress_callback
8836 * @disk_usage: (out) (optional): the number of bytes of disk space used
8837 * @num_dirs: (out) (optional): the number of directories encountered
8838 * @num_files: (out) (optional): the number of non-directories encountered
8839 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
8841 * Recursively measures the disk usage of @file.
8843 * This is essentially an analog of the 'du' command, but it also
8844 * reports the number of directories and non-directory files encountered
8845 * (including things like symbolic links).
8847 * By default, errors are only reported against the toplevel file
8848 * itself. Errors found while recursing are silently ignored, unless
8849 * %G_FILE_MEASURE_REPORT_ANY_ERROR is given in @flags.
8851 * The returned size, @disk_usage, is in bytes and should be formatted
8852 * with g_format_size() in order to get something reasonable for showing
8853 * in a user interface.
8855 * @progress_callback and @progress_data can be given to request
8856 * periodic progress updates while scanning. See the documentation for
8857 * #GFileMeasureProgressCallback for information about when and how the
8858 * callback will be invoked.
8860 * Returns: %TRUE if successful, with the out parameters set.
8861 * %FALSE otherwise, with @error set.
8866 g_file_measure_disk_usage (GFile *file,
8867 GFileMeasureFlags flags,
8868 GCancellable *cancellable,
8869 GFileMeasureProgressCallback progress_callback,
8870 gpointer progress_data,
8871 guint64 *disk_usage,
8876 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8877 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
8878 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8880 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
8881 progress_callback, progress_data,
8882 disk_usage, num_dirs, num_files,
8887 * g_file_measure_disk_usage_async:
8889 * @flags: #GFileMeasureFlags
8890 * @io_priority: the [I/O priority][io-priority] of the request
8891 * @cancellable: (nullable): optional #GCancellable
8892 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
8893 * @progress_data: user_data for @progress_callback
8894 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
8895 * @user_data: the data to pass to callback function
8897 * Recursively measures the disk usage of @file.
8899 * This is the asynchronous version of g_file_measure_disk_usage(). See
8900 * there for more information.
8905 g_file_measure_disk_usage_async (GFile *file,
8906 GFileMeasureFlags flags,
8908 GCancellable *cancellable,
8909 GFileMeasureProgressCallback progress_callback,
8910 gpointer progress_data,
8911 GAsyncReadyCallback callback,
8914 g_return_if_fail (G_IS_FILE (file));
8915 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8917 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
8918 progress_callback, progress_data,
8919 callback, user_data);
8923 * g_file_measure_disk_usage_finish:
8925 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
8926 * @disk_usage: (out) (optional): the number of bytes of disk space used
8927 * @num_dirs: (out) (optional): the number of directories encountered
8928 * @num_files: (out) (optional): the number of non-directories encountered
8929 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
8931 * Collects the results from an earlier call to
8932 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
8935 * Returns: %TRUE if successful, with the out parameters set.
8936 * %FALSE otherwise, with @error set.
8941 g_file_measure_disk_usage_finish (GFile *file,
8942 GAsyncResult *result,
8943 guint64 *disk_usage,
8948 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8949 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8951 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
8955 * g_file_start_mountable:
8956 * @file: input #GFile
8957 * @flags: flags affecting the operation
8958 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
8959 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
8960 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
8961 * @user_data: the data to pass to callback function
8963 * Starts a file of type %G_FILE_TYPE_MOUNTABLE.
8964 * Using @start_operation, you can request callbacks when, for instance,
8965 * passwords are needed during authentication.
8967 * If @cancellable is not %NULL, then the operation can be cancelled by
8968 * triggering the cancellable object from another thread. If the operation
8969 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8971 * When the operation is finished, @callback will be called.
8972 * You can then call g_file_mount_mountable_finish() to get
8973 * the result of the operation.
8978 g_file_start_mountable (GFile *file,
8979 GDriveStartFlags flags,
8980 GMountOperation *start_operation,
8981 GCancellable *cancellable,
8982 GAsyncReadyCallback callback,
8987 g_return_if_fail (G_IS_FILE (file));
8989 iface = G_FILE_GET_IFACE (file);
8991 if (iface->start_mountable == NULL)
8993 g_task_report_new_error (file, callback, user_data,
8994 g_file_start_mountable,
8995 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8996 _("Operation not supported"));
9000 (* iface->start_mountable) (file,
9009 * g_file_start_mountable_finish:
9010 * @file: input #GFile
9011 * @result: a #GAsyncResult
9012 * @error: a #GError, or %NULL
9014 * Finishes a start operation. See g_file_start_mountable() for details.
9016 * Finish an asynchronous start operation that was started
9017 * with g_file_start_mountable().
9019 * Returns: %TRUE if the operation finished successfully. %FALSE
9025 g_file_start_mountable_finish (GFile *file,
9026 GAsyncResult *result,
9031 g_return_val_if_fail (G_IS_FILE (file), FALSE);
9032 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9034 if (g_async_result_legacy_propagate_error (result, error))
9036 else if (g_async_result_is_tagged (result, g_file_start_mountable))
9037 return g_task_propagate_boolean (G_TASK (result), error);
9039 iface = G_FILE_GET_IFACE (file);
9040 return (* iface->start_mountable_finish) (file, result, error);
9044 * g_file_stop_mountable:
9045 * @file: input #GFile
9046 * @flags: flags affecting the operation
9047 * @mount_operation: (nullable): a #GMountOperation,
9048 * or %NULL to avoid user interaction.
9049 * @cancellable: (nullable): optional #GCancellable object,
9051 * @callback: (nullable): a #GAsyncReadyCallback to call
9052 * when the request is satisfied, or %NULL
9053 * @user_data: the data to pass to callback function
9055 * Stops a file of type %G_FILE_TYPE_MOUNTABLE.
9057 * If @cancellable is not %NULL, then the operation can be cancelled by
9058 * triggering the cancellable object from another thread. If the operation
9059 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
9061 * When the operation is finished, @callback will be called.
9062 * You can then call g_file_stop_mountable_finish() to get
9063 * the result of the operation.
9068 g_file_stop_mountable (GFile *file,
9069 GMountUnmountFlags flags,
9070 GMountOperation *mount_operation,
9071 GCancellable *cancellable,
9072 GAsyncReadyCallback callback,
9077 g_return_if_fail (G_IS_FILE (file));
9079 iface = G_FILE_GET_IFACE (file);
9081 if (iface->stop_mountable == NULL)
9083 g_task_report_new_error (file, callback, user_data,
9084 g_file_stop_mountable,
9085 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9086 _("Operation not supported"));
9090 (* iface->stop_mountable) (file,
9099 * g_file_stop_mountable_finish:
9100 * @file: input #GFile
9101 * @result: a #GAsyncResult
9102 * @error: a #GError, or %NULL
9104 * Finishes a stop operation, see g_file_stop_mountable() for details.
9106 * Finish an asynchronous stop operation that was started
9107 * with g_file_stop_mountable().
9109 * Returns: %TRUE if the operation finished successfully.
9115 g_file_stop_mountable_finish (GFile *file,
9116 GAsyncResult *result,
9121 g_return_val_if_fail (G_IS_FILE (file), FALSE);
9122 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9124 if (g_async_result_legacy_propagate_error (result, error))
9126 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
9127 return g_task_propagate_boolean (G_TASK (result), error);
9129 iface = G_FILE_GET_IFACE (file);
9130 return (* iface->stop_mountable_finish) (file, result, error);
9134 * g_file_poll_mountable:
9135 * @file: input #GFile
9136 * @cancellable: optional #GCancellable object, %NULL to ignore
9137 * @callback: (nullable): a #GAsyncReadyCallback to call
9138 * when the request is satisfied, or %NULL
9139 * @user_data: the data to pass to callback function
9141 * Polls a file of type %G_FILE_TYPE_MOUNTABLE.
9143 * If @cancellable is not %NULL, then the operation can be cancelled by
9144 * triggering the cancellable object from another thread. If the operation
9145 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
9147 * When the operation is finished, @callback will be called.
9148 * You can then call g_file_mount_mountable_finish() to get
9149 * the result of the operation.
9154 g_file_poll_mountable (GFile *file,
9155 GCancellable *cancellable,
9156 GAsyncReadyCallback callback,
9161 g_return_if_fail (G_IS_FILE (file));
9163 iface = G_FILE_GET_IFACE (file);
9165 if (iface->poll_mountable == NULL)
9167 g_task_report_new_error (file, callback, user_data,
9168 g_file_poll_mountable,
9169 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9170 _("Operation not supported"));
9174 (* iface->poll_mountable) (file,
9181 * g_file_poll_mountable_finish:
9182 * @file: input #GFile
9183 * @result: a #GAsyncResult
9184 * @error: a #GError, or %NULL
9186 * Finishes a poll operation. See g_file_poll_mountable() for details.
9188 * Finish an asynchronous poll operation that was polled
9189 * with g_file_poll_mountable().
9191 * Returns: %TRUE if the operation finished successfully. %FALSE
9197 g_file_poll_mountable_finish (GFile *file,
9198 GAsyncResult *result,
9203 g_return_val_if_fail (G_IS_FILE (file), FALSE);
9204 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9206 if (g_async_result_legacy_propagate_error (result, error))
9208 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
9209 return g_task_propagate_boolean (G_TASK (result), error);
9211 iface = G_FILE_GET_IFACE (file);
9212 return (* iface->poll_mountable_finish) (file, result, error);
9216 * g_file_supports_thread_contexts:
9219 * Checks if @file supports
9220 * [thread-default contexts][g-main-context-push-thread-default-context].
9221 * If this returns %FALSE, you cannot perform asynchronous operations on
9222 * @file in a thread that has a thread-default context.
9224 * Returns: Whether or not @file supports thread-default contexts.
9229 g_file_supports_thread_contexts (GFile *file)
9233 g_return_val_if_fail (G_IS_FILE (file), FALSE);
9235 iface = G_FILE_GET_IFACE (file);
9236 return iface->supports_thread_contexts;
9240 * g_file_load_bytes:
9242 * @cancellable: (nullable): a #GCancellable or %NULL
9243 * @etag_out: (out) (nullable) (optional): a location to place the current
9244 * entity tag for the file, or %NULL if the entity tag is not needed
9245 * @error: a location for a #GError or %NULL
9247 * Loads the contents of @file and returns it as #GBytes.
9249 * If @file is a resource:// based URI, the resulting bytes will reference the
9250 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
9251 * g_file_load_contents() and g_bytes_new_take().
9253 * For resources, @etag_out will be set to %NULL.
9255 * The data contained in the resulting #GBytes is always zero-terminated, but
9256 * this is not included in the #GBytes length. The resulting #GBytes should be
9257 * freed with g_bytes_unref() when no longer in use.
9259 * Returns: (transfer full): a #GBytes or %NULL and @error is set
9264 g_file_load_bytes (GFile *file,
9265 GCancellable *cancellable,
9272 g_return_val_if_fail (G_IS_FILE (file), NULL);
9273 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
9274 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9276 if (etag_out != NULL)
9279 if (g_file_has_uri_scheme (file, "resource"))
9282 gchar *uri, *unescaped;
9284 uri = g_file_get_uri (file);
9285 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
9288 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
9294 /* contents is guaranteed to be \0 terminated */
9295 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
9296 return g_bytes_new_take (g_steal_pointer (&contents), len);
9302 g_file_load_bytes_cb (GObject *object,
9303 GAsyncResult *result,
9306 GFile *file = G_FILE (object);
9307 GTask *task = user_data;
9308 GError *error = NULL;
9310 gchar *contents = NULL;
9313 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
9314 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
9317 g_task_return_error (task, g_steal_pointer (&error));
9319 g_task_return_pointer (task,
9320 g_bytes_new_take (g_steal_pointer (&contents), len),
9321 (GDestroyNotify)g_bytes_unref);
9323 g_object_unref (task);
9327 * g_file_load_bytes_async:
9329 * @cancellable: (nullable): a #GCancellable or %NULL
9330 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
9331 * to call when the request is satisfied
9332 * @user_data: the data to pass to callback function
9334 * Asynchronously loads the contents of @file as #GBytes.
9336 * If @file is a resource:// based URI, the resulting bytes will reference the
9337 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
9338 * g_file_load_contents_async() and g_bytes_new_take().
9340 * @callback should call g_file_load_bytes_finish() to get the result of this
9341 * asynchronous operation.
9343 * See g_file_load_bytes() for more information.
9348 g_file_load_bytes_async (GFile *file,
9349 GCancellable *cancellable,
9350 GAsyncReadyCallback callback,
9353 GError *error = NULL;
9357 g_return_if_fail (G_IS_FILE (file));
9358 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
9360 task = g_task_new (file, cancellable, callback, user_data);
9361 g_task_set_source_tag (task, g_file_load_bytes_async);
9363 if (!g_file_has_uri_scheme (file, "resource"))
9365 g_file_load_contents_async (file,
9367 g_file_load_bytes_cb,
9368 g_steal_pointer (&task));
9372 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
9375 g_task_return_error (task, g_steal_pointer (&error));
9377 g_task_return_pointer (task,
9378 g_steal_pointer (&bytes),
9379 (GDestroyNotify)g_bytes_unref);
9381 g_object_unref (task);
9385 * g_file_load_bytes_finish:
9387 * @result: a #GAsyncResult provided to the callback
9388 * @etag_out: (out) (nullable) (optional): a location to place the current
9389 * entity tag for the file, or %NULL if the entity tag is not needed
9390 * @error: a location for a #GError, or %NULL
9392 * Completes an asynchronous request to g_file_load_bytes_async().
9394 * For resources, @etag_out will be set to %NULL.
9396 * The data contained in the resulting #GBytes is always zero-terminated, but
9397 * this is not included in the #GBytes length. The resulting #GBytes should be
9398 * freed with g_bytes_unref() when no longer in use.
9400 * See g_file_load_bytes() for more information.
9402 * Returns: (transfer full): a #GBytes or %NULL and @error is set
9407 g_file_load_bytes_finish (GFile *file,
9408 GAsyncResult *result,
9414 g_return_val_if_fail (G_IS_FILE (file), NULL);
9415 g_return_val_if_fail (G_IS_TASK (result), NULL);
9416 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
9417 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9419 bytes = g_task_propagate_pointer (G_TASK (result), error);
9421 if (etag_out != NULL)
9422 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));