Initialize variable
[glib.git] / gio / gfile.c
blob334ad8ec3885a4f977b460bb18014e4c25d7bbf3
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
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>
23 #include "config.h"
25 #ifdef __linux__
26 #include <sys/ioctl.h>
27 #include <errno.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)
31 #endif
33 #ifdef HAVE_SPLICE
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #endif
40 #include <string.h>
41 #include <sys/types.h>
43 #include "gfile.h"
44 #include "glib/gstdio.h"
45 #ifdef G_OS_UNIX
46 #include "glib-unix.h"
47 #endif
48 #include "gvfs.h"
49 #include "gtask.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
53 #include "gappinfo.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"
61 #include "gioerror.h"
62 #include "glibintl.h"
65 /**
66 * SECTION:gfile
67 * @short_description: File and Directory Handling
68 * @include: gio/gio.h
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
98 * files.
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
110 * the actual name.
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
135 * more.
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
149 * HTTP 1.1
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,
157 int io_priority,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
160 gpointer user_data);
161 static GFileInfo * g_file_real_query_info_finish (GFile *file,
162 GAsyncResult *res,
163 GError **error);
164 static void g_file_real_query_filesystem_info_async (GFile *file,
165 const char *attributes,
166 int io_priority,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
169 gpointer user_data);
170 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
171 GAsyncResult *res,
172 GError **error);
173 static void g_file_real_enumerate_children_async (GFile *file,
174 const char *attributes,
175 GFileQueryInfoFlags flags,
176 int io_priority,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
179 gpointer user_data);
180 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
181 GAsyncResult *res,
182 GError **error);
183 static void g_file_real_read_async (GFile *file,
184 int io_priority,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
187 gpointer user_data);
188 static GFileInputStream * g_file_real_read_finish (GFile *file,
189 GAsyncResult *res,
190 GError **error);
191 static void g_file_real_append_to_async (GFile *file,
192 GFileCreateFlags flags,
193 int io_priority,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
196 gpointer user_data);
197 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
198 GAsyncResult *res,
199 GError **error);
200 static void g_file_real_create_async (GFile *file,
201 GFileCreateFlags flags,
202 int io_priority,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
205 gpointer user_data);
206 static GFileOutputStream *g_file_real_create_finish (GFile *file,
207 GAsyncResult *res,
208 GError **error);
209 static void g_file_real_replace_async (GFile *file,
210 const char *etag,
211 gboolean make_backup,
212 GFileCreateFlags flags,
213 int io_priority,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
216 gpointer user_data);
217 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
218 GAsyncResult *res,
219 GError **error);
220 static void g_file_real_delete_async (GFile *file,
221 int io_priority,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
224 gpointer user_data);
225 static gboolean g_file_real_delete_finish (GFile *file,
226 GAsyncResult *res,
227 GError **error);
228 static void g_file_real_trash_async (GFile *file,
229 int io_priority,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
232 gpointer user_data);
233 static gboolean g_file_real_trash_finish (GFile *file,
234 GAsyncResult *res,
235 GError **error);
236 static void g_file_real_make_directory_async (GFile *file,
237 int io_priority,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
240 gpointer user_data);
241 static gboolean g_file_real_make_directory_finish (GFile *file,
242 GAsyncResult *res,
243 GError **error);
244 static void g_file_real_open_readwrite_async (GFile *file,
245 int io_priority,
246 GCancellable *cancellable,
247 GAsyncReadyCallback callback,
248 gpointer user_data);
249 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
250 GAsyncResult *res,
251 GError **error);
252 static void g_file_real_create_readwrite_async (GFile *file,
253 GFileCreateFlags flags,
254 int io_priority,
255 GCancellable *cancellable,
256 GAsyncReadyCallback callback,
257 gpointer user_data);
258 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
259 GAsyncResult *res,
260 GError **error);
261 static void g_file_real_replace_readwrite_async (GFile *file,
262 const char *etag,
263 gboolean make_backup,
264 GFileCreateFlags flags,
265 int io_priority,
266 GCancellable *cancellable,
267 GAsyncReadyCallback callback,
268 gpointer user_data);
269 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
270 GAsyncResult *res,
271 GError **error);
272 static gboolean g_file_real_set_attributes_from_info (GFile *file,
273 GFileInfo *info,
274 GFileQueryInfoFlags flags,
275 GCancellable *cancellable,
276 GError **error);
277 static void g_file_real_set_display_name_async (GFile *file,
278 const char *display_name,
279 int io_priority,
280 GCancellable *cancellable,
281 GAsyncReadyCallback callback,
282 gpointer user_data);
283 static GFile * g_file_real_set_display_name_finish (GFile *file,
284 GAsyncResult *res,
285 GError **error);
286 static void g_file_real_set_attributes_async (GFile *file,
287 GFileInfo *info,
288 GFileQueryInfoFlags flags,
289 int io_priority,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
292 gpointer user_data);
293 static gboolean g_file_real_set_attributes_finish (GFile *file,
294 GAsyncResult *res,
295 GFileInfo **info,
296 GError **error);
297 static void g_file_real_find_enclosing_mount_async (GFile *file,
298 int io_priority,
299 GCancellable *cancellable,
300 GAsyncReadyCallback callback,
301 gpointer user_data);
302 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
303 GAsyncResult *res,
304 GError **error);
305 static void g_file_real_copy_async (GFile *source,
306 GFile *destination,
307 GFileCopyFlags flags,
308 int io_priority,
309 GCancellable *cancellable,
310 GFileProgressCallback progress_callback,
311 gpointer progress_callback_data,
312 GAsyncReadyCallback callback,
313 gpointer user_data);
314 static gboolean g_file_real_copy_finish (GFile *file,
315 GAsyncResult *res,
316 GError **error);
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,
323 guint64 *disk_usage,
324 guint64 *num_dirs,
325 guint64 *num_files,
326 GError **error);
327 static void g_file_real_measure_disk_usage_async (GFile *file,
328 GFileMeasureFlags flags,
329 gint io_priority,
330 GCancellable *cancellable,
331 GFileMeasureProgressCallback progress_callback,
332 gpointer progress_data,
333 GAsyncReadyCallback callback,
334 gpointer user_data);
335 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
336 GAsyncResult *result,
337 guint64 *disk_usage,
338 guint64 *num_dirs,
339 guint64 *num_files,
340 GError **error);
342 typedef GFileIface GFileInterface;
343 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
345 static void
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;
390 * g_file_is_native:
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
407 gboolean
408 g_file_is_native (GFile *file)
410 GFileIface *iface;
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.
433 gboolean
434 g_file_has_uri_scheme (GFile *file,
435 const char *uri_scheme)
437 GFileIface *iface;
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:
454 * |[
455 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
456 * ]|
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.
465 char *
466 g_file_get_uri_scheme (GFile *file)
468 GFileIface *iface;
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.
500 char *
501 g_file_get_basename (GFile *file)
503 GFileIface *iface;
505 g_return_val_if_fail (G_IS_FILE (file), NULL);
507 iface = G_FILE_GET_IFACE (file);
509 return (* iface->get_basename) (file);
513 * g_file_get_path:
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.
525 char *
526 g_file_get_path (GFile *file)
528 GFileIface *iface;
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
537 /* Original commit introducing this in libgsystem:
539 * fileutil: Handle recent: and trash: URIs
541 * The gs_file_get_path_cached() was rather brittle in its handling
542 * of URIs. It would assert() when a GFile didn't have a backing path
543 * (such as when handling trash: or recent: URIs), and didn't know
544 * how to get the target URI for those items either.
546 * Make sure that we do not assert() when a backing path cannot be
547 * found, and handle recent: and trash: URIs.
549 * https://bugzilla.gnome.org/show_bug.cgi?id=708435
551 static char *
552 file_get_target_path (GFile *file)
554 GFileInfo *info;
555 const char *target;
556 char *path;
558 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, NULL);
559 if (info == NULL)
560 return NULL;
561 target = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI);
562 path = g_filename_from_uri (target, NULL, NULL);
563 g_object_unref (info);
565 return path;
568 static const char *
569 file_peek_path_generic (GFile *file)
571 const char *path;
572 static GQuark _file_path_quark = 0;
574 if (G_UNLIKELY (_file_path_quark) == 0)
575 _file_path_quark = g_quark_from_static_string ("gio-file-path");
577 /* We need to be careful about threading, as two threads calling
578 * g_file_peek_path() on the same file could race: both would see
579 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
580 * the path, but the second thread to add it would end up freeing the path
581 * set by the first thread. The first thread would still return the pointer
582 * to that freed path, though, resulting an a read-after-free. Handle that
583 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
585 while (TRUE)
587 gchar *new_path = NULL;
589 path = g_object_get_qdata ((GObject*)file, _file_path_quark);
591 if (path != NULL)
592 break;
594 if (g_file_has_uri_scheme (file, "trash") ||
595 g_file_has_uri_scheme (file, "recent"))
596 new_path = file_get_target_path (file);
597 else
598 new_path = g_file_get_path (file);
599 if (new_path == NULL)
600 return NULL;
602 /* By passing NULL here, we ensure we never replace existing data: */
603 if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
604 NULL, (gpointer) new_path,
605 (GDestroyNotify) g_free, NULL))
606 break;
607 else
608 g_free (new_path);
611 return path;
615 * g_file_peek_path:
616 * @file: input #GFile
618 * Exactly like g_file_get_path(), but caches the result via
619 * g_object_set_qdata_full(). This is useful for example in C
620 * applications which mix `g_file_*` APIs with native ones. It
621 * also avoids an extra duplicated string when possible, so will be
622 * generally more efficient.
624 * This call does no blocking I/O.
626 * Returns: (type filename) (nullable): string containing the #GFile's path,
627 * or %NULL if no such path exists. The returned string is owned by @file.
628 * Since: 2.56
630 const char *
631 g_file_peek_path (GFile *file)
633 if (G_IS_LOCAL_FILE (file))
634 return _g_local_file_get_filename ((GLocalFile *) file);
635 return file_peek_path_generic (file);
639 * g_file_get_uri:
640 * @file: input #GFile
642 * Gets the URI for the @file.
644 * This call does no blocking I/O.
646 * Returns: a string containing the #GFile's URI.
647 * The returned string should be freed with g_free()
648 * when no longer needed.
650 char *
651 g_file_get_uri (GFile *file)
653 GFileIface *iface;
655 g_return_val_if_fail (G_IS_FILE (file), NULL);
657 iface = G_FILE_GET_IFACE (file);
659 return (* iface->get_uri) (file);
663 * g_file_get_parse_name:
664 * @file: input #GFile
666 * Gets the parse name of the @file.
667 * A parse name is a UTF-8 string that describes the
668 * file such that one can get the #GFile back using
669 * g_file_parse_name().
671 * This is generally used to show the #GFile as a nice
672 * full-pathname kind of string in a user interface,
673 * like in a location entry.
675 * For local files with names that can safely be converted
676 * to UTF-8 the pathname is used, otherwise the IRI is used
677 * (a form of URI that allows UTF-8 characters unescaped).
679 * This call does no blocking I/O.
681 * Returns: a string containing the #GFile's parse name.
682 * The returned string should be freed with g_free()
683 * when no longer needed.
685 char *
686 g_file_get_parse_name (GFile *file)
688 GFileIface *iface;
690 g_return_val_if_fail (G_IS_FILE (file), NULL);
692 iface = G_FILE_GET_IFACE (file);
694 return (* iface->get_parse_name) (file);
698 * g_file_dup:
699 * @file: input #GFile
701 * Duplicates a #GFile handle. This operation does not duplicate
702 * the actual file or directory represented by the #GFile; see
703 * g_file_copy() if attempting to copy a file.
705 * This call does no blocking I/O.
707 * Returns: (transfer full): a new #GFile that is a duplicate
708 * of the given #GFile.
710 GFile *
711 g_file_dup (GFile *file)
713 GFileIface *iface;
715 g_return_val_if_fail (G_IS_FILE (file), NULL);
717 iface = G_FILE_GET_IFACE (file);
719 return (* iface->dup) (file);
723 * g_file_hash:
724 * @file: (type GFile): #gconstpointer to a #GFile
726 * Creates a hash value for a #GFile.
728 * This call does no blocking I/O.
730 * Virtual: hash
731 * Returns: 0 if @file is not a valid #GFile, otherwise an
732 * integer that can be used as hash value for the #GFile.
733 * This function is intended for easily hashing a #GFile to
734 * add to a #GHashTable or similar data structure.
736 guint
737 g_file_hash (gconstpointer file)
739 GFileIface *iface;
741 g_return_val_if_fail (G_IS_FILE (file), 0);
743 iface = G_FILE_GET_IFACE (file);
745 return (* iface->hash) ((GFile *)file);
749 * g_file_equal:
750 * @file1: the first #GFile
751 * @file2: the second #GFile
753 * Checks if the two given #GFiles refer to the same file.
755 * Note that two #GFiles that differ can still refer to the same
756 * file on the filesystem due to various forms of filename
757 * aliasing.
759 * This call does no blocking I/O.
761 * Returns: %TRUE if @file1 and @file2 are equal.
763 gboolean
764 g_file_equal (GFile *file1,
765 GFile *file2)
767 GFileIface *iface;
769 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
770 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
772 if (file1 == file2)
773 return TRUE;
775 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
776 return FALSE;
778 iface = G_FILE_GET_IFACE (file1);
780 return (* iface->equal) (file1, file2);
785 * g_file_get_parent:
786 * @file: input #GFile
788 * Gets the parent directory for the @file.
789 * If the @file represents the root directory of the
790 * file system, then %NULL will be returned.
792 * This call does no blocking I/O.
794 * Returns: (nullable) (transfer full): a #GFile structure to the
795 * parent of the given #GFile or %NULL if there is no parent. Free
796 * the returned object with g_object_unref().
798 GFile *
799 g_file_get_parent (GFile *file)
801 GFileIface *iface;
803 g_return_val_if_fail (G_IS_FILE (file), NULL);
805 iface = G_FILE_GET_IFACE (file);
807 return (* iface->get_parent) (file);
811 * g_file_has_parent:
812 * @file: input #GFile
813 * @parent: (nullable): the parent to check for, or %NULL
815 * Checks if @file has a parent, and optionally, if it is @parent.
817 * If @parent is %NULL then this function returns %TRUE if @file has any
818 * parent at all. If @parent is non-%NULL then %TRUE is only returned
819 * if @file is an immediate child of @parent.
821 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
822 * the case that @parent is %NULL).
824 * Since: 2.24
826 gboolean
827 g_file_has_parent (GFile *file,
828 GFile *parent)
830 GFile *actual_parent;
831 gboolean result;
833 g_return_val_if_fail (G_IS_FILE (file), FALSE);
834 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
836 actual_parent = g_file_get_parent (file);
838 if (actual_parent != NULL)
840 if (parent != NULL)
841 result = g_file_equal (parent, actual_parent);
842 else
843 result = TRUE;
845 g_object_unref (actual_parent);
847 else
848 result = FALSE;
850 return result;
854 * g_file_get_child:
855 * @file: input #GFile
856 * @name: (type filename): string containing the child's basename
858 * Gets a child of @file with basename equal to @name.
860 * Note that the file with that specific name might not exist, but
861 * you can still have a #GFile that points to it. You can use this
862 * for instance to create that file.
864 * This call does no blocking I/O.
866 * Returns: (transfer full): a #GFile to a child specified by @name.
867 * Free the returned object with g_object_unref().
869 GFile *
870 g_file_get_child (GFile *file,
871 const char *name)
873 g_return_val_if_fail (G_IS_FILE (file), NULL);
874 g_return_val_if_fail (name != NULL, NULL);
876 return g_file_resolve_relative_path (file, name);
880 * g_file_get_child_for_display_name:
881 * @file: input #GFile
882 * @display_name: string to a possible child
883 * @error: return location for an error
885 * Gets the child of @file for a given @display_name (i.e. a UTF-8
886 * version of the name). If this function fails, it returns %NULL
887 * and @error will be set. This is very useful when constructing a
888 * #GFile for a new file and the user entered the filename in the
889 * user interface, for instance when you select a directory and
890 * type a filename in the file selector.
892 * This call does no blocking I/O.
894 * Returns: (transfer full): a #GFile to the specified child, or
895 * %NULL if the display name couldn't be converted.
896 * Free the returned object with g_object_unref().
898 GFile *
899 g_file_get_child_for_display_name (GFile *file,
900 const char *display_name,
901 GError **error)
903 GFileIface *iface;
905 g_return_val_if_fail (G_IS_FILE (file), NULL);
906 g_return_val_if_fail (display_name != NULL, NULL);
908 iface = G_FILE_GET_IFACE (file);
910 return (* iface->get_child_for_display_name) (file, display_name, error);
914 * g_file_has_prefix:
915 * @file: input #GFile
916 * @prefix: input #GFile
918 * Checks whether @file has the prefix specified by @prefix.
920 * In other words, if the names of initial elements of @file's
921 * pathname match @prefix. Only full pathname elements are matched,
922 * so a path like /foo is not considered a prefix of /foobar, only
923 * of /foo/bar.
925 * A #GFile is not a prefix of itself. If you want to check for
926 * equality, use g_file_equal().
928 * This call does no I/O, as it works purely on names. As such it can
929 * sometimes return %FALSE even if @file is inside a @prefix (from a
930 * filesystem point of view), because the prefix of @file is an alias
931 * of @prefix.
933 * Virtual: prefix_matches
934 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
935 * %FALSE otherwise.
937 gboolean
938 g_file_has_prefix (GFile *file,
939 GFile *prefix)
941 GFileIface *iface;
943 g_return_val_if_fail (G_IS_FILE (file), FALSE);
944 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
946 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
947 return FALSE;
949 iface = G_FILE_GET_IFACE (file);
951 /* The vtable function differs in arg order since
952 * we're using the old contains_file call
954 return (* iface->prefix_matches) (prefix, file);
958 * g_file_get_relative_path:
959 * @parent: input #GFile
960 * @descendant: input #GFile
962 * Gets the path for @descendant relative to @parent.
964 * This call does no blocking I/O.
966 * Returns: (type filename) (nullable): string with the relative path from
967 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
968 * prefix. The returned string should be freed with g_free() when
969 * no longer needed.
971 char *
972 g_file_get_relative_path (GFile *parent,
973 GFile *descendant)
975 GFileIface *iface;
977 g_return_val_if_fail (G_IS_FILE (parent), NULL);
978 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
980 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
981 return NULL;
983 iface = G_FILE_GET_IFACE (parent);
985 return (* iface->get_relative_path) (parent, descendant);
989 * g_file_resolve_relative_path:
990 * @file: input #GFile
991 * @relative_path: (type filename): a given relative path string
993 * Resolves a relative path for @file to an absolute path.
995 * This call does no blocking I/O.
997 * Returns: (transfer full): #GFile to the resolved path.
998 * %NULL if @relative_path is %NULL or if @file is invalid.
999 * Free the returned object with g_object_unref().
1001 GFile *
1002 g_file_resolve_relative_path (GFile *file,
1003 const char *relative_path)
1005 GFileIface *iface;
1007 g_return_val_if_fail (G_IS_FILE (file), NULL);
1008 g_return_val_if_fail (relative_path != NULL, NULL);
1010 iface = G_FILE_GET_IFACE (file);
1012 return (* iface->resolve_relative_path) (file, relative_path);
1016 * g_file_enumerate_children:
1017 * @file: input #GFile
1018 * @attributes: an attribute query string
1019 * @flags: a set of #GFileQueryInfoFlags
1020 * @cancellable: (nullable): optional #GCancellable object,
1021 * %NULL to ignore
1022 * @error: #GError for error reporting
1024 * Gets the requested information about the files in a directory.
1025 * The result is a #GFileEnumerator object that will give out
1026 * #GFileInfo objects for all the files in the directory.
1028 * The @attributes value is a string that specifies the file
1029 * attributes that should be gathered. It is not an error if
1030 * it's not possible to read a particular requested attribute
1031 * from a file - it just won't be set. @attributes should
1032 * be a comma-separated list of attributes or attribute wildcards.
1033 * The wildcard "*" means all attributes, and a wildcard like
1034 * "standard::*" means all attributes in the standard namespace.
1035 * An example attribute query be "standard::*,owner::user".
1036 * The standard attributes are available as defines, like
1037 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1039 * If @cancellable is not %NULL, then the operation can be cancelled
1040 * by triggering the cancellable object from another thread. If the
1041 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1042 * returned.
1044 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1045 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1046 * error will be returned. Other errors are possible too.
1048 * Returns: (transfer full): A #GFileEnumerator if successful,
1049 * %NULL on error. Free the returned object with g_object_unref().
1051 GFileEnumerator *
1052 g_file_enumerate_children (GFile *file,
1053 const char *attributes,
1054 GFileQueryInfoFlags flags,
1055 GCancellable *cancellable,
1056 GError **error)
1058 GFileIface *iface;
1060 g_return_val_if_fail (G_IS_FILE (file), NULL);
1062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1063 return NULL;
1065 iface = G_FILE_GET_IFACE (file);
1067 if (iface->enumerate_children == NULL)
1069 g_set_error_literal (error, G_IO_ERROR,
1070 G_IO_ERROR_NOT_SUPPORTED,
1071 _("Operation not supported"));
1072 return NULL;
1075 return (* iface->enumerate_children) (file, attributes, flags,
1076 cancellable, error);
1080 * g_file_enumerate_children_async:
1081 * @file: input #GFile
1082 * @attributes: an attribute query string
1083 * @flags: a set of #GFileQueryInfoFlags
1084 * @io_priority: the [I/O priority][io-priority] of the request
1085 * @cancellable: (nullable): optional #GCancellable object,
1086 * %NULL to ignore
1087 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1088 * request is satisfied
1089 * @user_data: (closure): the data to pass to callback function
1091 * Asynchronously gets the requested information about the files
1092 * in a directory. The result is a #GFileEnumerator object that will
1093 * give out #GFileInfo objects for all the files in the directory.
1095 * For more details, see g_file_enumerate_children() which is
1096 * the synchronous version of this call.
1098 * When the operation is finished, @callback will be called. You can
1099 * then call g_file_enumerate_children_finish() to get the result of
1100 * the operation.
1102 void
1103 g_file_enumerate_children_async (GFile *file,
1104 const char *attributes,
1105 GFileQueryInfoFlags flags,
1106 int io_priority,
1107 GCancellable *cancellable,
1108 GAsyncReadyCallback callback,
1109 gpointer user_data)
1111 GFileIface *iface;
1113 g_return_if_fail (G_IS_FILE (file));
1115 iface = G_FILE_GET_IFACE (file);
1116 (* iface->enumerate_children_async) (file,
1117 attributes,
1118 flags,
1119 io_priority,
1120 cancellable,
1121 callback,
1122 user_data);
1126 * g_file_enumerate_children_finish:
1127 * @file: input #GFile
1128 * @res: a #GAsyncResult
1129 * @error: a #GError
1131 * Finishes an async enumerate children operation.
1132 * See g_file_enumerate_children_async().
1134 * Returns: (transfer full): a #GFileEnumerator or %NULL
1135 * if an error occurred.
1136 * Free the returned object with g_object_unref().
1138 GFileEnumerator *
1139 g_file_enumerate_children_finish (GFile *file,
1140 GAsyncResult *res,
1141 GError **error)
1143 GFileIface *iface;
1145 g_return_val_if_fail (G_IS_FILE (file), NULL);
1146 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1148 if (g_async_result_legacy_propagate_error (res, error))
1149 return NULL;
1151 iface = G_FILE_GET_IFACE (file);
1152 return (* iface->enumerate_children_finish) (file, res, error);
1156 * g_file_query_exists:
1157 * @file: input #GFile
1158 * @cancellable: (nullable): optional #GCancellable object,
1159 * %NULL to ignore
1161 * Utility function to check if a particular file exists. This is
1162 * implemented using g_file_query_info() and as such does blocking I/O.
1164 * Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
1165 * and then execute something based on the outcome of that, because the
1166 * file might have been created or removed in between the operations. The
1167 * general approach to handling that is to not check, but just do the
1168 * operation and handle the errors as they come.
1170 * As an example of race-free checking, take the case of reading a file,
1171 * and if it doesn't exist, creating it. There are two racy versions: read
1172 * it, and on error create it; and: check if it exists, if not create it.
1173 * These can both result in two processes creating the file (with perhaps
1174 * a partially written file as the result). The correct approach is to
1175 * always try to create the file with g_file_create() which will either
1176 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1178 * However, in many cases an existence check is useful in a user interface,
1179 * for instance to make a menu item sensitive/insensitive, so that you don't
1180 * have to fool users that something is possible and then just show an error
1181 * dialog. If you do this, you should make sure to also handle the errors
1182 * that can happen due to races when you execute the operation.
1184 * Returns: %TRUE if the file exists (and can be detected without error),
1185 * %FALSE otherwise (or if cancelled).
1187 gboolean
1188 g_file_query_exists (GFile *file,
1189 GCancellable *cancellable)
1191 GFileInfo *info;
1193 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1195 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1196 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1197 if (info != NULL)
1199 g_object_unref (info);
1200 return TRUE;
1203 return FALSE;
1207 * g_file_query_file_type:
1208 * @file: input #GFile
1209 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1210 * @cancellable: (nullable): optional #GCancellable object,
1211 * %NULL to ignore
1213 * Utility function to inspect the #GFileType of a file. This is
1214 * implemented using g_file_query_info() and as such does blocking I/O.
1216 * The primary use case of this method is to check if a file is
1217 * a regular file, directory, or symlink.
1219 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1220 * if the file does not exist
1222 * Since: 2.18
1224 GFileType
1225 g_file_query_file_type (GFile *file,
1226 GFileQueryInfoFlags flags,
1227 GCancellable *cancellable)
1229 GFileInfo *info;
1230 GFileType file_type;
1232 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1233 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1234 cancellable, NULL);
1235 if (info != NULL)
1237 file_type = g_file_info_get_file_type (info);
1238 g_object_unref (info);
1240 else
1241 file_type = G_FILE_TYPE_UNKNOWN;
1243 return file_type;
1247 * g_file_query_info:
1248 * @file: input #GFile
1249 * @attributes: an attribute query string
1250 * @flags: a set of #GFileQueryInfoFlags
1251 * @cancellable: (nullable): optional #GCancellable object,
1252 * %NULL to ignore
1253 * @error: a #GError
1255 * Gets the requested information about specified @file.
1256 * The result is a #GFileInfo object that contains key-value
1257 * attributes (such as the type or size of the file).
1259 * The @attributes value is a string that specifies the file
1260 * attributes that should be gathered. It is not an error if
1261 * it's not possible to read a particular requested attribute
1262 * from a file - it just won't be set. @attributes should be a
1263 * comma-separated list of attributes or attribute wildcards.
1264 * The wildcard "*" means all attributes, and a wildcard like
1265 * "standard::*" means all attributes in the standard namespace.
1266 * An example attribute query be "standard::*,owner::user".
1267 * The standard attributes are available as defines, like
1268 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1270 * If @cancellable is not %NULL, then the operation can be cancelled
1271 * by triggering the cancellable object from another thread. If the
1272 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1273 * returned.
1275 * For symlinks, normally the information about the target of the
1276 * symlink is returned, rather than information about the symlink
1277 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1278 * in @flags the information about the symlink itself will be returned.
1279 * Also, for symlinks that point to non-existing files the information
1280 * about the symlink itself will be returned.
1282 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1283 * returned. Other errors are possible too, and depend on what kind of
1284 * filesystem the file is on.
1286 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1287 * on error. Free the returned object with g_object_unref().
1289 GFileInfo *
1290 g_file_query_info (GFile *file,
1291 const char *attributes,
1292 GFileQueryInfoFlags flags,
1293 GCancellable *cancellable,
1294 GError **error)
1296 GFileIface *iface;
1298 g_return_val_if_fail (G_IS_FILE (file), NULL);
1300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1301 return NULL;
1303 iface = G_FILE_GET_IFACE (file);
1305 if (iface->query_info == NULL)
1307 g_set_error_literal (error, G_IO_ERROR,
1308 G_IO_ERROR_NOT_SUPPORTED,
1309 _("Operation not supported"));
1310 return NULL;
1313 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1317 * g_file_query_info_async:
1318 * @file: input #GFile
1319 * @attributes: an attribute query string
1320 * @flags: a set of #GFileQueryInfoFlags
1321 * @io_priority: the [I/O priority][io-priority] of the request
1322 * @cancellable: (nullable): optional #GCancellable object,
1323 * %NULL to ignore
1324 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1325 * request is satisfied
1326 * @user_data: (closure): the data to pass to callback function
1328 * Asynchronously gets the requested information about specified @file.
1329 * The result is a #GFileInfo object that contains key-value attributes
1330 * (such as type or size for the file).
1332 * For more details, see g_file_query_info() which is the synchronous
1333 * version of this call.
1335 * When the operation is finished, @callback will be called. You can
1336 * then call g_file_query_info_finish() to get the result of the operation.
1338 void
1339 g_file_query_info_async (GFile *file,
1340 const char *attributes,
1341 GFileQueryInfoFlags flags,
1342 int io_priority,
1343 GCancellable *cancellable,
1344 GAsyncReadyCallback callback,
1345 gpointer user_data)
1347 GFileIface *iface;
1349 g_return_if_fail (G_IS_FILE (file));
1351 iface = G_FILE_GET_IFACE (file);
1352 (* iface->query_info_async) (file,
1353 attributes,
1354 flags,
1355 io_priority,
1356 cancellable,
1357 callback,
1358 user_data);
1362 * g_file_query_info_finish:
1363 * @file: input #GFile
1364 * @res: a #GAsyncResult
1365 * @error: a #GError
1367 * Finishes an asynchronous file info query.
1368 * See g_file_query_info_async().
1370 * Returns: (transfer full): #GFileInfo for given @file
1371 * or %NULL on error. Free the returned object with
1372 * g_object_unref().
1374 GFileInfo *
1375 g_file_query_info_finish (GFile *file,
1376 GAsyncResult *res,
1377 GError **error)
1379 GFileIface *iface;
1381 g_return_val_if_fail (G_IS_FILE (file), NULL);
1382 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1384 if (g_async_result_legacy_propagate_error (res, error))
1385 return NULL;
1387 iface = G_FILE_GET_IFACE (file);
1388 return (* iface->query_info_finish) (file, res, error);
1392 * g_file_query_filesystem_info:
1393 * @file: input #GFile
1394 * @attributes: an attribute query string
1395 * @cancellable: (nullable): optional #GCancellable object,
1396 * %NULL to ignore
1397 * @error: a #GError
1399 * Similar to g_file_query_info(), but obtains information
1400 * about the filesystem the @file is on, rather than the file itself.
1401 * For instance the amount of space available and the type of
1402 * the filesystem.
1404 * The @attributes value is a string that specifies the attributes
1405 * that should be gathered. It is not an error if it's not possible
1406 * to read a particular requested attribute from a file - it just
1407 * won't be set. @attributes should be a comma-separated list of
1408 * attributes or attribute wildcards. The wildcard "*" means all
1409 * attributes, and a wildcard like "filesystem::*" means all attributes
1410 * in the filesystem namespace. The standard namespace for filesystem
1411 * attributes is "filesystem". Common attributes of interest are
1412 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1413 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1414 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1416 * If @cancellable is not %NULL, then the operation can be cancelled
1417 * by triggering the cancellable object from another thread. If the
1418 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1419 * returned.
1421 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1422 * be returned. Other errors are possible too, and depend on what
1423 * kind of filesystem the file is on.
1425 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1426 * Free the returned object with g_object_unref().
1428 GFileInfo *
1429 g_file_query_filesystem_info (GFile *file,
1430 const char *attributes,
1431 GCancellable *cancellable,
1432 GError **error)
1434 GFileIface *iface;
1436 g_return_val_if_fail (G_IS_FILE (file), NULL);
1438 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1439 return NULL;
1441 iface = G_FILE_GET_IFACE (file);
1443 if (iface->query_filesystem_info == NULL)
1445 g_set_error_literal (error, G_IO_ERROR,
1446 G_IO_ERROR_NOT_SUPPORTED,
1447 _("Operation not supported"));
1448 return NULL;
1451 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1455 * g_file_query_filesystem_info_async:
1456 * @file: input #GFile
1457 * @attributes: an attribute query string
1458 * @io_priority: the [I/O priority][io-priority] of the request
1459 * @cancellable: (nullable): optional #GCancellable object,
1460 * %NULL to ignore
1461 * @callback: (scope async): a #GAsyncReadyCallback to call
1462 * when the request is satisfied
1463 * @user_data: (closure): the data to pass to callback function
1465 * Asynchronously gets the requested information about the filesystem
1466 * that the specified @file is on. The result is a #GFileInfo object
1467 * that contains key-value attributes (such as type or size for the
1468 * file).
1470 * For more details, see g_file_query_filesystem_info() which is the
1471 * synchronous version of this call.
1473 * When the operation is finished, @callback will be called. You can
1474 * then call g_file_query_info_finish() to get the result of the
1475 * operation.
1477 void
1478 g_file_query_filesystem_info_async (GFile *file,
1479 const char *attributes,
1480 int io_priority,
1481 GCancellable *cancellable,
1482 GAsyncReadyCallback callback,
1483 gpointer user_data)
1485 GFileIface *iface;
1487 g_return_if_fail (G_IS_FILE (file));
1489 iface = G_FILE_GET_IFACE (file);
1490 (* iface->query_filesystem_info_async) (file,
1491 attributes,
1492 io_priority,
1493 cancellable,
1494 callback,
1495 user_data);
1499 * g_file_query_filesystem_info_finish:
1500 * @file: input #GFile
1501 * @res: a #GAsyncResult
1502 * @error: a #GError
1504 * Finishes an asynchronous filesystem info query.
1505 * See g_file_query_filesystem_info_async().
1507 * Returns: (transfer full): #GFileInfo for given @file
1508 * or %NULL on error.
1509 * Free the returned object with g_object_unref().
1511 GFileInfo *
1512 g_file_query_filesystem_info_finish (GFile *file,
1513 GAsyncResult *res,
1514 GError **error)
1516 GFileIface *iface;
1518 g_return_val_if_fail (G_IS_FILE (file), NULL);
1519 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1521 if (g_async_result_legacy_propagate_error (res, error))
1522 return NULL;
1524 iface = G_FILE_GET_IFACE (file);
1525 return (* iface->query_filesystem_info_finish) (file, res, error);
1529 * g_file_find_enclosing_mount:
1530 * @file: input #GFile
1531 * @cancellable: (nullable): optional #GCancellable object,
1532 * %NULL to ignore
1533 * @error: a #GError
1535 * Gets a #GMount for the #GFile.
1537 * If the #GFileIface for @file does not have a mount (e.g.
1538 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1539 * and %NULL will be returned.
1541 * If @cancellable is not %NULL, then the operation can be cancelled by
1542 * triggering the cancellable object from another thread. If the operation
1543 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1545 * Returns: (transfer full): a #GMount where the @file is located
1546 * or %NULL on error.
1547 * Free the returned object with g_object_unref().
1549 GMount *
1550 g_file_find_enclosing_mount (GFile *file,
1551 GCancellable *cancellable,
1552 GError **error)
1554 GFileIface *iface;
1556 g_return_val_if_fail (G_IS_FILE (file), NULL);
1558 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1559 return NULL;
1561 iface = G_FILE_GET_IFACE (file);
1562 if (iface->find_enclosing_mount == NULL)
1565 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1566 /* Translators: This is an error message when
1567 * trying to find the enclosing (user visible)
1568 * mount of a file, but none exists.
1570 _("Containing mount does not exist"));
1571 return NULL;
1574 return (* iface->find_enclosing_mount) (file, cancellable, error);
1578 * g_file_find_enclosing_mount_async:
1579 * @file: a #GFile
1580 * @io_priority: the [I/O priority][io-priority] of the request
1581 * @cancellable: (nullable): optional #GCancellable object,
1582 * %NULL to ignore
1583 * @callback: (scope async): a #GAsyncReadyCallback to call
1584 * when the request is satisfied
1585 * @user_data: (closure): the data to pass to callback function
1587 * Asynchronously gets the mount for the file.
1589 * For more details, see g_file_find_enclosing_mount() which is
1590 * the synchronous version of this call.
1592 * When the operation is finished, @callback will be called.
1593 * You can then call g_file_find_enclosing_mount_finish() to
1594 * get the result of the operation.
1596 void
1597 g_file_find_enclosing_mount_async (GFile *file,
1598 int io_priority,
1599 GCancellable *cancellable,
1600 GAsyncReadyCallback callback,
1601 gpointer user_data)
1603 GFileIface *iface;
1605 g_return_if_fail (G_IS_FILE (file));
1607 iface = G_FILE_GET_IFACE (file);
1608 (* iface->find_enclosing_mount_async) (file,
1609 io_priority,
1610 cancellable,
1611 callback,
1612 user_data);
1616 * g_file_find_enclosing_mount_finish:
1617 * @file: a #GFile
1618 * @res: a #GAsyncResult
1619 * @error: a #GError
1621 * Finishes an asynchronous find mount request.
1622 * See g_file_find_enclosing_mount_async().
1624 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1625 * Free the returned object with g_object_unref().
1627 GMount *
1628 g_file_find_enclosing_mount_finish (GFile *file,
1629 GAsyncResult *res,
1630 GError **error)
1632 GFileIface *iface;
1634 g_return_val_if_fail (G_IS_FILE (file), NULL);
1635 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1637 if (g_async_result_legacy_propagate_error (res, error))
1638 return NULL;
1640 iface = G_FILE_GET_IFACE (file);
1641 return (* iface->find_enclosing_mount_finish) (file, res, error);
1646 * g_file_read:
1647 * @file: #GFile to read
1648 * @cancellable: (nullable): a #GCancellable
1649 * @error: a #GError, or %NULL
1651 * Opens a file for reading. The result is a #GFileInputStream that
1652 * can be used to read the contents of the file.
1654 * If @cancellable is not %NULL, then the operation can be cancelled by
1655 * triggering the cancellable object from another thread. If the operation
1656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1658 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1659 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1660 * error will be returned. Other errors are possible too, and depend
1661 * on what kind of filesystem the file is on.
1663 * Virtual: read_fn
1664 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1665 * Free the returned object with g_object_unref().
1667 GFileInputStream *
1668 g_file_read (GFile *file,
1669 GCancellable *cancellable,
1670 GError **error)
1672 GFileIface *iface;
1674 g_return_val_if_fail (G_IS_FILE (file), NULL);
1676 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1677 return NULL;
1679 iface = G_FILE_GET_IFACE (file);
1681 if (iface->read_fn == NULL)
1683 g_set_error_literal (error, G_IO_ERROR,
1684 G_IO_ERROR_NOT_SUPPORTED,
1685 _("Operation not supported"));
1686 return NULL;
1689 return (* iface->read_fn) (file, cancellable, error);
1693 * g_file_append_to:
1694 * @file: input #GFile
1695 * @flags: a set of #GFileCreateFlags
1696 * @cancellable: (nullable): optional #GCancellable object,
1697 * %NULL to ignore
1698 * @error: a #GError, or %NULL
1700 * Gets an output stream for appending data to the file.
1701 * If the file doesn't already exist it is created.
1703 * By default files created are generally readable by everyone,
1704 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1705 * will be made readable only to the current user, to the level that
1706 * is supported on the target filesystem.
1708 * If @cancellable is not %NULL, then the operation can be cancelled
1709 * by triggering the cancellable object from another thread. If the
1710 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1711 * returned.
1713 * Some file systems don't allow all file names, and may return an
1714 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1715 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1716 * possible too, and depend on what kind of filesystem the file is on.
1718 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1719 * Free the returned object with g_object_unref().
1721 GFileOutputStream *
1722 g_file_append_to (GFile *file,
1723 GFileCreateFlags flags,
1724 GCancellable *cancellable,
1725 GError **error)
1727 GFileIface *iface;
1729 g_return_val_if_fail (G_IS_FILE (file), NULL);
1731 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1732 return NULL;
1734 iface = G_FILE_GET_IFACE (file);
1736 if (iface->append_to == NULL)
1738 g_set_error_literal (error, G_IO_ERROR,
1739 G_IO_ERROR_NOT_SUPPORTED,
1740 _("Operation not supported"));
1741 return NULL;
1744 return (* iface->append_to) (file, flags, cancellable, error);
1748 * g_file_create:
1749 * @file: input #GFile
1750 * @flags: a set of #GFileCreateFlags
1751 * @cancellable: (nullable): optional #GCancellable object,
1752 * %NULL to ignore
1753 * @error: a #GError, or %NULL
1755 * Creates a new file and returns an output stream for writing to it.
1756 * The file must not already exist.
1758 * By default files created are generally readable by everyone,
1759 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1760 * will be made readable only to the current user, to the level
1761 * that is supported on the target filesystem.
1763 * If @cancellable is not %NULL, then the operation can be cancelled
1764 * by triggering the cancellable object from another thread. If the
1765 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1766 * returned.
1768 * If a file or directory with this name already exists the
1769 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1770 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1771 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1772 * be returned. Other errors are possible too, and depend on what kind
1773 * of filesystem the file is on.
1775 * Returns: (transfer full): a #GFileOutputStream for the newly created
1776 * file, or %NULL on error.
1777 * Free the returned object with g_object_unref().
1779 GFileOutputStream *
1780 g_file_create (GFile *file,
1781 GFileCreateFlags flags,
1782 GCancellable *cancellable,
1783 GError **error)
1785 GFileIface *iface;
1787 g_return_val_if_fail (G_IS_FILE (file), NULL);
1789 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1790 return NULL;
1792 iface = G_FILE_GET_IFACE (file);
1794 if (iface->create == NULL)
1796 g_set_error_literal (error, G_IO_ERROR,
1797 G_IO_ERROR_NOT_SUPPORTED,
1798 _("Operation not supported"));
1799 return NULL;
1802 return (* iface->create) (file, flags, cancellable, error);
1806 * g_file_replace:
1807 * @file: input #GFile
1808 * @etag: (nullable): an optional [entity tag][gfile-etag]
1809 * for the current #GFile, or #NULL to ignore
1810 * @make_backup: %TRUE if a backup should be created
1811 * @flags: a set of #GFileCreateFlags
1812 * @cancellable: (nullable): optional #GCancellable object,
1813 * %NULL to ignore
1814 * @error: a #GError, or %NULL
1816 * Returns an output stream for overwriting the file, possibly
1817 * creating a backup copy of the file first. If the file doesn't exist,
1818 * it will be created.
1820 * This will try to replace the file in the safest way possible so
1821 * that any errors during the writing will not affect an already
1822 * existing copy of the file. For instance, for local files it
1823 * may write to a temporary file and then atomically rename over
1824 * the destination when the stream is closed.
1826 * By default files created are generally readable by everyone,
1827 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1828 * will be made readable only to the current user, to the level that
1829 * is supported on the target filesystem.
1831 * If @cancellable is not %NULL, then the operation can be cancelled
1832 * by triggering the cancellable object from another thread. If the
1833 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1834 * returned.
1836 * If you pass in a non-%NULL @etag value and @file already exists, then
1837 * this value is compared to the current entity tag of the file, and if
1838 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1839 * generally means that the file has been changed since you last read
1840 * it. You can get the new etag from g_file_output_stream_get_etag()
1841 * after you've finished writing and closed the #GFileOutputStream. When
1842 * you load a new file you can use g_file_input_stream_query_info() to
1843 * get the etag of the file.
1845 * If @make_backup is %TRUE, this function will attempt to make a
1846 * backup of the current file before overwriting it. If this fails
1847 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1848 * want to replace anyway, try again with @make_backup set to %FALSE.
1850 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1851 * be returned, and if the file is some other form of non-regular file
1852 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1853 * file systems don't allow all file names, and may return an
1854 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1855 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1856 * possible too, and depend on what kind of filesystem the file is on.
1858 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1859 * Free the returned object with g_object_unref().
1861 GFileOutputStream *
1862 g_file_replace (GFile *file,
1863 const char *etag,
1864 gboolean make_backup,
1865 GFileCreateFlags flags,
1866 GCancellable *cancellable,
1867 GError **error)
1869 GFileIface *iface;
1871 g_return_val_if_fail (G_IS_FILE (file), NULL);
1873 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1874 return NULL;
1876 iface = G_FILE_GET_IFACE (file);
1878 if (iface->replace == NULL)
1880 g_set_error_literal (error, G_IO_ERROR,
1881 G_IO_ERROR_NOT_SUPPORTED,
1882 _("Operation not supported"));
1883 return NULL;
1886 /* Handle empty tag string as NULL in consistent way. */
1887 if (etag && *etag == 0)
1888 etag = NULL;
1890 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1894 * g_file_open_readwrite:
1895 * @file: #GFile to open
1896 * @cancellable: (nullable): a #GCancellable
1897 * @error: a #GError, or %NULL
1899 * Opens an existing file for reading and writing. The result is
1900 * a #GFileIOStream that can be used to read and write the contents
1901 * of the file.
1903 * If @cancellable is not %NULL, then the operation can be cancelled
1904 * by triggering the cancellable object from another thread. If the
1905 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1906 * returned.
1908 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1909 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1910 * error will be returned. Other errors are possible too, and depend on
1911 * what kind of filesystem the file is on. Note that in many non-local
1912 * file cases read and write streams are not supported, so make sure you
1913 * really need to do read and write streaming, rather than just opening
1914 * for reading or writing.
1916 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1917 * Free the returned object with g_object_unref().
1919 * Since: 2.22
1921 GFileIOStream *
1922 g_file_open_readwrite (GFile *file,
1923 GCancellable *cancellable,
1924 GError **error)
1926 GFileIface *iface;
1928 g_return_val_if_fail (G_IS_FILE (file), NULL);
1930 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1931 return NULL;
1933 iface = G_FILE_GET_IFACE (file);
1935 if (iface->open_readwrite == NULL)
1937 g_set_error_literal (error, G_IO_ERROR,
1938 G_IO_ERROR_NOT_SUPPORTED,
1939 _("Operation not supported"));
1940 return NULL;
1943 return (* iface->open_readwrite) (file, cancellable, error);
1947 * g_file_create_readwrite:
1948 * @file: a #GFile
1949 * @flags: a set of #GFileCreateFlags
1950 * @cancellable: (nullable): optional #GCancellable object,
1951 * %NULL to ignore
1952 * @error: return location for a #GError, or %NULL
1954 * Creates a new file and returns a stream for reading and
1955 * writing to it. The file must not already exist.
1957 * By default files created are generally readable by everyone,
1958 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1959 * will be made readable only to the current user, to the level
1960 * that is supported on the target filesystem.
1962 * If @cancellable is not %NULL, then the operation can be cancelled
1963 * by triggering the cancellable object from another thread. If the
1964 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1965 * returned.
1967 * If a file or directory with this name already exists, the
1968 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1969 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1970 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1971 * will be returned. Other errors are possible too, and depend on what
1972 * kind of filesystem the file is on.
1974 * Note that in many non-local file cases read and write streams are
1975 * not supported, so make sure you really need to do read and write
1976 * streaming, rather than just opening for reading or writing.
1978 * Returns: (transfer full): a #GFileIOStream for the newly created
1979 * file, or %NULL on error.
1980 * Free the returned object with g_object_unref().
1982 * Since: 2.22
1984 GFileIOStream *
1985 g_file_create_readwrite (GFile *file,
1986 GFileCreateFlags flags,
1987 GCancellable *cancellable,
1988 GError **error)
1990 GFileIface *iface;
1992 g_return_val_if_fail (G_IS_FILE (file), NULL);
1994 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1995 return NULL;
1997 iface = G_FILE_GET_IFACE (file);
1999 if (iface->create_readwrite == NULL)
2001 g_set_error_literal (error, G_IO_ERROR,
2002 G_IO_ERROR_NOT_SUPPORTED,
2003 _("Operation not supported"));
2004 return NULL;
2007 return (* iface->create_readwrite) (file, flags, cancellable, error);
2011 * g_file_replace_readwrite:
2012 * @file: a #GFile
2013 * @etag: (nullable): an optional [entity tag][gfile-etag]
2014 * for the current #GFile, or #NULL to ignore
2015 * @make_backup: %TRUE if a backup should be created
2016 * @flags: a set of #GFileCreateFlags
2017 * @cancellable: (nullable): optional #GCancellable object,
2018 * %NULL to ignore
2019 * @error: return location for a #GError, or %NULL
2021 * Returns an output stream for overwriting the file in readwrite mode,
2022 * possibly creating a backup copy of the file first. If the file doesn't
2023 * exist, it will be created.
2025 * For details about the behaviour, see g_file_replace() which does the
2026 * same thing but returns an output stream only.
2028 * Note that in many non-local file cases read and write streams are not
2029 * supported, so make sure you really need to do read and write streaming,
2030 * rather than just opening for reading or writing.
2032 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2033 * Free the returned object with g_object_unref().
2035 * Since: 2.22
2037 GFileIOStream *
2038 g_file_replace_readwrite (GFile *file,
2039 const char *etag,
2040 gboolean make_backup,
2041 GFileCreateFlags flags,
2042 GCancellable *cancellable,
2043 GError **error)
2045 GFileIface *iface;
2047 g_return_val_if_fail (G_IS_FILE (file), NULL);
2049 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2050 return NULL;
2052 iface = G_FILE_GET_IFACE (file);
2054 if (iface->replace_readwrite == NULL)
2056 g_set_error_literal (error, G_IO_ERROR,
2057 G_IO_ERROR_NOT_SUPPORTED,
2058 _("Operation not supported"));
2059 return NULL;
2062 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2066 * g_file_read_async:
2067 * @file: input #GFile
2068 * @io_priority: the [I/O priority][io-priority] of the request
2069 * @cancellable: (nullable): optional #GCancellable object,
2070 * %NULL to ignore
2071 * @callback: (scope async): a #GAsyncReadyCallback to call
2072 * when the request is satisfied
2073 * @user_data: (closure): the data to pass to callback function
2075 * Asynchronously opens @file for reading.
2077 * For more details, see g_file_read() which is
2078 * the synchronous version of this call.
2080 * When the operation is finished, @callback will be called.
2081 * You can then call g_file_read_finish() to get the result
2082 * of the operation.
2084 void
2085 g_file_read_async (GFile *file,
2086 int io_priority,
2087 GCancellable *cancellable,
2088 GAsyncReadyCallback callback,
2089 gpointer user_data)
2091 GFileIface *iface;
2093 g_return_if_fail (G_IS_FILE (file));
2095 iface = G_FILE_GET_IFACE (file);
2096 (* iface->read_async) (file,
2097 io_priority,
2098 cancellable,
2099 callback,
2100 user_data);
2104 * g_file_read_finish:
2105 * @file: input #GFile
2106 * @res: a #GAsyncResult
2107 * @error: a #GError, or %NULL
2109 * Finishes an asynchronous file read operation started with
2110 * g_file_read_async().
2112 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2113 * Free the returned object with g_object_unref().
2115 GFileInputStream *
2116 g_file_read_finish (GFile *file,
2117 GAsyncResult *res,
2118 GError **error)
2120 GFileIface *iface;
2122 g_return_val_if_fail (G_IS_FILE (file), NULL);
2123 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2125 if (g_async_result_legacy_propagate_error (res, error))
2126 return NULL;
2128 iface = G_FILE_GET_IFACE (file);
2129 return (* iface->read_finish) (file, res, error);
2133 * g_file_append_to_async:
2134 * @file: input #GFile
2135 * @flags: a set of #GFileCreateFlags
2136 * @io_priority: the [I/O priority][io-priority] of the request
2137 * @cancellable: (nullable): optional #GCancellable object,
2138 * %NULL to ignore
2139 * @callback: (scope async): a #GAsyncReadyCallback to call
2140 * when the request is satisfied
2141 * @user_data: (closure): the data to pass to callback function
2143 * Asynchronously opens @file for appending.
2145 * For more details, see g_file_append_to() which is
2146 * the synchronous version of this call.
2148 * When the operation is finished, @callback will be called.
2149 * You can then call g_file_append_to_finish() to get the result
2150 * of the operation.
2152 void
2153 g_file_append_to_async (GFile *file,
2154 GFileCreateFlags flags,
2155 int io_priority,
2156 GCancellable *cancellable,
2157 GAsyncReadyCallback callback,
2158 gpointer user_data)
2160 GFileIface *iface;
2162 g_return_if_fail (G_IS_FILE (file));
2164 iface = G_FILE_GET_IFACE (file);
2165 (* iface->append_to_async) (file,
2166 flags,
2167 io_priority,
2168 cancellable,
2169 callback,
2170 user_data);
2174 * g_file_append_to_finish:
2175 * @file: input #GFile
2176 * @res: #GAsyncResult
2177 * @error: a #GError, or %NULL
2179 * Finishes an asynchronous file append operation started with
2180 * g_file_append_to_async().
2182 * Returns: (transfer full): a valid #GFileOutputStream
2183 * or %NULL on error.
2184 * Free the returned object with g_object_unref().
2186 GFileOutputStream *
2187 g_file_append_to_finish (GFile *file,
2188 GAsyncResult *res,
2189 GError **error)
2191 GFileIface *iface;
2193 g_return_val_if_fail (G_IS_FILE (file), NULL);
2194 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2196 if (g_async_result_legacy_propagate_error (res, error))
2197 return NULL;
2199 iface = G_FILE_GET_IFACE (file);
2200 return (* iface->append_to_finish) (file, res, error);
2204 * g_file_create_async:
2205 * @file: input #GFile
2206 * @flags: a set of #GFileCreateFlags
2207 * @io_priority: the [I/O priority][io-priority] of the request
2208 * @cancellable: (nullable): optional #GCancellable object,
2209 * %NULL to ignore
2210 * @callback: (scope async): a #GAsyncReadyCallback to call
2211 * when the request is satisfied
2212 * @user_data: (closure): the data to pass to callback function
2214 * Asynchronously creates a new file and returns an output stream
2215 * for writing to it. The file must not already exist.
2217 * For more details, see g_file_create() which is
2218 * the synchronous version of this call.
2220 * When the operation is finished, @callback will be called.
2221 * You can then call g_file_create_finish() to get the result
2222 * of the operation.
2224 void
2225 g_file_create_async (GFile *file,
2226 GFileCreateFlags flags,
2227 int io_priority,
2228 GCancellable *cancellable,
2229 GAsyncReadyCallback callback,
2230 gpointer user_data)
2232 GFileIface *iface;
2234 g_return_if_fail (G_IS_FILE (file));
2236 iface = G_FILE_GET_IFACE (file);
2237 (* iface->create_async) (file,
2238 flags,
2239 io_priority,
2240 cancellable,
2241 callback,
2242 user_data);
2246 * g_file_create_finish:
2247 * @file: input #GFile
2248 * @res: a #GAsyncResult
2249 * @error: a #GError, or %NULL
2251 * Finishes an asynchronous file create operation started with
2252 * g_file_create_async().
2254 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2255 * Free the returned object with g_object_unref().
2257 GFileOutputStream *
2258 g_file_create_finish (GFile *file,
2259 GAsyncResult *res,
2260 GError **error)
2262 GFileIface *iface;
2264 g_return_val_if_fail (G_IS_FILE (file), NULL);
2265 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2267 if (g_async_result_legacy_propagate_error (res, error))
2268 return NULL;
2270 iface = G_FILE_GET_IFACE (file);
2271 return (* iface->create_finish) (file, res, error);
2275 * g_file_replace_async:
2276 * @file: input #GFile
2277 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2278 * or %NULL to ignore
2279 * @make_backup: %TRUE if a backup should be created
2280 * @flags: a set of #GFileCreateFlags
2281 * @io_priority: the [I/O priority][io-priority] of the request
2282 * @cancellable: (nullable): optional #GCancellable object,
2283 * %NULL to ignore
2284 * @callback: (scope async): a #GAsyncReadyCallback to call
2285 * when the request is satisfied
2286 * @user_data: (closure): the data to pass to callback function
2288 * Asynchronously overwrites the file, replacing the contents,
2289 * possibly creating a backup copy of the file first.
2291 * For more details, see g_file_replace() which is
2292 * the synchronous version of this call.
2294 * When the operation is finished, @callback will be called.
2295 * You can then call g_file_replace_finish() to get the result
2296 * of the operation.
2298 void
2299 g_file_replace_async (GFile *file,
2300 const char *etag,
2301 gboolean make_backup,
2302 GFileCreateFlags flags,
2303 int io_priority,
2304 GCancellable *cancellable,
2305 GAsyncReadyCallback callback,
2306 gpointer user_data)
2308 GFileIface *iface;
2310 g_return_if_fail (G_IS_FILE (file));
2312 iface = G_FILE_GET_IFACE (file);
2313 (* iface->replace_async) (file,
2314 etag,
2315 make_backup,
2316 flags,
2317 io_priority,
2318 cancellable,
2319 callback,
2320 user_data);
2324 * g_file_replace_finish:
2325 * @file: input #GFile
2326 * @res: a #GAsyncResult
2327 * @error: a #GError, or %NULL
2329 * Finishes an asynchronous file replace operation started with
2330 * g_file_replace_async().
2332 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2333 * Free the returned object with g_object_unref().
2335 GFileOutputStream *
2336 g_file_replace_finish (GFile *file,
2337 GAsyncResult *res,
2338 GError **error)
2340 GFileIface *iface;
2342 g_return_val_if_fail (G_IS_FILE (file), NULL);
2343 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2345 if (g_async_result_legacy_propagate_error (res, error))
2346 return NULL;
2348 iface = G_FILE_GET_IFACE (file);
2349 return (* iface->replace_finish) (file, res, error);
2353 * g_file_open_readwrite_async
2354 * @file: input #GFile
2355 * @io_priority: the [I/O priority][io-priority] of the request
2356 * @cancellable: (nullable): optional #GCancellable object,
2357 * %NULL to ignore
2358 * @callback: (scope async): a #GAsyncReadyCallback to call
2359 * when the request is satisfied
2360 * @user_data: (closure): the data to pass to callback function
2362 * Asynchronously opens @file for reading and writing.
2364 * For more details, see g_file_open_readwrite() which is
2365 * the synchronous version of this call.
2367 * When the operation is finished, @callback will be called.
2368 * You can then call g_file_open_readwrite_finish() to get
2369 * the result of the operation.
2371 * Since: 2.22
2373 void
2374 g_file_open_readwrite_async (GFile *file,
2375 int io_priority,
2376 GCancellable *cancellable,
2377 GAsyncReadyCallback callback,
2378 gpointer user_data)
2380 GFileIface *iface;
2382 g_return_if_fail (G_IS_FILE (file));
2384 iface = G_FILE_GET_IFACE (file);
2385 (* iface->open_readwrite_async) (file,
2386 io_priority,
2387 cancellable,
2388 callback,
2389 user_data);
2393 * g_file_open_readwrite_finish:
2394 * @file: input #GFile
2395 * @res: a #GAsyncResult
2396 * @error: a #GError, or %NULL
2398 * Finishes an asynchronous file read operation started with
2399 * g_file_open_readwrite_async().
2401 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2402 * Free the returned object with g_object_unref().
2404 * Since: 2.22
2406 GFileIOStream *
2407 g_file_open_readwrite_finish (GFile *file,
2408 GAsyncResult *res,
2409 GError **error)
2411 GFileIface *iface;
2413 g_return_val_if_fail (G_IS_FILE (file), NULL);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2416 if (g_async_result_legacy_propagate_error (res, error))
2417 return NULL;
2419 iface = G_FILE_GET_IFACE (file);
2420 return (* iface->open_readwrite_finish) (file, res, error);
2424 * g_file_create_readwrite_async:
2425 * @file: input #GFile
2426 * @flags: a set of #GFileCreateFlags
2427 * @io_priority: the [I/O priority][io-priority] of the request
2428 * @cancellable: (nullable): optional #GCancellable object,
2429 * %NULL to ignore
2430 * @callback: (scope async): a #GAsyncReadyCallback to call
2431 * when the request is satisfied
2432 * @user_data: (closure): the data to pass to callback function
2434 * Asynchronously creates a new file and returns a stream
2435 * for reading and writing to it. The file must not already exist.
2437 * For more details, see g_file_create_readwrite() which is
2438 * the synchronous version of this call.
2440 * When the operation is finished, @callback will be called.
2441 * You can then call g_file_create_readwrite_finish() to get
2442 * the result of the operation.
2444 * Since: 2.22
2446 void
2447 g_file_create_readwrite_async (GFile *file,
2448 GFileCreateFlags flags,
2449 int io_priority,
2450 GCancellable *cancellable,
2451 GAsyncReadyCallback callback,
2452 gpointer user_data)
2454 GFileIface *iface;
2456 g_return_if_fail (G_IS_FILE (file));
2458 iface = G_FILE_GET_IFACE (file);
2459 (* iface->create_readwrite_async) (file,
2460 flags,
2461 io_priority,
2462 cancellable,
2463 callback,
2464 user_data);
2468 * g_file_create_readwrite_finish:
2469 * @file: input #GFile
2470 * @res: a #GAsyncResult
2471 * @error: a #GError, or %NULL
2473 * Finishes an asynchronous file create operation started with
2474 * g_file_create_readwrite_async().
2476 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2477 * Free the returned object with g_object_unref().
2479 * Since: 2.22
2481 GFileIOStream *
2482 g_file_create_readwrite_finish (GFile *file,
2483 GAsyncResult *res,
2484 GError **error)
2486 GFileIface *iface;
2488 g_return_val_if_fail (G_IS_FILE (file), NULL);
2489 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2491 if (g_async_result_legacy_propagate_error (res, error))
2492 return NULL;
2494 iface = G_FILE_GET_IFACE (file);
2495 return (* iface->create_readwrite_finish) (file, res, error);
2499 * g_file_replace_readwrite_async:
2500 * @file: input #GFile
2501 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2502 * or %NULL to ignore
2503 * @make_backup: %TRUE if a backup should be created
2504 * @flags: a set of #GFileCreateFlags
2505 * @io_priority: the [I/O priority][io-priority] of the request
2506 * @cancellable: (nullable): optional #GCancellable object,
2507 * %NULL to ignore
2508 * @callback: (scope async): a #GAsyncReadyCallback to call
2509 * when the request is satisfied
2510 * @user_data: (closure): the data to pass to callback function
2512 * Asynchronously overwrites the file in read-write mode,
2513 * replacing the contents, possibly creating a backup copy
2514 * of the file first.
2516 * For more details, see g_file_replace_readwrite() which is
2517 * the synchronous version of this call.
2519 * When the operation is finished, @callback will be called.
2520 * You can then call g_file_replace_readwrite_finish() to get
2521 * the result of the operation.
2523 * Since: 2.22
2525 void
2526 g_file_replace_readwrite_async (GFile *file,
2527 const char *etag,
2528 gboolean make_backup,
2529 GFileCreateFlags flags,
2530 int io_priority,
2531 GCancellable *cancellable,
2532 GAsyncReadyCallback callback,
2533 gpointer user_data)
2535 GFileIface *iface;
2537 g_return_if_fail (G_IS_FILE (file));
2539 iface = G_FILE_GET_IFACE (file);
2540 (* iface->replace_readwrite_async) (file,
2541 etag,
2542 make_backup,
2543 flags,
2544 io_priority,
2545 cancellable,
2546 callback,
2547 user_data);
2551 * g_file_replace_readwrite_finish:
2552 * @file: input #GFile
2553 * @res: a #GAsyncResult
2554 * @error: a #GError, or %NULL
2556 * Finishes an asynchronous file replace operation started with
2557 * g_file_replace_readwrite_async().
2559 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2560 * Free the returned object with g_object_unref().
2562 * Since: 2.22
2564 GFileIOStream *
2565 g_file_replace_readwrite_finish (GFile *file,
2566 GAsyncResult *res,
2567 GError **error)
2569 GFileIface *iface;
2571 g_return_val_if_fail (G_IS_FILE (file), NULL);
2572 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2574 if (g_async_result_legacy_propagate_error (res, error))
2575 return NULL;
2577 iface = G_FILE_GET_IFACE (file);
2578 return (* iface->replace_readwrite_finish) (file, res, error);
2581 static gboolean
2582 copy_symlink (GFile *destination,
2583 GFileCopyFlags flags,
2584 GCancellable *cancellable,
2585 const char *target,
2586 GError **error)
2588 GError *my_error;
2589 gboolean tried_delete;
2590 GFileInfo *info;
2591 GFileType file_type;
2593 tried_delete = FALSE;
2595 retry:
2596 my_error = NULL;
2597 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2599 /* Maybe it already existed, and we want to overwrite? */
2600 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2601 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2603 g_clear_error (&my_error);
2605 /* Don't overwrite if the destination is a directory */
2606 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2607 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2608 cancellable, &my_error);
2609 if (info != NULL)
2611 file_type = g_file_info_get_file_type (info);
2612 g_object_unref (info);
2614 if (file_type == G_FILE_TYPE_DIRECTORY)
2616 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2617 _("Can’t copy over directory"));
2618 return FALSE;
2622 if (!g_file_delete (destination, cancellable, error))
2623 return FALSE;
2625 tried_delete = TRUE;
2626 goto retry;
2628 /* Nah, fail */
2629 g_propagate_error (error, my_error);
2630 return FALSE;
2633 return TRUE;
2636 static GFileInputStream *
2637 open_source_for_copy (GFile *source,
2638 GFile *destination,
2639 GFileCopyFlags flags,
2640 GCancellable *cancellable,
2641 GError **error)
2643 GError *my_error;
2644 GFileInputStream *ret;
2645 GFileInfo *info;
2646 GFileType file_type;
2648 my_error = NULL;
2649 ret = g_file_read (source, cancellable, &my_error);
2650 if (ret != NULL)
2651 return ret;
2653 /* There was an error opening the source, try to set a good error for it: */
2654 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2656 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2657 * as that is less useful to the app. Better check for errors on the
2658 * target instead.
2660 g_error_free (my_error);
2661 my_error = NULL;
2663 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2664 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2665 cancellable, &my_error);
2666 if (info != NULL &&
2667 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2669 file_type = g_file_info_get_file_type (info);
2670 g_object_unref (info);
2672 if (flags & G_FILE_COPY_OVERWRITE)
2674 if (file_type == G_FILE_TYPE_DIRECTORY)
2676 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2677 _("Can’t copy directory over directory"));
2678 return NULL;
2680 /* continue to would_recurse error */
2682 else
2684 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2685 _("Target file exists"));
2686 return NULL;
2689 else
2691 /* Error getting info from target, return that error
2692 * (except for NOT_FOUND, which is no error here)
2694 g_clear_object (&info);
2695 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2697 g_propagate_error (error, my_error);
2698 return NULL;
2700 g_clear_error (&my_error);
2703 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2704 _("Can’t recursively copy directory"));
2705 return NULL;
2708 g_propagate_error (error, my_error);
2709 return NULL;
2712 static gboolean
2713 should_copy (GFileAttributeInfo *info,
2714 gboolean copy_all_attributes,
2715 gboolean skip_perms)
2717 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2718 return FALSE;
2720 if (copy_all_attributes)
2721 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2722 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2725 static gboolean
2726 build_attribute_list_for_copy (GFile *file,
2727 GFileCopyFlags flags,
2728 char **out_attributes,
2729 GCancellable *cancellable,
2730 GError **error)
2732 gboolean ret = FALSE;
2733 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2734 GString *s = NULL;
2735 gboolean first;
2736 int i;
2737 gboolean copy_all_attributes;
2738 gboolean skip_perms;
2740 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2741 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2743 /* Ignore errors here, if the target supports no attributes there is
2744 * nothing to copy. We still honor the cancellable though.
2746 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2747 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2748 goto out;
2750 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2751 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2752 goto out;
2754 if (attributes == NULL && namespaces == NULL)
2755 goto out;
2757 first = TRUE;
2758 s = g_string_new ("");
2760 if (attributes)
2762 for (i = 0; i < attributes->n_infos; i++)
2764 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2766 if (first)
2767 first = FALSE;
2768 else
2769 g_string_append_c (s, ',');
2771 g_string_append (s, attributes->infos[i].name);
2776 if (namespaces)
2778 for (i = 0; i < namespaces->n_infos; i++)
2780 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2782 if (first)
2783 first = FALSE;
2784 else
2785 g_string_append_c (s, ',');
2787 g_string_append (s, namespaces->infos[i].name);
2788 g_string_append (s, "::*");
2793 ret = TRUE;
2794 *out_attributes = g_string_free (s, FALSE);
2795 s = NULL;
2796 out:
2797 if (s)
2798 g_string_free (s, TRUE);
2799 if (attributes)
2800 g_file_attribute_info_list_unref (attributes);
2801 if (namespaces)
2802 g_file_attribute_info_list_unref (namespaces);
2804 return ret;
2808 * g_file_copy_attributes:
2809 * @source: a #GFile with attributes
2810 * @destination: a #GFile to copy attributes to
2811 * @flags: a set of #GFileCopyFlags
2812 * @cancellable: (nullable): optional #GCancellable object,
2813 * %NULL to ignore
2814 * @error: a #GError, %NULL to ignore
2816 * Copies the file attributes from @source to @destination.
2818 * Normally only a subset of the file attributes are copied,
2819 * those that are copies in a normal file copy operation
2820 * (which for instance does not include e.g. owner). However
2821 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2822 * all the metadata that is possible to copy is copied. This
2823 * is useful when implementing move by copy + delete source.
2825 * Returns: %TRUE if the attributes were copied successfully,
2826 * %FALSE otherwise.
2828 gboolean
2829 g_file_copy_attributes (GFile *source,
2830 GFile *destination,
2831 GFileCopyFlags flags,
2832 GCancellable *cancellable,
2833 GError **error)
2835 char *attrs_to_read;
2836 gboolean res;
2837 GFileInfo *info;
2838 gboolean source_nofollow_symlinks;
2840 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2841 cancellable, error))
2842 return FALSE;
2844 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2846 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2847 * we just don't copy it.
2849 info = g_file_query_info (source, attrs_to_read,
2850 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2851 cancellable,
2852 NULL);
2854 g_free (attrs_to_read);
2856 res = TRUE;
2857 if (info)
2859 res = g_file_set_attributes_from_info (destination,
2860 info,
2861 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2862 cancellable,
2863 error);
2864 g_object_unref (info);
2867 return res;
2870 /* 256k minus malloc overhead */
2871 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2873 static gboolean
2874 copy_stream_with_progress (GInputStream *in,
2875 GOutputStream *out,
2876 GFile *source,
2877 GCancellable *cancellable,
2878 GFileProgressCallback progress_callback,
2879 gpointer progress_callback_data,
2880 GError **error)
2882 gssize n_read;
2883 gsize n_written;
2884 goffset current_size;
2885 char *buffer;
2886 gboolean res;
2887 goffset total_size;
2888 GFileInfo *info;
2890 total_size = -1;
2891 /* avoid performance impact of querying total size when it's not needed */
2892 if (progress_callback)
2894 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2895 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2896 cancellable, NULL);
2897 if (info)
2899 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2900 total_size = g_file_info_get_size (info);
2901 g_object_unref (info);
2904 if (total_size == -1)
2906 info = g_file_query_info (source,
2907 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2908 G_FILE_QUERY_INFO_NONE,
2909 cancellable, NULL);
2910 if (info)
2912 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2913 total_size = g_file_info_get_size (info);
2914 g_object_unref (info);
2919 if (total_size == -1)
2920 total_size = 0;
2922 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2923 current_size = 0;
2924 res = TRUE;
2925 while (TRUE)
2927 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2928 if (n_read == -1)
2930 res = FALSE;
2931 break;
2934 if (n_read == 0)
2935 break;
2937 current_size += n_read;
2939 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2940 if (!res)
2941 break;
2943 if (progress_callback)
2944 progress_callback (current_size, total_size, progress_callback_data);
2946 g_free (buffer);
2948 /* Make sure we send full copied size */
2949 if (progress_callback)
2950 progress_callback (current_size, total_size, progress_callback_data);
2952 return res;
2955 #ifdef HAVE_SPLICE
2957 static gboolean
2958 do_splice (int fd_in,
2959 loff_t *off_in,
2960 int fd_out,
2961 loff_t *off_out,
2962 size_t len,
2963 long *bytes_transferd,
2964 GError **error)
2966 long result;
2968 retry:
2969 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2971 if (result == -1)
2973 int errsv = errno;
2975 if (errsv == EINTR)
2976 goto retry;
2977 else if (errsv == ENOSYS || errsv == EINVAL)
2978 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2979 _("Splice not supported"));
2980 else
2981 g_set_error (error, G_IO_ERROR,
2982 g_io_error_from_errno (errsv),
2983 _("Error splicing file: %s"),
2984 g_strerror (errsv));
2986 return FALSE;
2989 *bytes_transferd = result;
2990 return TRUE;
2993 static gboolean
2994 splice_stream_with_progress (GInputStream *in,
2995 GOutputStream *out,
2996 GCancellable *cancellable,
2997 GFileProgressCallback progress_callback,
2998 gpointer progress_callback_data,
2999 GError **error)
3001 int buffer[2] = { -1, -1 };
3002 int buffer_size;
3003 gboolean res;
3004 goffset total_size;
3005 loff_t offset_in;
3006 loff_t offset_out;
3007 int fd_in, fd_out;
3009 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3010 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3012 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
3013 return FALSE;
3015 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3016 * pipe size. See: https://bugzilla.gnome.org/791457 */
3017 buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3018 if (buffer_size <= 0)
3020 int errsv;
3021 buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3022 errsv = errno;
3024 if (buffer_size <= 0)
3026 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3027 _("Error splicing file: %s"), g_strerror (errsv));
3028 res = FALSE;
3029 goto out;
3033 g_assert (buffer_size > 0);
3035 total_size = -1;
3036 /* avoid performance impact of querying total size when it's not needed */
3037 if (progress_callback)
3039 struct stat sbuf;
3041 if (fstat (fd_in, &sbuf) == 0)
3042 total_size = sbuf.st_size;
3045 if (total_size == -1)
3046 total_size = 0;
3048 offset_in = offset_out = 0;
3049 res = FALSE;
3050 while (TRUE)
3052 long n_read;
3053 long n_written;
3055 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3056 break;
3058 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3059 break;
3061 if (n_read == 0)
3063 res = TRUE;
3064 break;
3067 while (n_read > 0)
3069 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3070 goto out;
3072 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3073 goto out;
3075 n_read -= n_written;
3078 if (progress_callback)
3079 progress_callback (offset_in, total_size, progress_callback_data);
3082 /* Make sure we send full copied size */
3083 if (progress_callback)
3084 progress_callback (offset_in, total_size, progress_callback_data);
3086 if (!g_close (buffer[0], error))
3087 goto out;
3088 buffer[0] = -1;
3089 if (!g_close (buffer[1], error))
3090 goto out;
3091 buffer[1] = -1;
3092 out:
3093 if (buffer[0] != -1)
3094 (void) g_close (buffer[0], NULL);
3095 if (buffer[1] != -1)
3096 (void) g_close (buffer[1], NULL);
3098 return res;
3100 #endif
3102 #ifdef __linux__
3103 static gboolean
3104 btrfs_reflink_with_progress (GInputStream *in,
3105 GOutputStream *out,
3106 GFileInfo *info,
3107 GCancellable *cancellable,
3108 GFileProgressCallback progress_callback,
3109 gpointer progress_callback_data,
3110 GError **error)
3112 goffset source_size;
3113 int fd_in, fd_out;
3114 int ret, errsv;
3116 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3117 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3119 if (progress_callback)
3120 source_size = g_file_info_get_size (info);
3122 /* Btrfs clone ioctl properties:
3123 * - Works at the inode level
3124 * - Doesn't work with directories
3125 * - Always follows symlinks (source and destination)
3127 * By the time we get here, *in and *out are both regular files */
3128 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3129 errsv = errno;
3131 if (ret < 0)
3133 if (errsv == EXDEV)
3134 g_set_error_literal (error, G_IO_ERROR,
3135 G_IO_ERROR_NOT_SUPPORTED,
3136 _("Copy (reflink/clone) between mounts is not supported"));
3137 else if (errsv == EINVAL)
3138 g_set_error_literal (error, G_IO_ERROR,
3139 G_IO_ERROR_NOT_SUPPORTED,
3140 _("Copy (reflink/clone) is not supported or invalid"));
3141 else
3142 /* Most probably something odd happened; retry with fallback */
3143 g_set_error_literal (error, G_IO_ERROR,
3144 G_IO_ERROR_NOT_SUPPORTED,
3145 _("Copy (reflink/clone) is not supported or didn’t work"));
3146 /* We retry with fallback for all error cases because Btrfs is currently
3147 * unstable, and so we can't trust it to do clone properly.
3148 * In addition, any hard errors here would cause the same failure in the
3149 * fallback manual copy as well. */
3150 return FALSE;
3153 /* Make sure we send full copied size */
3154 if (progress_callback)
3155 progress_callback (source_size, source_size, progress_callback_data);
3157 return TRUE;
3159 #endif
3161 static gboolean
3162 file_copy_fallback (GFile *source,
3163 GFile *destination,
3164 GFileCopyFlags flags,
3165 GCancellable *cancellable,
3166 GFileProgressCallback progress_callback,
3167 gpointer progress_callback_data,
3168 GError **error)
3170 gboolean ret = FALSE;
3171 GFileInputStream *file_in = NULL;
3172 GInputStream *in = NULL;
3173 GOutputStream *out = NULL;
3174 GFileInfo *info = NULL;
3175 const char *target;
3176 char *attrs_to_read;
3177 gboolean do_set_attributes = FALSE;
3179 /* need to know the file type */
3180 info = g_file_query_info (source,
3181 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3182 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3183 cancellable,
3184 error);
3185 if (!info)
3186 goto out;
3188 /* Maybe copy the symlink? */
3189 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3190 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3192 target = g_file_info_get_symlink_target (info);
3193 if (target)
3195 if (!copy_symlink (destination, flags, cancellable, target, error))
3196 goto out;
3198 ret = TRUE;
3199 goto out;
3201 /* ... else fall back on a regular file copy */
3203 /* Handle "special" files (pipes, device nodes, ...)? */
3204 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3206 /* FIXME: could try to recreate device nodes and others? */
3207 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3208 _("Can’t copy special file"));
3209 goto out;
3212 /* Everything else should just fall back on a regular copy. */
3214 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3215 if (!file_in)
3216 goto out;
3217 in = G_INPUT_STREAM (file_in);
3219 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3220 cancellable, error))
3221 goto out;
3223 if (attrs_to_read != NULL)
3225 GError *tmp_error = NULL;
3227 /* Ok, ditch the previous lightweight info (on Unix we just
3228 * called lstat()); at this point we gather all the information
3229 * we need about the source from the opened file descriptor.
3231 g_object_unref (info);
3233 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3234 cancellable, &tmp_error);
3235 if (!info)
3237 /* Not all gvfs backends implement query_info_on_read(), we
3238 * can just fall back to the pathname again.
3239 * https://bugzilla.gnome.org/706254
3241 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3243 g_clear_error (&tmp_error);
3244 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3245 cancellable, error);
3247 else
3249 g_free (attrs_to_read);
3250 g_propagate_error (error, tmp_error);
3251 goto out;
3254 g_free (attrs_to_read);
3255 if (!info)
3256 goto out;
3258 do_set_attributes = TRUE;
3261 /* In the local file path, we pass down the source info which
3262 * includes things like unix::mode, to ensure that the target file
3263 * is not created with different permissions from the source file.
3265 * If a future API like g_file_replace_with_info() is added, switch
3266 * this code to use that.
3268 if (G_IS_LOCAL_FILE (destination))
3270 if (flags & G_FILE_COPY_OVERWRITE)
3271 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3272 FALSE, NULL,
3273 flags & G_FILE_COPY_BACKUP,
3274 G_FILE_CREATE_REPLACE_DESTINATION,
3275 info,
3276 cancellable, error);
3277 else
3278 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3279 FALSE, 0, info,
3280 cancellable, error);
3282 else if (flags & G_FILE_COPY_OVERWRITE)
3284 out = (GOutputStream *)g_file_replace (destination,
3285 NULL,
3286 flags & G_FILE_COPY_BACKUP,
3287 G_FILE_CREATE_REPLACE_DESTINATION,
3288 cancellable, error);
3290 else
3292 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3295 if (!out)
3296 goto out;
3298 #ifdef __linux__
3299 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3301 GError *reflink_err = NULL;
3303 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3304 progress_callback, progress_callback_data,
3305 &reflink_err))
3307 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3309 g_clear_error (&reflink_err);
3311 else
3313 g_propagate_error (error, reflink_err);
3314 goto out;
3317 else
3319 ret = TRUE;
3320 goto out;
3323 #endif
3325 #ifdef HAVE_SPLICE
3326 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3328 GError *splice_err = NULL;
3330 if (!splice_stream_with_progress (in, out, cancellable,
3331 progress_callback, progress_callback_data,
3332 &splice_err))
3334 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3336 g_clear_error (&splice_err);
3338 else
3340 g_propagate_error (error, splice_err);
3341 goto out;
3344 else
3346 ret = TRUE;
3347 goto out;
3351 #endif
3353 /* A plain read/write loop */
3354 if (!copy_stream_with_progress (in, out, source, cancellable,
3355 progress_callback, progress_callback_data,
3356 error))
3357 goto out;
3359 ret = TRUE;
3360 out:
3361 if (in)
3363 /* Don't care about errors in source here */
3364 (void) g_input_stream_close (in, cancellable, NULL);
3365 g_object_unref (in);
3368 if (out)
3370 /* But write errors on close are bad! */
3371 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3372 ret = FALSE;
3373 g_object_unref (out);
3376 /* Ignore errors here. Failure to copy metadata is not a hard error */
3377 /* TODO: set these attributes /before/ we do the rename() on Unix */
3378 if (ret && do_set_attributes)
3380 g_file_set_attributes_from_info (destination,
3381 info,
3382 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3383 cancellable,
3384 NULL);
3387 g_clear_object (&info);
3389 return ret;
3393 * g_file_copy:
3394 * @source: input #GFile
3395 * @destination: destination #GFile
3396 * @flags: set of #GFileCopyFlags
3397 * @cancellable: (nullable): optional #GCancellable object,
3398 * %NULL to ignore
3399 * @progress_callback: (nullable) (scope call): function to callback with
3400 * progress information, or %NULL if progress information is not needed
3401 * @progress_callback_data: (closure): user data to pass to @progress_callback
3402 * @error: #GError to set on error, or %NULL
3404 * Copies the file @source to the location specified by @destination.
3405 * Can not handle recursive copies of directories.
3407 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3408 * existing @destination file is overwritten.
3410 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3411 * will be copied as symlinks, otherwise the target of the
3412 * @source symlink will be copied.
3414 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3415 * that is possible to copy is copied, not just the default subset (which,
3416 * for instance, does not include the owner, see #GFileInfo).
3418 * If @cancellable is not %NULL, then the operation can be cancelled by
3419 * triggering the cancellable object from another thread. If the operation
3420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3422 * If @progress_callback is not %NULL, then the operation can be monitored
3423 * by setting this to a #GFileProgressCallback function.
3424 * @progress_callback_data will be passed to this function. It is guaranteed
3425 * that this callback will be called after all data has been transferred with
3426 * the total number of bytes copied during the operation.
3428 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3429 * is returned, independent on the status of the @destination.
3431 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3432 * the error %G_IO_ERROR_EXISTS is returned.
3434 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3435 * error is returned. If trying to overwrite a directory with a directory the
3436 * %G_IO_ERROR_WOULD_MERGE error is returned.
3438 * If the source is a directory and the target does not exist, or
3439 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3440 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3442 * If you are interested in copying the #GFile object itself (not the on-disk
3443 * file), see g_file_dup().
3445 * Returns: %TRUE on success, %FALSE otherwise.
3447 gboolean
3448 g_file_copy (GFile *source,
3449 GFile *destination,
3450 GFileCopyFlags flags,
3451 GCancellable *cancellable,
3452 GFileProgressCallback progress_callback,
3453 gpointer progress_callback_data,
3454 GError **error)
3456 GFileIface *iface;
3457 GError *my_error;
3458 gboolean res;
3460 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3461 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3463 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3464 return FALSE;
3466 iface = G_FILE_GET_IFACE (destination);
3467 if (iface->copy)
3469 my_error = NULL;
3470 res = (* iface->copy) (source, destination,
3471 flags, cancellable,
3472 progress_callback, progress_callback_data,
3473 &my_error);
3475 if (res)
3476 return TRUE;
3478 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3480 g_propagate_error (error, my_error);
3481 return FALSE;
3483 else
3484 g_clear_error (&my_error);
3487 /* If the types are different, and the destination method failed
3488 * also try the source method
3490 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3492 iface = G_FILE_GET_IFACE (source);
3494 if (iface->copy)
3496 my_error = NULL;
3497 res = (* iface->copy) (source, destination,
3498 flags, cancellable,
3499 progress_callback, progress_callback_data,
3500 &my_error);
3502 if (res)
3503 return TRUE;
3505 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3507 g_propagate_error (error, my_error);
3508 return FALSE;
3510 else
3511 g_clear_error (&my_error);
3515 return file_copy_fallback (source, destination, flags, cancellable,
3516 progress_callback, progress_callback_data,
3517 error);
3521 * g_file_copy_async:
3522 * @source: input #GFile
3523 * @destination: destination #GFile
3524 * @flags: set of #GFileCopyFlags
3525 * @io_priority: the [I/O priority][io-priority] of the request
3526 * @cancellable: (nullable): optional #GCancellable object,
3527 * %NULL to ignore
3528 * @progress_callback: (nullable) (scope notified): function to callback with progress
3529 * information, or %NULL if progress information is not needed
3530 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3531 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3532 * @user_data: (closure callback): the data to pass to callback function
3534 * Copies the file @source to the location specified by @destination
3535 * asynchronously. For details of the behaviour, see g_file_copy().
3537 * If @progress_callback is not %NULL, then that function that will be called
3538 * just like in g_file_copy(). The callback will run in the default main context
3539 * of the thread calling g_file_copy_async() — the same context as @callback is
3540 * run in.
3542 * When the operation is finished, @callback will be called. You can then call
3543 * g_file_copy_finish() to get the result of the operation.
3545 void
3546 g_file_copy_async (GFile *source,
3547 GFile *destination,
3548 GFileCopyFlags flags,
3549 int io_priority,
3550 GCancellable *cancellable,
3551 GFileProgressCallback progress_callback,
3552 gpointer progress_callback_data,
3553 GAsyncReadyCallback callback,
3554 gpointer user_data)
3556 GFileIface *iface;
3558 g_return_if_fail (G_IS_FILE (source));
3559 g_return_if_fail (G_IS_FILE (destination));
3561 iface = G_FILE_GET_IFACE (source);
3562 (* iface->copy_async) (source,
3563 destination,
3564 flags,
3565 io_priority,
3566 cancellable,
3567 progress_callback,
3568 progress_callback_data,
3569 callback,
3570 user_data);
3574 * g_file_copy_finish:
3575 * @file: input #GFile
3576 * @res: a #GAsyncResult
3577 * @error: a #GError, or %NULL
3579 * Finishes copying the file started with g_file_copy_async().
3581 * Returns: a %TRUE on success, %FALSE on error.
3583 gboolean
3584 g_file_copy_finish (GFile *file,
3585 GAsyncResult *res,
3586 GError **error)
3588 GFileIface *iface;
3590 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3591 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3593 if (g_async_result_legacy_propagate_error (res, error))
3594 return FALSE;
3596 iface = G_FILE_GET_IFACE (file);
3597 return (* iface->copy_finish) (file, res, error);
3601 * g_file_move:
3602 * @source: #GFile pointing to the source location
3603 * @destination: #GFile pointing to the destination location
3604 * @flags: set of #GFileCopyFlags
3605 * @cancellable: (nullable): optional #GCancellable object,
3606 * %NULL to ignore
3607 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3608 * function for updates
3609 * @progress_callback_data: (closure): gpointer to user data for
3610 * the callback function
3611 * @error: #GError for returning error conditions, or %NULL
3613 * Tries to move the file or directory @source to the location specified
3614 * by @destination. If native move operations are supported then this is
3615 * used, otherwise a copy + delete fallback is used. The native
3616 * implementation may support moving directories (for instance on moves
3617 * inside the same filesystem), but the fallback code does not.
3619 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3620 * existing @destination file is overwritten.
3622 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3623 * will be copied as symlinks, otherwise the target of the
3624 * @source symlink will be copied.
3626 * If @cancellable is not %NULL, then the operation can be cancelled by
3627 * triggering the cancellable object from another thread. If the operation
3628 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3630 * If @progress_callback is not %NULL, then the operation can be monitored
3631 * by setting this to a #GFileProgressCallback function.
3632 * @progress_callback_data will be passed to this function. It is
3633 * guaranteed that this callback will be called after all data has been
3634 * transferred with the total number of bytes copied during the operation.
3636 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3637 * error is returned, independent on the status of the @destination.
3639 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3640 * then the error %G_IO_ERROR_EXISTS is returned.
3642 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3643 * error is returned. If trying to overwrite a directory with a directory the
3644 * %G_IO_ERROR_WOULD_MERGE error is returned.
3646 * If the source is a directory and the target does not exist, or
3647 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3648 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3649 * move operation isn't available).
3651 * Returns: %TRUE on successful move, %FALSE otherwise.
3653 gboolean
3654 g_file_move (GFile *source,
3655 GFile *destination,
3656 GFileCopyFlags flags,
3657 GCancellable *cancellable,
3658 GFileProgressCallback progress_callback,
3659 gpointer progress_callback_data,
3660 GError **error)
3662 GFileIface *iface;
3663 GError *my_error;
3664 gboolean res;
3666 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3667 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3669 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3670 return FALSE;
3672 iface = G_FILE_GET_IFACE (destination);
3673 if (iface->move)
3675 my_error = NULL;
3676 res = (* iface->move) (source, destination,
3677 flags, cancellable,
3678 progress_callback, progress_callback_data,
3679 &my_error);
3681 if (res)
3682 return TRUE;
3684 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3686 g_propagate_error (error, my_error);
3687 return FALSE;
3689 else
3690 g_clear_error (&my_error);
3693 /* If the types are different, and the destination method failed
3694 * also try the source method
3696 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3698 iface = G_FILE_GET_IFACE (source);
3700 if (iface->move)
3702 my_error = NULL;
3703 res = (* iface->move) (source, destination,
3704 flags, cancellable,
3705 progress_callback, progress_callback_data,
3706 &my_error);
3708 if (res)
3709 return TRUE;
3711 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3713 g_propagate_error (error, my_error);
3714 return FALSE;
3716 else
3717 g_clear_error (&my_error);
3721 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3723 g_set_error_literal (error, G_IO_ERROR,
3724 G_IO_ERROR_NOT_SUPPORTED,
3725 _("Operation not supported"));
3726 return FALSE;
3729 flags |= G_FILE_COPY_ALL_METADATA;
3730 if (!g_file_copy (source, destination, flags, cancellable,
3731 progress_callback, progress_callback_data,
3732 error))
3733 return FALSE;
3735 return g_file_delete (source, cancellable, error);
3739 * g_file_make_directory:
3740 * @file: input #GFile
3741 * @cancellable: (nullable): optional #GCancellable object,
3742 * %NULL to ignore
3743 * @error: a #GError, or %NULL
3745 * Creates a directory. Note that this will only create a child directory
3746 * of the immediate parent directory of the path or URI given by the #GFile.
3747 * To recursively create directories, see g_file_make_directory_with_parents().
3748 * This function will fail if the parent directory does not exist, setting
3749 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3750 * creating directories, this function will fail, setting @error to
3751 * %G_IO_ERROR_NOT_SUPPORTED.
3753 * For a local #GFile the newly created directory will have the default
3754 * (current) ownership and permissions of the current process.
3756 * If @cancellable is not %NULL, then the operation can be cancelled by
3757 * triggering the cancellable object from another thread. If the operation
3758 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3760 * Returns: %TRUE on successful creation, %FALSE otherwise.
3762 gboolean
3763 g_file_make_directory (GFile *file,
3764 GCancellable *cancellable,
3765 GError **error)
3767 GFileIface *iface;
3769 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3772 return FALSE;
3774 iface = G_FILE_GET_IFACE (file);
3776 if (iface->make_directory == NULL)
3778 g_set_error_literal (error, G_IO_ERROR,
3779 G_IO_ERROR_NOT_SUPPORTED,
3780 _("Operation not supported"));
3781 return FALSE;
3784 return (* iface->make_directory) (file, cancellable, error);
3788 * g_file_make_directory_async:
3789 * @file: input #GFile
3790 * @io_priority: the [I/O priority][io-priority] of the request
3791 * @cancellable: (nullable): optional #GCancellable object,
3792 * %NULL to ignore
3793 * @callback: a #GAsyncReadyCallback to call
3794 * when the request is satisfied
3795 * @user_data: the data to pass to callback function
3797 * Asynchronously creates a directory.
3799 * Virtual: make_directory_async
3800 * Since: 2.38
3802 void
3803 g_file_make_directory_async (GFile *file,
3804 int io_priority,
3805 GCancellable *cancellable,
3806 GAsyncReadyCallback callback,
3807 gpointer user_data)
3809 GFileIface *iface;
3811 g_return_if_fail (G_IS_FILE (file));
3813 iface = G_FILE_GET_IFACE (file);
3814 (* iface->make_directory_async) (file,
3815 io_priority,
3816 cancellable,
3817 callback,
3818 user_data);
3822 * g_file_make_directory_finish:
3823 * @file: input #GFile
3824 * @result: a #GAsyncResult
3825 * @error: a #GError, or %NULL
3827 * Finishes an asynchronous directory creation, started with
3828 * g_file_make_directory_async().
3830 * Virtual: make_directory_finish
3831 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3832 * Since: 2.38
3834 gboolean
3835 g_file_make_directory_finish (GFile *file,
3836 GAsyncResult *result,
3837 GError **error)
3839 GFileIface *iface;
3841 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3842 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3844 iface = G_FILE_GET_IFACE (file);
3845 return (* iface->make_directory_finish) (file, result, error);
3849 * g_file_make_directory_with_parents:
3850 * @file: input #GFile
3851 * @cancellable: (nullable): optional #GCancellable object,
3852 * %NULL to ignore
3853 * @error: a #GError, or %NULL
3855 * Creates a directory and any parent directories that may not
3856 * exist similar to 'mkdir -p'. If the file system does not support
3857 * creating directories, this function will fail, setting @error to
3858 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3859 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3860 * the similar g_mkdir_with_parents().
3862 * For a local #GFile the newly created directories will have the default
3863 * (current) ownership and permissions of the current process.
3865 * If @cancellable is not %NULL, then the operation can be cancelled by
3866 * triggering the cancellable object from another thread. If the operation
3867 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3869 * Returns: %TRUE if all directories have been successfully created, %FALSE
3870 * otherwise.
3872 * Since: 2.18
3874 gboolean
3875 g_file_make_directory_with_parents (GFile *file,
3876 GCancellable *cancellable,
3877 GError **error)
3879 GFile *work_file = NULL;
3880 GList *list = NULL, *l;
3881 GError *my_error = NULL;
3883 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3885 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3886 return FALSE;
3888 /* Try for the simple case of not having to create any parent
3889 * directories. If any parent directory needs to be created, this
3890 * call will fail with NOT_FOUND. If that happens, then that value of
3891 * my_error persists into the while loop below.
3893 g_file_make_directory (file, cancellable, &my_error);
3894 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3896 if (my_error)
3897 g_propagate_error (error, my_error);
3898 return my_error == NULL;
3901 work_file = g_object_ref (file);
3903 /* Creates the parent directories as needed. In case any particular
3904 * creation operation fails for lack of other parent directories
3905 * (NOT_FOUND), the directory is added to a list of directories to
3906 * create later, and the value of my_error is retained until the next
3907 * iteration of the loop. After the loop my_error should either be
3908 * empty or contain a real failure condition.
3910 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3912 GFile *parent_file;
3914 parent_file = g_file_get_parent (work_file);
3915 if (parent_file == NULL)
3916 break;
3918 g_clear_error (&my_error);
3919 g_file_make_directory (parent_file, cancellable, &my_error);
3920 /* Another process may have created the directory in between the
3921 * G_IO_ERROR_NOT_FOUND and now
3923 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3924 g_clear_error (&my_error);
3926 g_object_unref (work_file);
3927 work_file = g_object_ref (parent_file);
3929 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3930 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3931 else
3932 g_object_unref (parent_file);
3935 /* All directories should be able to be created now, so an error at
3936 * this point means the whole operation must fail -- except an EXISTS
3937 * error, which means that another process already created the
3938 * directory in between the previous failure and now.
3940 for (l = list; my_error == NULL && l; l = l->next)
3942 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3943 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3944 g_clear_error (&my_error);
3947 if (work_file)
3948 g_object_unref (work_file);
3950 /* Clean up */
3951 while (list != NULL)
3953 g_object_unref ((GFile *) list->data);
3954 list = g_list_remove (list, list->data);
3957 /* At this point an error in my_error means a that something
3958 * unexpected failed in either of the loops above, so the whole
3959 * operation must fail.
3961 if (my_error != NULL)
3963 g_propagate_error (error, my_error);
3964 return FALSE;
3967 return g_file_make_directory (file, cancellable, error);
3971 * g_file_make_symbolic_link:
3972 * @file: a #GFile with the name of the symlink to create
3973 * @symlink_value: (type filename): a string with the path for the target
3974 * of the new symlink
3975 * @cancellable: (nullable): optional #GCancellable object,
3976 * %NULL to ignore
3977 * @error: a #GError
3979 * Creates a symbolic link named @file which contains the string
3980 * @symlink_value.
3982 * If @cancellable is not %NULL, then the operation can be cancelled by
3983 * triggering the cancellable object from another thread. If the operation
3984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3986 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3988 gboolean
3989 g_file_make_symbolic_link (GFile *file,
3990 const char *symlink_value,
3991 GCancellable *cancellable,
3992 GError **error)
3994 GFileIface *iface;
3996 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3997 g_return_val_if_fail (symlink_value != NULL, FALSE);
3999 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4000 return FALSE;
4002 if (*symlink_value == '\0')
4004 g_set_error_literal (error, G_IO_ERROR,
4005 G_IO_ERROR_INVALID_ARGUMENT,
4006 _("Invalid symlink value given"));
4007 return FALSE;
4010 iface = G_FILE_GET_IFACE (file);
4012 if (iface->make_symbolic_link == NULL)
4014 g_set_error_literal (error, G_IO_ERROR,
4015 G_IO_ERROR_NOT_SUPPORTED,
4016 _("Operation not supported"));
4017 return FALSE;
4020 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4024 * g_file_delete:
4025 * @file: input #GFile
4026 * @cancellable: (nullable): optional #GCancellable object,
4027 * %NULL to ignore
4028 * @error: a #GError, or %NULL
4030 * Deletes a file. If the @file is a directory, it will only be
4031 * deleted if it is empty. This has the same semantics as g_unlink().
4033 * If @cancellable is not %NULL, then the operation can be cancelled by
4034 * triggering the cancellable object from another thread. If the operation
4035 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4037 * Virtual: delete_file
4038 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4040 gboolean
4041 g_file_delete (GFile *file,
4042 GCancellable *cancellable,
4043 GError **error)
4045 GFileIface *iface;
4047 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4049 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4050 return FALSE;
4052 iface = G_FILE_GET_IFACE (file);
4054 if (iface->delete_file == NULL)
4056 g_set_error_literal (error, G_IO_ERROR,
4057 G_IO_ERROR_NOT_SUPPORTED,
4058 _("Operation not supported"));
4059 return FALSE;
4062 return (* iface->delete_file) (file, cancellable, error);
4066 * g_file_delete_async:
4067 * @file: input #GFile
4068 * @io_priority: the [I/O priority][io-priority] of the request
4069 * @cancellable: (nullable): optional #GCancellable object,
4070 * %NULL to ignore
4071 * @callback: a #GAsyncReadyCallback to call
4072 * when the request is satisfied
4073 * @user_data: the data to pass to callback function
4075 * Asynchronously delete a file. If the @file is a directory, it will
4076 * only be deleted if it is empty. This has the same semantics as
4077 * g_unlink().
4079 * Virtual: delete_file_async
4080 * Since: 2.34
4082 void
4083 g_file_delete_async (GFile *file,
4084 int io_priority,
4085 GCancellable *cancellable,
4086 GAsyncReadyCallback callback,
4087 gpointer user_data)
4089 GFileIface *iface;
4091 g_return_if_fail (G_IS_FILE (file));
4093 iface = G_FILE_GET_IFACE (file);
4094 (* iface->delete_file_async) (file,
4095 io_priority,
4096 cancellable,
4097 callback,
4098 user_data);
4102 * g_file_delete_finish:
4103 * @file: input #GFile
4104 * @result: a #GAsyncResult
4105 * @error: a #GError, or %NULL
4107 * Finishes deleting a file started with g_file_delete_async().
4109 * Virtual: delete_file_finish
4110 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4111 * Since: 2.34
4113 gboolean
4114 g_file_delete_finish (GFile *file,
4115 GAsyncResult *result,
4116 GError **error)
4118 GFileIface *iface;
4120 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4121 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4123 if (g_async_result_legacy_propagate_error (result, error))
4124 return FALSE;
4126 iface = G_FILE_GET_IFACE (file);
4127 return (* iface->delete_file_finish) (file, result, error);
4131 * g_file_trash:
4132 * @file: #GFile to send to trash
4133 * @cancellable: (nullable): optional #GCancellable object,
4134 * %NULL to ignore
4135 * @error: a #GError, or %NULL
4137 * Sends @file to the "Trashcan", if possible. This is similar to
4138 * deleting it, but the user can recover it before emptying the trashcan.
4139 * Not all file systems support trashing, so this call can return the
4140 * %G_IO_ERROR_NOT_SUPPORTED error.
4142 * If @cancellable is not %NULL, then the operation can be cancelled by
4143 * triggering the cancellable object from another thread. If the operation
4144 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4146 * Virtual: trash
4147 * Returns: %TRUE on successful trash, %FALSE otherwise.
4149 gboolean
4150 g_file_trash (GFile *file,
4151 GCancellable *cancellable,
4152 GError **error)
4154 GFileIface *iface;
4156 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4158 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4159 return FALSE;
4161 iface = G_FILE_GET_IFACE (file);
4163 if (iface->trash == NULL)
4165 g_set_error_literal (error,
4166 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4167 _("Trash not supported"));
4168 return FALSE;
4171 return (* iface->trash) (file, cancellable, error);
4175 * g_file_trash_async:
4176 * @file: input #GFile
4177 * @io_priority: the [I/O priority][io-priority] of the request
4178 * @cancellable: (nullable): optional #GCancellable object,
4179 * %NULL to ignore
4180 * @callback: a #GAsyncReadyCallback to call
4181 * when the request is satisfied
4182 * @user_data: the data to pass to callback function
4184 * Asynchronously sends @file to the Trash location, if possible.
4186 * Virtual: trash_async
4187 * Since: 2.38
4189 void
4190 g_file_trash_async (GFile *file,
4191 int io_priority,
4192 GCancellable *cancellable,
4193 GAsyncReadyCallback callback,
4194 gpointer user_data)
4196 GFileIface *iface;
4198 g_return_if_fail (G_IS_FILE (file));
4200 iface = G_FILE_GET_IFACE (file);
4201 (* iface->trash_async) (file,
4202 io_priority,
4203 cancellable,
4204 callback,
4205 user_data);
4209 * g_file_trash_finish:
4210 * @file: input #GFile
4211 * @result: a #GAsyncResult
4212 * @error: a #GError, or %NULL
4214 * Finishes an asynchronous file trashing operation, started with
4215 * g_file_trash_async().
4217 * Virtual: trash_finish
4218 * Returns: %TRUE on successful trash, %FALSE otherwise.
4219 * Since: 2.38
4221 gboolean
4222 g_file_trash_finish (GFile *file,
4223 GAsyncResult *result,
4224 GError **error)
4226 GFileIface *iface;
4228 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4229 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4231 iface = G_FILE_GET_IFACE (file);
4232 return (* iface->trash_finish) (file, result, error);
4236 * g_file_set_display_name:
4237 * @file: input #GFile
4238 * @display_name: a string
4239 * @cancellable: (nullable): optional #GCancellable object,
4240 * %NULL to ignore
4241 * @error: a #GError, or %NULL
4243 * Renames @file to the specified display name.
4245 * The display name is converted from UTF-8 to the correct encoding
4246 * for the target filesystem if possible and the @file is renamed to this.
4248 * If you want to implement a rename operation in the user interface the
4249 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4250 * initial value in the rename widget, and then the result after editing
4251 * should be passed to g_file_set_display_name().
4253 * On success the resulting converted filename is returned.
4255 * If @cancellable is not %NULL, then the operation can be cancelled by
4256 * triggering the cancellable object from another thread. If the operation
4257 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4259 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4260 * or %NULL if there was an error.
4261 * Free the returned object with g_object_unref().
4263 GFile *
4264 g_file_set_display_name (GFile *file,
4265 const gchar *display_name,
4266 GCancellable *cancellable,
4267 GError **error)
4269 GFileIface *iface;
4271 g_return_val_if_fail (G_IS_FILE (file), NULL);
4272 g_return_val_if_fail (display_name != NULL, NULL);
4274 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4276 g_set_error (error,
4277 G_IO_ERROR,
4278 G_IO_ERROR_INVALID_ARGUMENT,
4279 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4280 return NULL;
4283 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4284 return NULL;
4286 iface = G_FILE_GET_IFACE (file);
4288 return (* iface->set_display_name) (file, display_name, cancellable, error);
4292 * g_file_set_display_name_async:
4293 * @file: input #GFile
4294 * @display_name: a string
4295 * @io_priority: the [I/O priority][io-priority] of the request
4296 * @cancellable: (nullable): optional #GCancellable object,
4297 * %NULL to ignore
4298 * @callback: (scope async): a #GAsyncReadyCallback to call
4299 * when the request is satisfied
4300 * @user_data: (closure): the data to pass to callback function
4302 * Asynchronously sets the display name for a given #GFile.
4304 * For more details, see g_file_set_display_name() which is
4305 * the synchronous version of this call.
4307 * When the operation is finished, @callback will be called.
4308 * You can then call g_file_set_display_name_finish() to get
4309 * the result of the operation.
4311 void
4312 g_file_set_display_name_async (GFile *file,
4313 const gchar *display_name,
4314 gint io_priority,
4315 GCancellable *cancellable,
4316 GAsyncReadyCallback callback,
4317 gpointer user_data)
4319 GFileIface *iface;
4321 g_return_if_fail (G_IS_FILE (file));
4322 g_return_if_fail (display_name != NULL);
4324 iface = G_FILE_GET_IFACE (file);
4325 (* iface->set_display_name_async) (file,
4326 display_name,
4327 io_priority,
4328 cancellable,
4329 callback,
4330 user_data);
4334 * g_file_set_display_name_finish:
4335 * @file: input #GFile
4336 * @res: a #GAsyncResult
4337 * @error: a #GError, or %NULL
4339 * Finishes setting a display name started with
4340 * g_file_set_display_name_async().
4342 * Returns: (transfer full): a #GFile or %NULL on error.
4343 * Free the returned object with g_object_unref().
4345 GFile *
4346 g_file_set_display_name_finish (GFile *file,
4347 GAsyncResult *res,
4348 GError **error)
4350 GFileIface *iface;
4352 g_return_val_if_fail (G_IS_FILE (file), NULL);
4353 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4355 if (g_async_result_legacy_propagate_error (res, error))
4356 return NULL;
4358 iface = G_FILE_GET_IFACE (file);
4359 return (* iface->set_display_name_finish) (file, res, error);
4363 * g_file_query_settable_attributes:
4364 * @file: input #GFile
4365 * @cancellable: (nullable): optional #GCancellable object,
4366 * %NULL to ignore
4367 * @error: a #GError, or %NULL
4369 * Obtain the list of settable attributes for the file.
4371 * Returns the type and full attribute name of all the attributes
4372 * that can be set on this file. This doesn't mean setting it will
4373 * always succeed though, you might get an access failure, or some
4374 * specific file may not support a specific attribute.
4376 * If @cancellable is not %NULL, then the operation can be cancelled by
4377 * triggering the cancellable object from another thread. If the operation
4378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4380 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4381 * When you are done with it, release it with
4382 * g_file_attribute_info_list_unref()
4384 GFileAttributeInfoList *
4385 g_file_query_settable_attributes (GFile *file,
4386 GCancellable *cancellable,
4387 GError **error)
4389 GFileIface *iface;
4390 GError *my_error;
4391 GFileAttributeInfoList *list;
4393 g_return_val_if_fail (G_IS_FILE (file), NULL);
4395 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4396 return NULL;
4398 iface = G_FILE_GET_IFACE (file);
4400 if (iface->query_settable_attributes == NULL)
4401 return g_file_attribute_info_list_new ();
4403 my_error = NULL;
4404 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4406 if (list == NULL)
4408 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4410 list = g_file_attribute_info_list_new ();
4411 g_error_free (my_error);
4413 else
4414 g_propagate_error (error, my_error);
4417 return list;
4421 * g_file_query_writable_namespaces:
4422 * @file: input #GFile
4423 * @cancellable: (nullable): optional #GCancellable object,
4424 * %NULL to ignore
4425 * @error: a #GError, or %NULL
4427 * Obtain the list of attribute namespaces where new attributes
4428 * can be created by a user. An example of this is extended
4429 * attributes (in the "xattr" namespace).
4431 * If @cancellable is not %NULL, then the operation can be cancelled by
4432 * triggering the cancellable object from another thread. If the operation
4433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4435 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4436 * When you are done with it, release it with
4437 * g_file_attribute_info_list_unref()
4439 GFileAttributeInfoList *
4440 g_file_query_writable_namespaces (GFile *file,
4441 GCancellable *cancellable,
4442 GError **error)
4444 GFileIface *iface;
4445 GError *my_error;
4446 GFileAttributeInfoList *list;
4448 g_return_val_if_fail (G_IS_FILE (file), NULL);
4450 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4451 return NULL;
4453 iface = G_FILE_GET_IFACE (file);
4455 if (iface->query_writable_namespaces == NULL)
4456 return g_file_attribute_info_list_new ();
4458 my_error = NULL;
4459 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4461 if (list == NULL)
4463 g_warn_if_reached();
4464 list = g_file_attribute_info_list_new ();
4467 if (my_error != NULL)
4469 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4471 g_error_free (my_error);
4473 else
4474 g_propagate_error (error, my_error);
4477 return list;
4481 * g_file_set_attribute:
4482 * @file: input #GFile
4483 * @attribute: a string containing the attribute's name
4484 * @type: The type of the attribute
4485 * @value_p: (nullable): a pointer to the value (or the pointer
4486 * itself if the type is a pointer type)
4487 * @flags: a set of #GFileQueryInfoFlags
4488 * @cancellable: (nullable): optional #GCancellable object,
4489 * %NULL to ignore
4490 * @error: a #GError, or %NULL
4492 * Sets an attribute in the file with attribute name @attribute to @value.
4494 * Some attributes can be unset by setting @type to
4495 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4497 * If @cancellable is not %NULL, then the operation can be cancelled by
4498 * triggering the cancellable object from another thread. If the operation
4499 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4501 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4503 gboolean
4504 g_file_set_attribute (GFile *file,
4505 const gchar *attribute,
4506 GFileAttributeType type,
4507 gpointer value_p,
4508 GFileQueryInfoFlags flags,
4509 GCancellable *cancellable,
4510 GError **error)
4512 GFileIface *iface;
4514 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4515 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4517 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4518 return FALSE;
4520 iface = G_FILE_GET_IFACE (file);
4522 if (iface->set_attribute == NULL)
4524 g_set_error_literal (error, G_IO_ERROR,
4525 G_IO_ERROR_NOT_SUPPORTED,
4526 _("Operation not supported"));
4527 return FALSE;
4530 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4534 * g_file_set_attributes_from_info:
4535 * @file: input #GFile
4536 * @info: a #GFileInfo
4537 * @flags: #GFileQueryInfoFlags
4538 * @cancellable: (nullable): optional #GCancellable object,
4539 * %NULL to ignore
4540 * @error: a #GError, or %NULL
4542 * Tries to set all attributes in the #GFileInfo on the target
4543 * values, not stopping on the first error.
4545 * If there is any error during this operation then @error will
4546 * be set to the first error. Error on particular fields are flagged
4547 * by setting the "status" field in the attribute value to
4548 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4549 * also detect further errors.
4551 * If @cancellable is not %NULL, then the operation can be cancelled by
4552 * triggering the cancellable object from another thread. If the operation
4553 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4555 * Returns: %FALSE if there was any error, %TRUE otherwise.
4557 gboolean
4558 g_file_set_attributes_from_info (GFile *file,
4559 GFileInfo *info,
4560 GFileQueryInfoFlags flags,
4561 GCancellable *cancellable,
4562 GError **error)
4564 GFileIface *iface;
4566 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4567 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4569 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4570 return FALSE;
4572 g_file_info_clear_status (info);
4574 iface = G_FILE_GET_IFACE (file);
4576 return (* iface->set_attributes_from_info) (file,
4577 info,
4578 flags,
4579 cancellable,
4580 error);
4583 static gboolean
4584 g_file_real_set_attributes_from_info (GFile *file,
4585 GFileInfo *info,
4586 GFileQueryInfoFlags flags,
4587 GCancellable *cancellable,
4588 GError **error)
4590 char **attributes;
4591 int i;
4592 gboolean res;
4593 GFileAttributeValue *value;
4595 res = TRUE;
4597 attributes = g_file_info_list_attributes (info, NULL);
4599 for (i = 0; attributes[i] != NULL; i++)
4601 value = _g_file_info_get_attribute_value (info, attributes[i]);
4603 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4604 continue;
4606 if (!g_file_set_attribute (file, attributes[i],
4607 value->type, _g_file_attribute_value_peek_as_pointer (value),
4608 flags, cancellable, error))
4610 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4611 res = FALSE;
4612 /* Don't set error multiple times */
4613 error = NULL;
4615 else
4616 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4619 g_strfreev (attributes);
4621 return res;
4625 * g_file_set_attributes_async:
4626 * @file: input #GFile
4627 * @info: a #GFileInfo
4628 * @flags: a #GFileQueryInfoFlags
4629 * @io_priority: the [I/O priority][io-priority] of the request
4630 * @cancellable: (nullable): optional #GCancellable object,
4631 * %NULL to ignore
4632 * @callback: (scope async): a #GAsyncReadyCallback
4633 * @user_data: (closure): a #gpointer
4635 * Asynchronously sets the attributes of @file with @info.
4637 * For more details, see g_file_set_attributes_from_info(),
4638 * which is the synchronous version of this call.
4640 * When the operation is finished, @callback will be called.
4641 * You can then call g_file_set_attributes_finish() to get
4642 * the result of the operation.
4644 void
4645 g_file_set_attributes_async (GFile *file,
4646 GFileInfo *info,
4647 GFileQueryInfoFlags flags,
4648 int io_priority,
4649 GCancellable *cancellable,
4650 GAsyncReadyCallback callback,
4651 gpointer user_data)
4653 GFileIface *iface;
4655 g_return_if_fail (G_IS_FILE (file));
4656 g_return_if_fail (G_IS_FILE_INFO (info));
4658 iface = G_FILE_GET_IFACE (file);
4659 (* iface->set_attributes_async) (file,
4660 info,
4661 flags,
4662 io_priority,
4663 cancellable,
4664 callback,
4665 user_data);
4669 * g_file_set_attributes_finish:
4670 * @file: input #GFile
4671 * @result: a #GAsyncResult
4672 * @info: (out) (transfer full): a #GFileInfo
4673 * @error: a #GError, or %NULL
4675 * Finishes setting an attribute started in g_file_set_attributes_async().
4677 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4679 gboolean
4680 g_file_set_attributes_finish (GFile *file,
4681 GAsyncResult *result,
4682 GFileInfo **info,
4683 GError **error)
4685 GFileIface *iface;
4687 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4688 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4690 /* No standard handling of errors here, as we must set info even
4691 * on errors
4693 iface = G_FILE_GET_IFACE (file);
4694 return (* iface->set_attributes_finish) (file, result, info, error);
4698 * g_file_set_attribute_string:
4699 * @file: input #GFile
4700 * @attribute: a string containing the attribute's name
4701 * @value: a string containing the attribute's value
4702 * @flags: #GFileQueryInfoFlags
4703 * @cancellable: (nullable): optional #GCancellable object,
4704 * %NULL to ignore
4705 * @error: a #GError, or %NULL
4707 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4708 * If @attribute is of a different type, this operation will fail.
4710 * If @cancellable is not %NULL, then the operation can be cancelled by
4711 * triggering the cancellable object from another thread. If the operation
4712 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4714 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4716 gboolean
4717 g_file_set_attribute_string (GFile *file,
4718 const char *attribute,
4719 const char *value,
4720 GFileQueryInfoFlags flags,
4721 GCancellable *cancellable,
4722 GError **error)
4724 return g_file_set_attribute (file, attribute,
4725 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4726 flags, cancellable, error);
4730 * g_file_set_attribute_byte_string:
4731 * @file: input #GFile
4732 * @attribute: a string containing the attribute's name
4733 * @value: a string containing the attribute's new value
4734 * @flags: a #GFileQueryInfoFlags
4735 * @cancellable: (nullable): optional #GCancellable object,
4736 * %NULL to ignore
4737 * @error: a #GError, or %NULL
4739 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4740 * If @attribute is of a different type, this operation will fail,
4741 * returning %FALSE.
4743 * If @cancellable is not %NULL, then the operation can be cancelled by
4744 * triggering the cancellable object from another thread. If the operation
4745 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4747 * Returns: %TRUE if the @attribute was successfully set to @value
4748 * in the @file, %FALSE otherwise.
4750 gboolean
4751 g_file_set_attribute_byte_string (GFile *file,
4752 const gchar *attribute,
4753 const gchar *value,
4754 GFileQueryInfoFlags flags,
4755 GCancellable *cancellable,
4756 GError **error)
4758 return g_file_set_attribute (file, attribute,
4759 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4760 flags, cancellable, error);
4764 * g_file_set_attribute_uint32:
4765 * @file: input #GFile
4766 * @attribute: a string containing the attribute's name
4767 * @value: a #guint32 containing the attribute's new value
4768 * @flags: a #GFileQueryInfoFlags
4769 * @cancellable: (nullable): optional #GCancellable object,
4770 * %NULL to ignore
4771 * @error: a #GError, or %NULL
4773 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4774 * If @attribute is of a different type, this operation will fail.
4776 * If @cancellable is not %NULL, then the operation can be cancelled by
4777 * triggering the cancellable object from another thread. If the operation
4778 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4780 * Returns: %TRUE if the @attribute was successfully set to @value
4781 * in the @file, %FALSE otherwise.
4783 gboolean
4784 g_file_set_attribute_uint32 (GFile *file,
4785 const gchar *attribute,
4786 guint32 value,
4787 GFileQueryInfoFlags flags,
4788 GCancellable *cancellable,
4789 GError **error)
4791 return g_file_set_attribute (file, attribute,
4792 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4793 flags, cancellable, error);
4797 * g_file_set_attribute_int32:
4798 * @file: input #GFile
4799 * @attribute: a string containing the attribute's name
4800 * @value: a #gint32 containing the attribute's new value
4801 * @flags: a #GFileQueryInfoFlags
4802 * @cancellable: (nullable): optional #GCancellable object,
4803 * %NULL to ignore
4804 * @error: a #GError, or %NULL
4806 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4807 * If @attribute is of a different type, this operation will fail.
4809 * If @cancellable is not %NULL, then the operation can be cancelled by
4810 * triggering the cancellable object from another thread. If the operation
4811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4813 * Returns: %TRUE if the @attribute was successfully set to @value
4814 * in the @file, %FALSE otherwise.
4816 gboolean
4817 g_file_set_attribute_int32 (GFile *file,
4818 const gchar *attribute,
4819 gint32 value,
4820 GFileQueryInfoFlags flags,
4821 GCancellable *cancellable,
4822 GError **error)
4824 return g_file_set_attribute (file, attribute,
4825 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4826 flags, cancellable, error);
4830 * g_file_set_attribute_uint64:
4831 * @file: input #GFile
4832 * @attribute: a string containing the attribute's name
4833 * @value: a #guint64 containing the attribute's new value
4834 * @flags: a #GFileQueryInfoFlags
4835 * @cancellable: (nullable): optional #GCancellable object,
4836 * %NULL to ignore
4837 * @error: a #GError, or %NULL
4839 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4840 * If @attribute is of a different type, this operation will fail.
4842 * If @cancellable is not %NULL, then the operation can be cancelled by
4843 * triggering the cancellable object from another thread. If the operation
4844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4846 * Returns: %TRUE if the @attribute was successfully set to @value
4847 * in the @file, %FALSE otherwise.
4849 gboolean
4850 g_file_set_attribute_uint64 (GFile *file,
4851 const gchar *attribute,
4852 guint64 value,
4853 GFileQueryInfoFlags flags,
4854 GCancellable *cancellable,
4855 GError **error)
4857 return g_file_set_attribute (file, attribute,
4858 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4859 flags, cancellable, error);
4863 * g_file_set_attribute_int64:
4864 * @file: input #GFile
4865 * @attribute: a string containing the attribute's name
4866 * @value: a #guint64 containing the attribute's new value
4867 * @flags: a #GFileQueryInfoFlags
4868 * @cancellable: (nullable): optional #GCancellable object,
4869 * %NULL to ignore
4870 * @error: a #GError, or %NULL
4872 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4873 * If @attribute is of a different type, this operation will fail.
4875 * If @cancellable is not %NULL, then the operation can be cancelled by
4876 * triggering the cancellable object from another thread. If the operation
4877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4879 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4881 gboolean
4882 g_file_set_attribute_int64 (GFile *file,
4883 const gchar *attribute,
4884 gint64 value,
4885 GFileQueryInfoFlags flags,
4886 GCancellable *cancellable,
4887 GError **error)
4889 return g_file_set_attribute (file, attribute,
4890 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4891 flags, cancellable, error);
4895 * g_file_mount_mountable:
4896 * @file: input #GFile
4897 * @flags: flags affecting the operation
4898 * @mount_operation: (nullable): a #GMountOperation,
4899 * or %NULL to avoid user interaction
4900 * @cancellable: (nullable): optional #GCancellable object,
4901 * %NULL to ignore
4902 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4903 * when the request is satisfied, or %NULL
4904 * @user_data: (closure): the data to pass to callback function
4906 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4907 * Using @mount_operation, you can request callbacks when, for instance,
4908 * passwords are needed during authentication.
4910 * If @cancellable is not %NULL, then the operation can be cancelled by
4911 * triggering the cancellable object from another thread. If the operation
4912 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4914 * When the operation is finished, @callback will be called.
4915 * You can then call g_file_mount_mountable_finish() to get
4916 * the result of the operation.
4918 void
4919 g_file_mount_mountable (GFile *file,
4920 GMountMountFlags flags,
4921 GMountOperation *mount_operation,
4922 GCancellable *cancellable,
4923 GAsyncReadyCallback callback,
4924 gpointer user_data)
4926 GFileIface *iface;
4928 g_return_if_fail (G_IS_FILE (file));
4930 iface = G_FILE_GET_IFACE (file);
4932 if (iface->mount_mountable == NULL)
4934 g_task_report_new_error (file, callback, user_data,
4935 g_file_mount_mountable,
4936 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4937 _("Operation not supported"));
4938 return;
4941 (* iface->mount_mountable) (file,
4942 flags,
4943 mount_operation,
4944 cancellable,
4945 callback,
4946 user_data);
4950 * g_file_mount_mountable_finish:
4951 * @file: input #GFile
4952 * @result: a #GAsyncResult
4953 * @error: a #GError, or %NULL
4955 * Finishes a mount operation. See g_file_mount_mountable() for details.
4957 * Finish an asynchronous mount operation that was started
4958 * with g_file_mount_mountable().
4960 * Returns: (transfer full): a #GFile or %NULL on error.
4961 * Free the returned object with g_object_unref().
4963 GFile *
4964 g_file_mount_mountable_finish (GFile *file,
4965 GAsyncResult *result,
4966 GError **error)
4968 GFileIface *iface;
4970 g_return_val_if_fail (G_IS_FILE (file), NULL);
4971 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4973 if (g_async_result_legacy_propagate_error (result, error))
4974 return NULL;
4975 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4976 return g_task_propagate_pointer (G_TASK (result), error);
4978 iface = G_FILE_GET_IFACE (file);
4979 return (* iface->mount_mountable_finish) (file, result, error);
4983 * g_file_unmount_mountable:
4984 * @file: input #GFile
4985 * @flags: flags affecting the operation
4986 * @cancellable: (nullable): optional #GCancellable object,
4987 * %NULL to ignore
4988 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4989 * when the request is satisfied, or %NULL
4990 * @user_data: (closure): the data to pass to callback function
4992 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4994 * If @cancellable is not %NULL, then the operation can be cancelled by
4995 * triggering the cancellable object from another thread. If the operation
4996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4998 * When the operation is finished, @callback will be called.
4999 * You can then call g_file_unmount_mountable_finish() to get
5000 * the result of the operation.
5002 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5004 void
5005 g_file_unmount_mountable (GFile *file,
5006 GMountUnmountFlags flags,
5007 GCancellable *cancellable,
5008 GAsyncReadyCallback callback,
5009 gpointer user_data)
5011 GFileIface *iface;
5013 g_return_if_fail (G_IS_FILE (file));
5015 iface = G_FILE_GET_IFACE (file);
5017 if (iface->unmount_mountable == NULL)
5019 g_task_report_new_error (file, callback, user_data,
5020 g_file_unmount_mountable_with_operation,
5021 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5022 _("Operation not supported"));
5023 return;
5026 (* iface->unmount_mountable) (file,
5027 flags,
5028 cancellable,
5029 callback,
5030 user_data);
5034 * g_file_unmount_mountable_finish:
5035 * @file: input #GFile
5036 * @result: a #GAsyncResult
5037 * @error: a #GError, or %NULL
5039 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5041 * Finish an asynchronous unmount operation that was started
5042 * with g_file_unmount_mountable().
5044 * Returns: %TRUE if the operation finished successfully.
5045 * %FALSE otherwise.
5047 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5048 * instead.
5050 gboolean
5051 g_file_unmount_mountable_finish (GFile *file,
5052 GAsyncResult *result,
5053 GError **error)
5055 GFileIface *iface;
5057 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5058 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5060 if (g_async_result_legacy_propagate_error (result, error))
5061 return FALSE;
5062 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5063 return g_task_propagate_boolean (G_TASK (result), error);
5065 iface = G_FILE_GET_IFACE (file);
5066 return (* iface->unmount_mountable_finish) (file, result, error);
5070 * g_file_unmount_mountable_with_operation:
5071 * @file: input #GFile
5072 * @flags: flags affecting the operation
5073 * @mount_operation: (nullable): a #GMountOperation,
5074 * or %NULL to avoid user interaction
5075 * @cancellable: (nullable): optional #GCancellable object,
5076 * %NULL to ignore
5077 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5078 * when the request is satisfied, or %NULL
5079 * @user_data: (closure): the data to pass to callback function
5081 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5083 * If @cancellable is not %NULL, then the operation can be cancelled by
5084 * triggering the cancellable object from another thread. If the operation
5085 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5087 * When the operation is finished, @callback will be called.
5088 * You can then call g_file_unmount_mountable_finish() to get
5089 * the result of the operation.
5091 * Since: 2.22
5093 void
5094 g_file_unmount_mountable_with_operation (GFile *file,
5095 GMountUnmountFlags flags,
5096 GMountOperation *mount_operation,
5097 GCancellable *cancellable,
5098 GAsyncReadyCallback callback,
5099 gpointer user_data)
5101 GFileIface *iface;
5103 g_return_if_fail (G_IS_FILE (file));
5105 iface = G_FILE_GET_IFACE (file);
5107 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5109 g_task_report_new_error (file, callback, user_data,
5110 g_file_unmount_mountable_with_operation,
5111 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5112 _("Operation not supported"));
5113 return;
5116 if (iface->unmount_mountable_with_operation != NULL)
5117 (* iface->unmount_mountable_with_operation) (file,
5118 flags,
5119 mount_operation,
5120 cancellable,
5121 callback,
5122 user_data);
5123 else
5124 (* iface->unmount_mountable) (file,
5125 flags,
5126 cancellable,
5127 callback,
5128 user_data);
5132 * g_file_unmount_mountable_with_operation_finish:
5133 * @file: input #GFile
5134 * @result: a #GAsyncResult
5135 * @error: a #GError, or %NULL
5137 * Finishes an unmount operation,
5138 * see g_file_unmount_mountable_with_operation() for details.
5140 * Finish an asynchronous unmount operation that was started
5141 * with g_file_unmount_mountable_with_operation().
5143 * Returns: %TRUE if the operation finished successfully.
5144 * %FALSE otherwise.
5146 * Since: 2.22
5148 gboolean
5149 g_file_unmount_mountable_with_operation_finish (GFile *file,
5150 GAsyncResult *result,
5151 GError **error)
5153 GFileIface *iface;
5155 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5156 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5158 if (g_async_result_legacy_propagate_error (result, error))
5159 return FALSE;
5160 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5161 return g_task_propagate_boolean (G_TASK (result), error);
5163 iface = G_FILE_GET_IFACE (file);
5164 if (iface->unmount_mountable_with_operation_finish != NULL)
5165 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5166 else
5167 return (* iface->unmount_mountable_finish) (file, result, error);
5171 * g_file_eject_mountable:
5172 * @file: input #GFile
5173 * @flags: flags affecting the operation
5174 * @cancellable: (nullable): optional #GCancellable object,
5175 * %NULL to ignore
5176 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5177 * when the request is satisfied, or %NULL
5178 * @user_data: (closure): the data to pass to callback function
5180 * Starts an asynchronous eject on a mountable.
5181 * When this operation has completed, @callback will be called with
5182 * @user_user data, and the operation can be finalized with
5183 * g_file_eject_mountable_finish().
5185 * If @cancellable is not %NULL, then the operation can be cancelled by
5186 * triggering the cancellable object from another thread. If the operation
5187 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5189 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5191 void
5192 g_file_eject_mountable (GFile *file,
5193 GMountUnmountFlags flags,
5194 GCancellable *cancellable,
5195 GAsyncReadyCallback callback,
5196 gpointer user_data)
5198 GFileIface *iface;
5200 g_return_if_fail (G_IS_FILE (file));
5202 iface = G_FILE_GET_IFACE (file);
5204 if (iface->eject_mountable == NULL)
5206 g_task_report_new_error (file, callback, user_data,
5207 g_file_eject_mountable_with_operation,
5208 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5209 _("Operation not supported"));
5210 return;
5213 (* iface->eject_mountable) (file,
5214 flags,
5215 cancellable,
5216 callback,
5217 user_data);
5221 * g_file_eject_mountable_finish:
5222 * @file: input #GFile
5223 * @result: a #GAsyncResult
5224 * @error: a #GError, or %NULL
5226 * Finishes an asynchronous eject operation started by
5227 * g_file_eject_mountable().
5229 * Returns: %TRUE if the @file was ejected successfully.
5230 * %FALSE otherwise.
5232 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5233 * instead.
5235 gboolean
5236 g_file_eject_mountable_finish (GFile *file,
5237 GAsyncResult *result,
5238 GError **error)
5240 GFileIface *iface;
5242 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5243 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5245 if (g_async_result_legacy_propagate_error (result, error))
5246 return FALSE;
5247 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5248 return g_task_propagate_boolean (G_TASK (result), error);
5250 iface = G_FILE_GET_IFACE (file);
5251 return (* iface->eject_mountable_finish) (file, result, error);
5255 * g_file_eject_mountable_with_operation:
5256 * @file: input #GFile
5257 * @flags: flags affecting the operation
5258 * @mount_operation: (nullable): a #GMountOperation,
5259 * or %NULL to avoid user interaction
5260 * @cancellable: (nullable): optional #GCancellable object,
5261 * %NULL to ignore
5262 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5263 * when the request is satisfied, or %NULL
5264 * @user_data: (closure): the data to pass to callback function
5266 * Starts an asynchronous eject on a mountable.
5267 * When this operation has completed, @callback will be called with
5268 * @user_user data, and the operation can be finalized with
5269 * g_file_eject_mountable_with_operation_finish().
5271 * If @cancellable is not %NULL, then the operation can be cancelled by
5272 * triggering the cancellable object from another thread. If the operation
5273 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5275 * Since: 2.22
5277 void
5278 g_file_eject_mountable_with_operation (GFile *file,
5279 GMountUnmountFlags flags,
5280 GMountOperation *mount_operation,
5281 GCancellable *cancellable,
5282 GAsyncReadyCallback callback,
5283 gpointer user_data)
5285 GFileIface *iface;
5287 g_return_if_fail (G_IS_FILE (file));
5289 iface = G_FILE_GET_IFACE (file);
5291 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5293 g_task_report_new_error (file, callback, user_data,
5294 g_file_eject_mountable_with_operation,
5295 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5296 _("Operation not supported"));
5297 return;
5300 if (iface->eject_mountable_with_operation != NULL)
5301 (* iface->eject_mountable_with_operation) (file,
5302 flags,
5303 mount_operation,
5304 cancellable,
5305 callback,
5306 user_data);
5307 else
5308 (* iface->eject_mountable) (file,
5309 flags,
5310 cancellable,
5311 callback,
5312 user_data);
5316 * g_file_eject_mountable_with_operation_finish:
5317 * @file: input #GFile
5318 * @result: a #GAsyncResult
5319 * @error: a #GError, or %NULL
5321 * Finishes an asynchronous eject operation started by
5322 * g_file_eject_mountable_with_operation().
5324 * Returns: %TRUE if the @file was ejected successfully.
5325 * %FALSE otherwise.
5327 * Since: 2.22
5329 gboolean
5330 g_file_eject_mountable_with_operation_finish (GFile *file,
5331 GAsyncResult *result,
5332 GError **error)
5334 GFileIface *iface;
5336 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5337 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5339 if (g_async_result_legacy_propagate_error (result, error))
5340 return FALSE;
5341 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5342 return g_task_propagate_boolean (G_TASK (result), error);
5344 iface = G_FILE_GET_IFACE (file);
5345 if (iface->eject_mountable_with_operation_finish != NULL)
5346 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5347 else
5348 return (* iface->eject_mountable_finish) (file, result, error);
5352 * g_file_monitor_directory:
5353 * @file: input #GFile
5354 * @flags: a set of #GFileMonitorFlags
5355 * @cancellable: (nullable): optional #GCancellable object,
5356 * %NULL to ignore
5357 * @error: a #GError, or %NULL
5359 * Obtains a directory monitor for the given file.
5360 * This may fail if directory monitoring is not supported.
5362 * If @cancellable is not %NULL, then the operation can be cancelled by
5363 * triggering the cancellable object from another thread. If the operation
5364 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5366 * It does not make sense for @flags to contain
5367 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5368 * directories. It is not possible to monitor all the files in a
5369 * directory for changes made via hard links; if you want to do this then
5370 * you must register individual watches with g_file_monitor().
5372 * Virtual: monitor_dir
5373 * Returns: (transfer full): a #GFileMonitor for the given @file,
5374 * or %NULL on error.
5375 * Free the returned object with g_object_unref().
5377 GFileMonitor *
5378 g_file_monitor_directory (GFile *file,
5379 GFileMonitorFlags flags,
5380 GCancellable *cancellable,
5381 GError **error)
5383 GFileIface *iface;
5385 g_return_val_if_fail (G_IS_FILE (file), NULL);
5386 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5388 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5389 return NULL;
5391 iface = G_FILE_GET_IFACE (file);
5393 if (iface->monitor_dir == NULL)
5395 g_set_error_literal (error, G_IO_ERROR,
5396 G_IO_ERROR_NOT_SUPPORTED,
5397 _("Operation not supported"));
5398 return NULL;
5401 return (* iface->monitor_dir) (file, flags, cancellable, error);
5405 * g_file_monitor_file:
5406 * @file: input #GFile
5407 * @flags: a set of #GFileMonitorFlags
5408 * @cancellable: (nullable): optional #GCancellable object,
5409 * %NULL to ignore
5410 * @error: a #GError, or %NULL
5412 * Obtains a file monitor for the given file. If no file notification
5413 * mechanism exists, then regular polling of the file is used.
5415 * If @cancellable is not %NULL, then the operation can be cancelled by
5416 * triggering the cancellable object from another thread. If the operation
5417 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5419 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5420 * will also attempt to report changes made to the file via another
5421 * filename (ie, a hard link). Without this flag, you can only rely on
5422 * changes made through the filename contained in @file to be
5423 * reported. Using this flag may result in an increase in resource
5424 * usage, and may not have any effect depending on the #GFileMonitor
5425 * backend and/or filesystem type.
5427 * Returns: (transfer full): a #GFileMonitor for the given @file,
5428 * or %NULL on error.
5429 * Free the returned object with g_object_unref().
5431 GFileMonitor *
5432 g_file_monitor_file (GFile *file,
5433 GFileMonitorFlags flags,
5434 GCancellable *cancellable,
5435 GError **error)
5437 GFileIface *iface;
5438 GFileMonitor *monitor;
5440 g_return_val_if_fail (G_IS_FILE (file), NULL);
5442 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5443 return NULL;
5445 iface = G_FILE_GET_IFACE (file);
5447 monitor = NULL;
5449 if (iface->monitor_file)
5450 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5452 /* Fallback to polling */
5453 if (monitor == NULL)
5454 monitor = _g_poll_file_monitor_new (file);
5456 return monitor;
5460 * g_file_monitor:
5461 * @file: input #GFile
5462 * @flags: a set of #GFileMonitorFlags
5463 * @cancellable: (nullable): optional #GCancellable object,
5464 * %NULL to ignore
5465 * @error: a #GError, or %NULL
5467 * Obtains a file or directory monitor for the given file,
5468 * depending on the type of the file.
5470 * If @cancellable is not %NULL, then the operation can be cancelled by
5471 * triggering the cancellable object from another thread. If the operation
5472 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5474 * Returns: (transfer full): a #GFileMonitor for the given @file,
5475 * or %NULL on error.
5476 * Free the returned object with g_object_unref().
5478 * Since: 2.18
5480 GFileMonitor *
5481 g_file_monitor (GFile *file,
5482 GFileMonitorFlags flags,
5483 GCancellable *cancellable,
5484 GError **error)
5486 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5487 return g_file_monitor_directory (file,
5488 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5489 cancellable, error);
5490 else
5491 return g_file_monitor_file (file, flags, cancellable, error);
5494 /********************************************
5495 * Default implementation of async ops *
5496 ********************************************/
5498 typedef struct {
5499 char *attributes;
5500 GFileQueryInfoFlags flags;
5501 } QueryInfoAsyncData;
5503 static void
5504 query_info_data_free (QueryInfoAsyncData *data)
5506 g_free (data->attributes);
5507 g_free (data);
5510 static void
5511 query_info_async_thread (GTask *task,
5512 gpointer object,
5513 gpointer task_data,
5514 GCancellable *cancellable)
5516 QueryInfoAsyncData *data = task_data;
5517 GFileInfo *info;
5518 GError *error = NULL;
5520 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5521 if (info)
5522 g_task_return_pointer (task, info, g_object_unref);
5523 else
5524 g_task_return_error (task, error);
5527 static void
5528 g_file_real_query_info_async (GFile *file,
5529 const char *attributes,
5530 GFileQueryInfoFlags flags,
5531 int io_priority,
5532 GCancellable *cancellable,
5533 GAsyncReadyCallback callback,
5534 gpointer user_data)
5536 GTask *task;
5537 QueryInfoAsyncData *data;
5539 data = g_new0 (QueryInfoAsyncData, 1);
5540 data->attributes = g_strdup (attributes);
5541 data->flags = flags;
5543 task = g_task_new (file, cancellable, callback, user_data);
5544 g_task_set_source_tag (task, g_file_real_query_info_async);
5545 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5546 g_task_set_priority (task, io_priority);
5547 g_task_run_in_thread (task, query_info_async_thread);
5548 g_object_unref (task);
5551 static GFileInfo *
5552 g_file_real_query_info_finish (GFile *file,
5553 GAsyncResult *res,
5554 GError **error)
5556 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5558 return g_task_propagate_pointer (G_TASK (res), error);
5561 static void
5562 query_filesystem_info_async_thread (GTask *task,
5563 gpointer object,
5564 gpointer task_data,
5565 GCancellable *cancellable)
5567 const char *attributes = task_data;
5568 GFileInfo *info;
5569 GError *error = NULL;
5571 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5572 if (info)
5573 g_task_return_pointer (task, info, g_object_unref);
5574 else
5575 g_task_return_error (task, error);
5578 static void
5579 g_file_real_query_filesystem_info_async (GFile *file,
5580 const char *attributes,
5581 int io_priority,
5582 GCancellable *cancellable,
5583 GAsyncReadyCallback callback,
5584 gpointer user_data)
5586 GTask *task;
5588 task = g_task_new (file, cancellable, callback, user_data);
5589 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5590 g_task_set_task_data (task, g_strdup (attributes), g_free);
5591 g_task_set_priority (task, io_priority);
5592 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5593 g_object_unref (task);
5596 static GFileInfo *
5597 g_file_real_query_filesystem_info_finish (GFile *file,
5598 GAsyncResult *res,
5599 GError **error)
5601 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5603 return g_task_propagate_pointer (G_TASK (res), error);
5606 static void
5607 enumerate_children_async_thread (GTask *task,
5608 gpointer object,
5609 gpointer task_data,
5610 GCancellable *cancellable)
5612 QueryInfoAsyncData *data = task_data;
5613 GFileEnumerator *enumerator;
5614 GError *error = NULL;
5616 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5617 if (error)
5618 g_task_return_error (task, error);
5619 else
5620 g_task_return_pointer (task, enumerator, g_object_unref);
5623 static void
5624 g_file_real_enumerate_children_async (GFile *file,
5625 const char *attributes,
5626 GFileQueryInfoFlags flags,
5627 int io_priority,
5628 GCancellable *cancellable,
5629 GAsyncReadyCallback callback,
5630 gpointer user_data)
5632 GTask *task;
5633 QueryInfoAsyncData *data;
5635 data = g_new0 (QueryInfoAsyncData, 1);
5636 data->attributes = g_strdup (attributes);
5637 data->flags = flags;
5639 task = g_task_new (file, cancellable, callback, user_data);
5640 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5641 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5642 g_task_set_priority (task, io_priority);
5643 g_task_run_in_thread (task, enumerate_children_async_thread);
5644 g_object_unref (task);
5647 static GFileEnumerator *
5648 g_file_real_enumerate_children_finish (GFile *file,
5649 GAsyncResult *res,
5650 GError **error)
5652 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5654 return g_task_propagate_pointer (G_TASK (res), error);
5657 static void
5658 open_read_async_thread (GTask *task,
5659 gpointer object,
5660 gpointer task_data,
5661 GCancellable *cancellable)
5663 GFileInputStream *stream;
5664 GError *error = NULL;
5666 stream = g_file_read (G_FILE (object), cancellable, &error);
5667 if (stream)
5668 g_task_return_pointer (task, stream, g_object_unref);
5669 else
5670 g_task_return_error (task, error);
5673 static void
5674 g_file_real_read_async (GFile *file,
5675 int io_priority,
5676 GCancellable *cancellable,
5677 GAsyncReadyCallback callback,
5678 gpointer user_data)
5680 GTask *task;
5682 task = g_task_new (file, cancellable, callback, user_data);
5683 g_task_set_source_tag (task, g_file_real_read_async);
5684 g_task_set_priority (task, io_priority);
5685 g_task_run_in_thread (task, open_read_async_thread);
5686 g_object_unref (task);
5689 static GFileInputStream *
5690 g_file_real_read_finish (GFile *file,
5691 GAsyncResult *res,
5692 GError **error)
5694 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5696 return g_task_propagate_pointer (G_TASK (res), error);
5699 static void
5700 append_to_async_thread (GTask *task,
5701 gpointer source_object,
5702 gpointer task_data,
5703 GCancellable *cancellable)
5705 GFileCreateFlags *data = task_data;
5706 GFileOutputStream *stream;
5707 GError *error = NULL;
5709 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5710 if (stream)
5711 g_task_return_pointer (task, stream, g_object_unref);
5712 else
5713 g_task_return_error (task, error);
5716 static void
5717 g_file_real_append_to_async (GFile *file,
5718 GFileCreateFlags flags,
5719 int io_priority,
5720 GCancellable *cancellable,
5721 GAsyncReadyCallback callback,
5722 gpointer user_data)
5724 GFileCreateFlags *data;
5725 GTask *task;
5727 data = g_new0 (GFileCreateFlags, 1);
5728 *data = flags;
5730 task = g_task_new (file, cancellable, callback, user_data);
5731 g_task_set_source_tag (task, g_file_real_append_to_async);
5732 g_task_set_task_data (task, data, g_free);
5733 g_task_set_priority (task, io_priority);
5735 g_task_run_in_thread (task, append_to_async_thread);
5736 g_object_unref (task);
5739 static GFileOutputStream *
5740 g_file_real_append_to_finish (GFile *file,
5741 GAsyncResult *res,
5742 GError **error)
5744 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5746 return g_task_propagate_pointer (G_TASK (res), error);
5749 static void
5750 create_async_thread (GTask *task,
5751 gpointer source_object,
5752 gpointer task_data,
5753 GCancellable *cancellable)
5755 GFileCreateFlags *data = task_data;
5756 GFileOutputStream *stream;
5757 GError *error = NULL;
5759 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5760 if (stream)
5761 g_task_return_pointer (task, stream, g_object_unref);
5762 else
5763 g_task_return_error (task, error);
5766 static void
5767 g_file_real_create_async (GFile *file,
5768 GFileCreateFlags flags,
5769 int io_priority,
5770 GCancellable *cancellable,
5771 GAsyncReadyCallback callback,
5772 gpointer user_data)
5774 GFileCreateFlags *data;
5775 GTask *task;
5777 data = g_new0 (GFileCreateFlags, 1);
5778 *data = flags;
5780 task = g_task_new (file, cancellable, callback, user_data);
5781 g_task_set_source_tag (task, g_file_real_create_async);
5782 g_task_set_task_data (task, data, g_free);
5783 g_task_set_priority (task, io_priority);
5785 g_task_run_in_thread (task, create_async_thread);
5786 g_object_unref (task);
5789 static GFileOutputStream *
5790 g_file_real_create_finish (GFile *file,
5791 GAsyncResult *res,
5792 GError **error)
5794 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5796 return g_task_propagate_pointer (G_TASK (res), error);
5799 typedef struct {
5800 GFileOutputStream *stream;
5801 char *etag;
5802 gboolean make_backup;
5803 GFileCreateFlags flags;
5804 } ReplaceAsyncData;
5806 static void
5807 replace_async_data_free (ReplaceAsyncData *data)
5809 if (data->stream)
5810 g_object_unref (data->stream);
5811 g_free (data->etag);
5812 g_free (data);
5815 static void
5816 replace_async_thread (GTask *task,
5817 gpointer source_object,
5818 gpointer task_data,
5819 GCancellable *cancellable)
5821 GFileOutputStream *stream;
5822 ReplaceAsyncData *data = task_data;
5823 GError *error = NULL;
5825 stream = g_file_replace (G_FILE (source_object),
5826 data->etag,
5827 data->make_backup,
5828 data->flags,
5829 cancellable,
5830 &error);
5832 if (stream)
5833 g_task_return_pointer (task, stream, g_object_unref);
5834 else
5835 g_task_return_error (task, error);
5838 static void
5839 g_file_real_replace_async (GFile *file,
5840 const char *etag,
5841 gboolean make_backup,
5842 GFileCreateFlags flags,
5843 int io_priority,
5844 GCancellable *cancellable,
5845 GAsyncReadyCallback callback,
5846 gpointer user_data)
5848 GTask *task;
5849 ReplaceAsyncData *data;
5851 data = g_new0 (ReplaceAsyncData, 1);
5852 data->etag = g_strdup (etag);
5853 data->make_backup = make_backup;
5854 data->flags = flags;
5856 task = g_task_new (file, cancellable, callback, user_data);
5857 g_task_set_source_tag (task, g_file_real_replace_async);
5858 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5859 g_task_set_priority (task, io_priority);
5861 g_task_run_in_thread (task, replace_async_thread);
5862 g_object_unref (task);
5865 static GFileOutputStream *
5866 g_file_real_replace_finish (GFile *file,
5867 GAsyncResult *res,
5868 GError **error)
5870 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5872 return g_task_propagate_pointer (G_TASK (res), error);
5875 static void
5876 delete_async_thread (GTask *task,
5877 gpointer object,
5878 gpointer task_data,
5879 GCancellable *cancellable)
5881 GError *error = NULL;
5883 if (g_file_delete (G_FILE (object), cancellable, &error))
5884 g_task_return_boolean (task, TRUE);
5885 else
5886 g_task_return_error (task, error);
5889 static void
5890 g_file_real_delete_async (GFile *file,
5891 int io_priority,
5892 GCancellable *cancellable,
5893 GAsyncReadyCallback callback,
5894 gpointer user_data)
5896 GTask *task;
5898 task = g_task_new (file, cancellable, callback, user_data);
5899 g_task_set_source_tag (task, g_file_real_delete_async);
5900 g_task_set_priority (task, io_priority);
5901 g_task_run_in_thread (task, delete_async_thread);
5902 g_object_unref (task);
5905 static gboolean
5906 g_file_real_delete_finish (GFile *file,
5907 GAsyncResult *res,
5908 GError **error)
5910 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5912 return g_task_propagate_boolean (G_TASK (res), error);
5915 static void
5916 trash_async_thread (GTask *task,
5917 gpointer object,
5918 gpointer task_data,
5919 GCancellable *cancellable)
5921 GError *error = NULL;
5923 if (g_file_trash (G_FILE (object), cancellable, &error))
5924 g_task_return_boolean (task, TRUE);
5925 else
5926 g_task_return_error (task, error);
5929 static void
5930 g_file_real_trash_async (GFile *file,
5931 int io_priority,
5932 GCancellable *cancellable,
5933 GAsyncReadyCallback callback,
5934 gpointer user_data)
5936 GTask *task;
5938 task = g_task_new (file, cancellable, callback, user_data);
5939 g_task_set_source_tag (task, g_file_real_trash_async);
5940 g_task_set_priority (task, io_priority);
5941 g_task_run_in_thread (task, trash_async_thread);
5942 g_object_unref (task);
5945 static gboolean
5946 g_file_real_trash_finish (GFile *file,
5947 GAsyncResult *res,
5948 GError **error)
5950 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5952 return g_task_propagate_boolean (G_TASK (res), error);
5955 static void
5956 make_directory_async_thread (GTask *task,
5957 gpointer object,
5958 gpointer task_data,
5959 GCancellable *cancellable)
5961 GError *error = NULL;
5963 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5964 g_task_return_boolean (task, TRUE);
5965 else
5966 g_task_return_error (task, error);
5969 static void
5970 g_file_real_make_directory_async (GFile *file,
5971 int io_priority,
5972 GCancellable *cancellable,
5973 GAsyncReadyCallback callback,
5974 gpointer user_data)
5976 GTask *task;
5978 task = g_task_new (file, cancellable, callback, user_data);
5979 g_task_set_source_tag (task, g_file_real_make_directory_async);
5980 g_task_set_priority (task, io_priority);
5981 g_task_run_in_thread (task, make_directory_async_thread);
5982 g_object_unref (task);
5985 static gboolean
5986 g_file_real_make_directory_finish (GFile *file,
5987 GAsyncResult *res,
5988 GError **error)
5990 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5992 return g_task_propagate_boolean (G_TASK (res), error);
5995 static void
5996 open_readwrite_async_thread (GTask *task,
5997 gpointer object,
5998 gpointer task_data,
5999 GCancellable *cancellable)
6001 GFileIOStream *stream;
6002 GError *error = NULL;
6004 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6006 if (stream == NULL)
6007 g_task_return_error (task, error);
6008 else
6009 g_task_return_pointer (task, stream, g_object_unref);
6012 static void
6013 g_file_real_open_readwrite_async (GFile *file,
6014 int io_priority,
6015 GCancellable *cancellable,
6016 GAsyncReadyCallback callback,
6017 gpointer user_data)
6019 GTask *task;
6021 task = g_task_new (file, cancellable, callback, user_data);
6022 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6023 g_task_set_priority (task, io_priority);
6025 g_task_run_in_thread (task, open_readwrite_async_thread);
6026 g_object_unref (task);
6029 static GFileIOStream *
6030 g_file_real_open_readwrite_finish (GFile *file,
6031 GAsyncResult *res,
6032 GError **error)
6034 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6036 return g_task_propagate_pointer (G_TASK (res), error);
6039 static void
6040 create_readwrite_async_thread (GTask *task,
6041 gpointer object,
6042 gpointer task_data,
6043 GCancellable *cancellable)
6045 GFileCreateFlags *data = task_data;
6046 GFileIOStream *stream;
6047 GError *error = NULL;
6049 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6051 if (stream == NULL)
6052 g_task_return_error (task, error);
6053 else
6054 g_task_return_pointer (task, stream, g_object_unref);
6057 static void
6058 g_file_real_create_readwrite_async (GFile *file,
6059 GFileCreateFlags flags,
6060 int io_priority,
6061 GCancellable *cancellable,
6062 GAsyncReadyCallback callback,
6063 gpointer user_data)
6065 GFileCreateFlags *data;
6066 GTask *task;
6068 data = g_new0 (GFileCreateFlags, 1);
6069 *data = flags;
6071 task = g_task_new (file, cancellable, callback, user_data);
6072 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6073 g_task_set_task_data (task, data, g_free);
6074 g_task_set_priority (task, io_priority);
6076 g_task_run_in_thread (task, create_readwrite_async_thread);
6077 g_object_unref (task);
6080 static GFileIOStream *
6081 g_file_real_create_readwrite_finish (GFile *file,
6082 GAsyncResult *res,
6083 GError **error)
6085 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6087 return g_task_propagate_pointer (G_TASK (res), error);
6090 typedef struct {
6091 char *etag;
6092 gboolean make_backup;
6093 GFileCreateFlags flags;
6094 } ReplaceRWAsyncData;
6096 static void
6097 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6099 g_free (data->etag);
6100 g_free (data);
6103 static void
6104 replace_readwrite_async_thread (GTask *task,
6105 gpointer object,
6106 gpointer task_data,
6107 GCancellable *cancellable)
6109 GFileIOStream *stream;
6110 GError *error = NULL;
6111 ReplaceRWAsyncData *data = task_data;
6113 stream = g_file_replace_readwrite (G_FILE (object),
6114 data->etag,
6115 data->make_backup,
6116 data->flags,
6117 cancellable,
6118 &error);
6120 if (stream == NULL)
6121 g_task_return_error (task, error);
6122 else
6123 g_task_return_pointer (task, stream, g_object_unref);
6126 static void
6127 g_file_real_replace_readwrite_async (GFile *file,
6128 const char *etag,
6129 gboolean make_backup,
6130 GFileCreateFlags flags,
6131 int io_priority,
6132 GCancellable *cancellable,
6133 GAsyncReadyCallback callback,
6134 gpointer user_data)
6136 GTask *task;
6137 ReplaceRWAsyncData *data;
6139 data = g_new0 (ReplaceRWAsyncData, 1);
6140 data->etag = g_strdup (etag);
6141 data->make_backup = make_backup;
6142 data->flags = flags;
6144 task = g_task_new (file, cancellable, callback, user_data);
6145 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6146 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6147 g_task_set_priority (task, io_priority);
6149 g_task_run_in_thread (task, replace_readwrite_async_thread);
6150 g_object_unref (task);
6153 static GFileIOStream *
6154 g_file_real_replace_readwrite_finish (GFile *file,
6155 GAsyncResult *res,
6156 GError **error)
6158 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6160 return g_task_propagate_pointer (G_TASK (res), error);
6163 static void
6164 set_display_name_async_thread (GTask *task,
6165 gpointer object,
6166 gpointer task_data,
6167 GCancellable *cancellable)
6169 GError *error = NULL;
6170 char *name = task_data;
6171 GFile *file;
6173 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6175 if (file == NULL)
6176 g_task_return_error (task, error);
6177 else
6178 g_task_return_pointer (task, file, g_object_unref);
6181 static void
6182 g_file_real_set_display_name_async (GFile *file,
6183 const char *display_name,
6184 int io_priority,
6185 GCancellable *cancellable,
6186 GAsyncReadyCallback callback,
6187 gpointer user_data)
6189 GTask *task;
6191 task = g_task_new (file, cancellable, callback, user_data);
6192 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6193 g_task_set_task_data (task, g_strdup (display_name), g_free);
6194 g_task_set_priority (task, io_priority);
6196 g_task_run_in_thread (task, set_display_name_async_thread);
6197 g_object_unref (task);
6200 static GFile *
6201 g_file_real_set_display_name_finish (GFile *file,
6202 GAsyncResult *res,
6203 GError **error)
6205 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6207 return g_task_propagate_pointer (G_TASK (res), error);
6210 typedef struct {
6211 GFileQueryInfoFlags flags;
6212 GFileInfo *info;
6213 gboolean res;
6214 GError *error;
6215 } SetInfoAsyncData;
6217 static void
6218 set_info_data_free (SetInfoAsyncData *data)
6220 if (data->info)
6221 g_object_unref (data->info);
6222 if (data->error)
6223 g_error_free (data->error);
6224 g_free (data);
6227 static void
6228 set_info_async_thread (GTask *task,
6229 gpointer object,
6230 gpointer task_data,
6231 GCancellable *cancellable)
6233 SetInfoAsyncData *data = task_data;
6235 data->error = NULL;
6236 data->res = g_file_set_attributes_from_info (G_FILE (object),
6237 data->info,
6238 data->flags,
6239 cancellable,
6240 &data->error);
6243 static void
6244 g_file_real_set_attributes_async (GFile *file,
6245 GFileInfo *info,
6246 GFileQueryInfoFlags flags,
6247 int io_priority,
6248 GCancellable *cancellable,
6249 GAsyncReadyCallback callback,
6250 gpointer user_data)
6252 GTask *task;
6253 SetInfoAsyncData *data;
6255 data = g_new0 (SetInfoAsyncData, 1);
6256 data->info = g_file_info_dup (info);
6257 data->flags = flags;
6259 task = g_task_new (file, cancellable, callback, user_data);
6260 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6261 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6262 g_task_set_priority (task, io_priority);
6264 g_task_run_in_thread (task, set_info_async_thread);
6265 g_object_unref (task);
6268 static gboolean
6269 g_file_real_set_attributes_finish (GFile *file,
6270 GAsyncResult *res,
6271 GFileInfo **info,
6272 GError **error)
6274 SetInfoAsyncData *data;
6276 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6278 data = g_task_get_task_data (G_TASK (res));
6280 if (info)
6281 *info = g_object_ref (data->info);
6283 if (error != NULL && data->error)
6284 *error = g_error_copy (data->error);
6286 return data->res;
6289 static void
6290 find_enclosing_mount_async_thread (GTask *task,
6291 gpointer object,
6292 gpointer task_data,
6293 GCancellable *cancellable)
6295 GError *error = NULL;
6296 GMount *mount;
6298 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6300 if (mount == NULL)
6301 g_task_return_error (task, error);
6302 else
6303 g_task_return_pointer (task, mount, g_object_unref);
6306 static void
6307 g_file_real_find_enclosing_mount_async (GFile *file,
6308 int io_priority,
6309 GCancellable *cancellable,
6310 GAsyncReadyCallback callback,
6311 gpointer user_data)
6313 GTask *task;
6315 task = g_task_new (file, cancellable, callback, user_data);
6316 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6317 g_task_set_priority (task, io_priority);
6319 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6320 g_object_unref (task);
6323 static GMount *
6324 g_file_real_find_enclosing_mount_finish (GFile *file,
6325 GAsyncResult *res,
6326 GError **error)
6328 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6330 return g_task_propagate_pointer (G_TASK (res), error);
6334 typedef struct {
6335 GFile *source;
6336 GFile *destination;
6337 GFileCopyFlags flags;
6338 GFileProgressCallback progress_cb;
6339 gpointer progress_cb_data;
6340 } CopyAsyncData;
6342 static void
6343 copy_async_data_free (CopyAsyncData *data)
6345 g_object_unref (data->source);
6346 g_object_unref (data->destination);
6347 g_slice_free (CopyAsyncData, data);
6350 typedef struct {
6351 CopyAsyncData *data;
6352 goffset current_num_bytes;
6353 goffset total_num_bytes;
6354 } ProgressData;
6356 static gboolean
6357 copy_async_progress_in_main (gpointer user_data)
6359 ProgressData *progress = user_data;
6360 CopyAsyncData *data = progress->data;
6362 data->progress_cb (progress->current_num_bytes,
6363 progress->total_num_bytes,
6364 data->progress_cb_data);
6366 return FALSE;
6369 static void
6370 copy_async_progress_callback (goffset current_num_bytes,
6371 goffset total_num_bytes,
6372 gpointer user_data)
6374 GTask *task = user_data;
6375 CopyAsyncData *data = g_task_get_task_data (task);
6376 ProgressData *progress;
6378 progress = g_new (ProgressData, 1);
6379 progress->data = data;
6380 progress->current_num_bytes = current_num_bytes;
6381 progress->total_num_bytes = total_num_bytes;
6383 g_main_context_invoke_full (g_task_get_context (task),
6384 g_task_get_priority (task),
6385 copy_async_progress_in_main,
6386 progress,
6387 g_free);
6390 static void
6391 copy_async_thread (GTask *task,
6392 gpointer source,
6393 gpointer task_data,
6394 GCancellable *cancellable)
6396 CopyAsyncData *data = task_data;
6397 gboolean result;
6398 GError *error = NULL;
6400 result = g_file_copy (data->source,
6401 data->destination,
6402 data->flags,
6403 cancellable,
6404 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6405 task,
6406 &error);
6407 if (result)
6408 g_task_return_boolean (task, TRUE);
6409 else
6410 g_task_return_error (task, error);
6413 static void
6414 g_file_real_copy_async (GFile *source,
6415 GFile *destination,
6416 GFileCopyFlags flags,
6417 int io_priority,
6418 GCancellable *cancellable,
6419 GFileProgressCallback progress_callback,
6420 gpointer progress_callback_data,
6421 GAsyncReadyCallback callback,
6422 gpointer user_data)
6424 GTask *task;
6425 CopyAsyncData *data;
6427 data = g_slice_new (CopyAsyncData);
6428 data->source = g_object_ref (source);
6429 data->destination = g_object_ref (destination);
6430 data->flags = flags;
6431 data->progress_cb = progress_callback;
6432 data->progress_cb_data = progress_callback_data;
6434 task = g_task_new (source, cancellable, callback, user_data);
6435 g_task_set_source_tag (task, g_file_real_copy_async);
6436 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6437 g_task_set_priority (task, io_priority);
6438 g_task_run_in_thread (task, copy_async_thread);
6439 g_object_unref (task);
6442 static gboolean
6443 g_file_real_copy_finish (GFile *file,
6444 GAsyncResult *res,
6445 GError **error)
6447 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6449 return g_task_propagate_boolean (G_TASK (res), error);
6453 /********************************************
6454 * Default VFS operations *
6455 ********************************************/
6458 * g_file_new_for_path:
6459 * @path: (type filename): a string containing a relative or absolute path.
6460 * The string must be encoded in the glib filename encoding.
6462 * Constructs a #GFile for a given path. This operation never
6463 * fails, but the returned object might not support any I/O
6464 * operation if @path is malformed.
6466 * Returns: (transfer full): a new #GFile for the given @path.
6467 * Free the returned object with g_object_unref().
6469 GFile *
6470 g_file_new_for_path (const char *path)
6472 g_return_val_if_fail (path != NULL, NULL);
6474 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6478 * g_file_new_for_uri:
6479 * @uri: a UTF-8 string containing a URI
6481 * Constructs a #GFile for a given URI. This operation never
6482 * fails, but the returned object might not support any I/O
6483 * operation if @uri is malformed or if the uri type is
6484 * not supported.
6486 * Returns: (transfer full): a new #GFile for the given @uri.
6487 * Free the returned object with g_object_unref().
6489 GFile *
6490 g_file_new_for_uri (const char *uri)
6492 g_return_val_if_fail (uri != NULL, NULL);
6494 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6498 * g_file_new_tmp:
6499 * @tmpl: (type filename) (nullable): Template for the file
6500 * name, as in g_file_open_tmp(), or %NULL for a default template
6501 * @iostream: (out): on return, a #GFileIOStream for the created file
6502 * @error: a #GError, or %NULL
6504 * Opens a file in the preferred directory for temporary files (as
6505 * returned by g_get_tmp_dir()) and returns a #GFile and
6506 * #GFileIOStream pointing to it.
6508 * @tmpl should be a string in the GLib file name encoding
6509 * containing a sequence of six 'X' characters, and containing no
6510 * directory components. If it is %NULL, a default template is used.
6512 * Unlike the other #GFile constructors, this will return %NULL if
6513 * a temporary file could not be created.
6515 * Returns: (transfer full): a new #GFile.
6516 * Free the returned object with g_object_unref().
6518 * Since: 2.32
6520 GFile *
6521 g_file_new_tmp (const char *tmpl,
6522 GFileIOStream **iostream,
6523 GError **error)
6525 gint fd;
6526 gchar *path;
6527 GFile *file;
6528 GFileOutputStream *output;
6530 g_return_val_if_fail (iostream != NULL, NULL);
6532 fd = g_file_open_tmp (tmpl, &path, error);
6533 if (fd == -1)
6534 return NULL;
6536 file = g_file_new_for_path (path);
6538 output = _g_local_file_output_stream_new (fd);
6539 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6541 g_object_unref (output);
6542 g_free (path);
6544 return file;
6548 * g_file_parse_name:
6549 * @parse_name: a file name or path to be parsed
6551 * Constructs a #GFile with the given @parse_name (i.e. something
6552 * given by g_file_get_parse_name()). This operation never fails,
6553 * but the returned object might not support any I/O operation if
6554 * the @parse_name cannot be parsed.
6556 * Returns: (transfer full): a new #GFile.
6558 GFile *
6559 g_file_parse_name (const char *parse_name)
6561 g_return_val_if_fail (parse_name != NULL, NULL);
6563 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6567 * g_file_new_build_filename:
6568 * @first_element: (type filename): the first element in the path
6569 * @...: remaining elements in path, terminated by %NULL
6571 * Constructs a #GFile from a series of elements using the correct
6572 * separator for filenames.
6574 * Using this function is equivalent to calling g_build_filename(),
6575 * followed by g_file_new_for_path() on the result.
6577 * Returns: (transfer full): a new #GFile
6579 * Since: 2.56
6581 GFile *
6582 g_file_new_build_filename (const gchar *first_element,
6583 ...)
6585 gchar *str;
6586 GFile *file;
6587 va_list args;
6589 g_return_val_if_fail (first_element != NULL, NULL);
6591 va_start (args, first_element);
6592 str = g_build_filename_valist (first_element, &args);
6593 va_end (args);
6595 file = g_file_new_for_path (str);
6596 g_free (str);
6598 return file;
6601 static gboolean
6602 is_valid_scheme_character (char c)
6604 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6607 /* Following RFC 2396, valid schemes are built like:
6608 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6610 static gboolean
6611 has_valid_scheme (const char *uri)
6613 const char *p;
6615 p = uri;
6617 if (!g_ascii_isalpha (*p))
6618 return FALSE;
6620 do {
6621 p++;
6622 } while (is_valid_scheme_character (*p));
6624 return *p == ':';
6627 static GFile *
6628 new_for_cmdline_arg (const gchar *arg,
6629 const gchar *cwd)
6631 GFile *file;
6632 char *filename;
6634 if (g_path_is_absolute (arg))
6635 return g_file_new_for_path (arg);
6637 if (has_valid_scheme (arg))
6638 return g_file_new_for_uri (arg);
6640 if (cwd == NULL)
6642 char *current_dir;
6644 current_dir = g_get_current_dir ();
6645 filename = g_build_filename (current_dir, arg, NULL);
6646 g_free (current_dir);
6648 else
6649 filename = g_build_filename (cwd, arg, NULL);
6651 file = g_file_new_for_path (filename);
6652 g_free (filename);
6654 return file;
6658 * g_file_new_for_commandline_arg:
6659 * @arg: (type filename): a command line string
6661 * Creates a #GFile with the given argument from the command line.
6662 * The value of @arg can be either a URI, an absolute path or a
6663 * relative path resolved relative to the current working directory.
6664 * This operation never fails, but the returned object might not
6665 * support any I/O operation if @arg points to a malformed path.
6667 * Note that on Windows, this function expects its argument to be in
6668 * UTF-8 -- not the system code page. This means that you
6669 * should not use this function with string from argv as it is passed
6670 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6671 * the commandline. #GApplication also uses UTF-8 but
6672 * g_application_command_line_create_file_for_arg() may be more useful
6673 * for you there. It is also always possible to use this function with
6674 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6676 * Returns: (transfer full): a new #GFile.
6677 * Free the returned object with g_object_unref().
6679 GFile *
6680 g_file_new_for_commandline_arg (const char *arg)
6682 g_return_val_if_fail (arg != NULL, NULL);
6684 return new_for_cmdline_arg (arg, NULL);
6688 * g_file_new_for_commandline_arg_and_cwd:
6689 * @arg: (type filename): a command line string
6690 * @cwd: (type filename): the current working directory of the commandline
6692 * Creates a #GFile with the given argument from the command line.
6694 * This function is similar to g_file_new_for_commandline_arg() except
6695 * that it allows for passing the current working directory as an
6696 * argument instead of using the current working directory of the
6697 * process.
6699 * This is useful if the commandline argument was given in a context
6700 * other than the invocation of the current process.
6702 * See also g_application_command_line_create_file_for_arg().
6704 * Returns: (transfer full): a new #GFile
6706 * Since: 2.36
6708 GFile *
6709 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6710 const gchar *cwd)
6712 g_return_val_if_fail (arg != NULL, NULL);
6713 g_return_val_if_fail (cwd != NULL, NULL);
6715 return new_for_cmdline_arg (arg, cwd);
6719 * g_file_mount_enclosing_volume:
6720 * @location: input #GFile
6721 * @flags: flags affecting the operation
6722 * @mount_operation: (nullable): a #GMountOperation
6723 * or %NULL to avoid user interaction
6724 * @cancellable: (nullable): optional #GCancellable object,
6725 * %NULL to ignore
6726 * @callback: (nullable): a #GAsyncReadyCallback to call
6727 * when the request is satisfied, or %NULL
6728 * @user_data: the data to pass to callback function
6730 * Starts a @mount_operation, mounting the volume that contains
6731 * the file @location.
6733 * When this operation has completed, @callback will be called with
6734 * @user_user data, and the operation can be finalized with
6735 * g_file_mount_enclosing_volume_finish().
6737 * If @cancellable is not %NULL, then the operation can be cancelled by
6738 * triggering the cancellable object from another thread. If the operation
6739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6741 void
6742 g_file_mount_enclosing_volume (GFile *location,
6743 GMountMountFlags flags,
6744 GMountOperation *mount_operation,
6745 GCancellable *cancellable,
6746 GAsyncReadyCallback callback,
6747 gpointer user_data)
6749 GFileIface *iface;
6751 g_return_if_fail (G_IS_FILE (location));
6753 iface = G_FILE_GET_IFACE (location);
6755 if (iface->mount_enclosing_volume == NULL)
6757 g_task_report_new_error (location, callback, user_data,
6758 g_file_mount_enclosing_volume,
6759 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6760 _("volume doesn’t implement mount"));
6761 return;
6764 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6769 * g_file_mount_enclosing_volume_finish:
6770 * @location: input #GFile
6771 * @result: a #GAsyncResult
6772 * @error: a #GError, or %NULL
6774 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6776 * Returns: %TRUE if successful. If an error has occurred,
6777 * this function will return %FALSE and set @error
6778 * appropriately if present.
6780 gboolean
6781 g_file_mount_enclosing_volume_finish (GFile *location,
6782 GAsyncResult *result,
6783 GError **error)
6785 GFileIface *iface;
6787 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6788 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6790 if (g_async_result_legacy_propagate_error (result, error))
6791 return FALSE;
6792 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6793 return g_task_propagate_boolean (G_TASK (result), error);
6795 iface = G_FILE_GET_IFACE (location);
6797 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6800 /********************************************
6801 * Utility functions *
6802 ********************************************/
6805 * g_file_query_default_handler:
6806 * @file: a #GFile to open
6807 * @cancellable: optional #GCancellable object, %NULL to ignore
6808 * @error: a #GError, or %NULL
6810 * Returns the #GAppInfo that is registered as the default
6811 * application to handle the file specified by @file.
6813 * If @cancellable is not %NULL, then the operation can be cancelled by
6814 * triggering the cancellable object from another thread. If the operation
6815 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6817 * Returns: (transfer full): a #GAppInfo if the handle was found,
6818 * %NULL if there were errors.
6819 * When you are done with it, release it with g_object_unref()
6821 GAppInfo *
6822 g_file_query_default_handler (GFile *file,
6823 GCancellable *cancellable,
6824 GError **error)
6826 char *uri_scheme;
6827 const char *content_type;
6828 GAppInfo *appinfo;
6829 GFileInfo *info;
6830 char *path;
6832 uri_scheme = g_file_get_uri_scheme (file);
6833 if (uri_scheme && uri_scheme[0] != '\0')
6835 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6836 g_free (uri_scheme);
6838 if (appinfo != NULL)
6839 return appinfo;
6842 info = g_file_query_info (file,
6843 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6845 cancellable,
6846 error);
6847 if (info == NULL)
6848 return NULL;
6850 appinfo = NULL;
6852 content_type = g_file_info_get_content_type (info);
6853 if (content_type)
6855 /* Don't use is_native(), as we want to support fuse paths if available */
6856 path = g_file_get_path (file);
6857 appinfo = g_app_info_get_default_for_type (content_type,
6858 path == NULL);
6859 g_free (path);
6862 g_object_unref (info);
6864 if (appinfo != NULL)
6865 return appinfo;
6867 g_set_error_literal (error, G_IO_ERROR,
6868 G_IO_ERROR_NOT_SUPPORTED,
6869 _("No application is registered as handling this file"));
6870 return NULL;
6873 #define GET_CONTENT_BLOCK_SIZE 8192
6876 * g_file_load_contents:
6877 * @file: input #GFile
6878 * @cancellable: optional #GCancellable object, %NULL to ignore
6879 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6880 * @length: (out) (optional): a location to place the length of the contents of the file,
6881 * or %NULL if the length is not needed
6882 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6883 * or %NULL if the entity tag is not needed
6884 * @error: a #GError, or %NULL
6886 * Loads the content of the file into memory. The data is always
6887 * zero-terminated, but this is not included in the resultant @length.
6888 * The returned @content should be freed with g_free() when no longer
6889 * needed.
6891 * If @cancellable is not %NULL, then the operation can be cancelled by
6892 * triggering the cancellable object from another thread. If the operation
6893 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6895 * Returns: %TRUE if the @file's contents were successfully loaded.
6896 * %FALSE if there were errors.
6898 gboolean
6899 g_file_load_contents (GFile *file,
6900 GCancellable *cancellable,
6901 char **contents,
6902 gsize *length,
6903 char **etag_out,
6904 GError **error)
6906 GFileInputStream *in;
6907 GByteArray *content;
6908 gsize pos;
6909 gssize res;
6910 GFileInfo *info;
6912 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6913 g_return_val_if_fail (contents != NULL, FALSE);
6915 in = g_file_read (file, cancellable, error);
6916 if (in == NULL)
6917 return FALSE;
6919 content = g_byte_array_new ();
6920 pos = 0;
6922 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6923 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6924 content->data + pos,
6925 GET_CONTENT_BLOCK_SIZE,
6926 cancellable, error)) > 0)
6928 pos += res;
6929 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6932 if (etag_out)
6934 *etag_out = NULL;
6936 info = g_file_input_stream_query_info (in,
6937 G_FILE_ATTRIBUTE_ETAG_VALUE,
6938 cancellable,
6939 NULL);
6940 if (info)
6942 *etag_out = g_strdup (g_file_info_get_etag (info));
6943 g_object_unref (info);
6947 /* Ignore errors on close */
6948 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6949 g_object_unref (in);
6951 if (res < 0)
6953 /* error is set already */
6954 g_byte_array_free (content, TRUE);
6955 return FALSE;
6958 if (length)
6959 *length = pos;
6961 /* Zero terminate (we got an extra byte allocated for this */
6962 content->data[pos] = 0;
6964 *contents = (char *)g_byte_array_free (content, FALSE);
6966 return TRUE;
6969 typedef struct {
6970 GTask *task;
6971 GFileReadMoreCallback read_more_callback;
6972 GByteArray *content;
6973 gsize pos;
6974 char *etag;
6975 } LoadContentsData;
6978 static void
6979 load_contents_data_free (LoadContentsData *data)
6981 if (data->content)
6982 g_byte_array_free (data->content, TRUE);
6983 g_free (data->etag);
6984 g_free (data);
6987 static void
6988 load_contents_close_callback (GObject *obj,
6989 GAsyncResult *close_res,
6990 gpointer user_data)
6992 GInputStream *stream = G_INPUT_STREAM (obj);
6993 LoadContentsData *data = user_data;
6995 /* Ignore errors here, we're only reading anyway */
6996 g_input_stream_close_finish (stream, close_res, NULL);
6997 g_object_unref (stream);
6999 g_task_return_boolean (data->task, TRUE);
7000 g_object_unref (data->task);
7003 static void
7004 load_contents_fstat_callback (GObject *obj,
7005 GAsyncResult *stat_res,
7006 gpointer user_data)
7008 GInputStream *stream = G_INPUT_STREAM (obj);
7009 LoadContentsData *data = user_data;
7010 GFileInfo *info;
7012 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
7013 stat_res, NULL);
7014 if (info)
7016 data->etag = g_strdup (g_file_info_get_etag (info));
7017 g_object_unref (info);
7020 g_input_stream_close_async (stream, 0,
7021 g_task_get_cancellable (data->task),
7022 load_contents_close_callback, data);
7025 static void
7026 load_contents_read_callback (GObject *obj,
7027 GAsyncResult *read_res,
7028 gpointer user_data)
7030 GInputStream *stream = G_INPUT_STREAM (obj);
7031 LoadContentsData *data = user_data;
7032 GError *error = NULL;
7033 gssize read_size;
7035 read_size = g_input_stream_read_finish (stream, read_res, &error);
7037 if (read_size < 0)
7039 g_task_return_error (data->task, error);
7040 g_object_unref (data->task);
7042 /* Close the file ignoring any error */
7043 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7044 g_object_unref (stream);
7046 else if (read_size == 0)
7048 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7049 G_FILE_ATTRIBUTE_ETAG_VALUE,
7051 g_task_get_cancellable (data->task),
7052 load_contents_fstat_callback,
7053 data);
7055 else if (read_size > 0)
7057 data->pos += read_size;
7059 g_byte_array_set_size (data->content,
7060 data->pos + GET_CONTENT_BLOCK_SIZE);
7063 if (data->read_more_callback &&
7064 !data->read_more_callback ((char *)data->content->data, data->pos,
7065 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
7066 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7067 G_FILE_ATTRIBUTE_ETAG_VALUE,
7069 g_task_get_cancellable (data->task),
7070 load_contents_fstat_callback,
7071 data);
7072 else
7073 g_input_stream_read_async (stream,
7074 data->content->data + data->pos,
7075 GET_CONTENT_BLOCK_SIZE,
7077 g_task_get_cancellable (data->task),
7078 load_contents_read_callback,
7079 data);
7083 static void
7084 load_contents_open_callback (GObject *obj,
7085 GAsyncResult *open_res,
7086 gpointer user_data)
7088 GFile *file = G_FILE (obj);
7089 GFileInputStream *stream;
7090 LoadContentsData *data = user_data;
7091 GError *error = NULL;
7093 stream = g_file_read_finish (file, open_res, &error);
7095 if (stream)
7097 g_byte_array_set_size (data->content,
7098 data->pos + GET_CONTENT_BLOCK_SIZE);
7099 g_input_stream_read_async (G_INPUT_STREAM (stream),
7100 data->content->data + data->pos,
7101 GET_CONTENT_BLOCK_SIZE,
7103 g_task_get_cancellable (data->task),
7104 load_contents_read_callback,
7105 data);
7107 else
7109 g_task_return_error (data->task, error);
7110 g_object_unref (data->task);
7115 * g_file_load_partial_contents_async: (skip)
7116 * @file: input #GFile
7117 * @cancellable: optional #GCancellable object, %NULL to ignore
7118 * @read_more_callback: (scope call) (closure user_data): a
7119 * #GFileReadMoreCallback to receive partial data
7120 * and to specify whether further data should be read
7121 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7122 * when the request is satisfied
7123 * @user_data: the data to pass to the callback functions
7125 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7126 * be used to stop reading from the file when appropriate, else this
7127 * function will behave exactly as g_file_load_contents_async(). This
7128 * operation can be finished by g_file_load_partial_contents_finish().
7130 * Users of this function should be aware that @user_data is passed to
7131 * both the @read_more_callback and the @callback.
7133 * If @cancellable is not %NULL, then the operation can be cancelled by
7134 * triggering the cancellable object from another thread. If the operation
7135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7137 void
7138 g_file_load_partial_contents_async (GFile *file,
7139 GCancellable *cancellable,
7140 GFileReadMoreCallback read_more_callback,
7141 GAsyncReadyCallback callback,
7142 gpointer user_data)
7144 LoadContentsData *data;
7146 g_return_if_fail (G_IS_FILE (file));
7148 data = g_new0 (LoadContentsData, 1);
7149 data->read_more_callback = read_more_callback;
7150 data->content = g_byte_array_new ();
7152 data->task = g_task_new (file, cancellable, callback, user_data);
7153 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7154 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7156 g_file_read_async (file,
7158 g_task_get_cancellable (data->task),
7159 load_contents_open_callback,
7160 data);
7164 * g_file_load_partial_contents_finish:
7165 * @file: input #GFile
7166 * @res: a #GAsyncResult
7167 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7168 * @length: (out) (optional): a location to place the length of the contents of the file,
7169 * or %NULL if the length is not needed
7170 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7171 * or %NULL if the entity tag is not needed
7172 * @error: a #GError, or %NULL
7174 * Finishes an asynchronous partial load operation that was started
7175 * with g_file_load_partial_contents_async(). The data is always
7176 * zero-terminated, but this is not included in the resultant @length.
7177 * The returned @content should be freed with g_free() when no longer
7178 * needed.
7180 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7181 * present, it will be set appropriately.
7183 gboolean
7184 g_file_load_partial_contents_finish (GFile *file,
7185 GAsyncResult *res,
7186 char **contents,
7187 gsize *length,
7188 char **etag_out,
7189 GError **error)
7191 GTask *task;
7192 LoadContentsData *data;
7194 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7195 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7196 g_return_val_if_fail (contents != NULL, FALSE);
7198 task = G_TASK (res);
7200 if (!g_task_propagate_boolean (task, error))
7202 if (length)
7203 *length = 0;
7204 return FALSE;
7207 data = g_task_get_task_data (task);
7209 if (length)
7210 *length = data->pos;
7212 if (etag_out)
7214 *etag_out = data->etag;
7215 data->etag = NULL;
7218 /* Zero terminate */
7219 g_byte_array_set_size (data->content, data->pos + 1);
7220 data->content->data[data->pos] = 0;
7222 *contents = (char *)g_byte_array_free (data->content, FALSE);
7223 data->content = NULL;
7225 return TRUE;
7229 * g_file_load_contents_async:
7230 * @file: input #GFile
7231 * @cancellable: optional #GCancellable object, %NULL to ignore
7232 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7233 * @user_data: the data to pass to callback function
7235 * Starts an asynchronous load of the @file's contents.
7237 * For more details, see g_file_load_contents() which is
7238 * the synchronous version of this call.
7240 * When the load operation has completed, @callback will be called
7241 * with @user data. To finish the operation, call
7242 * g_file_load_contents_finish() with the #GAsyncResult returned by
7243 * the @callback.
7245 * If @cancellable is not %NULL, then the operation can be cancelled by
7246 * triggering the cancellable object from another thread. If the operation
7247 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7249 void
7250 g_file_load_contents_async (GFile *file,
7251 GCancellable *cancellable,
7252 GAsyncReadyCallback callback,
7253 gpointer user_data)
7255 g_file_load_partial_contents_async (file,
7256 cancellable,
7257 NULL,
7258 callback, user_data);
7262 * g_file_load_contents_finish:
7263 * @file: input #GFile
7264 * @res: a #GAsyncResult
7265 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7266 * @length: (out) (optional): a location to place the length of the contents of the file,
7267 * or %NULL if the length is not needed
7268 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7269 * or %NULL if the entity tag is not needed
7270 * @error: a #GError, or %NULL
7272 * Finishes an asynchronous load of the @file's contents.
7273 * The contents are placed in @contents, and @length is set to the
7274 * size of the @contents string. The @content should be freed with
7275 * g_free() when no longer needed. If @etag_out is present, it will be
7276 * set to the new entity tag for the @file.
7278 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7279 * present, it will be set appropriately.
7281 gboolean
7282 g_file_load_contents_finish (GFile *file,
7283 GAsyncResult *res,
7284 char **contents,
7285 gsize *length,
7286 char **etag_out,
7287 GError **error)
7289 return g_file_load_partial_contents_finish (file,
7290 res,
7291 contents,
7292 length,
7293 etag_out,
7294 error);
7298 * g_file_replace_contents:
7299 * @file: input #GFile
7300 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7301 * @length: the length of @contents in bytes
7302 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7303 * or %NULL
7304 * @make_backup: %TRUE if a backup should be created
7305 * @flags: a set of #GFileCreateFlags
7306 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7307 * for the document. This should be freed with g_free() when no longer
7308 * needed, or %NULL
7309 * @cancellable: optional #GCancellable object, %NULL to ignore
7310 * @error: a #GError, or %NULL
7312 * Replaces the contents of @file with @contents of @length bytes.
7314 * If @etag is specified (not %NULL), any existing file must have that etag,
7315 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7317 * If @make_backup is %TRUE, this function will attempt to make a backup
7318 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7319 * file contents in the safest way possible. For example, atomic renames are
7320 * used when replacing local files’ contents.
7322 * If @cancellable is not %NULL, then the operation can be cancelled by
7323 * triggering the cancellable object from another thread. If the operation
7324 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7326 * The returned @new_etag can be used to verify that the file hasn't
7327 * changed the next time it is saved over.
7329 * Returns: %TRUE if successful. If an error has occurred, this function
7330 * will return %FALSE and set @error appropriately if present.
7332 gboolean
7333 g_file_replace_contents (GFile *file,
7334 const char *contents,
7335 gsize length,
7336 const char *etag,
7337 gboolean make_backup,
7338 GFileCreateFlags flags,
7339 char **new_etag,
7340 GCancellable *cancellable,
7341 GError **error)
7343 GFileOutputStream *out;
7344 gsize pos, remainder;
7345 gssize res;
7346 gboolean ret;
7348 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7349 g_return_val_if_fail (contents != NULL, FALSE);
7351 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7352 if (out == NULL)
7353 return FALSE;
7355 pos = 0;
7356 remainder = length;
7357 while (remainder > 0 &&
7358 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7359 contents + pos,
7360 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7361 cancellable,
7362 error)) > 0)
7364 pos += res;
7365 remainder -= res;
7368 if (remainder > 0 && res < 0)
7370 /* Ignore errors on close */
7371 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7372 g_object_unref (out);
7374 /* error is set already */
7375 return FALSE;
7378 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7380 if (new_etag)
7381 *new_etag = g_file_output_stream_get_etag (out);
7383 g_object_unref (out);
7385 return ret;
7388 typedef struct {
7389 GTask *task;
7390 GBytes *content;
7391 gsize pos;
7392 char *etag;
7393 gboolean failed;
7394 } ReplaceContentsData;
7396 static void
7397 replace_contents_data_free (ReplaceContentsData *data)
7399 g_bytes_unref (data->content);
7400 g_free (data->etag);
7401 g_free (data);
7404 static void
7405 replace_contents_close_callback (GObject *obj,
7406 GAsyncResult *close_res,
7407 gpointer user_data)
7409 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7410 ReplaceContentsData *data = user_data;
7412 /* Ignore errors here, we're only reading anyway */
7413 g_output_stream_close_finish (stream, close_res, NULL);
7414 g_object_unref (stream);
7416 if (!data->failed)
7418 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7419 g_task_return_boolean (data->task, TRUE);
7421 g_object_unref (data->task);
7424 static void
7425 replace_contents_write_callback (GObject *obj,
7426 GAsyncResult *read_res,
7427 gpointer user_data)
7429 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7430 ReplaceContentsData *data = user_data;
7431 GError *error = NULL;
7432 gssize write_size;
7434 write_size = g_output_stream_write_finish (stream, read_res, &error);
7436 if (write_size <= 0)
7438 /* Error or EOF, close the file */
7439 if (write_size < 0)
7441 data->failed = TRUE;
7442 g_task_return_error (data->task, error);
7444 g_output_stream_close_async (stream, 0,
7445 g_task_get_cancellable (data->task),
7446 replace_contents_close_callback, data);
7448 else if (write_size > 0)
7450 const gchar *content;
7451 gsize length;
7453 content = g_bytes_get_data (data->content, &length);
7454 data->pos += write_size;
7456 if (data->pos >= length)
7457 g_output_stream_close_async (stream, 0,
7458 g_task_get_cancellable (data->task),
7459 replace_contents_close_callback, data);
7460 else
7461 g_output_stream_write_async (stream,
7462 content + data->pos,
7463 length - data->pos,
7465 g_task_get_cancellable (data->task),
7466 replace_contents_write_callback,
7467 data);
7471 static void
7472 replace_contents_open_callback (GObject *obj,
7473 GAsyncResult *open_res,
7474 gpointer user_data)
7476 GFile *file = G_FILE (obj);
7477 GFileOutputStream *stream;
7478 ReplaceContentsData *data = user_data;
7479 GError *error = NULL;
7481 stream = g_file_replace_finish (file, open_res, &error);
7483 if (stream)
7485 const gchar *content;
7486 gsize length;
7488 content = g_bytes_get_data (data->content, &length);
7489 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7490 content + data->pos,
7491 length - data->pos,
7493 g_task_get_cancellable (data->task),
7494 replace_contents_write_callback,
7495 data);
7497 else
7499 g_task_return_error (data->task, error);
7500 g_object_unref (data->task);
7505 * g_file_replace_contents_async:
7506 * @file: input #GFile
7507 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7508 * @length: the length of @contents in bytes
7509 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7510 * @make_backup: %TRUE if a backup should be created
7511 * @flags: a set of #GFileCreateFlags
7512 * @cancellable: optional #GCancellable object, %NULL to ignore
7513 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7514 * @user_data: the data to pass to callback function
7516 * Starts an asynchronous replacement of @file with the given
7517 * @contents of @length bytes. @etag will replace the document's
7518 * current entity tag.
7520 * When this operation has completed, @callback will be called with
7521 * @user_user data, and the operation can be finalized with
7522 * g_file_replace_contents_finish().
7524 * If @cancellable is not %NULL, then the operation can be cancelled by
7525 * triggering the cancellable object from another thread. If the operation
7526 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7528 * If @make_backup is %TRUE, this function will attempt to
7529 * make a backup of @file.
7531 * Note that no copy of @content will be made, so it must stay valid
7532 * until @callback is called. See g_file_replace_contents_bytes_async()
7533 * for a #GBytes version that will automatically hold a reference to the
7534 * contents (without copying) for the duration of the call.
7536 void
7537 g_file_replace_contents_async (GFile *file,
7538 const char *contents,
7539 gsize length,
7540 const char *etag,
7541 gboolean make_backup,
7542 GFileCreateFlags flags,
7543 GCancellable *cancellable,
7544 GAsyncReadyCallback callback,
7545 gpointer user_data)
7547 GBytes *bytes;
7549 bytes = g_bytes_new_static (contents, length);
7550 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7551 cancellable, callback, user_data);
7552 g_bytes_unref (bytes);
7556 * g_file_replace_contents_bytes_async:
7557 * @file: input #GFile
7558 * @contents: a #GBytes
7559 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7560 * @make_backup: %TRUE if a backup should be created
7561 * @flags: a set of #GFileCreateFlags
7562 * @cancellable: optional #GCancellable object, %NULL to ignore
7563 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7564 * @user_data: the data to pass to callback function
7566 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7567 * This function will keep a ref on @contents until the operation is done.
7568 * Unlike g_file_replace_contents_async() this allows forgetting about the
7569 * content without waiting for the callback.
7571 * When this operation has completed, @callback will be called with
7572 * @user_user data, and the operation can be finalized with
7573 * g_file_replace_contents_finish().
7575 * Since: 2.40
7577 void
7578 g_file_replace_contents_bytes_async (GFile *file,
7579 GBytes *contents,
7580 const char *etag,
7581 gboolean make_backup,
7582 GFileCreateFlags flags,
7583 GCancellable *cancellable,
7584 GAsyncReadyCallback callback,
7585 gpointer user_data)
7587 ReplaceContentsData *data;
7589 g_return_if_fail (G_IS_FILE (file));
7590 g_return_if_fail (contents != NULL);
7592 data = g_new0 (ReplaceContentsData, 1);
7594 data->content = g_bytes_ref (contents);
7596 data->task = g_task_new (file, cancellable, callback, user_data);
7597 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7598 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7600 g_file_replace_async (file,
7601 etag,
7602 make_backup,
7603 flags,
7605 g_task_get_cancellable (data->task),
7606 replace_contents_open_callback,
7607 data);
7611 * g_file_replace_contents_finish:
7612 * @file: input #GFile
7613 * @res: a #GAsyncResult
7614 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7615 * for the document. This should be freed with g_free() when it is no
7616 * longer needed, or %NULL
7617 * @error: a #GError, or %NULL
7619 * Finishes an asynchronous replace of the given @file. See
7620 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7621 * tag for the document, if present.
7623 * Returns: %TRUE on success, %FALSE on failure.
7625 gboolean
7626 g_file_replace_contents_finish (GFile *file,
7627 GAsyncResult *res,
7628 char **new_etag,
7629 GError **error)
7631 GTask *task;
7632 ReplaceContentsData *data;
7634 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7635 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7637 task = G_TASK (res);
7639 if (!g_task_propagate_boolean (task, error))
7640 return FALSE;
7642 data = g_task_get_task_data (task);
7644 if (new_etag)
7646 *new_etag = data->etag;
7647 data->etag = NULL; /* Take ownership */
7650 return TRUE;
7653 gboolean
7654 g_file_real_measure_disk_usage (GFile *file,
7655 GFileMeasureFlags flags,
7656 GCancellable *cancellable,
7657 GFileMeasureProgressCallback progress_callback,
7658 gpointer progress_data,
7659 guint64 *disk_usage,
7660 guint64 *num_dirs,
7661 guint64 *num_files,
7662 GError **error)
7664 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7665 "Operation not supported for the current backend.");
7666 return FALSE;
7669 typedef struct
7671 GFileMeasureFlags flags;
7672 GFileMeasureProgressCallback progress_callback;
7673 gpointer progress_data;
7674 } MeasureTaskData;
7676 typedef struct
7678 guint64 disk_usage;
7679 guint64 num_dirs;
7680 guint64 num_files;
7681 } MeasureResult;
7683 typedef struct
7685 GFileMeasureProgressCallback callback;
7686 gpointer user_data;
7687 gboolean reporting;
7688 guint64 current_size;
7689 guint64 num_dirs;
7690 guint64 num_files;
7691 } MeasureProgress;
7693 static gboolean
7694 measure_disk_usage_invoke_progress (gpointer user_data)
7696 MeasureProgress *progress = user_data;
7698 (* progress->callback) (progress->reporting,
7699 progress->current_size, progress->num_dirs, progress->num_files,
7700 progress->user_data);
7702 return FALSE;
7705 static void
7706 measure_disk_usage_progress (gboolean reporting,
7707 guint64 current_size,
7708 guint64 num_dirs,
7709 guint64 num_files,
7710 gpointer user_data)
7712 MeasureProgress progress;
7713 GTask *task = user_data;
7714 MeasureTaskData *data;
7716 data = g_task_get_task_data (task);
7718 progress.callback = data->progress_callback;
7719 progress.user_data = data->progress_data;
7720 progress.reporting = reporting;
7721 progress.current_size = current_size;
7722 progress.num_dirs = num_dirs;
7723 progress.num_files = num_files;
7725 g_main_context_invoke_full (g_task_get_context (task),
7726 g_task_get_priority (task),
7727 measure_disk_usage_invoke_progress,
7728 g_memdup (&progress, sizeof progress),
7729 g_free);
7732 static void
7733 measure_disk_usage_thread (GTask *task,
7734 gpointer source_object,
7735 gpointer task_data,
7736 GCancellable *cancellable)
7738 MeasureTaskData *data = task_data;
7739 GError *error = NULL;
7740 MeasureResult result = { 0, };
7742 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7743 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7744 &result.disk_usage, &result.num_dirs, &result.num_files,
7745 &error))
7746 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7747 else
7748 g_task_return_error (task, error);
7751 static void
7752 g_file_real_measure_disk_usage_async (GFile *file,
7753 GFileMeasureFlags flags,
7754 gint io_priority,
7755 GCancellable *cancellable,
7756 GFileMeasureProgressCallback progress_callback,
7757 gpointer progress_data,
7758 GAsyncReadyCallback callback,
7759 gpointer user_data)
7761 MeasureTaskData data;
7762 GTask *task;
7764 data.flags = flags;
7765 data.progress_callback = progress_callback;
7766 data.progress_data = progress_data;
7768 task = g_task_new (file, cancellable, callback, user_data);
7769 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7770 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7771 g_task_set_priority (task, io_priority);
7773 g_task_run_in_thread (task, measure_disk_usage_thread);
7774 g_object_unref (task);
7777 static gboolean
7778 g_file_real_measure_disk_usage_finish (GFile *file,
7779 GAsyncResult *result,
7780 guint64 *disk_usage,
7781 guint64 *num_dirs,
7782 guint64 *num_files,
7783 GError **error)
7785 MeasureResult *measure_result;
7787 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7789 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7791 if (measure_result == NULL)
7792 return FALSE;
7794 if (disk_usage)
7795 *disk_usage = measure_result->disk_usage;
7797 if (num_dirs)
7798 *num_dirs = measure_result->num_dirs;
7800 if (num_files)
7801 *num_files = measure_result->num_files;
7803 g_free (measure_result);
7805 return TRUE;
7809 * g_file_measure_disk_usage:
7810 * @file: a #GFile
7811 * @flags: #GFileMeasureFlags
7812 * @cancellable: (nullable): optional #GCancellable
7813 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7814 * @progress_data: user_data for @progress_callback
7815 * @disk_usage: (out) (optional): the number of bytes of disk space used
7816 * @num_dirs: (out) (optional): the number of directories encountered
7817 * @num_files: (out) (optional): the number of non-directories encountered
7818 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7820 * Recursively measures the disk usage of @file.
7822 * This is essentially an analog of the 'du' command, but it also
7823 * reports the number of directories and non-directory files encountered
7824 * (including things like symbolic links).
7826 * By default, errors are only reported against the toplevel file
7827 * itself. Errors found while recursing are silently ignored, unless
7828 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7830 * The returned size, @disk_usage, is in bytes and should be formatted
7831 * with g_format_size() in order to get something reasonable for showing
7832 * in a user interface.
7834 * @progress_callback and @progress_data can be given to request
7835 * periodic progress updates while scanning. See the documentation for
7836 * #GFileMeasureProgressCallback for information about when and how the
7837 * callback will be invoked.
7839 * Returns: %TRUE if successful, with the out parameters set.
7840 * %FALSE otherwise, with @error set.
7842 * Since: 2.38
7844 gboolean
7845 g_file_measure_disk_usage (GFile *file,
7846 GFileMeasureFlags flags,
7847 GCancellable *cancellable,
7848 GFileMeasureProgressCallback progress_callback,
7849 gpointer progress_data,
7850 guint64 *disk_usage,
7851 guint64 *num_dirs,
7852 guint64 *num_files,
7853 GError **error)
7855 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7856 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7857 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7859 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7860 progress_callback, progress_data,
7861 disk_usage, num_dirs, num_files,
7862 error);
7866 * g_file_measure_disk_usage_async:
7867 * @file: a #GFile
7868 * @flags: #GFileMeasureFlags
7869 * @io_priority: the [I/O priority][io-priority] of the request
7870 * @cancellable: (nullable): optional #GCancellable
7871 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7872 * @progress_data: user_data for @progress_callback
7873 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7874 * @user_data: the data to pass to callback function
7876 * Recursively measures the disk usage of @file.
7878 * This is the asynchronous version of g_file_measure_disk_usage(). See
7879 * there for more information.
7881 * Since: 2.38
7883 void
7884 g_file_measure_disk_usage_async (GFile *file,
7885 GFileMeasureFlags flags,
7886 gint io_priority,
7887 GCancellable *cancellable,
7888 GFileMeasureProgressCallback progress_callback,
7889 gpointer progress_data,
7890 GAsyncReadyCallback callback,
7891 gpointer user_data)
7893 g_return_if_fail (G_IS_FILE (file));
7894 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7896 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7897 progress_callback, progress_data,
7898 callback, user_data);
7902 * g_file_measure_disk_usage_finish:
7903 * @file: a #GFile
7904 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7905 * @disk_usage: (out) (optional): the number of bytes of disk space used
7906 * @num_dirs: (out) (optional): the number of directories encountered
7907 * @num_files: (out) (optional): the number of non-directories encountered
7908 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7910 * Collects the results from an earlier call to
7911 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7912 * more information.
7914 * Returns: %TRUE if successful, with the out parameters set.
7915 * %FALSE otherwise, with @error set.
7917 * Since: 2.38
7919 gboolean
7920 g_file_measure_disk_usage_finish (GFile *file,
7921 GAsyncResult *result,
7922 guint64 *disk_usage,
7923 guint64 *num_dirs,
7924 guint64 *num_files,
7925 GError **error)
7927 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7928 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7930 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7934 * g_file_start_mountable:
7935 * @file: input #GFile
7936 * @flags: flags affecting the operation
7937 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7938 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7939 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7940 * @user_data: the data to pass to callback function
7942 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7943 * Using @start_operation, you can request callbacks when, for instance,
7944 * passwords are needed during authentication.
7946 * If @cancellable is not %NULL, then the operation can be cancelled by
7947 * triggering the cancellable object from another thread. If the operation
7948 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7950 * When the operation is finished, @callback will be called.
7951 * You can then call g_file_mount_mountable_finish() to get
7952 * the result of the operation.
7954 * Since: 2.22
7956 void
7957 g_file_start_mountable (GFile *file,
7958 GDriveStartFlags flags,
7959 GMountOperation *start_operation,
7960 GCancellable *cancellable,
7961 GAsyncReadyCallback callback,
7962 gpointer user_data)
7964 GFileIface *iface;
7966 g_return_if_fail (G_IS_FILE (file));
7968 iface = G_FILE_GET_IFACE (file);
7970 if (iface->start_mountable == NULL)
7972 g_task_report_new_error (file, callback, user_data,
7973 g_file_start_mountable,
7974 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7975 _("Operation not supported"));
7976 return;
7979 (* iface->start_mountable) (file,
7980 flags,
7981 start_operation,
7982 cancellable,
7983 callback,
7984 user_data);
7988 * g_file_start_mountable_finish:
7989 * @file: input #GFile
7990 * @result: a #GAsyncResult
7991 * @error: a #GError, or %NULL
7993 * Finishes a start operation. See g_file_start_mountable() for details.
7995 * Finish an asynchronous start operation that was started
7996 * with g_file_start_mountable().
7998 * Returns: %TRUE if the operation finished successfully. %FALSE
7999 * otherwise.
8001 * Since: 2.22
8003 gboolean
8004 g_file_start_mountable_finish (GFile *file,
8005 GAsyncResult *result,
8006 GError **error)
8008 GFileIface *iface;
8010 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8011 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8013 if (g_async_result_legacy_propagate_error (result, error))
8014 return FALSE;
8015 else if (g_async_result_is_tagged (result, g_file_start_mountable))
8016 return g_task_propagate_boolean (G_TASK (result), error);
8018 iface = G_FILE_GET_IFACE (file);
8019 return (* iface->start_mountable_finish) (file, result, error);
8023 * g_file_stop_mountable:
8024 * @file: input #GFile
8025 * @flags: flags affecting the operation
8026 * @mount_operation: (nullable): a #GMountOperation,
8027 * or %NULL to avoid user interaction.
8028 * @cancellable: (nullable): optional #GCancellable object,
8029 * %NULL to ignore
8030 * @callback: (nullable): a #GAsyncReadyCallback to call
8031 * when the request is satisfied, or %NULL
8032 * @user_data: the data to pass to callback function
8034 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8036 * If @cancellable is not %NULL, then the operation can be cancelled by
8037 * triggering the cancellable object from another thread. If the operation
8038 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8040 * When the operation is finished, @callback will be called.
8041 * You can then call g_file_stop_mountable_finish() to get
8042 * the result of the operation.
8044 * Since: 2.22
8046 void
8047 g_file_stop_mountable (GFile *file,
8048 GMountUnmountFlags flags,
8049 GMountOperation *mount_operation,
8050 GCancellable *cancellable,
8051 GAsyncReadyCallback callback,
8052 gpointer user_data)
8054 GFileIface *iface;
8056 g_return_if_fail (G_IS_FILE (file));
8058 iface = G_FILE_GET_IFACE (file);
8060 if (iface->stop_mountable == NULL)
8062 g_task_report_new_error (file, callback, user_data,
8063 g_file_stop_mountable,
8064 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8065 _("Operation not supported"));
8066 return;
8069 (* iface->stop_mountable) (file,
8070 flags,
8071 mount_operation,
8072 cancellable,
8073 callback,
8074 user_data);
8078 * g_file_stop_mountable_finish:
8079 * @file: input #GFile
8080 * @result: a #GAsyncResult
8081 * @error: a #GError, or %NULL
8083 * Finishes an stop operation, see g_file_stop_mountable() for details.
8085 * Finish an asynchronous stop operation that was started
8086 * with g_file_stop_mountable().
8088 * Returns: %TRUE if the operation finished successfully.
8089 * %FALSE otherwise.
8091 * Since: 2.22
8093 gboolean
8094 g_file_stop_mountable_finish (GFile *file,
8095 GAsyncResult *result,
8096 GError **error)
8098 GFileIface *iface;
8100 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8101 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8103 if (g_async_result_legacy_propagate_error (result, error))
8104 return FALSE;
8105 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8106 return g_task_propagate_boolean (G_TASK (result), error);
8108 iface = G_FILE_GET_IFACE (file);
8109 return (* iface->stop_mountable_finish) (file, result, error);
8113 * g_file_poll_mountable:
8114 * @file: input #GFile
8115 * @cancellable: optional #GCancellable object, %NULL to ignore
8116 * @callback: (nullable): a #GAsyncReadyCallback to call
8117 * when the request is satisfied, or %NULL
8118 * @user_data: the data to pass to callback function
8120 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8122 * If @cancellable is not %NULL, then the operation can be cancelled by
8123 * triggering the cancellable object from another thread. If the operation
8124 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8126 * When the operation is finished, @callback will be called.
8127 * You can then call g_file_mount_mountable_finish() to get
8128 * the result of the operation.
8130 * Since: 2.22
8132 void
8133 g_file_poll_mountable (GFile *file,
8134 GCancellable *cancellable,
8135 GAsyncReadyCallback callback,
8136 gpointer user_data)
8138 GFileIface *iface;
8140 g_return_if_fail (G_IS_FILE (file));
8142 iface = G_FILE_GET_IFACE (file);
8144 if (iface->poll_mountable == NULL)
8146 g_task_report_new_error (file, callback, user_data,
8147 g_file_poll_mountable,
8148 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8149 _("Operation not supported"));
8150 return;
8153 (* iface->poll_mountable) (file,
8154 cancellable,
8155 callback,
8156 user_data);
8160 * g_file_poll_mountable_finish:
8161 * @file: input #GFile
8162 * @result: a #GAsyncResult
8163 * @error: a #GError, or %NULL
8165 * Finishes a poll operation. See g_file_poll_mountable() for details.
8167 * Finish an asynchronous poll operation that was polled
8168 * with g_file_poll_mountable().
8170 * Returns: %TRUE if the operation finished successfully. %FALSE
8171 * otherwise.
8173 * Since: 2.22
8175 gboolean
8176 g_file_poll_mountable_finish (GFile *file,
8177 GAsyncResult *result,
8178 GError **error)
8180 GFileIface *iface;
8182 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8183 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8185 if (g_async_result_legacy_propagate_error (result, error))
8186 return FALSE;
8187 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8188 return g_task_propagate_boolean (G_TASK (result), error);
8190 iface = G_FILE_GET_IFACE (file);
8191 return (* iface->poll_mountable_finish) (file, result, error);
8195 * g_file_supports_thread_contexts:
8196 * @file: a #GFile
8198 * Checks if @file supports
8199 * [thread-default contexts][g-main-context-push-thread-default-context].
8200 * If this returns %FALSE, you cannot perform asynchronous operations on
8201 * @file in a thread that has a thread-default context.
8203 * Returns: Whether or not @file supports thread-default contexts.
8205 * Since: 2.22
8207 gboolean
8208 g_file_supports_thread_contexts (GFile *file)
8210 GFileIface *iface;
8212 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8214 iface = G_FILE_GET_IFACE (file);
8215 return iface->supports_thread_contexts;
8219 * g_file_load_bytes:
8220 * @file: a #GFile
8221 * @cancellable: (nullable): a #GCancellable or %NULL
8222 * @etag_out: (out) (nullable) (optional): a location to place the current
8223 * entity tag for the file, or %NULL if the entity tag is not needed
8224 * @error: a location for a #GError or %NULL
8226 * Loads the contents of @file and returns it as #GBytes.
8228 * If @file is a resource:// based URI, the resulting bytes will reference the
8229 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8230 * g_file_load_contents() and g_bytes_new_take().
8232 * For resources, @etag_out will be set to %NULL.
8234 * The data contained in the resulting #GBytes is always zero-terminated, but
8235 * this is not included in the #GBytes length. The resulting #GBytes should be
8236 * freed with g_bytes_unref() when no longer in use.
8238 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8240 * Since: 2.56
8242 GBytes *
8243 g_file_load_bytes (GFile *file,
8244 GCancellable *cancellable,
8245 gchar **etag_out,
8246 GError **error)
8248 gchar *contents;
8249 gsize len;
8251 g_return_val_if_fail (G_IS_FILE (file), NULL);
8252 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8253 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8255 if (etag_out != NULL)
8256 *etag_out = NULL;
8258 if (g_file_has_uri_scheme (file, "resource"))
8260 GBytes *bytes;
8261 gchar *uri, *unescaped;
8263 uri = g_file_get_uri (file);
8264 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8265 g_free (uri);
8267 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8268 g_free (unescaped);
8270 return bytes;
8273 /* contents is guaranteed to be \0 terminated */
8274 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8275 return g_bytes_new_take (g_steal_pointer (&contents), len);
8277 return NULL;
8280 static void
8281 g_file_load_bytes_cb (GObject *object,
8282 GAsyncResult *result,
8283 gpointer user_data)
8285 GFile *file = G_FILE (object);
8286 GTask *task = user_data;
8287 GError *error = NULL;
8288 gchar *etag = NULL;
8289 gchar *contents = NULL;
8290 gsize len = 0;
8292 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8293 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8295 if (error != NULL)
8296 g_task_return_error (task, g_steal_pointer (&error));
8297 else
8298 g_task_return_pointer (task,
8299 g_bytes_new_take (g_steal_pointer (&contents), len),
8300 (GDestroyNotify)g_bytes_unref);
8302 g_object_unref (task);
8306 * g_file_load_bytes_async:
8307 * @file: a #GFile
8308 * @cancellable: (nullable): a #GCancellable or %NULL
8309 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8310 * request is satisfied
8311 * @user_data: (closure): the data to pass to callback function
8313 * Asynchronously loads the contents of @file as #GBytes.
8315 * If @file is a resource:// based URI, the resulting bytes will reference the
8316 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8317 * g_file_load_contents_async() and g_bytes_new_take().
8319 * @callback should call g_file_load_bytes_finish() to get the result of this
8320 * asynchronous operation.
8322 * See g_file_load_bytes() for more information.
8324 * Since: 2.56
8326 void
8327 g_file_load_bytes_async (GFile *file,
8328 GCancellable *cancellable,
8329 GAsyncReadyCallback callback,
8330 gpointer user_data)
8332 GError *error = NULL;
8333 GBytes *bytes;
8334 GTask *task;
8336 g_return_if_fail (G_IS_FILE (file));
8337 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8339 task = g_task_new (file, cancellable, callback, user_data);
8340 g_task_set_source_tag (task, g_file_load_bytes_async);
8342 if (!g_file_has_uri_scheme (file, "resource"))
8344 g_file_load_contents_async (file,
8345 cancellable,
8346 g_file_load_bytes_cb,
8347 g_steal_pointer (&task));
8348 return;
8351 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8353 if (bytes == NULL)
8354 g_task_return_error (task, g_steal_pointer (&error));
8355 else
8356 g_task_return_pointer (task,
8357 g_steal_pointer (&bytes),
8358 (GDestroyNotify)g_bytes_unref);
8360 g_object_unref (task);
8364 * g_file_load_bytes_finish:
8365 * @file: a #GFile
8366 * @result: a #GAsyncResult provided to the callback
8367 * @etag_out: (out) (nullable) (optional): a location to place the current
8368 * entity tag for the file, or %NULL if the entity tag is not needed
8369 * @error: a location for a #GError, or %NULL
8371 * Completes an asynchronous request to g_file_load_bytes_async().
8373 * For resources, @etag_out will be set to %NULL.
8375 * The data contained in the resulting #GBytes is always zero-terminated, but
8376 * this is not included in the #GBytes length. The resulting #GBytes should be
8377 * freed with g_bytes_unref() when no longer in use.
8379 * See g_file_load_bytes() for more information.
8381 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8383 * Since: 2.56
8385 GBytes *
8386 g_file_load_bytes_finish (GFile *file,
8387 GAsyncResult *result,
8388 gchar **etag_out,
8389 GError **error)
8391 GBytes *bytes;
8393 g_return_val_if_fail (G_IS_FILE (file), NULL);
8394 g_return_val_if_fail (G_IS_TASK (result), NULL);
8395 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8396 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8398 bytes = g_task_propagate_pointer (G_TASK (result), error);
8400 if (etag_out != NULL)
8401 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));
8403 return bytes;