Merge branch '896-variant-type-docs' into 'master'
[glib.git] / gio / gfile.c
bloba5709a4cc8074e4597fd287270bfacac6ad81731
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 is one expressed in the platform-native filename format,
396 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397 * as it might be on a locally mounted remote filesystem.
399 * On some systems non-native files may be available using the native
400 * filesystem via a userspace filesystem (FUSE), in these cases this call
401 * will return %FALSE, but g_file_get_path() will still return a native path.
403 * This call does no blocking I/O.
405 * Returns: %TRUE if @file is native
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 * g_file_dup() is useful when a second handle is needed to the same underlying
706 * file, for use in a separate thread (#GFile is not thread-safe). For use
707 * within the same thread, use g_object_ref() to increment the existing object’s
708 * reference count.
710 * This call does no blocking I/O.
712 * Returns: (transfer full): a new #GFile that is a duplicate
713 * of the given #GFile.
715 GFile *
716 g_file_dup (GFile *file)
718 GFileIface *iface;
720 g_return_val_if_fail (G_IS_FILE (file), NULL);
722 iface = G_FILE_GET_IFACE (file);
724 return (* iface->dup) (file);
728 * g_file_hash:
729 * @file: (type GFile): #gconstpointer to a #GFile
731 * Creates a hash value for a #GFile.
733 * This call does no blocking I/O.
735 * Virtual: hash
736 * Returns: 0 if @file is not a valid #GFile, otherwise an
737 * integer that can be used as hash value for the #GFile.
738 * This function is intended for easily hashing a #GFile to
739 * add to a #GHashTable or similar data structure.
741 guint
742 g_file_hash (gconstpointer file)
744 GFileIface *iface;
746 g_return_val_if_fail (G_IS_FILE (file), 0);
748 iface = G_FILE_GET_IFACE (file);
750 return (* iface->hash) ((GFile *)file);
754 * g_file_equal:
755 * @file1: the first #GFile
756 * @file2: the second #GFile
758 * Checks if the two given #GFiles refer to the same file.
760 * Note that two #GFiles that differ can still refer to the same
761 * file on the filesystem due to various forms of filename
762 * aliasing.
764 * This call does no blocking I/O.
766 * Returns: %TRUE if @file1 and @file2 are equal.
768 gboolean
769 g_file_equal (GFile *file1,
770 GFile *file2)
772 GFileIface *iface;
774 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
775 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
777 if (file1 == file2)
778 return TRUE;
780 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
781 return FALSE;
783 iface = G_FILE_GET_IFACE (file1);
785 return (* iface->equal) (file1, file2);
790 * g_file_get_parent:
791 * @file: input #GFile
793 * Gets the parent directory for the @file.
794 * If the @file represents the root directory of the
795 * file system, then %NULL will be returned.
797 * This call does no blocking I/O.
799 * Returns: (nullable) (transfer full): a #GFile structure to the
800 * parent of the given #GFile or %NULL if there is no parent. Free
801 * the returned object with g_object_unref().
803 GFile *
804 g_file_get_parent (GFile *file)
806 GFileIface *iface;
808 g_return_val_if_fail (G_IS_FILE (file), NULL);
810 iface = G_FILE_GET_IFACE (file);
812 return (* iface->get_parent) (file);
816 * g_file_has_parent:
817 * @file: input #GFile
818 * @parent: (nullable): the parent to check for, or %NULL
820 * Checks if @file has a parent, and optionally, if it is @parent.
822 * If @parent is %NULL then this function returns %TRUE if @file has any
823 * parent at all. If @parent is non-%NULL then %TRUE is only returned
824 * if @file is an immediate child of @parent.
826 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
827 * the case that @parent is %NULL).
829 * Since: 2.24
831 gboolean
832 g_file_has_parent (GFile *file,
833 GFile *parent)
835 GFile *actual_parent;
836 gboolean result;
838 g_return_val_if_fail (G_IS_FILE (file), FALSE);
839 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
841 actual_parent = g_file_get_parent (file);
843 if (actual_parent != NULL)
845 if (parent != NULL)
846 result = g_file_equal (parent, actual_parent);
847 else
848 result = TRUE;
850 g_object_unref (actual_parent);
852 else
853 result = FALSE;
855 return result;
859 * g_file_get_child:
860 * @file: input #GFile
861 * @name: (type filename): string containing the child's basename
863 * Gets a child of @file with basename equal to @name.
865 * Note that the file with that specific name might not exist, but
866 * you can still have a #GFile that points to it. You can use this
867 * for instance to create that file.
869 * This call does no blocking I/O.
871 * Returns: (transfer full): a #GFile to a child specified by @name.
872 * Free the returned object with g_object_unref().
874 GFile *
875 g_file_get_child (GFile *file,
876 const char *name)
878 g_return_val_if_fail (G_IS_FILE (file), NULL);
879 g_return_val_if_fail (name != NULL, NULL);
881 return g_file_resolve_relative_path (file, name);
885 * g_file_get_child_for_display_name:
886 * @file: input #GFile
887 * @display_name: string to a possible child
888 * @error: return location for an error
890 * Gets the child of @file for a given @display_name (i.e. a UTF-8
891 * version of the name). If this function fails, it returns %NULL
892 * and @error will be set. This is very useful when constructing a
893 * #GFile for a new file and the user entered the filename in the
894 * user interface, for instance when you select a directory and
895 * type a filename in the file selector.
897 * This call does no blocking I/O.
899 * Returns: (transfer full): a #GFile to the specified child, or
900 * %NULL if the display name couldn't be converted.
901 * Free the returned object with g_object_unref().
903 GFile *
904 g_file_get_child_for_display_name (GFile *file,
905 const char *display_name,
906 GError **error)
908 GFileIface *iface;
910 g_return_val_if_fail (G_IS_FILE (file), NULL);
911 g_return_val_if_fail (display_name != NULL, NULL);
913 iface = G_FILE_GET_IFACE (file);
915 return (* iface->get_child_for_display_name) (file, display_name, error);
919 * g_file_has_prefix:
920 * @file: input #GFile
921 * @prefix: input #GFile
923 * Checks whether @file has the prefix specified by @prefix.
925 * In other words, if the names of initial elements of @file's
926 * pathname match @prefix. Only full pathname elements are matched,
927 * so a path like /foo is not considered a prefix of /foobar, only
928 * of /foo/bar.
930 * A #GFile is not a prefix of itself. If you want to check for
931 * equality, use g_file_equal().
933 * This call does no I/O, as it works purely on names. As such it can
934 * sometimes return %FALSE even if @file is inside a @prefix (from a
935 * filesystem point of view), because the prefix of @file is an alias
936 * of @prefix.
938 * Virtual: prefix_matches
939 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
940 * %FALSE otherwise.
942 gboolean
943 g_file_has_prefix (GFile *file,
944 GFile *prefix)
946 GFileIface *iface;
948 g_return_val_if_fail (G_IS_FILE (file), FALSE);
949 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
951 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
952 return FALSE;
954 iface = G_FILE_GET_IFACE (file);
956 /* The vtable function differs in arg order since
957 * we're using the old contains_file call
959 return (* iface->prefix_matches) (prefix, file);
963 * g_file_get_relative_path:
964 * @parent: input #GFile
965 * @descendant: input #GFile
967 * Gets the path for @descendant relative to @parent.
969 * This call does no blocking I/O.
971 * Returns: (type filename) (nullable): string with the relative path from
972 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
973 * prefix. The returned string should be freed with g_free() when
974 * no longer needed.
976 char *
977 g_file_get_relative_path (GFile *parent,
978 GFile *descendant)
980 GFileIface *iface;
982 g_return_val_if_fail (G_IS_FILE (parent), NULL);
983 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
985 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
986 return NULL;
988 iface = G_FILE_GET_IFACE (parent);
990 return (* iface->get_relative_path) (parent, descendant);
994 * g_file_resolve_relative_path:
995 * @file: input #GFile
996 * @relative_path: (type filename): a given relative path string
998 * Resolves a relative path for @file to an absolute path.
1000 * This call does no blocking I/O.
1002 * Returns: (transfer full): #GFile to the resolved path.
1003 * %NULL if @relative_path is %NULL or if @file is invalid.
1004 * Free the returned object with g_object_unref().
1006 GFile *
1007 g_file_resolve_relative_path (GFile *file,
1008 const char *relative_path)
1010 GFileIface *iface;
1012 g_return_val_if_fail (G_IS_FILE (file), NULL);
1013 g_return_val_if_fail (relative_path != NULL, NULL);
1015 iface = G_FILE_GET_IFACE (file);
1017 return (* iface->resolve_relative_path) (file, relative_path);
1021 * g_file_enumerate_children:
1022 * @file: input #GFile
1023 * @attributes: an attribute query string
1024 * @flags: a set of #GFileQueryInfoFlags
1025 * @cancellable: (nullable): optional #GCancellable object,
1026 * %NULL to ignore
1027 * @error: #GError for error reporting
1029 * Gets the requested information about the files in a directory.
1030 * The result is a #GFileEnumerator object that will give out
1031 * #GFileInfo objects for all the files in the directory.
1033 * The @attributes value is a string that specifies the file
1034 * attributes that should be gathered. It is not an error if
1035 * it's not possible to read a particular requested attribute
1036 * from a file - it just won't be set. @attributes should
1037 * be a comma-separated list of attributes or attribute wildcards.
1038 * The wildcard "*" means all attributes, and a wildcard like
1039 * "standard::*" means all attributes in the standard namespace.
1040 * An example attribute query be "standard::*,owner::user".
1041 * The standard attributes are available as defines, like
1042 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1044 * If @cancellable is not %NULL, then the operation can be cancelled
1045 * by triggering the cancellable object from another thread. If the
1046 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1047 * returned.
1049 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1050 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1051 * error will be returned. Other errors are possible too.
1053 * Returns: (transfer full): A #GFileEnumerator if successful,
1054 * %NULL on error. Free the returned object with g_object_unref().
1056 GFileEnumerator *
1057 g_file_enumerate_children (GFile *file,
1058 const char *attributes,
1059 GFileQueryInfoFlags flags,
1060 GCancellable *cancellable,
1061 GError **error)
1063 GFileIface *iface;
1065 g_return_val_if_fail (G_IS_FILE (file), NULL);
1067 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1068 return NULL;
1070 iface = G_FILE_GET_IFACE (file);
1072 if (iface->enumerate_children == NULL)
1074 g_set_error_literal (error, G_IO_ERROR,
1075 G_IO_ERROR_NOT_SUPPORTED,
1076 _("Operation not supported"));
1077 return NULL;
1080 return (* iface->enumerate_children) (file, attributes, flags,
1081 cancellable, error);
1085 * g_file_enumerate_children_async:
1086 * @file: input #GFile
1087 * @attributes: an attribute query string
1088 * @flags: a set of #GFileQueryInfoFlags
1089 * @io_priority: the [I/O priority][io-priority] of the request
1090 * @cancellable: (nullable): optional #GCancellable object,
1091 * %NULL to ignore
1092 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1093 * request is satisfied
1094 * @user_data: (closure): the data to pass to callback function
1096 * Asynchronously gets the requested information about the files
1097 * in a directory. The result is a #GFileEnumerator object that will
1098 * give out #GFileInfo objects for all the files in the directory.
1100 * For more details, see g_file_enumerate_children() which is
1101 * the synchronous version of this call.
1103 * When the operation is finished, @callback will be called. You can
1104 * then call g_file_enumerate_children_finish() to get the result of
1105 * the operation.
1107 void
1108 g_file_enumerate_children_async (GFile *file,
1109 const char *attributes,
1110 GFileQueryInfoFlags flags,
1111 int io_priority,
1112 GCancellable *cancellable,
1113 GAsyncReadyCallback callback,
1114 gpointer user_data)
1116 GFileIface *iface;
1118 g_return_if_fail (G_IS_FILE (file));
1120 iface = G_FILE_GET_IFACE (file);
1121 (* iface->enumerate_children_async) (file,
1122 attributes,
1123 flags,
1124 io_priority,
1125 cancellable,
1126 callback,
1127 user_data);
1131 * g_file_enumerate_children_finish:
1132 * @file: input #GFile
1133 * @res: a #GAsyncResult
1134 * @error: a #GError
1136 * Finishes an async enumerate children operation.
1137 * See g_file_enumerate_children_async().
1139 * Returns: (transfer full): a #GFileEnumerator or %NULL
1140 * if an error occurred.
1141 * Free the returned object with g_object_unref().
1143 GFileEnumerator *
1144 g_file_enumerate_children_finish (GFile *file,
1145 GAsyncResult *res,
1146 GError **error)
1148 GFileIface *iface;
1150 g_return_val_if_fail (G_IS_FILE (file), NULL);
1151 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1153 if (g_async_result_legacy_propagate_error (res, error))
1154 return NULL;
1156 iface = G_FILE_GET_IFACE (file);
1157 return (* iface->enumerate_children_finish) (file, res, error);
1161 * g_file_query_exists:
1162 * @file: input #GFile
1163 * @cancellable: (nullable): optional #GCancellable object,
1164 * %NULL to ignore
1166 * Utility function to check if a particular file exists. This is
1167 * implemented using g_file_query_info() and as such does blocking I/O.
1169 * 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)
1170 * and then execute something based on the outcome of that, because the
1171 * file might have been created or removed in between the operations. The
1172 * general approach to handling that is to not check, but just do the
1173 * operation and handle the errors as they come.
1175 * As an example of race-free checking, take the case of reading a file,
1176 * and if it doesn't exist, creating it. There are two racy versions: read
1177 * it, and on error create it; and: check if it exists, if not create it.
1178 * These can both result in two processes creating the file (with perhaps
1179 * a partially written file as the result). The correct approach is to
1180 * always try to create the file with g_file_create() which will either
1181 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1183 * However, in many cases an existence check is useful in a user interface,
1184 * for instance to make a menu item sensitive/insensitive, so that you don't
1185 * have to fool users that something is possible and then just show an error
1186 * dialog. If you do this, you should make sure to also handle the errors
1187 * that can happen due to races when you execute the operation.
1189 * Returns: %TRUE if the file exists (and can be detected without error),
1190 * %FALSE otherwise (or if cancelled).
1192 gboolean
1193 g_file_query_exists (GFile *file,
1194 GCancellable *cancellable)
1196 GFileInfo *info;
1198 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1200 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1201 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1202 if (info != NULL)
1204 g_object_unref (info);
1205 return TRUE;
1208 return FALSE;
1212 * g_file_query_file_type:
1213 * @file: input #GFile
1214 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1215 * @cancellable: (nullable): optional #GCancellable object,
1216 * %NULL to ignore
1218 * Utility function to inspect the #GFileType of a file. This is
1219 * implemented using g_file_query_info() and as such does blocking I/O.
1221 * The primary use case of this method is to check if a file is
1222 * a regular file, directory, or symlink.
1224 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1225 * if the file does not exist
1227 * Since: 2.18
1229 GFileType
1230 g_file_query_file_type (GFile *file,
1231 GFileQueryInfoFlags flags,
1232 GCancellable *cancellable)
1234 GFileInfo *info;
1235 GFileType file_type;
1237 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1238 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1239 cancellable, NULL);
1240 if (info != NULL)
1242 file_type = g_file_info_get_file_type (info);
1243 g_object_unref (info);
1245 else
1246 file_type = G_FILE_TYPE_UNKNOWN;
1248 return file_type;
1252 * g_file_query_info:
1253 * @file: input #GFile
1254 * @attributes: an attribute query string
1255 * @flags: a set of #GFileQueryInfoFlags
1256 * @cancellable: (nullable): optional #GCancellable object,
1257 * %NULL to ignore
1258 * @error: a #GError
1260 * Gets the requested information about specified @file.
1261 * The result is a #GFileInfo object that contains key-value
1262 * attributes (such as the type or size of the file).
1264 * The @attributes value is a string that specifies the file
1265 * attributes that should be gathered. It is not an error if
1266 * it's not possible to read a particular requested attribute
1267 * from a file - it just won't be set. @attributes should be a
1268 * comma-separated list of attributes or attribute wildcards.
1269 * The wildcard "*" means all attributes, and a wildcard like
1270 * "standard::*" means all attributes in the standard namespace.
1271 * An example attribute query be "standard::*,owner::user".
1272 * The standard attributes are available as defines, like
1273 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1275 * If @cancellable is not %NULL, then the operation can be cancelled
1276 * by triggering the cancellable object from another thread. If the
1277 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1278 * returned.
1280 * For symlinks, normally the information about the target of the
1281 * symlink is returned, rather than information about the symlink
1282 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1283 * in @flags the information about the symlink itself will be returned.
1284 * Also, for symlinks that point to non-existing files the information
1285 * about the symlink itself will be returned.
1287 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1288 * returned. Other errors are possible too, and depend on what kind of
1289 * filesystem the file is on.
1291 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1292 * on error. Free the returned object with g_object_unref().
1294 GFileInfo *
1295 g_file_query_info (GFile *file,
1296 const char *attributes,
1297 GFileQueryInfoFlags flags,
1298 GCancellable *cancellable,
1299 GError **error)
1301 GFileIface *iface;
1303 g_return_val_if_fail (G_IS_FILE (file), NULL);
1305 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1306 return NULL;
1308 iface = G_FILE_GET_IFACE (file);
1310 if (iface->query_info == NULL)
1312 g_set_error_literal (error, G_IO_ERROR,
1313 G_IO_ERROR_NOT_SUPPORTED,
1314 _("Operation not supported"));
1315 return NULL;
1318 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1322 * g_file_query_info_async:
1323 * @file: input #GFile
1324 * @attributes: an attribute query string
1325 * @flags: a set of #GFileQueryInfoFlags
1326 * @io_priority: the [I/O priority][io-priority] of the request
1327 * @cancellable: (nullable): optional #GCancellable object,
1328 * %NULL to ignore
1329 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1330 * request is satisfied
1331 * @user_data: (closure): the data to pass to callback function
1333 * Asynchronously gets the requested information about specified @file.
1334 * The result is a #GFileInfo object that contains key-value attributes
1335 * (such as type or size for the file).
1337 * For more details, see g_file_query_info() which is the synchronous
1338 * version of this call.
1340 * When the operation is finished, @callback will be called. You can
1341 * then call g_file_query_info_finish() to get the result of the operation.
1343 void
1344 g_file_query_info_async (GFile *file,
1345 const char *attributes,
1346 GFileQueryInfoFlags flags,
1347 int io_priority,
1348 GCancellable *cancellable,
1349 GAsyncReadyCallback callback,
1350 gpointer user_data)
1352 GFileIface *iface;
1354 g_return_if_fail (G_IS_FILE (file));
1356 iface = G_FILE_GET_IFACE (file);
1357 (* iface->query_info_async) (file,
1358 attributes,
1359 flags,
1360 io_priority,
1361 cancellable,
1362 callback,
1363 user_data);
1367 * g_file_query_info_finish:
1368 * @file: input #GFile
1369 * @res: a #GAsyncResult
1370 * @error: a #GError
1372 * Finishes an asynchronous file info query.
1373 * See g_file_query_info_async().
1375 * Returns: (transfer full): #GFileInfo for given @file
1376 * or %NULL on error. Free the returned object with
1377 * g_object_unref().
1379 GFileInfo *
1380 g_file_query_info_finish (GFile *file,
1381 GAsyncResult *res,
1382 GError **error)
1384 GFileIface *iface;
1386 g_return_val_if_fail (G_IS_FILE (file), NULL);
1387 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1389 if (g_async_result_legacy_propagate_error (res, error))
1390 return NULL;
1392 iface = G_FILE_GET_IFACE (file);
1393 return (* iface->query_info_finish) (file, res, error);
1397 * g_file_query_filesystem_info:
1398 * @file: input #GFile
1399 * @attributes: an attribute query string
1400 * @cancellable: (nullable): optional #GCancellable object,
1401 * %NULL to ignore
1402 * @error: a #GError
1404 * Similar to g_file_query_info(), but obtains information
1405 * about the filesystem the @file is on, rather than the file itself.
1406 * For instance the amount of space available and the type of
1407 * the filesystem.
1409 * The @attributes value is a string that specifies the attributes
1410 * that should be gathered. It is not an error if it's not possible
1411 * to read a particular requested attribute from a file - it just
1412 * won't be set. @attributes should be a comma-separated list of
1413 * attributes or attribute wildcards. The wildcard "*" means all
1414 * attributes, and a wildcard like "filesystem::*" means all attributes
1415 * in the filesystem namespace. The standard namespace for filesystem
1416 * attributes is "filesystem". Common attributes of interest are
1417 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1418 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1419 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1421 * If @cancellable is not %NULL, then the operation can be cancelled
1422 * by triggering the cancellable object from another thread. If the
1423 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1424 * returned.
1426 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1427 * be returned. Other errors are possible too, and depend on what
1428 * kind of filesystem the file is on.
1430 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1431 * Free the returned object with g_object_unref().
1433 GFileInfo *
1434 g_file_query_filesystem_info (GFile *file,
1435 const char *attributes,
1436 GCancellable *cancellable,
1437 GError **error)
1439 GFileIface *iface;
1441 g_return_val_if_fail (G_IS_FILE (file), NULL);
1443 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1444 return NULL;
1446 iface = G_FILE_GET_IFACE (file);
1448 if (iface->query_filesystem_info == NULL)
1450 g_set_error_literal (error, G_IO_ERROR,
1451 G_IO_ERROR_NOT_SUPPORTED,
1452 _("Operation not supported"));
1453 return NULL;
1456 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1460 * g_file_query_filesystem_info_async:
1461 * @file: input #GFile
1462 * @attributes: an attribute query string
1463 * @io_priority: the [I/O priority][io-priority] of the request
1464 * @cancellable: (nullable): optional #GCancellable object,
1465 * %NULL to ignore
1466 * @callback: (scope async): a #GAsyncReadyCallback to call
1467 * when the request is satisfied
1468 * @user_data: (closure): the data to pass to callback function
1470 * Asynchronously gets the requested information about the filesystem
1471 * that the specified @file is on. The result is a #GFileInfo object
1472 * that contains key-value attributes (such as type or size for the
1473 * file).
1475 * For more details, see g_file_query_filesystem_info() which is the
1476 * synchronous version of this call.
1478 * When the operation is finished, @callback will be called. You can
1479 * then call g_file_query_info_finish() to get the result of the
1480 * operation.
1482 void
1483 g_file_query_filesystem_info_async (GFile *file,
1484 const char *attributes,
1485 int io_priority,
1486 GCancellable *cancellable,
1487 GAsyncReadyCallback callback,
1488 gpointer user_data)
1490 GFileIface *iface;
1492 g_return_if_fail (G_IS_FILE (file));
1494 iface = G_FILE_GET_IFACE (file);
1495 (* iface->query_filesystem_info_async) (file,
1496 attributes,
1497 io_priority,
1498 cancellable,
1499 callback,
1500 user_data);
1504 * g_file_query_filesystem_info_finish:
1505 * @file: input #GFile
1506 * @res: a #GAsyncResult
1507 * @error: a #GError
1509 * Finishes an asynchronous filesystem info query.
1510 * See g_file_query_filesystem_info_async().
1512 * Returns: (transfer full): #GFileInfo for given @file
1513 * or %NULL on error.
1514 * Free the returned object with g_object_unref().
1516 GFileInfo *
1517 g_file_query_filesystem_info_finish (GFile *file,
1518 GAsyncResult *res,
1519 GError **error)
1521 GFileIface *iface;
1523 g_return_val_if_fail (G_IS_FILE (file), NULL);
1524 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1526 if (g_async_result_legacy_propagate_error (res, error))
1527 return NULL;
1529 iface = G_FILE_GET_IFACE (file);
1530 return (* iface->query_filesystem_info_finish) (file, res, error);
1534 * g_file_find_enclosing_mount:
1535 * @file: input #GFile
1536 * @cancellable: (nullable): optional #GCancellable object,
1537 * %NULL to ignore
1538 * @error: a #GError
1540 * Gets a #GMount for the #GFile.
1542 * If the #GFileIface for @file does not have a mount (e.g.
1543 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1544 * and %NULL will be returned.
1546 * If @cancellable is not %NULL, then the operation can be cancelled by
1547 * triggering the cancellable object from another thread. If the operation
1548 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1550 * Returns: (transfer full): a #GMount where the @file is located
1551 * or %NULL on error.
1552 * Free the returned object with g_object_unref().
1554 GMount *
1555 g_file_find_enclosing_mount (GFile *file,
1556 GCancellable *cancellable,
1557 GError **error)
1559 GFileIface *iface;
1561 g_return_val_if_fail (G_IS_FILE (file), NULL);
1563 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1564 return NULL;
1566 iface = G_FILE_GET_IFACE (file);
1567 if (iface->find_enclosing_mount == NULL)
1570 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1571 /* Translators: This is an error message when
1572 * trying to find the enclosing (user visible)
1573 * mount of a file, but none exists.
1575 _("Containing mount does not exist"));
1576 return NULL;
1579 return (* iface->find_enclosing_mount) (file, cancellable, error);
1583 * g_file_find_enclosing_mount_async:
1584 * @file: a #GFile
1585 * @io_priority: the [I/O priority][io-priority] of the request
1586 * @cancellable: (nullable): optional #GCancellable object,
1587 * %NULL to ignore
1588 * @callback: (scope async): a #GAsyncReadyCallback to call
1589 * when the request is satisfied
1590 * @user_data: (closure): the data to pass to callback function
1592 * Asynchronously gets the mount for the file.
1594 * For more details, see g_file_find_enclosing_mount() which is
1595 * the synchronous version of this call.
1597 * When the operation is finished, @callback will be called.
1598 * You can then call g_file_find_enclosing_mount_finish() to
1599 * get the result of the operation.
1601 void
1602 g_file_find_enclosing_mount_async (GFile *file,
1603 int io_priority,
1604 GCancellable *cancellable,
1605 GAsyncReadyCallback callback,
1606 gpointer user_data)
1608 GFileIface *iface;
1610 g_return_if_fail (G_IS_FILE (file));
1612 iface = G_FILE_GET_IFACE (file);
1613 (* iface->find_enclosing_mount_async) (file,
1614 io_priority,
1615 cancellable,
1616 callback,
1617 user_data);
1621 * g_file_find_enclosing_mount_finish:
1622 * @file: a #GFile
1623 * @res: a #GAsyncResult
1624 * @error: a #GError
1626 * Finishes an asynchronous find mount request.
1627 * See g_file_find_enclosing_mount_async().
1629 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1630 * Free the returned object with g_object_unref().
1632 GMount *
1633 g_file_find_enclosing_mount_finish (GFile *file,
1634 GAsyncResult *res,
1635 GError **error)
1637 GFileIface *iface;
1639 g_return_val_if_fail (G_IS_FILE (file), NULL);
1640 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1642 if (g_async_result_legacy_propagate_error (res, error))
1643 return NULL;
1645 iface = G_FILE_GET_IFACE (file);
1646 return (* iface->find_enclosing_mount_finish) (file, res, error);
1651 * g_file_read:
1652 * @file: #GFile to read
1653 * @cancellable: (nullable): a #GCancellable
1654 * @error: a #GError, or %NULL
1656 * Opens a file for reading. The result is a #GFileInputStream that
1657 * can be used to read the contents of the file.
1659 * If @cancellable is not %NULL, then the operation can be cancelled by
1660 * triggering the cancellable object from another thread. If the operation
1661 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1663 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1664 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1665 * error will be returned. Other errors are possible too, and depend
1666 * on what kind of filesystem the file is on.
1668 * Virtual: read_fn
1669 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1670 * Free the returned object with g_object_unref().
1672 GFileInputStream *
1673 g_file_read (GFile *file,
1674 GCancellable *cancellable,
1675 GError **error)
1677 GFileIface *iface;
1679 g_return_val_if_fail (G_IS_FILE (file), NULL);
1681 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1682 return NULL;
1684 iface = G_FILE_GET_IFACE (file);
1686 if (iface->read_fn == NULL)
1688 g_set_error_literal (error, G_IO_ERROR,
1689 G_IO_ERROR_NOT_SUPPORTED,
1690 _("Operation not supported"));
1691 return NULL;
1694 return (* iface->read_fn) (file, cancellable, error);
1698 * g_file_append_to:
1699 * @file: input #GFile
1700 * @flags: a set of #GFileCreateFlags
1701 * @cancellable: (nullable): optional #GCancellable object,
1702 * %NULL to ignore
1703 * @error: a #GError, or %NULL
1705 * Gets an output stream for appending data to the file.
1706 * If the file doesn't already exist it is created.
1708 * By default files created are generally readable by everyone,
1709 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1710 * will be made readable only to the current user, to the level that
1711 * is supported on the target filesystem.
1713 * If @cancellable is not %NULL, then the operation can be cancelled
1714 * by triggering the cancellable object from another thread. If the
1715 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1716 * returned.
1718 * Some file systems don't allow all file names, and may return an
1719 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1720 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1721 * possible too, and depend on what kind of filesystem the file is on.
1723 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1724 * Free the returned object with g_object_unref().
1726 GFileOutputStream *
1727 g_file_append_to (GFile *file,
1728 GFileCreateFlags flags,
1729 GCancellable *cancellable,
1730 GError **error)
1732 GFileIface *iface;
1734 g_return_val_if_fail (G_IS_FILE (file), NULL);
1736 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1737 return NULL;
1739 iface = G_FILE_GET_IFACE (file);
1741 if (iface->append_to == NULL)
1743 g_set_error_literal (error, G_IO_ERROR,
1744 G_IO_ERROR_NOT_SUPPORTED,
1745 _("Operation not supported"));
1746 return NULL;
1749 return (* iface->append_to) (file, flags, cancellable, error);
1753 * g_file_create:
1754 * @file: input #GFile
1755 * @flags: a set of #GFileCreateFlags
1756 * @cancellable: (nullable): optional #GCancellable object,
1757 * %NULL to ignore
1758 * @error: a #GError, or %NULL
1760 * Creates a new file and returns an output stream for writing to it.
1761 * The file must not already exist.
1763 * By default files created are generally readable by everyone,
1764 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1765 * will be made readable only to the current user, to the level
1766 * that is supported on the target filesystem.
1768 * If @cancellable is not %NULL, then the operation can be cancelled
1769 * by triggering the cancellable object from another thread. If the
1770 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1771 * returned.
1773 * If a file or directory with this name already exists the
1774 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1775 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1776 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1777 * be returned. Other errors are possible too, and depend on what kind
1778 * of filesystem the file is on.
1780 * Returns: (transfer full): a #GFileOutputStream for the newly created
1781 * file, or %NULL on error.
1782 * Free the returned object with g_object_unref().
1784 GFileOutputStream *
1785 g_file_create (GFile *file,
1786 GFileCreateFlags flags,
1787 GCancellable *cancellable,
1788 GError **error)
1790 GFileIface *iface;
1792 g_return_val_if_fail (G_IS_FILE (file), NULL);
1794 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1795 return NULL;
1797 iface = G_FILE_GET_IFACE (file);
1799 if (iface->create == NULL)
1801 g_set_error_literal (error, G_IO_ERROR,
1802 G_IO_ERROR_NOT_SUPPORTED,
1803 _("Operation not supported"));
1804 return NULL;
1807 return (* iface->create) (file, flags, cancellable, error);
1811 * g_file_replace:
1812 * @file: input #GFile
1813 * @etag: (nullable): an optional [entity tag][gfile-etag]
1814 * for the current #GFile, or #NULL to ignore
1815 * @make_backup: %TRUE if a backup should be created
1816 * @flags: a set of #GFileCreateFlags
1817 * @cancellable: (nullable): optional #GCancellable object,
1818 * %NULL to ignore
1819 * @error: a #GError, or %NULL
1821 * Returns an output stream for overwriting the file, possibly
1822 * creating a backup copy of the file first. If the file doesn't exist,
1823 * it will be created.
1825 * This will try to replace the file in the safest way possible so
1826 * that any errors during the writing will not affect an already
1827 * existing copy of the file. For instance, for local files it
1828 * may write to a temporary file and then atomically rename over
1829 * the destination when the stream is closed.
1831 * By default files created are generally readable by everyone,
1832 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1833 * will be made readable only to the current user, to the level that
1834 * is supported on the target filesystem.
1836 * If @cancellable is not %NULL, then the operation can be cancelled
1837 * by triggering the cancellable object from another thread. If the
1838 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1839 * returned.
1841 * If you pass in a non-%NULL @etag value and @file already exists, then
1842 * this value is compared to the current entity tag of the file, and if
1843 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1844 * generally means that the file has been changed since you last read
1845 * it. You can get the new etag from g_file_output_stream_get_etag()
1846 * after you've finished writing and closed the #GFileOutputStream. When
1847 * you load a new file you can use g_file_input_stream_query_info() to
1848 * get the etag of the file.
1850 * If @make_backup is %TRUE, this function will attempt to make a
1851 * backup of the current file before overwriting it. If this fails
1852 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1853 * want to replace anyway, try again with @make_backup set to %FALSE.
1855 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1856 * be returned, and if the file is some other form of non-regular file
1857 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1858 * file systems don't allow all file names, and may return an
1859 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1860 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1861 * possible too, and depend on what kind of filesystem the file is on.
1863 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1864 * Free the returned object with g_object_unref().
1866 GFileOutputStream *
1867 g_file_replace (GFile *file,
1868 const char *etag,
1869 gboolean make_backup,
1870 GFileCreateFlags flags,
1871 GCancellable *cancellable,
1872 GError **error)
1874 GFileIface *iface;
1876 g_return_val_if_fail (G_IS_FILE (file), NULL);
1878 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1879 return NULL;
1881 iface = G_FILE_GET_IFACE (file);
1883 if (iface->replace == NULL)
1885 g_set_error_literal (error, G_IO_ERROR,
1886 G_IO_ERROR_NOT_SUPPORTED,
1887 _("Operation not supported"));
1888 return NULL;
1891 /* Handle empty tag string as NULL in consistent way. */
1892 if (etag && *etag == 0)
1893 etag = NULL;
1895 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1899 * g_file_open_readwrite:
1900 * @file: #GFile to open
1901 * @cancellable: (nullable): a #GCancellable
1902 * @error: a #GError, or %NULL
1904 * Opens an existing file for reading and writing. The result is
1905 * a #GFileIOStream that can be used to read and write the contents
1906 * of the file.
1908 * If @cancellable is not %NULL, then the operation can be cancelled
1909 * by triggering the cancellable object from another thread. If the
1910 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1911 * returned.
1913 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1914 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1915 * error will be returned. Other errors are possible too, and depend on
1916 * what kind of filesystem the file is on. Note that in many non-local
1917 * file cases read and write streams are not supported, so make sure you
1918 * really need to do read and write streaming, rather than just opening
1919 * for reading or writing.
1921 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1922 * Free the returned object with g_object_unref().
1924 * Since: 2.22
1926 GFileIOStream *
1927 g_file_open_readwrite (GFile *file,
1928 GCancellable *cancellable,
1929 GError **error)
1931 GFileIface *iface;
1933 g_return_val_if_fail (G_IS_FILE (file), NULL);
1935 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1936 return NULL;
1938 iface = G_FILE_GET_IFACE (file);
1940 if (iface->open_readwrite == NULL)
1942 g_set_error_literal (error, G_IO_ERROR,
1943 G_IO_ERROR_NOT_SUPPORTED,
1944 _("Operation not supported"));
1945 return NULL;
1948 return (* iface->open_readwrite) (file, cancellable, error);
1952 * g_file_create_readwrite:
1953 * @file: a #GFile
1954 * @flags: a set of #GFileCreateFlags
1955 * @cancellable: (nullable): optional #GCancellable object,
1956 * %NULL to ignore
1957 * @error: return location for a #GError, or %NULL
1959 * Creates a new file and returns a stream for reading and
1960 * writing to it. The file must not already exist.
1962 * By default files created are generally readable by everyone,
1963 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1964 * will be made readable only to the current user, to the level
1965 * that is supported on the target filesystem.
1967 * If @cancellable is not %NULL, then the operation can be cancelled
1968 * by triggering the cancellable object from another thread. If the
1969 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1970 * returned.
1972 * If a file or directory with this name already exists, the
1973 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1974 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1975 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1976 * will be returned. Other errors are possible too, and depend on what
1977 * kind of filesystem the file is on.
1979 * Note that in many non-local file cases read and write streams are
1980 * not supported, so make sure you really need to do read and write
1981 * streaming, rather than just opening for reading or writing.
1983 * Returns: (transfer full): a #GFileIOStream for the newly created
1984 * file, or %NULL on error.
1985 * Free the returned object with g_object_unref().
1987 * Since: 2.22
1989 GFileIOStream *
1990 g_file_create_readwrite (GFile *file,
1991 GFileCreateFlags flags,
1992 GCancellable *cancellable,
1993 GError **error)
1995 GFileIface *iface;
1997 g_return_val_if_fail (G_IS_FILE (file), NULL);
1999 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2000 return NULL;
2002 iface = G_FILE_GET_IFACE (file);
2004 if (iface->create_readwrite == NULL)
2006 g_set_error_literal (error, G_IO_ERROR,
2007 G_IO_ERROR_NOT_SUPPORTED,
2008 _("Operation not supported"));
2009 return NULL;
2012 return (* iface->create_readwrite) (file, flags, cancellable, error);
2016 * g_file_replace_readwrite:
2017 * @file: a #GFile
2018 * @etag: (nullable): an optional [entity tag][gfile-etag]
2019 * for the current #GFile, or #NULL to ignore
2020 * @make_backup: %TRUE if a backup should be created
2021 * @flags: a set of #GFileCreateFlags
2022 * @cancellable: (nullable): optional #GCancellable object,
2023 * %NULL to ignore
2024 * @error: return location for a #GError, or %NULL
2026 * Returns an output stream for overwriting the file in readwrite mode,
2027 * possibly creating a backup copy of the file first. If the file doesn't
2028 * exist, it will be created.
2030 * For details about the behaviour, see g_file_replace() which does the
2031 * same thing but returns an output stream only.
2033 * Note that in many non-local file cases read and write streams are not
2034 * supported, so make sure you really need to do read and write streaming,
2035 * rather than just opening for reading or writing.
2037 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2038 * Free the returned object with g_object_unref().
2040 * Since: 2.22
2042 GFileIOStream *
2043 g_file_replace_readwrite (GFile *file,
2044 const char *etag,
2045 gboolean make_backup,
2046 GFileCreateFlags flags,
2047 GCancellable *cancellable,
2048 GError **error)
2050 GFileIface *iface;
2052 g_return_val_if_fail (G_IS_FILE (file), NULL);
2054 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2055 return NULL;
2057 iface = G_FILE_GET_IFACE (file);
2059 if (iface->replace_readwrite == NULL)
2061 g_set_error_literal (error, G_IO_ERROR,
2062 G_IO_ERROR_NOT_SUPPORTED,
2063 _("Operation not supported"));
2064 return NULL;
2067 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2071 * g_file_read_async:
2072 * @file: input #GFile
2073 * @io_priority: the [I/O priority][io-priority] of the request
2074 * @cancellable: (nullable): optional #GCancellable object,
2075 * %NULL to ignore
2076 * @callback: (scope async): a #GAsyncReadyCallback to call
2077 * when the request is satisfied
2078 * @user_data: (closure): the data to pass to callback function
2080 * Asynchronously opens @file for reading.
2082 * For more details, see g_file_read() which is
2083 * the synchronous version of this call.
2085 * When the operation is finished, @callback will be called.
2086 * You can then call g_file_read_finish() to get the result
2087 * of the operation.
2089 void
2090 g_file_read_async (GFile *file,
2091 int io_priority,
2092 GCancellable *cancellable,
2093 GAsyncReadyCallback callback,
2094 gpointer user_data)
2096 GFileIface *iface;
2098 g_return_if_fail (G_IS_FILE (file));
2100 iface = G_FILE_GET_IFACE (file);
2101 (* iface->read_async) (file,
2102 io_priority,
2103 cancellable,
2104 callback,
2105 user_data);
2109 * g_file_read_finish:
2110 * @file: input #GFile
2111 * @res: a #GAsyncResult
2112 * @error: a #GError, or %NULL
2114 * Finishes an asynchronous file read operation started with
2115 * g_file_read_async().
2117 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2118 * Free the returned object with g_object_unref().
2120 GFileInputStream *
2121 g_file_read_finish (GFile *file,
2122 GAsyncResult *res,
2123 GError **error)
2125 GFileIface *iface;
2127 g_return_val_if_fail (G_IS_FILE (file), NULL);
2128 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2130 if (g_async_result_legacy_propagate_error (res, error))
2131 return NULL;
2133 iface = G_FILE_GET_IFACE (file);
2134 return (* iface->read_finish) (file, res, error);
2138 * g_file_append_to_async:
2139 * @file: input #GFile
2140 * @flags: a set of #GFileCreateFlags
2141 * @io_priority: the [I/O priority][io-priority] of the request
2142 * @cancellable: (nullable): optional #GCancellable object,
2143 * %NULL to ignore
2144 * @callback: (scope async): a #GAsyncReadyCallback to call
2145 * when the request is satisfied
2146 * @user_data: (closure): the data to pass to callback function
2148 * Asynchronously opens @file for appending.
2150 * For more details, see g_file_append_to() which is
2151 * the synchronous version of this call.
2153 * When the operation is finished, @callback will be called.
2154 * You can then call g_file_append_to_finish() to get the result
2155 * of the operation.
2157 void
2158 g_file_append_to_async (GFile *file,
2159 GFileCreateFlags flags,
2160 int io_priority,
2161 GCancellable *cancellable,
2162 GAsyncReadyCallback callback,
2163 gpointer user_data)
2165 GFileIface *iface;
2167 g_return_if_fail (G_IS_FILE (file));
2169 iface = G_FILE_GET_IFACE (file);
2170 (* iface->append_to_async) (file,
2171 flags,
2172 io_priority,
2173 cancellable,
2174 callback,
2175 user_data);
2179 * g_file_append_to_finish:
2180 * @file: input #GFile
2181 * @res: #GAsyncResult
2182 * @error: a #GError, or %NULL
2184 * Finishes an asynchronous file append operation started with
2185 * g_file_append_to_async().
2187 * Returns: (transfer full): a valid #GFileOutputStream
2188 * or %NULL on error.
2189 * Free the returned object with g_object_unref().
2191 GFileOutputStream *
2192 g_file_append_to_finish (GFile *file,
2193 GAsyncResult *res,
2194 GError **error)
2196 GFileIface *iface;
2198 g_return_val_if_fail (G_IS_FILE (file), NULL);
2199 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2201 if (g_async_result_legacy_propagate_error (res, error))
2202 return NULL;
2204 iface = G_FILE_GET_IFACE (file);
2205 return (* iface->append_to_finish) (file, res, error);
2209 * g_file_create_async:
2210 * @file: input #GFile
2211 * @flags: a set of #GFileCreateFlags
2212 * @io_priority: the [I/O priority][io-priority] of the request
2213 * @cancellable: (nullable): optional #GCancellable object,
2214 * %NULL to ignore
2215 * @callback: (scope async): a #GAsyncReadyCallback to call
2216 * when the request is satisfied
2217 * @user_data: (closure): the data to pass to callback function
2219 * Asynchronously creates a new file and returns an output stream
2220 * for writing to it. The file must not already exist.
2222 * For more details, see g_file_create() which is
2223 * the synchronous version of this call.
2225 * When the operation is finished, @callback will be called.
2226 * You can then call g_file_create_finish() to get the result
2227 * of the operation.
2229 void
2230 g_file_create_async (GFile *file,
2231 GFileCreateFlags flags,
2232 int io_priority,
2233 GCancellable *cancellable,
2234 GAsyncReadyCallback callback,
2235 gpointer user_data)
2237 GFileIface *iface;
2239 g_return_if_fail (G_IS_FILE (file));
2241 iface = G_FILE_GET_IFACE (file);
2242 (* iface->create_async) (file,
2243 flags,
2244 io_priority,
2245 cancellable,
2246 callback,
2247 user_data);
2251 * g_file_create_finish:
2252 * @file: input #GFile
2253 * @res: a #GAsyncResult
2254 * @error: a #GError, or %NULL
2256 * Finishes an asynchronous file create operation started with
2257 * g_file_create_async().
2259 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2260 * Free the returned object with g_object_unref().
2262 GFileOutputStream *
2263 g_file_create_finish (GFile *file,
2264 GAsyncResult *res,
2265 GError **error)
2267 GFileIface *iface;
2269 g_return_val_if_fail (G_IS_FILE (file), NULL);
2270 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2272 if (g_async_result_legacy_propagate_error (res, error))
2273 return NULL;
2275 iface = G_FILE_GET_IFACE (file);
2276 return (* iface->create_finish) (file, res, error);
2280 * g_file_replace_async:
2281 * @file: input #GFile
2282 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2283 * or %NULL to ignore
2284 * @make_backup: %TRUE if a backup should be created
2285 * @flags: a set of #GFileCreateFlags
2286 * @io_priority: the [I/O priority][io-priority] of the request
2287 * @cancellable: (nullable): optional #GCancellable object,
2288 * %NULL to ignore
2289 * @callback: (scope async): a #GAsyncReadyCallback to call
2290 * when the request is satisfied
2291 * @user_data: (closure): the data to pass to callback function
2293 * Asynchronously overwrites the file, replacing the contents,
2294 * possibly creating a backup copy of the file first.
2296 * For more details, see g_file_replace() which is
2297 * the synchronous version of this call.
2299 * When the operation is finished, @callback will be called.
2300 * You can then call g_file_replace_finish() to get the result
2301 * of the operation.
2303 void
2304 g_file_replace_async (GFile *file,
2305 const char *etag,
2306 gboolean make_backup,
2307 GFileCreateFlags flags,
2308 int io_priority,
2309 GCancellable *cancellable,
2310 GAsyncReadyCallback callback,
2311 gpointer user_data)
2313 GFileIface *iface;
2315 g_return_if_fail (G_IS_FILE (file));
2317 iface = G_FILE_GET_IFACE (file);
2318 (* iface->replace_async) (file,
2319 etag,
2320 make_backup,
2321 flags,
2322 io_priority,
2323 cancellable,
2324 callback,
2325 user_data);
2329 * g_file_replace_finish:
2330 * @file: input #GFile
2331 * @res: a #GAsyncResult
2332 * @error: a #GError, or %NULL
2334 * Finishes an asynchronous file replace operation started with
2335 * g_file_replace_async().
2337 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2338 * Free the returned object with g_object_unref().
2340 GFileOutputStream *
2341 g_file_replace_finish (GFile *file,
2342 GAsyncResult *res,
2343 GError **error)
2345 GFileIface *iface;
2347 g_return_val_if_fail (G_IS_FILE (file), NULL);
2348 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2350 if (g_async_result_legacy_propagate_error (res, error))
2351 return NULL;
2353 iface = G_FILE_GET_IFACE (file);
2354 return (* iface->replace_finish) (file, res, error);
2358 * g_file_open_readwrite_async
2359 * @file: input #GFile
2360 * @io_priority: the [I/O priority][io-priority] of the request
2361 * @cancellable: (nullable): optional #GCancellable object,
2362 * %NULL to ignore
2363 * @callback: (scope async): a #GAsyncReadyCallback to call
2364 * when the request is satisfied
2365 * @user_data: (closure): the data to pass to callback function
2367 * Asynchronously opens @file for reading and writing.
2369 * For more details, see g_file_open_readwrite() which is
2370 * the synchronous version of this call.
2372 * When the operation is finished, @callback will be called.
2373 * You can then call g_file_open_readwrite_finish() to get
2374 * the result of the operation.
2376 * Since: 2.22
2378 void
2379 g_file_open_readwrite_async (GFile *file,
2380 int io_priority,
2381 GCancellable *cancellable,
2382 GAsyncReadyCallback callback,
2383 gpointer user_data)
2385 GFileIface *iface;
2387 g_return_if_fail (G_IS_FILE (file));
2389 iface = G_FILE_GET_IFACE (file);
2390 (* iface->open_readwrite_async) (file,
2391 io_priority,
2392 cancellable,
2393 callback,
2394 user_data);
2398 * g_file_open_readwrite_finish:
2399 * @file: input #GFile
2400 * @res: a #GAsyncResult
2401 * @error: a #GError, or %NULL
2403 * Finishes an asynchronous file read operation started with
2404 * g_file_open_readwrite_async().
2406 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2407 * Free the returned object with g_object_unref().
2409 * Since: 2.22
2411 GFileIOStream *
2412 g_file_open_readwrite_finish (GFile *file,
2413 GAsyncResult *res,
2414 GError **error)
2416 GFileIface *iface;
2418 g_return_val_if_fail (G_IS_FILE (file), NULL);
2419 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2421 if (g_async_result_legacy_propagate_error (res, error))
2422 return NULL;
2424 iface = G_FILE_GET_IFACE (file);
2425 return (* iface->open_readwrite_finish) (file, res, error);
2429 * g_file_create_readwrite_async:
2430 * @file: input #GFile
2431 * @flags: a set of #GFileCreateFlags
2432 * @io_priority: the [I/O priority][io-priority] of the request
2433 * @cancellable: (nullable): optional #GCancellable object,
2434 * %NULL to ignore
2435 * @callback: (scope async): a #GAsyncReadyCallback to call
2436 * when the request is satisfied
2437 * @user_data: (closure): the data to pass to callback function
2439 * Asynchronously creates a new file and returns a stream
2440 * for reading and writing to it. The file must not already exist.
2442 * For more details, see g_file_create_readwrite() which is
2443 * the synchronous version of this call.
2445 * When the operation is finished, @callback will be called.
2446 * You can then call g_file_create_readwrite_finish() to get
2447 * the result of the operation.
2449 * Since: 2.22
2451 void
2452 g_file_create_readwrite_async (GFile *file,
2453 GFileCreateFlags flags,
2454 int io_priority,
2455 GCancellable *cancellable,
2456 GAsyncReadyCallback callback,
2457 gpointer user_data)
2459 GFileIface *iface;
2461 g_return_if_fail (G_IS_FILE (file));
2463 iface = G_FILE_GET_IFACE (file);
2464 (* iface->create_readwrite_async) (file,
2465 flags,
2466 io_priority,
2467 cancellable,
2468 callback,
2469 user_data);
2473 * g_file_create_readwrite_finish:
2474 * @file: input #GFile
2475 * @res: a #GAsyncResult
2476 * @error: a #GError, or %NULL
2478 * Finishes an asynchronous file create operation started with
2479 * g_file_create_readwrite_async().
2481 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2482 * Free the returned object with g_object_unref().
2484 * Since: 2.22
2486 GFileIOStream *
2487 g_file_create_readwrite_finish (GFile *file,
2488 GAsyncResult *res,
2489 GError **error)
2491 GFileIface *iface;
2493 g_return_val_if_fail (G_IS_FILE (file), NULL);
2494 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2496 if (g_async_result_legacy_propagate_error (res, error))
2497 return NULL;
2499 iface = G_FILE_GET_IFACE (file);
2500 return (* iface->create_readwrite_finish) (file, res, error);
2504 * g_file_replace_readwrite_async:
2505 * @file: input #GFile
2506 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2507 * or %NULL to ignore
2508 * @make_backup: %TRUE if a backup should be created
2509 * @flags: a set of #GFileCreateFlags
2510 * @io_priority: the [I/O priority][io-priority] of the request
2511 * @cancellable: (nullable): optional #GCancellable object,
2512 * %NULL to ignore
2513 * @callback: (scope async): a #GAsyncReadyCallback to call
2514 * when the request is satisfied
2515 * @user_data: (closure): the data to pass to callback function
2517 * Asynchronously overwrites the file in read-write mode,
2518 * replacing the contents, possibly creating a backup copy
2519 * of the file first.
2521 * For more details, see g_file_replace_readwrite() which is
2522 * the synchronous version of this call.
2524 * When the operation is finished, @callback will be called.
2525 * You can then call g_file_replace_readwrite_finish() to get
2526 * the result of the operation.
2528 * Since: 2.22
2530 void
2531 g_file_replace_readwrite_async (GFile *file,
2532 const char *etag,
2533 gboolean make_backup,
2534 GFileCreateFlags flags,
2535 int io_priority,
2536 GCancellable *cancellable,
2537 GAsyncReadyCallback callback,
2538 gpointer user_data)
2540 GFileIface *iface;
2542 g_return_if_fail (G_IS_FILE (file));
2544 iface = G_FILE_GET_IFACE (file);
2545 (* iface->replace_readwrite_async) (file,
2546 etag,
2547 make_backup,
2548 flags,
2549 io_priority,
2550 cancellable,
2551 callback,
2552 user_data);
2556 * g_file_replace_readwrite_finish:
2557 * @file: input #GFile
2558 * @res: a #GAsyncResult
2559 * @error: a #GError, or %NULL
2561 * Finishes an asynchronous file replace operation started with
2562 * g_file_replace_readwrite_async().
2564 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2565 * Free the returned object with g_object_unref().
2567 * Since: 2.22
2569 GFileIOStream *
2570 g_file_replace_readwrite_finish (GFile *file,
2571 GAsyncResult *res,
2572 GError **error)
2574 GFileIface *iface;
2576 g_return_val_if_fail (G_IS_FILE (file), NULL);
2577 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2579 if (g_async_result_legacy_propagate_error (res, error))
2580 return NULL;
2582 iface = G_FILE_GET_IFACE (file);
2583 return (* iface->replace_readwrite_finish) (file, res, error);
2586 static gboolean
2587 copy_symlink (GFile *destination,
2588 GFileCopyFlags flags,
2589 GCancellable *cancellable,
2590 const char *target,
2591 GError **error)
2593 GError *my_error;
2594 gboolean tried_delete;
2595 GFileInfo *info;
2596 GFileType file_type;
2598 tried_delete = FALSE;
2600 retry:
2601 my_error = NULL;
2602 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2604 /* Maybe it already existed, and we want to overwrite? */
2605 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2606 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2608 g_clear_error (&my_error);
2610 /* Don't overwrite if the destination is a directory */
2611 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2612 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2613 cancellable, &my_error);
2614 if (info != NULL)
2616 file_type = g_file_info_get_file_type (info);
2617 g_object_unref (info);
2619 if (file_type == G_FILE_TYPE_DIRECTORY)
2621 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2622 _("Can’t copy over directory"));
2623 return FALSE;
2627 if (!g_file_delete (destination, cancellable, error))
2628 return FALSE;
2630 tried_delete = TRUE;
2631 goto retry;
2633 /* Nah, fail */
2634 g_propagate_error (error, my_error);
2635 return FALSE;
2638 return TRUE;
2641 static GFileInputStream *
2642 open_source_for_copy (GFile *source,
2643 GFile *destination,
2644 GFileCopyFlags flags,
2645 GCancellable *cancellable,
2646 GError **error)
2648 GError *my_error;
2649 GFileInputStream *ret;
2650 GFileInfo *info;
2651 GFileType file_type;
2653 my_error = NULL;
2654 ret = g_file_read (source, cancellable, &my_error);
2655 if (ret != NULL)
2656 return ret;
2658 /* There was an error opening the source, try to set a good error for it: */
2659 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2661 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2662 * as that is less useful to the app. Better check for errors on the
2663 * target instead.
2665 g_error_free (my_error);
2666 my_error = NULL;
2668 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2669 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2670 cancellable, &my_error);
2671 if (info != NULL &&
2672 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2674 file_type = g_file_info_get_file_type (info);
2675 g_object_unref (info);
2677 if (flags & G_FILE_COPY_OVERWRITE)
2679 if (file_type == G_FILE_TYPE_DIRECTORY)
2681 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2682 _("Can’t copy directory over directory"));
2683 return NULL;
2685 /* continue to would_recurse error */
2687 else
2689 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2690 _("Target file exists"));
2691 return NULL;
2694 else
2696 /* Error getting info from target, return that error
2697 * (except for NOT_FOUND, which is no error here)
2699 g_clear_object (&info);
2700 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2702 g_propagate_error (error, my_error);
2703 return NULL;
2705 g_clear_error (&my_error);
2708 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2709 _("Can’t recursively copy directory"));
2710 return NULL;
2713 g_propagate_error (error, my_error);
2714 return NULL;
2717 static gboolean
2718 should_copy (GFileAttributeInfo *info,
2719 gboolean copy_all_attributes,
2720 gboolean skip_perms)
2722 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2723 return FALSE;
2725 if (copy_all_attributes)
2726 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2727 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2730 static gboolean
2731 build_attribute_list_for_copy (GFile *file,
2732 GFileCopyFlags flags,
2733 char **out_attributes,
2734 GCancellable *cancellable,
2735 GError **error)
2737 gboolean ret = FALSE;
2738 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2739 GString *s = NULL;
2740 gboolean first;
2741 int i;
2742 gboolean copy_all_attributes;
2743 gboolean skip_perms;
2745 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2746 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2748 /* Ignore errors here, if the target supports no attributes there is
2749 * nothing to copy. We still honor the cancellable though.
2751 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2752 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2753 goto out;
2755 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2756 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2757 goto out;
2759 if (attributes == NULL && namespaces == NULL)
2760 goto out;
2762 first = TRUE;
2763 s = g_string_new ("");
2765 if (attributes)
2767 for (i = 0; i < attributes->n_infos; i++)
2769 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2771 if (first)
2772 first = FALSE;
2773 else
2774 g_string_append_c (s, ',');
2776 g_string_append (s, attributes->infos[i].name);
2781 if (namespaces)
2783 for (i = 0; i < namespaces->n_infos; i++)
2785 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2787 if (first)
2788 first = FALSE;
2789 else
2790 g_string_append_c (s, ',');
2792 g_string_append (s, namespaces->infos[i].name);
2793 g_string_append (s, "::*");
2798 ret = TRUE;
2799 *out_attributes = g_string_free (s, FALSE);
2800 s = NULL;
2801 out:
2802 if (s)
2803 g_string_free (s, TRUE);
2804 if (attributes)
2805 g_file_attribute_info_list_unref (attributes);
2806 if (namespaces)
2807 g_file_attribute_info_list_unref (namespaces);
2809 return ret;
2813 * g_file_copy_attributes:
2814 * @source: a #GFile with attributes
2815 * @destination: a #GFile to copy attributes to
2816 * @flags: a set of #GFileCopyFlags
2817 * @cancellable: (nullable): optional #GCancellable object,
2818 * %NULL to ignore
2819 * @error: a #GError, %NULL to ignore
2821 * Copies the file attributes from @source to @destination.
2823 * Normally only a subset of the file attributes are copied,
2824 * those that are copies in a normal file copy operation
2825 * (which for instance does not include e.g. owner). However
2826 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2827 * all the metadata that is possible to copy is copied. This
2828 * is useful when implementing move by copy + delete source.
2830 * Returns: %TRUE if the attributes were copied successfully,
2831 * %FALSE otherwise.
2833 gboolean
2834 g_file_copy_attributes (GFile *source,
2835 GFile *destination,
2836 GFileCopyFlags flags,
2837 GCancellable *cancellable,
2838 GError **error)
2840 char *attrs_to_read;
2841 gboolean res;
2842 GFileInfo *info;
2843 gboolean source_nofollow_symlinks;
2845 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2846 cancellable, error))
2847 return FALSE;
2849 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2851 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2852 * we just don't copy it.
2854 info = g_file_query_info (source, attrs_to_read,
2855 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2856 cancellable,
2857 NULL);
2859 g_free (attrs_to_read);
2861 res = TRUE;
2862 if (info)
2864 res = g_file_set_attributes_from_info (destination,
2865 info,
2866 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2867 cancellable,
2868 error);
2869 g_object_unref (info);
2872 return res;
2875 /* 256k minus malloc overhead */
2876 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2878 static gboolean
2879 copy_stream_with_progress (GInputStream *in,
2880 GOutputStream *out,
2881 GFile *source,
2882 GCancellable *cancellable,
2883 GFileProgressCallback progress_callback,
2884 gpointer progress_callback_data,
2885 GError **error)
2887 gssize n_read;
2888 gsize n_written;
2889 goffset current_size;
2890 char *buffer;
2891 gboolean res;
2892 goffset total_size;
2893 GFileInfo *info;
2895 total_size = -1;
2896 /* avoid performance impact of querying total size when it's not needed */
2897 if (progress_callback)
2899 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2900 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2901 cancellable, NULL);
2902 if (info)
2904 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2905 total_size = g_file_info_get_size (info);
2906 g_object_unref (info);
2909 if (total_size == -1)
2911 info = g_file_query_info (source,
2912 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2913 G_FILE_QUERY_INFO_NONE,
2914 cancellable, NULL);
2915 if (info)
2917 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2918 total_size = g_file_info_get_size (info);
2919 g_object_unref (info);
2924 if (total_size == -1)
2925 total_size = 0;
2927 buffer = g_malloc0 (STREAM_BUFFER_SIZE);
2928 current_size = 0;
2929 res = TRUE;
2930 while (TRUE)
2932 n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
2933 if (n_read == -1)
2935 res = FALSE;
2936 break;
2939 if (n_read == 0)
2940 break;
2942 current_size += n_read;
2944 res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
2945 if (!res)
2946 break;
2948 if (progress_callback)
2949 progress_callback (current_size, total_size, progress_callback_data);
2951 g_free (buffer);
2953 /* Make sure we send full copied size */
2954 if (progress_callback)
2955 progress_callback (current_size, total_size, progress_callback_data);
2957 return res;
2960 #ifdef HAVE_SPLICE
2962 static gboolean
2963 do_splice (int fd_in,
2964 loff_t *off_in,
2965 int fd_out,
2966 loff_t *off_out,
2967 size_t len,
2968 long *bytes_transferd,
2969 GError **error)
2971 long result;
2973 retry:
2974 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2976 if (result == -1)
2978 int errsv = errno;
2980 if (errsv == EINTR)
2981 goto retry;
2982 else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
2983 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2984 _("Splice not supported"));
2985 else
2986 g_set_error (error, G_IO_ERROR,
2987 g_io_error_from_errno (errsv),
2988 _("Error splicing file: %s"),
2989 g_strerror (errsv));
2991 return FALSE;
2994 *bytes_transferd = result;
2995 return TRUE;
2998 static gboolean
2999 splice_stream_with_progress (GInputStream *in,
3000 GOutputStream *out,
3001 GCancellable *cancellable,
3002 GFileProgressCallback progress_callback,
3003 gpointer progress_callback_data,
3004 GError **error)
3006 int buffer[2] = { -1, -1 };
3007 int buffer_size;
3008 gboolean res;
3009 goffset total_size;
3010 loff_t offset_in;
3011 loff_t offset_out;
3012 int fd_in, fd_out;
3014 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3015 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3017 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
3018 return FALSE;
3020 #if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
3021 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3022 * pipe size. See: https://bugzilla.gnome.org/791457 */
3023 buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3024 if (buffer_size <= 0)
3026 int errsv;
3027 buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3028 errsv = errno;
3030 if (buffer_size <= 0)
3032 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
3033 _("Error splicing file: %s"), g_strerror (errsv));
3034 res = FALSE;
3035 goto out;
3038 #else
3039 /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3040 * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
3041 * running on Linux < 2.6.11 (where the capacity was the system page size,
3042 * typically 4KiB) because it’s ancient. See pipe(7). */
3043 buffer_size = 1024 * 64;
3044 #endif
3046 g_assert (buffer_size > 0);
3048 total_size = -1;
3049 /* avoid performance impact of querying total size when it's not needed */
3050 if (progress_callback)
3052 struct stat sbuf;
3054 if (fstat (fd_in, &sbuf) == 0)
3055 total_size = sbuf.st_size;
3058 if (total_size == -1)
3059 total_size = 0;
3061 offset_in = offset_out = 0;
3062 res = FALSE;
3063 while (TRUE)
3065 long n_read;
3066 long n_written;
3068 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3069 break;
3071 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3072 break;
3074 if (n_read == 0)
3076 res = TRUE;
3077 break;
3080 while (n_read > 0)
3082 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3083 goto out;
3085 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3086 goto out;
3088 n_read -= n_written;
3091 if (progress_callback)
3092 progress_callback (offset_in, total_size, progress_callback_data);
3095 /* Make sure we send full copied size */
3096 if (progress_callback)
3097 progress_callback (offset_in, total_size, progress_callback_data);
3099 if (!g_close (buffer[0], error))
3100 goto out;
3101 buffer[0] = -1;
3102 if (!g_close (buffer[1], error))
3103 goto out;
3104 buffer[1] = -1;
3105 out:
3106 if (buffer[0] != -1)
3107 (void) g_close (buffer[0], NULL);
3108 if (buffer[1] != -1)
3109 (void) g_close (buffer[1], NULL);
3111 return res;
3113 #endif
3115 #ifdef __linux__
3116 static gboolean
3117 btrfs_reflink_with_progress (GInputStream *in,
3118 GOutputStream *out,
3119 GFileInfo *info,
3120 GCancellable *cancellable,
3121 GFileProgressCallback progress_callback,
3122 gpointer progress_callback_data,
3123 GError **error)
3125 goffset source_size;
3126 int fd_in, fd_out;
3127 int ret, errsv;
3129 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3130 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3132 if (progress_callback)
3133 source_size = g_file_info_get_size (info);
3135 /* Btrfs clone ioctl properties:
3136 * - Works at the inode level
3137 * - Doesn't work with directories
3138 * - Always follows symlinks (source and destination)
3140 * By the time we get here, *in and *out are both regular files */
3141 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3142 errsv = errno;
3144 if (ret < 0)
3146 if (errsv == EXDEV)
3147 g_set_error_literal (error, G_IO_ERROR,
3148 G_IO_ERROR_NOT_SUPPORTED,
3149 _("Copy (reflink/clone) between mounts is not supported"));
3150 else if (errsv == EINVAL)
3151 g_set_error_literal (error, G_IO_ERROR,
3152 G_IO_ERROR_NOT_SUPPORTED,
3153 _("Copy (reflink/clone) is not supported or invalid"));
3154 else
3155 /* Most probably something odd happened; retry with fallback */
3156 g_set_error_literal (error, G_IO_ERROR,
3157 G_IO_ERROR_NOT_SUPPORTED,
3158 _("Copy (reflink/clone) is not supported or didn’t work"));
3159 /* We retry with fallback for all error cases because Btrfs is currently
3160 * unstable, and so we can't trust it to do clone properly.
3161 * In addition, any hard errors here would cause the same failure in the
3162 * fallback manual copy as well. */
3163 return FALSE;
3166 /* Make sure we send full copied size */
3167 if (progress_callback)
3168 progress_callback (source_size, source_size, progress_callback_data);
3170 return TRUE;
3172 #endif
3174 static gboolean
3175 file_copy_fallback (GFile *source,
3176 GFile *destination,
3177 GFileCopyFlags flags,
3178 GCancellable *cancellable,
3179 GFileProgressCallback progress_callback,
3180 gpointer progress_callback_data,
3181 GError **error)
3183 gboolean ret = FALSE;
3184 GFileInputStream *file_in = NULL;
3185 GInputStream *in = NULL;
3186 GOutputStream *out = NULL;
3187 GFileInfo *info = NULL;
3188 const char *target;
3189 char *attrs_to_read;
3190 gboolean do_set_attributes = FALSE;
3192 /* need to know the file type */
3193 info = g_file_query_info (source,
3194 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3195 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3196 cancellable,
3197 error);
3198 if (!info)
3199 goto out;
3201 /* Maybe copy the symlink? */
3202 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3203 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3205 target = g_file_info_get_symlink_target (info);
3206 if (target)
3208 if (!copy_symlink (destination, flags, cancellable, target, error))
3209 goto out;
3211 ret = TRUE;
3212 goto out;
3214 /* ... else fall back on a regular file copy */
3216 /* Handle "special" files (pipes, device nodes, ...)? */
3217 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3219 /* FIXME: could try to recreate device nodes and others? */
3220 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3221 _("Can’t copy special file"));
3222 goto out;
3225 /* Everything else should just fall back on a regular copy. */
3227 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3228 if (!file_in)
3229 goto out;
3230 in = G_INPUT_STREAM (file_in);
3232 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3233 cancellable, error))
3234 goto out;
3236 if (attrs_to_read != NULL)
3238 GError *tmp_error = NULL;
3240 /* Ok, ditch the previous lightweight info (on Unix we just
3241 * called lstat()); at this point we gather all the information
3242 * we need about the source from the opened file descriptor.
3244 g_object_unref (info);
3246 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3247 cancellable, &tmp_error);
3248 if (!info)
3250 /* Not all gvfs backends implement query_info_on_read(), we
3251 * can just fall back to the pathname again.
3252 * https://bugzilla.gnome.org/706254
3254 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3256 g_clear_error (&tmp_error);
3257 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3258 cancellable, error);
3260 else
3262 g_free (attrs_to_read);
3263 g_propagate_error (error, tmp_error);
3264 goto out;
3267 g_free (attrs_to_read);
3268 if (!info)
3269 goto out;
3271 do_set_attributes = TRUE;
3274 /* In the local file path, we pass down the source info which
3275 * includes things like unix::mode, to ensure that the target file
3276 * is not created with different permissions from the source file.
3278 * If a future API like g_file_replace_with_info() is added, switch
3279 * this code to use that.
3281 if (G_IS_LOCAL_FILE (destination))
3283 if (flags & G_FILE_COPY_OVERWRITE)
3284 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3285 FALSE, NULL,
3286 flags & G_FILE_COPY_BACKUP,
3287 G_FILE_CREATE_REPLACE_DESTINATION,
3288 info,
3289 cancellable, error);
3290 else
3291 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3292 FALSE, 0, info,
3293 cancellable, error);
3295 else if (flags & G_FILE_COPY_OVERWRITE)
3297 out = (GOutputStream *)g_file_replace (destination,
3298 NULL,
3299 flags & G_FILE_COPY_BACKUP,
3300 G_FILE_CREATE_REPLACE_DESTINATION,
3301 cancellable, error);
3303 else
3305 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3308 if (!out)
3309 goto out;
3311 #ifdef __linux__
3312 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3314 GError *reflink_err = NULL;
3316 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3317 progress_callback, progress_callback_data,
3318 &reflink_err))
3320 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3322 g_clear_error (&reflink_err);
3324 else
3326 g_propagate_error (error, reflink_err);
3327 goto out;
3330 else
3332 ret = TRUE;
3333 goto out;
3336 #endif
3338 #ifdef HAVE_SPLICE
3339 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3341 GError *splice_err = NULL;
3343 if (!splice_stream_with_progress (in, out, cancellable,
3344 progress_callback, progress_callback_data,
3345 &splice_err))
3347 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3349 g_clear_error (&splice_err);
3351 else
3353 g_propagate_error (error, splice_err);
3354 goto out;
3357 else
3359 ret = TRUE;
3360 goto out;
3364 #endif
3366 /* A plain read/write loop */
3367 if (!copy_stream_with_progress (in, out, source, cancellable,
3368 progress_callback, progress_callback_data,
3369 error))
3370 goto out;
3372 ret = TRUE;
3373 out:
3374 if (in)
3376 /* Don't care about errors in source here */
3377 (void) g_input_stream_close (in, cancellable, NULL);
3378 g_object_unref (in);
3381 if (out)
3383 /* But write errors on close are bad! */
3384 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3385 ret = FALSE;
3386 g_object_unref (out);
3389 /* Ignore errors here. Failure to copy metadata is not a hard error */
3390 /* TODO: set these attributes /before/ we do the rename() on Unix */
3391 if (ret && do_set_attributes)
3393 g_file_set_attributes_from_info (destination,
3394 info,
3395 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3396 cancellable,
3397 NULL);
3400 g_clear_object (&info);
3402 return ret;
3406 * g_file_copy:
3407 * @source: input #GFile
3408 * @destination: destination #GFile
3409 * @flags: set of #GFileCopyFlags
3410 * @cancellable: (nullable): optional #GCancellable object,
3411 * %NULL to ignore
3412 * @progress_callback: (nullable) (scope call): function to callback with
3413 * progress information, or %NULL if progress information is not needed
3414 * @progress_callback_data: (closure): user data to pass to @progress_callback
3415 * @error: #GError to set on error, or %NULL
3417 * Copies the file @source to the location specified by @destination.
3418 * Can not handle recursive copies of directories.
3420 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3421 * existing @destination file is overwritten.
3423 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3424 * will be copied as symlinks, otherwise the target of the
3425 * @source symlink will be copied.
3427 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3428 * that is possible to copy is copied, not just the default subset (which,
3429 * for instance, does not include the owner, see #GFileInfo).
3431 * If @cancellable is not %NULL, then the operation can be cancelled by
3432 * triggering the cancellable object from another thread. If the operation
3433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3435 * If @progress_callback is not %NULL, then the operation can be monitored
3436 * by setting this to a #GFileProgressCallback function.
3437 * @progress_callback_data will be passed to this function. It is guaranteed
3438 * that this callback will be called after all data has been transferred with
3439 * the total number of bytes copied during the operation.
3441 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3442 * is returned, independent on the status of the @destination.
3444 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3445 * the error %G_IO_ERROR_EXISTS is returned.
3447 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3448 * error is returned. If trying to overwrite a directory with a directory the
3449 * %G_IO_ERROR_WOULD_MERGE error is returned.
3451 * If the source is a directory and the target does not exist, or
3452 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3453 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3455 * If you are interested in copying the #GFile object itself (not the on-disk
3456 * file), see g_file_dup().
3458 * Returns: %TRUE on success, %FALSE otherwise.
3460 gboolean
3461 g_file_copy (GFile *source,
3462 GFile *destination,
3463 GFileCopyFlags flags,
3464 GCancellable *cancellable,
3465 GFileProgressCallback progress_callback,
3466 gpointer progress_callback_data,
3467 GError **error)
3469 GFileIface *iface;
3470 GError *my_error;
3471 gboolean res;
3473 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3474 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3476 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3477 return FALSE;
3479 iface = G_FILE_GET_IFACE (destination);
3480 if (iface->copy)
3482 my_error = NULL;
3483 res = (* iface->copy) (source, destination,
3484 flags, cancellable,
3485 progress_callback, progress_callback_data,
3486 &my_error);
3488 if (res)
3489 return TRUE;
3491 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3493 g_propagate_error (error, my_error);
3494 return FALSE;
3496 else
3497 g_clear_error (&my_error);
3500 /* If the types are different, and the destination method failed
3501 * also try the source method
3503 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3505 iface = G_FILE_GET_IFACE (source);
3507 if (iface->copy)
3509 my_error = NULL;
3510 res = (* iface->copy) (source, destination,
3511 flags, cancellable,
3512 progress_callback, progress_callback_data,
3513 &my_error);
3515 if (res)
3516 return TRUE;
3518 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3520 g_propagate_error (error, my_error);
3521 return FALSE;
3523 else
3524 g_clear_error (&my_error);
3528 return file_copy_fallback (source, destination, flags, cancellable,
3529 progress_callback, progress_callback_data,
3530 error);
3534 * g_file_copy_async:
3535 * @source: input #GFile
3536 * @destination: destination #GFile
3537 * @flags: set of #GFileCopyFlags
3538 * @io_priority: the [I/O priority][io-priority] of the request
3539 * @cancellable: (nullable): optional #GCancellable object,
3540 * %NULL to ignore
3541 * @progress_callback: (nullable) (scope notified): function to callback with progress
3542 * information, or %NULL if progress information is not needed
3543 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3544 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3545 * @user_data: (closure callback): the data to pass to callback function
3547 * Copies the file @source to the location specified by @destination
3548 * asynchronously. For details of the behaviour, see g_file_copy().
3550 * If @progress_callback is not %NULL, then that function that will be called
3551 * just like in g_file_copy(). The callback will run in the default main context
3552 * of the thread calling g_file_copy_async() — the same context as @callback is
3553 * run in.
3555 * When the operation is finished, @callback will be called. You can then call
3556 * g_file_copy_finish() to get the result of the operation.
3558 void
3559 g_file_copy_async (GFile *source,
3560 GFile *destination,
3561 GFileCopyFlags flags,
3562 int io_priority,
3563 GCancellable *cancellable,
3564 GFileProgressCallback progress_callback,
3565 gpointer progress_callback_data,
3566 GAsyncReadyCallback callback,
3567 gpointer user_data)
3569 GFileIface *iface;
3571 g_return_if_fail (G_IS_FILE (source));
3572 g_return_if_fail (G_IS_FILE (destination));
3574 iface = G_FILE_GET_IFACE (source);
3575 (* iface->copy_async) (source,
3576 destination,
3577 flags,
3578 io_priority,
3579 cancellable,
3580 progress_callback,
3581 progress_callback_data,
3582 callback,
3583 user_data);
3587 * g_file_copy_finish:
3588 * @file: input #GFile
3589 * @res: a #GAsyncResult
3590 * @error: a #GError, or %NULL
3592 * Finishes copying the file started with g_file_copy_async().
3594 * Returns: a %TRUE on success, %FALSE on error.
3596 gboolean
3597 g_file_copy_finish (GFile *file,
3598 GAsyncResult *res,
3599 GError **error)
3601 GFileIface *iface;
3603 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3604 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3606 if (g_async_result_legacy_propagate_error (res, error))
3607 return FALSE;
3609 iface = G_FILE_GET_IFACE (file);
3610 return (* iface->copy_finish) (file, res, error);
3614 * g_file_move:
3615 * @source: #GFile pointing to the source location
3616 * @destination: #GFile pointing to the destination location
3617 * @flags: set of #GFileCopyFlags
3618 * @cancellable: (nullable): optional #GCancellable object,
3619 * %NULL to ignore
3620 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3621 * function for updates
3622 * @progress_callback_data: (closure): gpointer to user data for
3623 * the callback function
3624 * @error: #GError for returning error conditions, or %NULL
3626 * Tries to move the file or directory @source to the location specified
3627 * by @destination. If native move operations are supported then this is
3628 * used, otherwise a copy + delete fallback is used. The native
3629 * implementation may support moving directories (for instance on moves
3630 * inside the same filesystem), but the fallback code does not.
3632 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3633 * existing @destination file is overwritten.
3635 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3636 * will be copied as symlinks, otherwise the target of the
3637 * @source symlink will be copied.
3639 * If @cancellable is not %NULL, then the operation can be cancelled by
3640 * triggering the cancellable object from another thread. If the operation
3641 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3643 * If @progress_callback is not %NULL, then the operation can be monitored
3644 * by setting this to a #GFileProgressCallback function.
3645 * @progress_callback_data will be passed to this function. It is
3646 * guaranteed that this callback will be called after all data has been
3647 * transferred with the total number of bytes copied during the operation.
3649 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3650 * error is returned, independent on the status of the @destination.
3652 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3653 * then the error %G_IO_ERROR_EXISTS is returned.
3655 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3656 * error is returned. If trying to overwrite a directory with a directory the
3657 * %G_IO_ERROR_WOULD_MERGE error is returned.
3659 * If the source is a directory and the target does not exist, or
3660 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3661 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3662 * move operation isn't available).
3664 * Returns: %TRUE on successful move, %FALSE otherwise.
3666 gboolean
3667 g_file_move (GFile *source,
3668 GFile *destination,
3669 GFileCopyFlags flags,
3670 GCancellable *cancellable,
3671 GFileProgressCallback progress_callback,
3672 gpointer progress_callback_data,
3673 GError **error)
3675 GFileIface *iface;
3676 GError *my_error;
3677 gboolean res;
3679 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3680 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3682 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3683 return FALSE;
3685 iface = G_FILE_GET_IFACE (destination);
3686 if (iface->move)
3688 my_error = NULL;
3689 res = (* iface->move) (source, destination,
3690 flags, cancellable,
3691 progress_callback, progress_callback_data,
3692 &my_error);
3694 if (res)
3695 return TRUE;
3697 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3699 g_propagate_error (error, my_error);
3700 return FALSE;
3702 else
3703 g_clear_error (&my_error);
3706 /* If the types are different, and the destination method failed
3707 * also try the source method
3709 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3711 iface = G_FILE_GET_IFACE (source);
3713 if (iface->move)
3715 my_error = NULL;
3716 res = (* iface->move) (source, destination,
3717 flags, cancellable,
3718 progress_callback, progress_callback_data,
3719 &my_error);
3721 if (res)
3722 return TRUE;
3724 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3726 g_propagate_error (error, my_error);
3727 return FALSE;
3729 else
3730 g_clear_error (&my_error);
3734 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3736 g_set_error_literal (error, G_IO_ERROR,
3737 G_IO_ERROR_NOT_SUPPORTED,
3738 _("Operation not supported"));
3739 return FALSE;
3742 flags |= G_FILE_COPY_ALL_METADATA;
3743 if (!g_file_copy (source, destination, flags, cancellable,
3744 progress_callback, progress_callback_data,
3745 error))
3746 return FALSE;
3748 return g_file_delete (source, cancellable, error);
3752 * g_file_make_directory:
3753 * @file: input #GFile
3754 * @cancellable: (nullable): optional #GCancellable object,
3755 * %NULL to ignore
3756 * @error: a #GError, or %NULL
3758 * Creates a directory. Note that this will only create a child directory
3759 * of the immediate parent directory of the path or URI given by the #GFile.
3760 * To recursively create directories, see g_file_make_directory_with_parents().
3761 * This function will fail if the parent directory does not exist, setting
3762 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3763 * creating directories, this function will fail, setting @error to
3764 * %G_IO_ERROR_NOT_SUPPORTED.
3766 * For a local #GFile the newly created directory will have the default
3767 * (current) ownership and permissions of the current process.
3769 * If @cancellable is not %NULL, then the operation can be cancelled by
3770 * triggering the cancellable object from another thread. If the operation
3771 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3773 * Returns: %TRUE on successful creation, %FALSE otherwise.
3775 gboolean
3776 g_file_make_directory (GFile *file,
3777 GCancellable *cancellable,
3778 GError **error)
3780 GFileIface *iface;
3782 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3784 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3785 return FALSE;
3787 iface = G_FILE_GET_IFACE (file);
3789 if (iface->make_directory == NULL)
3791 g_set_error_literal (error, G_IO_ERROR,
3792 G_IO_ERROR_NOT_SUPPORTED,
3793 _("Operation not supported"));
3794 return FALSE;
3797 return (* iface->make_directory) (file, cancellable, error);
3801 * g_file_make_directory_async:
3802 * @file: input #GFile
3803 * @io_priority: the [I/O priority][io-priority] of the request
3804 * @cancellable: (nullable): optional #GCancellable object,
3805 * %NULL to ignore
3806 * @callback: a #GAsyncReadyCallback to call
3807 * when the request is satisfied
3808 * @user_data: the data to pass to callback function
3810 * Asynchronously creates a directory.
3812 * Virtual: make_directory_async
3813 * Since: 2.38
3815 void
3816 g_file_make_directory_async (GFile *file,
3817 int io_priority,
3818 GCancellable *cancellable,
3819 GAsyncReadyCallback callback,
3820 gpointer user_data)
3822 GFileIface *iface;
3824 g_return_if_fail (G_IS_FILE (file));
3826 iface = G_FILE_GET_IFACE (file);
3827 (* iface->make_directory_async) (file,
3828 io_priority,
3829 cancellable,
3830 callback,
3831 user_data);
3835 * g_file_make_directory_finish:
3836 * @file: input #GFile
3837 * @result: a #GAsyncResult
3838 * @error: a #GError, or %NULL
3840 * Finishes an asynchronous directory creation, started with
3841 * g_file_make_directory_async().
3843 * Virtual: make_directory_finish
3844 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3845 * Since: 2.38
3847 gboolean
3848 g_file_make_directory_finish (GFile *file,
3849 GAsyncResult *result,
3850 GError **error)
3852 GFileIface *iface;
3854 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3855 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3857 iface = G_FILE_GET_IFACE (file);
3858 return (* iface->make_directory_finish) (file, result, error);
3862 * g_file_make_directory_with_parents:
3863 * @file: input #GFile
3864 * @cancellable: (nullable): optional #GCancellable object,
3865 * %NULL to ignore
3866 * @error: a #GError, or %NULL
3868 * Creates a directory and any parent directories that may not
3869 * exist similar to 'mkdir -p'. If the file system does not support
3870 * creating directories, this function will fail, setting @error to
3871 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3872 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3873 * the similar g_mkdir_with_parents().
3875 * For a local #GFile the newly created directories will have the default
3876 * (current) ownership and permissions of the current process.
3878 * If @cancellable is not %NULL, then the operation can be cancelled by
3879 * triggering the cancellable object from another thread. If the operation
3880 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3882 * Returns: %TRUE if all directories have been successfully created, %FALSE
3883 * otherwise.
3885 * Since: 2.18
3887 gboolean
3888 g_file_make_directory_with_parents (GFile *file,
3889 GCancellable *cancellable,
3890 GError **error)
3892 GFile *work_file = NULL;
3893 GList *list = NULL, *l;
3894 GError *my_error = NULL;
3896 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3898 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3899 return FALSE;
3901 /* Try for the simple case of not having to create any parent
3902 * directories. If any parent directory needs to be created, this
3903 * call will fail with NOT_FOUND. If that happens, then that value of
3904 * my_error persists into the while loop below.
3906 g_file_make_directory (file, cancellable, &my_error);
3907 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3909 if (my_error)
3910 g_propagate_error (error, my_error);
3911 return my_error == NULL;
3914 work_file = g_object_ref (file);
3916 /* Creates the parent directories as needed. In case any particular
3917 * creation operation fails for lack of other parent directories
3918 * (NOT_FOUND), the directory is added to a list of directories to
3919 * create later, and the value of my_error is retained until the next
3920 * iteration of the loop. After the loop my_error should either be
3921 * empty or contain a real failure condition.
3923 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3925 GFile *parent_file;
3927 parent_file = g_file_get_parent (work_file);
3928 if (parent_file == NULL)
3929 break;
3931 g_clear_error (&my_error);
3932 g_file_make_directory (parent_file, cancellable, &my_error);
3933 /* Another process may have created the directory in between the
3934 * G_IO_ERROR_NOT_FOUND and now
3936 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3937 g_clear_error (&my_error);
3939 g_object_unref (work_file);
3940 work_file = g_object_ref (parent_file);
3942 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3943 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3944 else
3945 g_object_unref (parent_file);
3948 /* All directories should be able to be created now, so an error at
3949 * this point means the whole operation must fail -- except an EXISTS
3950 * error, which means that another process already created the
3951 * directory in between the previous failure and now.
3953 for (l = list; my_error == NULL && l; l = l->next)
3955 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3956 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3957 g_clear_error (&my_error);
3960 if (work_file)
3961 g_object_unref (work_file);
3963 /* Clean up */
3964 while (list != NULL)
3966 g_object_unref ((GFile *) list->data);
3967 list = g_list_remove (list, list->data);
3970 /* At this point an error in my_error means a that something
3971 * unexpected failed in either of the loops above, so the whole
3972 * operation must fail.
3974 if (my_error != NULL)
3976 g_propagate_error (error, my_error);
3977 return FALSE;
3980 return g_file_make_directory (file, cancellable, error);
3984 * g_file_make_symbolic_link:
3985 * @file: a #GFile with the name of the symlink to create
3986 * @symlink_value: (type filename): a string with the path for the target
3987 * of the new symlink
3988 * @cancellable: (nullable): optional #GCancellable object,
3989 * %NULL to ignore
3990 * @error: a #GError
3992 * Creates a symbolic link named @file which contains the string
3993 * @symlink_value.
3995 * If @cancellable is not %NULL, then the operation can be cancelled by
3996 * triggering the cancellable object from another thread. If the operation
3997 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3999 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
4001 gboolean
4002 g_file_make_symbolic_link (GFile *file,
4003 const char *symlink_value,
4004 GCancellable *cancellable,
4005 GError **error)
4007 GFileIface *iface;
4009 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4010 g_return_val_if_fail (symlink_value != NULL, FALSE);
4012 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4013 return FALSE;
4015 if (*symlink_value == '\0')
4017 g_set_error_literal (error, G_IO_ERROR,
4018 G_IO_ERROR_INVALID_ARGUMENT,
4019 _("Invalid symlink value given"));
4020 return FALSE;
4023 iface = G_FILE_GET_IFACE (file);
4025 if (iface->make_symbolic_link == NULL)
4027 g_set_error_literal (error, G_IO_ERROR,
4028 G_IO_ERROR_NOT_SUPPORTED,
4029 _("Operation not supported"));
4030 return FALSE;
4033 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4037 * g_file_delete:
4038 * @file: input #GFile
4039 * @cancellable: (nullable): optional #GCancellable object,
4040 * %NULL to ignore
4041 * @error: a #GError, or %NULL
4043 * Deletes a file. If the @file is a directory, it will only be
4044 * deleted if it is empty. This has the same semantics as g_unlink().
4046 * If @cancellable is not %NULL, then the operation can be cancelled by
4047 * triggering the cancellable object from another thread. If the operation
4048 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4050 * Virtual: delete_file
4051 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4053 gboolean
4054 g_file_delete (GFile *file,
4055 GCancellable *cancellable,
4056 GError **error)
4058 GFileIface *iface;
4060 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4063 return FALSE;
4065 iface = G_FILE_GET_IFACE (file);
4067 if (iface->delete_file == NULL)
4069 g_set_error_literal (error, G_IO_ERROR,
4070 G_IO_ERROR_NOT_SUPPORTED,
4071 _("Operation not supported"));
4072 return FALSE;
4075 return (* iface->delete_file) (file, cancellable, error);
4079 * g_file_delete_async:
4080 * @file: input #GFile
4081 * @io_priority: the [I/O priority][io-priority] of the request
4082 * @cancellable: (nullable): optional #GCancellable object,
4083 * %NULL to ignore
4084 * @callback: a #GAsyncReadyCallback to call
4085 * when the request is satisfied
4086 * @user_data: the data to pass to callback function
4088 * Asynchronously delete a file. If the @file is a directory, it will
4089 * only be deleted if it is empty. This has the same semantics as
4090 * g_unlink().
4092 * Virtual: delete_file_async
4093 * Since: 2.34
4095 void
4096 g_file_delete_async (GFile *file,
4097 int io_priority,
4098 GCancellable *cancellable,
4099 GAsyncReadyCallback callback,
4100 gpointer user_data)
4102 GFileIface *iface;
4104 g_return_if_fail (G_IS_FILE (file));
4106 iface = G_FILE_GET_IFACE (file);
4107 (* iface->delete_file_async) (file,
4108 io_priority,
4109 cancellable,
4110 callback,
4111 user_data);
4115 * g_file_delete_finish:
4116 * @file: input #GFile
4117 * @result: a #GAsyncResult
4118 * @error: a #GError, or %NULL
4120 * Finishes deleting a file started with g_file_delete_async().
4122 * Virtual: delete_file_finish
4123 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4124 * Since: 2.34
4126 gboolean
4127 g_file_delete_finish (GFile *file,
4128 GAsyncResult *result,
4129 GError **error)
4131 GFileIface *iface;
4133 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4134 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4136 if (g_async_result_legacy_propagate_error (result, error))
4137 return FALSE;
4139 iface = G_FILE_GET_IFACE (file);
4140 return (* iface->delete_file_finish) (file, result, error);
4144 * g_file_trash:
4145 * @file: #GFile to send to trash
4146 * @cancellable: (nullable): optional #GCancellable object,
4147 * %NULL to ignore
4148 * @error: a #GError, or %NULL
4150 * Sends @file to the "Trashcan", if possible. This is similar to
4151 * deleting it, but the user can recover it before emptying the trashcan.
4152 * Not all file systems support trashing, so this call can return the
4153 * %G_IO_ERROR_NOT_SUPPORTED error.
4155 * If @cancellable is not %NULL, then the operation can be cancelled by
4156 * triggering the cancellable object from another thread. If the operation
4157 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4159 * Virtual: trash
4160 * Returns: %TRUE on successful trash, %FALSE otherwise.
4162 gboolean
4163 g_file_trash (GFile *file,
4164 GCancellable *cancellable,
4165 GError **error)
4167 GFileIface *iface;
4169 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4171 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4172 return FALSE;
4174 iface = G_FILE_GET_IFACE (file);
4176 if (iface->trash == NULL)
4178 g_set_error_literal (error,
4179 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4180 _("Trash not supported"));
4181 return FALSE;
4184 return (* iface->trash) (file, cancellable, error);
4188 * g_file_trash_async:
4189 * @file: input #GFile
4190 * @io_priority: the [I/O priority][io-priority] of the request
4191 * @cancellable: (nullable): optional #GCancellable object,
4192 * %NULL to ignore
4193 * @callback: a #GAsyncReadyCallback to call
4194 * when the request is satisfied
4195 * @user_data: the data to pass to callback function
4197 * Asynchronously sends @file to the Trash location, if possible.
4199 * Virtual: trash_async
4200 * Since: 2.38
4202 void
4203 g_file_trash_async (GFile *file,
4204 int io_priority,
4205 GCancellable *cancellable,
4206 GAsyncReadyCallback callback,
4207 gpointer user_data)
4209 GFileIface *iface;
4211 g_return_if_fail (G_IS_FILE (file));
4213 iface = G_FILE_GET_IFACE (file);
4214 (* iface->trash_async) (file,
4215 io_priority,
4216 cancellable,
4217 callback,
4218 user_data);
4222 * g_file_trash_finish:
4223 * @file: input #GFile
4224 * @result: a #GAsyncResult
4225 * @error: a #GError, or %NULL
4227 * Finishes an asynchronous file trashing operation, started with
4228 * g_file_trash_async().
4230 * Virtual: trash_finish
4231 * Returns: %TRUE on successful trash, %FALSE otherwise.
4232 * Since: 2.38
4234 gboolean
4235 g_file_trash_finish (GFile *file,
4236 GAsyncResult *result,
4237 GError **error)
4239 GFileIface *iface;
4241 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4242 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4244 iface = G_FILE_GET_IFACE (file);
4245 return (* iface->trash_finish) (file, result, error);
4249 * g_file_set_display_name:
4250 * @file: input #GFile
4251 * @display_name: a string
4252 * @cancellable: (nullable): optional #GCancellable object,
4253 * %NULL to ignore
4254 * @error: a #GError, or %NULL
4256 * Renames @file to the specified display name.
4258 * The display name is converted from UTF-8 to the correct encoding
4259 * for the target filesystem if possible and the @file is renamed to this.
4261 * If you want to implement a rename operation in the user interface the
4262 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4263 * initial value in the rename widget, and then the result after editing
4264 * should be passed to g_file_set_display_name().
4266 * On success the resulting converted filename is returned.
4268 * If @cancellable is not %NULL, then the operation can be cancelled by
4269 * triggering the cancellable object from another thread. If the operation
4270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4272 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4273 * or %NULL if there was an error.
4274 * Free the returned object with g_object_unref().
4276 GFile *
4277 g_file_set_display_name (GFile *file,
4278 const gchar *display_name,
4279 GCancellable *cancellable,
4280 GError **error)
4282 GFileIface *iface;
4284 g_return_val_if_fail (G_IS_FILE (file), NULL);
4285 g_return_val_if_fail (display_name != NULL, NULL);
4287 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4289 g_set_error (error,
4290 G_IO_ERROR,
4291 G_IO_ERROR_INVALID_ARGUMENT,
4292 _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4293 return NULL;
4296 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4297 return NULL;
4299 iface = G_FILE_GET_IFACE (file);
4301 return (* iface->set_display_name) (file, display_name, cancellable, error);
4305 * g_file_set_display_name_async:
4306 * @file: input #GFile
4307 * @display_name: a string
4308 * @io_priority: the [I/O priority][io-priority] of the request
4309 * @cancellable: (nullable): optional #GCancellable object,
4310 * %NULL to ignore
4311 * @callback: (scope async): a #GAsyncReadyCallback to call
4312 * when the request is satisfied
4313 * @user_data: (closure): the data to pass to callback function
4315 * Asynchronously sets the display name for a given #GFile.
4317 * For more details, see g_file_set_display_name() which is
4318 * the synchronous version of this call.
4320 * When the operation is finished, @callback will be called.
4321 * You can then call g_file_set_display_name_finish() to get
4322 * the result of the operation.
4324 void
4325 g_file_set_display_name_async (GFile *file,
4326 const gchar *display_name,
4327 gint io_priority,
4328 GCancellable *cancellable,
4329 GAsyncReadyCallback callback,
4330 gpointer user_data)
4332 GFileIface *iface;
4334 g_return_if_fail (G_IS_FILE (file));
4335 g_return_if_fail (display_name != NULL);
4337 iface = G_FILE_GET_IFACE (file);
4338 (* iface->set_display_name_async) (file,
4339 display_name,
4340 io_priority,
4341 cancellable,
4342 callback,
4343 user_data);
4347 * g_file_set_display_name_finish:
4348 * @file: input #GFile
4349 * @res: a #GAsyncResult
4350 * @error: a #GError, or %NULL
4352 * Finishes setting a display name started with
4353 * g_file_set_display_name_async().
4355 * Returns: (transfer full): a #GFile or %NULL on error.
4356 * Free the returned object with g_object_unref().
4358 GFile *
4359 g_file_set_display_name_finish (GFile *file,
4360 GAsyncResult *res,
4361 GError **error)
4363 GFileIface *iface;
4365 g_return_val_if_fail (G_IS_FILE (file), NULL);
4366 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4368 if (g_async_result_legacy_propagate_error (res, error))
4369 return NULL;
4371 iface = G_FILE_GET_IFACE (file);
4372 return (* iface->set_display_name_finish) (file, res, error);
4376 * g_file_query_settable_attributes:
4377 * @file: input #GFile
4378 * @cancellable: (nullable): optional #GCancellable object,
4379 * %NULL to ignore
4380 * @error: a #GError, or %NULL
4382 * Obtain the list of settable attributes for the file.
4384 * Returns the type and full attribute name of all the attributes
4385 * that can be set on this file. This doesn't mean setting it will
4386 * always succeed though, you might get an access failure, or some
4387 * specific file may not support a specific attribute.
4389 * If @cancellable is not %NULL, then the operation can be cancelled by
4390 * triggering the cancellable object from another thread. If the operation
4391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4393 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4394 * When you are done with it, release it with
4395 * g_file_attribute_info_list_unref()
4397 GFileAttributeInfoList *
4398 g_file_query_settable_attributes (GFile *file,
4399 GCancellable *cancellable,
4400 GError **error)
4402 GFileIface *iface;
4403 GError *my_error;
4404 GFileAttributeInfoList *list;
4406 g_return_val_if_fail (G_IS_FILE (file), NULL);
4408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4409 return NULL;
4411 iface = G_FILE_GET_IFACE (file);
4413 if (iface->query_settable_attributes == NULL)
4414 return g_file_attribute_info_list_new ();
4416 my_error = NULL;
4417 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4419 if (list == NULL)
4421 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4423 list = g_file_attribute_info_list_new ();
4424 g_error_free (my_error);
4426 else
4427 g_propagate_error (error, my_error);
4430 return list;
4434 * g_file_query_writable_namespaces:
4435 * @file: input #GFile
4436 * @cancellable: (nullable): optional #GCancellable object,
4437 * %NULL to ignore
4438 * @error: a #GError, or %NULL
4440 * Obtain the list of attribute namespaces where new attributes
4441 * can be created by a user. An example of this is extended
4442 * attributes (in the "xattr" namespace).
4444 * If @cancellable is not %NULL, then the operation can be cancelled by
4445 * triggering the cancellable object from another thread. If the operation
4446 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4448 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4449 * When you are done with it, release it with
4450 * g_file_attribute_info_list_unref()
4452 GFileAttributeInfoList *
4453 g_file_query_writable_namespaces (GFile *file,
4454 GCancellable *cancellable,
4455 GError **error)
4457 GFileIface *iface;
4458 GError *my_error;
4459 GFileAttributeInfoList *list;
4461 g_return_val_if_fail (G_IS_FILE (file), NULL);
4463 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4464 return NULL;
4466 iface = G_FILE_GET_IFACE (file);
4468 if (iface->query_writable_namespaces == NULL)
4469 return g_file_attribute_info_list_new ();
4471 my_error = NULL;
4472 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4474 if (list == NULL)
4476 g_warn_if_reached();
4477 list = g_file_attribute_info_list_new ();
4480 if (my_error != NULL)
4482 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4484 g_error_free (my_error);
4486 else
4487 g_propagate_error (error, my_error);
4490 return list;
4494 * g_file_set_attribute:
4495 * @file: input #GFile
4496 * @attribute: a string containing the attribute's name
4497 * @type: The type of the attribute
4498 * @value_p: (nullable): a pointer to the value (or the pointer
4499 * itself if the type is a pointer type)
4500 * @flags: a set of #GFileQueryInfoFlags
4501 * @cancellable: (nullable): optional #GCancellable object,
4502 * %NULL to ignore
4503 * @error: a #GError, or %NULL
4505 * Sets an attribute in the file with attribute name @attribute to @value.
4507 * Some attributes can be unset by setting @type to
4508 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4510 * If @cancellable is not %NULL, then the operation can be cancelled by
4511 * triggering the cancellable object from another thread. If the operation
4512 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4514 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4516 gboolean
4517 g_file_set_attribute (GFile *file,
4518 const gchar *attribute,
4519 GFileAttributeType type,
4520 gpointer value_p,
4521 GFileQueryInfoFlags flags,
4522 GCancellable *cancellable,
4523 GError **error)
4525 GFileIface *iface;
4527 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4528 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4530 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4531 return FALSE;
4533 iface = G_FILE_GET_IFACE (file);
4535 if (iface->set_attribute == NULL)
4537 g_set_error_literal (error, G_IO_ERROR,
4538 G_IO_ERROR_NOT_SUPPORTED,
4539 _("Operation not supported"));
4540 return FALSE;
4543 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4547 * g_file_set_attributes_from_info:
4548 * @file: input #GFile
4549 * @info: a #GFileInfo
4550 * @flags: #GFileQueryInfoFlags
4551 * @cancellable: (nullable): optional #GCancellable object,
4552 * %NULL to ignore
4553 * @error: a #GError, or %NULL
4555 * Tries to set all attributes in the #GFileInfo on the target
4556 * values, not stopping on the first error.
4558 * If there is any error during this operation then @error will
4559 * be set to the first error. Error on particular fields are flagged
4560 * by setting the "status" field in the attribute value to
4561 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4562 * also detect further errors.
4564 * If @cancellable is not %NULL, then the operation can be cancelled by
4565 * triggering the cancellable object from another thread. If the operation
4566 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4568 * Returns: %FALSE if there was any error, %TRUE otherwise.
4570 gboolean
4571 g_file_set_attributes_from_info (GFile *file,
4572 GFileInfo *info,
4573 GFileQueryInfoFlags flags,
4574 GCancellable *cancellable,
4575 GError **error)
4577 GFileIface *iface;
4579 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4580 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4582 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4583 return FALSE;
4585 g_file_info_clear_status (info);
4587 iface = G_FILE_GET_IFACE (file);
4589 return (* iface->set_attributes_from_info) (file,
4590 info,
4591 flags,
4592 cancellable,
4593 error);
4596 static gboolean
4597 g_file_real_set_attributes_from_info (GFile *file,
4598 GFileInfo *info,
4599 GFileQueryInfoFlags flags,
4600 GCancellable *cancellable,
4601 GError **error)
4603 char **attributes;
4604 int i;
4605 gboolean res;
4606 GFileAttributeValue *value;
4608 res = TRUE;
4610 attributes = g_file_info_list_attributes (info, NULL);
4612 for (i = 0; attributes[i] != NULL; i++)
4614 value = _g_file_info_get_attribute_value (info, attributes[i]);
4616 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4617 continue;
4619 if (!g_file_set_attribute (file, attributes[i],
4620 value->type, _g_file_attribute_value_peek_as_pointer (value),
4621 flags, cancellable, error))
4623 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4624 res = FALSE;
4625 /* Don't set error multiple times */
4626 error = NULL;
4628 else
4629 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4632 g_strfreev (attributes);
4634 return res;
4638 * g_file_set_attributes_async:
4639 * @file: input #GFile
4640 * @info: a #GFileInfo
4641 * @flags: a #GFileQueryInfoFlags
4642 * @io_priority: the [I/O priority][io-priority] of the request
4643 * @cancellable: (nullable): optional #GCancellable object,
4644 * %NULL to ignore
4645 * @callback: (scope async): a #GAsyncReadyCallback
4646 * @user_data: (closure): a #gpointer
4648 * Asynchronously sets the attributes of @file with @info.
4650 * For more details, see g_file_set_attributes_from_info(),
4651 * which is the synchronous version of this call.
4653 * When the operation is finished, @callback will be called.
4654 * You can then call g_file_set_attributes_finish() to get
4655 * the result of the operation.
4657 void
4658 g_file_set_attributes_async (GFile *file,
4659 GFileInfo *info,
4660 GFileQueryInfoFlags flags,
4661 int io_priority,
4662 GCancellable *cancellable,
4663 GAsyncReadyCallback callback,
4664 gpointer user_data)
4666 GFileIface *iface;
4668 g_return_if_fail (G_IS_FILE (file));
4669 g_return_if_fail (G_IS_FILE_INFO (info));
4671 iface = G_FILE_GET_IFACE (file);
4672 (* iface->set_attributes_async) (file,
4673 info,
4674 flags,
4675 io_priority,
4676 cancellable,
4677 callback,
4678 user_data);
4682 * g_file_set_attributes_finish:
4683 * @file: input #GFile
4684 * @result: a #GAsyncResult
4685 * @info: (out) (transfer full): a #GFileInfo
4686 * @error: a #GError, or %NULL
4688 * Finishes setting an attribute started in g_file_set_attributes_async().
4690 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4692 gboolean
4693 g_file_set_attributes_finish (GFile *file,
4694 GAsyncResult *result,
4695 GFileInfo **info,
4696 GError **error)
4698 GFileIface *iface;
4700 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4701 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4703 /* No standard handling of errors here, as we must set info even
4704 * on errors
4706 iface = G_FILE_GET_IFACE (file);
4707 return (* iface->set_attributes_finish) (file, result, info, error);
4711 * g_file_set_attribute_string:
4712 * @file: input #GFile
4713 * @attribute: a string containing the attribute's name
4714 * @value: a string containing the attribute's value
4715 * @flags: #GFileQueryInfoFlags
4716 * @cancellable: (nullable): optional #GCancellable object,
4717 * %NULL to ignore
4718 * @error: a #GError, or %NULL
4720 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4721 * If @attribute is of a different type, this operation will fail.
4723 * If @cancellable is not %NULL, then the operation can be cancelled by
4724 * triggering the cancellable object from another thread. If the operation
4725 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4727 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4729 gboolean
4730 g_file_set_attribute_string (GFile *file,
4731 const char *attribute,
4732 const char *value,
4733 GFileQueryInfoFlags flags,
4734 GCancellable *cancellable,
4735 GError **error)
4737 return g_file_set_attribute (file, attribute,
4738 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4739 flags, cancellable, error);
4743 * g_file_set_attribute_byte_string:
4744 * @file: input #GFile
4745 * @attribute: a string containing the attribute's name
4746 * @value: a string containing the attribute's new value
4747 * @flags: a #GFileQueryInfoFlags
4748 * @cancellable: (nullable): optional #GCancellable object,
4749 * %NULL to ignore
4750 * @error: a #GError, or %NULL
4752 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4753 * If @attribute is of a different type, this operation will fail,
4754 * returning %FALSE.
4756 * If @cancellable is not %NULL, then the operation can be cancelled by
4757 * triggering the cancellable object from another thread. If the operation
4758 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4760 * Returns: %TRUE if the @attribute was successfully set to @value
4761 * in the @file, %FALSE otherwise.
4763 gboolean
4764 g_file_set_attribute_byte_string (GFile *file,
4765 const gchar *attribute,
4766 const gchar *value,
4767 GFileQueryInfoFlags flags,
4768 GCancellable *cancellable,
4769 GError **error)
4771 return g_file_set_attribute (file, attribute,
4772 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4773 flags, cancellable, error);
4777 * g_file_set_attribute_uint32:
4778 * @file: input #GFile
4779 * @attribute: a string containing the attribute's name
4780 * @value: a #guint32 containing the attribute's new value
4781 * @flags: a #GFileQueryInfoFlags
4782 * @cancellable: (nullable): optional #GCancellable object,
4783 * %NULL to ignore
4784 * @error: a #GError, or %NULL
4786 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4787 * If @attribute is of a different type, this operation will fail.
4789 * If @cancellable is not %NULL, then the operation can be cancelled by
4790 * triggering the cancellable object from another thread. If the operation
4791 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4793 * Returns: %TRUE if the @attribute was successfully set to @value
4794 * in the @file, %FALSE otherwise.
4796 gboolean
4797 g_file_set_attribute_uint32 (GFile *file,
4798 const gchar *attribute,
4799 guint32 value,
4800 GFileQueryInfoFlags flags,
4801 GCancellable *cancellable,
4802 GError **error)
4804 return g_file_set_attribute (file, attribute,
4805 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4806 flags, cancellable, error);
4810 * g_file_set_attribute_int32:
4811 * @file: input #GFile
4812 * @attribute: a string containing the attribute's name
4813 * @value: a #gint32 containing the attribute's new value
4814 * @flags: a #GFileQueryInfoFlags
4815 * @cancellable: (nullable): optional #GCancellable object,
4816 * %NULL to ignore
4817 * @error: a #GError, or %NULL
4819 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4820 * If @attribute is of a different type, this operation will fail.
4822 * If @cancellable is not %NULL, then the operation can be cancelled by
4823 * triggering the cancellable object from another thread. If the operation
4824 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4826 * Returns: %TRUE if the @attribute was successfully set to @value
4827 * in the @file, %FALSE otherwise.
4829 gboolean
4830 g_file_set_attribute_int32 (GFile *file,
4831 const gchar *attribute,
4832 gint32 value,
4833 GFileQueryInfoFlags flags,
4834 GCancellable *cancellable,
4835 GError **error)
4837 return g_file_set_attribute (file, attribute,
4838 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4839 flags, cancellable, error);
4843 * g_file_set_attribute_uint64:
4844 * @file: input #GFile
4845 * @attribute: a string containing the attribute's name
4846 * @value: a #guint64 containing the attribute's new value
4847 * @flags: a #GFileQueryInfoFlags
4848 * @cancellable: (nullable): optional #GCancellable object,
4849 * %NULL to ignore
4850 * @error: a #GError, or %NULL
4852 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4853 * If @attribute is of a different type, this operation will fail.
4855 * If @cancellable is not %NULL, then the operation can be cancelled by
4856 * triggering the cancellable object from another thread. If the operation
4857 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4859 * Returns: %TRUE if the @attribute was successfully set to @value
4860 * in the @file, %FALSE otherwise.
4862 gboolean
4863 g_file_set_attribute_uint64 (GFile *file,
4864 const gchar *attribute,
4865 guint64 value,
4866 GFileQueryInfoFlags flags,
4867 GCancellable *cancellable,
4868 GError **error)
4870 return g_file_set_attribute (file, attribute,
4871 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4872 flags, cancellable, error);
4876 * g_file_set_attribute_int64:
4877 * @file: input #GFile
4878 * @attribute: a string containing the attribute's name
4879 * @value: a #guint64 containing the attribute's new value
4880 * @flags: a #GFileQueryInfoFlags
4881 * @cancellable: (nullable): optional #GCancellable object,
4882 * %NULL to ignore
4883 * @error: a #GError, or %NULL
4885 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4886 * If @attribute is of a different type, this operation will fail.
4888 * If @cancellable is not %NULL, then the operation can be cancelled by
4889 * triggering the cancellable object from another thread. If the operation
4890 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4892 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4894 gboolean
4895 g_file_set_attribute_int64 (GFile *file,
4896 const gchar *attribute,
4897 gint64 value,
4898 GFileQueryInfoFlags flags,
4899 GCancellable *cancellable,
4900 GError **error)
4902 return g_file_set_attribute (file, attribute,
4903 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4904 flags, cancellable, error);
4908 * g_file_mount_mountable:
4909 * @file: input #GFile
4910 * @flags: flags affecting the operation
4911 * @mount_operation: (nullable): a #GMountOperation,
4912 * or %NULL to avoid user interaction
4913 * @cancellable: (nullable): optional #GCancellable object,
4914 * %NULL to ignore
4915 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4916 * when the request is satisfied, or %NULL
4917 * @user_data: (closure): the data to pass to callback function
4919 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4920 * Using @mount_operation, you can request callbacks when, for instance,
4921 * passwords are needed during authentication.
4923 * If @cancellable is not %NULL, then the operation can be cancelled by
4924 * triggering the cancellable object from another thread. If the operation
4925 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4927 * When the operation is finished, @callback will be called.
4928 * You can then call g_file_mount_mountable_finish() to get
4929 * the result of the operation.
4931 void
4932 g_file_mount_mountable (GFile *file,
4933 GMountMountFlags flags,
4934 GMountOperation *mount_operation,
4935 GCancellable *cancellable,
4936 GAsyncReadyCallback callback,
4937 gpointer user_data)
4939 GFileIface *iface;
4941 g_return_if_fail (G_IS_FILE (file));
4943 iface = G_FILE_GET_IFACE (file);
4945 if (iface->mount_mountable == NULL)
4947 g_task_report_new_error (file, callback, user_data,
4948 g_file_mount_mountable,
4949 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4950 _("Operation not supported"));
4951 return;
4954 (* iface->mount_mountable) (file,
4955 flags,
4956 mount_operation,
4957 cancellable,
4958 callback,
4959 user_data);
4963 * g_file_mount_mountable_finish:
4964 * @file: input #GFile
4965 * @result: a #GAsyncResult
4966 * @error: a #GError, or %NULL
4968 * Finishes a mount operation. See g_file_mount_mountable() for details.
4970 * Finish an asynchronous mount operation that was started
4971 * with g_file_mount_mountable().
4973 * Returns: (transfer full): a #GFile or %NULL on error.
4974 * Free the returned object with g_object_unref().
4976 GFile *
4977 g_file_mount_mountable_finish (GFile *file,
4978 GAsyncResult *result,
4979 GError **error)
4981 GFileIface *iface;
4983 g_return_val_if_fail (G_IS_FILE (file), NULL);
4984 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4986 if (g_async_result_legacy_propagate_error (result, error))
4987 return NULL;
4988 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4989 return g_task_propagate_pointer (G_TASK (result), error);
4991 iface = G_FILE_GET_IFACE (file);
4992 return (* iface->mount_mountable_finish) (file, result, error);
4996 * g_file_unmount_mountable:
4997 * @file: input #GFile
4998 * @flags: flags affecting the operation
4999 * @cancellable: (nullable): optional #GCancellable object,
5000 * %NULL to ignore
5001 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5002 * when the request is satisfied, or %NULL
5003 * @user_data: (closure): the data to pass to callback function
5005 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5007 * If @cancellable is not %NULL, then the operation can be cancelled by
5008 * triggering the cancellable object from another thread. If the operation
5009 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5011 * When the operation is finished, @callback will be called.
5012 * You can then call g_file_unmount_mountable_finish() to get
5013 * the result of the operation.
5015 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5017 void
5018 g_file_unmount_mountable (GFile *file,
5019 GMountUnmountFlags flags,
5020 GCancellable *cancellable,
5021 GAsyncReadyCallback callback,
5022 gpointer user_data)
5024 GFileIface *iface;
5026 g_return_if_fail (G_IS_FILE (file));
5028 iface = G_FILE_GET_IFACE (file);
5030 if (iface->unmount_mountable == NULL)
5032 g_task_report_new_error (file, callback, user_data,
5033 g_file_unmount_mountable_with_operation,
5034 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5035 _("Operation not supported"));
5036 return;
5039 (* iface->unmount_mountable) (file,
5040 flags,
5041 cancellable,
5042 callback,
5043 user_data);
5047 * g_file_unmount_mountable_finish:
5048 * @file: input #GFile
5049 * @result: a #GAsyncResult
5050 * @error: a #GError, or %NULL
5052 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5054 * Finish an asynchronous unmount operation that was started
5055 * with g_file_unmount_mountable().
5057 * Returns: %TRUE if the operation finished successfully.
5058 * %FALSE otherwise.
5060 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5061 * instead.
5063 gboolean
5064 g_file_unmount_mountable_finish (GFile *file,
5065 GAsyncResult *result,
5066 GError **error)
5068 GFileIface *iface;
5070 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5071 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5073 if (g_async_result_legacy_propagate_error (result, error))
5074 return FALSE;
5075 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5076 return g_task_propagate_boolean (G_TASK (result), error);
5078 iface = G_FILE_GET_IFACE (file);
5079 return (* iface->unmount_mountable_finish) (file, result, error);
5083 * g_file_unmount_mountable_with_operation:
5084 * @file: input #GFile
5085 * @flags: flags affecting the operation
5086 * @mount_operation: (nullable): a #GMountOperation,
5087 * or %NULL to avoid user interaction
5088 * @cancellable: (nullable): optional #GCancellable object,
5089 * %NULL to ignore
5090 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5091 * when the request is satisfied, or %NULL
5092 * @user_data: (closure): the data to pass to callback function
5094 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5096 * If @cancellable is not %NULL, then the operation can be cancelled by
5097 * triggering the cancellable object from another thread. If the operation
5098 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5100 * When the operation is finished, @callback will be called.
5101 * You can then call g_file_unmount_mountable_finish() to get
5102 * the result of the operation.
5104 * Since: 2.22
5106 void
5107 g_file_unmount_mountable_with_operation (GFile *file,
5108 GMountUnmountFlags flags,
5109 GMountOperation *mount_operation,
5110 GCancellable *cancellable,
5111 GAsyncReadyCallback callback,
5112 gpointer user_data)
5114 GFileIface *iface;
5116 g_return_if_fail (G_IS_FILE (file));
5118 iface = G_FILE_GET_IFACE (file);
5120 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5122 g_task_report_new_error (file, callback, user_data,
5123 g_file_unmount_mountable_with_operation,
5124 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5125 _("Operation not supported"));
5126 return;
5129 if (iface->unmount_mountable_with_operation != NULL)
5130 (* iface->unmount_mountable_with_operation) (file,
5131 flags,
5132 mount_operation,
5133 cancellable,
5134 callback,
5135 user_data);
5136 else
5137 (* iface->unmount_mountable) (file,
5138 flags,
5139 cancellable,
5140 callback,
5141 user_data);
5145 * g_file_unmount_mountable_with_operation_finish:
5146 * @file: input #GFile
5147 * @result: a #GAsyncResult
5148 * @error: a #GError, or %NULL
5150 * Finishes an unmount operation,
5151 * see g_file_unmount_mountable_with_operation() for details.
5153 * Finish an asynchronous unmount operation that was started
5154 * with g_file_unmount_mountable_with_operation().
5156 * Returns: %TRUE if the operation finished successfully.
5157 * %FALSE otherwise.
5159 * Since: 2.22
5161 gboolean
5162 g_file_unmount_mountable_with_operation_finish (GFile *file,
5163 GAsyncResult *result,
5164 GError **error)
5166 GFileIface *iface;
5168 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5169 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5171 if (g_async_result_legacy_propagate_error (result, error))
5172 return FALSE;
5173 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5174 return g_task_propagate_boolean (G_TASK (result), error);
5176 iface = G_FILE_GET_IFACE (file);
5177 if (iface->unmount_mountable_with_operation_finish != NULL)
5178 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5179 else
5180 return (* iface->unmount_mountable_finish) (file, result, error);
5184 * g_file_eject_mountable:
5185 * @file: input #GFile
5186 * @flags: flags affecting the operation
5187 * @cancellable: (nullable): optional #GCancellable object,
5188 * %NULL to ignore
5189 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5190 * when the request is satisfied, or %NULL
5191 * @user_data: (closure): the data to pass to callback function
5193 * Starts an asynchronous eject on a mountable.
5194 * When this operation has completed, @callback will be called with
5195 * @user_user data, and the operation can be finalized with
5196 * g_file_eject_mountable_finish().
5198 * If @cancellable is not %NULL, then the operation can be cancelled by
5199 * triggering the cancellable object from another thread. If the operation
5200 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5202 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5204 void
5205 g_file_eject_mountable (GFile *file,
5206 GMountUnmountFlags flags,
5207 GCancellable *cancellable,
5208 GAsyncReadyCallback callback,
5209 gpointer user_data)
5211 GFileIface *iface;
5213 g_return_if_fail (G_IS_FILE (file));
5215 iface = G_FILE_GET_IFACE (file);
5217 if (iface->eject_mountable == NULL)
5219 g_task_report_new_error (file, callback, user_data,
5220 g_file_eject_mountable_with_operation,
5221 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5222 _("Operation not supported"));
5223 return;
5226 (* iface->eject_mountable) (file,
5227 flags,
5228 cancellable,
5229 callback,
5230 user_data);
5234 * g_file_eject_mountable_finish:
5235 * @file: input #GFile
5236 * @result: a #GAsyncResult
5237 * @error: a #GError, or %NULL
5239 * Finishes an asynchronous eject operation started by
5240 * g_file_eject_mountable().
5242 * Returns: %TRUE if the @file was ejected successfully.
5243 * %FALSE otherwise.
5245 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5246 * instead.
5248 gboolean
5249 g_file_eject_mountable_finish (GFile *file,
5250 GAsyncResult *result,
5251 GError **error)
5253 GFileIface *iface;
5255 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5256 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5258 if (g_async_result_legacy_propagate_error (result, error))
5259 return FALSE;
5260 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5261 return g_task_propagate_boolean (G_TASK (result), error);
5263 iface = G_FILE_GET_IFACE (file);
5264 return (* iface->eject_mountable_finish) (file, result, error);
5268 * g_file_eject_mountable_with_operation:
5269 * @file: input #GFile
5270 * @flags: flags affecting the operation
5271 * @mount_operation: (nullable): a #GMountOperation,
5272 * or %NULL to avoid user interaction
5273 * @cancellable: (nullable): optional #GCancellable object,
5274 * %NULL to ignore
5275 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5276 * when the request is satisfied, or %NULL
5277 * @user_data: (closure): the data to pass to callback function
5279 * Starts an asynchronous eject on a mountable.
5280 * When this operation has completed, @callback will be called with
5281 * @user_user data, and the operation can be finalized with
5282 * g_file_eject_mountable_with_operation_finish().
5284 * If @cancellable is not %NULL, then the operation can be cancelled by
5285 * triggering the cancellable object from another thread. If the operation
5286 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5288 * Since: 2.22
5290 void
5291 g_file_eject_mountable_with_operation (GFile *file,
5292 GMountUnmountFlags flags,
5293 GMountOperation *mount_operation,
5294 GCancellable *cancellable,
5295 GAsyncReadyCallback callback,
5296 gpointer user_data)
5298 GFileIface *iface;
5300 g_return_if_fail (G_IS_FILE (file));
5302 iface = G_FILE_GET_IFACE (file);
5304 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5306 g_task_report_new_error (file, callback, user_data,
5307 g_file_eject_mountable_with_operation,
5308 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5309 _("Operation not supported"));
5310 return;
5313 if (iface->eject_mountable_with_operation != NULL)
5314 (* iface->eject_mountable_with_operation) (file,
5315 flags,
5316 mount_operation,
5317 cancellable,
5318 callback,
5319 user_data);
5320 else
5321 (* iface->eject_mountable) (file,
5322 flags,
5323 cancellable,
5324 callback,
5325 user_data);
5329 * g_file_eject_mountable_with_operation_finish:
5330 * @file: input #GFile
5331 * @result: a #GAsyncResult
5332 * @error: a #GError, or %NULL
5334 * Finishes an asynchronous eject operation started by
5335 * g_file_eject_mountable_with_operation().
5337 * Returns: %TRUE if the @file was ejected successfully.
5338 * %FALSE otherwise.
5340 * Since: 2.22
5342 gboolean
5343 g_file_eject_mountable_with_operation_finish (GFile *file,
5344 GAsyncResult *result,
5345 GError **error)
5347 GFileIface *iface;
5349 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5350 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5352 if (g_async_result_legacy_propagate_error (result, error))
5353 return FALSE;
5354 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5355 return g_task_propagate_boolean (G_TASK (result), error);
5357 iface = G_FILE_GET_IFACE (file);
5358 if (iface->eject_mountable_with_operation_finish != NULL)
5359 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5360 else
5361 return (* iface->eject_mountable_finish) (file, result, error);
5365 * g_file_monitor_directory:
5366 * @file: input #GFile
5367 * @flags: a set of #GFileMonitorFlags
5368 * @cancellable: (nullable): optional #GCancellable object,
5369 * %NULL to ignore
5370 * @error: a #GError, or %NULL
5372 * Obtains a directory monitor for the given file.
5373 * This may fail if directory monitoring is not supported.
5375 * If @cancellable is not %NULL, then the operation can be cancelled by
5376 * triggering the cancellable object from another thread. If the operation
5377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5379 * It does not make sense for @flags to contain
5380 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5381 * directories. It is not possible to monitor all the files in a
5382 * directory for changes made via hard links; if you want to do this then
5383 * you must register individual watches with g_file_monitor().
5385 * Virtual: monitor_dir
5386 * Returns: (transfer full): a #GFileMonitor for the given @file,
5387 * or %NULL on error.
5388 * Free the returned object with g_object_unref().
5390 GFileMonitor *
5391 g_file_monitor_directory (GFile *file,
5392 GFileMonitorFlags flags,
5393 GCancellable *cancellable,
5394 GError **error)
5396 GFileIface *iface;
5398 g_return_val_if_fail (G_IS_FILE (file), NULL);
5399 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5401 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5402 return NULL;
5404 iface = G_FILE_GET_IFACE (file);
5406 if (iface->monitor_dir == NULL)
5408 g_set_error_literal (error, G_IO_ERROR,
5409 G_IO_ERROR_NOT_SUPPORTED,
5410 _("Operation not supported"));
5411 return NULL;
5414 return (* iface->monitor_dir) (file, flags, cancellable, error);
5418 * g_file_monitor_file:
5419 * @file: input #GFile
5420 * @flags: a set of #GFileMonitorFlags
5421 * @cancellable: (nullable): optional #GCancellable object,
5422 * %NULL to ignore
5423 * @error: a #GError, or %NULL
5425 * Obtains a file monitor for the given file. If no file notification
5426 * mechanism exists, then regular polling of the file is used.
5428 * If @cancellable is not %NULL, then the operation can be cancelled by
5429 * triggering the cancellable object from another thread. If the operation
5430 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5432 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5433 * will also attempt to report changes made to the file via another
5434 * filename (ie, a hard link). Without this flag, you can only rely on
5435 * changes made through the filename contained in @file to be
5436 * reported. Using this flag may result in an increase in resource
5437 * usage, and may not have any effect depending on the #GFileMonitor
5438 * backend and/or filesystem type.
5440 * Returns: (transfer full): a #GFileMonitor for the given @file,
5441 * or %NULL on error.
5442 * Free the returned object with g_object_unref().
5444 GFileMonitor *
5445 g_file_monitor_file (GFile *file,
5446 GFileMonitorFlags flags,
5447 GCancellable *cancellable,
5448 GError **error)
5450 GFileIface *iface;
5451 GFileMonitor *monitor;
5453 g_return_val_if_fail (G_IS_FILE (file), NULL);
5455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5456 return NULL;
5458 iface = G_FILE_GET_IFACE (file);
5460 monitor = NULL;
5462 if (iface->monitor_file)
5463 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5465 /* Fallback to polling */
5466 if (monitor == NULL)
5467 monitor = _g_poll_file_monitor_new (file);
5469 return monitor;
5473 * g_file_monitor:
5474 * @file: input #GFile
5475 * @flags: a set of #GFileMonitorFlags
5476 * @cancellable: (nullable): optional #GCancellable object,
5477 * %NULL to ignore
5478 * @error: a #GError, or %NULL
5480 * Obtains a file or directory monitor for the given file,
5481 * depending on the type of the file.
5483 * If @cancellable is not %NULL, then the operation can be cancelled by
5484 * triggering the cancellable object from another thread. If the operation
5485 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5487 * Returns: (transfer full): a #GFileMonitor for the given @file,
5488 * or %NULL on error.
5489 * Free the returned object with g_object_unref().
5491 * Since: 2.18
5493 GFileMonitor *
5494 g_file_monitor (GFile *file,
5495 GFileMonitorFlags flags,
5496 GCancellable *cancellable,
5497 GError **error)
5499 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5500 return g_file_monitor_directory (file,
5501 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5502 cancellable, error);
5503 else
5504 return g_file_monitor_file (file, flags, cancellable, error);
5507 /********************************************
5508 * Default implementation of async ops *
5509 ********************************************/
5511 typedef struct {
5512 char *attributes;
5513 GFileQueryInfoFlags flags;
5514 } QueryInfoAsyncData;
5516 static void
5517 query_info_data_free (QueryInfoAsyncData *data)
5519 g_free (data->attributes);
5520 g_free (data);
5523 static void
5524 query_info_async_thread (GTask *task,
5525 gpointer object,
5526 gpointer task_data,
5527 GCancellable *cancellable)
5529 QueryInfoAsyncData *data = task_data;
5530 GFileInfo *info;
5531 GError *error = NULL;
5533 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5534 if (info)
5535 g_task_return_pointer (task, info, g_object_unref);
5536 else
5537 g_task_return_error (task, error);
5540 static void
5541 g_file_real_query_info_async (GFile *file,
5542 const char *attributes,
5543 GFileQueryInfoFlags flags,
5544 int io_priority,
5545 GCancellable *cancellable,
5546 GAsyncReadyCallback callback,
5547 gpointer user_data)
5549 GTask *task;
5550 QueryInfoAsyncData *data;
5552 data = g_new0 (QueryInfoAsyncData, 1);
5553 data->attributes = g_strdup (attributes);
5554 data->flags = flags;
5556 task = g_task_new (file, cancellable, callback, user_data);
5557 g_task_set_source_tag (task, g_file_real_query_info_async);
5558 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5559 g_task_set_priority (task, io_priority);
5560 g_task_run_in_thread (task, query_info_async_thread);
5561 g_object_unref (task);
5564 static GFileInfo *
5565 g_file_real_query_info_finish (GFile *file,
5566 GAsyncResult *res,
5567 GError **error)
5569 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5571 return g_task_propagate_pointer (G_TASK (res), error);
5574 static void
5575 query_filesystem_info_async_thread (GTask *task,
5576 gpointer object,
5577 gpointer task_data,
5578 GCancellable *cancellable)
5580 const char *attributes = task_data;
5581 GFileInfo *info;
5582 GError *error = NULL;
5584 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5585 if (info)
5586 g_task_return_pointer (task, info, g_object_unref);
5587 else
5588 g_task_return_error (task, error);
5591 static void
5592 g_file_real_query_filesystem_info_async (GFile *file,
5593 const char *attributes,
5594 int io_priority,
5595 GCancellable *cancellable,
5596 GAsyncReadyCallback callback,
5597 gpointer user_data)
5599 GTask *task;
5601 task = g_task_new (file, cancellable, callback, user_data);
5602 g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
5603 g_task_set_task_data (task, g_strdup (attributes), g_free);
5604 g_task_set_priority (task, io_priority);
5605 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5606 g_object_unref (task);
5609 static GFileInfo *
5610 g_file_real_query_filesystem_info_finish (GFile *file,
5611 GAsyncResult *res,
5612 GError **error)
5614 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5616 return g_task_propagate_pointer (G_TASK (res), error);
5619 static void
5620 enumerate_children_async_thread (GTask *task,
5621 gpointer object,
5622 gpointer task_data,
5623 GCancellable *cancellable)
5625 QueryInfoAsyncData *data = task_data;
5626 GFileEnumerator *enumerator;
5627 GError *error = NULL;
5629 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5630 if (error)
5631 g_task_return_error (task, error);
5632 else
5633 g_task_return_pointer (task, enumerator, g_object_unref);
5636 static void
5637 g_file_real_enumerate_children_async (GFile *file,
5638 const char *attributes,
5639 GFileQueryInfoFlags flags,
5640 int io_priority,
5641 GCancellable *cancellable,
5642 GAsyncReadyCallback callback,
5643 gpointer user_data)
5645 GTask *task;
5646 QueryInfoAsyncData *data;
5648 data = g_new0 (QueryInfoAsyncData, 1);
5649 data->attributes = g_strdup (attributes);
5650 data->flags = flags;
5652 task = g_task_new (file, cancellable, callback, user_data);
5653 g_task_set_source_tag (task, g_file_real_enumerate_children_async);
5654 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5655 g_task_set_priority (task, io_priority);
5656 g_task_run_in_thread (task, enumerate_children_async_thread);
5657 g_object_unref (task);
5660 static GFileEnumerator *
5661 g_file_real_enumerate_children_finish (GFile *file,
5662 GAsyncResult *res,
5663 GError **error)
5665 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5667 return g_task_propagate_pointer (G_TASK (res), error);
5670 static void
5671 open_read_async_thread (GTask *task,
5672 gpointer object,
5673 gpointer task_data,
5674 GCancellable *cancellable)
5676 GFileInputStream *stream;
5677 GError *error = NULL;
5679 stream = g_file_read (G_FILE (object), cancellable, &error);
5680 if (stream)
5681 g_task_return_pointer (task, stream, g_object_unref);
5682 else
5683 g_task_return_error (task, error);
5686 static void
5687 g_file_real_read_async (GFile *file,
5688 int io_priority,
5689 GCancellable *cancellable,
5690 GAsyncReadyCallback callback,
5691 gpointer user_data)
5693 GTask *task;
5695 task = g_task_new (file, cancellable, callback, user_data);
5696 g_task_set_source_tag (task, g_file_real_read_async);
5697 g_task_set_priority (task, io_priority);
5698 g_task_run_in_thread (task, open_read_async_thread);
5699 g_object_unref (task);
5702 static GFileInputStream *
5703 g_file_real_read_finish (GFile *file,
5704 GAsyncResult *res,
5705 GError **error)
5707 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5709 return g_task_propagate_pointer (G_TASK (res), error);
5712 static void
5713 append_to_async_thread (GTask *task,
5714 gpointer source_object,
5715 gpointer task_data,
5716 GCancellable *cancellable)
5718 GFileCreateFlags *data = task_data;
5719 GFileOutputStream *stream;
5720 GError *error = NULL;
5722 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5723 if (stream)
5724 g_task_return_pointer (task, stream, g_object_unref);
5725 else
5726 g_task_return_error (task, error);
5729 static void
5730 g_file_real_append_to_async (GFile *file,
5731 GFileCreateFlags flags,
5732 int io_priority,
5733 GCancellable *cancellable,
5734 GAsyncReadyCallback callback,
5735 gpointer user_data)
5737 GFileCreateFlags *data;
5738 GTask *task;
5740 data = g_new0 (GFileCreateFlags, 1);
5741 *data = flags;
5743 task = g_task_new (file, cancellable, callback, user_data);
5744 g_task_set_source_tag (task, g_file_real_append_to_async);
5745 g_task_set_task_data (task, data, g_free);
5746 g_task_set_priority (task, io_priority);
5748 g_task_run_in_thread (task, append_to_async_thread);
5749 g_object_unref (task);
5752 static GFileOutputStream *
5753 g_file_real_append_to_finish (GFile *file,
5754 GAsyncResult *res,
5755 GError **error)
5757 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5759 return g_task_propagate_pointer (G_TASK (res), error);
5762 static void
5763 create_async_thread (GTask *task,
5764 gpointer source_object,
5765 gpointer task_data,
5766 GCancellable *cancellable)
5768 GFileCreateFlags *data = task_data;
5769 GFileOutputStream *stream;
5770 GError *error = NULL;
5772 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5773 if (stream)
5774 g_task_return_pointer (task, stream, g_object_unref);
5775 else
5776 g_task_return_error (task, error);
5779 static void
5780 g_file_real_create_async (GFile *file,
5781 GFileCreateFlags flags,
5782 int io_priority,
5783 GCancellable *cancellable,
5784 GAsyncReadyCallback callback,
5785 gpointer user_data)
5787 GFileCreateFlags *data;
5788 GTask *task;
5790 data = g_new0 (GFileCreateFlags, 1);
5791 *data = flags;
5793 task = g_task_new (file, cancellable, callback, user_data);
5794 g_task_set_source_tag (task, g_file_real_create_async);
5795 g_task_set_task_data (task, data, g_free);
5796 g_task_set_priority (task, io_priority);
5798 g_task_run_in_thread (task, create_async_thread);
5799 g_object_unref (task);
5802 static GFileOutputStream *
5803 g_file_real_create_finish (GFile *file,
5804 GAsyncResult *res,
5805 GError **error)
5807 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5809 return g_task_propagate_pointer (G_TASK (res), error);
5812 typedef struct {
5813 GFileOutputStream *stream;
5814 char *etag;
5815 gboolean make_backup;
5816 GFileCreateFlags flags;
5817 } ReplaceAsyncData;
5819 static void
5820 replace_async_data_free (ReplaceAsyncData *data)
5822 if (data->stream)
5823 g_object_unref (data->stream);
5824 g_free (data->etag);
5825 g_free (data);
5828 static void
5829 replace_async_thread (GTask *task,
5830 gpointer source_object,
5831 gpointer task_data,
5832 GCancellable *cancellable)
5834 GFileOutputStream *stream;
5835 ReplaceAsyncData *data = task_data;
5836 GError *error = NULL;
5838 stream = g_file_replace (G_FILE (source_object),
5839 data->etag,
5840 data->make_backup,
5841 data->flags,
5842 cancellable,
5843 &error);
5845 if (stream)
5846 g_task_return_pointer (task, stream, g_object_unref);
5847 else
5848 g_task_return_error (task, error);
5851 static void
5852 g_file_real_replace_async (GFile *file,
5853 const char *etag,
5854 gboolean make_backup,
5855 GFileCreateFlags flags,
5856 int io_priority,
5857 GCancellable *cancellable,
5858 GAsyncReadyCallback callback,
5859 gpointer user_data)
5861 GTask *task;
5862 ReplaceAsyncData *data;
5864 data = g_new0 (ReplaceAsyncData, 1);
5865 data->etag = g_strdup (etag);
5866 data->make_backup = make_backup;
5867 data->flags = flags;
5869 task = g_task_new (file, cancellable, callback, user_data);
5870 g_task_set_source_tag (task, g_file_real_replace_async);
5871 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5872 g_task_set_priority (task, io_priority);
5874 g_task_run_in_thread (task, replace_async_thread);
5875 g_object_unref (task);
5878 static GFileOutputStream *
5879 g_file_real_replace_finish (GFile *file,
5880 GAsyncResult *res,
5881 GError **error)
5883 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5885 return g_task_propagate_pointer (G_TASK (res), error);
5888 static void
5889 delete_async_thread (GTask *task,
5890 gpointer object,
5891 gpointer task_data,
5892 GCancellable *cancellable)
5894 GError *error = NULL;
5896 if (g_file_delete (G_FILE (object), cancellable, &error))
5897 g_task_return_boolean (task, TRUE);
5898 else
5899 g_task_return_error (task, error);
5902 static void
5903 g_file_real_delete_async (GFile *file,
5904 int io_priority,
5905 GCancellable *cancellable,
5906 GAsyncReadyCallback callback,
5907 gpointer user_data)
5909 GTask *task;
5911 task = g_task_new (file, cancellable, callback, user_data);
5912 g_task_set_source_tag (task, g_file_real_delete_async);
5913 g_task_set_priority (task, io_priority);
5914 g_task_run_in_thread (task, delete_async_thread);
5915 g_object_unref (task);
5918 static gboolean
5919 g_file_real_delete_finish (GFile *file,
5920 GAsyncResult *res,
5921 GError **error)
5923 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5925 return g_task_propagate_boolean (G_TASK (res), error);
5928 static void
5929 trash_async_thread (GTask *task,
5930 gpointer object,
5931 gpointer task_data,
5932 GCancellable *cancellable)
5934 GError *error = NULL;
5936 if (g_file_trash (G_FILE (object), cancellable, &error))
5937 g_task_return_boolean (task, TRUE);
5938 else
5939 g_task_return_error (task, error);
5942 static void
5943 g_file_real_trash_async (GFile *file,
5944 int io_priority,
5945 GCancellable *cancellable,
5946 GAsyncReadyCallback callback,
5947 gpointer user_data)
5949 GTask *task;
5951 task = g_task_new (file, cancellable, callback, user_data);
5952 g_task_set_source_tag (task, g_file_real_trash_async);
5953 g_task_set_priority (task, io_priority);
5954 g_task_run_in_thread (task, trash_async_thread);
5955 g_object_unref (task);
5958 static gboolean
5959 g_file_real_trash_finish (GFile *file,
5960 GAsyncResult *res,
5961 GError **error)
5963 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5965 return g_task_propagate_boolean (G_TASK (res), error);
5968 static void
5969 make_directory_async_thread (GTask *task,
5970 gpointer object,
5971 gpointer task_data,
5972 GCancellable *cancellable)
5974 GError *error = NULL;
5976 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5977 g_task_return_boolean (task, TRUE);
5978 else
5979 g_task_return_error (task, error);
5982 static void
5983 g_file_real_make_directory_async (GFile *file,
5984 int io_priority,
5985 GCancellable *cancellable,
5986 GAsyncReadyCallback callback,
5987 gpointer user_data)
5989 GTask *task;
5991 task = g_task_new (file, cancellable, callback, user_data);
5992 g_task_set_source_tag (task, g_file_real_make_directory_async);
5993 g_task_set_priority (task, io_priority);
5994 g_task_run_in_thread (task, make_directory_async_thread);
5995 g_object_unref (task);
5998 static gboolean
5999 g_file_real_make_directory_finish (GFile *file,
6000 GAsyncResult *res,
6001 GError **error)
6003 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6005 return g_task_propagate_boolean (G_TASK (res), error);
6008 static void
6009 open_readwrite_async_thread (GTask *task,
6010 gpointer object,
6011 gpointer task_data,
6012 GCancellable *cancellable)
6014 GFileIOStream *stream;
6015 GError *error = NULL;
6017 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6019 if (stream == NULL)
6020 g_task_return_error (task, error);
6021 else
6022 g_task_return_pointer (task, stream, g_object_unref);
6025 static void
6026 g_file_real_open_readwrite_async (GFile *file,
6027 int io_priority,
6028 GCancellable *cancellable,
6029 GAsyncReadyCallback callback,
6030 gpointer user_data)
6032 GTask *task;
6034 task = g_task_new (file, cancellable, callback, user_data);
6035 g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6036 g_task_set_priority (task, io_priority);
6038 g_task_run_in_thread (task, open_readwrite_async_thread);
6039 g_object_unref (task);
6042 static GFileIOStream *
6043 g_file_real_open_readwrite_finish (GFile *file,
6044 GAsyncResult *res,
6045 GError **error)
6047 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6049 return g_task_propagate_pointer (G_TASK (res), error);
6052 static void
6053 create_readwrite_async_thread (GTask *task,
6054 gpointer object,
6055 gpointer task_data,
6056 GCancellable *cancellable)
6058 GFileCreateFlags *data = task_data;
6059 GFileIOStream *stream;
6060 GError *error = NULL;
6062 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6064 if (stream == NULL)
6065 g_task_return_error (task, error);
6066 else
6067 g_task_return_pointer (task, stream, g_object_unref);
6070 static void
6071 g_file_real_create_readwrite_async (GFile *file,
6072 GFileCreateFlags flags,
6073 int io_priority,
6074 GCancellable *cancellable,
6075 GAsyncReadyCallback callback,
6076 gpointer user_data)
6078 GFileCreateFlags *data;
6079 GTask *task;
6081 data = g_new0 (GFileCreateFlags, 1);
6082 *data = flags;
6084 task = g_task_new (file, cancellable, callback, user_data);
6085 g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6086 g_task_set_task_data (task, data, g_free);
6087 g_task_set_priority (task, io_priority);
6089 g_task_run_in_thread (task, create_readwrite_async_thread);
6090 g_object_unref (task);
6093 static GFileIOStream *
6094 g_file_real_create_readwrite_finish (GFile *file,
6095 GAsyncResult *res,
6096 GError **error)
6098 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6100 return g_task_propagate_pointer (G_TASK (res), error);
6103 typedef struct {
6104 char *etag;
6105 gboolean make_backup;
6106 GFileCreateFlags flags;
6107 } ReplaceRWAsyncData;
6109 static void
6110 replace_rw_async_data_free (ReplaceRWAsyncData *data)
6112 g_free (data->etag);
6113 g_free (data);
6116 static void
6117 replace_readwrite_async_thread (GTask *task,
6118 gpointer object,
6119 gpointer task_data,
6120 GCancellable *cancellable)
6122 GFileIOStream *stream;
6123 GError *error = NULL;
6124 ReplaceRWAsyncData *data = task_data;
6126 stream = g_file_replace_readwrite (G_FILE (object),
6127 data->etag,
6128 data->make_backup,
6129 data->flags,
6130 cancellable,
6131 &error);
6133 if (stream == NULL)
6134 g_task_return_error (task, error);
6135 else
6136 g_task_return_pointer (task, stream, g_object_unref);
6139 static void
6140 g_file_real_replace_readwrite_async (GFile *file,
6141 const char *etag,
6142 gboolean make_backup,
6143 GFileCreateFlags flags,
6144 int io_priority,
6145 GCancellable *cancellable,
6146 GAsyncReadyCallback callback,
6147 gpointer user_data)
6149 GTask *task;
6150 ReplaceRWAsyncData *data;
6152 data = g_new0 (ReplaceRWAsyncData, 1);
6153 data->etag = g_strdup (etag);
6154 data->make_backup = make_backup;
6155 data->flags = flags;
6157 task = g_task_new (file, cancellable, callback, user_data);
6158 g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6159 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6160 g_task_set_priority (task, io_priority);
6162 g_task_run_in_thread (task, replace_readwrite_async_thread);
6163 g_object_unref (task);
6166 static GFileIOStream *
6167 g_file_real_replace_readwrite_finish (GFile *file,
6168 GAsyncResult *res,
6169 GError **error)
6171 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6173 return g_task_propagate_pointer (G_TASK (res), error);
6176 static void
6177 set_display_name_async_thread (GTask *task,
6178 gpointer object,
6179 gpointer task_data,
6180 GCancellable *cancellable)
6182 GError *error = NULL;
6183 char *name = task_data;
6184 GFile *file;
6186 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6188 if (file == NULL)
6189 g_task_return_error (task, error);
6190 else
6191 g_task_return_pointer (task, file, g_object_unref);
6194 static void
6195 g_file_real_set_display_name_async (GFile *file,
6196 const char *display_name,
6197 int io_priority,
6198 GCancellable *cancellable,
6199 GAsyncReadyCallback callback,
6200 gpointer user_data)
6202 GTask *task;
6204 task = g_task_new (file, cancellable, callback, user_data);
6205 g_task_set_source_tag (task, g_file_real_set_display_name_async);
6206 g_task_set_task_data (task, g_strdup (display_name), g_free);
6207 g_task_set_priority (task, io_priority);
6209 g_task_run_in_thread (task, set_display_name_async_thread);
6210 g_object_unref (task);
6213 static GFile *
6214 g_file_real_set_display_name_finish (GFile *file,
6215 GAsyncResult *res,
6216 GError **error)
6218 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6220 return g_task_propagate_pointer (G_TASK (res), error);
6223 typedef struct {
6224 GFileQueryInfoFlags flags;
6225 GFileInfo *info;
6226 gboolean res;
6227 GError *error;
6228 } SetInfoAsyncData;
6230 static void
6231 set_info_data_free (SetInfoAsyncData *data)
6233 if (data->info)
6234 g_object_unref (data->info);
6235 if (data->error)
6236 g_error_free (data->error);
6237 g_free (data);
6240 static void
6241 set_info_async_thread (GTask *task,
6242 gpointer object,
6243 gpointer task_data,
6244 GCancellable *cancellable)
6246 SetInfoAsyncData *data = task_data;
6248 data->error = NULL;
6249 data->res = g_file_set_attributes_from_info (G_FILE (object),
6250 data->info,
6251 data->flags,
6252 cancellable,
6253 &data->error);
6256 static void
6257 g_file_real_set_attributes_async (GFile *file,
6258 GFileInfo *info,
6259 GFileQueryInfoFlags flags,
6260 int io_priority,
6261 GCancellable *cancellable,
6262 GAsyncReadyCallback callback,
6263 gpointer user_data)
6265 GTask *task;
6266 SetInfoAsyncData *data;
6268 data = g_new0 (SetInfoAsyncData, 1);
6269 data->info = g_file_info_dup (info);
6270 data->flags = flags;
6272 task = g_task_new (file, cancellable, callback, user_data);
6273 g_task_set_source_tag (task, g_file_real_set_attributes_async);
6274 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6275 g_task_set_priority (task, io_priority);
6277 g_task_run_in_thread (task, set_info_async_thread);
6278 g_object_unref (task);
6281 static gboolean
6282 g_file_real_set_attributes_finish (GFile *file,
6283 GAsyncResult *res,
6284 GFileInfo **info,
6285 GError **error)
6287 SetInfoAsyncData *data;
6289 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6291 data = g_task_get_task_data (G_TASK (res));
6293 if (info)
6294 *info = g_object_ref (data->info);
6296 if (error != NULL && data->error)
6297 *error = g_error_copy (data->error);
6299 return data->res;
6302 static void
6303 find_enclosing_mount_async_thread (GTask *task,
6304 gpointer object,
6305 gpointer task_data,
6306 GCancellable *cancellable)
6308 GError *error = NULL;
6309 GMount *mount;
6311 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6313 if (mount == NULL)
6314 g_task_return_error (task, error);
6315 else
6316 g_task_return_pointer (task, mount, g_object_unref);
6319 static void
6320 g_file_real_find_enclosing_mount_async (GFile *file,
6321 int io_priority,
6322 GCancellable *cancellable,
6323 GAsyncReadyCallback callback,
6324 gpointer user_data)
6326 GTask *task;
6328 task = g_task_new (file, cancellable, callback, user_data);
6329 g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
6330 g_task_set_priority (task, io_priority);
6332 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6333 g_object_unref (task);
6336 static GMount *
6337 g_file_real_find_enclosing_mount_finish (GFile *file,
6338 GAsyncResult *res,
6339 GError **error)
6341 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6343 return g_task_propagate_pointer (G_TASK (res), error);
6347 typedef struct {
6348 GFile *source;
6349 GFile *destination;
6350 GFileCopyFlags flags;
6351 GFileProgressCallback progress_cb;
6352 gpointer progress_cb_data;
6353 } CopyAsyncData;
6355 static void
6356 copy_async_data_free (CopyAsyncData *data)
6358 g_object_unref (data->source);
6359 g_object_unref (data->destination);
6360 g_slice_free (CopyAsyncData, data);
6363 typedef struct {
6364 CopyAsyncData *data;
6365 goffset current_num_bytes;
6366 goffset total_num_bytes;
6367 } ProgressData;
6369 static gboolean
6370 copy_async_progress_in_main (gpointer user_data)
6372 ProgressData *progress = user_data;
6373 CopyAsyncData *data = progress->data;
6375 data->progress_cb (progress->current_num_bytes,
6376 progress->total_num_bytes,
6377 data->progress_cb_data);
6379 return FALSE;
6382 static void
6383 copy_async_progress_callback (goffset current_num_bytes,
6384 goffset total_num_bytes,
6385 gpointer user_data)
6387 GTask *task = user_data;
6388 CopyAsyncData *data = g_task_get_task_data (task);
6389 ProgressData *progress;
6391 progress = g_new (ProgressData, 1);
6392 progress->data = data;
6393 progress->current_num_bytes = current_num_bytes;
6394 progress->total_num_bytes = total_num_bytes;
6396 g_main_context_invoke_full (g_task_get_context (task),
6397 g_task_get_priority (task),
6398 copy_async_progress_in_main,
6399 progress,
6400 g_free);
6403 static void
6404 copy_async_thread (GTask *task,
6405 gpointer source,
6406 gpointer task_data,
6407 GCancellable *cancellable)
6409 CopyAsyncData *data = task_data;
6410 gboolean result;
6411 GError *error = NULL;
6413 result = g_file_copy (data->source,
6414 data->destination,
6415 data->flags,
6416 cancellable,
6417 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6418 task,
6419 &error);
6420 if (result)
6421 g_task_return_boolean (task, TRUE);
6422 else
6423 g_task_return_error (task, error);
6426 static void
6427 g_file_real_copy_async (GFile *source,
6428 GFile *destination,
6429 GFileCopyFlags flags,
6430 int io_priority,
6431 GCancellable *cancellable,
6432 GFileProgressCallback progress_callback,
6433 gpointer progress_callback_data,
6434 GAsyncReadyCallback callback,
6435 gpointer user_data)
6437 GTask *task;
6438 CopyAsyncData *data;
6440 data = g_slice_new (CopyAsyncData);
6441 data->source = g_object_ref (source);
6442 data->destination = g_object_ref (destination);
6443 data->flags = flags;
6444 data->progress_cb = progress_callback;
6445 data->progress_cb_data = progress_callback_data;
6447 task = g_task_new (source, cancellable, callback, user_data);
6448 g_task_set_source_tag (task, g_file_real_copy_async);
6449 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6450 g_task_set_priority (task, io_priority);
6451 g_task_run_in_thread (task, copy_async_thread);
6452 g_object_unref (task);
6455 static gboolean
6456 g_file_real_copy_finish (GFile *file,
6457 GAsyncResult *res,
6458 GError **error)
6460 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6462 return g_task_propagate_boolean (G_TASK (res), error);
6466 /********************************************
6467 * Default VFS operations *
6468 ********************************************/
6471 * g_file_new_for_path:
6472 * @path: (type filename): a string containing a relative or absolute path.
6473 * The string must be encoded in the glib filename encoding.
6475 * Constructs a #GFile for a given path. This operation never
6476 * fails, but the returned object might not support any I/O
6477 * operation if @path is malformed.
6479 * Returns: (transfer full): a new #GFile for the given @path.
6480 * Free the returned object with g_object_unref().
6482 GFile *
6483 g_file_new_for_path (const char *path)
6485 g_return_val_if_fail (path != NULL, NULL);
6487 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6491 * g_file_new_for_uri:
6492 * @uri: a UTF-8 string containing a URI
6494 * Constructs a #GFile for a given URI. This operation never
6495 * fails, but the returned object might not support any I/O
6496 * operation if @uri is malformed or if the uri type is
6497 * not supported.
6499 * Returns: (transfer full): a new #GFile for the given @uri.
6500 * Free the returned object with g_object_unref().
6502 GFile *
6503 g_file_new_for_uri (const char *uri)
6505 g_return_val_if_fail (uri != NULL, NULL);
6507 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6511 * g_file_new_tmp:
6512 * @tmpl: (type filename) (nullable): Template for the file
6513 * name, as in g_file_open_tmp(), or %NULL for a default template
6514 * @iostream: (out): on return, a #GFileIOStream for the created file
6515 * @error: a #GError, or %NULL
6517 * Opens a file in the preferred directory for temporary files (as
6518 * returned by g_get_tmp_dir()) and returns a #GFile and
6519 * #GFileIOStream pointing to it.
6521 * @tmpl should be a string in the GLib file name encoding
6522 * containing a sequence of six 'X' characters, and containing no
6523 * directory components. If it is %NULL, a default template is used.
6525 * Unlike the other #GFile constructors, this will return %NULL if
6526 * a temporary file could not be created.
6528 * Returns: (transfer full): a new #GFile.
6529 * Free the returned object with g_object_unref().
6531 * Since: 2.32
6533 GFile *
6534 g_file_new_tmp (const char *tmpl,
6535 GFileIOStream **iostream,
6536 GError **error)
6538 gint fd;
6539 gchar *path;
6540 GFile *file;
6541 GFileOutputStream *output;
6543 g_return_val_if_fail (iostream != NULL, NULL);
6545 fd = g_file_open_tmp (tmpl, &path, error);
6546 if (fd == -1)
6547 return NULL;
6549 file = g_file_new_for_path (path);
6551 output = _g_local_file_output_stream_new (fd);
6552 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6554 g_object_unref (output);
6555 g_free (path);
6557 return file;
6561 * g_file_parse_name:
6562 * @parse_name: a file name or path to be parsed
6564 * Constructs a #GFile with the given @parse_name (i.e. something
6565 * given by g_file_get_parse_name()). This operation never fails,
6566 * but the returned object might not support any I/O operation if
6567 * the @parse_name cannot be parsed.
6569 * Returns: (transfer full): a new #GFile.
6571 GFile *
6572 g_file_parse_name (const char *parse_name)
6574 g_return_val_if_fail (parse_name != NULL, NULL);
6576 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6580 * g_file_new_build_filename:
6581 * @first_element: (type filename): the first element in the path
6582 * @...: remaining elements in path, terminated by %NULL
6584 * Constructs a #GFile from a series of elements using the correct
6585 * separator for filenames.
6587 * Using this function is equivalent to calling g_build_filename(),
6588 * followed by g_file_new_for_path() on the result.
6590 * Returns: (transfer full): a new #GFile
6592 * Since: 2.56
6594 GFile *
6595 g_file_new_build_filename (const gchar *first_element,
6596 ...)
6598 gchar *str;
6599 GFile *file;
6600 va_list args;
6602 g_return_val_if_fail (first_element != NULL, NULL);
6604 va_start (args, first_element);
6605 str = g_build_filename_valist (first_element, &args);
6606 va_end (args);
6608 file = g_file_new_for_path (str);
6609 g_free (str);
6611 return file;
6614 static gboolean
6615 is_valid_scheme_character (char c)
6617 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6620 /* Following RFC 2396, valid schemes are built like:
6621 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6623 static gboolean
6624 has_valid_scheme (const char *uri)
6626 const char *p;
6628 p = uri;
6630 if (!g_ascii_isalpha (*p))
6631 return FALSE;
6633 do {
6634 p++;
6635 } while (is_valid_scheme_character (*p));
6637 return *p == ':';
6640 static GFile *
6641 new_for_cmdline_arg (const gchar *arg,
6642 const gchar *cwd)
6644 GFile *file;
6645 char *filename;
6647 if (g_path_is_absolute (arg))
6648 return g_file_new_for_path (arg);
6650 if (has_valid_scheme (arg))
6651 return g_file_new_for_uri (arg);
6653 if (cwd == NULL)
6655 char *current_dir;
6657 current_dir = g_get_current_dir ();
6658 filename = g_build_filename (current_dir, arg, NULL);
6659 g_free (current_dir);
6661 else
6662 filename = g_build_filename (cwd, arg, NULL);
6664 file = g_file_new_for_path (filename);
6665 g_free (filename);
6667 return file;
6671 * g_file_new_for_commandline_arg:
6672 * @arg: (type filename): a command line string
6674 * Creates a #GFile with the given argument from the command line.
6675 * The value of @arg can be either a URI, an absolute path or a
6676 * relative path resolved relative to the current working directory.
6677 * This operation never fails, but the returned object might not
6678 * support any I/O operation if @arg points to a malformed path.
6680 * Note that on Windows, this function expects its argument to be in
6681 * UTF-8 -- not the system code page. This means that you
6682 * should not use this function with string from argv as it is passed
6683 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6684 * the commandline. #GApplication also uses UTF-8 but
6685 * g_application_command_line_create_file_for_arg() may be more useful
6686 * for you there. It is also always possible to use this function with
6687 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6689 * Returns: (transfer full): a new #GFile.
6690 * Free the returned object with g_object_unref().
6692 GFile *
6693 g_file_new_for_commandline_arg (const char *arg)
6695 g_return_val_if_fail (arg != NULL, NULL);
6697 return new_for_cmdline_arg (arg, NULL);
6701 * g_file_new_for_commandline_arg_and_cwd:
6702 * @arg: (type filename): a command line string
6703 * @cwd: (type filename): the current working directory of the commandline
6705 * Creates a #GFile with the given argument from the command line.
6707 * This function is similar to g_file_new_for_commandline_arg() except
6708 * that it allows for passing the current working directory as an
6709 * argument instead of using the current working directory of the
6710 * process.
6712 * This is useful if the commandline argument was given in a context
6713 * other than the invocation of the current process.
6715 * See also g_application_command_line_create_file_for_arg().
6717 * Returns: (transfer full): a new #GFile
6719 * Since: 2.36
6721 GFile *
6722 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6723 const gchar *cwd)
6725 g_return_val_if_fail (arg != NULL, NULL);
6726 g_return_val_if_fail (cwd != NULL, NULL);
6728 return new_for_cmdline_arg (arg, cwd);
6732 * g_file_mount_enclosing_volume:
6733 * @location: input #GFile
6734 * @flags: flags affecting the operation
6735 * @mount_operation: (nullable): a #GMountOperation
6736 * or %NULL to avoid user interaction
6737 * @cancellable: (nullable): optional #GCancellable object,
6738 * %NULL to ignore
6739 * @callback: (nullable): a #GAsyncReadyCallback to call
6740 * when the request is satisfied, or %NULL
6741 * @user_data: the data to pass to callback function
6743 * Starts a @mount_operation, mounting the volume that contains
6744 * the file @location.
6746 * When this operation has completed, @callback will be called with
6747 * @user_user data, and the operation can be finalized with
6748 * g_file_mount_enclosing_volume_finish().
6750 * If @cancellable is not %NULL, then the operation can be cancelled by
6751 * triggering the cancellable object from another thread. If the operation
6752 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6754 void
6755 g_file_mount_enclosing_volume (GFile *location,
6756 GMountMountFlags flags,
6757 GMountOperation *mount_operation,
6758 GCancellable *cancellable,
6759 GAsyncReadyCallback callback,
6760 gpointer user_data)
6762 GFileIface *iface;
6764 g_return_if_fail (G_IS_FILE (location));
6766 iface = G_FILE_GET_IFACE (location);
6768 if (iface->mount_enclosing_volume == NULL)
6770 g_task_report_new_error (location, callback, user_data,
6771 g_file_mount_enclosing_volume,
6772 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6773 _("volume doesn’t implement mount"));
6774 return;
6777 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6782 * g_file_mount_enclosing_volume_finish:
6783 * @location: input #GFile
6784 * @result: a #GAsyncResult
6785 * @error: a #GError, or %NULL
6787 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6789 * Returns: %TRUE if successful. If an error has occurred,
6790 * this function will return %FALSE and set @error
6791 * appropriately if present.
6793 gboolean
6794 g_file_mount_enclosing_volume_finish (GFile *location,
6795 GAsyncResult *result,
6796 GError **error)
6798 GFileIface *iface;
6800 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6801 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6803 if (g_async_result_legacy_propagate_error (result, error))
6804 return FALSE;
6805 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6806 return g_task_propagate_boolean (G_TASK (result), error);
6808 iface = G_FILE_GET_IFACE (location);
6810 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6813 /********************************************
6814 * Utility functions *
6815 ********************************************/
6818 * g_file_query_default_handler:
6819 * @file: a #GFile to open
6820 * @cancellable: optional #GCancellable object, %NULL to ignore
6821 * @error: a #GError, or %NULL
6823 * Returns the #GAppInfo that is registered as the default
6824 * application to handle the file specified by @file.
6826 * If @cancellable is not %NULL, then the operation can be cancelled by
6827 * triggering the cancellable object from another thread. If the operation
6828 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6830 * Returns: (transfer full): a #GAppInfo if the handle was found,
6831 * %NULL if there were errors.
6832 * When you are done with it, release it with g_object_unref()
6834 GAppInfo *
6835 g_file_query_default_handler (GFile *file,
6836 GCancellable *cancellable,
6837 GError **error)
6839 char *uri_scheme;
6840 const char *content_type;
6841 GAppInfo *appinfo;
6842 GFileInfo *info;
6843 char *path;
6845 uri_scheme = g_file_get_uri_scheme (file);
6846 if (uri_scheme && uri_scheme[0] != '\0')
6848 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6849 g_free (uri_scheme);
6851 if (appinfo != NULL)
6852 return appinfo;
6855 info = g_file_query_info (file,
6856 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6858 cancellable,
6859 error);
6860 if (info == NULL)
6861 return NULL;
6863 appinfo = NULL;
6865 content_type = g_file_info_get_content_type (info);
6866 if (content_type)
6868 /* Don't use is_native(), as we want to support fuse paths if available */
6869 path = g_file_get_path (file);
6870 appinfo = g_app_info_get_default_for_type (content_type,
6871 path == NULL);
6872 g_free (path);
6875 g_object_unref (info);
6877 if (appinfo != NULL)
6878 return appinfo;
6880 g_set_error_literal (error, G_IO_ERROR,
6881 G_IO_ERROR_NOT_SUPPORTED,
6882 _("No application is registered as handling this file"));
6883 return NULL;
6886 #define GET_CONTENT_BLOCK_SIZE 8192
6889 * g_file_load_contents:
6890 * @file: input #GFile
6891 * @cancellable: optional #GCancellable object, %NULL to ignore
6892 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6893 * @length: (out) (optional): a location to place the length of the contents of the file,
6894 * or %NULL if the length is not needed
6895 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6896 * or %NULL if the entity tag is not needed
6897 * @error: a #GError, or %NULL
6899 * Loads the content of the file into memory. The data is always
6900 * zero-terminated, but this is not included in the resultant @length.
6901 * The returned @content should be freed with g_free() when no longer
6902 * needed.
6904 * If @cancellable is not %NULL, then the operation can be cancelled by
6905 * triggering the cancellable object from another thread. If the operation
6906 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6908 * Returns: %TRUE if the @file's contents were successfully loaded.
6909 * %FALSE if there were errors.
6911 gboolean
6912 g_file_load_contents (GFile *file,
6913 GCancellable *cancellable,
6914 char **contents,
6915 gsize *length,
6916 char **etag_out,
6917 GError **error)
6919 GFileInputStream *in;
6920 GByteArray *content;
6921 gsize pos;
6922 gssize res;
6923 GFileInfo *info;
6925 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6926 g_return_val_if_fail (contents != NULL, FALSE);
6928 in = g_file_read (file, cancellable, error);
6929 if (in == NULL)
6930 return FALSE;
6932 content = g_byte_array_new ();
6933 pos = 0;
6935 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6936 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6937 content->data + pos,
6938 GET_CONTENT_BLOCK_SIZE,
6939 cancellable, error)) > 0)
6941 pos += res;
6942 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6945 if (etag_out)
6947 *etag_out = NULL;
6949 info = g_file_input_stream_query_info (in,
6950 G_FILE_ATTRIBUTE_ETAG_VALUE,
6951 cancellable,
6952 NULL);
6953 if (info)
6955 *etag_out = g_strdup (g_file_info_get_etag (info));
6956 g_object_unref (info);
6960 /* Ignore errors on close */
6961 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6962 g_object_unref (in);
6964 if (res < 0)
6966 /* error is set already */
6967 g_byte_array_free (content, TRUE);
6968 return FALSE;
6971 if (length)
6972 *length = pos;
6974 /* Zero terminate (we got an extra byte allocated for this */
6975 content->data[pos] = 0;
6977 *contents = (char *)g_byte_array_free (content, FALSE);
6979 return TRUE;
6982 typedef struct {
6983 GTask *task;
6984 GFileReadMoreCallback read_more_callback;
6985 GByteArray *content;
6986 gsize pos;
6987 char *etag;
6988 } LoadContentsData;
6991 static void
6992 load_contents_data_free (LoadContentsData *data)
6994 if (data->content)
6995 g_byte_array_free (data->content, TRUE);
6996 g_free (data->etag);
6997 g_free (data);
7000 static void
7001 load_contents_close_callback (GObject *obj,
7002 GAsyncResult *close_res,
7003 gpointer user_data)
7005 GInputStream *stream = G_INPUT_STREAM (obj);
7006 LoadContentsData *data = user_data;
7008 /* Ignore errors here, we're only reading anyway */
7009 g_input_stream_close_finish (stream, close_res, NULL);
7010 g_object_unref (stream);
7012 g_task_return_boolean (data->task, TRUE);
7013 g_object_unref (data->task);
7016 static void
7017 load_contents_fstat_callback (GObject *obj,
7018 GAsyncResult *stat_res,
7019 gpointer user_data)
7021 GInputStream *stream = G_INPUT_STREAM (obj);
7022 LoadContentsData *data = user_data;
7023 GFileInfo *info;
7025 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
7026 stat_res, NULL);
7027 if (info)
7029 data->etag = g_strdup (g_file_info_get_etag (info));
7030 g_object_unref (info);
7033 g_input_stream_close_async (stream, 0,
7034 g_task_get_cancellable (data->task),
7035 load_contents_close_callback, data);
7038 static void
7039 load_contents_read_callback (GObject *obj,
7040 GAsyncResult *read_res,
7041 gpointer user_data)
7043 GInputStream *stream = G_INPUT_STREAM (obj);
7044 LoadContentsData *data = user_data;
7045 GError *error = NULL;
7046 gssize read_size;
7048 read_size = g_input_stream_read_finish (stream, read_res, &error);
7050 if (read_size < 0)
7052 g_task_return_error (data->task, error);
7053 g_object_unref (data->task);
7055 /* Close the file ignoring any error */
7056 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
7057 g_object_unref (stream);
7059 else if (read_size == 0)
7061 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7062 G_FILE_ATTRIBUTE_ETAG_VALUE,
7064 g_task_get_cancellable (data->task),
7065 load_contents_fstat_callback,
7066 data);
7068 else if (read_size > 0)
7070 data->pos += read_size;
7072 g_byte_array_set_size (data->content,
7073 data->pos + GET_CONTENT_BLOCK_SIZE);
7076 if (data->read_more_callback &&
7077 !data->read_more_callback ((char *)data->content->data, data->pos,
7078 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
7079 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
7080 G_FILE_ATTRIBUTE_ETAG_VALUE,
7082 g_task_get_cancellable (data->task),
7083 load_contents_fstat_callback,
7084 data);
7085 else
7086 g_input_stream_read_async (stream,
7087 data->content->data + data->pos,
7088 GET_CONTENT_BLOCK_SIZE,
7090 g_task_get_cancellable (data->task),
7091 load_contents_read_callback,
7092 data);
7096 static void
7097 load_contents_open_callback (GObject *obj,
7098 GAsyncResult *open_res,
7099 gpointer user_data)
7101 GFile *file = G_FILE (obj);
7102 GFileInputStream *stream;
7103 LoadContentsData *data = user_data;
7104 GError *error = NULL;
7106 stream = g_file_read_finish (file, open_res, &error);
7108 if (stream)
7110 g_byte_array_set_size (data->content,
7111 data->pos + GET_CONTENT_BLOCK_SIZE);
7112 g_input_stream_read_async (G_INPUT_STREAM (stream),
7113 data->content->data + data->pos,
7114 GET_CONTENT_BLOCK_SIZE,
7116 g_task_get_cancellable (data->task),
7117 load_contents_read_callback,
7118 data);
7120 else
7122 g_task_return_error (data->task, error);
7123 g_object_unref (data->task);
7128 * g_file_load_partial_contents_async: (skip)
7129 * @file: input #GFile
7130 * @cancellable: optional #GCancellable object, %NULL to ignore
7131 * @read_more_callback: (scope call) (closure user_data): a
7132 * #GFileReadMoreCallback to receive partial data
7133 * and to specify whether further data should be read
7134 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7135 * when the request is satisfied
7136 * @user_data: the data to pass to the callback functions
7138 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7139 * be used to stop reading from the file when appropriate, else this
7140 * function will behave exactly as g_file_load_contents_async(). This
7141 * operation can be finished by g_file_load_partial_contents_finish().
7143 * Users of this function should be aware that @user_data is passed to
7144 * both the @read_more_callback and the @callback.
7146 * If @cancellable is not %NULL, then the operation can be cancelled by
7147 * triggering the cancellable object from another thread. If the operation
7148 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7150 void
7151 g_file_load_partial_contents_async (GFile *file,
7152 GCancellable *cancellable,
7153 GFileReadMoreCallback read_more_callback,
7154 GAsyncReadyCallback callback,
7155 gpointer user_data)
7157 LoadContentsData *data;
7159 g_return_if_fail (G_IS_FILE (file));
7161 data = g_new0 (LoadContentsData, 1);
7162 data->read_more_callback = read_more_callback;
7163 data->content = g_byte_array_new ();
7165 data->task = g_task_new (file, cancellable, callback, user_data);
7166 g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
7167 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
7169 g_file_read_async (file,
7171 g_task_get_cancellable (data->task),
7172 load_contents_open_callback,
7173 data);
7177 * g_file_load_partial_contents_finish:
7178 * @file: input #GFile
7179 * @res: a #GAsyncResult
7180 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7181 * @length: (out) (optional): a location to place the length of the contents of the file,
7182 * or %NULL if the length is not needed
7183 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7184 * or %NULL if the entity tag is not needed
7185 * @error: a #GError, or %NULL
7187 * Finishes an asynchronous partial load operation that was started
7188 * with g_file_load_partial_contents_async(). The data is always
7189 * zero-terminated, but this is not included in the resultant @length.
7190 * The returned @content should be freed with g_free() when no longer
7191 * needed.
7193 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7194 * present, it will be set appropriately.
7196 gboolean
7197 g_file_load_partial_contents_finish (GFile *file,
7198 GAsyncResult *res,
7199 char **contents,
7200 gsize *length,
7201 char **etag_out,
7202 GError **error)
7204 GTask *task;
7205 LoadContentsData *data;
7207 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7208 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7209 g_return_val_if_fail (contents != NULL, FALSE);
7211 task = G_TASK (res);
7213 if (!g_task_propagate_boolean (task, error))
7215 if (length)
7216 *length = 0;
7217 return FALSE;
7220 data = g_task_get_task_data (task);
7222 if (length)
7223 *length = data->pos;
7225 if (etag_out)
7227 *etag_out = data->etag;
7228 data->etag = NULL;
7231 /* Zero terminate */
7232 g_byte_array_set_size (data->content, data->pos + 1);
7233 data->content->data[data->pos] = 0;
7235 *contents = (char *)g_byte_array_free (data->content, FALSE);
7236 data->content = NULL;
7238 return TRUE;
7242 * g_file_load_contents_async:
7243 * @file: input #GFile
7244 * @cancellable: optional #GCancellable object, %NULL to ignore
7245 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7246 * @user_data: the data to pass to callback function
7248 * Starts an asynchronous load of the @file's contents.
7250 * For more details, see g_file_load_contents() which is
7251 * the synchronous version of this call.
7253 * When the load operation has completed, @callback will be called
7254 * with @user data. To finish the operation, call
7255 * g_file_load_contents_finish() with the #GAsyncResult returned by
7256 * the @callback.
7258 * If @cancellable is not %NULL, then the operation can be cancelled by
7259 * triggering the cancellable object from another thread. If the operation
7260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7262 void
7263 g_file_load_contents_async (GFile *file,
7264 GCancellable *cancellable,
7265 GAsyncReadyCallback callback,
7266 gpointer user_data)
7268 g_file_load_partial_contents_async (file,
7269 cancellable,
7270 NULL,
7271 callback, user_data);
7275 * g_file_load_contents_finish:
7276 * @file: input #GFile
7277 * @res: a #GAsyncResult
7278 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7279 * @length: (out) (optional): a location to place the length of the contents of the file,
7280 * or %NULL if the length is not needed
7281 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7282 * or %NULL if the entity tag is not needed
7283 * @error: a #GError, or %NULL
7285 * Finishes an asynchronous load of the @file's contents.
7286 * The contents are placed in @contents, and @length is set to the
7287 * size of the @contents string. The @content should be freed with
7288 * g_free() when no longer needed. If @etag_out is present, it will be
7289 * set to the new entity tag for the @file.
7291 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7292 * present, it will be set appropriately.
7294 gboolean
7295 g_file_load_contents_finish (GFile *file,
7296 GAsyncResult *res,
7297 char **contents,
7298 gsize *length,
7299 char **etag_out,
7300 GError **error)
7302 return g_file_load_partial_contents_finish (file,
7303 res,
7304 contents,
7305 length,
7306 etag_out,
7307 error);
7311 * g_file_replace_contents:
7312 * @file: input #GFile
7313 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7314 * @length: the length of @contents in bytes
7315 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7316 * or %NULL
7317 * @make_backup: %TRUE if a backup should be created
7318 * @flags: a set of #GFileCreateFlags
7319 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7320 * for the document. This should be freed with g_free() when no longer
7321 * needed, or %NULL
7322 * @cancellable: optional #GCancellable object, %NULL to ignore
7323 * @error: a #GError, or %NULL
7325 * Replaces the contents of @file with @contents of @length bytes.
7327 * If @etag is specified (not %NULL), any existing file must have that etag,
7328 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7330 * If @make_backup is %TRUE, this function will attempt to make a backup
7331 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7332 * file contents in the safest way possible. For example, atomic renames are
7333 * used when replacing local files’ contents.
7335 * If @cancellable is not %NULL, then the operation can be cancelled by
7336 * triggering the cancellable object from another thread. If the operation
7337 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7339 * The returned @new_etag can be used to verify that the file hasn't
7340 * changed the next time it is saved over.
7342 * Returns: %TRUE if successful. If an error has occurred, this function
7343 * will return %FALSE and set @error appropriately if present.
7345 gboolean
7346 g_file_replace_contents (GFile *file,
7347 const char *contents,
7348 gsize length,
7349 const char *etag,
7350 gboolean make_backup,
7351 GFileCreateFlags flags,
7352 char **new_etag,
7353 GCancellable *cancellable,
7354 GError **error)
7356 GFileOutputStream *out;
7357 gsize pos, remainder;
7358 gssize res;
7359 gboolean ret;
7361 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7362 g_return_val_if_fail (contents != NULL, FALSE);
7364 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7365 if (out == NULL)
7366 return FALSE;
7368 pos = 0;
7369 remainder = length;
7370 while (remainder > 0 &&
7371 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7372 contents + pos,
7373 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7374 cancellable,
7375 error)) > 0)
7377 pos += res;
7378 remainder -= res;
7381 if (remainder > 0 && res < 0)
7383 /* Ignore errors on close */
7384 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7385 g_object_unref (out);
7387 /* error is set already */
7388 return FALSE;
7391 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7393 if (new_etag)
7394 *new_etag = g_file_output_stream_get_etag (out);
7396 g_object_unref (out);
7398 return ret;
7401 typedef struct {
7402 GTask *task;
7403 GBytes *content;
7404 gsize pos;
7405 char *etag;
7406 gboolean failed;
7407 } ReplaceContentsData;
7409 static void
7410 replace_contents_data_free (ReplaceContentsData *data)
7412 g_bytes_unref (data->content);
7413 g_free (data->etag);
7414 g_free (data);
7417 static void
7418 replace_contents_close_callback (GObject *obj,
7419 GAsyncResult *close_res,
7420 gpointer user_data)
7422 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7423 ReplaceContentsData *data = user_data;
7425 /* Ignore errors here, we're only reading anyway */
7426 g_output_stream_close_finish (stream, close_res, NULL);
7427 g_object_unref (stream);
7429 if (!data->failed)
7431 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7432 g_task_return_boolean (data->task, TRUE);
7434 g_object_unref (data->task);
7437 static void
7438 replace_contents_write_callback (GObject *obj,
7439 GAsyncResult *read_res,
7440 gpointer user_data)
7442 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7443 ReplaceContentsData *data = user_data;
7444 GError *error = NULL;
7445 gssize write_size;
7447 write_size = g_output_stream_write_finish (stream, read_res, &error);
7449 if (write_size <= 0)
7451 /* Error or EOF, close the file */
7452 if (write_size < 0)
7454 data->failed = TRUE;
7455 g_task_return_error (data->task, error);
7457 g_output_stream_close_async (stream, 0,
7458 g_task_get_cancellable (data->task),
7459 replace_contents_close_callback, data);
7461 else if (write_size > 0)
7463 const gchar *content;
7464 gsize length;
7466 content = g_bytes_get_data (data->content, &length);
7467 data->pos += write_size;
7469 if (data->pos >= length)
7470 g_output_stream_close_async (stream, 0,
7471 g_task_get_cancellable (data->task),
7472 replace_contents_close_callback, data);
7473 else
7474 g_output_stream_write_async (stream,
7475 content + data->pos,
7476 length - data->pos,
7478 g_task_get_cancellable (data->task),
7479 replace_contents_write_callback,
7480 data);
7484 static void
7485 replace_contents_open_callback (GObject *obj,
7486 GAsyncResult *open_res,
7487 gpointer user_data)
7489 GFile *file = G_FILE (obj);
7490 GFileOutputStream *stream;
7491 ReplaceContentsData *data = user_data;
7492 GError *error = NULL;
7494 stream = g_file_replace_finish (file, open_res, &error);
7496 if (stream)
7498 const gchar *content;
7499 gsize length;
7501 content = g_bytes_get_data (data->content, &length);
7502 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7503 content + data->pos,
7504 length - data->pos,
7506 g_task_get_cancellable (data->task),
7507 replace_contents_write_callback,
7508 data);
7510 else
7512 g_task_return_error (data->task, error);
7513 g_object_unref (data->task);
7518 * g_file_replace_contents_async:
7519 * @file: input #GFile
7520 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7521 * @length: the length of @contents in bytes
7522 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7523 * @make_backup: %TRUE if a backup should be created
7524 * @flags: a set of #GFileCreateFlags
7525 * @cancellable: optional #GCancellable object, %NULL to ignore
7526 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7527 * @user_data: the data to pass to callback function
7529 * Starts an asynchronous replacement of @file with the given
7530 * @contents of @length bytes. @etag will replace the document's
7531 * current entity tag.
7533 * When this operation has completed, @callback will be called with
7534 * @user_user data, and the operation can be finalized with
7535 * g_file_replace_contents_finish().
7537 * If @cancellable is not %NULL, then the operation can be cancelled by
7538 * triggering the cancellable object from another thread. If the operation
7539 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7541 * If @make_backup is %TRUE, this function will attempt to
7542 * make a backup of @file.
7544 * Note that no copy of @content will be made, so it must stay valid
7545 * until @callback is called. See g_file_replace_contents_bytes_async()
7546 * for a #GBytes version that will automatically hold a reference to the
7547 * contents (without copying) for the duration of the call.
7549 void
7550 g_file_replace_contents_async (GFile *file,
7551 const char *contents,
7552 gsize length,
7553 const char *etag,
7554 gboolean make_backup,
7555 GFileCreateFlags flags,
7556 GCancellable *cancellable,
7557 GAsyncReadyCallback callback,
7558 gpointer user_data)
7560 GBytes *bytes;
7562 bytes = g_bytes_new_static (contents, length);
7563 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7564 cancellable, callback, user_data);
7565 g_bytes_unref (bytes);
7569 * g_file_replace_contents_bytes_async:
7570 * @file: input #GFile
7571 * @contents: a #GBytes
7572 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7573 * @make_backup: %TRUE if a backup should be created
7574 * @flags: a set of #GFileCreateFlags
7575 * @cancellable: optional #GCancellable object, %NULL to ignore
7576 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7577 * @user_data: the data to pass to callback function
7579 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7580 * This function will keep a ref on @contents until the operation is done.
7581 * Unlike g_file_replace_contents_async() this allows forgetting about the
7582 * content without waiting for the callback.
7584 * When this operation has completed, @callback will be called with
7585 * @user_user data, and the operation can be finalized with
7586 * g_file_replace_contents_finish().
7588 * Since: 2.40
7590 void
7591 g_file_replace_contents_bytes_async (GFile *file,
7592 GBytes *contents,
7593 const char *etag,
7594 gboolean make_backup,
7595 GFileCreateFlags flags,
7596 GCancellable *cancellable,
7597 GAsyncReadyCallback callback,
7598 gpointer user_data)
7600 ReplaceContentsData *data;
7602 g_return_if_fail (G_IS_FILE (file));
7603 g_return_if_fail (contents != NULL);
7605 data = g_new0 (ReplaceContentsData, 1);
7607 data->content = g_bytes_ref (contents);
7609 data->task = g_task_new (file, cancellable, callback, user_data);
7610 g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
7611 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7613 g_file_replace_async (file,
7614 etag,
7615 make_backup,
7616 flags,
7618 g_task_get_cancellable (data->task),
7619 replace_contents_open_callback,
7620 data);
7624 * g_file_replace_contents_finish:
7625 * @file: input #GFile
7626 * @res: a #GAsyncResult
7627 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7628 * for the document. This should be freed with g_free() when it is no
7629 * longer needed, or %NULL
7630 * @error: a #GError, or %NULL
7632 * Finishes an asynchronous replace of the given @file. See
7633 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7634 * tag for the document, if present.
7636 * Returns: %TRUE on success, %FALSE on failure.
7638 gboolean
7639 g_file_replace_contents_finish (GFile *file,
7640 GAsyncResult *res,
7641 char **new_etag,
7642 GError **error)
7644 GTask *task;
7645 ReplaceContentsData *data;
7647 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7648 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7650 task = G_TASK (res);
7652 if (!g_task_propagate_boolean (task, error))
7653 return FALSE;
7655 data = g_task_get_task_data (task);
7657 if (new_etag)
7659 *new_etag = data->etag;
7660 data->etag = NULL; /* Take ownership */
7663 return TRUE;
7666 gboolean
7667 g_file_real_measure_disk_usage (GFile *file,
7668 GFileMeasureFlags flags,
7669 GCancellable *cancellable,
7670 GFileMeasureProgressCallback progress_callback,
7671 gpointer progress_data,
7672 guint64 *disk_usage,
7673 guint64 *num_dirs,
7674 guint64 *num_files,
7675 GError **error)
7677 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7678 "Operation not supported for the current backend.");
7679 return FALSE;
7682 typedef struct
7684 GFileMeasureFlags flags;
7685 GFileMeasureProgressCallback progress_callback;
7686 gpointer progress_data;
7687 } MeasureTaskData;
7689 typedef struct
7691 guint64 disk_usage;
7692 guint64 num_dirs;
7693 guint64 num_files;
7694 } MeasureResult;
7696 typedef struct
7698 GFileMeasureProgressCallback callback;
7699 gpointer user_data;
7700 gboolean reporting;
7701 guint64 current_size;
7702 guint64 num_dirs;
7703 guint64 num_files;
7704 } MeasureProgress;
7706 static gboolean
7707 measure_disk_usage_invoke_progress (gpointer user_data)
7709 MeasureProgress *progress = user_data;
7711 (* progress->callback) (progress->reporting,
7712 progress->current_size, progress->num_dirs, progress->num_files,
7713 progress->user_data);
7715 return FALSE;
7718 static void
7719 measure_disk_usage_progress (gboolean reporting,
7720 guint64 current_size,
7721 guint64 num_dirs,
7722 guint64 num_files,
7723 gpointer user_data)
7725 MeasureProgress progress;
7726 GTask *task = user_data;
7727 MeasureTaskData *data;
7729 data = g_task_get_task_data (task);
7731 progress.callback = data->progress_callback;
7732 progress.user_data = data->progress_data;
7733 progress.reporting = reporting;
7734 progress.current_size = current_size;
7735 progress.num_dirs = num_dirs;
7736 progress.num_files = num_files;
7738 g_main_context_invoke_full (g_task_get_context (task),
7739 g_task_get_priority (task),
7740 measure_disk_usage_invoke_progress,
7741 g_memdup (&progress, sizeof progress),
7742 g_free);
7745 static void
7746 measure_disk_usage_thread (GTask *task,
7747 gpointer source_object,
7748 gpointer task_data,
7749 GCancellable *cancellable)
7751 MeasureTaskData *data = task_data;
7752 GError *error = NULL;
7753 MeasureResult result = { 0, };
7755 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7756 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7757 &result.disk_usage, &result.num_dirs, &result.num_files,
7758 &error))
7759 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7760 else
7761 g_task_return_error (task, error);
7764 static void
7765 g_file_real_measure_disk_usage_async (GFile *file,
7766 GFileMeasureFlags flags,
7767 gint io_priority,
7768 GCancellable *cancellable,
7769 GFileMeasureProgressCallback progress_callback,
7770 gpointer progress_data,
7771 GAsyncReadyCallback callback,
7772 gpointer user_data)
7774 MeasureTaskData data;
7775 GTask *task;
7777 data.flags = flags;
7778 data.progress_callback = progress_callback;
7779 data.progress_data = progress_data;
7781 task = g_task_new (file, cancellable, callback, user_data);
7782 g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
7783 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7784 g_task_set_priority (task, io_priority);
7786 g_task_run_in_thread (task, measure_disk_usage_thread);
7787 g_object_unref (task);
7790 static gboolean
7791 g_file_real_measure_disk_usage_finish (GFile *file,
7792 GAsyncResult *result,
7793 guint64 *disk_usage,
7794 guint64 *num_dirs,
7795 guint64 *num_files,
7796 GError **error)
7798 MeasureResult *measure_result;
7800 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7802 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7804 if (measure_result == NULL)
7805 return FALSE;
7807 if (disk_usage)
7808 *disk_usage = measure_result->disk_usage;
7810 if (num_dirs)
7811 *num_dirs = measure_result->num_dirs;
7813 if (num_files)
7814 *num_files = measure_result->num_files;
7816 g_free (measure_result);
7818 return TRUE;
7822 * g_file_measure_disk_usage:
7823 * @file: a #GFile
7824 * @flags: #GFileMeasureFlags
7825 * @cancellable: (nullable): optional #GCancellable
7826 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7827 * @progress_data: user_data for @progress_callback
7828 * @disk_usage: (out) (optional): the number of bytes of disk space used
7829 * @num_dirs: (out) (optional): the number of directories encountered
7830 * @num_files: (out) (optional): the number of non-directories encountered
7831 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7833 * Recursively measures the disk usage of @file.
7835 * This is essentially an analog of the 'du' command, but it also
7836 * reports the number of directories and non-directory files encountered
7837 * (including things like symbolic links).
7839 * By default, errors are only reported against the toplevel file
7840 * itself. Errors found while recursing are silently ignored, unless
7841 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7843 * The returned size, @disk_usage, is in bytes and should be formatted
7844 * with g_format_size() in order to get something reasonable for showing
7845 * in a user interface.
7847 * @progress_callback and @progress_data can be given to request
7848 * periodic progress updates while scanning. See the documentation for
7849 * #GFileMeasureProgressCallback for information about when and how the
7850 * callback will be invoked.
7852 * Returns: %TRUE if successful, with the out parameters set.
7853 * %FALSE otherwise, with @error set.
7855 * Since: 2.38
7857 gboolean
7858 g_file_measure_disk_usage (GFile *file,
7859 GFileMeasureFlags flags,
7860 GCancellable *cancellable,
7861 GFileMeasureProgressCallback progress_callback,
7862 gpointer progress_data,
7863 guint64 *disk_usage,
7864 guint64 *num_dirs,
7865 guint64 *num_files,
7866 GError **error)
7868 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7869 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7870 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7872 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7873 progress_callback, progress_data,
7874 disk_usage, num_dirs, num_files,
7875 error);
7879 * g_file_measure_disk_usage_async:
7880 * @file: a #GFile
7881 * @flags: #GFileMeasureFlags
7882 * @io_priority: the [I/O priority][io-priority] of the request
7883 * @cancellable: (nullable): optional #GCancellable
7884 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7885 * @progress_data: user_data for @progress_callback
7886 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7887 * @user_data: the data to pass to callback function
7889 * Recursively measures the disk usage of @file.
7891 * This is the asynchronous version of g_file_measure_disk_usage(). See
7892 * there for more information.
7894 * Since: 2.38
7896 void
7897 g_file_measure_disk_usage_async (GFile *file,
7898 GFileMeasureFlags flags,
7899 gint io_priority,
7900 GCancellable *cancellable,
7901 GFileMeasureProgressCallback progress_callback,
7902 gpointer progress_data,
7903 GAsyncReadyCallback callback,
7904 gpointer user_data)
7906 g_return_if_fail (G_IS_FILE (file));
7907 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7909 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7910 progress_callback, progress_data,
7911 callback, user_data);
7915 * g_file_measure_disk_usage_finish:
7916 * @file: a #GFile
7917 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7918 * @disk_usage: (out) (optional): the number of bytes of disk space used
7919 * @num_dirs: (out) (optional): the number of directories encountered
7920 * @num_files: (out) (optional): the number of non-directories encountered
7921 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7923 * Collects the results from an earlier call to
7924 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7925 * more information.
7927 * Returns: %TRUE if successful, with the out parameters set.
7928 * %FALSE otherwise, with @error set.
7930 * Since: 2.38
7932 gboolean
7933 g_file_measure_disk_usage_finish (GFile *file,
7934 GAsyncResult *result,
7935 guint64 *disk_usage,
7936 guint64 *num_dirs,
7937 guint64 *num_files,
7938 GError **error)
7940 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7941 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7943 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7947 * g_file_start_mountable:
7948 * @file: input #GFile
7949 * @flags: flags affecting the operation
7950 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7951 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7952 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7953 * @user_data: the data to pass to callback function
7955 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7956 * Using @start_operation, you can request callbacks when, for instance,
7957 * passwords are needed during authentication.
7959 * If @cancellable is not %NULL, then the operation can be cancelled by
7960 * triggering the cancellable object from another thread. If the operation
7961 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7963 * When the operation is finished, @callback will be called.
7964 * You can then call g_file_mount_mountable_finish() to get
7965 * the result of the operation.
7967 * Since: 2.22
7969 void
7970 g_file_start_mountable (GFile *file,
7971 GDriveStartFlags flags,
7972 GMountOperation *start_operation,
7973 GCancellable *cancellable,
7974 GAsyncReadyCallback callback,
7975 gpointer user_data)
7977 GFileIface *iface;
7979 g_return_if_fail (G_IS_FILE (file));
7981 iface = G_FILE_GET_IFACE (file);
7983 if (iface->start_mountable == NULL)
7985 g_task_report_new_error (file, callback, user_data,
7986 g_file_start_mountable,
7987 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7988 _("Operation not supported"));
7989 return;
7992 (* iface->start_mountable) (file,
7993 flags,
7994 start_operation,
7995 cancellable,
7996 callback,
7997 user_data);
8001 * g_file_start_mountable_finish:
8002 * @file: input #GFile
8003 * @result: a #GAsyncResult
8004 * @error: a #GError, or %NULL
8006 * Finishes a start operation. See g_file_start_mountable() for details.
8008 * Finish an asynchronous start operation that was started
8009 * with g_file_start_mountable().
8011 * Returns: %TRUE if the operation finished successfully. %FALSE
8012 * otherwise.
8014 * Since: 2.22
8016 gboolean
8017 g_file_start_mountable_finish (GFile *file,
8018 GAsyncResult *result,
8019 GError **error)
8021 GFileIface *iface;
8023 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8024 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8026 if (g_async_result_legacy_propagate_error (result, error))
8027 return FALSE;
8028 else if (g_async_result_is_tagged (result, g_file_start_mountable))
8029 return g_task_propagate_boolean (G_TASK (result), error);
8031 iface = G_FILE_GET_IFACE (file);
8032 return (* iface->start_mountable_finish) (file, result, error);
8036 * g_file_stop_mountable:
8037 * @file: input #GFile
8038 * @flags: flags affecting the operation
8039 * @mount_operation: (nullable): a #GMountOperation,
8040 * or %NULL to avoid user interaction.
8041 * @cancellable: (nullable): optional #GCancellable object,
8042 * %NULL to ignore
8043 * @callback: (nullable): a #GAsyncReadyCallback to call
8044 * when the request is satisfied, or %NULL
8045 * @user_data: the data to pass to callback function
8047 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8049 * If @cancellable is not %NULL, then the operation can be cancelled by
8050 * triggering the cancellable object from another thread. If the operation
8051 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8053 * When the operation is finished, @callback will be called.
8054 * You can then call g_file_stop_mountable_finish() to get
8055 * the result of the operation.
8057 * Since: 2.22
8059 void
8060 g_file_stop_mountable (GFile *file,
8061 GMountUnmountFlags flags,
8062 GMountOperation *mount_operation,
8063 GCancellable *cancellable,
8064 GAsyncReadyCallback callback,
8065 gpointer user_data)
8067 GFileIface *iface;
8069 g_return_if_fail (G_IS_FILE (file));
8071 iface = G_FILE_GET_IFACE (file);
8073 if (iface->stop_mountable == NULL)
8075 g_task_report_new_error (file, callback, user_data,
8076 g_file_stop_mountable,
8077 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8078 _("Operation not supported"));
8079 return;
8082 (* iface->stop_mountable) (file,
8083 flags,
8084 mount_operation,
8085 cancellable,
8086 callback,
8087 user_data);
8091 * g_file_stop_mountable_finish:
8092 * @file: input #GFile
8093 * @result: a #GAsyncResult
8094 * @error: a #GError, or %NULL
8096 * Finishes an stop operation, see g_file_stop_mountable() for details.
8098 * Finish an asynchronous stop operation that was started
8099 * with g_file_stop_mountable().
8101 * Returns: %TRUE if the operation finished successfully.
8102 * %FALSE otherwise.
8104 * Since: 2.22
8106 gboolean
8107 g_file_stop_mountable_finish (GFile *file,
8108 GAsyncResult *result,
8109 GError **error)
8111 GFileIface *iface;
8113 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8114 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8116 if (g_async_result_legacy_propagate_error (result, error))
8117 return FALSE;
8118 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
8119 return g_task_propagate_boolean (G_TASK (result), error);
8121 iface = G_FILE_GET_IFACE (file);
8122 return (* iface->stop_mountable_finish) (file, result, error);
8126 * g_file_poll_mountable:
8127 * @file: input #GFile
8128 * @cancellable: optional #GCancellable object, %NULL to ignore
8129 * @callback: (nullable): a #GAsyncReadyCallback to call
8130 * when the request is satisfied, or %NULL
8131 * @user_data: the data to pass to callback function
8133 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8135 * If @cancellable is not %NULL, then the operation can be cancelled by
8136 * triggering the cancellable object from another thread. If the operation
8137 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8139 * When the operation is finished, @callback will be called.
8140 * You can then call g_file_mount_mountable_finish() to get
8141 * the result of the operation.
8143 * Since: 2.22
8145 void
8146 g_file_poll_mountable (GFile *file,
8147 GCancellable *cancellable,
8148 GAsyncReadyCallback callback,
8149 gpointer user_data)
8151 GFileIface *iface;
8153 g_return_if_fail (G_IS_FILE (file));
8155 iface = G_FILE_GET_IFACE (file);
8157 if (iface->poll_mountable == NULL)
8159 g_task_report_new_error (file, callback, user_data,
8160 g_file_poll_mountable,
8161 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8162 _("Operation not supported"));
8163 return;
8166 (* iface->poll_mountable) (file,
8167 cancellable,
8168 callback,
8169 user_data);
8173 * g_file_poll_mountable_finish:
8174 * @file: input #GFile
8175 * @result: a #GAsyncResult
8176 * @error: a #GError, or %NULL
8178 * Finishes a poll operation. See g_file_poll_mountable() for details.
8180 * Finish an asynchronous poll operation that was polled
8181 * with g_file_poll_mountable().
8183 * Returns: %TRUE if the operation finished successfully. %FALSE
8184 * otherwise.
8186 * Since: 2.22
8188 gboolean
8189 g_file_poll_mountable_finish (GFile *file,
8190 GAsyncResult *result,
8191 GError **error)
8193 GFileIface *iface;
8195 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8196 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8198 if (g_async_result_legacy_propagate_error (result, error))
8199 return FALSE;
8200 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8201 return g_task_propagate_boolean (G_TASK (result), error);
8203 iface = G_FILE_GET_IFACE (file);
8204 return (* iface->poll_mountable_finish) (file, result, error);
8208 * g_file_supports_thread_contexts:
8209 * @file: a #GFile
8211 * Checks if @file supports
8212 * [thread-default contexts][g-main-context-push-thread-default-context].
8213 * If this returns %FALSE, you cannot perform asynchronous operations on
8214 * @file in a thread that has a thread-default context.
8216 * Returns: Whether or not @file supports thread-default contexts.
8218 * Since: 2.22
8220 gboolean
8221 g_file_supports_thread_contexts (GFile *file)
8223 GFileIface *iface;
8225 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8227 iface = G_FILE_GET_IFACE (file);
8228 return iface->supports_thread_contexts;
8232 * g_file_load_bytes:
8233 * @file: a #GFile
8234 * @cancellable: (nullable): a #GCancellable or %NULL
8235 * @etag_out: (out) (nullable) (optional): a location to place the current
8236 * entity tag for the file, or %NULL if the entity tag is not needed
8237 * @error: a location for a #GError or %NULL
8239 * Loads the contents of @file and returns it as #GBytes.
8241 * If @file is a resource:// based URI, the resulting bytes will reference the
8242 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8243 * g_file_load_contents() and g_bytes_new_take().
8245 * For resources, @etag_out will be set to %NULL.
8247 * The data contained in the resulting #GBytes is always zero-terminated, but
8248 * this is not included in the #GBytes length. The resulting #GBytes should be
8249 * freed with g_bytes_unref() when no longer in use.
8251 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8253 * Since: 2.56
8255 GBytes *
8256 g_file_load_bytes (GFile *file,
8257 GCancellable *cancellable,
8258 gchar **etag_out,
8259 GError **error)
8261 gchar *contents;
8262 gsize len;
8264 g_return_val_if_fail (G_IS_FILE (file), NULL);
8265 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
8266 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8268 if (etag_out != NULL)
8269 *etag_out = NULL;
8271 if (g_file_has_uri_scheme (file, "resource"))
8273 GBytes *bytes;
8274 gchar *uri, *unescaped;
8276 uri = g_file_get_uri (file);
8277 unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
8278 g_free (uri);
8280 bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
8281 g_free (unescaped);
8283 return bytes;
8286 /* contents is guaranteed to be \0 terminated */
8287 if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
8288 return g_bytes_new_take (g_steal_pointer (&contents), len);
8290 return NULL;
8293 static void
8294 g_file_load_bytes_cb (GObject *object,
8295 GAsyncResult *result,
8296 gpointer user_data)
8298 GFile *file = G_FILE (object);
8299 GTask *task = user_data;
8300 GError *error = NULL;
8301 gchar *etag = NULL;
8302 gchar *contents = NULL;
8303 gsize len = 0;
8305 g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
8306 g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
8308 if (error != NULL)
8309 g_task_return_error (task, g_steal_pointer (&error));
8310 else
8311 g_task_return_pointer (task,
8312 g_bytes_new_take (g_steal_pointer (&contents), len),
8313 (GDestroyNotify)g_bytes_unref);
8315 g_object_unref (task);
8319 * g_file_load_bytes_async:
8320 * @file: a #GFile
8321 * @cancellable: (nullable): a #GCancellable or %NULL
8322 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8323 * request is satisfied
8324 * @user_data: (closure): the data to pass to callback function
8326 * Asynchronously loads the contents of @file as #GBytes.
8328 * If @file is a resource:// based URI, the resulting bytes will reference the
8329 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8330 * g_file_load_contents_async() and g_bytes_new_take().
8332 * @callback should call g_file_load_bytes_finish() to get the result of this
8333 * asynchronous operation.
8335 * See g_file_load_bytes() for more information.
8337 * Since: 2.56
8339 void
8340 g_file_load_bytes_async (GFile *file,
8341 GCancellable *cancellable,
8342 GAsyncReadyCallback callback,
8343 gpointer user_data)
8345 GError *error = NULL;
8346 GBytes *bytes;
8347 GTask *task;
8349 g_return_if_fail (G_IS_FILE (file));
8350 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
8352 task = g_task_new (file, cancellable, callback, user_data);
8353 g_task_set_source_tag (task, g_file_load_bytes_async);
8355 if (!g_file_has_uri_scheme (file, "resource"))
8357 g_file_load_contents_async (file,
8358 cancellable,
8359 g_file_load_bytes_cb,
8360 g_steal_pointer (&task));
8361 return;
8364 bytes = g_file_load_bytes (file, cancellable, NULL, &error);
8366 if (bytes == NULL)
8367 g_task_return_error (task, g_steal_pointer (&error));
8368 else
8369 g_task_return_pointer (task,
8370 g_steal_pointer (&bytes),
8371 (GDestroyNotify)g_bytes_unref);
8373 g_object_unref (task);
8377 * g_file_load_bytes_finish:
8378 * @file: a #GFile
8379 * @result: a #GAsyncResult provided to the callback
8380 * @etag_out: (out) (nullable) (optional): a location to place the current
8381 * entity tag for the file, or %NULL if the entity tag is not needed
8382 * @error: a location for a #GError, or %NULL
8384 * Completes an asynchronous request to g_file_load_bytes_async().
8386 * For resources, @etag_out will be set to %NULL.
8388 * The data contained in the resulting #GBytes is always zero-terminated, but
8389 * this is not included in the #GBytes length. The resulting #GBytes should be
8390 * freed with g_bytes_unref() when no longer in use.
8392 * See g_file_load_bytes() for more information.
8394 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8396 * Since: 2.56
8398 GBytes *
8399 g_file_load_bytes_finish (GFile *file,
8400 GAsyncResult *result,
8401 gchar **etag_out,
8402 GError **error)
8404 GBytes *bytes;
8406 g_return_val_if_fail (G_IS_FILE (file), NULL);
8407 g_return_val_if_fail (G_IS_TASK (result), NULL);
8408 g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
8409 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
8411 bytes = g_task_propagate_pointer (G_TASK (result), error);
8413 if (etag_out != NULL)
8414 *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));
8416 return bytes;