1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
84 * - g_file_new_build_filename() to create a file from path elements.
86 * One way to think of a #GFile is as an abstraction of a pathname. For
87 * normal files the system pathname is what is stored internally, but as
88 * #GFiles are extensible it could also be something else that corresponds
89 * to a pathname in a userspace implementation of a filesystem.
91 * #GFiles make up hierarchies of directories and files that correspond to
92 * the files on a filesystem. You can move through the file system with
93 * #GFile using g_file_get_parent() to get an identifier for the parent
94 * directory, g_file_get_child() to get a child within a directory,
95 * g_file_resolve_relative_path() to resolve a relative path between two
96 * #GFiles. There can be multiple hierarchies, so you may not end up at
97 * the same root if you repeatedly call g_file_get_parent() on two different
100 * All #GFiles have a basename (get with g_file_get_basename()). These names
101 * are byte strings that are used to identify the file on the filesystem
102 * (relative to its parent directory) and there is no guarantees that they
103 * have any particular charset encoding or even make any sense at all. If
104 * you want to use filenames in a user interface you should use the display
105 * name that you can get by requesting the
106 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
107 * This is guaranteed to be in UTF-8 and can be used in a user interface.
108 * But always store the real basename or the #GFile to use to actually
109 * access the file, because there is no way to go from a display name to
112 * Using #GFile as an identifier has the same weaknesses as using a path
113 * in that there may be multiple aliases for the same file. For instance,
114 * hard or soft links may cause two different #GFiles to refer to the same
115 * file. Other possible causes for aliases are: case insensitive filesystems,
116 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
117 * check if two #GFiles point to the same file you can query for the
118 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
119 * canonicalization of pathnames passed in, so that trivial differences in
120 * the path string used at creation (duplicated slashes, slash at end of
121 * path, "." or ".." path segments, etc) does not create different #GFiles.
123 * Many #GFile operations have both synchronous and asynchronous versions
124 * to suit your application. Asynchronous versions of synchronous functions
125 * simply have _async() appended to their function names. The asynchronous
126 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
127 * the operation, producing a GAsyncResult which is then passed to the
128 * function's matching _finish() operation.
130 * It is highly recommended to use asynchronous calls when running within a
131 * shared main loop, such as in the main thread of an application. This avoids
132 * I/O operations blocking other sources on the main loop from being dispatched.
133 * Synchronous I/O operations should be performed from worker threads. See the
134 * [introduction to asynchronous programming section][async-programming] for
137 * Some #GFile operations almost always take a noticeable amount of time, and
138 * so do not have synchronous analogs. Notable cases include:
139 * - g_file_mount_mountable() to mount a mountable file.
140 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
141 * - g_file_eject_mountable_with_operation() to eject a mountable file.
143 * ## Entity Tags # {#gfile-etag}
145 * One notable feature of #GFiles are entity tags, or "etags" for
146 * short. Entity tags are somewhat like a more abstract version of the
147 * traditional mtime, and can be used to quickly determine if the file
148 * has been modified from the version on the file system. See the
150 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
151 * for HTTP Etag headers, which are a very similar concept.
154 static void g_file_real_query_info_async (GFile *file,
155 const char *attributes,
156 GFileQueryInfoFlags flags,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFileInfo * g_file_real_query_info_finish (GFile *file,
164 static void g_file_real_query_filesystem_info_async (GFile *file,
165 const char *attributes,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
170 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
173 static void g_file_real_enumerate_children_async (GFile *file,
174 const char *attributes,
175 GFileQueryInfoFlags flags,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
183 static void g_file_real_read_async (GFile *file,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
188 static GFileInputStream * g_file_real_read_finish (GFile *file,
191 static void g_file_real_append_to_async (GFile *file,
192 GFileCreateFlags flags,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
197 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
200 static void g_file_real_create_async (GFile *file,
201 GFileCreateFlags flags,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFileOutputStream *g_file_real_create_finish (GFile *file,
209 static void g_file_real_replace_async (GFile *file,
211 gboolean make_backup,
212 GFileCreateFlags flags,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
217 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
220 static void g_file_real_delete_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static gboolean g_file_real_delete_finish (GFile *file,
228 static void g_file_real_trash_async (GFile *file,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static gboolean g_file_real_trash_finish (GFile *file,
236 static void g_file_real_make_directory_async (GFile *file,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
241 static gboolean g_file_real_make_directory_finish (GFile *file,
244 static void g_file_real_open_readwrite_async (GFile *file,
246 GCancellable *cancellable,
247 GAsyncReadyCallback callback,
249 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
252 static void g_file_real_create_readwrite_async (GFile *file,
253 GFileCreateFlags flags,
255 GCancellable *cancellable,
256 GAsyncReadyCallback callback,
258 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
261 static void g_file_real_replace_readwrite_async (GFile *file,
263 gboolean make_backup,
264 GFileCreateFlags flags,
266 GCancellable *cancellable,
267 GAsyncReadyCallback callback,
269 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
272 static gboolean g_file_real_set_attributes_from_info (GFile *file,
274 GFileQueryInfoFlags flags,
275 GCancellable *cancellable,
277 static void g_file_real_set_display_name_async (GFile *file,
278 const char *display_name,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
283 static GFile * g_file_real_set_display_name_finish (GFile *file,
286 static void g_file_real_set_attributes_async (GFile *file,
288 GFileQueryInfoFlags flags,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
293 static gboolean g_file_real_set_attributes_finish (GFile *file,
297 static void g_file_real_find_enclosing_mount_async (GFile *file,
299 GCancellable *cancellable,
300 GAsyncReadyCallback callback,
302 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
305 static void g_file_real_copy_async (GFile *source,
307 GFileCopyFlags flags,
309 GCancellable *cancellable,
310 GFileProgressCallback progress_callback,
311 gpointer progress_callback_data,
312 GAsyncReadyCallback callback,
314 static gboolean g_file_real_copy_finish (GFile *file,
318 static gboolean g_file_real_measure_disk_usage (GFile *file,
319 GFileMeasureFlags flags,
320 GCancellable *cancellable,
321 GFileMeasureProgressCallback progress_callback,
322 gpointer progress_data,
327 static void g_file_real_measure_disk_usage_async (GFile *file,
328 GFileMeasureFlags flags,
330 GCancellable *cancellable,
331 GFileMeasureProgressCallback progress_callback,
332 gpointer progress_data,
333 GAsyncReadyCallback callback,
335 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
336 GAsyncResult *result,
342 typedef GFileIface GFileInterface;
343 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
346 g_file_default_init (GFileIface *iface)
348 iface->enumerate_children_async = g_file_real_enumerate_children_async;
349 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
350 iface->set_display_name_async = g_file_real_set_display_name_async;
351 iface->set_display_name_finish = g_file_real_set_display_name_finish;
352 iface->query_info_async = g_file_real_query_info_async;
353 iface->query_info_finish = g_file_real_query_info_finish;
354 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
355 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
356 iface->set_attributes_async = g_file_real_set_attributes_async;
357 iface->set_attributes_finish = g_file_real_set_attributes_finish;
358 iface->read_async = g_file_real_read_async;
359 iface->read_finish = g_file_real_read_finish;
360 iface->append_to_async = g_file_real_append_to_async;
361 iface->append_to_finish = g_file_real_append_to_finish;
362 iface->create_async = g_file_real_create_async;
363 iface->create_finish = g_file_real_create_finish;
364 iface->replace_async = g_file_real_replace_async;
365 iface->replace_finish = g_file_real_replace_finish;
366 iface->delete_file_async = g_file_real_delete_async;
367 iface->delete_file_finish = g_file_real_delete_finish;
368 iface->trash_async = g_file_real_trash_async;
369 iface->trash_finish = g_file_real_trash_finish;
370 iface->make_directory_async = g_file_real_make_directory_async;
371 iface->make_directory_finish = g_file_real_make_directory_finish;
372 iface->open_readwrite_async = g_file_real_open_readwrite_async;
373 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
374 iface->create_readwrite_async = g_file_real_create_readwrite_async;
375 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
376 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
377 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
378 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
379 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
380 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
381 iface->copy_async = g_file_real_copy_async;
382 iface->copy_finish = g_file_real_copy_finish;
383 iface->measure_disk_usage = g_file_real_measure_disk_usage;
384 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
385 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
391 * @file: input #GFile
393 * Checks to see if a file is native to the platform.
395 * A native file is one expressed in the platform-native filename format,
396 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397 * as it might be on a locally mounted remote filesystem.
399 * On some systems non-native files may be available using the native
400 * filesystem via a userspace filesystem (FUSE), in these cases this call
401 * will return %FALSE, but g_file_get_path() will still return a native path.
403 * This call does no blocking I/O.
405 * Returns: %TRUE if @file is native
408 g_file_is_native (GFile *file)
412 g_return_val_if_fail (G_IS_FILE (file), FALSE);
414 iface = G_FILE_GET_IFACE (file);
416 return (* iface->is_native) (file);
421 * g_file_has_uri_scheme:
422 * @file: input #GFile
423 * @uri_scheme: a string containing a URI scheme
425 * Checks to see if a #GFile has a given URI scheme.
427 * This call does no blocking I/O.
429 * Returns: %TRUE if #GFile's backend supports the
430 * given URI scheme, %FALSE if URI scheme is %NULL,
431 * not supported, or #GFile is invalid.
434 g_file_has_uri_scheme (GFile *file,
435 const char *uri_scheme)
439 g_return_val_if_fail (G_IS_FILE (file), FALSE);
440 g_return_val_if_fail (uri_scheme != NULL, FALSE);
442 iface = G_FILE_GET_IFACE (file);
444 return (* iface->has_uri_scheme) (file, uri_scheme);
449 * g_file_get_uri_scheme:
450 * @file: input #GFile
452 * Gets the URI scheme for a #GFile.
453 * RFC 3986 decodes the scheme as:
455 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
457 * Common schemes include "file", "http", "ftp", etc.
459 * This call does no blocking I/O.
461 * Returns: a string containing the URI scheme for the given
462 * #GFile. The returned string should be freed with g_free()
463 * when no longer needed.
466 g_file_get_uri_scheme (GFile *file)
470 g_return_val_if_fail (G_IS_FILE (file), NULL);
472 iface = G_FILE_GET_IFACE (file);
474 return (* iface->get_uri_scheme) (file);
479 * g_file_get_basename:
480 * @file: input #GFile
482 * Gets the base name (the last component of the path) for a given #GFile.
484 * If called for the top level of a system (such as the filesystem root
485 * or a uri like sftp://host/) it will return a single directory separator
486 * (and on Windows, possibly a drive letter).
488 * The base name is a byte string (not UTF-8). It has no defined encoding
489 * or rules other than it may not contain zero bytes. If you want to use
490 * filenames in a user interface you should use the display name that you
491 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
492 * attribute with g_file_query_info().
494 * This call does no blocking I/O.
496 * Returns: (type filename) (nullable): string containing the #GFile's
497 * base name, or %NULL if given #GFile is invalid. The returned string
498 * should be freed with g_free() when no longer needed.
501 g_file_get_basename (GFile *file)
505 g_return_val_if_fail (G_IS_FILE (file), NULL);
507 iface = G_FILE_GET_IFACE (file);
509 return (* iface->get_basename) (file);
514 * @file: input #GFile
516 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
517 * guaranteed to be an absolute, canonical path. It might contain symlinks.
519 * This call does no blocking I/O.
521 * Returns: (type filename) (nullable): string containing the #GFile's path,
522 * or %NULL if no such path exists. The returned string should be freed
523 * with g_free() when no longer needed.
526 g_file_get_path (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
538 file_peek_path_generic (GFile *file)
541 static GQuark _file_path_quark = 0;
543 if (G_UNLIKELY (_file_path_quark) == 0)
544 _file_path_quark = g_quark_from_static_string ("gio-file-path");
546 /* We need to be careful about threading, as two threads calling
547 * g_file_peek_path() on the same file could race: both would see
548 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
549 * the path, but the second thread to add it would end up freeing the path
550 * set by the first thread. The first thread would still return the pointer
551 * to that freed path, though, resulting an a read-after-free. Handle that
552 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
556 gchar *new_path = NULL;
558 path = g_object_get_qdata ((GObject*)file, _file_path_quark);
563 new_path = g_file_get_path (file);
564 if (new_path == NULL)
567 /* By passing NULL here, we ensure we never replace existing data: */
568 if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
569 NULL, (gpointer) new_path,
570 (GDestroyNotify) g_free, NULL))
584 * @file: input #GFile
586 * Exactly like g_file_get_path(), but caches the result via
587 * g_object_set_qdata_full(). This is useful for example in C
588 * applications which mix `g_file_*` APIs with native ones. It
589 * also avoids an extra duplicated string when possible, so will be
590 * generally more efficient.
592 * This call does no blocking I/O.
594 * Returns: (type filename) (nullable): string containing the #GFile's path,
595 * or %NULL if no such path exists. The returned string is owned by @file.
599 g_file_peek_path (GFile *file)
601 if (G_IS_LOCAL_FILE (file))
602 return _g_local_file_get_filename ((GLocalFile *) file);
603 return file_peek_path_generic (file);
608 * @file: input #GFile
610 * Gets the URI for the @file.
612 * This call does no blocking I/O.
614 * Returns: a string containing the #GFile's URI.
615 * The returned string should be freed with g_free()
616 * when no longer needed.
619 g_file_get_uri (GFile *file)
623 g_return_val_if_fail (G_IS_FILE (file), NULL);
625 iface = G_FILE_GET_IFACE (file);
627 return (* iface->get_uri) (file);
631 * g_file_get_parse_name:
632 * @file: input #GFile
634 * Gets the parse name of the @file.
635 * A parse name is a UTF-8 string that describes the
636 * file such that one can get the #GFile back using
637 * g_file_parse_name().
639 * This is generally used to show the #GFile as a nice
640 * full-pathname kind of string in a user interface,
641 * like in a location entry.
643 * For local files with names that can safely be converted
644 * to UTF-8 the pathname is used, otherwise the IRI is used
645 * (a form of URI that allows UTF-8 characters unescaped).
647 * This call does no blocking I/O.
649 * Returns: a string containing the #GFile's parse name.
650 * The returned string should be freed with g_free()
651 * when no longer needed.
654 g_file_get_parse_name (GFile *file)
658 g_return_val_if_fail (G_IS_FILE (file), NULL);
660 iface = G_FILE_GET_IFACE (file);
662 return (* iface->get_parse_name) (file);
667 * @file: input #GFile
669 * Duplicates a #GFile handle. This operation does not duplicate
670 * the actual file or directory represented by the #GFile; see
671 * g_file_copy() if attempting to copy a file.
673 * g_file_dup() is useful when a second handle is needed to the same underlying
674 * file, for use in a separate thread (#GFile is not thread-safe). For use
675 * within the same thread, use g_object_ref() to increment the existing object’s
678 * This call does no blocking I/O.
680 * Returns: (transfer full): a new #GFile that is a duplicate
681 * of the given #GFile.
684 g_file_dup (GFile *file)
688 g_return_val_if_fail (G_IS_FILE (file), NULL);
690 iface = G_FILE_GET_IFACE (file);
692 return (* iface->dup) (file);
697 * @file: (type GFile): #gconstpointer to a #GFile
699 * Creates a hash value for a #GFile.
701 * This call does no blocking I/O.
704 * Returns: 0 if @file is not a valid #GFile, otherwise an
705 * integer that can be used as hash value for the #GFile.
706 * This function is intended for easily hashing a #GFile to
707 * add to a #GHashTable or similar data structure.
710 g_file_hash (gconstpointer file)
714 g_return_val_if_fail (G_IS_FILE (file), 0);
716 iface = G_FILE_GET_IFACE (file);
718 return (* iface->hash) ((GFile *)file);
723 * @file1: the first #GFile
724 * @file2: the second #GFile
726 * Checks if the two given #GFiles refer to the same file.
728 * Note that two #GFiles that differ can still refer to the same
729 * file on the filesystem due to various forms of filename
732 * This call does no blocking I/O.
734 * Returns: %TRUE if @file1 and @file2 are equal.
737 g_file_equal (GFile *file1,
742 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
743 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
748 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
751 iface = G_FILE_GET_IFACE (file1);
753 return (* iface->equal) (file1, file2);
759 * @file: input #GFile
761 * Gets the parent directory for the @file.
762 * If the @file represents the root directory of the
763 * file system, then %NULL will be returned.
765 * This call does no blocking I/O.
767 * Returns: (nullable) (transfer full): a #GFile structure to the
768 * parent of the given #GFile or %NULL if there is no parent. Free
769 * the returned object with g_object_unref().
772 g_file_get_parent (GFile *file)
776 g_return_val_if_fail (G_IS_FILE (file), NULL);
778 iface = G_FILE_GET_IFACE (file);
780 return (* iface->get_parent) (file);
785 * @file: input #GFile
786 * @parent: (nullable): the parent to check for, or %NULL
788 * Checks if @file has a parent, and optionally, if it is @parent.
790 * If @parent is %NULL then this function returns %TRUE if @file has any
791 * parent at all. If @parent is non-%NULL then %TRUE is only returned
792 * if @file is an immediate child of @parent.
794 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
795 * the case that @parent is %NULL).
800 g_file_has_parent (GFile *file,
803 GFile *actual_parent;
806 g_return_val_if_fail (G_IS_FILE (file), FALSE);
807 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
809 actual_parent = g_file_get_parent (file);
811 if (actual_parent != NULL)
814 result = g_file_equal (parent, actual_parent);
818 g_object_unref (actual_parent);
828 * @file: input #GFile
829 * @name: (type filename): string containing the child's basename
831 * Gets a child of @file with basename equal to @name.
833 * Note that the file with that specific name might not exist, but
834 * you can still have a #GFile that points to it. You can use this
835 * for instance to create that file.
837 * This call does no blocking I/O.
839 * Returns: (transfer full): a #GFile to a child specified by @name.
840 * Free the returned object with g_object_unref().
843 g_file_get_child (GFile *file,
846 g_return_val_if_fail (G_IS_FILE (file), NULL);
847 g_return_val_if_fail (name != NULL, NULL);
849 return g_file_resolve_relative_path (file, name);
853 * g_file_get_child_for_display_name:
854 * @file: input #GFile
855 * @display_name: string to a possible child
856 * @error: return location for an error
858 * Gets the child of @file for a given @display_name (i.e. a UTF-8
859 * version of the name). If this function fails, it returns %NULL
860 * and @error will be set. This is very useful when constructing a
861 * #GFile for a new file and the user entered the filename in the
862 * user interface, for instance when you select a directory and
863 * type a filename in the file selector.
865 * This call does no blocking I/O.
867 * Returns: (transfer full): a #GFile to the specified child, or
868 * %NULL if the display name couldn't be converted.
869 * Free the returned object with g_object_unref().
872 g_file_get_child_for_display_name (GFile *file,
873 const char *display_name,
878 g_return_val_if_fail (G_IS_FILE (file), NULL);
879 g_return_val_if_fail (display_name != NULL, NULL);
881 iface = G_FILE_GET_IFACE (file);
883 return (* iface->get_child_for_display_name) (file, display_name, error);
888 * @file: input #GFile
889 * @prefix: input #GFile
891 * Checks whether @file has the prefix specified by @prefix.
893 * In other words, if the names of initial elements of @file's
894 * pathname match @prefix. Only full pathname elements are matched,
895 * so a path like /foo is not considered a prefix of /foobar, only
898 * A #GFile is not a prefix of itself. If you want to check for
899 * equality, use g_file_equal().
901 * This call does no I/O, as it works purely on names. As such it can
902 * sometimes return %FALSE even if @file is inside a @prefix (from a
903 * filesystem point of view), because the prefix of @file is an alias
906 * Virtual: prefix_matches
907 * Returns: %TRUE if the @file's parent, grandparent, etc is @prefix,
911 g_file_has_prefix (GFile *file,
916 g_return_val_if_fail (G_IS_FILE (file), FALSE);
917 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
919 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
922 iface = G_FILE_GET_IFACE (file);
924 /* The vtable function differs in arg order since
925 * we're using the old contains_file call
927 return (* iface->prefix_matches) (prefix, file);
931 * g_file_get_relative_path:
932 * @parent: input #GFile
933 * @descendant: input #GFile
935 * Gets the path for @descendant relative to @parent.
937 * This call does no blocking I/O.
939 * Returns: (type filename) (nullable): string with the relative path from
940 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
941 * prefix. The returned string should be freed with g_free() when
945 g_file_get_relative_path (GFile *parent,
950 g_return_val_if_fail (G_IS_FILE (parent), NULL);
951 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
953 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
956 iface = G_FILE_GET_IFACE (parent);
958 return (* iface->get_relative_path) (parent, descendant);
962 * g_file_resolve_relative_path:
963 * @file: input #GFile
964 * @relative_path: (type filename): a given relative path string
966 * Resolves a relative path for @file to an absolute path.
968 * This call does no blocking I/O.
970 * Returns: (transfer full): #GFile to the resolved path.
971 * %NULL if @relative_path is %NULL or if @file is invalid.
972 * Free the returned object with g_object_unref().
975 g_file_resolve_relative_path (GFile *file,
976 const char *relative_path)
980 g_return_val_if_fail (G_IS_FILE (file), NULL);
981 g_return_val_if_fail (relative_path != NULL, NULL);
983 iface = G_FILE_GET_IFACE (file);
985 return (* iface->resolve_relative_path) (file, relative_path);
989 * g_file_enumerate_children:
990 * @file: input #GFile
991 * @attributes: an attribute query string
992 * @flags: a set of #GFileQueryInfoFlags
993 * @cancellable: (nullable): optional #GCancellable object,
995 * @error: #GError for error reporting
997 * Gets the requested information about the files in a directory.
998 * The result is a #GFileEnumerator object that will give out
999 * #GFileInfo objects for all the files in the directory.
1001 * The @attributes value is a string that specifies the file
1002 * attributes that should be gathered. It is not an error if
1003 * it's not possible to read a particular requested attribute
1004 * from a file - it just won't be set. @attributes should
1005 * be a comma-separated list of attributes or attribute wildcards.
1006 * The wildcard "*" means all attributes, and a wildcard like
1007 * "standard::*" means all attributes in the standard namespace.
1008 * An example attribute query be "standard::*,owner::user".
1009 * The standard attributes are available as defines, like
1010 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1012 * If @cancellable is not %NULL, then the operation can be cancelled
1013 * by triggering the cancellable object from another thread. If the
1014 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1017 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1018 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1019 * error will be returned. Other errors are possible too.
1021 * Returns: (transfer full): A #GFileEnumerator if successful,
1022 * %NULL on error. Free the returned object with g_object_unref().
1025 g_file_enumerate_children (GFile *file,
1026 const char *attributes,
1027 GFileQueryInfoFlags flags,
1028 GCancellable *cancellable,
1033 g_return_val_if_fail (G_IS_FILE (file), NULL);
1035 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1038 iface = G_FILE_GET_IFACE (file);
1040 if (iface->enumerate_children == NULL)
1042 g_set_error_literal (error, G_IO_ERROR,
1043 G_IO_ERROR_NOT_SUPPORTED,
1044 _("Operation not supported"));
1048 return (* iface->enumerate_children) (file, attributes, flags,
1049 cancellable, error);
1053 * g_file_enumerate_children_async:
1054 * @file: input #GFile
1055 * @attributes: an attribute query string
1056 * @flags: a set of #GFileQueryInfoFlags
1057 * @io_priority: the [I/O priority][io-priority] of the request
1058 * @cancellable: (nullable): optional #GCancellable object,
1060 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1061 * request is satisfied
1062 * @user_data: (closure): the data to pass to callback function
1064 * Asynchronously gets the requested information about the files
1065 * in a directory. The result is a #GFileEnumerator object that will
1066 * give out #GFileInfo objects for all the files in the directory.
1068 * For more details, see g_file_enumerate_children() which is
1069 * the synchronous version of this call.
1071 * When the operation is finished, @callback will be called. You can
1072 * then call g_file_enumerate_children_finish() to get the result of
1076 g_file_enumerate_children_async (GFile *file,
1077 const char *attributes,
1078 GFileQueryInfoFlags flags,
1080 GCancellable *cancellable,
1081 GAsyncReadyCallback callback,
1086 g_return_if_fail (G_IS_FILE (file));
1088 iface = G_FILE_GET_IFACE (file);
1089 (* iface->enumerate_children_async) (file,
1099 * g_file_enumerate_children_finish:
1100 * @file: input #GFile
1101 * @res: a #GAsyncResult
1104 * Finishes an async enumerate children operation.
1105 * See g_file_enumerate_children_async().
1107 * Returns: (transfer full): a #GFileEnumerator or %NULL
1108 * if an error occurred.
1109 * Free the returned object with g_object_unref().
1112 g_file_enumerate_children_finish (GFile *file,
1118 g_return_val_if_fail (G_IS_FILE (file), NULL);
1119 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1121 if (g_async_result_legacy_propagate_error (res, error))
1124 iface = G_FILE_GET_IFACE (file);
1125 return (* iface->enumerate_children_finish) (file, res, error);
1129 * g_file_query_exists:
1130 * @file: input #GFile
1131 * @cancellable: (nullable): optional #GCancellable object,
1134 * Utility function to check if a particular file exists. This is
1135 * implemented using g_file_query_info() and as such does blocking I/O.
1137 * 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)
1138 * and then execute something based on the outcome of that, because the
1139 * file might have been created or removed in between the operations. The
1140 * general approach to handling that is to not check, but just do the
1141 * operation and handle the errors as they come.
1143 * As an example of race-free checking, take the case of reading a file,
1144 * and if it doesn't exist, creating it. There are two racy versions: read
1145 * it, and on error create it; and: check if it exists, if not create it.
1146 * These can both result in two processes creating the file (with perhaps
1147 * a partially written file as the result). The correct approach is to
1148 * always try to create the file with g_file_create() which will either
1149 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1151 * However, in many cases an existence check is useful in a user interface,
1152 * for instance to make a menu item sensitive/insensitive, so that you don't
1153 * have to fool users that something is possible and then just show an error
1154 * dialog. If you do this, you should make sure to also handle the errors
1155 * that can happen due to races when you execute the operation.
1157 * Returns: %TRUE if the file exists (and can be detected without error),
1158 * %FALSE otherwise (or if cancelled).
1161 g_file_query_exists (GFile *file,
1162 GCancellable *cancellable)
1166 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1168 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1169 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1172 g_object_unref (info);
1180 * g_file_query_file_type:
1181 * @file: input #GFile
1182 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1183 * @cancellable: (nullable): optional #GCancellable object,
1186 * Utility function to inspect the #GFileType of a file. This is
1187 * implemented using g_file_query_info() and as such does blocking I/O.
1189 * The primary use case of this method is to check if a file is
1190 * a regular file, directory, or symlink.
1192 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1193 * if the file does not exist
1198 g_file_query_file_type (GFile *file,
1199 GFileQueryInfoFlags flags,
1200 GCancellable *cancellable)
1203 GFileType file_type;
1205 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1206 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1210 file_type = g_file_info_get_file_type (info);
1211 g_object_unref (info);
1214 file_type = G_FILE_TYPE_UNKNOWN;
1220 * g_file_query_info:
1221 * @file: input #GFile
1222 * @attributes: an attribute query string
1223 * @flags: a set of #GFileQueryInfoFlags
1224 * @cancellable: (nullable): optional #GCancellable object,
1228 * Gets the requested information about specified @file.
1229 * The result is a #GFileInfo object that contains key-value
1230 * attributes (such as the type or size of the file).
1232 * The @attributes value is a string that specifies the file
1233 * attributes that should be gathered. It is not an error if
1234 * it's not possible to read a particular requested attribute
1235 * from a file - it just won't be set. @attributes should be a
1236 * comma-separated list of attributes or attribute wildcards.
1237 * The wildcard "*" means all attributes, and a wildcard like
1238 * "standard::*" means all attributes in the standard namespace.
1239 * An example attribute query be "standard::*,owner::user".
1240 * The standard attributes are available as defines, like
1241 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1243 * If @cancellable is not %NULL, then the operation can be cancelled
1244 * by triggering the cancellable object from another thread. If the
1245 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1248 * For symlinks, normally the information about the target of the
1249 * symlink is returned, rather than information about the symlink
1250 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1251 * in @flags the information about the symlink itself will be returned.
1252 * Also, for symlinks that point to non-existing files the information
1253 * about the symlink itself will be returned.
1255 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1256 * returned. Other errors are possible too, and depend on what kind of
1257 * filesystem the file is on.
1259 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1260 * on error. Free the returned object with g_object_unref().
1263 g_file_query_info (GFile *file,
1264 const char *attributes,
1265 GFileQueryInfoFlags flags,
1266 GCancellable *cancellable,
1271 g_return_val_if_fail (G_IS_FILE (file), NULL);
1273 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1276 iface = G_FILE_GET_IFACE (file);
1278 if (iface->query_info == NULL)
1280 g_set_error_literal (error, G_IO_ERROR,
1281 G_IO_ERROR_NOT_SUPPORTED,
1282 _("Operation not supported"));
1286 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1290 * g_file_query_info_async:
1291 * @file: input #GFile
1292 * @attributes: an attribute query string
1293 * @flags: a set of #GFileQueryInfoFlags
1294 * @io_priority: the [I/O priority][io-priority] of the request
1295 * @cancellable: (nullable): optional #GCancellable object,
1297 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1298 * request is satisfied
1299 * @user_data: (closure): the data to pass to callback function
1301 * Asynchronously gets the requested information about specified @file.
1302 * The result is a #GFileInfo object that contains key-value attributes
1303 * (such as type or size for the file).
1305 * For more details, see g_file_query_info() which is the synchronous
1306 * version of this call.
1308 * When the operation is finished, @callback will be called. You can
1309 * then call g_file_query_info_finish() to get the result of the operation.
1312 g_file_query_info_async (GFile *file,
1313 const char *attributes,
1314 GFileQueryInfoFlags flags,
1316 GCancellable *cancellable,
1317 GAsyncReadyCallback callback,
1322 g_return_if_fail (G_IS_FILE (file));
1324 iface = G_FILE_GET_IFACE (file);
1325 (* iface->query_info_async) (file,
1335 * g_file_query_info_finish:
1336 * @file: input #GFile
1337 * @res: a #GAsyncResult
1340 * Finishes an asynchronous file info query.
1341 * See g_file_query_info_async().
1343 * Returns: (transfer full): #GFileInfo for given @file
1344 * or %NULL on error. Free the returned object with
1348 g_file_query_info_finish (GFile *file,
1354 g_return_val_if_fail (G_IS_FILE (file), NULL);
1355 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1357 if (g_async_result_legacy_propagate_error (res, error))
1360 iface = G_FILE_GET_IFACE (file);
1361 return (* iface->query_info_finish) (file, res, error);
1365 * g_file_query_filesystem_info:
1366 * @file: input #GFile
1367 * @attributes: an attribute query string
1368 * @cancellable: (nullable): optional #GCancellable object,
1372 * Similar to g_file_query_info(), but obtains information
1373 * about the filesystem the @file is on, rather than the file itself.
1374 * For instance the amount of space available and the type of
1377 * The @attributes value is a string that specifies the attributes
1378 * that should be gathered. It is not an error if it's not possible
1379 * to read a particular requested attribute from a file - it just
1380 * won't be set. @attributes should be a comma-separated list of
1381 * attributes or attribute wildcards. The wildcard "*" means all
1382 * attributes, and a wildcard like "filesystem::*" means all attributes
1383 * in the filesystem namespace. The standard namespace for filesystem
1384 * attributes is "filesystem". Common attributes of interest are
1385 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1386 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1387 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1389 * If @cancellable is not %NULL, then the operation can be cancelled
1390 * by triggering the cancellable object from another thread. If the
1391 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1394 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1395 * be returned. Other errors are possible too, and depend on what
1396 * kind of filesystem the file is on.
1398 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1399 * Free the returned object with g_object_unref().
1402 g_file_query_filesystem_info (GFile *file,
1403 const char *attributes,
1404 GCancellable *cancellable,
1409 g_return_val_if_fail (G_IS_FILE (file), NULL);
1411 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1414 iface = G_FILE_GET_IFACE (file);
1416 if (iface->query_filesystem_info == NULL)
1418 g_set_error_literal (error, G_IO_ERROR,
1419 G_IO_ERROR_NOT_SUPPORTED,
1420 _("Operation not supported"));
1424 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1428 * g_file_query_filesystem_info_async:
1429 * @file: input #GFile
1430 * @attributes: an attribute query string
1431 * @io_priority: the [I/O priority][io-priority] of the request
1432 * @cancellable: (nullable): optional #GCancellable object,
1434 * @callback: (scope async): a #GAsyncReadyCallback to call
1435 * when the request is satisfied
1436 * @user_data: (closure): the data to pass to callback function
1438 * Asynchronously gets the requested information about the filesystem
1439 * that the specified @file is on. The result is a #GFileInfo object
1440 * that contains key-value attributes (such as type or size for the
1443 * For more details, see g_file_query_filesystem_info() which is the
1444 * synchronous version of this call.
1446 * When the operation is finished, @callback will be called. You can
1447 * then call g_file_query_info_finish() to get the result of the
1451 g_file_query_filesystem_info_async (GFile *file,
1452 const char *attributes,
1454 GCancellable *cancellable,
1455 GAsyncReadyCallback callback,
1460 g_return_if_fail (G_IS_FILE (file));
1462 iface = G_FILE_GET_IFACE (file);
1463 (* iface->query_filesystem_info_async) (file,
1472 * g_file_query_filesystem_info_finish:
1473 * @file: input #GFile
1474 * @res: a #GAsyncResult
1477 * Finishes an asynchronous filesystem info query.
1478 * See g_file_query_filesystem_info_async().
1480 * Returns: (transfer full): #GFileInfo for given @file
1481 * or %NULL on error.
1482 * Free the returned object with g_object_unref().
1485 g_file_query_filesystem_info_finish (GFile *file,
1491 g_return_val_if_fail (G_IS_FILE (file), NULL);
1492 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1494 if (g_async_result_legacy_propagate_error (res, error))
1497 iface = G_FILE_GET_IFACE (file);
1498 return (* iface->query_filesystem_info_finish) (file, res, error);
1502 * g_file_find_enclosing_mount:
1503 * @file: input #GFile
1504 * @cancellable: (nullable): optional #GCancellable object,
1508 * Gets a #GMount for the #GFile.
1510 * #GMount is returned only for user interesting locations, see
1511 * #GVolumeMonitor. If the #GFileIface for @file does not have a #mount,
1512 * @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL #will be returned.
1514 * If @cancellable is not %NULL, then the operation can be cancelled by
1515 * triggering the cancellable object from another thread. If the operation
1516 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1518 * Returns: (transfer full): a #GMount where the @file is located
1519 * or %NULL on error.
1520 * Free the returned object with g_object_unref().
1523 g_file_find_enclosing_mount (GFile *file,
1524 GCancellable *cancellable,
1529 g_return_val_if_fail (G_IS_FILE (file), NULL);
1531 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1534 iface = G_FILE_GET_IFACE (file);
1535 if (iface->find_enclosing_mount == NULL)
1538 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1539 /* Translators: This is an error message when
1540 * trying to find the enclosing (user visible)
1541 * mount of a file, but none exists.
1543 _("Containing mount does not exist"));
1547 return (* iface->find_enclosing_mount) (file, cancellable, error);
1551 * g_file_find_enclosing_mount_async:
1553 * @io_priority: the [I/O priority][io-priority] of the request
1554 * @cancellable: (nullable): optional #GCancellable object,
1556 * @callback: (scope async): a #GAsyncReadyCallback to call
1557 * when the request is satisfied
1558 * @user_data: (closure): the data to pass to callback function
1560 * Asynchronously gets the mount for the file.
1562 * For more details, see g_file_find_enclosing_mount() which is
1563 * the synchronous version of this call.
1565 * When the operation is finished, @callback will be called.
1566 * You can then call g_file_find_enclosing_mount_finish() to
1567 * get the result of the operation.
1570 g_file_find_enclosing_mount_async (GFile *file,
1572 GCancellable *cancellable,
1573 GAsyncReadyCallback callback,
1578 g_return_if_fail (G_IS_FILE (file));
1580 iface = G_FILE_GET_IFACE (file);
1581 (* iface->find_enclosing_mount_async) (file,
1589 * g_file_find_enclosing_mount_finish:
1591 * @res: a #GAsyncResult
1594 * Finishes an asynchronous find mount request.
1595 * See g_file_find_enclosing_mount_async().
1597 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1598 * Free the returned object with g_object_unref().
1601 g_file_find_enclosing_mount_finish (GFile *file,
1607 g_return_val_if_fail (G_IS_FILE (file), NULL);
1608 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1610 if (g_async_result_legacy_propagate_error (res, error))
1613 iface = G_FILE_GET_IFACE (file);
1614 return (* iface->find_enclosing_mount_finish) (file, res, error);
1620 * @file: #GFile to read
1621 * @cancellable: (nullable): a #GCancellable
1622 * @error: a #GError, or %NULL
1624 * Opens a file for reading. The result is a #GFileInputStream that
1625 * can be used to read the contents of the file.
1627 * If @cancellable is not %NULL, then the operation can be cancelled by
1628 * triggering the cancellable object from another thread. If the operation
1629 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1631 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1632 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1633 * error will be returned. Other errors are possible too, and depend
1634 * on what kind of filesystem the file is on.
1637 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1638 * Free the returned object with g_object_unref().
1641 g_file_read (GFile *file,
1642 GCancellable *cancellable,
1647 g_return_val_if_fail (G_IS_FILE (file), NULL);
1649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1652 iface = G_FILE_GET_IFACE (file);
1654 if (iface->read_fn == NULL)
1656 g_set_error_literal (error, G_IO_ERROR,
1657 G_IO_ERROR_NOT_SUPPORTED,
1658 _("Operation not supported"));
1662 return (* iface->read_fn) (file, cancellable, error);
1667 * @file: input #GFile
1668 * @flags: a set of #GFileCreateFlags
1669 * @cancellable: (nullable): optional #GCancellable object,
1671 * @error: a #GError, or %NULL
1673 * Gets an output stream for appending data to the file.
1674 * If the file doesn't already exist it is created.
1676 * By default files created are generally readable by everyone,
1677 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1678 * will be made readable only to the current user, to the level that
1679 * is supported on the target filesystem.
1681 * If @cancellable is not %NULL, then the operation can be cancelled
1682 * by triggering the cancellable object from another thread. If the
1683 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1686 * Some file systems don't allow all file names, and may return an
1687 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1688 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1689 * possible too, and depend on what kind of filesystem the file is on.
1691 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1692 * Free the returned object with g_object_unref().
1695 g_file_append_to (GFile *file,
1696 GFileCreateFlags flags,
1697 GCancellable *cancellable,
1702 g_return_val_if_fail (G_IS_FILE (file), NULL);
1704 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1707 iface = G_FILE_GET_IFACE (file);
1709 if (iface->append_to == NULL)
1711 g_set_error_literal (error, G_IO_ERROR,
1712 G_IO_ERROR_NOT_SUPPORTED,
1713 _("Operation not supported"));
1717 return (* iface->append_to) (file, flags, cancellable, error);
1722 * @file: input #GFile
1723 * @flags: a set of #GFileCreateFlags
1724 * @cancellable: (nullable): optional #GCancellable object,
1726 * @error: a #GError, or %NULL
1728 * Creates a new file and returns an output stream for writing to it.
1729 * The file must not already exist.
1731 * By default files created are generally readable by everyone,
1732 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1733 * will be made readable only to the current user, to the level
1734 * that is supported on the target filesystem.
1736 * If @cancellable is not %NULL, then the operation can be cancelled
1737 * by triggering the cancellable object from another thread. If the
1738 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1741 * If a file or directory with this name already exists the
1742 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1743 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1744 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1745 * be returned. Other errors are possible too, and depend on what kind
1746 * of filesystem the file is on.
1748 * Returns: (transfer full): a #GFileOutputStream for the newly created
1749 * file, or %NULL on error.
1750 * Free the returned object with g_object_unref().
1753 g_file_create (GFile *file,
1754 GFileCreateFlags flags,
1755 GCancellable *cancellable,
1760 g_return_val_if_fail (G_IS_FILE (file), NULL);
1762 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1765 iface = G_FILE_GET_IFACE (file);
1767 if (iface->create == NULL)
1769 g_set_error_literal (error, G_IO_ERROR,
1770 G_IO_ERROR_NOT_SUPPORTED,
1771 _("Operation not supported"));
1775 return (* iface->create) (file, flags, cancellable, error);
1780 * @file: input #GFile
1781 * @etag: (nullable): an optional [entity tag][gfile-etag]
1782 * for the current #GFile, or #NULL to ignore
1783 * @make_backup: %TRUE if a backup should be created
1784 * @flags: a set of #GFileCreateFlags
1785 * @cancellable: (nullable): optional #GCancellable object,
1787 * @error: a #GError, or %NULL
1789 * Returns an output stream for overwriting the file, possibly
1790 * creating a backup copy of the file first. If the file doesn't exist,
1791 * it will be created.
1793 * This will try to replace the file in the safest way possible so
1794 * that any errors during the writing will not affect an already
1795 * existing copy of the file. For instance, for local files it
1796 * may write to a temporary file and then atomically rename over
1797 * the destination when the stream is closed.
1799 * By default files created are generally readable by everyone,
1800 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1801 * will be made readable only to the current user, to the level that
1802 * is supported on the target filesystem.
1804 * If @cancellable is not %NULL, then the operation can be cancelled
1805 * by triggering the cancellable object from another thread. If the
1806 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1809 * If you pass in a non-%NULL @etag value and @file already exists, then
1810 * this value is compared to the current entity tag of the file, and if
1811 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1812 * generally means that the file has been changed since you last read
1813 * it. You can get the new etag from g_file_output_stream_get_etag()
1814 * after you've finished writing and closed the #GFileOutputStream. When
1815 * you load a new file you can use g_file_input_stream_query_info() to
1816 * get the etag of the file.
1818 * If @make_backup is %TRUE, this function will attempt to make a
1819 * backup of the current file before overwriting it. If this fails
1820 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1821 * want to replace anyway, try again with @make_backup set to %FALSE.
1823 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1824 * be returned, and if the file is some other form of non-regular file
1825 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1826 * file systems don't allow all file names, and may return an
1827 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1828 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1829 * possible too, and depend on what kind of filesystem the file is on.
1831 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1832 * Free the returned object with g_object_unref().
1835 g_file_replace (GFile *file,
1837 gboolean make_backup,
1838 GFileCreateFlags flags,
1839 GCancellable *cancellable,
1844 g_return_val_if_fail (G_IS_FILE (file), NULL);
1846 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1849 iface = G_FILE_GET_IFACE (file);
1851 if (iface->replace == NULL)
1853 g_set_error_literal (error, G_IO_ERROR,
1854 G_IO_ERROR_NOT_SUPPORTED,
1855 _("Operation not supported"));
1859 /* Handle empty tag string as NULL in consistent way. */
1860 if (etag && *etag == 0)
1863 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1867 * g_file_open_readwrite:
1868 * @file: #GFile to open
1869 * @cancellable: (nullable): a #GCancellable
1870 * @error: a #GError, or %NULL
1872 * Opens an existing file for reading and writing. The result is
1873 * a #GFileIOStream that can be used to read and write the contents
1876 * If @cancellable is not %NULL, then the operation can be cancelled
1877 * by triggering the cancellable object from another thread. If the
1878 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1881 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1882 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1883 * error will be returned. Other errors are possible too, and depend on
1884 * what kind of filesystem the file is on. Note that in many non-local
1885 * file cases read and write streams are not supported, so make sure you
1886 * really need to do read and write streaming, rather than just opening
1887 * for reading or writing.
1889 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1890 * Free the returned object with g_object_unref().
1895 g_file_open_readwrite (GFile *file,
1896 GCancellable *cancellable,
1901 g_return_val_if_fail (G_IS_FILE (file), NULL);
1903 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1906 iface = G_FILE_GET_IFACE (file);
1908 if (iface->open_readwrite == NULL)
1910 g_set_error_literal (error, G_IO_ERROR,
1911 G_IO_ERROR_NOT_SUPPORTED,
1912 _("Operation not supported"));
1916 return (* iface->open_readwrite) (file, cancellable, error);
1920 * g_file_create_readwrite:
1922 * @flags: a set of #GFileCreateFlags
1923 * @cancellable: (nullable): optional #GCancellable object,
1925 * @error: return location for a #GError, or %NULL
1927 * Creates a new file and returns a stream for reading and
1928 * writing to it. The file must not already exist.
1930 * By default files created are generally readable by everyone,
1931 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1932 * will be made readable only to the current user, to the level
1933 * that is supported on the target filesystem.
1935 * If @cancellable is not %NULL, then the operation can be cancelled
1936 * by triggering the cancellable object from another thread. If the
1937 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1940 * If a file or directory with this name already exists, the
1941 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1942 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1943 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1944 * will be returned. Other errors are possible too, and depend on what
1945 * kind of filesystem the file is on.
1947 * Note that in many non-local file cases read and write streams are
1948 * not supported, so make sure you really need to do read and write
1949 * streaming, rather than just opening for reading or writing.
1951 * Returns: (transfer full): a #GFileIOStream for the newly created
1952 * file, or %NULL on error.
1953 * Free the returned object with g_object_unref().
1958 g_file_create_readwrite (GFile *file,
1959 GFileCreateFlags flags,
1960 GCancellable *cancellable,
1965 g_return_val_if_fail (G_IS_FILE (file), NULL);
1967 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1970 iface = G_FILE_GET_IFACE (file);
1972 if (iface->create_readwrite == NULL)
1974 g_set_error_literal (error, G_IO_ERROR,
1975 G_IO_ERROR_NOT_SUPPORTED,
1976 _("Operation not supported"));
1980 return (* iface->create_readwrite) (file, flags, cancellable, error);
1984 * g_file_replace_readwrite:
1986 * @etag: (nullable): an optional [entity tag][gfile-etag]
1987 * for the current #GFile, or #NULL to ignore
1988 * @make_backup: %TRUE if a backup should be created
1989 * @flags: a set of #GFileCreateFlags
1990 * @cancellable: (nullable): optional #GCancellable object,
1992 * @error: return location for a #GError, or %NULL
1994 * Returns an output stream for overwriting the file in readwrite mode,
1995 * possibly creating a backup copy of the file first. If the file doesn't
1996 * exist, it will be created.
1998 * For details about the behaviour, see g_file_replace() which does the
1999 * same thing but returns an output stream only.
2001 * Note that in many non-local file cases read and write streams are not
2002 * supported, so make sure you really need to do read and write streaming,
2003 * rather than just opening for reading or writing.
2005 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2006 * Free the returned object with g_object_unref().
2011 g_file_replace_readwrite (GFile *file,
2013 gboolean make_backup,
2014 GFileCreateFlags flags,
2015 GCancellable *cancellable,
2020 g_return_val_if_fail (G_IS_FILE (file), NULL);
2022 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2025 iface = G_FILE_GET_IFACE (file);
2027 if (iface->replace_readwrite == NULL)
2029 g_set_error_literal (error, G_IO_ERROR,
2030 G_IO_ERROR_NOT_SUPPORTED,
2031 _("Operation not supported"));
2035 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2039 * g_file_read_async:
2040 * @file: input #GFile
2041 * @io_priority: the [I/O priority][io-priority] of the request
2042 * @cancellable: (nullable): optional #GCancellable object,
2044 * @callback: (scope async): a #GAsyncReadyCallback to call
2045 * when the request is satisfied
2046 * @user_data: (closure): the data to pass to callback function
2048 * Asynchronously opens @file for reading.
2050 * For more details, see g_file_read() which is
2051 * the synchronous version of this call.
2053 * When the operation is finished, @callback will be called.
2054 * You can then call g_file_read_finish() to get the result
2058 g_file_read_async (GFile *file,
2060 GCancellable *cancellable,
2061 GAsyncReadyCallback callback,
2066 g_return_if_fail (G_IS_FILE (file));
2068 iface = G_FILE_GET_IFACE (file);
2069 (* iface->read_async) (file,
2077 * g_file_read_finish:
2078 * @file: input #GFile
2079 * @res: a #GAsyncResult
2080 * @error: a #GError, or %NULL
2082 * Finishes an asynchronous file read operation started with
2083 * g_file_read_async().
2085 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2086 * Free the returned object with g_object_unref().
2089 g_file_read_finish (GFile *file,
2095 g_return_val_if_fail (G_IS_FILE (file), NULL);
2096 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2098 if (g_async_result_legacy_propagate_error (res, error))
2101 iface = G_FILE_GET_IFACE (file);
2102 return (* iface->read_finish) (file, res, error);
2106 * g_file_append_to_async:
2107 * @file: input #GFile
2108 * @flags: a set of #GFileCreateFlags
2109 * @io_priority: the [I/O priority][io-priority] of the request
2110 * @cancellable: (nullable): optional #GCancellable object,
2112 * @callback: (scope async): a #GAsyncReadyCallback to call
2113 * when the request is satisfied
2114 * @user_data: (closure): the data to pass to callback function
2116 * Asynchronously opens @file for appending.
2118 * For more details, see g_file_append_to() which is
2119 * the synchronous version of this call.
2121 * When the operation is finished, @callback will be called.
2122 * You can then call g_file_append_to_finish() to get the result
2126 g_file_append_to_async (GFile *file,
2127 GFileCreateFlags flags,
2129 GCancellable *cancellable,
2130 GAsyncReadyCallback callback,
2135 g_return_if_fail (G_IS_FILE (file));
2137 iface = G_FILE_GET_IFACE (file);
2138 (* iface->append_to_async) (file,
2147 * g_file_append_to_finish:
2148 * @file: input #GFile
2149 * @res: #GAsyncResult
2150 * @error: a #GError, or %NULL
2152 * Finishes an asynchronous file append operation started with
2153 * g_file_append_to_async().
2155 * Returns: (transfer full): a valid #GFileOutputStream
2156 * or %NULL on error.
2157 * Free the returned object with g_object_unref().
2160 g_file_append_to_finish (GFile *file,
2166 g_return_val_if_fail (G_IS_FILE (file), NULL);
2167 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2169 if (g_async_result_legacy_propagate_error (res, error))
2172 iface = G_FILE_GET_IFACE (file);
2173 return (* iface->append_to_finish) (file, res, error);
2177 * g_file_create_async:
2178 * @file: input #GFile
2179 * @flags: a set of #GFileCreateFlags
2180 * @io_priority: the [I/O priority][io-priority] of the request
2181 * @cancellable: (nullable): optional #GCancellable object,
2183 * @callback: (scope async): a #GAsyncReadyCallback to call
2184 * when the request is satisfied
2185 * @user_data: (closure): the data to pass to callback function
2187 * Asynchronously creates a new file and returns an output stream
2188 * for writing to it. The file must not already exist.
2190 * For more details, see g_file_create() which is
2191 * the synchronous version of this call.
2193 * When the operation is finished, @callback will be called.
2194 * You can then call g_file_create_finish() to get the result
2198 g_file_create_async (GFile *file,
2199 GFileCreateFlags flags,
2201 GCancellable *cancellable,
2202 GAsyncReadyCallback callback,
2207 g_return_if_fail (G_IS_FILE (file));
2209 iface = G_FILE_GET_IFACE (file);
2210 (* iface->create_async) (file,
2219 * g_file_create_finish:
2220 * @file: input #GFile
2221 * @res: a #GAsyncResult
2222 * @error: a #GError, or %NULL
2224 * Finishes an asynchronous file create operation started with
2225 * g_file_create_async().
2227 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2228 * Free the returned object with g_object_unref().
2231 g_file_create_finish (GFile *file,
2237 g_return_val_if_fail (G_IS_FILE (file), NULL);
2238 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2240 if (g_async_result_legacy_propagate_error (res, error))
2243 iface = G_FILE_GET_IFACE (file);
2244 return (* iface->create_finish) (file, res, error);
2248 * g_file_replace_async:
2249 * @file: input #GFile
2250 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2251 * or %NULL to ignore
2252 * @make_backup: %TRUE if a backup should be created
2253 * @flags: a set of #GFileCreateFlags
2254 * @io_priority: the [I/O priority][io-priority] of the request
2255 * @cancellable: (nullable): optional #GCancellable object,
2257 * @callback: (scope async): a #GAsyncReadyCallback to call
2258 * when the request is satisfied
2259 * @user_data: (closure): the data to pass to callback function
2261 * Asynchronously overwrites the file, replacing the contents,
2262 * possibly creating a backup copy of the file first.
2264 * For more details, see g_file_replace() which is
2265 * the synchronous version of this call.
2267 * When the operation is finished, @callback will be called.
2268 * You can then call g_file_replace_finish() to get the result
2272 g_file_replace_async (GFile *file,
2274 gboolean make_backup,
2275 GFileCreateFlags flags,
2277 GCancellable *cancellable,
2278 GAsyncReadyCallback callback,
2283 g_return_if_fail (G_IS_FILE (file));
2285 iface = G_FILE_GET_IFACE (file);
2286 (* iface->replace_async) (file,
2297 * g_file_replace_finish:
2298 * @file: input #GFile
2299 * @res: a #GAsyncResult
2300 * @error: a #GError, or %NULL
2302 * Finishes an asynchronous file replace operation started with
2303 * g_file_replace_async().
2305 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2306 * Free the returned object with g_object_unref().
2309 g_file_replace_finish (GFile *file,
2315 g_return_val_if_fail (G_IS_FILE (file), NULL);
2316 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2318 if (g_async_result_legacy_propagate_error (res, error))
2321 iface = G_FILE_GET_IFACE (file);
2322 return (* iface->replace_finish) (file, res, error);
2326 * g_file_open_readwrite_async
2327 * @file: input #GFile
2328 * @io_priority: the [I/O priority][io-priority] of the request
2329 * @cancellable: (nullable): optional #GCancellable object,
2331 * @callback: (scope async): a #GAsyncReadyCallback to call
2332 * when the request is satisfied
2333 * @user_data: (closure): the data to pass to callback function
2335 * Asynchronously opens @file for reading and writing.
2337 * For more details, see g_file_open_readwrite() which is
2338 * the synchronous version of this call.
2340 * When the operation is finished, @callback will be called.
2341 * You can then call g_file_open_readwrite_finish() to get
2342 * the result of the operation.
2347 g_file_open_readwrite_async (GFile *file,
2349 GCancellable *cancellable,
2350 GAsyncReadyCallback callback,
2355 g_return_if_fail (G_IS_FILE (file));
2357 iface = G_FILE_GET_IFACE (file);
2358 (* iface->open_readwrite_async) (file,
2366 * g_file_open_readwrite_finish:
2367 * @file: input #GFile
2368 * @res: a #GAsyncResult
2369 * @error: a #GError, or %NULL
2371 * Finishes an asynchronous file read operation started with
2372 * g_file_open_readwrite_async().
2374 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2375 * Free the returned object with g_object_unref().
2380 g_file_open_readwrite_finish (GFile *file,
2386 g_return_val_if_fail (G_IS_FILE (file), NULL);
2387 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2389 if (g_async_result_legacy_propagate_error (res, error))
2392 iface = G_FILE_GET_IFACE (file);
2393 return (* iface->open_readwrite_finish) (file, res, error);
2397 * g_file_create_readwrite_async:
2398 * @file: input #GFile
2399 * @flags: a set of #GFileCreateFlags
2400 * @io_priority: the [I/O priority][io-priority] of the request
2401 * @cancellable: (nullable): optional #GCancellable object,
2403 * @callback: (scope async): a #GAsyncReadyCallback to call
2404 * when the request is satisfied
2405 * @user_data: (closure): the data to pass to callback function
2407 * Asynchronously creates a new file and returns a stream
2408 * for reading and writing to it. The file must not already exist.
2410 * For more details, see g_file_create_readwrite() which is
2411 * the synchronous version of this call.
2413 * When the operation is finished, @callback will be called.
2414 * You can then call g_file_create_readwrite_finish() to get
2415 * the result of the operation.
2420 g_file_create_readwrite_async (GFile *file,
2421 GFileCreateFlags flags,
2423 GCancellable *cancellable,
2424 GAsyncReadyCallback callback,
2429 g_return_if_fail (G_IS_FILE (file));
2431 iface = G_FILE_GET_IFACE (file);
2432 (* iface->create_readwrite_async) (file,
2441 * g_file_create_readwrite_finish:
2442 * @file: input #GFile
2443 * @res: a #GAsyncResult
2444 * @error: a #GError, or %NULL
2446 * Finishes an asynchronous file create operation started with
2447 * g_file_create_readwrite_async().
2449 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2450 * Free the returned object with g_object_unref().
2455 g_file_create_readwrite_finish (GFile *file,
2461 g_return_val_if_fail (G_IS_FILE (file), NULL);
2462 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2464 if (g_async_result_legacy_propagate_error (res, error))
2467 iface = G_FILE_GET_IFACE (file);
2468 return (* iface->create_readwrite_finish) (file, res, error);
2472 * g_file_replace_readwrite_async:
2473 * @file: input #GFile
2474 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2475 * or %NULL to ignore
2476 * @make_backup: %TRUE if a backup should be created
2477 * @flags: a set of #GFileCreateFlags
2478 * @io_priority: the [I/O priority][io-priority] of the request
2479 * @cancellable: (nullable): optional #GCancellable object,
2481 * @callback: (scope async): a #GAsyncReadyCallback to call
2482 * when the request is satisfied
2483 * @user_data: (closure): the data to pass to callback function
2485 * Asynchronously overwrites the file in read-write mode,
2486 * replacing the contents, possibly creating a backup copy
2487 * of the file first.
2489 * For more details, see g_file_replace_readwrite() which is
2490 * the synchronous version of this call.
2492 * When the operation is finished, @callback will be called.
2493 * You can then call g_file_replace_readwrite_finish() to get
2494 * the result of the operation.
2499 g_file_replace_readwrite_async (GFile *file,
2501 gboolean make_backup,
2502 GFileCreateFlags flags,
2504 GCancellable *cancellable,
2505 GAsyncReadyCallback callback,
2510 g_return_if_fail (G_IS_FILE (file));
2512 iface = G_FILE_GET_IFACE (file);
2513 (* iface->replace_readwrite_async) (file,
2524 * g_file_replace_readwrite_finish:
2525 * @file: input #GFile
2526 * @res: a #GAsyncResult
2527 * @error: a #GError, or %NULL
2529 * Finishes an asynchronous file replace operation started with
2530 * g_file_replace_readwrite_async().
2532 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2533 * Free the returned object with g_object_unref().
2538 g_file_replace_readwrite_finish (GFile *file,
2544 g_return_val_if_fail (G_IS_FILE (file), NULL);
2545 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2547 if (g_async_result_legacy_propagate_error (res, error))
2550 iface = G_FILE_GET_IFACE (file);
2551 return (* iface->replace_readwrite_finish) (file, res, error);
2555 copy_symlink (GFile *destination,
2556 GFileCopyFlags flags,
2557 GCancellable *cancellable,
2562 gboolean tried_delete;
2564 GFileType file_type;
2566 tried_delete = FALSE;
2570 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2572 /* Maybe it already existed, and we want to overwrite? */
2573 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2574 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2576 g_clear_error (&my_error);
2578 /* Don't overwrite if the destination is a directory */
2579 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2580 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2581 cancellable, &my_error);
2584 file_type = g_file_info_get_file_type (info);
2585 g_object_unref (info);
2587 if (file_type == G_FILE_TYPE_DIRECTORY)
2589 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2590 _("Can’t copy over directory"));
2595 if (!g_file_delete (destination, cancellable, error))
2598 tried_delete = TRUE;
2602 g_propagate_error (error, my_error);
2609 static GFileInputStream *
2610 open_source_for_copy (GFile *source,
2612 GFileCopyFlags flags,
2613 GCancellable *cancellable,
2617 GFileInputStream *ret;
2619 GFileType file_type;
2622 ret = g_file_read (source, cancellable, &my_error);
2626 /* There was an error opening the source, try to set a good error for it: */
2627 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2629 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2630 * as that is less useful to the app. Better check for errors on the
2633 g_error_free (my_error);
2636 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2637 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2638 cancellable, &my_error);
2640 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2642 file_type = g_file_info_get_file_type (info);
2643 g_object_unref (info);
2645 if (flags & G_FILE_COPY_OVERWRITE)
2647 if (file_type == G_FILE_TYPE_DIRECTORY)
2649 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2650 _("Can’t copy directory over directory"));
2653 /* continue to would_recurse error */
2657 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2658 _("Target file exists"));
2664 /* Error getting info from target, return that error
2665 * (except for NOT_FOUND, which is no error here)
2667 g_clear_object (&info);
2668 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2670 g_propagate_error (error, my_error);
2673 g_clear_error (&my_error);
2676 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2677 _("Can’t recursively copy directory"));
2681 g_propagate_error (error, my_error);
2686 should_copy (GFileAttributeInfo *info,
2687 gboolean copy_all_attributes,
2688 gboolean skip_perms)
2690 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2693 if (copy_all_attributes)
2694 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2695 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2699 build_attribute_list_for_copy (GFile *file,
2700 GFileCopyFlags flags,
2701 char **out_attributes,
2702 GCancellable *cancellable,
2705 gboolean ret = FALSE;
2706 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2710 gboolean copy_all_attributes;
2711 gboolean skip_perms;
2713 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2714 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2716 /* Ignore errors here, if the target supports no attributes there is
2717 * nothing to copy. We still honor the cancellable though.
2719 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2720 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2723 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2724 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2727 if (attributes == NULL && namespaces == NULL)
2731 s = g_string_new ("");
2735 for (i = 0; i < attributes->n_infos; i++)
2737 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2742 g_string_append_c (s, ',');
2744 g_string_append (s, attributes->infos[i].name);
2751 for (i = 0; i < namespaces->n_infos; i++)
2753 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2758 g_string_append_c (s, ',');
2760 g_string_append (s, namespaces->infos[i].name);
2761 g_string_append (s, "::*");
2767 *out_attributes = g_string_free (s, FALSE);
2771 g_string_free (s, TRUE);
2773 g_file_attribute_info_list_unref (attributes);
2775 g_file_attribute_info_list_unref (namespaces);
2781 * g_file_copy_attributes:
2782 * @source: a #GFile with attributes
2783 * @destination: a #GFile to copy attributes to
2784 * @flags: a set of #GFileCopyFlags
2785 * @cancellable: (nullable): optional #GCancellable object,
2787 * @error: a #GError, %NULL to ignore
2789 * Copies the file attributes from @source to @destination.
2791 * Normally only a subset of the file attributes are copied,
2792 * those that are copies in a normal file copy operation
2793 * (which for instance does not include e.g. owner). However
2794 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2795 * all the metadata that is possible to copy is copied. This
2796 * is useful when implementing move by copy + delete source.
2798 * Returns: %TRUE if the attributes were copied successfully,
2802 g_file_copy_attributes (GFile *source,
2804 GFileCopyFlags flags,
2805 GCancellable *cancellable,
2808 char *attrs_to_read;
2811 gboolean source_nofollow_symlinks;
2813 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2814 cancellable, error))
2817 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2819 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2820 * we just don't copy it.
2822 info = g_file_query_info (source, attrs_to_read,
2823 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2827 g_free (attrs_to_read);
2832 res = g_file_set_attributes_from_info (destination,
2834 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2837 g_object_unref (info);
2843 /* 256k minus malloc overhead */
2844 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2847 copy_stream_with_progress (GInputStream *in,
2850 GCancellable *cancellable,
2851 GFileProgressCallback progress_callback,
2852 gpointer progress_callback_data,
2857 goffset current_size;
2864 /* avoid performance impact of querying total size when it's not needed */
2865 if (progress_callback)
2867 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2868 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2872 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2873 total_size = g_file_info_get_size (info);
2874 g_object_unref (info);
2877 if (total_size == -1)
2879 info = g_file_query_info (source,
2880 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2881 G_FILE_QUERY_INFO_NONE,
2885 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2886 total_size = g_file_info_get_size (info);
2887 g_object_unref (info);
2892 if (total_size == -1)
2895 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2900 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2910 current_size += n_read;
2912 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2916 if (progress_callback)
2917 progress_callback (current_size, total_size, progress_callback_data);
2921 /* Make sure we send full copied size */
2922 if (progress_callback)
2923 progress_callback (current_size, total_size, progress_callback_data);
2931 do_splice (int fd_in,
2936 long *bytes_transferd,
2942 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2950 else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
2951 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2952 _("Splice not supported"));
2954 g_set_error (error, G_IO_ERROR,
2955 g_io_error_from_errno (errsv),
2956 _("Error splicing file: %s"),
2957 g_strerror (errsv));
2962 *bytes_transferd = result;
2967 splice_stream_with_progress (GInputStream *in,
2969 GCancellable *cancellable,
2970 GFileProgressCallback progress_callback,
2971 gpointer progress_callback_data,
2974 int buffer[2] = { -1, -1 };
2982 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2983 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2985 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2988 #if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
2989 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
2990 * pipe size. See: https://bugzilla.gnome.org/791457 */
2991 buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
2992 if (buffer_size <= 0)
2995 buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
2998 if (buffer_size <= 0)
3000 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3001 _("Error splicing file: %s"), g_strerror (errsv));
3007 /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3008 * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
3009 * running on Linux < 2.6.11 (where the capacity was the system page size,
3010 * typically 4KiB) because it’s ancient. See pipe(7). */
3011 buffer_size = 1024 * 64;
3014 g_assert (buffer_size > 0);
3017 /* avoid performance impact of querying total size when it's not needed */
3018 if (progress_callback)
3022 if (fstat (fd_in, &sbuf) == 0)
3023 total_size = sbuf.st_size;
3026 if (total_size == -1)
3029 offset_in = offset_out = 0;
3036 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3039 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3050 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3053 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3056 n_read -= n_written;
3059 if (progress_callback)
3060 progress_callback (offset_in, total_size, progress_callback_data);
3063 /* Make sure we send full copied size */
3064 if (progress_callback)
3065 progress_callback (offset_in, total_size, progress_callback_data);
3067 if (!g_close (buffer[0], error))
3070 if (!g_close (buffer[1], error))
3074 if (buffer[0] != -1)
3075 (void) g_close (buffer[0], NULL);
3076 if (buffer[1] != -1)
3077 (void) g_close (buffer[1], NULL);
3085 btrfs_reflink_with_progress (GInputStream *in,
3088 GCancellable *cancellable,
3089 GFileProgressCallback progress_callback,
3090 gpointer progress_callback_data,
3093 goffset source_size;
3097 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3098 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3100 if (progress_callback)
3101 source_size = g_file_info_get_size (info);
3103 /* Btrfs clone ioctl properties:
3104 * - Works at the inode level
3105 * - Doesn't work with directories
3106 * - Always follows symlinks (source and destination)
3108 * By the time we get here, *in and *out are both regular files */
3109 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3115 g_set_error_literal (error, G_IO_ERROR,
3116 G_IO_ERROR_NOT_SUPPORTED,
3117 _("Copy (reflink/clone) between mounts is not supported"));
3118 else if (errsv == EINVAL)
3119 g_set_error_literal (error, G_IO_ERROR,
3120 G_IO_ERROR_NOT_SUPPORTED,
3121 _("Copy (reflink/clone) is not supported or invalid"));
3123 /* Most probably something odd happened; retry with fallback */
3124 g_set_error_literal (error, G_IO_ERROR,
3125 G_IO_ERROR_NOT_SUPPORTED,
3126 _("Copy (reflink/clone) is not supported or didn’t work"));
3127 /* We retry with fallback for all error cases because Btrfs is currently
3128 * unstable, and so we can't trust it to do clone properly.
3129 * In addition, any hard errors here would cause the same failure in the
3130 * fallback manual copy as well. */
3134 /* Make sure we send full copied size */
3135 if (progress_callback)
3136 progress_callback (source_size, source_size, progress_callback_data);
3143 file_copy_fallback (GFile *source,
3145 GFileCopyFlags flags,
3146 GCancellable *cancellable,
3147 GFileProgressCallback progress_callback,
3148 gpointer progress_callback_data,
3151 gboolean ret = FALSE;
3152 GFileInputStream *file_in = NULL;
3153 GInputStream *in = NULL;
3154 GOutputStream *out = NULL;
3155 GFileInfo *info = NULL;
3157 char *attrs_to_read;
3158 gboolean do_set_attributes = FALSE;
3159 GFileCreateFlags create_flags;
3161 /* need to know the file type */
3162 info = g_file_query_info (source,
3163 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3164 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3170 /* Maybe copy the symlink? */
3171 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3172 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3174 target = g_file_info_get_symlink_target (info);
3177 if (!copy_symlink (destination, flags, cancellable, target, error))
3183 /* ... else fall back on a regular file copy */
3185 /* Handle "special" files (pipes, device nodes, ...)? */
3186 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3188 /* FIXME: could try to recreate device nodes and others? */
3189 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3190 _("Can’t copy special file"));
3194 /* Everything else should just fall back on a regular copy. */
3196 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3199 in = G_INPUT_STREAM (file_in);
3201 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3202 cancellable, error))
3205 if (attrs_to_read != NULL)
3207 GError *tmp_error = NULL;
3209 /* Ok, ditch the previous lightweight info (on Unix we just
3210 * called lstat()); at this point we gather all the information
3211 * we need about the source from the opened file descriptor.
3213 g_object_unref (info);
3215 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3216 cancellable, &tmp_error);
3219 /* Not all gvfs backends implement query_info_on_read(), we
3220 * can just fall back to the pathname again.
3221 * https://bugzilla.gnome.org/706254
3223 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3225 g_clear_error (&tmp_error);
3226 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3227 cancellable, error);
3231 g_free (attrs_to_read);
3232 g_propagate_error (error, tmp_error);
3236 g_free (attrs_to_read);
3240 do_set_attributes = TRUE;
3243 /* In the local file path, we pass down the source info which
3244 * includes things like unix::mode, to ensure that the target file
3245 * is not created with different permissions from the source file.
3247 * If a future API like g_file_replace_with_info() is added, switch
3248 * this code to use that.
3250 * Use %G_FILE_CREATE_PRIVATE unless
3251 * - we were told to create the file with default permissions (i.e. the
3253 * - or if the source file is on a file system which doesn’t support
3254 * `unix::mode` (in which case it probably also makes sense to create the
3255 * destination with default permissions because the source cannot be
3257 * - or if the destination file is a `GLocalFile`, in which case we can
3258 * directly open() it with the permissions from the source file.
3260 create_flags = G_FILE_CREATE_NONE;
3261 if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3262 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3263 !G_IS_LOCAL_FILE (destination))
3264 create_flags |= G_FILE_CREATE_PRIVATE;
3265 if (flags & G_FILE_COPY_OVERWRITE)
3266 create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3268 if (G_IS_LOCAL_FILE (destination))
3270 if (flags & G_FILE_COPY_OVERWRITE)
3271 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3273 flags & G_FILE_COPY_BACKUP,
3275 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3276 cancellable, error);
3278 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3279 FALSE, create_flags,
3280 (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3281 cancellable, error);
3283 else if (flags & G_FILE_COPY_OVERWRITE)
3285 out = (GOutputStream *)g_file_replace (destination,
3287 flags & G_FILE_COPY_BACKUP,
3289 cancellable, error);
3293 out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3300 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3302 GError *reflink_err = NULL;
3304 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3305 progress_callback, progress_callback_data,
3308 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3310 g_clear_error (&reflink_err);
3314 g_propagate_error (error, reflink_err);
3327 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3329 GError *splice_err = NULL;
3331 if (!splice_stream_with_progress (in, out, cancellable,
3332 progress_callback, progress_callback_data,
3335 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3337 g_clear_error (&splice_err);
3341 g_propagate_error (error, splice_err);
3354 /* A plain read/write loop */
3355 if (!copy_stream_with_progress (in, out, source, cancellable,
3356 progress_callback, progress_callback_data,
3364 /* Don't care about errors in source here */
3365 (void) g_input_stream_close (in, cancellable, NULL);
3366 g_object_unref (in);
3371 /* But write errors on close are bad! */
3372 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3374 g_object_unref (out);
3377 /* Ignore errors here. Failure to copy metadata is not a hard error */
3378 /* TODO: set these attributes /before/ we do the rename() on Unix */
3379 if (ret && do_set_attributes)
3381 g_file_set_attributes_from_info (destination,
3383 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3388 g_clear_object (&info);
3395 * @source: input #GFile
3396 * @destination: destination #GFile
3397 * @flags: set of #GFileCopyFlags
3398 * @cancellable: (nullable): optional #GCancellable object,
3400 * @progress_callback: (nullable) (scope call): function to callback with
3401 * progress information, or %NULL if progress information is not needed
3402 * @progress_callback_data: (closure): user data to pass to @progress_callback
3403 * @error: #GError to set on error, or %NULL
3405 * Copies the file @source to the location specified by @destination.
3406 * Can not handle recursive copies of directories.
3408 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3409 * existing @destination file is overwritten.
3411 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3412 * will be copied as symlinks, otherwise the target of the
3413 * @source symlink will be copied.
3415 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3416 * that is possible to copy is copied, not just the default subset (which,
3417 * for instance, does not include the owner, see #GFileInfo).
3419 * If @cancellable is not %NULL, then the operation can be cancelled by
3420 * triggering the cancellable object from another thread. If the operation
3421 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3423 * If @progress_callback is not %NULL, then the operation can be monitored
3424 * by setting this to a #GFileProgressCallback function.
3425 * @progress_callback_data will be passed to this function. It is guaranteed
3426 * that this callback will be called after all data has been transferred with
3427 * the total number of bytes copied during the operation.
3429 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3430 * is returned, independent on the status of the @destination.
3432 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3433 * the error %G_IO_ERROR_EXISTS is returned.
3435 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3436 * error is returned. If trying to overwrite a directory with a directory the
3437 * %G_IO_ERROR_WOULD_MERGE error is returned.
3439 * If the source is a directory and the target does not exist, or
3440 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3441 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3443 * If you are interested in copying the #GFile object itself (not the on-disk
3444 * file), see g_file_dup().
3446 * Returns: %TRUE on success, %FALSE otherwise.
3449 g_file_copy (GFile *source,
3451 GFileCopyFlags flags,
3452 GCancellable *cancellable,
3453 GFileProgressCallback progress_callback,
3454 gpointer progress_callback_data,
3461 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3462 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3464 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3467 iface = G_FILE_GET_IFACE (destination);
3471 res = (* iface->copy) (source, destination,
3473 progress_callback, progress_callback_data,
3479 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3481 g_propagate_error (error, my_error);
3485 g_clear_error (&my_error);
3488 /* If the types are different, and the destination method failed
3489 * also try the source method
3491 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3493 iface = G_FILE_GET_IFACE (source);
3498 res = (* iface->copy) (source, destination,
3500 progress_callback, progress_callback_data,
3506 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3508 g_propagate_error (error, my_error);
3512 g_clear_error (&my_error);
3516 return file_copy_fallback (source, destination, flags, cancellable,
3517 progress_callback, progress_callback_data,
3522 * g_file_copy_async:
3523 * @source: input #GFile
3524 * @destination: destination #GFile
3525 * @flags: set of #GFileCopyFlags
3526 * @io_priority: the [I/O priority][io-priority] of the request
3527 * @cancellable: (nullable): optional #GCancellable object,
3529 * @progress_callback: (nullable) (scope notified): function to callback with progress
3530 * information, or %NULL if progress information is not needed
3531 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3532 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3533 * @user_data: (closure callback): the data to pass to callback function
3535 * Copies the file @source to the location specified by @destination
3536 * asynchronously. For details of the behaviour, see g_file_copy().
3538 * If @progress_callback is not %NULL, then that function that will be called
3539 * just like in g_file_copy(). The callback will run in the default main context
3540 * of the thread calling g_file_copy_async() — the same context as @callback is
3543 * When the operation is finished, @callback will be called. You can then call
3544 * g_file_copy_finish() to get the result of the operation.
3547 g_file_copy_async (GFile *source,
3549 GFileCopyFlags flags,
3551 GCancellable *cancellable,
3552 GFileProgressCallback progress_callback,
3553 gpointer progress_callback_data,
3554 GAsyncReadyCallback callback,
3559 g_return_if_fail (G_IS_FILE (source));
3560 g_return_if_fail (G_IS_FILE (destination));
3562 iface = G_FILE_GET_IFACE (source);
3563 (* iface->copy_async) (source,
3569 progress_callback_data,
3575 * g_file_copy_finish:
3576 * @file: input #GFile
3577 * @res: a #GAsyncResult
3578 * @error: a #GError, or %NULL
3580 * Finishes copying the file started with g_file_copy_async().
3582 * Returns: a %TRUE on success, %FALSE on error.
3585 g_file_copy_finish (GFile *file,
3591 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3592 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3594 if (g_async_result_legacy_propagate_error (res, error))
3597 iface = G_FILE_GET_IFACE (file);
3598 return (* iface->copy_finish) (file, res, error);
3603 * @source: #GFile pointing to the source location
3604 * @destination: #GFile pointing to the destination location
3605 * @flags: set of #GFileCopyFlags
3606 * @cancellable: (nullable): optional #GCancellable object,
3608 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3609 * function for updates
3610 * @progress_callback_data: (closure): gpointer to user data for
3611 * the callback function
3612 * @error: #GError for returning error conditions, or %NULL
3614 * Tries to move the file or directory @source to the location specified
3615 * by @destination. If native move operations are supported then this is
3616 * used, otherwise a copy + delete fallback is used. The native
3617 * implementation may support moving directories (for instance on moves
3618 * inside the same filesystem), but the fallback code does not.
3620 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3621 * existing @destination file is overwritten.
3623 * If @cancellable is not %NULL, then the operation can be cancelled by
3624 * triggering the cancellable object from another thread. If the operation
3625 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3627 * If @progress_callback is not %NULL, then the operation can be monitored
3628 * by setting this to a #GFileProgressCallback function.
3629 * @progress_callback_data will be passed to this function. It is
3630 * guaranteed that this callback will be called after all data has been
3631 * transferred with the total number of bytes copied during the operation.
3633 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3634 * error is returned, independent on the status of the @destination.
3636 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3637 * then the error %G_IO_ERROR_EXISTS is returned.
3639 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3640 * error is returned. If trying to overwrite a directory with a directory the
3641 * %G_IO_ERROR_WOULD_MERGE error is returned.
3643 * If the source is a directory and the target does not exist, or
3644 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3645 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3646 * move operation isn't available).
3648 * Returns: %TRUE on successful move, %FALSE otherwise.
3651 g_file_move (GFile *source,
3653 GFileCopyFlags flags,
3654 GCancellable *cancellable,
3655 GFileProgressCallback progress_callback,
3656 gpointer progress_callback_data,
3663 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3664 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3666 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3669 iface = G_FILE_GET_IFACE (destination);
3673 res = (* iface->move) (source, destination,
3675 progress_callback, progress_callback_data,
3681 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3683 g_propagate_error (error, my_error);
3687 g_clear_error (&my_error);
3690 /* If the types are different, and the destination method failed
3691 * also try the source method
3693 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3695 iface = G_FILE_GET_IFACE (source);
3700 res = (* iface->move) (source, destination,
3702 progress_callback, progress_callback_data,
3708 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3710 g_propagate_error (error, my_error);
3714 g_clear_error (&my_error);
3718 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3720 g_set_error_literal (error, G_IO_ERROR,
3721 G_IO_ERROR_NOT_SUPPORTED,
3722 _("Operation not supported"));
3726 flags |= G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS;
3727 if (!g_file_copy (source, destination, flags, cancellable,
3728 progress_callback, progress_callback_data,
3732 return g_file_delete (source, cancellable, error);
3736 * g_file_make_directory:
3737 * @file: input #GFile
3738 * @cancellable: (nullable): optional #GCancellable object,
3740 * @error: a #GError, or %NULL
3742 * Creates a directory. Note that this will only create a child directory
3743 * of the immediate parent directory of the path or URI given by the #GFile.
3744 * To recursively create directories, see g_file_make_directory_with_parents().
3745 * This function will fail if the parent directory does not exist, setting
3746 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3747 * creating directories, this function will fail, setting @error to
3748 * %G_IO_ERROR_NOT_SUPPORTED.
3750 * For a local #GFile the newly created directory will have the default
3751 * (current) ownership and permissions of the current process.
3753 * If @cancellable is not %NULL, then the operation can be cancelled by
3754 * triggering the cancellable object from another thread. If the operation
3755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3757 * Returns: %TRUE on successful creation, %FALSE otherwise.
3760 g_file_make_directory (GFile *file,
3761 GCancellable *cancellable,
3766 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3771 iface = G_FILE_GET_IFACE (file);
3773 if (iface->make_directory == NULL)
3775 g_set_error_literal (error, G_IO_ERROR,
3776 G_IO_ERROR_NOT_SUPPORTED,
3777 _("Operation not supported"));
3781 return (* iface->make_directory) (file, cancellable, error);
3785 * g_file_make_directory_async:
3786 * @file: input #GFile
3787 * @io_priority: the [I/O priority][io-priority] of the request
3788 * @cancellable: (nullable): optional #GCancellable object,
3790 * @callback: a #GAsyncReadyCallback to call
3791 * when the request is satisfied
3792 * @user_data: the data to pass to callback function
3794 * Asynchronously creates a directory.
3796 * Virtual: make_directory_async
3800 g_file_make_directory_async (GFile *file,
3802 GCancellable *cancellable,
3803 GAsyncReadyCallback callback,
3808 g_return_if_fail (G_IS_FILE (file));
3810 iface = G_FILE_GET_IFACE (file);
3811 (* iface->make_directory_async) (file,
3819 * g_file_make_directory_finish:
3820 * @file: input #GFile
3821 * @result: a #GAsyncResult
3822 * @error: a #GError, or %NULL
3824 * Finishes an asynchronous directory creation, started with
3825 * g_file_make_directory_async().
3827 * Virtual: make_directory_finish
3828 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3832 g_file_make_directory_finish (GFile *file,
3833 GAsyncResult *result,
3838 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3839 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3841 iface = G_FILE_GET_IFACE (file);
3842 return (* iface->make_directory_finish) (file, result, error);
3846 * g_file_make_directory_with_parents:
3847 * @file: input #GFile
3848 * @cancellable: (nullable): optional #GCancellable object,
3850 * @error: a #GError, or %NULL
3852 * Creates a directory and any parent directories that may not
3853 * exist similar to 'mkdir -p'. If the file system does not support
3854 * creating directories, this function will fail, setting @error to
3855 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3856 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3857 * the similar g_mkdir_with_parents().
3859 * For a local #GFile the newly created directories will have the default
3860 * (current) ownership and permissions of the current process.
3862 * If @cancellable is not %NULL, then the operation can be cancelled by
3863 * triggering the cancellable object from another thread. If the operation
3864 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3866 * Returns: %TRUE if all directories have been successfully created, %FALSE
3872 g_file_make_directory_with_parents (GFile *file,
3873 GCancellable *cancellable,
3876 GFile *work_file = NULL;
3877 GList *list = NULL, *l;
3878 GError *my_error = NULL;
3880 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3885 /* Try for the simple case of not having to create any parent
3886 * directories. If any parent directory needs to be created, this
3887 * call will fail with NOT_FOUND. If that happens, then that value of
3888 * my_error persists into the while loop below.
3890 g_file_make_directory (file, cancellable, &my_error);
3891 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3894 g_propagate_error (error, my_error);
3895 return my_error == NULL;
3898 work_file = g_object_ref (file);
3900 /* Creates the parent directories as needed. In case any particular
3901 * creation operation fails for lack of other parent directories
3902 * (NOT_FOUND), the directory is added to a list of directories to
3903 * create later, and the value of my_error is retained until the next
3904 * iteration of the loop. After the loop my_error should either be
3905 * empty or contain a real failure condition.
3907 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3911 parent_file = g_file_get_parent (work_file);
3912 if (parent_file == NULL)
3915 g_clear_error (&my_error);
3916 g_file_make_directory (parent_file, cancellable, &my_error);
3917 /* Another process may have created the directory in between the
3918 * G_IO_ERROR_NOT_FOUND and now
3920 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3921 g_clear_error (&my_error);
3923 g_object_unref (work_file);
3924 work_file = g_object_ref (parent_file);
3926 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3927 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3929 g_object_unref (parent_file);
3932 /* All directories should be able to be created now, so an error at
3933 * this point means the whole operation must fail -- except an EXISTS
3934 * error, which means that another process already created the
3935 * directory in between the previous failure and now.
3937 for (l = list; my_error == NULL && l; l = l->next)
3939 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3940 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3941 g_clear_error (&my_error);
3945 g_object_unref (work_file);
3948 while (list != NULL)
3950 g_object_unref ((GFile *) list->data);
3951 list = g_list_remove (list, list->data);
3954 /* At this point an error in my_error means a that something
3955 * unexpected failed in either of the loops above, so the whole
3956 * operation must fail.
3958 if (my_error != NULL)
3960 g_propagate_error (error, my_error);
3964 return g_file_make_directory (file, cancellable, error);
3968 * g_file_make_symbolic_link:
3969 * @file: a #GFile with the name of the symlink to create
3970 * @symlink_value: (type filename): a string with the path for the target
3971 * of the new symlink
3972 * @cancellable: (nullable): optional #GCancellable object,
3976 * Creates a symbolic link named @file which contains the string
3979 * If @cancellable is not %NULL, then the operation can be cancelled by
3980 * triggering the cancellable object from another thread. If the operation
3981 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3983 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3986 g_file_make_symbolic_link (GFile *file,
3987 const char *symlink_value,
3988 GCancellable *cancellable,
3993 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3994 g_return_val_if_fail (symlink_value != NULL, FALSE);
3996 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3999 if (*symlink_value == '\0')
4001 g_set_error_literal (error, G_IO_ERROR,
4002 G_IO_ERROR_INVALID_ARGUMENT,
4003 _("Invalid symlink value given"));
4007 iface = G_FILE_GET_IFACE (file);
4009 if (iface->make_symbolic_link == NULL)
4011 g_set_error_literal (error, G_IO_ERROR,
4012 G_IO_ERROR_NOT_SUPPORTED,
4013 _("Symbolic links not supported"));
4017 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4022 * @file: input #GFile
4023 * @cancellable: (nullable): optional #GCancellable object,
4025 * @error: a #GError, or %NULL
4027 * Deletes a file. If the @file is a directory, it will only be
4028 * deleted if it is empty. This has the same semantics as g_unlink().
4030 * If @cancellable is not %NULL, then the operation can be cancelled by
4031 * triggering the cancellable object from another thread. If the operation
4032 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4034 * Virtual: delete_file
4035 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4038 g_file_delete (GFile *file,
4039 GCancellable *cancellable,
4044 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4046 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4049 iface = G_FILE_GET_IFACE (file);
4051 if (iface->delete_file == NULL)
4053 g_set_error_literal (error, G_IO_ERROR,
4054 G_IO_ERROR_NOT_SUPPORTED,
4055 _("Operation not supported"));
4059 return (* iface->delete_file) (file, cancellable, error);
4063 * g_file_delete_async:
4064 * @file: input #GFile
4065 * @io_priority: the [I/O priority][io-priority] of the request
4066 * @cancellable: (nullable): optional #GCancellable object,
4068 * @callback: a #GAsyncReadyCallback to call
4069 * when the request is satisfied
4070 * @user_data: the data to pass to callback function
4072 * Asynchronously delete a file. If the @file is a directory, it will
4073 * only be deleted if it is empty. This has the same semantics as
4076 * Virtual: delete_file_async
4080 g_file_delete_async (GFile *file,
4082 GCancellable *cancellable,
4083 GAsyncReadyCallback callback,
4088 g_return_if_fail (G_IS_FILE (file));
4090 iface = G_FILE_GET_IFACE (file);
4091 (* iface->delete_file_async) (file,
4099 * g_file_delete_finish:
4100 * @file: input #GFile
4101 * @result: a #GAsyncResult
4102 * @error: a #GError, or %NULL
4104 * Finishes deleting a file started with g_file_delete_async().
4106 * Virtual: delete_file_finish
4107 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4111 g_file_delete_finish (GFile *file,
4112 GAsyncResult *result,
4117 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4118 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4120 if (g_async_result_legacy_propagate_error (result, error))
4123 iface = G_FILE_GET_IFACE (file);
4124 return (* iface->delete_file_finish) (file, result, error);
4129 * @file: #GFile to send to trash
4130 * @cancellable: (nullable): optional #GCancellable object,
4132 * @error: a #GError, or %NULL
4134 * Sends @file to the "Trashcan", if possible. This is similar to
4135 * deleting it, but the user can recover it before emptying the trashcan.
4136 * Not all file systems support trashing, so this call can return the
4137 * %G_IO_ERROR_NOT_SUPPORTED error.
4139 * If @cancellable is not %NULL, then the operation can be cancelled by
4140 * triggering the cancellable object from another thread. If the operation
4141 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4144 * Returns: %TRUE on successful trash, %FALSE otherwise.
4147 g_file_trash (GFile *file,
4148 GCancellable *cancellable,
4153 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4155 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4158 iface = G_FILE_GET_IFACE (file);
4160 if (iface->trash == NULL)
4162 g_set_error_literal (error,
4163 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4164 _("Trash not supported"));
4168 return (* iface->trash) (file, cancellable, error);
4172 * g_file_trash_async:
4173 * @file: input #GFile
4174 * @io_priority: the [I/O priority][io-priority] of the request
4175 * @cancellable: (nullable): optional #GCancellable object,
4177 * @callback: a #GAsyncReadyCallback to call
4178 * when the request is satisfied
4179 * @user_data: the data to pass to callback function
4181 * Asynchronously sends @file to the Trash location, if possible.
4183 * Virtual: trash_async
4187 g_file_trash_async (GFile *file,
4189 GCancellable *cancellable,
4190 GAsyncReadyCallback callback,
4195 g_return_if_fail (G_IS_FILE (file));
4197 iface = G_FILE_GET_IFACE (file);
4198 (* iface->trash_async) (file,
4206 * g_file_trash_finish:
4207 * @file: input #GFile
4208 * @result: a #GAsyncResult
4209 * @error: a #GError, or %NULL
4211 * Finishes an asynchronous file trashing operation, started with
4212 * g_file_trash_async().
4214 * Virtual: trash_finish
4215 * Returns: %TRUE on successful trash, %FALSE otherwise.
4219 g_file_trash_finish (GFile *file,
4220 GAsyncResult *result,
4225 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4226 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4228 iface = G_FILE_GET_IFACE (file);
4229 return (* iface->trash_finish) (file, result, error);
4233 * g_file_set_display_name:
4234 * @file: input #GFile
4235 * @display_name: a string
4236 * @cancellable: (nullable): optional #GCancellable object,
4238 * @error: a #GError, or %NULL
4240 * Renames @file to the specified display name.
4242 * The display name is converted from UTF-8 to the correct encoding
4243 * for the target filesystem if possible and the @file is renamed to this.
4245 * If you want to implement a rename operation in the user interface the
4246 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4247 * initial value in the rename widget, and then the result after editing
4248 * should be passed to g_file_set_display_name().
4250 * On success the resulting converted filename is returned.
4252 * If @cancellable is not %NULL, then the operation can be cancelled by
4253 * triggering the cancellable object from another thread. If the operation
4254 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4256 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4257 * or %NULL if there was an error.
4258 * Free the returned object with g_object_unref().
4261 g_file_set_display_name (GFile *file,
4262 const gchar *display_name,
4263 GCancellable *cancellable,
4268 g_return_val_if_fail (G_IS_FILE (file), NULL);
4269 g_return_val_if_fail (display_name != NULL, NULL);
4271 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4275 G_IO_ERROR_INVALID_ARGUMENT,
4276 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4280 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4283 iface = G_FILE_GET_IFACE (file);
4285 return (* iface->set_display_name) (file, display_name, cancellable, error);
4289 * g_file_set_display_name_async:
4290 * @file: input #GFile
4291 * @display_name: a string
4292 * @io_priority: the [I/O priority][io-priority] of the request
4293 * @cancellable: (nullable): optional #GCancellable object,
4295 * @callback: (scope async): a #GAsyncReadyCallback to call
4296 * when the request is satisfied
4297 * @user_data: (closure): the data to pass to callback function
4299 * Asynchronously sets the display name for a given #GFile.
4301 * For more details, see g_file_set_display_name() which is
4302 * the synchronous version of this call.
4304 * When the operation is finished, @callback will be called.
4305 * You can then call g_file_set_display_name_finish() to get
4306 * the result of the operation.
4309 g_file_set_display_name_async (GFile *file,
4310 const gchar *display_name,
4312 GCancellable *cancellable,
4313 GAsyncReadyCallback callback,
4318 g_return_if_fail (G_IS_FILE (file));
4319 g_return_if_fail (display_name != NULL);
4321 iface = G_FILE_GET_IFACE (file);
4322 (* iface->set_display_name_async) (file,
4331 * g_file_set_display_name_finish:
4332 * @file: input #GFile
4333 * @res: a #GAsyncResult
4334 * @error: a #GError, or %NULL
4336 * Finishes setting a display name started with
4337 * g_file_set_display_name_async().
4339 * Returns: (transfer full): a #GFile or %NULL on error.
4340 * Free the returned object with g_object_unref().
4343 g_file_set_display_name_finish (GFile *file,
4349 g_return_val_if_fail (G_IS_FILE (file), NULL);
4350 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4352 if (g_async_result_legacy_propagate_error (res, error))
4355 iface = G_FILE_GET_IFACE (file);
4356 return (* iface->set_display_name_finish) (file, res, error);
4360 * g_file_query_settable_attributes:
4361 * @file: input #GFile
4362 * @cancellable: (nullable): optional #GCancellable object,
4364 * @error: a #GError, or %NULL
4366 * Obtain the list of settable attributes for the file.
4368 * Returns the type and full attribute name of all the attributes
4369 * that can be set on this file. This doesn't mean setting it will
4370 * always succeed though, you might get an access failure, or some
4371 * specific file may not support a specific attribute.
4373 * If @cancellable is not %NULL, then the operation can be cancelled by
4374 * triggering the cancellable object from another thread. If the operation
4375 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4377 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4378 * When you are done with it, release it with
4379 * g_file_attribute_info_list_unref()
4381 GFileAttributeInfoList *
4382 g_file_query_settable_attributes (GFile *file,
4383 GCancellable *cancellable,
4388 GFileAttributeInfoList *list;
4390 g_return_val_if_fail (G_IS_FILE (file), NULL);
4392 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4395 iface = G_FILE_GET_IFACE (file);
4397 if (iface->query_settable_attributes == NULL)
4398 return g_file_attribute_info_list_new ();
4401 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4405 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4407 list = g_file_attribute_info_list_new ();
4408 g_error_free (my_error);
4411 g_propagate_error (error, my_error);
4418 * g_file_query_writable_namespaces:
4419 * @file: input #GFile
4420 * @cancellable: (nullable): optional #GCancellable object,
4422 * @error: a #GError, or %NULL
4424 * Obtain the list of attribute namespaces where new attributes
4425 * can be created by a user. An example of this is extended
4426 * attributes (in the "xattr" namespace).
4428 * If @cancellable is not %NULL, then the operation can be cancelled by
4429 * triggering the cancellable object from another thread. If the operation
4430 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4432 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4433 * When you are done with it, release it with
4434 * g_file_attribute_info_list_unref()
4436 GFileAttributeInfoList *
4437 g_file_query_writable_namespaces (GFile *file,
4438 GCancellable *cancellable,
4443 GFileAttributeInfoList *list;
4445 g_return_val_if_fail (G_IS_FILE (file), NULL);
4447 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4450 iface = G_FILE_GET_IFACE (file);
4452 if (iface->query_writable_namespaces == NULL)
4453 return g_file_attribute_info_list_new ();
4456 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4460 g_warn_if_reached();
4461 list = g_file_attribute_info_list_new ();
4464 if (my_error != NULL)
4466 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4468 g_error_free (my_error);
4471 g_propagate_error (error, my_error);
4478 * g_file_set_attribute:
4479 * @file: input #GFile
4480 * @attribute: a string containing the attribute's name
4481 * @type: The type of the attribute
4482 * @value_p: (nullable): a pointer to the value (or the pointer
4483 * itself if the type is a pointer type)
4484 * @flags: a set of #GFileQueryInfoFlags
4485 * @cancellable: (nullable): optional #GCancellable object,
4487 * @error: a #GError, or %NULL
4489 * Sets an attribute in the file with attribute name @attribute to @value_p.
4491 * Some attributes can be unset by setting @type to
4492 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4494 * If @cancellable is not %NULL, then the operation can be cancelled by
4495 * triggering the cancellable object from another thread. If the operation
4496 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4498 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4501 g_file_set_attribute (GFile *file,
4502 const gchar *attribute,
4503 GFileAttributeType type,
4505 GFileQueryInfoFlags flags,
4506 GCancellable *cancellable,
4511 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4512 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4514 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4517 iface = G_FILE_GET_IFACE (file);
4519 if (iface->set_attribute == NULL)
4521 g_set_error_literal (error, G_IO_ERROR,
4522 G_IO_ERROR_NOT_SUPPORTED,
4523 _("Operation not supported"));
4527 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4531 * g_file_set_attributes_from_info:
4532 * @file: input #GFile
4533 * @info: a #GFileInfo
4534 * @flags: #GFileQueryInfoFlags
4535 * @cancellable: (nullable): optional #GCancellable object,
4537 * @error: a #GError, or %NULL
4539 * Tries to set all attributes in the #GFileInfo on the target
4540 * values, not stopping on the first error.
4542 * If there is any error during this operation then @error will
4543 * be set to the first error. Error on particular fields are flagged
4544 * by setting the "status" field in the attribute value to
4545 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4546 * also detect further errors.
4548 * If @cancellable is not %NULL, then the operation can be cancelled by
4549 * triggering the cancellable object from another thread. If the operation
4550 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4552 * Returns: %FALSE if there was any error, %TRUE otherwise.
4555 g_file_set_attributes_from_info (GFile *file,
4557 GFileQueryInfoFlags flags,
4558 GCancellable *cancellable,
4563 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4564 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4569 g_file_info_clear_status (info);
4571 iface = G_FILE_GET_IFACE (file);
4573 return (* iface->set_attributes_from_info) (file,
4581 g_file_real_set_attributes_from_info (GFile *file,
4583 GFileQueryInfoFlags flags,
4584 GCancellable *cancellable,
4590 GFileAttributeValue *value;
4594 attributes = g_file_info_list_attributes (info, NULL);
4596 for (i = 0; attributes[i] != NULL; i++)
4598 value = _g_file_info_get_attribute_value (info, attributes[i]);
4600 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4603 if (!g_file_set_attribute (file, attributes[i],
4604 value->type, _g_file_attribute_value_peek_as_pointer (value),
4605 flags, cancellable, error))
4607 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4609 /* Don't set error multiple times */
4613 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4616 g_strfreev (attributes);
4622 * g_file_set_attributes_async:
4623 * @file: input #GFile
4624 * @info: a #GFileInfo
4625 * @flags: a #GFileQueryInfoFlags
4626 * @io_priority: the [I/O priority][io-priority] of the request
4627 * @cancellable: (nullable): optional #GCancellable object,
4629 * @callback: (scope async): a #GAsyncReadyCallback
4630 * @user_data: (closure): a #gpointer
4632 * Asynchronously sets the attributes of @file with @info.
4634 * For more details, see g_file_set_attributes_from_info(),
4635 * which is the synchronous version of this call.
4637 * When the operation is finished, @callback will be called.
4638 * You can then call g_file_set_attributes_finish() to get
4639 * the result of the operation.
4642 g_file_set_attributes_async (GFile *file,
4644 GFileQueryInfoFlags flags,
4646 GCancellable *cancellable,
4647 GAsyncReadyCallback callback,
4652 g_return_if_fail (G_IS_FILE (file));
4653 g_return_if_fail (G_IS_FILE_INFO (info));
4655 iface = G_FILE_GET_IFACE (file);
4656 (* iface->set_attributes_async) (file,
4666 * g_file_set_attributes_finish:
4667 * @file: input #GFile
4668 * @result: a #GAsyncResult
4669 * @info: (out) (transfer full): a #GFileInfo
4670 * @error: a #GError, or %NULL
4672 * Finishes setting an attribute started in g_file_set_attributes_async().
4674 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4677 g_file_set_attributes_finish (GFile *file,
4678 GAsyncResult *result,
4684 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4685 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4687 /* No standard handling of errors here, as we must set info even
4690 iface = G_FILE_GET_IFACE (file);
4691 return (* iface->set_attributes_finish) (file, result, info, error);
4695 * g_file_set_attribute_string:
4696 * @file: input #GFile
4697 * @attribute: a string containing the attribute's name
4698 * @value: a string containing the attribute's value
4699 * @flags: #GFileQueryInfoFlags
4700 * @cancellable: (nullable): optional #GCancellable object,
4702 * @error: a #GError, or %NULL
4704 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4705 * If @attribute is of a different type, this operation will fail.
4707 * If @cancellable is not %NULL, then the operation can be cancelled by
4708 * triggering the cancellable object from another thread. If the operation
4709 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4711 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4714 g_file_set_attribute_string (GFile *file,
4715 const char *attribute,
4717 GFileQueryInfoFlags flags,
4718 GCancellable *cancellable,
4721 return g_file_set_attribute (file, attribute,
4722 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4723 flags, cancellable, error);
4727 * g_file_set_attribute_byte_string:
4728 * @file: input #GFile
4729 * @attribute: a string containing the attribute's name
4730 * @value: a string containing the attribute's new value
4731 * @flags: a #GFileQueryInfoFlags
4732 * @cancellable: (nullable): optional #GCancellable object,
4734 * @error: a #GError, or %NULL
4736 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4737 * If @attribute is of a different type, this operation will fail,
4740 * If @cancellable is not %NULL, then the operation can be cancelled by
4741 * triggering the cancellable object from another thread. If the operation
4742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4744 * Returns: %TRUE if the @attribute was successfully set to @value
4745 * in the @file, %FALSE otherwise.
4748 g_file_set_attribute_byte_string (GFile *file,
4749 const gchar *attribute,
4751 GFileQueryInfoFlags flags,
4752 GCancellable *cancellable,
4755 return g_file_set_attribute (file, attribute,
4756 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4757 flags, cancellable, error);
4761 * g_file_set_attribute_uint32:
4762 * @file: input #GFile
4763 * @attribute: a string containing the attribute's name
4764 * @value: a #guint32 containing the attribute's new value
4765 * @flags: a #GFileQueryInfoFlags
4766 * @cancellable: (nullable): optional #GCancellable object,
4768 * @error: a #GError, or %NULL
4770 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4771 * If @attribute is of a different type, this operation will fail.
4773 * If @cancellable is not %NULL, then the operation can be cancelled by
4774 * triggering the cancellable object from another thread. If the operation
4775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4777 * Returns: %TRUE if the @attribute was successfully set to @value
4778 * in the @file, %FALSE otherwise.
4781 g_file_set_attribute_uint32 (GFile *file,
4782 const gchar *attribute,
4784 GFileQueryInfoFlags flags,
4785 GCancellable *cancellable,
4788 return g_file_set_attribute (file, attribute,
4789 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4790 flags, cancellable, error);
4794 * g_file_set_attribute_int32:
4795 * @file: input #GFile
4796 * @attribute: a string containing the attribute's name
4797 * @value: a #gint32 containing the attribute's new value
4798 * @flags: a #GFileQueryInfoFlags
4799 * @cancellable: (nullable): optional #GCancellable object,
4801 * @error: a #GError, or %NULL
4803 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4804 * If @attribute is of a different type, this operation will fail.
4806 * If @cancellable is not %NULL, then the operation can be cancelled by
4807 * triggering the cancellable object from another thread. If the operation
4808 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4810 * Returns: %TRUE if the @attribute was successfully set to @value
4811 * in the @file, %FALSE otherwise.
4814 g_file_set_attribute_int32 (GFile *file,
4815 const gchar *attribute,
4817 GFileQueryInfoFlags flags,
4818 GCancellable *cancellable,
4821 return g_file_set_attribute (file, attribute,
4822 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4823 flags, cancellable, error);
4827 * g_file_set_attribute_uint64:
4828 * @file: input #GFile
4829 * @attribute: a string containing the attribute's name
4830 * @value: a #guint64 containing the attribute's new value
4831 * @flags: a #GFileQueryInfoFlags
4832 * @cancellable: (nullable): optional #GCancellable object,
4834 * @error: a #GError, or %NULL
4836 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4837 * If @attribute is of a different type, this operation will fail.
4839 * If @cancellable is not %NULL, then the operation can be cancelled by
4840 * triggering the cancellable object from another thread. If the operation
4841 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4843 * Returns: %TRUE if the @attribute was successfully set to @value
4844 * in the @file, %FALSE otherwise.
4847 g_file_set_attribute_uint64 (GFile *file,
4848 const gchar *attribute,
4850 GFileQueryInfoFlags flags,
4851 GCancellable *cancellable,
4854 return g_file_set_attribute (file, attribute,
4855 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4856 flags, cancellable, error);
4860 * g_file_set_attribute_int64:
4861 * @file: input #GFile
4862 * @attribute: a string containing the attribute's name
4863 * @value: a #guint64 containing the attribute's new value
4864 * @flags: a #GFileQueryInfoFlags
4865 * @cancellable: (nullable): optional #GCancellable object,
4867 * @error: a #GError, or %NULL
4869 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4870 * If @attribute is of a different type, this operation will fail.
4872 * If @cancellable is not %NULL, then the operation can be cancelled by
4873 * triggering the cancellable object from another thread. If the operation
4874 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4876 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4879 g_file_set_attribute_int64 (GFile *file,
4880 const gchar *attribute,
4882 GFileQueryInfoFlags flags,
4883 GCancellable *cancellable,
4886 return g_file_set_attribute (file, attribute,
4887 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4888 flags, cancellable, error);
4892 * g_file_mount_mountable:
4893 * @file: input #GFile
4894 * @flags: flags affecting the operation
4895 * @mount_operation: (nullable): a #GMountOperation,
4896 * or %NULL to avoid user interaction
4897 * @cancellable: (nullable): optional #GCancellable object,
4899 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4900 * when the request is satisfied, or %NULL
4901 * @user_data: (closure): the data to pass to callback function
4903 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4904 * Using @mount_operation, you can request callbacks when, for instance,
4905 * passwords are needed during authentication.
4907 * If @cancellable is not %NULL, then the operation can be cancelled by
4908 * triggering the cancellable object from another thread. If the operation
4909 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4911 * When the operation is finished, @callback will be called.
4912 * You can then call g_file_mount_mountable_finish() to get
4913 * the result of the operation.
4916 g_file_mount_mountable (GFile *file,
4917 GMountMountFlags flags,
4918 GMountOperation *mount_operation,
4919 GCancellable *cancellable,
4920 GAsyncReadyCallback callback,
4925 g_return_if_fail (G_IS_FILE (file));
4927 iface = G_FILE_GET_IFACE (file);
4929 if (iface->mount_mountable == NULL)
4931 g_task_report_new_error (file, callback, user_data,
4932 g_file_mount_mountable,
4933 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4934 _("Operation not supported"));
4938 (* iface->mount_mountable) (file,
4947 * g_file_mount_mountable_finish:
4948 * @file: input #GFile
4949 * @result: a #GAsyncResult
4950 * @error: a #GError, or %NULL
4952 * Finishes a mount operation. See g_file_mount_mountable() for details.
4954 * Finish an asynchronous mount operation that was started
4955 * with g_file_mount_mountable().
4957 * Returns: (transfer full): a #GFile or %NULL on error.
4958 * Free the returned object with g_object_unref().
4961 g_file_mount_mountable_finish (GFile *file,
4962 GAsyncResult *result,
4967 g_return_val_if_fail (G_IS_FILE (file), NULL);
4968 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4970 if (g_async_result_legacy_propagate_error (result, error))
4972 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4973 return g_task_propagate_pointer (G_TASK (result), error);
4975 iface = G_FILE_GET_IFACE (file);
4976 return (* iface->mount_mountable_finish) (file, result, error);
4980 * g_file_unmount_mountable:
4981 * @file: input #GFile
4982 * @flags: flags affecting the operation
4983 * @cancellable: (nullable): optional #GCancellable object,
4985 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4986 * when the request is satisfied, or %NULL
4987 * @user_data: (closure): the data to pass to callback function
4989 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4991 * If @cancellable is not %NULL, then the operation can be cancelled by
4992 * triggering the cancellable object from another thread. If the operation
4993 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4995 * When the operation is finished, @callback will be called.
4996 * You can then call g_file_unmount_mountable_finish() to get
4997 * the result of the operation.
4999 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5002 g_file_unmount_mountable (GFile *file,
5003 GMountUnmountFlags flags,
5004 GCancellable *cancellable,
5005 GAsyncReadyCallback callback,
5010 g_return_if_fail (G_IS_FILE (file));
5012 iface = G_FILE_GET_IFACE (file);
5014 if (iface->unmount_mountable == NULL)
5016 g_task_report_new_error (file, callback, user_data,
5017 g_file_unmount_mountable_with_operation,
5018 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5019 _("Operation not supported"));
5023 (* iface->unmount_mountable) (file,
5031 * g_file_unmount_mountable_finish:
5032 * @file: input #GFile
5033 * @result: a #GAsyncResult
5034 * @error: a #GError, or %NULL
5036 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5038 * Finish an asynchronous unmount operation that was started
5039 * with g_file_unmount_mountable().
5041 * Returns: %TRUE if the operation finished successfully.
5044 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5048 g_file_unmount_mountable_finish (GFile *file,
5049 GAsyncResult *result,
5054 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5055 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5057 if (g_async_result_legacy_propagate_error (result, error))
5059 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5060 return g_task_propagate_boolean (G_TASK (result), error);
5062 iface = G_FILE_GET_IFACE (file);
5063 return (* iface->unmount_mountable_finish) (file, result, error);
5067 * g_file_unmount_mountable_with_operation:
5068 * @file: input #GFile
5069 * @flags: flags affecting the operation
5070 * @mount_operation: (nullable): a #GMountOperation,
5071 * or %NULL to avoid user interaction
5072 * @cancellable: (nullable): optional #GCancellable object,
5074 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5075 * when the request is satisfied, or %NULL
5076 * @user_data: (closure): the data to pass to callback function
5078 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5080 * If @cancellable is not %NULL, then the operation can be cancelled by
5081 * triggering the cancellable object from another thread. If the operation
5082 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5084 * When the operation is finished, @callback will be called.
5085 * You can then call g_file_unmount_mountable_finish() to get
5086 * the result of the operation.
5091 g_file_unmount_mountable_with_operation (GFile *file,
5092 GMountUnmountFlags flags,
5093 GMountOperation *mount_operation,
5094 GCancellable *cancellable,
5095 GAsyncReadyCallback callback,
5100 g_return_if_fail (G_IS_FILE (file));
5102 iface = G_FILE_GET_IFACE (file);
5104 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5106 g_task_report_new_error (file, callback, user_data,
5107 g_file_unmount_mountable_with_operation,
5108 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5109 _("Operation not supported"));
5113 if (iface->unmount_mountable_with_operation != NULL)
5114 (* iface->unmount_mountable_with_operation) (file,
5121 (* iface->unmount_mountable) (file,
5129 * g_file_unmount_mountable_with_operation_finish:
5130 * @file: input #GFile
5131 * @result: a #GAsyncResult
5132 * @error: a #GError, or %NULL
5134 * Finishes an unmount operation,
5135 * see g_file_unmount_mountable_with_operation() for details.
5137 * Finish an asynchronous unmount operation that was started
5138 * with g_file_unmount_mountable_with_operation().
5140 * Returns: %TRUE if the operation finished successfully.
5146 g_file_unmount_mountable_with_operation_finish (GFile *file,
5147 GAsyncResult *result,
5152 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5153 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5155 if (g_async_result_legacy_propagate_error (result, error))
5157 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5158 return g_task_propagate_boolean (G_TASK (result), error);
5160 iface = G_FILE_GET_IFACE (file);
5161 if (iface->unmount_mountable_with_operation_finish != NULL)
5162 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5164 return (* iface->unmount_mountable_finish) (file, result, error);
5168 * g_file_eject_mountable:
5169 * @file: input #GFile
5170 * @flags: flags affecting the operation
5171 * @cancellable: (nullable): optional #GCancellable object,
5173 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5174 * when the request is satisfied, or %NULL
5175 * @user_data: (closure): the data to pass to callback function
5177 * Starts an asynchronous eject on a mountable.
5178 * When this operation has completed, @callback will be called with
5179 * @user_user data, and the operation can be finalized with
5180 * g_file_eject_mountable_finish().
5182 * If @cancellable is not %NULL, then the operation can be cancelled by
5183 * triggering the cancellable object from another thread. If the operation
5184 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5186 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5189 g_file_eject_mountable (GFile *file,
5190 GMountUnmountFlags flags,
5191 GCancellable *cancellable,
5192 GAsyncReadyCallback callback,
5197 g_return_if_fail (G_IS_FILE (file));
5199 iface = G_FILE_GET_IFACE (file);
5201 if (iface->eject_mountable == NULL)
5203 g_task_report_new_error (file, callback, user_data,
5204 g_file_eject_mountable_with_operation,
5205 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5206 _("Operation not supported"));
5210 (* iface->eject_mountable) (file,
5218 * g_file_eject_mountable_finish:
5219 * @file: input #GFile
5220 * @result: a #GAsyncResult
5221 * @error: a #GError, or %NULL
5223 * Finishes an asynchronous eject operation started by
5224 * g_file_eject_mountable().
5226 * Returns: %TRUE if the @file was ejected successfully.
5229 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5233 g_file_eject_mountable_finish (GFile *file,
5234 GAsyncResult *result,
5239 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5240 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5242 if (g_async_result_legacy_propagate_error (result, error))
5244 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5245 return g_task_propagate_boolean (G_TASK (result), error);
5247 iface = G_FILE_GET_IFACE (file);
5248 return (* iface->eject_mountable_finish) (file, result, error);
5252 * g_file_eject_mountable_with_operation:
5253 * @file: input #GFile
5254 * @flags: flags affecting the operation
5255 * @mount_operation: (nullable): a #GMountOperation,
5256 * or %NULL to avoid user interaction
5257 * @cancellable: (nullable): optional #GCancellable object,
5259 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5260 * when the request is satisfied, or %NULL
5261 * @user_data: (closure): the data to pass to callback function
5263 * Starts an asynchronous eject on a mountable.
5264 * When this operation has completed, @callback will be called with
5265 * @user_user data, and the operation can be finalized with
5266 * g_file_eject_mountable_with_operation_finish().
5268 * If @cancellable is not %NULL, then the operation can be cancelled by
5269 * triggering the cancellable object from another thread. If the operation
5270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5275 g_file_eject_mountable_with_operation (GFile *file,
5276 GMountUnmountFlags flags,
5277 GMountOperation *mount_operation,
5278 GCancellable *cancellable,
5279 GAsyncReadyCallback callback,
5284 g_return_if_fail (G_IS_FILE (file));
5286 iface = G_FILE_GET_IFACE (file);
5288 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5290 g_task_report_new_error (file, callback, user_data,
5291 g_file_eject_mountable_with_operation,
5292 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5293 _("Operation not supported"));
5297 if (iface->eject_mountable_with_operation != NULL)
5298 (* iface->eject_mountable_with_operation) (file,
5305 (* iface->eject_mountable) (file,
5313 * g_file_eject_mountable_with_operation_finish:
5314 * @file: input #GFile
5315 * @result: a #GAsyncResult
5316 * @error: a #GError, or %NULL
5318 * Finishes an asynchronous eject operation started by
5319 * g_file_eject_mountable_with_operation().
5321 * Returns: %TRUE if the @file was ejected successfully.
5327 g_file_eject_mountable_with_operation_finish (GFile *file,
5328 GAsyncResult *result,
5333 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5334 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5336 if (g_async_result_legacy_propagate_error (result, error))
5338 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5339 return g_task_propagate_boolean (G_TASK (result), error);
5341 iface = G_FILE_GET_IFACE (file);
5342 if (iface->eject_mountable_with_operation_finish != NULL)
5343 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5345 return (* iface->eject_mountable_finish) (file, result, error);
5349 * g_file_monitor_directory:
5350 * @file: input #GFile
5351 * @flags: a set of #GFileMonitorFlags
5352 * @cancellable: (nullable): optional #GCancellable object,
5354 * @error: a #GError, or %NULL
5356 * Obtains a directory monitor for the given file.
5357 * This may fail if directory monitoring is not supported.
5359 * If @cancellable is not %NULL, then the operation can be cancelled by
5360 * triggering the cancellable object from another thread. If the operation
5361 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5363 * It does not make sense for @flags to contain
5364 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5365 * directories. It is not possible to monitor all the files in a
5366 * directory for changes made via hard links; if you want to do this then
5367 * you must register individual watches with g_file_monitor().
5369 * Virtual: monitor_dir
5370 * Returns: (transfer full): a #GFileMonitor for the given @file,
5371 * or %NULL on error.
5372 * Free the returned object with g_object_unref().
5375 g_file_monitor_directory (GFile *file,
5376 GFileMonitorFlags flags,
5377 GCancellable *cancellable,
5382 g_return_val_if_fail (G_IS_FILE (file), NULL);
5383 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5385 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5388 iface = G_FILE_GET_IFACE (file);
5390 if (iface->monitor_dir == NULL)
5392 g_set_error_literal (error, G_IO_ERROR,
5393 G_IO_ERROR_NOT_SUPPORTED,
5394 _("Operation not supported"));
5398 return (* iface->monitor_dir) (file, flags, cancellable, error);
5402 * g_file_monitor_file:
5403 * @file: input #GFile
5404 * @flags: a set of #GFileMonitorFlags
5405 * @cancellable: (nullable): optional #GCancellable object,
5407 * @error: a #GError, or %NULL
5409 * Obtains a file monitor for the given file. If no file notification
5410 * mechanism exists, then regular polling of the file is used.
5412 * If @cancellable is not %NULL, then the operation can be cancelled by
5413 * triggering the cancellable object from another thread. If the operation
5414 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5416 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5417 * will also attempt to report changes made to the file via another
5418 * filename (ie, a hard link). Without this flag, you can only rely on
5419 * changes made through the filename contained in @file to be
5420 * reported. Using this flag may result in an increase in resource
5421 * usage, and may not have any effect depending on the #GFileMonitor
5422 * backend and/or filesystem type.
5424 * Returns: (transfer full): a #GFileMonitor for the given @file,
5425 * or %NULL on error.
5426 * Free the returned object with g_object_unref().
5429 g_file_monitor_file (GFile *file,
5430 GFileMonitorFlags flags,
5431 GCancellable *cancellable,
5435 GFileMonitor *monitor;
5437 g_return_val_if_fail (G_IS_FILE (file), NULL);
5439 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5442 iface = G_FILE_GET_IFACE (file);
5446 if (iface->monitor_file)
5447 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5449 /* Fallback to polling */
5450 if (monitor == NULL)
5451 monitor = _g_poll_file_monitor_new (file);
5458 * @file: input #GFile
5459 * @flags: a set of #GFileMonitorFlags
5460 * @cancellable: (nullable): optional #GCancellable object,
5462 * @error: a #GError, or %NULL
5464 * Obtains a file or directory monitor for the given file,
5465 * depending on the type of the file.
5467 * If @cancellable is not %NULL, then the operation can be cancelled by
5468 * triggering the cancellable object from another thread. If the operation
5469 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5471 * Returns: (transfer full): a #GFileMonitor for the given @file,
5472 * or %NULL on error.
5473 * Free the returned object with g_object_unref().
5478 g_file_monitor (GFile *file,
5479 GFileMonitorFlags flags,
5480 GCancellable *cancellable,
5483 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5484 return g_file_monitor_directory (file,
5485 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5486 cancellable, error);
5488 return g_file_monitor_file (file, flags, cancellable, error);
5491 /********************************************
5492 * Default implementation of async ops *
5493 ********************************************/
5497 GFileQueryInfoFlags flags;
5498 } QueryInfoAsyncData;
5501 query_info_data_free (QueryInfoAsyncData *data)
5503 g_free (data->attributes);
5508 query_info_async_thread (GTask *task,
5511 GCancellable *cancellable)
5513 QueryInfoAsyncData *data = task_data;
5515 GError *error = NULL;
5517 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5519 g_task_return_pointer (task, info, g_object_unref);
5521 g_task_return_error (task, error);
5525 g_file_real_query_info_async (GFile *file,
5526 const char *attributes,
5527 GFileQueryInfoFlags flags,
5529 GCancellable *cancellable,
5530 GAsyncReadyCallback callback,
5534 QueryInfoAsyncData *data;
5536 data = g_new0 (QueryInfoAsyncData, 1);
5537 data->attributes = g_strdup (attributes);
5538 data->flags = flags;
5540 task = g_task_new (file, cancellable, callback, user_data);
5541 g_task_set_source_tag (task, g_file_real_query_info_async);
5542 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5543 g_task_set_priority (task, io_priority);
5544 g_task_run_in_thread (task, query_info_async_thread);
5545 g_object_unref (task);
5549 g_file_real_query_info_finish (GFile *file,
5553 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5555 return g_task_propagate_pointer (G_TASK (res), error);
5559 query_filesystem_info_async_thread (GTask *task,
5562 GCancellable *cancellable)
5564 const char *attributes = task_data;
5566 GError *error = NULL;
5568 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5570 g_task_return_pointer (task, info, g_object_unref);
5572 g_task_return_error (task, error);
5576 g_file_real_query_filesystem_info_async (GFile *file,
5577 const char *attributes,
5579 GCancellable *cancellable,
5580 GAsyncReadyCallback callback,
5585 task = g_task_new (file, cancellable, callback, user_data);
5586 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5587 g_task_set_task_data (task, g_strdup (attributes), g_free);
5588 g_task_set_priority (task, io_priority);
5589 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5590 g_object_unref (task);
5594 g_file_real_query_filesystem_info_finish (GFile *file,
5598 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5600 return g_task_propagate_pointer (G_TASK (res), error);
5604 enumerate_children_async_thread (GTask *task,
5607 GCancellable *cancellable)
5609 QueryInfoAsyncData *data = task_data;
5610 GFileEnumerator *enumerator;
5611 GError *error = NULL;
5613 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5615 g_task_return_error (task, error);
5617 g_task_return_pointer (task, enumerator, g_object_unref);
5621 g_file_real_enumerate_children_async (GFile *file,
5622 const char *attributes,
5623 GFileQueryInfoFlags flags,
5625 GCancellable *cancellable,
5626 GAsyncReadyCallback callback,
5630 QueryInfoAsyncData *data;
5632 data = g_new0 (QueryInfoAsyncData, 1);
5633 data->attributes = g_strdup (attributes);
5634 data->flags = flags;
5636 task = g_task_new (file, cancellable, callback, user_data);
5637 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5638 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5639 g_task_set_priority (task, io_priority);
5640 g_task_run_in_thread (task, enumerate_children_async_thread);
5641 g_object_unref (task);
5644 static GFileEnumerator *
5645 g_file_real_enumerate_children_finish (GFile *file,
5649 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5651 return g_task_propagate_pointer (G_TASK (res), error);
5655 open_read_async_thread (GTask *task,
5658 GCancellable *cancellable)
5660 GFileInputStream *stream;
5661 GError *error = NULL;
5663 stream = g_file_read (G_FILE (object), cancellable, &error);
5665 g_task_return_pointer (task, stream, g_object_unref);
5667 g_task_return_error (task, error);
5671 g_file_real_read_async (GFile *file,
5673 GCancellable *cancellable,
5674 GAsyncReadyCallback callback,
5679 task = g_task_new (file, cancellable, callback, user_data);
5680 g_task_set_source_tag (task, g_file_real_read_async);
5681 g_task_set_priority (task, io_priority);
5682 g_task_run_in_thread (task, open_read_async_thread);
5683 g_object_unref (task);
5686 static GFileInputStream *
5687 g_file_real_read_finish (GFile *file,
5691 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5693 return g_task_propagate_pointer (G_TASK (res), error);
5697 append_to_async_thread (GTask *task,
5698 gpointer source_object,
5700 GCancellable *cancellable)
5702 GFileCreateFlags *data = task_data;
5703 GFileOutputStream *stream;
5704 GError *error = NULL;
5706 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5708 g_task_return_pointer (task, stream, g_object_unref);
5710 g_task_return_error (task, error);
5714 g_file_real_append_to_async (GFile *file,
5715 GFileCreateFlags flags,
5717 GCancellable *cancellable,
5718 GAsyncReadyCallback callback,
5721 GFileCreateFlags *data;
5724 data = g_new0 (GFileCreateFlags, 1);
5727 task = g_task_new (file, cancellable, callback, user_data);
5728 g_task_set_source_tag (task, g_file_real_append_to_async);
5729 g_task_set_task_data (task, data, g_free);
5730 g_task_set_priority (task, io_priority);
5732 g_task_run_in_thread (task, append_to_async_thread);
5733 g_object_unref (task);
5736 static GFileOutputStream *
5737 g_file_real_append_to_finish (GFile *file,
5741 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5743 return g_task_propagate_pointer (G_TASK (res), error);
5747 create_async_thread (GTask *task,
5748 gpointer source_object,
5750 GCancellable *cancellable)
5752 GFileCreateFlags *data = task_data;
5753 GFileOutputStream *stream;
5754 GError *error = NULL;
5756 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5758 g_task_return_pointer (task, stream, g_object_unref);
5760 g_task_return_error (task, error);
5764 g_file_real_create_async (GFile *file,
5765 GFileCreateFlags flags,
5767 GCancellable *cancellable,
5768 GAsyncReadyCallback callback,
5771 GFileCreateFlags *data;
5774 data = g_new0 (GFileCreateFlags, 1);
5777 task = g_task_new (file, cancellable, callback, user_data);
5778 g_task_set_source_tag (task, g_file_real_create_async);
5779 g_task_set_task_data (task, data, g_free);
5780 g_task_set_priority (task, io_priority);
5782 g_task_run_in_thread (task, create_async_thread);
5783 g_object_unref (task);
5786 static GFileOutputStream *
5787 g_file_real_create_finish (GFile *file,
5791 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5793 return g_task_propagate_pointer (G_TASK (res), error);
5797 GFileOutputStream *stream;
5799 gboolean make_backup;
5800 GFileCreateFlags flags;
5804 replace_async_data_free (ReplaceAsyncData *data)
5807 g_object_unref (data->stream);
5808 g_free (data->etag);
5813 replace_async_thread (GTask *task,
5814 gpointer source_object,
5816 GCancellable *cancellable)
5818 GFileOutputStream *stream;
5819 ReplaceAsyncData *data = task_data;
5820 GError *error = NULL;
5822 stream = g_file_replace (G_FILE (source_object),
5830 g_task_return_pointer (task, stream, g_object_unref);
5832 g_task_return_error (task, error);
5836 g_file_real_replace_async (GFile *file,
5838 gboolean make_backup,
5839 GFileCreateFlags flags,
5841 GCancellable *cancellable,
5842 GAsyncReadyCallback callback,
5846 ReplaceAsyncData *data;
5848 data = g_new0 (ReplaceAsyncData, 1);
5849 data->etag = g_strdup (etag);
5850 data->make_backup = make_backup;
5851 data->flags = flags;
5853 task = g_task_new (file, cancellable, callback, user_data);
5854 g_task_set_source_tag (task, g_file_real_replace_async);
5855 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5856 g_task_set_priority (task, io_priority);
5858 g_task_run_in_thread (task, replace_async_thread);
5859 g_object_unref (task);
5862 static GFileOutputStream *
5863 g_file_real_replace_finish (GFile *file,
5867 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5869 return g_task_propagate_pointer (G_TASK (res), error);
5873 delete_async_thread (GTask *task,
5876 GCancellable *cancellable)
5878 GError *error = NULL;
5880 if (g_file_delete (G_FILE (object), cancellable, &error))
5881 g_task_return_boolean (task, TRUE);
5883 g_task_return_error (task, error);
5887 g_file_real_delete_async (GFile *file,
5889 GCancellable *cancellable,
5890 GAsyncReadyCallback callback,
5895 task = g_task_new (file, cancellable, callback, user_data);
5896 g_task_set_source_tag (task, g_file_real_delete_async);
5897 g_task_set_priority (task, io_priority);
5898 g_task_run_in_thread (task, delete_async_thread);
5899 g_object_unref (task);
5903 g_file_real_delete_finish (GFile *file,
5907 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5909 return g_task_propagate_boolean (G_TASK (res), error);
5913 trash_async_thread (GTask *task,
5916 GCancellable *cancellable)
5918 GError *error = NULL;
5920 if (g_file_trash (G_FILE (object), cancellable, &error))
5921 g_task_return_boolean (task, TRUE);
5923 g_task_return_error (task, error);
5927 g_file_real_trash_async (GFile *file,
5929 GCancellable *cancellable,
5930 GAsyncReadyCallback callback,
5935 task = g_task_new (file, cancellable, callback, user_data);
5936 g_task_set_source_tag (task, g_file_real_trash_async);
5937 g_task_set_priority (task, io_priority);
5938 g_task_run_in_thread (task, trash_async_thread);
5939 g_object_unref (task);
5943 g_file_real_trash_finish (GFile *file,
5947 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5949 return g_task_propagate_boolean (G_TASK (res), error);
5953 make_directory_async_thread (GTask *task,
5956 GCancellable *cancellable)
5958 GError *error = NULL;
5960 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5961 g_task_return_boolean (task, TRUE);
5963 g_task_return_error (task, error);
5967 g_file_real_make_directory_async (GFile *file,
5969 GCancellable *cancellable,
5970 GAsyncReadyCallback callback,
5975 task = g_task_new (file, cancellable, callback, user_data);
5976 g_task_set_source_tag (task, g_file_real_make_directory_async);
5977 g_task_set_priority (task, io_priority);
5978 g_task_run_in_thread (task, make_directory_async_thread);
5979 g_object_unref (task);
5983 g_file_real_make_directory_finish (GFile *file,
5987 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5989 return g_task_propagate_boolean (G_TASK (res), error);
5993 open_readwrite_async_thread (GTask *task,
5996 GCancellable *cancellable)
5998 GFileIOStream *stream;
5999 GError *error = NULL;
6001 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6004 g_task_return_error (task, error);
6006 g_task_return_pointer (task, stream, g_object_unref);
6010 g_file_real_open_readwrite_async (GFile *file,
6012 GCancellable *cancellable,
6013 GAsyncReadyCallback callback,
6018 task = g_task_new (file, cancellable, callback, user_data);
6019 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6020 g_task_set_priority (task, io_priority);
6022 g_task_run_in_thread (task, open_readwrite_async_thread);
6023 g_object_unref (task);
6026 static GFileIOStream *
6027 g_file_real_open_readwrite_finish (GFile *file,
6031 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6033 return g_task_propagate_pointer (G_TASK (res), error);
6037 create_readwrite_async_thread (GTask *task,
6040 GCancellable *cancellable)
6042 GFileCreateFlags *data = task_data;
6043 GFileIOStream *stream;
6044 GError *error = NULL;
6046 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6049 g_task_return_error (task, error);
6051 g_task_return_pointer (task, stream, g_object_unref);
6055 g_file_real_create_readwrite_async (GFile *file,
6056 GFileCreateFlags flags,
6058 GCancellable *cancellable,
6059 GAsyncReadyCallback callback,
6062 GFileCreateFlags *data;
6065 data = g_new0 (GFileCreateFlags, 1);
6068 task = g_task_new (file, cancellable, callback, user_data);
6069 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6070 g_task_set_task_data (task, data, g_free);
6071 g_task_set_priority (task, io_priority);
6073 g_task_run_in_thread (task, create_readwrite_async_thread);
6074 g_object_unref (task);
6077 static GFileIOStream *
6078 g_file_real_create_readwrite_finish (GFile *file,
6082 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6084 return g_task_propagate_pointer (G_TASK (res), error);
6089 gboolean make_backup;
6090 GFileCreateFlags flags;
6091 } ReplaceRWAsyncData;
6094 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6096 g_free (data->etag);
6101 replace_readwrite_async_thread (GTask *task,
6104 GCancellable *cancellable)
6106 GFileIOStream *stream;
6107 GError *error = NULL;
6108 ReplaceRWAsyncData *data = task_data;
6110 stream = g_file_replace_readwrite (G_FILE (object),
6118 g_task_return_error (task, error);
6120 g_task_return_pointer (task, stream, g_object_unref);
6124 g_file_real_replace_readwrite_async (GFile *file,
6126 gboolean make_backup,
6127 GFileCreateFlags flags,
6129 GCancellable *cancellable,
6130 GAsyncReadyCallback callback,
6134 ReplaceRWAsyncData *data;
6136 data = g_new0 (ReplaceRWAsyncData, 1);
6137 data->etag = g_strdup (etag);
6138 data->make_backup = make_backup;
6139 data->flags = flags;
6141 task = g_task_new (file, cancellable, callback, user_data);
6142 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6143 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6144 g_task_set_priority (task, io_priority);
6146 g_task_run_in_thread (task, replace_readwrite_async_thread);
6147 g_object_unref (task);
6150 static GFileIOStream *
6151 g_file_real_replace_readwrite_finish (GFile *file,
6155 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6157 return g_task_propagate_pointer (G_TASK (res), error);
6161 set_display_name_async_thread (GTask *task,
6164 GCancellable *cancellable)
6166 GError *error = NULL;
6167 char *name = task_data;
6170 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6173 g_task_return_error (task, error);
6175 g_task_return_pointer (task, file, g_object_unref);
6179 g_file_real_set_display_name_async (GFile *file,
6180 const char *display_name,
6182 GCancellable *cancellable,
6183 GAsyncReadyCallback callback,
6188 task = g_task_new (file, cancellable, callback, user_data);
6189 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6190 g_task_set_task_data (task, g_strdup (display_name), g_free);
6191 g_task_set_priority (task, io_priority);
6193 g_task_run_in_thread (task, set_display_name_async_thread);
6194 g_object_unref (task);
6198 g_file_real_set_display_name_finish (GFile *file,
6202 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6204 return g_task_propagate_pointer (G_TASK (res), error);
6208 GFileQueryInfoFlags flags;
6215 set_info_data_free (SetInfoAsyncData *data)
6218 g_object_unref (data->info);
6220 g_error_free (data->error);
6225 set_info_async_thread (GTask *task,
6228 GCancellable *cancellable)
6230 SetInfoAsyncData *data = task_data;
6233 data->res = g_file_set_attributes_from_info (G_FILE (object),
6241 g_file_real_set_attributes_async (GFile *file,
6243 GFileQueryInfoFlags flags,
6245 GCancellable *cancellable,
6246 GAsyncReadyCallback callback,
6250 SetInfoAsyncData *data;
6252 data = g_new0 (SetInfoAsyncData, 1);
6253 data->info = g_file_info_dup (info);
6254 data->flags = flags;
6256 task = g_task_new (file, cancellable, callback, user_data);
6257 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6258 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6259 g_task_set_priority (task, io_priority);
6261 g_task_run_in_thread (task, set_info_async_thread);
6262 g_object_unref (task);
6266 g_file_real_set_attributes_finish (GFile *file,
6271 SetInfoAsyncData *data;
6273 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6275 data = g_task_get_task_data (G_TASK (res));
6278 *info = g_object_ref (data->info);
6280 if (error != NULL && data->error)
6281 *error = g_error_copy (data->error);
6287 find_enclosing_mount_async_thread (GTask *task,
6290 GCancellable *cancellable)
6292 GError *error = NULL;
6295 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6298 g_task_return_error (task, error);
6300 g_task_return_pointer (task, mount, g_object_unref);
6304 g_file_real_find_enclosing_mount_async (GFile *file,
6306 GCancellable *cancellable,
6307 GAsyncReadyCallback callback,
6312 task = g_task_new (file, cancellable, callback, user_data);
6313 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6314 g_task_set_priority (task, io_priority);
6316 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6317 g_object_unref (task);
6321 g_file_real_find_enclosing_mount_finish (GFile *file,
6325 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6327 return g_task_propagate_pointer (G_TASK (res), error);
6334 GFileCopyFlags flags;
6335 GFileProgressCallback progress_cb;
6336 gpointer progress_cb_data;
6340 copy_async_data_free (CopyAsyncData *data)
6342 g_object_unref (data->source);
6343 g_object_unref (data->destination);
6344 g_slice_free (CopyAsyncData, data);
6348 CopyAsyncData *data;
6349 goffset current_num_bytes;
6350 goffset total_num_bytes;
6354 copy_async_progress_in_main (gpointer user_data)
6356 ProgressData *progress = user_data;
6357 CopyAsyncData *data = progress->data;
6359 data->progress_cb (progress->current_num_bytes,
6360 progress->total_num_bytes,
6361 data->progress_cb_data);
6367 copy_async_progress_callback (goffset current_num_bytes,
6368 goffset total_num_bytes,
6371 GTask *task = user_data;
6372 CopyAsyncData *data = g_task_get_task_data (task);
6373 ProgressData *progress;
6375 progress = g_new (ProgressData, 1);
6376 progress->data = data;
6377 progress->current_num_bytes = current_num_bytes;
6378 progress->total_num_bytes = total_num_bytes;
6380 g_main_context_invoke_full (g_task_get_context (task),
6381 g_task_get_priority (task),
6382 copy_async_progress_in_main,
6388 copy_async_thread (GTask *task,
6391 GCancellable *cancellable)
6393 CopyAsyncData *data = task_data;
6395 GError *error = NULL;
6397 result = g_file_copy (data->source,
6401 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6405 g_task_return_boolean (task, TRUE);
6407 g_task_return_error (task, error);
6411 g_file_real_copy_async (GFile *source,
6413 GFileCopyFlags flags,
6415 GCancellable *cancellable,
6416 GFileProgressCallback progress_callback,
6417 gpointer progress_callback_data,
6418 GAsyncReadyCallback callback,
6422 CopyAsyncData *data;
6424 data = g_slice_new (CopyAsyncData);
6425 data->source = g_object_ref (source);
6426 data->destination = g_object_ref (destination);
6427 data->flags = flags;
6428 data->progress_cb = progress_callback;
6429 data->progress_cb_data = progress_callback_data;
6431 task = g_task_new (source, cancellable, callback, user_data);
6432 g_task_set_source_tag (task, g_file_real_copy_async);
6433 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6434 g_task_set_priority (task, io_priority);
6435 g_task_run_in_thread (task, copy_async_thread);
6436 g_object_unref (task);
6440 g_file_real_copy_finish (GFile *file,
6444 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6446 return g_task_propagate_boolean (G_TASK (res), error);
6450 /********************************************
6451 * Default VFS operations *
6452 ********************************************/
6455 * g_file_new_for_path:
6456 * @path: (type filename): a string containing a relative or absolute path.
6457 * The string must be encoded in the glib filename encoding.
6459 * Constructs a #GFile for a given path. This operation never
6460 * fails, but the returned object might not support any I/O
6461 * operation if @path is malformed.
6463 * Returns: (transfer full): a new #GFile for the given @path.
6464 * Free the returned object with g_object_unref().
6467 g_file_new_for_path (const char *path)
6469 g_return_val_if_fail (path != NULL, NULL);
6471 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6475 * g_file_new_for_uri:
6476 * @uri: a UTF-8 string containing a URI
6478 * Constructs a #GFile for a given URI. This operation never
6479 * fails, but the returned object might not support any I/O
6480 * operation if @uri is malformed or if the uri type is
6483 * Returns: (transfer full): a new #GFile for the given @uri.
6484 * Free the returned object with g_object_unref().
6487 g_file_new_for_uri (const char *uri)
6489 g_return_val_if_fail (uri != NULL, NULL);
6491 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6496 * @tmpl: (type filename) (nullable): Template for the file
6497 * name, as in g_file_open_tmp(), or %NULL for a default template
6498 * @iostream: (out): on return, a #GFileIOStream for the created file
6499 * @error: a #GError, or %NULL
6501 * Opens a file in the preferred directory for temporary files (as
6502 * returned by g_get_tmp_dir()) and returns a #GFile and
6503 * #GFileIOStream pointing to it.
6505 * @tmpl should be a string in the GLib file name encoding
6506 * containing a sequence of six 'X' characters, and containing no
6507 * directory components. If it is %NULL, a default template is used.
6509 * Unlike the other #GFile constructors, this will return %NULL if
6510 * a temporary file could not be created.
6512 * Returns: (transfer full): a new #GFile.
6513 * Free the returned object with g_object_unref().
6518 g_file_new_tmp (const char *tmpl,
6519 GFileIOStream **iostream,
6525 GFileOutputStream *output;
6527 g_return_val_if_fail (iostream != NULL, NULL);
6529 fd = g_file_open_tmp (tmpl, &path, error);
6533 file = g_file_new_for_path (path);
6535 output = _g_local_file_output_stream_new (fd);
6536 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6538 g_object_unref (output);
6545 * g_file_parse_name:
6546 * @parse_name: a file name or path to be parsed
6548 * Constructs a #GFile with the given @parse_name (i.e. something
6549 * given by g_file_get_parse_name()). This operation never fails,
6550 * but the returned object might not support any I/O operation if
6551 * the @parse_name cannot be parsed.
6553 * Returns: (transfer full): a new #GFile.
6556 g_file_parse_name (const char *parse_name)
6558 g_return_val_if_fail (parse_name != NULL, NULL);
6560 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6564 * g_file_new_build_filename:
6565 * @first_element: (type filename): the first element in the path
6566 * @...: remaining elements in path, terminated by %NULL
6568 * Constructs a #GFile from a series of elements using the correct
6569 * separator for filenames.
6571 * Using this function is equivalent to calling g_build_filename(),
6572 * followed by g_file_new_for_path() on the result.
6574 * Returns: (transfer full): a new #GFile
6579 g_file_new_build_filename (const gchar *first_element,
6586 g_return_val_if_fail (first_element != NULL, NULL);
6588 va_start (args, first_element);
6589 str = g_build_filename_valist (first_element, &args);
6592 file = g_file_new_for_path (str);
6599 is_valid_scheme_character (char c)
6601 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6604 /* Following RFC 2396, valid schemes are built like:
6605 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6608 has_valid_scheme (const char *uri)
6614 if (!g_ascii_isalpha (*p))
6619 } while (is_valid_scheme_character (*p));
6625 new_for_cmdline_arg (const gchar *arg,
6631 if (g_path_is_absolute (arg))
6632 return g_file_new_for_path (arg);
6634 if (has_valid_scheme (arg))
6635 return g_file_new_for_uri (arg);
6641 current_dir = g_get_current_dir ();
6642 filename = g_build_filename (current_dir, arg, NULL);
6643 g_free (current_dir);
6646 filename = g_build_filename (cwd, arg, NULL);
6648 file = g_file_new_for_path (filename);
6655 * g_file_new_for_commandline_arg:
6656 * @arg: (type filename): a command line string
6658 * Creates a #GFile with the given argument from the command line.
6659 * The value of @arg can be either a URI, an absolute path or a
6660 * relative path resolved relative to the current working directory.
6661 * This operation never fails, but the returned object might not
6662 * support any I/O operation if @arg points to a malformed path.
6664 * Note that on Windows, this function expects its argument to be in
6665 * UTF-8 -- not the system code page. This means that you
6666 * should not use this function with string from argv as it is passed
6667 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6668 * the commandline. #GApplication also uses UTF-8 but
6669 * g_application_command_line_create_file_for_arg() may be more useful
6670 * for you there. It is also always possible to use this function with
6671 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6673 * Returns: (transfer full): a new #GFile.
6674 * Free the returned object with g_object_unref().
6677 g_file_new_for_commandline_arg (const char *arg)
6679 g_return_val_if_fail (arg != NULL, NULL);
6681 return new_for_cmdline_arg (arg, NULL);
6685 * g_file_new_for_commandline_arg_and_cwd:
6686 * @arg: (type filename): a command line string
6687 * @cwd: (type filename): the current working directory of the commandline
6689 * Creates a #GFile with the given argument from the command line.
6691 * This function is similar to g_file_new_for_commandline_arg() except
6692 * that it allows for passing the current working directory as an
6693 * argument instead of using the current working directory of the
6696 * This is useful if the commandline argument was given in a context
6697 * other than the invocation of the current process.
6699 * See also g_application_command_line_create_file_for_arg().
6701 * Returns: (transfer full): a new #GFile
6706 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6709 g_return_val_if_fail (arg != NULL, NULL);
6710 g_return_val_if_fail (cwd != NULL, NULL);
6712 return new_for_cmdline_arg (arg, cwd);
6716 * g_file_mount_enclosing_volume:
6717 * @location: input #GFile
6718 * @flags: flags affecting the operation
6719 * @mount_operation: (nullable): a #GMountOperation
6720 * or %NULL to avoid user interaction
6721 * @cancellable: (nullable): optional #GCancellable object,
6723 * @callback: (nullable): a #GAsyncReadyCallback to call
6724 * when the request is satisfied, or %NULL
6725 * @user_data: the data to pass to callback function
6727 * Starts a @mount_operation, mounting the volume that contains
6728 * the file @location.
6730 * When this operation has completed, @callback will be called with
6731 * @user_user data, and the operation can be finalized with
6732 * g_file_mount_enclosing_volume_finish().
6734 * If @cancellable is not %NULL, then the operation can be cancelled by
6735 * triggering the cancellable object from another thread. If the operation
6736 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6739 g_file_mount_enclosing_volume (GFile *location,
6740 GMountMountFlags flags,
6741 GMountOperation *mount_operation,
6742 GCancellable *cancellable,
6743 GAsyncReadyCallback callback,
6748 g_return_if_fail (G_IS_FILE (location));
6750 iface = G_FILE_GET_IFACE (location);
6752 if (iface->mount_enclosing_volume == NULL)
6754 g_task_report_new_error (location, callback, user_data,
6755 g_file_mount_enclosing_volume,
6756 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6757 _("volume doesn’t implement mount"));
6761 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6766 * g_file_mount_enclosing_volume_finish:
6767 * @location: input #GFile
6768 * @result: a #GAsyncResult
6769 * @error: a #GError, or %NULL
6771 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6773 * Returns: %TRUE if successful. If an error has occurred,
6774 * this function will return %FALSE and set @error
6775 * appropriately if present.
6778 g_file_mount_enclosing_volume_finish (GFile *location,
6779 GAsyncResult *result,
6784 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6785 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6787 if (g_async_result_legacy_propagate_error (result, error))
6789 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6790 return g_task_propagate_boolean (G_TASK (result), error);
6792 iface = G_FILE_GET_IFACE (location);
6794 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6797 /********************************************
6798 * Utility functions *
6799 ********************************************/
6802 * g_file_query_default_handler:
6803 * @file: a #GFile to open
6804 * @cancellable: optional #GCancellable object, %NULL to ignore
6805 * @error: a #GError, or %NULL
6807 * Returns the #GAppInfo that is registered as the default
6808 * application to handle the file specified by @file.
6810 * If @cancellable is not %NULL, then the operation can be cancelled by
6811 * triggering the cancellable object from another thread. If the operation
6812 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6814 * Returns: (transfer full): a #GAppInfo if the handle was found,
6815 * %NULL if there were errors.
6816 * When you are done with it, release it with g_object_unref()
6819 g_file_query_default_handler (GFile *file,
6820 GCancellable *cancellable,
6824 const char *content_type;
6829 uri_scheme = g_file_get_uri_scheme (file);
6830 if (uri_scheme && uri_scheme[0] != '\0')
6832 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6833 g_free (uri_scheme);
6835 if (appinfo != NULL)
6839 g_free (uri_scheme);
6841 info = g_file_query_info (file,
6842 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
6843 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
6852 content_type = g_file_info_get_content_type (info);
6853 if (content_type == NULL)
6854 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
6857 /* Don't use is_native(), as we want to support fuse paths if available */
6858 path = g_file_get_path (file);
6859 appinfo = g_app_info_get_default_for_type (content_type,
6864 g_object_unref (info);
6866 if (appinfo != NULL)
6869 g_set_error_literal (error, G_IO_ERROR,
6870 G_IO_ERROR_NOT_SUPPORTED,
6871 _("No application is registered as handling this file"));
6876 query_default_handler_query_info_cb (GObject *object,
6877 GAsyncResult *result,
6880 GFile *file = G_FILE (object);
6881 GTask *task = G_TASK (user_data);
6882 GError *error = NULL;
6884 const char *content_type;
6885 GAppInfo *appinfo = NULL;
6887 info = g_file_query_info_finish (file, result, &error);
6890 g_task_return_error (task, g_steal_pointer (&error));
6891 g_object_unref (task);
6895 content_type = g_file_info_get_content_type (info);
6896 if (content_type == NULL)
6897 content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
6902 /* Don't use is_native(), as we want to support fuse paths if available */
6903 path = g_file_get_path (file);
6905 /* FIXME: The following still uses blocking calls. */
6906 appinfo = g_app_info_get_default_for_type (content_type,
6911 g_object_unref (info);
6913 if (appinfo != NULL)
6914 g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6916 g_task_return_new_error (task,
6918 G_IO_ERROR_NOT_SUPPORTED,
6919 _("No application is registered as handling this file"));
6920 g_object_unref (task);
6924 * g_file_query_default_handler_async:
6925 * @file: a #GFile to open
6926 * @io_priority: the [I/O priority][io-priority] of the request
6927 * @cancellable: optional #GCancellable object, %NULL to ignore
6928 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
6929 * @user_data: (nullable): data to pass to @callback
6931 * Async version of g_file_query_default_handler().
6936 g_file_query_default_handler_async (GFile *file,
6938 GCancellable *cancellable,
6939 GAsyncReadyCallback callback,
6945 task = g_task_new (file, cancellable, callback, user_data);
6946 g_task_set_source_tag (task, g_file_query_default_handler_async);
6948 uri_scheme = g_file_get_uri_scheme (file);
6949 if (uri_scheme && uri_scheme[0] != '\0')
6953 /* FIXME: The following still uses blocking calls. */
6954 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6955 g_free (uri_scheme);
6957 if (appinfo != NULL)
6959 g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
6960 g_object_unref (task);
6965 g_free (uri_scheme);
6967 g_file_query_info_async (file,
6968 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
6969 G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
6973 query_default_handler_query_info_cb,
6974 g_steal_pointer (&task));
6978 * g_file_query_default_handler_finish:
6979 * @file: a #GFile to open
6980 * @result: a #GAsyncResult
6981 * @error: (nullable): a #GError
6983 * Finishes a g_file_query_default_handler_async() operation.
6985 * Returns: (transfer full): a #GAppInfo if the handle was found,
6986 * %NULL if there were errors.
6987 * When you are done with it, release it with g_object_unref()
6992 g_file_query_default_handler_finish (GFile *file,
6993 GAsyncResult *result,
6996 g_return_val_if_fail (G_IS_FILE (file), NULL);
6997 g_return_val_if_fail (g_task_is_valid (result, file), NULL);
6999 return g_task_propagate_pointer (G_TASK (result), error);
7002 #define GET_CONTENT_BLOCK_SIZE 8192
7005 * g_file_load_contents:
7006 * @file: input #GFile
7007 * @cancellable: optional #GCancellable object, %NULL to ignore
7008 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7009 * @length: (out) (optional): a location to place the length of the contents of the file,
7010 * or %NULL if the length is not needed
7011 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7012 * or %NULL if the entity tag is not needed
7013 * @error: a #GError, or %NULL
7015 * Loads the content of the file into memory. The data is always
7016 * zero-terminated, but this is not included in the resultant @length.
7017 * The returned @contents should be freed with g_free() when no longer
7020 * If @cancellable is not %NULL, then the operation can be cancelled by
7021 * triggering the cancellable object from another thread. If the operation
7022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7024 * Returns: %TRUE if the @file's contents were successfully loaded.
7025 * %FALSE if there were errors.
7028 g_file_load_contents (GFile *file,
7029 GCancellable *cancellable,
7035 GFileInputStream *in;
7036 GByteArray *content;
7041 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7042 g_return_val_if_fail (contents != NULL, FALSE);
7044 in = g_file_read (file, cancellable, error);
7048 content = g_byte_array_new ();
7051 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7052 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
7053 content->data + pos,
7054 GET_CONTENT_BLOCK_SIZE,
7055 cancellable, error)) > 0)
7058 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
7065 info = g_file_input_stream_query_info (in,
7066 G_FILE_ATTRIBUTE_ETAG_VALUE,
7071 *etag_out = g_strdup (g_file_info_get_etag (info));
7072 g_object_unref (info);
7076 /* Ignore errors on close */
7077 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
7078 g_object_unref (in);
7082 /* error is set already */
7083 g_byte_array_free (content, TRUE);
7090 /* Zero terminate (we got an extra byte allocated for this */
7091 content->data[pos] = 0;
7093 *contents = (char *)g_byte_array_free (content, FALSE);
7100 GFileReadMoreCallback read_more_callback;
7101 GByteArray *content;
7108 load_contents_data_free (LoadContentsData *data)
7111 g_byte_array_free (data->content, TRUE);
7112 g_free (data->etag);
7117 load_contents_close_callback (GObject *obj,
7118 GAsyncResult *close_res,
7121 GInputStream *stream = G_INPUT_STREAM (obj);
7122 LoadContentsData *data = user_data;
7124 /* Ignore errors here, we're only reading anyway */
7125 g_input_stream_close_finish (stream, close_res, NULL);
7126 g_object_unref (stream);
7128 g_task_return_boolean (data->task, TRUE);
7129 g_object_unref (data->task);
7133 load_contents_fstat_callback (GObject *obj,
7134 GAsyncResult *stat_res,
7137 GInputStream *stream = G_INPUT_STREAM (obj);
7138 LoadContentsData *data = user_data;
7141 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
7145 data->etag = g_strdup (g_file_info_get_etag (info));
7146 g_object_unref (info);
7149 g_input_stream_close_async (stream, 0,
7150 g_task_get_cancellable (data->task),
7151 load_contents_close_callback, data);
7155 load_contents_read_callback (GObject *obj,
7156 GAsyncResult *read_res,
7159 GInputStream *stream = G_INPUT_STREAM (obj);
7160 LoadContentsData *data = user_data;
7161 GError *error = NULL;
7164 read_size = g_input_stream_read_finish (stream, read_res, &error);
7168 g_task_return_error (data->task, error);
7169 g_object_unref (data->task);
7171 /* Close the file ignoring any error */
7172 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7173 g_object_unref (stream);
7175 else if (read_size == 0)
7177 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7178 G_FILE_ATTRIBUTE_ETAG_VALUE,
7180 g_task_get_cancellable (data->task),
7181 load_contents_fstat_callback,
7184 else if (read_size > 0)
7186 data->pos += read_size;
7188 g_byte_array_set_size (data->content,
7189 data->pos + GET_CONTENT_BLOCK_SIZE);
7192 if (data->read_more_callback &&
7193 !data->read_more_callback ((char *)data->content->data, data->pos,
7194 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
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 g_input_stream_read_async (stream,
7203 data->content->data + data->pos,
7204 GET_CONTENT_BLOCK_SIZE,
7206 g_task_get_cancellable (data->task),
7207 load_contents_read_callback,
7213 load_contents_open_callback (GObject *obj,
7214 GAsyncResult *open_res,
7217 GFile *file = G_FILE (obj);
7218 GFileInputStream *stream;
7219 LoadContentsData *data = user_data;
7220 GError *error = NULL;
7222 stream = g_file_read_finish (file, open_res, &error);
7226 g_byte_array_set_size (data->content,
7227 data->pos + GET_CONTENT_BLOCK_SIZE);
7228 g_input_stream_read_async (G_INPUT_STREAM (stream),
7229 data->content->data + data->pos,
7230 GET_CONTENT_BLOCK_SIZE,
7232 g_task_get_cancellable (data->task),
7233 load_contents_read_callback,
7238 g_task_return_error (data->task, error);
7239 g_object_unref (data->task);
7244 * g_file_load_partial_contents_async: (skip)
7245 * @file: input #GFile
7246 * @cancellable: optional #GCancellable object, %NULL to ignore
7247 * @read_more_callback: (scope call) (closure user_data): a
7248 * #GFileReadMoreCallback to receive partial data
7249 * and to specify whether further data should be read
7250 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7251 * when the request is satisfied
7252 * @user_data: the data to pass to the callback functions
7254 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7255 * be used to stop reading from the file when appropriate, else this
7256 * function will behave exactly as g_file_load_contents_async(). This
7257 * operation can be finished by g_file_load_partial_contents_finish().
7259 * Users of this function should be aware that @user_data is passed to
7260 * both the @read_more_callback and the @callback.
7262 * If @cancellable is not %NULL, then the operation can be cancelled by
7263 * triggering the cancellable object from another thread. If the operation
7264 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7267 g_file_load_partial_contents_async (GFile *file,
7268 GCancellable *cancellable,
7269 GFileReadMoreCallback read_more_callback,
7270 GAsyncReadyCallback callback,
7273 LoadContentsData *data;
7275 g_return_if_fail (G_IS_FILE (file));
7277 data = g_new0 (LoadContentsData, 1);
7278 data->read_more_callback = read_more_callback;
7279 data->content = g_byte_array_new ();
7281 data->task = g_task_new (file, cancellable, callback, user_data);
7282 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7283 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7285 g_file_read_async (file,
7287 g_task_get_cancellable (data->task),
7288 load_contents_open_callback,
7293 * g_file_load_partial_contents_finish:
7294 * @file: input #GFile
7295 * @res: a #GAsyncResult
7296 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7297 * @length: (out) (optional): a location to place the length of the contents of the file,
7298 * or %NULL if the length is not needed
7299 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7300 * or %NULL if the entity tag is not needed
7301 * @error: a #GError, or %NULL
7303 * Finishes an asynchronous partial load operation that was started
7304 * with g_file_load_partial_contents_async(). The data is always
7305 * zero-terminated, but this is not included in the resultant @length.
7306 * The returned @contents should be freed with g_free() when no longer
7309 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7310 * present, it will be set appropriately.
7313 g_file_load_partial_contents_finish (GFile *file,
7321 LoadContentsData *data;
7323 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7324 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7325 g_return_val_if_fail (contents != NULL, FALSE);
7327 task = G_TASK (res);
7329 if (!g_task_propagate_boolean (task, error))
7336 data = g_task_get_task_data (task);
7339 *length = data->pos;
7343 *etag_out = data->etag;
7347 /* Zero terminate */
7348 g_byte_array_set_size (data->content, data->pos + 1);
7349 data->content->data[data->pos] = 0;
7351 *contents = (char *)g_byte_array_free (data->content, FALSE);
7352 data->content = NULL;
7358 * g_file_load_contents_async:
7359 * @file: input #GFile
7360 * @cancellable: optional #GCancellable object, %NULL to ignore
7361 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7362 * @user_data: the data to pass to callback function
7364 * Starts an asynchronous load of the @file's contents.
7366 * For more details, see g_file_load_contents() which is
7367 * the synchronous version of this call.
7369 * When the load operation has completed, @callback will be called
7370 * with @user data. To finish the operation, call
7371 * g_file_load_contents_finish() with the #GAsyncResult returned by
7374 * If @cancellable is not %NULL, then the operation can be cancelled by
7375 * triggering the cancellable object from another thread. If the operation
7376 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7379 g_file_load_contents_async (GFile *file,
7380 GCancellable *cancellable,
7381 GAsyncReadyCallback callback,
7384 g_file_load_partial_contents_async (file,
7387 callback, user_data);
7391 * g_file_load_contents_finish:
7392 * @file: input #GFile
7393 * @res: a #GAsyncResult
7394 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7395 * @length: (out) (optional): a location to place the length of the contents of the file,
7396 * or %NULL if the length is not needed
7397 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7398 * or %NULL if the entity tag is not needed
7399 * @error: a #GError, or %NULL
7401 * Finishes an asynchronous load of the @file's contents.
7402 * The contents are placed in @contents, and @length is set to the
7403 * size of the @contents string. The @contents should be freed with
7404 * g_free() when no longer needed. If @etag_out is present, it will be
7405 * set to the new entity tag for the @file.
7407 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7408 * present, it will be set appropriately.
7411 g_file_load_contents_finish (GFile *file,
7418 return g_file_load_partial_contents_finish (file,
7427 * g_file_replace_contents:
7428 * @file: input #GFile
7429 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7430 * @length: the length of @contents in bytes
7431 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7433 * @make_backup: %TRUE if a backup should be created
7434 * @flags: a set of #GFileCreateFlags
7435 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7436 * for the document. This should be freed with g_free() when no longer
7438 * @cancellable: optional #GCancellable object, %NULL to ignore
7439 * @error: a #GError, or %NULL
7441 * Replaces the contents of @file with @contents of @length bytes.
7443 * If @etag is specified (not %NULL), any existing file must have that etag,
7444 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7446 * If @make_backup is %TRUE, this function will attempt to make a backup
7447 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7448 * file contents in the safest way possible. For example, atomic renames are
7449 * used when replacing local files’ contents.
7451 * If @cancellable is not %NULL, then the operation can be cancelled by
7452 * triggering the cancellable object from another thread. If the operation
7453 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7455 * The returned @new_etag can be used to verify that the file hasn't
7456 * changed the next time it is saved over.
7458 * Returns: %TRUE if successful. If an error has occurred, this function
7459 * will return %FALSE and set @error appropriately if present.
7462 g_file_replace_contents (GFile *file,
7463 const char *contents,
7466 gboolean make_backup,
7467 GFileCreateFlags flags,
7469 GCancellable *cancellable,
7472 GFileOutputStream *out;
7473 gsize pos, remainder;
7477 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7478 g_return_val_if_fail (contents != NULL, FALSE);
7480 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7486 while (remainder > 0 &&
7487 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7489 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7497 if (remainder > 0 && res < 0)
7499 /* Ignore errors on close */
7500 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7501 g_object_unref (out);
7503 /* error is set already */
7507 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7510 *new_etag = g_file_output_stream_get_etag (out);
7512 g_object_unref (out);
7523 } ReplaceContentsData;
7526 replace_contents_data_free (ReplaceContentsData *data)
7528 g_bytes_unref (data->content);
7529 g_free (data->etag);
7534 replace_contents_close_callback (GObject *obj,
7535 GAsyncResult *close_res,
7538 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7539 ReplaceContentsData *data = user_data;
7541 /* Ignore errors here, we're only reading anyway */
7542 g_output_stream_close_finish (stream, close_res, NULL);
7543 g_object_unref (stream);
7547 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7548 g_task_return_boolean (data->task, TRUE);
7550 g_object_unref (data->task);
7554 replace_contents_write_callback (GObject *obj,
7555 GAsyncResult *read_res,
7558 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7559 ReplaceContentsData *data = user_data;
7560 GError *error = NULL;
7563 write_size = g_output_stream_write_finish (stream, read_res, &error);
7565 if (write_size <= 0)
7567 /* Error or EOF, close the file */
7570 data->failed = TRUE;
7571 g_task_return_error (data->task, error);
7573 g_output_stream_close_async (stream, 0,
7574 g_task_get_cancellable (data->task),
7575 replace_contents_close_callback, data);
7577 else if (write_size > 0)
7579 const gchar *content;
7582 content = g_bytes_get_data (data->content, &length);
7583 data->pos += write_size;
7585 if (data->pos >= length)
7586 g_output_stream_close_async (stream, 0,
7587 g_task_get_cancellable (data->task),
7588 replace_contents_close_callback, data);
7590 g_output_stream_write_async (stream,
7591 content + data->pos,
7594 g_task_get_cancellable (data->task),
7595 replace_contents_write_callback,
7601 replace_contents_open_callback (GObject *obj,
7602 GAsyncResult *open_res,
7605 GFile *file = G_FILE (obj);
7606 GFileOutputStream *stream;
7607 ReplaceContentsData *data = user_data;
7608 GError *error = NULL;
7610 stream = g_file_replace_finish (file, open_res, &error);
7614 const gchar *content;
7617 content = g_bytes_get_data (data->content, &length);
7618 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7619 content + data->pos,
7622 g_task_get_cancellable (data->task),
7623 replace_contents_write_callback,
7628 g_task_return_error (data->task, error);
7629 g_object_unref (data->task);
7634 * g_file_replace_contents_async:
7635 * @file: input #GFile
7636 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7637 * @length: the length of @contents in bytes
7638 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7639 * @make_backup: %TRUE if a backup should be created
7640 * @flags: a set of #GFileCreateFlags
7641 * @cancellable: optional #GCancellable object, %NULL to ignore
7642 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7643 * @user_data: the data to pass to callback function
7645 * Starts an asynchronous replacement of @file with the given
7646 * @contents of @length bytes. @etag will replace the document's
7647 * current entity tag.
7649 * When this operation has completed, @callback will be called with
7650 * @user_user data, and the operation can be finalized with
7651 * g_file_replace_contents_finish().
7653 * If @cancellable is not %NULL, then the operation can be cancelled by
7654 * triggering the cancellable object from another thread. If the operation
7655 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7657 * If @make_backup is %TRUE, this function will attempt to
7658 * make a backup of @file.
7660 * Note that no copy of @contents will be made, so it must stay valid
7661 * until @callback is called. See g_file_replace_contents_bytes_async()
7662 * for a #GBytes version that will automatically hold a reference to the
7663 * contents (without copying) for the duration of the call.
7666 g_file_replace_contents_async (GFile *file,
7667 const char *contents,
7670 gboolean make_backup,
7671 GFileCreateFlags flags,
7672 GCancellable *cancellable,
7673 GAsyncReadyCallback callback,
7678 bytes = g_bytes_new_static (contents, length);
7679 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7680 cancellable, callback, user_data);
7681 g_bytes_unref (bytes);
7685 * g_file_replace_contents_bytes_async:
7686 * @file: input #GFile
7687 * @contents: a #GBytes
7688 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7689 * @make_backup: %TRUE if a backup should be created
7690 * @flags: a set of #GFileCreateFlags
7691 * @cancellable: optional #GCancellable object, %NULL to ignore
7692 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7693 * @user_data: the data to pass to callback function
7695 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7696 * This function will keep a ref on @contents until the operation is done.
7697 * Unlike g_file_replace_contents_async() this allows forgetting about the
7698 * content without waiting for the callback.
7700 * When this operation has completed, @callback will be called with
7701 * @user_user data, and the operation can be finalized with
7702 * g_file_replace_contents_finish().
7707 g_file_replace_contents_bytes_async (GFile *file,
7710 gboolean make_backup,
7711 GFileCreateFlags flags,
7712 GCancellable *cancellable,
7713 GAsyncReadyCallback callback,
7716 ReplaceContentsData *data;
7718 g_return_if_fail (G_IS_FILE (file));
7719 g_return_if_fail (contents != NULL);
7721 data = g_new0 (ReplaceContentsData, 1);
7723 data->content = g_bytes_ref (contents);
7725 data->task = g_task_new (file, cancellable, callback, user_data);
7726 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7727 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7729 g_file_replace_async (file,
7734 g_task_get_cancellable (data->task),
7735 replace_contents_open_callback,
7740 * g_file_replace_contents_finish:
7741 * @file: input #GFile
7742 * @res: a #GAsyncResult
7743 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7744 * for the document. This should be freed with g_free() when it is no
7745 * longer needed, or %NULL
7746 * @error: a #GError, or %NULL
7748 * Finishes an asynchronous replace of the given @file. See
7749 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7750 * tag for the document, if present.
7752 * Returns: %TRUE on success, %FALSE on failure.
7755 g_file_replace_contents_finish (GFile *file,
7761 ReplaceContentsData *data;
7763 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7764 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7766 task = G_TASK (res);
7768 if (!g_task_propagate_boolean (task, error))
7771 data = g_task_get_task_data (task);
7775 *new_etag = data->etag;
7776 data->etag = NULL; /* Take ownership */
7783 g_file_real_measure_disk_usage (GFile *file,
7784 GFileMeasureFlags flags,
7785 GCancellable *cancellable,
7786 GFileMeasureProgressCallback progress_callback,
7787 gpointer progress_data,
7788 guint64 *disk_usage,
7793 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7794 "Operation not supported for the current backend.");
7800 GFileMeasureFlags flags;
7801 GFileMeasureProgressCallback progress_callback;
7802 gpointer progress_data;
7814 GFileMeasureProgressCallback callback;
7817 guint64 current_size;
7823 measure_disk_usage_invoke_progress (gpointer user_data)
7825 MeasureProgress *progress = user_data;
7827 (* progress->callback) (progress->reporting,
7828 progress->current_size, progress->num_dirs, progress->num_files,
7829 progress->user_data);
7835 measure_disk_usage_progress (gboolean reporting,
7836 guint64 current_size,
7841 MeasureProgress progress;
7842 GTask *task = user_data;
7843 MeasureTaskData *data;
7845 data = g_task_get_task_data (task);
7847 progress.callback = data->progress_callback;
7848 progress.user_data = data->progress_data;
7849 progress.reporting = reporting;
7850 progress.current_size = current_size;
7851 progress.num_dirs = num_dirs;
7852 progress.num_files = num_files;
7854 g_main_context_invoke_full (g_task_get_context (task),
7855 g_task_get_priority (task),
7856 measure_disk_usage_invoke_progress,
7857 g_memdup (&progress, sizeof progress),
7862 measure_disk_usage_thread (GTask *task,
7863 gpointer source_object,
7865 GCancellable *cancellable)
7867 MeasureTaskData *data = task_data;
7868 GError *error = NULL;
7869 MeasureResult result = { 0, };
7871 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7872 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7873 &result.disk_usage, &result.num_dirs, &result.num_files,
7875 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7877 g_task_return_error (task, error);
7881 g_file_real_measure_disk_usage_async (GFile *file,
7882 GFileMeasureFlags flags,
7884 GCancellable *cancellable,
7885 GFileMeasureProgressCallback progress_callback,
7886 gpointer progress_data,
7887 GAsyncReadyCallback callback,
7890 MeasureTaskData data;
7894 data.progress_callback = progress_callback;
7895 data.progress_data = progress_data;
7897 task = g_task_new (file, cancellable, callback, user_data);
7898 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7899 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7900 g_task_set_priority (task, io_priority);
7902 g_task_run_in_thread (task, measure_disk_usage_thread);
7903 g_object_unref (task);
7907 g_file_real_measure_disk_usage_finish (GFile *file,
7908 GAsyncResult *result,
7909 guint64 *disk_usage,
7914 MeasureResult *measure_result;
7916 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7918 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7920 if (measure_result == NULL)
7924 *disk_usage = measure_result->disk_usage;
7927 *num_dirs = measure_result->num_dirs;
7930 *num_files = measure_result->num_files;
7932 g_free (measure_result);
7938 * g_file_measure_disk_usage:
7940 * @flags: #GFileMeasureFlags
7941 * @cancellable: (nullable): optional #GCancellable
7942 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7943 * @progress_data: user_data for @progress_callback
7944 * @disk_usage: (out) (optional): the number of bytes of disk space used
7945 * @num_dirs: (out) (optional): the number of directories encountered
7946 * @num_files: (out) (optional): the number of non-directories encountered
7947 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7949 * Recursively measures the disk usage of @file.
7951 * This is essentially an analog of the 'du' command, but it also
7952 * reports the number of directories and non-directory files encountered
7953 * (including things like symbolic links).
7955 * By default, errors are only reported against the toplevel file
7956 * itself. Errors found while recursing are silently ignored, unless
7957 * %G_FILE_MEASURE_REPORT_ANY_ERROR is given in @flags.
7959 * The returned size, @disk_usage, is in bytes and should be formatted
7960 * with g_format_size() in order to get something reasonable for showing
7961 * in a user interface.
7963 * @progress_callback and @progress_data can be given to request
7964 * periodic progress updates while scanning. See the documentation for
7965 * #GFileMeasureProgressCallback for information about when and how the
7966 * callback will be invoked.
7968 * Returns: %TRUE if successful, with the out parameters set.
7969 * %FALSE otherwise, with @error set.
7974 g_file_measure_disk_usage (GFile *file,
7975 GFileMeasureFlags flags,
7976 GCancellable *cancellable,
7977 GFileMeasureProgressCallback progress_callback,
7978 gpointer progress_data,
7979 guint64 *disk_usage,
7984 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7985 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7986 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7988 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7989 progress_callback, progress_data,
7990 disk_usage, num_dirs, num_files,
7995 * g_file_measure_disk_usage_async:
7997 * @flags: #GFileMeasureFlags
7998 * @io_priority: the [I/O priority][io-priority] of the request
7999 * @cancellable: (nullable): optional #GCancellable
8000 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
8001 * @progress_data: user_data for @progress_callback
8002 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
8003 * @user_data: the data to pass to callback function
8005 * Recursively measures the disk usage of @file.
8007 * This is the asynchronous version of g_file_measure_disk_usage(). See
8008 * there for more information.
8013 g_file_measure_disk_usage_async (GFile *file,
8014 GFileMeasureFlags flags,
8016 GCancellable *cancellable,
8017 GFileMeasureProgressCallback progress_callback,
8018 gpointer progress_data,
8019 GAsyncReadyCallback callback,
8022 g_return_if_fail (G_IS_FILE (file));
8023 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8025 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
8026 progress_callback, progress_data,
8027 callback, user_data);
8031 * g_file_measure_disk_usage_finish:
8033 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
8034 * @disk_usage: (out) (optional): the number of bytes of disk space used
8035 * @num_dirs: (out) (optional): the number of directories encountered
8036 * @num_files: (out) (optional): the number of non-directories encountered
8037 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
8039 * Collects the results from an earlier call to
8040 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
8043 * Returns: %TRUE if successful, with the out parameters set.
8044 * %FALSE otherwise, with @error set.
8049 g_file_measure_disk_usage_finish (GFile *file,
8050 GAsyncResult *result,
8051 guint64 *disk_usage,
8056 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8057 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
8059 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
8063 * g_file_start_mountable:
8064 * @file: input #GFile
8065 * @flags: flags affecting the operation
8066 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
8067 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
8068 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
8069 * @user_data: the data to pass to callback function
8071 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
8072 * Using @start_operation, you can request callbacks when, for instance,
8073 * passwords are needed during authentication.
8075 * If @cancellable is not %NULL, then the operation can be cancelled by
8076 * triggering the cancellable object from another thread. If the operation
8077 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8079 * When the operation is finished, @callback will be called.
8080 * You can then call g_file_mount_mountable_finish() to get
8081 * the result of the operation.
8086 g_file_start_mountable (GFile *file,
8087 GDriveStartFlags flags,
8088 GMountOperation *start_operation,
8089 GCancellable *cancellable,
8090 GAsyncReadyCallback callback,
8095 g_return_if_fail (G_IS_FILE (file));
8097 iface = G_FILE_GET_IFACE (file);
8099 if (iface->start_mountable == NULL)
8101 g_task_report_new_error (file, callback, user_data,
8102 g_file_start_mountable,
8103 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8104 _("Operation not supported"));
8108 (* iface->start_mountable) (file,
8117 * g_file_start_mountable_finish:
8118 * @file: input #GFile
8119 * @result: a #GAsyncResult
8120 * @error: a #GError, or %NULL
8122 * Finishes a start operation. See g_file_start_mountable() for details.
8124 * Finish an asynchronous start operation that was started
8125 * with g_file_start_mountable().
8127 * Returns: %TRUE if the operation finished successfully. %FALSE
8133 g_file_start_mountable_finish (GFile *file,
8134 GAsyncResult *result,
8139 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8140 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8142 if (g_async_result_legacy_propagate_error (result, error))
8144 else if (g_async_result_is_tagged (result, g_file_start_mountable))
8145 return g_task_propagate_boolean (G_TASK (result), error);
8147 iface = G_FILE_GET_IFACE (file);
8148 return (* iface->start_mountable_finish) (file, result, error);
8152 * g_file_stop_mountable:
8153 * @file: input #GFile
8154 * @flags: flags affecting the operation
8155 * @mount_operation: (nullable): a #GMountOperation,
8156 * or %NULL to avoid user interaction.
8157 * @cancellable: (nullable): optional #GCancellable object,
8159 * @callback: (nullable): a #GAsyncReadyCallback to call
8160 * when the request is satisfied, or %NULL
8161 * @user_data: the data to pass to callback function
8163 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8165 * If @cancellable is not %NULL, then the operation can be cancelled by
8166 * triggering the cancellable object from another thread. If the operation
8167 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8169 * When the operation is finished, @callback will be called.
8170 * You can then call g_file_stop_mountable_finish() to get
8171 * the result of the operation.
8176 g_file_stop_mountable (GFile *file,
8177 GMountUnmountFlags flags,
8178 GMountOperation *mount_operation,
8179 GCancellable *cancellable,
8180 GAsyncReadyCallback callback,
8185 g_return_if_fail (G_IS_FILE (file));
8187 iface = G_FILE_GET_IFACE (file);
8189 if (iface->stop_mountable == NULL)
8191 g_task_report_new_error (file, callback, user_data,
8192 g_file_stop_mountable,
8193 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8194 _("Operation not supported"));
8198 (* iface->stop_mountable) (file,
8207 * g_file_stop_mountable_finish:
8208 * @file: input #GFile
8209 * @result: a #GAsyncResult
8210 * @error: a #GError, or %NULL
8212 * Finishes a stop operation, see g_file_stop_mountable() for details.
8214 * Finish an asynchronous stop operation that was started
8215 * with g_file_stop_mountable().
8217 * Returns: %TRUE if the operation finished successfully.
8223 g_file_stop_mountable_finish (GFile *file,
8224 GAsyncResult *result,
8229 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8230 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8232 if (g_async_result_legacy_propagate_error (result, error))
8234 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8235 return g_task_propagate_boolean (G_TASK (result), error);
8237 iface = G_FILE_GET_IFACE (file);
8238 return (* iface->stop_mountable_finish) (file, result, error);
8242 * g_file_poll_mountable:
8243 * @file: input #GFile
8244 * @cancellable: optional #GCancellable object, %NULL to ignore
8245 * @callback: (nullable): a #GAsyncReadyCallback to call
8246 * when the request is satisfied, or %NULL
8247 * @user_data: the data to pass to callback function
8249 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8251 * If @cancellable is not %NULL, then the operation can be cancelled by
8252 * triggering the cancellable object from another thread. If the operation
8253 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8255 * When the operation is finished, @callback will be called.
8256 * You can then call g_file_mount_mountable_finish() to get
8257 * the result of the operation.
8262 g_file_poll_mountable (GFile *file,
8263 GCancellable *cancellable,
8264 GAsyncReadyCallback callback,
8269 g_return_if_fail (G_IS_FILE (file));
8271 iface = G_FILE_GET_IFACE (file);
8273 if (iface->poll_mountable == NULL)
8275 g_task_report_new_error (file, callback, user_data,
8276 g_file_poll_mountable,
8277 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8278 _("Operation not supported"));
8282 (* iface->poll_mountable) (file,
8289 * g_file_poll_mountable_finish:
8290 * @file: input #GFile
8291 * @result: a #GAsyncResult
8292 * @error: a #GError, or %NULL
8294 * Finishes a poll operation. See g_file_poll_mountable() for details.
8296 * Finish an asynchronous poll operation that was polled
8297 * with g_file_poll_mountable().
8299 * Returns: %TRUE if the operation finished successfully. %FALSE
8305 g_file_poll_mountable_finish (GFile *file,
8306 GAsyncResult *result,
8311 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8312 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8314 if (g_async_result_legacy_propagate_error (result, error))
8316 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8317 return g_task_propagate_boolean (G_TASK (result), error);
8319 iface = G_FILE_GET_IFACE (file);
8320 return (* iface->poll_mountable_finish) (file, result, error);
8324 * g_file_supports_thread_contexts:
8327 * Checks if @file supports
8328 * [thread-default contexts][g-main-context-push-thread-default-context].
8329 * If this returns %FALSE, you cannot perform asynchronous operations on
8330 * @file in a thread that has a thread-default context.
8332 * Returns: Whether or not @file supports thread-default contexts.
8337 g_file_supports_thread_contexts (GFile *file)
8341 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8343 iface = G_FILE_GET_IFACE (file);
8344 return iface->supports_thread_contexts;
8348 * g_file_load_bytes:
8350 * @cancellable: (nullable): a #GCancellable or %NULL
8351 * @etag_out: (out) (nullable) (optional): a location to place the current
8352 * entity tag for the file, or %NULL if the entity tag is not needed
8353 * @error: a location for a #GError or %NULL
8355 * Loads the contents of @file and returns it as #GBytes.
8357 * If @file is a resource:// based URI, the resulting bytes will reference the
8358 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8359 * g_file_load_contents() and g_bytes_new_take().
8361 * For resources, @etag_out will be set to %NULL.
8363 * The data contained in the resulting #GBytes is always zero-terminated, but
8364 * this is not included in the #GBytes length. The resulting #GBytes should be
8365 * freed with g_bytes_unref() when no longer in use.
8367 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8372 g_file_load_bytes (GFile *file,
8373 GCancellable *cancellable,
8380 g_return_val_if_fail (G_IS_FILE (file), NULL);
8381 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8382 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8384 if (etag_out != NULL)
8387 if (g_file_has_uri_scheme (file, "resource"))
8390 gchar *uri, *unescaped;
8392 uri = g_file_get_uri (file);
8393 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8396 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8402 /* contents is guaranteed to be \0 terminated */
8403 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8404 return g_bytes_new_take (g_steal_pointer (&contents), len);
8410 g_file_load_bytes_cb (GObject *object,
8411 GAsyncResult *result,
8414 GFile *file = G_FILE (object);
8415 GTask *task = user_data;
8416 GError *error = NULL;
8418 gchar *contents = NULL;
8421 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8422 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8425 g_task_return_error (task, g_steal_pointer (&error));
8427 g_task_return_pointer (task,
8428 g_bytes_new_take (g_steal_pointer (&contents), len),
8429 (GDestroyNotify)g_bytes_unref);
8431 g_object_unref (task);
8435 * g_file_load_bytes_async:
8437 * @cancellable: (nullable): a #GCancellable or %NULL
8438 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8439 * request is satisfied
8440 * @user_data: (closure): the data to pass to callback function
8442 * Asynchronously loads the contents of @file as #GBytes.
8444 * If @file is a resource:// based URI, the resulting bytes will reference the
8445 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8446 * g_file_load_contents_async() and g_bytes_new_take().
8448 * @callback should call g_file_load_bytes_finish() to get the result of this
8449 * asynchronous operation.
8451 * See g_file_load_bytes() for more information.
8456 g_file_load_bytes_async (GFile *file,
8457 GCancellable *cancellable,
8458 GAsyncReadyCallback callback,
8461 GError *error = NULL;
8465 g_return_if_fail (G_IS_FILE (file));
8466 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8468 task = g_task_new (file, cancellable, callback, user_data);
8469 g_task_set_source_tag (task, g_file_load_bytes_async);
8471 if (!g_file_has_uri_scheme (file, "resource"))
8473 g_file_load_contents_async (file,
8475 g_file_load_bytes_cb,
8476 g_steal_pointer (&task));
8480 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8483 g_task_return_error (task, g_steal_pointer (&error));
8485 g_task_return_pointer (task,
8486 g_steal_pointer (&bytes),
8487 (GDestroyNotify)g_bytes_unref);
8489 g_object_unref (task);
8493 * g_file_load_bytes_finish:
8495 * @result: a #GAsyncResult provided to the callback
8496 * @etag_out: (out) (nullable) (optional): a location to place the current
8497 * entity tag for the file, or %NULL if the entity tag is not needed
8498 * @error: a location for a #GError, or %NULL
8500 * Completes an asynchronous request to g_file_load_bytes_async().
8502 * For resources, @etag_out will be set to %NULL.
8504 * The data contained in the resulting #GBytes is always zero-terminated, but
8505 * this is not included in the #GBytes length. The resulting #GBytes should be
8506 * freed with g_bytes_unref() when no longer in use.
8508 * See g_file_load_bytes() for more information.
8510 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8515 g_file_load_bytes_finish (GFile *file,
8516 GAsyncResult *result,
8522 g_return_val_if_fail (G_IS_FILE (file), NULL);
8523 g_return_val_if_fail (G_IS_TASK (result), NULL);
8524 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8525 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8527 bytes = g_task_propagate_pointer (G_TASK (result), error);
8529 if (etag_out != NULL)
8530 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));