1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
84 * - g_file_new_build_filename() to create a file from path elements.
86 * One way to think of a #GFile is as an abstraction of a pathname. For
87 * normal files the system pathname is what is stored internally, but as
88 * #GFiles are extensible it could also be something else that corresponds
89 * to a pathname in a userspace implementation of a filesystem.
91 * #GFiles make up hierarchies of directories and files that correspond to
92 * the files on a filesystem. You can move through the file system with
93 * #GFile using g_file_get_parent() to get an identifier for the parent
94 * directory, g_file_get_child() to get a child within a directory,
95 * g_file_resolve_relative_path() to resolve a relative path between two
96 * #GFiles. There can be multiple hierarchies, so you may not end up at
97 * the same root if you repeatedly call g_file_get_parent() on two different
100 * All #GFiles have a basename (get with g_file_get_basename()). These names
101 * are byte strings that are used to identify the file on the filesystem
102 * (relative to its parent directory) and there is no guarantees that they
103 * have any particular charset encoding or even make any sense at all. If
104 * you want to use filenames in a user interface you should use the display
105 * name that you can get by requesting the
106 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
107 * This is guaranteed to be in UTF-8 and can be used in a user interface.
108 * But always store the real basename or the #GFile to use to actually
109 * access the file, because there is no way to go from a display name to
112 * Using #GFile as an identifier has the same weaknesses as using a path
113 * in that there may be multiple aliases for the same file. For instance,
114 * hard or soft links may cause two different #GFiles to refer to the same
115 * file. Other possible causes for aliases are: case insensitive filesystems,
116 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
117 * check if two #GFiles point to the same file you can query for the
118 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
119 * canonicalization of pathnames passed in, so that trivial differences in
120 * the path string used at creation (duplicated slashes, slash at end of
121 * path, "." or ".." path segments, etc) does not create different #GFiles.
123 * Many #GFile operations have both synchronous and asynchronous versions
124 * to suit your application. Asynchronous versions of synchronous functions
125 * simply have _async() appended to their function names. The asynchronous
126 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
127 * the operation, producing a GAsyncResult which is then passed to the
128 * function's matching _finish() operation.
130 * It is highly recommended to use asynchronous calls when running within a
131 * shared main loop, such as in the main thread of an application. This avoids
132 * I/O operations blocking other sources on the main loop from being dispatched.
133 * Synchronous I/O operations should be performed from worker threads. See the
134 * [introduction to asynchronous programming section][async-programming] for
137 * Some #GFile operations almost always take a noticeable amount of time, and
138 * so do not have synchronous analogs. Notable cases include:
139 * - g_file_mount_mountable() to mount a mountable file.
140 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
141 * - g_file_eject_mountable_with_operation() to eject a mountable file.
143 * ## Entity Tags # {#gfile-etag}
145 * One notable feature of #GFiles are entity tags, or "etags" for
146 * short. Entity tags are somewhat like a more abstract version of the
147 * traditional mtime, and can be used to quickly determine if the file
148 * has been modified from the version on the file system. See the
150 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
151 * for HTTP Etag headers, which are a very similar concept.
154 static void g_file_real_query_info_async (GFile *file,
155 const char *attributes,
156 GFileQueryInfoFlags flags,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFileInfo * g_file_real_query_info_finish (GFile *file,
164 static void g_file_real_query_filesystem_info_async (GFile *file,
165 const char *attributes,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
170 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
173 static void g_file_real_enumerate_children_async (GFile *file,
174 const char *attributes,
175 GFileQueryInfoFlags flags,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
183 static void g_file_real_read_async (GFile *file,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
188 static GFileInputStream * g_file_real_read_finish (GFile *file,
191 static void g_file_real_append_to_async (GFile *file,
192 GFileCreateFlags flags,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
197 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
200 static void g_file_real_create_async (GFile *file,
201 GFileCreateFlags flags,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFileOutputStream *g_file_real_create_finish (GFile *file,
209 static void g_file_real_replace_async (GFile *file,
211 gboolean make_backup,
212 GFileCreateFlags flags,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
217 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
220 static void g_file_real_delete_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static gboolean g_file_real_delete_finish (GFile *file,
228 static void g_file_real_trash_async (GFile *file,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static gboolean g_file_real_trash_finish (GFile *file,
236 static void g_file_real_make_directory_async (GFile *file,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
241 static gboolean g_file_real_make_directory_finish (GFile *file,
244 static void g_file_real_open_readwrite_async (GFile *file,
246 GCancellable *cancellable,
247 GAsyncReadyCallback callback,
249 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
252 static void g_file_real_create_readwrite_async (GFile *file,
253 GFileCreateFlags flags,
255 GCancellable *cancellable,
256 GAsyncReadyCallback callback,
258 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
261 static void g_file_real_replace_readwrite_async (GFile *file,
263 gboolean make_backup,
264 GFileCreateFlags flags,
266 GCancellable *cancellable,
267 GAsyncReadyCallback callback,
269 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
272 static gboolean g_file_real_set_attributes_from_info (GFile *file,
274 GFileQueryInfoFlags flags,
275 GCancellable *cancellable,
277 static void g_file_real_set_display_name_async (GFile *file,
278 const char *display_name,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
283 static GFile * g_file_real_set_display_name_finish (GFile *file,
286 static void g_file_real_set_attributes_async (GFile *file,
288 GFileQueryInfoFlags flags,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
293 static gboolean g_file_real_set_attributes_finish (GFile *file,
297 static void g_file_real_find_enclosing_mount_async (GFile *file,
299 GCancellable *cancellable,
300 GAsyncReadyCallback callback,
302 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
305 static void g_file_real_copy_async (GFile *source,
307 GFileCopyFlags flags,
309 GCancellable *cancellable,
310 GFileProgressCallback progress_callback,
311 gpointer progress_callback_data,
312 GAsyncReadyCallback callback,
314 static gboolean g_file_real_copy_finish (GFile *file,
318 static gboolean g_file_real_measure_disk_usage (GFile *file,
319 GFileMeasureFlags flags,
320 GCancellable *cancellable,
321 GFileMeasureProgressCallback progress_callback,
322 gpointer progress_data,
327 static void g_file_real_measure_disk_usage_async (GFile *file,
328 GFileMeasureFlags flags,
330 GCancellable *cancellable,
331 GFileMeasureProgressCallback progress_callback,
332 gpointer progress_data,
333 GAsyncReadyCallback callback,
335 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
336 GAsyncResult *result,
342 typedef GFileIface GFileInterface;
343 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
346 g_file_default_init (GFileIface *iface)
348 iface->enumerate_children_async = g_file_real_enumerate_children_async;
349 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
350 iface->set_display_name_async = g_file_real_set_display_name_async;
351 iface->set_display_name_finish = g_file_real_set_display_name_finish;
352 iface->query_info_async = g_file_real_query_info_async;
353 iface->query_info_finish = g_file_real_query_info_finish;
354 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
355 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
356 iface->set_attributes_async = g_file_real_set_attributes_async;
357 iface->set_attributes_finish = g_file_real_set_attributes_finish;
358 iface->read_async = g_file_real_read_async;
359 iface->read_finish = g_file_real_read_finish;
360 iface->append_to_async = g_file_real_append_to_async;
361 iface->append_to_finish = g_file_real_append_to_finish;
362 iface->create_async = g_file_real_create_async;
363 iface->create_finish = g_file_real_create_finish;
364 iface->replace_async = g_file_real_replace_async;
365 iface->replace_finish = g_file_real_replace_finish;
366 iface->delete_file_async = g_file_real_delete_async;
367 iface->delete_file_finish = g_file_real_delete_finish;
368 iface->trash_async = g_file_real_trash_async;
369 iface->trash_finish = g_file_real_trash_finish;
370 iface->make_directory_async = g_file_real_make_directory_async;
371 iface->make_directory_finish = g_file_real_make_directory_finish;
372 iface->open_readwrite_async = g_file_real_open_readwrite_async;
373 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
374 iface->create_readwrite_async = g_file_real_create_readwrite_async;
375 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
376 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
377 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
378 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
379 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
380 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
381 iface->copy_async = g_file_real_copy_async;
382 iface->copy_finish = g_file_real_copy_finish;
383 iface->measure_disk_usage = g_file_real_measure_disk_usage;
384 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
385 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
391 * @file: input #GFile
393 * Checks to see if a file is native to the platform.
395 * A native file s one expressed in the platform-native filename format,
396 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397 * as it might be on a locally mounted remote filesystem.
399 * On some systems non-native files may be available using the native
400 * filesystem via a userspace filesystem (FUSE), in these cases this call
401 * will return %FALSE, but g_file_get_path() will still return a native path.
403 * This call does no blocking I/O.
405 * Returns: %TRUE if @file is native
408 g_file_is_native (GFile *file)
412 g_return_val_if_fail (G_IS_FILE (file), FALSE);
414 iface = G_FILE_GET_IFACE (file);
416 return (* iface->is_native) (file);
421 * g_file_has_uri_scheme:
422 * @file: input #GFile
423 * @uri_scheme: a string containing a URI scheme
425 * Checks to see if a #GFile has a given URI scheme.
427 * This call does no blocking I/O.
429 * Returns: %TRUE if #GFile's backend supports the
430 * given URI scheme, %FALSE if URI scheme is %NULL,
431 * not supported, or #GFile is invalid.
434 g_file_has_uri_scheme (GFile *file,
435 const char *uri_scheme)
439 g_return_val_if_fail (G_IS_FILE (file), FALSE);
440 g_return_val_if_fail (uri_scheme != NULL, FALSE);
442 iface = G_FILE_GET_IFACE (file);
444 return (* iface->has_uri_scheme) (file, uri_scheme);
449 * g_file_get_uri_scheme:
450 * @file: input #GFile
452 * Gets the URI scheme for a #GFile.
453 * RFC 3986 decodes the scheme as:
455 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
457 * Common schemes include "file", "http", "ftp", etc.
459 * This call does no blocking I/O.
461 * Returns: a string containing the URI scheme for the given
462 * #GFile. The returned string should be freed with g_free()
463 * when no longer needed.
466 g_file_get_uri_scheme (GFile *file)
470 g_return_val_if_fail (G_IS_FILE (file), NULL);
472 iface = G_FILE_GET_IFACE (file);
474 return (* iface->get_uri_scheme) (file);
479 * g_file_get_basename:
480 * @file: input #GFile
482 * Gets the base name (the last component of the path) for a given #GFile.
484 * If called for the top level of a system (such as the filesystem root
485 * or a uri like sftp://host/) it will return a single directory separator
486 * (and on Windows, possibly a drive letter).
488 * The base name is a byte string (not UTF-8). It has no defined encoding
489 * or rules other than it may not contain zero bytes. If you want to use
490 * filenames in a user interface you should use the display name that you
491 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
492 * attribute with g_file_query_info().
494 * This call does no blocking I/O.
496 * Returns: (type filename) (nullable): string containing the #GFile's
497 * base name, or %NULL if given #GFile is invalid. The returned string
498 * should be freed with g_free() when no longer needed.
501 g_file_get_basename (GFile *file)
505 g_return_val_if_fail (G_IS_FILE (file), NULL);
507 iface = G_FILE_GET_IFACE (file);
509 return (* iface->get_basename) (file);
514 * @file: input #GFile
516 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
517 * guaranteed to be an absolute, canonical path. It might contain symlinks.
519 * This call does no blocking I/O.
521 * Returns: (type filename) (nullable): string containing the #GFile's path,
522 * or %NULL if no such path exists. The returned string should be freed
523 * with g_free() when no longer needed.
526 g_file_get_path (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
539 * @file: input #GFile
541 * Gets the URI for the @file.
543 * This call does no blocking I/O.
545 * Returns: a string containing the #GFile's URI.
546 * The returned string should be freed with g_free()
547 * when no longer needed.
550 g_file_get_uri (GFile *file)
554 g_return_val_if_fail (G_IS_FILE (file), NULL);
556 iface = G_FILE_GET_IFACE (file);
558 return (* iface->get_uri) (file);
562 * g_file_get_parse_name:
563 * @file: input #GFile
565 * Gets the parse name of the @file.
566 * A parse name is a UTF-8 string that describes the
567 * file such that one can get the #GFile back using
568 * g_file_parse_name().
570 * This is generally used to show the #GFile as a nice
571 * full-pathname kind of string in a user interface,
572 * like in a location entry.
574 * For local files with names that can safely be converted
575 * to UTF-8 the pathname is used, otherwise the IRI is used
576 * (a form of URI that allows UTF-8 characters unescaped).
578 * This call does no blocking I/O.
580 * Returns: a string containing the #GFile's parse name.
581 * The returned string should be freed with g_free()
582 * when no longer needed.
585 g_file_get_parse_name (GFile *file)
589 g_return_val_if_fail (G_IS_FILE (file), NULL);
591 iface = G_FILE_GET_IFACE (file);
593 return (* iface->get_parse_name) (file);
598 * @file: input #GFile
600 * Duplicates a #GFile handle. This operation does not duplicate
601 * the actual file or directory represented by the #GFile; see
602 * g_file_copy() if attempting to copy a file.
604 * This call does no blocking I/O.
606 * Returns: (transfer full): a new #GFile that is a duplicate
607 * of the given #GFile.
610 g_file_dup (GFile *file)
614 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 iface = G_FILE_GET_IFACE (file);
618 return (* iface->dup) (file);
623 * @file: (type GFile): #gconstpointer to a #GFile
625 * Creates a hash value for a #GFile.
627 * This call does no blocking I/O.
630 * Returns: 0 if @file is not a valid #GFile, otherwise an
631 * integer that can be used as hash value for the #GFile.
632 * This function is intended for easily hashing a #GFile to
633 * add to a #GHashTable or similar data structure.
636 g_file_hash (gconstpointer file)
640 g_return_val_if_fail (G_IS_FILE (file), 0);
642 iface = G_FILE_GET_IFACE (file);
644 return (* iface->hash) ((GFile *)file);
649 * @file1: the first #GFile
650 * @file2: the second #GFile
652 * Checks if the two given #GFiles refer to the same file.
654 * Note that two #GFiles that differ can still refer to the same
655 * file on the filesystem due to various forms of filename
658 * This call does no blocking I/O.
660 * Returns: %TRUE if @file1 and @file2 are equal.
663 g_file_equal (GFile *file1,
668 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
669 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
674 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
677 iface = G_FILE_GET_IFACE (file1);
679 return (* iface->equal) (file1, file2);
685 * @file: input #GFile
687 * Gets the parent directory for the @file.
688 * If the @file represents the root directory of the
689 * file system, then %NULL will be returned.
691 * This call does no blocking I/O.
693 * Returns: (nullable) (transfer full): a #GFile structure to the
694 * parent of the given #GFile or %NULL if there is no parent. Free
695 * the returned object with g_object_unref().
698 g_file_get_parent (GFile *file)
702 g_return_val_if_fail (G_IS_FILE (file), NULL);
704 iface = G_FILE_GET_IFACE (file);
706 return (* iface->get_parent) (file);
711 * @file: input #GFile
712 * @parent: (nullable): the parent to check for, or %NULL
714 * Checks if @file has a parent, and optionally, if it is @parent.
716 * If @parent is %NULL then this function returns %TRUE if @file has any
717 * parent at all. If @parent is non-%NULL then %TRUE is only returned
718 * if @file is an immediate child of @parent.
720 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
721 * the case that @parent is %NULL).
726 g_file_has_parent (GFile *file,
729 GFile *actual_parent;
732 g_return_val_if_fail (G_IS_FILE (file), FALSE);
733 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
735 actual_parent = g_file_get_parent (file);
737 if (actual_parent != NULL)
740 result = g_file_equal (parent, actual_parent);
744 g_object_unref (actual_parent);
754 * @file: input #GFile
755 * @name: (type filename): string containing the child's basename
757 * Gets a child of @file with basename equal to @name.
759 * Note that the file with that specific name might not exist, but
760 * you can still have a #GFile that points to it. You can use this
761 * for instance to create that file.
763 * This call does no blocking I/O.
765 * Returns: (transfer full): a #GFile to a child specified by @name.
766 * Free the returned object with g_object_unref().
769 g_file_get_child (GFile *file,
772 g_return_val_if_fail (G_IS_FILE (file), NULL);
773 g_return_val_if_fail (name != NULL, NULL);
775 return g_file_resolve_relative_path (file, name);
779 * g_file_get_child_for_display_name:
780 * @file: input #GFile
781 * @display_name: string to a possible child
782 * @error: return location for an error
784 * Gets the child of @file for a given @display_name (i.e. a UTF-8
785 * version of the name). If this function fails, it returns %NULL
786 * and @error will be set. This is very useful when constructing a
787 * #GFile for a new file and the user entered the filename in the
788 * user interface, for instance when you select a directory and
789 * type a filename in the file selector.
791 * This call does no blocking I/O.
793 * Returns: (transfer full): a #GFile to the specified child, or
794 * %NULL if the display name couldn't be converted.
795 * Free the returned object with g_object_unref().
798 g_file_get_child_for_display_name (GFile *file,
799 const char *display_name,
804 g_return_val_if_fail (G_IS_FILE (file), NULL);
805 g_return_val_if_fail (display_name != NULL, NULL);
807 iface = G_FILE_GET_IFACE (file);
809 return (* iface->get_child_for_display_name) (file, display_name, error);
814 * @file: input #GFile
815 * @prefix: input #GFile
817 * Checks whether @file has the prefix specified by @prefix.
819 * In other words, if the names of initial elements of @file's
820 * pathname match @prefix. Only full pathname elements are matched,
821 * so a path like /foo is not considered a prefix of /foobar, only
824 * A #GFile is not a prefix of itself. If you want to check for
825 * equality, use g_file_equal().
827 * This call does no I/O, as it works purely on names. As such it can
828 * sometimes return %FALSE even if @file is inside a @prefix (from a
829 * filesystem point of view), because the prefix of @file is an alias
832 * Virtual: prefix_matches
833 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
837 g_file_has_prefix (GFile *file,
842 g_return_val_if_fail (G_IS_FILE (file), FALSE);
843 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
845 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
848 iface = G_FILE_GET_IFACE (file);
850 /* The vtable function differs in arg order since
851 * we're using the old contains_file call
853 return (* iface->prefix_matches) (prefix, file);
857 * g_file_get_relative_path:
858 * @parent: input #GFile
859 * @descendant: input #GFile
861 * Gets the path for @descendant relative to @parent.
863 * This call does no blocking I/O.
865 * Returns: (type filename) (nullable): string with the relative path from
866 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
867 * prefix. The returned string should be freed with g_free() when
871 g_file_get_relative_path (GFile *parent,
876 g_return_val_if_fail (G_IS_FILE (parent), NULL);
877 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
879 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
882 iface = G_FILE_GET_IFACE (parent);
884 return (* iface->get_relative_path) (parent, descendant);
888 * g_file_resolve_relative_path:
889 * @file: input #GFile
890 * @relative_path: (type filename): a given relative path string
892 * Resolves a relative path for @file to an absolute path.
894 * This call does no blocking I/O.
896 * Returns: (transfer full): #GFile to the resolved path.
897 * %NULL if @relative_path is %NULL or if @file is invalid.
898 * Free the returned object with g_object_unref().
901 g_file_resolve_relative_path (GFile *file,
902 const char *relative_path)
906 g_return_val_if_fail (G_IS_FILE (file), NULL);
907 g_return_val_if_fail (relative_path != NULL, NULL);
909 iface = G_FILE_GET_IFACE (file);
911 return (* iface->resolve_relative_path) (file, relative_path);
915 * g_file_enumerate_children:
916 * @file: input #GFile
917 * @attributes: an attribute query string
918 * @flags: a set of #GFileQueryInfoFlags
919 * @cancellable: (nullable): optional #GCancellable object,
921 * @error: #GError for error reporting
923 * Gets the requested information about the files in a directory.
924 * The result is a #GFileEnumerator object that will give out
925 * #GFileInfo objects for all the files in the directory.
927 * The @attributes value is a string that specifies the file
928 * attributes that should be gathered. It is not an error if
929 * it's not possible to read a particular requested attribute
930 * from a file - it just won't be set. @attributes should
931 * be a comma-separated list of attributes or attribute wildcards.
932 * The wildcard "*" means all attributes, and a wildcard like
933 * "standard::*" means all attributes in the standard namespace.
934 * An example attribute query be "standard::*,owner::user".
935 * The standard attributes are available as defines, like
936 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
938 * If @cancellable is not %NULL, then the operation can be cancelled
939 * by triggering the cancellable object from another thread. If the
940 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
943 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
944 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
945 * error will be returned. Other errors are possible too.
947 * Returns: (transfer full): A #GFileEnumerator if successful,
948 * %NULL on error. Free the returned object with g_object_unref().
951 g_file_enumerate_children (GFile *file,
952 const char *attributes,
953 GFileQueryInfoFlags flags,
954 GCancellable *cancellable,
959 g_return_val_if_fail (G_IS_FILE (file), NULL);
961 if (g_cancellable_set_error_if_cancelled (cancellable, error))
964 iface = G_FILE_GET_IFACE (file);
966 if (iface->enumerate_children == NULL)
968 g_set_error_literal (error, G_IO_ERROR,
969 G_IO_ERROR_NOT_SUPPORTED,
970 _("Operation not supported"));
974 return (* iface->enumerate_children) (file, attributes, flags,
979 * g_file_enumerate_children_async:
980 * @file: input #GFile
981 * @attributes: an attribute query string
982 * @flags: a set of #GFileQueryInfoFlags
983 * @io_priority: the [I/O priority][io-priority] of the request
984 * @cancellable: (nullable): optional #GCancellable object,
986 * @callback: (scope async): a #GAsyncReadyCallback to call when the
987 * request is satisfied
988 * @user_data: (closure): the data to pass to callback function
990 * Asynchronously gets the requested information about the files
991 * in a directory. The result is a #GFileEnumerator object that will
992 * give out #GFileInfo objects for all the files in the directory.
994 * For more details, see g_file_enumerate_children() which is
995 * the synchronous version of this call.
997 * When the operation is finished, @callback will be called. You can
998 * then call g_file_enumerate_children_finish() to get the result of
1002 g_file_enumerate_children_async (GFile *file,
1003 const char *attributes,
1004 GFileQueryInfoFlags flags,
1006 GCancellable *cancellable,
1007 GAsyncReadyCallback callback,
1012 g_return_if_fail (G_IS_FILE (file));
1014 iface = G_FILE_GET_IFACE (file);
1015 (* iface->enumerate_children_async) (file,
1025 * g_file_enumerate_children_finish:
1026 * @file: input #GFile
1027 * @res: a #GAsyncResult
1030 * Finishes an async enumerate children operation.
1031 * See g_file_enumerate_children_async().
1033 * Returns: (transfer full): a #GFileEnumerator or %NULL
1034 * if an error occurred.
1035 * Free the returned object with g_object_unref().
1038 g_file_enumerate_children_finish (GFile *file,
1044 g_return_val_if_fail (G_IS_FILE (file), NULL);
1045 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1047 if (g_async_result_legacy_propagate_error (res, error))
1050 iface = G_FILE_GET_IFACE (file);
1051 return (* iface->enumerate_children_finish) (file, res, error);
1055 * g_file_query_exists:
1056 * @file: input #GFile
1057 * @cancellable: (nullable): optional #GCancellable object,
1060 * Utility function to check if a particular file exists. This is
1061 * implemented using g_file_query_info() and as such does blocking I/O.
1063 * Note that in many cases it is racy to first check for file existence
1064 * and then execute something based on the outcome of that, because the
1065 * file might have been created or removed in between the operations. The
1066 * general approach to handling that is to not check, but just do the
1067 * operation and handle the errors as they come.
1069 * As an example of race-free checking, take the case of reading a file,
1070 * and if it doesn't exist, creating it. There are two racy versions: read
1071 * it, and on error create it; and: check if it exists, if not create it.
1072 * These can both result in two processes creating the file (with perhaps
1073 * a partially written file as the result). The correct approach is to
1074 * always try to create the file with g_file_create() which will either
1075 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1077 * However, in many cases an existence check is useful in a user interface,
1078 * for instance to make a menu item sensitive/insensitive, so that you don't
1079 * have to fool users that something is possible and then just show an error
1080 * dialog. If you do this, you should make sure to also handle the errors
1081 * that can happen due to races when you execute the operation.
1083 * Returns: %TRUE if the file exists (and can be detected without error),
1084 * %FALSE otherwise (or if cancelled).
1087 g_file_query_exists (GFile *file,
1088 GCancellable *cancellable)
1092 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1094 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1095 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1098 g_object_unref (info);
1106 * g_file_query_file_type:
1107 * @file: input #GFile
1108 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1109 * @cancellable: (nullable): optional #GCancellable object,
1112 * Utility function to inspect the #GFileType of a file. This is
1113 * implemented using g_file_query_info() and as such does blocking I/O.
1115 * The primary use case of this method is to check if a file is
1116 * a regular file, directory, or symlink.
1118 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1119 * if the file does not exist
1124 g_file_query_file_type (GFile *file,
1125 GFileQueryInfoFlags flags,
1126 GCancellable *cancellable)
1129 GFileType file_type;
1131 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1132 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1136 file_type = g_file_info_get_file_type (info);
1137 g_object_unref (info);
1140 file_type = G_FILE_TYPE_UNKNOWN;
1146 * g_file_query_info:
1147 * @file: input #GFile
1148 * @attributes: an attribute query string
1149 * @flags: a set of #GFileQueryInfoFlags
1150 * @cancellable: (nullable): optional #GCancellable object,
1154 * Gets the requested information about specified @file.
1155 * The result is a #GFileInfo object that contains key-value
1156 * attributes (such as the type or size of the file).
1158 * The @attributes value is a string that specifies the file
1159 * attributes that should be gathered. It is not an error if
1160 * it's not possible to read a particular requested attribute
1161 * from a file - it just won't be set. @attributes should be a
1162 * comma-separated list of attributes or attribute wildcards.
1163 * The wildcard "*" means all attributes, and a wildcard like
1164 * "standard::*" means all attributes in the standard namespace.
1165 * An example attribute query be "standard::*,owner::user".
1166 * The standard attributes are available as defines, like
1167 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1169 * If @cancellable is not %NULL, then the operation can be cancelled
1170 * by triggering the cancellable object from another thread. If the
1171 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1174 * For symlinks, normally the information about the target of the
1175 * symlink is returned, rather than information about the symlink
1176 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1177 * in @flags the information about the symlink itself will be returned.
1178 * Also, for symlinks that point to non-existing files the information
1179 * about the symlink itself will be returned.
1181 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1182 * returned. Other errors are possible too, and depend on what kind of
1183 * filesystem the file is on.
1185 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1186 * on error. Free the returned object with g_object_unref().
1189 g_file_query_info (GFile *file,
1190 const char *attributes,
1191 GFileQueryInfoFlags flags,
1192 GCancellable *cancellable,
1197 g_return_val_if_fail (G_IS_FILE (file), NULL);
1199 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1202 iface = G_FILE_GET_IFACE (file);
1204 if (iface->query_info == NULL)
1206 g_set_error_literal (error, G_IO_ERROR,
1207 G_IO_ERROR_NOT_SUPPORTED,
1208 _("Operation not supported"));
1212 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1216 * g_file_query_info_async:
1217 * @file: input #GFile
1218 * @attributes: an attribute query string
1219 * @flags: a set of #GFileQueryInfoFlags
1220 * @io_priority: the [I/O priority][io-priority] of the request
1221 * @cancellable: (nullable): optional #GCancellable object,
1223 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1224 * request is satisfied
1225 * @user_data: (closure): the data to pass to callback function
1227 * Asynchronously gets the requested information about specified @file.
1228 * The result is a #GFileInfo object that contains key-value attributes
1229 * (such as type or size for the file).
1231 * For more details, see g_file_query_info() which is the synchronous
1232 * version of this call.
1234 * When the operation is finished, @callback will be called. You can
1235 * then call g_file_query_info_finish() to get the result of the operation.
1238 g_file_query_info_async (GFile *file,
1239 const char *attributes,
1240 GFileQueryInfoFlags flags,
1242 GCancellable *cancellable,
1243 GAsyncReadyCallback callback,
1248 g_return_if_fail (G_IS_FILE (file));
1250 iface = G_FILE_GET_IFACE (file);
1251 (* iface->query_info_async) (file,
1261 * g_file_query_info_finish:
1262 * @file: input #GFile
1263 * @res: a #GAsyncResult
1266 * Finishes an asynchronous file info query.
1267 * See g_file_query_info_async().
1269 * Returns: (transfer full): #GFileInfo for given @file
1270 * or %NULL on error. Free the returned object with
1274 g_file_query_info_finish (GFile *file,
1280 g_return_val_if_fail (G_IS_FILE (file), NULL);
1281 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1283 if (g_async_result_legacy_propagate_error (res, error))
1286 iface = G_FILE_GET_IFACE (file);
1287 return (* iface->query_info_finish) (file, res, error);
1291 * g_file_query_filesystem_info:
1292 * @file: input #GFile
1293 * @attributes: an attribute query string
1294 * @cancellable: (nullable): optional #GCancellable object,
1298 * Similar to g_file_query_info(), but obtains information
1299 * about the filesystem the @file is on, rather than the file itself.
1300 * For instance the amount of space available and the type of
1303 * The @attributes value is a string that specifies the attributes
1304 * that should be gathered. It is not an error if it's not possible
1305 * to read a particular requested attribute from a file - it just
1306 * won't be set. @attributes should be a comma-separated list of
1307 * attributes or attribute wildcards. The wildcard "*" means all
1308 * attributes, and a wildcard like "filesystem::*" means all attributes
1309 * in the filesystem namespace. The standard namespace for filesystem
1310 * attributes is "filesystem". Common attributes of interest are
1311 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1312 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1313 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1315 * If @cancellable is not %NULL, then the operation can be cancelled
1316 * by triggering the cancellable object from another thread. If the
1317 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1320 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1321 * be returned. Other errors are possible too, and depend on what
1322 * kind of filesystem the file is on.
1324 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1325 * Free the returned object with g_object_unref().
1328 g_file_query_filesystem_info (GFile *file,
1329 const char *attributes,
1330 GCancellable *cancellable,
1335 g_return_val_if_fail (G_IS_FILE (file), NULL);
1337 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1340 iface = G_FILE_GET_IFACE (file);
1342 if (iface->query_filesystem_info == NULL)
1344 g_set_error_literal (error, G_IO_ERROR,
1345 G_IO_ERROR_NOT_SUPPORTED,
1346 _("Operation not supported"));
1350 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1354 * g_file_query_filesystem_info_async:
1355 * @file: input #GFile
1356 * @attributes: an attribute query string
1357 * @io_priority: the [I/O priority][io-priority] of the request
1358 * @cancellable: (nullable): optional #GCancellable object,
1360 * @callback: (scope async): a #GAsyncReadyCallback to call
1361 * when the request is satisfied
1362 * @user_data: (closure): the data to pass to callback function
1364 * Asynchronously gets the requested information about the filesystem
1365 * that the specified @file is on. The result is a #GFileInfo object
1366 * that contains key-value attributes (such as type or size for the
1369 * For more details, see g_file_query_filesystem_info() which is the
1370 * synchronous version of this call.
1372 * When the operation is finished, @callback will be called. You can
1373 * then call g_file_query_info_finish() to get the result of the
1377 g_file_query_filesystem_info_async (GFile *file,
1378 const char *attributes,
1380 GCancellable *cancellable,
1381 GAsyncReadyCallback callback,
1386 g_return_if_fail (G_IS_FILE (file));
1388 iface = G_FILE_GET_IFACE (file);
1389 (* iface->query_filesystem_info_async) (file,
1398 * g_file_query_filesystem_info_finish:
1399 * @file: input #GFile
1400 * @res: a #GAsyncResult
1403 * Finishes an asynchronous filesystem info query.
1404 * See g_file_query_filesystem_info_async().
1406 * Returns: (transfer full): #GFileInfo for given @file
1407 * or %NULL on error.
1408 * Free the returned object with g_object_unref().
1411 g_file_query_filesystem_info_finish (GFile *file,
1417 g_return_val_if_fail (G_IS_FILE (file), NULL);
1418 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1420 if (g_async_result_legacy_propagate_error (res, error))
1423 iface = G_FILE_GET_IFACE (file);
1424 return (* iface->query_filesystem_info_finish) (file, res, error);
1428 * g_file_find_enclosing_mount:
1429 * @file: input #GFile
1430 * @cancellable: (nullable): optional #GCancellable object,
1434 * Gets a #GMount for the #GFile.
1436 * If the #GFileIface for @file does not have a mount (e.g.
1437 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1438 * and %NULL will be returned.
1440 * If @cancellable is not %NULL, then the operation can be cancelled by
1441 * triggering the cancellable object from another thread. If the operation
1442 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1444 * Returns: (transfer full): a #GMount where the @file is located
1445 * or %NULL on error.
1446 * Free the returned object with g_object_unref().
1449 g_file_find_enclosing_mount (GFile *file,
1450 GCancellable *cancellable,
1455 g_return_val_if_fail (G_IS_FILE (file), NULL);
1457 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1460 iface = G_FILE_GET_IFACE (file);
1461 if (iface->find_enclosing_mount == NULL)
1464 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1465 /* Translators: This is an error message when
1466 * trying to find the enclosing (user visible)
1467 * mount of a file, but none exists.
1469 _("Containing mount does not exist"));
1473 return (* iface->find_enclosing_mount) (file, cancellable, error);
1477 * g_file_find_enclosing_mount_async:
1479 * @io_priority: the [I/O priority][io-priority] of the request
1480 * @cancellable: (nullable): optional #GCancellable object,
1482 * @callback: (scope async): a #GAsyncReadyCallback to call
1483 * when the request is satisfied
1484 * @user_data: (closure): the data to pass to callback function
1486 * Asynchronously gets the mount for the file.
1488 * For more details, see g_file_find_enclosing_mount() which is
1489 * the synchronous version of this call.
1491 * When the operation is finished, @callback will be called.
1492 * You can then call g_file_find_enclosing_mount_finish() to
1493 * get the result of the operation.
1496 g_file_find_enclosing_mount_async (GFile *file,
1498 GCancellable *cancellable,
1499 GAsyncReadyCallback callback,
1504 g_return_if_fail (G_IS_FILE (file));
1506 iface = G_FILE_GET_IFACE (file);
1507 (* iface->find_enclosing_mount_async) (file,
1515 * g_file_find_enclosing_mount_finish:
1517 * @res: a #GAsyncResult
1520 * Finishes an asynchronous find mount request.
1521 * See g_file_find_enclosing_mount_async().
1523 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1524 * Free the returned object with g_object_unref().
1527 g_file_find_enclosing_mount_finish (GFile *file,
1533 g_return_val_if_fail (G_IS_FILE (file), NULL);
1534 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1536 if (g_async_result_legacy_propagate_error (res, error))
1539 iface = G_FILE_GET_IFACE (file);
1540 return (* iface->find_enclosing_mount_finish) (file, res, error);
1546 * @file: #GFile to read
1547 * @cancellable: (nullable): a #GCancellable
1548 * @error: a #GError, or %NULL
1550 * Opens a file for reading. The result is a #GFileInputStream that
1551 * can be used to read the contents of the file.
1553 * If @cancellable is not %NULL, then the operation can be cancelled by
1554 * triggering the cancellable object from another thread. If the operation
1555 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1557 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1558 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1559 * error will be returned. Other errors are possible too, and depend
1560 * on what kind of filesystem the file is on.
1563 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1564 * Free the returned object with g_object_unref().
1567 g_file_read (GFile *file,
1568 GCancellable *cancellable,
1573 g_return_val_if_fail (G_IS_FILE (file), NULL);
1575 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1578 iface = G_FILE_GET_IFACE (file);
1580 if (iface->read_fn == NULL)
1582 g_set_error_literal (error, G_IO_ERROR,
1583 G_IO_ERROR_NOT_SUPPORTED,
1584 _("Operation not supported"));
1588 return (* iface->read_fn) (file, cancellable, error);
1593 * @file: input #GFile
1594 * @flags: a set of #GFileCreateFlags
1595 * @cancellable: (nullable): optional #GCancellable object,
1597 * @error: a #GError, or %NULL
1599 * Gets an output stream for appending data to the file.
1600 * If the file doesn't already exist it is created.
1602 * By default files created are generally readable by everyone,
1603 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1604 * will be made readable only to the current user, to the level that
1605 * is supported on the target filesystem.
1607 * If @cancellable is not %NULL, then the operation can be cancelled
1608 * by triggering the cancellable object from another thread. If the
1609 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1612 * Some file systems don't allow all file names, and may return an
1613 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1614 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1615 * possible too, and depend on what kind of filesystem the file is on.
1617 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1618 * Free the returned object with g_object_unref().
1621 g_file_append_to (GFile *file,
1622 GFileCreateFlags flags,
1623 GCancellable *cancellable,
1628 g_return_val_if_fail (G_IS_FILE (file), NULL);
1630 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1633 iface = G_FILE_GET_IFACE (file);
1635 if (iface->append_to == NULL)
1637 g_set_error_literal (error, G_IO_ERROR,
1638 G_IO_ERROR_NOT_SUPPORTED,
1639 _("Operation not supported"));
1643 return (* iface->append_to) (file, flags, cancellable, error);
1648 * @file: input #GFile
1649 * @flags: a set of #GFileCreateFlags
1650 * @cancellable: (nullable): optional #GCancellable object,
1652 * @error: a #GError, or %NULL
1654 * Creates a new file and returns an output stream for writing to it.
1655 * The file must not already exist.
1657 * By default files created are generally readable by everyone,
1658 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1659 * will be made readable only to the current user, to the level
1660 * that is supported on the target filesystem.
1662 * If @cancellable is not %NULL, then the operation can be cancelled
1663 * by triggering the cancellable object from another thread. If the
1664 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1667 * If a file or directory with this name already exists the
1668 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1669 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1670 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1671 * be returned. Other errors are possible too, and depend on what kind
1672 * of filesystem the file is on.
1674 * Returns: (transfer full): a #GFileOutputStream for the newly created
1675 * file, or %NULL on error.
1676 * Free the returned object with g_object_unref().
1679 g_file_create (GFile *file,
1680 GFileCreateFlags flags,
1681 GCancellable *cancellable,
1686 g_return_val_if_fail (G_IS_FILE (file), NULL);
1688 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1691 iface = G_FILE_GET_IFACE (file);
1693 if (iface->create == NULL)
1695 g_set_error_literal (error, G_IO_ERROR,
1696 G_IO_ERROR_NOT_SUPPORTED,
1697 _("Operation not supported"));
1701 return (* iface->create) (file, flags, cancellable, error);
1706 * @file: input #GFile
1707 * @etag: (nullable): an optional [entity tag][gfile-etag]
1708 * for the current #GFile, or #NULL to ignore
1709 * @make_backup: %TRUE if a backup should be created
1710 * @flags: a set of #GFileCreateFlags
1711 * @cancellable: (nullable): optional #GCancellable object,
1713 * @error: a #GError, or %NULL
1715 * Returns an output stream for overwriting the file, possibly
1716 * creating a backup copy of the file first. If the file doesn't exist,
1717 * it will be created.
1719 * This will try to replace the file in the safest way possible so
1720 * that any errors during the writing will not affect an already
1721 * existing copy of the file. For instance, for local files it
1722 * may write to a temporary file and then atomically rename over
1723 * the destination when the stream is closed.
1725 * By default files created are generally readable by everyone,
1726 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1727 * will be made readable only to the current user, to the level that
1728 * is supported on the target filesystem.
1730 * If @cancellable is not %NULL, then the operation can be cancelled
1731 * by triggering the cancellable object from another thread. If the
1732 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1735 * If you pass in a non-%NULL @etag value and @file already exists, then
1736 * this value is compared to the current entity tag of the file, and if
1737 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1738 * generally means that the file has been changed since you last read
1739 * it. You can get the new etag from g_file_output_stream_get_etag()
1740 * after you've finished writing and closed the #GFileOutputStream. When
1741 * you load a new file you can use g_file_input_stream_query_info() to
1742 * get the etag of the file.
1744 * If @make_backup is %TRUE, this function will attempt to make a
1745 * backup of the current file before overwriting it. If this fails
1746 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1747 * want to replace anyway, try again with @make_backup set to %FALSE.
1749 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1750 * be returned, and if the file is some other form of non-regular file
1751 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1752 * file systems don't allow all file names, and may return an
1753 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1754 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1755 * possible too, and depend on what kind of filesystem the file is on.
1757 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1758 * Free the returned object with g_object_unref().
1761 g_file_replace (GFile *file,
1763 gboolean make_backup,
1764 GFileCreateFlags flags,
1765 GCancellable *cancellable,
1770 g_return_val_if_fail (G_IS_FILE (file), NULL);
1772 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1775 iface = G_FILE_GET_IFACE (file);
1777 if (iface->replace == NULL)
1779 g_set_error_literal (error, G_IO_ERROR,
1780 G_IO_ERROR_NOT_SUPPORTED,
1781 _("Operation not supported"));
1785 /* Handle empty tag string as NULL in consistent way. */
1786 if (etag && *etag == 0)
1789 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1793 * g_file_open_readwrite:
1794 * @file: #GFile to open
1795 * @cancellable: (nullable): a #GCancellable
1796 * @error: a #GError, or %NULL
1798 * Opens an existing file for reading and writing. The result is
1799 * a #GFileIOStream that can be used to read and write the contents
1802 * If @cancellable is not %NULL, then the operation can be cancelled
1803 * by triggering the cancellable object from another thread. If the
1804 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1807 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1808 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1809 * error will be returned. Other errors are possible too, and depend on
1810 * what kind of filesystem the file is on. Note that in many non-local
1811 * file cases read and write streams are not supported, so make sure you
1812 * really need to do read and write streaming, rather than just opening
1813 * for reading or writing.
1815 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1816 * Free the returned object with g_object_unref().
1821 g_file_open_readwrite (GFile *file,
1822 GCancellable *cancellable,
1827 g_return_val_if_fail (G_IS_FILE (file), NULL);
1829 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1832 iface = G_FILE_GET_IFACE (file);
1834 if (iface->open_readwrite == NULL)
1836 g_set_error_literal (error, G_IO_ERROR,
1837 G_IO_ERROR_NOT_SUPPORTED,
1838 _("Operation not supported"));
1842 return (* iface->open_readwrite) (file, cancellable, error);
1846 * g_file_create_readwrite:
1848 * @flags: a set of #GFileCreateFlags
1849 * @cancellable: (nullable): optional #GCancellable object,
1851 * @error: return location for a #GError, or %NULL
1853 * Creates a new file and returns a stream for reading and
1854 * writing to it. The file must not already exist.
1856 * By default files created are generally readable by everyone,
1857 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1858 * will be made readable only to the current user, to the level
1859 * that is supported on the target filesystem.
1861 * If @cancellable is not %NULL, then the operation can be cancelled
1862 * by triggering the cancellable object from another thread. If the
1863 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1866 * If a file or directory with this name already exists, the
1867 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1868 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1869 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1870 * will be returned. Other errors are possible too, and depend on what
1871 * kind of filesystem the file is on.
1873 * Note that in many non-local file cases read and write streams are
1874 * not supported, so make sure you really need to do read and write
1875 * streaming, rather than just opening for reading or writing.
1877 * Returns: (transfer full): a #GFileIOStream for the newly created
1878 * file, or %NULL on error.
1879 * Free the returned object with g_object_unref().
1884 g_file_create_readwrite (GFile *file,
1885 GFileCreateFlags flags,
1886 GCancellable *cancellable,
1891 g_return_val_if_fail (G_IS_FILE (file), NULL);
1893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1896 iface = G_FILE_GET_IFACE (file);
1898 if (iface->create_readwrite == NULL)
1900 g_set_error_literal (error, G_IO_ERROR,
1901 G_IO_ERROR_NOT_SUPPORTED,
1902 _("Operation not supported"));
1906 return (* iface->create_readwrite) (file, flags, cancellable, error);
1910 * g_file_replace_readwrite:
1912 * @etag: (nullable): an optional [entity tag][gfile-etag]
1913 * for the current #GFile, or #NULL to ignore
1914 * @make_backup: %TRUE if a backup should be created
1915 * @flags: a set of #GFileCreateFlags
1916 * @cancellable: (nullable): optional #GCancellable object,
1918 * @error: return location for a #GError, or %NULL
1920 * Returns an output stream for overwriting the file in readwrite mode,
1921 * possibly creating a backup copy of the file first. If the file doesn't
1922 * exist, it will be created.
1924 * For details about the behaviour, see g_file_replace() which does the
1925 * same thing but returns an output stream only.
1927 * Note that in many non-local file cases read and write streams are not
1928 * supported, so make sure you really need to do read and write streaming,
1929 * rather than just opening for reading or writing.
1931 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1932 * Free the returned object with g_object_unref().
1937 g_file_replace_readwrite (GFile *file,
1939 gboolean make_backup,
1940 GFileCreateFlags flags,
1941 GCancellable *cancellable,
1946 g_return_val_if_fail (G_IS_FILE (file), NULL);
1948 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1951 iface = G_FILE_GET_IFACE (file);
1953 if (iface->replace_readwrite == NULL)
1955 g_set_error_literal (error, G_IO_ERROR,
1956 G_IO_ERROR_NOT_SUPPORTED,
1957 _("Operation not supported"));
1961 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1965 * g_file_read_async:
1966 * @file: input #GFile
1967 * @io_priority: the [I/O priority][io-priority] of the request
1968 * @cancellable: (nullable): optional #GCancellable object,
1970 * @callback: (scope async): a #GAsyncReadyCallback to call
1971 * when the request is satisfied
1972 * @user_data: (closure): the data to pass to callback function
1974 * Asynchronously opens @file for reading.
1976 * For more details, see g_file_read() which is
1977 * the synchronous version of this call.
1979 * When the operation is finished, @callback will be called.
1980 * You can then call g_file_read_finish() to get the result
1984 g_file_read_async (GFile *file,
1986 GCancellable *cancellable,
1987 GAsyncReadyCallback callback,
1992 g_return_if_fail (G_IS_FILE (file));
1994 iface = G_FILE_GET_IFACE (file);
1995 (* iface->read_async) (file,
2003 * g_file_read_finish:
2004 * @file: input #GFile
2005 * @res: a #GAsyncResult
2006 * @error: a #GError, or %NULL
2008 * Finishes an asynchronous file read operation started with
2009 * g_file_read_async().
2011 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2012 * Free the returned object with g_object_unref().
2015 g_file_read_finish (GFile *file,
2021 g_return_val_if_fail (G_IS_FILE (file), NULL);
2022 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2024 if (g_async_result_legacy_propagate_error (res, error))
2027 iface = G_FILE_GET_IFACE (file);
2028 return (* iface->read_finish) (file, res, error);
2032 * g_file_append_to_async:
2033 * @file: input #GFile
2034 * @flags: a set of #GFileCreateFlags
2035 * @io_priority: the [I/O priority][io-priority] of the request
2036 * @cancellable: (nullable): optional #GCancellable object,
2038 * @callback: (scope async): a #GAsyncReadyCallback to call
2039 * when the request is satisfied
2040 * @user_data: (closure): the data to pass to callback function
2042 * Asynchronously opens @file for appending.
2044 * For more details, see g_file_append_to() which is
2045 * the synchronous version of this call.
2047 * When the operation is finished, @callback will be called.
2048 * You can then call g_file_append_to_finish() to get the result
2052 g_file_append_to_async (GFile *file,
2053 GFileCreateFlags flags,
2055 GCancellable *cancellable,
2056 GAsyncReadyCallback callback,
2061 g_return_if_fail (G_IS_FILE (file));
2063 iface = G_FILE_GET_IFACE (file);
2064 (* iface->append_to_async) (file,
2073 * g_file_append_to_finish:
2074 * @file: input #GFile
2075 * @res: #GAsyncResult
2076 * @error: a #GError, or %NULL
2078 * Finishes an asynchronous file append operation started with
2079 * g_file_append_to_async().
2081 * Returns: (transfer full): a valid #GFileOutputStream
2082 * or %NULL on error.
2083 * Free the returned object with g_object_unref().
2086 g_file_append_to_finish (GFile *file,
2092 g_return_val_if_fail (G_IS_FILE (file), NULL);
2093 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2095 if (g_async_result_legacy_propagate_error (res, error))
2098 iface = G_FILE_GET_IFACE (file);
2099 return (* iface->append_to_finish) (file, res, error);
2103 * g_file_create_async:
2104 * @file: input #GFile
2105 * @flags: a set of #GFileCreateFlags
2106 * @io_priority: the [I/O priority][io-priority] of the request
2107 * @cancellable: (nullable): optional #GCancellable object,
2109 * @callback: (scope async): a #GAsyncReadyCallback to call
2110 * when the request is satisfied
2111 * @user_data: (closure): the data to pass to callback function
2113 * Asynchronously creates a new file and returns an output stream
2114 * for writing to it. The file must not already exist.
2116 * For more details, see g_file_create() which is
2117 * the synchronous version of this call.
2119 * When the operation is finished, @callback will be called.
2120 * You can then call g_file_create_finish() to get the result
2124 g_file_create_async (GFile *file,
2125 GFileCreateFlags flags,
2127 GCancellable *cancellable,
2128 GAsyncReadyCallback callback,
2133 g_return_if_fail (G_IS_FILE (file));
2135 iface = G_FILE_GET_IFACE (file);
2136 (* iface->create_async) (file,
2145 * g_file_create_finish:
2146 * @file: input #GFile
2147 * @res: a #GAsyncResult
2148 * @error: a #GError, or %NULL
2150 * Finishes an asynchronous file create operation started with
2151 * g_file_create_async().
2153 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2154 * Free the returned object with g_object_unref().
2157 g_file_create_finish (GFile *file,
2163 g_return_val_if_fail (G_IS_FILE (file), NULL);
2164 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2166 if (g_async_result_legacy_propagate_error (res, error))
2169 iface = G_FILE_GET_IFACE (file);
2170 return (* iface->create_finish) (file, res, error);
2174 * g_file_replace_async:
2175 * @file: input #GFile
2176 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2177 * or %NULL to ignore
2178 * @make_backup: %TRUE if a backup should be created
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 overwrites the file, replacing the contents,
2188 * possibly creating a backup copy of the file first.
2190 * For more details, see g_file_replace() 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_replace_finish() to get the result
2198 g_file_replace_async (GFile *file,
2200 gboolean make_backup,
2201 GFileCreateFlags flags,
2203 GCancellable *cancellable,
2204 GAsyncReadyCallback callback,
2209 g_return_if_fail (G_IS_FILE (file));
2211 iface = G_FILE_GET_IFACE (file);
2212 (* iface->replace_async) (file,
2223 * g_file_replace_finish:
2224 * @file: input #GFile
2225 * @res: a #GAsyncResult
2226 * @error: a #GError, or %NULL
2228 * Finishes an asynchronous file replace operation started with
2229 * g_file_replace_async().
2231 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2232 * Free the returned object with g_object_unref().
2235 g_file_replace_finish (GFile *file,
2241 g_return_val_if_fail (G_IS_FILE (file), NULL);
2242 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2244 if (g_async_result_legacy_propagate_error (res, error))
2247 iface = G_FILE_GET_IFACE (file);
2248 return (* iface->replace_finish) (file, res, error);
2252 * g_file_open_readwrite_async
2253 * @file: input #GFile
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 opens @file for reading and writing.
2263 * For more details, see g_file_open_readwrite() which is
2264 * the synchronous version of this call.
2266 * When the operation is finished, @callback will be called.
2267 * You can then call g_file_open_readwrite_finish() to get
2268 * the result of the operation.
2273 g_file_open_readwrite_async (GFile *file,
2275 GCancellable *cancellable,
2276 GAsyncReadyCallback callback,
2281 g_return_if_fail (G_IS_FILE (file));
2283 iface = G_FILE_GET_IFACE (file);
2284 (* iface->open_readwrite_async) (file,
2292 * g_file_open_readwrite_finish:
2293 * @file: input #GFile
2294 * @res: a #GAsyncResult
2295 * @error: a #GError, or %NULL
2297 * Finishes an asynchronous file read operation started with
2298 * g_file_open_readwrite_async().
2300 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2301 * Free the returned object with g_object_unref().
2306 g_file_open_readwrite_finish (GFile *file,
2312 g_return_val_if_fail (G_IS_FILE (file), NULL);
2313 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2315 if (g_async_result_legacy_propagate_error (res, error))
2318 iface = G_FILE_GET_IFACE (file);
2319 return (* iface->open_readwrite_finish) (file, res, error);
2323 * g_file_create_readwrite_async:
2324 * @file: input #GFile
2325 * @flags: a set of #GFileCreateFlags
2326 * @io_priority: the [I/O priority][io-priority] of the request
2327 * @cancellable: (nullable): optional #GCancellable object,
2329 * @callback: (scope async): a #GAsyncReadyCallback to call
2330 * when the request is satisfied
2331 * @user_data: (closure): the data to pass to callback function
2333 * Asynchronously creates a new file and returns a stream
2334 * for reading and writing to it. The file must not already exist.
2336 * For more details, see g_file_create_readwrite() which is
2337 * the synchronous version of this call.
2339 * When the operation is finished, @callback will be called.
2340 * You can then call g_file_create_readwrite_finish() to get
2341 * the result of the operation.
2346 g_file_create_readwrite_async (GFile *file,
2347 GFileCreateFlags flags,
2349 GCancellable *cancellable,
2350 GAsyncReadyCallback callback,
2355 g_return_if_fail (G_IS_FILE (file));
2357 iface = G_FILE_GET_IFACE (file);
2358 (* iface->create_readwrite_async) (file,
2367 * g_file_create_readwrite_finish:
2368 * @file: input #GFile
2369 * @res: a #GAsyncResult
2370 * @error: a #GError, or %NULL
2372 * Finishes an asynchronous file create operation started with
2373 * g_file_create_readwrite_async().
2375 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2376 * Free the returned object with g_object_unref().
2381 g_file_create_readwrite_finish (GFile *file,
2387 g_return_val_if_fail (G_IS_FILE (file), NULL);
2388 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2390 if (g_async_result_legacy_propagate_error (res, error))
2393 iface = G_FILE_GET_IFACE (file);
2394 return (* iface->create_readwrite_finish) (file, res, error);
2398 * g_file_replace_readwrite_async:
2399 * @file: input #GFile
2400 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2401 * or %NULL to ignore
2402 * @make_backup: %TRUE if a backup should be created
2403 * @flags: a set of #GFileCreateFlags
2404 * @io_priority: the [I/O priority][io-priority] of the request
2405 * @cancellable: (nullable): optional #GCancellable object,
2407 * @callback: (scope async): a #GAsyncReadyCallback to call
2408 * when the request is satisfied
2409 * @user_data: (closure): the data to pass to callback function
2411 * Asynchronously overwrites the file in read-write mode,
2412 * replacing the contents, possibly creating a backup copy
2413 * of the file first.
2415 * For more details, see g_file_replace_readwrite() which is
2416 * the synchronous version of this call.
2418 * When the operation is finished, @callback will be called.
2419 * You can then call g_file_replace_readwrite_finish() to get
2420 * the result of the operation.
2425 g_file_replace_readwrite_async (GFile *file,
2427 gboolean make_backup,
2428 GFileCreateFlags flags,
2430 GCancellable *cancellable,
2431 GAsyncReadyCallback callback,
2436 g_return_if_fail (G_IS_FILE (file));
2438 iface = G_FILE_GET_IFACE (file);
2439 (* iface->replace_readwrite_async) (file,
2450 * g_file_replace_readwrite_finish:
2451 * @file: input #GFile
2452 * @res: a #GAsyncResult
2453 * @error: a #GError, or %NULL
2455 * Finishes an asynchronous file replace operation started with
2456 * g_file_replace_readwrite_async().
2458 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2459 * Free the returned object with g_object_unref().
2464 g_file_replace_readwrite_finish (GFile *file,
2470 g_return_val_if_fail (G_IS_FILE (file), NULL);
2471 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2473 if (g_async_result_legacy_propagate_error (res, error))
2476 iface = G_FILE_GET_IFACE (file);
2477 return (* iface->replace_readwrite_finish) (file, res, error);
2481 copy_symlink (GFile *destination,
2482 GFileCopyFlags flags,
2483 GCancellable *cancellable,
2488 gboolean tried_delete;
2490 GFileType file_type;
2492 tried_delete = FALSE;
2496 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2498 /* Maybe it already existed, and we want to overwrite? */
2499 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2500 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2502 g_clear_error (&my_error);
2504 /* Don't overwrite if the destination is a directory */
2505 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2506 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2507 cancellable, &my_error);
2510 file_type = g_file_info_get_file_type (info);
2511 g_object_unref (info);
2513 if (file_type == G_FILE_TYPE_DIRECTORY)
2515 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2516 _("Can’t copy over directory"));
2521 if (!g_file_delete (destination, cancellable, error))
2524 tried_delete = TRUE;
2528 g_propagate_error (error, my_error);
2535 static GFileInputStream *
2536 open_source_for_copy (GFile *source,
2538 GFileCopyFlags flags,
2539 GCancellable *cancellable,
2543 GFileInputStream *ret;
2545 GFileType file_type;
2548 ret = g_file_read (source, cancellable, &my_error);
2552 /* There was an error opening the source, try to set a good error for it: */
2553 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2555 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2556 * as that is less useful to the app. Better check for errors on the
2559 g_error_free (my_error);
2562 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2563 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2564 cancellable, &my_error);
2566 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2568 file_type = g_file_info_get_file_type (info);
2569 g_object_unref (info);
2571 if (flags & G_FILE_COPY_OVERWRITE)
2573 if (file_type == G_FILE_TYPE_DIRECTORY)
2575 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2576 _("Can’t copy directory over directory"));
2579 /* continue to would_recurse error */
2583 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2584 _("Target file exists"));
2590 /* Error getting info from target, return that error
2591 * (except for NOT_FOUND, which is no error here)
2593 g_clear_object (&info);
2594 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2596 g_propagate_error (error, my_error);
2599 g_clear_error (&my_error);
2602 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2603 _("Can’t recursively copy directory"));
2607 g_propagate_error (error, my_error);
2612 should_copy (GFileAttributeInfo *info,
2613 gboolean copy_all_attributes,
2614 gboolean skip_perms)
2616 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2619 if (copy_all_attributes)
2620 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2621 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2625 build_attribute_list_for_copy (GFile *file,
2626 GFileCopyFlags flags,
2627 char **out_attributes,
2628 GCancellable *cancellable,
2631 gboolean ret = FALSE;
2632 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2636 gboolean copy_all_attributes;
2637 gboolean skip_perms;
2639 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2640 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2642 /* Ignore errors here, if the target supports no attributes there is
2643 * nothing to copy. We still honor the cancellable though.
2645 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2646 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2649 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2650 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2653 if (attributes == NULL && namespaces == NULL)
2657 s = g_string_new ("");
2661 for (i = 0; i < attributes->n_infos; i++)
2663 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2668 g_string_append_c (s, ',');
2670 g_string_append (s, attributes->infos[i].name);
2677 for (i = 0; i < namespaces->n_infos; i++)
2679 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2684 g_string_append_c (s, ',');
2686 g_string_append (s, namespaces->infos[i].name);
2687 g_string_append (s, "::*");
2693 *out_attributes = g_string_free (s, FALSE);
2697 g_string_free (s, TRUE);
2699 g_file_attribute_info_list_unref (attributes);
2701 g_file_attribute_info_list_unref (namespaces);
2707 * g_file_copy_attributes:
2708 * @source: a #GFile with attributes
2709 * @destination: a #GFile to copy attributes to
2710 * @flags: a set of #GFileCopyFlags
2711 * @cancellable: (nullable): optional #GCancellable object,
2713 * @error: a #GError, %NULL to ignore
2715 * Copies the file attributes from @source to @destination.
2717 * Normally only a subset of the file attributes are copied,
2718 * those that are copies in a normal file copy operation
2719 * (which for instance does not include e.g. owner). However
2720 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2721 * all the metadata that is possible to copy is copied. This
2722 * is useful when implementing move by copy + delete source.
2724 * Returns: %TRUE if the attributes were copied successfully,
2728 g_file_copy_attributes (GFile *source,
2730 GFileCopyFlags flags,
2731 GCancellable *cancellable,
2734 char *attrs_to_read;
2737 gboolean source_nofollow_symlinks;
2739 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2740 cancellable, error))
2743 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2745 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2746 * we just don't copy it.
2748 info = g_file_query_info (source, attrs_to_read,
2749 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2753 g_free (attrs_to_read);
2758 res = g_file_set_attributes_from_info (destination,
2760 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2763 g_object_unref (info);
2769 /* 256k minus malloc overhead */
2770 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2773 copy_stream_with_progress (GInputStream *in,
2776 GCancellable *cancellable,
2777 GFileProgressCallback progress_callback,
2778 gpointer progress_callback_data,
2783 goffset current_size;
2790 /* avoid performance impact of querying total size when it's not needed */
2791 if (progress_callback)
2793 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2794 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2798 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2799 total_size = g_file_info_get_size (info);
2800 g_object_unref (info);
2803 if (total_size == -1)
2805 info = g_file_query_info (source,
2806 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2807 G_FILE_QUERY_INFO_NONE,
2811 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2812 total_size = g_file_info_get_size (info);
2813 g_object_unref (info);
2818 if (total_size == -1)
2821 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2826 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2836 current_size += n_read;
2838 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2842 if (progress_callback)
2843 progress_callback (current_size, total_size, progress_callback_data);
2847 /* Make sure we send full copied size */
2848 if (progress_callback)
2849 progress_callback (current_size, total_size, progress_callback_data);
2857 do_splice (int fd_in,
2862 long *bytes_transferd,
2868 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2876 else if (errsv == ENOSYS || errsv == EINVAL)
2877 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2878 _("Splice not supported"));
2880 g_set_error (error, G_IO_ERROR,
2881 g_io_error_from_errno (errsv),
2882 _("Error splicing file: %s"),
2883 g_strerror (errsv));
2888 *bytes_transferd = result;
2893 splice_stream_with_progress (GInputStream *in,
2895 GCancellable *cancellable,
2896 GFileProgressCallback progress_callback,
2897 gpointer progress_callback_data,
2900 int buffer[2] = { -1, -1 };
2907 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2908 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2910 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2914 /* avoid performance impact of querying total size when it's not needed */
2915 if (progress_callback)
2919 if (fstat (fd_in, &sbuf) == 0)
2920 total_size = sbuf.st_size;
2923 if (total_size == -1)
2926 offset_in = offset_out = 0;
2933 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2936 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2947 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2950 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2953 n_read -= n_written;
2956 if (progress_callback)
2957 progress_callback (offset_in, total_size, progress_callback_data);
2960 /* Make sure we send full copied size */
2961 if (progress_callback)
2962 progress_callback (offset_in, total_size, progress_callback_data);
2964 if (!g_close (buffer[0], error))
2967 if (!g_close (buffer[1], error))
2971 if (buffer[0] != -1)
2972 (void) g_close (buffer[0], NULL);
2973 if (buffer[1] != -1)
2974 (void) g_close (buffer[1], NULL);
2982 btrfs_reflink_with_progress (GInputStream *in,
2985 GCancellable *cancellable,
2986 GFileProgressCallback progress_callback,
2987 gpointer progress_callback_data,
2990 goffset source_size;
2994 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2995 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2997 if (progress_callback)
2998 source_size = g_file_info_get_size (info);
3000 /* Btrfs clone ioctl properties:
3001 * - Works at the inode level
3002 * - Doesn't work with directories
3003 * - Always follows symlinks (source and destination)
3005 * By the time we get here, *in and *out are both regular files */
3006 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3012 g_set_error_literal (error, G_IO_ERROR,
3013 G_IO_ERROR_NOT_SUPPORTED,
3014 _("Copy (reflink/clone) between mounts is not supported"));
3015 else if (errsv == EINVAL)
3016 g_set_error_literal (error, G_IO_ERROR,
3017 G_IO_ERROR_NOT_SUPPORTED,
3018 _("Copy (reflink/clone) is not supported or invalid"));
3020 /* Most probably something odd happened; retry with fallback */
3021 g_set_error_literal (error, G_IO_ERROR,
3022 G_IO_ERROR_NOT_SUPPORTED,
3023 _("Copy (reflink/clone) is not supported or didn’t work"));
3024 /* We retry with fallback for all error cases because Btrfs is currently
3025 * unstable, and so we can't trust it to do clone properly.
3026 * In addition, any hard errors here would cause the same failure in the
3027 * fallback manual copy as well. */
3031 /* Make sure we send full copied size */
3032 if (progress_callback)
3033 progress_callback (source_size, source_size, progress_callback_data);
3040 file_copy_fallback (GFile *source,
3042 GFileCopyFlags flags,
3043 GCancellable *cancellable,
3044 GFileProgressCallback progress_callback,
3045 gpointer progress_callback_data,
3048 gboolean ret = FALSE;
3049 GFileInputStream *file_in = NULL;
3050 GInputStream *in = NULL;
3051 GOutputStream *out = NULL;
3052 GFileInfo *info = NULL;
3054 char *attrs_to_read;
3055 gboolean do_set_attributes = FALSE;
3057 /* need to know the file type */
3058 info = g_file_query_info (source,
3059 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3060 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3066 /* Maybe copy the symlink? */
3067 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3068 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3070 target = g_file_info_get_symlink_target (info);
3073 if (!copy_symlink (destination, flags, cancellable, target, error))
3079 /* ... else fall back on a regular file copy */
3081 /* Handle "special" files (pipes, device nodes, ...)? */
3082 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3084 /* FIXME: could try to recreate device nodes and others? */
3085 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3086 _("Can’t copy special file"));
3090 /* Everything else should just fall back on a regular copy. */
3092 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3095 in = G_INPUT_STREAM (file_in);
3097 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3098 cancellable, error))
3101 if (attrs_to_read != NULL)
3103 GError *tmp_error = NULL;
3105 /* Ok, ditch the previous lightweight info (on Unix we just
3106 * called lstat()); at this point we gather all the information
3107 * we need about the source from the opened file descriptor.
3109 g_object_unref (info);
3111 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3112 cancellable, &tmp_error);
3115 /* Not all gvfs backends implement query_info_on_read(), we
3116 * can just fall back to the pathname again.
3117 * https://bugzilla.gnome.org/706254
3119 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3121 g_clear_error (&tmp_error);
3122 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3123 cancellable, error);
3127 g_free (attrs_to_read);
3128 g_propagate_error (error, tmp_error);
3132 g_free (attrs_to_read);
3136 do_set_attributes = TRUE;
3139 /* In the local file path, we pass down the source info which
3140 * includes things like unix::mode, to ensure that the target file
3141 * is not created with different permissions from the source file.
3143 * If a future API like g_file_replace_with_info() is added, switch
3144 * this code to use that.
3146 if (G_IS_LOCAL_FILE (destination))
3148 if (flags & G_FILE_COPY_OVERWRITE)
3149 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3151 flags & G_FILE_COPY_BACKUP,
3152 G_FILE_CREATE_REPLACE_DESTINATION,
3154 cancellable, error);
3156 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3158 cancellable, error);
3160 else if (flags & G_FILE_COPY_OVERWRITE)
3162 out = (GOutputStream *)g_file_replace (destination,
3164 flags & G_FILE_COPY_BACKUP,
3165 G_FILE_CREATE_REPLACE_DESTINATION,
3166 cancellable, error);
3170 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3177 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3179 GError *reflink_err = NULL;
3181 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3182 progress_callback, progress_callback_data,
3185 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3187 g_clear_error (&reflink_err);
3191 g_propagate_error (error, reflink_err);
3204 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3206 GError *splice_err = NULL;
3208 if (!splice_stream_with_progress (in, out, cancellable,
3209 progress_callback, progress_callback_data,
3212 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3214 g_clear_error (&splice_err);
3218 g_propagate_error (error, splice_err);
3231 /* A plain read/write loop */
3232 if (!copy_stream_with_progress (in, out, source, cancellable,
3233 progress_callback, progress_callback_data,
3241 /* Don't care about errors in source here */
3242 (void) g_input_stream_close (in, cancellable, NULL);
3243 g_object_unref (in);
3248 /* But write errors on close are bad! */
3249 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3251 g_object_unref (out);
3254 /* Ignore errors here. Failure to copy metadata is not a hard error */
3255 /* TODO: set these attributes /before/ we do the rename() on Unix */
3256 if (ret && do_set_attributes)
3258 g_file_set_attributes_from_info (destination,
3260 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3265 g_clear_object (&info);
3272 * @source: input #GFile
3273 * @destination: destination #GFile
3274 * @flags: set of #GFileCopyFlags
3275 * @cancellable: (nullable): optional #GCancellable object,
3277 * @progress_callback: (nullable) (scope call): function to callback with
3278 * progress information, or %NULL if progress information is not needed
3279 * @progress_callback_data: (closure): user data to pass to @progress_callback
3280 * @error: #GError to set on error, or %NULL
3282 * Copies the file @source to the location specified by @destination.
3283 * Can not handle recursive copies of directories.
3285 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3286 * existing @destination file is overwritten.
3288 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3289 * will be copied as symlinks, otherwise the target of the
3290 * @source symlink will be copied.
3292 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3293 * that is possible to copy is copied, not just the default subset (which,
3294 * for instance, does not include the owner, see #GFileInfo).
3296 * If @cancellable is not %NULL, then the operation can be cancelled by
3297 * triggering the cancellable object from another thread. If the operation
3298 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3300 * If @progress_callback is not %NULL, then the operation can be monitored
3301 * by setting this to a #GFileProgressCallback function.
3302 * @progress_callback_data will be passed to this function. It is guaranteed
3303 * that this callback will be called after all data has been transferred with
3304 * the total number of bytes copied during the operation.
3306 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3307 * is returned, independent on the status of the @destination.
3309 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3310 * the error %G_IO_ERROR_EXISTS is returned.
3312 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3313 * error is returned. If trying to overwrite a directory with a directory the
3314 * %G_IO_ERROR_WOULD_MERGE error is returned.
3316 * If the source is a directory and the target does not exist, or
3317 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3318 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3320 * If you are interested in copying the #GFile object itself (not the on-disk
3321 * file), see g_file_dup().
3323 * Returns: %TRUE on success, %FALSE otherwise.
3326 g_file_copy (GFile *source,
3328 GFileCopyFlags flags,
3329 GCancellable *cancellable,
3330 GFileProgressCallback progress_callback,
3331 gpointer progress_callback_data,
3338 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3339 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3341 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3344 iface = G_FILE_GET_IFACE (destination);
3348 res = (* iface->copy) (source, destination,
3350 progress_callback, progress_callback_data,
3356 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3358 g_propagate_error (error, my_error);
3362 g_clear_error (&my_error);
3365 /* If the types are different, and the destination method failed
3366 * also try the source method
3368 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3370 iface = G_FILE_GET_IFACE (source);
3375 res = (* iface->copy) (source, destination,
3377 progress_callback, progress_callback_data,
3383 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3385 g_propagate_error (error, my_error);
3389 g_clear_error (&my_error);
3393 return file_copy_fallback (source, destination, flags, cancellable,
3394 progress_callback, progress_callback_data,
3399 * g_file_copy_async:
3400 * @source: input #GFile
3401 * @destination: destination #GFile
3402 * @flags: set of #GFileCopyFlags
3403 * @io_priority: the [I/O priority][io-priority] of the request
3404 * @cancellable: (nullable): optional #GCancellable object,
3406 * @progress_callback: (nullable) (scope notified): function to callback with progress
3407 * information, or %NULL if progress information is not needed
3408 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3409 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3410 * @user_data: (closure callback): the data to pass to callback function
3412 * Copies the file @source to the location specified by @destination
3413 * asynchronously. For details of the behaviour, see g_file_copy().
3415 * If @progress_callback is not %NULL, then that function that will be called
3416 * just like in g_file_copy(). The callback will run in the default main context
3417 * of the thread calling g_file_copy_async() — the same context as @callback is
3420 * When the operation is finished, @callback will be called. You can then call
3421 * g_file_copy_finish() to get the result of the operation.
3424 g_file_copy_async (GFile *source,
3426 GFileCopyFlags flags,
3428 GCancellable *cancellable,
3429 GFileProgressCallback progress_callback,
3430 gpointer progress_callback_data,
3431 GAsyncReadyCallback callback,
3436 g_return_if_fail (G_IS_FILE (source));
3437 g_return_if_fail (G_IS_FILE (destination));
3439 iface = G_FILE_GET_IFACE (source);
3440 (* iface->copy_async) (source,
3446 progress_callback_data,
3452 * g_file_copy_finish:
3453 * @file: input #GFile
3454 * @res: a #GAsyncResult
3455 * @error: a #GError, or %NULL
3457 * Finishes copying the file started with g_file_copy_async().
3459 * Returns: a %TRUE on success, %FALSE on error.
3462 g_file_copy_finish (GFile *file,
3468 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3469 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3471 if (g_async_result_legacy_propagate_error (res, error))
3474 iface = G_FILE_GET_IFACE (file);
3475 return (* iface->copy_finish) (file, res, error);
3480 * @source: #GFile pointing to the source location
3481 * @destination: #GFile pointing to the destination location
3482 * @flags: set of #GFileCopyFlags
3483 * @cancellable: (nullable): optional #GCancellable object,
3485 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3486 * function for updates
3487 * @progress_callback_data: (closure): gpointer to user data for
3488 * the callback function
3489 * @error: #GError for returning error conditions, or %NULL
3491 * Tries to move the file or directory @source to the location specified
3492 * by @destination. If native move operations are supported then this is
3493 * used, otherwise a copy + delete fallback is used. The native
3494 * implementation may support moving directories (for instance on moves
3495 * inside the same filesystem), but the fallback code does not.
3497 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3498 * existing @destination file is overwritten.
3500 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3501 * will be copied as symlinks, otherwise the target of the
3502 * @source symlink will be copied.
3504 * If @cancellable is not %NULL, then the operation can be cancelled by
3505 * triggering the cancellable object from another thread. If the operation
3506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3508 * If @progress_callback is not %NULL, then the operation can be monitored
3509 * by setting this to a #GFileProgressCallback function.
3510 * @progress_callback_data will be passed to this function. It is
3511 * guaranteed that this callback will be called after all data has been
3512 * transferred with the total number of bytes copied during the operation.
3514 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3515 * error is returned, independent on the status of the @destination.
3517 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3518 * then the error %G_IO_ERROR_EXISTS is returned.
3520 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3521 * error is returned. If trying to overwrite a directory with a directory the
3522 * %G_IO_ERROR_WOULD_MERGE error is returned.
3524 * If the source is a directory and the target does not exist, or
3525 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3526 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3527 * move operation isn't available).
3529 * Returns: %TRUE on successful move, %FALSE otherwise.
3532 g_file_move (GFile *source,
3534 GFileCopyFlags flags,
3535 GCancellable *cancellable,
3536 GFileProgressCallback progress_callback,
3537 gpointer progress_callback_data,
3544 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3545 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3547 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3550 iface = G_FILE_GET_IFACE (destination);
3554 res = (* iface->move) (source, destination,
3556 progress_callback, progress_callback_data,
3562 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3564 g_propagate_error (error, my_error);
3568 g_clear_error (&my_error);
3571 /* If the types are different, and the destination method failed
3572 * also try the source method
3574 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3576 iface = G_FILE_GET_IFACE (source);
3581 res = (* iface->move) (source, destination,
3583 progress_callback, progress_callback_data,
3589 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3591 g_propagate_error (error, my_error);
3595 g_clear_error (&my_error);
3599 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3601 g_set_error_literal (error, G_IO_ERROR,
3602 G_IO_ERROR_NOT_SUPPORTED,
3603 _("Operation not supported"));
3607 flags |= G_FILE_COPY_ALL_METADATA;
3608 if (!g_file_copy (source, destination, flags, cancellable,
3609 progress_callback, progress_callback_data,
3613 return g_file_delete (source, cancellable, error);
3617 * g_file_make_directory:
3618 * @file: input #GFile
3619 * @cancellable: (nullable): optional #GCancellable object,
3621 * @error: a #GError, or %NULL
3623 * Creates a directory. Note that this will only create a child directory
3624 * of the immediate parent directory of the path or URI given by the #GFile.
3625 * To recursively create directories, see g_file_make_directory_with_parents().
3626 * This function will fail if the parent directory does not exist, setting
3627 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3628 * creating directories, this function will fail, setting @error to
3629 * %G_IO_ERROR_NOT_SUPPORTED.
3631 * For a local #GFile the newly created directory will have the default
3632 * (current) ownership and permissions of the current process.
3634 * If @cancellable is not %NULL, then the operation can be cancelled by
3635 * triggering the cancellable object from another thread. If the operation
3636 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3638 * Returns: %TRUE on successful creation, %FALSE otherwise.
3641 g_file_make_directory (GFile *file,
3642 GCancellable *cancellable,
3647 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3652 iface = G_FILE_GET_IFACE (file);
3654 if (iface->make_directory == NULL)
3656 g_set_error_literal (error, G_IO_ERROR,
3657 G_IO_ERROR_NOT_SUPPORTED,
3658 _("Operation not supported"));
3662 return (* iface->make_directory) (file, cancellable, error);
3666 * g_file_make_directory_async:
3667 * @file: input #GFile
3668 * @io_priority: the [I/O priority][io-priority] of the request
3669 * @cancellable: (nullable): optional #GCancellable object,
3671 * @callback: a #GAsyncReadyCallback to call
3672 * when the request is satisfied
3673 * @user_data: the data to pass to callback function
3675 * Asynchronously creates a directory.
3677 * Virtual: make_directory_async
3681 g_file_make_directory_async (GFile *file,
3683 GCancellable *cancellable,
3684 GAsyncReadyCallback callback,
3689 g_return_if_fail (G_IS_FILE (file));
3691 iface = G_FILE_GET_IFACE (file);
3692 (* iface->make_directory_async) (file,
3700 * g_file_make_directory_finish:
3701 * @file: input #GFile
3702 * @result: a #GAsyncResult
3703 * @error: a #GError, or %NULL
3705 * Finishes an asynchronous directory creation, started with
3706 * g_file_make_directory_async().
3708 * Virtual: make_directory_finish
3709 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3713 g_file_make_directory_finish (GFile *file,
3714 GAsyncResult *result,
3719 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3720 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3722 iface = G_FILE_GET_IFACE (file);
3723 return (* iface->make_directory_finish) (file, result, error);
3727 * g_file_make_directory_with_parents:
3728 * @file: input #GFile
3729 * @cancellable: (nullable): optional #GCancellable object,
3731 * @error: a #GError, or %NULL
3733 * Creates a directory and any parent directories that may not
3734 * exist similar to 'mkdir -p'. If the file system does not support
3735 * creating directories, this function will fail, setting @error to
3736 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3737 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3738 * the similar g_mkdir_with_parents().
3740 * For a local #GFile the newly created directories will have the default
3741 * (current) ownership and permissions of the current process.
3743 * If @cancellable is not %NULL, then the operation can be cancelled by
3744 * triggering the cancellable object from another thread. If the operation
3745 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3747 * Returns: %TRUE if all directories have been successfully created, %FALSE
3753 g_file_make_directory_with_parents (GFile *file,
3754 GCancellable *cancellable,
3757 GFile *work_file = NULL;
3758 GList *list = NULL, *l;
3759 GError *my_error = NULL;
3761 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3766 /* Try for the simple case of not having to create any parent
3767 * directories. If any parent directory needs to be created, this
3768 * call will fail with NOT_FOUND. If that happens, then that value of
3769 * my_error persists into the while loop below.
3771 g_file_make_directory (file, cancellable, &my_error);
3772 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3775 g_propagate_error (error, my_error);
3776 return my_error == NULL;
3779 work_file = g_object_ref (file);
3781 /* Creates the parent directories as needed. In case any particular
3782 * creation operation fails for lack of other parent directories
3783 * (NOT_FOUND), the directory is added to a list of directories to
3784 * create later, and the value of my_error is retained until the next
3785 * iteration of the loop. After the loop my_error should either be
3786 * empty or contain a real failure condition.
3788 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3792 parent_file = g_file_get_parent (work_file);
3793 if (parent_file == NULL)
3796 g_clear_error (&my_error);
3797 g_file_make_directory (parent_file, cancellable, &my_error);
3798 /* Another process may have created the directory in between the
3799 * G_IO_ERROR_NOT_FOUND and now
3801 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3802 g_clear_error (&my_error);
3804 g_object_unref (work_file);
3805 work_file = g_object_ref (parent_file);
3807 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3808 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3810 g_object_unref (parent_file);
3813 /* All directories should be able to be created now, so an error at
3814 * this point means the whole operation must fail -- except an EXISTS
3815 * error, which means that another process already created the
3816 * directory in between the previous failure and now.
3818 for (l = list; my_error == NULL && l; l = l->next)
3820 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3821 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3822 g_clear_error (&my_error);
3826 g_object_unref (work_file);
3829 while (list != NULL)
3831 g_object_unref ((GFile *) list->data);
3832 list = g_list_remove (list, list->data);
3835 /* At this point an error in my_error means a that something
3836 * unexpected failed in either of the loops above, so the whole
3837 * operation must fail.
3839 if (my_error != NULL)
3841 g_propagate_error (error, my_error);
3845 return g_file_make_directory (file, cancellable, error);
3849 * g_file_make_symbolic_link:
3850 * @file: a #GFile with the name of the symlink to create
3851 * @symlink_value: (type filename): a string with the path for the target
3852 * of the new symlink
3853 * @cancellable: (nullable): optional #GCancellable object,
3857 * Creates a symbolic link named @file which contains the string
3860 * If @cancellable is not %NULL, then the operation can be cancelled by
3861 * triggering the cancellable object from another thread. If the operation
3862 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3864 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3867 g_file_make_symbolic_link (GFile *file,
3868 const char *symlink_value,
3869 GCancellable *cancellable,
3874 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3875 g_return_val_if_fail (symlink_value != NULL, FALSE);
3877 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3880 if (*symlink_value == '\0')
3882 g_set_error_literal (error, G_IO_ERROR,
3883 G_IO_ERROR_INVALID_ARGUMENT,
3884 _("Invalid symlink value given"));
3888 iface = G_FILE_GET_IFACE (file);
3890 if (iface->make_symbolic_link == NULL)
3892 g_set_error_literal (error, G_IO_ERROR,
3893 G_IO_ERROR_NOT_SUPPORTED,
3894 _("Operation not supported"));
3898 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3903 * @file: input #GFile
3904 * @cancellable: (nullable): optional #GCancellable object,
3906 * @error: a #GError, or %NULL
3908 * Deletes a file. If the @file is a directory, it will only be
3909 * deleted if it is empty. This has the same semantics as g_unlink().
3911 * If @cancellable is not %NULL, then the operation can be cancelled by
3912 * triggering the cancellable object from another thread. If the operation
3913 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3915 * Virtual: delete_file
3916 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3919 g_file_delete (GFile *file,
3920 GCancellable *cancellable,
3925 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3927 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3930 iface = G_FILE_GET_IFACE (file);
3932 if (iface->delete_file == NULL)
3934 g_set_error_literal (error, G_IO_ERROR,
3935 G_IO_ERROR_NOT_SUPPORTED,
3936 _("Operation not supported"));
3940 return (* iface->delete_file) (file, cancellable, error);
3944 * g_file_delete_async:
3945 * @file: input #GFile
3946 * @io_priority: the [I/O priority][io-priority] of the request
3947 * @cancellable: (nullable): optional #GCancellable object,
3949 * @callback: a #GAsyncReadyCallback to call
3950 * when the request is satisfied
3951 * @user_data: the data to pass to callback function
3953 * Asynchronously delete a file. If the @file is a directory, it will
3954 * only be deleted if it is empty. This has the same semantics as
3957 * Virtual: delete_file_async
3961 g_file_delete_async (GFile *file,
3963 GCancellable *cancellable,
3964 GAsyncReadyCallback callback,
3969 g_return_if_fail (G_IS_FILE (file));
3971 iface = G_FILE_GET_IFACE (file);
3972 (* iface->delete_file_async) (file,
3980 * g_file_delete_finish:
3981 * @file: input #GFile
3982 * @result: a #GAsyncResult
3983 * @error: a #GError, or %NULL
3985 * Finishes deleting a file started with g_file_delete_async().
3987 * Virtual: delete_file_finish
3988 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3992 g_file_delete_finish (GFile *file,
3993 GAsyncResult *result,
3998 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3999 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4001 if (g_async_result_legacy_propagate_error (result, error))
4004 iface = G_FILE_GET_IFACE (file);
4005 return (* iface->delete_file_finish) (file, result, error);
4010 * @file: #GFile to send to trash
4011 * @cancellable: (nullable): optional #GCancellable object,
4013 * @error: a #GError, or %NULL
4015 * Sends @file to the "Trashcan", if possible. This is similar to
4016 * deleting it, but the user can recover it before emptying the trashcan.
4017 * Not all file systems support trashing, so this call can return the
4018 * %G_IO_ERROR_NOT_SUPPORTED error.
4020 * If @cancellable is not %NULL, then the operation can be cancelled by
4021 * triggering the cancellable object from another thread. If the operation
4022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4025 * Returns: %TRUE on successful trash, %FALSE otherwise.
4028 g_file_trash (GFile *file,
4029 GCancellable *cancellable,
4034 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4036 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4039 iface = G_FILE_GET_IFACE (file);
4041 if (iface->trash == NULL)
4043 g_set_error_literal (error,
4044 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4045 _("Trash not supported"));
4049 return (* iface->trash) (file, cancellable, error);
4053 * g_file_trash_async:
4054 * @file: input #GFile
4055 * @io_priority: the [I/O priority][io-priority] of the request
4056 * @cancellable: (nullable): optional #GCancellable object,
4058 * @callback: a #GAsyncReadyCallback to call
4059 * when the request is satisfied
4060 * @user_data: the data to pass to callback function
4062 * Asynchronously sends @file to the Trash location, if possible.
4064 * Virtual: trash_async
4068 g_file_trash_async (GFile *file,
4070 GCancellable *cancellable,
4071 GAsyncReadyCallback callback,
4076 g_return_if_fail (G_IS_FILE (file));
4078 iface = G_FILE_GET_IFACE (file);
4079 (* iface->trash_async) (file,
4087 * g_file_trash_finish:
4088 * @file: input #GFile
4089 * @result: a #GAsyncResult
4090 * @error: a #GError, or %NULL
4092 * Finishes an asynchronous file trashing operation, started with
4093 * g_file_trash_async().
4095 * Virtual: trash_finish
4096 * Returns: %TRUE on successful trash, %FALSE otherwise.
4100 g_file_trash_finish (GFile *file,
4101 GAsyncResult *result,
4106 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4107 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4109 iface = G_FILE_GET_IFACE (file);
4110 return (* iface->trash_finish) (file, result, error);
4114 * g_file_set_display_name:
4115 * @file: input #GFile
4116 * @display_name: a string
4117 * @cancellable: (nullable): optional #GCancellable object,
4119 * @error: a #GError, or %NULL
4121 * Renames @file to the specified display name.
4123 * The display name is converted from UTF-8 to the correct encoding
4124 * for the target filesystem if possible and the @file is renamed to this.
4126 * If you want to implement a rename operation in the user interface the
4127 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4128 * initial value in the rename widget, and then the result after editing
4129 * should be passed to g_file_set_display_name().
4131 * On success the resulting converted filename is returned.
4133 * If @cancellable is not %NULL, then the operation can be cancelled by
4134 * triggering the cancellable object from another thread. If the operation
4135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4137 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4138 * or %NULL if there was an error.
4139 * Free the returned object with g_object_unref().
4142 g_file_set_display_name (GFile *file,
4143 const gchar *display_name,
4144 GCancellable *cancellable,
4149 g_return_val_if_fail (G_IS_FILE (file), NULL);
4150 g_return_val_if_fail (display_name != NULL, NULL);
4152 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4156 G_IO_ERROR_INVALID_ARGUMENT,
4157 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4161 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4164 iface = G_FILE_GET_IFACE (file);
4166 return (* iface->set_display_name) (file, display_name, cancellable, error);
4170 * g_file_set_display_name_async:
4171 * @file: input #GFile
4172 * @display_name: a string
4173 * @io_priority: the [I/O priority][io-priority] of the request
4174 * @cancellable: (nullable): optional #GCancellable object,
4176 * @callback: (scope async): a #GAsyncReadyCallback to call
4177 * when the request is satisfied
4178 * @user_data: (closure): the data to pass to callback function
4180 * Asynchronously sets the display name for a given #GFile.
4182 * For more details, see g_file_set_display_name() which is
4183 * the synchronous version of this call.
4185 * When the operation is finished, @callback will be called.
4186 * You can then call g_file_set_display_name_finish() to get
4187 * the result of the operation.
4190 g_file_set_display_name_async (GFile *file,
4191 const gchar *display_name,
4193 GCancellable *cancellable,
4194 GAsyncReadyCallback callback,
4199 g_return_if_fail (G_IS_FILE (file));
4200 g_return_if_fail (display_name != NULL);
4202 iface = G_FILE_GET_IFACE (file);
4203 (* iface->set_display_name_async) (file,
4212 * g_file_set_display_name_finish:
4213 * @file: input #GFile
4214 * @res: a #GAsyncResult
4215 * @error: a #GError, or %NULL
4217 * Finishes setting a display name started with
4218 * g_file_set_display_name_async().
4220 * Returns: (transfer full): a #GFile or %NULL on error.
4221 * Free the returned object with g_object_unref().
4224 g_file_set_display_name_finish (GFile *file,
4230 g_return_val_if_fail (G_IS_FILE (file), NULL);
4231 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4233 if (g_async_result_legacy_propagate_error (res, error))
4236 iface = G_FILE_GET_IFACE (file);
4237 return (* iface->set_display_name_finish) (file, res, error);
4241 * g_file_query_settable_attributes:
4242 * @file: input #GFile
4243 * @cancellable: (nullable): optional #GCancellable object,
4245 * @error: a #GError, or %NULL
4247 * Obtain the list of settable attributes for the file.
4249 * Returns the type and full attribute name of all the attributes
4250 * that can be set on this file. This doesn't mean setting it will
4251 * always succeed though, you might get an access failure, or some
4252 * specific file may not support a specific attribute.
4254 * If @cancellable is not %NULL, then the operation can be cancelled by
4255 * triggering the cancellable object from another thread. If the operation
4256 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4258 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4259 * When you are done with it, release it with
4260 * g_file_attribute_info_list_unref()
4262 GFileAttributeInfoList *
4263 g_file_query_settable_attributes (GFile *file,
4264 GCancellable *cancellable,
4269 GFileAttributeInfoList *list;
4271 g_return_val_if_fail (G_IS_FILE (file), NULL);
4273 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4276 iface = G_FILE_GET_IFACE (file);
4278 if (iface->query_settable_attributes == NULL)
4279 return g_file_attribute_info_list_new ();
4282 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4286 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4288 list = g_file_attribute_info_list_new ();
4289 g_error_free (my_error);
4292 g_propagate_error (error, my_error);
4299 * g_file_query_writable_namespaces:
4300 * @file: input #GFile
4301 * @cancellable: (nullable): optional #GCancellable object,
4303 * @error: a #GError, or %NULL
4305 * Obtain the list of attribute namespaces where new attributes
4306 * can be created by a user. An example of this is extended
4307 * attributes (in the "xattr" namespace).
4309 * If @cancellable is not %NULL, then the operation can be cancelled by
4310 * triggering the cancellable object from another thread. If the operation
4311 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4313 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4314 * When you are done with it, release it with
4315 * g_file_attribute_info_list_unref()
4317 GFileAttributeInfoList *
4318 g_file_query_writable_namespaces (GFile *file,
4319 GCancellable *cancellable,
4324 GFileAttributeInfoList *list;
4326 g_return_val_if_fail (G_IS_FILE (file), NULL);
4328 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4331 iface = G_FILE_GET_IFACE (file);
4333 if (iface->query_writable_namespaces == NULL)
4334 return g_file_attribute_info_list_new ();
4337 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4341 g_warn_if_reached();
4342 list = g_file_attribute_info_list_new ();
4345 if (my_error != NULL)
4347 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4349 g_error_free (my_error);
4352 g_propagate_error (error, my_error);
4359 * g_file_set_attribute:
4360 * @file: input #GFile
4361 * @attribute: a string containing the attribute's name
4362 * @type: The type of the attribute
4363 * @value_p: (nullable): a pointer to the value (or the pointer
4364 * itself if the type is a pointer type)
4365 * @flags: a set of #GFileQueryInfoFlags
4366 * @cancellable: (nullable): optional #GCancellable object,
4368 * @error: a #GError, or %NULL
4370 * Sets an attribute in the file with attribute name @attribute to @value.
4372 * Some attributes can be unset by setting @type to
4373 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4375 * If @cancellable is not %NULL, then the operation can be cancelled by
4376 * triggering the cancellable object from another thread. If the operation
4377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4379 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4382 g_file_set_attribute (GFile *file,
4383 const gchar *attribute,
4384 GFileAttributeType type,
4386 GFileQueryInfoFlags flags,
4387 GCancellable *cancellable,
4392 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4393 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4395 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4398 iface = G_FILE_GET_IFACE (file);
4400 if (iface->set_attribute == NULL)
4402 g_set_error_literal (error, G_IO_ERROR,
4403 G_IO_ERROR_NOT_SUPPORTED,
4404 _("Operation not supported"));
4408 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4412 * g_file_set_attributes_from_info:
4413 * @file: input #GFile
4414 * @info: a #GFileInfo
4415 * @flags: #GFileQueryInfoFlags
4416 * @cancellable: (nullable): optional #GCancellable object,
4418 * @error: a #GError, or %NULL
4420 * Tries to set all attributes in the #GFileInfo on the target
4421 * values, not stopping on the first error.
4423 * If there is any error during this operation then @error will
4424 * be set to the first error. Error on particular fields are flagged
4425 * by setting the "status" field in the attribute value to
4426 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4427 * also detect further errors.
4429 * If @cancellable is not %NULL, then the operation can be cancelled by
4430 * triggering the cancellable object from another thread. If the operation
4431 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4433 * Returns: %FALSE if there was any error, %TRUE otherwise.
4436 g_file_set_attributes_from_info (GFile *file,
4438 GFileQueryInfoFlags flags,
4439 GCancellable *cancellable,
4444 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4445 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4447 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4450 g_file_info_clear_status (info);
4452 iface = G_FILE_GET_IFACE (file);
4454 return (* iface->set_attributes_from_info) (file,
4462 g_file_real_set_attributes_from_info (GFile *file,
4464 GFileQueryInfoFlags flags,
4465 GCancellable *cancellable,
4471 GFileAttributeValue *value;
4475 attributes = g_file_info_list_attributes (info, NULL);
4477 for (i = 0; attributes[i] != NULL; i++)
4479 value = _g_file_info_get_attribute_value (info, attributes[i]);
4481 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4484 if (!g_file_set_attribute (file, attributes[i],
4485 value->type, _g_file_attribute_value_peek_as_pointer (value),
4486 flags, cancellable, error))
4488 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4490 /* Don't set error multiple times */
4494 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4497 g_strfreev (attributes);
4503 * g_file_set_attributes_async:
4504 * @file: input #GFile
4505 * @info: a #GFileInfo
4506 * @flags: a #GFileQueryInfoFlags
4507 * @io_priority: the [I/O priority][io-priority] of the request
4508 * @cancellable: (nullable): optional #GCancellable object,
4510 * @callback: (scope async): a #GAsyncReadyCallback
4511 * @user_data: (closure): a #gpointer
4513 * Asynchronously sets the attributes of @file with @info.
4515 * For more details, see g_file_set_attributes_from_info(),
4516 * which is the synchronous version of this call.
4518 * When the operation is finished, @callback will be called.
4519 * You can then call g_file_set_attributes_finish() to get
4520 * the result of the operation.
4523 g_file_set_attributes_async (GFile *file,
4525 GFileQueryInfoFlags flags,
4527 GCancellable *cancellable,
4528 GAsyncReadyCallback callback,
4533 g_return_if_fail (G_IS_FILE (file));
4534 g_return_if_fail (G_IS_FILE_INFO (info));
4536 iface = G_FILE_GET_IFACE (file);
4537 (* iface->set_attributes_async) (file,
4547 * g_file_set_attributes_finish:
4548 * @file: input #GFile
4549 * @result: a #GAsyncResult
4550 * @info: (out) (transfer full): a #GFileInfo
4551 * @error: a #GError, or %NULL
4553 * Finishes setting an attribute started in g_file_set_attributes_async().
4555 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4558 g_file_set_attributes_finish (GFile *file,
4559 GAsyncResult *result,
4565 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4566 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4568 /* No standard handling of errors here, as we must set info even
4571 iface = G_FILE_GET_IFACE (file);
4572 return (* iface->set_attributes_finish) (file, result, info, error);
4576 * g_file_set_attribute_string:
4577 * @file: input #GFile
4578 * @attribute: a string containing the attribute's name
4579 * @value: a string containing the attribute's value
4580 * @flags: #GFileQueryInfoFlags
4581 * @cancellable: (nullable): optional #GCancellable object,
4583 * @error: a #GError, or %NULL
4585 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4586 * If @attribute is of a different type, this operation will fail.
4588 * If @cancellable is not %NULL, then the operation can be cancelled by
4589 * triggering the cancellable object from another thread. If the operation
4590 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4592 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4595 g_file_set_attribute_string (GFile *file,
4596 const char *attribute,
4598 GFileQueryInfoFlags flags,
4599 GCancellable *cancellable,
4602 return g_file_set_attribute (file, attribute,
4603 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4604 flags, cancellable, error);
4608 * g_file_set_attribute_byte_string:
4609 * @file: input #GFile
4610 * @attribute: a string containing the attribute's name
4611 * @value: a string containing the attribute's new value
4612 * @flags: a #GFileQueryInfoFlags
4613 * @cancellable: (nullable): optional #GCancellable object,
4615 * @error: a #GError, or %NULL
4617 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4618 * If @attribute is of a different type, this operation will fail,
4621 * If @cancellable is not %NULL, then the operation can be cancelled by
4622 * triggering the cancellable object from another thread. If the operation
4623 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4625 * Returns: %TRUE if the @attribute was successfully set to @value
4626 * in the @file, %FALSE otherwise.
4629 g_file_set_attribute_byte_string (GFile *file,
4630 const gchar *attribute,
4632 GFileQueryInfoFlags flags,
4633 GCancellable *cancellable,
4636 return g_file_set_attribute (file, attribute,
4637 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4638 flags, cancellable, error);
4642 * g_file_set_attribute_uint32:
4643 * @file: input #GFile
4644 * @attribute: a string containing the attribute's name
4645 * @value: a #guint32 containing the attribute's new value
4646 * @flags: a #GFileQueryInfoFlags
4647 * @cancellable: (nullable): optional #GCancellable object,
4649 * @error: a #GError, or %NULL
4651 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4652 * If @attribute is of a different type, this operation will fail.
4654 * If @cancellable is not %NULL, then the operation can be cancelled by
4655 * triggering the cancellable object from another thread. If the operation
4656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4658 * Returns: %TRUE if the @attribute was successfully set to @value
4659 * in the @file, %FALSE otherwise.
4662 g_file_set_attribute_uint32 (GFile *file,
4663 const gchar *attribute,
4665 GFileQueryInfoFlags flags,
4666 GCancellable *cancellable,
4669 return g_file_set_attribute (file, attribute,
4670 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4671 flags, cancellable, error);
4675 * g_file_set_attribute_int32:
4676 * @file: input #GFile
4677 * @attribute: a string containing the attribute's name
4678 * @value: a #gint32 containing the attribute's new value
4679 * @flags: a #GFileQueryInfoFlags
4680 * @cancellable: (nullable): optional #GCancellable object,
4682 * @error: a #GError, or %NULL
4684 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4685 * If @attribute is of a different type, this operation will fail.
4687 * If @cancellable is not %NULL, then the operation can be cancelled by
4688 * triggering the cancellable object from another thread. If the operation
4689 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4691 * Returns: %TRUE if the @attribute was successfully set to @value
4692 * in the @file, %FALSE otherwise.
4695 g_file_set_attribute_int32 (GFile *file,
4696 const gchar *attribute,
4698 GFileQueryInfoFlags flags,
4699 GCancellable *cancellable,
4702 return g_file_set_attribute (file, attribute,
4703 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4704 flags, cancellable, error);
4708 * g_file_set_attribute_uint64:
4709 * @file: input #GFile
4710 * @attribute: a string containing the attribute's name
4711 * @value: a #guint64 containing the attribute's new value
4712 * @flags: a #GFileQueryInfoFlags
4713 * @cancellable: (nullable): optional #GCancellable object,
4715 * @error: a #GError, or %NULL
4717 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4718 * If @attribute is of a different type, this operation will fail.
4720 * If @cancellable is not %NULL, then the operation can be cancelled by
4721 * triggering the cancellable object from another thread. If the operation
4722 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4724 * Returns: %TRUE if the @attribute was successfully set to @value
4725 * in the @file, %FALSE otherwise.
4728 g_file_set_attribute_uint64 (GFile *file,
4729 const gchar *attribute,
4731 GFileQueryInfoFlags flags,
4732 GCancellable *cancellable,
4735 return g_file_set_attribute (file, attribute,
4736 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4737 flags, cancellable, error);
4741 * g_file_set_attribute_int64:
4742 * @file: input #GFile
4743 * @attribute: a string containing the attribute's name
4744 * @value: a #guint64 containing the attribute's new value
4745 * @flags: a #GFileQueryInfoFlags
4746 * @cancellable: (nullable): optional #GCancellable object,
4748 * @error: a #GError, or %NULL
4750 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4751 * If @attribute is of a different type, this operation will fail.
4753 * If @cancellable is not %NULL, then the operation can be cancelled by
4754 * triggering the cancellable object from another thread. If the operation
4755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4757 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4760 g_file_set_attribute_int64 (GFile *file,
4761 const gchar *attribute,
4763 GFileQueryInfoFlags flags,
4764 GCancellable *cancellable,
4767 return g_file_set_attribute (file, attribute,
4768 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4769 flags, cancellable, error);
4773 * g_file_mount_mountable:
4774 * @file: input #GFile
4775 * @flags: flags affecting the operation
4776 * @mount_operation: (nullable): a #GMountOperation,
4777 * or %NULL to avoid user interaction
4778 * @cancellable: (nullable): optional #GCancellable object,
4780 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4781 * when the request is satisfied, or %NULL
4782 * @user_data: (closure): the data to pass to callback function
4784 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4785 * Using @mount_operation, you can request callbacks when, for instance,
4786 * passwords are needed during authentication.
4788 * If @cancellable is not %NULL, then the operation can be cancelled by
4789 * triggering the cancellable object from another thread. If the operation
4790 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4792 * When the operation is finished, @callback will be called.
4793 * You can then call g_file_mount_mountable_finish() to get
4794 * the result of the operation.
4797 g_file_mount_mountable (GFile *file,
4798 GMountMountFlags flags,
4799 GMountOperation *mount_operation,
4800 GCancellable *cancellable,
4801 GAsyncReadyCallback callback,
4806 g_return_if_fail (G_IS_FILE (file));
4808 iface = G_FILE_GET_IFACE (file);
4810 if (iface->mount_mountable == NULL)
4812 g_task_report_new_error (file, callback, user_data,
4813 g_file_mount_mountable,
4814 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4815 _("Operation not supported"));
4819 (* iface->mount_mountable) (file,
4828 * g_file_mount_mountable_finish:
4829 * @file: input #GFile
4830 * @result: a #GAsyncResult
4831 * @error: a #GError, or %NULL
4833 * Finishes a mount operation. See g_file_mount_mountable() for details.
4835 * Finish an asynchronous mount operation that was started
4836 * with g_file_mount_mountable().
4838 * Returns: (transfer full): a #GFile or %NULL on error.
4839 * Free the returned object with g_object_unref().
4842 g_file_mount_mountable_finish (GFile *file,
4843 GAsyncResult *result,
4848 g_return_val_if_fail (G_IS_FILE (file), NULL);
4849 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4851 if (g_async_result_legacy_propagate_error (result, error))
4853 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4854 return g_task_propagate_pointer (G_TASK (result), error);
4856 iface = G_FILE_GET_IFACE (file);
4857 return (* iface->mount_mountable_finish) (file, result, error);
4861 * g_file_unmount_mountable:
4862 * @file: input #GFile
4863 * @flags: flags affecting the operation
4864 * @cancellable: (nullable): optional #GCancellable object,
4866 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4867 * when the request is satisfied, or %NULL
4868 * @user_data: (closure): the data to pass to callback function
4870 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
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 * When the operation is finished, @callback will be called.
4877 * You can then call g_file_unmount_mountable_finish() to get
4878 * the result of the operation.
4880 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4883 g_file_unmount_mountable (GFile *file,
4884 GMountUnmountFlags flags,
4885 GCancellable *cancellable,
4886 GAsyncReadyCallback callback,
4891 g_return_if_fail (G_IS_FILE (file));
4893 iface = G_FILE_GET_IFACE (file);
4895 if (iface->unmount_mountable == NULL)
4897 g_task_report_new_error (file, callback, user_data,
4898 g_file_unmount_mountable_with_operation,
4899 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4900 _("Operation not supported"));
4904 (* iface->unmount_mountable) (file,
4912 * g_file_unmount_mountable_finish:
4913 * @file: input #GFile
4914 * @result: a #GAsyncResult
4915 * @error: a #GError, or %NULL
4917 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4919 * Finish an asynchronous unmount operation that was started
4920 * with g_file_unmount_mountable().
4922 * Returns: %TRUE if the operation finished successfully.
4925 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4929 g_file_unmount_mountable_finish (GFile *file,
4930 GAsyncResult *result,
4935 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4936 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4938 if (g_async_result_legacy_propagate_error (result, error))
4940 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4941 return g_task_propagate_boolean (G_TASK (result), error);
4943 iface = G_FILE_GET_IFACE (file);
4944 return (* iface->unmount_mountable_finish) (file, result, error);
4948 * g_file_unmount_mountable_with_operation:
4949 * @file: input #GFile
4950 * @flags: flags affecting the operation
4951 * @mount_operation: (nullable): a #GMountOperation,
4952 * or %NULL to avoid user interaction
4953 * @cancellable: (nullable): optional #GCancellable object,
4955 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4956 * when the request is satisfied, or %NULL
4957 * @user_data: (closure): the data to pass to callback function
4959 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4961 * If @cancellable is not %NULL, then the operation can be cancelled by
4962 * triggering the cancellable object from another thread. If the operation
4963 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4965 * When the operation is finished, @callback will be called.
4966 * You can then call g_file_unmount_mountable_finish() to get
4967 * the result of the operation.
4972 g_file_unmount_mountable_with_operation (GFile *file,
4973 GMountUnmountFlags flags,
4974 GMountOperation *mount_operation,
4975 GCancellable *cancellable,
4976 GAsyncReadyCallback callback,
4981 g_return_if_fail (G_IS_FILE (file));
4983 iface = G_FILE_GET_IFACE (file);
4985 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4987 g_task_report_new_error (file, callback, user_data,
4988 g_file_unmount_mountable_with_operation,
4989 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4990 _("Operation not supported"));
4994 if (iface->unmount_mountable_with_operation != NULL)
4995 (* iface->unmount_mountable_with_operation) (file,
5002 (* iface->unmount_mountable) (file,
5010 * g_file_unmount_mountable_with_operation_finish:
5011 * @file: input #GFile
5012 * @result: a #GAsyncResult
5013 * @error: a #GError, or %NULL
5015 * Finishes an unmount operation,
5016 * see g_file_unmount_mountable_with_operation() for details.
5018 * Finish an asynchronous unmount operation that was started
5019 * with g_file_unmount_mountable_with_operation().
5021 * Returns: %TRUE if the operation finished successfully.
5027 g_file_unmount_mountable_with_operation_finish (GFile *file,
5028 GAsyncResult *result,
5033 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5034 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5036 if (g_async_result_legacy_propagate_error (result, error))
5038 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5039 return g_task_propagate_boolean (G_TASK (result), error);
5041 iface = G_FILE_GET_IFACE (file);
5042 if (iface->unmount_mountable_with_operation_finish != NULL)
5043 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5045 return (* iface->unmount_mountable_finish) (file, result, error);
5049 * g_file_eject_mountable:
5050 * @file: input #GFile
5051 * @flags: flags affecting the operation
5052 * @cancellable: (nullable): optional #GCancellable object,
5054 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5055 * when the request is satisfied, or %NULL
5056 * @user_data: (closure): the data to pass to callback function
5058 * Starts an asynchronous eject on a mountable.
5059 * When this operation has completed, @callback will be called with
5060 * @user_user data, and the operation can be finalized with
5061 * g_file_eject_mountable_finish().
5063 * If @cancellable is not %NULL, then the operation can be cancelled by
5064 * triggering the cancellable object from another thread. If the operation
5065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5067 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5070 g_file_eject_mountable (GFile *file,
5071 GMountUnmountFlags flags,
5072 GCancellable *cancellable,
5073 GAsyncReadyCallback callback,
5078 g_return_if_fail (G_IS_FILE (file));
5080 iface = G_FILE_GET_IFACE (file);
5082 if (iface->eject_mountable == NULL)
5084 g_task_report_new_error (file, callback, user_data,
5085 g_file_eject_mountable_with_operation,
5086 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5087 _("Operation not supported"));
5091 (* iface->eject_mountable) (file,
5099 * g_file_eject_mountable_finish:
5100 * @file: input #GFile
5101 * @result: a #GAsyncResult
5102 * @error: a #GError, or %NULL
5104 * Finishes an asynchronous eject operation started by
5105 * g_file_eject_mountable().
5107 * Returns: %TRUE if the @file was ejected successfully.
5110 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5114 g_file_eject_mountable_finish (GFile *file,
5115 GAsyncResult *result,
5120 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5121 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5123 if (g_async_result_legacy_propagate_error (result, error))
5125 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5126 return g_task_propagate_boolean (G_TASK (result), error);
5128 iface = G_FILE_GET_IFACE (file);
5129 return (* iface->eject_mountable_finish) (file, result, error);
5133 * g_file_eject_mountable_with_operation:
5134 * @file: input #GFile
5135 * @flags: flags affecting the operation
5136 * @mount_operation: (nullable): a #GMountOperation,
5137 * or %NULL to avoid user interaction
5138 * @cancellable: (nullable): optional #GCancellable object,
5140 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5141 * when the request is satisfied, or %NULL
5142 * @user_data: (closure): the data to pass to callback function
5144 * Starts an asynchronous eject on a mountable.
5145 * When this operation has completed, @callback will be called with
5146 * @user_user data, and the operation can be finalized with
5147 * g_file_eject_mountable_with_operation_finish().
5149 * If @cancellable is not %NULL, then the operation can be cancelled by
5150 * triggering the cancellable object from another thread. If the operation
5151 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5156 g_file_eject_mountable_with_operation (GFile *file,
5157 GMountUnmountFlags flags,
5158 GMountOperation *mount_operation,
5159 GCancellable *cancellable,
5160 GAsyncReadyCallback callback,
5165 g_return_if_fail (G_IS_FILE (file));
5167 iface = G_FILE_GET_IFACE (file);
5169 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5171 g_task_report_new_error (file, callback, user_data,
5172 g_file_eject_mountable_with_operation,
5173 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5174 _("Operation not supported"));
5178 if (iface->eject_mountable_with_operation != NULL)
5179 (* iface->eject_mountable_with_operation) (file,
5186 (* iface->eject_mountable) (file,
5194 * g_file_eject_mountable_with_operation_finish:
5195 * @file: input #GFile
5196 * @result: a #GAsyncResult
5197 * @error: a #GError, or %NULL
5199 * Finishes an asynchronous eject operation started by
5200 * g_file_eject_mountable_with_operation().
5202 * Returns: %TRUE if the @file was ejected successfully.
5208 g_file_eject_mountable_with_operation_finish (GFile *file,
5209 GAsyncResult *result,
5214 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5215 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5217 if (g_async_result_legacy_propagate_error (result, error))
5219 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5220 return g_task_propagate_boolean (G_TASK (result), error);
5222 iface = G_FILE_GET_IFACE (file);
5223 if (iface->eject_mountable_with_operation_finish != NULL)
5224 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5226 return (* iface->eject_mountable_finish) (file, result, error);
5230 * g_file_monitor_directory:
5231 * @file: input #GFile
5232 * @flags: a set of #GFileMonitorFlags
5233 * @cancellable: (nullable): optional #GCancellable object,
5235 * @error: a #GError, or %NULL
5237 * Obtains a directory monitor for the given file.
5238 * This may fail if directory monitoring is not supported.
5240 * If @cancellable is not %NULL, then the operation can be cancelled by
5241 * triggering the cancellable object from another thread. If the operation
5242 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5244 * It does not make sense for @flags to contain
5245 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5246 * directories. It is not possible to monitor all the files in a
5247 * directory for changes made via hard links; if you want to do this then
5248 * you must register individual watches with g_file_monitor().
5250 * Virtual: monitor_dir
5251 * Returns: (transfer full): a #GFileMonitor for the given @file,
5252 * or %NULL on error.
5253 * Free the returned object with g_object_unref().
5256 g_file_monitor_directory (GFile *file,
5257 GFileMonitorFlags flags,
5258 GCancellable *cancellable,
5263 g_return_val_if_fail (G_IS_FILE (file), NULL);
5264 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5266 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5269 iface = G_FILE_GET_IFACE (file);
5271 if (iface->monitor_dir == NULL)
5273 g_set_error_literal (error, G_IO_ERROR,
5274 G_IO_ERROR_NOT_SUPPORTED,
5275 _("Operation not supported"));
5279 return (* iface->monitor_dir) (file, flags, cancellable, error);
5283 * g_file_monitor_file:
5284 * @file: input #GFile
5285 * @flags: a set of #GFileMonitorFlags
5286 * @cancellable: (nullable): optional #GCancellable object,
5288 * @error: a #GError, or %NULL
5290 * Obtains a file monitor for the given file. If no file notification
5291 * mechanism exists, then regular polling of the file is used.
5293 * If @cancellable is not %NULL, then the operation can be cancelled by
5294 * triggering the cancellable object from another thread. If the operation
5295 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5297 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5298 * will also attempt to report changes made to the file via another
5299 * filename (ie, a hard link). Without this flag, you can only rely on
5300 * changes made through the filename contained in @file to be
5301 * reported. Using this flag may result in an increase in resource
5302 * usage, and may not have any effect depending on the #GFileMonitor
5303 * backend and/or filesystem type.
5305 * Returns: (transfer full): a #GFileMonitor for the given @file,
5306 * or %NULL on error.
5307 * Free the returned object with g_object_unref().
5310 g_file_monitor_file (GFile *file,
5311 GFileMonitorFlags flags,
5312 GCancellable *cancellable,
5316 GFileMonitor *monitor;
5318 g_return_val_if_fail (G_IS_FILE (file), NULL);
5320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5323 iface = G_FILE_GET_IFACE (file);
5327 if (iface->monitor_file)
5328 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5330 /* Fallback to polling */
5331 if (monitor == NULL)
5332 monitor = _g_poll_file_monitor_new (file);
5339 * @file: input #GFile
5340 * @flags: a set of #GFileMonitorFlags
5341 * @cancellable: (nullable): optional #GCancellable object,
5343 * @error: a #GError, or %NULL
5345 * Obtains a file or directory monitor for the given file,
5346 * depending on the type of the file.
5348 * If @cancellable is not %NULL, then the operation can be cancelled by
5349 * triggering the cancellable object from another thread. If the operation
5350 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5352 * Returns: (transfer full): a #GFileMonitor for the given @file,
5353 * or %NULL on error.
5354 * Free the returned object with g_object_unref().
5359 g_file_monitor (GFile *file,
5360 GFileMonitorFlags flags,
5361 GCancellable *cancellable,
5364 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5365 return g_file_monitor_directory (file,
5366 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5367 cancellable, error);
5369 return g_file_monitor_file (file, flags, cancellable, error);
5372 /********************************************
5373 * Default implementation of async ops *
5374 ********************************************/
5378 GFileQueryInfoFlags flags;
5379 } QueryInfoAsyncData;
5382 query_info_data_free (QueryInfoAsyncData *data)
5384 g_free (data->attributes);
5389 query_info_async_thread (GTask *task,
5392 GCancellable *cancellable)
5394 QueryInfoAsyncData *data = task_data;
5396 GError *error = NULL;
5398 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5400 g_task_return_pointer (task, info, g_object_unref);
5402 g_task_return_error (task, error);
5406 g_file_real_query_info_async (GFile *file,
5407 const char *attributes,
5408 GFileQueryInfoFlags flags,
5410 GCancellable *cancellable,
5411 GAsyncReadyCallback callback,
5415 QueryInfoAsyncData *data;
5417 data = g_new0 (QueryInfoAsyncData, 1);
5418 data->attributes = g_strdup (attributes);
5419 data->flags = flags;
5421 task = g_task_new (file, cancellable, callback, user_data);
5422 g_task_set_source_tag (task, g_file_real_query_info_async);
5423 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5424 g_task_set_priority (task, io_priority);
5425 g_task_run_in_thread (task, query_info_async_thread);
5426 g_object_unref (task);
5430 g_file_real_query_info_finish (GFile *file,
5434 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5436 return g_task_propagate_pointer (G_TASK (res), error);
5440 query_filesystem_info_async_thread (GTask *task,
5443 GCancellable *cancellable)
5445 const char *attributes = task_data;
5447 GError *error = NULL;
5449 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5451 g_task_return_pointer (task, info, g_object_unref);
5453 g_task_return_error (task, error);
5457 g_file_real_query_filesystem_info_async (GFile *file,
5458 const char *attributes,
5460 GCancellable *cancellable,
5461 GAsyncReadyCallback callback,
5466 task = g_task_new (file, cancellable, callback, user_data);
5467 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5468 g_task_set_task_data (task, g_strdup (attributes), g_free);
5469 g_task_set_priority (task, io_priority);
5470 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5471 g_object_unref (task);
5475 g_file_real_query_filesystem_info_finish (GFile *file,
5479 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5481 return g_task_propagate_pointer (G_TASK (res), error);
5485 enumerate_children_async_thread (GTask *task,
5488 GCancellable *cancellable)
5490 QueryInfoAsyncData *data = task_data;
5491 GFileEnumerator *enumerator;
5492 GError *error = NULL;
5494 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5496 g_task_return_error (task, error);
5498 g_task_return_pointer (task, enumerator, g_object_unref);
5502 g_file_real_enumerate_children_async (GFile *file,
5503 const char *attributes,
5504 GFileQueryInfoFlags flags,
5506 GCancellable *cancellable,
5507 GAsyncReadyCallback callback,
5511 QueryInfoAsyncData *data;
5513 data = g_new0 (QueryInfoAsyncData, 1);
5514 data->attributes = g_strdup (attributes);
5515 data->flags = flags;
5517 task = g_task_new (file, cancellable, callback, user_data);
5518 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5519 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5520 g_task_set_priority (task, io_priority);
5521 g_task_run_in_thread (task, enumerate_children_async_thread);
5522 g_object_unref (task);
5525 static GFileEnumerator *
5526 g_file_real_enumerate_children_finish (GFile *file,
5530 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5532 return g_task_propagate_pointer (G_TASK (res), error);
5536 open_read_async_thread (GTask *task,
5539 GCancellable *cancellable)
5541 GFileInputStream *stream;
5542 GError *error = NULL;
5544 stream = g_file_read (G_FILE (object), cancellable, &error);
5546 g_task_return_pointer (task, stream, g_object_unref);
5548 g_task_return_error (task, error);
5552 g_file_real_read_async (GFile *file,
5554 GCancellable *cancellable,
5555 GAsyncReadyCallback callback,
5560 task = g_task_new (file, cancellable, callback, user_data);
5561 g_task_set_source_tag (task, g_file_real_read_async);
5562 g_task_set_priority (task, io_priority);
5563 g_task_run_in_thread (task, open_read_async_thread);
5564 g_object_unref (task);
5567 static GFileInputStream *
5568 g_file_real_read_finish (GFile *file,
5572 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5574 return g_task_propagate_pointer (G_TASK (res), error);
5578 append_to_async_thread (GTask *task,
5579 gpointer source_object,
5581 GCancellable *cancellable)
5583 GFileCreateFlags *data = task_data;
5584 GFileOutputStream *stream;
5585 GError *error = NULL;
5587 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5589 g_task_return_pointer (task, stream, g_object_unref);
5591 g_task_return_error (task, error);
5595 g_file_real_append_to_async (GFile *file,
5596 GFileCreateFlags flags,
5598 GCancellable *cancellable,
5599 GAsyncReadyCallback callback,
5602 GFileCreateFlags *data;
5605 data = g_new0 (GFileCreateFlags, 1);
5608 task = g_task_new (file, cancellable, callback, user_data);
5609 g_task_set_source_tag (task, g_file_real_append_to_async);
5610 g_task_set_task_data (task, data, g_free);
5611 g_task_set_priority (task, io_priority);
5613 g_task_run_in_thread (task, append_to_async_thread);
5614 g_object_unref (task);
5617 static GFileOutputStream *
5618 g_file_real_append_to_finish (GFile *file,
5622 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5624 return g_task_propagate_pointer (G_TASK (res), error);
5628 create_async_thread (GTask *task,
5629 gpointer source_object,
5631 GCancellable *cancellable)
5633 GFileCreateFlags *data = task_data;
5634 GFileOutputStream *stream;
5635 GError *error = NULL;
5637 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5639 g_task_return_pointer (task, stream, g_object_unref);
5641 g_task_return_error (task, error);
5645 g_file_real_create_async (GFile *file,
5646 GFileCreateFlags flags,
5648 GCancellable *cancellable,
5649 GAsyncReadyCallback callback,
5652 GFileCreateFlags *data;
5655 data = g_new0 (GFileCreateFlags, 1);
5658 task = g_task_new (file, cancellable, callback, user_data);
5659 g_task_set_source_tag (task, g_file_real_create_async);
5660 g_task_set_task_data (task, data, g_free);
5661 g_task_set_priority (task, io_priority);
5663 g_task_run_in_thread (task, create_async_thread);
5664 g_object_unref (task);
5667 static GFileOutputStream *
5668 g_file_real_create_finish (GFile *file,
5672 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5674 return g_task_propagate_pointer (G_TASK (res), error);
5678 GFileOutputStream *stream;
5680 gboolean make_backup;
5681 GFileCreateFlags flags;
5685 replace_async_data_free (ReplaceAsyncData *data)
5688 g_object_unref (data->stream);
5689 g_free (data->etag);
5694 replace_async_thread (GTask *task,
5695 gpointer source_object,
5697 GCancellable *cancellable)
5699 GFileOutputStream *stream;
5700 ReplaceAsyncData *data = task_data;
5701 GError *error = NULL;
5703 stream = g_file_replace (G_FILE (source_object),
5711 g_task_return_pointer (task, stream, g_object_unref);
5713 g_task_return_error (task, error);
5717 g_file_real_replace_async (GFile *file,
5719 gboolean make_backup,
5720 GFileCreateFlags flags,
5722 GCancellable *cancellable,
5723 GAsyncReadyCallback callback,
5727 ReplaceAsyncData *data;
5729 data = g_new0 (ReplaceAsyncData, 1);
5730 data->etag = g_strdup (etag);
5731 data->make_backup = make_backup;
5732 data->flags = flags;
5734 task = g_task_new (file, cancellable, callback, user_data);
5735 g_task_set_source_tag (task, g_file_real_replace_async);
5736 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5737 g_task_set_priority (task, io_priority);
5739 g_task_run_in_thread (task, replace_async_thread);
5740 g_object_unref (task);
5743 static GFileOutputStream *
5744 g_file_real_replace_finish (GFile *file,
5748 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5750 return g_task_propagate_pointer (G_TASK (res), error);
5754 delete_async_thread (GTask *task,
5757 GCancellable *cancellable)
5759 GError *error = NULL;
5761 if (g_file_delete (G_FILE (object), cancellable, &error))
5762 g_task_return_boolean (task, TRUE);
5764 g_task_return_error (task, error);
5768 g_file_real_delete_async (GFile *file,
5770 GCancellable *cancellable,
5771 GAsyncReadyCallback callback,
5776 task = g_task_new (file, cancellable, callback, user_data);
5777 g_task_set_source_tag (task, g_file_real_delete_async);
5778 g_task_set_priority (task, io_priority);
5779 g_task_run_in_thread (task, delete_async_thread);
5780 g_object_unref (task);
5784 g_file_real_delete_finish (GFile *file,
5788 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5790 return g_task_propagate_boolean (G_TASK (res), error);
5794 trash_async_thread (GTask *task,
5797 GCancellable *cancellable)
5799 GError *error = NULL;
5801 if (g_file_trash (G_FILE (object), cancellable, &error))
5802 g_task_return_boolean (task, TRUE);
5804 g_task_return_error (task, error);
5808 g_file_real_trash_async (GFile *file,
5810 GCancellable *cancellable,
5811 GAsyncReadyCallback callback,
5816 task = g_task_new (file, cancellable, callback, user_data);
5817 g_task_set_source_tag (task, g_file_real_trash_async);
5818 g_task_set_priority (task, io_priority);
5819 g_task_run_in_thread (task, trash_async_thread);
5820 g_object_unref (task);
5824 g_file_real_trash_finish (GFile *file,
5828 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5830 return g_task_propagate_boolean (G_TASK (res), error);
5834 make_directory_async_thread (GTask *task,
5837 GCancellable *cancellable)
5839 GError *error = NULL;
5841 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5842 g_task_return_boolean (task, TRUE);
5844 g_task_return_error (task, error);
5848 g_file_real_make_directory_async (GFile *file,
5850 GCancellable *cancellable,
5851 GAsyncReadyCallback callback,
5856 task = g_task_new (file, cancellable, callback, user_data);
5857 g_task_set_source_tag (task, g_file_real_make_directory_async);
5858 g_task_set_priority (task, io_priority);
5859 g_task_run_in_thread (task, make_directory_async_thread);
5860 g_object_unref (task);
5864 g_file_real_make_directory_finish (GFile *file,
5868 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5870 return g_task_propagate_boolean (G_TASK (res), error);
5874 open_readwrite_async_thread (GTask *task,
5877 GCancellable *cancellable)
5879 GFileIOStream *stream;
5880 GError *error = NULL;
5882 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5885 g_task_return_error (task, error);
5887 g_task_return_pointer (task, stream, g_object_unref);
5891 g_file_real_open_readwrite_async (GFile *file,
5893 GCancellable *cancellable,
5894 GAsyncReadyCallback callback,
5899 task = g_task_new (file, cancellable, callback, user_data);
5900 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
5901 g_task_set_priority (task, io_priority);
5903 g_task_run_in_thread (task, open_readwrite_async_thread);
5904 g_object_unref (task);
5907 static GFileIOStream *
5908 g_file_real_open_readwrite_finish (GFile *file,
5912 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5914 return g_task_propagate_pointer (G_TASK (res), error);
5918 create_readwrite_async_thread (GTask *task,
5921 GCancellable *cancellable)
5923 GFileCreateFlags *data = task_data;
5924 GFileIOStream *stream;
5925 GError *error = NULL;
5927 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5930 g_task_return_error (task, error);
5932 g_task_return_pointer (task, stream, g_object_unref);
5936 g_file_real_create_readwrite_async (GFile *file,
5937 GFileCreateFlags flags,
5939 GCancellable *cancellable,
5940 GAsyncReadyCallback callback,
5943 GFileCreateFlags *data;
5946 data = g_new0 (GFileCreateFlags, 1);
5949 task = g_task_new (file, cancellable, callback, user_data);
5950 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
5951 g_task_set_task_data (task, data, g_free);
5952 g_task_set_priority (task, io_priority);
5954 g_task_run_in_thread (task, create_readwrite_async_thread);
5955 g_object_unref (task);
5958 static GFileIOStream *
5959 g_file_real_create_readwrite_finish (GFile *file,
5963 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5965 return g_task_propagate_pointer (G_TASK (res), error);
5970 gboolean make_backup;
5971 GFileCreateFlags flags;
5972 } ReplaceRWAsyncData;
5975 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5977 g_free (data->etag);
5982 replace_readwrite_async_thread (GTask *task,
5985 GCancellable *cancellable)
5987 GFileIOStream *stream;
5988 GError *error = NULL;
5989 ReplaceRWAsyncData *data = task_data;
5991 stream = g_file_replace_readwrite (G_FILE (object),
5999 g_task_return_error (task, error);
6001 g_task_return_pointer (task, stream, g_object_unref);
6005 g_file_real_replace_readwrite_async (GFile *file,
6007 gboolean make_backup,
6008 GFileCreateFlags flags,
6010 GCancellable *cancellable,
6011 GAsyncReadyCallback callback,
6015 ReplaceRWAsyncData *data;
6017 data = g_new0 (ReplaceRWAsyncData, 1);
6018 data->etag = g_strdup (etag);
6019 data->make_backup = make_backup;
6020 data->flags = flags;
6022 task = g_task_new (file, cancellable, callback, user_data);
6023 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6024 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6025 g_task_set_priority (task, io_priority);
6027 g_task_run_in_thread (task, replace_readwrite_async_thread);
6028 g_object_unref (task);
6031 static GFileIOStream *
6032 g_file_real_replace_readwrite_finish (GFile *file,
6036 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6038 return g_task_propagate_pointer (G_TASK (res), error);
6042 set_display_name_async_thread (GTask *task,
6045 GCancellable *cancellable)
6047 GError *error = NULL;
6048 char *name = task_data;
6051 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6054 g_task_return_error (task, error);
6056 g_task_return_pointer (task, file, g_object_unref);
6060 g_file_real_set_display_name_async (GFile *file,
6061 const char *display_name,
6063 GCancellable *cancellable,
6064 GAsyncReadyCallback callback,
6069 task = g_task_new (file, cancellable, callback, user_data);
6070 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6071 g_task_set_task_data (task, g_strdup (display_name), g_free);
6072 g_task_set_priority (task, io_priority);
6074 g_task_run_in_thread (task, set_display_name_async_thread);
6075 g_object_unref (task);
6079 g_file_real_set_display_name_finish (GFile *file,
6083 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6085 return g_task_propagate_pointer (G_TASK (res), error);
6089 GFileQueryInfoFlags flags;
6096 set_info_data_free (SetInfoAsyncData *data)
6099 g_object_unref (data->info);
6101 g_error_free (data->error);
6106 set_info_async_thread (GTask *task,
6109 GCancellable *cancellable)
6111 SetInfoAsyncData *data = task_data;
6114 data->res = g_file_set_attributes_from_info (G_FILE (object),
6122 g_file_real_set_attributes_async (GFile *file,
6124 GFileQueryInfoFlags flags,
6126 GCancellable *cancellable,
6127 GAsyncReadyCallback callback,
6131 SetInfoAsyncData *data;
6133 data = g_new0 (SetInfoAsyncData, 1);
6134 data->info = g_file_info_dup (info);
6135 data->flags = flags;
6137 task = g_task_new (file, cancellable, callback, user_data);
6138 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6139 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6140 g_task_set_priority (task, io_priority);
6142 g_task_run_in_thread (task, set_info_async_thread);
6143 g_object_unref (task);
6147 g_file_real_set_attributes_finish (GFile *file,
6152 SetInfoAsyncData *data;
6154 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6156 data = g_task_get_task_data (G_TASK (res));
6159 *info = g_object_ref (data->info);
6161 if (error != NULL && data->error)
6162 *error = g_error_copy (data->error);
6168 find_enclosing_mount_async_thread (GTask *task,
6171 GCancellable *cancellable)
6173 GError *error = NULL;
6176 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6179 g_task_return_error (task, error);
6181 g_task_return_pointer (task, mount, g_object_unref);
6185 g_file_real_find_enclosing_mount_async (GFile *file,
6187 GCancellable *cancellable,
6188 GAsyncReadyCallback callback,
6193 task = g_task_new (file, cancellable, callback, user_data);
6194 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6195 g_task_set_priority (task, io_priority);
6197 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6198 g_object_unref (task);
6202 g_file_real_find_enclosing_mount_finish (GFile *file,
6206 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6208 return g_task_propagate_pointer (G_TASK (res), error);
6215 GFileCopyFlags flags;
6216 GFileProgressCallback progress_cb;
6217 gpointer progress_cb_data;
6221 copy_async_data_free (CopyAsyncData *data)
6223 g_object_unref (data->source);
6224 g_object_unref (data->destination);
6225 g_slice_free (CopyAsyncData, data);
6229 CopyAsyncData *data;
6230 goffset current_num_bytes;
6231 goffset total_num_bytes;
6235 copy_async_progress_in_main (gpointer user_data)
6237 ProgressData *progress = user_data;
6238 CopyAsyncData *data = progress->data;
6240 data->progress_cb (progress->current_num_bytes,
6241 progress->total_num_bytes,
6242 data->progress_cb_data);
6248 copy_async_progress_callback (goffset current_num_bytes,
6249 goffset total_num_bytes,
6252 GTask *task = user_data;
6253 CopyAsyncData *data = g_task_get_task_data (task);
6254 ProgressData *progress;
6256 progress = g_new (ProgressData, 1);
6257 progress->data = data;
6258 progress->current_num_bytes = current_num_bytes;
6259 progress->total_num_bytes = total_num_bytes;
6261 g_main_context_invoke_full (g_task_get_context (task),
6262 g_task_get_priority (task),
6263 copy_async_progress_in_main,
6269 copy_async_thread (GTask *task,
6272 GCancellable *cancellable)
6274 CopyAsyncData *data = task_data;
6276 GError *error = NULL;
6278 result = g_file_copy (data->source,
6282 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6286 g_task_return_boolean (task, TRUE);
6288 g_task_return_error (task, error);
6292 g_file_real_copy_async (GFile *source,
6294 GFileCopyFlags flags,
6296 GCancellable *cancellable,
6297 GFileProgressCallback progress_callback,
6298 gpointer progress_callback_data,
6299 GAsyncReadyCallback callback,
6303 CopyAsyncData *data;
6305 data = g_slice_new (CopyAsyncData);
6306 data->source = g_object_ref (source);
6307 data->destination = g_object_ref (destination);
6308 data->flags = flags;
6309 data->progress_cb = progress_callback;
6310 data->progress_cb_data = progress_callback_data;
6312 task = g_task_new (source, cancellable, callback, user_data);
6313 g_task_set_source_tag (task, g_file_real_copy_async);
6314 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6315 g_task_set_priority (task, io_priority);
6316 g_task_run_in_thread (task, copy_async_thread);
6317 g_object_unref (task);
6321 g_file_real_copy_finish (GFile *file,
6325 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6327 return g_task_propagate_boolean (G_TASK (res), error);
6331 /********************************************
6332 * Default VFS operations *
6333 ********************************************/
6336 * g_file_new_for_path:
6337 * @path: (type filename): a string containing a relative or absolute path.
6338 * The string must be encoded in the glib filename encoding.
6340 * Constructs a #GFile for a given path. This operation never
6341 * fails, but the returned object might not support any I/O
6342 * operation if @path is malformed.
6344 * Returns: (transfer full): a new #GFile for the given @path.
6345 * Free the returned object with g_object_unref().
6348 g_file_new_for_path (const char *path)
6350 g_return_val_if_fail (path != NULL, NULL);
6352 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6356 * g_file_new_for_uri:
6357 * @uri: a UTF-8 string containing a URI
6359 * Constructs a #GFile for a given URI. This operation never
6360 * fails, but the returned object might not support any I/O
6361 * operation if @uri is malformed or if the uri type is
6364 * Returns: (transfer full): a new #GFile for the given @uri.
6365 * Free the returned object with g_object_unref().
6368 g_file_new_for_uri (const char *uri)
6370 g_return_val_if_fail (uri != NULL, NULL);
6372 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6377 * @tmpl: (type filename) (nullable): Template for the file
6378 * name, as in g_file_open_tmp(), or %NULL for a default template
6379 * @iostream: (out): on return, a #GFileIOStream for the created file
6380 * @error: a #GError, or %NULL
6382 * Opens a file in the preferred directory for temporary files (as
6383 * returned by g_get_tmp_dir()) and returns a #GFile and
6384 * #GFileIOStream pointing to it.
6386 * @tmpl should be a string in the GLib file name encoding
6387 * containing a sequence of six 'X' characters, and containing no
6388 * directory components. If it is %NULL, a default template is used.
6390 * Unlike the other #GFile constructors, this will return %NULL if
6391 * a temporary file could not be created.
6393 * Returns: (transfer full): a new #GFile.
6394 * Free the returned object with g_object_unref().
6399 g_file_new_tmp (const char *tmpl,
6400 GFileIOStream **iostream,
6406 GFileOutputStream *output;
6408 g_return_val_if_fail (iostream != NULL, NULL);
6410 fd = g_file_open_tmp (tmpl, &path, error);
6414 file = g_file_new_for_path (path);
6416 output = _g_local_file_output_stream_new (fd);
6417 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6419 g_object_unref (output);
6426 * g_file_parse_name:
6427 * @parse_name: a file name or path to be parsed
6429 * Constructs a #GFile with the given @parse_name (i.e. something
6430 * given by g_file_get_parse_name()). This operation never fails,
6431 * but the returned object might not support any I/O operation if
6432 * the @parse_name cannot be parsed.
6434 * Returns: (transfer full): a new #GFile.
6437 g_file_parse_name (const char *parse_name)
6439 g_return_val_if_fail (parse_name != NULL, NULL);
6441 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6445 * g_file_new_build_filename:
6446 * @first_element: (type filename): the first element in the path
6447 * @...: remaining elements in path, terminated by %NULL
6449 * Constructs a #GFile from a series of elements using the correct
6450 * separator for filenames.
6452 * Using this function is equivalent to calling g_build_filename(),
6453 * followed by g_file_new_for_path() on the result.
6455 * Returns: (transfer full): a new #GFile
6460 g_file_new_build_filename (const gchar *first_element,
6467 g_return_val_if_fail (first_element != NULL, NULL);
6469 va_start (args, first_element);
6470 str = g_build_filename_valist (first_element, &args);
6473 file = g_file_new_for_path (str);
6480 is_valid_scheme_character (char c)
6482 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6485 /* Following RFC 2396, valid schemes are built like:
6486 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6489 has_valid_scheme (const char *uri)
6495 if (!g_ascii_isalpha (*p))
6500 } while (is_valid_scheme_character (*p));
6506 new_for_cmdline_arg (const gchar *arg,
6512 if (g_path_is_absolute (arg))
6513 return g_file_new_for_path (arg);
6515 if (has_valid_scheme (arg))
6516 return g_file_new_for_uri (arg);
6522 current_dir = g_get_current_dir ();
6523 filename = g_build_filename (current_dir, arg, NULL);
6524 g_free (current_dir);
6527 filename = g_build_filename (cwd, arg, NULL);
6529 file = g_file_new_for_path (filename);
6536 * g_file_new_for_commandline_arg:
6537 * @arg: (type filename): a command line string
6539 * Creates a #GFile with the given argument from the command line.
6540 * The value of @arg can be either a URI, an absolute path or a
6541 * relative path resolved relative to the current working directory.
6542 * This operation never fails, but the returned object might not
6543 * support any I/O operation if @arg points to a malformed path.
6545 * Note that on Windows, this function expects its argument to be in
6546 * UTF-8 -- not the system code page. This means that you
6547 * should not use this function with string from argv as it is passed
6548 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6549 * the commandline. #GApplication also uses UTF-8 but
6550 * g_application_command_line_create_file_for_arg() may be more useful
6551 * for you there. It is also always possible to use this function with
6552 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6554 * Returns: (transfer full): a new #GFile.
6555 * Free the returned object with g_object_unref().
6558 g_file_new_for_commandline_arg (const char *arg)
6560 g_return_val_if_fail (arg != NULL, NULL);
6562 return new_for_cmdline_arg (arg, NULL);
6566 * g_file_new_for_commandline_arg_and_cwd:
6567 * @arg: (type filename): a command line string
6568 * @cwd: (type filename): the current working directory of the commandline
6570 * Creates a #GFile with the given argument from the command line.
6572 * This function is similar to g_file_new_for_commandline_arg() except
6573 * that it allows for passing the current working directory as an
6574 * argument instead of using the current working directory of the
6577 * This is useful if the commandline argument was given in a context
6578 * other than the invocation of the current process.
6580 * See also g_application_command_line_create_file_for_arg().
6582 * Returns: (transfer full): a new #GFile
6587 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6590 g_return_val_if_fail (arg != NULL, NULL);
6591 g_return_val_if_fail (cwd != NULL, NULL);
6593 return new_for_cmdline_arg (arg, cwd);
6597 * g_file_mount_enclosing_volume:
6598 * @location: input #GFile
6599 * @flags: flags affecting the operation
6600 * @mount_operation: (nullable): a #GMountOperation
6601 * or %NULL to avoid user interaction
6602 * @cancellable: (nullable): optional #GCancellable object,
6604 * @callback: (nullable): a #GAsyncReadyCallback to call
6605 * when the request is satisfied, or %NULL
6606 * @user_data: the data to pass to callback function
6608 * Starts a @mount_operation, mounting the volume that contains
6609 * the file @location.
6611 * When this operation has completed, @callback will be called with
6612 * @user_user data, and the operation can be finalized with
6613 * g_file_mount_enclosing_volume_finish().
6615 * If @cancellable is not %NULL, then the operation can be cancelled by
6616 * triggering the cancellable object from another thread. If the operation
6617 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6620 g_file_mount_enclosing_volume (GFile *location,
6621 GMountMountFlags flags,
6622 GMountOperation *mount_operation,
6623 GCancellable *cancellable,
6624 GAsyncReadyCallback callback,
6629 g_return_if_fail (G_IS_FILE (location));
6631 iface = G_FILE_GET_IFACE (location);
6633 if (iface->mount_enclosing_volume == NULL)
6635 g_task_report_new_error (location, callback, user_data,
6636 g_file_mount_enclosing_volume,
6637 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6638 _("volume doesn’t implement mount"));
6642 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6647 * g_file_mount_enclosing_volume_finish:
6648 * @location: input #GFile
6649 * @result: a #GAsyncResult
6650 * @error: a #GError, or %NULL
6652 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6654 * Returns: %TRUE if successful. If an error has occurred,
6655 * this function will return %FALSE and set @error
6656 * appropriately if present.
6659 g_file_mount_enclosing_volume_finish (GFile *location,
6660 GAsyncResult *result,
6665 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6666 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6668 if (g_async_result_legacy_propagate_error (result, error))
6670 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6671 return g_task_propagate_boolean (G_TASK (result), error);
6673 iface = G_FILE_GET_IFACE (location);
6675 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6678 /********************************************
6679 * Utility functions *
6680 ********************************************/
6683 * g_file_query_default_handler:
6684 * @file: a #GFile to open
6685 * @cancellable: optional #GCancellable object, %NULL to ignore
6686 * @error: a #GError, or %NULL
6688 * Returns the #GAppInfo that is registered as the default
6689 * application to handle the file specified by @file.
6691 * If @cancellable is not %NULL, then the operation can be cancelled by
6692 * triggering the cancellable object from another thread. If the operation
6693 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6695 * Returns: (transfer full): a #GAppInfo if the handle was found,
6696 * %NULL if there were errors.
6697 * When you are done with it, release it with g_object_unref()
6700 g_file_query_default_handler (GFile *file,
6701 GCancellable *cancellable,
6705 const char *content_type;
6710 uri_scheme = g_file_get_uri_scheme (file);
6711 if (uri_scheme && uri_scheme[0] != '\0')
6713 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6714 g_free (uri_scheme);
6716 if (appinfo != NULL)
6720 info = g_file_query_info (file,
6721 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6730 content_type = g_file_info_get_content_type (info);
6733 /* Don't use is_native(), as we want to support fuse paths if available */
6734 path = g_file_get_path (file);
6735 appinfo = g_app_info_get_default_for_type (content_type,
6740 g_object_unref (info);
6742 if (appinfo != NULL)
6745 g_set_error_literal (error, G_IO_ERROR,
6746 G_IO_ERROR_NOT_SUPPORTED,
6747 _("No application is registered as handling this file"));
6751 #define GET_CONTENT_BLOCK_SIZE 8192
6754 * g_file_load_contents:
6755 * @file: input #GFile
6756 * @cancellable: optional #GCancellable object, %NULL to ignore
6757 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6758 * @length: (out) (optional): a location to place the length of the contents of the file,
6759 * or %NULL if the length is not needed
6760 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6761 * or %NULL if the entity tag is not needed
6762 * @error: a #GError, or %NULL
6764 * Loads the content of the file into memory. The data is always
6765 * zero-terminated, but this is not included in the resultant @length.
6766 * The returned @content should be freed with g_free() when no longer
6769 * If @cancellable is not %NULL, then the operation can be cancelled by
6770 * triggering the cancellable object from another thread. If the operation
6771 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6773 * Returns: %TRUE if the @file's contents were successfully loaded.
6774 * %FALSE if there were errors.
6777 g_file_load_contents (GFile *file,
6778 GCancellable *cancellable,
6784 GFileInputStream *in;
6785 GByteArray *content;
6790 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6791 g_return_val_if_fail (contents != NULL, FALSE);
6793 in = g_file_read (file, cancellable, error);
6797 content = g_byte_array_new ();
6800 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6801 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6802 content->data + pos,
6803 GET_CONTENT_BLOCK_SIZE,
6804 cancellable, error)) > 0)
6807 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6814 info = g_file_input_stream_query_info (in,
6815 G_FILE_ATTRIBUTE_ETAG_VALUE,
6820 *etag_out = g_strdup (g_file_info_get_etag (info));
6821 g_object_unref (info);
6825 /* Ignore errors on close */
6826 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6827 g_object_unref (in);
6831 /* error is set already */
6832 g_byte_array_free (content, TRUE);
6839 /* Zero terminate (we got an extra byte allocated for this */
6840 content->data[pos] = 0;
6842 *contents = (char *)g_byte_array_free (content, FALSE);
6849 GFileReadMoreCallback read_more_callback;
6850 GByteArray *content;
6857 load_contents_data_free (LoadContentsData *data)
6860 g_byte_array_free (data->content, TRUE);
6861 g_free (data->etag);
6866 load_contents_close_callback (GObject *obj,
6867 GAsyncResult *close_res,
6870 GInputStream *stream = G_INPUT_STREAM (obj);
6871 LoadContentsData *data = user_data;
6873 /* Ignore errors here, we're only reading anyway */
6874 g_input_stream_close_finish (stream, close_res, NULL);
6875 g_object_unref (stream);
6877 g_task_return_boolean (data->task, TRUE);
6878 g_object_unref (data->task);
6882 load_contents_fstat_callback (GObject *obj,
6883 GAsyncResult *stat_res,
6886 GInputStream *stream = G_INPUT_STREAM (obj);
6887 LoadContentsData *data = user_data;
6890 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6894 data->etag = g_strdup (g_file_info_get_etag (info));
6895 g_object_unref (info);
6898 g_input_stream_close_async (stream, 0,
6899 g_task_get_cancellable (data->task),
6900 load_contents_close_callback, data);
6904 load_contents_read_callback (GObject *obj,
6905 GAsyncResult *read_res,
6908 GInputStream *stream = G_INPUT_STREAM (obj);
6909 LoadContentsData *data = user_data;
6910 GError *error = NULL;
6913 read_size = g_input_stream_read_finish (stream, read_res, &error);
6917 g_task_return_error (data->task, error);
6918 g_object_unref (data->task);
6920 /* Close the file ignoring any error */
6921 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6922 g_object_unref (stream);
6924 else if (read_size == 0)
6926 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6927 G_FILE_ATTRIBUTE_ETAG_VALUE,
6929 g_task_get_cancellable (data->task),
6930 load_contents_fstat_callback,
6933 else if (read_size > 0)
6935 data->pos += read_size;
6937 g_byte_array_set_size (data->content,
6938 data->pos + GET_CONTENT_BLOCK_SIZE);
6941 if (data->read_more_callback &&
6942 !data->read_more_callback ((char *)data->content->data, data->pos,
6943 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6944 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6945 G_FILE_ATTRIBUTE_ETAG_VALUE,
6947 g_task_get_cancellable (data->task),
6948 load_contents_fstat_callback,
6951 g_input_stream_read_async (stream,
6952 data->content->data + data->pos,
6953 GET_CONTENT_BLOCK_SIZE,
6955 g_task_get_cancellable (data->task),
6956 load_contents_read_callback,
6962 load_contents_open_callback (GObject *obj,
6963 GAsyncResult *open_res,
6966 GFile *file = G_FILE (obj);
6967 GFileInputStream *stream;
6968 LoadContentsData *data = user_data;
6969 GError *error = NULL;
6971 stream = g_file_read_finish (file, open_res, &error);
6975 g_byte_array_set_size (data->content,
6976 data->pos + GET_CONTENT_BLOCK_SIZE);
6977 g_input_stream_read_async (G_INPUT_STREAM (stream),
6978 data->content->data + data->pos,
6979 GET_CONTENT_BLOCK_SIZE,
6981 g_task_get_cancellable (data->task),
6982 load_contents_read_callback,
6987 g_task_return_error (data->task, error);
6988 g_object_unref (data->task);
6993 * g_file_load_partial_contents_async: (skip)
6994 * @file: input #GFile
6995 * @cancellable: optional #GCancellable object, %NULL to ignore
6996 * @read_more_callback: (scope call) (closure user_data): a
6997 * #GFileReadMoreCallback to receive partial data
6998 * and to specify whether further data should be read
6999 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7000 * when the request is satisfied
7001 * @user_data: the data to pass to the callback functions
7003 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7004 * be used to stop reading from the file when appropriate, else this
7005 * function will behave exactly as g_file_load_contents_async(). This
7006 * operation can be finished by g_file_load_partial_contents_finish().
7008 * Users of this function should be aware that @user_data is passed to
7009 * both the @read_more_callback and the @callback.
7011 * If @cancellable is not %NULL, then the operation can be cancelled by
7012 * triggering the cancellable object from another thread. If the operation
7013 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7016 g_file_load_partial_contents_async (GFile *file,
7017 GCancellable *cancellable,
7018 GFileReadMoreCallback read_more_callback,
7019 GAsyncReadyCallback callback,
7022 LoadContentsData *data;
7024 g_return_if_fail (G_IS_FILE (file));
7026 data = g_new0 (LoadContentsData, 1);
7027 data->read_more_callback = read_more_callback;
7028 data->content = g_byte_array_new ();
7030 data->task = g_task_new (file, cancellable, callback, user_data);
7031 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7032 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7034 g_file_read_async (file,
7036 g_task_get_cancellable (data->task),
7037 load_contents_open_callback,
7042 * g_file_load_partial_contents_finish:
7043 * @file: input #GFile
7044 * @res: a #GAsyncResult
7045 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7046 * @length: (out) (optional): a location to place the length of the contents of the file,
7047 * or %NULL if the length is not needed
7048 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7049 * or %NULL if the entity tag is not needed
7050 * @error: a #GError, or %NULL
7052 * Finishes an asynchronous partial load operation that was started
7053 * with g_file_load_partial_contents_async(). The data is always
7054 * zero-terminated, but this is not included in the resultant @length.
7055 * The returned @content should be freed with g_free() when no longer
7058 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7059 * present, it will be set appropriately.
7062 g_file_load_partial_contents_finish (GFile *file,
7070 LoadContentsData *data;
7072 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7073 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7074 g_return_val_if_fail (contents != NULL, FALSE);
7076 task = G_TASK (res);
7078 if (!g_task_propagate_boolean (task, error))
7085 data = g_task_get_task_data (task);
7088 *length = data->pos;
7092 *etag_out = data->etag;
7096 /* Zero terminate */
7097 g_byte_array_set_size (data->content, data->pos + 1);
7098 data->content->data[data->pos] = 0;
7100 *contents = (char *)g_byte_array_free (data->content, FALSE);
7101 data->content = NULL;
7107 * g_file_load_contents_async:
7108 * @file: input #GFile
7109 * @cancellable: optional #GCancellable object, %NULL to ignore
7110 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7111 * @user_data: the data to pass to callback function
7113 * Starts an asynchronous load of the @file's contents.
7115 * For more details, see g_file_load_contents() which is
7116 * the synchronous version of this call.
7118 * When the load operation has completed, @callback will be called
7119 * with @user data. To finish the operation, call
7120 * g_file_load_contents_finish() with the #GAsyncResult returned by
7123 * If @cancellable is not %NULL, then the operation can be cancelled by
7124 * triggering the cancellable object from another thread. If the operation
7125 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7128 g_file_load_contents_async (GFile *file,
7129 GCancellable *cancellable,
7130 GAsyncReadyCallback callback,
7133 g_file_load_partial_contents_async (file,
7136 callback, user_data);
7140 * g_file_load_contents_finish:
7141 * @file: input #GFile
7142 * @res: a #GAsyncResult
7143 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7144 * @length: (out) (optional): a location to place the length of the contents of the file,
7145 * or %NULL if the length is not needed
7146 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7147 * or %NULL if the entity tag is not needed
7148 * @error: a #GError, or %NULL
7150 * Finishes an asynchronous load of the @file's contents.
7151 * The contents are placed in @contents, and @length is set to the
7152 * size of the @contents string. The @content should be freed with
7153 * g_free() when no longer needed. If @etag_out is present, it will be
7154 * set to the new entity tag for the @file.
7156 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7157 * present, it will be set appropriately.
7160 g_file_load_contents_finish (GFile *file,
7167 return g_file_load_partial_contents_finish (file,
7176 * g_file_replace_contents:
7177 * @file: input #GFile
7178 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7179 * @length: the length of @contents in bytes
7180 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7182 * @make_backup: %TRUE if a backup should be created
7183 * @flags: a set of #GFileCreateFlags
7184 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7185 * for the document. This should be freed with g_free() when no longer
7187 * @cancellable: optional #GCancellable object, %NULL to ignore
7188 * @error: a #GError, or %NULL
7190 * Replaces the contents of @file with @contents of @length bytes.
7192 * If @etag is specified (not %NULL), any existing file must have that etag,
7193 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7195 * If @make_backup is %TRUE, this function will attempt to make a backup
7196 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7197 * file contents in the safest way possible. For example, atomic renames are
7198 * used when replacing local files’ contents.
7200 * If @cancellable is not %NULL, then the operation can be cancelled by
7201 * triggering the cancellable object from another thread. If the operation
7202 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7204 * The returned @new_etag can be used to verify that the file hasn't
7205 * changed the next time it is saved over.
7207 * Returns: %TRUE if successful. If an error has occurred, this function
7208 * will return %FALSE and set @error appropriately if present.
7211 g_file_replace_contents (GFile *file,
7212 const char *contents,
7215 gboolean make_backup,
7216 GFileCreateFlags flags,
7218 GCancellable *cancellable,
7221 GFileOutputStream *out;
7222 gsize pos, remainder;
7226 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7227 g_return_val_if_fail (contents != NULL, FALSE);
7229 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7235 while (remainder > 0 &&
7236 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7238 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7246 if (remainder > 0 && res < 0)
7248 /* Ignore errors on close */
7249 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7250 g_object_unref (out);
7252 /* error is set already */
7256 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7259 *new_etag = g_file_output_stream_get_etag (out);
7261 g_object_unref (out);
7272 } ReplaceContentsData;
7275 replace_contents_data_free (ReplaceContentsData *data)
7277 g_bytes_unref (data->content);
7278 g_free (data->etag);
7283 replace_contents_close_callback (GObject *obj,
7284 GAsyncResult *close_res,
7287 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7288 ReplaceContentsData *data = user_data;
7290 /* Ignore errors here, we're only reading anyway */
7291 g_output_stream_close_finish (stream, close_res, NULL);
7292 g_object_unref (stream);
7296 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7297 g_task_return_boolean (data->task, TRUE);
7299 g_object_unref (data->task);
7303 replace_contents_write_callback (GObject *obj,
7304 GAsyncResult *read_res,
7307 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7308 ReplaceContentsData *data = user_data;
7309 GError *error = NULL;
7312 write_size = g_output_stream_write_finish (stream, read_res, &error);
7314 if (write_size <= 0)
7316 /* Error or EOF, close the file */
7319 data->failed = TRUE;
7320 g_task_return_error (data->task, error);
7322 g_output_stream_close_async (stream, 0,
7323 g_task_get_cancellable (data->task),
7324 replace_contents_close_callback, data);
7326 else if (write_size > 0)
7328 const gchar *content;
7331 content = g_bytes_get_data (data->content, &length);
7332 data->pos += write_size;
7334 if (data->pos >= length)
7335 g_output_stream_close_async (stream, 0,
7336 g_task_get_cancellable (data->task),
7337 replace_contents_close_callback, data);
7339 g_output_stream_write_async (stream,
7340 content + data->pos,
7343 g_task_get_cancellable (data->task),
7344 replace_contents_write_callback,
7350 replace_contents_open_callback (GObject *obj,
7351 GAsyncResult *open_res,
7354 GFile *file = G_FILE (obj);
7355 GFileOutputStream *stream;
7356 ReplaceContentsData *data = user_data;
7357 GError *error = NULL;
7359 stream = g_file_replace_finish (file, open_res, &error);
7363 const gchar *content;
7366 content = g_bytes_get_data (data->content, &length);
7367 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7368 content + data->pos,
7371 g_task_get_cancellable (data->task),
7372 replace_contents_write_callback,
7377 g_task_return_error (data->task, error);
7378 g_object_unref (data->task);
7383 * g_file_replace_contents_async:
7384 * @file: input #GFile
7385 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7386 * @length: the length of @contents in bytes
7387 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7388 * @make_backup: %TRUE if a backup should be created
7389 * @flags: a set of #GFileCreateFlags
7390 * @cancellable: optional #GCancellable object, %NULL to ignore
7391 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7392 * @user_data: the data to pass to callback function
7394 * Starts an asynchronous replacement of @file with the given
7395 * @contents of @length bytes. @etag will replace the document's
7396 * current entity tag.
7398 * When this operation has completed, @callback will be called with
7399 * @user_user data, and the operation can be finalized with
7400 * g_file_replace_contents_finish().
7402 * If @cancellable is not %NULL, then the operation can be cancelled by
7403 * triggering the cancellable object from another thread. If the operation
7404 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7406 * If @make_backup is %TRUE, this function will attempt to
7407 * make a backup of @file.
7409 * Note that no copy of @content will be made, so it must stay valid
7410 * until @callback is called. See g_file_replace_contents_bytes_async()
7411 * for a #GBytes version that will automatically hold a reference to the
7412 * contents (without copying) for the duration of the call.
7415 g_file_replace_contents_async (GFile *file,
7416 const char *contents,
7419 gboolean make_backup,
7420 GFileCreateFlags flags,
7421 GCancellable *cancellable,
7422 GAsyncReadyCallback callback,
7427 bytes = g_bytes_new_static (contents, length);
7428 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7429 cancellable, callback, user_data);
7430 g_bytes_unref (bytes);
7434 * g_file_replace_contents_bytes_async:
7435 * @file: input #GFile
7436 * @contents: a #GBytes
7437 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7438 * @make_backup: %TRUE if a backup should be created
7439 * @flags: a set of #GFileCreateFlags
7440 * @cancellable: optional #GCancellable object, %NULL to ignore
7441 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7442 * @user_data: the data to pass to callback function
7444 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7445 * This function will keep a ref on @contents until the operation is done.
7446 * Unlike g_file_replace_contents_async() this allows forgetting about the
7447 * content without waiting for the callback.
7449 * When this operation has completed, @callback will be called with
7450 * @user_user data, and the operation can be finalized with
7451 * g_file_replace_contents_finish().
7456 g_file_replace_contents_bytes_async (GFile *file,
7459 gboolean make_backup,
7460 GFileCreateFlags flags,
7461 GCancellable *cancellable,
7462 GAsyncReadyCallback callback,
7465 ReplaceContentsData *data;
7467 g_return_if_fail (G_IS_FILE (file));
7468 g_return_if_fail (contents != NULL);
7470 data = g_new0 (ReplaceContentsData, 1);
7472 data->content = g_bytes_ref (contents);
7474 data->task = g_task_new (file, cancellable, callback, user_data);
7475 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7476 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7478 g_file_replace_async (file,
7483 g_task_get_cancellable (data->task),
7484 replace_contents_open_callback,
7489 * g_file_replace_contents_finish:
7490 * @file: input #GFile
7491 * @res: a #GAsyncResult
7492 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7493 * for the document. This should be freed with g_free() when it is no
7494 * longer needed, or %NULL
7495 * @error: a #GError, or %NULL
7497 * Finishes an asynchronous replace of the given @file. See
7498 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7499 * tag for the document, if present.
7501 * Returns: %TRUE on success, %FALSE on failure.
7504 g_file_replace_contents_finish (GFile *file,
7510 ReplaceContentsData *data;
7512 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7513 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7515 task = G_TASK (res);
7517 if (!g_task_propagate_boolean (task, error))
7520 data = g_task_get_task_data (task);
7524 *new_etag = data->etag;
7525 data->etag = NULL; /* Take ownership */
7532 g_file_real_measure_disk_usage (GFile *file,
7533 GFileMeasureFlags flags,
7534 GCancellable *cancellable,
7535 GFileMeasureProgressCallback progress_callback,
7536 gpointer progress_data,
7537 guint64 *disk_usage,
7542 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7543 "Operation not supported for the current backend.");
7549 GFileMeasureFlags flags;
7550 GFileMeasureProgressCallback progress_callback;
7551 gpointer progress_data;
7563 GFileMeasureProgressCallback callback;
7566 guint64 current_size;
7572 measure_disk_usage_invoke_progress (gpointer user_data)
7574 MeasureProgress *progress = user_data;
7576 (* progress->callback) (progress->reporting,
7577 progress->current_size, progress->num_dirs, progress->num_files,
7578 progress->user_data);
7584 measure_disk_usage_progress (gboolean reporting,
7585 guint64 current_size,
7590 MeasureProgress progress;
7591 GTask *task = user_data;
7592 MeasureTaskData *data;
7594 data = g_task_get_task_data (task);
7596 progress.callback = data->progress_callback;
7597 progress.user_data = data->progress_data;
7598 progress.reporting = reporting;
7599 progress.current_size = current_size;
7600 progress.num_dirs = num_dirs;
7601 progress.num_files = num_files;
7603 g_main_context_invoke_full (g_task_get_context (task),
7604 g_task_get_priority (task),
7605 measure_disk_usage_invoke_progress,
7606 g_memdup (&progress, sizeof progress),
7611 measure_disk_usage_thread (GTask *task,
7612 gpointer source_object,
7614 GCancellable *cancellable)
7616 MeasureTaskData *data = task_data;
7617 GError *error = NULL;
7618 MeasureResult result = { 0, };
7620 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7621 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7622 &result.disk_usage, &result.num_dirs, &result.num_files,
7624 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7626 g_task_return_error (task, error);
7630 g_file_real_measure_disk_usage_async (GFile *file,
7631 GFileMeasureFlags flags,
7633 GCancellable *cancellable,
7634 GFileMeasureProgressCallback progress_callback,
7635 gpointer progress_data,
7636 GAsyncReadyCallback callback,
7639 MeasureTaskData data;
7643 data.progress_callback = progress_callback;
7644 data.progress_data = progress_data;
7646 task = g_task_new (file, cancellable, callback, user_data);
7647 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7648 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7649 g_task_set_priority (task, io_priority);
7651 g_task_run_in_thread (task, measure_disk_usage_thread);
7652 g_object_unref (task);
7656 g_file_real_measure_disk_usage_finish (GFile *file,
7657 GAsyncResult *result,
7658 guint64 *disk_usage,
7663 MeasureResult *measure_result;
7665 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7667 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7669 if (measure_result == NULL)
7673 *disk_usage = measure_result->disk_usage;
7676 *num_dirs = measure_result->num_dirs;
7679 *num_files = measure_result->num_files;
7681 g_free (measure_result);
7687 * g_file_measure_disk_usage:
7689 * @flags: #GFileMeasureFlags
7690 * @cancellable: (nullable): optional #GCancellable
7691 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7692 * @progress_data: user_data for @progress_callback
7693 * @disk_usage: (out) (optional): the number of bytes of disk space used
7694 * @num_dirs: (out) (optional): the number of directories encountered
7695 * @num_files: (out) (optional): the number of non-directories encountered
7696 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7698 * Recursively measures the disk usage of @file.
7700 * This is essentially an analog of the 'du' command, but it also
7701 * reports the number of directories and non-directory files encountered
7702 * (including things like symbolic links).
7704 * By default, errors are only reported against the toplevel file
7705 * itself. Errors found while recursing are silently ignored, unless
7706 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7708 * The returned size, @disk_usage, is in bytes and should be formatted
7709 * with g_format_size() in order to get something reasonable for showing
7710 * in a user interface.
7712 * @progress_callback and @progress_data can be given to request
7713 * periodic progress updates while scanning. See the documentation for
7714 * #GFileMeasureProgressCallback for information about when and how the
7715 * callback will be invoked.
7717 * Returns: %TRUE if successful, with the out parameters set.
7718 * %FALSE otherwise, with @error set.
7723 g_file_measure_disk_usage (GFile *file,
7724 GFileMeasureFlags flags,
7725 GCancellable *cancellable,
7726 GFileMeasureProgressCallback progress_callback,
7727 gpointer progress_data,
7728 guint64 *disk_usage,
7733 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7734 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7735 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7737 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7738 progress_callback, progress_data,
7739 disk_usage, num_dirs, num_files,
7744 * g_file_measure_disk_usage_async:
7746 * @flags: #GFileMeasureFlags
7747 * @io_priority: the [I/O priority][io-priority] of the request
7748 * @cancellable: (nullable): optional #GCancellable
7749 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7750 * @progress_data: user_data for @progress_callback
7751 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7752 * @user_data: the data to pass to callback function
7754 * Recursively measures the disk usage of @file.
7756 * This is the asynchronous version of g_file_measure_disk_usage(). See
7757 * there for more information.
7762 g_file_measure_disk_usage_async (GFile *file,
7763 GFileMeasureFlags flags,
7765 GCancellable *cancellable,
7766 GFileMeasureProgressCallback progress_callback,
7767 gpointer progress_data,
7768 GAsyncReadyCallback callback,
7771 g_return_if_fail (G_IS_FILE (file));
7772 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7774 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7775 progress_callback, progress_data,
7776 callback, user_data);
7780 * g_file_measure_disk_usage_finish:
7782 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7783 * @disk_usage: (out) (optional): the number of bytes of disk space used
7784 * @num_dirs: (out) (optional): the number of directories encountered
7785 * @num_files: (out) (optional): the number of non-directories encountered
7786 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7788 * Collects the results from an earlier call to
7789 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7792 * Returns: %TRUE if successful, with the out parameters set.
7793 * %FALSE otherwise, with @error set.
7798 g_file_measure_disk_usage_finish (GFile *file,
7799 GAsyncResult *result,
7800 guint64 *disk_usage,
7805 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7806 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7808 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7812 * g_file_start_mountable:
7813 * @file: input #GFile
7814 * @flags: flags affecting the operation
7815 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7816 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7817 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7818 * @user_data: the data to pass to callback function
7820 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7821 * Using @start_operation, you can request callbacks when, for instance,
7822 * passwords are needed during authentication.
7824 * If @cancellable is not %NULL, then the operation can be cancelled by
7825 * triggering the cancellable object from another thread. If the operation
7826 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7828 * When the operation is finished, @callback will be called.
7829 * You can then call g_file_mount_mountable_finish() to get
7830 * the result of the operation.
7835 g_file_start_mountable (GFile *file,
7836 GDriveStartFlags flags,
7837 GMountOperation *start_operation,
7838 GCancellable *cancellable,
7839 GAsyncReadyCallback callback,
7844 g_return_if_fail (G_IS_FILE (file));
7846 iface = G_FILE_GET_IFACE (file);
7848 if (iface->start_mountable == NULL)
7850 g_task_report_new_error (file, callback, user_data,
7851 g_file_start_mountable,
7852 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7853 _("Operation not supported"));
7857 (* iface->start_mountable) (file,
7866 * g_file_start_mountable_finish:
7867 * @file: input #GFile
7868 * @result: a #GAsyncResult
7869 * @error: a #GError, or %NULL
7871 * Finishes a start operation. See g_file_start_mountable() for details.
7873 * Finish an asynchronous start operation that was started
7874 * with g_file_start_mountable().
7876 * Returns: %TRUE if the operation finished successfully. %FALSE
7882 g_file_start_mountable_finish (GFile *file,
7883 GAsyncResult *result,
7888 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7889 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7891 if (g_async_result_legacy_propagate_error (result, error))
7893 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7894 return g_task_propagate_boolean (G_TASK (result), error);
7896 iface = G_FILE_GET_IFACE (file);
7897 return (* iface->start_mountable_finish) (file, result, error);
7901 * g_file_stop_mountable:
7902 * @file: input #GFile
7903 * @flags: flags affecting the operation
7904 * @mount_operation: (nullable): a #GMountOperation,
7905 * or %NULL to avoid user interaction.
7906 * @cancellable: (nullable): optional #GCancellable object,
7908 * @callback: (nullable): a #GAsyncReadyCallback to call
7909 * when the request is satisfied, or %NULL
7910 * @user_data: the data to pass to callback function
7912 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7914 * If @cancellable is not %NULL, then the operation can be cancelled by
7915 * triggering the cancellable object from another thread. If the operation
7916 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7918 * When the operation is finished, @callback will be called.
7919 * You can then call g_file_stop_mountable_finish() to get
7920 * the result of the operation.
7925 g_file_stop_mountable (GFile *file,
7926 GMountUnmountFlags flags,
7927 GMountOperation *mount_operation,
7928 GCancellable *cancellable,
7929 GAsyncReadyCallback callback,
7934 g_return_if_fail (G_IS_FILE (file));
7936 iface = G_FILE_GET_IFACE (file);
7938 if (iface->stop_mountable == NULL)
7940 g_task_report_new_error (file, callback, user_data,
7941 g_file_stop_mountable,
7942 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7943 _("Operation not supported"));
7947 (* iface->stop_mountable) (file,
7956 * g_file_stop_mountable_finish:
7957 * @file: input #GFile
7958 * @result: a #GAsyncResult
7959 * @error: a #GError, or %NULL
7961 * Finishes an stop operation, see g_file_stop_mountable() for details.
7963 * Finish an asynchronous stop operation that was started
7964 * with g_file_stop_mountable().
7966 * Returns: %TRUE if the operation finished successfully.
7972 g_file_stop_mountable_finish (GFile *file,
7973 GAsyncResult *result,
7978 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7979 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7981 if (g_async_result_legacy_propagate_error (result, error))
7983 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7984 return g_task_propagate_boolean (G_TASK (result), error);
7986 iface = G_FILE_GET_IFACE (file);
7987 return (* iface->stop_mountable_finish) (file, result, error);
7991 * g_file_poll_mountable:
7992 * @file: input #GFile
7993 * @cancellable: optional #GCancellable object, %NULL to ignore
7994 * @callback: (nullable): a #GAsyncReadyCallback to call
7995 * when the request is satisfied, or %NULL
7996 * @user_data: the data to pass to callback function
7998 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8000 * If @cancellable is not %NULL, then the operation can be cancelled by
8001 * triggering the cancellable object from another thread. If the operation
8002 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8004 * When the operation is finished, @callback will be called.
8005 * You can then call g_file_mount_mountable_finish() to get
8006 * the result of the operation.
8011 g_file_poll_mountable (GFile *file,
8012 GCancellable *cancellable,
8013 GAsyncReadyCallback callback,
8018 g_return_if_fail (G_IS_FILE (file));
8020 iface = G_FILE_GET_IFACE (file);
8022 if (iface->poll_mountable == NULL)
8024 g_task_report_new_error (file, callback, user_data,
8025 g_file_poll_mountable,
8026 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8027 _("Operation not supported"));
8031 (* iface->poll_mountable) (file,
8038 * g_file_poll_mountable_finish:
8039 * @file: input #GFile
8040 * @result: a #GAsyncResult
8041 * @error: a #GError, or %NULL
8043 * Finishes a poll operation. See g_file_poll_mountable() for details.
8045 * Finish an asynchronous poll operation that was polled
8046 * with g_file_poll_mountable().
8048 * Returns: %TRUE if the operation finished successfully. %FALSE
8054 g_file_poll_mountable_finish (GFile *file,
8055 GAsyncResult *result,
8060 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8061 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8063 if (g_async_result_legacy_propagate_error (result, error))
8065 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8066 return g_task_propagate_boolean (G_TASK (result), error);
8068 iface = G_FILE_GET_IFACE (file);
8069 return (* iface->poll_mountable_finish) (file, result, error);
8073 * g_file_supports_thread_contexts:
8076 * Checks if @file supports
8077 * [thread-default contexts][g-main-context-push-thread-default-context].
8078 * If this returns %FALSE, you cannot perform asynchronous operations on
8079 * @file in a thread that has a thread-default context.
8081 * Returns: Whether or not @file supports thread-default contexts.
8086 g_file_supports_thread_contexts (GFile *file)
8090 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8092 iface = G_FILE_GET_IFACE (file);
8093 return iface->supports_thread_contexts;
8097 * g_file_load_bytes:
8099 * @cancellable: (nullable): a #GCancellable or %NULL
8100 * @etag_out: (out) (nullable) (optional): a location to place the current
8101 * entity tag for the file, or %NULL if the entity tag is not needed
8102 * @error: a location for a #GError or %NULL
8104 * Loads the contents of @file and returns it as #GBytes.
8106 * If @file is a resource:// based URI, the resulting bytes will reference the
8107 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8108 * g_file_load_contents() and g_bytes_new_take().
8110 * For resources, @etag_out will be set to %NULL.
8112 * The data contained in the resulting #GBytes is always zero-terminated, but
8113 * this is not included in the #GBytes length. The resulting #GBytes should be
8114 * freed with g_bytes_unref() when no longer in use.
8116 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8121 g_file_load_bytes (GFile *file,
8122 GCancellable *cancellable,
8129 g_return_val_if_fail (G_IS_FILE (file), NULL);
8130 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8131 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8133 if (etag_out != NULL)
8136 if (g_file_has_uri_scheme (file, "resource"))
8139 gchar *uri, *unescaped;
8141 uri = g_file_get_uri (file);
8142 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8145 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8151 /* contents is guaranteed to be \0 terminated */
8152 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8153 return g_bytes_new_take (g_steal_pointer (&contents), len);
8159 g_file_load_bytes_cb (GObject *object,
8160 GAsyncResult *result,
8163 GFile *file = G_FILE (object);
8164 GTask *task = user_data;
8165 GError *error = NULL;
8167 gchar *contents = NULL;
8170 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8171 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8174 g_task_return_error (task, g_steal_pointer (&error));
8176 g_task_return_pointer (task,
8177 g_bytes_new_take (g_steal_pointer (&contents), len),
8178 (GDestroyNotify)g_bytes_unref);
8180 g_object_unref (task);
8184 * g_file_load_bytes_async:
8186 * @cancellable: (nullable): a #GCancellable or %NULL
8187 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8188 * request is satisfied
8189 * @user_data: (closure): the data to pass to callback function
8191 * Asynchronously loads the contents of @file as #GBytes.
8193 * If @file is a resource:// based URI, the resulting bytes will reference the
8194 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8195 * g_file_load_contents_async() and g_bytes_new_take().
8197 * @callback should call g_file_load_bytes_finish() to get the result of this
8198 * asynchronous operation.
8200 * See g_file_load_bytes() for more information.
8205 g_file_load_bytes_async (GFile *file,
8206 GCancellable *cancellable,
8207 GAsyncReadyCallback callback,
8210 GError *error = NULL;
8214 g_return_if_fail (G_IS_FILE (file));
8215 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8217 task = g_task_new (file, cancellable, callback, user_data);
8218 g_task_set_source_tag (task, g_file_load_bytes_async);
8220 if (!g_file_has_uri_scheme (file, "resource"))
8222 g_file_load_contents_async (file,
8224 g_file_load_bytes_cb,
8225 g_steal_pointer (&task));
8229 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8232 g_task_return_error (task, g_steal_pointer (&error));
8234 g_task_return_pointer (task,
8235 g_steal_pointer (&bytes),
8236 (GDestroyNotify)g_bytes_unref);
8238 g_object_unref (task);
8242 * g_file_load_bytes_finish:
8244 * @result: a #GAsyncResult provided to the callback
8245 * @etag_out: (out) (nullable) (optional): a location to place the current
8246 * entity tag for the file, or %NULL if the entity tag is not needed
8247 * @error: a location for a #GError, or %NULL
8249 * Completes an asynchronous request to g_file_load_bytes_async().
8251 * For resources, @etag_out will be set to %NULL.
8253 * The data contained in the resulting #GBytes is always zero-terminated, but
8254 * this is not included in the #GBytes length. The resulting #GBytes should be
8255 * freed with g_bytes_unref() when no longer in use.
8257 * See g_file_load_bytes() for more information.
8259 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8264 g_file_load_bytes_finish (GFile *file,
8265 GAsyncResult *result,
8271 g_return_val_if_fail (G_IS_FILE (file), NULL);
8272 g_return_val_if_fail (G_IS_TASK (result), NULL);
8273 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8274 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8276 bytes = g_task_propagate_pointer (G_TASK (result), error);
8278 if (etag_out != NULL)
8279 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));