1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
63 #include "gstrfuncsprivate.h"
68 * @short_description: File and Directory Handling
70 * @see_also: #GFileInfo, #GFileEnumerator
72 * #GFile is a high level abstraction for manipulating files on a
73 * virtual file system. #GFiles are lightweight, immutable objects
74 * that do no I/O upon creation. It is necessary to understand that
75 * #GFile objects do not represent files, merely an identifier for a
76 * file. All file content I/O is implemented as streaming operations
77 * (see #GInputStream and #GOutputStream).
79 * To construct a #GFile, you can use:
80 * - g_file_new_for_path() if you have a path.
81 * - g_file_new_for_uri() if you have a URI.
82 * - g_file_new_for_commandline_arg() for a command line argument.
83 * - g_file_new_tmp() to create a temporary file from a template.
84 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
85 * - g_file_new_build_filename() to create a file from path elements.
87 * One way to think of a #GFile is as an abstraction of a pathname. For
88 * normal files the system pathname is what is stored internally, but as
89 * #GFiles are extensible it could also be something else that corresponds
90 * to a pathname in a userspace implementation of a filesystem.
92 * #GFiles make up hierarchies of directories and files that correspond to
93 * the files on a filesystem. You can move through the file system with
94 * #GFile using g_file_get_parent() to get an identifier for the parent
95 * directory, g_file_get_child() to get a child within a directory,
96 * g_file_resolve_relative_path() to resolve a relative path between two
97 * #GFiles. There can be multiple hierarchies, so you may not end up at
98 * the same root if you repeatedly call g_file_get_parent() on two different
101 * All #GFiles have a basename (get with g_file_get_basename()). These names
102 * are byte strings that are used to identify the file on the filesystem
103 * (relative to its parent directory) and there is no guarantees that they
104 * have any particular charset encoding or even make any sense at all. If
105 * you want to use filenames in a user interface you should use the display
106 * name that you can get by requesting the
107 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
108 * This is guaranteed to be in UTF-8 and can be used in a user interface.
109 * But always store the real basename or the #GFile to use to actually
110 * access the file, because there is no way to go from a display name to
113 * Using #GFile as an identifier has the same weaknesses as using a path
114 * in that there may be multiple aliases for the same file. For instance,
115 * hard or soft links may cause two different #GFiles to refer to the same
116 * file. Other possible causes for aliases are: case insensitive filesystems,
117 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
118 * check if two #GFiles point to the same file you can query for the
119 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
120 * canonicalization of pathnames passed in, so that trivial differences in
121 * the path string used at creation (duplicated slashes, slash at end of
122 * path, "." or ".." path segments, etc) does not create different #GFiles.
124 * Many #GFile operations have both synchronous and asynchronous versions
125 * to suit your application. Asynchronous versions of synchronous functions
126 * simply have _async() appended to their function names. The asynchronous
127 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
128 * the operation, producing a GAsyncResult which is then passed to the
129 * function's matching _finish() operation.
131 * It is highly recommended to use asynchronous calls when running within a
132 * shared main loop, such as in the main thread of an application. This avoids
133 * I/O operations blocking other sources on the main loop from being dispatched.
134 * Synchronous I/O operations should be performed from worker threads. See the
135 * [introduction to asynchronous programming section][async-programming] for
138 * Some #GFile operations almost always take a noticeable amount of time, and
139 * so do not have synchronous analogs. Notable cases include:
140 * - g_file_mount_mountable() to mount a mountable file.
141 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
142 * - g_file_eject_mountable_with_operation() to eject a mountable file.
144 * ## Entity Tags # {#gfile-etag}
146 * One notable feature of #GFiles are entity tags, or "etags" for
147 * short. Entity tags are somewhat like a more abstract version of the
148 * traditional mtime, and can be used to quickly determine if the file
149 * has been modified from the version on the file system. See the
151 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
152 * for HTTP Etag headers, which are a very similar concept.
155 static void g_file_real_query_info_async (GFile *file,
156 const char *attributes,
157 GFileQueryInfoFlags flags,
159 GCancellable *cancellable,
160 GAsyncReadyCallback callback,
162 static GFileInfo * g_file_real_query_info_finish (GFile *file,
165 static void g_file_real_query_filesystem_info_async (GFile *file,
166 const char *attributes,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
171 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
174 static void g_file_real_enumerate_children_async (GFile *file,
175 const char *attributes,
176 GFileQueryInfoFlags flags,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
184 static void g_file_real_read_async (GFile *file,
186 GCancellable *cancellable,
187 GAsyncReadyCallback callback,
189 static GFileInputStream * g_file_real_read_finish (GFile *file,
192 static void g_file_real_append_to_async (GFile *file,
193 GFileCreateFlags flags,
195 GCancellable *cancellable,
196 GAsyncReadyCallback callback,
198 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
201 static void g_file_real_create_async (GFile *file,
202 GFileCreateFlags flags,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
207 static GFileOutputStream *g_file_real_create_finish (GFile *file,
210 static void g_file_real_replace_async (GFile *file,
212 gboolean make_backup,
213 GFileCreateFlags flags,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
221 static void g_file_real_delete_async (GFile *file,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
226 static gboolean g_file_real_delete_finish (GFile *file,
229 static void g_file_real_trash_async (GFile *file,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static gboolean g_file_real_trash_finish (GFile *file,
237 static void g_file_real_make_directory_async (GFile *file,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static gboolean g_file_real_make_directory_finish (GFile *file,
245 static void g_file_real_open_readwrite_async (GFile *file,
247 GCancellable *cancellable,
248 GAsyncReadyCallback callback,
250 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
253 static void g_file_real_create_readwrite_async (GFile *file,
254 GFileCreateFlags flags,
256 GCancellable *cancellable,
257 GAsyncReadyCallback callback,
259 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
262 static void g_file_real_replace_readwrite_async (GFile *file,
264 gboolean make_backup,
265 GFileCreateFlags flags,
267 GCancellable *cancellable,
268 GAsyncReadyCallback callback,
270 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
273 static gboolean g_file_real_set_attributes_from_info (GFile *file,
275 GFileQueryInfoFlags flags,
276 GCancellable *cancellable,
278 static void g_file_real_set_display_name_async (GFile *file,
279 const char *display_name,
281 GCancellable *cancellable,
282 GAsyncReadyCallback callback,
284 static GFile * g_file_real_set_display_name_finish (GFile *file,
287 static void g_file_real_set_attributes_async (GFile *file,
289 GFileQueryInfoFlags flags,
291 GCancellable *cancellable,
292 GAsyncReadyCallback callback,
294 static gboolean g_file_real_set_attributes_finish (GFile *file,
298 static void g_file_real_find_enclosing_mount_async (GFile *file,
300 GCancellable *cancellable,
301 GAsyncReadyCallback callback,
303 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
306 static void g_file_real_copy_async (GFile *source,
308 GFileCopyFlags flags,
310 GCancellable *cancellable,
311 GFileProgressCallback progress_callback,
312 gpointer progress_callback_data,
313 GAsyncReadyCallback callback,
315 static gboolean g_file_real_copy_finish (GFile *file,
319 static gboolean g_file_real_measure_disk_usage (GFile *file,
320 GFileMeasureFlags flags,
321 GCancellable *cancellable,
322 GFileMeasureProgressCallback progress_callback,
323 gpointer progress_data,
328 static void g_file_real_measure_disk_usage_async (GFile *file,
329 GFileMeasureFlags flags,
331 GCancellable *cancellable,
332 GFileMeasureProgressCallback progress_callback,
333 gpointer progress_data,
334 GAsyncReadyCallback callback,
336 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
337 GAsyncResult *result,
343 typedef GFileIface GFileInterface;
344 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
347 g_file_default_init (GFileIface *iface)
349 iface->enumerate_children_async = g_file_real_enumerate_children_async;
350 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
351 iface->set_display_name_async = g_file_real_set_display_name_async;
352 iface->set_display_name_finish = g_file_real_set_display_name_finish;
353 iface->query_info_async = g_file_real_query_info_async;
354 iface->query_info_finish = g_file_real_query_info_finish;
355 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
356 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
357 iface->set_attributes_async = g_file_real_set_attributes_async;
358 iface->set_attributes_finish = g_file_real_set_attributes_finish;
359 iface->read_async = g_file_real_read_async;
360 iface->read_finish = g_file_real_read_finish;
361 iface->append_to_async = g_file_real_append_to_async;
362 iface->append_to_finish = g_file_real_append_to_finish;
363 iface->create_async = g_file_real_create_async;
364 iface->create_finish = g_file_real_create_finish;
365 iface->replace_async = g_file_real_replace_async;
366 iface->replace_finish = g_file_real_replace_finish;
367 iface->delete_file_async = g_file_real_delete_async;
368 iface->delete_file_finish = g_file_real_delete_finish;
369 iface->trash_async = g_file_real_trash_async;
370 iface->trash_finish = g_file_real_trash_finish;
371 iface->make_directory_async = g_file_real_make_directory_async;
372 iface->make_directory_finish = g_file_real_make_directory_finish;
373 iface->open_readwrite_async = g_file_real_open_readwrite_async;
374 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
375 iface->create_readwrite_async = g_file_real_create_readwrite_async;
376 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
377 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
378 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
379 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
380 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
381 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
382 iface->copy_async = g_file_real_copy_async;
383 iface->copy_finish = g_file_real_copy_finish;
384 iface->measure_disk_usage = g_file_real_measure_disk_usage;
385 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
386 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
392 * @file: input #GFile
394 * Checks to see if a file is native to the platform.
396 * A native file is one expressed in the platform-native filename format,
397 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
398 * as it might be on a locally mounted remote filesystem.
400 * On some systems non-native files may be available using the native
401 * filesystem via a userspace filesystem (FUSE), in these cases this call
402 * will return %FALSE, but g_file_get_path() will still return a native path.
404 * This call does no blocking I/O.
406 * Returns: %TRUE if @file is native
409 g_file_is_native (GFile *file)
413 g_return_val_if_fail (G_IS_FILE (file), FALSE);
415 iface = G_FILE_GET_IFACE (file);
417 return (* iface->is_native) (file);
422 * g_file_has_uri_scheme:
423 * @file: input #GFile
424 * @uri_scheme: a string containing a URI scheme
426 * Checks to see if a #GFile has a given URI scheme.
428 * This call does no blocking I/O.
430 * Returns: %TRUE if #GFile's backend supports the
431 * given URI scheme, %FALSE if URI scheme is %NULL,
432 * not supported, or #GFile is invalid.
435 g_file_has_uri_scheme (GFile *file,
436 const char *uri_scheme)
440 g_return_val_if_fail (G_IS_FILE (file), FALSE);
441 g_return_val_if_fail (uri_scheme != NULL, FALSE);
443 iface = G_FILE_GET_IFACE (file);
445 return (* iface->has_uri_scheme) (file, uri_scheme);
450 * g_file_get_uri_scheme:
451 * @file: input #GFile
453 * Gets the URI scheme for a #GFile.
454 * RFC 3986 decodes the scheme as:
456 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
458 * Common schemes include "file", "http", "ftp", etc.
460 * This call does no blocking I/O.
462 * Returns: a string containing the URI scheme for the given
463 * #GFile. The returned string should be freed with g_free()
464 * when no longer needed.
467 g_file_get_uri_scheme (GFile *file)
471 g_return_val_if_fail (G_IS_FILE (file), NULL);
473 iface = G_FILE_GET_IFACE (file);
475 return (* iface->get_uri_scheme) (file);
480 * g_file_get_basename:
481 * @file: input #GFile
483 * Gets the base name (the last component of the path) for a given #GFile.
485 * If called for the top level of a system (such as the filesystem root
486 * or a uri like sftp://host/) it will return a single directory separator
487 * (and on Windows, possibly a drive letter).
489 * The base name is a byte string (not UTF-8). It has no defined encoding
490 * or rules other than it may not contain zero bytes. If you want to use
491 * filenames in a user interface you should use the display name that you
492 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
493 * attribute with g_file_query_info().
495 * This call does no blocking I/O.
497 * Returns: (type filename) (nullable): string containing the #GFile's
498 * base name, or %NULL if given #GFile is invalid. The returned string
499 * should be freed with g_free() when no longer needed.
502 g_file_get_basename (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_basename) (file);
515 * @file: input #GFile
517 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
518 * guaranteed to be an absolute, canonical path. It might contain symlinks.
520 * This call does no blocking I/O.
522 * Returns: (type filename) (nullable): string containing the #GFile's path,
523 * or %NULL if no such path exists. The returned string should be freed
524 * with g_free() when no longer needed.
527 g_file_get_path (GFile *file)
531 g_return_val_if_fail (G_IS_FILE (file), NULL);
533 iface = G_FILE_GET_IFACE (file);
535 return (* iface->get_path) (file);
539 file_peek_path_generic (GFile *file)
542 static GQuark _file_path_quark = 0;
544 if (G_UNLIKELY (_file_path_quark) == 0)
545 _file_path_quark = g_quark_from_static_string ("gio-file-path");
547 /* We need to be careful about threading, as two threads calling
548 * g_file_peek_path() on the same file could race: both would see
549 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
550 * the path, but the second thread to add it would end up freeing the path
551 * set by the first thread. The first thread would still return the pointer
552 * to that freed path, though, resulting an a read-after-free. Handle that
553 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
557 gchar *new_path = NULL;
559 path = g_object_get_qdata ((GObject*)file, _file_path_quark);
564 new_path = g_file_get_path (file);
565 if (new_path == NULL)
568 /* By passing NULL here, we ensure we never replace existing data: */
569 if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
570 NULL, (gpointer) new_path,
571 (GDestroyNotify) g_free, NULL))
585 * @file: input #GFile
587 * Exactly like g_file_get_path(), but caches the result via
588 * g_object_set_qdata_full(). This is useful for example in C
589 * applications which mix `g_file_*` APIs with native ones. It
590 * also avoids an extra duplicated string when possible, so will be
591 * generally more efficient.
593 * This call does no blocking I/O.
595 * Returns: (type filename) (nullable): string containing the #GFile's path,
596 * or %NULL if no such path exists. The returned string is owned by @file.
600 g_file_peek_path (GFile *file)
602 if (G_IS_LOCAL_FILE (file))
603 return _g_local_file_get_filename ((GLocalFile *) file);
604 return file_peek_path_generic (file);
609 * @file: input #GFile
611 * Gets the URI for the @file.
613 * This call does no blocking I/O.
615 * Returns: a string containing the #GFile's URI.
616 * The returned string should be freed with g_free()
617 * when no longer needed.
620 g_file_get_uri (GFile *file)
624 g_return_val_if_fail (G_IS_FILE (file), NULL);
626 iface = G_FILE_GET_IFACE (file);
628 return (* iface->get_uri) (file);
632 * g_file_get_parse_name:
633 * @file: input #GFile
635 * Gets the parse name of the @file.
636 * A parse name is a UTF-8 string that describes the
637 * file such that one can get the #GFile back using
638 * g_file_parse_name().
640 * This is generally used to show the #GFile as a nice
641 * full-pathname kind of string in a user interface,
642 * like in a location entry.
644 * For local files with names that can safely be converted
645 * to UTF-8 the pathname is used, otherwise the IRI is used
646 * (a form of URI that allows UTF-8 characters unescaped).
648 * This call does no blocking I/O.
650 * Returns: a string containing the #GFile's parse name.
651 * The returned string should be freed with g_free()
652 * when no longer needed.
655 g_file_get_parse_name (GFile *file)
659 g_return_val_if_fail (G_IS_FILE (file), NULL);
661 iface = G_FILE_GET_IFACE (file);
663 return (* iface->get_parse_name) (file);
668 * @file: input #GFile
670 * Duplicates a #GFile handle. This operation does not duplicate
671 * the actual file or directory represented by the #GFile; see
672 * g_file_copy() if attempting to copy a file.
674 * g_file_dup() is useful when a second handle is needed to the same underlying
675 * file, for use in a separate thread (#GFile is not thread-safe). For use
676 * within the same thread, use g_object_ref() to increment the existing object’s
679 * This call does no blocking I/O.
681 * Returns: (transfer full): a new #GFile that is a duplicate
682 * of the given #GFile.
685 g_file_dup (GFile *file)
689 g_return_val_if_fail (G_IS_FILE (file), NULL);
691 iface = G_FILE_GET_IFACE (file);
693 return (* iface->dup) (file);
698 * @file: (type GFile): #gconstpointer to a #GFile
700 * Creates a hash value for a #GFile.
702 * This call does no blocking I/O.
705 * Returns: 0 if @file is not a valid #GFile, otherwise an
706 * integer that can be used as hash value for the #GFile.
707 * This function is intended for easily hashing a #GFile to
708 * add to a #GHashTable or similar data structure.
711 g_file_hash (gconstpointer file)
715 g_return_val_if_fail (G_IS_FILE (file), 0);
717 iface = G_FILE_GET_IFACE (file);
719 return (* iface->hash) ((GFile *)file);
724 * @file1: the first #GFile
725 * @file2: the second #GFile
727 * Checks if the two given #GFiles refer to the same file.
729 * Note that two #GFiles that differ can still refer to the same
730 * file on the filesystem due to various forms of filename
733 * This call does no blocking I/O.
735 * Returns: %TRUE if @file1 and @file2 are equal.
738 g_file_equal (GFile *file1,
743 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
744 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
749 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
752 iface = G_FILE_GET_IFACE (file1);
754 return (* iface->equal) (file1, file2);
760 * @file: input #GFile
762 * Gets the parent directory for the @file.
763 * If the @file represents the root directory of the
764 * file system, then %NULL will be returned.
766 * This call does no blocking I/O.
768 * Returns: (nullable) (transfer full): a #GFile structure to the
769 * parent of the given #GFile or %NULL if there is no parent. Free
770 * the returned object with g_object_unref().
773 g_file_get_parent (GFile *file)
777 g_return_val_if_fail (G_IS_FILE (file), NULL);
779 iface = G_FILE_GET_IFACE (file);
781 return (* iface->get_parent) (file);
786 * @file: input #GFile
787 * @parent: (nullable): the parent to check for, or %NULL
789 * Checks if @file has a parent, and optionally, if it is @parent.
791 * If @parent is %NULL then this function returns %TRUE if @file has any
792 * parent at all. If @parent is non-%NULL then %TRUE is only returned
793 * if @file is an immediate child of @parent.
795 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
796 * the case that @parent is %NULL).
801 g_file_has_parent (GFile *file,
804 GFile *actual_parent;
807 g_return_val_if_fail (G_IS_FILE (file), FALSE);
808 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
810 actual_parent = g_file_get_parent (file);
812 if (actual_parent != NULL)
815 result = g_file_equal (parent, actual_parent);
819 g_object_unref (actual_parent);
829 * @file: input #GFile
830 * @name: (type filename): string containing the child's basename
832 * Gets a child of @file with basename equal to @name.
834 * Note that the file with that specific name might not exist, but
835 * you can still have a #GFile that points to it. You can use this
836 * for instance to create that file.
838 * This call does no blocking I/O.
840 * Returns: (transfer full): a #GFile to a child specified by @name.
841 * Free the returned object with g_object_unref().
844 g_file_get_child (GFile *file,
847 g_return_val_if_fail (G_IS_FILE (file), NULL);
848 g_return_val_if_fail (name != NULL, NULL);
850 return g_file_resolve_relative_path (file, name);
854 * g_file_get_child_for_display_name:
855 * @file: input #GFile
856 * @display_name: string to a possible child
857 * @error: return location for an error
859 * Gets the child of @file for a given @display_name (i.e. a UTF-8
860 * version of the name). If this function fails, it returns %NULL
861 * and @error will be set. This is very useful when constructing a
862 * #GFile for a new file and the user entered the filename in the
863 * user interface, for instance when you select a directory and
864 * type a filename in the file selector.
866 * This call does no blocking I/O.
868 * Returns: (transfer full): a #GFile to the specified child, or
869 * %NULL if the display name couldn't be converted.
870 * Free the returned object with g_object_unref().
873 g_file_get_child_for_display_name (GFile *file,
874 const char *display_name,
879 g_return_val_if_fail (G_IS_FILE (file), NULL);
880 g_return_val_if_fail (display_name != NULL, NULL);
882 iface = G_FILE_GET_IFACE (file);
884 return (* iface->get_child_for_display_name) (file, display_name, error);
889 * @file: input #GFile
890 * @prefix: input #GFile
892 * Checks whether @file has the prefix specified by @prefix.
894 * In other words, if the names of initial elements of @file's
895 * pathname match @prefix. Only full pathname elements are matched,
896 * so a path like /foo is not considered a prefix of /foobar, only
899 * A #GFile is not a prefix of itself. If you want to check for
900 * equality, use g_file_equal().
902 * This call does no I/O, as it works purely on names. As such it can
903 * sometimes return %FALSE even if @file is inside a @prefix (from a
904 * filesystem point of view), because the prefix of @file is an alias
907 * Virtual: prefix_matches
908 * Returns: %TRUE if the @file's parent, grandparent, etc is @prefix,
912 g_file_has_prefix (GFile *file,
917 g_return_val_if_fail (G_IS_FILE (file), FALSE);
918 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
920 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
923 iface = G_FILE_GET_IFACE (file);
925 /* The vtable function differs in arg order since
926 * we're using the old contains_file call
928 return (* iface->prefix_matches) (prefix, file);
932 * g_file_get_relative_path:
933 * @parent: input #GFile
934 * @descendant: input #GFile
936 * Gets the path for @descendant relative to @parent.
938 * This call does no blocking I/O.
940 * Returns: (type filename) (nullable): string with the relative path from
941 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
942 * prefix. The returned string should be freed with g_free() when
946 g_file_get_relative_path (GFile *parent,
951 g_return_val_if_fail (G_IS_FILE (parent), NULL);
952 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
954 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
957 iface = G_FILE_GET_IFACE (parent);
959 return (* iface->get_relative_path) (parent, descendant);
963 * g_file_resolve_relative_path:
964 * @file: input #GFile
965 * @relative_path: (type filename): a given relative path string
967 * Resolves a relative path for @file to an absolute path.
969 * This call does no blocking I/O.
971 * Returns: (transfer full): #GFile to the resolved path.
972 * %NULL if @relative_path is %NULL or if @file is invalid.
973 * Free the returned object with g_object_unref().
976 g_file_resolve_relative_path (GFile *file,
977 const char *relative_path)
981 g_return_val_if_fail (G_IS_FILE (file), NULL);
982 g_return_val_if_fail (relative_path != NULL, NULL);
984 iface = G_FILE_GET_IFACE (file);
986 return (* iface->resolve_relative_path) (file, relative_path);
990 * g_file_enumerate_children:
991 * @file: input #GFile
992 * @attributes: an attribute query string
993 * @flags: a set of #GFileQueryInfoFlags
994 * @cancellable: (nullable): optional #GCancellable object,
996 * @error: #GError for error reporting
998 * Gets the requested information about the files in a directory.
999 * The result is a #GFileEnumerator object that will give out
1000 * #GFileInfo objects for all the files in the directory.
1002 * The @attributes value is a string that specifies the file
1003 * attributes that should be gathered. It is not an error if
1004 * it's not possible to read a particular requested attribute
1005 * from a file - it just won't be set. @attributes should
1006 * be a comma-separated list of attributes or attribute wildcards.
1007 * The wildcard "*" means all attributes, and a wildcard like
1008 * "standard::*" means all attributes in the standard namespace.
1009 * An example attribute query be "standard::*,owner::user".
1010 * The standard attributes are available as defines, like
1011 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1013 * If @cancellable is not %NULL, then the operation can be cancelled
1014 * by triggering the cancellable object from another thread. If the
1015 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1018 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1019 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1020 * error will be returned. Other errors are possible too.
1022 * Returns: (transfer full): A #GFileEnumerator if successful,
1023 * %NULL on error. Free the returned object with g_object_unref().
1026 g_file_enumerate_children (GFile *file,
1027 const char *attributes,
1028 GFileQueryInfoFlags flags,
1029 GCancellable *cancellable,
1034 g_return_val_if_fail (G_IS_FILE (file), NULL);
1036 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1039 iface = G_FILE_GET_IFACE (file);
1041 if (iface->enumerate_children == NULL)
1043 g_set_error_literal (error, G_IO_ERROR,
1044 G_IO_ERROR_NOT_SUPPORTED,
1045 _("Operation not supported"));
1049 return (* iface->enumerate_children) (file, attributes, flags,
1050 cancellable, error);
1054 * g_file_enumerate_children_async:
1055 * @file: input #GFile
1056 * @attributes: an attribute query string
1057 * @flags: a set of #GFileQueryInfoFlags
1058 * @io_priority: the [I/O priority][io-priority] of the request
1059 * @cancellable: (nullable): optional #GCancellable object,
1061 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1062 * request is satisfied
1063 * @user_data: (closure): the data to pass to callback function
1065 * Asynchronously gets the requested information about the files
1066 * in a directory. The result is a #GFileEnumerator object that will
1067 * give out #GFileInfo objects for all the files in the directory.
1069 * For more details, see g_file_enumerate_children() which is
1070 * the synchronous version of this call.
1072 * When the operation is finished, @callback will be called. You can
1073 * then call g_file_enumerate_children_finish() to get the result of
1077 g_file_enumerate_children_async (GFile *file,
1078 const char *attributes,
1079 GFileQueryInfoFlags flags,
1081 GCancellable *cancellable,
1082 GAsyncReadyCallback callback,
1087 g_return_if_fail (G_IS_FILE (file));
1089 iface = G_FILE_GET_IFACE (file);
1090 (* iface->enumerate_children_async) (file,
1100 * g_file_enumerate_children_finish:
1101 * @file: input #GFile
1102 * @res: a #GAsyncResult
1105 * Finishes an async enumerate children operation.
1106 * See g_file_enumerate_children_async().
1108 * Returns: (transfer full): a #GFileEnumerator or %NULL
1109 * if an error occurred.
1110 * Free the returned object with g_object_unref().
1113 g_file_enumerate_children_finish (GFile *file,
1119 g_return_val_if_fail (G_IS_FILE (file), NULL);
1120 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1122 if (g_async_result_legacy_propagate_error (res, error))
1125 iface = G_FILE_GET_IFACE (file);
1126 return (* iface->enumerate_children_finish) (file, res, error);
1130 * g_file_query_exists:
1131 * @file: input #GFile
1132 * @cancellable: (nullable): optional #GCancellable object,
1135 * Utility function to check if a particular file exists. This is
1136 * implemented using g_file_query_info() and as such does blocking I/O.
1138 * 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)
1139 * and then execute something based on the outcome of that, because the
1140 * file might have been created or removed in between the operations. The
1141 * general approach to handling that is to not check, but just do the
1142 * operation and handle the errors as they come.
1144 * As an example of race-free checking, take the case of reading a file,
1145 * and if it doesn't exist, creating it. There are two racy versions: read
1146 * it, and on error create it; and: check if it exists, if not create it.
1147 * These can both result in two processes creating the file (with perhaps
1148 * a partially written file as the result). The correct approach is to
1149 * always try to create the file with g_file_create() which will either
1150 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1152 * However, in many cases an existence check is useful in a user interface,
1153 * for instance to make a menu item sensitive/insensitive, so that you don't
1154 * have to fool users that something is possible and then just show an error
1155 * dialog. If you do this, you should make sure to also handle the errors
1156 * that can happen due to races when you execute the operation.
1158 * Returns: %TRUE if the file exists (and can be detected without error),
1159 * %FALSE otherwise (or if cancelled).
1162 g_file_query_exists (GFile *file,
1163 GCancellable *cancellable)
1167 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1169 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1170 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1173 g_object_unref (info);
1181 * g_file_query_file_type:
1182 * @file: input #GFile
1183 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1184 * @cancellable: (nullable): optional #GCancellable object,
1187 * Utility function to inspect the #GFileType of a file. This is
1188 * implemented using g_file_query_info() and as such does blocking I/O.
1190 * The primary use case of this method is to check if a file is
1191 * a regular file, directory, or symlink.
1193 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1194 * if the file does not exist
1199 g_file_query_file_type (GFile *file,
1200 GFileQueryInfoFlags flags,
1201 GCancellable *cancellable)
1204 GFileType file_type;
1206 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1207 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1211 file_type = g_file_info_get_file_type (info);
1212 g_object_unref (info);
1215 file_type = G_FILE_TYPE_UNKNOWN;
1221 * g_file_query_info:
1222 * @file: input #GFile
1223 * @attributes: an attribute query string
1224 * @flags: a set of #GFileQueryInfoFlags
1225 * @cancellable: (nullable): optional #GCancellable object,
1229 * Gets the requested information about specified @file.
1230 * The result is a #GFileInfo object that contains key-value
1231 * attributes (such as the type or size of the file).
1233 * The @attributes value is a string that specifies the file
1234 * attributes that should be gathered. It is not an error if
1235 * it's not possible to read a particular requested attribute
1236 * from a file - it just won't be set. @attributes should be a
1237 * comma-separated list of attributes or attribute wildcards.
1238 * The wildcard "*" means all attributes, and a wildcard like
1239 * "standard::*" means all attributes in the standard namespace.
1240 * An example attribute query be "standard::*,owner::user".
1241 * The standard attributes are available as defines, like
1242 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1244 * If @cancellable is not %NULL, then the operation can be cancelled
1245 * by triggering the cancellable object from another thread. If the
1246 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1249 * For symlinks, normally the information about the target of the
1250 * symlink is returned, rather than information about the symlink
1251 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1252 * in @flags the information about the symlink itself will be returned.
1253 * Also, for symlinks that point to non-existing files the information
1254 * about the symlink itself will be returned.
1256 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1257 * returned. Other errors are possible too, and depend on what kind of
1258 * filesystem the file is on.
1260 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1261 * on error. Free the returned object with g_object_unref().
1264 g_file_query_info (GFile *file,
1265 const char *attributes,
1266 GFileQueryInfoFlags flags,
1267 GCancellable *cancellable,
1272 g_return_val_if_fail (G_IS_FILE (file), NULL);
1274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1277 iface = G_FILE_GET_IFACE (file);
1279 if (iface->query_info == NULL)
1281 g_set_error_literal (error, G_IO_ERROR,
1282 G_IO_ERROR_NOT_SUPPORTED,
1283 _("Operation not supported"));
1287 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1291 * g_file_query_info_async:
1292 * @file: input #GFile
1293 * @attributes: an attribute query string
1294 * @flags: a set of #GFileQueryInfoFlags
1295 * @io_priority: the [I/O priority][io-priority] of the request
1296 * @cancellable: (nullable): optional #GCancellable object,
1298 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1299 * request is satisfied
1300 * @user_data: (closure): the data to pass to callback function
1302 * Asynchronously gets the requested information about specified @file.
1303 * The result is a #GFileInfo object that contains key-value attributes
1304 * (such as type or size for the file).
1306 * For more details, see g_file_query_info() which is the synchronous
1307 * version of this call.
1309 * When the operation is finished, @callback will be called. You can
1310 * then call g_file_query_info_finish() to get the result of the operation.
1313 g_file_query_info_async (GFile *file,
1314 const char *attributes,
1315 GFileQueryInfoFlags flags,
1317 GCancellable *cancellable,
1318 GAsyncReadyCallback callback,
1323 g_return_if_fail (G_IS_FILE (file));
1325 iface = G_FILE_GET_IFACE (file);
1326 (* iface->query_info_async) (file,
1336 * g_file_query_info_finish:
1337 * @file: input #GFile
1338 * @res: a #GAsyncResult
1341 * Finishes an asynchronous file info query.
1342 * See g_file_query_info_async().
1344 * Returns: (transfer full): #GFileInfo for given @file
1345 * or %NULL on error. Free the returned object with
1349 g_file_query_info_finish (GFile *file,
1355 g_return_val_if_fail (G_IS_FILE (file), NULL);
1356 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1358 if (g_async_result_legacy_propagate_error (res, error))
1361 iface = G_FILE_GET_IFACE (file);
1362 return (* iface->query_info_finish) (file, res, error);
1366 * g_file_query_filesystem_info:
1367 * @file: input #GFile
1368 * @attributes: an attribute query string
1369 * @cancellable: (nullable): optional #GCancellable object,
1373 * Similar to g_file_query_info(), but obtains information
1374 * about the filesystem the @file is on, rather than the file itself.
1375 * For instance the amount of space available and the type of
1378 * The @attributes value is a string that specifies the attributes
1379 * that should be gathered. It is not an error if it's not possible
1380 * to read a particular requested attribute from a file - it just
1381 * won't be set. @attributes should be a comma-separated list of
1382 * attributes or attribute wildcards. The wildcard "*" means all
1383 * attributes, and a wildcard like "filesystem::*" means all attributes
1384 * in the filesystem namespace. The standard namespace for filesystem
1385 * attributes is "filesystem". Common attributes of interest are
1386 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1387 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1388 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1390 * If @cancellable is not %NULL, then the operation can be cancelled
1391 * by triggering the cancellable object from another thread. If the
1392 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1395 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1396 * be returned. Other errors are possible too, and depend on what
1397 * kind of filesystem the file is on.
1399 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1400 * Free the returned object with g_object_unref().
1403 g_file_query_filesystem_info (GFile *file,
1404 const char *attributes,
1405 GCancellable *cancellable,
1410 g_return_val_if_fail (G_IS_FILE (file), NULL);
1412 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1415 iface = G_FILE_GET_IFACE (file);
1417 if (iface->query_filesystem_info == NULL)
1419 g_set_error_literal (error, G_IO_ERROR,
1420 G_IO_ERROR_NOT_SUPPORTED,
1421 _("Operation not supported"));
1425 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1429 * g_file_query_filesystem_info_async:
1430 * @file: input #GFile
1431 * @attributes: an attribute query string
1432 * @io_priority: the [I/O priority][io-priority] of the request
1433 * @cancellable: (nullable): optional #GCancellable object,
1435 * @callback: (scope async): a #GAsyncReadyCallback to call
1436 * when the request is satisfied
1437 * @user_data: (closure): the data to pass to callback function
1439 * Asynchronously gets the requested information about the filesystem
1440 * that the specified @file is on. The result is a #GFileInfo object
1441 * that contains key-value attributes (such as type or size for the
1444 * For more details, see g_file_query_filesystem_info() which is the
1445 * synchronous version of this call.
1447 * When the operation is finished, @callback will be called. You can
1448 * then call g_file_query_info_finish() to get the result of the
1452 g_file_query_filesystem_info_async (GFile *file,
1453 const char *attributes,
1455 GCancellable *cancellable,
1456 GAsyncReadyCallback callback,
1461 g_return_if_fail (G_IS_FILE (file));
1463 iface = G_FILE_GET_IFACE (file);
1464 (* iface->query_filesystem_info_async) (file,
1473 * g_file_query_filesystem_info_finish:
1474 * @file: input #GFile
1475 * @res: a #GAsyncResult
1478 * Finishes an asynchronous filesystem info query.
1479 * See g_file_query_filesystem_info_async().
1481 * Returns: (transfer full): #GFileInfo for given @file
1482 * or %NULL on error.
1483 * Free the returned object with g_object_unref().
1486 g_file_query_filesystem_info_finish (GFile *file,
1492 g_return_val_if_fail (G_IS_FILE (file), NULL);
1493 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1495 if (g_async_result_legacy_propagate_error (res, error))
1498 iface = G_FILE_GET_IFACE (file);
1499 return (* iface->query_filesystem_info_finish) (file, res, error);
1503 * g_file_find_enclosing_mount:
1504 * @file: input #GFile
1505 * @cancellable: (nullable): optional #GCancellable object,
1509 * Gets a #GMount for the #GFile.
1511 * #GMount is returned only for user interesting locations, see
1512 * #GVolumeMonitor. If the #GFileIface for @file does not have a #mount,
1513 * @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL #will be returned.
1515 * If @cancellable is not %NULL, then the operation can be cancelled by
1516 * triggering the cancellable object from another thread. If the operation
1517 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1519 * Returns: (transfer full): a #GMount where the @file is located
1520 * or %NULL on error.
1521 * Free the returned object with g_object_unref().
1524 g_file_find_enclosing_mount (GFile *file,
1525 GCancellable *cancellable,
1530 g_return_val_if_fail (G_IS_FILE (file), NULL);
1532 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1535 iface = G_FILE_GET_IFACE (file);
1536 if (iface->find_enclosing_mount == NULL)
1539 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1540 /* Translators: This is an error message when
1541 * trying to find the enclosing (user visible)
1542 * mount of a file, but none exists.
1544 _("Containing mount does not exist"));
1548 return (* iface->find_enclosing_mount) (file, cancellable, error);
1552 * g_file_find_enclosing_mount_async:
1554 * @io_priority: the [I/O priority][io-priority] of the request
1555 * @cancellable: (nullable): optional #GCancellable object,
1557 * @callback: (scope async): a #GAsyncReadyCallback to call
1558 * when the request is satisfied
1559 * @user_data: (closure): the data to pass to callback function
1561 * Asynchronously gets the mount for the file.
1563 * For more details, see g_file_find_enclosing_mount() which is
1564 * the synchronous version of this call.
1566 * When the operation is finished, @callback will be called.
1567 * You can then call g_file_find_enclosing_mount_finish() to
1568 * get the result of the operation.
1571 g_file_find_enclosing_mount_async (GFile *file,
1573 GCancellable *cancellable,
1574 GAsyncReadyCallback callback,
1579 g_return_if_fail (G_IS_FILE (file));
1581 iface = G_FILE_GET_IFACE (file);
1582 (* iface->find_enclosing_mount_async) (file,
1590 * g_file_find_enclosing_mount_finish:
1592 * @res: a #GAsyncResult
1595 * Finishes an asynchronous find mount request.
1596 * See g_file_find_enclosing_mount_async().
1598 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1599 * Free the returned object with g_object_unref().
1602 g_file_find_enclosing_mount_finish (GFile *file,
1608 g_return_val_if_fail (G_IS_FILE (file), NULL);
1609 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1611 if (g_async_result_legacy_propagate_error (res, error))
1614 iface = G_FILE_GET_IFACE (file);
1615 return (* iface->find_enclosing_mount_finish) (file, res, error);
1621 * @file: #GFile to read
1622 * @cancellable: (nullable): a #GCancellable
1623 * @error: a #GError, or %NULL
1625 * Opens a file for reading. The result is a #GFileInputStream that
1626 * can be used to read the contents of the file.
1628 * If @cancellable is not %NULL, then the operation can be cancelled by
1629 * triggering the cancellable object from another thread. If the operation
1630 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1632 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1633 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1634 * error will be returned. Other errors are possible too, and depend
1635 * on what kind of filesystem the file is on.
1638 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1639 * Free the returned object with g_object_unref().
1642 g_file_read (GFile *file,
1643 GCancellable *cancellable,
1648 g_return_val_if_fail (G_IS_FILE (file), NULL);
1650 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1653 iface = G_FILE_GET_IFACE (file);
1655 if (iface->read_fn == NULL)
1657 g_set_error_literal (error, G_IO_ERROR,
1658 G_IO_ERROR_NOT_SUPPORTED,
1659 _("Operation not supported"));
1663 return (* iface->read_fn) (file, cancellable, error);
1668 * @file: input #GFile
1669 * @flags: a set of #GFileCreateFlags
1670 * @cancellable: (nullable): optional #GCancellable object,
1672 * @error: a #GError, or %NULL
1674 * Gets an output stream for appending data to the file.
1675 * If the file doesn't already exist it is created.
1677 * By default files created are generally readable by everyone,
1678 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1679 * will be made readable only to the current user, to the level that
1680 * is supported on the target filesystem.
1682 * If @cancellable is not %NULL, then the operation can be cancelled
1683 * by triggering the cancellable object from another thread. If the
1684 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1687 * Some file systems don't allow all file names, and may return an
1688 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1689 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1690 * possible too, and depend on what kind of filesystem the file is on.
1692 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1693 * Free the returned object with g_object_unref().
1696 g_file_append_to (GFile *file,
1697 GFileCreateFlags flags,
1698 GCancellable *cancellable,
1703 g_return_val_if_fail (G_IS_FILE (file), NULL);
1705 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1708 iface = G_FILE_GET_IFACE (file);
1710 if (iface->append_to == NULL)
1712 g_set_error_literal (error, G_IO_ERROR,
1713 G_IO_ERROR_NOT_SUPPORTED,
1714 _("Operation not supported"));
1718 return (* iface->append_to) (file, flags, cancellable, error);
1723 * @file: input #GFile
1724 * @flags: a set of #GFileCreateFlags
1725 * @cancellable: (nullable): optional #GCancellable object,
1727 * @error: a #GError, or %NULL
1729 * Creates a new file and returns an output stream for writing to it.
1730 * The file must not already exist.
1732 * By default files created are generally readable by everyone,
1733 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1734 * will be made readable only to the current user, to the level
1735 * that is supported on the target filesystem.
1737 * If @cancellable is not %NULL, then the operation can be cancelled
1738 * by triggering the cancellable object from another thread. If the
1739 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1742 * If a file or directory with this name already exists the
1743 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1744 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1745 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1746 * be returned. Other errors are possible too, and depend on what kind
1747 * of filesystem the file is on.
1749 * Returns: (transfer full): a #GFileOutputStream for the newly created
1750 * file, or %NULL on error.
1751 * Free the returned object with g_object_unref().
1754 g_file_create (GFile *file,
1755 GFileCreateFlags flags,
1756 GCancellable *cancellable,
1761 g_return_val_if_fail (G_IS_FILE (file), NULL);
1763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1766 iface = G_FILE_GET_IFACE (file);
1768 if (iface->create == NULL)
1770 g_set_error_literal (error, G_IO_ERROR,
1771 G_IO_ERROR_NOT_SUPPORTED,
1772 _("Operation not supported"));
1776 return (* iface->create) (file, flags, cancellable, error);
1781 * @file: input #GFile
1782 * @etag: (nullable): an optional [entity tag][gfile-etag]
1783 * for the current #GFile, or #NULL to ignore
1784 * @make_backup: %TRUE if a backup should be created
1785 * @flags: a set of #GFileCreateFlags
1786 * @cancellable: (nullable): optional #GCancellable object,
1788 * @error: a #GError, or %NULL
1790 * Returns an output stream for overwriting the file, possibly
1791 * creating a backup copy of the file first. If the file doesn't exist,
1792 * it will be created.
1794 * This will try to replace the file in the safest way possible so
1795 * that any errors during the writing will not affect an already
1796 * existing copy of the file. For instance, for local files it
1797 * may write to a temporary file and then atomically rename over
1798 * the destination when the stream is closed.
1800 * By default files created are generally readable by everyone,
1801 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1802 * will be made readable only to the current user, to the level that
1803 * is supported on the target filesystem.
1805 * If @cancellable is not %NULL, then the operation can be cancelled
1806 * by triggering the cancellable object from another thread. If the
1807 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1810 * If you pass in a non-%NULL @etag value and @file already exists, then
1811 * this value is compared to the current entity tag of the file, and if
1812 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1813 * generally means that the file has been changed since you last read
1814 * it. You can get the new etag from g_file_output_stream_get_etag()
1815 * after you've finished writing and closed the #GFileOutputStream. When
1816 * you load a new file you can use g_file_input_stream_query_info() to
1817 * get the etag of the file.
1819 * If @make_backup is %TRUE, this function will attempt to make a
1820 * backup of the current file before overwriting it. If this fails
1821 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1822 * want to replace anyway, try again with @make_backup set to %FALSE.
1824 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1825 * be returned, and if the file is some other form of non-regular file
1826 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1827 * file systems don't allow all file names, and may return an
1828 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1829 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1830 * possible too, and depend on what kind of filesystem the file is on.
1832 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1833 * Free the returned object with g_object_unref().
1836 g_file_replace (GFile *file,
1838 gboolean make_backup,
1839 GFileCreateFlags flags,
1840 GCancellable *cancellable,
1845 g_return_val_if_fail (G_IS_FILE (file), NULL);
1847 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1850 iface = G_FILE_GET_IFACE (file);
1852 if (iface->replace == NULL)
1854 g_set_error_literal (error, G_IO_ERROR,
1855 G_IO_ERROR_NOT_SUPPORTED,
1856 _("Operation not supported"));
1860 /* Handle empty tag string as NULL in consistent way. */
1861 if (etag && *etag == 0)
1864 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1868 * g_file_open_readwrite:
1869 * @file: #GFile to open
1870 * @cancellable: (nullable): a #GCancellable
1871 * @error: a #GError, or %NULL
1873 * Opens an existing file for reading and writing. The result is
1874 * a #GFileIOStream that can be used to read and write the contents
1877 * If @cancellable is not %NULL, then the operation can be cancelled
1878 * by triggering the cancellable object from another thread. If the
1879 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1882 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1883 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1884 * error will be returned. Other errors are possible too, and depend on
1885 * what kind of filesystem the file is on. Note that in many non-local
1886 * file cases read and write streams are not supported, so make sure you
1887 * really need to do read and write streaming, rather than just opening
1888 * for reading or writing.
1890 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1891 * Free the returned object with g_object_unref().
1896 g_file_open_readwrite (GFile *file,
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->open_readwrite == NULL)
1911 g_set_error_literal (error, G_IO_ERROR,
1912 G_IO_ERROR_NOT_SUPPORTED,
1913 _("Operation not supported"));
1917 return (* iface->open_readwrite) (file, cancellable, error);
1921 * g_file_create_readwrite:
1923 * @flags: a set of #GFileCreateFlags
1924 * @cancellable: (nullable): optional #GCancellable object,
1926 * @error: return location for a #GError, or %NULL
1928 * Creates a new file and returns a stream for reading and
1929 * writing to it. The file must not already exist.
1931 * By default files created are generally readable by everyone,
1932 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1933 * will be made readable only to the current user, to the level
1934 * that is supported on the target filesystem.
1936 * If @cancellable is not %NULL, then the operation can be cancelled
1937 * by triggering the cancellable object from another thread. If the
1938 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1941 * If a file or directory with this name already exists, the
1942 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1943 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1944 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1945 * will be returned. Other errors are possible too, and depend on what
1946 * kind of filesystem the file is on.
1948 * Note that in many non-local file cases read and write streams are
1949 * not supported, so make sure you really need to do read and write
1950 * streaming, rather than just opening for reading or writing.
1952 * Returns: (transfer full): a #GFileIOStream for the newly created
1953 * file, or %NULL on error.
1954 * Free the returned object with g_object_unref().
1959 g_file_create_readwrite (GFile *file,
1960 GFileCreateFlags flags,
1961 GCancellable *cancellable,
1966 g_return_val_if_fail (G_IS_FILE (file), NULL);
1968 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1971 iface = G_FILE_GET_IFACE (file);
1973 if (iface->create_readwrite == NULL)
1975 g_set_error_literal (error, G_IO_ERROR,
1976 G_IO_ERROR_NOT_SUPPORTED,
1977 _("Operation not supported"));
1981 return (* iface->create_readwrite) (file, flags, cancellable, error);
1985 * g_file_replace_readwrite:
1987 * @etag: (nullable): an optional [entity tag][gfile-etag]
1988 * for the current #GFile, or #NULL to ignore
1989 * @make_backup: %TRUE if a backup should be created
1990 * @flags: a set of #GFileCreateFlags
1991 * @cancellable: (nullable): optional #GCancellable object,
1993 * @error: return location for a #GError, or %NULL
1995 * Returns an output stream for overwriting the file in readwrite mode,
1996 * possibly creating a backup copy of the file first. If the file doesn't
1997 * exist, it will be created.
1999 * For details about the behaviour, see g_file_replace() which does the
2000 * same thing but returns an output stream only.
2002 * Note that in many non-local file cases read and write streams are not
2003 * supported, so make sure you really need to do read and write streaming,
2004 * rather than just opening for reading or writing.
2006 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2007 * Free the returned object with g_object_unref().
2012 g_file_replace_readwrite (GFile *file,
2014 gboolean make_backup,
2015 GFileCreateFlags flags,
2016 GCancellable *cancellable,
2021 g_return_val_if_fail (G_IS_FILE (file), NULL);
2023 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2026 iface = G_FILE_GET_IFACE (file);
2028 if (iface->replace_readwrite == NULL)
2030 g_set_error_literal (error, G_IO_ERROR,
2031 G_IO_ERROR_NOT_SUPPORTED,
2032 _("Operation not supported"));
2036 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2040 * g_file_read_async:
2041 * @file: input #GFile
2042 * @io_priority: the [I/O priority][io-priority] of the request
2043 * @cancellable: (nullable): optional #GCancellable object,
2045 * @callback: (scope async): a #GAsyncReadyCallback to call
2046 * when the request is satisfied
2047 * @user_data: (closure): the data to pass to callback function
2049 * Asynchronously opens @file for reading.
2051 * For more details, see g_file_read() which is
2052 * the synchronous version of this call.
2054 * When the operation is finished, @callback will be called.
2055 * You can then call g_file_read_finish() to get the result
2059 g_file_read_async (GFile *file,
2061 GCancellable *cancellable,
2062 GAsyncReadyCallback callback,
2067 g_return_if_fail (G_IS_FILE (file));
2069 iface = G_FILE_GET_IFACE (file);
2070 (* iface->read_async) (file,
2078 * g_file_read_finish:
2079 * @file: input #GFile
2080 * @res: a #GAsyncResult
2081 * @error: a #GError, or %NULL
2083 * Finishes an asynchronous file read operation started with
2084 * g_file_read_async().
2086 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2087 * Free the returned object with g_object_unref().
2090 g_file_read_finish (GFile *file,
2096 g_return_val_if_fail (G_IS_FILE (file), NULL);
2097 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2099 if (g_async_result_legacy_propagate_error (res, error))
2102 iface = G_FILE_GET_IFACE (file);
2103 return (* iface->read_finish) (file, res, error);
2107 * g_file_append_to_async:
2108 * @file: input #GFile
2109 * @flags: a set of #GFileCreateFlags
2110 * @io_priority: the [I/O priority][io-priority] of the request
2111 * @cancellable: (nullable): optional #GCancellable object,
2113 * @callback: (scope async): a #GAsyncReadyCallback to call
2114 * when the request is satisfied
2115 * @user_data: (closure): the data to pass to callback function
2117 * Asynchronously opens @file for appending.
2119 * For more details, see g_file_append_to() which is
2120 * the synchronous version of this call.
2122 * When the operation is finished, @callback will be called.
2123 * You can then call g_file_append_to_finish() to get the result
2127 g_file_append_to_async (GFile *file,
2128 GFileCreateFlags flags,
2130 GCancellable *cancellable,
2131 GAsyncReadyCallback callback,
2136 g_return_if_fail (G_IS_FILE (file));
2138 iface = G_FILE_GET_IFACE (file);
2139 (* iface->append_to_async) (file,
2148 * g_file_append_to_finish:
2149 * @file: input #GFile
2150 * @res: #GAsyncResult
2151 * @error: a #GError, or %NULL
2153 * Finishes an asynchronous file append operation started with
2154 * g_file_append_to_async().
2156 * Returns: (transfer full): a valid #GFileOutputStream
2157 * or %NULL on error.
2158 * Free the returned object with g_object_unref().
2161 g_file_append_to_finish (GFile *file,
2167 g_return_val_if_fail (G_IS_FILE (file), NULL);
2168 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2170 if (g_async_result_legacy_propagate_error (res, error))
2173 iface = G_FILE_GET_IFACE (file);
2174 return (* iface->append_to_finish) (file, res, error);
2178 * g_file_create_async:
2179 * @file: input #GFile
2180 * @flags: a set of #GFileCreateFlags
2181 * @io_priority: the [I/O priority][io-priority] of the request
2182 * @cancellable: (nullable): optional #GCancellable object,
2184 * @callback: (scope async): a #GAsyncReadyCallback to call
2185 * when the request is satisfied
2186 * @user_data: (closure): the data to pass to callback function
2188 * Asynchronously creates a new file and returns an output stream
2189 * for writing to it. The file must not already exist.
2191 * For more details, see g_file_create() which is
2192 * the synchronous version of this call.
2194 * When the operation is finished, @callback will be called.
2195 * You can then call g_file_create_finish() to get the result
2199 g_file_create_async (GFile *file,
2200 GFileCreateFlags flags,
2202 GCancellable *cancellable,
2203 GAsyncReadyCallback callback,
2208 g_return_if_fail (G_IS_FILE (file));
2210 iface = G_FILE_GET_IFACE (file);
2211 (* iface->create_async) (file,
2220 * g_file_create_finish:
2221 * @file: input #GFile
2222 * @res: a #GAsyncResult
2223 * @error: a #GError, or %NULL
2225 * Finishes an asynchronous file create operation started with
2226 * g_file_create_async().
2228 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2229 * Free the returned object with g_object_unref().
2232 g_file_create_finish (GFile *file,
2238 g_return_val_if_fail (G_IS_FILE (file), NULL);
2239 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2241 if (g_async_result_legacy_propagate_error (res, error))
2244 iface = G_FILE_GET_IFACE (file);
2245 return (* iface->create_finish) (file, res, error);
2249 * g_file_replace_async:
2250 * @file: input #GFile
2251 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2252 * or %NULL to ignore
2253 * @make_backup: %TRUE if a backup should be created
2254 * @flags: a set of #GFileCreateFlags
2255 * @io_priority: the [I/O priority][io-priority] of the request
2256 * @cancellable: (nullable): optional #GCancellable object,
2258 * @callback: (scope async): a #GAsyncReadyCallback to call
2259 * when the request is satisfied
2260 * @user_data: (closure): the data to pass to callback function
2262 * Asynchronously overwrites the file, replacing the contents,
2263 * possibly creating a backup copy of the file first.
2265 * For more details, see g_file_replace() which is
2266 * the synchronous version of this call.
2268 * When the operation is finished, @callback will be called.
2269 * You can then call g_file_replace_finish() to get the result
2273 g_file_replace_async (GFile *file,
2275 gboolean make_backup,
2276 GFileCreateFlags flags,
2278 GCancellable *cancellable,
2279 GAsyncReadyCallback callback,
2284 g_return_if_fail (G_IS_FILE (file));
2286 iface = G_FILE_GET_IFACE (file);
2287 (* iface->replace_async) (file,
2298 * g_file_replace_finish:
2299 * @file: input #GFile
2300 * @res: a #GAsyncResult
2301 * @error: a #GError, or %NULL
2303 * Finishes an asynchronous file replace operation started with
2304 * g_file_replace_async().
2306 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2307 * Free the returned object with g_object_unref().
2310 g_file_replace_finish (GFile *file,
2316 g_return_val_if_fail (G_IS_FILE (file), NULL);
2317 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2319 if (g_async_result_legacy_propagate_error (res, error))
2322 iface = G_FILE_GET_IFACE (file);
2323 return (* iface->replace_finish) (file, res, error);
2327 * g_file_open_readwrite_async
2328 * @file: input #GFile
2329 * @io_priority: the [I/O priority][io-priority] of the request
2330 * @cancellable: (nullable): optional #GCancellable object,
2332 * @callback: (scope async): a #GAsyncReadyCallback to call
2333 * when the request is satisfied
2334 * @user_data: (closure): the data to pass to callback function
2336 * Asynchronously opens @file for reading and writing.
2338 * For more details, see g_file_open_readwrite() which is
2339 * the synchronous version of this call.
2341 * When the operation is finished, @callback will be called.
2342 * You can then call g_file_open_readwrite_finish() to get
2343 * the result of the operation.
2348 g_file_open_readwrite_async (GFile *file,
2350 GCancellable *cancellable,
2351 GAsyncReadyCallback callback,
2356 g_return_if_fail (G_IS_FILE (file));
2358 iface = G_FILE_GET_IFACE (file);
2359 (* iface->open_readwrite_async) (file,
2367 * g_file_open_readwrite_finish:
2368 * @file: input #GFile
2369 * @res: a #GAsyncResult
2370 * @error: a #GError, or %NULL
2372 * Finishes an asynchronous file read operation started with
2373 * g_file_open_readwrite_async().
2375 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2376 * Free the returned object with g_object_unref().
2381 g_file_open_readwrite_finish (GFile *file,
2387 g_return_val_if_fail (G_IS_FILE (file), NULL);
2388 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2390 if (g_async_result_legacy_propagate_error (res, error))
2393 iface = G_FILE_GET_IFACE (file);
2394 return (* iface->open_readwrite_finish) (file, res, error);
2398 * g_file_create_readwrite_async:
2399 * @file: input #GFile
2400 * @flags: a set of #GFileCreateFlags
2401 * @io_priority: the [I/O priority][io-priority] of the request
2402 * @cancellable: (nullable): optional #GCancellable object,
2404 * @callback: (scope async): a #GAsyncReadyCallback to call
2405 * when the request is satisfied
2406 * @user_data: (closure): the data to pass to callback function
2408 * Asynchronously creates a new file and returns a stream
2409 * for reading and writing to it. The file must not already exist.
2411 * For more details, see g_file_create_readwrite() which is
2412 * the synchronous version of this call.
2414 * When the operation is finished, @callback will be called.
2415 * You can then call g_file_create_readwrite_finish() to get
2416 * the result of the operation.
2421 g_file_create_readwrite_async (GFile *file,
2422 GFileCreateFlags flags,
2424 GCancellable *cancellable,
2425 GAsyncReadyCallback callback,
2430 g_return_if_fail (G_IS_FILE (file));
2432 iface = G_FILE_GET_IFACE (file);
2433 (* iface->create_readwrite_async) (file,
2442 * g_file_create_readwrite_finish:
2443 * @file: input #GFile
2444 * @res: a #GAsyncResult
2445 * @error: a #GError, or %NULL
2447 * Finishes an asynchronous file create operation started with
2448 * g_file_create_readwrite_async().
2450 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2451 * Free the returned object with g_object_unref().
2456 g_file_create_readwrite_finish (GFile *file,
2462 g_return_val_if_fail (G_IS_FILE (file), NULL);
2463 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2465 if (g_async_result_legacy_propagate_error (res, error))
2468 iface = G_FILE_GET_IFACE (file);
2469 return (* iface->create_readwrite_finish) (file, res, error);
2473 * g_file_replace_readwrite_async:
2474 * @file: input #GFile
2475 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2476 * or %NULL to ignore
2477 * @make_backup: %TRUE if a backup should be created
2478 * @flags: a set of #GFileCreateFlags
2479 * @io_priority: the [I/O priority][io-priority] of the request
2480 * @cancellable: (nullable): optional #GCancellable object,
2482 * @callback: (scope async): a #GAsyncReadyCallback to call
2483 * when the request is satisfied
2484 * @user_data: (closure): the data to pass to callback function
2486 * Asynchronously overwrites the file in read-write mode,
2487 * replacing the contents, possibly creating a backup copy
2488 * of the file first.
2490 * For more details, see g_file_replace_readwrite() which is
2491 * the synchronous version of this call.
2493 * When the operation is finished, @callback will be called.
2494 * You can then call g_file_replace_readwrite_finish() to get
2495 * the result of the operation.
2500 g_file_replace_readwrite_async (GFile *file,
2502 gboolean make_backup,
2503 GFileCreateFlags flags,
2505 GCancellable *cancellable,
2506 GAsyncReadyCallback callback,
2511 g_return_if_fail (G_IS_FILE (file));
2513 iface = G_FILE_GET_IFACE (file);
2514 (* iface->replace_readwrite_async) (file,
2525 * g_file_replace_readwrite_finish:
2526 * @file: input #GFile
2527 * @res: a #GAsyncResult
2528 * @error: a #GError, or %NULL
2530 * Finishes an asynchronous file replace operation started with
2531 * g_file_replace_readwrite_async().
2533 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2534 * Free the returned object with g_object_unref().
2539 g_file_replace_readwrite_finish (GFile *file,
2545 g_return_val_if_fail (G_IS_FILE (file), NULL);
2546 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2548 if (g_async_result_legacy_propagate_error (res, error))
2551 iface = G_FILE_GET_IFACE (file);
2552 return (* iface->replace_readwrite_finish) (file, res, error);
2556 copy_symlink (GFile *destination,
2557 GFileCopyFlags flags,
2558 GCancellable *cancellable,
2563 gboolean tried_delete;
2565 GFileType file_type;
2567 tried_delete = FALSE;
2571 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2573 /* Maybe it already existed, and we want to overwrite? */
2574 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2575 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2577 g_clear_error (&my_error);
2579 /* Don't overwrite if the destination is a directory */
2580 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2581 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2582 cancellable, &my_error);
2585 file_type = g_file_info_get_file_type (info);
2586 g_object_unref (info);
2588 if (file_type == G_FILE_TYPE_DIRECTORY)
2590 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2591 _("Can’t copy over directory"));
2596 if (!g_file_delete (destination, cancellable, error))
2599 tried_delete = TRUE;
2603 g_propagate_error (error, my_error);
2610 static GFileInputStream *
2611 open_source_for_copy (GFile *source,
2613 GFileCopyFlags flags,
2614 GCancellable *cancellable,
2618 GFileInputStream *ret;
2620 GFileType file_type;
2623 ret = g_file_read (source, cancellable, &my_error);
2627 /* There was an error opening the source, try to set a good error for it: */
2628 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2630 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2631 * as that is less useful to the app. Better check for errors on the
2634 g_error_free (my_error);
2637 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2638 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2639 cancellable, &my_error);
2641 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2643 file_type = g_file_info_get_file_type (info);
2644 g_object_unref (info);
2646 if (flags & G_FILE_COPY_OVERWRITE)
2648 if (file_type == G_FILE_TYPE_DIRECTORY)
2650 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2651 _("Can’t copy directory over directory"));
2654 /* continue to would_recurse error */
2658 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2659 _("Target file exists"));
2665 /* Error getting info from target, return that error
2666 * (except for NOT_FOUND, which is no error here)
2668 g_clear_object (&info);
2669 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2671 g_propagate_error (error, my_error);
2674 g_clear_error (&my_error);
2677 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2678 _("Can’t recursively copy directory"));
2682 g_propagate_error (error, my_error);
2687 should_copy (GFileAttributeInfo *info,
2688 gboolean copy_all_attributes,
2689 gboolean skip_perms)
2691 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2694 if (copy_all_attributes)
2695 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2696 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2700 build_attribute_list_for_copy (GFile *file,
2701 GFileCopyFlags flags,
2702 char **out_attributes,
2703 GCancellable *cancellable,
2706 gboolean ret = FALSE;
2707 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2711 gboolean copy_all_attributes;
2712 gboolean skip_perms;
2714 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2715 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2717 /* Ignore errors here, if the target supports no attributes there is
2718 * nothing to copy. We still honor the cancellable though.
2720 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2721 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2724 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2725 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2728 if (attributes == NULL && namespaces == NULL)
2732 s = g_string_new ("");
2736 for (i = 0; i < attributes->n_infos; i++)
2738 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2743 g_string_append_c (s, ',');
2745 g_string_append (s, attributes->infos[i].name);
2752 for (i = 0; i < namespaces->n_infos; i++)
2754 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2759 g_string_append_c (s, ',');
2761 g_string_append (s, namespaces->infos[i].name);
2762 g_string_append (s, "::*");
2768 *out_attributes = g_string_free (s, FALSE);
2772 g_string_free (s, TRUE);
2774 g_file_attribute_info_list_unref (attributes);
2776 g_file_attribute_info_list_unref (namespaces);
2782 * g_file_copy_attributes:
2783 * @source: a #GFile with attributes
2784 * @destination: a #GFile to copy attributes to
2785 * @flags: a set of #GFileCopyFlags
2786 * @cancellable: (nullable): optional #GCancellable object,
2788 * @error: a #GError, %NULL to ignore
2790 * Copies the file attributes from @source to @destination.
2792 * Normally only a subset of the file attributes are copied,
2793 * those that are copies in a normal file copy operation
2794 * (which for instance does not include e.g. owner). However
2795 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2796 * all the metadata that is possible to copy is copied. This
2797 * is useful when implementing move by copy + delete source.
2799 * Returns: %TRUE if the attributes were copied successfully,
2803 g_file_copy_attributes (GFile *source,
2805 GFileCopyFlags flags,
2806 GCancellable *cancellable,
2809 char *attrs_to_read;
2812 gboolean source_nofollow_symlinks;
2814 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2815 cancellable, error))
2818 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2820 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2821 * we just don't copy it.
2823 info = g_file_query_info (source, attrs_to_read,
2824 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2828 g_free (attrs_to_read);
2833 res = g_file_set_attributes_from_info (destination,
2835 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2838 g_object_unref (info);
2844 /* 256k minus malloc overhead */
2845 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2848 copy_stream_with_progress (GInputStream *in,
2851 GCancellable *cancellable,
2852 GFileProgressCallback progress_callback,
2853 gpointer progress_callback_data,
2858 goffset current_size;
2865 /* avoid performance impact of querying total size when it's not needed */
2866 if (progress_callback)
2868 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2869 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2873 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2874 total_size = g_file_info_get_size (info);
2875 g_object_unref (info);
2878 if (total_size == -1)
2880 info = g_file_query_info (source,
2881 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2882 G_FILE_QUERY_INFO_NONE,
2886 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2887 total_size = g_file_info_get_size (info);
2888 g_object_unref (info);
2893 if (total_size == -1)
2896 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2901 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2911 current_size += n_read;
2913 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2917 if (progress_callback)
2918 progress_callback (current_size, total_size, progress_callback_data);
2922 /* Make sure we send full copied size */
2923 if (progress_callback)
2924 progress_callback (current_size, total_size, progress_callback_data);
2932 do_splice (int fd_in,
2937 long *bytes_transferd,
2943 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2951 else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
2952 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2953 _("Splice not supported"));
2955 g_set_error (error, G_IO_ERROR,
2956 g_io_error_from_errno (errsv),
2957 _("Error splicing file: %s"),
2958 g_strerror (errsv));
2963 *bytes_transferd = result;
2968 splice_stream_with_progress (GInputStream *in,
2970 GCancellable *cancellable,
2971 GFileProgressCallback progress_callback,
2972 gpointer progress_callback_data,
2975 int buffer[2] = { -1, -1 };
2983 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2984 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2986 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2989 #if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
2990 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
2991 * pipe size. See: https://bugzilla.gnome.org/791457 */
2992 buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
2993 if (buffer_size <= 0)
2996 buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
2999 if (buffer_size <= 0)
3001 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3002 _("Error splicing file: %s"), g_strerror (errsv));
3008 /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3009 * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
3010 * running on Linux < 2.6.11 (where the capacity was the system page size,
3011 * typically 4KiB) because it’s ancient. See pipe(7). */
3012 buffer_size = 1024 * 64;
3015 g_assert (buffer_size > 0);
3018 /* avoid performance impact of querying total size when it's not needed */
3019 if (progress_callback)
3023 if (fstat (fd_in, &sbuf) == 0)
3024 total_size = sbuf.st_size;
3027 if (total_size == -1)
3030 offset_in = offset_out = 0;
3037 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3040 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3051 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3054 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3057 n_read -= n_written;
3060 if (progress_callback)
3061 progress_callback (offset_in, total_size, progress_callback_data);
3064 /* Make sure we send full copied size */
3065 if (progress_callback)
3066 progress_callback (offset_in, total_size, progress_callback_data);
3068 if (!g_close (buffer[0], error))
3071 if (!g_close (buffer[1], error))
3075 if (buffer[0] != -1)
3076 (void) g_close (buffer[0], NULL);
3077 if (buffer[1] != -1)
3078 (void) g_close (buffer[1], NULL);
3086 btrfs_reflink_with_progress (GInputStream *in,
3089 GCancellable *cancellable,
3090 GFileProgressCallback progress_callback,
3091 gpointer progress_callback_data,
3094 goffset source_size;
3098 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3099 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3101 if (progress_callback)
3102 source_size = g_file_info_get_size (info);
3104 /* Btrfs clone ioctl properties:
3105 * - Works at the inode level
3106 * - Doesn't work with directories
3107 * - Always follows symlinks (source and destination)
3109 * By the time we get here, *in and *out are both regular files */
3110 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3116 g_set_error_literal (error, G_IO_ERROR,
3117 G_IO_ERROR_NOT_SUPPORTED,
3118 _("Copy (reflink/clone) between mounts is not supported"));
3119 else if (errsv == EINVAL)
3120 g_set_error_literal (error, G_IO_ERROR,
3121 G_IO_ERROR_NOT_SUPPORTED,
3122 _("Copy (reflink/clone) is not supported or invalid"));
3124 /* Most probably something odd happened; retry with fallback */
3125 g_set_error_literal (error, G_IO_ERROR,
3126 G_IO_ERROR_NOT_SUPPORTED,
3127 _("Copy (reflink/clone) is not supported or didn’t work"));
3128 /* We retry with fallback for all error cases because Btrfs is currently
3129 * unstable, and so we can't trust it to do clone properly.
3130 * In addition, any hard errors here would cause the same failure in the
3131 * fallback manual copy as well. */
3135 /* Make sure we send full copied size */
3136 if (progress_callback)
3137 progress_callback (source_size, source_size, progress_callback_data);
3144 file_copy_fallback (GFile *source,
3146 GFileCopyFlags flags,
3147 GCancellable *cancellable,
3148 GFileProgressCallback progress_callback,
3149 gpointer progress_callback_data,
3152 gboolean ret = FALSE;
3153 GFileInputStream *file_in = NULL;
3154 GInputStream *in = NULL;
3155 GOutputStream *out = NULL;
3156 GFileInfo *info = NULL;
3158 char *attrs_to_read;
3159 gboolean do_set_attributes = FALSE;
3160 GFileCreateFlags create_flags;
3162 /* need to know the file type */
3163 info = g_file_query_info (source,
3164 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3165 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3171 /* Maybe copy the symlink? */
3172 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3173 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3175 target = g_file_info_get_symlink_target (info);
3178 if (!copy_symlink (destination, flags, cancellable, target, error))
3184 /* ... else fall back on a regular file copy */
3186 /* Handle "special" files (pipes, device nodes, ...)? */
3187 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3189 /* FIXME: could try to recreate device nodes and others? */
3190 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3191 _("Can’t copy special file"));
3195 /* Everything else should just fall back on a regular copy. */
3197 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3200 in = G_INPUT_STREAM (file_in);
3202 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3203 cancellable, error))
3206 if (attrs_to_read != NULL)
3208 GError *tmp_error = NULL;
3210 /* Ok, ditch the previous lightweight info (on Unix we just
3211 * called lstat()); at this point we gather all the information
3212 * we need about the source from the opened file descriptor.
3214 g_object_unref (info);
3216 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3217 cancellable, &tmp_error);
3220 /* Not all gvfs backends implement query_info_on_read(), we
3221 * can just fall back to the pathname again.
3222 * https://bugzilla.gnome.org/706254
3224 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3226 g_clear_error (&tmp_error);
3227 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3228 cancellable, error);
3232 g_free (attrs_to_read);
3233 g_propagate_error (error, tmp_error);
3237 g_free (attrs_to_read);
3241 do_set_attributes = TRUE;
3244 /* In the local file path, we pass down the source info which
3245 * includes things like unix::mode, to ensure that the target file
3246 * is not created with different permissions from the source file.
3248 * If a future API like g_file_replace_with_info() is added, switch
3249 * this code to use that.
3251 * Use %G_FILE_CREATE_PRIVATE unless
3252 * - we were told to create the file with default permissions (i.e. the
3254 * - or if the source file is on a file system which doesn’t support
3255 * `unix::mode` (in which case it probably also makes sense to create the
3256 * destination with default permissions because the source cannot be
3258 * - or if the destination file is a `GLocalFile`, in which case we can
3259 * directly open() it with the permissions from the source file.
3261 create_flags = G_FILE_CREATE_NONE;
3262 if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3263 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3264 !G_IS_LOCAL_FILE (destination))
3265 create_flags |= G_FILE_CREATE_PRIVATE;
3266 if (flags & G_FILE_COPY_OVERWRITE)
3267 create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3269 if (G_IS_LOCAL_FILE (destination))
3271 if (flags & G_FILE_COPY_OVERWRITE)
3272 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3274 flags & G_FILE_COPY_BACKUP,
3276 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3277 cancellable, error);
3279 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3280 FALSE, create_flags,
3281 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3282 cancellable, error);
3284 else if (flags & G_FILE_COPY_OVERWRITE)
3286 out = (GOutputStream *)g_file_replace (destination,
3288 flags & G_FILE_COPY_BACKUP,
3290 cancellable, error);
3294 out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3301 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3303 GError *reflink_err = NULL;
3305 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3306 progress_callback, progress_callback_data,
3309 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3311 g_clear_error (&reflink_err);
3315 g_propagate_error (error, reflink_err);
3328 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3330 GError *splice_err = NULL;
3332 if (!splice_stream_with_progress (in, out, cancellable,
3333 progress_callback, progress_callback_data,
3336 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3338 g_clear_error (&splice_err);
3342 g_propagate_error (error, splice_err);
3355 /* A plain read/write loop */
3356 if (!copy_stream_with_progress (in, out, source, cancellable,
3357 progress_callback, progress_callback_data,
3365 /* Don't care about errors in source here */
3366 (void) g_input_stream_close (in, cancellable, NULL);
3367 g_object_unref (in);
3372 /* But write errors on close are bad! */
3373 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3375 g_object_unref (out);
3378 /* Ignore errors here. Failure to copy metadata is not a hard error */
3379 /* TODO: set these attributes /before/ we do the rename() on Unix */
3380 if (ret && do_set_attributes)
3382 g_file_set_attributes_from_info (destination,
3384 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3389 g_clear_object (&info);
3396 * @source: input #GFile
3397 * @destination: destination #GFile
3398 * @flags: set of #GFileCopyFlags
3399 * @cancellable: (nullable): optional #GCancellable object,
3401 * @progress_callback: (nullable) (scope call): function to callback with
3402 * progress information, or %NULL if progress information is not needed
3403 * @progress_callback_data: (closure): user data to pass to @progress_callback
3404 * @error: #GError to set on error, or %NULL
3406 * Copies the file @source to the location specified by @destination.
3407 * Can not handle recursive copies of directories.
3409 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3410 * existing @destination file is overwritten.
3412 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3413 * will be copied as symlinks, otherwise the target of the
3414 * @source symlink will be copied.
3416 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3417 * that is possible to copy is copied, not just the default subset (which,
3418 * for instance, does not include the owner, see #GFileInfo).
3420 * If @cancellable is not %NULL, then the operation can be cancelled by
3421 * triggering the cancellable object from another thread. If the operation
3422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3424 * If @progress_callback is not %NULL, then the operation can be monitored
3425 * by setting this to a #GFileProgressCallback function.
3426 * @progress_callback_data will be passed to this function. It is guaranteed
3427 * that this callback will be called after all data has been transferred with
3428 * the total number of bytes copied during the operation.
3430 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3431 * is returned, independent on the status of the @destination.
3433 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3434 * the error %G_IO_ERROR_EXISTS is returned.
3436 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3437 * error is returned. If trying to overwrite a directory with a directory the
3438 * %G_IO_ERROR_WOULD_MERGE error is returned.
3440 * If the source is a directory and the target does not exist, or
3441 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3442 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3444 * If you are interested in copying the #GFile object itself (not the on-disk
3445 * file), see g_file_dup().
3447 * Returns: %TRUE on success, %FALSE otherwise.
3450 g_file_copy (GFile *source,
3452 GFileCopyFlags flags,
3453 GCancellable *cancellable,
3454 GFileProgressCallback progress_callback,
3455 gpointer progress_callback_data,
3462 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3463 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3465 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3468 iface = G_FILE_GET_IFACE (destination);
3472 res = (* iface->copy) (source, destination,
3474 progress_callback, progress_callback_data,
3480 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3482 g_propagate_error (error, my_error);
3486 g_clear_error (&my_error);
3489 /* If the types are different, and the destination method failed
3490 * also try the source method
3492 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3494 iface = G_FILE_GET_IFACE (source);
3499 res = (* iface->copy) (source, destination,
3501 progress_callback, progress_callback_data,
3507 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3509 g_propagate_error (error, my_error);
3513 g_clear_error (&my_error);
3517 return file_copy_fallback (source, destination, flags, cancellable,
3518 progress_callback, progress_callback_data,
3523 * g_file_copy_async:
3524 * @source: input #GFile
3525 * @destination: destination #GFile
3526 * @flags: set of #GFileCopyFlags
3527 * @io_priority: the [I/O priority][io-priority] of the request
3528 * @cancellable: (nullable): optional #GCancellable object,
3530 * @progress_callback: (nullable) (scope notified): function to callback with progress
3531 * information, or %NULL if progress information is not needed
3532 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3533 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3534 * @user_data: (closure callback): the data to pass to callback function
3536 * Copies the file @source to the location specified by @destination
3537 * asynchronously. For details of the behaviour, see g_file_copy().
3539 * If @progress_callback is not %NULL, then that function that will be called
3540 * just like in g_file_copy(). The callback will run in the default main context
3541 * of the thread calling g_file_copy_async() — the same context as @callback is
3544 * When the operation is finished, @callback will be called. You can then call
3545 * g_file_copy_finish() to get the result of the operation.
3548 g_file_copy_async (GFile *source,
3550 GFileCopyFlags flags,
3552 GCancellable *cancellable,
3553 GFileProgressCallback progress_callback,
3554 gpointer progress_callback_data,
3555 GAsyncReadyCallback callback,
3560 g_return_if_fail (G_IS_FILE (source));
3561 g_return_if_fail (G_IS_FILE (destination));
3563 iface = G_FILE_GET_IFACE (source);
3564 (* iface->copy_async) (source,
3570 progress_callback_data,
3576 * g_file_copy_finish:
3577 * @file: input #GFile
3578 * @res: a #GAsyncResult
3579 * @error: a #GError, or %NULL
3581 * Finishes copying the file started with g_file_copy_async().
3583 * Returns: a %TRUE on success, %FALSE on error.
3586 g_file_copy_finish (GFile *file,
3592 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3593 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3595 if (g_async_result_legacy_propagate_error (res, error))
3598 iface = G_FILE_GET_IFACE (file);
3599 return (* iface->copy_finish) (file, res, error);
3604 * @source: #GFile pointing to the source location
3605 * @destination: #GFile pointing to the destination location
3606 * @flags: set of #GFileCopyFlags
3607 * @cancellable: (nullable): optional #GCancellable object,
3609 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3610 * function for updates
3611 * @progress_callback_data: (closure): gpointer to user data for
3612 * the callback function
3613 * @error: #GError for returning error conditions, or %NULL
3615 * Tries to move the file or directory @source to the location specified
3616 * by @destination. If native move operations are supported then this is
3617 * used, otherwise a copy + delete fallback is used. The native
3618 * implementation may support moving directories (for instance on moves
3619 * inside the same filesystem), but the fallback code does not.
3621 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3622 * existing @destination file is overwritten.
3624 * If @cancellable is not %NULL, then the operation can be cancelled by
3625 * triggering the cancellable object from another thread. If the operation
3626 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3628 * If @progress_callback is not %NULL, then the operation can be monitored
3629 * by setting this to a #GFileProgressCallback function.
3630 * @progress_callback_data will be passed to this function. It is
3631 * guaranteed that this callback will be called after all data has been
3632 * transferred with the total number of bytes copied during the operation.
3634 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3635 * error is returned, independent on the status of the @destination.
3637 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3638 * then the error %G_IO_ERROR_EXISTS is returned.
3640 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3641 * error is returned. If trying to overwrite a directory with a directory the
3642 * %G_IO_ERROR_WOULD_MERGE error is returned.
3644 * If the source is a directory and the target does not exist, or
3645 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3646 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3647 * move operation isn't available).
3649 * Returns: %TRUE on successful move, %FALSE otherwise.
3652 g_file_move (GFile *source,
3654 GFileCopyFlags flags,
3655 GCancellable *cancellable,
3656 GFileProgressCallback progress_callback,
3657 gpointer progress_callback_data,
3664 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3665 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3667 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3670 iface = G_FILE_GET_IFACE (destination);
3674 res = (* iface->move) (source, destination,
3676 progress_callback, progress_callback_data,
3682 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3684 g_propagate_error (error, my_error);
3688 g_clear_error (&my_error);
3691 /* If the types are different, and the destination method failed
3692 * also try the source method
3694 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3696 iface = G_FILE_GET_IFACE (source);
3701 res = (* iface->move) (source, destination,
3703 progress_callback, progress_callback_data,
3709 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3711 g_propagate_error (error, my_error);
3715 g_clear_error (&my_error);
3719 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3721 g_set_error_literal (error, G_IO_ERROR,
3722 G_IO_ERROR_NOT_SUPPORTED,
3723 _("Operation not supported"));
3727 flags |= G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS;
3728 if (!g_file_copy (source, destination, flags, cancellable,
3729 progress_callback, progress_callback_data,
3733 return g_file_delete (source, cancellable, error);
3737 * g_file_make_directory:
3738 * @file: input #GFile
3739 * @cancellable: (nullable): optional #GCancellable object,
3741 * @error: a #GError, or %NULL
3743 * Creates a directory. Note that this will only create a child directory
3744 * of the immediate parent directory of the path or URI given by the #GFile.
3745 * To recursively create directories, see g_file_make_directory_with_parents().
3746 * This function will fail if the parent directory does not exist, setting
3747 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3748 * creating directories, this function will fail, setting @error to
3749 * %G_IO_ERROR_NOT_SUPPORTED.
3751 * For a local #GFile the newly created directory will have the default
3752 * (current) ownership and permissions of the current process.
3754 * If @cancellable is not %NULL, then the operation can be cancelled by
3755 * triggering the cancellable object from another thread. If the operation
3756 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3758 * Returns: %TRUE on successful creation, %FALSE otherwise.
3761 g_file_make_directory (GFile *file,
3762 GCancellable *cancellable,
3767 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3769 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3772 iface = G_FILE_GET_IFACE (file);
3774 if (iface->make_directory == NULL)
3776 g_set_error_literal (error, G_IO_ERROR,
3777 G_IO_ERROR_NOT_SUPPORTED,
3778 _("Operation not supported"));
3782 return (* iface->make_directory) (file, cancellable, error);
3786 * g_file_make_directory_async:
3787 * @file: input #GFile
3788 * @io_priority: the [I/O priority][io-priority] of the request
3789 * @cancellable: (nullable): optional #GCancellable object,
3791 * @callback: a #GAsyncReadyCallback to call
3792 * when the request is satisfied
3793 * @user_data: the data to pass to callback function
3795 * Asynchronously creates a directory.
3797 * Virtual: make_directory_async
3801 g_file_make_directory_async (GFile *file,
3803 GCancellable *cancellable,
3804 GAsyncReadyCallback callback,
3809 g_return_if_fail (G_IS_FILE (file));
3811 iface = G_FILE_GET_IFACE (file);
3812 (* iface->make_directory_async) (file,
3820 * g_file_make_directory_finish:
3821 * @file: input #GFile
3822 * @result: a #GAsyncResult
3823 * @error: a #GError, or %NULL
3825 * Finishes an asynchronous directory creation, started with
3826 * g_file_make_directory_async().
3828 * Virtual: make_directory_finish
3829 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3833 g_file_make_directory_finish (GFile *file,
3834 GAsyncResult *result,
3839 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3840 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3842 iface = G_FILE_GET_IFACE (file);
3843 return (* iface->make_directory_finish) (file, result, error);
3847 * g_file_make_directory_with_parents:
3848 * @file: input #GFile
3849 * @cancellable: (nullable): optional #GCancellable object,
3851 * @error: a #GError, or %NULL
3853 * Creates a directory and any parent directories that may not
3854 * exist similar to 'mkdir -p'. If the file system does not support
3855 * creating directories, this function will fail, setting @error to
3856 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3857 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3858 * the similar g_mkdir_with_parents().
3860 * For a local #GFile the newly created directories will have the default
3861 * (current) ownership and permissions of the current process.
3863 * If @cancellable is not %NULL, then the operation can be cancelled by
3864 * triggering the cancellable object from another thread. If the operation
3865 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3867 * Returns: %TRUE if all directories have been successfully created, %FALSE
3873 g_file_make_directory_with_parents (GFile *file,
3874 GCancellable *cancellable,
3877 GFile *work_file = NULL;
3878 GList *list = NULL, *l;
3879 GError *my_error = NULL;
3881 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3883 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3886 /* Try for the simple case of not having to create any parent
3887 * directories. If any parent directory needs to be created, this
3888 * call will fail with NOT_FOUND. If that happens, then that value of
3889 * my_error persists into the while loop below.
3891 g_file_make_directory (file, cancellable, &my_error);
3892 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3895 g_propagate_error (error, my_error);
3896 return my_error == NULL;
3899 work_file = g_object_ref (file);
3901 /* Creates the parent directories as needed. In case any particular
3902 * creation operation fails for lack of other parent directories
3903 * (NOT_FOUND), the directory is added to a list of directories to
3904 * create later, and the value of my_error is retained until the next
3905 * iteration of the loop. After the loop my_error should either be
3906 * empty or contain a real failure condition.
3908 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3912 parent_file = g_file_get_parent (work_file);
3913 if (parent_file == NULL)
3916 g_clear_error (&my_error);
3917 g_file_make_directory (parent_file, cancellable, &my_error);
3918 /* Another process may have created the directory in between the
3919 * G_IO_ERROR_NOT_FOUND and now
3921 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3922 g_clear_error (&my_error);
3924 g_object_unref (work_file);
3925 work_file = g_object_ref (parent_file);
3927 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3928 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3930 g_object_unref (parent_file);
3933 /* All directories should be able to be created now, so an error at
3934 * this point means the whole operation must fail -- except an EXISTS
3935 * error, which means that another process already created the
3936 * directory in between the previous failure and now.
3938 for (l = list; my_error == NULL && l; l = l->next)
3940 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3941 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3942 g_clear_error (&my_error);
3946 g_object_unref (work_file);
3949 while (list != NULL)
3951 g_object_unref ((GFile *) list->data);
3952 list = g_list_remove (list, list->data);
3955 /* At this point an error in my_error means a that something
3956 * unexpected failed in either of the loops above, so the whole
3957 * operation must fail.
3959 if (my_error != NULL)
3961 g_propagate_error (error, my_error);
3965 return g_file_make_directory (file, cancellable, error);
3969 * g_file_make_symbolic_link:
3970 * @file: a #GFile with the name of the symlink to create
3971 * @symlink_value: (type filename): a string with the path for the target
3972 * of the new symlink
3973 * @cancellable: (nullable): optional #GCancellable object,
3977 * Creates a symbolic link named @file which contains the string
3980 * If @cancellable is not %NULL, then the operation can be cancelled by
3981 * triggering the cancellable object from another thread. If the operation
3982 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3984 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3987 g_file_make_symbolic_link (GFile *file,
3988 const char *symlink_value,
3989 GCancellable *cancellable,
3994 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3995 g_return_val_if_fail (symlink_value != NULL, FALSE);
3997 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4000 if (*symlink_value == '\0')
4002 g_set_error_literal (error, G_IO_ERROR,
4003 G_IO_ERROR_INVALID_ARGUMENT,
4004 _("Invalid symlink value given"));
4008 iface = G_FILE_GET_IFACE (file);
4010 if (iface->make_symbolic_link == NULL)
4012 g_set_error_literal (error, G_IO_ERROR,
4013 G_IO_ERROR_NOT_SUPPORTED,
4014 _("Symbolic links not supported"));
4018 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4023 * @file: input #GFile
4024 * @cancellable: (nullable): optional #GCancellable object,
4026 * @error: a #GError, or %NULL
4028 * Deletes a file. If the @file is a directory, it will only be
4029 * deleted if it is empty. This has the same semantics as g_unlink().
4031 * If @file doesn’t exist, %G_IO_ERROR_NOT_FOUND will be returned. This allows
4032 * for deletion to be implemented avoiding
4033 * [time-of-check to time-of-use races](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use):
4035 * g_autoptr(GError) local_error = NULL;
4036 * if (!g_file_delete (my_file, my_cancellable, &local_error) &&
4037 * !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4039 * // deletion failed for some reason other than the file not existing:
4040 * // so report the error
4041 * g_warning ("Failed to delete %s: %s",
4042 * g_file_peek_path (my_file), local_error->message);
4046 * If @cancellable is not %NULL, then the operation can be cancelled by
4047 * triggering the cancellable object from another thread. If the operation
4048 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4050 * Virtual: delete_file
4051 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4054 g_file_delete (GFile *file,
4055 GCancellable *cancellable,
4060 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4065 iface = G_FILE_GET_IFACE (file);
4067 if (iface->delete_file == NULL)
4069 g_set_error_literal (error, G_IO_ERROR,
4070 G_IO_ERROR_NOT_SUPPORTED,
4071 _("Operation not supported"));
4075 return (* iface->delete_file) (file, cancellable, error);
4079 * g_file_delete_async:
4080 * @file: input #GFile
4081 * @io_priority: the [I/O priority][io-priority] of the request
4082 * @cancellable: (nullable): optional #GCancellable object,
4084 * @callback: a #GAsyncReadyCallback to call
4085 * when the request is satisfied
4086 * @user_data: the data to pass to callback function
4088 * Asynchronously delete a file. If the @file is a directory, it will
4089 * only be deleted if it is empty. This has the same semantics as
4092 * Virtual: delete_file_async
4096 g_file_delete_async (GFile *file,
4098 GCancellable *cancellable,
4099 GAsyncReadyCallback callback,
4104 g_return_if_fail (G_IS_FILE (file));
4106 iface = G_FILE_GET_IFACE (file);
4107 (* iface->delete_file_async) (file,
4115 * g_file_delete_finish:
4116 * @file: input #GFile
4117 * @result: a #GAsyncResult
4118 * @error: a #GError, or %NULL
4120 * Finishes deleting a file started with g_file_delete_async().
4122 * Virtual: delete_file_finish
4123 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4127 g_file_delete_finish (GFile *file,
4128 GAsyncResult *result,
4133 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4134 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4136 if (g_async_result_legacy_propagate_error (result, error))
4139 iface = G_FILE_GET_IFACE (file);
4140 return (* iface->delete_file_finish) (file, result, error);
4145 * @file: #GFile to send to trash
4146 * @cancellable: (nullable): optional #GCancellable object,
4148 * @error: a #GError, or %NULL
4150 * Sends @file to the "Trashcan", if possible. This is similar to
4151 * deleting it, but the user can recover it before emptying the trashcan.
4152 * Not all file systems support trashing, so this call can return the
4153 * %G_IO_ERROR_NOT_SUPPORTED error. Since GLib 2.66, the `x-gvfs-notrash` unix
4154 * mount option can be used to disable g_file_trash() support for certain
4155 * mounts, the %G_IO_ERROR_NOT_SUPPORTED error will be returned in that case.
4157 * If @cancellable is not %NULL, then the operation can be cancelled by
4158 * triggering the cancellable object from another thread. If the operation
4159 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4162 * Returns: %TRUE on successful trash, %FALSE otherwise.
4165 g_file_trash (GFile *file,
4166 GCancellable *cancellable,
4171 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4173 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4176 iface = G_FILE_GET_IFACE (file);
4178 if (iface->trash == NULL)
4180 g_set_error_literal (error,
4181 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4182 _("Trash not supported"));
4186 return (* iface->trash) (file, cancellable, error);
4190 * g_file_trash_async:
4191 * @file: input #GFile
4192 * @io_priority: the [I/O priority][io-priority] of the request
4193 * @cancellable: (nullable): optional #GCancellable object,
4195 * @callback: a #GAsyncReadyCallback to call
4196 * when the request is satisfied
4197 * @user_data: the data to pass to callback function
4199 * Asynchronously sends @file to the Trash location, if possible.
4201 * Virtual: trash_async
4205 g_file_trash_async (GFile *file,
4207 GCancellable *cancellable,
4208 GAsyncReadyCallback callback,
4213 g_return_if_fail (G_IS_FILE (file));
4215 iface = G_FILE_GET_IFACE (file);
4216 (* iface->trash_async) (file,
4224 * g_file_trash_finish:
4225 * @file: input #GFile
4226 * @result: a #GAsyncResult
4227 * @error: a #GError, or %NULL
4229 * Finishes an asynchronous file trashing operation, started with
4230 * g_file_trash_async().
4232 * Virtual: trash_finish
4233 * Returns: %TRUE on successful trash, %FALSE otherwise.
4237 g_file_trash_finish (GFile *file,
4238 GAsyncResult *result,
4243 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4244 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4246 iface = G_FILE_GET_IFACE (file);
4247 return (* iface->trash_finish) (file, result, error);
4251 * g_file_set_display_name:
4252 * @file: input #GFile
4253 * @display_name: a string
4254 * @cancellable: (nullable): optional #GCancellable object,
4256 * @error: a #GError, or %NULL
4258 * Renames @file to the specified display name.
4260 * The display name is converted from UTF-8 to the correct encoding
4261 * for the target filesystem if possible and the @file is renamed to this.
4263 * If you want to implement a rename operation in the user interface the
4264 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4265 * initial value in the rename widget, and then the result after editing
4266 * should be passed to g_file_set_display_name().
4268 * On success the resulting converted filename is returned.
4270 * If @cancellable is not %NULL, then the operation can be cancelled by
4271 * triggering the cancellable object from another thread. If the operation
4272 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4274 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4275 * or %NULL if there was an error.
4276 * Free the returned object with g_object_unref().
4279 g_file_set_display_name (GFile *file,
4280 const gchar *display_name,
4281 GCancellable *cancellable,
4286 g_return_val_if_fail (G_IS_FILE (file), NULL);
4287 g_return_val_if_fail (display_name != NULL, NULL);
4289 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4293 G_IO_ERROR_INVALID_ARGUMENT,
4294 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4298 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4301 iface = G_FILE_GET_IFACE (file);
4303 return (* iface->set_display_name) (file, display_name, cancellable, error);
4307 * g_file_set_display_name_async:
4308 * @file: input #GFile
4309 * @display_name: a string
4310 * @io_priority: the [I/O priority][io-priority] of the request
4311 * @cancellable: (nullable): optional #GCancellable object,
4313 * @callback: (scope async): a #GAsyncReadyCallback to call
4314 * when the request is satisfied
4315 * @user_data: (closure): the data to pass to callback function
4317 * Asynchronously sets the display name for a given #GFile.
4319 * For more details, see g_file_set_display_name() which is
4320 * the synchronous version of this call.
4322 * When the operation is finished, @callback will be called.
4323 * You can then call g_file_set_display_name_finish() to get
4324 * the result of the operation.
4327 g_file_set_display_name_async (GFile *file,
4328 const gchar *display_name,
4330 GCancellable *cancellable,
4331 GAsyncReadyCallback callback,
4336 g_return_if_fail (G_IS_FILE (file));
4337 g_return_if_fail (display_name != NULL);
4339 iface = G_FILE_GET_IFACE (file);
4340 (* iface->set_display_name_async) (file,
4349 * g_file_set_display_name_finish:
4350 * @file: input #GFile
4351 * @res: a #GAsyncResult
4352 * @error: a #GError, or %NULL
4354 * Finishes setting a display name started with
4355 * g_file_set_display_name_async().
4357 * Returns: (transfer full): a #GFile or %NULL on error.
4358 * Free the returned object with g_object_unref().
4361 g_file_set_display_name_finish (GFile *file,
4367 g_return_val_if_fail (G_IS_FILE (file), NULL);
4368 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4370 if (g_async_result_legacy_propagate_error (res, error))
4373 iface = G_FILE_GET_IFACE (file);
4374 return (* iface->set_display_name_finish) (file, res, error);
4378 * g_file_query_settable_attributes:
4379 * @file: input #GFile
4380 * @cancellable: (nullable): optional #GCancellable object,
4382 * @error: a #GError, or %NULL
4384 * Obtain the list of settable attributes for the file.
4386 * Returns the type and full attribute name of all the attributes
4387 * that can be set on this file. This doesn't mean setting it will
4388 * always succeed though, you might get an access failure, or some
4389 * specific file may not support a specific attribute.
4391 * If @cancellable is not %NULL, then the operation can be cancelled by
4392 * triggering the cancellable object from another thread. If the operation
4393 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4395 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4396 * When you are done with it, release it with
4397 * g_file_attribute_info_list_unref()
4399 GFileAttributeInfoList *
4400 g_file_query_settable_attributes (GFile *file,
4401 GCancellable *cancellable,
4406 GFileAttributeInfoList *list;
4408 g_return_val_if_fail (G_IS_FILE (file), NULL);
4410 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4413 iface = G_FILE_GET_IFACE (file);
4415 if (iface->query_settable_attributes == NULL)
4416 return g_file_attribute_info_list_new ();
4419 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4423 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4425 list = g_file_attribute_info_list_new ();
4426 g_error_free (my_error);
4429 g_propagate_error (error, my_error);
4436 * g_file_query_writable_namespaces:
4437 * @file: input #GFile
4438 * @cancellable: (nullable): optional #GCancellable object,
4440 * @error: a #GError, or %NULL
4442 * Obtain the list of attribute namespaces where new attributes
4443 * can be created by a user. An example of this is extended
4444 * attributes (in the "xattr" namespace).
4446 * If @cancellable is not %NULL, then the operation can be cancelled by
4447 * triggering the cancellable object from another thread. If the operation
4448 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4450 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4451 * When you are done with it, release it with
4452 * g_file_attribute_info_list_unref()
4454 GFileAttributeInfoList *
4455 g_file_query_writable_namespaces (GFile *file,
4456 GCancellable *cancellable,
4461 GFileAttributeInfoList *list;
4463 g_return_val_if_fail (G_IS_FILE (file), NULL);
4465 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4468 iface = G_FILE_GET_IFACE (file);
4470 if (iface->query_writable_namespaces == NULL)
4471 return g_file_attribute_info_list_new ();
4474 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4478 g_warn_if_reached();
4479 list = g_file_attribute_info_list_new ();
4482 if (my_error != NULL)
4484 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4486 g_error_free (my_error);
4489 g_propagate_error (error, my_error);
4496 * g_file_set_attribute:
4497 * @file: input #GFile
4498 * @attribute: a string containing the attribute's name
4499 * @type: The type of the attribute
4500 * @value_p: (nullable): a pointer to the value (or the pointer
4501 * itself if the type is a pointer type)
4502 * @flags: a set of #GFileQueryInfoFlags
4503 * @cancellable: (nullable): optional #GCancellable object,
4505 * @error: a #GError, or %NULL
4507 * Sets an attribute in the file with attribute name @attribute to @value_p.
4509 * Some attributes can be unset by setting @type to
4510 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4512 * If @cancellable is not %NULL, then the operation can be cancelled by
4513 * triggering the cancellable object from another thread. If the operation
4514 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4516 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4519 g_file_set_attribute (GFile *file,
4520 const gchar *attribute,
4521 GFileAttributeType type,
4523 GFileQueryInfoFlags flags,
4524 GCancellable *cancellable,
4529 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4530 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4532 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4535 iface = G_FILE_GET_IFACE (file);
4537 if (iface->set_attribute == NULL)
4539 g_set_error_literal (error, G_IO_ERROR,
4540 G_IO_ERROR_NOT_SUPPORTED,
4541 _("Operation not supported"));
4545 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4549 * g_file_set_attributes_from_info:
4550 * @file: input #GFile
4551 * @info: a #GFileInfo
4552 * @flags: #GFileQueryInfoFlags
4553 * @cancellable: (nullable): optional #GCancellable object,
4555 * @error: a #GError, or %NULL
4557 * Tries to set all attributes in the #GFileInfo on the target
4558 * values, not stopping on the first error.
4560 * If there is any error during this operation then @error will
4561 * be set to the first error. Error on particular fields are flagged
4562 * by setting the "status" field in the attribute value to
4563 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4564 * also detect further errors.
4566 * If @cancellable is not %NULL, then the operation can be cancelled by
4567 * triggering the cancellable object from another thread. If the operation
4568 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4570 * Returns: %FALSE if there was any error, %TRUE otherwise.
4573 g_file_set_attributes_from_info (GFile *file,
4575 GFileQueryInfoFlags flags,
4576 GCancellable *cancellable,
4581 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4582 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4584 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4587 g_file_info_clear_status (info);
4589 iface = G_FILE_GET_IFACE (file);
4591 return (* iface->set_attributes_from_info) (file,
4599 g_file_real_set_attributes_from_info (GFile *file,
4601 GFileQueryInfoFlags flags,
4602 GCancellable *cancellable,
4608 GFileAttributeValue *value;
4612 attributes = g_file_info_list_attributes (info, NULL);
4614 for (i = 0; attributes[i] != NULL; i++)
4616 value = _g_file_info_get_attribute_value (info, attributes[i]);
4618 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4621 if (!g_file_set_attribute (file, attributes[i],
4622 value->type, _g_file_attribute_value_peek_as_pointer (value),
4623 flags, cancellable, error))
4625 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4627 /* Don't set error multiple times */
4631 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4634 g_strfreev (attributes);
4640 * g_file_set_attributes_async:
4641 * @file: input #GFile
4642 * @info: a #GFileInfo
4643 * @flags: a #GFileQueryInfoFlags
4644 * @io_priority: the [I/O priority][io-priority] of the request
4645 * @cancellable: (nullable): optional #GCancellable object,
4647 * @callback: (scope async): a #GAsyncReadyCallback
4648 * @user_data: (closure): a #gpointer
4650 * Asynchronously sets the attributes of @file with @info.
4652 * For more details, see g_file_set_attributes_from_info(),
4653 * which is the synchronous version of this call.
4655 * When the operation is finished, @callback will be called.
4656 * You can then call g_file_set_attributes_finish() to get
4657 * the result of the operation.
4660 g_file_set_attributes_async (GFile *file,
4662 GFileQueryInfoFlags flags,
4664 GCancellable *cancellable,
4665 GAsyncReadyCallback callback,
4670 g_return_if_fail (G_IS_FILE (file));
4671 g_return_if_fail (G_IS_FILE_INFO (info));
4673 iface = G_FILE_GET_IFACE (file);
4674 (* iface->set_attributes_async) (file,
4684 * g_file_set_attributes_finish:
4685 * @file: input #GFile
4686 * @result: a #GAsyncResult
4687 * @info: (out) (transfer full): a #GFileInfo
4688 * @error: a #GError, or %NULL
4690 * Finishes setting an attribute started in g_file_set_attributes_async().
4692 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4695 g_file_set_attributes_finish (GFile *file,
4696 GAsyncResult *result,
4702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4703 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4705 /* No standard handling of errors here, as we must set info even
4708 iface = G_FILE_GET_IFACE (file);
4709 return (* iface->set_attributes_finish) (file, result, info, error);
4713 * g_file_set_attribute_string:
4714 * @file: input #GFile
4715 * @attribute: a string containing the attribute's name
4716 * @value: a string containing the attribute's value
4717 * @flags: #GFileQueryInfoFlags
4718 * @cancellable: (nullable): optional #GCancellable object,
4720 * @error: a #GError, or %NULL
4722 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4723 * If @attribute is of a different type, this operation will fail.
4725 * If @cancellable is not %NULL, then the operation can be cancelled by
4726 * triggering the cancellable object from another thread. If the operation
4727 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4729 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4732 g_file_set_attribute_string (GFile *file,
4733 const char *attribute,
4735 GFileQueryInfoFlags flags,
4736 GCancellable *cancellable,
4739 return g_file_set_attribute (file, attribute,
4740 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4741 flags, cancellable, error);
4745 * g_file_set_attribute_byte_string:
4746 * @file: input #GFile
4747 * @attribute: a string containing the attribute's name
4748 * @value: a string containing the attribute's new value
4749 * @flags: a #GFileQueryInfoFlags
4750 * @cancellable: (nullable): optional #GCancellable object,
4752 * @error: a #GError, or %NULL
4754 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4755 * If @attribute is of a different type, this operation will fail,
4758 * If @cancellable is not %NULL, then the operation can be cancelled by
4759 * triggering the cancellable object from another thread. If the operation
4760 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4762 * Returns: %TRUE if the @attribute was successfully set to @value
4763 * in the @file, %FALSE otherwise.
4766 g_file_set_attribute_byte_string (GFile *file,
4767 const gchar *attribute,
4769 GFileQueryInfoFlags flags,
4770 GCancellable *cancellable,
4773 return g_file_set_attribute (file, attribute,
4774 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4775 flags, cancellable, error);
4779 * g_file_set_attribute_uint32:
4780 * @file: input #GFile
4781 * @attribute: a string containing the attribute's name
4782 * @value: a #guint32 containing the attribute's new value
4783 * @flags: a #GFileQueryInfoFlags
4784 * @cancellable: (nullable): optional #GCancellable object,
4786 * @error: a #GError, or %NULL
4788 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4789 * If @attribute is of a different type, this operation will fail.
4791 * If @cancellable is not %NULL, then the operation can be cancelled by
4792 * triggering the cancellable object from another thread. If the operation
4793 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4795 * Returns: %TRUE if the @attribute was successfully set to @value
4796 * in the @file, %FALSE otherwise.
4799 g_file_set_attribute_uint32 (GFile *file,
4800 const gchar *attribute,
4802 GFileQueryInfoFlags flags,
4803 GCancellable *cancellable,
4806 return g_file_set_attribute (file, attribute,
4807 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4808 flags, cancellable, error);
4812 * g_file_set_attribute_int32:
4813 * @file: input #GFile
4814 * @attribute: a string containing the attribute's name
4815 * @value: a #gint32 containing the attribute's new value
4816 * @flags: a #GFileQueryInfoFlags
4817 * @cancellable: (nullable): optional #GCancellable object,
4819 * @error: a #GError, or %NULL
4821 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4822 * If @attribute is of a different type, this operation will fail.
4824 * If @cancellable is not %NULL, then the operation can be cancelled by
4825 * triggering the cancellable object from another thread. If the operation
4826 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4828 * Returns: %TRUE if the @attribute was successfully set to @value
4829 * in the @file, %FALSE otherwise.
4832 g_file_set_attribute_int32 (GFile *file,
4833 const gchar *attribute,
4835 GFileQueryInfoFlags flags,
4836 GCancellable *cancellable,
4839 return g_file_set_attribute (file, attribute,
4840 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4841 flags, cancellable, error);
4845 * g_file_set_attribute_uint64:
4846 * @file: input #GFile
4847 * @attribute: a string containing the attribute's name
4848 * @value: a #guint64 containing the attribute's new value
4849 * @flags: a #GFileQueryInfoFlags
4850 * @cancellable: (nullable): optional #GCancellable object,
4852 * @error: a #GError, or %NULL
4854 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4855 * If @attribute is of a different type, this operation will fail.
4857 * If @cancellable is not %NULL, then the operation can be cancelled by
4858 * triggering the cancellable object from another thread. If the operation
4859 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4861 * Returns: %TRUE if the @attribute was successfully set to @value
4862 * in the @file, %FALSE otherwise.
4865 g_file_set_attribute_uint64 (GFile *file,
4866 const gchar *attribute,
4868 GFileQueryInfoFlags flags,
4869 GCancellable *cancellable,
4872 return g_file_set_attribute (file, attribute,
4873 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4874 flags, cancellable, error);
4878 * g_file_set_attribute_int64:
4879 * @file: input #GFile
4880 * @attribute: a string containing the attribute's name
4881 * @value: a #guint64 containing the attribute's new value
4882 * @flags: a #GFileQueryInfoFlags
4883 * @cancellable: (nullable): optional #GCancellable object,
4885 * @error: a #GError, or %NULL
4887 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4888 * If @attribute is of a different type, this operation will fail.
4890 * If @cancellable is not %NULL, then the operation can be cancelled by
4891 * triggering the cancellable object from another thread. If the operation
4892 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4894 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4897 g_file_set_attribute_int64 (GFile *file,
4898 const gchar *attribute,
4900 GFileQueryInfoFlags flags,
4901 GCancellable *cancellable,
4904 return g_file_set_attribute (file, attribute,
4905 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4906 flags, cancellable, error);
4910 * g_file_mount_mountable:
4911 * @file: input #GFile
4912 * @flags: flags affecting the operation
4913 * @mount_operation: (nullable): a #GMountOperation,
4914 * or %NULL to avoid user interaction
4915 * @cancellable: (nullable): optional #GCancellable object,
4917 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4918 * when the request is satisfied, or %NULL
4919 * @user_data: (closure): the data to pass to callback function
4921 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4922 * Using @mount_operation, you can request callbacks when, for instance,
4923 * passwords are needed during authentication.
4925 * If @cancellable is not %NULL, then the operation can be cancelled by
4926 * triggering the cancellable object from another thread. If the operation
4927 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4929 * When the operation is finished, @callback will be called.
4930 * You can then call g_file_mount_mountable_finish() to get
4931 * the result of the operation.
4934 g_file_mount_mountable (GFile *file,
4935 GMountMountFlags flags,
4936 GMountOperation *mount_operation,
4937 GCancellable *cancellable,
4938 GAsyncReadyCallback callback,
4943 g_return_if_fail (G_IS_FILE (file));
4945 iface = G_FILE_GET_IFACE (file);
4947 if (iface->mount_mountable == NULL)
4949 g_task_report_new_error (file, callback, user_data,
4950 g_file_mount_mountable,
4951 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4952 _("Operation not supported"));
4956 (* iface->mount_mountable) (file,
4965 * g_file_mount_mountable_finish:
4966 * @file: input #GFile
4967 * @result: a #GAsyncResult
4968 * @error: a #GError, or %NULL
4970 * Finishes a mount operation. See g_file_mount_mountable() for details.
4972 * Finish an asynchronous mount operation that was started
4973 * with g_file_mount_mountable().
4975 * Returns: (transfer full): a #GFile or %NULL on error.
4976 * Free the returned object with g_object_unref().
4979 g_file_mount_mountable_finish (GFile *file,
4980 GAsyncResult *result,
4985 g_return_val_if_fail (G_IS_FILE (file), NULL);
4986 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4988 if (g_async_result_legacy_propagate_error (result, error))
4990 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4991 return g_task_propagate_pointer (G_TASK (result), error);
4993 iface = G_FILE_GET_IFACE (file);
4994 return (* iface->mount_mountable_finish) (file, result, error);
4998 * g_file_unmount_mountable:
4999 * @file: input #GFile
5000 * @flags: flags affecting the operation
5001 * @cancellable: (nullable): optional #GCancellable object,
5003 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5004 * when the request is satisfied, or %NULL
5005 * @user_data: (closure): the data to pass to callback function
5007 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5009 * If @cancellable is not %NULL, then the operation can be cancelled by
5010 * triggering the cancellable object from another thread. If the operation
5011 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5013 * When the operation is finished, @callback will be called.
5014 * You can then call g_file_unmount_mountable_finish() to get
5015 * the result of the operation.
5017 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5020 g_file_unmount_mountable (GFile *file,
5021 GMountUnmountFlags flags,
5022 GCancellable *cancellable,
5023 GAsyncReadyCallback callback,
5028 g_return_if_fail (G_IS_FILE (file));
5030 iface = G_FILE_GET_IFACE (file);
5032 if (iface->unmount_mountable == NULL)
5034 g_task_report_new_error (file, callback, user_data,
5035 g_file_unmount_mountable_with_operation,
5036 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5037 _("Operation not supported"));
5041 (* iface->unmount_mountable) (file,
5049 * g_file_unmount_mountable_finish:
5050 * @file: input #GFile
5051 * @result: a #GAsyncResult
5052 * @error: a #GError, or %NULL
5054 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5056 * Finish an asynchronous unmount operation that was started
5057 * with g_file_unmount_mountable().
5059 * Returns: %TRUE if the operation finished successfully.
5062 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5066 g_file_unmount_mountable_finish (GFile *file,
5067 GAsyncResult *result,
5072 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5073 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5075 if (g_async_result_legacy_propagate_error (result, error))
5077 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5078 return g_task_propagate_boolean (G_TASK (result), error);
5080 iface = G_FILE_GET_IFACE (file);
5081 return (* iface->unmount_mountable_finish) (file, result, error);
5085 * g_file_unmount_mountable_with_operation:
5086 * @file: input #GFile
5087 * @flags: flags affecting the operation
5088 * @mount_operation: (nullable): a #GMountOperation,
5089 * or %NULL to avoid user interaction
5090 * @cancellable: (nullable): optional #GCancellable object,
5092 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5093 * when the request is satisfied, or %NULL
5094 * @user_data: (closure): the data to pass to callback function
5096 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5098 * If @cancellable is not %NULL, then the operation can be cancelled by
5099 * triggering the cancellable object from another thread. If the operation
5100 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5102 * When the operation is finished, @callback will be called.
5103 * You can then call g_file_unmount_mountable_finish() to get
5104 * the result of the operation.
5109 g_file_unmount_mountable_with_operation (GFile *file,
5110 GMountUnmountFlags flags,
5111 GMountOperation *mount_operation,
5112 GCancellable *cancellable,
5113 GAsyncReadyCallback callback,
5118 g_return_if_fail (G_IS_FILE (file));
5120 iface = G_FILE_GET_IFACE (file);
5122 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5124 g_task_report_new_error (file, callback, user_data,
5125 g_file_unmount_mountable_with_operation,
5126 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5127 _("Operation not supported"));
5131 if (iface->unmount_mountable_with_operation != NULL)
5132 (* iface->unmount_mountable_with_operation) (file,
5139 (* iface->unmount_mountable) (file,
5147 * g_file_unmount_mountable_with_operation_finish:
5148 * @file: input #GFile
5149 * @result: a #GAsyncResult
5150 * @error: a #GError, or %NULL
5152 * Finishes an unmount operation,
5153 * see g_file_unmount_mountable_with_operation() for details.
5155 * Finish an asynchronous unmount operation that was started
5156 * with g_file_unmount_mountable_with_operation().
5158 * Returns: %TRUE if the operation finished successfully.
5164 g_file_unmount_mountable_with_operation_finish (GFile *file,
5165 GAsyncResult *result,
5170 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5171 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5173 if (g_async_result_legacy_propagate_error (result, error))
5175 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5176 return g_task_propagate_boolean (G_TASK (result), error);
5178 iface = G_FILE_GET_IFACE (file);
5179 if (iface->unmount_mountable_with_operation_finish != NULL)
5180 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5182 return (* iface->unmount_mountable_finish) (file, result, error);
5186 * g_file_eject_mountable:
5187 * @file: input #GFile
5188 * @flags: flags affecting the operation
5189 * @cancellable: (nullable): optional #GCancellable object,
5191 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5192 * when the request is satisfied, or %NULL
5193 * @user_data: (closure): the data to pass to callback function
5195 * Starts an asynchronous eject on a mountable.
5196 * When this operation has completed, @callback will be called with
5197 * @user_user data, and the operation can be finalized with
5198 * g_file_eject_mountable_finish().
5200 * If @cancellable is not %NULL, then the operation can be cancelled by
5201 * triggering the cancellable object from another thread. If the operation
5202 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5204 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5207 g_file_eject_mountable (GFile *file,
5208 GMountUnmountFlags flags,
5209 GCancellable *cancellable,
5210 GAsyncReadyCallback callback,
5215 g_return_if_fail (G_IS_FILE (file));
5217 iface = G_FILE_GET_IFACE (file);
5219 if (iface->eject_mountable == NULL)
5221 g_task_report_new_error (file, callback, user_data,
5222 g_file_eject_mountable_with_operation,
5223 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5224 _("Operation not supported"));
5228 (* iface->eject_mountable) (file,
5236 * g_file_eject_mountable_finish:
5237 * @file: input #GFile
5238 * @result: a #GAsyncResult
5239 * @error: a #GError, or %NULL
5241 * Finishes an asynchronous eject operation started by
5242 * g_file_eject_mountable().
5244 * Returns: %TRUE if the @file was ejected successfully.
5247 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5251 g_file_eject_mountable_finish (GFile *file,
5252 GAsyncResult *result,
5257 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5258 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5260 if (g_async_result_legacy_propagate_error (result, error))
5262 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5263 return g_task_propagate_boolean (G_TASK (result), error);
5265 iface = G_FILE_GET_IFACE (file);
5266 return (* iface->eject_mountable_finish) (file, result, error);
5270 * g_file_eject_mountable_with_operation:
5271 * @file: input #GFile
5272 * @flags: flags affecting the operation
5273 * @mount_operation: (nullable): a #GMountOperation,
5274 * or %NULL to avoid user interaction
5275 * @cancellable: (nullable): optional #GCancellable object,
5277 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5278 * when the request is satisfied, or %NULL
5279 * @user_data: (closure): the data to pass to callback function
5281 * Starts an asynchronous eject on a mountable.
5282 * When this operation has completed, @callback will be called with
5283 * @user_user data, and the operation can be finalized with
5284 * g_file_eject_mountable_with_operation_finish().
5286 * If @cancellable is not %NULL, then the operation can be cancelled by
5287 * triggering the cancellable object from another thread. If the operation
5288 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5293 g_file_eject_mountable_with_operation (GFile *file,
5294 GMountUnmountFlags flags,
5295 GMountOperation *mount_operation,
5296 GCancellable *cancellable,
5297 GAsyncReadyCallback callback,
5302 g_return_if_fail (G_IS_FILE (file));
5304 iface = G_FILE_GET_IFACE (file);
5306 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5308 g_task_report_new_error (file, callback, user_data,
5309 g_file_eject_mountable_with_operation,
5310 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5311 _("Operation not supported"));
5315 if (iface->eject_mountable_with_operation != NULL)
5316 (* iface->eject_mountable_with_operation) (file,
5323 (* iface->eject_mountable) (file,
5331 * g_file_eject_mountable_with_operation_finish:
5332 * @file: input #GFile
5333 * @result: a #GAsyncResult
5334 * @error: a #GError, or %NULL
5336 * Finishes an asynchronous eject operation started by
5337 * g_file_eject_mountable_with_operation().
5339 * Returns: %TRUE if the @file was ejected successfully.
5345 g_file_eject_mountable_with_operation_finish (GFile *file,
5346 GAsyncResult *result,
5351 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5352 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5354 if (g_async_result_legacy_propagate_error (result, error))
5356 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5357 return g_task_propagate_boolean (G_TASK (result), error);
5359 iface = G_FILE_GET_IFACE (file);
5360 if (iface->eject_mountable_with_operation_finish != NULL)
5361 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5363 return (* iface->eject_mountable_finish) (file, result, error);
5367 * g_file_monitor_directory:
5368 * @file: input #GFile
5369 * @flags: a set of #GFileMonitorFlags
5370 * @cancellable: (nullable): optional #GCancellable object,
5372 * @error: a #GError, or %NULL
5374 * Obtains a directory monitor for the given file.
5375 * This may fail if directory monitoring is not supported.
5377 * If @cancellable is not %NULL, then the operation can be cancelled by
5378 * triggering the cancellable object from another thread. If the operation
5379 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5381 * It does not make sense for @flags to contain
5382 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5383 * directories. It is not possible to monitor all the files in a
5384 * directory for changes made via hard links; if you want to do this then
5385 * you must register individual watches with g_file_monitor().
5387 * Virtual: monitor_dir
5388 * Returns: (transfer full): a #GFileMonitor for the given @file,
5389 * or %NULL on error.
5390 * Free the returned object with g_object_unref().
5393 g_file_monitor_directory (GFile *file,
5394 GFileMonitorFlags flags,
5395 GCancellable *cancellable,
5400 g_return_val_if_fail (G_IS_FILE (file), NULL);
5401 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5403 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5406 iface = G_FILE_GET_IFACE (file);
5408 if (iface->monitor_dir == NULL)
5410 g_set_error_literal (error, G_IO_ERROR,
5411 G_IO_ERROR_NOT_SUPPORTED,
5412 _("Operation not supported"));
5416 return (* iface->monitor_dir) (file, flags, cancellable, error);
5420 * g_file_monitor_file:
5421 * @file: input #GFile
5422 * @flags: a set of #GFileMonitorFlags
5423 * @cancellable: (nullable): optional #GCancellable object,
5425 * @error: a #GError, or %NULL
5427 * Obtains a file monitor for the given file. If no file notification
5428 * mechanism exists, then regular polling of the file is used.
5430 * If @cancellable is not %NULL, then the operation can be cancelled by
5431 * triggering the cancellable object from another thread. If the operation
5432 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5434 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5435 * will also attempt to report changes made to the file via another
5436 * filename (ie, a hard link). Without this flag, you can only rely on
5437 * changes made through the filename contained in @file to be
5438 * reported. Using this flag may result in an increase in resource
5439 * usage, and may not have any effect depending on the #GFileMonitor
5440 * backend and/or filesystem type.
5442 * Returns: (transfer full): a #GFileMonitor for the given @file,
5443 * or %NULL on error.
5444 * Free the returned object with g_object_unref().
5447 g_file_monitor_file (GFile *file,
5448 GFileMonitorFlags flags,
5449 GCancellable *cancellable,
5453 GFileMonitor *monitor;
5455 g_return_val_if_fail (G_IS_FILE (file), NULL);
5457 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5460 iface = G_FILE_GET_IFACE (file);
5464 if (iface->monitor_file)
5465 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5467 /* Fallback to polling */
5468 if (monitor == NULL)
5469 monitor = _g_poll_file_monitor_new (file);
5476 * @file: input #GFile
5477 * @flags: a set of #GFileMonitorFlags
5478 * @cancellable: (nullable): optional #GCancellable object,
5480 * @error: a #GError, or %NULL
5482 * Obtains a file or directory monitor for the given file,
5483 * depending on the type of the file.
5485 * If @cancellable is not %NULL, then the operation can be cancelled by
5486 * triggering the cancellable object from another thread. If the operation
5487 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5489 * Returns: (transfer full): a #GFileMonitor for the given @file,
5490 * or %NULL on error.
5491 * Free the returned object with g_object_unref().
5496 g_file_monitor (GFile *file,
5497 GFileMonitorFlags flags,
5498 GCancellable *cancellable,
5501 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5502 return g_file_monitor_directory (file,
5503 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5504 cancellable, error);
5506 return g_file_monitor_file (file, flags, cancellable, error);
5509 /********************************************
5510 * Default implementation of async ops *
5511 ********************************************/
5515 GFileQueryInfoFlags flags;
5516 } QueryInfoAsyncData;
5519 query_info_data_free (QueryInfoAsyncData *data)
5521 g_free (data->attributes);
5526 query_info_async_thread (GTask *task,
5529 GCancellable *cancellable)
5531 QueryInfoAsyncData *data = task_data;
5533 GError *error = NULL;
5535 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5537 g_task_return_pointer (task, info, g_object_unref);
5539 g_task_return_error (task, error);
5543 g_file_real_query_info_async (GFile *file,
5544 const char *attributes,
5545 GFileQueryInfoFlags flags,
5547 GCancellable *cancellable,
5548 GAsyncReadyCallback callback,
5552 QueryInfoAsyncData *data;
5554 data = g_new0 (QueryInfoAsyncData, 1);
5555 data->attributes = g_strdup (attributes);
5556 data->flags = flags;
5558 task = g_task_new (file, cancellable, callback, user_data);
5559 g_task_set_source_tag (task, g_file_real_query_info_async);
5560 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5561 g_task_set_priority (task, io_priority);
5562 g_task_run_in_thread (task, query_info_async_thread);
5563 g_object_unref (task);
5567 g_file_real_query_info_finish (GFile *file,
5571 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5573 return g_task_propagate_pointer (G_TASK (res), error);
5577 query_filesystem_info_async_thread (GTask *task,
5580 GCancellable *cancellable)
5582 const char *attributes = task_data;
5584 GError *error = NULL;
5586 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5588 g_task_return_pointer (task, info, g_object_unref);
5590 g_task_return_error (task, error);
5594 g_file_real_query_filesystem_info_async (GFile *file,
5595 const char *attributes,
5597 GCancellable *cancellable,
5598 GAsyncReadyCallback callback,
5603 task = g_task_new (file, cancellable, callback, user_data);
5604 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5605 g_task_set_task_data (task, g_strdup (attributes), g_free);
5606 g_task_set_priority (task, io_priority);
5607 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5608 g_object_unref (task);
5612 g_file_real_query_filesystem_info_finish (GFile *file,
5616 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5618 return g_task_propagate_pointer (G_TASK (res), error);
5622 enumerate_children_async_thread (GTask *task,
5625 GCancellable *cancellable)
5627 QueryInfoAsyncData *data = task_data;
5628 GFileEnumerator *enumerator;
5629 GError *error = NULL;
5631 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5633 g_task_return_error (task, error);
5635 g_task_return_pointer (task, enumerator, g_object_unref);
5639 g_file_real_enumerate_children_async (GFile *file,
5640 const char *attributes,
5641 GFileQueryInfoFlags flags,
5643 GCancellable *cancellable,
5644 GAsyncReadyCallback callback,
5648 QueryInfoAsyncData *data;
5650 data = g_new0 (QueryInfoAsyncData, 1);
5651 data->attributes = g_strdup (attributes);
5652 data->flags = flags;
5654 task = g_task_new (file, cancellable, callback, user_data);
5655 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5656 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5657 g_task_set_priority (task, io_priority);
5658 g_task_run_in_thread (task, enumerate_children_async_thread);
5659 g_object_unref (task);
5662 static GFileEnumerator *
5663 g_file_real_enumerate_children_finish (GFile *file,
5667 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5669 return g_task_propagate_pointer (G_TASK (res), error);
5673 open_read_async_thread (GTask *task,
5676 GCancellable *cancellable)
5678 GFileInputStream *stream;
5679 GError *error = NULL;
5681 stream = g_file_read (G_FILE (object), cancellable, &error);
5683 g_task_return_pointer (task, stream, g_object_unref);
5685 g_task_return_error (task, error);
5689 g_file_real_read_async (GFile *file,
5691 GCancellable *cancellable,
5692 GAsyncReadyCallback callback,
5697 task = g_task_new (file, cancellable, callback, user_data);
5698 g_task_set_source_tag (task, g_file_real_read_async);
5699 g_task_set_priority (task, io_priority);
5700 g_task_run_in_thread (task, open_read_async_thread);
5701 g_object_unref (task);
5704 static GFileInputStream *
5705 g_file_real_read_finish (GFile *file,
5709 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5711 return g_task_propagate_pointer (G_TASK (res), error);
5715 append_to_async_thread (GTask *task,
5716 gpointer source_object,
5718 GCancellable *cancellable)
5720 GFileCreateFlags *data = task_data;
5721 GFileOutputStream *stream;
5722 GError *error = NULL;
5724 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5726 g_task_return_pointer (task, stream, g_object_unref);
5728 g_task_return_error (task, error);
5732 g_file_real_append_to_async (GFile *file,
5733 GFileCreateFlags flags,
5735 GCancellable *cancellable,
5736 GAsyncReadyCallback callback,
5739 GFileCreateFlags *data;
5742 data = g_new0 (GFileCreateFlags, 1);
5745 task = g_task_new (file, cancellable, callback, user_data);
5746 g_task_set_source_tag (task, g_file_real_append_to_async);
5747 g_task_set_task_data (task, data, g_free);
5748 g_task_set_priority (task, io_priority);
5750 g_task_run_in_thread (task, append_to_async_thread);
5751 g_object_unref (task);
5754 static GFileOutputStream *
5755 g_file_real_append_to_finish (GFile *file,
5759 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5761 return g_task_propagate_pointer (G_TASK (res), error);
5765 create_async_thread (GTask *task,
5766 gpointer source_object,
5768 GCancellable *cancellable)
5770 GFileCreateFlags *data = task_data;
5771 GFileOutputStream *stream;
5772 GError *error = NULL;
5774 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5776 g_task_return_pointer (task, stream, g_object_unref);
5778 g_task_return_error (task, error);
5782 g_file_real_create_async (GFile *file,
5783 GFileCreateFlags flags,
5785 GCancellable *cancellable,
5786 GAsyncReadyCallback callback,
5789 GFileCreateFlags *data;
5792 data = g_new0 (GFileCreateFlags, 1);
5795 task = g_task_new (file, cancellable, callback, user_data);
5796 g_task_set_source_tag (task, g_file_real_create_async);
5797 g_task_set_task_data (task, data, g_free);
5798 g_task_set_priority (task, io_priority);
5800 g_task_run_in_thread (task, create_async_thread);
5801 g_object_unref (task);
5804 static GFileOutputStream *
5805 g_file_real_create_finish (GFile *file,
5809 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5811 return g_task_propagate_pointer (G_TASK (res), error);
5815 GFileOutputStream *stream;
5817 gboolean make_backup;
5818 GFileCreateFlags flags;
5822 replace_async_data_free (ReplaceAsyncData *data)
5825 g_object_unref (data->stream);
5826 g_free (data->etag);
5831 replace_async_thread (GTask *task,
5832 gpointer source_object,
5834 GCancellable *cancellable)
5836 GFileOutputStream *stream;
5837 ReplaceAsyncData *data = task_data;
5838 GError *error = NULL;
5840 stream = g_file_replace (G_FILE (source_object),
5848 g_task_return_pointer (task, stream, g_object_unref);
5850 g_task_return_error (task, error);
5854 g_file_real_replace_async (GFile *file,
5856 gboolean make_backup,
5857 GFileCreateFlags flags,
5859 GCancellable *cancellable,
5860 GAsyncReadyCallback callback,
5864 ReplaceAsyncData *data;
5866 data = g_new0 (ReplaceAsyncData, 1);
5867 data->etag = g_strdup (etag);
5868 data->make_backup = make_backup;
5869 data->flags = flags;
5871 task = g_task_new (file, cancellable, callback, user_data);
5872 g_task_set_source_tag (task, g_file_real_replace_async);
5873 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5874 g_task_set_priority (task, io_priority);
5876 g_task_run_in_thread (task, replace_async_thread);
5877 g_object_unref (task);
5880 static GFileOutputStream *
5881 g_file_real_replace_finish (GFile *file,
5885 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5887 return g_task_propagate_pointer (G_TASK (res), error);
5891 delete_async_thread (GTask *task,
5894 GCancellable *cancellable)
5896 GError *error = NULL;
5898 if (g_file_delete (G_FILE (object), cancellable, &error))
5899 g_task_return_boolean (task, TRUE);
5901 g_task_return_error (task, error);
5905 g_file_real_delete_async (GFile *file,
5907 GCancellable *cancellable,
5908 GAsyncReadyCallback callback,
5913 task = g_task_new (file, cancellable, callback, user_data);
5914 g_task_set_source_tag (task, g_file_real_delete_async);
5915 g_task_set_priority (task, io_priority);
5916 g_task_run_in_thread (task, delete_async_thread);
5917 g_object_unref (task);
5921 g_file_real_delete_finish (GFile *file,
5925 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5927 return g_task_propagate_boolean (G_TASK (res), error);
5931 trash_async_thread (GTask *task,
5934 GCancellable *cancellable)
5936 GError *error = NULL;
5938 if (g_file_trash (G_FILE (object), cancellable, &error))
5939 g_task_return_boolean (task, TRUE);
5941 g_task_return_error (task, error);
5945 g_file_real_trash_async (GFile *file,
5947 GCancellable *cancellable,
5948 GAsyncReadyCallback callback,
5953 task = g_task_new (file, cancellable, callback, user_data);
5954 g_task_set_source_tag (task, g_file_real_trash_async);
5955 g_task_set_priority (task, io_priority);
5956 g_task_run_in_thread (task, trash_async_thread);
5957 g_object_unref (task);
5961 g_file_real_trash_finish (GFile *file,
5965 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5967 return g_task_propagate_boolean (G_TASK (res), error);
5971 make_directory_async_thread (GTask *task,
5974 GCancellable *cancellable)
5976 GError *error = NULL;
5978 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5979 g_task_return_boolean (task, TRUE);
5981 g_task_return_error (task, error);
5985 g_file_real_make_directory_async (GFile *file,
5987 GCancellable *cancellable,
5988 GAsyncReadyCallback callback,
5993 task = g_task_new (file, cancellable, callback, user_data);
5994 g_task_set_source_tag (task, g_file_real_make_directory_async);
5995 g_task_set_priority (task, io_priority);
5996 g_task_run_in_thread (task, make_directory_async_thread);
5997 g_object_unref (task);
6001 g_file_real_make_directory_finish (GFile *file,
6005 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6007 return g_task_propagate_boolean (G_TASK (res), error);
6011 open_readwrite_async_thread (GTask *task,
6014 GCancellable *cancellable)
6016 GFileIOStream *stream;
6017 GError *error = NULL;
6019 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6022 g_task_return_error (task, error);
6024 g_task_return_pointer (task, stream, g_object_unref);
6028 g_file_real_open_readwrite_async (GFile *file,
6030 GCancellable *cancellable,
6031 GAsyncReadyCallback callback,
6036 task = g_task_new (file, cancellable, callback, user_data);
6037 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6038 g_task_set_priority (task, io_priority);
6040 g_task_run_in_thread (task, open_readwrite_async_thread);
6041 g_object_unref (task);
6044 static GFileIOStream *
6045 g_file_real_open_readwrite_finish (GFile *file,
6049 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6051 return g_task_propagate_pointer (G_TASK (res), error);
6055 create_readwrite_async_thread (GTask *task,
6058 GCancellable *cancellable)
6060 GFileCreateFlags *data = task_data;
6061 GFileIOStream *stream;
6062 GError *error = NULL;
6064 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6067 g_task_return_error (task, error);
6069 g_task_return_pointer (task, stream, g_object_unref);
6073 g_file_real_create_readwrite_async (GFile *file,
6074 GFileCreateFlags flags,
6076 GCancellable *cancellable,
6077 GAsyncReadyCallback callback,
6080 GFileCreateFlags *data;
6083 data = g_new0 (GFileCreateFlags, 1);
6086 task = g_task_new (file, cancellable, callback, user_data);
6087 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6088 g_task_set_task_data (task, data, g_free);
6089 g_task_set_priority (task, io_priority);
6091 g_task_run_in_thread (task, create_readwrite_async_thread);
6092 g_object_unref (task);
6095 static GFileIOStream *
6096 g_file_real_create_readwrite_finish (GFile *file,
6100 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6102 return g_task_propagate_pointer (G_TASK (res), error);
6107 gboolean make_backup;
6108 GFileCreateFlags flags;
6109 } ReplaceRWAsyncData;
6112 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6114 g_free (data->etag);
6119 replace_readwrite_async_thread (GTask *task,
6122 GCancellable *cancellable)
6124 GFileIOStream *stream;
6125 GError *error = NULL;
6126 ReplaceRWAsyncData *data = task_data;
6128 stream = g_file_replace_readwrite (G_FILE (object),
6136 g_task_return_error (task, error);
6138 g_task_return_pointer (task, stream, g_object_unref);
6142 g_file_real_replace_readwrite_async (GFile *file,
6144 gboolean make_backup,
6145 GFileCreateFlags flags,
6147 GCancellable *cancellable,
6148 GAsyncReadyCallback callback,
6152 ReplaceRWAsyncData *data;
6154 data = g_new0 (ReplaceRWAsyncData, 1);
6155 data->etag = g_strdup (etag);
6156 data->make_backup = make_backup;
6157 data->flags = flags;
6159 task = g_task_new (file, cancellable, callback, user_data);
6160 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6161 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6162 g_task_set_priority (task, io_priority);
6164 g_task_run_in_thread (task, replace_readwrite_async_thread);
6165 g_object_unref (task);
6168 static GFileIOStream *
6169 g_file_real_replace_readwrite_finish (GFile *file,
6173 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6175 return g_task_propagate_pointer (G_TASK (res), error);
6179 set_display_name_async_thread (GTask *task,
6182 GCancellable *cancellable)
6184 GError *error = NULL;
6185 char *name = task_data;
6188 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6191 g_task_return_error (task, error);
6193 g_task_return_pointer (task, file, g_object_unref);
6197 g_file_real_set_display_name_async (GFile *file,
6198 const char *display_name,
6200 GCancellable *cancellable,
6201 GAsyncReadyCallback callback,
6206 task = g_task_new (file, cancellable, callback, user_data);
6207 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6208 g_task_set_task_data (task, g_strdup (display_name), g_free);
6209 g_task_set_priority (task, io_priority);
6211 g_task_run_in_thread (task, set_display_name_async_thread);
6212 g_object_unref (task);
6216 g_file_real_set_display_name_finish (GFile *file,
6220 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6222 return g_task_propagate_pointer (G_TASK (res), error);
6226 GFileQueryInfoFlags flags;
6233 set_info_data_free (SetInfoAsyncData *data)
6236 g_object_unref (data->info);
6238 g_error_free (data->error);
6243 set_info_async_thread (GTask *task,
6246 GCancellable *cancellable)
6248 SetInfoAsyncData *data = task_data;
6251 data->res = g_file_set_attributes_from_info (G_FILE (object),
6259 g_file_real_set_attributes_async (GFile *file,
6261 GFileQueryInfoFlags flags,
6263 GCancellable *cancellable,
6264 GAsyncReadyCallback callback,
6268 SetInfoAsyncData *data;
6270 data = g_new0 (SetInfoAsyncData, 1);
6271 data->info = g_file_info_dup (info);
6272 data->flags = flags;
6274 task = g_task_new (file, cancellable, callback, user_data);
6275 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6276 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6277 g_task_set_priority (task, io_priority);
6279 g_task_run_in_thread (task, set_info_async_thread);
6280 g_object_unref (task);
6284 g_file_real_set_attributes_finish (GFile *file,
6289 SetInfoAsyncData *data;
6291 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6293 data = g_task_get_task_data (G_TASK (res));
6296 *info = g_object_ref (data->info);
6298 if (error != NULL && data->error)
6299 *error = g_error_copy (data->error);
6305 find_enclosing_mount_async_thread (GTask *task,
6308 GCancellable *cancellable)
6310 GError *error = NULL;
6313 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6316 g_task_return_error (task, error);
6318 g_task_return_pointer (task, mount, g_object_unref);
6322 g_file_real_find_enclosing_mount_async (GFile *file,
6324 GCancellable *cancellable,
6325 GAsyncReadyCallback callback,
6330 task = g_task_new (file, cancellable, callback, user_data);
6331 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6332 g_task_set_priority (task, io_priority);
6334 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6335 g_object_unref (task);
6339 g_file_real_find_enclosing_mount_finish (GFile *file,
6343 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6345 return g_task_propagate_pointer (G_TASK (res), error);
6352 GFileCopyFlags flags;
6353 GFileProgressCallback progress_cb;
6354 gpointer progress_cb_data;
6358 copy_async_data_free (CopyAsyncData *data)
6360 g_object_unref (data->source);
6361 g_object_unref (data->destination);
6362 g_slice_free (CopyAsyncData, data);
6366 CopyAsyncData *data;
6367 goffset current_num_bytes;
6368 goffset total_num_bytes;
6372 copy_async_progress_in_main (gpointer user_data)
6374 ProgressData *progress = user_data;
6375 CopyAsyncData *data = progress->data;
6377 data->progress_cb (progress->current_num_bytes,
6378 progress->total_num_bytes,
6379 data->progress_cb_data);
6385 copy_async_progress_callback (goffset current_num_bytes,
6386 goffset total_num_bytes,
6389 GTask *task = user_data;
6390 CopyAsyncData *data = g_task_get_task_data (task);
6391 ProgressData *progress;
6393 progress = g_new (ProgressData, 1);
6394 progress->data = data;
6395 progress->current_num_bytes = current_num_bytes;
6396 progress->total_num_bytes = total_num_bytes;
6398 g_main_context_invoke_full (g_task_get_context (task),
6399 g_task_get_priority (task),
6400 copy_async_progress_in_main,
6406 copy_async_thread (GTask *task,
6409 GCancellable *cancellable)
6411 CopyAsyncData *data = task_data;
6413 GError *error = NULL;
6415 result = g_file_copy (data->source,
6419 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6423 g_task_return_boolean (task, TRUE);
6425 g_task_return_error (task, error);
6429 g_file_real_copy_async (GFile *source,
6431 GFileCopyFlags flags,
6433 GCancellable *cancellable,
6434 GFileProgressCallback progress_callback,
6435 gpointer progress_callback_data,
6436 GAsyncReadyCallback callback,
6440 CopyAsyncData *data;
6442 data = g_slice_new (CopyAsyncData);
6443 data->source = g_object_ref (source);
6444 data->destination = g_object_ref (destination);
6445 data->flags = flags;
6446 data->progress_cb = progress_callback;
6447 data->progress_cb_data = progress_callback_data;
6449 task = g_task_new (source, cancellable, callback, user_data);
6450 g_task_set_source_tag (task, g_file_real_copy_async);
6451 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6452 g_task_set_priority (task, io_priority);
6453 g_task_run_in_thread (task, copy_async_thread);
6454 g_object_unref (task);
6458 g_file_real_copy_finish (GFile *file,
6462 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6464 return g_task_propagate_boolean (G_TASK (res), error);
6468 /********************************************
6469 * Default VFS operations *
6470 ********************************************/
6473 * g_file_new_for_path:
6474 * @path: (type filename): a string containing a relative or absolute path.
6475 * The string must be encoded in the glib filename encoding.
6477 * Constructs a #GFile for a given path. This operation never
6478 * fails, but the returned object might not support any I/O
6479 * operation if @path is malformed.
6481 * Returns: (transfer full): a new #GFile for the given @path.
6482 * Free the returned object with g_object_unref().
6485 g_file_new_for_path (const char *path)
6487 g_return_val_if_fail (path != NULL, NULL);
6489 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6493 * g_file_new_for_uri:
6494 * @uri: a UTF-8 string containing a URI
6496 * Constructs a #GFile for a given URI. This operation never
6497 * fails, but the returned object might not support any I/O
6498 * operation if @uri is malformed or if the uri type is
6501 * Returns: (transfer full): a new #GFile for the given @uri.
6502 * Free the returned object with g_object_unref().
6505 g_file_new_for_uri (const char *uri)
6507 g_return_val_if_fail (uri != NULL, NULL);
6509 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6514 * @tmpl: (type filename) (nullable): Template for the file
6515 * name, as in g_file_open_tmp(), or %NULL for a default template
6516 * @iostream: (out): on return, a #GFileIOStream for the created file
6517 * @error: a #GError, or %NULL
6519 * Opens a file in the preferred directory for temporary files (as
6520 * returned by g_get_tmp_dir()) and returns a #GFile and
6521 * #GFileIOStream pointing to it.
6523 * @tmpl should be a string in the GLib file name encoding
6524 * containing a sequence of six 'X' characters, and containing no
6525 * directory components. If it is %NULL, a default template is used.
6527 * Unlike the other #GFile constructors, this will return %NULL if
6528 * a temporary file could not be created.
6530 * Returns: (transfer full): a new #GFile.
6531 * Free the returned object with g_object_unref().
6536 g_file_new_tmp (const char *tmpl,
6537 GFileIOStream **iostream,
6543 GFileOutputStream *output;
6545 g_return_val_if_fail (iostream != NULL, NULL);
6547 fd = g_file_open_tmp (tmpl, &path, error);
6551 file = g_file_new_for_path (path);
6553 output = _g_local_file_output_stream_new (fd);
6554 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6556 g_object_unref (output);
6563 * g_file_parse_name:
6564 * @parse_name: a file name or path to be parsed
6566 * Constructs a #GFile with the given @parse_name (i.e. something
6567 * given by g_file_get_parse_name()). This operation never fails,
6568 * but the returned object might not support any I/O operation if
6569 * the @parse_name cannot be parsed.
6571 * Returns: (transfer full): a new #GFile.
6574 g_file_parse_name (const char *parse_name)
6576 g_return_val_if_fail (parse_name != NULL, NULL);
6578 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6582 * g_file_new_build_filename:
6583 * @first_element: (type filename): the first element in the path
6584 * @...: remaining elements in path, terminated by %NULL
6586 * Constructs a #GFile from a series of elements using the correct
6587 * separator for filenames.
6589 * Using this function is equivalent to calling g_build_filename(),
6590 * followed by g_file_new_for_path() on the result.
6592 * Returns: (transfer full): a new #GFile
6597 g_file_new_build_filename (const gchar *first_element,
6604 g_return_val_if_fail (first_element != NULL, NULL);
6606 va_start (args, first_element);
6607 str = g_build_filename_valist (first_element, &args);
6610 file = g_file_new_for_path (str);
6617 is_valid_scheme_character (char c)
6619 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6622 /* Following RFC 2396, valid schemes are built like:
6623 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6626 has_valid_scheme (const char *uri)
6632 if (!g_ascii_isalpha (*p))
6637 } while (is_valid_scheme_character (*p));
6643 new_for_cmdline_arg (const gchar *arg,
6649 if (g_path_is_absolute (arg))
6650 return g_file_new_for_path (arg);
6652 if (has_valid_scheme (arg))
6653 return g_file_new_for_uri (arg);
6659 current_dir = g_get_current_dir ();
6660 filename = g_build_filename (current_dir, arg, NULL);
6661 g_free (current_dir);
6664 filename = g_build_filename (cwd, arg, NULL);
6666 file = g_file_new_for_path (filename);
6673 * g_file_new_for_commandline_arg:
6674 * @arg: (type filename): a command line string
6676 * Creates a #GFile with the given argument from the command line.
6677 * The value of @arg can be either a URI, an absolute path or a
6678 * relative path resolved relative to the current working directory.
6679 * This operation never fails, but the returned object might not
6680 * support any I/O operation if @arg points to a malformed path.
6682 * Note that on Windows, this function expects its argument to be in
6683 * UTF-8 -- not the system code page. This means that you
6684 * should not use this function with string from argv as it is passed
6685 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6686 * the commandline. #GApplication also uses UTF-8 but
6687 * g_application_command_line_create_file_for_arg() may be more useful
6688 * for you there. It is also always possible to use this function with
6689 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6691 * Returns: (transfer full): a new #GFile.
6692 * Free the returned object with g_object_unref().
6695 g_file_new_for_commandline_arg (const char *arg)
6697 g_return_val_if_fail (arg != NULL, NULL);
6699 return new_for_cmdline_arg (arg, NULL);
6703 * g_file_new_for_commandline_arg_and_cwd:
6704 * @arg: (type filename): a command line string
6705 * @cwd: (type filename): the current working directory of the commandline
6707 * Creates a #GFile with the given argument from the command line.
6709 * This function is similar to g_file_new_for_commandline_arg() except
6710 * that it allows for passing the current working directory as an
6711 * argument instead of using the current working directory of the
6714 * This is useful if the commandline argument was given in a context
6715 * other than the invocation of the current process.
6717 * See also g_application_command_line_create_file_for_arg().
6719 * Returns: (transfer full): a new #GFile
6724 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6727 g_return_val_if_fail (arg != NULL, NULL);
6728 g_return_val_if_fail (cwd != NULL, NULL);
6730 return new_for_cmdline_arg (arg, cwd);
6734 * g_file_mount_enclosing_volume:
6735 * @location: input #GFile
6736 * @flags: flags affecting the operation
6737 * @mount_operation: (nullable): a #GMountOperation
6738 * or %NULL to avoid user interaction
6739 * @cancellable: (nullable): optional #GCancellable object,
6741 * @callback: (nullable): a #GAsyncReadyCallback to call
6742 * when the request is satisfied, or %NULL
6743 * @user_data: the data to pass to callback function
6745 * Starts a @mount_operation, mounting the volume that contains
6746 * the file @location.
6748 * When this operation has completed, @callback will be called with
6749 * @user_user data, and the operation can be finalized with
6750 * g_file_mount_enclosing_volume_finish().
6752 * If @cancellable is not %NULL, then the operation can be cancelled by
6753 * triggering the cancellable object from another thread. If the operation
6754 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6757 g_file_mount_enclosing_volume (GFile *location,
6758 GMountMountFlags flags,
6759 GMountOperation *mount_operation,
6760 GCancellable *cancellable,
6761 GAsyncReadyCallback callback,
6766 g_return_if_fail (G_IS_FILE (location));
6768 iface = G_FILE_GET_IFACE (location);
6770 if (iface->mount_enclosing_volume == NULL)
6772 g_task_report_new_error (location, callback, user_data,
6773 g_file_mount_enclosing_volume,
6774 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6775 _("volume doesn’t implement mount"));
6779 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6784 * g_file_mount_enclosing_volume_finish:
6785 * @location: input #GFile
6786 * @result: a #GAsyncResult
6787 * @error: a #GError, or %NULL
6789 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6791 * Returns: %TRUE if successful. If an error has occurred,
6792 * this function will return %FALSE and set @error
6793 * appropriately if present.
6796 g_file_mount_enclosing_volume_finish (GFile *location,
6797 GAsyncResult *result,
6802 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6803 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6805 if (g_async_result_legacy_propagate_error (result, error))
6807 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6808 return g_task_propagate_boolean (G_TASK (result), error);
6810 iface = G_FILE_GET_IFACE (location);
6812 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6815 /********************************************
6816 * Utility functions *
6817 ********************************************/
6820 * g_file_query_default_handler:
6821 * @file: a #GFile to open
6822 * @cancellable: optional #GCancellable object, %NULL to ignore
6823 * @error: a #GError, or %NULL
6825 * Returns the #GAppInfo that is registered as the default
6826 * application to handle the file specified by @file.
6828 * If @cancellable is not %NULL, then the operation can be cancelled by
6829 * triggering the cancellable object from another thread. If the operation
6830 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6832 * Returns: (transfer full): a #GAppInfo if the handle was found,
6833 * %NULL if there were errors.
6834 * When you are done with it, release it with g_object_unref()
6837 g_file_query_default_handler (GFile *file,
6838 GCancellable *cancellable,
6842 const char *content_type;
6847 uri_scheme = g_file_get_uri_scheme (file);
6848 if (uri_scheme && uri_scheme[0] != '\0')
6850 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6851 g_free (uri_scheme);
6853 if (appinfo != NULL)
6857 g_free (uri_scheme);
6859 info = g_file_query_info (file,
6860 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
6861 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
6870 content_type = g_file_info_get_content_type (info);
6871 if (content_type == NULL)
6872 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
6875 /* Don't use is_native(), as we want to support fuse paths if available */
6876 path = g_file_get_path (file);
6877 appinfo = g_app_info_get_default_for_type (content_type,
6882 g_object_unref (info);
6884 if (appinfo != NULL)
6887 g_set_error_literal (error, G_IO_ERROR,
6888 G_IO_ERROR_NOT_SUPPORTED,
6889 _("No application is registered as handling this file"));
6894 query_default_handler_query_info_cb (GObject *object,
6895 GAsyncResult *result,
6898 GFile *file = G_FILE (object);
6899 GTask *task = G_TASK (user_data);
6900 GError *error = NULL;
6902 const char *content_type;
6903 GAppInfo *appinfo = NULL;
6905 info = g_file_query_info_finish (file, result, &error);
6908 g_task_return_error (task, g_steal_pointer (&error));
6909 g_object_unref (task);
6913 content_type = g_file_info_get_content_type (info);
6914 if (content_type == NULL)
6915 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
6920 /* Don't use is_native(), as we want to support fuse paths if available */
6921 path = g_file_get_path (file);
6923 /* FIXME: The following still uses blocking calls. */
6924 appinfo = g_app_info_get_default_for_type (content_type,
6929 g_object_unref (info);
6931 if (appinfo != NULL)
6932 g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6934 g_task_return_new_error (task,
6936 G_IO_ERROR_NOT_SUPPORTED,
6937 _("No application is registered as handling this file"));
6938 g_object_unref (task);
6942 * g_file_query_default_handler_async:
6943 * @file: a #GFile to open
6944 * @io_priority: the [I/O priority][io-priority] of the request
6945 * @cancellable: optional #GCancellable object, %NULL to ignore
6946 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
6947 * @user_data: (nullable): data to pass to @callback
6949 * Async version of g_file_query_default_handler().
6954 g_file_query_default_handler_async (GFile *file,
6956 GCancellable *cancellable,
6957 GAsyncReadyCallback callback,
6963 task = g_task_new (file, cancellable, callback, user_data);
6964 g_task_set_source_tag (task, g_file_query_default_handler_async);
6966 uri_scheme = g_file_get_uri_scheme (file);
6967 if (uri_scheme && uri_scheme[0] != '\0')
6971 /* FIXME: The following still uses blocking calls. */
6972 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6973 g_free (uri_scheme);
6975 if (appinfo != NULL)
6977 g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6978 g_object_unref (task);
6983 g_free (uri_scheme);
6985 g_file_query_info_async (file,
6986 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
6987 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
6991 query_default_handler_query_info_cb,
6992 g_steal_pointer (&task));
6996 * g_file_query_default_handler_finish:
6997 * @file: a #GFile to open
6998 * @result: a #GAsyncResult
6999 * @error: (nullable): a #GError
7001 * Finishes a g_file_query_default_handler_async() operation.
7003 * Returns: (transfer full): a #GAppInfo if the handle was found,
7004 * %NULL if there were errors.
7005 * When you are done with it, release it with g_object_unref()
7010 g_file_query_default_handler_finish (GFile *file,
7011 GAsyncResult *result,
7014 g_return_val_if_fail (G_IS_FILE (file), NULL);
7015 g_return_val_if_fail (g_task_is_valid (result, file), NULL);
7017 return g_task_propagate_pointer (G_TASK (result), error);
7020 #define GET_CONTENT_BLOCK_SIZE 8192
7023 * g_file_load_contents:
7024 * @file: input #GFile
7025 * @cancellable: optional #GCancellable object, %NULL to ignore
7026 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7027 * @length: (out) (optional): a location to place the length of the contents of the file,
7028 * or %NULL if the length is not needed
7029 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7030 * or %NULL if the entity tag is not needed
7031 * @error: a #GError, or %NULL
7033 * Loads the content of the file into memory. The data is always
7034 * zero-terminated, but this is not included in the resultant @length.
7035 * The returned @contents should be freed with g_free() when no longer
7038 * If @cancellable is not %NULL, then the operation can be cancelled by
7039 * triggering the cancellable object from another thread. If the operation
7040 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7042 * Returns: %TRUE if the @file's contents were successfully loaded.
7043 * %FALSE if there were errors.
7046 g_file_load_contents (GFile *file,
7047 GCancellable *cancellable,
7053 GFileInputStream *in;
7054 GByteArray *content;
7059 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7060 g_return_val_if_fail (contents != NULL, FALSE);
7062 in = g_file_read (file, cancellable, error);
7066 content = g_byte_array_new ();
7069 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7070 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
7071 content->data + pos,
7072 GET_CONTENT_BLOCK_SIZE,
7073 cancellable, error)) > 0)
7076 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7083 info = g_file_input_stream_query_info (in,
7084 G_FILE_ATTRIBUTE_ETAG_VALUE,
7089 *etag_out = g_strdup (g_file_info_get_etag (info));
7090 g_object_unref (info);
7094 /* Ignore errors on close */
7095 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
7096 g_object_unref (in);
7100 /* error is set already */
7101 g_byte_array_free (content, TRUE);
7108 /* Zero terminate (we got an extra byte allocated for this */
7109 content->data[pos] = 0;
7111 *contents = (char *)g_byte_array_free (content, FALSE);
7118 GFileReadMoreCallback read_more_callback;
7119 GByteArray *content;
7126 load_contents_data_free (LoadContentsData *data)
7129 g_byte_array_free (data->content, TRUE);
7130 g_free (data->etag);
7135 load_contents_close_callback (GObject *obj,
7136 GAsyncResult *close_res,
7139 GInputStream *stream = G_INPUT_STREAM (obj);
7140 LoadContentsData *data = user_data;
7142 /* Ignore errors here, we're only reading anyway */
7143 g_input_stream_close_finish (stream, close_res, NULL);
7144 g_object_unref (stream);
7146 g_task_return_boolean (data->task, TRUE);
7147 g_object_unref (data->task);
7151 load_contents_fstat_callback (GObject *obj,
7152 GAsyncResult *stat_res,
7155 GInputStream *stream = G_INPUT_STREAM (obj);
7156 LoadContentsData *data = user_data;
7159 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
7163 data->etag = g_strdup (g_file_info_get_etag (info));
7164 g_object_unref (info);
7167 g_input_stream_close_async (stream, 0,
7168 g_task_get_cancellable (data->task),
7169 load_contents_close_callback, data);
7173 load_contents_read_callback (GObject *obj,
7174 GAsyncResult *read_res,
7177 GInputStream *stream = G_INPUT_STREAM (obj);
7178 LoadContentsData *data = user_data;
7179 GError *error = NULL;
7182 read_size = g_input_stream_read_finish (stream, read_res, &error);
7186 g_task_return_error (data->task, error);
7187 g_object_unref (data->task);
7189 /* Close the file ignoring any error */
7190 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7191 g_object_unref (stream);
7193 else if (read_size == 0)
7195 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7196 G_FILE_ATTRIBUTE_ETAG_VALUE,
7198 g_task_get_cancellable (data->task),
7199 load_contents_fstat_callback,
7202 else if (read_size > 0)
7204 data->pos += read_size;
7206 g_byte_array_set_size (data->content,
7207 data->pos + GET_CONTENT_BLOCK_SIZE);
7210 if (data->read_more_callback &&
7211 !data->read_more_callback ((char *)data->content->data, data->pos,
7212 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
7213 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7214 G_FILE_ATTRIBUTE_ETAG_VALUE,
7216 g_task_get_cancellable (data->task),
7217 load_contents_fstat_callback,
7220 g_input_stream_read_async (stream,
7221 data->content->data + data->pos,
7222 GET_CONTENT_BLOCK_SIZE,
7224 g_task_get_cancellable (data->task),
7225 load_contents_read_callback,
7231 load_contents_open_callback (GObject *obj,
7232 GAsyncResult *open_res,
7235 GFile *file = G_FILE (obj);
7236 GFileInputStream *stream;
7237 LoadContentsData *data = user_data;
7238 GError *error = NULL;
7240 stream = g_file_read_finish (file, open_res, &error);
7244 g_byte_array_set_size (data->content,
7245 data->pos + GET_CONTENT_BLOCK_SIZE);
7246 g_input_stream_read_async (G_INPUT_STREAM (stream),
7247 data->content->data + data->pos,
7248 GET_CONTENT_BLOCK_SIZE,
7250 g_task_get_cancellable (data->task),
7251 load_contents_read_callback,
7256 g_task_return_error (data->task, error);
7257 g_object_unref (data->task);
7262 * g_file_load_partial_contents_async: (skip)
7263 * @file: input #GFile
7264 * @cancellable: optional #GCancellable object, %NULL to ignore
7265 * @read_more_callback: (scope call) (closure user_data): a
7266 * #GFileReadMoreCallback to receive partial data
7267 * and to specify whether further data should be read
7268 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7269 * when the request is satisfied
7270 * @user_data: the data to pass to the callback functions
7272 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7273 * be used to stop reading from the file when appropriate, else this
7274 * function will behave exactly as g_file_load_contents_async(). This
7275 * operation can be finished by g_file_load_partial_contents_finish().
7277 * Users of this function should be aware that @user_data is passed to
7278 * both the @read_more_callback and the @callback.
7280 * If @cancellable is not %NULL, then the operation can be cancelled by
7281 * triggering the cancellable object from another thread. If the operation
7282 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7285 g_file_load_partial_contents_async (GFile *file,
7286 GCancellable *cancellable,
7287 GFileReadMoreCallback read_more_callback,
7288 GAsyncReadyCallback callback,
7291 LoadContentsData *data;
7293 g_return_if_fail (G_IS_FILE (file));
7295 data = g_new0 (LoadContentsData, 1);
7296 data->read_more_callback = read_more_callback;
7297 data->content = g_byte_array_new ();
7299 data->task = g_task_new (file, cancellable, callback, user_data);
7300 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7301 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7303 g_file_read_async (file,
7305 g_task_get_cancellable (data->task),
7306 load_contents_open_callback,
7311 * g_file_load_partial_contents_finish:
7312 * @file: input #GFile
7313 * @res: a #GAsyncResult
7314 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7315 * @length: (out) (optional): a location to place the length of the contents of the file,
7316 * or %NULL if the length is not needed
7317 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7318 * or %NULL if the entity tag is not needed
7319 * @error: a #GError, or %NULL
7321 * Finishes an asynchronous partial load operation that was started
7322 * with g_file_load_partial_contents_async(). The data is always
7323 * zero-terminated, but this is not included in the resultant @length.
7324 * The returned @contents should be freed with g_free() when no longer
7327 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7328 * present, it will be set appropriately.
7331 g_file_load_partial_contents_finish (GFile *file,
7339 LoadContentsData *data;
7341 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7342 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7343 g_return_val_if_fail (contents != NULL, FALSE);
7345 task = G_TASK (res);
7347 if (!g_task_propagate_boolean (task, error))
7354 data = g_task_get_task_data (task);
7357 *length = data->pos;
7361 *etag_out = data->etag;
7365 /* Zero terminate */
7366 g_byte_array_set_size (data->content, data->pos + 1);
7367 data->content->data[data->pos] = 0;
7369 *contents = (char *)g_byte_array_free (data->content, FALSE);
7370 data->content = NULL;
7376 * g_file_load_contents_async:
7377 * @file: input #GFile
7378 * @cancellable: optional #GCancellable object, %NULL to ignore
7379 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7380 * @user_data: the data to pass to callback function
7382 * Starts an asynchronous load of the @file's contents.
7384 * For more details, see g_file_load_contents() which is
7385 * the synchronous version of this call.
7387 * When the load operation has completed, @callback will be called
7388 * with @user data. To finish the operation, call
7389 * g_file_load_contents_finish() with the #GAsyncResult returned by
7392 * If @cancellable is not %NULL, then the operation can be cancelled by
7393 * triggering the cancellable object from another thread. If the operation
7394 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7397 g_file_load_contents_async (GFile *file,
7398 GCancellable *cancellable,
7399 GAsyncReadyCallback callback,
7402 g_file_load_partial_contents_async (file,
7405 callback, user_data);
7409 * g_file_load_contents_finish:
7410 * @file: input #GFile
7411 * @res: a #GAsyncResult
7412 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7413 * @length: (out) (optional): a location to place the length of the contents of the file,
7414 * or %NULL if the length is not needed
7415 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7416 * or %NULL if the entity tag is not needed
7417 * @error: a #GError, or %NULL
7419 * Finishes an asynchronous load of the @file's contents.
7420 * The contents are placed in @contents, and @length is set to the
7421 * size of the @contents string. The @contents should be freed with
7422 * g_free() when no longer needed. If @etag_out is present, it will be
7423 * set to the new entity tag for the @file.
7425 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7426 * present, it will be set appropriately.
7429 g_file_load_contents_finish (GFile *file,
7436 return g_file_load_partial_contents_finish (file,
7445 * g_file_replace_contents:
7446 * @file: input #GFile
7447 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7448 * @length: the length of @contents in bytes
7449 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7451 * @make_backup: %TRUE if a backup should be created
7452 * @flags: a set of #GFileCreateFlags
7453 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7454 * for the document. This should be freed with g_free() when no longer
7456 * @cancellable: optional #GCancellable object, %NULL to ignore
7457 * @error: a #GError, or %NULL
7459 * Replaces the contents of @file with @contents of @length bytes.
7461 * If @etag is specified (not %NULL), any existing file must have that etag,
7462 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7464 * If @make_backup is %TRUE, this function will attempt to make a backup
7465 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7466 * file contents in the safest way possible. For example, atomic renames are
7467 * used when replacing local files’ contents.
7469 * If @cancellable is not %NULL, then the operation can be cancelled by
7470 * triggering the cancellable object from another thread. If the operation
7471 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7473 * The returned @new_etag can be used to verify that the file hasn't
7474 * changed the next time it is saved over.
7476 * Returns: %TRUE if successful. If an error has occurred, this function
7477 * will return %FALSE and set @error appropriately if present.
7480 g_file_replace_contents (GFile *file,
7481 const char *contents,
7484 gboolean make_backup,
7485 GFileCreateFlags flags,
7487 GCancellable *cancellable,
7490 GFileOutputStream *out;
7491 gsize pos, remainder;
7495 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7496 g_return_val_if_fail (contents != NULL, FALSE);
7498 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7504 while (remainder > 0 &&
7505 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7507 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7515 if (remainder > 0 && res < 0)
7517 /* Ignore errors on close */
7518 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7519 g_object_unref (out);
7521 /* error is set already */
7525 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7528 *new_etag = g_file_output_stream_get_etag (out);
7530 g_object_unref (out);
7541 } ReplaceContentsData;
7544 replace_contents_data_free (ReplaceContentsData *data)
7546 g_bytes_unref (data->content);
7547 g_free (data->etag);
7552 replace_contents_close_callback (GObject *obj,
7553 GAsyncResult *close_res,
7556 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7557 ReplaceContentsData *data = user_data;
7559 /* Ignore errors here, we're only reading anyway */
7560 g_output_stream_close_finish (stream, close_res, NULL);
7561 g_object_unref (stream);
7565 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7566 g_task_return_boolean (data->task, TRUE);
7568 g_object_unref (data->task);
7572 replace_contents_write_callback (GObject *obj,
7573 GAsyncResult *read_res,
7576 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7577 ReplaceContentsData *data = user_data;
7578 GError *error = NULL;
7581 write_size = g_output_stream_write_finish (stream, read_res, &error);
7583 if (write_size <= 0)
7585 /* Error or EOF, close the file */
7588 data->failed = TRUE;
7589 g_task_return_error (data->task, error);
7591 g_output_stream_close_async (stream, 0,
7592 g_task_get_cancellable (data->task),
7593 replace_contents_close_callback, data);
7595 else if (write_size > 0)
7597 const gchar *content;
7600 content = g_bytes_get_data (data->content, &length);
7601 data->pos += write_size;
7603 if (data->pos >= length)
7604 g_output_stream_close_async (stream, 0,
7605 g_task_get_cancellable (data->task),
7606 replace_contents_close_callback, data);
7608 g_output_stream_write_async (stream,
7609 content + data->pos,
7612 g_task_get_cancellable (data->task),
7613 replace_contents_write_callback,
7619 replace_contents_open_callback (GObject *obj,
7620 GAsyncResult *open_res,
7623 GFile *file = G_FILE (obj);
7624 GFileOutputStream *stream;
7625 ReplaceContentsData *data = user_data;
7626 GError *error = NULL;
7628 stream = g_file_replace_finish (file, open_res, &error);
7632 const gchar *content;
7635 content = g_bytes_get_data (data->content, &length);
7636 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7637 content + data->pos,
7640 g_task_get_cancellable (data->task),
7641 replace_contents_write_callback,
7646 g_task_return_error (data->task, error);
7647 g_object_unref (data->task);
7652 * g_file_replace_contents_async:
7653 * @file: input #GFile
7654 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7655 * @length: the length of @contents in bytes
7656 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7657 * @make_backup: %TRUE if a backup should be created
7658 * @flags: a set of #GFileCreateFlags
7659 * @cancellable: optional #GCancellable object, %NULL to ignore
7660 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7661 * @user_data: the data to pass to callback function
7663 * Starts an asynchronous replacement of @file with the given
7664 * @contents of @length bytes. @etag will replace the document's
7665 * current entity tag.
7667 * When this operation has completed, @callback will be called with
7668 * @user_user data, and the operation can be finalized with
7669 * g_file_replace_contents_finish().
7671 * If @cancellable is not %NULL, then the operation can be cancelled by
7672 * triggering the cancellable object from another thread. If the operation
7673 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7675 * If @make_backup is %TRUE, this function will attempt to
7676 * make a backup of @file.
7678 * Note that no copy of @contents will be made, so it must stay valid
7679 * until @callback is called. See g_file_replace_contents_bytes_async()
7680 * for a #GBytes version that will automatically hold a reference to the
7681 * contents (without copying) for the duration of the call.
7684 g_file_replace_contents_async (GFile *file,
7685 const char *contents,
7688 gboolean make_backup,
7689 GFileCreateFlags flags,
7690 GCancellable *cancellable,
7691 GAsyncReadyCallback callback,
7696 bytes = g_bytes_new_static (contents, length);
7697 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7698 cancellable, callback, user_data);
7699 g_bytes_unref (bytes);
7703 * g_file_replace_contents_bytes_async:
7704 * @file: input #GFile
7705 * @contents: a #GBytes
7706 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7707 * @make_backup: %TRUE if a backup should be created
7708 * @flags: a set of #GFileCreateFlags
7709 * @cancellable: optional #GCancellable object, %NULL to ignore
7710 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7711 * @user_data: the data to pass to callback function
7713 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7714 * This function will keep a ref on @contents until the operation is done.
7715 * Unlike g_file_replace_contents_async() this allows forgetting about the
7716 * content without waiting for the callback.
7718 * When this operation has completed, @callback will be called with
7719 * @user_user data, and the operation can be finalized with
7720 * g_file_replace_contents_finish().
7725 g_file_replace_contents_bytes_async (GFile *file,
7728 gboolean make_backup,
7729 GFileCreateFlags flags,
7730 GCancellable *cancellable,
7731 GAsyncReadyCallback callback,
7734 ReplaceContentsData *data;
7736 g_return_if_fail (G_IS_FILE (file));
7737 g_return_if_fail (contents != NULL);
7739 data = g_new0 (ReplaceContentsData, 1);
7741 data->content = g_bytes_ref (contents);
7743 data->task = g_task_new (file, cancellable, callback, user_data);
7744 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7745 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7747 g_file_replace_async (file,
7752 g_task_get_cancellable (data->task),
7753 replace_contents_open_callback,
7758 * g_file_replace_contents_finish:
7759 * @file: input #GFile
7760 * @res: a #GAsyncResult
7761 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7762 * for the document. This should be freed with g_free() when it is no
7763 * longer needed, or %NULL
7764 * @error: a #GError, or %NULL
7766 * Finishes an asynchronous replace of the given @file. See
7767 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7768 * tag for the document, if present.
7770 * Returns: %TRUE on success, %FALSE on failure.
7773 g_file_replace_contents_finish (GFile *file,
7779 ReplaceContentsData *data;
7781 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7782 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7784 task = G_TASK (res);
7786 if (!g_task_propagate_boolean (task, error))
7789 data = g_task_get_task_data (task);
7793 *new_etag = data->etag;
7794 data->etag = NULL; /* Take ownership */
7801 g_file_real_measure_disk_usage (GFile *file,
7802 GFileMeasureFlags flags,
7803 GCancellable *cancellable,
7804 GFileMeasureProgressCallback progress_callback,
7805 gpointer progress_data,
7806 guint64 *disk_usage,
7811 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7812 "Operation not supported for the current backend.");
7818 GFileMeasureFlags flags;
7819 GFileMeasureProgressCallback progress_callback;
7820 gpointer progress_data;
7832 GFileMeasureProgressCallback callback;
7835 guint64 current_size;
7841 measure_disk_usage_invoke_progress (gpointer user_data)
7843 MeasureProgress *progress = user_data;
7845 (* progress->callback) (progress->reporting,
7846 progress->current_size, progress->num_dirs, progress->num_files,
7847 progress->user_data);
7853 measure_disk_usage_progress (gboolean reporting,
7854 guint64 current_size,
7859 MeasureProgress progress;
7860 GTask *task = user_data;
7861 MeasureTaskData *data;
7863 data = g_task_get_task_data (task);
7865 progress.callback = data->progress_callback;
7866 progress.user_data = data->progress_data;
7867 progress.reporting = reporting;
7868 progress.current_size = current_size;
7869 progress.num_dirs = num_dirs;
7870 progress.num_files = num_files;
7872 g_main_context_invoke_full (g_task_get_context (task),
7873 g_task_get_priority (task),
7874 measure_disk_usage_invoke_progress,
7875 g_memdup2 (&progress, sizeof progress),
7880 measure_disk_usage_thread (GTask *task,
7881 gpointer source_object,
7883 GCancellable *cancellable)
7885 MeasureTaskData *data = task_data;
7886 GError *error = NULL;
7887 MeasureResult result = { 0, };
7889 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7890 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7891 &result.disk_usage, &result.num_dirs, &result.num_files,
7893 g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
7895 g_task_return_error (task, error);
7899 g_file_real_measure_disk_usage_async (GFile *file,
7900 GFileMeasureFlags flags,
7902 GCancellable *cancellable,
7903 GFileMeasureProgressCallback progress_callback,
7904 gpointer progress_data,
7905 GAsyncReadyCallback callback,
7908 MeasureTaskData data;
7912 data.progress_callback = progress_callback;
7913 data.progress_data = progress_data;
7915 task = g_task_new (file, cancellable, callback, user_data);
7916 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7917 g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
7918 g_task_set_priority (task, io_priority);
7920 g_task_run_in_thread (task, measure_disk_usage_thread);
7921 g_object_unref (task);
7925 g_file_real_measure_disk_usage_finish (GFile *file,
7926 GAsyncResult *result,
7927 guint64 *disk_usage,
7932 MeasureResult *measure_result;
7934 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7936 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7938 if (measure_result == NULL)
7942 *disk_usage = measure_result->disk_usage;
7945 *num_dirs = measure_result->num_dirs;
7948 *num_files = measure_result->num_files;
7950 g_free (measure_result);
7956 * g_file_measure_disk_usage:
7958 * @flags: #GFileMeasureFlags
7959 * @cancellable: (nullable): optional #GCancellable
7960 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7961 * @progress_data: user_data for @progress_callback
7962 * @disk_usage: (out) (optional): the number of bytes of disk space used
7963 * @num_dirs: (out) (optional): the number of directories encountered
7964 * @num_files: (out) (optional): the number of non-directories encountered
7965 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7967 * Recursively measures the disk usage of @file.
7969 * This is essentially an analog of the 'du' command, but it also
7970 * reports the number of directories and non-directory files encountered
7971 * (including things like symbolic links).
7973 * By default, errors are only reported against the toplevel file
7974 * itself. Errors found while recursing are silently ignored, unless
7975 * %G_FILE_MEASURE_REPORT_ANY_ERROR is given in @flags.
7977 * The returned size, @disk_usage, is in bytes and should be formatted
7978 * with g_format_size() in order to get something reasonable for showing
7979 * in a user interface.
7981 * @progress_callback and @progress_data can be given to request
7982 * periodic progress updates while scanning. See the documentation for
7983 * #GFileMeasureProgressCallback for information about when and how the
7984 * callback will be invoked.
7986 * Returns: %TRUE if successful, with the out parameters set.
7987 * %FALSE otherwise, with @error set.
7992 g_file_measure_disk_usage (GFile *file,
7993 GFileMeasureFlags flags,
7994 GCancellable *cancellable,
7995 GFileMeasureProgressCallback progress_callback,
7996 gpointer progress_data,
7997 guint64 *disk_usage,
8002 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8003 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
8004 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8006 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
8007 progress_callback, progress_data,
8008 disk_usage, num_dirs, num_files,
8013 * g_file_measure_disk_usage_async:
8015 * @flags: #GFileMeasureFlags
8016 * @io_priority: the [I/O priority][io-priority] of the request
8017 * @cancellable: (nullable): optional #GCancellable
8018 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
8019 * @progress_data: user_data for @progress_callback
8020 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
8021 * @user_data: the data to pass to callback function
8023 * Recursively measures the disk usage of @file.
8025 * This is the asynchronous version of g_file_measure_disk_usage(). See
8026 * there for more information.
8031 g_file_measure_disk_usage_async (GFile *file,
8032 GFileMeasureFlags flags,
8034 GCancellable *cancellable,
8035 GFileMeasureProgressCallback progress_callback,
8036 gpointer progress_data,
8037 GAsyncReadyCallback callback,
8040 g_return_if_fail (G_IS_FILE (file));
8041 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8043 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
8044 progress_callback, progress_data,
8045 callback, user_data);
8049 * g_file_measure_disk_usage_finish:
8051 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
8052 * @disk_usage: (out) (optional): the number of bytes of disk space used
8053 * @num_dirs: (out) (optional): the number of directories encountered
8054 * @num_files: (out) (optional): the number of non-directories encountered
8055 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
8057 * Collects the results from an earlier call to
8058 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
8061 * Returns: %TRUE if successful, with the out parameters set.
8062 * %FALSE otherwise, with @error set.
8067 g_file_measure_disk_usage_finish (GFile *file,
8068 GAsyncResult *result,
8069 guint64 *disk_usage,
8074 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8075 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8077 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
8081 * g_file_start_mountable:
8082 * @file: input #GFile
8083 * @flags: flags affecting the operation
8084 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
8085 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
8086 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
8087 * @user_data: the data to pass to callback function
8089 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
8090 * Using @start_operation, you can request callbacks when, for instance,
8091 * passwords are needed during authentication.
8093 * If @cancellable is not %NULL, then the operation can be cancelled by
8094 * triggering the cancellable object from another thread. If the operation
8095 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8097 * When the operation is finished, @callback will be called.
8098 * You can then call g_file_mount_mountable_finish() to get
8099 * the result of the operation.
8104 g_file_start_mountable (GFile *file,
8105 GDriveStartFlags flags,
8106 GMountOperation *start_operation,
8107 GCancellable *cancellable,
8108 GAsyncReadyCallback callback,
8113 g_return_if_fail (G_IS_FILE (file));
8115 iface = G_FILE_GET_IFACE (file);
8117 if (iface->start_mountable == NULL)
8119 g_task_report_new_error (file, callback, user_data,
8120 g_file_start_mountable,
8121 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8122 _("Operation not supported"));
8126 (* iface->start_mountable) (file,
8135 * g_file_start_mountable_finish:
8136 * @file: input #GFile
8137 * @result: a #GAsyncResult
8138 * @error: a #GError, or %NULL
8140 * Finishes a start operation. See g_file_start_mountable() for details.
8142 * Finish an asynchronous start operation that was started
8143 * with g_file_start_mountable().
8145 * Returns: %TRUE if the operation finished successfully. %FALSE
8151 g_file_start_mountable_finish (GFile *file,
8152 GAsyncResult *result,
8157 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8158 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8160 if (g_async_result_legacy_propagate_error (result, error))
8162 else if (g_async_result_is_tagged (result, g_file_start_mountable))
8163 return g_task_propagate_boolean (G_TASK (result), error);
8165 iface = G_FILE_GET_IFACE (file);
8166 return (* iface->start_mountable_finish) (file, result, error);
8170 * g_file_stop_mountable:
8171 * @file: input #GFile
8172 * @flags: flags affecting the operation
8173 * @mount_operation: (nullable): a #GMountOperation,
8174 * or %NULL to avoid user interaction.
8175 * @cancellable: (nullable): optional #GCancellable object,
8177 * @callback: (nullable): a #GAsyncReadyCallback to call
8178 * when the request is satisfied, or %NULL
8179 * @user_data: the data to pass to callback function
8181 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8183 * If @cancellable is not %NULL, then the operation can be cancelled by
8184 * triggering the cancellable object from another thread. If the operation
8185 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8187 * When the operation is finished, @callback will be called.
8188 * You can then call g_file_stop_mountable_finish() to get
8189 * the result of the operation.
8194 g_file_stop_mountable (GFile *file,
8195 GMountUnmountFlags flags,
8196 GMountOperation *mount_operation,
8197 GCancellable *cancellable,
8198 GAsyncReadyCallback callback,
8203 g_return_if_fail (G_IS_FILE (file));
8205 iface = G_FILE_GET_IFACE (file);
8207 if (iface->stop_mountable == NULL)
8209 g_task_report_new_error (file, callback, user_data,
8210 g_file_stop_mountable,
8211 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8212 _("Operation not supported"));
8216 (* iface->stop_mountable) (file,
8225 * g_file_stop_mountable_finish:
8226 * @file: input #GFile
8227 * @result: a #GAsyncResult
8228 * @error: a #GError, or %NULL
8230 * Finishes a stop operation, see g_file_stop_mountable() for details.
8232 * Finish an asynchronous stop operation that was started
8233 * with g_file_stop_mountable().
8235 * Returns: %TRUE if the operation finished successfully.
8241 g_file_stop_mountable_finish (GFile *file,
8242 GAsyncResult *result,
8247 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8248 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8250 if (g_async_result_legacy_propagate_error (result, error))
8252 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8253 return g_task_propagate_boolean (G_TASK (result), error);
8255 iface = G_FILE_GET_IFACE (file);
8256 return (* iface->stop_mountable_finish) (file, result, error);
8260 * g_file_poll_mountable:
8261 * @file: input #GFile
8262 * @cancellable: optional #GCancellable object, %NULL to ignore
8263 * @callback: (nullable): a #GAsyncReadyCallback to call
8264 * when the request is satisfied, or %NULL
8265 * @user_data: the data to pass to callback function
8267 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8269 * If @cancellable is not %NULL, then the operation can be cancelled by
8270 * triggering the cancellable object from another thread. If the operation
8271 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8273 * When the operation is finished, @callback will be called.
8274 * You can then call g_file_mount_mountable_finish() to get
8275 * the result of the operation.
8280 g_file_poll_mountable (GFile *file,
8281 GCancellable *cancellable,
8282 GAsyncReadyCallback callback,
8287 g_return_if_fail (G_IS_FILE (file));
8289 iface = G_FILE_GET_IFACE (file);
8291 if (iface->poll_mountable == NULL)
8293 g_task_report_new_error (file, callback, user_data,
8294 g_file_poll_mountable,
8295 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8296 _("Operation not supported"));
8300 (* iface->poll_mountable) (file,
8307 * g_file_poll_mountable_finish:
8308 * @file: input #GFile
8309 * @result: a #GAsyncResult
8310 * @error: a #GError, or %NULL
8312 * Finishes a poll operation. See g_file_poll_mountable() for details.
8314 * Finish an asynchronous poll operation that was polled
8315 * with g_file_poll_mountable().
8317 * Returns: %TRUE if the operation finished successfully. %FALSE
8323 g_file_poll_mountable_finish (GFile *file,
8324 GAsyncResult *result,
8329 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8330 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8332 if (g_async_result_legacy_propagate_error (result, error))
8334 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8335 return g_task_propagate_boolean (G_TASK (result), error);
8337 iface = G_FILE_GET_IFACE (file);
8338 return (* iface->poll_mountable_finish) (file, result, error);
8342 * g_file_supports_thread_contexts:
8345 * Checks if @file supports
8346 * [thread-default contexts][g-main-context-push-thread-default-context].
8347 * If this returns %FALSE, you cannot perform asynchronous operations on
8348 * @file in a thread that has a thread-default context.
8350 * Returns: Whether or not @file supports thread-default contexts.
8355 g_file_supports_thread_contexts (GFile *file)
8359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8361 iface = G_FILE_GET_IFACE (file);
8362 return iface->supports_thread_contexts;
8366 * g_file_load_bytes:
8368 * @cancellable: (nullable): a #GCancellable or %NULL
8369 * @etag_out: (out) (nullable) (optional): a location to place the current
8370 * entity tag for the file, or %NULL if the entity tag is not needed
8371 * @error: a location for a #GError or %NULL
8373 * Loads the contents of @file and returns it as #GBytes.
8375 * If @file is a resource:// based URI, the resulting bytes will reference the
8376 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8377 * g_file_load_contents() and g_bytes_new_take().
8379 * For resources, @etag_out will be set to %NULL.
8381 * The data contained in the resulting #GBytes is always zero-terminated, but
8382 * this is not included in the #GBytes length. The resulting #GBytes should be
8383 * freed with g_bytes_unref() when no longer in use.
8385 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8390 g_file_load_bytes (GFile *file,
8391 GCancellable *cancellable,
8398 g_return_val_if_fail (G_IS_FILE (file), NULL);
8399 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8400 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8402 if (etag_out != NULL)
8405 if (g_file_has_uri_scheme (file, "resource"))
8408 gchar *uri, *unescaped;
8410 uri = g_file_get_uri (file);
8411 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8414 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8420 /* contents is guaranteed to be \0 terminated */
8421 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8422 return g_bytes_new_take (g_steal_pointer (&contents), len);
8428 g_file_load_bytes_cb (GObject *object,
8429 GAsyncResult *result,
8432 GFile *file = G_FILE (object);
8433 GTask *task = user_data;
8434 GError *error = NULL;
8436 gchar *contents = NULL;
8439 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8440 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8443 g_task_return_error (task, g_steal_pointer (&error));
8445 g_task_return_pointer (task,
8446 g_bytes_new_take (g_steal_pointer (&contents), len),
8447 (GDestroyNotify)g_bytes_unref);
8449 g_object_unref (task);
8453 * g_file_load_bytes_async:
8455 * @cancellable: (nullable): a #GCancellable or %NULL
8456 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8457 * request is satisfied
8458 * @user_data: (closure): the data to pass to callback function
8460 * Asynchronously loads the contents of @file as #GBytes.
8462 * If @file is a resource:// based URI, the resulting bytes will reference the
8463 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8464 * g_file_load_contents_async() and g_bytes_new_take().
8466 * @callback should call g_file_load_bytes_finish() to get the result of this
8467 * asynchronous operation.
8469 * See g_file_load_bytes() for more information.
8474 g_file_load_bytes_async (GFile *file,
8475 GCancellable *cancellable,
8476 GAsyncReadyCallback callback,
8479 GError *error = NULL;
8483 g_return_if_fail (G_IS_FILE (file));
8484 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8486 task = g_task_new (file, cancellable, callback, user_data);
8487 g_task_set_source_tag (task, g_file_load_bytes_async);
8489 if (!g_file_has_uri_scheme (file, "resource"))
8491 g_file_load_contents_async (file,
8493 g_file_load_bytes_cb,
8494 g_steal_pointer (&task));
8498 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8501 g_task_return_error (task, g_steal_pointer (&error));
8503 g_task_return_pointer (task,
8504 g_steal_pointer (&bytes),
8505 (GDestroyNotify)g_bytes_unref);
8507 g_object_unref (task);
8511 * g_file_load_bytes_finish:
8513 * @result: a #GAsyncResult provided to the callback
8514 * @etag_out: (out) (nullable) (optional): a location to place the current
8515 * entity tag for the file, or %NULL if the entity tag is not needed
8516 * @error: a location for a #GError, or %NULL
8518 * Completes an asynchronous request to g_file_load_bytes_async().
8520 * For resources, @etag_out will be set to %NULL.
8522 * The data contained in the resulting #GBytes is always zero-terminated, but
8523 * this is not included in the #GBytes length. The resulting #GBytes should be
8524 * freed with g_bytes_unref() when no longer in use.
8526 * See g_file_load_bytes() for more information.
8528 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8533 g_file_load_bytes_finish (GFile *file,
8534 GAsyncResult *result,
8540 g_return_val_if_fail (G_IS_FILE (file), NULL);
8541 g_return_val_if_fail (G_IS_TASK (result), NULL);
8542 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8543 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8545 bytes = g_task_propagate_pointer (G_TASK (result), error);
8547 if (etag_out != NULL)
8548 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));