Partly revert "gio: Add filename type annotations"
[glib.git] / gio / gfile.c
blob6226e360c74fdf4a4a755de959edb4e81a185ee8
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 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().
85 * One way to think of a #GFile is as an abstraction of a pathname. For
86 * normal files the system pathname is what is stored internally, but as
87 * #GFiles are extensible it could also be something else that corresponds
88 * to a pathname in a userspace implementation of a filesystem.
90 * #GFiles make up hierarchies of directories and files that correspond to
91 * the files on a filesystem. You can move through the file system with
92 * #GFile using g_file_get_parent() to get an identifier for the parent
93 * directory, g_file_get_child() to get a child within a directory,
94 * g_file_resolve_relative_path() to resolve a relative path between two
95 * #GFiles. There can be multiple hierarchies, so you may not end up at
96 * the same root if you repeatedly call g_file_get_parent() on two different
97 * files.
99 * All #GFiles have a basename (get with g_file_get_basename()). These names
100 * are byte strings that are used to identify the file on the filesystem
101 * (relative to its parent directory) and there is no guarantees that they
102 * have any particular charset encoding or even make any sense at all. If
103 * you want to use filenames in a user interface you should use the display
104 * name that you can get by requesting the
105 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
106 * This is guaranteed to be in UTF-8 and can be used in a user interface.
107 * But always store the real basename or the #GFile to use to actually
108 * access the file, because there is no way to go from a display name to
109 * the actual name.
111 * Using #GFile as an identifier has the same weaknesses as using a path
112 * in that there may be multiple aliases for the same file. For instance,
113 * hard or soft links may cause two different #GFiles to refer to the same
114 * file. Other possible causes for aliases are: case insensitive filesystems,
115 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
116 * check if two #GFiles point to the same file you can query for the
117 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
118 * canonicalization of pathnames passed in, so that trivial differences in
119 * the path string used at creation (duplicated slashes, slash at end of
120 * path, "." or ".." path segments, etc) does not create different #GFiles.
122 * Many #GFile operations have both synchronous and asynchronous versions
123 * to suit your application. Asynchronous versions of synchronous functions
124 * simply have _async() appended to their function names. The asynchronous
125 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
126 * the operation, producing a GAsyncResult which is then passed to the
127 * function's matching _finish() operation.
129 * It is highly recommended to use asynchronous calls when running within a
130 * shared main loop, such as in the main thread of an application. This avoids
131 * I/O operations blocking other sources on the main loop from being dispatched.
132 * Synchronous I/O operations should be performed from worker threads. See the
133 * [introduction to asynchronous programming section][async-programming] for
134 * more.
136 * Some #GFile operations almost always take a noticeable amount of time, and
137 * so do not have synchronous analogs. Notable cases include:
138 * - g_file_mount_mountable() to mount a mountable file.
139 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
140 * - g_file_eject_mountable_with_operation() to eject a mountable file.
142 * ## Entity Tags # {#gfile-etag}
144 * One notable feature of #GFiles are entity tags, or "etags" for
145 * short. Entity tags are somewhat like a more abstract version of the
146 * traditional mtime, and can be used to quickly determine if the file
147 * has been modified from the version on the file system. See the
148 * HTTP 1.1
149 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
150 * for HTTP Etag headers, which are a very similar concept.
153 static void g_file_real_query_info_async (GFile *file,
154 const char *attributes,
155 GFileQueryInfoFlags flags,
156 int io_priority,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
159 gpointer user_data);
160 static GFileInfo * g_file_real_query_info_finish (GFile *file,
161 GAsyncResult *res,
162 GError **error);
163 static void g_file_real_query_filesystem_info_async (GFile *file,
164 const char *attributes,
165 int io_priority,
166 GCancellable *cancellable,
167 GAsyncReadyCallback callback,
168 gpointer user_data);
169 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
170 GAsyncResult *res,
171 GError **error);
172 static void g_file_real_enumerate_children_async (GFile *file,
173 const char *attributes,
174 GFileQueryInfoFlags flags,
175 int io_priority,
176 GCancellable *cancellable,
177 GAsyncReadyCallback callback,
178 gpointer user_data);
179 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
180 GAsyncResult *res,
181 GError **error);
182 static void g_file_real_read_async (GFile *file,
183 int io_priority,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
186 gpointer user_data);
187 static GFileInputStream * g_file_real_read_finish (GFile *file,
188 GAsyncResult *res,
189 GError **error);
190 static void g_file_real_append_to_async (GFile *file,
191 GFileCreateFlags flags,
192 int io_priority,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
195 gpointer user_data);
196 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
197 GAsyncResult *res,
198 GError **error);
199 static void g_file_real_create_async (GFile *file,
200 GFileCreateFlags flags,
201 int io_priority,
202 GCancellable *cancellable,
203 GAsyncReadyCallback callback,
204 gpointer user_data);
205 static GFileOutputStream *g_file_real_create_finish (GFile *file,
206 GAsyncResult *res,
207 GError **error);
208 static void g_file_real_replace_async (GFile *file,
209 const char *etag,
210 gboolean make_backup,
211 GFileCreateFlags flags,
212 int io_priority,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
215 gpointer user_data);
216 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
217 GAsyncResult *res,
218 GError **error);
219 static void g_file_real_delete_async (GFile *file,
220 int io_priority,
221 GCancellable *cancellable,
222 GAsyncReadyCallback callback,
223 gpointer user_data);
224 static gboolean g_file_real_delete_finish (GFile *file,
225 GAsyncResult *res,
226 GError **error);
227 static void g_file_real_trash_async (GFile *file,
228 int io_priority,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
231 gpointer user_data);
232 static gboolean g_file_real_trash_finish (GFile *file,
233 GAsyncResult *res,
234 GError **error);
235 static void g_file_real_make_directory_async (GFile *file,
236 int io_priority,
237 GCancellable *cancellable,
238 GAsyncReadyCallback callback,
239 gpointer user_data);
240 static gboolean g_file_real_make_directory_finish (GFile *file,
241 GAsyncResult *res,
242 GError **error);
243 static void g_file_real_open_readwrite_async (GFile *file,
244 int io_priority,
245 GCancellable *cancellable,
246 GAsyncReadyCallback callback,
247 gpointer user_data);
248 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
249 GAsyncResult *res,
250 GError **error);
251 static void g_file_real_create_readwrite_async (GFile *file,
252 GFileCreateFlags flags,
253 int io_priority,
254 GCancellable *cancellable,
255 GAsyncReadyCallback callback,
256 gpointer user_data);
257 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
258 GAsyncResult *res,
259 GError **error);
260 static void g_file_real_replace_readwrite_async (GFile *file,
261 const char *etag,
262 gboolean make_backup,
263 GFileCreateFlags flags,
264 int io_priority,
265 GCancellable *cancellable,
266 GAsyncReadyCallback callback,
267 gpointer user_data);
268 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
269 GAsyncResult *res,
270 GError **error);
271 static gboolean g_file_real_set_attributes_from_info (GFile *file,
272 GFileInfo *info,
273 GFileQueryInfoFlags flags,
274 GCancellable *cancellable,
275 GError **error);
276 static void g_file_real_set_display_name_async (GFile *file,
277 const char *display_name,
278 int io_priority,
279 GCancellable *cancellable,
280 GAsyncReadyCallback callback,
281 gpointer user_data);
282 static GFile * g_file_real_set_display_name_finish (GFile *file,
283 GAsyncResult *res,
284 GError **error);
285 static void g_file_real_set_attributes_async (GFile *file,
286 GFileInfo *info,
287 GFileQueryInfoFlags flags,
288 int io_priority,
289 GCancellable *cancellable,
290 GAsyncReadyCallback callback,
291 gpointer user_data);
292 static gboolean g_file_real_set_attributes_finish (GFile *file,
293 GAsyncResult *res,
294 GFileInfo **info,
295 GError **error);
296 static void g_file_real_find_enclosing_mount_async (GFile *file,
297 int io_priority,
298 GCancellable *cancellable,
299 GAsyncReadyCallback callback,
300 gpointer user_data);
301 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
302 GAsyncResult *res,
303 GError **error);
304 static void g_file_real_copy_async (GFile *source,
305 GFile *destination,
306 GFileCopyFlags flags,
307 int io_priority,
308 GCancellable *cancellable,
309 GFileProgressCallback progress_callback,
310 gpointer progress_callback_data,
311 GAsyncReadyCallback callback,
312 gpointer user_data);
313 static gboolean g_file_real_copy_finish (GFile *file,
314 GAsyncResult *res,
315 GError **error);
317 static gboolean g_file_real_measure_disk_usage (GFile *file,
318 GFileMeasureFlags flags,
319 GCancellable *cancellable,
320 GFileMeasureProgressCallback progress_callback,
321 gpointer progress_data,
322 guint64 *disk_usage,
323 guint64 *num_dirs,
324 guint64 *num_files,
325 GError **error);
326 static void g_file_real_measure_disk_usage_async (GFile *file,
327 GFileMeasureFlags flags,
328 gint io_priority,
329 GCancellable *cancellable,
330 GFileMeasureProgressCallback progress_callback,
331 gpointer progress_data,
332 GAsyncReadyCallback callback,
333 gpointer user_data);
334 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
335 GAsyncResult *result,
336 guint64 *disk_usage,
337 guint64 *num_dirs,
338 guint64 *num_files,
339 GError **error);
341 typedef GFileIface GFileInterface;
342 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
344 static void
345 g_file_default_init (GFileIface *iface)
347 iface->enumerate_children_async = g_file_real_enumerate_children_async;
348 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
349 iface->set_display_name_async = g_file_real_set_display_name_async;
350 iface->set_display_name_finish = g_file_real_set_display_name_finish;
351 iface->query_info_async = g_file_real_query_info_async;
352 iface->query_info_finish = g_file_real_query_info_finish;
353 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
354 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
355 iface->set_attributes_async = g_file_real_set_attributes_async;
356 iface->set_attributes_finish = g_file_real_set_attributes_finish;
357 iface->read_async = g_file_real_read_async;
358 iface->read_finish = g_file_real_read_finish;
359 iface->append_to_async = g_file_real_append_to_async;
360 iface->append_to_finish = g_file_real_append_to_finish;
361 iface->create_async = g_file_real_create_async;
362 iface->create_finish = g_file_real_create_finish;
363 iface->replace_async = g_file_real_replace_async;
364 iface->replace_finish = g_file_real_replace_finish;
365 iface->delete_file_async = g_file_real_delete_async;
366 iface->delete_file_finish = g_file_real_delete_finish;
367 iface->trash_async = g_file_real_trash_async;
368 iface->trash_finish = g_file_real_trash_finish;
369 iface->make_directory_async = g_file_real_make_directory_async;
370 iface->make_directory_finish = g_file_real_make_directory_finish;
371 iface->open_readwrite_async = g_file_real_open_readwrite_async;
372 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
373 iface->create_readwrite_async = g_file_real_create_readwrite_async;
374 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
375 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
376 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
377 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
378 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
379 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
380 iface->copy_async = g_file_real_copy_async;
381 iface->copy_finish = g_file_real_copy_finish;
382 iface->measure_disk_usage = g_file_real_measure_disk_usage;
383 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
384 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
389 * g_file_is_native:
390 * @file: input #GFile
392 * Checks to see if a file is native to the platform.
394 * A native file s one expressed in the platform-native filename format,
395 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
396 * as it might be on a locally mounted remote filesystem.
398 * On some systems non-native files may be available using the native
399 * filesystem via a userspace filesystem (FUSE), in these cases this call
400 * will return %FALSE, but g_file_get_path() will still return a native path.
402 * This call does no blocking I/O.
404 * Returns: %TRUE if @file is native
406 gboolean
407 g_file_is_native (GFile *file)
409 GFileIface *iface;
411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
413 iface = G_FILE_GET_IFACE (file);
415 return (* iface->is_native) (file);
420 * g_file_has_uri_scheme:
421 * @file: input #GFile
422 * @uri_scheme: a string containing a URI scheme
424 * Checks to see if a #GFile has a given URI scheme.
426 * This call does no blocking I/O.
428 * Returns: %TRUE if #GFile's backend supports the
429 * given URI scheme, %FALSE if URI scheme is %NULL,
430 * not supported, or #GFile is invalid.
432 gboolean
433 g_file_has_uri_scheme (GFile *file,
434 const char *uri_scheme)
436 GFileIface *iface;
438 g_return_val_if_fail (G_IS_FILE (file), FALSE);
439 g_return_val_if_fail (uri_scheme != NULL, FALSE);
441 iface = G_FILE_GET_IFACE (file);
443 return (* iface->has_uri_scheme) (file, uri_scheme);
448 * g_file_get_uri_scheme:
449 * @file: input #GFile
451 * Gets the URI scheme for a #GFile.
452 * RFC 3986 decodes the scheme as:
453 * |[
454 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
455 * ]|
456 * Common schemes include "file", "http", "ftp", etc.
458 * This call does no blocking I/O.
460 * Returns: a string containing the URI scheme for the given
461 * #GFile. The returned string should be freed with g_free()
462 * when no longer needed.
464 char *
465 g_file_get_uri_scheme (GFile *file)
467 GFileIface *iface;
469 g_return_val_if_fail (G_IS_FILE (file), NULL);
471 iface = G_FILE_GET_IFACE (file);
473 return (* iface->get_uri_scheme) (file);
478 * g_file_get_basename:
479 * @file: input #GFile
481 * Gets the base name (the last component of the path) for a given #GFile.
483 * If called for the top level of a system (such as the filesystem root
484 * or a uri like sftp://host/) it will return a single directory separator
485 * (and on Windows, possibly a drive letter).
487 * The base name is a byte string (not UTF-8). It has no defined encoding
488 * or rules other than it may not contain zero bytes. If you want to use
489 * filenames in a user interface you should use the display name that you
490 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
491 * attribute with g_file_query_info().
493 * This call does no blocking I/O.
495 * Returns: (type filename) (nullable): string containing the #GFile's
496 * base name, or %NULL if given #GFile is invalid. The returned string
497 * should be freed with g_free() when no longer needed.
499 char *
500 g_file_get_basename (GFile *file)
502 GFileIface *iface;
504 g_return_val_if_fail (G_IS_FILE (file), NULL);
506 iface = G_FILE_GET_IFACE (file);
508 return (* iface->get_basename) (file);
512 * g_file_get_path:
513 * @file: input #GFile
515 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
516 * guaranteed to be an absolute, canonical path. It might contain symlinks.
518 * This call does no blocking I/O.
520 * Returns: (type filename) (nullable): string containing the #GFile's path,
521 * or %NULL if no such path exists. The returned string should be freed
522 * with g_free() when no longer needed.
524 char *
525 g_file_get_path (GFile *file)
527 GFileIface *iface;
529 g_return_val_if_fail (G_IS_FILE (file), NULL);
531 iface = G_FILE_GET_IFACE (file);
533 return (* iface->get_path) (file);
537 * g_file_get_uri:
538 * @file: input #GFile
540 * Gets the URI for the @file.
542 * This call does no blocking I/O.
544 * Returns: a string containing the #GFile's URI.
545 * The returned string should be freed with g_free()
546 * when no longer needed.
548 char *
549 g_file_get_uri (GFile *file)
551 GFileIface *iface;
553 g_return_val_if_fail (G_IS_FILE (file), NULL);
555 iface = G_FILE_GET_IFACE (file);
557 return (* iface->get_uri) (file);
561 * g_file_get_parse_name:
562 * @file: input #GFile
564 * Gets the parse name of the @file.
565 * A parse name is a UTF-8 string that describes the
566 * file such that one can get the #GFile back using
567 * g_file_parse_name().
569 * This is generally used to show the #GFile as a nice
570 * full-pathname kind of string in a user interface,
571 * like in a location entry.
573 * For local files with names that can safely be converted
574 * to UTF-8 the pathname is used, otherwise the IRI is used
575 * (a form of URI that allows UTF-8 characters unescaped).
577 * This call does no blocking I/O.
579 * Returns: a string containing the #GFile's parse name.
580 * The returned string should be freed with g_free()
581 * when no longer needed.
583 char *
584 g_file_get_parse_name (GFile *file)
586 GFileIface *iface;
588 g_return_val_if_fail (G_IS_FILE (file), NULL);
590 iface = G_FILE_GET_IFACE (file);
592 return (* iface->get_parse_name) (file);
596 * g_file_dup:
597 * @file: input #GFile
599 * Duplicates a #GFile handle. This operation does not duplicate
600 * the actual file or directory represented by the #GFile; see
601 * g_file_copy() if attempting to copy a file.
603 * This call does no blocking I/O.
605 * Returns: (transfer full): a new #GFile that is a duplicate
606 * of the given #GFile.
608 GFile *
609 g_file_dup (GFile *file)
611 GFileIface *iface;
613 g_return_val_if_fail (G_IS_FILE (file), NULL);
615 iface = G_FILE_GET_IFACE (file);
617 return (* iface->dup) (file);
621 * g_file_hash:
622 * @file: (type GFile): #gconstpointer to a #GFile
624 * Creates a hash value for a #GFile.
626 * This call does no blocking I/O.
628 * Virtual: hash
629 * Returns: 0 if @file is not a valid #GFile, otherwise an
630 * integer that can be used as hash value for the #GFile.
631 * This function is intended for easily hashing a #GFile to
632 * add to a #GHashTable or similar data structure.
634 guint
635 g_file_hash (gconstpointer file)
637 GFileIface *iface;
639 g_return_val_if_fail (G_IS_FILE (file), 0);
641 iface = G_FILE_GET_IFACE (file);
643 return (* iface->hash) ((GFile *)file);
647 * g_file_equal:
648 * @file1: the first #GFile
649 * @file2: the second #GFile
651 * Checks if the two given #GFiles refer to the same file.
653 * Note that two #GFiles that differ can still refer to the same
654 * file on the filesystem due to various forms of filename
655 * aliasing.
657 * This call does no blocking I/O.
659 * Returns: %TRUE if @file1 and @file2 are equal.
661 gboolean
662 g_file_equal (GFile *file1,
663 GFile *file2)
665 GFileIface *iface;
667 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
668 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
670 if (file1 == file2)
671 return TRUE;
673 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
674 return FALSE;
676 iface = G_FILE_GET_IFACE (file1);
678 return (* iface->equal) (file1, file2);
683 * g_file_get_parent:
684 * @file: input #GFile
686 * Gets the parent directory for the @file.
687 * If the @file represents the root directory of the
688 * file system, then %NULL will be returned.
690 * This call does no blocking I/O.
692 * Returns: (nullable) (transfer full): a #GFile structure to the
693 * parent of the given #GFile or %NULL if there is no parent. Free
694 * the returned object with g_object_unref().
696 GFile *
697 g_file_get_parent (GFile *file)
699 GFileIface *iface;
701 g_return_val_if_fail (G_IS_FILE (file), NULL);
703 iface = G_FILE_GET_IFACE (file);
705 return (* iface->get_parent) (file);
709 * g_file_has_parent:
710 * @file: input #GFile
711 * @parent: (allow-none): the parent to check for, or %NULL
713 * Checks if @file has a parent, and optionally, if it is @parent.
715 * If @parent is %NULL then this function returns %TRUE if @file has any
716 * parent at all. If @parent is non-%NULL then %TRUE is only returned
717 * if @file is an immediate child of @parent.
719 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
720 * the case that @parent is %NULL).
722 * Since: 2.24
724 gboolean
725 g_file_has_parent (GFile *file,
726 GFile *parent)
728 GFile *actual_parent;
729 gboolean result;
731 g_return_val_if_fail (G_IS_FILE (file), FALSE);
732 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
734 actual_parent = g_file_get_parent (file);
736 if (actual_parent != NULL)
738 if (parent != NULL)
739 result = g_file_equal (parent, actual_parent);
740 else
741 result = TRUE;
743 g_object_unref (actual_parent);
745 else
746 result = FALSE;
748 return result;
752 * g_file_get_child:
753 * @file: input #GFile
754 * @name: string containing the child's basename
756 * Gets a child of @file with basename equal to @name.
758 * Note that the file with that specific name might not exist, but
759 * you can still have a #GFile that points to it. You can use this
760 * for instance to create that file.
762 * This call does no blocking I/O.
764 * Returns: (transfer full): a #GFile to a child specified by @name.
765 * Free the returned object with g_object_unref().
767 GFile *
768 g_file_get_child (GFile *file,
769 const char *name)
771 g_return_val_if_fail (G_IS_FILE (file), NULL);
772 g_return_val_if_fail (name != NULL, NULL);
774 return g_file_resolve_relative_path (file, name);
778 * g_file_get_child_for_display_name:
779 * @file: input #GFile
780 * @display_name: string to a possible child
781 * @error: return location for an error
783 * Gets the child of @file for a given @display_name (i.e. a UTF-8
784 * version of the name). If this function fails, it returns %NULL
785 * and @error will be set. This is very useful when constructing a
786 * #GFile for a new file and the user entered the filename in the
787 * user interface, for instance when you select a directory and
788 * type a filename in the file selector.
790 * This call does no blocking I/O.
792 * Returns: (transfer full): a #GFile to the specified child, or
793 * %NULL if the display name couldn't be converted.
794 * Free the returned object with g_object_unref().
796 GFile *
797 g_file_get_child_for_display_name (GFile *file,
798 const char *display_name,
799 GError **error)
801 GFileIface *iface;
803 g_return_val_if_fail (G_IS_FILE (file), NULL);
804 g_return_val_if_fail (display_name != NULL, NULL);
806 iface = G_FILE_GET_IFACE (file);
808 return (* iface->get_child_for_display_name) (file, display_name, error);
812 * g_file_has_prefix:
813 * @file: input #GFile
814 * @prefix: input #GFile
816 * Checks whether @file has the prefix specified by @prefix.
818 * In other words, if the names of initial elements of @file's
819 * pathname match @prefix. Only full pathname elements are matched,
820 * so a path like /foo is not considered a prefix of /foobar, only
821 * of /foo/bar.
823 * A #GFile is not a prefix of itself. If you want to check for
824 * equality, use g_file_equal().
826 * This call does no I/O, as it works purely on names. As such it can
827 * sometimes return %FALSE even if @file is inside a @prefix (from a
828 * filesystem point of view), because the prefix of @file is an alias
829 * of @prefix.
831 * Virtual: prefix_matches
832 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
833 * %FALSE otherwise.
835 gboolean
836 g_file_has_prefix (GFile *file,
837 GFile *prefix)
839 GFileIface *iface;
841 g_return_val_if_fail (G_IS_FILE (file), FALSE);
842 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
844 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
845 return FALSE;
847 iface = G_FILE_GET_IFACE (file);
849 /* The vtable function differs in arg order since
850 * we're using the old contains_file call
852 return (* iface->prefix_matches) (prefix, file);
856 * g_file_get_relative_path:
857 * @parent: input #GFile
858 * @descendant: input #GFile
860 * Gets the path for @descendant relative to @parent.
862 * This call does no blocking I/O.
864 * Returns: (type filename) (nullable): string with the relative path from
865 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
866 * prefix. The returned string should be freed with g_free() when
867 * no longer needed.
869 char *
870 g_file_get_relative_path (GFile *parent,
871 GFile *descendant)
873 GFileIface *iface;
875 g_return_val_if_fail (G_IS_FILE (parent), NULL);
876 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
878 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
879 return NULL;
881 iface = G_FILE_GET_IFACE (parent);
883 return (* iface->get_relative_path) (parent, descendant);
887 * g_file_resolve_relative_path:
888 * @file: input #GFile
889 * @relative_path: a given relative path string
891 * Resolves a relative path for @file to an absolute path.
893 * This call does no blocking I/O.
895 * Returns: (transfer full): #GFile to the resolved path.
896 * %NULL if @relative_path is %NULL or if @file is invalid.
897 * Free the returned object with g_object_unref().
899 GFile *
900 g_file_resolve_relative_path (GFile *file,
901 const char *relative_path)
903 GFileIface *iface;
905 g_return_val_if_fail (G_IS_FILE (file), NULL);
906 g_return_val_if_fail (relative_path != NULL, NULL);
908 iface = G_FILE_GET_IFACE (file);
910 return (* iface->resolve_relative_path) (file, relative_path);
914 * g_file_enumerate_children:
915 * @file: input #GFile
916 * @attributes: an attribute query string
917 * @flags: a set of #GFileQueryInfoFlags
918 * @cancellable: (allow-none): optional #GCancellable object,
919 * %NULL to ignore
920 * @error: #GError for error reporting
922 * Gets the requested information about the files in a directory.
923 * The result is a #GFileEnumerator object that will give out
924 * #GFileInfo objects for all the files in the directory.
926 * The @attributes value is a string that specifies the file
927 * attributes that should be gathered. It is not an error if
928 * it's not possible to read a particular requested attribute
929 * from a file - it just won't be set. @attributes should
930 * be a comma-separated list of attributes or attribute wildcards.
931 * The wildcard "*" means all attributes, and a wildcard like
932 * "standard::*" means all attributes in the standard namespace.
933 * An example attribute query be "standard::*,owner::user".
934 * The standard attributes are available as defines, like
935 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
937 * If @cancellable is not %NULL, then the operation can be cancelled
938 * by triggering the cancellable object from another thread. If the
939 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
940 * returned.
942 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
943 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
944 * error will be returned. Other errors are possible too.
946 * Returns: (transfer full): A #GFileEnumerator if successful,
947 * %NULL on error. Free the returned object with g_object_unref().
949 GFileEnumerator *
950 g_file_enumerate_children (GFile *file,
951 const char *attributes,
952 GFileQueryInfoFlags flags,
953 GCancellable *cancellable,
954 GError **error)
956 GFileIface *iface;
958 g_return_val_if_fail (G_IS_FILE (file), NULL);
960 if (g_cancellable_set_error_if_cancelled (cancellable, error))
961 return NULL;
963 iface = G_FILE_GET_IFACE (file);
965 if (iface->enumerate_children == NULL)
967 g_set_error_literal (error, G_IO_ERROR,
968 G_IO_ERROR_NOT_SUPPORTED,
969 _("Operation not supported"));
970 return NULL;
973 return (* iface->enumerate_children) (file, attributes, flags,
974 cancellable, error);
978 * g_file_enumerate_children_async:
979 * @file: input #GFile
980 * @attributes: an attribute query string
981 * @flags: a set of #GFileQueryInfoFlags
982 * @io_priority: the [I/O priority][io-priority] of the request
983 * @cancellable: (allow-none): optional #GCancellable object,
984 * %NULL to ignore
985 * @callback: (scope async): a #GAsyncReadyCallback to call when the
986 * request is satisfied
987 * @user_data: (closure): the data to pass to callback function
989 * Asynchronously gets the requested information about the files
990 * in a directory. The result is a #GFileEnumerator object that will
991 * give out #GFileInfo objects for all the files in the directory.
993 * For more details, see g_file_enumerate_children() which is
994 * the synchronous version of this call.
996 * When the operation is finished, @callback will be called. You can
997 * then call g_file_enumerate_children_finish() to get the result of
998 * the operation.
1000 void
1001 g_file_enumerate_children_async (GFile *file,
1002 const char *attributes,
1003 GFileQueryInfoFlags flags,
1004 int io_priority,
1005 GCancellable *cancellable,
1006 GAsyncReadyCallback callback,
1007 gpointer user_data)
1009 GFileIface *iface;
1011 g_return_if_fail (G_IS_FILE (file));
1013 iface = G_FILE_GET_IFACE (file);
1014 (* iface->enumerate_children_async) (file,
1015 attributes,
1016 flags,
1017 io_priority,
1018 cancellable,
1019 callback,
1020 user_data);
1024 * g_file_enumerate_children_finish:
1025 * @file: input #GFile
1026 * @res: a #GAsyncResult
1027 * @error: a #GError
1029 * Finishes an async enumerate children operation.
1030 * See g_file_enumerate_children_async().
1032 * Returns: (transfer full): a #GFileEnumerator or %NULL
1033 * if an error occurred.
1034 * Free the returned object with g_object_unref().
1036 GFileEnumerator *
1037 g_file_enumerate_children_finish (GFile *file,
1038 GAsyncResult *res,
1039 GError **error)
1041 GFileIface *iface;
1043 g_return_val_if_fail (G_IS_FILE (file), NULL);
1044 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1046 if (g_async_result_legacy_propagate_error (res, error))
1047 return NULL;
1049 iface = G_FILE_GET_IFACE (file);
1050 return (* iface->enumerate_children_finish) (file, res, error);
1054 * g_file_query_exists:
1055 * @file: input #GFile
1056 * @cancellable: (allow-none): optional #GCancellable object,
1057 * %NULL to ignore
1059 * Utility function to check if a particular file exists. This is
1060 * implemented using g_file_query_info() and as such does blocking I/O.
1062 * Note that in many cases it is racy to first check for file existence
1063 * and then execute something based on the outcome of that, because the
1064 * file might have been created or removed in between the operations. The
1065 * general approach to handling that is to not check, but just do the
1066 * operation and handle the errors as they come.
1068 * As an example of race-free checking, take the case of reading a file,
1069 * and if it doesn't exist, creating it. There are two racy versions: read
1070 * it, and on error create it; and: check if it exists, if not create it.
1071 * These can both result in two processes creating the file (with perhaps
1072 * a partially written file as the result). The correct approach is to
1073 * always try to create the file with g_file_create() which will either
1074 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1076 * However, in many cases an existence check is useful in a user interface,
1077 * for instance to make a menu item sensitive/insensitive, so that you don't
1078 * have to fool users that something is possible and then just show an error
1079 * dialog. If you do this, you should make sure to also handle the errors
1080 * that can happen due to races when you execute the operation.
1082 * Returns: %TRUE if the file exists (and can be detected without error),
1083 * %FALSE otherwise (or if cancelled).
1085 gboolean
1086 g_file_query_exists (GFile *file,
1087 GCancellable *cancellable)
1089 GFileInfo *info;
1091 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1093 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1094 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1095 if (info != NULL)
1097 g_object_unref (info);
1098 return TRUE;
1101 return FALSE;
1105 * g_file_query_file_type:
1106 * @file: input #GFile
1107 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1108 * @cancellable: (allow-none): optional #GCancellable object,
1109 * %NULL to ignore
1111 * Utility function to inspect the #GFileType of a file. This is
1112 * implemented using g_file_query_info() and as such does blocking I/O.
1114 * The primary use case of this method is to check if a file is
1115 * a regular file, directory, or symlink.
1117 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1118 * if the file does not exist
1120 * Since: 2.18
1122 GFileType
1123 g_file_query_file_type (GFile *file,
1124 GFileQueryInfoFlags flags,
1125 GCancellable *cancellable)
1127 GFileInfo *info;
1128 GFileType file_type;
1130 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1131 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1132 cancellable, NULL);
1133 if (info != NULL)
1135 file_type = g_file_info_get_file_type (info);
1136 g_object_unref (info);
1138 else
1139 file_type = G_FILE_TYPE_UNKNOWN;
1141 return file_type;
1145 * g_file_query_info:
1146 * @file: input #GFile
1147 * @attributes: an attribute query string
1148 * @flags: a set of #GFileQueryInfoFlags
1149 * @cancellable: (allow-none): optional #GCancellable object,
1150 * %NULL to ignore
1151 * @error: a #GError
1153 * Gets the requested information about specified @file.
1154 * The result is a #GFileInfo object that contains key-value
1155 * attributes (such as the type or size of the file).
1157 * The @attributes value is a string that specifies the file
1158 * attributes that should be gathered. It is not an error if
1159 * it's not possible to read a particular requested attribute
1160 * from a file - it just won't be set. @attributes should be a
1161 * comma-separated list of attributes or attribute wildcards.
1162 * The wildcard "*" means all attributes, and a wildcard like
1163 * "standard::*" means all attributes in the standard namespace.
1164 * An example attribute query be "standard::*,owner::user".
1165 * The standard attributes are available as defines, like
1166 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1168 * If @cancellable is not %NULL, then the operation can be cancelled
1169 * by triggering the cancellable object from another thread. If the
1170 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1171 * returned.
1173 * For symlinks, normally the information about the target of the
1174 * symlink is returned, rather than information about the symlink
1175 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1176 * in @flags the information about the symlink itself will be returned.
1177 * Also, for symlinks that point to non-existing files the information
1178 * about the symlink itself will be returned.
1180 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1181 * returned. Other errors are possible too, and depend on what kind of
1182 * filesystem the file is on.
1184 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1185 * on error. Free the returned object with g_object_unref().
1187 GFileInfo *
1188 g_file_query_info (GFile *file,
1189 const char *attributes,
1190 GFileQueryInfoFlags flags,
1191 GCancellable *cancellable,
1192 GError **error)
1194 GFileIface *iface;
1196 g_return_val_if_fail (G_IS_FILE (file), NULL);
1198 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1199 return NULL;
1201 iface = G_FILE_GET_IFACE (file);
1203 if (iface->query_info == NULL)
1205 g_set_error_literal (error, G_IO_ERROR,
1206 G_IO_ERROR_NOT_SUPPORTED,
1207 _("Operation not supported"));
1208 return NULL;
1211 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1215 * g_file_query_info_async:
1216 * @file: input #GFile
1217 * @attributes: an attribute query string
1218 * @flags: a set of #GFileQueryInfoFlags
1219 * @io_priority: the [I/O priority][io-priority] of the request
1220 * @cancellable: (allow-none): optional #GCancellable object,
1221 * %NULL to ignore
1222 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1223 * request is satisfied
1224 * @user_data: (closure): the data to pass to callback function
1226 * Asynchronously gets the requested information about specified @file.
1227 * The result is a #GFileInfo object that contains key-value attributes
1228 * (such as type or size for the file).
1230 * For more details, see g_file_query_info() which is the synchronous
1231 * version of this call.
1233 * When the operation is finished, @callback will be called. You can
1234 * then call g_file_query_info_finish() to get the result of the operation.
1236 void
1237 g_file_query_info_async (GFile *file,
1238 const char *attributes,
1239 GFileQueryInfoFlags flags,
1240 int io_priority,
1241 GCancellable *cancellable,
1242 GAsyncReadyCallback callback,
1243 gpointer user_data)
1245 GFileIface *iface;
1247 g_return_if_fail (G_IS_FILE (file));
1249 iface = G_FILE_GET_IFACE (file);
1250 (* iface->query_info_async) (file,
1251 attributes,
1252 flags,
1253 io_priority,
1254 cancellable,
1255 callback,
1256 user_data);
1260 * g_file_query_info_finish:
1261 * @file: input #GFile
1262 * @res: a #GAsyncResult
1263 * @error: a #GError
1265 * Finishes an asynchronous file info query.
1266 * See g_file_query_info_async().
1268 * Returns: (transfer full): #GFileInfo for given @file
1269 * or %NULL on error. Free the returned object with
1270 * g_object_unref().
1272 GFileInfo *
1273 g_file_query_info_finish (GFile *file,
1274 GAsyncResult *res,
1275 GError **error)
1277 GFileIface *iface;
1279 g_return_val_if_fail (G_IS_FILE (file), NULL);
1280 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1282 if (g_async_result_legacy_propagate_error (res, error))
1283 return NULL;
1285 iface = G_FILE_GET_IFACE (file);
1286 return (* iface->query_info_finish) (file, res, error);
1290 * g_file_query_filesystem_info:
1291 * @file: input #GFile
1292 * @attributes: an attribute query string
1293 * @cancellable: (allow-none): optional #GCancellable object,
1294 * %NULL to ignore
1295 * @error: a #GError
1297 * Similar to g_file_query_info(), but obtains information
1298 * about the filesystem the @file is on, rather than the file itself.
1299 * For instance the amount of space available and the type of
1300 * the filesystem.
1302 * The @attributes value is a string that specifies the attributes
1303 * that should be gathered. It is not an error if it's not possible
1304 * to read a particular requested attribute from a file - it just
1305 * won't be set. @attributes should be a comma-separated list of
1306 * attributes or attribute wildcards. The wildcard "*" means all
1307 * attributes, and a wildcard like "filesystem::*" means all attributes
1308 * in the filesystem namespace. The standard namespace for filesystem
1309 * attributes is "filesystem". Common attributes of interest are
1310 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1311 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1312 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1314 * If @cancellable is not %NULL, then the operation can be cancelled
1315 * by triggering the cancellable object from another thread. If the
1316 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1317 * returned.
1319 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1320 * be returned. Other errors are possible too, and depend on what
1321 * kind of filesystem the file is on.
1323 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1324 * Free the returned object with g_object_unref().
1326 GFileInfo *
1327 g_file_query_filesystem_info (GFile *file,
1328 const char *attributes,
1329 GCancellable *cancellable,
1330 GError **error)
1332 GFileIface *iface;
1334 g_return_val_if_fail (G_IS_FILE (file), NULL);
1336 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1337 return NULL;
1339 iface = G_FILE_GET_IFACE (file);
1341 if (iface->query_filesystem_info == NULL)
1343 g_set_error_literal (error, G_IO_ERROR,
1344 G_IO_ERROR_NOT_SUPPORTED,
1345 _("Operation not supported"));
1346 return NULL;
1349 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1353 * g_file_query_filesystem_info_async:
1354 * @file: input #GFile
1355 * @attributes: an attribute query string
1356 * @io_priority: the [I/O priority][io-priority] of the request
1357 * @cancellable: (allow-none): optional #GCancellable object,
1358 * %NULL to ignore
1359 * @callback: (scope async): a #GAsyncReadyCallback to call
1360 * when the request is satisfied
1361 * @user_data: (closure): the data to pass to callback function
1363 * Asynchronously gets the requested information about the filesystem
1364 * that the specified @file is on. The result is a #GFileInfo object
1365 * that contains key-value attributes (such as type or size for the
1366 * file).
1368 * For more details, see g_file_query_filesystem_info() which is the
1369 * synchronous version of this call.
1371 * When the operation is finished, @callback will be called. You can
1372 * then call g_file_query_info_finish() to get the result of the
1373 * operation.
1375 void
1376 g_file_query_filesystem_info_async (GFile *file,
1377 const char *attributes,
1378 int io_priority,
1379 GCancellable *cancellable,
1380 GAsyncReadyCallback callback,
1381 gpointer user_data)
1383 GFileIface *iface;
1385 g_return_if_fail (G_IS_FILE (file));
1387 iface = G_FILE_GET_IFACE (file);
1388 (* iface->query_filesystem_info_async) (file,
1389 attributes,
1390 io_priority,
1391 cancellable,
1392 callback,
1393 user_data);
1397 * g_file_query_filesystem_info_finish:
1398 * @file: input #GFile
1399 * @res: a #GAsyncResult
1400 * @error: a #GError
1402 * Finishes an asynchronous filesystem info query.
1403 * See g_file_query_filesystem_info_async().
1405 * Returns: (transfer full): #GFileInfo for given @file
1406 * or %NULL on error.
1407 * Free the returned object with g_object_unref().
1409 GFileInfo *
1410 g_file_query_filesystem_info_finish (GFile *file,
1411 GAsyncResult *res,
1412 GError **error)
1414 GFileIface *iface;
1416 g_return_val_if_fail (G_IS_FILE (file), NULL);
1417 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1419 if (g_async_result_legacy_propagate_error (res, error))
1420 return NULL;
1422 iface = G_FILE_GET_IFACE (file);
1423 return (* iface->query_filesystem_info_finish) (file, res, error);
1427 * g_file_find_enclosing_mount:
1428 * @file: input #GFile
1429 * @cancellable: (allow-none): optional #GCancellable object,
1430 * %NULL to ignore
1431 * @error: a #GError
1433 * Gets a #GMount for the #GFile.
1435 * If the #GFileIface for @file does not have a mount (e.g.
1436 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1437 * and %NULL will be returned.
1439 * If @cancellable is not %NULL, then the operation can be cancelled by
1440 * triggering the cancellable object from another thread. If the operation
1441 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1443 * Returns: (transfer full): a #GMount where the @file is located
1444 * or %NULL on error.
1445 * Free the returned object with g_object_unref().
1447 GMount *
1448 g_file_find_enclosing_mount (GFile *file,
1449 GCancellable *cancellable,
1450 GError **error)
1452 GFileIface *iface;
1454 g_return_val_if_fail (G_IS_FILE (file), NULL);
1456 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1457 return NULL;
1459 iface = G_FILE_GET_IFACE (file);
1460 if (iface->find_enclosing_mount == NULL)
1463 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1464 /* Translators: This is an error message when
1465 * trying to find the enclosing (user visible)
1466 * mount of a file, but none exists.
1468 _("Containing mount does not exist"));
1469 return NULL;
1472 return (* iface->find_enclosing_mount) (file, cancellable, error);
1476 * g_file_find_enclosing_mount_async:
1477 * @file: a #GFile
1478 * @io_priority: the [I/O priority][io-priority] of the request
1479 * @cancellable: (allow-none): optional #GCancellable object,
1480 * %NULL to ignore
1481 * @callback: (scope async): a #GAsyncReadyCallback to call
1482 * when the request is satisfied
1483 * @user_data: (closure): the data to pass to callback function
1485 * Asynchronously gets the mount for the file.
1487 * For more details, see g_file_find_enclosing_mount() which is
1488 * the synchronous version of this call.
1490 * When the operation is finished, @callback will be called.
1491 * You can then call g_file_find_enclosing_mount_finish() to
1492 * get the result of the operation.
1494 void
1495 g_file_find_enclosing_mount_async (GFile *file,
1496 int io_priority,
1497 GCancellable *cancellable,
1498 GAsyncReadyCallback callback,
1499 gpointer user_data)
1501 GFileIface *iface;
1503 g_return_if_fail (G_IS_FILE (file));
1505 iface = G_FILE_GET_IFACE (file);
1506 (* iface->find_enclosing_mount_async) (file,
1507 io_priority,
1508 cancellable,
1509 callback,
1510 user_data);
1514 * g_file_find_enclosing_mount_finish:
1515 * @file: a #GFile
1516 * @res: a #GAsyncResult
1517 * @error: a #GError
1519 * Finishes an asynchronous find mount request.
1520 * See g_file_find_enclosing_mount_async().
1522 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1523 * Free the returned object with g_object_unref().
1525 GMount *
1526 g_file_find_enclosing_mount_finish (GFile *file,
1527 GAsyncResult *res,
1528 GError **error)
1530 GFileIface *iface;
1532 g_return_val_if_fail (G_IS_FILE (file), NULL);
1533 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1535 if (g_async_result_legacy_propagate_error (res, error))
1536 return NULL;
1538 iface = G_FILE_GET_IFACE (file);
1539 return (* iface->find_enclosing_mount_finish) (file, res, error);
1544 * g_file_read:
1545 * @file: #GFile to read
1546 * @cancellable: (allow-none): a #GCancellable
1547 * @error: a #GError, or %NULL
1549 * Opens a file for reading. The result is a #GFileInputStream that
1550 * can be used to read the contents of the file.
1552 * If @cancellable is not %NULL, then the operation can be cancelled by
1553 * triggering the cancellable object from another thread. If the operation
1554 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1556 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1557 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1558 * error will be returned. Other errors are possible too, and depend
1559 * on what kind of filesystem the file is on.
1561 * Virtual: read_fn
1562 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1563 * Free the returned object with g_object_unref().
1565 GFileInputStream *
1566 g_file_read (GFile *file,
1567 GCancellable *cancellable,
1568 GError **error)
1570 GFileIface *iface;
1572 g_return_val_if_fail (G_IS_FILE (file), NULL);
1574 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1575 return NULL;
1577 iface = G_FILE_GET_IFACE (file);
1579 if (iface->read_fn == NULL)
1581 g_set_error_literal (error, G_IO_ERROR,
1582 G_IO_ERROR_NOT_SUPPORTED,
1583 _("Operation not supported"));
1584 return NULL;
1587 return (* iface->read_fn) (file, cancellable, error);
1591 * g_file_append_to:
1592 * @file: input #GFile
1593 * @flags: a set of #GFileCreateFlags
1594 * @cancellable: (allow-none): optional #GCancellable object,
1595 * %NULL to ignore
1596 * @error: a #GError, or %NULL
1598 * Gets an output stream for appending data to the file.
1599 * If the file doesn't already exist it is created.
1601 * By default files created are generally readable by everyone,
1602 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1603 * will be made readable only to the current user, to the level that
1604 * is supported on the target filesystem.
1606 * If @cancellable is not %NULL, then the operation can be cancelled
1607 * by triggering the cancellable object from another thread. If the
1608 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1609 * returned.
1611 * Some file systems don't allow all file names, and may return an
1612 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1613 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1614 * possible too, and depend on what kind of filesystem the file is on.
1616 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1617 * Free the returned object with g_object_unref().
1619 GFileOutputStream *
1620 g_file_append_to (GFile *file,
1621 GFileCreateFlags flags,
1622 GCancellable *cancellable,
1623 GError **error)
1625 GFileIface *iface;
1627 g_return_val_if_fail (G_IS_FILE (file), NULL);
1629 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1630 return NULL;
1632 iface = G_FILE_GET_IFACE (file);
1634 if (iface->append_to == NULL)
1636 g_set_error_literal (error, G_IO_ERROR,
1637 G_IO_ERROR_NOT_SUPPORTED,
1638 _("Operation not supported"));
1639 return NULL;
1642 return (* iface->append_to) (file, flags, cancellable, error);
1646 * g_file_create:
1647 * @file: input #GFile
1648 * @flags: a set of #GFileCreateFlags
1649 * @cancellable: (allow-none): optional #GCancellable object,
1650 * %NULL to ignore
1651 * @error: a #GError, or %NULL
1653 * Creates a new file and returns an output stream for writing to it.
1654 * The file must not already exist.
1656 * By default files created are generally readable by everyone,
1657 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1658 * will be made readable only to the current user, to the level
1659 * that is supported on the target filesystem.
1661 * If @cancellable is not %NULL, then the operation can be cancelled
1662 * by triggering the cancellable object from another thread. If the
1663 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1664 * returned.
1666 * If a file or directory with this name already exists the
1667 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1668 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1669 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1670 * be returned. Other errors are possible too, and depend on what kind
1671 * of filesystem the file is on.
1673 * Returns: (transfer full): a #GFileOutputStream for the newly created
1674 * file, or %NULL on error.
1675 * Free the returned object with g_object_unref().
1677 GFileOutputStream *
1678 g_file_create (GFile *file,
1679 GFileCreateFlags flags,
1680 GCancellable *cancellable,
1681 GError **error)
1683 GFileIface *iface;
1685 g_return_val_if_fail (G_IS_FILE (file), NULL);
1687 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1688 return NULL;
1690 iface = G_FILE_GET_IFACE (file);
1692 if (iface->create == NULL)
1694 g_set_error_literal (error, G_IO_ERROR,
1695 G_IO_ERROR_NOT_SUPPORTED,
1696 _("Operation not supported"));
1697 return NULL;
1700 return (* iface->create) (file, flags, cancellable, error);
1704 * g_file_replace:
1705 * @file: input #GFile
1706 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1707 * for the current #GFile, or #NULL to ignore
1708 * @make_backup: %TRUE if a backup should be created
1709 * @flags: a set of #GFileCreateFlags
1710 * @cancellable: (allow-none): optional #GCancellable object,
1711 * %NULL to ignore
1712 * @error: a #GError, or %NULL
1714 * Returns an output stream for overwriting the file, possibly
1715 * creating a backup copy of the file first. If the file doesn't exist,
1716 * it will be created.
1718 * This will try to replace the file in the safest way possible so
1719 * that any errors during the writing will not affect an already
1720 * existing copy of the file. For instance, for local files it
1721 * may write to a temporary file and then atomically rename over
1722 * the destination when the stream is closed.
1724 * By default files created are generally readable by everyone,
1725 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1726 * will be made readable only to the current user, to the level that
1727 * is supported on the target filesystem.
1729 * If @cancellable is not %NULL, then the operation can be cancelled
1730 * by triggering the cancellable object from another thread. If the
1731 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1732 * returned.
1734 * If you pass in a non-%NULL @etag value and @file already exists, then
1735 * this value is compared to the current entity tag of the file, and if
1736 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1737 * generally means that the file has been changed since you last read
1738 * it. You can get the new etag from g_file_output_stream_get_etag()
1739 * after you've finished writing and closed the #GFileOutputStream. When
1740 * you load a new file you can use g_file_input_stream_query_info() to
1741 * get the etag of the file.
1743 * If @make_backup is %TRUE, this function will attempt to make a
1744 * backup of the current file before overwriting it. If this fails
1745 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1746 * want to replace anyway, try again with @make_backup set to %FALSE.
1748 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1749 * be returned, and if the file is some other form of non-regular file
1750 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1751 * file systems don't allow all file names, and may return an
1752 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1753 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1754 * possible too, and depend on what kind of filesystem the file is on.
1756 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1757 * Free the returned object with g_object_unref().
1759 GFileOutputStream *
1760 g_file_replace (GFile *file,
1761 const char *etag,
1762 gboolean make_backup,
1763 GFileCreateFlags flags,
1764 GCancellable *cancellable,
1765 GError **error)
1767 GFileIface *iface;
1769 g_return_val_if_fail (G_IS_FILE (file), NULL);
1771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1772 return NULL;
1774 iface = G_FILE_GET_IFACE (file);
1776 if (iface->replace == NULL)
1778 g_set_error_literal (error, G_IO_ERROR,
1779 G_IO_ERROR_NOT_SUPPORTED,
1780 _("Operation not supported"));
1781 return NULL;
1784 /* Handle empty tag string as NULL in consistent way. */
1785 if (etag && *etag == 0)
1786 etag = NULL;
1788 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1792 * g_file_open_readwrite:
1793 * @file: #GFile to open
1794 * @cancellable: (allow-none): a #GCancellable
1795 * @error: a #GError, or %NULL
1797 * Opens an existing file for reading and writing. The result is
1798 * a #GFileIOStream that can be used to read and write the contents
1799 * of the file.
1801 * If @cancellable is not %NULL, then the operation can be cancelled
1802 * by triggering the cancellable object from another thread. If the
1803 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1804 * returned.
1806 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1807 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1808 * error will be returned. Other errors are possible too, and depend on
1809 * what kind of filesystem the file is on. Note that in many non-local
1810 * file cases read and write streams are not supported, so make sure you
1811 * really need to do read and write streaming, rather than just opening
1812 * for reading or writing.
1814 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1815 * Free the returned object with g_object_unref().
1817 * Since: 2.22
1819 GFileIOStream *
1820 g_file_open_readwrite (GFile *file,
1821 GCancellable *cancellable,
1822 GError **error)
1824 GFileIface *iface;
1826 g_return_val_if_fail (G_IS_FILE (file), NULL);
1828 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1829 return NULL;
1831 iface = G_FILE_GET_IFACE (file);
1833 if (iface->open_readwrite == NULL)
1835 g_set_error_literal (error, G_IO_ERROR,
1836 G_IO_ERROR_NOT_SUPPORTED,
1837 _("Operation not supported"));
1838 return NULL;
1841 return (* iface->open_readwrite) (file, cancellable, error);
1845 * g_file_create_readwrite:
1846 * @file: a #GFile
1847 * @flags: a set of #GFileCreateFlags
1848 * @cancellable: (allow-none): optional #GCancellable object,
1849 * %NULL to ignore
1850 * @error: return location for a #GError, or %NULL
1852 * Creates a new file and returns a stream for reading and
1853 * writing to it. The file must not already exist.
1855 * By default files created are generally readable by everyone,
1856 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1857 * will be made readable only to the current user, to the level
1858 * that is supported on the target filesystem.
1860 * If @cancellable is not %NULL, then the operation can be cancelled
1861 * by triggering the cancellable object from another thread. If the
1862 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1863 * returned.
1865 * If a file or directory with this name already exists, the
1866 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1867 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1868 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1869 * will be returned. Other errors are possible too, and depend on what
1870 * kind of filesystem the file is on.
1872 * Note that in many non-local file cases read and write streams are
1873 * not supported, so make sure you really need to do read and write
1874 * streaming, rather than just opening for reading or writing.
1876 * Returns: (transfer full): a #GFileIOStream for the newly created
1877 * file, or %NULL on error.
1878 * Free the returned object with g_object_unref().
1880 * Since: 2.22
1882 GFileIOStream *
1883 g_file_create_readwrite (GFile *file,
1884 GFileCreateFlags flags,
1885 GCancellable *cancellable,
1886 GError **error)
1888 GFileIface *iface;
1890 g_return_val_if_fail (G_IS_FILE (file), NULL);
1892 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1893 return NULL;
1895 iface = G_FILE_GET_IFACE (file);
1897 if (iface->create_readwrite == NULL)
1899 g_set_error_literal (error, G_IO_ERROR,
1900 G_IO_ERROR_NOT_SUPPORTED,
1901 _("Operation not supported"));
1902 return NULL;
1905 return (* iface->create_readwrite) (file, flags, cancellable, error);
1909 * g_file_replace_readwrite:
1910 * @file: a #GFile
1911 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1912 * for the current #GFile, or #NULL to ignore
1913 * @make_backup: %TRUE if a backup should be created
1914 * @flags: a set of #GFileCreateFlags
1915 * @cancellable: (allow-none): optional #GCancellable object,
1916 * %NULL to ignore
1917 * @error: return location for a #GError, or %NULL
1919 * Returns an output stream for overwriting the file in readwrite mode,
1920 * possibly creating a backup copy of the file first. If the file doesn't
1921 * exist, it will be created.
1923 * For details about the behaviour, see g_file_replace() which does the
1924 * same thing but returns an output stream only.
1926 * Note that in many non-local file cases read and write streams are not
1927 * supported, so make sure you really need to do read and write streaming,
1928 * rather than just opening for reading or writing.
1930 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1931 * Free the returned object with g_object_unref().
1933 * Since: 2.22
1935 GFileIOStream *
1936 g_file_replace_readwrite (GFile *file,
1937 const char *etag,
1938 gboolean make_backup,
1939 GFileCreateFlags flags,
1940 GCancellable *cancellable,
1941 GError **error)
1943 GFileIface *iface;
1945 g_return_val_if_fail (G_IS_FILE (file), NULL);
1947 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1948 return NULL;
1950 iface = G_FILE_GET_IFACE (file);
1952 if (iface->replace_readwrite == NULL)
1954 g_set_error_literal (error, G_IO_ERROR,
1955 G_IO_ERROR_NOT_SUPPORTED,
1956 _("Operation not supported"));
1957 return NULL;
1960 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1964 * g_file_read_async:
1965 * @file: input #GFile
1966 * @io_priority: the [I/O priority][io-priority] of the request
1967 * @cancellable: (allow-none): optional #GCancellable object,
1968 * %NULL to ignore
1969 * @callback: (scope async): a #GAsyncReadyCallback to call
1970 * when the request is satisfied
1971 * @user_data: (closure): the data to pass to callback function
1973 * Asynchronously opens @file for reading.
1975 * For more details, see g_file_read() which is
1976 * the synchronous version of this call.
1978 * When the operation is finished, @callback will be called.
1979 * You can then call g_file_read_finish() to get the result
1980 * of the operation.
1982 void
1983 g_file_read_async (GFile *file,
1984 int io_priority,
1985 GCancellable *cancellable,
1986 GAsyncReadyCallback callback,
1987 gpointer user_data)
1989 GFileIface *iface;
1991 g_return_if_fail (G_IS_FILE (file));
1993 iface = G_FILE_GET_IFACE (file);
1994 (* iface->read_async) (file,
1995 io_priority,
1996 cancellable,
1997 callback,
1998 user_data);
2002 * g_file_read_finish:
2003 * @file: input #GFile
2004 * @res: a #GAsyncResult
2005 * @error: a #GError, or %NULL
2007 * Finishes an asynchronous file read operation started with
2008 * g_file_read_async().
2010 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2011 * Free the returned object with g_object_unref().
2013 GFileInputStream *
2014 g_file_read_finish (GFile *file,
2015 GAsyncResult *res,
2016 GError **error)
2018 GFileIface *iface;
2020 g_return_val_if_fail (G_IS_FILE (file), NULL);
2021 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2023 if (g_async_result_legacy_propagate_error (res, error))
2024 return NULL;
2026 iface = G_FILE_GET_IFACE (file);
2027 return (* iface->read_finish) (file, res, error);
2031 * g_file_append_to_async:
2032 * @file: input #GFile
2033 * @flags: a set of #GFileCreateFlags
2034 * @io_priority: the [I/O priority][io-priority] of the request
2035 * @cancellable: (allow-none): optional #GCancellable object,
2036 * %NULL to ignore
2037 * @callback: (scope async): a #GAsyncReadyCallback to call
2038 * when the request is satisfied
2039 * @user_data: (closure): the data to pass to callback function
2041 * Asynchronously opens @file for appending.
2043 * For more details, see g_file_append_to() which is
2044 * the synchronous version of this call.
2046 * When the operation is finished, @callback will be called.
2047 * You can then call g_file_append_to_finish() to get the result
2048 * of the operation.
2050 void
2051 g_file_append_to_async (GFile *file,
2052 GFileCreateFlags flags,
2053 int io_priority,
2054 GCancellable *cancellable,
2055 GAsyncReadyCallback callback,
2056 gpointer user_data)
2058 GFileIface *iface;
2060 g_return_if_fail (G_IS_FILE (file));
2062 iface = G_FILE_GET_IFACE (file);
2063 (* iface->append_to_async) (file,
2064 flags,
2065 io_priority,
2066 cancellable,
2067 callback,
2068 user_data);
2072 * g_file_append_to_finish:
2073 * @file: input #GFile
2074 * @res: #GAsyncResult
2075 * @error: a #GError, or %NULL
2077 * Finishes an asynchronous file append operation started with
2078 * g_file_append_to_async().
2080 * Returns: (transfer full): a valid #GFileOutputStream
2081 * or %NULL on error.
2082 * Free the returned object with g_object_unref().
2084 GFileOutputStream *
2085 g_file_append_to_finish (GFile *file,
2086 GAsyncResult *res,
2087 GError **error)
2089 GFileIface *iface;
2091 g_return_val_if_fail (G_IS_FILE (file), NULL);
2092 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2094 if (g_async_result_legacy_propagate_error (res, error))
2095 return NULL;
2097 iface = G_FILE_GET_IFACE (file);
2098 return (* iface->append_to_finish) (file, res, error);
2102 * g_file_create_async:
2103 * @file: input #GFile
2104 * @flags: a set of #GFileCreateFlags
2105 * @io_priority: the [I/O priority][io-priority] of the request
2106 * @cancellable: (allow-none): optional #GCancellable object,
2107 * %NULL to ignore
2108 * @callback: (scope async): a #GAsyncReadyCallback to call
2109 * when the request is satisfied
2110 * @user_data: (closure): the data to pass to callback function
2112 * Asynchronously creates a new file and returns an output stream
2113 * for writing to it. The file must not already exist.
2115 * For more details, see g_file_create() which is
2116 * the synchronous version of this call.
2118 * When the operation is finished, @callback will be called.
2119 * You can then call g_file_create_finish() to get the result
2120 * of the operation.
2122 void
2123 g_file_create_async (GFile *file,
2124 GFileCreateFlags flags,
2125 int io_priority,
2126 GCancellable *cancellable,
2127 GAsyncReadyCallback callback,
2128 gpointer user_data)
2130 GFileIface *iface;
2132 g_return_if_fail (G_IS_FILE (file));
2134 iface = G_FILE_GET_IFACE (file);
2135 (* iface->create_async) (file,
2136 flags,
2137 io_priority,
2138 cancellable,
2139 callback,
2140 user_data);
2144 * g_file_create_finish:
2145 * @file: input #GFile
2146 * @res: a #GAsyncResult
2147 * @error: a #GError, or %NULL
2149 * Finishes an asynchronous file create operation started with
2150 * g_file_create_async().
2152 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2153 * Free the returned object with g_object_unref().
2155 GFileOutputStream *
2156 g_file_create_finish (GFile *file,
2157 GAsyncResult *res,
2158 GError **error)
2160 GFileIface *iface;
2162 g_return_val_if_fail (G_IS_FILE (file), NULL);
2163 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2165 if (g_async_result_legacy_propagate_error (res, error))
2166 return NULL;
2168 iface = G_FILE_GET_IFACE (file);
2169 return (* iface->create_finish) (file, res, error);
2173 * g_file_replace_async:
2174 * @file: input #GFile
2175 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2176 * or %NULL to ignore
2177 * @make_backup: %TRUE if a backup should be created
2178 * @flags: a set of #GFileCreateFlags
2179 * @io_priority: the [I/O priority][io-priority] of the request
2180 * @cancellable: (allow-none): optional #GCancellable object,
2181 * %NULL to ignore
2182 * @callback: (scope async): a #GAsyncReadyCallback to call
2183 * when the request is satisfied
2184 * @user_data: (closure): the data to pass to callback function
2186 * Asynchronously overwrites the file, replacing the contents,
2187 * possibly creating a backup copy of the file first.
2189 * For more details, see g_file_replace() which is
2190 * the synchronous version of this call.
2192 * When the operation is finished, @callback will be called.
2193 * You can then call g_file_replace_finish() to get the result
2194 * of the operation.
2196 void
2197 g_file_replace_async (GFile *file,
2198 const char *etag,
2199 gboolean make_backup,
2200 GFileCreateFlags flags,
2201 int io_priority,
2202 GCancellable *cancellable,
2203 GAsyncReadyCallback callback,
2204 gpointer user_data)
2206 GFileIface *iface;
2208 g_return_if_fail (G_IS_FILE (file));
2210 iface = G_FILE_GET_IFACE (file);
2211 (* iface->replace_async) (file,
2212 etag,
2213 make_backup,
2214 flags,
2215 io_priority,
2216 cancellable,
2217 callback,
2218 user_data);
2222 * g_file_replace_finish:
2223 * @file: input #GFile
2224 * @res: a #GAsyncResult
2225 * @error: a #GError, or %NULL
2227 * Finishes an asynchronous file replace operation started with
2228 * g_file_replace_async().
2230 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2231 * Free the returned object with g_object_unref().
2233 GFileOutputStream *
2234 g_file_replace_finish (GFile *file,
2235 GAsyncResult *res,
2236 GError **error)
2238 GFileIface *iface;
2240 g_return_val_if_fail (G_IS_FILE (file), NULL);
2241 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2243 if (g_async_result_legacy_propagate_error (res, error))
2244 return NULL;
2246 iface = G_FILE_GET_IFACE (file);
2247 return (* iface->replace_finish) (file, res, error);
2251 * g_file_open_readwrite_async
2252 * @file: input #GFile
2253 * @io_priority: the [I/O priority][io-priority] of the request
2254 * @cancellable: (allow-none): optional #GCancellable object,
2255 * %NULL to ignore
2256 * @callback: (scope async): a #GAsyncReadyCallback to call
2257 * when the request is satisfied
2258 * @user_data: (closure): the data to pass to callback function
2260 * Asynchronously opens @file for reading and writing.
2262 * For more details, see g_file_open_readwrite() which is
2263 * the synchronous version of this call.
2265 * When the operation is finished, @callback will be called.
2266 * You can then call g_file_open_readwrite_finish() to get
2267 * the result of the operation.
2269 * Since: 2.22
2271 void
2272 g_file_open_readwrite_async (GFile *file,
2273 int io_priority,
2274 GCancellable *cancellable,
2275 GAsyncReadyCallback callback,
2276 gpointer user_data)
2278 GFileIface *iface;
2280 g_return_if_fail (G_IS_FILE (file));
2282 iface = G_FILE_GET_IFACE (file);
2283 (* iface->open_readwrite_async) (file,
2284 io_priority,
2285 cancellable,
2286 callback,
2287 user_data);
2291 * g_file_open_readwrite_finish:
2292 * @file: input #GFile
2293 * @res: a #GAsyncResult
2294 * @error: a #GError, or %NULL
2296 * Finishes an asynchronous file read operation started with
2297 * g_file_open_readwrite_async().
2299 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2300 * Free the returned object with g_object_unref().
2302 * Since: 2.22
2304 GFileIOStream *
2305 g_file_open_readwrite_finish (GFile *file,
2306 GAsyncResult *res,
2307 GError **error)
2309 GFileIface *iface;
2311 g_return_val_if_fail (G_IS_FILE (file), NULL);
2312 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2314 if (g_async_result_legacy_propagate_error (res, error))
2315 return NULL;
2317 iface = G_FILE_GET_IFACE (file);
2318 return (* iface->open_readwrite_finish) (file, res, error);
2322 * g_file_create_readwrite_async:
2323 * @file: input #GFile
2324 * @flags: a set of #GFileCreateFlags
2325 * @io_priority: the [I/O priority][io-priority] of the request
2326 * @cancellable: (allow-none): optional #GCancellable object,
2327 * %NULL to ignore
2328 * @callback: (scope async): a #GAsyncReadyCallback to call
2329 * when the request is satisfied
2330 * @user_data: (closure): the data to pass to callback function
2332 * Asynchronously creates a new file and returns a stream
2333 * for reading and writing to it. The file must not already exist.
2335 * For more details, see g_file_create_readwrite() which is
2336 * the synchronous version of this call.
2338 * When the operation is finished, @callback will be called.
2339 * You can then call g_file_create_readwrite_finish() to get
2340 * the result of the operation.
2342 * Since: 2.22
2344 void
2345 g_file_create_readwrite_async (GFile *file,
2346 GFileCreateFlags flags,
2347 int io_priority,
2348 GCancellable *cancellable,
2349 GAsyncReadyCallback callback,
2350 gpointer user_data)
2352 GFileIface *iface;
2354 g_return_if_fail (G_IS_FILE (file));
2356 iface = G_FILE_GET_IFACE (file);
2357 (* iface->create_readwrite_async) (file,
2358 flags,
2359 io_priority,
2360 cancellable,
2361 callback,
2362 user_data);
2366 * g_file_create_readwrite_finish:
2367 * @file: input #GFile
2368 * @res: a #GAsyncResult
2369 * @error: a #GError, or %NULL
2371 * Finishes an asynchronous file create operation started with
2372 * g_file_create_readwrite_async().
2374 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2375 * Free the returned object with g_object_unref().
2377 * Since: 2.22
2379 GFileIOStream *
2380 g_file_create_readwrite_finish (GFile *file,
2381 GAsyncResult *res,
2382 GError **error)
2384 GFileIface *iface;
2386 g_return_val_if_fail (G_IS_FILE (file), NULL);
2387 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2389 if (g_async_result_legacy_propagate_error (res, error))
2390 return NULL;
2392 iface = G_FILE_GET_IFACE (file);
2393 return (* iface->create_readwrite_finish) (file, res, error);
2397 * g_file_replace_readwrite_async:
2398 * @file: input #GFile
2399 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2400 * or %NULL to ignore
2401 * @make_backup: %TRUE if a backup should be created
2402 * @flags: a set of #GFileCreateFlags
2403 * @io_priority: the [I/O priority][io-priority] of the request
2404 * @cancellable: (allow-none): optional #GCancellable object,
2405 * %NULL to ignore
2406 * @callback: (scope async): a #GAsyncReadyCallback to call
2407 * when the request is satisfied
2408 * @user_data: (closure): the data to pass to callback function
2410 * Asynchronously overwrites the file in read-write mode,
2411 * replacing the contents, possibly creating a backup copy
2412 * of the file first.
2414 * For more details, see g_file_replace_readwrite() which is
2415 * the synchronous version of this call.
2417 * When the operation is finished, @callback will be called.
2418 * You can then call g_file_replace_readwrite_finish() to get
2419 * the result of the operation.
2421 * Since: 2.22
2423 void
2424 g_file_replace_readwrite_async (GFile *file,
2425 const char *etag,
2426 gboolean make_backup,
2427 GFileCreateFlags flags,
2428 int io_priority,
2429 GCancellable *cancellable,
2430 GAsyncReadyCallback callback,
2431 gpointer user_data)
2433 GFileIface *iface;
2435 g_return_if_fail (G_IS_FILE (file));
2437 iface = G_FILE_GET_IFACE (file);
2438 (* iface->replace_readwrite_async) (file,
2439 etag,
2440 make_backup,
2441 flags,
2442 io_priority,
2443 cancellable,
2444 callback,
2445 user_data);
2449 * g_file_replace_readwrite_finish:
2450 * @file: input #GFile
2451 * @res: a #GAsyncResult
2452 * @error: a #GError, or %NULL
2454 * Finishes an asynchronous file replace operation started with
2455 * g_file_replace_readwrite_async().
2457 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2458 * Free the returned object with g_object_unref().
2460 * Since: 2.22
2462 GFileIOStream *
2463 g_file_replace_readwrite_finish (GFile *file,
2464 GAsyncResult *res,
2465 GError **error)
2467 GFileIface *iface;
2469 g_return_val_if_fail (G_IS_FILE (file), NULL);
2470 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2472 if (g_async_result_legacy_propagate_error (res, error))
2473 return NULL;
2475 iface = G_FILE_GET_IFACE (file);
2476 return (* iface->replace_readwrite_finish) (file, res, error);
2479 static gboolean
2480 copy_symlink (GFile *destination,
2481 GFileCopyFlags flags,
2482 GCancellable *cancellable,
2483 const char *target,
2484 GError **error)
2486 GError *my_error;
2487 gboolean tried_delete;
2488 GFileInfo *info;
2489 GFileType file_type;
2491 tried_delete = FALSE;
2493 retry:
2494 my_error = NULL;
2495 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2497 /* Maybe it already existed, and we want to overwrite? */
2498 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2499 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2501 g_clear_error (&my_error);
2503 /* Don't overwrite if the destination is a directory */
2504 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2505 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2506 cancellable, &my_error);
2507 if (info != NULL)
2509 file_type = g_file_info_get_file_type (info);
2510 g_object_unref (info);
2512 if (file_type == G_FILE_TYPE_DIRECTORY)
2514 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2515 _("Can't copy over directory"));
2516 return FALSE;
2520 if (!g_file_delete (destination, cancellable, error))
2521 return FALSE;
2523 tried_delete = TRUE;
2524 goto retry;
2526 /* Nah, fail */
2527 g_propagate_error (error, my_error);
2528 return FALSE;
2531 return TRUE;
2534 static GFileInputStream *
2535 open_source_for_copy (GFile *source,
2536 GFile *destination,
2537 GFileCopyFlags flags,
2538 GCancellable *cancellable,
2539 GError **error)
2541 GError *my_error;
2542 GFileInputStream *ret;
2543 GFileInfo *info;
2544 GFileType file_type;
2546 my_error = NULL;
2547 ret = g_file_read (source, cancellable, &my_error);
2548 if (ret != NULL)
2549 return ret;
2551 /* There was an error opening the source, try to set a good error for it: */
2552 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2554 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2555 * as that is less useful to the app. Better check for errors on the
2556 * target instead.
2558 g_error_free (my_error);
2559 my_error = NULL;
2561 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2562 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2563 cancellable, &my_error);
2564 if (info != NULL &&
2565 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2567 file_type = g_file_info_get_file_type (info);
2568 g_object_unref (info);
2570 if (flags & G_FILE_COPY_OVERWRITE)
2572 if (file_type == G_FILE_TYPE_DIRECTORY)
2574 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2575 _("Can't copy directory over directory"));
2576 return NULL;
2578 /* continue to would_recurse error */
2580 else
2582 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2583 _("Target file exists"));
2584 return NULL;
2587 else
2589 /* Error getting info from target, return that error
2590 * (except for NOT_FOUND, which is no error here)
2592 g_clear_object (&info);
2593 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2595 g_propagate_error (error, my_error);
2596 return NULL;
2598 g_clear_error (&my_error);
2601 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2602 _("Can't recursively copy directory"));
2603 return NULL;
2606 g_propagate_error (error, my_error);
2607 return NULL;
2610 static gboolean
2611 should_copy (GFileAttributeInfo *info,
2612 gboolean copy_all_attributes,
2613 gboolean skip_perms)
2615 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2616 return FALSE;
2618 if (copy_all_attributes)
2619 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2620 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2623 static gboolean
2624 build_attribute_list_for_copy (GFile *file,
2625 GFileCopyFlags flags,
2626 char **out_attributes,
2627 GCancellable *cancellable,
2628 GError **error)
2630 gboolean ret = FALSE;
2631 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2632 GString *s = NULL;
2633 gboolean first;
2634 int i;
2635 gboolean copy_all_attributes;
2636 gboolean skip_perms;
2638 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2639 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2641 /* Ignore errors here, if the target supports no attributes there is
2642 * nothing to copy. We still honor the cancellable though.
2644 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2645 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2646 goto out;
2648 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2650 goto out;
2652 if (attributes == NULL && namespaces == NULL)
2653 goto out;
2655 first = TRUE;
2656 s = g_string_new ("");
2658 if (attributes)
2660 for (i = 0; i < attributes->n_infos; i++)
2662 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2664 if (first)
2665 first = FALSE;
2666 else
2667 g_string_append_c (s, ',');
2669 g_string_append (s, attributes->infos[i].name);
2674 if (namespaces)
2676 for (i = 0; i < namespaces->n_infos; i++)
2678 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2680 if (first)
2681 first = FALSE;
2682 else
2683 g_string_append_c (s, ',');
2685 g_string_append (s, namespaces->infos[i].name);
2686 g_string_append (s, "::*");
2691 ret = TRUE;
2692 *out_attributes = g_string_free (s, FALSE);
2693 s = NULL;
2694 out:
2695 if (s)
2696 g_string_free (s, TRUE);
2697 if (attributes)
2698 g_file_attribute_info_list_unref (attributes);
2699 if (namespaces)
2700 g_file_attribute_info_list_unref (namespaces);
2702 return ret;
2706 * g_file_copy_attributes:
2707 * @source: a #GFile with attributes
2708 * @destination: a #GFile to copy attributes to
2709 * @flags: a set of #GFileCopyFlags
2710 * @cancellable: (allow-none): optional #GCancellable object,
2711 * %NULL to ignore
2712 * @error: a #GError, %NULL to ignore
2714 * Copies the file attributes from @source to @destination.
2716 * Normally only a subset of the file attributes are copied,
2717 * those that are copies in a normal file copy operation
2718 * (which for instance does not include e.g. owner). However
2719 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2720 * all the metadata that is possible to copy is copied. This
2721 * is useful when implementing move by copy + delete source.
2723 * Returns: %TRUE if the attributes were copied successfully,
2724 * %FALSE otherwise.
2726 gboolean
2727 g_file_copy_attributes (GFile *source,
2728 GFile *destination,
2729 GFileCopyFlags flags,
2730 GCancellable *cancellable,
2731 GError **error)
2733 char *attrs_to_read;
2734 gboolean res;
2735 GFileInfo *info;
2736 gboolean source_nofollow_symlinks;
2738 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2739 cancellable, error))
2740 return FALSE;
2742 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2744 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2745 * we just don't copy it.
2747 info = g_file_query_info (source, attrs_to_read,
2748 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2749 cancellable,
2750 NULL);
2752 g_free (attrs_to_read);
2754 res = TRUE;
2755 if (info)
2757 res = g_file_set_attributes_from_info (destination,
2758 info,
2759 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2760 cancellable,
2761 error);
2762 g_object_unref (info);
2765 return res;
2768 static gboolean
2769 copy_stream_with_progress (GInputStream *in,
2770 GOutputStream *out,
2771 GFile *source,
2772 GCancellable *cancellable,
2773 GFileProgressCallback progress_callback,
2774 gpointer progress_callback_data,
2775 GError **error)
2777 gssize n_read, n_written;
2778 goffset current_size;
2779 char buffer[1024*64], *p;
2780 gboolean res;
2781 goffset total_size;
2782 GFileInfo *info;
2784 total_size = -1;
2785 /* avoid performance impact of querying total size when it's not needed */
2786 if (progress_callback)
2788 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2789 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2790 cancellable, NULL);
2791 if (info)
2793 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2794 total_size = g_file_info_get_size (info);
2795 g_object_unref (info);
2798 if (total_size == -1)
2800 info = g_file_query_info (source,
2801 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2802 G_FILE_QUERY_INFO_NONE,
2803 cancellable, NULL);
2804 if (info)
2806 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2807 total_size = g_file_info_get_size (info);
2808 g_object_unref (info);
2813 if (total_size == -1)
2814 total_size = 0;
2816 current_size = 0;
2817 res = TRUE;
2818 while (TRUE)
2820 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2821 if (n_read == -1)
2823 res = FALSE;
2824 break;
2827 if (n_read == 0)
2828 break;
2830 current_size += n_read;
2832 p = buffer;
2833 while (n_read > 0)
2835 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2836 if (n_written == -1)
2838 res = FALSE;
2839 break;
2842 p += n_written;
2843 n_read -= n_written;
2846 if (!res)
2847 break;
2849 if (progress_callback)
2850 progress_callback (current_size, total_size, progress_callback_data);
2853 /* Make sure we send full copied size */
2854 if (progress_callback)
2855 progress_callback (current_size, total_size, progress_callback_data);
2857 return res;
2860 #ifdef HAVE_SPLICE
2862 static gboolean
2863 do_splice (int fd_in,
2864 loff_t *off_in,
2865 int fd_out,
2866 loff_t *off_out,
2867 size_t len,
2868 long *bytes_transferd,
2869 GError **error)
2871 long result;
2873 retry:
2874 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2876 if (result == -1)
2878 int errsv = errno;
2880 if (errsv == EINTR)
2881 goto retry;
2882 else if (errsv == ENOSYS || errsv == EINVAL)
2883 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2884 _("Splice not supported"));
2885 else
2886 g_set_error (error, G_IO_ERROR,
2887 g_io_error_from_errno (errsv),
2888 _("Error splicing file: %s"),
2889 g_strerror (errsv));
2891 return FALSE;
2894 *bytes_transferd = result;
2895 return TRUE;
2898 static gboolean
2899 splice_stream_with_progress (GInputStream *in,
2900 GOutputStream *out,
2901 GCancellable *cancellable,
2902 GFileProgressCallback progress_callback,
2903 gpointer progress_callback_data,
2904 GError **error)
2906 int buffer[2] = { -1, -1 };
2907 gboolean res;
2908 goffset total_size;
2909 loff_t offset_in;
2910 loff_t offset_out;
2911 int fd_in, fd_out;
2913 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2914 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2916 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2917 return FALSE;
2919 total_size = -1;
2920 /* avoid performance impact of querying total size when it's not needed */
2921 if (progress_callback)
2923 struct stat sbuf;
2925 if (fstat (fd_in, &sbuf) == 0)
2926 total_size = sbuf.st_size;
2929 if (total_size == -1)
2930 total_size = 0;
2932 offset_in = offset_out = 0;
2933 res = FALSE;
2934 while (TRUE)
2936 long n_read;
2937 long n_written;
2939 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2940 break;
2942 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2943 break;
2945 if (n_read == 0)
2947 res = TRUE;
2948 break;
2951 while (n_read > 0)
2953 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2954 goto out;
2956 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2957 goto out;
2959 n_read -= n_written;
2962 if (progress_callback)
2963 progress_callback (offset_in, total_size, progress_callback_data);
2966 /* Make sure we send full copied size */
2967 if (progress_callback)
2968 progress_callback (offset_in, total_size, progress_callback_data);
2970 if (!g_close (buffer[0], error))
2971 goto out;
2972 buffer[0] = -1;
2973 if (!g_close (buffer[1], error))
2974 goto out;
2975 buffer[1] = -1;
2976 out:
2977 if (buffer[0] != -1)
2978 (void) g_close (buffer[0], NULL);
2979 if (buffer[1] != -1)
2980 (void) g_close (buffer[1], NULL);
2982 return res;
2984 #endif
2986 #ifdef __linux__
2987 static gboolean
2988 btrfs_reflink_with_progress (GInputStream *in,
2989 GOutputStream *out,
2990 GFileInfo *info,
2991 GCancellable *cancellable,
2992 GFileProgressCallback progress_callback,
2993 gpointer progress_callback_data,
2994 GError **error)
2996 goffset source_size;
2997 int fd_in, fd_out;
2998 int ret;
3000 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3001 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3003 if (progress_callback)
3004 source_size = g_file_info_get_size (info);
3006 /* Btrfs clone ioctl properties:
3007 * - Works at the inode level
3008 * - Doesn't work with directories
3009 * - Always follows symlinks (source and destination)
3011 * By the time we get here, *in and *out are both regular files */
3012 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3014 if (ret < 0)
3016 if (errno == EXDEV)
3017 g_set_error_literal (error, G_IO_ERROR,
3018 G_IO_ERROR_NOT_SUPPORTED,
3019 _("Copy (reflink/clone) between mounts is not supported"));
3020 else if (errno == EINVAL)
3021 g_set_error_literal (error, G_IO_ERROR,
3022 G_IO_ERROR_NOT_SUPPORTED,
3023 _("Copy (reflink/clone) is not supported or invalid"));
3024 else
3025 /* Most probably something odd happened; retry with fallback */
3026 g_set_error_literal (error, G_IO_ERROR,
3027 G_IO_ERROR_NOT_SUPPORTED,
3028 _("Copy (reflink/clone) is not supported or didn't work"));
3029 /* We retry with fallback for all error cases because Btrfs is currently
3030 * unstable, and so we can't trust it to do clone properly.
3031 * In addition, any hard errors here would cause the same failure in the
3032 * fallback manual copy as well. */
3033 return FALSE;
3036 /* Make sure we send full copied size */
3037 if (progress_callback)
3038 progress_callback (source_size, source_size, progress_callback_data);
3040 return TRUE;
3042 #endif
3044 static gboolean
3045 file_copy_fallback (GFile *source,
3046 GFile *destination,
3047 GFileCopyFlags flags,
3048 GCancellable *cancellable,
3049 GFileProgressCallback progress_callback,
3050 gpointer progress_callback_data,
3051 GError **error)
3053 gboolean ret = FALSE;
3054 GFileInputStream *file_in = NULL;
3055 GInputStream *in = NULL;
3056 GOutputStream *out = NULL;
3057 GFileInfo *info = NULL;
3058 const char *target;
3059 char *attrs_to_read;
3060 gboolean do_set_attributes = FALSE;
3062 /* need to know the file type */
3063 info = g_file_query_info (source,
3064 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3065 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3066 cancellable,
3067 error);
3068 if (!info)
3069 goto out;
3071 /* Maybe copy the symlink? */
3072 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3073 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3075 target = g_file_info_get_symlink_target (info);
3076 if (target)
3078 if (!copy_symlink (destination, flags, cancellable, target, error))
3079 goto out;
3081 ret = TRUE;
3082 goto out;
3084 /* ... else fall back on a regular file copy */
3086 /* Handle "special" files (pipes, device nodes, ...)? */
3087 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3089 /* FIXME: could try to recreate device nodes and others? */
3090 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3091 _("Can't copy special file"));
3092 goto out;
3095 /* Everything else should just fall back on a regular copy. */
3097 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3098 if (!file_in)
3099 goto out;
3100 in = G_INPUT_STREAM (file_in);
3102 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3103 cancellable, error))
3104 goto out;
3106 if (attrs_to_read != NULL)
3108 GError *tmp_error = NULL;
3110 /* Ok, ditch the previous lightweight info (on Unix we just
3111 * called lstat()); at this point we gather all the information
3112 * we need about the source from the opened file descriptor.
3114 g_object_unref (info);
3116 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3117 cancellable, &tmp_error);
3118 if (!info)
3120 /* Not all gvfs backends implement query_info_on_read(), we
3121 * can just fall back to the pathname again.
3122 * https://bugzilla.gnome.org/706254
3124 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3126 g_clear_error (&tmp_error);
3127 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3128 cancellable, error);
3130 else
3132 g_free (attrs_to_read);
3133 g_propagate_error (error, tmp_error);
3134 goto out;
3137 g_free (attrs_to_read);
3138 if (!info)
3139 goto out;
3141 do_set_attributes = TRUE;
3144 /* In the local file path, we pass down the source info which
3145 * includes things like unix::mode, to ensure that the target file
3146 * is not created with different permissions from the source file.
3148 * If a future API like g_file_replace_with_info() is added, switch
3149 * this code to use that.
3151 if (G_IS_LOCAL_FILE (destination))
3153 if (flags & G_FILE_COPY_OVERWRITE)
3154 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3155 FALSE, NULL,
3156 flags & G_FILE_COPY_BACKUP,
3157 G_FILE_CREATE_REPLACE_DESTINATION,
3158 info,
3159 cancellable, error);
3160 else
3161 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3162 FALSE, 0, info,
3163 cancellable, error);
3165 else if (flags & G_FILE_COPY_OVERWRITE)
3167 out = (GOutputStream *)g_file_replace (destination,
3168 NULL,
3169 flags & G_FILE_COPY_BACKUP,
3170 G_FILE_CREATE_REPLACE_DESTINATION,
3171 cancellable, error);
3173 else
3175 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3178 if (!out)
3179 goto out;
3181 #ifdef __linux__
3182 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3184 GError *reflink_err = NULL;
3186 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3187 progress_callback, progress_callback_data,
3188 &reflink_err))
3190 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3192 g_clear_error (&reflink_err);
3194 else
3196 g_propagate_error (error, reflink_err);
3197 goto out;
3200 else
3202 ret = TRUE;
3203 goto out;
3206 #endif
3208 #ifdef HAVE_SPLICE
3209 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3211 GError *splice_err = NULL;
3213 if (!splice_stream_with_progress (in, out, cancellable,
3214 progress_callback, progress_callback_data,
3215 &splice_err))
3217 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3219 g_clear_error (&splice_err);
3221 else
3223 g_propagate_error (error, splice_err);
3224 goto out;
3227 else
3229 ret = TRUE;
3230 goto out;
3234 #endif
3236 /* A plain read/write loop */
3237 if (!copy_stream_with_progress (in, out, source, cancellable,
3238 progress_callback, progress_callback_data,
3239 error))
3240 goto out;
3242 ret = TRUE;
3243 out:
3244 if (in)
3246 /* Don't care about errors in source here */
3247 (void) g_input_stream_close (in, cancellable, NULL);
3248 g_object_unref (in);
3251 if (out)
3253 /* But write errors on close are bad! */
3254 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3255 ret = FALSE;
3256 g_object_unref (out);
3259 /* Ignore errors here. Failure to copy metadata is not a hard error */
3260 /* TODO: set these attributes /before/ we do the rename() on Unix */
3261 if (ret && do_set_attributes)
3263 g_file_set_attributes_from_info (destination,
3264 info,
3265 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3266 cancellable,
3267 NULL);
3270 g_clear_object (&info);
3272 return ret;
3276 * g_file_copy:
3277 * @source: input #GFile
3278 * @destination: destination #GFile
3279 * @flags: set of #GFileCopyFlags
3280 * @cancellable: (allow-none): optional #GCancellable object,
3281 * %NULL to ignore
3282 * @progress_callback: (allow-none) (scope call): function to callback with
3283 * progress information, or %NULL if progress information is not needed
3284 * @progress_callback_data: (closure): user data to pass to @progress_callback
3285 * @error: #GError to set on error, or %NULL
3287 * Copies the file @source to the location specified by @destination.
3288 * Can not handle recursive copies of directories.
3290 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3291 * existing @destination file is overwritten.
3293 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3294 * will be copied as symlinks, otherwise the target of the
3295 * @source symlink will be copied.
3297 * If @cancellable is not %NULL, then the operation can be cancelled by
3298 * triggering the cancellable object from another thread. If the operation
3299 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3301 * If @progress_callback is not %NULL, then the operation can be monitored
3302 * by setting this to a #GFileProgressCallback function.
3303 * @progress_callback_data will be passed to this function. It is guaranteed
3304 * that this callback will be called after all data has been transferred with
3305 * the total number of bytes copied during the operation.
3307 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3308 * is returned, independent on the status of the @destination.
3310 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3311 * the error %G_IO_ERROR_EXISTS is returned.
3313 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3314 * error is returned. If trying to overwrite a directory with a directory the
3315 * %G_IO_ERROR_WOULD_MERGE error is returned.
3317 * If the source is a directory and the target does not exist, or
3318 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3319 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3321 * If you are interested in copying the #GFile object itself (not the on-disk
3322 * file), see g_file_dup().
3324 * Returns: %TRUE on success, %FALSE otherwise.
3326 gboolean
3327 g_file_copy (GFile *source,
3328 GFile *destination,
3329 GFileCopyFlags flags,
3330 GCancellable *cancellable,
3331 GFileProgressCallback progress_callback,
3332 gpointer progress_callback_data,
3333 GError **error)
3335 GFileIface *iface;
3336 GError *my_error;
3337 gboolean res;
3339 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3340 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3342 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3343 return FALSE;
3345 iface = G_FILE_GET_IFACE (destination);
3346 if (iface->copy)
3348 my_error = NULL;
3349 res = (* iface->copy) (source, destination,
3350 flags, cancellable,
3351 progress_callback, progress_callback_data,
3352 &my_error);
3354 if (res)
3355 return TRUE;
3357 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3359 g_propagate_error (error, my_error);
3360 return FALSE;
3362 else
3363 g_clear_error (&my_error);
3366 /* If the types are different, and the destination method failed
3367 * also try the source method
3369 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3371 iface = G_FILE_GET_IFACE (source);
3373 if (iface->copy)
3375 my_error = NULL;
3376 res = (* iface->copy) (source, destination,
3377 flags, cancellable,
3378 progress_callback, progress_callback_data,
3379 &my_error);
3381 if (res)
3382 return TRUE;
3384 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3386 g_propagate_error (error, my_error);
3387 return FALSE;
3389 else
3390 g_clear_error (&my_error);
3394 return file_copy_fallback (source, destination, flags, cancellable,
3395 progress_callback, progress_callback_data,
3396 error);
3400 * g_file_copy_async: (skip)
3401 * @source: input #GFile
3402 * @destination: destination #GFile
3403 * @flags: set of #GFileCopyFlags
3404 * @io_priority: the [I/O priority][io-priority] of the request
3405 * @cancellable: (allow-none): optional #GCancellable object,
3406 * %NULL to ignore
3407 * @progress_callback: (allow-none): function to callback with progress
3408 * information, or %NULL if progress information is not needed
3409 * @progress_callback_data: (closure): user data to pass to @progress_callback
3410 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3411 * @user_data: the data to pass to callback function
3413 * Copies the file @source to the location specified by @destination
3414 * asynchronously. For details of the behaviour, see g_file_copy().
3416 * If @progress_callback is not %NULL, then that function that will be called
3417 * just like in g_file_copy(). The callback will run in the default main context
3418 * of the thread calling g_file_copy_async() — the same context as @callback is
3419 * run in.
3421 * When the operation is finished, @callback will be called. You can then call
3422 * g_file_copy_finish() to get the result of the operation.
3424 void
3425 g_file_copy_async (GFile *source,
3426 GFile *destination,
3427 GFileCopyFlags flags,
3428 int io_priority,
3429 GCancellable *cancellable,
3430 GFileProgressCallback progress_callback,
3431 gpointer progress_callback_data,
3432 GAsyncReadyCallback callback,
3433 gpointer user_data)
3435 GFileIface *iface;
3437 g_return_if_fail (G_IS_FILE (source));
3438 g_return_if_fail (G_IS_FILE (destination));
3440 iface = G_FILE_GET_IFACE (source);
3441 (* iface->copy_async) (source,
3442 destination,
3443 flags,
3444 io_priority,
3445 cancellable,
3446 progress_callback,
3447 progress_callback_data,
3448 callback,
3449 user_data);
3453 * g_file_copy_finish:
3454 * @file: input #GFile
3455 * @res: a #GAsyncResult
3456 * @error: a #GError, or %NULL
3458 * Finishes copying the file started with g_file_copy_async().
3460 * Returns: a %TRUE on success, %FALSE on error.
3462 gboolean
3463 g_file_copy_finish (GFile *file,
3464 GAsyncResult *res,
3465 GError **error)
3467 GFileIface *iface;
3469 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3470 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3472 if (g_async_result_legacy_propagate_error (res, error))
3473 return FALSE;
3475 iface = G_FILE_GET_IFACE (file);
3476 return (* iface->copy_finish) (file, res, error);
3480 * g_file_move:
3481 * @source: #GFile pointing to the source location
3482 * @destination: #GFile pointing to the destination location
3483 * @flags: set of #GFileCopyFlags
3484 * @cancellable: (allow-none): optional #GCancellable object,
3485 * %NULL to ignore
3486 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3487 * function for updates
3488 * @progress_callback_data: (closure): gpointer to user data for
3489 * the callback function
3490 * @error: #GError for returning error conditions, or %NULL
3492 * Tries to move the file or directory @source to the location specified
3493 * by @destination. If native move operations are supported then this is
3494 * used, otherwise a copy + delete fallback is used. The native
3495 * implementation may support moving directories (for instance on moves
3496 * inside the same filesystem), but the fallback code does not.
3498 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3499 * existing @destination file is overwritten.
3501 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3502 * will be copied as symlinks, otherwise the target of the
3503 * @source symlink will be copied.
3505 * If @cancellable is not %NULL, then the operation can be cancelled by
3506 * triggering the cancellable object from another thread. If the operation
3507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3509 * If @progress_callback is not %NULL, then the operation can be monitored
3510 * by setting this to a #GFileProgressCallback function.
3511 * @progress_callback_data will be passed to this function. It is
3512 * guaranteed that this callback will be called after all data has been
3513 * transferred with the total number of bytes copied during the operation.
3515 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3516 * error is returned, independent on the status of the @destination.
3518 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3519 * then the error %G_IO_ERROR_EXISTS is returned.
3521 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3522 * error is returned. If trying to overwrite a directory with a directory the
3523 * %G_IO_ERROR_WOULD_MERGE error is returned.
3525 * If the source is a directory and the target does not exist, or
3526 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3527 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3528 * move operation isn't available).
3530 * Returns: %TRUE on successful move, %FALSE otherwise.
3532 gboolean
3533 g_file_move (GFile *source,
3534 GFile *destination,
3535 GFileCopyFlags flags,
3536 GCancellable *cancellable,
3537 GFileProgressCallback progress_callback,
3538 gpointer progress_callback_data,
3539 GError **error)
3541 GFileIface *iface;
3542 GError *my_error;
3543 gboolean res;
3545 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3546 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3548 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549 return FALSE;
3551 iface = G_FILE_GET_IFACE (destination);
3552 if (iface->move)
3554 my_error = NULL;
3555 res = (* iface->move) (source, destination,
3556 flags, cancellable,
3557 progress_callback, progress_callback_data,
3558 &my_error);
3560 if (res)
3561 return TRUE;
3563 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3565 g_propagate_error (error, my_error);
3566 return FALSE;
3568 else
3569 g_clear_error (&my_error);
3572 /* If the types are different, and the destination method failed
3573 * also try the source method
3575 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3577 iface = G_FILE_GET_IFACE (source);
3579 if (iface->move)
3581 my_error = NULL;
3582 res = (* iface->move) (source, destination,
3583 flags, cancellable,
3584 progress_callback, progress_callback_data,
3585 &my_error);
3587 if (res)
3588 return TRUE;
3590 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3592 g_propagate_error (error, my_error);
3593 return FALSE;
3595 else
3596 g_clear_error (&my_error);
3600 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3602 g_set_error_literal (error, G_IO_ERROR,
3603 G_IO_ERROR_NOT_SUPPORTED,
3604 _("Operation not supported"));
3605 return FALSE;
3608 flags |= G_FILE_COPY_ALL_METADATA;
3609 if (!g_file_copy (source, destination, flags, cancellable,
3610 progress_callback, progress_callback_data,
3611 error))
3612 return FALSE;
3614 return g_file_delete (source, cancellable, error);
3618 * g_file_make_directory:
3619 * @file: input #GFile
3620 * @cancellable: (allow-none): optional #GCancellable object,
3621 * %NULL to ignore
3622 * @error: a #GError, or %NULL
3624 * Creates a directory. Note that this will only create a child directory
3625 * of the immediate parent directory of the path or URI given by the #GFile.
3626 * To recursively create directories, see g_file_make_directory_with_parents().
3627 * This function will fail if the parent directory does not exist, setting
3628 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3629 * creating directories, this function will fail, setting @error to
3630 * %G_IO_ERROR_NOT_SUPPORTED.
3632 * For a local #GFile the newly created directory will have the default
3633 * (current) ownership and permissions of the current process.
3635 * If @cancellable is not %NULL, then the operation can be cancelled by
3636 * triggering the cancellable object from another thread. If the operation
3637 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3639 * Returns: %TRUE on successful creation, %FALSE otherwise.
3641 gboolean
3642 g_file_make_directory (GFile *file,
3643 GCancellable *cancellable,
3644 GError **error)
3646 GFileIface *iface;
3648 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3650 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3651 return FALSE;
3653 iface = G_FILE_GET_IFACE (file);
3655 if (iface->make_directory == NULL)
3657 g_set_error_literal (error, G_IO_ERROR,
3658 G_IO_ERROR_NOT_SUPPORTED,
3659 _("Operation not supported"));
3660 return FALSE;
3663 return (* iface->make_directory) (file, cancellable, error);
3667 * g_file_make_directory_async:
3668 * @file: input #GFile
3669 * @io_priority: the [I/O priority][io-priority] of the request
3670 * @cancellable: (allow-none): optional #GCancellable object,
3671 * %NULL to ignore
3672 * @callback: a #GAsyncReadyCallback to call
3673 * when the request is satisfied
3674 * @user_data: the data to pass to callback function
3676 * Asynchronously creates a directory.
3678 * Virtual: make_directory_async
3679 * Since: 2.38
3681 void
3682 g_file_make_directory_async (GFile *file,
3683 int io_priority,
3684 GCancellable *cancellable,
3685 GAsyncReadyCallback callback,
3686 gpointer user_data)
3688 GFileIface *iface;
3690 g_return_if_fail (G_IS_FILE (file));
3692 iface = G_FILE_GET_IFACE (file);
3693 (* iface->make_directory_async) (file,
3694 io_priority,
3695 cancellable,
3696 callback,
3697 user_data);
3701 * g_file_make_directory_finish:
3702 * @file: input #GFile
3703 * @result: a #GAsyncResult
3704 * @error: a #GError, or %NULL
3706 * Finishes an asynchronous directory creation, started with
3707 * g_file_make_directory_async().
3709 * Virtual: make_directory_finish
3710 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3711 * Since: 2.38
3713 gboolean
3714 g_file_make_directory_finish (GFile *file,
3715 GAsyncResult *result,
3716 GError **error)
3718 GFileIface *iface;
3720 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3721 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3723 iface = G_FILE_GET_IFACE (file);
3724 return (* iface->make_directory_finish) (file, result, error);
3728 * g_file_make_directory_with_parents:
3729 * @file: input #GFile
3730 * @cancellable: (allow-none): optional #GCancellable object,
3731 * %NULL to ignore
3732 * @error: a #GError, or %NULL
3734 * Creates a directory and any parent directories that may not
3735 * exist similar to 'mkdir -p'. If the file system does not support
3736 * creating directories, this function will fail, setting @error to
3737 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3738 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3739 * the similar g_mkdir_with_parents().
3741 * For a local #GFile the newly created directories will have the default
3742 * (current) ownership and permissions of the current process.
3744 * If @cancellable is not %NULL, then the operation can be cancelled by
3745 * triggering the cancellable object from another thread. If the operation
3746 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3748 * Returns: %TRUE if all directories have been successfully created, %FALSE
3749 * otherwise.
3751 * Since: 2.18
3753 gboolean
3754 g_file_make_directory_with_parents (GFile *file,
3755 GCancellable *cancellable,
3756 GError **error)
3758 GFile *work_file = NULL;
3759 GList *list = NULL, *l;
3760 GError *my_error = NULL;
3762 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3764 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3765 return FALSE;
3767 /* Try for the simple case of not having to create any parent
3768 * directories. If any parent directory needs to be created, this
3769 * call will fail with NOT_FOUND. If that happens, then that value of
3770 * my_error persists into the while loop below.
3772 g_file_make_directory (file, cancellable, &my_error);
3773 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3775 if (my_error)
3776 g_propagate_error (error, my_error);
3777 return my_error == NULL;
3780 work_file = g_object_ref (file);
3782 /* Creates the parent directories as needed. In case any particular
3783 * creation operation fails for lack of other parent directories
3784 * (NOT_FOUND), the directory is added to a list of directories to
3785 * create later, and the value of my_error is retained until the next
3786 * iteration of the loop. After the loop my_error should either be
3787 * empty or contain a real failure condition.
3789 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3791 GFile *parent_file;
3793 parent_file = g_file_get_parent (work_file);
3794 if (parent_file == NULL)
3795 break;
3797 g_clear_error (&my_error);
3798 g_file_make_directory (parent_file, cancellable, &my_error);
3799 /* Another process may have created the directory in between the
3800 * G_IO_ERROR_NOT_FOUND and now
3802 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3803 g_clear_error (&my_error);
3805 g_object_unref (work_file);
3806 work_file = g_object_ref (parent_file);
3808 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3809 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3810 else
3811 g_object_unref (parent_file);
3814 /* All directories should be able to be created now, so an error at
3815 * this point means the whole operation must fail -- except an EXISTS
3816 * error, which means that another process already created the
3817 * directory in between the previous failure and now.
3819 for (l = list; my_error == NULL && l; l = l->next)
3821 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3822 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3823 g_clear_error (&my_error);
3826 if (work_file)
3827 g_object_unref (work_file);
3829 /* Clean up */
3830 while (list != NULL)
3832 g_object_unref ((GFile *) list->data);
3833 list = g_list_remove (list, list->data);
3836 /* At this point an error in my_error means a that something
3837 * unexpected failed in either of the loops above, so the whole
3838 * operation must fail.
3840 if (my_error != NULL)
3842 g_propagate_error (error, my_error);
3843 return FALSE;
3846 return g_file_make_directory (file, cancellable, error);
3850 * g_file_make_symbolic_link:
3851 * @file: a #GFile with the name of the symlink to create
3852 * @symlink_value: (type filename): a string with the path for the target
3853 * of the new symlink
3854 * @cancellable: (allow-none): optional #GCancellable object,
3855 * %NULL to ignore
3856 * @error: a #GError
3858 * Creates a symbolic link named @file which contains the string
3859 * @symlink_value.
3861 * If @cancellable is not %NULL, then the operation can be cancelled by
3862 * triggering the cancellable object from another thread. If the operation
3863 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3865 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3867 gboolean
3868 g_file_make_symbolic_link (GFile *file,
3869 const char *symlink_value,
3870 GCancellable *cancellable,
3871 GError **error)
3873 GFileIface *iface;
3875 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3876 g_return_val_if_fail (symlink_value != NULL, FALSE);
3878 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3879 return FALSE;
3881 if (*symlink_value == '\0')
3883 g_set_error_literal (error, G_IO_ERROR,
3884 G_IO_ERROR_INVALID_ARGUMENT,
3885 _("Invalid symlink value given"));
3886 return FALSE;
3889 iface = G_FILE_GET_IFACE (file);
3891 if (iface->make_symbolic_link == NULL)
3893 g_set_error_literal (error, G_IO_ERROR,
3894 G_IO_ERROR_NOT_SUPPORTED,
3895 _("Operation not supported"));
3896 return FALSE;
3899 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3903 * g_file_delete:
3904 * @file: input #GFile
3905 * @cancellable: (allow-none): optional #GCancellable object,
3906 * %NULL to ignore
3907 * @error: a #GError, or %NULL
3909 * Deletes a file. If the @file is a directory, it will only be
3910 * deleted if it is empty. This has the same semantics as g_unlink().
3912 * If @cancellable is not %NULL, then the operation can be cancelled by
3913 * triggering the cancellable object from another thread. If the operation
3914 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3916 * Virtual: delete_file
3917 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3919 gboolean
3920 g_file_delete (GFile *file,
3921 GCancellable *cancellable,
3922 GError **error)
3924 GFileIface *iface;
3926 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3928 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3929 return FALSE;
3931 iface = G_FILE_GET_IFACE (file);
3933 if (iface->delete_file == NULL)
3935 g_set_error_literal (error, G_IO_ERROR,
3936 G_IO_ERROR_NOT_SUPPORTED,
3937 _("Operation not supported"));
3938 return FALSE;
3941 return (* iface->delete_file) (file, cancellable, error);
3945 * g_file_delete_async:
3946 * @file: input #GFile
3947 * @io_priority: the [I/O priority][io-priority] of the request
3948 * @cancellable: (allow-none): optional #GCancellable object,
3949 * %NULL to ignore
3950 * @callback: a #GAsyncReadyCallback to call
3951 * when the request is satisfied
3952 * @user_data: the data to pass to callback function
3954 * Asynchronously delete a file. If the @file is a directory, it will
3955 * only be deleted if it is empty. This has the same semantics as
3956 * g_unlink().
3958 * Virtual: delete_file_async
3959 * Since: 2.34
3961 void
3962 g_file_delete_async (GFile *file,
3963 int io_priority,
3964 GCancellable *cancellable,
3965 GAsyncReadyCallback callback,
3966 gpointer user_data)
3968 GFileIface *iface;
3970 g_return_if_fail (G_IS_FILE (file));
3972 iface = G_FILE_GET_IFACE (file);
3973 (* iface->delete_file_async) (file,
3974 io_priority,
3975 cancellable,
3976 callback,
3977 user_data);
3981 * g_file_delete_finish:
3982 * @file: input #GFile
3983 * @result: a #GAsyncResult
3984 * @error: a #GError, or %NULL
3986 * Finishes deleting a file started with g_file_delete_async().
3988 * Virtual: delete_file_finish
3989 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3990 * Since: 2.34
3992 gboolean
3993 g_file_delete_finish (GFile *file,
3994 GAsyncResult *result,
3995 GError **error)
3997 GFileIface *iface;
3999 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4000 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4002 if (g_async_result_legacy_propagate_error (result, error))
4003 return FALSE;
4005 iface = G_FILE_GET_IFACE (file);
4006 return (* iface->delete_file_finish) (file, result, error);
4010 * g_file_trash:
4011 * @file: #GFile to send to trash
4012 * @cancellable: (allow-none): optional #GCancellable object,
4013 * %NULL to ignore
4014 * @error: a #GError, or %NULL
4016 * Sends @file to the "Trashcan", if possible. This is similar to
4017 * deleting it, but the user can recover it before emptying the trashcan.
4018 * Not all file systems support trashing, so this call can return the
4019 * %G_IO_ERROR_NOT_SUPPORTED error.
4021 * If @cancellable is not %NULL, then the operation can be cancelled by
4022 * triggering the cancellable object from another thread. If the operation
4023 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4025 * Virtual: trash
4026 * Returns: %TRUE on successful trash, %FALSE otherwise.
4028 gboolean
4029 g_file_trash (GFile *file,
4030 GCancellable *cancellable,
4031 GError **error)
4033 GFileIface *iface;
4035 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4037 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4038 return FALSE;
4040 iface = G_FILE_GET_IFACE (file);
4042 if (iface->trash == NULL)
4044 g_set_error_literal (error,
4045 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4046 _("Trash not supported"));
4047 return FALSE;
4050 return (* iface->trash) (file, cancellable, error);
4054 * g_file_trash_async:
4055 * @file: input #GFile
4056 * @io_priority: the [I/O priority][io-priority] of the request
4057 * @cancellable: (allow-none): optional #GCancellable object,
4058 * %NULL to ignore
4059 * @callback: a #GAsyncReadyCallback to call
4060 * when the request is satisfied
4061 * @user_data: the data to pass to callback function
4063 * Asynchronously sends @file to the Trash location, if possible.
4065 * Virtual: trash_async
4066 * Since: 2.38
4068 void
4069 g_file_trash_async (GFile *file,
4070 int io_priority,
4071 GCancellable *cancellable,
4072 GAsyncReadyCallback callback,
4073 gpointer user_data)
4075 GFileIface *iface;
4077 g_return_if_fail (G_IS_FILE (file));
4079 iface = G_FILE_GET_IFACE (file);
4080 (* iface->trash_async) (file,
4081 io_priority,
4082 cancellable,
4083 callback,
4084 user_data);
4088 * g_file_trash_finish:
4089 * @file: input #GFile
4090 * @result: a #GAsyncResult
4091 * @error: a #GError, or %NULL
4093 * Finishes an asynchronous file trashing operation, started with
4094 * g_file_trash_async().
4096 * Virtual: trash_finish
4097 * Returns: %TRUE on successful trash, %FALSE otherwise.
4098 * Since: 2.38
4100 gboolean
4101 g_file_trash_finish (GFile *file,
4102 GAsyncResult *result,
4103 GError **error)
4105 GFileIface *iface;
4107 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4108 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4110 iface = G_FILE_GET_IFACE (file);
4111 return (* iface->trash_finish) (file, result, error);
4115 * g_file_set_display_name:
4116 * @file: input #GFile
4117 * @display_name: a string
4118 * @cancellable: (allow-none): optional #GCancellable object,
4119 * %NULL to ignore
4120 * @error: a #GError, or %NULL
4122 * Renames @file to the specified display name.
4124 * The display name is converted from UTF-8 to the correct encoding
4125 * for the target filesystem if possible and the @file is renamed to this.
4127 * If you want to implement a rename operation in the user interface the
4128 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4129 * initial value in the rename widget, and then the result after editing
4130 * should be passed to g_file_set_display_name().
4132 * On success the resulting converted filename is returned.
4134 * If @cancellable is not %NULL, then the operation can be cancelled by
4135 * triggering the cancellable object from another thread. If the operation
4136 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4138 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4139 * or %NULL if there was an error.
4140 * Free the returned object with g_object_unref().
4142 GFile *
4143 g_file_set_display_name (GFile *file,
4144 const gchar *display_name,
4145 GCancellable *cancellable,
4146 GError **error)
4148 GFileIface *iface;
4150 g_return_val_if_fail (G_IS_FILE (file), NULL);
4151 g_return_val_if_fail (display_name != NULL, NULL);
4153 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4155 g_set_error (error,
4156 G_IO_ERROR,
4157 G_IO_ERROR_INVALID_ARGUMENT,
4158 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4159 return NULL;
4162 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4163 return NULL;
4165 iface = G_FILE_GET_IFACE (file);
4167 return (* iface->set_display_name) (file, display_name, cancellable, error);
4171 * g_file_set_display_name_async:
4172 * @file: input #GFile
4173 * @display_name: a string
4174 * @io_priority: the [I/O priority][io-priority] of the request
4175 * @cancellable: (allow-none): optional #GCancellable object,
4176 * %NULL to ignore
4177 * @callback: (scope async): a #GAsyncReadyCallback to call
4178 * when the request is satisfied
4179 * @user_data: (closure): the data to pass to callback function
4181 * Asynchronously sets the display name for a given #GFile.
4183 * For more details, see g_file_set_display_name() which is
4184 * the synchronous version of this call.
4186 * When the operation is finished, @callback will be called.
4187 * You can then call g_file_set_display_name_finish() to get
4188 * the result of the operation.
4190 void
4191 g_file_set_display_name_async (GFile *file,
4192 const gchar *display_name,
4193 gint io_priority,
4194 GCancellable *cancellable,
4195 GAsyncReadyCallback callback,
4196 gpointer user_data)
4198 GFileIface *iface;
4200 g_return_if_fail (G_IS_FILE (file));
4201 g_return_if_fail (display_name != NULL);
4203 iface = G_FILE_GET_IFACE (file);
4204 (* iface->set_display_name_async) (file,
4205 display_name,
4206 io_priority,
4207 cancellable,
4208 callback,
4209 user_data);
4213 * g_file_set_display_name_finish:
4214 * @file: input #GFile
4215 * @res: a #GAsyncResult
4216 * @error: a #GError, or %NULL
4218 * Finishes setting a display name started with
4219 * g_file_set_display_name_async().
4221 * Returns: (transfer full): a #GFile or %NULL on error.
4222 * Free the returned object with g_object_unref().
4224 GFile *
4225 g_file_set_display_name_finish (GFile *file,
4226 GAsyncResult *res,
4227 GError **error)
4229 GFileIface *iface;
4231 g_return_val_if_fail (G_IS_FILE (file), NULL);
4232 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4234 if (g_async_result_legacy_propagate_error (res, error))
4235 return NULL;
4237 iface = G_FILE_GET_IFACE (file);
4238 return (* iface->set_display_name_finish) (file, res, error);
4242 * g_file_query_settable_attributes:
4243 * @file: input #GFile
4244 * @cancellable: (allow-none): optional #GCancellable object,
4245 * %NULL to ignore
4246 * @error: a #GError, or %NULL
4248 * Obtain the list of settable attributes for the file.
4250 * Returns the type and full attribute name of all the attributes
4251 * that can be set on this file. This doesn't mean setting it will
4252 * always succeed though, you might get an access failure, or some
4253 * specific file may not support a specific attribute.
4255 * If @cancellable is not %NULL, then the operation can be cancelled by
4256 * triggering the cancellable object from another thread. If the operation
4257 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4259 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4260 * When you are done with it, release it with
4261 * g_file_attribute_info_list_unref()
4263 GFileAttributeInfoList *
4264 g_file_query_settable_attributes (GFile *file,
4265 GCancellable *cancellable,
4266 GError **error)
4268 GFileIface *iface;
4269 GError *my_error;
4270 GFileAttributeInfoList *list;
4272 g_return_val_if_fail (G_IS_FILE (file), NULL);
4274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4275 return NULL;
4277 iface = G_FILE_GET_IFACE (file);
4279 if (iface->query_settable_attributes == NULL)
4280 return g_file_attribute_info_list_new ();
4282 my_error = NULL;
4283 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4285 if (list == NULL)
4287 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4289 list = g_file_attribute_info_list_new ();
4290 g_error_free (my_error);
4292 else
4293 g_propagate_error (error, my_error);
4296 return list;
4300 * g_file_query_writable_namespaces:
4301 * @file: input #GFile
4302 * @cancellable: (allow-none): optional #GCancellable object,
4303 * %NULL to ignore
4304 * @error: a #GError, or %NULL
4306 * Obtain the list of attribute namespaces where new attributes
4307 * can be created by a user. An example of this is extended
4308 * attributes (in the "xattr" namespace).
4310 * If @cancellable is not %NULL, then the operation can be cancelled by
4311 * triggering the cancellable object from another thread. If the operation
4312 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4314 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4315 * When you are done with it, release it with
4316 * g_file_attribute_info_list_unref()
4318 GFileAttributeInfoList *
4319 g_file_query_writable_namespaces (GFile *file,
4320 GCancellable *cancellable,
4321 GError **error)
4323 GFileIface *iface;
4324 GError *my_error;
4325 GFileAttributeInfoList *list;
4327 g_return_val_if_fail (G_IS_FILE (file), NULL);
4329 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4330 return NULL;
4332 iface = G_FILE_GET_IFACE (file);
4334 if (iface->query_writable_namespaces == NULL)
4335 return g_file_attribute_info_list_new ();
4337 my_error = NULL;
4338 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4340 if (list == NULL)
4342 g_warn_if_reached();
4343 list = g_file_attribute_info_list_new ();
4346 if (my_error != NULL)
4348 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4350 g_error_free (my_error);
4352 else
4353 g_propagate_error (error, my_error);
4356 return list;
4360 * g_file_set_attribute:
4361 * @file: input #GFile
4362 * @attribute: a string containing the attribute's name
4363 * @type: The type of the attribute
4364 * @value_p: (allow-none): a pointer to the value (or the pointer
4365 * itself if the type is a pointer type)
4366 * @flags: a set of #GFileQueryInfoFlags
4367 * @cancellable: (allow-none): optional #GCancellable object,
4368 * %NULL to ignore
4369 * @error: a #GError, or %NULL
4371 * Sets an attribute in the file with attribute name @attribute to @value.
4373 * Some attributes can be unset by setting @attribute to
4374 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4376 * If @cancellable is not %NULL, then the operation can be cancelled by
4377 * triggering the cancellable object from another thread. If the operation
4378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4380 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4382 gboolean
4383 g_file_set_attribute (GFile *file,
4384 const gchar *attribute,
4385 GFileAttributeType type,
4386 gpointer value_p,
4387 GFileQueryInfoFlags flags,
4388 GCancellable *cancellable,
4389 GError **error)
4391 GFileIface *iface;
4393 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4394 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4396 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4397 return FALSE;
4399 iface = G_FILE_GET_IFACE (file);
4401 if (iface->set_attribute == NULL)
4403 g_set_error_literal (error, G_IO_ERROR,
4404 G_IO_ERROR_NOT_SUPPORTED,
4405 _("Operation not supported"));
4406 return FALSE;
4409 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4413 * g_file_set_attributes_from_info:
4414 * @file: input #GFile
4415 * @info: a #GFileInfo
4416 * @flags: #GFileQueryInfoFlags
4417 * @cancellable: (allow-none): optional #GCancellable object,
4418 * %NULL to ignore
4419 * @error: a #GError, or %NULL
4421 * Tries to set all attributes in the #GFileInfo on the target
4422 * values, not stopping on the first error.
4424 * If there is any error during this operation then @error will
4425 * be set to the first error. Error on particular fields are flagged
4426 * by setting the "status" field in the attribute value to
4427 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4428 * also detect further errors.
4430 * If @cancellable is not %NULL, then the operation can be cancelled by
4431 * triggering the cancellable object from another thread. If the operation
4432 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4434 * Returns: %FALSE if there was any error, %TRUE otherwise.
4436 gboolean
4437 g_file_set_attributes_from_info (GFile *file,
4438 GFileInfo *info,
4439 GFileQueryInfoFlags flags,
4440 GCancellable *cancellable,
4441 GError **error)
4443 GFileIface *iface;
4445 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4446 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4448 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4449 return FALSE;
4451 g_file_info_clear_status (info);
4453 iface = G_FILE_GET_IFACE (file);
4455 return (* iface->set_attributes_from_info) (file,
4456 info,
4457 flags,
4458 cancellable,
4459 error);
4462 static gboolean
4463 g_file_real_set_attributes_from_info (GFile *file,
4464 GFileInfo *info,
4465 GFileQueryInfoFlags flags,
4466 GCancellable *cancellable,
4467 GError **error)
4469 char **attributes;
4470 int i;
4471 gboolean res;
4472 GFileAttributeValue *value;
4474 res = TRUE;
4476 attributes = g_file_info_list_attributes (info, NULL);
4478 for (i = 0; attributes[i] != NULL; i++)
4480 value = _g_file_info_get_attribute_value (info, attributes[i]);
4482 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4483 continue;
4485 if (!g_file_set_attribute (file, attributes[i],
4486 value->type, _g_file_attribute_value_peek_as_pointer (value),
4487 flags, cancellable, error))
4489 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4490 res = FALSE;
4491 /* Don't set error multiple times */
4492 error = NULL;
4494 else
4495 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4498 g_strfreev (attributes);
4500 return res;
4504 * g_file_set_attributes_async:
4505 * @file: input #GFile
4506 * @info: a #GFileInfo
4507 * @flags: a #GFileQueryInfoFlags
4508 * @io_priority: the [I/O priority][io-priority] of the request
4509 * @cancellable: (allow-none): optional #GCancellable object,
4510 * %NULL to ignore
4511 * @callback: (scope async): a #GAsyncReadyCallback
4512 * @user_data: (closure): a #gpointer
4514 * Asynchronously sets the attributes of @file with @info.
4516 * For more details, see g_file_set_attributes_from_info(),
4517 * which is the synchronous version of this call.
4519 * When the operation is finished, @callback will be called.
4520 * You can then call g_file_set_attributes_finish() to get
4521 * the result of the operation.
4523 void
4524 g_file_set_attributes_async (GFile *file,
4525 GFileInfo *info,
4526 GFileQueryInfoFlags flags,
4527 int io_priority,
4528 GCancellable *cancellable,
4529 GAsyncReadyCallback callback,
4530 gpointer user_data)
4532 GFileIface *iface;
4534 g_return_if_fail (G_IS_FILE (file));
4535 g_return_if_fail (G_IS_FILE_INFO (info));
4537 iface = G_FILE_GET_IFACE (file);
4538 (* iface->set_attributes_async) (file,
4539 info,
4540 flags,
4541 io_priority,
4542 cancellable,
4543 callback,
4544 user_data);
4548 * g_file_set_attributes_finish:
4549 * @file: input #GFile
4550 * @result: a #GAsyncResult
4551 * @info: (out) (transfer full): a #GFileInfo
4552 * @error: a #GError, or %NULL
4554 * Finishes setting an attribute started in g_file_set_attributes_async().
4556 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4558 gboolean
4559 g_file_set_attributes_finish (GFile *file,
4560 GAsyncResult *result,
4561 GFileInfo **info,
4562 GError **error)
4564 GFileIface *iface;
4566 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4567 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4569 /* No standard handling of errors here, as we must set info even
4570 * on errors
4572 iface = G_FILE_GET_IFACE (file);
4573 return (* iface->set_attributes_finish) (file, result, info, error);
4577 * g_file_set_attribute_string:
4578 * @file: input #GFile
4579 * @attribute: a string containing the attribute's name
4580 * @value: a string containing the attribute's value
4581 * @flags: #GFileQueryInfoFlags
4582 * @cancellable: (allow-none): optional #GCancellable object,
4583 * %NULL to ignore
4584 * @error: a #GError, or %NULL
4586 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4587 * If @attribute is of a different type, this operation will fail.
4589 * If @cancellable is not %NULL, then the operation can be cancelled by
4590 * triggering the cancellable object from another thread. If the operation
4591 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4593 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4595 gboolean
4596 g_file_set_attribute_string (GFile *file,
4597 const char *attribute,
4598 const char *value,
4599 GFileQueryInfoFlags flags,
4600 GCancellable *cancellable,
4601 GError **error)
4603 return g_file_set_attribute (file, attribute,
4604 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4605 flags, cancellable, error);
4609 * g_file_set_attribute_byte_string:
4610 * @file: input #GFile
4611 * @attribute: a string containing the attribute's name
4612 * @value: a string containing the attribute's new value
4613 * @flags: a #GFileQueryInfoFlags
4614 * @cancellable: (allow-none): optional #GCancellable object,
4615 * %NULL to ignore
4616 * @error: a #GError, or %NULL
4618 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4619 * If @attribute is of a different type, this operation will fail,
4620 * returning %FALSE.
4622 * If @cancellable is not %NULL, then the operation can be cancelled by
4623 * triggering the cancellable object from another thread. If the operation
4624 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4626 * Returns: %TRUE if the @attribute was successfully set to @value
4627 * in the @file, %FALSE otherwise.
4629 gboolean
4630 g_file_set_attribute_byte_string (GFile *file,
4631 const gchar *attribute,
4632 const gchar *value,
4633 GFileQueryInfoFlags flags,
4634 GCancellable *cancellable,
4635 GError **error)
4637 return g_file_set_attribute (file, attribute,
4638 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4639 flags, cancellable, error);
4643 * g_file_set_attribute_uint32:
4644 * @file: input #GFile
4645 * @attribute: a string containing the attribute's name
4646 * @value: a #guint32 containing the attribute's new value
4647 * @flags: a #GFileQueryInfoFlags
4648 * @cancellable: (allow-none): optional #GCancellable object,
4649 * %NULL to ignore
4650 * @error: a #GError, or %NULL
4652 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4653 * If @attribute is of a different type, this operation will fail.
4655 * If @cancellable is not %NULL, then the operation can be cancelled by
4656 * triggering the cancellable object from another thread. If the operation
4657 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4659 * Returns: %TRUE if the @attribute was successfully set to @value
4660 * in the @file, %FALSE otherwise.
4662 gboolean
4663 g_file_set_attribute_uint32 (GFile *file,
4664 const gchar *attribute,
4665 guint32 value,
4666 GFileQueryInfoFlags flags,
4667 GCancellable *cancellable,
4668 GError **error)
4670 return g_file_set_attribute (file, attribute,
4671 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4672 flags, cancellable, error);
4676 * g_file_set_attribute_int32:
4677 * @file: input #GFile
4678 * @attribute: a string containing the attribute's name
4679 * @value: a #gint32 containing the attribute's new value
4680 * @flags: a #GFileQueryInfoFlags
4681 * @cancellable: (allow-none): optional #GCancellable object,
4682 * %NULL to ignore
4683 * @error: a #GError, or %NULL
4685 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4686 * If @attribute is of a different type, this operation will fail.
4688 * If @cancellable is not %NULL, then the operation can be cancelled by
4689 * triggering the cancellable object from another thread. If the operation
4690 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4692 * Returns: %TRUE if the @attribute was successfully set to @value
4693 * in the @file, %FALSE otherwise.
4695 gboolean
4696 g_file_set_attribute_int32 (GFile *file,
4697 const gchar *attribute,
4698 gint32 value,
4699 GFileQueryInfoFlags flags,
4700 GCancellable *cancellable,
4701 GError **error)
4703 return g_file_set_attribute (file, attribute,
4704 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4705 flags, cancellable, error);
4709 * g_file_set_attribute_uint64:
4710 * @file: input #GFile
4711 * @attribute: a string containing the attribute's name
4712 * @value: a #guint64 containing the attribute's new value
4713 * @flags: a #GFileQueryInfoFlags
4714 * @cancellable: (allow-none): optional #GCancellable object,
4715 * %NULL to ignore
4716 * @error: a #GError, or %NULL
4718 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4719 * If @attribute is of a different type, this operation will fail.
4721 * If @cancellable is not %NULL, then the operation can be cancelled by
4722 * triggering the cancellable object from another thread. If the operation
4723 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4725 * Returns: %TRUE if the @attribute was successfully set to @value
4726 * in the @file, %FALSE otherwise.
4728 gboolean
4729 g_file_set_attribute_uint64 (GFile *file,
4730 const gchar *attribute,
4731 guint64 value,
4732 GFileQueryInfoFlags flags,
4733 GCancellable *cancellable,
4734 GError **error)
4736 return g_file_set_attribute (file, attribute,
4737 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4738 flags, cancellable, error);
4742 * g_file_set_attribute_int64:
4743 * @file: input #GFile
4744 * @attribute: a string containing the attribute's name
4745 * @value: a #guint64 containing the attribute's new value
4746 * @flags: a #GFileQueryInfoFlags
4747 * @cancellable: (allow-none): optional #GCancellable object,
4748 * %NULL to ignore
4749 * @error: a #GError, or %NULL
4751 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4752 * If @attribute is of a different type, this operation will fail.
4754 * If @cancellable is not %NULL, then the operation can be cancelled by
4755 * triggering the cancellable object from another thread. If the operation
4756 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4758 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4760 gboolean
4761 g_file_set_attribute_int64 (GFile *file,
4762 const gchar *attribute,
4763 gint64 value,
4764 GFileQueryInfoFlags flags,
4765 GCancellable *cancellable,
4766 GError **error)
4768 return g_file_set_attribute (file, attribute,
4769 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4770 flags, cancellable, error);
4774 * g_file_mount_mountable:
4775 * @file: input #GFile
4776 * @flags: flags affecting the operation
4777 * @mount_operation: (allow-none): a #GMountOperation,
4778 * or %NULL to avoid user interaction
4779 * @cancellable: (allow-none): optional #GCancellable object,
4780 * %NULL to ignore
4781 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4782 * when the request is satisfied, or %NULL
4783 * @user_data: (closure): the data to pass to callback function
4785 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4786 * Using @mount_operation, you can request callbacks when, for instance,
4787 * passwords are needed during authentication.
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 * When the operation is finished, @callback will be called.
4794 * You can then call g_file_mount_mountable_finish() to get
4795 * the result of the operation.
4797 void
4798 g_file_mount_mountable (GFile *file,
4799 GMountMountFlags flags,
4800 GMountOperation *mount_operation,
4801 GCancellable *cancellable,
4802 GAsyncReadyCallback callback,
4803 gpointer user_data)
4805 GFileIface *iface;
4807 g_return_if_fail (G_IS_FILE (file));
4809 iface = G_FILE_GET_IFACE (file);
4811 if (iface->mount_mountable == NULL)
4813 g_task_report_new_error (file, callback, user_data,
4814 g_file_mount_mountable,
4815 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4816 _("Operation not supported"));
4817 return;
4820 (* iface->mount_mountable) (file,
4821 flags,
4822 mount_operation,
4823 cancellable,
4824 callback,
4825 user_data);
4829 * g_file_mount_mountable_finish:
4830 * @file: input #GFile
4831 * @result: a #GAsyncResult
4832 * @error: a #GError, or %NULL
4834 * Finishes a mount operation. See g_file_mount_mountable() for details.
4836 * Finish an asynchronous mount operation that was started
4837 * with g_file_mount_mountable().
4839 * Returns: (transfer full): a #GFile or %NULL on error.
4840 * Free the returned object with g_object_unref().
4842 GFile *
4843 g_file_mount_mountable_finish (GFile *file,
4844 GAsyncResult *result,
4845 GError **error)
4847 GFileIface *iface;
4849 g_return_val_if_fail (G_IS_FILE (file), NULL);
4850 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4852 if (g_async_result_legacy_propagate_error (result, error))
4853 return NULL;
4854 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4855 return g_task_propagate_pointer (G_TASK (result), error);
4857 iface = G_FILE_GET_IFACE (file);
4858 return (* iface->mount_mountable_finish) (file, result, error);
4862 * g_file_unmount_mountable:
4863 * @file: input #GFile
4864 * @flags: flags affecting the operation
4865 * @cancellable: (allow-none): optional #GCancellable object,
4866 * %NULL to ignore
4867 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4868 * when the request is satisfied, or %NULL
4869 * @user_data: (closure): the data to pass to callback function
4871 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4873 * If @cancellable is not %NULL, then the operation can be cancelled by
4874 * triggering the cancellable object from another thread. If the operation
4875 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4877 * When the operation is finished, @callback will be called.
4878 * You can then call g_file_unmount_mountable_finish() to get
4879 * the result of the operation.
4881 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4883 void
4884 g_file_unmount_mountable (GFile *file,
4885 GMountUnmountFlags flags,
4886 GCancellable *cancellable,
4887 GAsyncReadyCallback callback,
4888 gpointer user_data)
4890 GFileIface *iface;
4892 g_return_if_fail (G_IS_FILE (file));
4894 iface = G_FILE_GET_IFACE (file);
4896 if (iface->unmount_mountable == NULL)
4898 g_task_report_new_error (file, callback, user_data,
4899 g_file_unmount_mountable_with_operation,
4900 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4901 _("Operation not supported"));
4902 return;
4905 (* iface->unmount_mountable) (file,
4906 flags,
4907 cancellable,
4908 callback,
4909 user_data);
4913 * g_file_unmount_mountable_finish:
4914 * @file: input #GFile
4915 * @result: a #GAsyncResult
4916 * @error: a #GError, or %NULL
4918 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4920 * Finish an asynchronous unmount operation that was started
4921 * with g_file_unmount_mountable().
4923 * Returns: %TRUE if the operation finished successfully.
4924 * %FALSE otherwise.
4926 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4927 * instead.
4929 gboolean
4930 g_file_unmount_mountable_finish (GFile *file,
4931 GAsyncResult *result,
4932 GError **error)
4934 GFileIface *iface;
4936 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4937 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4939 if (g_async_result_legacy_propagate_error (result, error))
4940 return FALSE;
4941 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4942 return g_task_propagate_boolean (G_TASK (result), error);
4944 iface = G_FILE_GET_IFACE (file);
4945 return (* iface->unmount_mountable_finish) (file, result, error);
4949 * g_file_unmount_mountable_with_operation:
4950 * @file: input #GFile
4951 * @flags: flags affecting the operation
4952 * @mount_operation: (allow-none): a #GMountOperation,
4953 * or %NULL to avoid user interaction
4954 * @cancellable: (allow-none): optional #GCancellable object,
4955 * %NULL to ignore
4956 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4957 * when the request is satisfied, or %NULL
4958 * @user_data: (closure): the data to pass to callback function
4960 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4962 * If @cancellable is not %NULL, then the operation can be cancelled by
4963 * triggering the cancellable object from another thread. If the operation
4964 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4966 * When the operation is finished, @callback will be called.
4967 * You can then call g_file_unmount_mountable_finish() to get
4968 * the result of the operation.
4970 * Since: 2.22
4972 void
4973 g_file_unmount_mountable_with_operation (GFile *file,
4974 GMountUnmountFlags flags,
4975 GMountOperation *mount_operation,
4976 GCancellable *cancellable,
4977 GAsyncReadyCallback callback,
4978 gpointer user_data)
4980 GFileIface *iface;
4982 g_return_if_fail (G_IS_FILE (file));
4984 iface = G_FILE_GET_IFACE (file);
4986 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4988 g_task_report_new_error (file, callback, user_data,
4989 g_file_unmount_mountable_with_operation,
4990 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4991 _("Operation not supported"));
4992 return;
4995 if (iface->unmount_mountable_with_operation != NULL)
4996 (* iface->unmount_mountable_with_operation) (file,
4997 flags,
4998 mount_operation,
4999 cancellable,
5000 callback,
5001 user_data);
5002 else
5003 (* iface->unmount_mountable) (file,
5004 flags,
5005 cancellable,
5006 callback,
5007 user_data);
5011 * g_file_unmount_mountable_with_operation_finish:
5012 * @file: input #GFile
5013 * @result: a #GAsyncResult
5014 * @error: a #GError, or %NULL
5016 * Finishes an unmount operation,
5017 * see g_file_unmount_mountable_with_operation() for details.
5019 * Finish an asynchronous unmount operation that was started
5020 * with g_file_unmount_mountable_with_operation().
5022 * Returns: %TRUE if the operation finished successfully.
5023 * %FALSE otherwise.
5025 * Since: 2.22
5027 gboolean
5028 g_file_unmount_mountable_with_operation_finish (GFile *file,
5029 GAsyncResult *result,
5030 GError **error)
5032 GFileIface *iface;
5034 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5035 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5037 if (g_async_result_legacy_propagate_error (result, error))
5038 return FALSE;
5039 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5040 return g_task_propagate_boolean (G_TASK (result), error);
5042 iface = G_FILE_GET_IFACE (file);
5043 if (iface->unmount_mountable_with_operation_finish != NULL)
5044 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5045 else
5046 return (* iface->unmount_mountable_finish) (file, result, error);
5050 * g_file_eject_mountable:
5051 * @file: input #GFile
5052 * @flags: flags affecting the operation
5053 * @cancellable: (allow-none): optional #GCancellable object,
5054 * %NULL to ignore
5055 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5056 * when the request is satisfied, or %NULL
5057 * @user_data: (closure): the data to pass to callback function
5059 * Starts an asynchronous eject on a mountable.
5060 * When this operation has completed, @callback will be called with
5061 * @user_user data, and the operation can be finalized with
5062 * g_file_eject_mountable_finish().
5064 * If @cancellable is not %NULL, then the operation can be cancelled by
5065 * triggering the cancellable object from another thread. If the operation
5066 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5068 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5070 void
5071 g_file_eject_mountable (GFile *file,
5072 GMountUnmountFlags flags,
5073 GCancellable *cancellable,
5074 GAsyncReadyCallback callback,
5075 gpointer user_data)
5077 GFileIface *iface;
5079 g_return_if_fail (G_IS_FILE (file));
5081 iface = G_FILE_GET_IFACE (file);
5083 if (iface->eject_mountable == NULL)
5085 g_task_report_new_error (file, callback, user_data,
5086 g_file_eject_mountable_with_operation,
5087 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5088 _("Operation not supported"));
5089 return;
5092 (* iface->eject_mountable) (file,
5093 flags,
5094 cancellable,
5095 callback,
5096 user_data);
5100 * g_file_eject_mountable_finish:
5101 * @file: input #GFile
5102 * @result: a #GAsyncResult
5103 * @error: a #GError, or %NULL
5105 * Finishes an asynchronous eject operation started by
5106 * g_file_eject_mountable().
5108 * Returns: %TRUE if the @file was ejected successfully.
5109 * %FALSE otherwise.
5111 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5112 * instead.
5114 gboolean
5115 g_file_eject_mountable_finish (GFile *file,
5116 GAsyncResult *result,
5117 GError **error)
5119 GFileIface *iface;
5121 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5122 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5124 if (g_async_result_legacy_propagate_error (result, error))
5125 return FALSE;
5126 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5127 return g_task_propagate_boolean (G_TASK (result), error);
5129 iface = G_FILE_GET_IFACE (file);
5130 return (* iface->eject_mountable_finish) (file, result, error);
5134 * g_file_eject_mountable_with_operation:
5135 * @file: input #GFile
5136 * @flags: flags affecting the operation
5137 * @mount_operation: (allow-none): a #GMountOperation,
5138 * or %NULL to avoid user interaction
5139 * @cancellable: (allow-none): optional #GCancellable object,
5140 * %NULL to ignore
5141 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5142 * when the request is satisfied, or %NULL
5143 * @user_data: (closure): the data to pass to callback function
5145 * Starts an asynchronous eject on a mountable.
5146 * When this operation has completed, @callback will be called with
5147 * @user_user data, and the operation can be finalized with
5148 * g_file_eject_mountable_with_operation_finish().
5150 * If @cancellable is not %NULL, then the operation can be cancelled by
5151 * triggering the cancellable object from another thread. If the operation
5152 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5154 * Since: 2.22
5156 void
5157 g_file_eject_mountable_with_operation (GFile *file,
5158 GMountUnmountFlags flags,
5159 GMountOperation *mount_operation,
5160 GCancellable *cancellable,
5161 GAsyncReadyCallback callback,
5162 gpointer user_data)
5164 GFileIface *iface;
5166 g_return_if_fail (G_IS_FILE (file));
5168 iface = G_FILE_GET_IFACE (file);
5170 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5172 g_task_report_new_error (file, callback, user_data,
5173 g_file_eject_mountable_with_operation,
5174 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5175 _("Operation not supported"));
5176 return;
5179 if (iface->eject_mountable_with_operation != NULL)
5180 (* iface->eject_mountable_with_operation) (file,
5181 flags,
5182 mount_operation,
5183 cancellable,
5184 callback,
5185 user_data);
5186 else
5187 (* iface->eject_mountable) (file,
5188 flags,
5189 cancellable,
5190 callback,
5191 user_data);
5195 * g_file_eject_mountable_with_operation_finish:
5196 * @file: input #GFile
5197 * @result: a #GAsyncResult
5198 * @error: a #GError, or %NULL
5200 * Finishes an asynchronous eject operation started by
5201 * g_file_eject_mountable_with_operation().
5203 * Returns: %TRUE if the @file was ejected successfully.
5204 * %FALSE otherwise.
5206 * Since: 2.22
5208 gboolean
5209 g_file_eject_mountable_with_operation_finish (GFile *file,
5210 GAsyncResult *result,
5211 GError **error)
5213 GFileIface *iface;
5215 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5216 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5218 if (g_async_result_legacy_propagate_error (result, error))
5219 return FALSE;
5220 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5221 return g_task_propagate_boolean (G_TASK (result), error);
5223 iface = G_FILE_GET_IFACE (file);
5224 if (iface->eject_mountable_with_operation_finish != NULL)
5225 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5226 else
5227 return (* iface->eject_mountable_finish) (file, result, error);
5231 * g_file_monitor_directory:
5232 * @file: input #GFile
5233 * @flags: a set of #GFileMonitorFlags
5234 * @cancellable: (allow-none): optional #GCancellable object,
5235 * %NULL to ignore
5236 * @error: a #GError, or %NULL
5238 * Obtains a directory monitor for the given file.
5239 * This may fail if directory monitoring is not supported.
5241 * If @cancellable is not %NULL, then the operation can be cancelled by
5242 * triggering the cancellable object from another thread. If the operation
5243 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5245 * It does not make sense for @flags to contain
5246 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5247 * directories. It is not possible to monitor all the files in a
5248 * directory for changes made via hard links; if you want to do this then
5249 * you must register individual watches with g_file_monitor().
5251 * Virtual: monitor_dir
5252 * Returns: (transfer full): a #GFileMonitor for the given @file,
5253 * or %NULL on error.
5254 * Free the returned object with g_object_unref().
5256 GFileMonitor *
5257 g_file_monitor_directory (GFile *file,
5258 GFileMonitorFlags flags,
5259 GCancellable *cancellable,
5260 GError **error)
5262 GFileIface *iface;
5264 g_return_val_if_fail (G_IS_FILE (file), NULL);
5265 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5267 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5268 return NULL;
5270 iface = G_FILE_GET_IFACE (file);
5272 if (iface->monitor_dir == NULL)
5274 g_set_error_literal (error, G_IO_ERROR,
5275 G_IO_ERROR_NOT_SUPPORTED,
5276 _("Operation not supported"));
5277 return NULL;
5280 return (* iface->monitor_dir) (file, flags, cancellable, error);
5284 * g_file_monitor_file:
5285 * @file: input #GFile
5286 * @flags: a set of #GFileMonitorFlags
5287 * @cancellable: (allow-none): optional #GCancellable object,
5288 * %NULL to ignore
5289 * @error: a #GError, or %NULL
5291 * Obtains a file monitor for the given file. If no file notification
5292 * mechanism exists, then regular polling of the file is used.
5294 * If @cancellable is not %NULL, then the operation can be cancelled by
5295 * triggering the cancellable object from another thread. If the operation
5296 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5298 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5299 * will also attempt to report changes made to the file via another
5300 * filename (ie, a hard link). Without this flag, you can only rely on
5301 * changes made through the filename contained in @file to be
5302 * reported. Using this flag may result in an increase in resource
5303 * usage, and may not have any effect depending on the #GFileMonitor
5304 * backend and/or filesystem type.
5306 * Returns: (transfer full): a #GFileMonitor for the given @file,
5307 * or %NULL on error.
5308 * Free the returned object with g_object_unref().
5310 GFileMonitor *
5311 g_file_monitor_file (GFile *file,
5312 GFileMonitorFlags flags,
5313 GCancellable *cancellable,
5314 GError **error)
5316 GFileIface *iface;
5317 GFileMonitor *monitor;
5319 g_return_val_if_fail (G_IS_FILE (file), NULL);
5321 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5322 return NULL;
5324 iface = G_FILE_GET_IFACE (file);
5326 monitor = NULL;
5328 if (iface->monitor_file)
5329 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5331 /* Fallback to polling */
5332 if (monitor == NULL)
5333 monitor = _g_poll_file_monitor_new (file);
5335 return monitor;
5339 * g_file_monitor:
5340 * @file: input #GFile
5341 * @flags: a set of #GFileMonitorFlags
5342 * @cancellable: (allow-none): optional #GCancellable object,
5343 * %NULL to ignore
5344 * @error: a #GError, or %NULL
5346 * Obtains a file or directory monitor for the given file,
5347 * depending on the type of the file.
5349 * If @cancellable is not %NULL, then the operation can be cancelled by
5350 * triggering the cancellable object from another thread. If the operation
5351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5353 * Returns: (transfer full): a #GFileMonitor for the given @file,
5354 * or %NULL on error.
5355 * Free the returned object with g_object_unref().
5357 * Since: 2.18
5359 GFileMonitor *
5360 g_file_monitor (GFile *file,
5361 GFileMonitorFlags flags,
5362 GCancellable *cancellable,
5363 GError **error)
5365 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5366 return g_file_monitor_directory (file,
5367 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5368 cancellable, error);
5369 else
5370 return g_file_monitor_file (file, flags, cancellable, error);
5373 /********************************************
5374 * Default implementation of async ops *
5375 ********************************************/
5377 typedef struct {
5378 char *attributes;
5379 GFileQueryInfoFlags flags;
5380 } QueryInfoAsyncData;
5382 static void
5383 query_info_data_free (QueryInfoAsyncData *data)
5385 g_free (data->attributes);
5386 g_free (data);
5389 static void
5390 query_info_async_thread (GTask *task,
5391 gpointer object,
5392 gpointer task_data,
5393 GCancellable *cancellable)
5395 QueryInfoAsyncData *data = task_data;
5396 GFileInfo *info;
5397 GError *error = NULL;
5399 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5400 if (info)
5401 g_task_return_pointer (task, info, g_object_unref);
5402 else
5403 g_task_return_error (task, error);
5406 static void
5407 g_file_real_query_info_async (GFile *file,
5408 const char *attributes,
5409 GFileQueryInfoFlags flags,
5410 int io_priority,
5411 GCancellable *cancellable,
5412 GAsyncReadyCallback callback,
5413 gpointer user_data)
5415 GTask *task;
5416 QueryInfoAsyncData *data;
5418 data = g_new0 (QueryInfoAsyncData, 1);
5419 data->attributes = g_strdup (attributes);
5420 data->flags = flags;
5422 task = g_task_new (file, cancellable, callback, user_data);
5423 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5424 g_task_set_priority (task, io_priority);
5425 g_task_run_in_thread (task, query_info_async_thread);
5426 g_object_unref (task);
5429 static GFileInfo *
5430 g_file_real_query_info_finish (GFile *file,
5431 GAsyncResult *res,
5432 GError **error)
5434 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5436 return g_task_propagate_pointer (G_TASK (res), error);
5439 static void
5440 query_filesystem_info_async_thread (GTask *task,
5441 gpointer object,
5442 gpointer task_data,
5443 GCancellable *cancellable)
5445 const char *attributes = task_data;
5446 GFileInfo *info;
5447 GError *error = NULL;
5449 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5450 if (info)
5451 g_task_return_pointer (task, info, g_object_unref);
5452 else
5453 g_task_return_error (task, error);
5456 static void
5457 g_file_real_query_filesystem_info_async (GFile *file,
5458 const char *attributes,
5459 int io_priority,
5460 GCancellable *cancellable,
5461 GAsyncReadyCallback callback,
5462 gpointer user_data)
5464 GTask *task;
5466 task = g_task_new (file, cancellable, callback, user_data);
5467 g_task_set_task_data (task, g_strdup (attributes), g_free);
5468 g_task_set_priority (task, io_priority);
5469 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5470 g_object_unref (task);
5473 static GFileInfo *
5474 g_file_real_query_filesystem_info_finish (GFile *file,
5475 GAsyncResult *res,
5476 GError **error)
5478 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5480 return g_task_propagate_pointer (G_TASK (res), error);
5483 static void
5484 enumerate_children_async_thread (GTask *task,
5485 gpointer object,
5486 gpointer task_data,
5487 GCancellable *cancellable)
5489 QueryInfoAsyncData *data = task_data;
5490 GFileEnumerator *enumerator;
5491 GError *error = NULL;
5493 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5494 if (error)
5495 g_task_return_error (task, error);
5496 else
5497 g_task_return_pointer (task, enumerator, g_object_unref);
5500 static void
5501 g_file_real_enumerate_children_async (GFile *file,
5502 const char *attributes,
5503 GFileQueryInfoFlags flags,
5504 int io_priority,
5505 GCancellable *cancellable,
5506 GAsyncReadyCallback callback,
5507 gpointer user_data)
5509 GTask *task;
5510 QueryInfoAsyncData *data;
5512 data = g_new0 (QueryInfoAsyncData, 1);
5513 data->attributes = g_strdup (attributes);
5514 data->flags = flags;
5516 task = g_task_new (file, cancellable, callback, user_data);
5517 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5518 g_task_set_priority (task, io_priority);
5519 g_task_run_in_thread (task, enumerate_children_async_thread);
5520 g_object_unref (task);
5523 static GFileEnumerator *
5524 g_file_real_enumerate_children_finish (GFile *file,
5525 GAsyncResult *res,
5526 GError **error)
5528 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5530 return g_task_propagate_pointer (G_TASK (res), error);
5533 static void
5534 open_read_async_thread (GTask *task,
5535 gpointer object,
5536 gpointer task_data,
5537 GCancellable *cancellable)
5539 GFileInputStream *stream;
5540 GError *error = NULL;
5542 stream = g_file_read (G_FILE (object), cancellable, &error);
5543 if (stream)
5544 g_task_return_pointer (task, stream, g_object_unref);
5545 else
5546 g_task_return_error (task, error);
5549 static void
5550 g_file_real_read_async (GFile *file,
5551 int io_priority,
5552 GCancellable *cancellable,
5553 GAsyncReadyCallback callback,
5554 gpointer user_data)
5556 GTask *task;
5558 task = g_task_new (file, cancellable, callback, user_data);
5559 g_task_set_priority (task, io_priority);
5560 g_task_run_in_thread (task, open_read_async_thread);
5561 g_object_unref (task);
5564 static GFileInputStream *
5565 g_file_real_read_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 append_to_async_thread (GTask *task,
5576 gpointer source_object,
5577 gpointer task_data,
5578 GCancellable *cancellable)
5580 GFileCreateFlags *data = task_data;
5581 GFileOutputStream *stream;
5582 GError *error = NULL;
5584 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5585 if (stream)
5586 g_task_return_pointer (task, stream, g_object_unref);
5587 else
5588 g_task_return_error (task, error);
5591 static void
5592 g_file_real_append_to_async (GFile *file,
5593 GFileCreateFlags flags,
5594 int io_priority,
5595 GCancellable *cancellable,
5596 GAsyncReadyCallback callback,
5597 gpointer user_data)
5599 GFileCreateFlags *data;
5600 GTask *task;
5602 data = g_new0 (GFileCreateFlags, 1);
5603 *data = flags;
5605 task = g_task_new (file, cancellable, callback, user_data);
5606 g_task_set_task_data (task, data, g_free);
5607 g_task_set_priority (task, io_priority);
5609 g_task_run_in_thread (task, append_to_async_thread);
5610 g_object_unref (task);
5613 static GFileOutputStream *
5614 g_file_real_append_to_finish (GFile *file,
5615 GAsyncResult *res,
5616 GError **error)
5618 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5620 return g_task_propagate_pointer (G_TASK (res), error);
5623 static void
5624 create_async_thread (GTask *task,
5625 gpointer source_object,
5626 gpointer task_data,
5627 GCancellable *cancellable)
5629 GFileCreateFlags *data = task_data;
5630 GFileOutputStream *stream;
5631 GError *error = NULL;
5633 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5634 if (stream)
5635 g_task_return_pointer (task, stream, g_object_unref);
5636 else
5637 g_task_return_error (task, error);
5640 static void
5641 g_file_real_create_async (GFile *file,
5642 GFileCreateFlags flags,
5643 int io_priority,
5644 GCancellable *cancellable,
5645 GAsyncReadyCallback callback,
5646 gpointer user_data)
5648 GFileCreateFlags *data;
5649 GTask *task;
5651 data = g_new0 (GFileCreateFlags, 1);
5652 *data = flags;
5654 task = g_task_new (file, cancellable, callback, user_data);
5655 g_task_set_task_data (task, data, g_free);
5656 g_task_set_priority (task, io_priority);
5658 g_task_run_in_thread (task, create_async_thread);
5659 g_object_unref (task);
5662 static GFileOutputStream *
5663 g_file_real_create_finish (GFile *file,
5664 GAsyncResult *res,
5665 GError **error)
5667 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5669 return g_task_propagate_pointer (G_TASK (res), error);
5672 typedef struct {
5673 GFileOutputStream *stream;
5674 char *etag;
5675 gboolean make_backup;
5676 GFileCreateFlags flags;
5677 } ReplaceAsyncData;
5679 static void
5680 replace_async_data_free (ReplaceAsyncData *data)
5682 if (data->stream)
5683 g_object_unref (data->stream);
5684 g_free (data->etag);
5685 g_free (data);
5688 static void
5689 replace_async_thread (GTask *task,
5690 gpointer source_object,
5691 gpointer task_data,
5692 GCancellable *cancellable)
5694 GFileOutputStream *stream;
5695 ReplaceAsyncData *data = task_data;
5696 GError *error = NULL;
5698 stream = g_file_replace (G_FILE (source_object),
5699 data->etag,
5700 data->make_backup,
5701 data->flags,
5702 cancellable,
5703 &error);
5705 if (stream)
5706 g_task_return_pointer (task, stream, g_object_unref);
5707 else
5708 g_task_return_error (task, error);
5711 static void
5712 g_file_real_replace_async (GFile *file,
5713 const char *etag,
5714 gboolean make_backup,
5715 GFileCreateFlags flags,
5716 int io_priority,
5717 GCancellable *cancellable,
5718 GAsyncReadyCallback callback,
5719 gpointer user_data)
5721 GTask *task;
5722 ReplaceAsyncData *data;
5724 data = g_new0 (ReplaceAsyncData, 1);
5725 data->etag = g_strdup (etag);
5726 data->make_backup = make_backup;
5727 data->flags = flags;
5729 task = g_task_new (file, cancellable, callback, user_data);
5730 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5731 g_task_set_priority (task, io_priority);
5733 g_task_run_in_thread (task, replace_async_thread);
5734 g_object_unref (task);
5737 static GFileOutputStream *
5738 g_file_real_replace_finish (GFile *file,
5739 GAsyncResult *res,
5740 GError **error)
5742 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5744 return g_task_propagate_pointer (G_TASK (res), error);
5747 static void
5748 delete_async_thread (GTask *task,
5749 gpointer object,
5750 gpointer task_data,
5751 GCancellable *cancellable)
5753 GError *error = NULL;
5755 if (g_file_delete (G_FILE (object), cancellable, &error))
5756 g_task_return_boolean (task, TRUE);
5757 else
5758 g_task_return_error (task, error);
5761 static void
5762 g_file_real_delete_async (GFile *file,
5763 int io_priority,
5764 GCancellable *cancellable,
5765 GAsyncReadyCallback callback,
5766 gpointer user_data)
5768 GTask *task;
5770 task = g_task_new (file, cancellable, callback, user_data);
5771 g_task_set_priority (task, io_priority);
5772 g_task_run_in_thread (task, delete_async_thread);
5773 g_object_unref (task);
5776 static gboolean
5777 g_file_real_delete_finish (GFile *file,
5778 GAsyncResult *res,
5779 GError **error)
5781 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5783 return g_task_propagate_boolean (G_TASK (res), error);
5786 static void
5787 trash_async_thread (GTask *task,
5788 gpointer object,
5789 gpointer task_data,
5790 GCancellable *cancellable)
5792 GError *error = NULL;
5794 if (g_file_trash (G_FILE (object), cancellable, &error))
5795 g_task_return_boolean (task, TRUE);
5796 else
5797 g_task_return_error (task, error);
5800 static void
5801 g_file_real_trash_async (GFile *file,
5802 int io_priority,
5803 GCancellable *cancellable,
5804 GAsyncReadyCallback callback,
5805 gpointer user_data)
5807 GTask *task;
5809 task = g_task_new (file, cancellable, callback, user_data);
5810 g_task_set_priority (task, io_priority);
5811 g_task_run_in_thread (task, trash_async_thread);
5812 g_object_unref (task);
5815 static gboolean
5816 g_file_real_trash_finish (GFile *file,
5817 GAsyncResult *res,
5818 GError **error)
5820 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5822 return g_task_propagate_boolean (G_TASK (res), error);
5825 static void
5826 make_directory_async_thread (GTask *task,
5827 gpointer object,
5828 gpointer task_data,
5829 GCancellable *cancellable)
5831 GError *error = NULL;
5833 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5834 g_task_return_boolean (task, TRUE);
5835 else
5836 g_task_return_error (task, error);
5839 static void
5840 g_file_real_make_directory_async (GFile *file,
5841 int io_priority,
5842 GCancellable *cancellable,
5843 GAsyncReadyCallback callback,
5844 gpointer user_data)
5846 GTask *task;
5848 task = g_task_new (file, cancellable, callback, user_data);
5849 g_task_set_priority (task, io_priority);
5850 g_task_run_in_thread (task, make_directory_async_thread);
5851 g_object_unref (task);
5854 static gboolean
5855 g_file_real_make_directory_finish (GFile *file,
5856 GAsyncResult *res,
5857 GError **error)
5859 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5861 return g_task_propagate_boolean (G_TASK (res), error);
5864 static void
5865 open_readwrite_async_thread (GTask *task,
5866 gpointer object,
5867 gpointer task_data,
5868 GCancellable *cancellable)
5870 GFileIOStream *stream;
5871 GError *error = NULL;
5873 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5875 if (stream == NULL)
5876 g_task_return_error (task, error);
5877 else
5878 g_task_return_pointer (task, stream, g_object_unref);
5881 static void
5882 g_file_real_open_readwrite_async (GFile *file,
5883 int io_priority,
5884 GCancellable *cancellable,
5885 GAsyncReadyCallback callback,
5886 gpointer user_data)
5888 GTask *task;
5890 task = g_task_new (file, cancellable, callback, user_data);
5891 g_task_set_priority (task, io_priority);
5893 g_task_run_in_thread (task, open_readwrite_async_thread);
5894 g_object_unref (task);
5897 static GFileIOStream *
5898 g_file_real_open_readwrite_finish (GFile *file,
5899 GAsyncResult *res,
5900 GError **error)
5902 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5904 return g_task_propagate_pointer (G_TASK (res), error);
5907 static void
5908 create_readwrite_async_thread (GTask *task,
5909 gpointer object,
5910 gpointer task_data,
5911 GCancellable *cancellable)
5913 GFileCreateFlags *data = task_data;
5914 GFileIOStream *stream;
5915 GError *error = NULL;
5917 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5919 if (stream == NULL)
5920 g_task_return_error (task, error);
5921 else
5922 g_task_return_pointer (task, stream, g_object_unref);
5925 static void
5926 g_file_real_create_readwrite_async (GFile *file,
5927 GFileCreateFlags flags,
5928 int io_priority,
5929 GCancellable *cancellable,
5930 GAsyncReadyCallback callback,
5931 gpointer user_data)
5933 GFileCreateFlags *data;
5934 GTask *task;
5936 data = g_new0 (GFileCreateFlags, 1);
5937 *data = flags;
5939 task = g_task_new (file, cancellable, callback, user_data);
5940 g_task_set_task_data (task, data, g_free);
5941 g_task_set_priority (task, io_priority);
5943 g_task_run_in_thread (task, create_readwrite_async_thread);
5944 g_object_unref (task);
5947 static GFileIOStream *
5948 g_file_real_create_readwrite_finish (GFile *file,
5949 GAsyncResult *res,
5950 GError **error)
5952 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5954 return g_task_propagate_pointer (G_TASK (res), error);
5957 typedef struct {
5958 char *etag;
5959 gboolean make_backup;
5960 GFileCreateFlags flags;
5961 } ReplaceRWAsyncData;
5963 static void
5964 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5966 g_free (data->etag);
5967 g_free (data);
5970 static void
5971 replace_readwrite_async_thread (GTask *task,
5972 gpointer object,
5973 gpointer task_data,
5974 GCancellable *cancellable)
5976 GFileIOStream *stream;
5977 GError *error = NULL;
5978 ReplaceRWAsyncData *data = task_data;
5980 stream = g_file_replace_readwrite (G_FILE (object),
5981 data->etag,
5982 data->make_backup,
5983 data->flags,
5984 cancellable,
5985 &error);
5987 if (stream == NULL)
5988 g_task_return_error (task, error);
5989 else
5990 g_task_return_pointer (task, stream, g_object_unref);
5993 static void
5994 g_file_real_replace_readwrite_async (GFile *file,
5995 const char *etag,
5996 gboolean make_backup,
5997 GFileCreateFlags flags,
5998 int io_priority,
5999 GCancellable *cancellable,
6000 GAsyncReadyCallback callback,
6001 gpointer user_data)
6003 GTask *task;
6004 ReplaceRWAsyncData *data;
6006 data = g_new0 (ReplaceRWAsyncData, 1);
6007 data->etag = g_strdup (etag);
6008 data->make_backup = make_backup;
6009 data->flags = flags;
6011 task = g_task_new (file, cancellable, callback, user_data);
6012 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6013 g_task_set_priority (task, io_priority);
6015 g_task_run_in_thread (task, replace_readwrite_async_thread);
6016 g_object_unref (task);
6019 static GFileIOStream *
6020 g_file_real_replace_readwrite_finish (GFile *file,
6021 GAsyncResult *res,
6022 GError **error)
6024 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6026 return g_task_propagate_pointer (G_TASK (res), error);
6029 static void
6030 set_display_name_async_thread (GTask *task,
6031 gpointer object,
6032 gpointer task_data,
6033 GCancellable *cancellable)
6035 GError *error = NULL;
6036 char *name = task_data;
6037 GFile *file;
6039 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6041 if (file == NULL)
6042 g_task_return_error (task, error);
6043 else
6044 g_task_return_pointer (task, file, g_object_unref);
6047 static void
6048 g_file_real_set_display_name_async (GFile *file,
6049 const char *display_name,
6050 int io_priority,
6051 GCancellable *cancellable,
6052 GAsyncReadyCallback callback,
6053 gpointer user_data)
6055 GTask *task;
6057 task = g_task_new (file, cancellable, callback, user_data);
6058 g_task_set_task_data (task, g_strdup (display_name), g_free);
6059 g_task_set_priority (task, io_priority);
6061 g_task_run_in_thread (task, set_display_name_async_thread);
6062 g_object_unref (task);
6065 static GFile *
6066 g_file_real_set_display_name_finish (GFile *file,
6067 GAsyncResult *res,
6068 GError **error)
6070 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6072 return g_task_propagate_pointer (G_TASK (res), error);
6075 typedef struct {
6076 GFileQueryInfoFlags flags;
6077 GFileInfo *info;
6078 gboolean res;
6079 GError *error;
6080 } SetInfoAsyncData;
6082 static void
6083 set_info_data_free (SetInfoAsyncData *data)
6085 if (data->info)
6086 g_object_unref (data->info);
6087 if (data->error)
6088 g_error_free (data->error);
6089 g_free (data);
6092 static void
6093 set_info_async_thread (GTask *task,
6094 gpointer object,
6095 gpointer task_data,
6096 GCancellable *cancellable)
6098 SetInfoAsyncData *data = task_data;
6100 data->error = NULL;
6101 data->res = g_file_set_attributes_from_info (G_FILE (object),
6102 data->info,
6103 data->flags,
6104 cancellable,
6105 &data->error);
6108 static void
6109 g_file_real_set_attributes_async (GFile *file,
6110 GFileInfo *info,
6111 GFileQueryInfoFlags flags,
6112 int io_priority,
6113 GCancellable *cancellable,
6114 GAsyncReadyCallback callback,
6115 gpointer user_data)
6117 GTask *task;
6118 SetInfoAsyncData *data;
6120 data = g_new0 (SetInfoAsyncData, 1);
6121 data->info = g_file_info_dup (info);
6122 data->flags = flags;
6124 task = g_task_new (file, cancellable, callback, user_data);
6125 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6126 g_task_set_priority (task, io_priority);
6128 g_task_run_in_thread (task, set_info_async_thread);
6129 g_object_unref (task);
6132 static gboolean
6133 g_file_real_set_attributes_finish (GFile *file,
6134 GAsyncResult *res,
6135 GFileInfo **info,
6136 GError **error)
6138 SetInfoAsyncData *data;
6140 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6142 data = g_task_get_task_data (G_TASK (res));
6144 if (info)
6145 *info = g_object_ref (data->info);
6147 if (error != NULL && data->error)
6148 *error = g_error_copy (data->error);
6150 return data->res;
6153 static void
6154 find_enclosing_mount_async_thread (GTask *task,
6155 gpointer object,
6156 gpointer task_data,
6157 GCancellable *cancellable)
6159 GError *error = NULL;
6160 GMount *mount;
6162 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6164 if (mount == NULL)
6165 g_task_return_error (task, error);
6166 else
6167 g_task_return_pointer (task, mount, g_object_unref);
6170 static void
6171 g_file_real_find_enclosing_mount_async (GFile *file,
6172 int io_priority,
6173 GCancellable *cancellable,
6174 GAsyncReadyCallback callback,
6175 gpointer user_data)
6177 GTask *task;
6179 task = g_task_new (file, cancellable, callback, user_data);
6180 g_task_set_priority (task, io_priority);
6182 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6183 g_object_unref (task);
6186 static GMount *
6187 g_file_real_find_enclosing_mount_finish (GFile *file,
6188 GAsyncResult *res,
6189 GError **error)
6191 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6193 return g_task_propagate_pointer (G_TASK (res), error);
6197 typedef struct {
6198 GFile *source;
6199 GFile *destination;
6200 GFileCopyFlags flags;
6201 GFileProgressCallback progress_cb;
6202 gpointer progress_cb_data;
6203 } CopyAsyncData;
6205 static void
6206 copy_async_data_free (CopyAsyncData *data)
6208 g_object_unref (data->source);
6209 g_object_unref (data->destination);
6210 g_slice_free (CopyAsyncData, data);
6213 typedef struct {
6214 CopyAsyncData *data;
6215 goffset current_num_bytes;
6216 goffset total_num_bytes;
6217 } ProgressData;
6219 static gboolean
6220 copy_async_progress_in_main (gpointer user_data)
6222 ProgressData *progress = user_data;
6223 CopyAsyncData *data = progress->data;
6225 data->progress_cb (progress->current_num_bytes,
6226 progress->total_num_bytes,
6227 data->progress_cb_data);
6229 return FALSE;
6232 static void
6233 copy_async_progress_callback (goffset current_num_bytes,
6234 goffset total_num_bytes,
6235 gpointer user_data)
6237 GTask *task = user_data;
6238 CopyAsyncData *data = g_task_get_task_data (task);
6239 ProgressData *progress;
6241 progress = g_new (ProgressData, 1);
6242 progress->data = data;
6243 progress->current_num_bytes = current_num_bytes;
6244 progress->total_num_bytes = total_num_bytes;
6246 g_main_context_invoke_full (g_task_get_context (task),
6247 g_task_get_priority (task),
6248 copy_async_progress_in_main,
6249 progress,
6250 g_free);
6253 static void
6254 copy_async_thread (GTask *task,
6255 gpointer source,
6256 gpointer task_data,
6257 GCancellable *cancellable)
6259 CopyAsyncData *data = task_data;
6260 gboolean result;
6261 GError *error = NULL;
6263 result = g_file_copy (data->source,
6264 data->destination,
6265 data->flags,
6266 cancellable,
6267 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6268 task,
6269 &error);
6270 if (result)
6271 g_task_return_boolean (task, TRUE);
6272 else
6273 g_task_return_error (task, error);
6276 static void
6277 g_file_real_copy_async (GFile *source,
6278 GFile *destination,
6279 GFileCopyFlags flags,
6280 int io_priority,
6281 GCancellable *cancellable,
6282 GFileProgressCallback progress_callback,
6283 gpointer progress_callback_data,
6284 GAsyncReadyCallback callback,
6285 gpointer user_data)
6287 GTask *task;
6288 CopyAsyncData *data;
6290 data = g_slice_new (CopyAsyncData);
6291 data->source = g_object_ref (source);
6292 data->destination = g_object_ref (destination);
6293 data->flags = flags;
6294 data->progress_cb = progress_callback;
6295 data->progress_cb_data = progress_callback_data;
6297 task = g_task_new (source, cancellable, callback, user_data);
6298 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6299 g_task_set_priority (task, io_priority);
6300 g_task_run_in_thread (task, copy_async_thread);
6301 g_object_unref (task);
6304 static gboolean
6305 g_file_real_copy_finish (GFile *file,
6306 GAsyncResult *res,
6307 GError **error)
6309 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6311 return g_task_propagate_boolean (G_TASK (res), error);
6315 /********************************************
6316 * Default VFS operations *
6317 ********************************************/
6320 * g_file_new_for_path:
6321 * @path: (type filename): a string containing a relative or absolute path.
6322 * The string must be encoded in the glib filename encoding.
6324 * Constructs a #GFile for a given path. This operation never
6325 * fails, but the returned object might not support any I/O
6326 * operation if @path is malformed.
6328 * Returns: (transfer full): a new #GFile for the given @path.
6329 * Free the returned object with g_object_unref().
6331 GFile *
6332 g_file_new_for_path (const char *path)
6334 g_return_val_if_fail (path != NULL, NULL);
6336 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6340 * g_file_new_for_uri:
6341 * @uri: a UTF-8 string containing a URI
6343 * Constructs a #GFile for a given URI. This operation never
6344 * fails, but the returned object might not support any I/O
6345 * operation if @uri is malformed or if the uri type is
6346 * not supported.
6348 * Returns: (transfer full): a new #GFile for the given @uri.
6349 * Free the returned object with g_object_unref().
6351 GFile *
6352 g_file_new_for_uri (const char *uri)
6354 g_return_val_if_fail (uri != NULL, NULL);
6356 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6360 * g_file_new_tmp:
6361 * @tmpl: (type filename) (allow-none): Template for the file
6362 * name, as in g_file_open_tmp(), or %NULL for a default template
6363 * @iostream: (out): on return, a #GFileIOStream for the created file
6364 * @error: a #GError, or %NULL
6366 * Opens a file in the preferred directory for temporary files (as
6367 * returned by g_get_tmp_dir()) and returns a #GFile and
6368 * #GFileIOStream pointing to it.
6370 * @tmpl should be a string in the GLib file name encoding
6371 * containing a sequence of six 'X' characters, and containing no
6372 * directory components. If it is %NULL, a default template is used.
6374 * Unlike the other #GFile constructors, this will return %NULL if
6375 * a temporary file could not be created.
6377 * Returns: (transfer full): a new #GFile.
6378 * Free the returned object with g_object_unref().
6380 * Since: 2.32
6382 GFile *
6383 g_file_new_tmp (const char *tmpl,
6384 GFileIOStream **iostream,
6385 GError **error)
6387 gint fd;
6388 gchar *path;
6389 GFile *file;
6390 GFileOutputStream *output;
6392 g_return_val_if_fail (iostream != NULL, NULL);
6394 fd = g_file_open_tmp (tmpl, &path, error);
6395 if (fd == -1)
6396 return NULL;
6398 file = g_file_new_for_path (path);
6400 output = _g_local_file_output_stream_new (fd);
6401 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6403 g_object_unref (output);
6404 g_free (path);
6406 return file;
6410 * g_file_parse_name:
6411 * @parse_name: a file name or path to be parsed
6413 * Constructs a #GFile with the given @parse_name (i.e. something
6414 * given by g_file_get_parse_name()). This operation never fails,
6415 * but the returned object might not support any I/O operation if
6416 * the @parse_name cannot be parsed.
6418 * Returns: (transfer full): a new #GFile.
6420 GFile *
6421 g_file_parse_name (const char *parse_name)
6423 g_return_val_if_fail (parse_name != NULL, NULL);
6425 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6428 static gboolean
6429 is_valid_scheme_character (char c)
6431 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6434 /* Following RFC 2396, valid schemes are built like:
6435 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6437 static gboolean
6438 has_valid_scheme (const char *uri)
6440 const char *p;
6442 p = uri;
6444 if (!g_ascii_isalpha (*p))
6445 return FALSE;
6447 do {
6448 p++;
6449 } while (is_valid_scheme_character (*p));
6451 return *p == ':';
6454 static GFile *
6455 new_for_cmdline_arg (const gchar *arg,
6456 const gchar *cwd)
6458 GFile *file;
6459 char *filename;
6461 if (g_path_is_absolute (arg))
6462 return g_file_new_for_path (arg);
6464 if (has_valid_scheme (arg))
6465 return g_file_new_for_uri (arg);
6467 if (cwd == NULL)
6469 char *current_dir;
6471 current_dir = g_get_current_dir ();
6472 filename = g_build_filename (current_dir, arg, NULL);
6473 g_free (current_dir);
6475 else
6476 filename = g_build_filename (cwd, arg, NULL);
6478 file = g_file_new_for_path (filename);
6479 g_free (filename);
6481 return file;
6485 * g_file_new_for_commandline_arg:
6486 * @arg: a command line string
6488 * Creates a #GFile with the given argument from the command line.
6489 * The value of @arg can be either a URI, an absolute path or a
6490 * relative path resolved relative to the current working directory.
6491 * This operation never fails, but the returned object might not
6492 * support any I/O operation if @arg points to a malformed path.
6494 * Note that on Windows, this function expects its argument to be in
6495 * UTF-8 -- not the system code page. This means that you
6496 * should not use this function with string from argv as it is passed
6497 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6498 * the commandline. #GApplication also uses UTF-8 but
6499 * g_application_command_line_create_file_for_arg() may be more useful
6500 * for you there. It is also always possible to use this function with
6501 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6503 * Returns: (transfer full): a new #GFile.
6504 * Free the returned object with g_object_unref().
6506 GFile *
6507 g_file_new_for_commandline_arg (const char *arg)
6509 g_return_val_if_fail (arg != NULL, NULL);
6511 return new_for_cmdline_arg (arg, NULL);
6515 * g_file_new_for_commandline_arg_and_cwd:
6516 * @arg: a command line string
6517 * @cwd: (type filename): the current working directory of the commandline
6519 * Creates a #GFile with the given argument from the command line.
6521 * This function is similar to g_file_new_for_commandline_arg() except
6522 * that it allows for passing the current working directory as an
6523 * argument instead of using the current working directory of the
6524 * process.
6526 * This is useful if the commandline argument was given in a context
6527 * other than the invocation of the current process.
6529 * See also g_application_command_line_create_file_for_arg().
6531 * Returns: (transfer full): a new #GFile
6533 * Since: 2.36
6535 GFile *
6536 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6537 const gchar *cwd)
6539 g_return_val_if_fail (arg != NULL, NULL);
6540 g_return_val_if_fail (cwd != NULL, NULL);
6542 return new_for_cmdline_arg (arg, cwd);
6546 * g_file_mount_enclosing_volume:
6547 * @location: input #GFile
6548 * @flags: flags affecting the operation
6549 * @mount_operation: (allow-none): a #GMountOperation
6550 * or %NULL to avoid user interaction
6551 * @cancellable: (allow-none): optional #GCancellable object,
6552 * %NULL to ignore
6553 * @callback: (allow-none): a #GAsyncReadyCallback to call
6554 * when the request is satisfied, or %NULL
6555 * @user_data: the data to pass to callback function
6557 * Starts a @mount_operation, mounting the volume that contains
6558 * the file @location.
6560 * When this operation has completed, @callback will be called with
6561 * @user_user data, and the operation can be finalized with
6562 * g_file_mount_enclosing_volume_finish().
6564 * If @cancellable is not %NULL, then the operation can be cancelled by
6565 * triggering the cancellable object from another thread. If the operation
6566 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6568 void
6569 g_file_mount_enclosing_volume (GFile *location,
6570 GMountMountFlags flags,
6571 GMountOperation *mount_operation,
6572 GCancellable *cancellable,
6573 GAsyncReadyCallback callback,
6574 gpointer user_data)
6576 GFileIface *iface;
6578 g_return_if_fail (G_IS_FILE (location));
6580 iface = G_FILE_GET_IFACE (location);
6582 if (iface->mount_enclosing_volume == NULL)
6584 g_task_report_new_error (location, callback, user_data,
6585 g_file_mount_enclosing_volume,
6586 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6587 _("volume doesn't implement mount"));
6588 return;
6591 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6596 * g_file_mount_enclosing_volume_finish:
6597 * @location: input #GFile
6598 * @result: a #GAsyncResult
6599 * @error: a #GError, or %NULL
6601 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6603 * Returns: %TRUE if successful. If an error has occurred,
6604 * this function will return %FALSE and set @error
6605 * appropriately if present.
6607 gboolean
6608 g_file_mount_enclosing_volume_finish (GFile *location,
6609 GAsyncResult *result,
6610 GError **error)
6612 GFileIface *iface;
6614 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6615 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6617 if (g_async_result_legacy_propagate_error (result, error))
6618 return FALSE;
6619 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6620 return g_task_propagate_boolean (G_TASK (result), error);
6622 iface = G_FILE_GET_IFACE (location);
6624 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6627 /********************************************
6628 * Utility functions *
6629 ********************************************/
6632 * g_file_query_default_handler:
6633 * @file: a #GFile to open
6634 * @cancellable: optional #GCancellable object, %NULL to ignore
6635 * @error: a #GError, or %NULL
6637 * Returns the #GAppInfo that is registered as the default
6638 * application to handle the file specified by @file.
6640 * If @cancellable is not %NULL, then the operation can be cancelled by
6641 * triggering the cancellable object from another thread. If the operation
6642 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6644 * Returns: (transfer full): a #GAppInfo if the handle was found,
6645 * %NULL if there were errors.
6646 * When you are done with it, release it with g_object_unref()
6648 GAppInfo *
6649 g_file_query_default_handler (GFile *file,
6650 GCancellable *cancellable,
6651 GError **error)
6653 char *uri_scheme;
6654 const char *content_type;
6655 GAppInfo *appinfo;
6656 GFileInfo *info;
6657 char *path;
6659 uri_scheme = g_file_get_uri_scheme (file);
6660 if (uri_scheme && uri_scheme[0] != '\0')
6662 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6663 g_free (uri_scheme);
6665 if (appinfo != NULL)
6666 return appinfo;
6669 info = g_file_query_info (file,
6670 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6672 cancellable,
6673 error);
6674 if (info == NULL)
6675 return NULL;
6677 appinfo = NULL;
6679 content_type = g_file_info_get_content_type (info);
6680 if (content_type)
6682 /* Don't use is_native(), as we want to support fuse paths if available */
6683 path = g_file_get_path (file);
6684 appinfo = g_app_info_get_default_for_type (content_type,
6685 path == NULL);
6686 g_free (path);
6689 g_object_unref (info);
6691 if (appinfo != NULL)
6692 return appinfo;
6694 g_set_error_literal (error, G_IO_ERROR,
6695 G_IO_ERROR_NOT_SUPPORTED,
6696 _("No application is registered as handling this file"));
6697 return NULL;
6700 #define GET_CONTENT_BLOCK_SIZE 8192
6703 * g_file_load_contents:
6704 * @file: input #GFile
6705 * @cancellable: optional #GCancellable object, %NULL to ignore
6706 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6707 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6708 * or %NULL if the length is not needed
6709 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6710 * or %NULL if the entity tag is not needed
6711 * @error: a #GError, or %NULL
6713 * Loads the content of the file into memory. The data is always
6714 * zero-terminated, but this is not included in the resultant @length.
6715 * The returned @content should be freed with g_free() when no longer
6716 * needed.
6718 * If @cancellable is not %NULL, then the operation can be cancelled by
6719 * triggering the cancellable object from another thread. If the operation
6720 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6722 * Returns: %TRUE if the @file's contents were successfully loaded.
6723 * %FALSE if there were errors.
6725 gboolean
6726 g_file_load_contents (GFile *file,
6727 GCancellable *cancellable,
6728 char **contents,
6729 gsize *length,
6730 char **etag_out,
6731 GError **error)
6733 GFileInputStream *in;
6734 GByteArray *content;
6735 gsize pos;
6736 gssize res;
6737 GFileInfo *info;
6739 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6740 g_return_val_if_fail (contents != NULL, FALSE);
6742 in = g_file_read (file, cancellable, error);
6743 if (in == NULL)
6744 return FALSE;
6746 content = g_byte_array_new ();
6747 pos = 0;
6749 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6750 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6751 content->data + pos,
6752 GET_CONTENT_BLOCK_SIZE,
6753 cancellable, error)) > 0)
6755 pos += res;
6756 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6759 if (etag_out)
6761 *etag_out = NULL;
6763 info = g_file_input_stream_query_info (in,
6764 G_FILE_ATTRIBUTE_ETAG_VALUE,
6765 cancellable,
6766 NULL);
6767 if (info)
6769 *etag_out = g_strdup (g_file_info_get_etag (info));
6770 g_object_unref (info);
6774 /* Ignore errors on close */
6775 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6776 g_object_unref (in);
6778 if (res < 0)
6780 /* error is set already */
6781 g_byte_array_free (content, TRUE);
6782 return FALSE;
6785 if (length)
6786 *length = pos;
6788 /* Zero terminate (we got an extra byte allocated for this */
6789 content->data[pos] = 0;
6791 *contents = (char *)g_byte_array_free (content, FALSE);
6793 return TRUE;
6796 typedef struct {
6797 GTask *task;
6798 GFileReadMoreCallback read_more_callback;
6799 GByteArray *content;
6800 gsize pos;
6801 char *etag;
6802 } LoadContentsData;
6805 static void
6806 load_contents_data_free (LoadContentsData *data)
6808 if (data->content)
6809 g_byte_array_free (data->content, TRUE);
6810 g_free (data->etag);
6811 g_free (data);
6814 static void
6815 load_contents_close_callback (GObject *obj,
6816 GAsyncResult *close_res,
6817 gpointer user_data)
6819 GInputStream *stream = G_INPUT_STREAM (obj);
6820 LoadContentsData *data = user_data;
6822 /* Ignore errors here, we're only reading anyway */
6823 g_input_stream_close_finish (stream, close_res, NULL);
6824 g_object_unref (stream);
6826 g_task_return_boolean (data->task, TRUE);
6827 g_object_unref (data->task);
6830 static void
6831 load_contents_fstat_callback (GObject *obj,
6832 GAsyncResult *stat_res,
6833 gpointer user_data)
6835 GInputStream *stream = G_INPUT_STREAM (obj);
6836 LoadContentsData *data = user_data;
6837 GFileInfo *info;
6839 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6840 stat_res, NULL);
6841 if (info)
6843 data->etag = g_strdup (g_file_info_get_etag (info));
6844 g_object_unref (info);
6847 g_input_stream_close_async (stream, 0,
6848 g_task_get_cancellable (data->task),
6849 load_contents_close_callback, data);
6852 static void
6853 load_contents_read_callback (GObject *obj,
6854 GAsyncResult *read_res,
6855 gpointer user_data)
6857 GInputStream *stream = G_INPUT_STREAM (obj);
6858 LoadContentsData *data = user_data;
6859 GError *error = NULL;
6860 gssize read_size;
6862 read_size = g_input_stream_read_finish (stream, read_res, &error);
6864 if (read_size < 0)
6866 g_task_return_error (data->task, error);
6867 g_object_unref (data->task);
6869 /* Close the file ignoring any error */
6870 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6871 g_object_unref (stream);
6873 else if (read_size == 0)
6875 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6876 G_FILE_ATTRIBUTE_ETAG_VALUE,
6878 g_task_get_cancellable (data->task),
6879 load_contents_fstat_callback,
6880 data);
6882 else if (read_size > 0)
6884 data->pos += read_size;
6886 g_byte_array_set_size (data->content,
6887 data->pos + GET_CONTENT_BLOCK_SIZE);
6890 if (data->read_more_callback &&
6891 !data->read_more_callback ((char *)data->content->data, data->pos,
6892 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6893 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6894 G_FILE_ATTRIBUTE_ETAG_VALUE,
6896 g_task_get_cancellable (data->task),
6897 load_contents_fstat_callback,
6898 data);
6899 else
6900 g_input_stream_read_async (stream,
6901 data->content->data + data->pos,
6902 GET_CONTENT_BLOCK_SIZE,
6904 g_task_get_cancellable (data->task),
6905 load_contents_read_callback,
6906 data);
6910 static void
6911 load_contents_open_callback (GObject *obj,
6912 GAsyncResult *open_res,
6913 gpointer user_data)
6915 GFile *file = G_FILE (obj);
6916 GFileInputStream *stream;
6917 LoadContentsData *data = user_data;
6918 GError *error = NULL;
6920 stream = g_file_read_finish (file, open_res, &error);
6922 if (stream)
6924 g_byte_array_set_size (data->content,
6925 data->pos + GET_CONTENT_BLOCK_SIZE);
6926 g_input_stream_read_async (G_INPUT_STREAM (stream),
6927 data->content->data + data->pos,
6928 GET_CONTENT_BLOCK_SIZE,
6930 g_task_get_cancellable (data->task),
6931 load_contents_read_callback,
6932 data);
6934 else
6936 g_task_return_error (data->task, error);
6937 g_object_unref (data->task);
6942 * g_file_load_partial_contents_async: (skip)
6943 * @file: input #GFile
6944 * @cancellable: optional #GCancellable object, %NULL to ignore
6945 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6946 * and to specify whether further data should be read
6947 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6948 * @user_data: the data to pass to the callback functions
6950 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6951 * be used to stop reading from the file when appropriate, else this
6952 * function will behave exactly as g_file_load_contents_async(). This
6953 * operation can be finished by g_file_load_partial_contents_finish().
6955 * Users of this function should be aware that @user_data is passed to
6956 * both the @read_more_callback and the @callback.
6958 * If @cancellable is not %NULL, then the operation can be cancelled by
6959 * triggering the cancellable object from another thread. If the operation
6960 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6962 void
6963 g_file_load_partial_contents_async (GFile *file,
6964 GCancellable *cancellable,
6965 GFileReadMoreCallback read_more_callback,
6966 GAsyncReadyCallback callback,
6967 gpointer user_data)
6969 LoadContentsData *data;
6971 g_return_if_fail (G_IS_FILE (file));
6973 data = g_new0 (LoadContentsData, 1);
6974 data->read_more_callback = read_more_callback;
6975 data->content = g_byte_array_new ();
6977 data->task = g_task_new (file, cancellable, callback, user_data);
6978 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6980 g_file_read_async (file,
6982 g_task_get_cancellable (data->task),
6983 load_contents_open_callback,
6984 data);
6988 * g_file_load_partial_contents_finish:
6989 * @file: input #GFile
6990 * @res: a #GAsyncResult
6991 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6992 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6993 * or %NULL if the length is not needed
6994 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6995 * or %NULL if the entity tag is not needed
6996 * @error: a #GError, or %NULL
6998 * Finishes an asynchronous partial load operation that was started
6999 * with g_file_load_partial_contents_async(). The data is always
7000 * zero-terminated, but this is not included in the resultant @length.
7001 * The returned @content should be freed with g_free() when no longer
7002 * needed.
7004 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7005 * present, it will be set appropriately.
7007 gboolean
7008 g_file_load_partial_contents_finish (GFile *file,
7009 GAsyncResult *res,
7010 char **contents,
7011 gsize *length,
7012 char **etag_out,
7013 GError **error)
7015 GTask *task;
7016 LoadContentsData *data;
7018 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7019 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7020 g_return_val_if_fail (contents != NULL, FALSE);
7022 task = G_TASK (res);
7024 if (!g_task_propagate_boolean (task, error))
7026 if (length)
7027 *length = 0;
7028 return FALSE;
7031 data = g_task_get_task_data (task);
7033 if (length)
7034 *length = data->pos;
7036 if (etag_out)
7038 *etag_out = data->etag;
7039 data->etag = NULL;
7042 /* Zero terminate */
7043 g_byte_array_set_size (data->content, data->pos + 1);
7044 data->content->data[data->pos] = 0;
7046 *contents = (char *)g_byte_array_free (data->content, FALSE);
7047 data->content = NULL;
7049 return TRUE;
7053 * g_file_load_contents_async:
7054 * @file: input #GFile
7055 * @cancellable: optional #GCancellable object, %NULL to ignore
7056 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7057 * @user_data: the data to pass to callback function
7059 * Starts an asynchronous load of the @file's contents.
7061 * For more details, see g_file_load_contents() which is
7062 * the synchronous version of this call.
7064 * When the load operation has completed, @callback will be called
7065 * with @user data. To finish the operation, call
7066 * g_file_load_contents_finish() with the #GAsyncResult returned by
7067 * the @callback.
7069 * If @cancellable is not %NULL, then the operation can be cancelled by
7070 * triggering the cancellable object from another thread. If the operation
7071 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7073 void
7074 g_file_load_contents_async (GFile *file,
7075 GCancellable *cancellable,
7076 GAsyncReadyCallback callback,
7077 gpointer user_data)
7079 g_file_load_partial_contents_async (file,
7080 cancellable,
7081 NULL,
7082 callback, user_data);
7086 * g_file_load_contents_finish:
7087 * @file: input #GFile
7088 * @res: a #GAsyncResult
7089 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7090 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7091 * or %NULL if the length is not needed
7092 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7093 * or %NULL if the entity tag is not needed
7094 * @error: a #GError, or %NULL
7096 * Finishes an asynchronous load of the @file's contents.
7097 * The contents are placed in @contents, and @length is set to the
7098 * size of the @contents string. The @content should be freed with
7099 * g_free() when no longer needed. If @etag_out is present, it will be
7100 * set to the new entity tag for the @file.
7102 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7103 * present, it will be set appropriately.
7105 gboolean
7106 g_file_load_contents_finish (GFile *file,
7107 GAsyncResult *res,
7108 char **contents,
7109 gsize *length,
7110 char **etag_out,
7111 GError **error)
7113 return g_file_load_partial_contents_finish (file,
7114 res,
7115 contents,
7116 length,
7117 etag_out,
7118 error);
7122 * g_file_replace_contents:
7123 * @file: input #GFile
7124 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7125 * @length: the length of @contents in bytes
7126 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7127 * or %NULL
7128 * @make_backup: %TRUE if a backup should be created
7129 * @flags: a set of #GFileCreateFlags
7130 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7131 * for the document. This should be freed with g_free() when no longer
7132 * needed, or %NULL
7133 * @cancellable: optional #GCancellable object, %NULL to ignore
7134 * @error: a #GError, or %NULL
7136 * Replaces the contents of @file with @contents of @length bytes.
7138 * If @etag is specified (not %NULL), any existing file must have that etag,
7139 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7141 * If @make_backup is %TRUE, this function will attempt to make a backup
7142 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7143 * file contents in the safest way possible. For example, atomic renames are
7144 * used when replacing local files’ contents.
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 * The returned @new_etag can be used to verify that the file hasn't
7151 * changed the next time it is saved over.
7153 * Returns: %TRUE if successful. If an error has occurred, this function
7154 * will return %FALSE and set @error appropriately if present.
7156 gboolean
7157 g_file_replace_contents (GFile *file,
7158 const char *contents,
7159 gsize length,
7160 const char *etag,
7161 gboolean make_backup,
7162 GFileCreateFlags flags,
7163 char **new_etag,
7164 GCancellable *cancellable,
7165 GError **error)
7167 GFileOutputStream *out;
7168 gsize pos, remainder;
7169 gssize res;
7170 gboolean ret;
7172 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7173 g_return_val_if_fail (contents != NULL, FALSE);
7175 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7176 if (out == NULL)
7177 return FALSE;
7179 pos = 0;
7180 remainder = length;
7181 while (remainder > 0 &&
7182 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7183 contents + pos,
7184 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7185 cancellable,
7186 error)) > 0)
7188 pos += res;
7189 remainder -= res;
7192 if (remainder > 0 && res < 0)
7194 /* Ignore errors on close */
7195 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7196 g_object_unref (out);
7198 /* error is set already */
7199 return FALSE;
7202 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7204 if (new_etag)
7205 *new_etag = g_file_output_stream_get_etag (out);
7207 g_object_unref (out);
7209 return ret;
7212 typedef struct {
7213 GTask *task;
7214 GBytes *content;
7215 gsize pos;
7216 char *etag;
7217 gboolean failed;
7218 } ReplaceContentsData;
7220 static void
7221 replace_contents_data_free (ReplaceContentsData *data)
7223 g_bytes_unref (data->content);
7224 g_free (data->etag);
7225 g_free (data);
7228 static void
7229 replace_contents_close_callback (GObject *obj,
7230 GAsyncResult *close_res,
7231 gpointer user_data)
7233 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7234 ReplaceContentsData *data = user_data;
7236 /* Ignore errors here, we're only reading anyway */
7237 g_output_stream_close_finish (stream, close_res, NULL);
7238 g_object_unref (stream);
7240 if (!data->failed)
7242 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7243 g_task_return_boolean (data->task, TRUE);
7245 g_object_unref (data->task);
7248 static void
7249 replace_contents_write_callback (GObject *obj,
7250 GAsyncResult *read_res,
7251 gpointer user_data)
7253 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7254 ReplaceContentsData *data = user_data;
7255 GError *error = NULL;
7256 gssize write_size;
7258 write_size = g_output_stream_write_finish (stream, read_res, &error);
7260 if (write_size <= 0)
7262 /* Error or EOF, close the file */
7263 if (write_size < 0)
7265 data->failed = TRUE;
7266 g_task_return_error (data->task, error);
7268 g_output_stream_close_async (stream, 0,
7269 g_task_get_cancellable (data->task),
7270 replace_contents_close_callback, data);
7272 else if (write_size > 0)
7274 const gchar *content;
7275 gsize length;
7277 content = g_bytes_get_data (data->content, &length);
7278 data->pos += write_size;
7280 if (data->pos >= length)
7281 g_output_stream_close_async (stream, 0,
7282 g_task_get_cancellable (data->task),
7283 replace_contents_close_callback, data);
7284 else
7285 g_output_stream_write_async (stream,
7286 content + data->pos,
7287 length - data->pos,
7289 g_task_get_cancellable (data->task),
7290 replace_contents_write_callback,
7291 data);
7295 static void
7296 replace_contents_open_callback (GObject *obj,
7297 GAsyncResult *open_res,
7298 gpointer user_data)
7300 GFile *file = G_FILE (obj);
7301 GFileOutputStream *stream;
7302 ReplaceContentsData *data = user_data;
7303 GError *error = NULL;
7305 stream = g_file_replace_finish (file, open_res, &error);
7307 if (stream)
7309 const gchar *content;
7310 gsize length;
7312 content = g_bytes_get_data (data->content, &length);
7313 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7314 content + data->pos,
7315 length - data->pos,
7317 g_task_get_cancellable (data->task),
7318 replace_contents_write_callback,
7319 data);
7321 else
7323 g_task_return_error (data->task, error);
7324 g_object_unref (data->task);
7329 * g_file_replace_contents_async:
7330 * @file: input #GFile
7331 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7332 * @length: the length of @contents in bytes
7333 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7334 * @make_backup: %TRUE if a backup should be created
7335 * @flags: a set of #GFileCreateFlags
7336 * @cancellable: optional #GCancellable object, %NULL to ignore
7337 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7338 * @user_data: the data to pass to callback function
7340 * Starts an asynchronous replacement of @file with the given
7341 * @contents of @length bytes. @etag will replace the document's
7342 * current entity tag.
7344 * When this operation has completed, @callback will be called with
7345 * @user_user data, and the operation can be finalized with
7346 * g_file_replace_contents_finish().
7348 * If @cancellable is not %NULL, then the operation can be cancelled by
7349 * triggering the cancellable object from another thread. If the operation
7350 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7352 * If @make_backup is %TRUE, this function will attempt to
7353 * make a backup of @file.
7355 * Note that no copy of @content will be made, so it must stay valid
7356 * until @callback is called. See g_file_replace_contents_bytes_async()
7357 * for a #GBytes version that will automatically hold a reference to the
7358 * contents (without copying) for the duration of the call.
7360 void
7361 g_file_replace_contents_async (GFile *file,
7362 const char *contents,
7363 gsize length,
7364 const char *etag,
7365 gboolean make_backup,
7366 GFileCreateFlags flags,
7367 GCancellable *cancellable,
7368 GAsyncReadyCallback callback,
7369 gpointer user_data)
7371 GBytes *bytes;
7373 bytes = g_bytes_new_static (contents, length);
7374 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7375 cancellable, callback, user_data);
7376 g_bytes_unref (bytes);
7380 * g_file_replace_contents_bytes_async:
7381 * @file: input #GFile
7382 * @contents: a #GBytes
7383 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7384 * @make_backup: %TRUE if a backup should be created
7385 * @flags: a set of #GFileCreateFlags
7386 * @cancellable: optional #GCancellable object, %NULL to ignore
7387 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7388 * @user_data: the data to pass to callback function
7390 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7391 * This function will keep a ref on @contents until the operation is done.
7392 * Unlike g_file_replace_contents_async() this allows forgetting about the
7393 * content without waiting for the callback.
7395 * When this operation has completed, @callback will be called with
7396 * @user_user data, and the operation can be finalized with
7397 * g_file_replace_contents_finish().
7399 * Since: 2.40
7401 void
7402 g_file_replace_contents_bytes_async (GFile *file,
7403 GBytes *contents,
7404 const char *etag,
7405 gboolean make_backup,
7406 GFileCreateFlags flags,
7407 GCancellable *cancellable,
7408 GAsyncReadyCallback callback,
7409 gpointer user_data)
7411 ReplaceContentsData *data;
7413 g_return_if_fail (G_IS_FILE (file));
7414 g_return_if_fail (contents != NULL);
7416 data = g_new0 (ReplaceContentsData, 1);
7418 data->content = g_bytes_ref (contents);
7420 data->task = g_task_new (file, cancellable, callback, user_data);
7421 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7423 g_file_replace_async (file,
7424 etag,
7425 make_backup,
7426 flags,
7428 g_task_get_cancellable (data->task),
7429 replace_contents_open_callback,
7430 data);
7434 * g_file_replace_contents_finish:
7435 * @file: input #GFile
7436 * @res: a #GAsyncResult
7437 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7438 * for the document. This should be freed with g_free() when it is no
7439 * longer needed, or %NULL
7440 * @error: a #GError, or %NULL
7442 * Finishes an asynchronous replace of the given @file. See
7443 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7444 * tag for the document, if present.
7446 * Returns: %TRUE on success, %FALSE on failure.
7448 gboolean
7449 g_file_replace_contents_finish (GFile *file,
7450 GAsyncResult *res,
7451 char **new_etag,
7452 GError **error)
7454 GTask *task;
7455 ReplaceContentsData *data;
7457 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7458 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7460 task = G_TASK (res);
7462 if (!g_task_propagate_boolean (task, error))
7463 return FALSE;
7465 data = g_task_get_task_data (task);
7467 if (new_etag)
7469 *new_etag = data->etag;
7470 data->etag = NULL; /* Take ownership */
7473 return TRUE;
7476 gboolean
7477 g_file_real_measure_disk_usage (GFile *file,
7478 GFileMeasureFlags flags,
7479 GCancellable *cancellable,
7480 GFileMeasureProgressCallback progress_callback,
7481 gpointer progress_data,
7482 guint64 *disk_usage,
7483 guint64 *num_dirs,
7484 guint64 *num_files,
7485 GError **error)
7487 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7488 "Operation not supported for the current backend.");
7489 return FALSE;
7492 typedef struct
7494 GFileMeasureFlags flags;
7495 GFileMeasureProgressCallback progress_callback;
7496 gpointer progress_data;
7497 } MeasureTaskData;
7499 typedef struct
7501 guint64 disk_usage;
7502 guint64 num_dirs;
7503 guint64 num_files;
7504 } MeasureResult;
7506 typedef struct
7508 GFileMeasureProgressCallback callback;
7509 gpointer user_data;
7510 gboolean reporting;
7511 guint64 current_size;
7512 guint64 num_dirs;
7513 guint64 num_files;
7514 } MeasureProgress;
7516 static gboolean
7517 measure_disk_usage_invoke_progress (gpointer user_data)
7519 MeasureProgress *progress = user_data;
7521 (* progress->callback) (progress->reporting,
7522 progress->current_size, progress->num_dirs, progress->num_files,
7523 progress->user_data);
7525 return FALSE;
7528 static void
7529 measure_disk_usage_progress (gboolean reporting,
7530 guint64 current_size,
7531 guint64 num_dirs,
7532 guint64 num_files,
7533 gpointer user_data)
7535 MeasureProgress progress;
7536 GTask *task = user_data;
7537 MeasureTaskData *data;
7539 data = g_task_get_task_data (task);
7541 progress.callback = data->progress_callback;
7542 progress.user_data = data->progress_data;
7543 progress.reporting = reporting;
7544 progress.current_size = current_size;
7545 progress.num_dirs = num_dirs;
7546 progress.num_files = num_files;
7548 g_main_context_invoke_full (g_task_get_context (task),
7549 g_task_get_priority (task),
7550 measure_disk_usage_invoke_progress,
7551 g_memdup (&progress, sizeof progress),
7552 g_free);
7555 static void
7556 measure_disk_usage_thread (GTask *task,
7557 gpointer source_object,
7558 gpointer task_data,
7559 GCancellable *cancellable)
7561 MeasureTaskData *data = task_data;
7562 GError *error = NULL;
7563 MeasureResult result = { 0, };
7565 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7566 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7567 &result.disk_usage, &result.num_dirs, &result.num_files,
7568 &error))
7569 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7570 else
7571 g_task_return_error (task, error);
7574 static void
7575 g_file_real_measure_disk_usage_async (GFile *file,
7576 GFileMeasureFlags flags,
7577 gint io_priority,
7578 GCancellable *cancellable,
7579 GFileMeasureProgressCallback progress_callback,
7580 gpointer progress_data,
7581 GAsyncReadyCallback callback,
7582 gpointer user_data)
7584 MeasureTaskData data;
7585 GTask *task;
7587 data.flags = flags;
7588 data.progress_callback = progress_callback;
7589 data.progress_data = progress_data;
7591 task = g_task_new (file, cancellable, callback, user_data);
7592 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7593 g_task_set_priority (task, io_priority);
7595 g_task_run_in_thread (task, measure_disk_usage_thread);
7596 g_object_unref (task);
7599 static gboolean
7600 g_file_real_measure_disk_usage_finish (GFile *file,
7601 GAsyncResult *result,
7602 guint64 *disk_usage,
7603 guint64 *num_dirs,
7604 guint64 *num_files,
7605 GError **error)
7607 MeasureResult *measure_result;
7609 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7611 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7613 if (measure_result == NULL)
7614 return FALSE;
7616 if (disk_usage)
7617 *disk_usage = measure_result->disk_usage;
7619 if (num_dirs)
7620 *num_dirs = measure_result->num_dirs;
7622 if (num_files)
7623 *num_files = measure_result->num_files;
7625 g_free (measure_result);
7627 return TRUE;
7631 * g_file_measure_disk_usage:
7632 * @file: a #GFile
7633 * @flags: #GFileMeasureFlags
7634 * @cancellable: (allow-none): optional #GCancellable
7635 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7636 * @progress_data: user_data for @progress_callback
7637 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7638 * @num_dirs: (allow-none) (out): the number of directories encountered
7639 * @num_files: (allow-none) (out): the number of non-directories encountered
7640 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7642 * Recursively measures the disk usage of @file.
7644 * This is essentially an analog of the 'du' command, but it also
7645 * reports the number of directories and non-directory files encountered
7646 * (including things like symbolic links).
7648 * By default, errors are only reported against the toplevel file
7649 * itself. Errors found while recursing are silently ignored, unless
7650 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7652 * The returned size, @disk_usage, is in bytes and should be formatted
7653 * with g_format_size() in order to get something reasonable for showing
7654 * in a user interface.
7656 * @progress_callback and @progress_data can be given to request
7657 * periodic progress updates while scanning. See the documentation for
7658 * #GFileMeasureProgressCallback for information about when and how the
7659 * callback will be invoked.
7661 * Returns: %TRUE if successful, with the out parameters set.
7662 * %FALSE otherwise, with @error set.
7664 * Since: 2.38
7666 gboolean
7667 g_file_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_return_val_if_fail (G_IS_FILE (file), FALSE);
7678 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7679 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7681 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7682 progress_callback, progress_data,
7683 disk_usage, num_dirs, num_files,
7684 error);
7688 * g_file_measure_disk_usage_async:
7689 * @file: a #GFile
7690 * @flags: #GFileMeasureFlags
7691 * @io_priority: the [I/O priority][io-priority] of the request
7692 * @cancellable: (allow-none): optional #GCancellable
7693 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7694 * @progress_data: user_data for @progress_callback
7695 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7696 * @user_data: the data to pass to callback function
7698 * Recursively measures the disk usage of @file.
7700 * This is the asynchronous version of g_file_measure_disk_usage(). See
7701 * there for more information.
7703 * Since: 2.38
7705 void
7706 g_file_measure_disk_usage_async (GFile *file,
7707 GFileMeasureFlags flags,
7708 gint io_priority,
7709 GCancellable *cancellable,
7710 GFileMeasureProgressCallback progress_callback,
7711 gpointer progress_data,
7712 GAsyncReadyCallback callback,
7713 gpointer user_data)
7715 g_return_if_fail (G_IS_FILE (file));
7716 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7718 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7719 progress_callback, progress_data,
7720 callback, user_data);
7724 * g_file_measure_disk_usage_finish:
7725 * @file: a #GFile
7726 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7727 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7728 * @num_dirs: (allow-none) (out): the number of directories encountered
7729 * @num_files: (allow-none) (out): the number of non-directories encountered
7730 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7732 * Collects the results from an earlier call to
7733 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7734 * more information.
7736 * Returns: %TRUE if successful, with the out parameters set.
7737 * %FALSE otherwise, with @error set.
7739 * Since: 2.38
7741 gboolean
7742 g_file_measure_disk_usage_finish (GFile *file,
7743 GAsyncResult *result,
7744 guint64 *disk_usage,
7745 guint64 *num_dirs,
7746 guint64 *num_files,
7747 GError **error)
7749 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7750 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7752 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7756 * g_file_start_mountable:
7757 * @file: input #GFile
7758 * @flags: flags affecting the operation
7759 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7760 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7761 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7762 * @user_data: the data to pass to callback function
7764 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7765 * Using @start_operation, you can request callbacks when, for instance,
7766 * passwords are needed during authentication.
7768 * If @cancellable is not %NULL, then the operation can be cancelled by
7769 * triggering the cancellable object from another thread. If the operation
7770 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7772 * When the operation is finished, @callback will be called.
7773 * You can then call g_file_mount_mountable_finish() to get
7774 * the result of the operation.
7776 * Since: 2.22
7778 void
7779 g_file_start_mountable (GFile *file,
7780 GDriveStartFlags flags,
7781 GMountOperation *start_operation,
7782 GCancellable *cancellable,
7783 GAsyncReadyCallback callback,
7784 gpointer user_data)
7786 GFileIface *iface;
7788 g_return_if_fail (G_IS_FILE (file));
7790 iface = G_FILE_GET_IFACE (file);
7792 if (iface->start_mountable == NULL)
7794 g_task_report_new_error (file, callback, user_data,
7795 g_file_start_mountable,
7796 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7797 _("Operation not supported"));
7798 return;
7801 (* iface->start_mountable) (file,
7802 flags,
7803 start_operation,
7804 cancellable,
7805 callback,
7806 user_data);
7810 * g_file_start_mountable_finish:
7811 * @file: input #GFile
7812 * @result: a #GAsyncResult
7813 * @error: a #GError, or %NULL
7815 * Finishes a start operation. See g_file_start_mountable() for details.
7817 * Finish an asynchronous start operation that was started
7818 * with g_file_start_mountable().
7820 * Returns: %TRUE if the operation finished successfully. %FALSE
7821 * otherwise.
7823 * Since: 2.22
7825 gboolean
7826 g_file_start_mountable_finish (GFile *file,
7827 GAsyncResult *result,
7828 GError **error)
7830 GFileIface *iface;
7832 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7833 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7835 if (g_async_result_legacy_propagate_error (result, error))
7836 return FALSE;
7837 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7838 return g_task_propagate_boolean (G_TASK (result), error);
7840 iface = G_FILE_GET_IFACE (file);
7841 return (* iface->start_mountable_finish) (file, result, error);
7845 * g_file_stop_mountable:
7846 * @file: input #GFile
7847 * @flags: flags affecting the operation
7848 * @mount_operation: (allow-none): a #GMountOperation,
7849 * or %NULL to avoid user interaction.
7850 * @cancellable: (allow-none): optional #GCancellable object,
7851 * %NULL to ignore
7852 * @callback: (allow-none): a #GAsyncReadyCallback to call
7853 * when the request is satisfied, or %NULL
7854 * @user_data: the data to pass to callback function
7856 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7858 * If @cancellable is not %NULL, then the operation can be cancelled by
7859 * triggering the cancellable object from another thread. If the operation
7860 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7862 * When the operation is finished, @callback will be called.
7863 * You can then call g_file_stop_mountable_finish() to get
7864 * the result of the operation.
7866 * Since: 2.22
7868 void
7869 g_file_stop_mountable (GFile *file,
7870 GMountUnmountFlags flags,
7871 GMountOperation *mount_operation,
7872 GCancellable *cancellable,
7873 GAsyncReadyCallback callback,
7874 gpointer user_data)
7876 GFileIface *iface;
7878 g_return_if_fail (G_IS_FILE (file));
7880 iface = G_FILE_GET_IFACE (file);
7882 if (iface->stop_mountable == NULL)
7884 g_task_report_new_error (file, callback, user_data,
7885 g_file_stop_mountable,
7886 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7887 _("Operation not supported"));
7888 return;
7891 (* iface->stop_mountable) (file,
7892 flags,
7893 mount_operation,
7894 cancellable,
7895 callback,
7896 user_data);
7900 * g_file_stop_mountable_finish:
7901 * @file: input #GFile
7902 * @result: a #GAsyncResult
7903 * @error: a #GError, or %NULL
7905 * Finishes an stop operation, see g_file_stop_mountable() for details.
7907 * Finish an asynchronous stop operation that was started
7908 * with g_file_stop_mountable().
7910 * Returns: %TRUE if the operation finished successfully.
7911 * %FALSE otherwise.
7913 * Since: 2.22
7915 gboolean
7916 g_file_stop_mountable_finish (GFile *file,
7917 GAsyncResult *result,
7918 GError **error)
7920 GFileIface *iface;
7922 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7923 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7925 if (g_async_result_legacy_propagate_error (result, error))
7926 return FALSE;
7927 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7928 return g_task_propagate_boolean (G_TASK (result), error);
7930 iface = G_FILE_GET_IFACE (file);
7931 return (* iface->stop_mountable_finish) (file, result, error);
7935 * g_file_poll_mountable:
7936 * @file: input #GFile
7937 * @cancellable: optional #GCancellable object, %NULL to ignore
7938 * @callback: (allow-none): a #GAsyncReadyCallback to call
7939 * when the request is satisfied, or %NULL
7940 * @user_data: the data to pass to callback function
7942 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7944 * If @cancellable is not %NULL, then the operation can be cancelled by
7945 * triggering the cancellable object from another thread. If the operation
7946 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7948 * When the operation is finished, @callback will be called.
7949 * You can then call g_file_mount_mountable_finish() to get
7950 * the result of the operation.
7952 * Since: 2.22
7954 void
7955 g_file_poll_mountable (GFile *file,
7956 GCancellable *cancellable,
7957 GAsyncReadyCallback callback,
7958 gpointer user_data)
7960 GFileIface *iface;
7962 g_return_if_fail (G_IS_FILE (file));
7964 iface = G_FILE_GET_IFACE (file);
7966 if (iface->poll_mountable == NULL)
7968 g_task_report_new_error (file, callback, user_data,
7969 g_file_poll_mountable,
7970 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7971 _("Operation not supported"));
7972 return;
7975 (* iface->poll_mountable) (file,
7976 cancellable,
7977 callback,
7978 user_data);
7982 * g_file_poll_mountable_finish:
7983 * @file: input #GFile
7984 * @result: a #GAsyncResult
7985 * @error: a #GError, or %NULL
7987 * Finishes a poll operation. See g_file_poll_mountable() for details.
7989 * Finish an asynchronous poll operation that was polled
7990 * with g_file_poll_mountable().
7992 * Returns: %TRUE if the operation finished successfully. %FALSE
7993 * otherwise.
7995 * Since: 2.22
7997 gboolean
7998 g_file_poll_mountable_finish (GFile *file,
7999 GAsyncResult *result,
8000 GError **error)
8002 GFileIface *iface;
8004 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8005 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8007 if (g_async_result_legacy_propagate_error (result, error))
8008 return FALSE;
8009 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8010 return g_task_propagate_boolean (G_TASK (result), error);
8012 iface = G_FILE_GET_IFACE (file);
8013 return (* iface->poll_mountable_finish) (file, result, error);
8017 * g_file_supports_thread_contexts:
8018 * @file: a #GFile
8020 * Checks if @file supports
8021 * [thread-default contexts][g-main-context-push-thread-default-context].
8022 * If this returns %FALSE, you cannot perform asynchronous operations on
8023 * @file in a thread that has a thread-default context.
8025 * Returns: Whether or not @file supports thread-default contexts.
8027 * Since: 2.22
8029 gboolean
8030 g_file_supports_thread_contexts (GFile *file)
8032 GFileIface *iface;
8034 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8036 iface = G_FILE_GET_IFACE (file);
8037 return iface->supports_thread_contexts;