Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 : :
3 : : /* GIO - GLib Input, Output and Streaming Library
4 : : *
5 : : * Copyright (C) 2006-2007 Red Hat, Inc.
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General
20 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 : : *
22 : : * Author: Alexander Larsson <alexl@redhat.com>
23 : : */
24 : :
25 : : #include "config.h"
26 : :
27 : : #ifdef __linux__
28 : : #include <sys/ioctl.h>
29 : : #include <errno.h>
30 : : /* See linux.git/fs/btrfs/ioctl.h */
31 : : #define BTRFS_IOCTL_MAGIC 0x94
32 : : #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
33 : : #endif
34 : :
35 : : #ifdef HAVE_SPLICE
36 : : #include <sys/stat.h>
37 : : #include <unistd.h>
38 : : #include <fcntl.h>
39 : : #include <errno.h>
40 : :
41 : : /*
42 : : * We duplicate the following Linux kernel header defines here so we can still
43 : : * run at full speed on modern kernels in cases where an old toolchain was used
44 : : * to build GLib. This is often done deliberately to allow shipping binaries
45 : : * that need to run on a wide range of systems.
46 : : */
47 : : #ifndef F_SETPIPE_SZ
48 : : #define F_SETPIPE_SZ 1031
49 : : #endif
50 : : #ifndef F_GETPIPE_SZ
51 : : #define F_GETPIPE_SZ 1032
52 : : #endif
53 : :
54 : : #endif
55 : :
56 : : #include <string.h>
57 : : #include <sys/types.h>
58 : :
59 : : #include "gfile.h"
60 : : #include "glib/gstdio.h"
61 : : #ifdef G_OS_UNIX
62 : : #include "glib-unix.h"
63 : : #endif
64 : : #include "gvfs.h"
65 : : #include "gtask.h"
66 : : #include "gfileattribute-priv.h"
67 : : #include "gfiledescriptorbased.h"
68 : : #include "gpollfilemonitor.h"
69 : : #include "gappinfo.h"
70 : : #include "gfileinputstream.h"
71 : : #include "gfileoutputstream.h"
72 : : #include "glocalfileoutputstream.h"
73 : : #include "glocalfileiostream.h"
74 : : #include "glocalfile.h"
75 : : #include "gcancellable.h"
76 : : #include "gasyncresult.h"
77 : : #include "gioerror.h"
78 : : #include "glibintl.h"
79 : :
80 : : /* Linux defines loff_t as a way to simplify the offset types for calls like
81 : : * splice() and copy_file_range(). BSD has copy_file_range() but doesn’t define
82 : : * loff_t. Abstract that. */
83 : : #ifndef HAVE_LOFF_T
84 : : typedef off_t loff_t;
85 : : #endif
86 : :
87 : : /**
88 : : * GFile:
89 : : *
90 : : * `GFile` is a high level abstraction for manipulating files on a
91 : : * virtual file system. `GFile`s are lightweight, immutable objects
92 : : * that do no I/O upon creation. It is necessary to understand that
93 : : * `GFile` objects do not represent files, merely an identifier for a
94 : : * file. All file content I/O is implemented as streaming operations
95 : : * (see [class@Gio.InputStream] and [class@Gio.OutputStream]).
96 : : *
97 : : * To construct a `GFile`, you can use:
98 : : *
99 : : * - [func@Gio.File.new_for_path] if you have a path.
100 : : * - [func@Gio.File.new_for_uri] if you have a URI.
101 : : * - [func@Gio.File.new_for_commandline_arg] or
102 : : * [func@Gio.File.new_for_commandline_arg_and_cwd] for a command line
103 : : * argument.
104 : : * - [func@Gio.File.new_tmp] to create a temporary file from a template.
105 : : * - [func@Gio.File.new_tmp_async] to asynchronously create a temporary file.
106 : : * - [func@Gio.File.new_tmp_dir_async] to asynchronously create a temporary
107 : : * directory.
108 : : * - [func@Gio.File.parse_name] from a UTF-8 string gotten from
109 : : * [method@Gio.File.get_parse_name].
110 : : * - [func@Gio.File.new_build_filename] or [func@Gio.File.new_build_filenamev]
111 : : * to create a file from path elements.
112 : : *
113 : : * One way to think of a `GFile` is as an abstraction of a pathname. For
114 : : * normal files the system pathname is what is stored internally, but as
115 : : * `GFile`s are extensible it could also be something else that corresponds
116 : : * to a pathname in a userspace implementation of a filesystem.
117 : : *
118 : : * `GFile`s make up hierarchies of directories and files that correspond to
119 : : * the files on a filesystem. You can move through the file system with
120 : : * `GFile` using [method@Gio.File.get_parent] to get an identifier for the
121 : : * parent directory, [method@Gio.File.get_child] to get a child within a
122 : : * directory, and [method@Gio.File.resolve_relative_path] to resolve a relative
123 : : * path between two `GFile`s. There can be multiple hierarchies, so you may not
124 : : * end up at the same root if you repeatedly call [method@Gio.File.get_parent]
125 : : * on two different files.
126 : : *
127 : : * All `GFile`s have a basename (get with [method@Gio.File.get_basename]). These
128 : : * names are byte strings that are used to identify the file on the filesystem
129 : : * (relative to its parent directory) and there is no guarantees that they
130 : : * have any particular charset encoding or even make any sense at all. If
131 : : * you want to use filenames in a user interface you should use the display
132 : : * name that you can get by requesting the
133 : : * `G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME` attribute with
134 : : * [method@Gio.File.query_info]. This is guaranteed to be in UTF-8 and can be
135 : : * used in a user interface. But always store the real basename or the `GFile`
136 : : * to use to actually access the file, because there is no way to go from a
137 : : * display name to the actual name.
138 : : *
139 : : * Using `GFile` as an identifier has the same weaknesses as using a path
140 : : * in that there may be multiple aliases for the same file. For instance,
141 : : * hard or soft links may cause two different `GFile`s to refer to the same
142 : : * file. Other possible causes for aliases are: case insensitive filesystems,
143 : : * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
144 : : * check if two `GFile`s point to the same file you can query for the
145 : : * `G_FILE_ATTRIBUTE_ID_FILE` attribute. Note that `GFile` does some trivial
146 : : * canonicalization of pathnames passed in, so that trivial differences in
147 : : * the path string used at creation (duplicated slashes, slash at end of
148 : : * path, `.` or `..` path segments, etc) does not create different `GFile`s.
149 : : *
150 : : * Many `GFile` operations have both synchronous and asynchronous versions
151 : : * to suit your application. Asynchronous versions of synchronous functions
152 : : * simply have `_async()` appended to their function names. The asynchronous
153 : : * I/O functions call a [callback@Gio.AsyncReadyCallback] which is then used to
154 : : * finalize the operation, producing a [iface@Gio.AsyncResult] which is then
155 : : * passed to the function’s matching `_finish()` operation.
156 : : *
157 : : * It is highly recommended to use asynchronous calls when running within a
158 : : * shared main loop, such as in the main thread of an application. This avoids
159 : : * I/O operations blocking other sources on the main loop from being dispatched.
160 : : * Synchronous I/O operations should be performed from worker threads. See the
161 : : * [introduction to asynchronous programming section](overview.html#asynchronous-programming)
162 : : * for more.
163 : : *
164 : : * Some `GFile` operations almost always take a noticeable amount of time, and
165 : : * so do not have synchronous analogs. Notable cases include:
166 : : *
167 : : * - [method@Gio.File.mount_mountable] to mount a mountable file.
168 : : * - [method@Gio.File.unmount_mountable_with_operation] to unmount a mountable
169 : : * file.
170 : : * - [method@Gio.File.eject_mountable_with_operation] to eject a mountable file.
171 : : *
172 : : * ## Entity Tags
173 : : *
174 : : * One notable feature of `GFile`s are entity tags, or ‘etags’ for
175 : : * short. Entity tags are somewhat like a more abstract version of the
176 : : * traditional mtime, and can be used to quickly determine if the file
177 : : * has been modified from the version on the file system. See the
178 : : * description of HTTP ETags in
179 : : * [RFC9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-etag).
180 : : * `GFile` Entity Tags are a very similar concept.
181 : : */
182 : :
183 : : static void g_file_real_query_info_async (GFile *file,
184 : : const char *attributes,
185 : : GFileQueryInfoFlags flags,
186 : : int io_priority,
187 : : GCancellable *cancellable,
188 : : GAsyncReadyCallback callback,
189 : : gpointer user_data);
190 : : static GFileInfo * g_file_real_query_info_finish (GFile *file,
191 : : GAsyncResult *res,
192 : : GError **error);
193 : : static void g_file_real_query_filesystem_info_async (GFile *file,
194 : : const char *attributes,
195 : : int io_priority,
196 : : GCancellable *cancellable,
197 : : GAsyncReadyCallback callback,
198 : : gpointer user_data);
199 : : static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
200 : : GAsyncResult *res,
201 : : GError **error);
202 : : static void g_file_real_enumerate_children_async (GFile *file,
203 : : const char *attributes,
204 : : GFileQueryInfoFlags flags,
205 : : int io_priority,
206 : : GCancellable *cancellable,
207 : : GAsyncReadyCallback callback,
208 : : gpointer user_data);
209 : : static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
210 : : GAsyncResult *res,
211 : : GError **error);
212 : : static void g_file_real_read_async (GFile *file,
213 : : int io_priority,
214 : : GCancellable *cancellable,
215 : : GAsyncReadyCallback callback,
216 : : gpointer user_data);
217 : : static GFileInputStream * g_file_real_read_finish (GFile *file,
218 : : GAsyncResult *res,
219 : : GError **error);
220 : : static void g_file_real_append_to_async (GFile *file,
221 : : GFileCreateFlags flags,
222 : : int io_priority,
223 : : GCancellable *cancellable,
224 : : GAsyncReadyCallback callback,
225 : : gpointer user_data);
226 : : static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
227 : : GAsyncResult *res,
228 : : GError **error);
229 : : static void g_file_real_create_async (GFile *file,
230 : : GFileCreateFlags flags,
231 : : int io_priority,
232 : : GCancellable *cancellable,
233 : : GAsyncReadyCallback callback,
234 : : gpointer user_data);
235 : : static GFileOutputStream *g_file_real_create_finish (GFile *file,
236 : : GAsyncResult *res,
237 : : GError **error);
238 : : static void g_file_real_replace_async (GFile *file,
239 : : const char *etag,
240 : : gboolean make_backup,
241 : : GFileCreateFlags flags,
242 : : int io_priority,
243 : : GCancellable *cancellable,
244 : : GAsyncReadyCallback callback,
245 : : gpointer user_data);
246 : : static GFileOutputStream *g_file_real_replace_finish (GFile *file,
247 : : GAsyncResult *res,
248 : : GError **error);
249 : : static void g_file_real_delete_async (GFile *file,
250 : : int io_priority,
251 : : GCancellable *cancellable,
252 : : GAsyncReadyCallback callback,
253 : : gpointer user_data);
254 : : static gboolean g_file_real_delete_finish (GFile *file,
255 : : GAsyncResult *res,
256 : : GError **error);
257 : : static void g_file_real_trash_async (GFile *file,
258 : : int io_priority,
259 : : GCancellable *cancellable,
260 : : GAsyncReadyCallback callback,
261 : : gpointer user_data);
262 : : static gboolean g_file_real_trash_finish (GFile *file,
263 : : GAsyncResult *res,
264 : : GError **error);
265 : : static void g_file_real_move_async (GFile *source,
266 : : GFile *destination,
267 : : GFileCopyFlags flags,
268 : : int io_priority,
269 : : GCancellable *cancellable,
270 : : GFileProgressCallback progress_callback,
271 : : gpointer progress_callback_data,
272 : : GAsyncReadyCallback callback,
273 : : gpointer user_data);
274 : : static gboolean g_file_real_move_finish (GFile *file,
275 : : GAsyncResult *result,
276 : : GError **error);
277 : : static void g_file_real_make_directory_async (GFile *file,
278 : : int io_priority,
279 : : GCancellable *cancellable,
280 : : GAsyncReadyCallback callback,
281 : : gpointer user_data);
282 : : static gboolean g_file_real_make_directory_finish (GFile *file,
283 : : GAsyncResult *res,
284 : : GError **error);
285 : : static void g_file_real_make_symbolic_link_async (GFile *file,
286 : : const char *symlink_value,
287 : : int io_priority,
288 : : GCancellable *cancellable,
289 : : GAsyncReadyCallback callback,
290 : : gpointer user_data);
291 : : static gboolean g_file_real_make_symbolic_link_finish (GFile *file,
292 : : GAsyncResult *result,
293 : : GError **error);
294 : : static void g_file_real_open_readwrite_async (GFile *file,
295 : : int io_priority,
296 : : GCancellable *cancellable,
297 : : GAsyncReadyCallback callback,
298 : : gpointer user_data);
299 : : static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
300 : : GAsyncResult *res,
301 : : GError **error);
302 : : static void g_file_real_create_readwrite_async (GFile *file,
303 : : GFileCreateFlags flags,
304 : : int io_priority,
305 : : GCancellable *cancellable,
306 : : GAsyncReadyCallback callback,
307 : : gpointer user_data);
308 : : static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
309 : : GAsyncResult *res,
310 : : GError **error);
311 : : static void g_file_real_replace_readwrite_async (GFile *file,
312 : : const char *etag,
313 : : gboolean make_backup,
314 : : GFileCreateFlags flags,
315 : : int io_priority,
316 : : GCancellable *cancellable,
317 : : GAsyncReadyCallback callback,
318 : : gpointer user_data);
319 : : static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
320 : : GAsyncResult *res,
321 : : GError **error);
322 : : static gboolean g_file_real_set_attributes_from_info (GFile *file,
323 : : GFileInfo *info,
324 : : GFileQueryInfoFlags flags,
325 : : GCancellable *cancellable,
326 : : GError **error);
327 : : static void g_file_real_set_display_name_async (GFile *file,
328 : : const char *display_name,
329 : : int io_priority,
330 : : GCancellable *cancellable,
331 : : GAsyncReadyCallback callback,
332 : : gpointer user_data);
333 : : static GFile * g_file_real_set_display_name_finish (GFile *file,
334 : : GAsyncResult *res,
335 : : GError **error);
336 : : static void g_file_real_set_attributes_async (GFile *file,
337 : : GFileInfo *info,
338 : : GFileQueryInfoFlags flags,
339 : : int io_priority,
340 : : GCancellable *cancellable,
341 : : GAsyncReadyCallback callback,
342 : : gpointer user_data);
343 : : static gboolean g_file_real_set_attributes_finish (GFile *file,
344 : : GAsyncResult *res,
345 : : GFileInfo **info,
346 : : GError **error);
347 : : static void g_file_real_find_enclosing_mount_async (GFile *file,
348 : : int io_priority,
349 : : GCancellable *cancellable,
350 : : GAsyncReadyCallback callback,
351 : : gpointer user_data);
352 : : static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
353 : : GAsyncResult *res,
354 : : GError **error);
355 : : static void g_file_real_copy_async (GFile *source,
356 : : GFile *destination,
357 : : GFileCopyFlags flags,
358 : : int io_priority,
359 : : GCancellable *cancellable,
360 : : GFileProgressCallback progress_callback,
361 : : gpointer progress_callback_data,
362 : : GAsyncReadyCallback callback,
363 : : gpointer user_data);
364 : : static gboolean g_file_real_copy_finish (GFile *file,
365 : : GAsyncResult *res,
366 : : GError **error);
367 : :
368 : : static gboolean g_file_real_measure_disk_usage (GFile *file,
369 : : GFileMeasureFlags flags,
370 : : GCancellable *cancellable,
371 : : GFileMeasureProgressCallback progress_callback,
372 : : gpointer progress_data,
373 : : guint64 *disk_usage,
374 : : guint64 *num_dirs,
375 : : guint64 *num_files,
376 : : GError **error);
377 : : static void g_file_real_measure_disk_usage_async (GFile *file,
378 : : GFileMeasureFlags flags,
379 : : gint io_priority,
380 : : GCancellable *cancellable,
381 : : GFileMeasureProgressCallback progress_callback,
382 : : gpointer progress_data,
383 : : GAsyncReadyCallback callback,
384 : : gpointer user_data);
385 : : static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
386 : : GAsyncResult *result,
387 : : guint64 *disk_usage,
388 : : guint64 *num_dirs,
389 : : guint64 *num_files,
390 : : GError **error);
391 : :
392 : : typedef GFileIface GFileInterface;
393 : 17589 : G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
394 : :
395 : : static void
396 : 50 : g_file_default_init (GFileIface *iface)
397 : : {
398 : 50 : iface->enumerate_children_async = g_file_real_enumerate_children_async;
399 : 50 : iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
400 : 50 : iface->set_display_name_async = g_file_real_set_display_name_async;
401 : 50 : iface->set_display_name_finish = g_file_real_set_display_name_finish;
402 : 50 : iface->query_info_async = g_file_real_query_info_async;
403 : 50 : iface->query_info_finish = g_file_real_query_info_finish;
404 : 50 : iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
405 : 50 : iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
406 : 50 : iface->set_attributes_async = g_file_real_set_attributes_async;
407 : 50 : iface->set_attributes_finish = g_file_real_set_attributes_finish;
408 : 50 : iface->read_async = g_file_real_read_async;
409 : 50 : iface->read_finish = g_file_real_read_finish;
410 : 50 : iface->append_to_async = g_file_real_append_to_async;
411 : 50 : iface->append_to_finish = g_file_real_append_to_finish;
412 : 50 : iface->create_async = g_file_real_create_async;
413 : 50 : iface->create_finish = g_file_real_create_finish;
414 : 50 : iface->replace_async = g_file_real_replace_async;
415 : 50 : iface->replace_finish = g_file_real_replace_finish;
416 : 50 : iface->delete_file_async = g_file_real_delete_async;
417 : 50 : iface->delete_file_finish = g_file_real_delete_finish;
418 : 50 : iface->trash_async = g_file_real_trash_async;
419 : 50 : iface->trash_finish = g_file_real_trash_finish;
420 : 50 : iface->move_async = g_file_real_move_async;
421 : 50 : iface->move_finish = g_file_real_move_finish;
422 : 50 : iface->make_directory_async = g_file_real_make_directory_async;
423 : 50 : iface->make_directory_finish = g_file_real_make_directory_finish;
424 : 50 : iface->make_symbolic_link_async = g_file_real_make_symbolic_link_async;
425 : 50 : iface->make_symbolic_link_finish = g_file_real_make_symbolic_link_finish;
426 : 50 : iface->open_readwrite_async = g_file_real_open_readwrite_async;
427 : 50 : iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
428 : 50 : iface->create_readwrite_async = g_file_real_create_readwrite_async;
429 : 50 : iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
430 : 50 : iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
431 : 50 : iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
432 : 50 : iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
433 : 50 : iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
434 : 50 : iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
435 : 50 : iface->copy_async = g_file_real_copy_async;
436 : 50 : iface->copy_finish = g_file_real_copy_finish;
437 : 50 : iface->measure_disk_usage = g_file_real_measure_disk_usage;
438 : 50 : iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
439 : 50 : iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
440 : 50 : }
441 : :
442 : :
443 : : /**
444 : : * g_file_is_native:
445 : : * @file: input #GFile
446 : : *
447 : : * Checks to see if a file is native to the platform.
448 : : *
449 : : * A native file is one expressed in the platform-native filename format,
450 : : * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
451 : : * as it might be on a locally mounted remote filesystem.
452 : : *
453 : : * On some systems non-native files may be available using the native
454 : : * filesystem via a userspace filesystem (FUSE), in these cases this call
455 : : * will return %FALSE, but g_file_get_path() will still return a native path.
456 : : *
457 : : * This call does no blocking I/O.
458 : : *
459 : : * Returns: %TRUE if @file is native
460 : : */
461 : : gboolean
462 : 12 : g_file_is_native (GFile *file)
463 : : {
464 : : GFileIface *iface;
465 : :
466 : 12 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
467 : :
468 : 12 : iface = G_FILE_GET_IFACE (file);
469 : :
470 : 12 : return (* iface->is_native) (file);
471 : : }
472 : :
473 : :
474 : : /**
475 : : * g_file_has_uri_scheme:
476 : : * @file: input #GFile
477 : : * @uri_scheme: a string containing a URI scheme
478 : : *
479 : : * Checks to see if a #GFile has a given URI scheme.
480 : : *
481 : : * This call does no blocking I/O.
482 : : *
483 : : * Returns: %TRUE if #GFile's backend supports the
484 : : * given URI scheme, %FALSE if URI scheme is %NULL,
485 : : * not supported, or #GFile is invalid.
486 : : */
487 : : gboolean
488 : 10 : g_file_has_uri_scheme (GFile *file,
489 : : const char *uri_scheme)
490 : : {
491 : : GFileIface *iface;
492 : :
493 : 10 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
494 : 10 : g_return_val_if_fail (uri_scheme != NULL, FALSE);
495 : :
496 : 10 : iface = G_FILE_GET_IFACE (file);
497 : :
498 : 10 : return (* iface->has_uri_scheme) (file, uri_scheme);
499 : : }
500 : :
501 : :
502 : : /**
503 : : * g_file_get_uri_scheme:
504 : : * @file: input #GFile
505 : : *
506 : : * Gets the URI scheme for a #GFile.
507 : : * RFC 3986 decodes the scheme as:
508 : : * |[
509 : : * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
510 : : * ]|
511 : : * Common schemes include "file", "http", "ftp", etc.
512 : : *
513 : : * The scheme can be different from the one used to construct the #GFile,
514 : : * in that it might be replaced with one that is logically equivalent to the #GFile.
515 : : *
516 : : * This call does no blocking I/O.
517 : : *
518 : : * Returns: (nullable): a string containing the URI scheme for the given
519 : : * #GFile or %NULL if the #GFile was constructed with an invalid URI. The
520 : : * returned string should be freed with g_free() when no longer needed.
521 : : */
522 : : char *
523 : 31 : g_file_get_uri_scheme (GFile *file)
524 : : {
525 : : GFileIface *iface;
526 : :
527 : 31 : g_return_val_if_fail (G_IS_FILE (file), NULL);
528 : :
529 : 31 : iface = G_FILE_GET_IFACE (file);
530 : :
531 : 31 : return (* iface->get_uri_scheme) (file);
532 : : }
533 : :
534 : :
535 : : /**
536 : : * g_file_get_basename: (virtual get_basename)
537 : : * @file: input #GFile
538 : : *
539 : : * Gets the base name (the last component of the path) for a given #GFile.
540 : : *
541 : : * If called for the top level of a system (such as the filesystem root
542 : : * or a uri like sftp://host/) it will return a single directory separator
543 : : * (and on Windows, possibly a drive letter).
544 : : *
545 : : * The base name is a byte string (not UTF-8). It has no defined encoding
546 : : * or rules other than it may not contain zero bytes. If you want to use
547 : : * filenames in a user interface you should use the display name that you
548 : : * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
549 : : * attribute with g_file_query_info().
550 : : *
551 : : * This call does no blocking I/O.
552 : : *
553 : : * Returns: (type filename) (nullable): string containing the #GFile's
554 : : * base name, or %NULL if given #GFile is invalid. The returned string
555 : : * should be freed with g_free() when no longer needed.
556 : : */
557 : : char *
558 : 63 : g_file_get_basename (GFile *file)
559 : : {
560 : : GFileIface *iface;
561 : :
562 : 63 : g_return_val_if_fail (G_IS_FILE (file), NULL);
563 : :
564 : 63 : iface = G_FILE_GET_IFACE (file);
565 : :
566 : 63 : return (* iface->get_basename) (file);
567 : : }
568 : :
569 : : /**
570 : : * g_file_get_path: (virtual get_path)
571 : : * @file: input #GFile
572 : : *
573 : : * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
574 : : * guaranteed to be an absolute, canonical path. It might contain symlinks.
575 : : *
576 : : * This call does no blocking I/O.
577 : : *
578 : : * Returns: (type filename) (nullable): string containing the #GFile's path,
579 : : * or %NULL if no such path exists. The returned string should be freed
580 : : * with g_free() when no longer needed.
581 : : */
582 : : char *
583 : 413 : g_file_get_path (GFile *file)
584 : : {
585 : : GFileIface *iface;
586 : :
587 : 413 : g_return_val_if_fail (G_IS_FILE (file), NULL);
588 : :
589 : 413 : iface = G_FILE_GET_IFACE (file);
590 : :
591 : 413 : return (* iface->get_path) (file);
592 : : }
593 : :
594 : : static const char *
595 : 0 : file_peek_path_generic (GFile *file)
596 : : {
597 : : const char *path;
598 : : static GQuark _file_path_quark = 0;
599 : :
600 : 0 : if (G_UNLIKELY (_file_path_quark) == 0)
601 : 0 : _file_path_quark = g_quark_from_static_string ("gio-file-path");
602 : :
603 : : /* We need to be careful about threading, as two threads calling
604 : : * g_file_peek_path() on the same file could race: both would see
605 : : * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
606 : : * the path, but the second thread to add it would end up freeing the path
607 : : * set by the first thread. The first thread would still return the pointer
608 : : * to that freed path, though, resulting an a read-after-free. Handle that
609 : : * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
610 : :
611 : : while (TRUE)
612 : 0 : {
613 : 0 : gchar *new_path = NULL;
614 : :
615 : 0 : path = g_object_get_qdata ((GObject*)file, _file_path_quark);
616 : :
617 : 0 : if (path != NULL)
618 : 0 : break;
619 : :
620 : 0 : new_path = g_file_get_path (file);
621 : 0 : if (new_path == NULL)
622 : 0 : return NULL;
623 : :
624 : : /* By passing NULL here, we ensure we never replace existing data: */
625 : 0 : if (g_object_replace_qdata ((GObject *) file, _file_path_quark,
626 : : NULL, (gpointer) new_path,
627 : : (GDestroyNotify) g_free, NULL))
628 : : {
629 : 0 : path = new_path;
630 : 0 : break;
631 : : }
632 : : else
633 : 0 : g_free (new_path);
634 : : }
635 : :
636 : 0 : return path;
637 : : }
638 : :
639 : : /**
640 : : * g_file_peek_path:
641 : : * @file: input #GFile
642 : : *
643 : : * Exactly like g_file_get_path(), but caches the result via
644 : : * g_object_set_qdata_full(). This is useful for example in C
645 : : * applications which mix `g_file_*` APIs with native ones. It
646 : : * also avoids an extra duplicated string when possible, so will be
647 : : * generally more efficient.
648 : : *
649 : : * This call does no blocking I/O.
650 : : *
651 : : * Returns: (type filename) (nullable): string containing the #GFile's path,
652 : : * or %NULL if no such path exists. The returned string is owned by @file.
653 : : * Since: 2.56
654 : : */
655 : : const char *
656 : 453 : g_file_peek_path (GFile *file)
657 : : {
658 : 453 : if (G_IS_LOCAL_FILE (file))
659 : 453 : return _g_local_file_get_filename ((GLocalFile *) file);
660 : 0 : return file_peek_path_generic (file);
661 : : }
662 : :
663 : : /**
664 : : * g_file_get_uri:
665 : : * @file: input #GFile
666 : : *
667 : : * Gets the URI for the @file.
668 : : *
669 : : * This call does no blocking I/O.
670 : : *
671 : : * Returns: a string containing the #GFile's URI. If the #GFile was constructed
672 : : * with an invalid URI, an invalid URI is returned.
673 : : * The returned string should be freed with g_free()
674 : : * when no longer needed.
675 : : */
676 : : char *
677 : 70 : g_file_get_uri (GFile *file)
678 : : {
679 : : GFileIface *iface;
680 : :
681 : 70 : g_return_val_if_fail (G_IS_FILE (file), NULL);
682 : :
683 : 70 : iface = G_FILE_GET_IFACE (file);
684 : :
685 : 70 : return (* iface->get_uri) (file);
686 : : }
687 : :
688 : : /**
689 : : * g_file_get_parse_name:
690 : : * @file: input #GFile
691 : : *
692 : : * Gets the parse name of the @file.
693 : : * A parse name is a UTF-8 string that describes the
694 : : * file such that one can get the #GFile back using
695 : : * g_file_parse_name().
696 : : *
697 : : * This is generally used to show the #GFile as a nice
698 : : * full-pathname kind of string in a user interface,
699 : : * like in a location entry.
700 : : *
701 : : * For local files with names that can safely be converted
702 : : * to UTF-8 the pathname is used, otherwise the IRI is used
703 : : * (a form of URI that allows UTF-8 characters unescaped).
704 : : *
705 : : * This call does no blocking I/O.
706 : : *
707 : : * Returns: a string containing the #GFile's parse name.
708 : : * The returned string should be freed with g_free()
709 : : * when no longer needed.
710 : : */
711 : : char *
712 : 8 : g_file_get_parse_name (GFile *file)
713 : : {
714 : : GFileIface *iface;
715 : :
716 : 8 : g_return_val_if_fail (G_IS_FILE (file), NULL);
717 : :
718 : 8 : iface = G_FILE_GET_IFACE (file);
719 : :
720 : 8 : return (* iface->get_parse_name) (file);
721 : : }
722 : :
723 : : /**
724 : : * g_file_dup:
725 : : * @file: input #GFile
726 : : *
727 : : * Duplicates a #GFile handle. This operation does not duplicate
728 : : * the actual file or directory represented by the #GFile; see
729 : : * g_file_copy() if attempting to copy a file.
730 : : *
731 : : * g_file_dup() is useful when a second handle is needed to the same underlying
732 : : * file, for use in a separate thread (#GFile is not thread-safe). For use
733 : : * within the same thread, use g_object_ref() to increment the existing object’s
734 : : * reference count.
735 : : *
736 : : * This call does no blocking I/O.
737 : : *
738 : : * Returns: (transfer full): a new #GFile that is a duplicate
739 : : * of the given #GFile.
740 : : */
741 : : GFile *
742 : 6 : g_file_dup (GFile *file)
743 : : {
744 : : GFileIface *iface;
745 : :
746 : 6 : g_return_val_if_fail (G_IS_FILE (file), NULL);
747 : :
748 : 6 : iface = G_FILE_GET_IFACE (file);
749 : :
750 : 6 : return (* iface->dup) (file);
751 : : }
752 : :
753 : : /**
754 : : * g_file_hash: (virtual hash)
755 : : * @file: (type GFile): #gconstpointer to a #GFile
756 : : *
757 : : * Creates a hash value for a #GFile.
758 : : *
759 : : * This call does no blocking I/O.
760 : : *
761 : : * Returns: 0 if @file is not a valid #GFile, otherwise an
762 : : * integer that can be used as hash value for the #GFile.
763 : : * This function is intended for easily hashing a #GFile to
764 : : * add to a #GHashTable or similar data structure.
765 : : */
766 : : guint
767 : 6 : g_file_hash (gconstpointer file)
768 : : {
769 : : GFileIface *iface;
770 : :
771 : 6 : g_return_val_if_fail (G_IS_FILE (file), 0);
772 : :
773 : 6 : iface = G_FILE_GET_IFACE (file);
774 : :
775 : 6 : return (* iface->hash) ((GFile *)file);
776 : : }
777 : :
778 : : /**
779 : : * g_file_equal:
780 : : * @file1: the first #GFile
781 : : * @file2: the second #GFile
782 : : *
783 : : * Checks if the two given #GFiles refer to the same file.
784 : : *
785 : : * This function can be used with [method@Gio.File.hash] to insert
786 : : * [iface@Gio.File]s efficiently in a hash table.
787 : : *
788 : : * Note that two #GFiles that differ can still refer to the same
789 : : * file on the filesystem due to various forms of filename
790 : : * aliasing. For local files, this function essentially compares the file paths,
791 : : * so two [iface@Gio.File]s which point to different hard or soft links will not
792 : : * be considered equal, despite pointing to the same content.
793 : : *
794 : : * For determining whether two files are hardlinked, see
795 : : * [const@Gio.FILE_ATTRIBUTE_ID_FILE].
796 : : *
797 : : * This call does no blocking I/O.
798 : : *
799 : : * Returns: %TRUE if @file1 and @file2 are equal.
800 : : */
801 : : gboolean
802 : 112 : g_file_equal (GFile *file1,
803 : : GFile *file2)
804 : : {
805 : : GFileIface *iface;
806 : :
807 : 112 : g_return_val_if_fail (G_IS_FILE (file1), FALSE);
808 : 112 : g_return_val_if_fail (G_IS_FILE (file2), FALSE);
809 : :
810 : 112 : if (file1 == file2)
811 : 0 : return TRUE;
812 : :
813 : 112 : if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
814 : 0 : return FALSE;
815 : :
816 : 112 : iface = G_FILE_GET_IFACE (file1);
817 : :
818 : 112 : return (* iface->equal) (file1, file2);
819 : : }
820 : :
821 : :
822 : : /**
823 : : * g_file_get_parent:
824 : : * @file: input #GFile
825 : : *
826 : : * Gets the parent directory for the @file.
827 : : * If the @file represents the root directory of the
828 : : * file system, then %NULL will be returned.
829 : : *
830 : : * This call does no blocking I/O.
831 : : *
832 : : * Returns: (nullable) (transfer full): a #GFile structure to the
833 : : * parent of the given #GFile or %NULL if there is no parent. Free
834 : : * the returned object with g_object_unref().
835 : : */
836 : : GFile *
837 : 96 : g_file_get_parent (GFile *file)
838 : : {
839 : : GFileIface *iface;
840 : :
841 : 96 : g_return_val_if_fail (G_IS_FILE (file), NULL);
842 : :
843 : 96 : iface = G_FILE_GET_IFACE (file);
844 : :
845 : 96 : return (* iface->get_parent) (file);
846 : : }
847 : :
848 : : /**
849 : : * g_file_has_parent:
850 : : * @file: input #GFile
851 : : * @parent: (nullable): the parent to check for, or %NULL
852 : : *
853 : : * Checks if @file has a parent, and optionally, if it is @parent.
854 : : *
855 : : * If @parent is %NULL then this function returns %TRUE if @file has any
856 : : * parent at all. If @parent is non-%NULL then %TRUE is only returned
857 : : * if @file is an immediate child of @parent.
858 : : *
859 : : * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
860 : : * the case that @parent is %NULL).
861 : : *
862 : : * Since: 2.24
863 : : */
864 : : gboolean
865 : 3 : g_file_has_parent (GFile *file,
866 : : GFile *parent)
867 : : {
868 : : GFile *actual_parent;
869 : : gboolean result;
870 : :
871 : 3 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
872 : 3 : g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
873 : :
874 : 3 : actual_parent = g_file_get_parent (file);
875 : :
876 : 3 : if (actual_parent != NULL)
877 : : {
878 : 3 : if (parent != NULL)
879 : 3 : result = g_file_equal (parent, actual_parent);
880 : : else
881 : 0 : result = TRUE;
882 : :
883 : 3 : g_object_unref (actual_parent);
884 : : }
885 : : else
886 : 0 : result = FALSE;
887 : :
888 : 3 : return result;
889 : : }
890 : :
891 : : /**
892 : : * g_file_get_child:
893 : : * @file: input #GFile
894 : : * @name: (type filename): string containing the child's basename
895 : : *
896 : : * Gets a child of @file with basename equal to @name.
897 : : *
898 : : * Note that the file with that specific name might not exist, but
899 : : * you can still have a #GFile that points to it. You can use this
900 : : * for instance to create that file.
901 : : *
902 : : * This call does no blocking I/O.
903 : : *
904 : : * Returns: (transfer full): a #GFile to a child specified by @name.
905 : : * Free the returned object with g_object_unref().
906 : : */
907 : : GFile *
908 : 1285 : g_file_get_child (GFile *file,
909 : : const char *name)
910 : : {
911 : 1285 : g_return_val_if_fail (G_IS_FILE (file), NULL);
912 : 1285 : g_return_val_if_fail (name != NULL, NULL);
913 : 1285 : g_return_val_if_fail (!g_path_is_absolute (name), NULL);
914 : :
915 : 1285 : return g_file_resolve_relative_path (file, name);
916 : : }
917 : :
918 : : /**
919 : : * g_file_get_child_for_display_name:
920 : : * @file: input #GFile
921 : : * @display_name: string to a possible child
922 : : * @error: return location for an error
923 : : *
924 : : * Gets the child of @file for a given @display_name (i.e. a UTF-8
925 : : * version of the name). If this function fails, it returns %NULL
926 : : * and @error will be set. This is very useful when constructing a
927 : : * #GFile for a new file and the user entered the filename in the
928 : : * user interface, for instance when you select a directory and
929 : : * type a filename in the file selector.
930 : : *
931 : : * This call does no blocking I/O.
932 : : *
933 : : * Returns: (transfer full): a #GFile to the specified child, or
934 : : * %NULL if the display name couldn't be converted.
935 : : * Free the returned object with g_object_unref().
936 : : */
937 : : GFile *
938 : 2 : g_file_get_child_for_display_name (GFile *file,
939 : : const char *display_name,
940 : : GError **error)
941 : : {
942 : : GFileIface *iface;
943 : :
944 : 2 : g_return_val_if_fail (G_IS_FILE (file), NULL);
945 : 2 : g_return_val_if_fail (display_name != NULL, NULL);
946 : :
947 : 2 : iface = G_FILE_GET_IFACE (file);
948 : :
949 : 2 : return (* iface->get_child_for_display_name) (file, display_name, error);
950 : : }
951 : :
952 : : /**
953 : : * g_file_has_prefix: (virtual prefix_matches)
954 : : * @file: input #GFile
955 : : * @prefix: input #GFile
956 : : *
957 : : * Checks whether @file has the prefix specified by @prefix.
958 : : *
959 : : * In other words, if the names of initial elements of @file's
960 : : * pathname match @prefix. Only full pathname elements are matched,
961 : : * so a path like /foo is not considered a prefix of /foobar, only
962 : : * of /foo/bar.
963 : : *
964 : : * A #GFile is not a prefix of itself. If you want to check for
965 : : * equality, use g_file_equal().
966 : : *
967 : : * This call does no I/O, as it works purely on names. As such it can
968 : : * sometimes return %FALSE even if @file is inside a @prefix (from a
969 : : * filesystem point of view), because the prefix of @file is an alias
970 : : * of @prefix.
971 : : *
972 : : * Returns: %TRUE if the @file's parent, grandparent, etc is @prefix,
973 : : * %FALSE otherwise.
974 : : */
975 : : gboolean
976 : 17 : g_file_has_prefix (GFile *file,
977 : : GFile *prefix)
978 : : {
979 : : GFileIface *iface;
980 : :
981 : 17 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
982 : 17 : g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
983 : :
984 : 17 : if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
985 : 0 : return FALSE;
986 : :
987 : 17 : iface = G_FILE_GET_IFACE (file);
988 : :
989 : : /* The vtable function differs in arg order since
990 : : * we're using the old contains_file call
991 : : */
992 : 17 : return (* iface->prefix_matches) (prefix, file);
993 : : }
994 : :
995 : : /**
996 : : * g_file_get_relative_path: (virtual get_relative_path)
997 : : * @parent: input #GFile
998 : : * @descendant: input #GFile
999 : : *
1000 : : * Gets the path for @descendant relative to @parent.
1001 : : *
1002 : : * This call does no blocking I/O.
1003 : : *
1004 : : * Returns: (type filename) (nullable): string with the relative path from
1005 : : * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
1006 : : * prefix. The returned string should be freed with g_free() when
1007 : : * no longer needed.
1008 : : */
1009 : : char *
1010 : 72 : g_file_get_relative_path (GFile *parent,
1011 : : GFile *descendant)
1012 : : {
1013 : : GFileIface *iface;
1014 : :
1015 : 72 : g_return_val_if_fail (G_IS_FILE (parent), NULL);
1016 : 72 : g_return_val_if_fail (G_IS_FILE (descendant), NULL);
1017 : :
1018 : 72 : if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
1019 : 0 : return NULL;
1020 : :
1021 : 72 : iface = G_FILE_GET_IFACE (parent);
1022 : :
1023 : 72 : return (* iface->get_relative_path) (parent, descendant);
1024 : : }
1025 : :
1026 : : /**
1027 : : * g_file_resolve_relative_path:
1028 : : * @file: input #GFile
1029 : : * @relative_path: (type filename): a given relative path string
1030 : : *
1031 : : * Resolves a relative path for @file to an absolute path.
1032 : : *
1033 : : * This call does no blocking I/O.
1034 : : *
1035 : : * If the @relative_path is an absolute path name, the resolution
1036 : : * is done absolutely (without taking @file path as base).
1037 : : *
1038 : : * Returns: (transfer full): a #GFile for the resolved path.
1039 : : */
1040 : : GFile *
1041 : 1297 : g_file_resolve_relative_path (GFile *file,
1042 : : const char *relative_path)
1043 : : {
1044 : : GFileIface *iface;
1045 : :
1046 : 1297 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1047 : 1297 : g_return_val_if_fail (relative_path != NULL, NULL);
1048 : :
1049 : 1297 : iface = G_FILE_GET_IFACE (file);
1050 : :
1051 : 1297 : return (* iface->resolve_relative_path) (file, relative_path);
1052 : : }
1053 : :
1054 : : /**
1055 : : * g_file_enumerate_children:
1056 : : * @file: input #GFile
1057 : : * @attributes: an attribute query string
1058 : : * @flags: a set of #GFileQueryInfoFlags
1059 : : * @cancellable: (nullable): optional #GCancellable object,
1060 : : * %NULL to ignore
1061 : : * @error: #GError for error reporting
1062 : : *
1063 : : * Gets the requested information about the files in a directory.
1064 : : * The result is a #GFileEnumerator object that will give out
1065 : : * #GFileInfo objects for all the files in the directory.
1066 : : *
1067 : : * The @attributes value is a string that specifies the file
1068 : : * attributes that should be gathered. It is not an error if
1069 : : * it's not possible to read a particular requested attribute
1070 : : * from a file - it just won't be set. @attributes should
1071 : : * be a comma-separated list of attributes or attribute wildcards.
1072 : : * The wildcard "*" means all attributes, and a wildcard like
1073 : : * "standard::*" means all attributes in the standard namespace.
1074 : : * An example attribute query be "standard::*,owner::user".
1075 : : * The standard attributes are available as defines, like
1076 : : * %G_FILE_ATTRIBUTE_STANDARD_NAME. %G_FILE_ATTRIBUTE_STANDARD_NAME should
1077 : : * always be specified if you plan to call g_file_enumerator_get_child() or
1078 : : * g_file_enumerator_iterate() on the returned enumerator.
1079 : : *
1080 : : * If @cancellable is not %NULL, then the operation can be cancelled
1081 : : * by triggering the cancellable object from another thread. If the
1082 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1083 : : * returned.
1084 : : *
1085 : : * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1086 : : * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1087 : : * error will be returned. Other errors are possible too.
1088 : : *
1089 : : * Returns: (transfer full): A #GFileEnumerator if successful,
1090 : : * %NULL on error. Free the returned object with g_object_unref().
1091 : : */
1092 : : GFileEnumerator *
1093 : 245 : g_file_enumerate_children (GFile *file,
1094 : : const char *attributes,
1095 : : GFileQueryInfoFlags flags,
1096 : : GCancellable *cancellable,
1097 : : GError **error)
1098 : : {
1099 : : GFileIface *iface;
1100 : :
1101 : 245 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1102 : :
1103 : 245 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1104 : 0 : return NULL;
1105 : :
1106 : 245 : iface = G_FILE_GET_IFACE (file);
1107 : :
1108 : 245 : if (iface->enumerate_children == NULL)
1109 : : {
1110 : 1 : g_set_error_literal (error, G_IO_ERROR,
1111 : : G_IO_ERROR_NOT_SUPPORTED,
1112 : : _("Operation not supported"));
1113 : 1 : return NULL;
1114 : : }
1115 : :
1116 : 244 : return (* iface->enumerate_children) (file, attributes, flags,
1117 : : cancellable, error);
1118 : : }
1119 : :
1120 : : /**
1121 : : * g_file_enumerate_children_async:
1122 : : * @file: input #GFile
1123 : : * @attributes: an attribute query string
1124 : : * @flags: a set of #GFileQueryInfoFlags
1125 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
1126 : : * @cancellable: (nullable): optional #GCancellable object,
1127 : : * %NULL to ignore
1128 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1129 : : * to call when the request is satisfied
1130 : : * @user_data: the data to pass to callback function
1131 : : *
1132 : : * Asynchronously gets the requested information about the files
1133 : : * in a directory. The result is a #GFileEnumerator object that will
1134 : : * give out #GFileInfo objects for all the files in the directory.
1135 : : *
1136 : : * For more details, see g_file_enumerate_children() which is
1137 : : * the synchronous version of this call.
1138 : : *
1139 : : * When the operation is finished, @callback will be called. You can
1140 : : * then call g_file_enumerate_children_finish() to get the result of
1141 : : * the operation.
1142 : : */
1143 : : void
1144 : 6 : g_file_enumerate_children_async (GFile *file,
1145 : : const char *attributes,
1146 : : GFileQueryInfoFlags flags,
1147 : : int io_priority,
1148 : : GCancellable *cancellable,
1149 : : GAsyncReadyCallback callback,
1150 : : gpointer user_data)
1151 : : {
1152 : : GFileIface *iface;
1153 : :
1154 : 6 : g_return_if_fail (G_IS_FILE (file));
1155 : :
1156 : 6 : iface = G_FILE_GET_IFACE (file);
1157 : 6 : (* iface->enumerate_children_async) (file,
1158 : : attributes,
1159 : : flags,
1160 : : io_priority,
1161 : : cancellable,
1162 : : callback,
1163 : : user_data);
1164 : : }
1165 : :
1166 : : /**
1167 : : * g_file_enumerate_children_finish:
1168 : : * @file: input #GFile
1169 : : * @res: a #GAsyncResult
1170 : : * @error: a #GError
1171 : : *
1172 : : * Finishes an async enumerate children operation.
1173 : : * See g_file_enumerate_children_async().
1174 : : *
1175 : : * Returns: (transfer full): a #GFileEnumerator or %NULL
1176 : : * if an error occurred.
1177 : : * Free the returned object with g_object_unref().
1178 : : */
1179 : : GFileEnumerator *
1180 : 6 : g_file_enumerate_children_finish (GFile *file,
1181 : : GAsyncResult *res,
1182 : : GError **error)
1183 : : {
1184 : : GFileIface *iface;
1185 : :
1186 : 6 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1187 : 6 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1188 : :
1189 : 6 : if (g_async_result_legacy_propagate_error (res, error))
1190 : 0 : return NULL;
1191 : :
1192 : 6 : iface = G_FILE_GET_IFACE (file);
1193 : 6 : return (* iface->enumerate_children_finish) (file, res, error);
1194 : : }
1195 : :
1196 : : /**
1197 : : * g_file_query_exists:
1198 : : * @file: input #GFile
1199 : : * @cancellable: (nullable): optional #GCancellable object,
1200 : : * %NULL to ignore
1201 : : *
1202 : : * Utility function to check if a particular file exists.
1203 : : *
1204 : : * The fallback implementation of this API is using [method@Gio.File.query_info]
1205 : : * and therefore may do blocking I/O. To asynchronously query the existence
1206 : : * of a file, use [method@Gio.File.query_info_async].
1207 : : *
1208 : : * Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
1209 : : * and then execute something based on the outcome of that, because the
1210 : : * file might have been created or removed in between the operations. The
1211 : : * general approach to handling that is to not check, but just do the
1212 : : * operation and handle the errors as they come.
1213 : : *
1214 : : * As an example of race-free checking, take the case of reading a file,
1215 : : * and if it doesn't exist, creating it. There are two racy versions: read
1216 : : * it, and on error create it; and: check if it exists, if not create it.
1217 : : * These can both result in two processes creating the file (with perhaps
1218 : : * a partially written file as the result). The correct approach is to
1219 : : * always try to create the file with g_file_create() which will either
1220 : : * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1221 : : *
1222 : : * However, in many cases an existence check is useful in a user interface,
1223 : : * for instance to make a menu item sensitive/insensitive, so that you don't
1224 : : * have to fool users that something is possible and then just show an error
1225 : : * dialog. If you do this, you should make sure to also handle the errors
1226 : : * that can happen due to races when you execute the operation.
1227 : : *
1228 : : * Returns: %TRUE if the file exists (and can be detected without error),
1229 : : * %FALSE otherwise (or if cancelled).
1230 : : */
1231 : : gboolean
1232 : 172 : g_file_query_exists (GFile *file,
1233 : : GCancellable *cancellable)
1234 : : {
1235 : : GFileIface *iface;
1236 : : GFileInfo *info;
1237 : :
1238 : 172 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
1239 : :
1240 : 172 : iface = G_FILE_GET_IFACE (file);
1241 : :
1242 : 172 : if (iface->query_exists)
1243 : 172 : return iface->query_exists (file, cancellable);
1244 : :
1245 : 0 : info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1246 : : G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1247 : 0 : if (info != NULL)
1248 : : {
1249 : 0 : g_object_unref (info);
1250 : 0 : return TRUE;
1251 : : }
1252 : :
1253 : 0 : return FALSE;
1254 : : }
1255 : :
1256 : : /**
1257 : : * g_file_query_file_type:
1258 : : * @file: input #GFile
1259 : : * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1260 : : * @cancellable: (nullable): optional #GCancellable object,
1261 : : * %NULL to ignore
1262 : : *
1263 : : * Utility function to inspect the #GFileType of a file. This is
1264 : : * implemented using g_file_query_info() and as such does blocking I/O.
1265 : : *
1266 : : * The primary use case of this method is to check if a file is
1267 : : * a regular file, directory, or symlink.
1268 : : *
1269 : : * Returns: The #GFileType of the file and %G_FILE_TYPE_UNKNOWN
1270 : : * if the file does not exist
1271 : : *
1272 : : * Since: 2.18
1273 : : */
1274 : : GFileType
1275 : 224 : g_file_query_file_type (GFile *file,
1276 : : GFileQueryInfoFlags flags,
1277 : : GCancellable *cancellable)
1278 : : {
1279 : : GFileInfo *info;
1280 : : GFileType file_type;
1281 : :
1282 : 224 : g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1283 : 224 : info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1284 : : cancellable, NULL);
1285 : 224 : if (info != NULL)
1286 : : {
1287 : 194 : file_type = g_file_info_get_file_type (info);
1288 : 194 : g_object_unref (info);
1289 : : }
1290 : : else
1291 : 30 : file_type = G_FILE_TYPE_UNKNOWN;
1292 : :
1293 : 224 : return file_type;
1294 : : }
1295 : :
1296 : : /**
1297 : : * g_file_query_info:
1298 : : * @file: input file
1299 : : * @attributes: an attribute query string
1300 : : * @flags: flags to affect the query operation
1301 : : * @cancellable: (nullable): optional cancellable object
1302 : : * @error: return location for an error
1303 : : *
1304 : : * Gets the requested information about specified @file.
1305 : : *
1306 : : * The result is a [class@Gio.FileInfo] object that contains key-value
1307 : : * attributes (such as the type or size of the file).
1308 : : *
1309 : : * The @attributes value is a string that specifies the file
1310 : : * attributes that should be gathered. It is not an error if
1311 : : * it’s not possible to read a particular requested attribute
1312 : : * from a file — it just won't be set. In particular this means that if a file
1313 : : * is inaccessible (due to being in a folder with restrictive permissions), for
1314 : : * example, you can expect the returned [class@Gio.FileInfo] to have very few
1315 : : * attributes set. You should check whether an attribute is set using
1316 : : * [method@Gio.FileInfo.has_attribute] before trying to retrieve its value.
1317 : : *
1318 : : * It is guaranteed that if any of the following attributes are listed in
1319 : : * @attributes, they will always be set in the returned [class@Gio.FileInfo],
1320 : : * even if the user doesn’t have permissions to access the file:
1321 : : *
1322 : : * - [const@Gio.FILE_ATTRIBUTE_STANDARD_NAME]
1323 : : * - [const@Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME]
1324 : : *
1325 : : * @attributes should be a comma-separated list of attributes or attribute
1326 : : * wildcards. The wildcard `"*"` means all attributes, and a wildcard like
1327 : : * `"standard::*"` means all attributes in the standard namespace.
1328 : : * An example attribute query might be `"standard::*,owner::user"`.
1329 : : * The standard attributes are available as defines, like
1330 : : * [const@Gio.FILE_ATTRIBUTE_STANDARD_NAME].
1331 : : *
1332 : : * If @cancellable is not `NULL`, then the operation can be cancelled
1333 : : * by triggering the cancellable object from another thread. If the
1334 : : * operation was cancelled, the error [error@Gio.IOErrorEnum.CANCELLED] will be
1335 : : * returned.
1336 : : *
1337 : : * For symlinks, normally the information about the target of the
1338 : : * symlink is returned, rather than information about the symlink
1339 : : * itself. However if you pass [flags@Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS]
1340 : : * in @flags the information about the symlink itself will be returned.
1341 : : * Also, for symlinks that point to non-existing files the information
1342 : : * about the symlink itself will be returned.
1343 : : *
1344 : : * If the file does not exist, the [error@Gio.IOErrorEnum.NOT_FOUND] error will be
1345 : : * returned. Other errors are possible too, and depend on what kind of
1346 : : * file system the file is on.
1347 : : *
1348 : : * Returns: (transfer full): a [class@Gio.FileInfo] for the given @file
1349 : : */
1350 : : GFileInfo *
1351 : 671 : g_file_query_info (GFile *file,
1352 : : const char *attributes,
1353 : : GFileQueryInfoFlags flags,
1354 : : GCancellable *cancellable,
1355 : : GError **error)
1356 : : {
1357 : : GFileIface *iface;
1358 : :
1359 : 671 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1360 : :
1361 : 671 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1362 : 3 : return NULL;
1363 : :
1364 : 668 : iface = G_FILE_GET_IFACE (file);
1365 : :
1366 : 668 : if (iface->query_info == NULL)
1367 : : {
1368 : 6 : g_set_error_literal (error, G_IO_ERROR,
1369 : : G_IO_ERROR_NOT_SUPPORTED,
1370 : : _("Operation not supported"));
1371 : 6 : return NULL;
1372 : : }
1373 : :
1374 : 662 : return (* iface->query_info) (file, attributes, flags, cancellable, error);
1375 : : }
1376 : :
1377 : : /**
1378 : : * g_file_query_info_async:
1379 : : * @file: input #GFile
1380 : : * @attributes: an attribute query string
1381 : : * @flags: a set of #GFileQueryInfoFlags
1382 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
1383 : : * @cancellable: (nullable): optional #GCancellable object,
1384 : : * %NULL to ignore
1385 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1386 : : * to call when the request is satisfied
1387 : : * @user_data: the data to pass to callback function
1388 : : *
1389 : : * Asynchronously gets the requested information about specified @file.
1390 : : * The result is a #GFileInfo object that contains key-value attributes
1391 : : * (such as type or size for the file).
1392 : : *
1393 : : * For more details, see g_file_query_info() which is the synchronous
1394 : : * version of this call.
1395 : : *
1396 : : * When the operation is finished, @callback will be called. You can
1397 : : * then call g_file_query_info_finish() to get the result of the operation.
1398 : : */
1399 : : void
1400 : 10 : g_file_query_info_async (GFile *file,
1401 : : const char *attributes,
1402 : : GFileQueryInfoFlags flags,
1403 : : int io_priority,
1404 : : GCancellable *cancellable,
1405 : : GAsyncReadyCallback callback,
1406 : : gpointer user_data)
1407 : : {
1408 : : GFileIface *iface;
1409 : :
1410 : 10 : g_return_if_fail (G_IS_FILE (file));
1411 : :
1412 : 10 : iface = G_FILE_GET_IFACE (file);
1413 : 10 : (* iface->query_info_async) (file,
1414 : : attributes,
1415 : : flags,
1416 : : io_priority,
1417 : : cancellable,
1418 : : callback,
1419 : : user_data);
1420 : : }
1421 : :
1422 : : /**
1423 : : * g_file_query_info_finish:
1424 : : * @file: input #GFile
1425 : : * @res: a #GAsyncResult
1426 : : * @error: a #GError
1427 : : *
1428 : : * Finishes an asynchronous file info query.
1429 : : * See g_file_query_info_async().
1430 : : *
1431 : : * Returns: (transfer full): #GFileInfo for given @file
1432 : : * or %NULL on error. Free the returned object with
1433 : : * g_object_unref().
1434 : : */
1435 : : GFileInfo *
1436 : 10 : g_file_query_info_finish (GFile *file,
1437 : : GAsyncResult *res,
1438 : : GError **error)
1439 : : {
1440 : : GFileIface *iface;
1441 : :
1442 : 10 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1443 : 10 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1444 : :
1445 : 10 : if (g_async_result_legacy_propagate_error (res, error))
1446 : 0 : return NULL;
1447 : :
1448 : 10 : iface = G_FILE_GET_IFACE (file);
1449 : 10 : return (* iface->query_info_finish) (file, res, error);
1450 : : }
1451 : :
1452 : : /**
1453 : : * g_file_query_filesystem_info:
1454 : : * @file: input #GFile
1455 : : * @attributes: an attribute query string
1456 : : * @cancellable: (nullable): optional #GCancellable object,
1457 : : * %NULL to ignore
1458 : : * @error: a #GError
1459 : : *
1460 : : * Similar to g_file_query_info(), but obtains information
1461 : : * about the filesystem the @file is on, rather than the file itself.
1462 : : * For instance the amount of space available and the type of
1463 : : * the filesystem.
1464 : : *
1465 : : * The @attributes value is a string that specifies the attributes
1466 : : * that should be gathered. It is not an error if it's not possible
1467 : : * to read a particular requested attribute from a file - it just
1468 : : * won't be set. @attributes should be a comma-separated list of
1469 : : * attributes or attribute wildcards. The wildcard "*" means all
1470 : : * attributes, and a wildcard like "filesystem::*" means all attributes
1471 : : * in the filesystem namespace. The standard namespace for filesystem
1472 : : * attributes is "filesystem". Common attributes of interest are
1473 : : * %G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1474 : : * in bytes), %G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1475 : : * and %G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1476 : : *
1477 : : * If @cancellable is not %NULL, then the operation can be cancelled
1478 : : * by triggering the cancellable object from another thread. If the
1479 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1480 : : * returned.
1481 : : *
1482 : : * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1483 : : * be returned. Other errors are possible too, and depend on what
1484 : : * kind of filesystem the file is on.
1485 : : *
1486 : : * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1487 : : * Free the returned object with g_object_unref().
1488 : : */
1489 : : GFileInfo *
1490 : 1 : g_file_query_filesystem_info (GFile *file,
1491 : : const char *attributes,
1492 : : GCancellable *cancellable,
1493 : : GError **error)
1494 : : {
1495 : : GFileIface *iface;
1496 : :
1497 : 1 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1498 : :
1499 : 1 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1500 : 0 : return NULL;
1501 : :
1502 : 1 : iface = G_FILE_GET_IFACE (file);
1503 : :
1504 : 1 : if (iface->query_filesystem_info == NULL)
1505 : : {
1506 : 0 : g_set_error_literal (error, G_IO_ERROR,
1507 : : G_IO_ERROR_NOT_SUPPORTED,
1508 : : _("Operation not supported"));
1509 : 0 : return NULL;
1510 : : }
1511 : :
1512 : 1 : return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1513 : : }
1514 : :
1515 : : /**
1516 : : * g_file_query_filesystem_info_async:
1517 : : * @file: input #GFile
1518 : : * @attributes: an attribute query string
1519 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
1520 : : * @cancellable: (nullable): optional #GCancellable object,
1521 : : * %NULL to ignore
1522 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1523 : : * to call when the request is satisfied
1524 : : * @user_data: the data to pass to callback function
1525 : : *
1526 : : * Asynchronously gets the requested information about the filesystem
1527 : : * that the specified @file is on. The result is a #GFileInfo object
1528 : : * that contains key-value attributes (such as type or size for the
1529 : : * file).
1530 : : *
1531 : : * For more details, see g_file_query_filesystem_info() which is the
1532 : : * synchronous version of this call.
1533 : : *
1534 : : * When the operation is finished, @callback will be called. You can
1535 : : * then call g_file_query_info_finish() to get the result of the
1536 : : * operation.
1537 : : */
1538 : : void
1539 : 0 : g_file_query_filesystem_info_async (GFile *file,
1540 : : const char *attributes,
1541 : : int io_priority,
1542 : : GCancellable *cancellable,
1543 : : GAsyncReadyCallback callback,
1544 : : gpointer user_data)
1545 : : {
1546 : : GFileIface *iface;
1547 : :
1548 : 0 : g_return_if_fail (G_IS_FILE (file));
1549 : :
1550 : 0 : iface = G_FILE_GET_IFACE (file);
1551 : 0 : (* iface->query_filesystem_info_async) (file,
1552 : : attributes,
1553 : : io_priority,
1554 : : cancellable,
1555 : : callback,
1556 : : user_data);
1557 : : }
1558 : :
1559 : : /**
1560 : : * g_file_query_filesystem_info_finish:
1561 : : * @file: input #GFile
1562 : : * @res: a #GAsyncResult
1563 : : * @error: a #GError
1564 : : *
1565 : : * Finishes an asynchronous filesystem info query.
1566 : : * See g_file_query_filesystem_info_async().
1567 : : *
1568 : : * Returns: (transfer full): #GFileInfo for given @file
1569 : : * or %NULL on error.
1570 : : * Free the returned object with g_object_unref().
1571 : : */
1572 : : GFileInfo *
1573 : 0 : g_file_query_filesystem_info_finish (GFile *file,
1574 : : GAsyncResult *res,
1575 : : GError **error)
1576 : : {
1577 : : GFileIface *iface;
1578 : :
1579 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1580 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1581 : :
1582 : 0 : if (g_async_result_legacy_propagate_error (res, error))
1583 : 0 : return NULL;
1584 : :
1585 : 0 : iface = G_FILE_GET_IFACE (file);
1586 : 0 : return (* iface->query_filesystem_info_finish) (file, res, error);
1587 : : }
1588 : :
1589 : : /**
1590 : : * g_file_find_enclosing_mount:
1591 : : * @file: input #GFile
1592 : : * @cancellable: (nullable): optional #GCancellable object,
1593 : : * %NULL to ignore
1594 : : * @error: a #GError
1595 : : *
1596 : : * Gets a #GMount for the #GFile.
1597 : : *
1598 : : * #GMount is returned only for user interesting locations, see
1599 : : * #GVolumeMonitor. If the #GFileIface for @file does not have a #mount,
1600 : : * @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL #will be returned.
1601 : : *
1602 : : * If @cancellable is not %NULL, then the operation can be cancelled by
1603 : : * triggering the cancellable object from another thread. If the operation
1604 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1605 : : *
1606 : : * Returns: (transfer full): a #GMount where the @file is located
1607 : : * or %NULL on error.
1608 : : * Free the returned object with g_object_unref().
1609 : : */
1610 : : GMount *
1611 : 0 : g_file_find_enclosing_mount (GFile *file,
1612 : : GCancellable *cancellable,
1613 : : GError **error)
1614 : : {
1615 : : GFileIface *iface;
1616 : :
1617 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1618 : :
1619 : 0 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1620 : 0 : return NULL;
1621 : :
1622 : 0 : iface = G_FILE_GET_IFACE (file);
1623 : 0 : if (iface->find_enclosing_mount == NULL)
1624 : : {
1625 : :
1626 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1627 : : /* Translators: This is an error message when
1628 : : * trying to find the enclosing (user visible)
1629 : : * mount of a file, but none exists.
1630 : : */
1631 : : _("Containing mount does not exist"));
1632 : 0 : return NULL;
1633 : : }
1634 : :
1635 : 0 : return (* iface->find_enclosing_mount) (file, cancellable, error);
1636 : : }
1637 : :
1638 : : /**
1639 : : * g_file_find_enclosing_mount_async:
1640 : : * @file: a #GFile
1641 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
1642 : : * @cancellable: (nullable): optional #GCancellable object,
1643 : : * %NULL to ignore
1644 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
1645 : : * to call when the request is satisfied
1646 : : * @user_data: the data to pass to callback function
1647 : : *
1648 : : * Asynchronously gets the mount for the file.
1649 : : *
1650 : : * For more details, see g_file_find_enclosing_mount() which is
1651 : : * the synchronous version of this call.
1652 : : *
1653 : : * When the operation is finished, @callback will be called.
1654 : : * You can then call g_file_find_enclosing_mount_finish() to
1655 : : * get the result of the operation.
1656 : : */
1657 : : void
1658 : 0 : g_file_find_enclosing_mount_async (GFile *file,
1659 : : int io_priority,
1660 : : GCancellable *cancellable,
1661 : : GAsyncReadyCallback callback,
1662 : : gpointer user_data)
1663 : : {
1664 : : GFileIface *iface;
1665 : :
1666 : 0 : g_return_if_fail (G_IS_FILE (file));
1667 : :
1668 : 0 : iface = G_FILE_GET_IFACE (file);
1669 : 0 : (* iface->find_enclosing_mount_async) (file,
1670 : : io_priority,
1671 : : cancellable,
1672 : : callback,
1673 : : user_data);
1674 : : }
1675 : :
1676 : : /**
1677 : : * g_file_find_enclosing_mount_finish:
1678 : : * @file: a #GFile
1679 : : * @res: a #GAsyncResult
1680 : : * @error: a #GError
1681 : : *
1682 : : * Finishes an asynchronous find mount request.
1683 : : * See g_file_find_enclosing_mount_async().
1684 : : *
1685 : : * Returns: (transfer full): #GMount for given @file or %NULL on error.
1686 : : * Free the returned object with g_object_unref().
1687 : : */
1688 : : GMount *
1689 : 0 : g_file_find_enclosing_mount_finish (GFile *file,
1690 : : GAsyncResult *res,
1691 : : GError **error)
1692 : : {
1693 : : GFileIface *iface;
1694 : :
1695 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1696 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1697 : :
1698 : 0 : if (g_async_result_legacy_propagate_error (res, error))
1699 : 0 : return NULL;
1700 : :
1701 : 0 : iface = G_FILE_GET_IFACE (file);
1702 : 0 : return (* iface->find_enclosing_mount_finish) (file, res, error);
1703 : : }
1704 : :
1705 : :
1706 : : /**
1707 : : * g_file_read: (virtual read_fn)
1708 : : * @file: #GFile to read
1709 : : * @cancellable: (nullable): a #GCancellable
1710 : : * @error: a #GError, or %NULL
1711 : : *
1712 : : * Opens a file for reading. The result is a #GFileInputStream that
1713 : : * can be used to read the contents of the file.
1714 : : *
1715 : : * If @cancellable is not %NULL, then the operation can be cancelled by
1716 : : * triggering the cancellable object from another thread. If the operation
1717 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1718 : : *
1719 : : * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1720 : : * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1721 : : * error will be returned. Other errors are possible too, and depend
1722 : : * on what kind of filesystem the file is on.
1723 : : *
1724 : : * Returns: (transfer full): #GFileInputStream or %NULL on error.
1725 : : * Free the returned object with g_object_unref().
1726 : : */
1727 : : GFileInputStream *
1728 : 143 : g_file_read (GFile *file,
1729 : : GCancellable *cancellable,
1730 : : GError **error)
1731 : : {
1732 : : GFileIface *iface;
1733 : :
1734 : 143 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1735 : :
1736 : 143 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1737 : 0 : return NULL;
1738 : :
1739 : 143 : iface = G_FILE_GET_IFACE (file);
1740 : :
1741 : 143 : if (iface->read_fn == NULL)
1742 : : {
1743 : 0 : g_set_error_literal (error, G_IO_ERROR,
1744 : : G_IO_ERROR_NOT_SUPPORTED,
1745 : : _("Operation not supported"));
1746 : 0 : return NULL;
1747 : : }
1748 : :
1749 : 143 : return (* iface->read_fn) (file, cancellable, error);
1750 : : }
1751 : :
1752 : : /**
1753 : : * g_file_append_to:
1754 : : * @file: input #GFile
1755 : : * @flags: a set of #GFileCreateFlags
1756 : : * @cancellable: (nullable): optional #GCancellable object,
1757 : : * %NULL to ignore
1758 : : * @error: a #GError, or %NULL
1759 : : *
1760 : : * Gets an output stream for appending data to the file.
1761 : : * If the file doesn't already exist it is created.
1762 : : *
1763 : : * By default files created are generally readable by everyone,
1764 : : * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1765 : : * will be made readable only to the current user, to the level that
1766 : : * is supported on the target filesystem.
1767 : : *
1768 : : * If @cancellable is not %NULL, then the operation can be cancelled
1769 : : * by triggering the cancellable object from another thread. If the
1770 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1771 : : * returned.
1772 : : *
1773 : : * Some file systems don't allow all file names, and may return an
1774 : : * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1775 : : * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1776 : : * possible too, and depend on what kind of filesystem the file is on.
1777 : : *
1778 : : * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1779 : : * Free the returned object with g_object_unref().
1780 : : */
1781 : : GFileOutputStream *
1782 : 6 : g_file_append_to (GFile *file,
1783 : : GFileCreateFlags flags,
1784 : : GCancellable *cancellable,
1785 : : GError **error)
1786 : : {
1787 : : GFileIface *iface;
1788 : :
1789 : 6 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1790 : :
1791 : 6 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1792 : 0 : return NULL;
1793 : :
1794 : 6 : iface = G_FILE_GET_IFACE (file);
1795 : :
1796 : 6 : if (iface->append_to == NULL)
1797 : : {
1798 : 0 : g_set_error_literal (error, G_IO_ERROR,
1799 : : G_IO_ERROR_NOT_SUPPORTED,
1800 : : _("Operation not supported"));
1801 : 0 : return NULL;
1802 : : }
1803 : :
1804 : 6 : return (* iface->append_to) (file, flags, cancellable, error);
1805 : : }
1806 : :
1807 : : /**
1808 : : * g_file_create:
1809 : : * @file: input #GFile
1810 : : * @flags: a set of #GFileCreateFlags
1811 : : * @cancellable: (nullable): optional #GCancellable object,
1812 : : * %NULL to ignore
1813 : : * @error: a #GError, or %NULL
1814 : : *
1815 : : * Creates a new file and returns an output stream for writing to it.
1816 : : * The file must not already exist.
1817 : : *
1818 : : * By default files created are generally readable by everyone,
1819 : : * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1820 : : * will be made readable only to the current user, to the level
1821 : : * that is supported on the target filesystem.
1822 : : *
1823 : : * If @cancellable is not %NULL, then the operation can be cancelled
1824 : : * by triggering the cancellable object from another thread. If the
1825 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1826 : : * returned.
1827 : : *
1828 : : * If a file or directory with this name already exists the
1829 : : * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1830 : : * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1831 : : * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1832 : : * be returned. Other errors are possible too, and depend on what kind
1833 : : * of filesystem the file is on.
1834 : : *
1835 : : * Returns: (transfer full): a #GFileOutputStream for the newly created
1836 : : * file, or %NULL on error.
1837 : : * Free the returned object with g_object_unref().
1838 : : */
1839 : : GFileOutputStream *
1840 : 13 : g_file_create (GFile *file,
1841 : : GFileCreateFlags flags,
1842 : : GCancellable *cancellable,
1843 : : GError **error)
1844 : : {
1845 : : GFileIface *iface;
1846 : :
1847 : 13 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1848 : :
1849 : 13 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1850 : 0 : return NULL;
1851 : :
1852 : 13 : iface = G_FILE_GET_IFACE (file);
1853 : :
1854 : 13 : if (iface->create == NULL)
1855 : : {
1856 : 0 : g_set_error_literal (error, G_IO_ERROR,
1857 : : G_IO_ERROR_NOT_SUPPORTED,
1858 : : _("Operation not supported"));
1859 : 0 : return NULL;
1860 : : }
1861 : :
1862 : 13 : return (* iface->create) (file, flags, cancellable, error);
1863 : : }
1864 : :
1865 : : /**
1866 : : * g_file_replace:
1867 : : * @file: input #GFile
1868 : : * @etag: (nullable): an optional [entity tag](#entity-tags)
1869 : : * for the current #GFile, or #NULL to ignore
1870 : : * @make_backup: %TRUE if a backup should be created
1871 : : * @flags: a set of #GFileCreateFlags
1872 : : * @cancellable: (nullable): optional #GCancellable object,
1873 : : * %NULL to ignore
1874 : : * @error: a #GError, or %NULL
1875 : : *
1876 : : * Returns an output stream for overwriting the file, possibly
1877 : : * creating a backup copy of the file first. If the file doesn't exist,
1878 : : * it will be created.
1879 : : *
1880 : : * This will try to replace the file in the safest way possible so
1881 : : * that any errors during the writing will not affect an already
1882 : : * existing copy of the file. For instance, for local files it
1883 : : * may write to a temporary file and then atomically rename over
1884 : : * the destination when the stream is closed.
1885 : : *
1886 : : * By default files created are generally readable by everyone,
1887 : : * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
1888 : : * will be made readable only to the current user, to the level that
1889 : : * is supported on the target filesystem.
1890 : : *
1891 : : * If @cancellable is not %NULL, then the operation can be cancelled
1892 : : * by triggering the cancellable object from another thread. If the
1893 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1894 : : * returned.
1895 : : *
1896 : : * If you pass in a non-%NULL @etag value and @file already exists, then
1897 : : * this value is compared to the current entity tag of the file, and if
1898 : : * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1899 : : * generally means that the file has been changed since you last read
1900 : : * it. You can get the new etag from g_file_output_stream_get_etag()
1901 : : * after you've finished writing and closed the #GFileOutputStream. When
1902 : : * you load a new file you can use g_file_input_stream_query_info() to
1903 : : * get the etag of the file.
1904 : : *
1905 : : * If @make_backup is %TRUE, this function will attempt to make a
1906 : : * backup of the current file before overwriting it. If this fails
1907 : : * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1908 : : * want to replace anyway, try again with @make_backup set to %FALSE.
1909 : : *
1910 : : * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1911 : : * be returned, and if the file is some other form of non-regular file
1912 : : * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1913 : : * file systems don't allow all file names, and may return an
1914 : : * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1915 : : * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1916 : : * possible too, and depend on what kind of filesystem the file is on.
1917 : : *
1918 : : * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1919 : : * Free the returned object with g_object_unref().
1920 : : */
1921 : : GFileOutputStream *
1922 : 150 : g_file_replace (GFile *file,
1923 : : const char *etag,
1924 : : gboolean make_backup,
1925 : : GFileCreateFlags flags,
1926 : : GCancellable *cancellable,
1927 : : GError **error)
1928 : : {
1929 : : GFileIface *iface;
1930 : :
1931 : 150 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1932 : :
1933 : 150 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1934 : 0 : return NULL;
1935 : :
1936 : 150 : iface = G_FILE_GET_IFACE (file);
1937 : :
1938 : 150 : if (iface->replace == NULL)
1939 : : {
1940 : 0 : g_set_error_literal (error, G_IO_ERROR,
1941 : : G_IO_ERROR_NOT_SUPPORTED,
1942 : : _("Operation not supported"));
1943 : 0 : return NULL;
1944 : : }
1945 : :
1946 : : /* Handle empty tag string as NULL in consistent way. */
1947 : 150 : if (etag && *etag == 0)
1948 : 0 : etag = NULL;
1949 : :
1950 : 150 : return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1951 : : }
1952 : :
1953 : : /**
1954 : : * g_file_open_readwrite:
1955 : : * @file: #GFile to open
1956 : : * @cancellable: (nullable): a #GCancellable
1957 : : * @error: a #GError, or %NULL
1958 : : *
1959 : : * Opens an existing file for reading and writing. The result is
1960 : : * a #GFileIOStream that can be used to read and write the contents
1961 : : * of the file.
1962 : : *
1963 : : * If @cancellable is not %NULL, then the operation can be cancelled
1964 : : * by triggering the cancellable object from another thread. If the
1965 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1966 : : * returned.
1967 : : *
1968 : : * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1969 : : * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1970 : : * error will be returned. Other errors are possible too, and depend on
1971 : : * what kind of filesystem the file is on. Note that in many non-local
1972 : : * file cases read and write streams are not supported, so make sure you
1973 : : * really need to do read and write streaming, rather than just opening
1974 : : * for reading or writing.
1975 : : *
1976 : : * Returns: (transfer full): #GFileIOStream or %NULL on error.
1977 : : * Free the returned object with g_object_unref().
1978 : : *
1979 : : * Since: 2.22
1980 : : */
1981 : : GFileIOStream *
1982 : 3 : g_file_open_readwrite (GFile *file,
1983 : : GCancellable *cancellable,
1984 : : GError **error)
1985 : : {
1986 : : GFileIface *iface;
1987 : :
1988 : 3 : g_return_val_if_fail (G_IS_FILE (file), NULL);
1989 : :
1990 : 3 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
1991 : 0 : return NULL;
1992 : :
1993 : 3 : iface = G_FILE_GET_IFACE (file);
1994 : :
1995 : 3 : if (iface->open_readwrite == NULL)
1996 : : {
1997 : 0 : g_set_error_literal (error, G_IO_ERROR,
1998 : : G_IO_ERROR_NOT_SUPPORTED,
1999 : : _("Operation not supported"));
2000 : 0 : return NULL;
2001 : : }
2002 : :
2003 : 3 : return (* iface->open_readwrite) (file, cancellable, error);
2004 : : }
2005 : :
2006 : : /**
2007 : : * g_file_create_readwrite:
2008 : : * @file: a #GFile
2009 : : * @flags: a set of #GFileCreateFlags
2010 : : * @cancellable: (nullable): optional #GCancellable object,
2011 : : * %NULL to ignore
2012 : : * @error: return location for a #GError, or %NULL
2013 : : *
2014 : : * Creates a new file and returns a stream for reading and
2015 : : * writing to it. The file must not already exist.
2016 : : *
2017 : : * By default files created are generally readable by everyone,
2018 : : * but if you pass %G_FILE_CREATE_PRIVATE in @flags the file
2019 : : * will be made readable only to the current user, to the level
2020 : : * that is supported on the target filesystem.
2021 : : *
2022 : : * If @cancellable is not %NULL, then the operation can be cancelled
2023 : : * by triggering the cancellable object from another thread. If the
2024 : : * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
2025 : : * returned.
2026 : : *
2027 : : * If a file or directory with this name already exists, the
2028 : : * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
2029 : : * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
2030 : : * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
2031 : : * will be returned. Other errors are possible too, and depend on what
2032 : : * kind of filesystem the file is on.
2033 : : *
2034 : : * Note that in many non-local file cases read and write streams are
2035 : : * not supported, so make sure you really need to do read and write
2036 : : * streaming, rather than just opening for reading or writing.
2037 : : *
2038 : : * Returns: (transfer full): a #GFileIOStream for the newly created
2039 : : * file, or %NULL on error.
2040 : : * Free the returned object with g_object_unref().
2041 : : *
2042 : : * Since: 2.22
2043 : : */
2044 : : GFileIOStream *
2045 : 11 : g_file_create_readwrite (GFile *file,
2046 : : GFileCreateFlags flags,
2047 : : GCancellable *cancellable,
2048 : : GError **error)
2049 : : {
2050 : : GFileIface *iface;
2051 : :
2052 : 11 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2053 : :
2054 : 11 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
2055 : 0 : return NULL;
2056 : :
2057 : 11 : iface = G_FILE_GET_IFACE (file);
2058 : :
2059 : 11 : if (iface->create_readwrite == NULL)
2060 : : {
2061 : 0 : g_set_error_literal (error, G_IO_ERROR,
2062 : : G_IO_ERROR_NOT_SUPPORTED,
2063 : : _("Operation not supported"));
2064 : 0 : return NULL;
2065 : : }
2066 : :
2067 : 11 : return (* iface->create_readwrite) (file, flags, cancellable, error);
2068 : : }
2069 : :
2070 : : /**
2071 : : * g_file_replace_readwrite:
2072 : : * @file: a #GFile
2073 : : * @etag: (nullable): an optional [entity tag](#entity-tags)
2074 : : * for the current #GFile, or #NULL to ignore
2075 : : * @make_backup: %TRUE if a backup should be created
2076 : : * @flags: a set of #GFileCreateFlags
2077 : : * @cancellable: (nullable): optional #GCancellable object,
2078 : : * %NULL to ignore
2079 : : * @error: return location for a #GError, or %NULL
2080 : : *
2081 : : * Returns an output stream for overwriting the file in readwrite mode,
2082 : : * possibly creating a backup copy of the file first. If the file doesn't
2083 : : * exist, it will be created.
2084 : : *
2085 : : * For details about the behaviour, see g_file_replace() which does the
2086 : : * same thing but returns an output stream only.
2087 : : *
2088 : : * Note that in many non-local file cases read and write streams are not
2089 : : * supported, so make sure you really need to do read and write streaming,
2090 : : * rather than just opening for reading or writing.
2091 : : *
2092 : : * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2093 : : * Free the returned object with g_object_unref().
2094 : : *
2095 : : * Since: 2.22
2096 : : */
2097 : : GFileIOStream *
2098 : 53 : g_file_replace_readwrite (GFile *file,
2099 : : const char *etag,
2100 : : gboolean make_backup,
2101 : : GFileCreateFlags flags,
2102 : : GCancellable *cancellable,
2103 : : GError **error)
2104 : : {
2105 : : GFileIface *iface;
2106 : :
2107 : 53 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2108 : :
2109 : 53 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
2110 : 0 : return NULL;
2111 : :
2112 : 53 : iface = G_FILE_GET_IFACE (file);
2113 : :
2114 : 53 : if (iface->replace_readwrite == NULL)
2115 : : {
2116 : 0 : g_set_error_literal (error, G_IO_ERROR,
2117 : : G_IO_ERROR_NOT_SUPPORTED,
2118 : : _("Operation not supported"));
2119 : 0 : return NULL;
2120 : : }
2121 : :
2122 : 53 : return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2123 : : }
2124 : :
2125 : : /**
2126 : : * g_file_read_async:
2127 : : * @file: input #GFile
2128 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2129 : : * @cancellable: (nullable): optional #GCancellable object,
2130 : : * %NULL to ignore
2131 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2132 : : * to call when the request is satisfied
2133 : : * @user_data: the data to pass to callback function
2134 : : *
2135 : : * Asynchronously opens @file for reading.
2136 : : *
2137 : : * For more details, see g_file_read() which is
2138 : : * the synchronous version of this call.
2139 : : *
2140 : : * When the operation is finished, @callback will be called.
2141 : : * You can then call g_file_read_finish() to get the result
2142 : : * of the operation.
2143 : : */
2144 : : void
2145 : 11 : g_file_read_async (GFile *file,
2146 : : int io_priority,
2147 : : GCancellable *cancellable,
2148 : : GAsyncReadyCallback callback,
2149 : : gpointer user_data)
2150 : : {
2151 : : GFileIface *iface;
2152 : :
2153 : 11 : g_return_if_fail (G_IS_FILE (file));
2154 : :
2155 : 11 : iface = G_FILE_GET_IFACE (file);
2156 : 11 : (* iface->read_async) (file,
2157 : : io_priority,
2158 : : cancellable,
2159 : : callback,
2160 : : user_data);
2161 : : }
2162 : :
2163 : : /**
2164 : : * g_file_read_finish:
2165 : : * @file: input #GFile
2166 : : * @res: a #GAsyncResult
2167 : : * @error: a #GError, or %NULL
2168 : : *
2169 : : * Finishes an asynchronous file read operation started with
2170 : : * g_file_read_async().
2171 : : *
2172 : : * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2173 : : * Free the returned object with g_object_unref().
2174 : : */
2175 : : GFileInputStream *
2176 : 11 : g_file_read_finish (GFile *file,
2177 : : GAsyncResult *res,
2178 : : GError **error)
2179 : : {
2180 : : GFileIface *iface;
2181 : :
2182 : 11 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2183 : 11 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2184 : :
2185 : 11 : if (g_async_result_legacy_propagate_error (res, error))
2186 : 0 : return NULL;
2187 : :
2188 : 11 : iface = G_FILE_GET_IFACE (file);
2189 : 11 : return (* iface->read_finish) (file, res, error);
2190 : : }
2191 : :
2192 : : /**
2193 : : * g_file_append_to_async:
2194 : : * @file: input #GFile
2195 : : * @flags: a set of #GFileCreateFlags
2196 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2197 : : * @cancellable: (nullable): optional #GCancellable object,
2198 : : * %NULL to ignore
2199 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2200 : : * to call when the request is satisfied
2201 : : * @user_data: the data to pass to callback function
2202 : : *
2203 : : * Asynchronously opens @file for appending.
2204 : : *
2205 : : * For more details, see g_file_append_to() which is
2206 : : * the synchronous version of this call.
2207 : : *
2208 : : * When the operation is finished, @callback will be called.
2209 : : * You can then call g_file_append_to_finish() to get the result
2210 : : * of the operation.
2211 : : */
2212 : : void
2213 : 0 : g_file_append_to_async (GFile *file,
2214 : : GFileCreateFlags flags,
2215 : : int io_priority,
2216 : : GCancellable *cancellable,
2217 : : GAsyncReadyCallback callback,
2218 : : gpointer user_data)
2219 : : {
2220 : : GFileIface *iface;
2221 : :
2222 : 0 : g_return_if_fail (G_IS_FILE (file));
2223 : :
2224 : 0 : iface = G_FILE_GET_IFACE (file);
2225 : 0 : (* iface->append_to_async) (file,
2226 : : flags,
2227 : : io_priority,
2228 : : cancellable,
2229 : : callback,
2230 : : user_data);
2231 : : }
2232 : :
2233 : : /**
2234 : : * g_file_append_to_finish:
2235 : : * @file: input #GFile
2236 : : * @res: #GAsyncResult
2237 : : * @error: a #GError, or %NULL
2238 : : *
2239 : : * Finishes an asynchronous file append operation started with
2240 : : * g_file_append_to_async().
2241 : : *
2242 : : * Returns: (transfer full): a valid #GFileOutputStream
2243 : : * or %NULL on error.
2244 : : * Free the returned object with g_object_unref().
2245 : : */
2246 : : GFileOutputStream *
2247 : 0 : g_file_append_to_finish (GFile *file,
2248 : : GAsyncResult *res,
2249 : : GError **error)
2250 : : {
2251 : : GFileIface *iface;
2252 : :
2253 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2254 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2255 : :
2256 : 0 : if (g_async_result_legacy_propagate_error (res, error))
2257 : 0 : return NULL;
2258 : :
2259 : 0 : iface = G_FILE_GET_IFACE (file);
2260 : 0 : return (* iface->append_to_finish) (file, res, error);
2261 : : }
2262 : :
2263 : : /**
2264 : : * g_file_create_async:
2265 : : * @file: input #GFile
2266 : : * @flags: a set of #GFileCreateFlags
2267 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2268 : : * @cancellable: (nullable): optional #GCancellable object,
2269 : : * %NULL to ignore
2270 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2271 : : * to call when the request is satisfied
2272 : : * @user_data: the data to pass to callback function
2273 : : *
2274 : : * Asynchronously creates a new file and returns an output stream
2275 : : * for writing to it. The file must not already exist.
2276 : : *
2277 : : * For more details, see g_file_create() which is
2278 : : * the synchronous version of this call.
2279 : : *
2280 : : * When the operation is finished, @callback will be called.
2281 : : * You can then call g_file_create_finish() to get the result
2282 : : * of the operation.
2283 : : */
2284 : : void
2285 : 5 : g_file_create_async (GFile *file,
2286 : : GFileCreateFlags flags,
2287 : : int io_priority,
2288 : : GCancellable *cancellable,
2289 : : GAsyncReadyCallback callback,
2290 : : gpointer user_data)
2291 : : {
2292 : : GFileIface *iface;
2293 : :
2294 : 5 : g_return_if_fail (G_IS_FILE (file));
2295 : :
2296 : 5 : iface = G_FILE_GET_IFACE (file);
2297 : 5 : (* iface->create_async) (file,
2298 : : flags,
2299 : : io_priority,
2300 : : cancellable,
2301 : : callback,
2302 : : user_data);
2303 : : }
2304 : :
2305 : : /**
2306 : : * g_file_create_finish:
2307 : : * @file: input #GFile
2308 : : * @res: a #GAsyncResult
2309 : : * @error: a #GError, or %NULL
2310 : : *
2311 : : * Finishes an asynchronous file create operation started with
2312 : : * g_file_create_async().
2313 : : *
2314 : : * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2315 : : * Free the returned object with g_object_unref().
2316 : : */
2317 : : GFileOutputStream *
2318 : 5 : g_file_create_finish (GFile *file,
2319 : : GAsyncResult *res,
2320 : : GError **error)
2321 : : {
2322 : : GFileIface *iface;
2323 : :
2324 : 5 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2325 : 5 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2326 : :
2327 : 5 : if (g_async_result_legacy_propagate_error (res, error))
2328 : 0 : return NULL;
2329 : :
2330 : 5 : iface = G_FILE_GET_IFACE (file);
2331 : 5 : return (* iface->create_finish) (file, res, error);
2332 : : }
2333 : :
2334 : : /**
2335 : : * g_file_replace_async:
2336 : : * @file: input #GFile
2337 : : * @etag: (nullable): an [entity tag](#entity-tags) for the current #GFile,
2338 : : * or %NULL to ignore
2339 : : * @make_backup: %TRUE if a backup should be created
2340 : : * @flags: a set of #GFileCreateFlags
2341 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2342 : : * @cancellable: (nullable): optional #GCancellable object,
2343 : : * %NULL to ignore
2344 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2345 : : * to call when the request is satisfied
2346 : : * @user_data: the data to pass to callback function
2347 : : *
2348 : : * Asynchronously overwrites the file, replacing the contents,
2349 : : * possibly creating a backup copy of the file first.
2350 : : *
2351 : : * For more details, see g_file_replace() which is
2352 : : * the synchronous version of this call.
2353 : : *
2354 : : * When the operation is finished, @callback will be called.
2355 : : * You can then call g_file_replace_finish() to get the result
2356 : : * of the operation.
2357 : : */
2358 : : void
2359 : 2 : g_file_replace_async (GFile *file,
2360 : : const char *etag,
2361 : : gboolean make_backup,
2362 : : GFileCreateFlags flags,
2363 : : int io_priority,
2364 : : GCancellable *cancellable,
2365 : : GAsyncReadyCallback callback,
2366 : : gpointer user_data)
2367 : : {
2368 : : GFileIface *iface;
2369 : :
2370 : 2 : g_return_if_fail (G_IS_FILE (file));
2371 : :
2372 : 2 : iface = G_FILE_GET_IFACE (file);
2373 : 2 : (* iface->replace_async) (file,
2374 : : etag,
2375 : : make_backup,
2376 : : flags,
2377 : : io_priority,
2378 : : cancellable,
2379 : : callback,
2380 : : user_data);
2381 : : }
2382 : :
2383 : : /**
2384 : : * g_file_replace_finish:
2385 : : * @file: input #GFile
2386 : : * @res: a #GAsyncResult
2387 : : * @error: a #GError, or %NULL
2388 : : *
2389 : : * Finishes an asynchronous file replace operation started with
2390 : : * g_file_replace_async().
2391 : : *
2392 : : * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2393 : : * Free the returned object with g_object_unref().
2394 : : */
2395 : : GFileOutputStream *
2396 : 2 : g_file_replace_finish (GFile *file,
2397 : : GAsyncResult *res,
2398 : : GError **error)
2399 : : {
2400 : : GFileIface *iface;
2401 : :
2402 : 2 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2403 : 2 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2404 : :
2405 : 2 : if (g_async_result_legacy_propagate_error (res, error))
2406 : 0 : return NULL;
2407 : :
2408 : 2 : iface = G_FILE_GET_IFACE (file);
2409 : 2 : return (* iface->replace_finish) (file, res, error);
2410 : : }
2411 : :
2412 : : /**
2413 : : * g_file_open_readwrite_async
2414 : : * @file: input #GFile
2415 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2416 : : * @cancellable: (nullable): optional #GCancellable object,
2417 : : * %NULL to ignore
2418 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2419 : : * to call when the request is satisfied
2420 : : * @user_data: the data to pass to callback function
2421 : : *
2422 : : * Asynchronously opens @file for reading and writing.
2423 : : *
2424 : : * For more details, see g_file_open_readwrite() which is
2425 : : * the synchronous version of this call.
2426 : : *
2427 : : * When the operation is finished, @callback will be called.
2428 : : * You can then call g_file_open_readwrite_finish() to get
2429 : : * the result of the operation.
2430 : : *
2431 : : * Since: 2.22
2432 : : */
2433 : : void
2434 : 0 : g_file_open_readwrite_async (GFile *file,
2435 : : int io_priority,
2436 : : GCancellable *cancellable,
2437 : : GAsyncReadyCallback callback,
2438 : : gpointer user_data)
2439 : : {
2440 : : GFileIface *iface;
2441 : :
2442 : 0 : g_return_if_fail (G_IS_FILE (file));
2443 : :
2444 : 0 : iface = G_FILE_GET_IFACE (file);
2445 : 0 : (* iface->open_readwrite_async) (file,
2446 : : io_priority,
2447 : : cancellable,
2448 : : callback,
2449 : : user_data);
2450 : : }
2451 : :
2452 : : /**
2453 : : * g_file_open_readwrite_finish:
2454 : : * @file: input #GFile
2455 : : * @res: a #GAsyncResult
2456 : : * @error: a #GError, or %NULL
2457 : : *
2458 : : * Finishes an asynchronous file read operation started with
2459 : : * g_file_open_readwrite_async().
2460 : : *
2461 : : * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2462 : : * Free the returned object with g_object_unref().
2463 : : *
2464 : : * Since: 2.22
2465 : : */
2466 : : GFileIOStream *
2467 : 0 : g_file_open_readwrite_finish (GFile *file,
2468 : : GAsyncResult *res,
2469 : : GError **error)
2470 : : {
2471 : : GFileIface *iface;
2472 : :
2473 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2474 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2475 : :
2476 : 0 : if (g_async_result_legacy_propagate_error (res, error))
2477 : 0 : return NULL;
2478 : :
2479 : 0 : iface = G_FILE_GET_IFACE (file);
2480 : 0 : return (* iface->open_readwrite_finish) (file, res, error);
2481 : : }
2482 : :
2483 : : /**
2484 : : * g_file_create_readwrite_async:
2485 : : * @file: input #GFile
2486 : : * @flags: a set of #GFileCreateFlags
2487 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2488 : : * @cancellable: (nullable): optional #GCancellable object,
2489 : : * %NULL to ignore
2490 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2491 : : * to call when the request is satisfied
2492 : : * @user_data: the data to pass to callback function
2493 : : *
2494 : : * Asynchronously creates a new file and returns a stream
2495 : : * for reading and writing to it. The file must not already exist.
2496 : : *
2497 : : * For more details, see g_file_create_readwrite() which is
2498 : : * the synchronous version of this call.
2499 : : *
2500 : : * When the operation is finished, @callback will be called.
2501 : : * You can then call g_file_create_readwrite_finish() to get
2502 : : * the result of the operation.
2503 : : *
2504 : : * Since: 2.22
2505 : : */
2506 : : void
2507 : 0 : g_file_create_readwrite_async (GFile *file,
2508 : : GFileCreateFlags flags,
2509 : : int io_priority,
2510 : : GCancellable *cancellable,
2511 : : GAsyncReadyCallback callback,
2512 : : gpointer user_data)
2513 : : {
2514 : : GFileIface *iface;
2515 : :
2516 : 0 : g_return_if_fail (G_IS_FILE (file));
2517 : :
2518 : 0 : iface = G_FILE_GET_IFACE (file);
2519 : 0 : (* iface->create_readwrite_async) (file,
2520 : : flags,
2521 : : io_priority,
2522 : : cancellable,
2523 : : callback,
2524 : : user_data);
2525 : : }
2526 : :
2527 : : /**
2528 : : * g_file_create_readwrite_finish:
2529 : : * @file: input #GFile
2530 : : * @res: a #GAsyncResult
2531 : : * @error: a #GError, or %NULL
2532 : : *
2533 : : * Finishes an asynchronous file create operation started with
2534 : : * g_file_create_readwrite_async().
2535 : : *
2536 : : * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2537 : : * Free the returned object with g_object_unref().
2538 : : *
2539 : : * Since: 2.22
2540 : : */
2541 : : GFileIOStream *
2542 : 0 : g_file_create_readwrite_finish (GFile *file,
2543 : : GAsyncResult *res,
2544 : : GError **error)
2545 : : {
2546 : : GFileIface *iface;
2547 : :
2548 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2549 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2550 : :
2551 : 0 : if (g_async_result_legacy_propagate_error (res, error))
2552 : 0 : return NULL;
2553 : :
2554 : 0 : iface = G_FILE_GET_IFACE (file);
2555 : 0 : return (* iface->create_readwrite_finish) (file, res, error);
2556 : : }
2557 : :
2558 : : /**
2559 : : * g_file_replace_readwrite_async:
2560 : : * @file: input #GFile
2561 : : * @etag: (nullable): an [entity tag](#entity-tags) for the current #GFile,
2562 : : * or %NULL to ignore
2563 : : * @make_backup: %TRUE if a backup should be created
2564 : : * @flags: a set of #GFileCreateFlags
2565 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
2566 : : * @cancellable: (nullable): optional #GCancellable object,
2567 : : * %NULL to ignore
2568 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
2569 : : * to call when the request is satisfied
2570 : : * @user_data: the data to pass to callback function
2571 : : *
2572 : : * Asynchronously overwrites the file in read-write mode,
2573 : : * replacing the contents, possibly creating a backup copy
2574 : : * of the file first.
2575 : : *
2576 : : * For more details, see g_file_replace_readwrite() which is
2577 : : * the synchronous version of this call.
2578 : : *
2579 : : * When the operation is finished, @callback will be called.
2580 : : * You can then call g_file_replace_readwrite_finish() to get
2581 : : * the result of the operation.
2582 : : *
2583 : : * Since: 2.22
2584 : : */
2585 : : void
2586 : 0 : g_file_replace_readwrite_async (GFile *file,
2587 : : const char *etag,
2588 : : gboolean make_backup,
2589 : : GFileCreateFlags flags,
2590 : : int io_priority,
2591 : : GCancellable *cancellable,
2592 : : GAsyncReadyCallback callback,
2593 : : gpointer user_data)
2594 : : {
2595 : : GFileIface *iface;
2596 : :
2597 : 0 : g_return_if_fail (G_IS_FILE (file));
2598 : :
2599 : 0 : iface = G_FILE_GET_IFACE (file);
2600 : 0 : (* iface->replace_readwrite_async) (file,
2601 : : etag,
2602 : : make_backup,
2603 : : flags,
2604 : : io_priority,
2605 : : cancellable,
2606 : : callback,
2607 : : user_data);
2608 : : }
2609 : :
2610 : : /**
2611 : : * g_file_replace_readwrite_finish:
2612 : : * @file: input #GFile
2613 : : * @res: a #GAsyncResult
2614 : : * @error: a #GError, or %NULL
2615 : : *
2616 : : * Finishes an asynchronous file replace operation started with
2617 : : * g_file_replace_readwrite_async().
2618 : : *
2619 : : * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2620 : : * Free the returned object with g_object_unref().
2621 : : *
2622 : : * Since: 2.22
2623 : : */
2624 : : GFileIOStream *
2625 : 0 : g_file_replace_readwrite_finish (GFile *file,
2626 : : GAsyncResult *res,
2627 : : GError **error)
2628 : : {
2629 : : GFileIface *iface;
2630 : :
2631 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2632 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2633 : :
2634 : 0 : if (g_async_result_legacy_propagate_error (res, error))
2635 : 0 : return NULL;
2636 : :
2637 : 0 : iface = G_FILE_GET_IFACE (file);
2638 : 0 : return (* iface->replace_readwrite_finish) (file, res, error);
2639 : : }
2640 : :
2641 : : static gboolean
2642 : 12 : copy_symlink (GFile *destination,
2643 : : GFileCopyFlags flags,
2644 : : GCancellable *cancellable,
2645 : : const char *target,
2646 : : GError **error)
2647 : : {
2648 : : GError *my_error;
2649 : : gboolean tried_delete;
2650 : : GFileInfo *info;
2651 : : GFileType file_type;
2652 : :
2653 : 12 : tried_delete = FALSE;
2654 : :
2655 : 12 : retry:
2656 : 12 : my_error = NULL;
2657 : 12 : if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2658 : : {
2659 : : /* Maybe it already existed, and we want to overwrite? */
2660 : 10 : if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2661 : 0 : my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2662 : : {
2663 : 0 : g_clear_error (&my_error);
2664 : :
2665 : : /* Don't overwrite if the destination is a directory */
2666 : 0 : info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2667 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2668 : : cancellable, &my_error);
2669 : 0 : if (info != NULL)
2670 : : {
2671 : 0 : file_type = g_file_info_get_file_type (info);
2672 : 0 : g_object_unref (info);
2673 : :
2674 : 0 : if (file_type == G_FILE_TYPE_DIRECTORY)
2675 : : {
2676 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2677 : : _("Can’t copy over directory"));
2678 : 0 : return FALSE;
2679 : : }
2680 : : }
2681 : :
2682 : 0 : if (!g_file_delete (destination, cancellable, error))
2683 : 0 : return FALSE;
2684 : :
2685 : 0 : tried_delete = TRUE;
2686 : 0 : goto retry;
2687 : : }
2688 : : /* Nah, fail */
2689 : 10 : g_propagate_error (error, my_error);
2690 : 10 : return FALSE;
2691 : : }
2692 : :
2693 : 2 : return TRUE;
2694 : : }
2695 : :
2696 : : static GFileInputStream *
2697 : 76 : open_source_for_copy (GFile *source,
2698 : : GFile *destination,
2699 : : GFileCopyFlags flags,
2700 : : GCancellable *cancellable,
2701 : : GError **error)
2702 : : {
2703 : : GError *my_error;
2704 : : GFileInputStream *ret;
2705 : : GFileInfo *info;
2706 : : GFileType file_type;
2707 : :
2708 : 76 : my_error = NULL;
2709 : 76 : ret = g_file_read (source, cancellable, &my_error);
2710 : 76 : if (ret != NULL)
2711 : 64 : return ret;
2712 : :
2713 : : /* There was an error opening the source, try to set a good error for it: */
2714 : 12 : if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2715 : : {
2716 : : /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2717 : : * as that is less useful to the app. Better check for errors on the
2718 : : * target instead.
2719 : : */
2720 : 12 : g_error_free (my_error);
2721 : 12 : my_error = NULL;
2722 : :
2723 : 12 : info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2724 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2725 : : cancellable, &my_error);
2726 : 16 : if (info != NULL &&
2727 : 4 : g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2728 : : {
2729 : 2 : file_type = g_file_info_get_file_type (info);
2730 : 2 : g_object_unref (info);
2731 : :
2732 : 2 : if (flags & G_FILE_COPY_OVERWRITE)
2733 : : {
2734 : 0 : if (file_type == G_FILE_TYPE_DIRECTORY)
2735 : : {
2736 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2737 : : _("Can’t copy directory over directory"));
2738 : 0 : return NULL;
2739 : : }
2740 : : /* continue to would_recurse error */
2741 : : }
2742 : : else
2743 : : {
2744 : 2 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2745 : : _("Target file exists"));
2746 : 2 : return NULL;
2747 : : }
2748 : : }
2749 : : else
2750 : : {
2751 : : /* Error getting info from target, return that error
2752 : : * (except for NOT_FOUND, which is no error here)
2753 : : */
2754 : 10 : g_clear_object (&info);
2755 : 10 : if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2756 : : {
2757 : 2 : g_propagate_error (error, my_error);
2758 : 2 : return NULL;
2759 : : }
2760 : 8 : g_clear_error (&my_error);
2761 : : }
2762 : :
2763 : 8 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2764 : : _("Can’t recursively copy directory"));
2765 : 8 : return NULL;
2766 : : }
2767 : :
2768 : 0 : g_propagate_error (error, my_error);
2769 : 0 : return NULL;
2770 : : }
2771 : :
2772 : : static gboolean
2773 : 852 : should_copy (GFileAttributeInfo *info,
2774 : : gboolean copy_all_attributes,
2775 : : gboolean skip_perms,
2776 : : gboolean skip_modified_time)
2777 : : {
2778 : 852 : if ((skip_perms && strcmp(info->name, "unix::mode") == 0) ||
2779 : 57 : (skip_modified_time && strncmp(info->name, "time::modified", 14) == 0))
2780 : 27 : return FALSE;
2781 : :
2782 : 825 : if (copy_all_attributes)
2783 : 95 : return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2784 : 730 : return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2785 : : }
2786 : :
2787 : : /**
2788 : : * g_file_build_attribute_list_for_copy:
2789 : : * @file: a #GFile to copy attributes to
2790 : : * @flags: a set of #GFileCopyFlags
2791 : : * @cancellable: (nullable): optional #GCancellable object,
2792 : : * %NULL to ignore
2793 : : * @error: a #GError, %NULL to ignore
2794 : : *
2795 : : * Prepares the file attribute query string for copying to @file.
2796 : : *
2797 : : * This function prepares an attribute query string to be
2798 : : * passed to g_file_query_info() to get a list of attributes
2799 : : * normally copied with the file (see g_file_copy_attributes()
2800 : : * for the detailed description). This function is used by the
2801 : : * implementation of g_file_copy_attributes() and is useful
2802 : : * when one needs to query and set the attributes in two
2803 : : * stages (e.g., for recursive move of a directory).
2804 : : *
2805 : : * Returns: an attribute query string for g_file_query_info(),
2806 : : * or %NULL if an error occurs.
2807 : : *
2808 : : * Since: 2.68
2809 : : */
2810 : : char *
2811 : 71 : g_file_build_attribute_list_for_copy (GFile *file,
2812 : : GFileCopyFlags flags,
2813 : : GCancellable *cancellable,
2814 : : GError **error)
2815 : : {
2816 : 71 : char *ret = NULL;
2817 : 71 : GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2818 : 71 : GString *s = NULL;
2819 : : gboolean first;
2820 : : int i;
2821 : : gboolean copy_all_attributes;
2822 : : gboolean skip_perms;
2823 : : gboolean skip_modified_time;
2824 : :
2825 : 71 : g_return_val_if_fail (G_IS_FILE (file), NULL);
2826 : 71 : g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
2827 : 71 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2828 : :
2829 : 71 : copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2830 : 71 : skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2831 : 71 : skip_modified_time = (flags & G_FILE_COPY_TARGET_DEFAULT_MODIFIED_TIME) != 0;
2832 : :
2833 : : /* Ignore errors here, if the target supports no attributes there is
2834 : : * nothing to copy. We still honor the cancellable though.
2835 : : */
2836 : 71 : attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2837 : 71 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
2838 : 0 : goto out;
2839 : :
2840 : 71 : namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2841 : 71 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
2842 : 0 : goto out;
2843 : :
2844 : 71 : if (attributes == NULL && namespaces == NULL)
2845 : 0 : goto out;
2846 : :
2847 : 71 : first = TRUE;
2848 : 71 : s = g_string_new ("");
2849 : :
2850 : : /* Always query the source file size, even though we can’t set that on the
2851 : : * destination. This is useful for the copy functions. */
2852 : 71 : first = FALSE;
2853 : 71 : g_string_append (s, G_FILE_ATTRIBUTE_STANDARD_SIZE);
2854 : :
2855 : 71 : if (attributes)
2856 : : {
2857 : 781 : for (i = 0; i < attributes->n_infos; i++)
2858 : : {
2859 : 710 : if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms, skip_modified_time))
2860 : : {
2861 : 302 : if (first)
2862 : 0 : first = FALSE;
2863 : : else
2864 : : g_string_append_c (s, ',');
2865 : :
2866 : 302 : g_string_append (s, attributes->infos[i].name);
2867 : : }
2868 : : }
2869 : : }
2870 : :
2871 : 71 : if (namespaces)
2872 : : {
2873 : 213 : for (i = 0; i < namespaces->n_infos; i++)
2874 : : {
2875 : 142 : if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE, FALSE))
2876 : : {
2877 : 80 : if (first)
2878 : 0 : first = FALSE;
2879 : : else
2880 : : g_string_append_c (s, ',');
2881 : :
2882 : 80 : g_string_append (s, namespaces->infos[i].name);
2883 : 160 : g_string_append (s, "::*");
2884 : : }
2885 : : }
2886 : : }
2887 : :
2888 : 71 : ret = g_string_free (s, FALSE);
2889 : 71 : s = NULL;
2890 : 71 : out:
2891 : 71 : if (s)
2892 : 0 : g_string_free (s, TRUE);
2893 : 71 : if (attributes)
2894 : 71 : g_file_attribute_info_list_unref (attributes);
2895 : 71 : if (namespaces)
2896 : 71 : g_file_attribute_info_list_unref (namespaces);
2897 : :
2898 : 71 : return ret;
2899 : : }
2900 : :
2901 : : /**
2902 : : * g_file_copy_attributes:
2903 : : * @source: a #GFile with attributes
2904 : : * @destination: a #GFile to copy attributes to
2905 : : * @flags: a set of #GFileCopyFlags
2906 : : * @cancellable: (nullable): optional #GCancellable object,
2907 : : * %NULL to ignore
2908 : : * @error: a #GError, %NULL to ignore
2909 : : *
2910 : : * Copies the file attributes from @source to @destination.
2911 : : *
2912 : : * Normally only a subset of the file attributes are copied,
2913 : : * those that are copies in a normal file copy operation
2914 : : * (which for instance does not include e.g. owner). However
2915 : : * if %G_FILE_COPY_ALL_METADATA is specified in @flags, then
2916 : : * all the metadata that is possible to copy is copied. This
2917 : : * is useful when implementing move by copy + delete source.
2918 : : *
2919 : : * Returns: %TRUE if the attributes were copied successfully,
2920 : : * %FALSE otherwise.
2921 : : */
2922 : : gboolean
2923 : 0 : g_file_copy_attributes (GFile *source,
2924 : : GFile *destination,
2925 : : GFileCopyFlags flags,
2926 : : GCancellable *cancellable,
2927 : : GError **error)
2928 : : {
2929 : : char *attrs_to_read;
2930 : : gboolean res;
2931 : : GFileInfo *info;
2932 : : gboolean source_nofollow_symlinks;
2933 : :
2934 : 0 : attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
2935 : : cancellable, error);
2936 : 0 : if (!attrs_to_read)
2937 : 0 : return FALSE;
2938 : :
2939 : 0 : source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2940 : :
2941 : : /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2942 : : * we just don't copy it.
2943 : : */
2944 : 0 : info = g_file_query_info (source, attrs_to_read,
2945 : : source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2946 : : cancellable,
2947 : : NULL);
2948 : :
2949 : 0 : g_free (attrs_to_read);
2950 : :
2951 : 0 : res = TRUE;
2952 : 0 : if (info)
2953 : : {
2954 : 0 : res = g_file_set_attributes_from_info (destination,
2955 : : info,
2956 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2957 : : cancellable,
2958 : : error);
2959 : 0 : g_object_unref (info);
2960 : : }
2961 : :
2962 : 0 : return res;
2963 : : }
2964 : :
2965 : : /* 256k minus malloc overhead */
2966 : : #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2967 : :
2968 : : static gboolean
2969 : 0 : copy_stream_with_progress (GInputStream *in,
2970 : : GOutputStream *out,
2971 : : GFile *source,
2972 : : GCancellable *cancellable,
2973 : : GFileProgressCallback progress_callback,
2974 : : gpointer progress_callback_data,
2975 : : GError **error)
2976 : : {
2977 : : gssize n_read;
2978 : : gsize n_written;
2979 : : goffset current_size;
2980 : : char *buffer;
2981 : : gboolean res;
2982 : : goffset total_size;
2983 : : GFileInfo *info;
2984 : :
2985 : 0 : total_size = -1;
2986 : : /* avoid performance impact of querying total size when it's not needed */
2987 : 0 : if (progress_callback)
2988 : : {
2989 : 0 : info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2990 : : G_FILE_ATTRIBUTE_STANDARD_SIZE,
2991 : : cancellable, NULL);
2992 : 0 : if (info)
2993 : : {
2994 : 0 : if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2995 : 0 : total_size = g_file_info_get_size (info);
2996 : 0 : g_object_unref (info);
2997 : : }
2998 : :
2999 : 0 : if (total_size == -1)
3000 : : {
3001 : 0 : info = g_file_query_info (source,
3002 : : G_FILE_ATTRIBUTE_STANDARD_SIZE,
3003 : : G_FILE_QUERY_INFO_NONE,
3004 : : cancellable, NULL);
3005 : 0 : if (info)
3006 : : {
3007 : 0 : if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
3008 : 0 : total_size = g_file_info_get_size (info);
3009 : 0 : g_object_unref (info);
3010 : : }
3011 : : }
3012 : : }
3013 : :
3014 : 0 : if (total_size == -1)
3015 : 0 : total_size = 0;
3016 : :
3017 : 0 : buffer = g_malloc0 (STREAM_BUFFER_SIZE);
3018 : 0 : current_size = 0;
3019 : 0 : res = TRUE;
3020 : : while (TRUE)
3021 : : {
3022 : 0 : n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
3023 : 0 : if (n_read == -1)
3024 : : {
3025 : 0 : res = FALSE;
3026 : 0 : break;
3027 : : }
3028 : :
3029 : 0 : if (n_read == 0)
3030 : 0 : break;
3031 : :
3032 : 0 : current_size += n_read;
3033 : :
3034 : 0 : res = g_output_stream_write_all (out, buffer, n_read, &n_written, cancellable, error);
3035 : 0 : if (!res)
3036 : 0 : break;
3037 : :
3038 : 0 : if (progress_callback)
3039 : 0 : progress_callback (current_size, total_size, progress_callback_data);
3040 : : }
3041 : 0 : g_free (buffer);
3042 : :
3043 : : /* Make sure we send full copied size */
3044 : 0 : if (progress_callback)
3045 : 0 : progress_callback (current_size, total_size, progress_callback_data);
3046 : :
3047 : 0 : return res;
3048 : : }
3049 : :
3050 : : #ifdef HAVE_COPY_FILE_RANGE
3051 : : static gboolean
3052 : 14 : do_copy_file_range (int fd_in,
3053 : : loff_t *off_in,
3054 : : int fd_out,
3055 : : loff_t *off_out,
3056 : : size_t len,
3057 : : size_t *bytes_transferred,
3058 : : GError **error)
3059 : : {
3060 : : ssize_t result;
3061 : :
3062 : : do
3063 : : {
3064 : 14 : result = copy_file_range (fd_in, off_in, fd_out, off_out, len, 0);
3065 : :
3066 : 14 : if (result == -1)
3067 : : {
3068 : 11 : int errsv = errno;
3069 : :
3070 : 11 : if (errsv == EINTR)
3071 : : {
3072 : 0 : continue;
3073 : : }
3074 : 11 : else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP || errsv == EXDEV)
3075 : : {
3076 : 11 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3077 : : _("Copy file range not supported"));
3078 : : }
3079 : : else
3080 : : {
3081 : 0 : g_set_error (error, G_IO_ERROR,
3082 : 0 : g_io_error_from_errno (errsv),
3083 : : _("Error splicing file: %s"),
3084 : : g_strerror (errsv));
3085 : : }
3086 : :
3087 : 11 : return FALSE;
3088 : : }
3089 : 3 : } while (result == -1);
3090 : :
3091 : 3 : g_assert (result >= 0);
3092 : 3 : *bytes_transferred = result;
3093 : :
3094 : 3 : return TRUE;
3095 : : }
3096 : :
3097 : : static gboolean
3098 : 34 : copy_file_range_with_progress (GInputStream *in,
3099 : : GFileInfo *in_info,
3100 : : GOutputStream *out,
3101 : : GCancellable *cancellable,
3102 : : GFileProgressCallback progress_callback,
3103 : : gpointer progress_callback_data,
3104 : : GError **error)
3105 : : {
3106 : : goffset total_size, last_notified_size;
3107 : : size_t copy_len;
3108 : : loff_t offset_in;
3109 : : loff_t offset_out;
3110 : : int fd_in, fd_out;
3111 : :
3112 : 34 : fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3113 : 34 : fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3114 : :
3115 : 34 : g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3116 : 34 : total_size = g_file_info_get_size (in_info);
3117 : :
3118 : : /* Bail out if the reported size of the file is zero. It might be zero, but it
3119 : : * might also just be a kernel file in /proc. They report their file size as
3120 : : * zero, but then have data when you start reading. Go to the fallback code
3121 : : * path for those. */
3122 : 34 : if (total_size == 0)
3123 : : {
3124 : 20 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3125 : : _("Copy file range not supported"));
3126 : 20 : return FALSE;
3127 : : }
3128 : :
3129 : 14 : offset_in = offset_out = 0;
3130 : 14 : copy_len = total_size;
3131 : 14 : last_notified_size = 0;
3132 : :
3133 : : /* Call copy_file_range() in a loop until the whole contents are copied. For
3134 : : * smaller files, this loop will iterate only once. For larger files, the
3135 : : * kernel (at least, kernel 6.1.6) will return after 2GB anyway, so that gives
3136 : : * us more loop iterations and more progress reporting. */
3137 : 17 : while (copy_len > 0)
3138 : : {
3139 : : size_t n_copied;
3140 : :
3141 : 28 : if (g_cancellable_set_error_if_cancelled (cancellable, error) ||
3142 : 14 : !do_copy_file_range (fd_in, &offset_in, fd_out, &offset_out, copy_len, &n_copied, error))
3143 : 11 : return FALSE;
3144 : :
3145 : 3 : if (n_copied == 0)
3146 : 0 : break;
3147 : :
3148 : 3 : g_assert (n_copied <= copy_len);
3149 : 3 : copy_len -= n_copied;
3150 : :
3151 : 3 : if (progress_callback)
3152 : : {
3153 : 2 : progress_callback (offset_in, total_size, progress_callback_data);
3154 : 2 : last_notified_size = total_size;
3155 : : }
3156 : : }
3157 : :
3158 : : /* Make sure we send full copied size */
3159 : 3 : if (progress_callback && last_notified_size != total_size)
3160 : 0 : progress_callback (offset_in, total_size, progress_callback_data);
3161 : :
3162 : 3 : return TRUE;
3163 : : }
3164 : : #endif /* HAVE_COPY_FILE_RANGE */
3165 : :
3166 : : #ifdef HAVE_SPLICE
3167 : :
3168 : : static gboolean
3169 : 53 : do_splice (int fd_in,
3170 : : loff_t *off_in,
3171 : : int fd_out,
3172 : : loff_t *off_out,
3173 : : size_t len,
3174 : : long *bytes_transferd,
3175 : : GError **error)
3176 : : {
3177 : : long result;
3178 : :
3179 : 53 : retry:
3180 : 53 : result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
3181 : :
3182 : 53 : if (result == -1)
3183 : : {
3184 : 0 : int errsv = errno;
3185 : :
3186 : 0 : if (errsv == EINTR)
3187 : 0 : goto retry;
3188 : 0 : else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP)
3189 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3190 : : _("Splice not supported"));
3191 : : else
3192 : 0 : g_set_error (error, G_IO_ERROR,
3193 : 0 : g_io_error_from_errno (errsv),
3194 : : _("Error splicing file: %s"),
3195 : : g_strerror (errsv));
3196 : :
3197 : 0 : return FALSE;
3198 : : }
3199 : :
3200 : 53 : *bytes_transferd = result;
3201 : 53 : return TRUE;
3202 : : }
3203 : :
3204 : : static gboolean
3205 : 31 : splice_stream_with_progress (GInputStream *in,
3206 : : GFileInfo *in_info,
3207 : : GOutputStream *out,
3208 : : GCancellable *cancellable,
3209 : : GFileProgressCallback progress_callback,
3210 : : gpointer progress_callback_data,
3211 : : GError **error)
3212 : : {
3213 : 31 : int buffer[2] = { -1, -1 };
3214 : : int buffer_size;
3215 : : gboolean res;
3216 : : goffset total_size;
3217 : : loff_t offset_in;
3218 : : loff_t offset_out;
3219 : : int fd_in, fd_out;
3220 : :
3221 : 31 : fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3222 : 31 : fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3223 : :
3224 : 31 : if (!g_unix_open_pipe (buffer, O_CLOEXEC, error))
3225 : 0 : return FALSE;
3226 : :
3227 : : /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3228 : : * pipe size. See: https://bugzilla.gnome.org/791457 */
3229 : 31 : buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3230 : 31 : if (buffer_size <= 0)
3231 : : {
3232 : 0 : buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3233 : 0 : if (buffer_size <= 0)
3234 : : {
3235 : : /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3236 : : * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the
3237 : : * possibility of running on Linux < 2.6.11 (where the capacity was
3238 : : * the system page size, typically 4KiB) because it’s ancient.
3239 : : * See pipe(7). */
3240 : 0 : buffer_size = 1024 * 64;
3241 : : }
3242 : : }
3243 : :
3244 : 31 : g_assert (buffer_size > 0);
3245 : :
3246 : 31 : total_size = -1;
3247 : : /* avoid performance impact of querying total size when it's not needed */
3248 : 31 : if (progress_callback)
3249 : : {
3250 : 0 : g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3251 : 0 : total_size = g_file_info_get_size (in_info);
3252 : : }
3253 : :
3254 : 31 : if (total_size == -1)
3255 : 31 : total_size = 0;
3256 : :
3257 : 31 : offset_in = offset_out = 0;
3258 : 31 : res = FALSE;
3259 : : while (TRUE)
3260 : 11 : {
3261 : : long n_read;
3262 : : long n_written;
3263 : :
3264 : 42 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
3265 : 0 : break;
3266 : :
3267 : 42 : if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3268 : 0 : break;
3269 : :
3270 : 42 : if (n_read == 0)
3271 : : {
3272 : 31 : res = TRUE;
3273 : 31 : break;
3274 : : }
3275 : :
3276 : 22 : while (n_read > 0)
3277 : : {
3278 : 11 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
3279 : 0 : goto out;
3280 : :
3281 : 11 : if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3282 : 0 : goto out;
3283 : :
3284 : 11 : n_read -= n_written;
3285 : : }
3286 : :
3287 : 11 : if (progress_callback)
3288 : 0 : progress_callback (offset_in, total_size, progress_callback_data);
3289 : : }
3290 : :
3291 : : /* Make sure we send full copied size */
3292 : 31 : if (progress_callback)
3293 : 0 : progress_callback (offset_in, total_size, progress_callback_data);
3294 : :
3295 : 31 : if (!g_close (buffer[0], error))
3296 : 0 : goto out;
3297 : 31 : buffer[0] = -1;
3298 : 31 : if (!g_close (buffer[1], error))
3299 : 0 : goto out;
3300 : 31 : buffer[1] = -1;
3301 : 31 : out:
3302 : 31 : if (buffer[0] != -1)
3303 : 0 : (void) g_close (buffer[0], NULL);
3304 : 31 : if (buffer[1] != -1)
3305 : 0 : (void) g_close (buffer[1], NULL);
3306 : :
3307 : 31 : return res;
3308 : : }
3309 : : #endif
3310 : :
3311 : : #ifdef __linux__
3312 : : static gboolean
3313 : 34 : btrfs_reflink_with_progress (GInputStream *in,
3314 : : GFileInfo *in_info,
3315 : : GOutputStream *out,
3316 : : GFileInfo *info,
3317 : : GCancellable *cancellable,
3318 : : GFileProgressCallback progress_callback,
3319 : : gpointer progress_callback_data,
3320 : : GError **error)
3321 : : {
3322 : : goffset total_size;
3323 : : int fd_in, fd_out;
3324 : : int ret, errsv;
3325 : :
3326 : 34 : fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3327 : 34 : fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3328 : :
3329 : 34 : total_size = -1;
3330 : : /* avoid performance impact of querying total size when it's not needed */
3331 : 34 : if (progress_callback)
3332 : : {
3333 : 2 : g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3334 : 2 : total_size = g_file_info_get_size (in_info);
3335 : : }
3336 : :
3337 : 34 : if (total_size == -1)
3338 : 32 : total_size = 0;
3339 : :
3340 : : /* Btrfs clone ioctl properties:
3341 : : * - Works at the inode level
3342 : : * - Doesn't work with directories
3343 : : * - Always follows symlinks (source and destination)
3344 : : *
3345 : : * By the time we get here, *in and *out are both regular files */
3346 : 34 : ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3347 : 34 : errsv = errno;
3348 : :
3349 : 34 : if (ret < 0)
3350 : : {
3351 : 34 : if (errsv == EXDEV)
3352 : 11 : g_set_error_literal (error, G_IO_ERROR,
3353 : : G_IO_ERROR_NOT_SUPPORTED,
3354 : : _("Copy (reflink/clone) between mounts is not supported"));
3355 : 23 : else if (errsv == EINVAL)
3356 : 0 : g_set_error_literal (error, G_IO_ERROR,
3357 : : G_IO_ERROR_NOT_SUPPORTED,
3358 : : _("Copy (reflink/clone) is not supported or invalid"));
3359 : : else
3360 : : /* Most probably something odd happened; retry with fallback */
3361 : 23 : g_set_error_literal (error, G_IO_ERROR,
3362 : : G_IO_ERROR_NOT_SUPPORTED,
3363 : : _("Copy (reflink/clone) is not supported or didn’t work"));
3364 : : /* We retry with fallback for all error cases because Btrfs is currently
3365 : : * unstable, and so we can't trust it to do clone properly.
3366 : : * In addition, any hard errors here would cause the same failure in the
3367 : : * fallback manual copy as well. */
3368 : 34 : return FALSE;
3369 : : }
3370 : :
3371 : : /* Make sure we send full copied size */
3372 : 0 : if (progress_callback)
3373 : 0 : progress_callback (total_size, total_size, progress_callback_data);
3374 : :
3375 : 0 : return TRUE;
3376 : : }
3377 : : #endif
3378 : :
3379 : : static gboolean
3380 : 94 : file_copy_fallback (GFile *source,
3381 : : GFile *destination,
3382 : : GFileCopyFlags flags,
3383 : : GCancellable *cancellable,
3384 : : GFileProgressCallback progress_callback,
3385 : : gpointer progress_callback_data,
3386 : : GError **error)
3387 : : {
3388 : 94 : gboolean ret = FALSE;
3389 : 94 : GFileInputStream *file_in = NULL;
3390 : 94 : GInputStream *in = NULL;
3391 : 94 : GOutputStream *out = NULL;
3392 : 94 : GFileInfo *info = NULL;
3393 : : const char *target;
3394 : : char *attrs_to_read;
3395 : 94 : gboolean do_set_attributes = FALSE;
3396 : : GFileCreateFlags create_flags;
3397 : 94 : GError *tmp_error = NULL;
3398 : :
3399 : : /* need to know the file type */
3400 : 94 : info = g_file_query_info (source,
3401 : : G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3402 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3403 : : cancellable,
3404 : : error);
3405 : 94 : if (!info)
3406 : 6 : goto out;
3407 : :
3408 : 88 : if (!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
3409 : : {
3410 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3411 : : _("Cannot retrieve attribute %s"), G_FILE_ATTRIBUTE_STANDARD_TYPE);
3412 : 0 : goto out;
3413 : : }
3414 : :
3415 : : /* Maybe copy the symlink? */
3416 : 163 : if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3417 : 75 : g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3418 : : {
3419 : 12 : if (!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
3420 : : {
3421 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3422 : : _("Cannot retrieve attribute %s"), G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
3423 : 0 : goto out;
3424 : : }
3425 : :
3426 : 12 : target = g_file_info_get_symlink_target (info);
3427 : 12 : if (target)
3428 : : {
3429 : 12 : if (!copy_symlink (destination, flags, cancellable, target, error))
3430 : 10 : goto out;
3431 : :
3432 : 2 : ret = TRUE;
3433 : 2 : goto out;
3434 : : }
3435 : : /* ... else fall back on a regular file copy */
3436 : : }
3437 : : /* Handle "special" files (pipes, device nodes, ...)? */
3438 : 76 : else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3439 : : {
3440 : : /* FIXME: could try to recreate device nodes and others? */
3441 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3442 : : _("Can’t copy special file"));
3443 : 0 : goto out;
3444 : : }
3445 : :
3446 : : /* Everything else should just fall back on a regular copy. */
3447 : :
3448 : 76 : file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3449 : 76 : if (!file_in)
3450 : 12 : goto out;
3451 : 64 : in = G_INPUT_STREAM (file_in);
3452 : :
3453 : 64 : attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
3454 : : cancellable, error);
3455 : 64 : if (!attrs_to_read)
3456 : 0 : goto out;
3457 : :
3458 : : /* Ok, ditch the previous lightweight info (on Unix we just
3459 : : * called lstat()); at this point we gather all the information
3460 : : * we need about the source from the opened file descriptor.
3461 : : */
3462 : 64 : g_object_unref (info);
3463 : :
3464 : 64 : info = g_file_input_stream_query_info (file_in, attrs_to_read,
3465 : : cancellable, &tmp_error);
3466 : 64 : if (!info)
3467 : : {
3468 : : /* Not all gvfs backends implement query_info_on_read(), we
3469 : : * can just fall back to the pathname again.
3470 : : * https://bugzilla.gnome.org/706254
3471 : : */
3472 : 0 : if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3473 : : {
3474 : 0 : g_clear_error (&tmp_error);
3475 : 0 : info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3476 : : cancellable, error);
3477 : : }
3478 : : else
3479 : : {
3480 : 0 : g_free (attrs_to_read);
3481 : 0 : g_propagate_error (error, tmp_error);
3482 : 0 : goto out;
3483 : : }
3484 : : }
3485 : 64 : g_free (attrs_to_read);
3486 : 64 : if (!info)
3487 : 0 : goto out;
3488 : :
3489 : 64 : do_set_attributes = TRUE;
3490 : :
3491 : : /* In the local file path, we pass down the source info which
3492 : : * includes things like unix::mode, to ensure that the target file
3493 : : * is not created with different permissions from the source file.
3494 : : *
3495 : : * If a future API like g_file_replace_with_info() is added, switch
3496 : : * this code to use that.
3497 : : *
3498 : : * Use %G_FILE_CREATE_PRIVATE unless
3499 : : * - we were told to create the file with default permissions (i.e. the
3500 : : * process’ umask),
3501 : : * - or if the source file is on a file system which doesn’t support
3502 : : * `unix::mode` (in which case it probably also makes sense to create the
3503 : : * destination with default permissions because the source cannot be
3504 : : * private),
3505 : : * - or if the destination file is a `GLocalFile`, in which case we can
3506 : : * directly open() it with the permissions from the source file.
3507 : : */
3508 : 64 : create_flags = G_FILE_CREATE_NONE;
3509 : 122 : if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3510 : 58 : g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3511 : 58 : !G_IS_LOCAL_FILE (destination))
3512 : 0 : create_flags |= G_FILE_CREATE_PRIVATE;
3513 : 64 : if (flags & G_FILE_COPY_OVERWRITE)
3514 : 20 : create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3515 : :
3516 : 64 : if (G_IS_LOCAL_FILE (destination))
3517 : : {
3518 : 64 : if (flags & G_FILE_COPY_OVERWRITE)
3519 : 40 : out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3520 : : FALSE, NULL,
3521 : 20 : flags & G_FILE_COPY_BACKUP,
3522 : : create_flags,
3523 : 20 : (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3524 : : cancellable, error);
3525 : : else
3526 : 44 : out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3527 : : FALSE, create_flags,
3528 : 44 : (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3529 : : cancellable, error);
3530 : : }
3531 : 0 : else if (flags & G_FILE_COPY_OVERWRITE)
3532 : : {
3533 : 0 : out = (GOutputStream *)g_file_replace (destination,
3534 : : NULL,
3535 : 0 : flags & G_FILE_COPY_BACKUP,
3536 : : create_flags,
3537 : : cancellable, error);
3538 : : }
3539 : : else
3540 : : {
3541 : 0 : out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3542 : : }
3543 : :
3544 : 64 : if (!out)
3545 : 30 : goto out;
3546 : :
3547 : : #ifdef __linux__
3548 : 34 : if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3549 : : {
3550 : 34 : GError *reflink_err = NULL;
3551 : :
3552 : 34 : if (!btrfs_reflink_with_progress (in, info, out, info, cancellable,
3553 : : progress_callback, progress_callback_data,
3554 : : &reflink_err))
3555 : : {
3556 : 34 : if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3557 : : {
3558 : 34 : g_clear_error (&reflink_err);
3559 : : }
3560 : : else
3561 : : {
3562 : 0 : g_propagate_error (error, reflink_err);
3563 : 0 : goto out;
3564 : : }
3565 : : }
3566 : : else
3567 : : {
3568 : 0 : ret = TRUE;
3569 : 0 : goto out;
3570 : : }
3571 : : }
3572 : : #endif
3573 : :
3574 : : #ifdef HAVE_COPY_FILE_RANGE
3575 : 34 : if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3576 : : {
3577 : 34 : GError *copy_file_range_error = NULL;
3578 : :
3579 : 34 : if (copy_file_range_with_progress (in, info, out, cancellable,
3580 : : progress_callback, progress_callback_data,
3581 : : ©_file_range_error))
3582 : : {
3583 : 3 : ret = TRUE;
3584 : 3 : goto out;
3585 : : }
3586 : 31 : else if (!g_error_matches (copy_file_range_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3587 : : {
3588 : 0 : g_propagate_error (error, g_steal_pointer (©_file_range_error));
3589 : 0 : goto out;
3590 : : }
3591 : : else
3592 : : {
3593 : 31 : g_clear_error (©_file_range_error);
3594 : : }
3595 : : }
3596 : : #endif /* HAVE_COPY_FILE_RANGE */
3597 : :
3598 : : #ifdef HAVE_SPLICE
3599 : 31 : if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3600 : : {
3601 : 31 : GError *splice_err = NULL;
3602 : :
3603 : 31 : if (!splice_stream_with_progress (in, info, out, cancellable,
3604 : : progress_callback, progress_callback_data,
3605 : : &splice_err))
3606 : : {
3607 : 0 : if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3608 : : {
3609 : 0 : g_clear_error (&splice_err);
3610 : : }
3611 : : else
3612 : : {
3613 : 0 : g_propagate_error (error, splice_err);
3614 : 31 : goto out;
3615 : : }
3616 : : }
3617 : : else
3618 : : {
3619 : 31 : ret = TRUE;
3620 : 31 : goto out;
3621 : : }
3622 : : }
3623 : :
3624 : : #endif
3625 : :
3626 : : /* A plain read/write loop */
3627 : 0 : if (!copy_stream_with_progress (in, out, source, cancellable,
3628 : : progress_callback, progress_callback_data,
3629 : : error))
3630 : 0 : goto out;
3631 : :
3632 : 0 : ret = TRUE;
3633 : 94 : out:
3634 : 94 : if (in)
3635 : : {
3636 : : /* Don't care about errors in source here */
3637 : 64 : (void) g_input_stream_close (in, cancellable, NULL);
3638 : 64 : g_object_unref (in);
3639 : : }
3640 : :
3641 : 94 : if (out)
3642 : : {
3643 : : /* But write errors on close are bad! */
3644 : 34 : if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3645 : 0 : ret = FALSE;
3646 : 34 : g_object_unref (out);
3647 : : }
3648 : :
3649 : : /* Ignore errors here. Failure to copy metadata is not a hard error */
3650 : : /* TODO: set these attributes /before/ we do the rename() on Unix */
3651 : 94 : if (ret && do_set_attributes)
3652 : : {
3653 : 34 : g_file_set_attributes_from_info (destination,
3654 : : info,
3655 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3656 : : cancellable,
3657 : : NULL);
3658 : : }
3659 : :
3660 : 94 : g_clear_object (&info);
3661 : :
3662 : 94 : return ret;
3663 : : }
3664 : :
3665 : : /**
3666 : : * g_file_copy:
3667 : : * @source: input #GFile
3668 : : * @destination: destination #GFile
3669 : : * @flags: set of #GFileCopyFlags
3670 : : * @cancellable: (nullable): optional #GCancellable object,
3671 : : * %NULL to ignore
3672 : : * @progress_callback: (nullable) (scope call) (closure progress_callback_data): function to callback with
3673 : : * progress information, or %NULL if progress information is not needed
3674 : : * @progress_callback_data: user data to pass to @progress_callback
3675 : : * @error: #GError to set on error, or %NULL
3676 : : *
3677 : : * Copies the file @source to the location specified by @destination.
3678 : : * Can not handle recursive copies of directories.
3679 : : *
3680 : : * If the flag %G_FILE_COPY_OVERWRITE is specified an already
3681 : : * existing @destination file is overwritten.
3682 : : *
3683 : : * If the flag %G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3684 : : * will be copied as symlinks, otherwise the target of the
3685 : : * @source symlink will be copied.
3686 : : *
3687 : : * If the flag %G_FILE_COPY_ALL_METADATA is specified then all the metadata
3688 : : * that is possible to copy is copied, not just the default subset (which,
3689 : : * for instance, does not include the owner, see #GFileInfo).
3690 : : *
3691 : : * If @cancellable is not %NULL, then the operation can be cancelled by
3692 : : * triggering the cancellable object from another thread. If the operation
3693 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3694 : : *
3695 : : * If @progress_callback is not %NULL, then the operation can be monitored
3696 : : * by setting this to a #GFileProgressCallback function.
3697 : : * @progress_callback_data will be passed to this function. It is guaranteed
3698 : : * that this callback will be called after all data has been transferred with
3699 : : * the total number of bytes copied during the operation.
3700 : : *
3701 : : * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3702 : : * is returned, independent on the status of the @destination.
3703 : : *
3704 : : * If %G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3705 : : * the error %G_IO_ERROR_EXISTS is returned.
3706 : : *
3707 : : * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3708 : : * error is returned. If trying to overwrite a directory with a directory the
3709 : : * %G_IO_ERROR_WOULD_MERGE error is returned.
3710 : : *
3711 : : * If the source is a directory and the target does not exist, or
3712 : : * %G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3713 : : * %G_IO_ERROR_WOULD_RECURSE error is returned.
3714 : : *
3715 : : * If you are interested in copying the #GFile object itself (not the on-disk
3716 : : * file), see g_file_dup().
3717 : : *
3718 : : * Returns: %TRUE on success, %FALSE otherwise.
3719 : : */
3720 : : gboolean
3721 : 94 : g_file_copy (GFile *source,
3722 : : GFile *destination,
3723 : : GFileCopyFlags flags,
3724 : : GCancellable *cancellable,
3725 : : GFileProgressCallback progress_callback,
3726 : : gpointer progress_callback_data,
3727 : : GError **error)
3728 : : {
3729 : : GFileIface *iface;
3730 : : GError *my_error;
3731 : : gboolean res;
3732 : :
3733 : 94 : g_return_val_if_fail (G_IS_FILE (source), FALSE);
3734 : 94 : g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3735 : :
3736 : 94 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
3737 : 0 : return FALSE;
3738 : :
3739 : 94 : iface = G_FILE_GET_IFACE (destination);
3740 : 94 : if (iface->copy)
3741 : : {
3742 : 0 : my_error = NULL;
3743 : 0 : res = (* iface->copy) (source, destination,
3744 : : flags, cancellable,
3745 : : progress_callback, progress_callback_data,
3746 : : &my_error);
3747 : :
3748 : 0 : if (res)
3749 : 0 : return TRUE;
3750 : :
3751 : 0 : if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3752 : : {
3753 : 0 : g_propagate_error (error, my_error);
3754 : 0 : return FALSE;
3755 : : }
3756 : : else
3757 : 0 : g_clear_error (&my_error);
3758 : : }
3759 : :
3760 : : /* If the types are different, and the destination method failed
3761 : : * also try the source method
3762 : : */
3763 : 94 : if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3764 : : {
3765 : 0 : iface = G_FILE_GET_IFACE (source);
3766 : :
3767 : 0 : if (iface->copy)
3768 : : {
3769 : 0 : my_error = NULL;
3770 : 0 : res = (* iface->copy) (source, destination,
3771 : : flags, cancellable,
3772 : : progress_callback, progress_callback_data,
3773 : : &my_error);
3774 : :
3775 : 0 : if (res)
3776 : 0 : return TRUE;
3777 : :
3778 : 0 : if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3779 : : {
3780 : 0 : g_propagate_error (error, my_error);
3781 : 0 : return FALSE;
3782 : : }
3783 : : else
3784 : 0 : g_clear_error (&my_error);
3785 : : }
3786 : : }
3787 : :
3788 : 94 : return file_copy_fallback (source, destination, flags, cancellable,
3789 : : progress_callback, progress_callback_data,
3790 : : error);
3791 : : }
3792 : :
3793 : : /**
3794 : : * g_file_copy_async:
3795 : : * @source: input #GFile
3796 : : * @destination: destination #GFile
3797 : : * @flags: set of #GFileCopyFlags
3798 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
3799 : : * @cancellable: (nullable): optional #GCancellable object,
3800 : : * %NULL to ignore
3801 : : * @progress_callback: (nullable) (scope notified) (closure progress_callback_data):
3802 : : * function to callback with progress information, or %NULL if
3803 : : * progress information is not needed
3804 : : * @progress_callback_data: user data to pass to @progress_callback
3805 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
3806 : : * to call when the request is satisfied
3807 : : * @user_data: the data to pass to callback
3808 : : *
3809 : : * Copies the file @source to the location specified by @destination
3810 : : * asynchronously. For details of the behaviour, see g_file_copy().
3811 : : *
3812 : : * If @progress_callback is not %NULL, then that function that will be called
3813 : : * just like in g_file_copy(). The callback will run in the default main context
3814 : : * of the thread calling g_file_copy_async() — the same context as @callback is
3815 : : * run in.
3816 : : *
3817 : : * When the operation is finished, @callback will be called. You can then call
3818 : : * g_file_copy_finish() to get the result of the operation.
3819 : : */
3820 : : void
3821 : 1 : g_file_copy_async (GFile *source,
3822 : : GFile *destination,
3823 : : GFileCopyFlags flags,
3824 : : int io_priority,
3825 : : GCancellable *cancellable,
3826 : : GFileProgressCallback progress_callback,
3827 : : gpointer progress_callback_data,
3828 : : GAsyncReadyCallback callback,
3829 : : gpointer user_data)
3830 : : {
3831 : : GFileIface *iface;
3832 : :
3833 : 1 : g_return_if_fail (G_IS_FILE (source));
3834 : 1 : g_return_if_fail (G_IS_FILE (destination));
3835 : :
3836 : 1 : iface = G_FILE_GET_IFACE (source);
3837 : 1 : (* iface->copy_async) (source,
3838 : : destination,
3839 : : flags,
3840 : : io_priority,
3841 : : cancellable,
3842 : : progress_callback,
3843 : : progress_callback_data,
3844 : : callback,
3845 : : user_data);
3846 : : }
3847 : :
3848 : : typedef struct _CopyAsyncClosuresData
3849 : : {
3850 : : GClosure *progress_callback_closure;
3851 : : GClosure *ready_callback_closure;
3852 : : } CopyAsyncClosuresData;
3853 : :
3854 : : static CopyAsyncClosuresData *
3855 : 2 : copy_async_closures_data_new (GClosure *progress_callback_closure,
3856 : : GClosure *ready_callback_closure)
3857 : : {
3858 : : CopyAsyncClosuresData *data;
3859 : :
3860 : 2 : data = g_new0 (CopyAsyncClosuresData, 1);
3861 : :
3862 : 2 : if (progress_callback_closure != NULL)
3863 : : {
3864 : 2 : data->progress_callback_closure = g_closure_ref (progress_callback_closure);
3865 : 2 : g_closure_sink (progress_callback_closure);
3866 : 2 : if (G_CLOSURE_NEEDS_MARSHAL (progress_callback_closure))
3867 : 2 : g_closure_set_marshal (progress_callback_closure, g_cclosure_marshal_generic);
3868 : : }
3869 : :
3870 : 2 : data->ready_callback_closure = g_closure_ref (ready_callback_closure);
3871 : 2 : g_closure_sink (ready_callback_closure);
3872 : 2 : if (G_CLOSURE_NEEDS_MARSHAL (ready_callback_closure))
3873 : 2 : g_closure_set_marshal (ready_callback_closure, g_cclosure_marshal_generic);
3874 : :
3875 : 2 : return data;
3876 : : }
3877 : :
3878 : : static void
3879 : 2 : copy_async_closures_data_free (CopyAsyncClosuresData *data)
3880 : : {
3881 : 2 : if (data->progress_callback_closure != NULL)
3882 : 2 : g_closure_unref (data->progress_callback_closure);
3883 : :
3884 : 2 : g_closure_unref (data->ready_callback_closure);
3885 : :
3886 : 2 : g_free (data);
3887 : 2 : }
3888 : :
3889 : : static void
3890 : 2 : copy_async_invoke_progress (goffset current_num_bytes,
3891 : : goffset total_num_bytes,
3892 : : void *user_data)
3893 : : {
3894 : 2 : CopyAsyncClosuresData *data = (CopyAsyncClosuresData *) user_data;
3895 : 2 : GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
3896 : :
3897 : : /* goffset is 64-bits even on 32-bits platforms */
3898 : 2 : g_value_init (¶ms[0], G_TYPE_INT64);
3899 : 2 : g_value_set_int64 (¶ms[0], current_num_bytes);
3900 : 2 : g_value_init (¶ms[1], G_TYPE_INT64);
3901 : 2 : g_value_set_int64 (¶ms[1], total_num_bytes);
3902 : :
3903 : 2 : g_closure_invoke (data->progress_callback_closure, /* result = */ NULL, 2, params, /* hint = */ NULL);
3904 : :
3905 : 2 : g_value_unset (¶ms[0]);
3906 : 2 : g_value_unset (¶ms[1]);
3907 : 2 : }
3908 : :
3909 : : static void
3910 : 2 : copy_async_invoke_ready (GObject *file,
3911 : : GAsyncResult *result,
3912 : : void *user_data)
3913 : : {
3914 : 2 : CopyAsyncClosuresData *data = (CopyAsyncClosuresData *) user_data;
3915 : 2 : GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
3916 : :
3917 : 2 : g_value_init (¶ms[0], G_TYPE_FILE);
3918 : 2 : g_value_set_object (¶ms[0], file);
3919 : 2 : g_value_init (¶ms[1], G_TYPE_ASYNC_RESULT);
3920 : 2 : g_value_set_object (¶ms[1], result);
3921 : :
3922 : 2 : g_closure_invoke (data->ready_callback_closure, /* result = */ NULL, 2, params, /* hint = */ NULL);
3923 : :
3924 : 2 : copy_async_closures_data_free (data);
3925 : 2 : g_value_unset (¶ms[0]);
3926 : 2 : g_value_unset (¶ms[1]);
3927 : 2 : }
3928 : :
3929 : : /**
3930 : : * g_file_copy_async_with_closures: (rename-to g_file_copy_async) (finish-func copy_finish):
3931 : : * @source: input [type@Gio.File]
3932 : : * @destination: destination [type@Gio.File]
3933 : : * @flags: set of [flags@Gio.FileCopyFlags]
3934 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
3935 : : * @cancellable: (nullable): optional [class@Gio.Cancellable] object,
3936 : : * `NULL` to ignore
3937 : : * @progress_callback_closure: (nullable): [type@GObject.Closure] to invoke with progress
3938 : : * information, or `NULL` if progress information is not needed
3939 : : * @ready_callback_closure: (not nullable): [type@GObject.Closure] to invoke when the request is satisfied
3940 : : *
3941 : : * Version of [method@Gio.File.copy_async] using closures instead of callbacks for
3942 : : * easier binding in other languages.
3943 : : *
3944 : : * Since: 2.82
3945 : : */
3946 : : void
3947 : 1 : g_file_copy_async_with_closures (GFile *source,
3948 : : GFile *destination,
3949 : : GFileCopyFlags flags,
3950 : : int io_priority,
3951 : : GCancellable *cancellable,
3952 : : GClosure *progress_callback_closure,
3953 : : GClosure *ready_callback_closure)
3954 : : {
3955 : : CopyAsyncClosuresData *data;
3956 : :
3957 : : /* freed in copy_async_invoke_ready */
3958 : 1 : data = copy_async_closures_data_new (progress_callback_closure, ready_callback_closure);
3959 : :
3960 : 1 : g_file_copy_async (source, destination, flags, io_priority, cancellable,
3961 : : progress_callback_closure == NULL ? NULL : copy_async_invoke_progress, data,
3962 : : copy_async_invoke_ready, data);
3963 : 1 : }
3964 : :
3965 : : /**
3966 : : * g_file_copy_finish:
3967 : : * @file: input #GFile
3968 : : * @res: a #GAsyncResult
3969 : : * @error: a #GError, or %NULL
3970 : : *
3971 : : * Finishes copying the file started with g_file_copy_async().
3972 : : *
3973 : : * Returns: a %TRUE on success, %FALSE on error.
3974 : : */
3975 : : gboolean
3976 : 0 : g_file_copy_finish (GFile *file,
3977 : : GAsyncResult *res,
3978 : : GError **error)
3979 : : {
3980 : : GFileIface *iface;
3981 : :
3982 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
3983 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3984 : :
3985 : 0 : if (g_async_result_legacy_propagate_error (res, error))
3986 : 0 : return FALSE;
3987 : :
3988 : 0 : iface = G_FILE_GET_IFACE (file);
3989 : 0 : return (* iface->copy_finish) (file, res, error);
3990 : : }
3991 : :
3992 : : /**
3993 : : * g_file_move:
3994 : : * @source: #GFile pointing to the source location
3995 : : * @destination: #GFile pointing to the destination location
3996 : : * @flags: set of #GFileCopyFlags
3997 : : * @cancellable: (nullable): optional #GCancellable object,
3998 : : * %NULL to ignore
3999 : : * @progress_callback: (nullable) (scope call) (closure progress_callback_data): #GFileProgressCallback
4000 : : * function for updates
4001 : : * @progress_callback_data: gpointer to user data for
4002 : : * the callback function
4003 : : * @error: #GError for returning error conditions, or %NULL
4004 : : *
4005 : : * Tries to move the file or directory @source to the location specified
4006 : : * by @destination. If native move operations are supported then this is
4007 : : * used, otherwise a copy + delete fallback is used. The native
4008 : : * implementation may support moving directories (for instance on moves
4009 : : * inside the same filesystem), but the fallback code does not.
4010 : : *
4011 : : * If the flag %G_FILE_COPY_OVERWRITE is specified an already
4012 : : * existing @destination file is overwritten.
4013 : : *
4014 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4015 : : * triggering the cancellable object from another thread. If the operation
4016 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4017 : : *
4018 : : * If @progress_callback is not %NULL, then the operation can be monitored
4019 : : * by setting this to a #GFileProgressCallback function.
4020 : : * @progress_callback_data will be passed to this function. It is
4021 : : * guaranteed that this callback will be called after all data has been
4022 : : * transferred with the total number of bytes copied during the operation.
4023 : : *
4024 : : * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
4025 : : * error is returned, independent on the status of the @destination.
4026 : : *
4027 : : * If %G_FILE_COPY_OVERWRITE is not specified and the target exists,
4028 : : * then the error %G_IO_ERROR_EXISTS is returned.
4029 : : *
4030 : : * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
4031 : : * error is returned. If trying to overwrite a directory with a directory the
4032 : : * %G_IO_ERROR_WOULD_MERGE error is returned.
4033 : : *
4034 : : * If the source is a directory and the target does not exist, or
4035 : : * %G_FILE_COPY_OVERWRITE is specified and the target is a file, then
4036 : : * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
4037 : : * move operation isn't available).
4038 : : *
4039 : : * Returns: %TRUE on successful move, %FALSE otherwise.
4040 : : */
4041 : : gboolean
4042 : 18 : g_file_move (GFile *source,
4043 : : GFile *destination,
4044 : : GFileCopyFlags flags,
4045 : : GCancellable *cancellable,
4046 : : GFileProgressCallback progress_callback,
4047 : : gpointer progress_callback_data,
4048 : : GError **error)
4049 : : {
4050 : : GFileIface *iface;
4051 : : GError *my_error;
4052 : : gboolean res;
4053 : :
4054 : 18 : g_return_val_if_fail (G_IS_FILE (source), FALSE);
4055 : 18 : g_return_val_if_fail (G_IS_FILE (destination), FALSE);
4056 : :
4057 : 18 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4058 : 0 : return FALSE;
4059 : :
4060 : 18 : iface = G_FILE_GET_IFACE (destination);
4061 : 18 : if (iface->move)
4062 : : {
4063 : 18 : my_error = NULL;
4064 : 18 : res = (* iface->move) (source, destination,
4065 : : flags, cancellable,
4066 : : progress_callback, progress_callback_data,
4067 : : &my_error);
4068 : :
4069 : 18 : if (res)
4070 : 17 : return TRUE;
4071 : :
4072 : 1 : if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
4073 : : {
4074 : 1 : g_propagate_error (error, my_error);
4075 : 1 : return FALSE;
4076 : : }
4077 : : else
4078 : 0 : g_clear_error (&my_error);
4079 : : }
4080 : :
4081 : : /* If the types are different, and the destination method failed
4082 : : * also try the source method
4083 : : */
4084 : 0 : if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
4085 : : {
4086 : 0 : iface = G_FILE_GET_IFACE (source);
4087 : :
4088 : 0 : if (iface->move)
4089 : : {
4090 : 0 : my_error = NULL;
4091 : 0 : res = (* iface->move) (source, destination,
4092 : : flags, cancellable,
4093 : : progress_callback, progress_callback_data,
4094 : : &my_error);
4095 : :
4096 : 0 : if (res)
4097 : 0 : return TRUE;
4098 : :
4099 : 0 : if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
4100 : : {
4101 : 0 : g_propagate_error (error, my_error);
4102 : 0 : return FALSE;
4103 : : }
4104 : : else
4105 : 0 : g_clear_error (&my_error);
4106 : : }
4107 : : }
4108 : :
4109 : 0 : if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
4110 : : {
4111 : 0 : g_set_error_literal (error, G_IO_ERROR,
4112 : : G_IO_ERROR_NOT_SUPPORTED,
4113 : : _("Operation not supported"));
4114 : 0 : return FALSE;
4115 : : }
4116 : :
4117 : 0 : flags |= G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS;
4118 : 0 : if (!g_file_copy (source, destination, flags, cancellable,
4119 : : progress_callback, progress_callback_data,
4120 : : error))
4121 : 0 : return FALSE;
4122 : :
4123 : 0 : return g_file_delete (source, cancellable, error);
4124 : : }
4125 : :
4126 : : /**
4127 : : * g_file_move_async:
4128 : : * @source: #GFile pointing to the source location
4129 : : * @destination: #GFile pointing to the destination location
4130 : : * @flags: set of #GFileCopyFlags
4131 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4132 : : * @cancellable: (nullable): optional #GCancellable object,
4133 : : * %NULL to ignore
4134 : : * @progress_callback: (nullable) (scope call) (closure progress_callback_data):
4135 : : * #GFileProgressCallback function for updates
4136 : : * @progress_callback_data: gpointer to user data for the callback function
4137 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
4138 : : * to call when the request is satisfied
4139 : : * @user_data: the data to pass to callback function
4140 : : *
4141 : : * Asynchronously moves a file @source to the location of @destination. For details of the behaviour, see g_file_move().
4142 : : *
4143 : : * If @progress_callback is not %NULL, then that function that will be called
4144 : : * just like in g_file_move(). The callback will run in the default main context
4145 : : * of the thread calling g_file_move_async() — the same context as @callback is
4146 : : * run in.
4147 : : *
4148 : : * When the operation is finished, @callback will be called. You can then call
4149 : : * g_file_move_finish() to get the result of the operation.
4150 : : *
4151 : : * Since: 2.72
4152 : : */
4153 : : void
4154 : 2 : g_file_move_async (GFile *source,
4155 : : GFile *destination,
4156 : : GFileCopyFlags flags,
4157 : : int io_priority,
4158 : : GCancellable *cancellable,
4159 : : GFileProgressCallback progress_callback,
4160 : : gpointer progress_callback_data,
4161 : : GAsyncReadyCallback callback,
4162 : : gpointer user_data)
4163 : : {
4164 : : GFileIface *iface;
4165 : :
4166 : 2 : g_return_if_fail (G_IS_FILE (source));
4167 : 2 : g_return_if_fail (G_IS_FILE (destination));
4168 : 2 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4169 : :
4170 : 2 : iface = G_FILE_GET_IFACE (source);
4171 : 2 : (* iface->move_async) (source,
4172 : : destination,
4173 : : flags,
4174 : : io_priority,
4175 : : cancellable,
4176 : : progress_callback,
4177 : : progress_callback_data,
4178 : : callback,
4179 : : user_data);
4180 : : }
4181 : :
4182 : : /**
4183 : : * g_file_move_async_with_closures: (rename-to g_file_move_async) (finish-func move_finish):
4184 : : * @source: input [type@Gio.File]
4185 : : * @destination: destination [type@Gio.File]
4186 : : * @flags: set of [flags@Gio.FileCopyFlags]
4187 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4188 : : * @cancellable: (nullable): optional [class@Gio.Cancellable] object,
4189 : : * `NULL` to ignore
4190 : : * @progress_callback_closure: (nullable): [type@GObject.Closure] to invoke with progress
4191 : : * information, or `NULL` if progress information is not needed
4192 : : * @ready_callback_closure: (not nullable): [type@GObject.Closure] to invoke when the request is satisfied
4193 : : *
4194 : : * Version of [method@Gio.File.move_async] using closures instead of callbacks for
4195 : : * easier binding in other languages.
4196 : : *
4197 : : * Since: 2.82
4198 : : */
4199 : : void
4200 : 1 : g_file_move_async_with_closures (GFile *source,
4201 : : GFile *destination,
4202 : : GFileCopyFlags flags,
4203 : : int io_priority,
4204 : : GCancellable *cancellable,
4205 : : GClosure *progress_callback_closure,
4206 : : GClosure *ready_callback_closure)
4207 : : {
4208 : : CopyAsyncClosuresData *data;
4209 : :
4210 : : /* freed in copy_async_invoke_ready */
4211 : 1 : data = copy_async_closures_data_new (progress_callback_closure, ready_callback_closure);
4212 : :
4213 : 1 : g_file_move_async (source, destination, flags, io_priority, cancellable,
4214 : : progress_callback_closure == NULL ? NULL : copy_async_invoke_progress, data,
4215 : : copy_async_invoke_ready, data);
4216 : 1 : }
4217 : :
4218 : : /**
4219 : : * g_file_move_finish:
4220 : : * @file: input source #GFile
4221 : : * @result: a #GAsyncResult
4222 : : * @error: a #GError, or %NULL
4223 : : *
4224 : : * Finishes an asynchronous file movement, started with
4225 : : * g_file_move_async().
4226 : : *
4227 : : * Returns: %TRUE on successful file move, %FALSE otherwise.
4228 : : *
4229 : : * Since: 2.72
4230 : : */
4231 : : gboolean
4232 : 3 : g_file_move_finish (GFile *file,
4233 : : GAsyncResult *result,
4234 : : GError **error)
4235 : : {
4236 : : GFileIface *iface;
4237 : :
4238 : 3 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4239 : 3 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4240 : 3 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4241 : :
4242 : 3 : iface = G_FILE_GET_IFACE (file);
4243 : 3 : return (* iface->move_finish) (file, result, error);
4244 : : }
4245 : :
4246 : : /**
4247 : : * g_file_make_directory:
4248 : : * @file: input #GFile
4249 : : * @cancellable: (nullable): optional #GCancellable object,
4250 : : * %NULL to ignore
4251 : : * @error: a #GError, or %NULL
4252 : : *
4253 : : * Creates a directory.
4254 : : *
4255 : : * Note that this will only create a child directory
4256 : : * of the immediate parent directory of the path or URI given by the #GFile.
4257 : : * To recursively create directories, see g_file_make_directory_with_parents().
4258 : : *
4259 : : * This function will fail if the parent directory does not exist, setting
4260 : : * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
4261 : : * creating directories, this function will fail, setting @error to
4262 : : * %G_IO_ERROR_NOT_SUPPORTED. If the directory already exists,
4263 : : * [error@Gio.IOErrorEnum.EXISTS] will be returned.
4264 : : *
4265 : : * For a local #GFile the newly created directory will have the default
4266 : : * (current) ownership and permissions of the current process.
4267 : : *
4268 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4269 : : * triggering the cancellable object from another thread. If the operation
4270 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4271 : : *
4272 : : * Returns: %TRUE on successful creation, %FALSE otherwise.
4273 : : */
4274 : : gboolean
4275 : 93 : g_file_make_directory (GFile *file,
4276 : : GCancellable *cancellable,
4277 : : GError **error)
4278 : : {
4279 : : GFileIface *iface;
4280 : :
4281 : 93 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4282 : :
4283 : 93 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4284 : 0 : return FALSE;
4285 : :
4286 : 93 : iface = G_FILE_GET_IFACE (file);
4287 : :
4288 : 93 : if (iface->make_directory == NULL)
4289 : : {
4290 : 0 : g_set_error_literal (error, G_IO_ERROR,
4291 : : G_IO_ERROR_NOT_SUPPORTED,
4292 : : _("Operation not supported"));
4293 : 0 : return FALSE;
4294 : : }
4295 : :
4296 : 93 : return (* iface->make_directory) (file, cancellable, error);
4297 : : }
4298 : :
4299 : : /**
4300 : : * g_file_make_directory_async: (virtual make_directory_async)
4301 : : * @file: input #GFile
4302 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4303 : : * @cancellable: (nullable): optional #GCancellable object,
4304 : : * %NULL to ignore
4305 : : * @callback: a #GAsyncReadyCallback to call
4306 : : * when the request is satisfied
4307 : : * @user_data: the data to pass to callback function
4308 : : *
4309 : : * Asynchronously creates a directory.
4310 : : *
4311 : : * Since: 2.38
4312 : : */
4313 : : void
4314 : 0 : g_file_make_directory_async (GFile *file,
4315 : : int io_priority,
4316 : : GCancellable *cancellable,
4317 : : GAsyncReadyCallback callback,
4318 : : gpointer user_data)
4319 : : {
4320 : : GFileIface *iface;
4321 : :
4322 : 0 : g_return_if_fail (G_IS_FILE (file));
4323 : :
4324 : 0 : iface = G_FILE_GET_IFACE (file);
4325 : 0 : (* iface->make_directory_async) (file,
4326 : : io_priority,
4327 : : cancellable,
4328 : : callback,
4329 : : user_data);
4330 : : }
4331 : :
4332 : : /**
4333 : : * g_file_make_directory_finish: (virtual make_directory_finish)
4334 : : * @file: input #GFile
4335 : : * @result: a #GAsyncResult
4336 : : * @error: a #GError, or %NULL
4337 : : *
4338 : : * Finishes an asynchronous directory creation, started with
4339 : : * g_file_make_directory_async().
4340 : : *
4341 : : * Returns: %TRUE on successful directory creation, %FALSE otherwise.
4342 : : * Since: 2.38
4343 : : */
4344 : : gboolean
4345 : 0 : g_file_make_directory_finish (GFile *file,
4346 : : GAsyncResult *result,
4347 : : GError **error)
4348 : : {
4349 : : GFileIface *iface;
4350 : :
4351 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4352 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4353 : :
4354 : 0 : iface = G_FILE_GET_IFACE (file);
4355 : 0 : return (* iface->make_directory_finish) (file, result, error);
4356 : : }
4357 : :
4358 : : /**
4359 : : * g_file_make_directory_with_parents:
4360 : : * @file: input #GFile
4361 : : * @cancellable: (nullable): optional #GCancellable object,
4362 : : * %NULL to ignore
4363 : : * @error: a #GError, or %NULL
4364 : : *
4365 : : * Creates a directory and any parent directories that may not
4366 : : * exist similar to 'mkdir -p'. If the file system does not support
4367 : : * creating directories, this function will fail, setting @error to
4368 : : * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
4369 : : * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
4370 : : * the similar g_mkdir_with_parents().
4371 : : *
4372 : : * For a local #GFile the newly created directories will have the default
4373 : : * (current) ownership and permissions of the current process.
4374 : : *
4375 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4376 : : * triggering the cancellable object from another thread. If the operation
4377 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4378 : : *
4379 : : * Returns: %TRUE if all directories have been successfully created, %FALSE
4380 : : * otherwise.
4381 : : *
4382 : : * Since: 2.18
4383 : : */
4384 : : gboolean
4385 : 16 : g_file_make_directory_with_parents (GFile *file,
4386 : : GCancellable *cancellable,
4387 : : GError **error)
4388 : : {
4389 : 16 : GFile *work_file = NULL;
4390 : 16 : GList *list = NULL, *l;
4391 : 16 : GError *my_error = NULL;
4392 : :
4393 : 16 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4394 : :
4395 : 16 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4396 : 0 : return FALSE;
4397 : :
4398 : : /* Try for the simple case of not having to create any parent
4399 : : * directories. If any parent directory needs to be created, this
4400 : : * call will fail with NOT_FOUND. If that happens, then that value of
4401 : : * my_error persists into the while loop below.
4402 : : */
4403 : 16 : g_file_make_directory (file, cancellable, &my_error);
4404 : 16 : if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4405 : : {
4406 : 6 : if (my_error)
4407 : 1 : g_propagate_error (error, my_error);
4408 : 6 : return my_error == NULL;
4409 : : }
4410 : :
4411 : 10 : work_file = g_object_ref (file);
4412 : :
4413 : : /* Creates the parent directories as needed. In case any particular
4414 : : * creation operation fails for lack of other parent directories
4415 : : * (NOT_FOUND), the directory is added to a list of directories to
4416 : : * create later, and the value of my_error is retained until the next
4417 : : * iteration of the loop. After the loop my_error should either be
4418 : : * empty or contain a real failure condition.
4419 : : */
4420 : 30 : while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4421 : : {
4422 : : GFile *parent_file;
4423 : :
4424 : 20 : parent_file = g_file_get_parent (work_file);
4425 : 20 : if (parent_file == NULL)
4426 : 0 : break;
4427 : :
4428 : 20 : g_clear_error (&my_error);
4429 : 20 : g_file_make_directory (parent_file, cancellable, &my_error);
4430 : : /* Another process may have created the directory in between the
4431 : : * G_IO_ERROR_NOT_FOUND and now
4432 : : */
4433 : 20 : if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4434 : 0 : g_clear_error (&my_error);
4435 : :
4436 : 20 : g_object_unref (work_file);
4437 : 20 : work_file = g_object_ref (parent_file);
4438 : :
4439 : 20 : if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4440 : 10 : list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
4441 : : else
4442 : 10 : g_object_unref (parent_file);
4443 : : }
4444 : :
4445 : : /* All directories should be able to be created now, so an error at
4446 : : * this point means the whole operation must fail -- except an EXISTS
4447 : : * error, which means that another process already created the
4448 : : * directory in between the previous failure and now.
4449 : : */
4450 : 20 : for (l = list; my_error == NULL && l; l = l->next)
4451 : : {
4452 : 10 : g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
4453 : 10 : if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4454 : 0 : g_clear_error (&my_error);
4455 : : }
4456 : :
4457 : 10 : if (work_file)
4458 : 10 : g_object_unref (work_file);
4459 : :
4460 : : /* Clean up */
4461 : 20 : while (list != NULL)
4462 : : {
4463 : 10 : g_object_unref ((GFile *) list->data);
4464 : 10 : list = g_list_remove (list, list->data);
4465 : : }
4466 : :
4467 : : /* At this point an error in my_error means a that something
4468 : : * unexpected failed in either of the loops above, so the whole
4469 : : * operation must fail.
4470 : : */
4471 : 10 : if (my_error != NULL)
4472 : : {
4473 : 1 : g_propagate_error (error, my_error);
4474 : 1 : return FALSE;
4475 : : }
4476 : :
4477 : 9 : return g_file_make_directory (file, cancellable, error);
4478 : : }
4479 : :
4480 : : /**
4481 : : * g_file_make_symbolic_link:
4482 : : * @file: a #GFile with the name of the symlink to create
4483 : : * @symlink_value: (type filename): a string with the path for the target
4484 : : * of the new symlink
4485 : : * @cancellable: (nullable): optional #GCancellable object,
4486 : : * %NULL to ignore
4487 : : * @error: a #GError
4488 : : *
4489 : : * Creates a symbolic link named @file which contains the string
4490 : : * @symlink_value.
4491 : : *
4492 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4493 : : * triggering the cancellable object from another thread. If the operation
4494 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4495 : : *
4496 : : * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
4497 : : */
4498 : : gboolean
4499 : 66 : g_file_make_symbolic_link (GFile *file,
4500 : : const char *symlink_value,
4501 : : GCancellable *cancellable,
4502 : : GError **error)
4503 : : {
4504 : : GFileIface *iface;
4505 : :
4506 : 66 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4507 : 66 : g_return_val_if_fail (symlink_value != NULL, FALSE);
4508 : :
4509 : 65 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4510 : 0 : return FALSE;
4511 : :
4512 : 65 : if (*symlink_value == '\0')
4513 : : {
4514 : 2 : g_set_error_literal (error, G_IO_ERROR,
4515 : : G_IO_ERROR_INVALID_ARGUMENT,
4516 : : _("Invalid symlink value given"));
4517 : 2 : return FALSE;
4518 : : }
4519 : :
4520 : 63 : iface = G_FILE_GET_IFACE (file);
4521 : :
4522 : 63 : if (iface->make_symbolic_link == NULL)
4523 : : {
4524 : 0 : g_set_error_literal (error, G_IO_ERROR,
4525 : : G_IO_ERROR_NOT_SUPPORTED,
4526 : : _("Symbolic links not supported"));
4527 : 0 : return FALSE;
4528 : : }
4529 : :
4530 : 63 : return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4531 : : }
4532 : :
4533 : : static void
4534 : 4 : make_symbolic_link_async_thread (GTask *task,
4535 : : gpointer object,
4536 : : gpointer task_data,
4537 : : GCancellable *cancellable)
4538 : : {
4539 : 4 : const char *symlink_value = task_data;
4540 : 4 : GError *error = NULL;
4541 : :
4542 : 4 : if (g_file_make_symbolic_link (G_FILE (object), symlink_value, cancellable, &error))
4543 : 1 : g_task_return_boolean (task, TRUE);
4544 : : else
4545 : 3 : g_task_return_error (task, g_steal_pointer (&error));
4546 : 4 : }
4547 : :
4548 : : static void
4549 : 4 : g_file_real_make_symbolic_link_async (GFile *file,
4550 : : const char *symlink_value,
4551 : : int io_priority,
4552 : : GCancellable *cancellable,
4553 : : GAsyncReadyCallback callback,
4554 : : gpointer user_data)
4555 : : {
4556 : : GTask *task;
4557 : :
4558 : 4 : g_return_if_fail (G_IS_FILE (file));
4559 : 4 : g_return_if_fail (symlink_value != NULL);
4560 : 4 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4561 : :
4562 : 4 : task = g_task_new (file, cancellable, callback, user_data);
4563 : 4 : g_task_set_source_tag (task, g_file_real_make_symbolic_link_async);
4564 : 4 : g_task_set_task_data (task, g_strdup (symlink_value), g_free);
4565 : 4 : g_task_set_priority (task, io_priority);
4566 : :
4567 : 4 : g_task_run_in_thread (task, make_symbolic_link_async_thread);
4568 : 4 : g_object_unref (task);
4569 : : }
4570 : :
4571 : : /**
4572 : : * g_file_make_symbolic_link_async: (virtual make_symbolic_link_async)
4573 : : * @file: a #GFile with the name of the symlink to create
4574 : : * @symlink_value: (type filename): a string with the path for the target
4575 : : * of the new symlink
4576 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4577 : : * @cancellable: (nullable): optional #GCancellable object,
4578 : : * %NULL to ignore
4579 : : * @callback: a #GAsyncReadyCallback to call
4580 : : * when the request is satisfied
4581 : : * @user_data: the data to pass to callback function
4582 : : *
4583 : : * Asynchronously creates a symbolic link named @file which contains the
4584 : : * string @symlink_value.
4585 : : *
4586 : : * Since: 2.74
4587 : : */
4588 : : void
4589 : 5 : g_file_make_symbolic_link_async (GFile *file,
4590 : : const char *symlink_value,
4591 : : int io_priority,
4592 : : GCancellable *cancellable,
4593 : : GAsyncReadyCallback callback,
4594 : : gpointer user_data)
4595 : : {
4596 : : GFileIface *iface;
4597 : :
4598 : 5 : g_return_if_fail (G_IS_FILE (file));
4599 : 5 : g_return_if_fail (symlink_value != NULL);
4600 : 4 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4601 : :
4602 : 4 : iface = G_FILE_GET_IFACE (file);
4603 : :
4604 : : /* Default implementation should always be provided by GFileIface */
4605 : 4 : g_assert (iface->make_symbolic_link_async != NULL);
4606 : :
4607 : 4 : (* iface->make_symbolic_link_async) (file, symlink_value, io_priority,
4608 : : cancellable, callback, user_data);
4609 : : }
4610 : :
4611 : : static gboolean
4612 : 4 : g_file_real_make_symbolic_link_finish (GFile *file,
4613 : : GAsyncResult *result,
4614 : : GError **error)
4615 : : {
4616 : 4 : g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
4617 : :
4618 : 4 : return g_task_propagate_boolean (G_TASK (result), error);
4619 : : }
4620 : :
4621 : : /**
4622 : : * g_file_make_symbolic_link_finish: (virtual make_symbolic_link_finish)
4623 : : * @file: input #GFile
4624 : : * @result: a #GAsyncResult
4625 : : * @error: a #GError, or %NULL
4626 : : *
4627 : : * Finishes an asynchronous symbolic link creation, started with
4628 : : * g_file_make_symbolic_link_async().
4629 : : *
4630 : : * Returns: %TRUE on successful directory creation, %FALSE otherwise.
4631 : : * Since: 2.74
4632 : : */
4633 : : gboolean
4634 : 4 : g_file_make_symbolic_link_finish (GFile *file,
4635 : : GAsyncResult *result,
4636 : : GError **error)
4637 : : {
4638 : : GFileIface *iface;
4639 : :
4640 : 4 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4641 : 4 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4642 : :
4643 : 4 : iface = G_FILE_GET_IFACE (file);
4644 : : /* Default implementation should always be provided by GFileIface */
4645 : 4 : g_assert (iface->make_symbolic_link_finish != NULL);
4646 : :
4647 : 4 : return (* iface->make_symbolic_link_finish) (file, result, error);
4648 : : }
4649 : :
4650 : : /**
4651 : : * g_file_delete: (virtual delete_file)
4652 : : * @file: input #GFile
4653 : : * @cancellable: (nullable): optional #GCancellable object,
4654 : : * %NULL to ignore
4655 : : * @error: a #GError, or %NULL
4656 : : *
4657 : : * Deletes a file. If the @file is a directory, it will only be
4658 : : * deleted if it is empty. This has the same semantics as g_unlink().
4659 : : *
4660 : : * If @file doesn’t exist, %G_IO_ERROR_NOT_FOUND will be returned. This allows
4661 : : * for deletion to be implemented avoiding
4662 : : * [time-of-check to time-of-use races](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use):
4663 : : * |[
4664 : : * g_autoptr(GError) local_error = NULL;
4665 : : * if (!g_file_delete (my_file, my_cancellable, &local_error) &&
4666 : : * !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4667 : : * {
4668 : : * // deletion failed for some reason other than the file not existing:
4669 : : * // so report the error
4670 : : * g_warning ("Failed to delete %s: %s",
4671 : : * g_file_peek_path (my_file), local_error->message);
4672 : : * }
4673 : : * ]|
4674 : : *
4675 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4676 : : * triggering the cancellable object from another thread. If the operation
4677 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4678 : : *
4679 : : * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4680 : : */
4681 : : gboolean
4682 : 737 : g_file_delete (GFile *file,
4683 : : GCancellable *cancellable,
4684 : : GError **error)
4685 : : {
4686 : : GFileIface *iface;
4687 : :
4688 : 737 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4689 : :
4690 : 737 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4691 : 0 : return FALSE;
4692 : :
4693 : 737 : iface = G_FILE_GET_IFACE (file);
4694 : :
4695 : 737 : if (iface->delete_file == NULL)
4696 : : {
4697 : 0 : g_set_error_literal (error, G_IO_ERROR,
4698 : : G_IO_ERROR_NOT_SUPPORTED,
4699 : : _("Operation not supported"));
4700 : 0 : return FALSE;
4701 : : }
4702 : :
4703 : 737 : return (* iface->delete_file) (file, cancellable, error);
4704 : : }
4705 : :
4706 : : /**
4707 : : * g_file_delete_async: (virtual delete_file_async)
4708 : : * @file: input #GFile
4709 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4710 : : * @cancellable: (nullable): optional #GCancellable object,
4711 : : * %NULL to ignore
4712 : : * @callback: a #GAsyncReadyCallback to call
4713 : : * when the request is satisfied
4714 : : * @user_data: the data to pass to callback function
4715 : : *
4716 : : * Asynchronously delete a file. If the @file is a directory, it will
4717 : : * only be deleted if it is empty. This has the same semantics as
4718 : : * g_unlink().
4719 : : *
4720 : : * Since: 2.34
4721 : : */
4722 : : void
4723 : 1 : g_file_delete_async (GFile *file,
4724 : : int io_priority,
4725 : : GCancellable *cancellable,
4726 : : GAsyncReadyCallback callback,
4727 : : gpointer user_data)
4728 : : {
4729 : : GFileIface *iface;
4730 : :
4731 : 1 : g_return_if_fail (G_IS_FILE (file));
4732 : :
4733 : 1 : iface = G_FILE_GET_IFACE (file);
4734 : 1 : (* iface->delete_file_async) (file,
4735 : : io_priority,
4736 : : cancellable,
4737 : : callback,
4738 : : user_data);
4739 : : }
4740 : :
4741 : : /**
4742 : : * g_file_delete_finish: (virtual delete_file_finish)
4743 : : * @file: input #GFile
4744 : : * @result: a #GAsyncResult
4745 : : * @error: a #GError, or %NULL
4746 : : *
4747 : : * Finishes deleting a file started with g_file_delete_async().
4748 : : *
4749 : : * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4750 : : * Since: 2.34
4751 : : **/
4752 : : gboolean
4753 : 1 : g_file_delete_finish (GFile *file,
4754 : : GAsyncResult *result,
4755 : : GError **error)
4756 : : {
4757 : : GFileIface *iface;
4758 : :
4759 : 1 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4760 : 1 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4761 : :
4762 : 1 : if (g_async_result_legacy_propagate_error (result, error))
4763 : 0 : return FALSE;
4764 : :
4765 : 1 : iface = G_FILE_GET_IFACE (file);
4766 : 1 : return (* iface->delete_file_finish) (file, result, error);
4767 : : }
4768 : :
4769 : : /**
4770 : : * g_file_trash: (virtual trash)
4771 : : * @file: #GFile to send to trash
4772 : : * @cancellable: (nullable): optional #GCancellable object,
4773 : : * %NULL to ignore
4774 : : * @error: a #GError, or %NULL
4775 : : *
4776 : : * Sends @file to the "Trashcan", if possible. This is similar to
4777 : : * deleting it, but the user can recover it before emptying the trashcan.
4778 : : * Trashing is disabled for system mounts by default (see
4779 : : * g_unix_mount_entry_is_system_internal()), so this call can return the
4780 : : * %G_IO_ERROR_NOT_SUPPORTED error. Since GLib 2.66, the `x-gvfs-notrash` unix
4781 : : * mount option can be used to disable g_file_trash() support for particular
4782 : : * mounts, the %G_IO_ERROR_NOT_SUPPORTED error will be returned in that case.
4783 : : * Since 2.82, the `x-gvfs-trash` unix mount option can be used to enable
4784 : : * g_file_trash() support for particular system mounts.
4785 : : *
4786 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4787 : : * triggering the cancellable object from another thread. If the operation
4788 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4789 : : *
4790 : : * Returns: %TRUE on successful trash, %FALSE otherwise.
4791 : : */
4792 : : gboolean
4793 : 4 : g_file_trash (GFile *file,
4794 : : GCancellable *cancellable,
4795 : : GError **error)
4796 : : {
4797 : : GFileIface *iface;
4798 : :
4799 : 4 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4800 : :
4801 : 4 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4802 : 0 : return FALSE;
4803 : :
4804 : 4 : iface = G_FILE_GET_IFACE (file);
4805 : :
4806 : 4 : if (iface->trash == NULL)
4807 : : {
4808 : 0 : g_set_error_literal (error,
4809 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4810 : : _("Trash not supported"));
4811 : 0 : return FALSE;
4812 : : }
4813 : :
4814 : 4 : return (* iface->trash) (file, cancellable, error);
4815 : : }
4816 : :
4817 : : /**
4818 : : * g_file_trash_async: (virtual trash_async)
4819 : : * @file: input #GFile
4820 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4821 : : * @cancellable: (nullable): optional #GCancellable object,
4822 : : * %NULL to ignore
4823 : : * @callback: a #GAsyncReadyCallback to call
4824 : : * when the request is satisfied
4825 : : * @user_data: the data to pass to callback function
4826 : : *
4827 : : * Asynchronously sends @file to the Trash location, if possible.
4828 : : *
4829 : : * Since: 2.38
4830 : : */
4831 : : void
4832 : 0 : g_file_trash_async (GFile *file,
4833 : : int io_priority,
4834 : : GCancellable *cancellable,
4835 : : GAsyncReadyCallback callback,
4836 : : gpointer user_data)
4837 : : {
4838 : : GFileIface *iface;
4839 : :
4840 : 0 : g_return_if_fail (G_IS_FILE (file));
4841 : :
4842 : 0 : iface = G_FILE_GET_IFACE (file);
4843 : 0 : (* iface->trash_async) (file,
4844 : : io_priority,
4845 : : cancellable,
4846 : : callback,
4847 : : user_data);
4848 : : }
4849 : :
4850 : : /**
4851 : : * g_file_trash_finish: (virtual trash_finish)
4852 : : * @file: input #GFile
4853 : : * @result: a #GAsyncResult
4854 : : * @error: a #GError, or %NULL
4855 : : *
4856 : : * Finishes an asynchronous file trashing operation, started with
4857 : : * g_file_trash_async().
4858 : : *
4859 : : * Returns: %TRUE on successful trash, %FALSE otherwise.
4860 : : * Since: 2.38
4861 : : */
4862 : : gboolean
4863 : 0 : g_file_trash_finish (GFile *file,
4864 : : GAsyncResult *result,
4865 : : GError **error)
4866 : : {
4867 : : GFileIface *iface;
4868 : :
4869 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
4870 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4871 : :
4872 : 0 : iface = G_FILE_GET_IFACE (file);
4873 : 0 : return (* iface->trash_finish) (file, result, error);
4874 : : }
4875 : :
4876 : : /**
4877 : : * g_file_set_display_name:
4878 : : * @file: input #GFile
4879 : : * @display_name: a string
4880 : : * @cancellable: (nullable): optional #GCancellable object,
4881 : : * %NULL to ignore
4882 : : * @error: a #GError, or %NULL
4883 : : *
4884 : : * Renames @file to the specified display name.
4885 : : *
4886 : : * The display name is converted from UTF-8 to the correct encoding
4887 : : * for the target filesystem if possible and the @file is renamed to this.
4888 : : *
4889 : : * If you want to implement a rename operation in the user interface the
4890 : : * edit name (%G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4891 : : * initial value in the rename widget, and then the result after editing
4892 : : * should be passed to g_file_set_display_name().
4893 : : *
4894 : : * On success the resulting converted filename is returned.
4895 : : *
4896 : : * If @cancellable is not %NULL, then the operation can be cancelled by
4897 : : * triggering the cancellable object from another thread. If the operation
4898 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4899 : : *
4900 : : * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4901 : : * or %NULL if there was an error.
4902 : : * Free the returned object with g_object_unref().
4903 : : */
4904 : : GFile *
4905 : 0 : g_file_set_display_name (GFile *file,
4906 : : const gchar *display_name,
4907 : : GCancellable *cancellable,
4908 : : GError **error)
4909 : : {
4910 : : GFileIface *iface;
4911 : :
4912 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
4913 : 0 : g_return_val_if_fail (display_name != NULL, NULL);
4914 : :
4915 : 0 : if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4916 : : {
4917 : 0 : g_set_error (error,
4918 : : G_IO_ERROR,
4919 : : G_IO_ERROR_INVALID_ARGUMENT,
4920 : : _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4921 : 0 : return NULL;
4922 : : }
4923 : :
4924 : 0 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
4925 : 0 : return NULL;
4926 : :
4927 : 0 : iface = G_FILE_GET_IFACE (file);
4928 : :
4929 : 0 : return (* iface->set_display_name) (file, display_name, cancellable, error);
4930 : : }
4931 : :
4932 : : /**
4933 : : * g_file_set_display_name_async:
4934 : : * @file: input #GFile
4935 : : * @display_name: a string
4936 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
4937 : : * @cancellable: (nullable): optional #GCancellable object,
4938 : : * %NULL to ignore
4939 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
4940 : : * to call when the request is satisfied
4941 : : * @user_data: the data to pass to callback function
4942 : : *
4943 : : * Asynchronously sets the display name for a given #GFile.
4944 : : *
4945 : : * For more details, see g_file_set_display_name() which is
4946 : : * the synchronous version of this call.
4947 : : *
4948 : : * When the operation is finished, @callback will be called.
4949 : : * You can then call g_file_set_display_name_finish() to get
4950 : : * the result of the operation.
4951 : : */
4952 : : void
4953 : 0 : g_file_set_display_name_async (GFile *file,
4954 : : const gchar *display_name,
4955 : : gint io_priority,
4956 : : GCancellable *cancellable,
4957 : : GAsyncReadyCallback callback,
4958 : : gpointer user_data)
4959 : : {
4960 : : GFileIface *iface;
4961 : :
4962 : 0 : g_return_if_fail (G_IS_FILE (file));
4963 : 0 : g_return_if_fail (display_name != NULL);
4964 : :
4965 : 0 : iface = G_FILE_GET_IFACE (file);
4966 : 0 : (* iface->set_display_name_async) (file,
4967 : : display_name,
4968 : : io_priority,
4969 : : cancellable,
4970 : : callback,
4971 : : user_data);
4972 : : }
4973 : :
4974 : : /**
4975 : : * g_file_set_display_name_finish:
4976 : : * @file: input #GFile
4977 : : * @res: a #GAsyncResult
4978 : : * @error: a #GError, or %NULL
4979 : : *
4980 : : * Finishes setting a display name started with
4981 : : * g_file_set_display_name_async().
4982 : : *
4983 : : * Returns: (transfer full): a #GFile or %NULL on error.
4984 : : * Free the returned object with g_object_unref().
4985 : : */
4986 : : GFile *
4987 : 0 : g_file_set_display_name_finish (GFile *file,
4988 : : GAsyncResult *res,
4989 : : GError **error)
4990 : : {
4991 : : GFileIface *iface;
4992 : :
4993 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
4994 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4995 : :
4996 : 0 : if (g_async_result_legacy_propagate_error (res, error))
4997 : 0 : return NULL;
4998 : :
4999 : 0 : iface = G_FILE_GET_IFACE (file);
5000 : 0 : return (* iface->set_display_name_finish) (file, res, error);
5001 : : }
5002 : :
5003 : : /**
5004 : : * g_file_query_settable_attributes:
5005 : : * @file: input #GFile
5006 : : * @cancellable: (nullable): optional #GCancellable object,
5007 : : * %NULL to ignore
5008 : : * @error: a #GError, or %NULL
5009 : : *
5010 : : * Obtain the list of settable attributes for the file.
5011 : : *
5012 : : * Returns the type and full attribute name of all the attributes
5013 : : * that can be set on this file. This doesn't mean setting it will
5014 : : * always succeed though, you might get an access failure, or some
5015 : : * specific file may not support a specific attribute.
5016 : : *
5017 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5018 : : * triggering the cancellable object from another thread. If the operation
5019 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5020 : : *
5021 : : * Returns: (transfer full): a #GFileAttributeInfoList describing the settable attributes.
5022 : : * When you are done with it, release it with
5023 : : * g_file_attribute_info_list_unref()
5024 : : */
5025 : : GFileAttributeInfoList *
5026 : 72 : g_file_query_settable_attributes (GFile *file,
5027 : : GCancellable *cancellable,
5028 : : GError **error)
5029 : : {
5030 : : GFileIface *iface;
5031 : : GError *my_error;
5032 : : GFileAttributeInfoList *list;
5033 : :
5034 : 72 : g_return_val_if_fail (G_IS_FILE (file), NULL);
5035 : :
5036 : 72 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
5037 : 0 : return NULL;
5038 : :
5039 : 72 : iface = G_FILE_GET_IFACE (file);
5040 : :
5041 : 72 : if (iface->query_settable_attributes == NULL)
5042 : 0 : return g_file_attribute_info_list_new ();
5043 : :
5044 : 72 : my_error = NULL;
5045 : 72 : list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
5046 : :
5047 : 72 : if (list == NULL)
5048 : : {
5049 : 0 : if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
5050 : : {
5051 : 0 : list = g_file_attribute_info_list_new ();
5052 : 0 : g_error_free (my_error);
5053 : : }
5054 : : else
5055 : 0 : g_propagate_error (error, my_error);
5056 : : }
5057 : :
5058 : 72 : return list;
5059 : : }
5060 : :
5061 : : /**
5062 : : * g_file_query_writable_namespaces:
5063 : : * @file: input #GFile
5064 : : * @cancellable: (nullable): optional #GCancellable object,
5065 : : * %NULL to ignore
5066 : : * @error: a #GError, or %NULL
5067 : : *
5068 : : * Obtain the list of attribute namespaces where new attributes
5069 : : * can be created by a user. An example of this is extended
5070 : : * attributes (in the "xattr" namespace).
5071 : : *
5072 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5073 : : * triggering the cancellable object from another thread. If the operation
5074 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5075 : : *
5076 : : * Returns: (transfer full): a #GFileAttributeInfoList describing the writable namespaces.
5077 : : * When you are done with it, release it with
5078 : : * g_file_attribute_info_list_unref()
5079 : : */
5080 : : GFileAttributeInfoList *
5081 : 72 : g_file_query_writable_namespaces (GFile *file,
5082 : : GCancellable *cancellable,
5083 : : GError **error)
5084 : : {
5085 : : GFileIface *iface;
5086 : : GError *my_error;
5087 : : GFileAttributeInfoList *list;
5088 : :
5089 : 72 : g_return_val_if_fail (G_IS_FILE (file), NULL);
5090 : :
5091 : 72 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
5092 : 0 : return NULL;
5093 : :
5094 : 72 : iface = G_FILE_GET_IFACE (file);
5095 : :
5096 : 72 : if (iface->query_writable_namespaces == NULL)
5097 : 0 : return g_file_attribute_info_list_new ();
5098 : :
5099 : 72 : my_error = NULL;
5100 : 72 : list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
5101 : :
5102 : 72 : if (list == NULL)
5103 : : {
5104 : 0 : g_warn_if_reached();
5105 : 0 : list = g_file_attribute_info_list_new ();
5106 : : }
5107 : :
5108 : 72 : if (my_error != NULL)
5109 : : {
5110 : 0 : if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
5111 : : {
5112 : 0 : g_error_free (my_error);
5113 : : }
5114 : : else
5115 : 0 : g_propagate_error (error, my_error);
5116 : : }
5117 : :
5118 : 72 : return list;
5119 : : }
5120 : :
5121 : : /**
5122 : : * g_file_set_attribute:
5123 : : * @file: input #GFile
5124 : : * @attribute: a string containing the attribute's name
5125 : : * @type: The type of the attribute
5126 : : * @value_p: (nullable): a pointer to the value (or the pointer
5127 : : * itself if the type is a pointer type)
5128 : : * @flags: a set of #GFileQueryInfoFlags
5129 : : * @cancellable: (nullable): optional #GCancellable object,
5130 : : * %NULL to ignore
5131 : : * @error: a #GError, or %NULL
5132 : : *
5133 : : * Sets an attribute in the file with attribute name @attribute to @value_p.
5134 : : *
5135 : : * Some attributes can be unset by setting @type to
5136 : : * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
5137 : : *
5138 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5139 : : * triggering the cancellable object from another thread. If the operation
5140 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5141 : : *
5142 : : * Returns: %TRUE if the attribute was set, %FALSE otherwise.
5143 : : */
5144 : : gboolean
5145 : 209 : g_file_set_attribute (GFile *file,
5146 : : const gchar *attribute,
5147 : : GFileAttributeType type,
5148 : : gpointer value_p,
5149 : : GFileQueryInfoFlags flags,
5150 : : GCancellable *cancellable,
5151 : : GError **error)
5152 : : {
5153 : : GFileIface *iface;
5154 : :
5155 : 209 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5156 : 209 : g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
5157 : :
5158 : 209 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
5159 : 0 : return FALSE;
5160 : :
5161 : 209 : iface = G_FILE_GET_IFACE (file);
5162 : :
5163 : 209 : if (iface->set_attribute == NULL)
5164 : : {
5165 : 0 : g_set_error_literal (error, G_IO_ERROR,
5166 : : G_IO_ERROR_NOT_SUPPORTED,
5167 : : _("Operation not supported"));
5168 : 0 : return FALSE;
5169 : : }
5170 : :
5171 : 209 : return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
5172 : : }
5173 : :
5174 : : /**
5175 : : * g_file_set_attributes_from_info:
5176 : : * @file: input #GFile
5177 : : * @info: a #GFileInfo
5178 : : * @flags: #GFileQueryInfoFlags
5179 : : * @cancellable: (nullable): optional #GCancellable object,
5180 : : * %NULL to ignore
5181 : : * @error: a #GError, or %NULL
5182 : : *
5183 : : * Tries to set all attributes in the #GFileInfo on the target
5184 : : * values, not stopping on the first error.
5185 : : *
5186 : : * If there is any error during this operation then @error will
5187 : : * be set to the first error. Error on particular fields are flagged
5188 : : * by setting the "status" field in the attribute value to
5189 : : * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
5190 : : * also detect further errors.
5191 : : *
5192 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5193 : : * triggering the cancellable object from another thread. If the operation
5194 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5195 : : *
5196 : : * Returns: %FALSE if there was any error, %TRUE otherwise.
5197 : : */
5198 : : gboolean
5199 : 36 : g_file_set_attributes_from_info (GFile *file,
5200 : : GFileInfo *info,
5201 : : GFileQueryInfoFlags flags,
5202 : : GCancellable *cancellable,
5203 : : GError **error)
5204 : : {
5205 : : GFileIface *iface;
5206 : :
5207 : 36 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5208 : 36 : g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
5209 : :
5210 : 36 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
5211 : 0 : return FALSE;
5212 : :
5213 : 36 : g_file_info_clear_status (info);
5214 : :
5215 : 36 : iface = G_FILE_GET_IFACE (file);
5216 : :
5217 : 36 : return (* iface->set_attributes_from_info) (file,
5218 : : info,
5219 : : flags,
5220 : : cancellable,
5221 : : error);
5222 : : }
5223 : :
5224 : : static gboolean
5225 : 36 : g_file_real_set_attributes_from_info (GFile *file,
5226 : : GFileInfo *info,
5227 : : GFileQueryInfoFlags flags,
5228 : : GCancellable *cancellable,
5229 : : GError **error)
5230 : : {
5231 : : char **attributes;
5232 : : int i;
5233 : : gboolean res;
5234 : : GFileAttributeValue *value;
5235 : :
5236 : 36 : res = TRUE;
5237 : :
5238 : 36 : attributes = g_file_info_list_attributes (info, NULL);
5239 : :
5240 : 233 : for (i = 0; attributes[i] != NULL; i++)
5241 : : {
5242 : 197 : value = _g_file_info_get_attribute_value (info, attributes[i]);
5243 : :
5244 : 197 : if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
5245 : 153 : continue;
5246 : :
5247 : 88 : if (!g_file_set_attribute (file, attributes[i],
5248 : 44 : value->type, _g_file_attribute_value_peek_as_pointer (value),
5249 : : flags, cancellable, error))
5250 : : {
5251 : 40 : value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
5252 : 40 : res = FALSE;
5253 : : /* Don't set error multiple times */
5254 : 40 : error = NULL;
5255 : : }
5256 : : else
5257 : 4 : value->status = G_FILE_ATTRIBUTE_STATUS_SET;
5258 : : }
5259 : :
5260 : 36 : g_strfreev (attributes);
5261 : :
5262 : 36 : return res;
5263 : : }
5264 : :
5265 : : /**
5266 : : * g_file_set_attributes_async:
5267 : : * @file: input #GFile
5268 : : * @info: a #GFileInfo
5269 : : * @flags: a #GFileQueryInfoFlags
5270 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
5271 : : * @cancellable: (nullable): optional #GCancellable object,
5272 : : * %NULL to ignore
5273 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
5274 : : * to call when the request is satisfied
5275 : : * @user_data: the data to pass to callback function
5276 : : *
5277 : : * Asynchronously sets the attributes of @file with @info.
5278 : : *
5279 : : * For more details, see g_file_set_attributes_from_info(),
5280 : : * which is the synchronous version of this call.
5281 : : *
5282 : : * When the operation is finished, @callback will be called.
5283 : : * You can then call g_file_set_attributes_finish() to get
5284 : : * the result of the operation.
5285 : : */
5286 : : void
5287 : 0 : g_file_set_attributes_async (GFile *file,
5288 : : GFileInfo *info,
5289 : : GFileQueryInfoFlags flags,
5290 : : int io_priority,
5291 : : GCancellable *cancellable,
5292 : : GAsyncReadyCallback callback,
5293 : : gpointer user_data)
5294 : : {
5295 : : GFileIface *iface;
5296 : :
5297 : 0 : g_return_if_fail (G_IS_FILE (file));
5298 : 0 : g_return_if_fail (G_IS_FILE_INFO (info));
5299 : :
5300 : 0 : iface = G_FILE_GET_IFACE (file);
5301 : 0 : (* iface->set_attributes_async) (file,
5302 : : info,
5303 : : flags,
5304 : : io_priority,
5305 : : cancellable,
5306 : : callback,
5307 : : user_data);
5308 : : }
5309 : :
5310 : : /**
5311 : : * g_file_set_attributes_finish:
5312 : : * @file: input #GFile
5313 : : * @result: a #GAsyncResult
5314 : : * @info: (out) (transfer full): a #GFileInfo
5315 : : * @error: a #GError, or %NULL
5316 : : *
5317 : : * Finishes setting an attribute started in g_file_set_attributes_async().
5318 : : *
5319 : : * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
5320 : : */
5321 : : gboolean
5322 : 0 : g_file_set_attributes_finish (GFile *file,
5323 : : GAsyncResult *result,
5324 : : GFileInfo **info,
5325 : : GError **error)
5326 : : {
5327 : : GFileIface *iface;
5328 : :
5329 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5330 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5331 : :
5332 : : /* No standard handling of errors here, as we must set info even
5333 : : * on errors
5334 : : */
5335 : 0 : iface = G_FILE_GET_IFACE (file);
5336 : 0 : return (* iface->set_attributes_finish) (file, result, info, error);
5337 : : }
5338 : :
5339 : : /**
5340 : : * g_file_set_attribute_string:
5341 : : * @file: input #GFile
5342 : : * @attribute: a string containing the attribute's name
5343 : : * @value: a string containing the attribute's value
5344 : : * @flags: #GFileQueryInfoFlags
5345 : : * @cancellable: (nullable): optional #GCancellable object,
5346 : : * %NULL to ignore
5347 : : * @error: a #GError, or %NULL
5348 : : *
5349 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
5350 : : * If @attribute is of a different type, this operation will fail.
5351 : : *
5352 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5353 : : * triggering the cancellable object from another thread. If the operation
5354 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5355 : : *
5356 : : * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
5357 : : */
5358 : : gboolean
5359 : 0 : g_file_set_attribute_string (GFile *file,
5360 : : const char *attribute,
5361 : : const char *value,
5362 : : GFileQueryInfoFlags flags,
5363 : : GCancellable *cancellable,
5364 : : GError **error)
5365 : : {
5366 : 0 : return g_file_set_attribute (file, attribute,
5367 : : G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
5368 : : flags, cancellable, error);
5369 : : }
5370 : :
5371 : : /**
5372 : : * g_file_set_attribute_byte_string:
5373 : : * @file: input #GFile
5374 : : * @attribute: a string containing the attribute's name
5375 : : * @value: a string containing the attribute's new value
5376 : : * @flags: a #GFileQueryInfoFlags
5377 : : * @cancellable: (nullable): optional #GCancellable object,
5378 : : * %NULL to ignore
5379 : : * @error: a #GError, or %NULL
5380 : : *
5381 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
5382 : : * If @attribute is of a different type, this operation will fail,
5383 : : * returning %FALSE.
5384 : : *
5385 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5386 : : * triggering the cancellable object from another thread. If the operation
5387 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5388 : : *
5389 : : * Returns: %TRUE if the @attribute was successfully set to @value
5390 : : * in the @file, %FALSE otherwise.
5391 : : */
5392 : : gboolean
5393 : 0 : g_file_set_attribute_byte_string (GFile *file,
5394 : : const gchar *attribute,
5395 : : const gchar *value,
5396 : : GFileQueryInfoFlags flags,
5397 : : GCancellable *cancellable,
5398 : : GError **error)
5399 : : {
5400 : 0 : return g_file_set_attribute (file, attribute,
5401 : : G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
5402 : : flags, cancellable, error);
5403 : : }
5404 : :
5405 : : /**
5406 : : * g_file_set_attribute_uint32:
5407 : : * @file: input #GFile
5408 : : * @attribute: a string containing the attribute's name
5409 : : * @value: a #guint32 containing the attribute's new value
5410 : : * @flags: a #GFileQueryInfoFlags
5411 : : * @cancellable: (nullable): optional #GCancellable object,
5412 : : * %NULL to ignore
5413 : : * @error: a #GError, or %NULL
5414 : : *
5415 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
5416 : : * If @attribute is of a different type, this operation will fail.
5417 : : *
5418 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5419 : : * triggering the cancellable object from another thread. If the operation
5420 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5421 : : *
5422 : : * Returns: %TRUE if the @attribute was successfully set to @value
5423 : : * in the @file, %FALSE otherwise.
5424 : : */
5425 : : gboolean
5426 : 152 : g_file_set_attribute_uint32 (GFile *file,
5427 : : const gchar *attribute,
5428 : : guint32 value,
5429 : : GFileQueryInfoFlags flags,
5430 : : GCancellable *cancellable,
5431 : : GError **error)
5432 : : {
5433 : 152 : return g_file_set_attribute (file, attribute,
5434 : : G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
5435 : : flags, cancellable, error);
5436 : : }
5437 : :
5438 : : /**
5439 : : * g_file_set_attribute_int32:
5440 : : * @file: input #GFile
5441 : : * @attribute: a string containing the attribute's name
5442 : : * @value: a #gint32 containing the attribute's new value
5443 : : * @flags: a #GFileQueryInfoFlags
5444 : : * @cancellable: (nullable): optional #GCancellable object,
5445 : : * %NULL to ignore
5446 : : * @error: a #GError, or %NULL
5447 : : *
5448 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
5449 : : * If @attribute is of a different type, this operation will fail.
5450 : : *
5451 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5452 : : * triggering the cancellable object from another thread. If the operation
5453 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5454 : : *
5455 : : * Returns: %TRUE if the @attribute was successfully set to @value
5456 : : * in the @file, %FALSE otherwise.
5457 : : */
5458 : : gboolean
5459 : 0 : g_file_set_attribute_int32 (GFile *file,
5460 : : const gchar *attribute,
5461 : : gint32 value,
5462 : : GFileQueryInfoFlags flags,
5463 : : GCancellable *cancellable,
5464 : : GError **error)
5465 : : {
5466 : 0 : return g_file_set_attribute (file, attribute,
5467 : : G_FILE_ATTRIBUTE_TYPE_INT32, &value,
5468 : : flags, cancellable, error);
5469 : : }
5470 : :
5471 : : /**
5472 : : * g_file_set_attribute_uint64:
5473 : : * @file: input #GFile
5474 : : * @attribute: a string containing the attribute's name
5475 : : * @value: a #guint64 containing the attribute's new value
5476 : : * @flags: a #GFileQueryInfoFlags
5477 : : * @cancellable: (nullable): optional #GCancellable object,
5478 : : * %NULL to ignore
5479 : : * @error: a #GError, or %NULL
5480 : : *
5481 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
5482 : : * If @attribute is of a different type, this operation will fail.
5483 : : *
5484 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5485 : : * triggering the cancellable object from another thread. If the operation
5486 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5487 : : *
5488 : : * Returns: %TRUE if the @attribute was successfully set to @value
5489 : : * in the @file, %FALSE otherwise.
5490 : : */
5491 : : gboolean
5492 : 0 : g_file_set_attribute_uint64 (GFile *file,
5493 : : const gchar *attribute,
5494 : : guint64 value,
5495 : : GFileQueryInfoFlags flags,
5496 : : GCancellable *cancellable,
5497 : : GError **error)
5498 : : {
5499 : 0 : return g_file_set_attribute (file, attribute,
5500 : : G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
5501 : : flags, cancellable, error);
5502 : : }
5503 : :
5504 : : /**
5505 : : * g_file_set_attribute_int64:
5506 : : * @file: input #GFile
5507 : : * @attribute: a string containing the attribute's name
5508 : : * @value: a #guint64 containing the attribute's new value
5509 : : * @flags: a #GFileQueryInfoFlags
5510 : : * @cancellable: (nullable): optional #GCancellable object,
5511 : : * %NULL to ignore
5512 : : * @error: a #GError, or %NULL
5513 : : *
5514 : : * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
5515 : : * If @attribute is of a different type, this operation will fail.
5516 : : *
5517 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5518 : : * triggering the cancellable object from another thread. If the operation
5519 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5520 : : *
5521 : : * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
5522 : : */
5523 : : gboolean
5524 : 0 : g_file_set_attribute_int64 (GFile *file,
5525 : : const gchar *attribute,
5526 : : gint64 value,
5527 : : GFileQueryInfoFlags flags,
5528 : : GCancellable *cancellable,
5529 : : GError **error)
5530 : : {
5531 : 0 : return g_file_set_attribute (file, attribute,
5532 : : G_FILE_ATTRIBUTE_TYPE_INT64, &value,
5533 : : flags, cancellable, error);
5534 : : }
5535 : :
5536 : : /**
5537 : : * g_file_mount_mountable:
5538 : : * @file: input #GFile
5539 : : * @flags: flags affecting the operation
5540 : : * @mount_operation: (nullable): a #GMountOperation,
5541 : : * or %NULL to avoid user interaction
5542 : : * @cancellable: (nullable): optional #GCancellable object,
5543 : : * %NULL to ignore
5544 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
5545 : : * to call when the request is satisfied
5546 : : * @user_data: the data to pass to callback function
5547 : : *
5548 : : * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
5549 : : * Using @mount_operation, you can request callbacks when, for instance,
5550 : : * passwords are needed during authentication.
5551 : : *
5552 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5553 : : * triggering the cancellable object from another thread. If the operation
5554 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5555 : : *
5556 : : * When the operation is finished, @callback will be called.
5557 : : * You can then call g_file_mount_mountable_finish() to get
5558 : : * the result of the operation.
5559 : : */
5560 : : void
5561 : 0 : g_file_mount_mountable (GFile *file,
5562 : : GMountMountFlags flags,
5563 : : GMountOperation *mount_operation,
5564 : : GCancellable *cancellable,
5565 : : GAsyncReadyCallback callback,
5566 : : gpointer user_data)
5567 : : {
5568 : : GFileIface *iface;
5569 : :
5570 : 0 : g_return_if_fail (G_IS_FILE (file));
5571 : :
5572 : 0 : iface = G_FILE_GET_IFACE (file);
5573 : :
5574 : 0 : if (iface->mount_mountable == NULL)
5575 : : {
5576 : 0 : g_task_report_new_error (file, callback, user_data,
5577 : : g_file_mount_mountable,
5578 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5579 : 0 : _("Operation not supported"));
5580 : 0 : return;
5581 : : }
5582 : :
5583 : 0 : (* iface->mount_mountable) (file,
5584 : : flags,
5585 : : mount_operation,
5586 : : cancellable,
5587 : : callback,
5588 : : user_data);
5589 : : }
5590 : :
5591 : : /**
5592 : : * g_file_mount_mountable_finish:
5593 : : * @file: input #GFile
5594 : : * @result: a #GAsyncResult
5595 : : * @error: a #GError, or %NULL
5596 : : *
5597 : : * Finishes a mount operation. See g_file_mount_mountable() for details.
5598 : : *
5599 : : * Finish an asynchronous mount operation that was started
5600 : : * with g_file_mount_mountable().
5601 : : *
5602 : : * Returns: (transfer full): a #GFile or %NULL on error.
5603 : : * Free the returned object with g_object_unref().
5604 : : */
5605 : : GFile *
5606 : 0 : g_file_mount_mountable_finish (GFile *file,
5607 : : GAsyncResult *result,
5608 : : GError **error)
5609 : : {
5610 : : GFileIface *iface;
5611 : :
5612 : 0 : g_return_val_if_fail (G_IS_FILE (file), NULL);
5613 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
5614 : :
5615 : 0 : if (g_async_result_legacy_propagate_error (result, error))
5616 : 0 : return NULL;
5617 : 0 : else if (g_async_result_is_tagged (result, g_file_mount_mountable))
5618 : 0 : return g_task_propagate_pointer (G_TASK (result), error);
5619 : :
5620 : 0 : iface = G_FILE_GET_IFACE (file);
5621 : 0 : return (* iface->mount_mountable_finish) (file, result, error);
5622 : : }
5623 : :
5624 : : /**
5625 : : * g_file_unmount_mountable:
5626 : : * @file: input #GFile
5627 : : * @flags: flags affecting the operation
5628 : : * @cancellable: (nullable): optional #GCancellable object,
5629 : : * %NULL to ignore
5630 : : * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5631 : : * to call when the request is satisfied
5632 : : * @user_data: the data to pass to callback function
5633 : : *
5634 : : * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5635 : : *
5636 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5637 : : * triggering the cancellable object from another thread. If the operation
5638 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5639 : : *
5640 : : * When the operation is finished, @callback will be called.
5641 : : * You can then call g_file_unmount_mountable_finish() to get
5642 : : * the result of the operation.
5643 : : *
5644 : : * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5645 : : */
5646 : : void
5647 : 0 : g_file_unmount_mountable (GFile *file,
5648 : : GMountUnmountFlags flags,
5649 : : GCancellable *cancellable,
5650 : : GAsyncReadyCallback callback,
5651 : : gpointer user_data)
5652 : : {
5653 : : GFileIface *iface;
5654 : :
5655 : 0 : g_return_if_fail (G_IS_FILE (file));
5656 : :
5657 : 0 : iface = G_FILE_GET_IFACE (file);
5658 : :
5659 : 0 : if (iface->unmount_mountable == NULL)
5660 : : {
5661 : 0 : g_task_report_new_error (file, callback, user_data,
5662 : : g_file_unmount_mountable_with_operation,
5663 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5664 : 0 : _("Operation not supported"));
5665 : 0 : return;
5666 : : }
5667 : :
5668 : 0 : (* iface->unmount_mountable) (file,
5669 : : flags,
5670 : : cancellable,
5671 : : callback,
5672 : : user_data);
5673 : : }
5674 : :
5675 : : /**
5676 : : * g_file_unmount_mountable_finish:
5677 : : * @file: input #GFile
5678 : : * @result: a #GAsyncResult
5679 : : * @error: a #GError, or %NULL
5680 : : *
5681 : : * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5682 : : *
5683 : : * Finish an asynchronous unmount operation that was started
5684 : : * with g_file_unmount_mountable().
5685 : : *
5686 : : * Returns: %TRUE if the operation finished successfully.
5687 : : * %FALSE otherwise.
5688 : : *
5689 : : * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5690 : : * instead.
5691 : : */
5692 : : gboolean
5693 : 0 : g_file_unmount_mountable_finish (GFile *file,
5694 : : GAsyncResult *result,
5695 : : GError **error)
5696 : : {
5697 : : GFileIface *iface;
5698 : :
5699 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5700 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5701 : :
5702 : 0 : if (g_async_result_legacy_propagate_error (result, error))
5703 : 0 : return FALSE;
5704 : 0 : else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5705 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
5706 : :
5707 : 0 : iface = G_FILE_GET_IFACE (file);
5708 : 0 : return (* iface->unmount_mountable_finish) (file, result, error);
5709 : : }
5710 : :
5711 : : /**
5712 : : * g_file_unmount_mountable_with_operation:
5713 : : * @file: input #GFile
5714 : : * @flags: flags affecting the operation
5715 : : * @mount_operation: (nullable): a #GMountOperation,
5716 : : * or %NULL to avoid user interaction
5717 : : * @cancellable: (nullable): optional #GCancellable object,
5718 : : * %NULL to ignore
5719 : : * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5720 : : * to call when the request is satisfied
5721 : : * @user_data: the data to pass to callback function
5722 : : *
5723 : : * Unmounts a file of type %G_FILE_TYPE_MOUNTABLE.
5724 : : *
5725 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5726 : : * triggering the cancellable object from another thread. If the operation
5727 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5728 : : *
5729 : : * When the operation is finished, @callback will be called.
5730 : : * You can then call g_file_unmount_mountable_finish() to get
5731 : : * the result of the operation.
5732 : : *
5733 : : * Since: 2.22
5734 : : */
5735 : : void
5736 : 0 : g_file_unmount_mountable_with_operation (GFile *file,
5737 : : GMountUnmountFlags flags,
5738 : : GMountOperation *mount_operation,
5739 : : GCancellable *cancellable,
5740 : : GAsyncReadyCallback callback,
5741 : : gpointer user_data)
5742 : : {
5743 : : GFileIface *iface;
5744 : :
5745 : 0 : g_return_if_fail (G_IS_FILE (file));
5746 : :
5747 : 0 : iface = G_FILE_GET_IFACE (file);
5748 : :
5749 : 0 : if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
5750 : : {
5751 : 0 : g_task_report_new_error (file, callback, user_data,
5752 : : g_file_unmount_mountable_with_operation,
5753 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5754 : 0 : _("Operation not supported"));
5755 : 0 : return;
5756 : : }
5757 : :
5758 : 0 : if (iface->unmount_mountable_with_operation != NULL)
5759 : 0 : (* iface->unmount_mountable_with_operation) (file,
5760 : : flags,
5761 : : mount_operation,
5762 : : cancellable,
5763 : : callback,
5764 : : user_data);
5765 : : else
5766 : 0 : (* iface->unmount_mountable) (file,
5767 : : flags,
5768 : : cancellable,
5769 : : callback,
5770 : : user_data);
5771 : : }
5772 : :
5773 : : /**
5774 : : * g_file_unmount_mountable_with_operation_finish:
5775 : : * @file: input #GFile
5776 : : * @result: a #GAsyncResult
5777 : : * @error: a #GError, or %NULL
5778 : : *
5779 : : * Finishes an unmount operation,
5780 : : * see g_file_unmount_mountable_with_operation() for details.
5781 : : *
5782 : : * Finish an asynchronous unmount operation that was started
5783 : : * with g_file_unmount_mountable_with_operation().
5784 : : *
5785 : : * Returns: %TRUE if the operation finished successfully.
5786 : : * %FALSE otherwise.
5787 : : *
5788 : : * Since: 2.22
5789 : : */
5790 : : gboolean
5791 : 0 : g_file_unmount_mountable_with_operation_finish (GFile *file,
5792 : : GAsyncResult *result,
5793 : : GError **error)
5794 : : {
5795 : : GFileIface *iface;
5796 : :
5797 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5798 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5799 : :
5800 : 0 : if (g_async_result_legacy_propagate_error (result, error))
5801 : 0 : return FALSE;
5802 : 0 : else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5803 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
5804 : :
5805 : 0 : iface = G_FILE_GET_IFACE (file);
5806 : 0 : if (iface->unmount_mountable_with_operation_finish != NULL)
5807 : 0 : return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5808 : : else
5809 : 0 : return (* iface->unmount_mountable_finish) (file, result, error);
5810 : : }
5811 : :
5812 : : /**
5813 : : * g_file_eject_mountable:
5814 : : * @file: input #GFile
5815 : : * @flags: flags affecting the operation
5816 : : * @cancellable: (nullable): optional #GCancellable object,
5817 : : * %NULL to ignore
5818 : : * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5819 : : * to call when the request is satisfied
5820 : : * @user_data: the data to pass to callback function
5821 : : *
5822 : : * Starts an asynchronous eject on a mountable.
5823 : : * When this operation has completed, @callback will be called with
5824 : : * @user_user data, and the operation can be finalized with
5825 : : * g_file_eject_mountable_finish().
5826 : : *
5827 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5828 : : * triggering the cancellable object from another thread. If the operation
5829 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5830 : : *
5831 : : * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5832 : : */
5833 : : void
5834 : 0 : g_file_eject_mountable (GFile *file,
5835 : : GMountUnmountFlags flags,
5836 : : GCancellable *cancellable,
5837 : : GAsyncReadyCallback callback,
5838 : : gpointer user_data)
5839 : : {
5840 : : GFileIface *iface;
5841 : :
5842 : 0 : g_return_if_fail (G_IS_FILE (file));
5843 : :
5844 : 0 : iface = G_FILE_GET_IFACE (file);
5845 : :
5846 : 0 : if (iface->eject_mountable == NULL)
5847 : : {
5848 : 0 : g_task_report_new_error (file, callback, user_data,
5849 : : g_file_eject_mountable_with_operation,
5850 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5851 : 0 : _("Operation not supported"));
5852 : 0 : return;
5853 : : }
5854 : :
5855 : 0 : (* iface->eject_mountable) (file,
5856 : : flags,
5857 : : cancellable,
5858 : : callback,
5859 : : user_data);
5860 : : }
5861 : :
5862 : : /**
5863 : : * g_file_eject_mountable_finish:
5864 : : * @file: input #GFile
5865 : : * @result: a #GAsyncResult
5866 : : * @error: a #GError, or %NULL
5867 : : *
5868 : : * Finishes an asynchronous eject operation started by
5869 : : * g_file_eject_mountable().
5870 : : *
5871 : : * Returns: %TRUE if the @file was ejected successfully.
5872 : : * %FALSE otherwise.
5873 : : *
5874 : : * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5875 : : * instead.
5876 : : */
5877 : : gboolean
5878 : 0 : g_file_eject_mountable_finish (GFile *file,
5879 : : GAsyncResult *result,
5880 : : GError **error)
5881 : : {
5882 : : GFileIface *iface;
5883 : :
5884 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5885 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5886 : :
5887 : 0 : if (g_async_result_legacy_propagate_error (result, error))
5888 : 0 : return FALSE;
5889 : 0 : else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5890 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
5891 : :
5892 : 0 : iface = G_FILE_GET_IFACE (file);
5893 : 0 : return (* iface->eject_mountable_finish) (file, result, error);
5894 : : }
5895 : :
5896 : : /**
5897 : : * g_file_eject_mountable_with_operation:
5898 : : * @file: input #GFile
5899 : : * @flags: flags affecting the operation
5900 : : * @mount_operation: (nullable): a #GMountOperation,
5901 : : * or %NULL to avoid user interaction
5902 : : * @cancellable: (nullable): optional #GCancellable object,
5903 : : * %NULL to ignore
5904 : : * @callback: (scope async) (nullable) (closure user_data): a #GAsyncReadyCallback
5905 : : * to call when the request is satisfied
5906 : : * @user_data: the data to pass to callback function
5907 : : *
5908 : : * Starts an asynchronous eject on a mountable.
5909 : : * When this operation has completed, @callback will be called with
5910 : : * @user_user data, and the operation can be finalized with
5911 : : * g_file_eject_mountable_with_operation_finish().
5912 : : *
5913 : : * If @cancellable is not %NULL, then the operation can be cancelled by
5914 : : * triggering the cancellable object from another thread. If the operation
5915 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5916 : : *
5917 : : * Since: 2.22
5918 : : */
5919 : : void
5920 : 0 : g_file_eject_mountable_with_operation (GFile *file,
5921 : : GMountUnmountFlags flags,
5922 : : GMountOperation *mount_operation,
5923 : : GCancellable *cancellable,
5924 : : GAsyncReadyCallback callback,
5925 : : gpointer user_data)
5926 : : {
5927 : : GFileIface *iface;
5928 : :
5929 : 0 : g_return_if_fail (G_IS_FILE (file));
5930 : :
5931 : 0 : iface = G_FILE_GET_IFACE (file);
5932 : :
5933 : 0 : if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5934 : : {
5935 : 0 : g_task_report_new_error (file, callback, user_data,
5936 : : g_file_eject_mountable_with_operation,
5937 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5938 : 0 : _("Operation not supported"));
5939 : 0 : return;
5940 : : }
5941 : :
5942 : 0 : if (iface->eject_mountable_with_operation != NULL)
5943 : 0 : (* iface->eject_mountable_with_operation) (file,
5944 : : flags,
5945 : : mount_operation,
5946 : : cancellable,
5947 : : callback,
5948 : : user_data);
5949 : : else
5950 : 0 : (* iface->eject_mountable) (file,
5951 : : flags,
5952 : : cancellable,
5953 : : callback,
5954 : : user_data);
5955 : : }
5956 : :
5957 : : /**
5958 : : * g_file_eject_mountable_with_operation_finish:
5959 : : * @file: input #GFile
5960 : : * @result: a #GAsyncResult
5961 : : * @error: a #GError, or %NULL
5962 : : *
5963 : : * Finishes an asynchronous eject operation started by
5964 : : * g_file_eject_mountable_with_operation().
5965 : : *
5966 : : * Returns: %TRUE if the @file was ejected successfully.
5967 : : * %FALSE otherwise.
5968 : : *
5969 : : * Since: 2.22
5970 : : */
5971 : : gboolean
5972 : 0 : g_file_eject_mountable_with_operation_finish (GFile *file,
5973 : : GAsyncResult *result,
5974 : : GError **error)
5975 : : {
5976 : : GFileIface *iface;
5977 : :
5978 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
5979 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5980 : :
5981 : 0 : if (g_async_result_legacy_propagate_error (result, error))
5982 : 0 : return FALSE;
5983 : 0 : else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5984 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
5985 : :
5986 : 0 : iface = G_FILE_GET_IFACE (file);
5987 : 0 : if (iface->eject_mountable_with_operation_finish != NULL)
5988 : 0 : return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5989 : : else
5990 : 0 : return (* iface->eject_mountable_finish) (file, result, error);
5991 : : }
5992 : :
5993 : : /**
5994 : : * g_file_monitor_directory: (virtual monitor_dir)
5995 : : * @file: input #GFile
5996 : : * @flags: a set of #GFileMonitorFlags
5997 : : * @cancellable: (nullable): optional #GCancellable object,
5998 : : * %NULL to ignore
5999 : : * @error: a #GError, or %NULL
6000 : : *
6001 : : * Obtains a directory monitor for the given file.
6002 : : * This may fail if directory monitoring is not supported.
6003 : : *
6004 : : * If @cancellable is not %NULL, then the operation can be cancelled by
6005 : : * triggering the cancellable object from another thread. If the operation
6006 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6007 : : *
6008 : : * It does not make sense for @flags to contain
6009 : : * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
6010 : : * directories. It is not possible to monitor all the files in a
6011 : : * directory for changes made via hard links; if you want to do this then
6012 : : * you must register individual watches with g_file_monitor().
6013 : : *
6014 : : * Returns: (transfer full): a #GFileMonitor for the given @file,
6015 : : * or %NULL on error. Free the returned object with g_object_unref().
6016 : : */
6017 : : GFileMonitor *
6018 : 10 : g_file_monitor_directory (GFile *file,
6019 : : GFileMonitorFlags flags,
6020 : : GCancellable *cancellable,
6021 : : GError **error)
6022 : : {
6023 : : GFileIface *iface;
6024 : :
6025 : 10 : g_return_val_if_fail (G_IS_FILE (file), NULL);
6026 : 10 : g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
6027 : :
6028 : 10 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
6029 : 0 : return NULL;
6030 : :
6031 : 10 : iface = G_FILE_GET_IFACE (file);
6032 : :
6033 : 10 : if (iface->monitor_dir == NULL)
6034 : : {
6035 : 0 : g_set_error_literal (error, G_IO_ERROR,
6036 : : G_IO_ERROR_NOT_SUPPORTED,
6037 : : _("Operation not supported"));
6038 : 0 : return NULL;
6039 : : }
6040 : :
6041 : 10 : return (* iface->monitor_dir) (file, flags, cancellable, error);
6042 : : }
6043 : :
6044 : : /**
6045 : : * g_file_monitor_file:
6046 : : * @file: input #GFile
6047 : : * @flags: a set of #GFileMonitorFlags
6048 : : * @cancellable: (nullable): optional #GCancellable object,
6049 : : * %NULL to ignore
6050 : : * @error: a #GError, or %NULL
6051 : : *
6052 : : * Obtains a file monitor for the given file. If no file notification
6053 : : * mechanism exists, then regular polling of the file is used.
6054 : : *
6055 : : * If @cancellable is not %NULL, then the operation can be cancelled by
6056 : : * triggering the cancellable object from another thread. If the operation
6057 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6058 : : *
6059 : : * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
6060 : : * will also attempt to report changes made to the file via another
6061 : : * filename (ie, a hard link). Without this flag, you can only rely on
6062 : : * changes made through the filename contained in @file to be
6063 : : * reported. Using this flag may result in an increase in resource
6064 : : * usage, and may not have any effect depending on the #GFileMonitor
6065 : : * backend and/or filesystem type.
6066 : : *
6067 : : * Returns: (transfer full): a #GFileMonitor for the given @file,
6068 : : * or %NULL on error.
6069 : : * Free the returned object with g_object_unref().
6070 : : */
6071 : : GFileMonitor *
6072 : 116 : g_file_monitor_file (GFile *file,
6073 : : GFileMonitorFlags flags,
6074 : : GCancellable *cancellable,
6075 : : GError **error)
6076 : : {
6077 : : GFileIface *iface;
6078 : : GFileMonitor *monitor;
6079 : :
6080 : 116 : g_return_val_if_fail (G_IS_FILE (file), NULL);
6081 : :
6082 : 116 : if (g_cancellable_set_error_if_cancelled (cancellable, error))
6083 : 0 : return NULL;
6084 : :
6085 : 116 : iface = G_FILE_GET_IFACE (file);
6086 : :
6087 : 116 : monitor = NULL;
6088 : :
6089 : 116 : if (iface->monitor_file)
6090 : 116 : monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
6091 : :
6092 : : /* Fallback to polling */
6093 : 116 : if (monitor == NULL)
6094 : 0 : monitor = _g_poll_file_monitor_new (file);
6095 : :
6096 : 116 : return monitor;
6097 : : }
6098 : :
6099 : : /**
6100 : : * g_file_monitor:
6101 : : * @file: input #GFile
6102 : : * @flags: a set of #GFileMonitorFlags
6103 : : * @cancellable: (nullable): optional #GCancellable object,
6104 : : * %NULL to ignore
6105 : : * @error: a #GError, or %NULL
6106 : : *
6107 : : * Obtains a file or directory monitor for the given file,
6108 : : * depending on the type of the file.
6109 : : *
6110 : : * If @cancellable is not %NULL, then the operation can be cancelled by
6111 : : * triggering the cancellable object from another thread. If the operation
6112 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6113 : : *
6114 : : * Returns: (transfer full): a #GFileMonitor for the given @file,
6115 : : * or %NULL on error.
6116 : : * Free the returned object with g_object_unref().
6117 : : *
6118 : : * Since: 2.18
6119 : : */
6120 : : GFileMonitor *
6121 : 12 : g_file_monitor (GFile *file,
6122 : : GFileMonitorFlags flags,
6123 : : GCancellable *cancellable,
6124 : : GError **error)
6125 : : {
6126 : 12 : if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
6127 : 6 : return g_file_monitor_directory (file,
6128 : 6 : flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
6129 : : cancellable, error);
6130 : : else
6131 : 6 : return g_file_monitor_file (file, flags, cancellable, error);
6132 : : }
6133 : :
6134 : : /********************************************
6135 : : * Default implementation of async ops *
6136 : : ********************************************/
6137 : :
6138 : : typedef struct {
6139 : : char *attributes;
6140 : : GFileQueryInfoFlags flags;
6141 : : } QueryInfoAsyncData;
6142 : :
6143 : : static void
6144 : 16 : query_info_data_free (QueryInfoAsyncData *data)
6145 : : {
6146 : 16 : g_free (data->attributes);
6147 : 16 : g_free (data);
6148 : 16 : }
6149 : :
6150 : : static void
6151 : 10 : query_info_async_thread (GTask *task,
6152 : : gpointer object,
6153 : : gpointer task_data,
6154 : : GCancellable *cancellable)
6155 : : {
6156 : 10 : QueryInfoAsyncData *data = task_data;
6157 : : GFileInfo *info;
6158 : 10 : GError *error = NULL;
6159 : :
6160 : 10 : info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
6161 : 10 : if (info)
6162 : 3 : g_task_return_pointer (task, info, g_object_unref);
6163 : : else
6164 : 7 : g_task_return_error (task, error);
6165 : 10 : }
6166 : :
6167 : : static void
6168 : 10 : g_file_real_query_info_async (GFile *file,
6169 : : const char *attributes,
6170 : : GFileQueryInfoFlags flags,
6171 : : int io_priority,
6172 : : GCancellable *cancellable,
6173 : : GAsyncReadyCallback callback,
6174 : : gpointer user_data)
6175 : : {
6176 : : GTask *task;
6177 : : QueryInfoAsyncData *data;
6178 : :
6179 : 10 : data = g_new0 (QueryInfoAsyncData, 1);
6180 : 10 : data->attributes = g_strdup (attributes);
6181 : 10 : data->flags = flags;
6182 : :
6183 : 10 : task = g_task_new (file, cancellable, callback, user_data);
6184 : 10 : g_task_set_source_tag (task, g_file_real_query_info_async);
6185 : 10 : g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
6186 : 10 : g_task_set_priority (task, io_priority);
6187 : 10 : g_task_run_in_thread (task, query_info_async_thread);
6188 : 10 : g_object_unref (task);
6189 : 10 : }
6190 : :
6191 : : static GFileInfo *
6192 : 10 : g_file_real_query_info_finish (GFile *file,
6193 : : GAsyncResult *res,
6194 : : GError **error)
6195 : : {
6196 : 10 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6197 : :
6198 : 10 : return g_task_propagate_pointer (G_TASK (res), error);
6199 : : }
6200 : :
6201 : : static void
6202 : 0 : query_filesystem_info_async_thread (GTask *task,
6203 : : gpointer object,
6204 : : gpointer task_data,
6205 : : GCancellable *cancellable)
6206 : : {
6207 : 0 : const char *attributes = task_data;
6208 : : GFileInfo *info;
6209 : 0 : GError *error = NULL;
6210 : :
6211 : 0 : info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
6212 : 0 : if (info)
6213 : 0 : g_task_return_pointer (task, info, g_object_unref);
6214 : : else
6215 : 0 : g_task_return_error (task, error);
6216 : 0 : }
6217 : :
6218 : : static void
6219 : 0 : g_file_real_query_filesystem_info_async (GFile *file,
6220 : : const char *attributes,
6221 : : int io_priority,
6222 : : GCancellable *cancellable,
6223 : : GAsyncReadyCallback callback,
6224 : : gpointer user_data)
6225 : : {
6226 : : GTask *task;
6227 : :
6228 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6229 : 0 : g_task_set_source_tag (task, g_file_real_query_filesystem_info_async);
6230 : 0 : g_task_set_task_data (task, g_strdup (attributes), g_free);
6231 : 0 : g_task_set_priority (task, io_priority);
6232 : 0 : g_task_run_in_thread (task, query_filesystem_info_async_thread);
6233 : 0 : g_object_unref (task);
6234 : 0 : }
6235 : :
6236 : : static GFileInfo *
6237 : 0 : g_file_real_query_filesystem_info_finish (GFile *file,
6238 : : GAsyncResult *res,
6239 : : GError **error)
6240 : : {
6241 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6242 : :
6243 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6244 : : }
6245 : :
6246 : : static void
6247 : 6 : enumerate_children_async_thread (GTask *task,
6248 : : gpointer object,
6249 : : gpointer task_data,
6250 : : GCancellable *cancellable)
6251 : : {
6252 : 6 : QueryInfoAsyncData *data = task_data;
6253 : : GFileEnumerator *enumerator;
6254 : 6 : GError *error = NULL;
6255 : :
6256 : 6 : enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
6257 : 6 : if (error)
6258 : 0 : g_task_return_error (task, error);
6259 : : else
6260 : 6 : g_task_return_pointer (task, enumerator, g_object_unref);
6261 : 6 : }
6262 : :
6263 : : static void
6264 : 6 : g_file_real_enumerate_children_async (GFile *file,
6265 : : const char *attributes,
6266 : : GFileQueryInfoFlags flags,
6267 : : int io_priority,
6268 : : GCancellable *cancellable,
6269 : : GAsyncReadyCallback callback,
6270 : : gpointer user_data)
6271 : : {
6272 : : GTask *task;
6273 : : QueryInfoAsyncData *data;
6274 : :
6275 : 6 : data = g_new0 (QueryInfoAsyncData, 1);
6276 : 6 : data->attributes = g_strdup (attributes);
6277 : 6 : data->flags = flags;
6278 : :
6279 : 6 : task = g_task_new (file, cancellable, callback, user_data);
6280 : 6 : g_task_set_source_tag (task, g_file_real_enumerate_children_async);
6281 : 6 : g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
6282 : 6 : g_task_set_priority (task, io_priority);
6283 : 6 : g_task_run_in_thread (task, enumerate_children_async_thread);
6284 : 6 : g_object_unref (task);
6285 : 6 : }
6286 : :
6287 : : static GFileEnumerator *
6288 : 6 : g_file_real_enumerate_children_finish (GFile *file,
6289 : : GAsyncResult *res,
6290 : : GError **error)
6291 : : {
6292 : 6 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6293 : :
6294 : 6 : return g_task_propagate_pointer (G_TASK (res), error);
6295 : : }
6296 : :
6297 : : static void
6298 : 11 : open_read_async_thread (GTask *task,
6299 : : gpointer object,
6300 : : gpointer task_data,
6301 : : GCancellable *cancellable)
6302 : : {
6303 : : GFileInputStream *stream;
6304 : 11 : GError *error = NULL;
6305 : :
6306 : 11 : stream = g_file_read (G_FILE (object), cancellable, &error);
6307 : 11 : if (stream)
6308 : 11 : g_task_return_pointer (task, stream, g_object_unref);
6309 : : else
6310 : 0 : g_task_return_error (task, error);
6311 : 11 : }
6312 : :
6313 : : static void
6314 : 11 : g_file_real_read_async (GFile *file,
6315 : : int io_priority,
6316 : : GCancellable *cancellable,
6317 : : GAsyncReadyCallback callback,
6318 : : gpointer user_data)
6319 : : {
6320 : : GTask *task;
6321 : :
6322 : 11 : task = g_task_new (file, cancellable, callback, user_data);
6323 : 11 : g_task_set_source_tag (task, g_file_real_read_async);
6324 : 11 : g_task_set_priority (task, io_priority);
6325 : 11 : g_task_run_in_thread (task, open_read_async_thread);
6326 : 11 : g_object_unref (task);
6327 : 11 : }
6328 : :
6329 : : static GFileInputStream *
6330 : 11 : g_file_real_read_finish (GFile *file,
6331 : : GAsyncResult *res,
6332 : : GError **error)
6333 : : {
6334 : 11 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6335 : :
6336 : 11 : return g_task_propagate_pointer (G_TASK (res), error);
6337 : : }
6338 : :
6339 : : static void
6340 : 0 : append_to_async_thread (GTask *task,
6341 : : gpointer source_object,
6342 : : gpointer task_data,
6343 : : GCancellable *cancellable)
6344 : : {
6345 : 0 : GFileCreateFlags *data = task_data;
6346 : : GFileOutputStream *stream;
6347 : 0 : GError *error = NULL;
6348 : :
6349 : 0 : stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
6350 : 0 : if (stream)
6351 : 0 : g_task_return_pointer (task, stream, g_object_unref);
6352 : : else
6353 : 0 : g_task_return_error (task, error);
6354 : 0 : }
6355 : :
6356 : : static void
6357 : 0 : g_file_real_append_to_async (GFile *file,
6358 : : GFileCreateFlags flags,
6359 : : int io_priority,
6360 : : GCancellable *cancellable,
6361 : : GAsyncReadyCallback callback,
6362 : : gpointer user_data)
6363 : : {
6364 : : GFileCreateFlags *data;
6365 : : GTask *task;
6366 : :
6367 : 0 : data = g_new0 (GFileCreateFlags, 1);
6368 : 0 : *data = flags;
6369 : :
6370 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6371 : 0 : g_task_set_source_tag (task, g_file_real_append_to_async);
6372 : 0 : g_task_set_task_data (task, data, g_free);
6373 : 0 : g_task_set_priority (task, io_priority);
6374 : :
6375 : 0 : g_task_run_in_thread (task, append_to_async_thread);
6376 : 0 : g_object_unref (task);
6377 : 0 : }
6378 : :
6379 : : static GFileOutputStream *
6380 : 0 : g_file_real_append_to_finish (GFile *file,
6381 : : GAsyncResult *res,
6382 : : GError **error)
6383 : : {
6384 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6385 : :
6386 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6387 : : }
6388 : :
6389 : : static void
6390 : 5 : create_async_thread (GTask *task,
6391 : : gpointer source_object,
6392 : : gpointer task_data,
6393 : : GCancellable *cancellable)
6394 : : {
6395 : 5 : GFileCreateFlags *data = task_data;
6396 : : GFileOutputStream *stream;
6397 : 5 : GError *error = NULL;
6398 : :
6399 : 5 : stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
6400 : 5 : if (stream)
6401 : 5 : g_task_return_pointer (task, stream, g_object_unref);
6402 : : else
6403 : 0 : g_task_return_error (task, error);
6404 : 5 : }
6405 : :
6406 : : static void
6407 : 5 : g_file_real_create_async (GFile *file,
6408 : : GFileCreateFlags flags,
6409 : : int io_priority,
6410 : : GCancellable *cancellable,
6411 : : GAsyncReadyCallback callback,
6412 : : gpointer user_data)
6413 : : {
6414 : : GFileCreateFlags *data;
6415 : : GTask *task;
6416 : :
6417 : 5 : data = g_new0 (GFileCreateFlags, 1);
6418 : 5 : *data = flags;
6419 : :
6420 : 5 : task = g_task_new (file, cancellable, callback, user_data);
6421 : 5 : g_task_set_source_tag (task, g_file_real_create_async);
6422 : 5 : g_task_set_task_data (task, data, g_free);
6423 : 5 : g_task_set_priority (task, io_priority);
6424 : :
6425 : 5 : g_task_run_in_thread (task, create_async_thread);
6426 : 5 : g_object_unref (task);
6427 : 5 : }
6428 : :
6429 : : static GFileOutputStream *
6430 : 5 : g_file_real_create_finish (GFile *file,
6431 : : GAsyncResult *res,
6432 : : GError **error)
6433 : : {
6434 : 5 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6435 : :
6436 : 5 : return g_task_propagate_pointer (G_TASK (res), error);
6437 : : }
6438 : :
6439 : : typedef struct {
6440 : : GFileOutputStream *stream;
6441 : : char *etag;
6442 : : gboolean make_backup;
6443 : : GFileCreateFlags flags;
6444 : : } ReplaceAsyncData;
6445 : :
6446 : : static void
6447 : 2 : replace_async_data_free (ReplaceAsyncData *data)
6448 : : {
6449 : 2 : if (data->stream)
6450 : 0 : g_object_unref (data->stream);
6451 : 2 : g_free (data->etag);
6452 : 2 : g_free (data);
6453 : 2 : }
6454 : :
6455 : : static void
6456 : 2 : replace_async_thread (GTask *task,
6457 : : gpointer source_object,
6458 : : gpointer task_data,
6459 : : GCancellable *cancellable)
6460 : : {
6461 : : GFileOutputStream *stream;
6462 : 2 : ReplaceAsyncData *data = task_data;
6463 : 2 : GError *error = NULL;
6464 : :
6465 : 2 : stream = g_file_replace (G_FILE (source_object),
6466 : 2 : data->etag,
6467 : : data->make_backup,
6468 : : data->flags,
6469 : : cancellable,
6470 : : &error);
6471 : :
6472 : 2 : if (stream)
6473 : 2 : g_task_return_pointer (task, stream, g_object_unref);
6474 : : else
6475 : 0 : g_task_return_error (task, error);
6476 : 2 : }
6477 : :
6478 : : static void
6479 : 2 : g_file_real_replace_async (GFile *file,
6480 : : const char *etag,
6481 : : gboolean make_backup,
6482 : : GFileCreateFlags flags,
6483 : : int io_priority,
6484 : : GCancellable *cancellable,
6485 : : GAsyncReadyCallback callback,
6486 : : gpointer user_data)
6487 : : {
6488 : : GTask *task;
6489 : : ReplaceAsyncData *data;
6490 : :
6491 : 2 : data = g_new0 (ReplaceAsyncData, 1);
6492 : 2 : data->etag = g_strdup (etag);
6493 : 2 : data->make_backup = make_backup;
6494 : 2 : data->flags = flags;
6495 : :
6496 : 2 : task = g_task_new (file, cancellable, callback, user_data);
6497 : 2 : g_task_set_source_tag (task, g_file_real_replace_async);
6498 : 2 : g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
6499 : 2 : g_task_set_priority (task, io_priority);
6500 : :
6501 : 2 : g_task_run_in_thread (task, replace_async_thread);
6502 : 2 : g_object_unref (task);
6503 : 2 : }
6504 : :
6505 : : static GFileOutputStream *
6506 : 2 : g_file_real_replace_finish (GFile *file,
6507 : : GAsyncResult *res,
6508 : : GError **error)
6509 : : {
6510 : 2 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6511 : :
6512 : 2 : return g_task_propagate_pointer (G_TASK (res), error);
6513 : : }
6514 : :
6515 : : static void
6516 : 1 : delete_async_thread (GTask *task,
6517 : : gpointer object,
6518 : : gpointer task_data,
6519 : : GCancellable *cancellable)
6520 : : {
6521 : 1 : GError *error = NULL;
6522 : :
6523 : 1 : if (g_file_delete (G_FILE (object), cancellable, &error))
6524 : 1 : g_task_return_boolean (task, TRUE);
6525 : : else
6526 : 0 : g_task_return_error (task, error);
6527 : 1 : }
6528 : :
6529 : : static void
6530 : 1 : g_file_real_delete_async (GFile *file,
6531 : : int io_priority,
6532 : : GCancellable *cancellable,
6533 : : GAsyncReadyCallback callback,
6534 : : gpointer user_data)
6535 : : {
6536 : : GTask *task;
6537 : :
6538 : 1 : task = g_task_new (file, cancellable, callback, user_data);
6539 : 1 : g_task_set_source_tag (task, g_file_real_delete_async);
6540 : 1 : g_task_set_priority (task, io_priority);
6541 : 1 : g_task_run_in_thread (task, delete_async_thread);
6542 : 1 : g_object_unref (task);
6543 : 1 : }
6544 : :
6545 : : static gboolean
6546 : 1 : g_file_real_delete_finish (GFile *file,
6547 : : GAsyncResult *res,
6548 : : GError **error)
6549 : : {
6550 : 1 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6551 : :
6552 : 1 : return g_task_propagate_boolean (G_TASK (res), error);
6553 : : }
6554 : :
6555 : : static void
6556 : 0 : trash_async_thread (GTask *task,
6557 : : gpointer object,
6558 : : gpointer task_data,
6559 : : GCancellable *cancellable)
6560 : : {
6561 : 0 : GError *error = NULL;
6562 : :
6563 : 0 : if (g_file_trash (G_FILE (object), cancellable, &error))
6564 : 0 : g_task_return_boolean (task, TRUE);
6565 : : else
6566 : 0 : g_task_return_error (task, error);
6567 : 0 : }
6568 : :
6569 : : static void
6570 : 0 : g_file_real_trash_async (GFile *file,
6571 : : int io_priority,
6572 : : GCancellable *cancellable,
6573 : : GAsyncReadyCallback callback,
6574 : : gpointer user_data)
6575 : : {
6576 : : GTask *task;
6577 : :
6578 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6579 : 0 : g_task_set_source_tag (task, g_file_real_trash_async);
6580 : 0 : g_task_set_priority (task, io_priority);
6581 : 0 : g_task_run_in_thread (task, trash_async_thread);
6582 : 0 : g_object_unref (task);
6583 : 0 : }
6584 : :
6585 : : static gboolean
6586 : 0 : g_file_real_trash_finish (GFile *file,
6587 : : GAsyncResult *res,
6588 : : GError **error)
6589 : : {
6590 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6591 : :
6592 : 0 : return g_task_propagate_boolean (G_TASK (res), error);
6593 : : }
6594 : :
6595 : :
6596 : : typedef struct {
6597 : : GFile *source; /* (owned) */
6598 : : GFile *destination; /* (owned) */
6599 : : GFileCopyFlags flags;
6600 : : GFileProgressCallback progress_cb;
6601 : : gpointer progress_cb_data;
6602 : : } MoveAsyncData;
6603 : :
6604 : : static void
6605 : 2 : move_async_data_free (MoveAsyncData *data)
6606 : : {
6607 : 2 : g_object_unref (data->source);
6608 : 2 : g_object_unref (data->destination);
6609 : 2 : g_slice_free (MoveAsyncData, data);
6610 : 2 : }
6611 : :
6612 : : typedef struct {
6613 : : MoveAsyncData *data; /* (unowned) */
6614 : : goffset current_num_bytes;
6615 : : goffset total_num_bytes;
6616 : : } MoveProgressData;
6617 : :
6618 : : static gboolean
6619 : 2 : move_async_progress_in_main (gpointer user_data)
6620 : : {
6621 : 2 : MoveProgressData *progress = user_data;
6622 : 2 : MoveAsyncData *data = progress->data;
6623 : :
6624 : 2 : data->progress_cb (progress->current_num_bytes,
6625 : : progress->total_num_bytes,
6626 : : data->progress_cb_data);
6627 : :
6628 : 2 : return G_SOURCE_REMOVE;
6629 : : }
6630 : :
6631 : : static void
6632 : 2 : move_async_progress_callback (goffset current_num_bytes,
6633 : : goffset total_num_bytes,
6634 : : gpointer user_data)
6635 : : {
6636 : 2 : GTask *task = user_data;
6637 : 2 : MoveAsyncData *data = g_task_get_task_data (task);
6638 : : MoveProgressData *progress;
6639 : :
6640 : 2 : progress = g_new0 (MoveProgressData, 1);
6641 : 2 : progress->data = data;
6642 : 2 : progress->current_num_bytes = current_num_bytes;
6643 : 2 : progress->total_num_bytes = total_num_bytes;
6644 : :
6645 : 2 : g_main_context_invoke_full (g_task_get_context (task),
6646 : : g_task_get_priority (task),
6647 : : move_async_progress_in_main,
6648 : : g_steal_pointer (&progress),
6649 : : g_free);
6650 : 2 : }
6651 : :
6652 : : static void
6653 : 2 : move_async_thread (GTask *task,
6654 : : gpointer source,
6655 : : gpointer task_data,
6656 : : GCancellable *cancellable)
6657 : : {
6658 : 2 : MoveAsyncData *data = task_data;
6659 : : gboolean result;
6660 : 2 : GError *error = NULL;
6661 : :
6662 : 2 : result = g_file_move (data->source,
6663 : : data->destination,
6664 : : data->flags,
6665 : : cancellable,
6666 : 2 : (data->progress_cb != NULL) ? move_async_progress_callback : NULL,
6667 : : task,
6668 : : &error);
6669 : 2 : if (result)
6670 : 2 : g_task_return_boolean (task, TRUE);
6671 : : else
6672 : 0 : g_task_return_error (task, g_steal_pointer (&error));
6673 : 2 : }
6674 : :
6675 : : static void
6676 : 2 : g_file_real_move_async (GFile *source,
6677 : : GFile *destination,
6678 : : GFileCopyFlags flags,
6679 : : int io_priority,
6680 : : GCancellable *cancellable,
6681 : : GFileProgressCallback progress_callback,
6682 : : gpointer progress_callback_data,
6683 : : GAsyncReadyCallback callback,
6684 : : gpointer user_data)
6685 : : {
6686 : : GTask *task;
6687 : : MoveAsyncData *data;
6688 : :
6689 : 2 : data = g_slice_new0 (MoveAsyncData);
6690 : 2 : data->source = g_object_ref (source);
6691 : 2 : data->destination = g_object_ref (destination);
6692 : 2 : data->flags = flags;
6693 : 2 : data->progress_cb = progress_callback;
6694 : 2 : data->progress_cb_data = progress_callback_data;
6695 : :
6696 : 2 : task = g_task_new (source, cancellable, callback, user_data);
6697 : 2 : g_task_set_source_tag (task, g_file_real_move_async);
6698 : 2 : g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) move_async_data_free);
6699 : 2 : g_task_set_priority (task, io_priority);
6700 : 2 : g_task_run_in_thread (task, move_async_thread);
6701 : 2 : g_object_unref (task);
6702 : 2 : }
6703 : :
6704 : : static gboolean
6705 : 3 : g_file_real_move_finish (GFile *file,
6706 : : GAsyncResult *result,
6707 : : GError **error)
6708 : : {
6709 : 3 : g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
6710 : :
6711 : 3 : return g_task_propagate_boolean (G_TASK (result), error);
6712 : : }
6713 : :
6714 : : static void
6715 : 0 : make_directory_async_thread (GTask *task,
6716 : : gpointer object,
6717 : : gpointer task_data,
6718 : : GCancellable *cancellable)
6719 : : {
6720 : 0 : GError *error = NULL;
6721 : :
6722 : 0 : if (g_file_make_directory (G_FILE (object), cancellable, &error))
6723 : 0 : g_task_return_boolean (task, TRUE);
6724 : : else
6725 : 0 : g_task_return_error (task, error);
6726 : 0 : }
6727 : :
6728 : : static void
6729 : 0 : g_file_real_make_directory_async (GFile *file,
6730 : : int io_priority,
6731 : : GCancellable *cancellable,
6732 : : GAsyncReadyCallback callback,
6733 : : gpointer user_data)
6734 : : {
6735 : : GTask *task;
6736 : :
6737 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6738 : 0 : g_task_set_source_tag (task, g_file_real_make_directory_async);
6739 : 0 : g_task_set_priority (task, io_priority);
6740 : 0 : g_task_run_in_thread (task, make_directory_async_thread);
6741 : 0 : g_object_unref (task);
6742 : 0 : }
6743 : :
6744 : : static gboolean
6745 : 0 : g_file_real_make_directory_finish (GFile *file,
6746 : : GAsyncResult *res,
6747 : : GError **error)
6748 : : {
6749 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6750 : :
6751 : 0 : return g_task_propagate_boolean (G_TASK (res), error);
6752 : : }
6753 : :
6754 : : static void
6755 : 0 : open_readwrite_async_thread (GTask *task,
6756 : : gpointer object,
6757 : : gpointer task_data,
6758 : : GCancellable *cancellable)
6759 : : {
6760 : : GFileIOStream *stream;
6761 : 0 : GError *error = NULL;
6762 : :
6763 : 0 : stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
6764 : :
6765 : 0 : if (stream == NULL)
6766 : 0 : g_task_return_error (task, error);
6767 : : else
6768 : 0 : g_task_return_pointer (task, stream, g_object_unref);
6769 : 0 : }
6770 : :
6771 : : static void
6772 : 0 : g_file_real_open_readwrite_async (GFile *file,
6773 : : int io_priority,
6774 : : GCancellable *cancellable,
6775 : : GAsyncReadyCallback callback,
6776 : : gpointer user_data)
6777 : : {
6778 : : GTask *task;
6779 : :
6780 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6781 : 0 : g_task_set_source_tag (task, g_file_real_open_readwrite_async);
6782 : 0 : g_task_set_priority (task, io_priority);
6783 : :
6784 : 0 : g_task_run_in_thread (task, open_readwrite_async_thread);
6785 : 0 : g_object_unref (task);
6786 : 0 : }
6787 : :
6788 : : static GFileIOStream *
6789 : 0 : g_file_real_open_readwrite_finish (GFile *file,
6790 : : GAsyncResult *res,
6791 : : GError **error)
6792 : : {
6793 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6794 : :
6795 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6796 : : }
6797 : :
6798 : : static void
6799 : 0 : create_readwrite_async_thread (GTask *task,
6800 : : gpointer object,
6801 : : gpointer task_data,
6802 : : GCancellable *cancellable)
6803 : : {
6804 : 0 : GFileCreateFlags *data = task_data;
6805 : : GFileIOStream *stream;
6806 : 0 : GError *error = NULL;
6807 : :
6808 : 0 : stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
6809 : :
6810 : 0 : if (stream == NULL)
6811 : 0 : g_task_return_error (task, error);
6812 : : else
6813 : 0 : g_task_return_pointer (task, stream, g_object_unref);
6814 : 0 : }
6815 : :
6816 : : static void
6817 : 0 : g_file_real_create_readwrite_async (GFile *file,
6818 : : GFileCreateFlags flags,
6819 : : int io_priority,
6820 : : GCancellable *cancellable,
6821 : : GAsyncReadyCallback callback,
6822 : : gpointer user_data)
6823 : : {
6824 : : GFileCreateFlags *data;
6825 : : GTask *task;
6826 : :
6827 : 0 : data = g_new0 (GFileCreateFlags, 1);
6828 : 0 : *data = flags;
6829 : :
6830 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6831 : 0 : g_task_set_source_tag (task, g_file_real_create_readwrite_async);
6832 : 0 : g_task_set_task_data (task, data, g_free);
6833 : 0 : g_task_set_priority (task, io_priority);
6834 : :
6835 : 0 : g_task_run_in_thread (task, create_readwrite_async_thread);
6836 : 0 : g_object_unref (task);
6837 : 0 : }
6838 : :
6839 : : static GFileIOStream *
6840 : 0 : g_file_real_create_readwrite_finish (GFile *file,
6841 : : GAsyncResult *res,
6842 : : GError **error)
6843 : : {
6844 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6845 : :
6846 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6847 : : }
6848 : :
6849 : : typedef struct {
6850 : : char *etag;
6851 : : gboolean make_backup;
6852 : : GFileCreateFlags flags;
6853 : : } ReplaceRWAsyncData;
6854 : :
6855 : : static void
6856 : 0 : replace_rw_async_data_free (ReplaceRWAsyncData *data)
6857 : : {
6858 : 0 : g_free (data->etag);
6859 : 0 : g_free (data);
6860 : 0 : }
6861 : :
6862 : : static void
6863 : 0 : replace_readwrite_async_thread (GTask *task,
6864 : : gpointer object,
6865 : : gpointer task_data,
6866 : : GCancellable *cancellable)
6867 : : {
6868 : : GFileIOStream *stream;
6869 : 0 : GError *error = NULL;
6870 : 0 : ReplaceRWAsyncData *data = task_data;
6871 : :
6872 : 0 : stream = g_file_replace_readwrite (G_FILE (object),
6873 : 0 : data->etag,
6874 : : data->make_backup,
6875 : : data->flags,
6876 : : cancellable,
6877 : : &error);
6878 : :
6879 : 0 : if (stream == NULL)
6880 : 0 : g_task_return_error (task, error);
6881 : : else
6882 : 0 : g_task_return_pointer (task, stream, g_object_unref);
6883 : 0 : }
6884 : :
6885 : : static void
6886 : 0 : g_file_real_replace_readwrite_async (GFile *file,
6887 : : const char *etag,
6888 : : gboolean make_backup,
6889 : : GFileCreateFlags flags,
6890 : : int io_priority,
6891 : : GCancellable *cancellable,
6892 : : GAsyncReadyCallback callback,
6893 : : gpointer user_data)
6894 : : {
6895 : : GTask *task;
6896 : : ReplaceRWAsyncData *data;
6897 : :
6898 : 0 : data = g_new0 (ReplaceRWAsyncData, 1);
6899 : 0 : data->etag = g_strdup (etag);
6900 : 0 : data->make_backup = make_backup;
6901 : 0 : data->flags = flags;
6902 : :
6903 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6904 : 0 : g_task_set_source_tag (task, g_file_real_replace_readwrite_async);
6905 : 0 : g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6906 : 0 : g_task_set_priority (task, io_priority);
6907 : :
6908 : 0 : g_task_run_in_thread (task, replace_readwrite_async_thread);
6909 : 0 : g_object_unref (task);
6910 : 0 : }
6911 : :
6912 : : static GFileIOStream *
6913 : 0 : g_file_real_replace_readwrite_finish (GFile *file,
6914 : : GAsyncResult *res,
6915 : : GError **error)
6916 : : {
6917 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6918 : :
6919 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6920 : : }
6921 : :
6922 : : static void
6923 : 0 : set_display_name_async_thread (GTask *task,
6924 : : gpointer object,
6925 : : gpointer task_data,
6926 : : GCancellable *cancellable)
6927 : : {
6928 : 0 : GError *error = NULL;
6929 : 0 : char *name = task_data;
6930 : : GFile *file;
6931 : :
6932 : 0 : file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6933 : :
6934 : 0 : if (file == NULL)
6935 : 0 : g_task_return_error (task, error);
6936 : : else
6937 : 0 : g_task_return_pointer (task, file, g_object_unref);
6938 : 0 : }
6939 : :
6940 : : static void
6941 : 0 : g_file_real_set_display_name_async (GFile *file,
6942 : : const char *display_name,
6943 : : int io_priority,
6944 : : GCancellable *cancellable,
6945 : : GAsyncReadyCallback callback,
6946 : : gpointer user_data)
6947 : : {
6948 : : GTask *task;
6949 : :
6950 : 0 : task = g_task_new (file, cancellable, callback, user_data);
6951 : 0 : g_task_set_source_tag (task, g_file_real_set_display_name_async);
6952 : 0 : g_task_set_task_data (task, g_strdup (display_name), g_free);
6953 : 0 : g_task_set_priority (task, io_priority);
6954 : :
6955 : 0 : g_task_run_in_thread (task, set_display_name_async_thread);
6956 : 0 : g_object_unref (task);
6957 : 0 : }
6958 : :
6959 : : static GFile *
6960 : 0 : g_file_real_set_display_name_finish (GFile *file,
6961 : : GAsyncResult *res,
6962 : : GError **error)
6963 : : {
6964 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6965 : :
6966 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
6967 : : }
6968 : :
6969 : : typedef struct {
6970 : : GFileQueryInfoFlags flags;
6971 : : GFileInfo *info;
6972 : : } SetInfoAsyncData;
6973 : :
6974 : : static void
6975 : 0 : set_info_data_free (SetInfoAsyncData *data)
6976 : : {
6977 : 0 : if (data->info)
6978 : 0 : g_object_unref (data->info);
6979 : 0 : g_free (data);
6980 : 0 : }
6981 : :
6982 : : static void
6983 : 0 : set_info_async_thread (GTask *task,
6984 : : gpointer object,
6985 : : gpointer task_data,
6986 : : GCancellable *cancellable)
6987 : : {
6988 : 0 : SetInfoAsyncData *data = task_data;
6989 : 0 : GError *error = NULL;
6990 : :
6991 : 0 : if (g_file_set_attributes_from_info (G_FILE (object),
6992 : : data->info,
6993 : : data->flags,
6994 : : cancellable,
6995 : : &error))
6996 : 0 : g_task_return_boolean (task, TRUE);
6997 : : else
6998 : 0 : g_task_return_error (task, error);
6999 : 0 : }
7000 : :
7001 : : static void
7002 : 0 : g_file_real_set_attributes_async (GFile *file,
7003 : : GFileInfo *info,
7004 : : GFileQueryInfoFlags flags,
7005 : : int io_priority,
7006 : : GCancellable *cancellable,
7007 : : GAsyncReadyCallback callback,
7008 : : gpointer user_data)
7009 : : {
7010 : : GTask *task;
7011 : : SetInfoAsyncData *data;
7012 : :
7013 : 0 : data = g_new0 (SetInfoAsyncData, 1);
7014 : 0 : data->info = g_file_info_dup (info);
7015 : 0 : data->flags = flags;
7016 : :
7017 : 0 : task = g_task_new (file, cancellable, callback, user_data);
7018 : 0 : g_task_set_source_tag (task, g_file_real_set_attributes_async);
7019 : 0 : g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
7020 : 0 : g_task_set_priority (task, io_priority);
7021 : :
7022 : 0 : g_task_run_in_thread (task, set_info_async_thread);
7023 : 0 : g_object_unref (task);
7024 : 0 : }
7025 : :
7026 : : static gboolean
7027 : 0 : g_file_real_set_attributes_finish (GFile *file,
7028 : : GAsyncResult *res,
7029 : : GFileInfo **info,
7030 : : GError **error)
7031 : : {
7032 : : SetInfoAsyncData *data;
7033 : :
7034 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7035 : :
7036 : 0 : data = g_task_get_task_data (G_TASK (res));
7037 : :
7038 : 0 : if (info)
7039 : 0 : *info = g_object_ref (data->info);
7040 : :
7041 : 0 : return g_task_propagate_boolean (G_TASK (res), error);
7042 : : }
7043 : :
7044 : : static void
7045 : 0 : find_enclosing_mount_async_thread (GTask *task,
7046 : : gpointer object,
7047 : : gpointer task_data,
7048 : : GCancellable *cancellable)
7049 : : {
7050 : 0 : GError *error = NULL;
7051 : : GMount *mount;
7052 : :
7053 : 0 : mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
7054 : :
7055 : 0 : if (mount == NULL)
7056 : 0 : g_task_return_error (task, error);
7057 : : else
7058 : 0 : g_task_return_pointer (task, mount, g_object_unref);
7059 : 0 : }
7060 : :
7061 : : static void
7062 : 0 : g_file_real_find_enclosing_mount_async (GFile *file,
7063 : : int io_priority,
7064 : : GCancellable *cancellable,
7065 : : GAsyncReadyCallback callback,
7066 : : gpointer user_data)
7067 : : {
7068 : : GTask *task;
7069 : :
7070 : 0 : task = g_task_new (file, cancellable, callback, user_data);
7071 : 0 : g_task_set_source_tag (task, g_file_real_find_enclosing_mount_async);
7072 : 0 : g_task_set_priority (task, io_priority);
7073 : :
7074 : 0 : g_task_run_in_thread (task, find_enclosing_mount_async_thread);
7075 : 0 : g_object_unref (task);
7076 : 0 : }
7077 : :
7078 : : static GMount *
7079 : 0 : g_file_real_find_enclosing_mount_finish (GFile *file,
7080 : : GAsyncResult *res,
7081 : : GError **error)
7082 : : {
7083 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), NULL);
7084 : :
7085 : 0 : return g_task_propagate_pointer (G_TASK (res), error);
7086 : : }
7087 : :
7088 : :
7089 : : typedef struct {
7090 : : GFile *source;
7091 : : GFile *destination;
7092 : : GFileCopyFlags flags;
7093 : : GFileProgressCallback progress_cb;
7094 : : gpointer progress_cb_data;
7095 : : } CopyAsyncData;
7096 : :
7097 : : static void
7098 : 1 : copy_async_data_free (CopyAsyncData *data)
7099 : : {
7100 : 1 : g_object_unref (data->source);
7101 : 1 : g_object_unref (data->destination);
7102 : 1 : g_slice_free (CopyAsyncData, data);
7103 : 1 : }
7104 : :
7105 : : typedef struct {
7106 : : CopyAsyncData *data;
7107 : : goffset current_num_bytes;
7108 : : goffset total_num_bytes;
7109 : : } CopyProgressData;
7110 : :
7111 : : static gboolean
7112 : 1 : copy_async_progress_in_main (gpointer user_data)
7113 : : {
7114 : 1 : CopyProgressData *progress = user_data;
7115 : 1 : CopyAsyncData *data = progress->data;
7116 : :
7117 : 1 : data->progress_cb (progress->current_num_bytes,
7118 : : progress->total_num_bytes,
7119 : : data->progress_cb_data);
7120 : :
7121 : 1 : return FALSE;
7122 : : }
7123 : :
7124 : : static void
7125 : 1 : copy_async_progress_callback (goffset current_num_bytes,
7126 : : goffset total_num_bytes,
7127 : : gpointer user_data)
7128 : : {
7129 : 1 : GTask *task = user_data;
7130 : 1 : CopyAsyncData *data = g_task_get_task_data (task);
7131 : : CopyProgressData *progress;
7132 : :
7133 : 1 : progress = g_new (CopyProgressData, 1);
7134 : 1 : progress->data = data;
7135 : 1 : progress->current_num_bytes = current_num_bytes;
7136 : 1 : progress->total_num_bytes = total_num_bytes;
7137 : :
7138 : 1 : g_main_context_invoke_full (g_task_get_context (task),
7139 : : g_task_get_priority (task),
7140 : : copy_async_progress_in_main,
7141 : : progress,
7142 : : g_free);
7143 : 1 : }
7144 : :
7145 : : static void
7146 : 1 : copy_async_thread (GTask *task,
7147 : : gpointer source,
7148 : : gpointer task_data,
7149 : : GCancellable *cancellable)
7150 : : {
7151 : 1 : CopyAsyncData *data = task_data;
7152 : : gboolean result;
7153 : 1 : GError *error = NULL;
7154 : :
7155 : 1 : result = g_file_copy (data->source,
7156 : : data->destination,
7157 : : data->flags,
7158 : : cancellable,
7159 : 1 : (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
7160 : : task,
7161 : : &error);
7162 : 1 : if (result)
7163 : 1 : g_task_return_boolean (task, TRUE);
7164 : : else
7165 : 0 : g_task_return_error (task, error);
7166 : 1 : }
7167 : :
7168 : : static void
7169 : 1 : g_file_real_copy_async (GFile *source,
7170 : : GFile *destination,
7171 : : GFileCopyFlags flags,
7172 : : int io_priority,
7173 : : GCancellable *cancellable,
7174 : : GFileProgressCallback progress_callback,
7175 : : gpointer progress_callback_data,
7176 : : GAsyncReadyCallback callback,
7177 : : gpointer user_data)
7178 : : {
7179 : : GTask *task;
7180 : : CopyAsyncData *data;
7181 : :
7182 : 1 : data = g_slice_new (CopyAsyncData);
7183 : 1 : data->source = g_object_ref (source);
7184 : 1 : data->destination = g_object_ref (destination);
7185 : 1 : data->flags = flags;
7186 : 1 : data->progress_cb = progress_callback;
7187 : 1 : data->progress_cb_data = progress_callback_data;
7188 : :
7189 : 1 : task = g_task_new (source, cancellable, callback, user_data);
7190 : 1 : g_task_set_source_tag (task, g_file_real_copy_async);
7191 : 1 : g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
7192 : 1 : g_task_set_priority (task, io_priority);
7193 : 1 : g_task_run_in_thread (task, copy_async_thread);
7194 : 1 : g_object_unref (task);
7195 : 1 : }
7196 : :
7197 : : static gboolean
7198 : 0 : g_file_real_copy_finish (GFile *file,
7199 : : GAsyncResult *res,
7200 : : GError **error)
7201 : : {
7202 : 0 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7203 : :
7204 : 0 : return g_task_propagate_boolean (G_TASK (res), error);
7205 : : }
7206 : :
7207 : :
7208 : : /********************************************
7209 : : * Default VFS operations *
7210 : : ********************************************/
7211 : :
7212 : : /**
7213 : : * g_file_new_for_path:
7214 : : * @path: (type filename): a string containing a relative or absolute path.
7215 : : * The string must be encoded in the glib filename encoding.
7216 : : *
7217 : : * Constructs a #GFile for a given path. This operation never
7218 : : * fails, but the returned object might not support any I/O
7219 : : * operation if @path is malformed.
7220 : : *
7221 : : * Returns: (transfer full): a new #GFile for the given @path.
7222 : : * Free the returned object with g_object_unref().
7223 : : */
7224 : : GFile *
7225 : 455 : g_file_new_for_path (const char *path)
7226 : : {
7227 : 455 : g_return_val_if_fail (path != NULL, NULL);
7228 : :
7229 : 455 : return g_vfs_get_file_for_path (g_vfs_get_default (), path);
7230 : : }
7231 : :
7232 : : /**
7233 : : * g_file_new_for_uri:
7234 : : * @uri: a UTF-8 string containing a URI
7235 : : *
7236 : : * Constructs a #GFile for a given URI. This operation never
7237 : : * fails, but the returned object might not support any I/O
7238 : : * operation if @uri is malformed or if the uri type is
7239 : : * not supported.
7240 : : *
7241 : : * Returns: (transfer full): a new #GFile for the given @uri.
7242 : : * Free the returned object with g_object_unref().
7243 : : */
7244 : : GFile *
7245 : 307 : g_file_new_for_uri (const char *uri)
7246 : : {
7247 : 307 : g_return_val_if_fail (uri != NULL, NULL);
7248 : :
7249 : 307 : return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
7250 : : }
7251 : :
7252 : : /**
7253 : : * g_file_new_tmp:
7254 : : * @tmpl: (type filename) (nullable): Template for the file
7255 : : * name, as in g_file_open_tmp(), or %NULL for a default template
7256 : : * @iostream: (out): on return, a #GFileIOStream for the created file
7257 : : * @error: a #GError, or %NULL
7258 : : *
7259 : : * Opens a file in the preferred directory for temporary files (as
7260 : : * returned by g_get_tmp_dir()) and returns a #GFile and
7261 : : * #GFileIOStream pointing to it.
7262 : : *
7263 : : * @tmpl should be a string in the GLib file name encoding
7264 : : * containing a sequence of six 'X' characters, and containing no
7265 : : * directory components. If it is %NULL, a default template is used.
7266 : : *
7267 : : * Unlike the other #GFile constructors, this will return %NULL if
7268 : : * a temporary file could not be created.
7269 : : *
7270 : : * Returns: (transfer full): a new #GFile.
7271 : : * Free the returned object with g_object_unref().
7272 : : *
7273 : : * Since: 2.32
7274 : : */
7275 : : GFile *
7276 : 73 : g_file_new_tmp (const char *tmpl,
7277 : : GFileIOStream **iostream,
7278 : : GError **error)
7279 : : {
7280 : : gint fd;
7281 : : gchar *path;
7282 : : GFile *file;
7283 : : GFileOutputStream *output;
7284 : :
7285 : 73 : g_return_val_if_fail (iostream != NULL, NULL);
7286 : :
7287 : 73 : fd = g_file_open_tmp (tmpl, &path, error);
7288 : 73 : if (fd == -1)
7289 : 1 : return NULL;
7290 : :
7291 : 72 : file = g_file_new_for_path (path);
7292 : :
7293 : 72 : output = _g_local_file_output_stream_new (fd);
7294 : 72 : *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
7295 : :
7296 : 72 : g_object_unref (output);
7297 : 72 : g_free (path);
7298 : :
7299 : 72 : return file;
7300 : : }
7301 : :
7302 : : typedef struct {
7303 : : GFile *file;
7304 : : GFileIOStream *iostream;
7305 : : } NewTmpAsyncData;
7306 : :
7307 : : static void
7308 : 2 : new_tmp_data_free (NewTmpAsyncData *data)
7309 : : {
7310 : 2 : g_clear_object (&data->file);
7311 : 2 : g_clear_object (&data->iostream);
7312 : 2 : g_free (data);
7313 : 2 : }
7314 : :
7315 : : static void
7316 : 3 : new_tmp_async_thread (GTask *task,
7317 : : gpointer object,
7318 : : gpointer task_data,
7319 : : GCancellable *cancellable)
7320 : : {
7321 : : GFile *file;
7322 : 3 : const char *tmpl = task_data;
7323 : 3 : GFileIOStream *iostream = NULL;
7324 : 3 : GError *error = NULL;
7325 : : NewTmpAsyncData *return_data;
7326 : :
7327 : 3 : if (g_task_return_error_if_cancelled (task))
7328 : 1 : return;
7329 : :
7330 : 3 : file = g_file_new_tmp (tmpl, &iostream, &error);
7331 : :
7332 : 3 : if (!file)
7333 : : {
7334 : 1 : int error_code = G_IO_ERROR_FAILED;
7335 : :
7336 : 1 : if (error->domain == G_IO_ERROR)
7337 : : {
7338 : 0 : g_task_return_error (task, g_steal_pointer (&error));
7339 : 0 : return;
7340 : : }
7341 : :
7342 : 1 : if (error->domain == G_FILE_ERROR)
7343 : 1 : error_code = g_io_error_from_file_error (error->code);
7344 : :
7345 : 1 : g_task_return_new_error (task, G_IO_ERROR, error_code,
7346 : 1 : _("Failed to create a temporary directory for "
7347 : : "template “%s”: %s"),
7348 : 1 : tmpl, error->message);
7349 : :
7350 : 1 : g_clear_error (&error);
7351 : 1 : return;
7352 : : }
7353 : :
7354 : 2 : return_data = g_new0 (NewTmpAsyncData, 1);
7355 : 2 : return_data->file = g_steal_pointer (&file);
7356 : 2 : return_data->iostream = g_steal_pointer (&iostream);
7357 : :
7358 : 2 : g_task_return_pointer (task, g_steal_pointer (&return_data),
7359 : : (GDestroyNotify) new_tmp_data_free);
7360 : : }
7361 : :
7362 : : /**
7363 : : * g_file_new_tmp_async:
7364 : : * @tmpl: (type filename) (nullable): Template for the file
7365 : : * name, as in g_file_open_tmp(), or %NULL for a default template
7366 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
7367 : : * @cancellable: optional #GCancellable object, %NULL to ignore
7368 : : * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
7369 : : * @user_data: (nullable): data to pass to @callback
7370 : : *
7371 : : * Asynchronously opens a file in the preferred directory for temporary files
7372 : : * (as returned by g_get_tmp_dir()) as g_file_new_tmp().
7373 : : *
7374 : : * @tmpl should be a string in the GLib file name encoding
7375 : : * containing a sequence of six 'X' characters, and containing no
7376 : : * directory components. If it is %NULL, a default template is used.
7377 : : *
7378 : : * Since: 2.74
7379 : : */
7380 : : void
7381 : 3 : g_file_new_tmp_async (const char *tmpl,
7382 : : int io_priority,
7383 : : GCancellable *cancellable,
7384 : : GAsyncReadyCallback callback,
7385 : : gpointer user_data)
7386 : : {
7387 : : GTask *task;
7388 : :
7389 : 3 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7390 : :
7391 : 3 : task = g_task_new (NULL, cancellable, callback, user_data);
7392 : 3 : g_task_set_source_tag (task, g_file_new_tmp_async);
7393 : 3 : g_task_set_task_data (task, g_strdup (tmpl), g_free);
7394 : 3 : g_task_set_priority (task, io_priority);
7395 : 3 : g_task_set_check_cancellable (task, TRUE);
7396 : 3 : g_task_run_in_thread (task, new_tmp_async_thread);
7397 : 3 : g_object_unref (task);
7398 : : }
7399 : :
7400 : : /**
7401 : : * g_file_new_tmp_finish:
7402 : : * @result: a #GAsyncResult
7403 : : * @iostream: (out) (not optional) (not nullable) (transfer full): on return, a #GFileIOStream for the created file
7404 : : * @error: a #GError, or %NULL
7405 : : *
7406 : : * Finishes a temporary file creation started by g_file_new_tmp_async().
7407 : : *
7408 : : * Returns: (transfer full): a new #GFile.
7409 : : * Free the returned object with g_object_unref().
7410 : : *
7411 : : * Since: 2.74
7412 : : */
7413 : : GFile *
7414 : 3 : g_file_new_tmp_finish (GAsyncResult *result,
7415 : : GFileIOStream **iostream,
7416 : : GError **error)
7417 : : {
7418 : : GFile *file;
7419 : : NewTmpAsyncData *data;
7420 : :
7421 : 3 : g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7422 : 3 : g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7423 : : g_file_new_tmp_async, NULL);
7424 : 3 : g_return_val_if_fail (iostream != NULL, NULL);
7425 : 3 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7426 : :
7427 : 3 : data = g_task_propagate_pointer (G_TASK (result), error);
7428 : :
7429 : 3 : if (!data)
7430 : : {
7431 : 2 : *iostream = NULL;
7432 : 2 : return NULL;
7433 : : }
7434 : :
7435 : 1 : file = g_steal_pointer (&data->file);
7436 : 1 : *iostream = g_steal_pointer (&data->iostream);
7437 : :
7438 : 1 : new_tmp_data_free (data);
7439 : :
7440 : 1 : return file;
7441 : : }
7442 : :
7443 : : static void
7444 : 3 : new_tmp_dir_async_thread (GTask *task,
7445 : : gpointer object,
7446 : : gpointer task_data,
7447 : : GCancellable *cancellable)
7448 : : {
7449 : : gchar *path;
7450 : 3 : const char *tmpl = task_data;
7451 : 3 : GError *error = NULL;
7452 : :
7453 : 3 : if (g_task_return_error_if_cancelled (task))
7454 : 1 : return;
7455 : :
7456 : 3 : path = g_dir_make_tmp (tmpl, &error);
7457 : :
7458 : 3 : if (!path)
7459 : : {
7460 : 1 : int error_code = G_IO_ERROR_FAILED;
7461 : :
7462 : 1 : if (error->domain == G_IO_ERROR)
7463 : : {
7464 : 0 : g_task_return_error (task, g_steal_pointer (&error));
7465 : 0 : return;
7466 : : }
7467 : :
7468 : 1 : if (error->domain == G_FILE_ERROR)
7469 : 1 : error_code = g_io_error_from_file_error (error->code);
7470 : :
7471 : 1 : g_task_return_new_error (task, G_IO_ERROR, error_code,
7472 : 1 : _("Failed to create a temporary directory for "
7473 : : "template “%s”: %s"),
7474 : 1 : tmpl, error->message);
7475 : :
7476 : 1 : g_clear_error (&error);
7477 : 1 : return;
7478 : : }
7479 : :
7480 : 2 : g_task_return_pointer (task, g_file_new_for_path (path), g_object_unref);
7481 : :
7482 : 2 : g_free (path);
7483 : : }
7484 : :
7485 : : /**
7486 : : * g_file_new_tmp_dir_async:
7487 : : * @tmpl: (type filename) (nullable): Template for the file
7488 : : * name, as in g_dir_make_tmp(), or %NULL for a default template
7489 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
7490 : : * @cancellable: optional #GCancellable object, %NULL to ignore
7491 : : * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
7492 : : * @user_data: (nullable): data to pass to @callback
7493 : : *
7494 : : * Asynchronously creates a directory in the preferred directory for
7495 : : * temporary files (as returned by g_get_tmp_dir()) as g_dir_make_tmp().
7496 : : *
7497 : : * @tmpl should be a string in the GLib file name encoding
7498 : : * containing a sequence of six 'X' characters, and containing no
7499 : : * directory components. If it is %NULL, a default template is used.
7500 : : *
7501 : : * Since: 2.74
7502 : : */
7503 : : void
7504 : 3 : g_file_new_tmp_dir_async (const char *tmpl,
7505 : : int io_priority,
7506 : : GCancellable *cancellable,
7507 : : GAsyncReadyCallback callback,
7508 : : gpointer user_data)
7509 : : {
7510 : : GTask *task;
7511 : :
7512 : 3 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7513 : :
7514 : 3 : task = g_task_new (NULL, cancellable, callback, user_data);
7515 : 3 : g_task_set_source_tag (task, g_file_new_tmp_dir_async);
7516 : 3 : g_task_set_task_data (task, g_strdup (tmpl), g_free);
7517 : 3 : g_task_set_priority (task, io_priority);
7518 : 3 : g_task_set_check_cancellable (task, TRUE);
7519 : 3 : g_task_run_in_thread (task, new_tmp_dir_async_thread);
7520 : 3 : g_object_unref (task);
7521 : : }
7522 : :
7523 : : /**
7524 : : * g_file_new_tmp_dir_finish:
7525 : : * @result: a #GAsyncResult
7526 : : * @error: a #GError, or %NULL
7527 : : *
7528 : : * Finishes a temporary directory creation started by
7529 : : * g_file_new_tmp_dir_async().
7530 : : *
7531 : : * Returns: (transfer full): a new #GFile.
7532 : : * Free the returned object with g_object_unref().
7533 : : *
7534 : : * Since: 2.74
7535 : : */
7536 : : GFile *
7537 : 3 : g_file_new_tmp_dir_finish (GAsyncResult *result,
7538 : : GError **error)
7539 : : {
7540 : 3 : g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7541 : 3 : g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7542 : : g_file_new_tmp_dir_async, NULL);
7543 : 3 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7544 : :
7545 : 3 : return g_task_propagate_pointer (G_TASK (result), error);
7546 : : }
7547 : :
7548 : : /**
7549 : : * g_file_parse_name:
7550 : : * @parse_name: a file name or path to be parsed
7551 : : *
7552 : : * Constructs a #GFile with the given @parse_name (i.e. something
7553 : : * given by g_file_get_parse_name()). This operation never fails,
7554 : : * but the returned object might not support any I/O operation if
7555 : : * the @parse_name cannot be parsed.
7556 : : *
7557 : : * Returns: (transfer full): a new #GFile.
7558 : : */
7559 : : GFile *
7560 : 25 : g_file_parse_name (const char *parse_name)
7561 : : {
7562 : 25 : g_return_val_if_fail (parse_name != NULL, NULL);
7563 : :
7564 : 25 : return g_vfs_parse_name (g_vfs_get_default (), parse_name);
7565 : : }
7566 : :
7567 : : /**
7568 : : * g_file_new_build_filename:
7569 : : * @first_element: (type filename): the first element in the path
7570 : : * @...: remaining elements in path, terminated by %NULL
7571 : : *
7572 : : * Constructs a #GFile from a series of elements using the correct
7573 : : * separator for filenames.
7574 : : *
7575 : : * Using this function is equivalent to calling g_build_filename(),
7576 : : * followed by g_file_new_for_path() on the result.
7577 : : *
7578 : : * Returns: (transfer full): a new #GFile
7579 : : *
7580 : : * Since: 2.56
7581 : : */
7582 : : GFile *
7583 : 43 : g_file_new_build_filename (const gchar *first_element,
7584 : : ...)
7585 : : {
7586 : : gchar *str;
7587 : : GFile *file;
7588 : : va_list args;
7589 : :
7590 : 43 : g_return_val_if_fail (first_element != NULL, NULL);
7591 : :
7592 : 43 : va_start (args, first_element);
7593 : 43 : str = g_build_filename_valist (first_element, &args);
7594 : 43 : va_end (args);
7595 : :
7596 : 43 : file = g_file_new_for_path (str);
7597 : 43 : g_free (str);
7598 : :
7599 : 43 : return file;
7600 : : }
7601 : :
7602 : :
7603 : : /**
7604 : : * g_file_new_build_filenamev:
7605 : : * @args: (array zero-terminated=1) (element-type filename): %NULL-terminated
7606 : : * array of strings containing the path elements.
7607 : : *
7608 : : * Constructs a #GFile from a vector of elements using the correct
7609 : : * separator for filenames.
7610 : : *
7611 : : * Using this function is equivalent to calling g_build_filenamev(),
7612 : : * followed by g_file_new_for_path() on the result.
7613 : : *
7614 : : * Returns: (transfer full): a new #GFile
7615 : : *
7616 : : * Since: 2.78
7617 : : */
7618 : : GFile *
7619 : 2 : g_file_new_build_filenamev (const gchar * const *args)
7620 : : {
7621 : : gchar *str;
7622 : : GFile *file;
7623 : :
7624 : 2 : str = g_build_filenamev ((gchar **) args);
7625 : 2 : file = g_file_new_for_path (str);
7626 : 2 : g_free (str);
7627 : :
7628 : 2 : return file;
7629 : : }
7630 : :
7631 : : static gboolean
7632 : 32 : is_valid_scheme_character (char c)
7633 : : {
7634 : 32 : return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
7635 : : }
7636 : :
7637 : : /* Following RFC 2396, valid schemes are built like:
7638 : : * scheme = alpha *( alpha | digit | "+" | "-" | "." )
7639 : : */
7640 : : static gboolean
7641 : 11 : has_valid_scheme (const char *uri)
7642 : : {
7643 : : const char *p;
7644 : :
7645 : 11 : p = uri;
7646 : :
7647 : 11 : if (!g_ascii_isalpha (*p))
7648 : 3 : return FALSE;
7649 : :
7650 : : do {
7651 : 32 : p++;
7652 : 32 : } while (is_valid_scheme_character (*p));
7653 : :
7654 : 8 : return *p == ':';
7655 : : }
7656 : :
7657 : : static GFile *
7658 : 39 : new_for_cmdline_arg (const gchar *arg,
7659 : : const gchar *cwd)
7660 : : {
7661 : : GFile *file;
7662 : : char *filename;
7663 : :
7664 : 39 : if (g_path_is_absolute (arg))
7665 : 28 : return g_file_new_for_path (arg);
7666 : :
7667 : 11 : if (has_valid_scheme (arg))
7668 : 8 : return g_file_new_for_uri (arg);
7669 : :
7670 : 3 : if (cwd == NULL)
7671 : : {
7672 : : char *current_dir;
7673 : :
7674 : 3 : current_dir = g_get_current_dir ();
7675 : 3 : filename = g_build_filename (current_dir, arg, NULL);
7676 : 3 : g_free (current_dir);
7677 : : }
7678 : : else
7679 : 0 : filename = g_build_filename (cwd, arg, NULL);
7680 : :
7681 : 3 : file = g_file_new_for_path (filename);
7682 : 3 : g_free (filename);
7683 : :
7684 : 3 : return file;
7685 : : }
7686 : :
7687 : : /**
7688 : : * g_file_new_for_commandline_arg:
7689 : : * @arg: (type filename): a command line string
7690 : : *
7691 : : * Creates a #GFile with the given argument from the command line.
7692 : : * The value of @arg can be either a URI, an absolute path or a
7693 : : * relative path resolved relative to the current working directory.
7694 : : * This operation never fails, but the returned object might not
7695 : : * support any I/O operation if @arg points to a malformed path.
7696 : : *
7697 : : * Note that on Windows, this function expects its argument to be in
7698 : : * UTF-8 -- not the system code page. This means that you
7699 : : * should not use this function with string from argv as it is passed
7700 : : * to main(). g_win32_get_command_line() will return a UTF-8 version of
7701 : : * the commandline. #GApplication also uses UTF-8 but
7702 : : * g_application_command_line_create_file_for_arg() may be more useful
7703 : : * for you there. It is also always possible to use this function with
7704 : : * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
7705 : : *
7706 : : * Returns: (transfer full): a new #GFile.
7707 : : * Free the returned object with g_object_unref().
7708 : : */
7709 : : GFile *
7710 : 39 : g_file_new_for_commandline_arg (const char *arg)
7711 : : {
7712 : 39 : g_return_val_if_fail (arg != NULL, NULL);
7713 : :
7714 : 39 : return new_for_cmdline_arg (arg, NULL);
7715 : : }
7716 : :
7717 : : /**
7718 : : * g_file_new_for_commandline_arg_and_cwd:
7719 : : * @arg: (type filename): a command line string
7720 : : * @cwd: (type filename): the current working directory of the commandline
7721 : : *
7722 : : * Creates a #GFile with the given argument from the command line.
7723 : : *
7724 : : * This function is similar to g_file_new_for_commandline_arg() except
7725 : : * that it allows for passing the current working directory as an
7726 : : * argument instead of using the current working directory of the
7727 : : * process.
7728 : : *
7729 : : * This is useful if the commandline argument was given in a context
7730 : : * other than the invocation of the current process.
7731 : : *
7732 : : * See also g_application_command_line_create_file_for_arg().
7733 : : *
7734 : : * Returns: (transfer full): a new #GFile
7735 : : *
7736 : : * Since: 2.36
7737 : : **/
7738 : : GFile *
7739 : 0 : g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
7740 : : const gchar *cwd)
7741 : : {
7742 : 0 : g_return_val_if_fail (arg != NULL, NULL);
7743 : 0 : g_return_val_if_fail (cwd != NULL, NULL);
7744 : :
7745 : 0 : return new_for_cmdline_arg (arg, cwd);
7746 : : }
7747 : :
7748 : : /**
7749 : : * g_file_mount_enclosing_volume:
7750 : : * @location: input #GFile
7751 : : * @flags: flags affecting the operation
7752 : : * @mount_operation: (nullable): a #GMountOperation
7753 : : * or %NULL to avoid user interaction
7754 : : * @cancellable: (nullable): optional #GCancellable object,
7755 : : * %NULL to ignore
7756 : : * @callback: (nullable): a #GAsyncReadyCallback to call
7757 : : * when the request is satisfied, or %NULL
7758 : : * @user_data: the data to pass to callback function
7759 : : *
7760 : : * Starts a @mount_operation, mounting the volume that contains
7761 : : * the file @location.
7762 : : *
7763 : : * When this operation has completed, @callback will be called with
7764 : : * @user_user data, and the operation can be finalized with
7765 : : * g_file_mount_enclosing_volume_finish().
7766 : : *
7767 : : * If @cancellable is not %NULL, then the operation can be cancelled by
7768 : : * triggering the cancellable object from another thread. If the operation
7769 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7770 : : */
7771 : : void
7772 : 0 : g_file_mount_enclosing_volume (GFile *location,
7773 : : GMountMountFlags flags,
7774 : : GMountOperation *mount_operation,
7775 : : GCancellable *cancellable,
7776 : : GAsyncReadyCallback callback,
7777 : : gpointer user_data)
7778 : : {
7779 : : GFileIface *iface;
7780 : :
7781 : 0 : g_return_if_fail (G_IS_FILE (location));
7782 : :
7783 : 0 : iface = G_FILE_GET_IFACE (location);
7784 : :
7785 : 0 : if (iface->mount_enclosing_volume == NULL)
7786 : : {
7787 : 0 : g_task_report_new_error (location, callback, user_data,
7788 : : g_file_mount_enclosing_volume,
7789 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7790 : 0 : _("volume doesn’t implement mount"));
7791 : 0 : return;
7792 : : }
7793 : :
7794 : 0 : (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
7795 : :
7796 : : }
7797 : :
7798 : : /**
7799 : : * g_file_mount_enclosing_volume_finish:
7800 : : * @location: input #GFile
7801 : : * @result: a #GAsyncResult
7802 : : * @error: a #GError, or %NULL
7803 : : *
7804 : : * Finishes a mount operation started by g_file_mount_enclosing_volume().
7805 : : *
7806 : : * Returns: %TRUE if successful. If an error has occurred,
7807 : : * this function will return %FALSE and set @error
7808 : : * appropriately if present.
7809 : : */
7810 : : gboolean
7811 : 0 : g_file_mount_enclosing_volume_finish (GFile *location,
7812 : : GAsyncResult *result,
7813 : : GError **error)
7814 : : {
7815 : : GFileIface *iface;
7816 : :
7817 : 0 : g_return_val_if_fail (G_IS_FILE (location), FALSE);
7818 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7819 : :
7820 : 0 : if (g_async_result_legacy_propagate_error (result, error))
7821 : 0 : return FALSE;
7822 : 0 : else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
7823 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
7824 : :
7825 : 0 : iface = G_FILE_GET_IFACE (location);
7826 : :
7827 : 0 : return (* iface->mount_enclosing_volume_finish) (location, result, error);
7828 : : }
7829 : :
7830 : : /********************************************
7831 : : * Utility functions *
7832 : : ********************************************/
7833 : :
7834 : : /**
7835 : : * g_file_query_default_handler:
7836 : : * @file: a #GFile to open
7837 : : * @cancellable: optional #GCancellable object, %NULL to ignore
7838 : : * @error: a #GError, or %NULL
7839 : : *
7840 : : * Returns the #GAppInfo that is registered as the default
7841 : : * application to handle the file specified by @file.
7842 : : *
7843 : : * If @cancellable is not %NULL, then the operation can be cancelled by
7844 : : * triggering the cancellable object from another thread. If the operation
7845 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7846 : : *
7847 : : * Returns: (transfer full): a #GAppInfo if the handle was found,
7848 : : * %NULL if there were errors.
7849 : : * When you are done with it, release it with g_object_unref()
7850 : : */
7851 : : GAppInfo *
7852 : 8 : g_file_query_default_handler (GFile *file,
7853 : : GCancellable *cancellable,
7854 : : GError **error)
7855 : : {
7856 : : char *uri_scheme;
7857 : : const char *content_type;
7858 : : GAppInfo *appinfo;
7859 : : GFileInfo *info;
7860 : : char *path;
7861 : :
7862 : 8 : uri_scheme = g_file_get_uri_scheme (file);
7863 : 8 : if (uri_scheme && uri_scheme[0] != '\0')
7864 : : {
7865 : 8 : appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
7866 : 8 : g_free (uri_scheme);
7867 : :
7868 : 8 : if (appinfo != NULL)
7869 : 1 : return appinfo;
7870 : : }
7871 : : else
7872 : 0 : g_free (uri_scheme);
7873 : :
7874 : 7 : info = g_file_query_info (file,
7875 : : G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
7876 : : G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
7877 : : 0,
7878 : : cancellable,
7879 : : error);
7880 : 7 : if (info == NULL)
7881 : 4 : return NULL;
7882 : :
7883 : 3 : appinfo = NULL;
7884 : :
7885 : 3 : content_type = g_file_info_get_content_type (info);
7886 : 3 : if (content_type == NULL)
7887 : 0 : content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7888 : 3 : if (content_type)
7889 : : {
7890 : : /* Don't use is_native(), as we want to support fuse paths if available */
7891 : 3 : path = g_file_get_path (file);
7892 : 3 : appinfo = g_app_info_get_default_for_type (content_type,
7893 : : path == NULL);
7894 : 3 : g_free (path);
7895 : : }
7896 : :
7897 : 3 : g_object_unref (info);
7898 : :
7899 : 3 : if (appinfo != NULL)
7900 : 1 : return appinfo;
7901 : :
7902 : 2 : g_set_error_literal (error, G_IO_ERROR,
7903 : : G_IO_ERROR_NOT_SUPPORTED,
7904 : : _("No application is registered as handling this file"));
7905 : 2 : return NULL;
7906 : : }
7907 : :
7908 : : static void
7909 : 3 : query_default_handler_query_app_info_for_type_cb (GObject *object,
7910 : : GAsyncResult *result,
7911 : : gpointer user_data)
7912 : : {
7913 : 3 : GTask *task = G_TASK (user_data);
7914 : : GAppInfo *appinfo;
7915 : 3 : GError *error = NULL;
7916 : :
7917 : 3 : appinfo = g_app_info_get_default_for_type_finish (result, &error);
7918 : :
7919 : 3 : if (appinfo != NULL)
7920 : : {
7921 : 1 : g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
7922 : : }
7923 : 2 : else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7924 : : {
7925 : 2 : g_task_return_new_error_literal (task,
7926 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7927 : 2 : error->message);
7928 : : }
7929 : : else
7930 : : {
7931 : 0 : g_task_return_error (task, g_steal_pointer (&error));
7932 : : }
7933 : :
7934 : 3 : g_clear_error (&error);
7935 : 3 : g_object_unref (task);
7936 : 3 : }
7937 : :
7938 : : static void
7939 : 10 : query_default_handler_query_info_cb (GObject *object,
7940 : : GAsyncResult *result,
7941 : : gpointer user_data)
7942 : : {
7943 : 10 : GFile *file = G_FILE (object);
7944 : 10 : GTask *task = G_TASK (user_data);
7945 : 10 : GError *error = NULL;
7946 : : GFileInfo *info;
7947 : : const char *content_type;
7948 : :
7949 : 10 : info = g_file_query_info_finish (file, result, &error);
7950 : 10 : if (info == NULL)
7951 : : {
7952 : 7 : g_task_return_error (task, g_steal_pointer (&error));
7953 : 7 : g_object_unref (task);
7954 : 7 : return;
7955 : : }
7956 : :
7957 : 3 : content_type = g_file_info_get_content_type (info);
7958 : 3 : if (content_type == NULL)
7959 : 0 : content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7960 : 3 : if (content_type)
7961 : : {
7962 : 3 : GCancellable *cancellable = g_task_get_cancellable (task);
7963 : : char *path;
7964 : :
7965 : : /* Don't use is_native(), as we want to support fuse paths if available */
7966 : 3 : path = g_file_get_path (file);
7967 : :
7968 : 3 : g_app_info_get_default_for_type_async (content_type,
7969 : : path == NULL,
7970 : : cancellable,
7971 : : query_default_handler_query_app_info_for_type_cb,
7972 : : g_steal_pointer (&task));
7973 : :
7974 : 3 : g_free (path);
7975 : : }
7976 : : else
7977 : : {
7978 : 0 : g_task_return_new_error_literal (task,
7979 : : G_IO_ERROR,
7980 : : G_IO_ERROR_NOT_SUPPORTED,
7981 : 0 : _("No application is registered as handling this file"));
7982 : : }
7983 : :
7984 : 3 : g_object_unref (info);
7985 : 3 : g_clear_object (&task);
7986 : : }
7987 : :
7988 : : static void
7989 : 11 : on_query_default_handler_for_uri_cb (GObject *object,
7990 : : GAsyncResult *result,
7991 : : gpointer user_data)
7992 : : {
7993 : 11 : GTask *task = user_data;
7994 : : GAppInfo *app_info;
7995 : :
7996 : 11 : app_info = g_app_info_get_default_for_uri_scheme_finish (result, NULL);
7997 : :
7998 : 11 : if (app_info)
7999 : : {
8000 : 1 : g_task_return_pointer (task, g_steal_pointer (&app_info), g_object_unref);
8001 : 1 : g_object_unref (task);
8002 : : }
8003 : : else
8004 : : {
8005 : 10 : g_file_query_info_async (g_task_get_source_object (task),
8006 : : G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
8007 : : G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
8008 : : 0,
8009 : : g_task_get_priority (task),
8010 : : g_task_get_cancellable (task),
8011 : : query_default_handler_query_info_cb,
8012 : : task);
8013 : : }
8014 : 11 : }
8015 : :
8016 : : /**
8017 : : * g_file_query_default_handler_async:
8018 : : * @file: a #GFile to open
8019 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
8020 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8021 : : * @callback: (nullable): a #GAsyncReadyCallback to call when the request is done
8022 : : * @user_data: (nullable): data to pass to @callback
8023 : : *
8024 : : * Async version of g_file_query_default_handler().
8025 : : *
8026 : : * Since: 2.60
8027 : : */
8028 : : void
8029 : 11 : g_file_query_default_handler_async (GFile *file,
8030 : : int io_priority,
8031 : : GCancellable *cancellable,
8032 : : GAsyncReadyCallback callback,
8033 : : gpointer user_data)
8034 : : {
8035 : : GTask *task;
8036 : : char *uri_scheme;
8037 : :
8038 : 11 : task = g_task_new (file, cancellable, callback, user_data);
8039 : 11 : g_task_set_source_tag (task, g_file_query_default_handler_async);
8040 : :
8041 : 11 : uri_scheme = g_file_get_uri_scheme (file);
8042 : 11 : if (uri_scheme && uri_scheme[0] != '\0')
8043 : : {
8044 : 11 : g_app_info_get_default_for_uri_scheme_async (uri_scheme,
8045 : : cancellable,
8046 : : on_query_default_handler_for_uri_cb,
8047 : : g_steal_pointer (&task));
8048 : 11 : g_free (uri_scheme);
8049 : 11 : return;
8050 : : }
8051 : :
8052 : 0 : g_file_query_info_async (file,
8053 : : G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
8054 : : G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
8055 : : 0,
8056 : : io_priority,
8057 : : cancellable,
8058 : : query_default_handler_query_info_cb,
8059 : : g_steal_pointer (&task));
8060 : :
8061 : 0 : g_free (uri_scheme);
8062 : : }
8063 : :
8064 : : /**
8065 : : * g_file_query_default_handler_finish:
8066 : : * @file: a #GFile to open
8067 : : * @result: a #GAsyncResult
8068 : : * @error: (nullable): a #GError
8069 : : *
8070 : : * Finishes a g_file_query_default_handler_async() operation.
8071 : : *
8072 : : * Returns: (transfer full): a #GAppInfo if the handle was found,
8073 : : * %NULL if there were errors.
8074 : : * When you are done with it, release it with g_object_unref()
8075 : : *
8076 : : * Since: 2.60
8077 : : */
8078 : : GAppInfo *
8079 : 11 : g_file_query_default_handler_finish (GFile *file,
8080 : : GAsyncResult *result,
8081 : : GError **error)
8082 : : {
8083 : 11 : g_return_val_if_fail (G_IS_FILE (file), NULL);
8084 : 11 : g_return_val_if_fail (g_task_is_valid (result, file), NULL);
8085 : :
8086 : 11 : return g_task_propagate_pointer (G_TASK (result), error);
8087 : : }
8088 : :
8089 : : #define GET_CONTENT_BLOCK_SIZE 8192
8090 : :
8091 : : /**
8092 : : * g_file_load_contents:
8093 : : * @file: input #GFile
8094 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8095 : : * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
8096 : : * @length: (out) (optional): a location to place the length of the contents of the file,
8097 : : * or %NULL if the length is not needed
8098 : : * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
8099 : : * or %NULL if the entity tag is not needed
8100 : : * @error: a #GError, or %NULL
8101 : : *
8102 : : * Loads the content of the file into memory. The data is always
8103 : : * zero-terminated, but this is not included in the resultant @length.
8104 : : * The returned @contents should be freed with g_free() when no longer
8105 : : * needed.
8106 : : *
8107 : : * If @cancellable is not %NULL, then the operation can be cancelled by
8108 : : * triggering the cancellable object from another thread. If the operation
8109 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8110 : : *
8111 : : * Returns: %TRUE if the @file's contents were successfully loaded.
8112 : : * %FALSE if there were errors.
8113 : : */
8114 : : gboolean
8115 : 37 : g_file_load_contents (GFile *file,
8116 : : GCancellable *cancellable,
8117 : : char **contents,
8118 : : gsize *length,
8119 : : char **etag_out,
8120 : : GError **error)
8121 : : {
8122 : : GFileInputStream *in;
8123 : : char *data;
8124 : : gsize size;
8125 : : gsize pos;
8126 : : gssize res;
8127 : : GFileInfo *info;
8128 : :
8129 : 37 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
8130 : 37 : g_return_val_if_fail (contents != NULL, FALSE);
8131 : :
8132 : 37 : in = g_file_read (file, cancellable, error);
8133 : 37 : if (in == NULL)
8134 : 6 : return FALSE;
8135 : :
8136 : 31 : size = GET_CONTENT_BLOCK_SIZE;
8137 : 31 : data = g_malloc (GET_CONTENT_BLOCK_SIZE);
8138 : 31 : pos = 0;
8139 : :
8140 : 86 : while ((res = g_input_stream_read (G_INPUT_STREAM (in),
8141 : 55 : data + pos,
8142 : : GET_CONTENT_BLOCK_SIZE,
8143 : 55 : cancellable, error)) > 0)
8144 : : {
8145 : 24 : pos += res;
8146 : 24 : if (size - pos < GET_CONTENT_BLOCK_SIZE)
8147 : : {
8148 : 24 : g_assert (size <= G_MAXSIZE / 2);
8149 : 24 : size *= 2;
8150 : 24 : data = g_realloc (data, size);
8151 : : }
8152 : : }
8153 : :
8154 : 31 : if (etag_out)
8155 : : {
8156 : 0 : *etag_out = NULL;
8157 : :
8158 : 0 : info = g_file_input_stream_query_info (in,
8159 : : G_FILE_ATTRIBUTE_ETAG_VALUE,
8160 : : cancellable,
8161 : : NULL);
8162 : 0 : if (info)
8163 : : {
8164 : 0 : *etag_out = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ETAG_VALUE) ? g_strdup (g_file_info_get_etag (info)) : NULL;
8165 : 0 : g_object_unref (info);
8166 : : }
8167 : : }
8168 : :
8169 : : /* Ignore errors on close */
8170 : 31 : g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
8171 : 31 : g_object_unref (in);
8172 : :
8173 : 31 : if (res < 0)
8174 : : {
8175 : : /* error is set already */
8176 : 0 : g_free (data);
8177 : 0 : return FALSE;
8178 : : }
8179 : :
8180 : 31 : if (length)
8181 : 28 : *length = pos;
8182 : :
8183 : : /* Zero terminate (allocating extra bytes if needed) */
8184 : 31 : if (pos >= size)
8185 : 0 : data = g_realloc (data, pos + 1);
8186 : 31 : data[pos] = 0;
8187 : :
8188 : 31 : *contents = g_steal_pointer (&data);
8189 : :
8190 : 31 : return TRUE;
8191 : : }
8192 : :
8193 : : typedef struct {
8194 : : GTask *task;
8195 : : GFileReadMoreCallback read_more_callback;
8196 : : char *data;
8197 : : gsize size;
8198 : : gsize pos;
8199 : : char *etag;
8200 : : } LoadContentsData;
8201 : :
8202 : :
8203 : : static void
8204 : 3 : load_contents_data_free (LoadContentsData *data)
8205 : : {
8206 : 3 : g_clear_pointer (&data->data, g_free);
8207 : 3 : g_free (data->etag);
8208 : 3 : g_free (data);
8209 : 3 : }
8210 : :
8211 : : static void
8212 : 9 : load_contents_data_ensure_space (LoadContentsData *data,
8213 : : gsize space)
8214 : : {
8215 : 9 : if (data->size - data->pos < space)
8216 : : {
8217 : 6 : if (data->data == NULL)
8218 : : {
8219 : 3 : data->size = space;
8220 : 3 : data->data = g_malloc (space);
8221 : : }
8222 : : else
8223 : : {
8224 : 3 : g_assert (data->size <= G_MAXSIZE / 2);
8225 : 3 : data->size *= 2;
8226 : 3 : data->data = g_realloc (data->data, data->size);
8227 : : }
8228 : : }
8229 : 9 : }
8230 : :
8231 : : static void
8232 : 3 : load_contents_close_callback (GObject *obj,
8233 : : GAsyncResult *close_res,
8234 : : gpointer user_data)
8235 : : {
8236 : 3 : GInputStream *stream = G_INPUT_STREAM (obj);
8237 : 3 : LoadContentsData *data = user_data;
8238 : :
8239 : : /* Ignore errors here, we're only reading anyway */
8240 : 3 : g_input_stream_close_finish (stream, close_res, NULL);
8241 : 3 : g_object_unref (stream);
8242 : :
8243 : 3 : g_task_return_boolean (data->task, TRUE);
8244 : 3 : g_object_unref (data->task);
8245 : 3 : }
8246 : :
8247 : : static void
8248 : 3 : load_contents_fstat_callback (GObject *obj,
8249 : : GAsyncResult *stat_res,
8250 : : gpointer user_data)
8251 : : {
8252 : 3 : GInputStream *stream = G_INPUT_STREAM (obj);
8253 : 3 : LoadContentsData *data = user_data;
8254 : : GFileInfo *info;
8255 : :
8256 : 3 : info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
8257 : : stat_res, NULL);
8258 : 3 : if (info)
8259 : : {
8260 : 6 : data->etag = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ETAG_VALUE) ? g_strdup (g_file_info_get_etag (info)) : NULL;
8261 : 3 : g_object_unref (info);
8262 : : }
8263 : :
8264 : 3 : g_input_stream_close_async (stream, 0,
8265 : : g_task_get_cancellable (data->task),
8266 : : load_contents_close_callback, data);
8267 : 3 : }
8268 : :
8269 : : static void
8270 : 6 : load_contents_read_callback (GObject *obj,
8271 : : GAsyncResult *read_res,
8272 : : gpointer user_data)
8273 : : {
8274 : 6 : GInputStream *stream = G_INPUT_STREAM (obj);
8275 : 6 : LoadContentsData *data = user_data;
8276 : 6 : GError *error = NULL;
8277 : : gssize read_size;
8278 : :
8279 : 6 : read_size = g_input_stream_read_finish (stream, read_res, &error);
8280 : :
8281 : 6 : if (read_size < 0)
8282 : : {
8283 : 0 : g_task_return_error (data->task, error);
8284 : 0 : g_object_unref (data->task);
8285 : :
8286 : : /* Close the file ignoring any error */
8287 : 0 : g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
8288 : 0 : g_object_unref (stream);
8289 : : }
8290 : 6 : else if (read_size == 0)
8291 : : {
8292 : 3 : g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
8293 : : G_FILE_ATTRIBUTE_ETAG_VALUE,
8294 : : 0,
8295 : : g_task_get_cancellable (data->task),
8296 : : load_contents_fstat_callback,
8297 : : data);
8298 : : }
8299 : 3 : else if (read_size > 0)
8300 : : {
8301 : 3 : data->pos += read_size;
8302 : :
8303 : 3 : load_contents_data_ensure_space (data, GET_CONTENT_BLOCK_SIZE);
8304 : :
8305 : 3 : if (data->read_more_callback &&
8306 : 0 : !data->read_more_callback (data->data, data->pos,
8307 : 0 : g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
8308 : 0 : g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
8309 : : G_FILE_ATTRIBUTE_ETAG_VALUE,
8310 : : 0,
8311 : : g_task_get_cancellable (data->task),
8312 : : load_contents_fstat_callback,
8313 : : data);
8314 : : else
8315 : 3 : g_input_stream_read_async (stream,
8316 : 3 : data->data + data->pos,
8317 : : GET_CONTENT_BLOCK_SIZE,
8318 : : 0,
8319 : : g_task_get_cancellable (data->task),
8320 : : load_contents_read_callback,
8321 : : data);
8322 : : }
8323 : 6 : }
8324 : :
8325 : : static void
8326 : 3 : load_contents_open_callback (GObject *obj,
8327 : : GAsyncResult *open_res,
8328 : : gpointer user_data)
8329 : : {
8330 : 3 : GFile *file = G_FILE (obj);
8331 : : GFileInputStream *stream;
8332 : 3 : LoadContentsData *data = user_data;
8333 : 3 : GError *error = NULL;
8334 : :
8335 : 3 : stream = g_file_read_finish (file, open_res, &error);
8336 : :
8337 : 3 : if (stream)
8338 : : {
8339 : 3 : load_contents_data_ensure_space (data, GET_CONTENT_BLOCK_SIZE);
8340 : 6 : g_input_stream_read_async (G_INPUT_STREAM (stream),
8341 : 3 : data->data + data->pos,
8342 : : GET_CONTENT_BLOCK_SIZE,
8343 : : 0,
8344 : : g_task_get_cancellable (data->task),
8345 : : load_contents_read_callback,
8346 : : data);
8347 : : }
8348 : : else
8349 : : {
8350 : 0 : g_task_return_error (data->task, error);
8351 : 0 : g_object_unref (data->task);
8352 : : }
8353 : 3 : }
8354 : :
8355 : : /**
8356 : : * g_file_load_partial_contents_async: (skip)
8357 : : * @file: input #GFile
8358 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8359 : : * @read_more_callback: (scope call) (closure user_data): a
8360 : : * #GFileReadMoreCallback to receive partial data
8361 : : * and to specify whether further data should be read
8362 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
8363 : : * when the request is satisfied
8364 : : * @user_data: the data to pass to the callback functions
8365 : : *
8366 : : * Reads the partial contents of a file. A #GFileReadMoreCallback should
8367 : : * be used to stop reading from the file when appropriate, else this
8368 : : * function will behave exactly as g_file_load_contents_async(). This
8369 : : * operation can be finished by g_file_load_partial_contents_finish().
8370 : : *
8371 : : * Users of this function should be aware that @user_data is passed to
8372 : : * both the @read_more_callback and the @callback.
8373 : : *
8374 : : * If @cancellable is not %NULL, then the operation can be cancelled by
8375 : : * triggering the cancellable object from another thread. If the operation
8376 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8377 : : */
8378 : : void
8379 : 3 : g_file_load_partial_contents_async (GFile *file,
8380 : : GCancellable *cancellable,
8381 : : GFileReadMoreCallback read_more_callback,
8382 : : GAsyncReadyCallback callback,
8383 : : gpointer user_data)
8384 : : {
8385 : : LoadContentsData *data;
8386 : :
8387 : 3 : g_return_if_fail (G_IS_FILE (file));
8388 : :
8389 : 3 : data = g_new0 (LoadContentsData, 1);
8390 : 3 : data->read_more_callback = read_more_callback;
8391 : :
8392 : 3 : data->task = g_task_new (file, cancellable, callback, user_data);
8393 : 3 : g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
8394 : 3 : g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
8395 : :
8396 : 3 : g_file_read_async (file,
8397 : : 0,
8398 : : g_task_get_cancellable (data->task),
8399 : : load_contents_open_callback,
8400 : : data);
8401 : : }
8402 : :
8403 : : /**
8404 : : * g_file_load_partial_contents_finish:
8405 : : * @file: input #GFile
8406 : : * @res: a #GAsyncResult
8407 : : * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
8408 : : * @length: (out) (optional): a location to place the length of the contents of the file,
8409 : : * or %NULL if the length is not needed
8410 : : * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
8411 : : * or %NULL if the entity tag is not needed
8412 : : * @error: a #GError, or %NULL
8413 : : *
8414 : : * Finishes an asynchronous partial load operation that was started
8415 : : * with g_file_load_partial_contents_async(). The data is always
8416 : : * zero-terminated, but this is not included in the resultant @length.
8417 : : * The returned @contents should be freed with g_free() when no longer
8418 : : * needed.
8419 : : *
8420 : : * Returns: %TRUE if the load was successful. If %FALSE and @error is
8421 : : * present, it will be set appropriately.
8422 : : */
8423 : : gboolean
8424 : 3 : g_file_load_partial_contents_finish (GFile *file,
8425 : : GAsyncResult *res,
8426 : : char **contents,
8427 : : gsize *length,
8428 : : char **etag_out,
8429 : : GError **error)
8430 : : {
8431 : : GTask *task;
8432 : : LoadContentsData *data;
8433 : :
8434 : 3 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
8435 : 3 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8436 : 3 : g_return_val_if_fail (contents != NULL, FALSE);
8437 : :
8438 : 3 : task = G_TASK (res);
8439 : :
8440 : 3 : if (!g_task_propagate_boolean (task, error))
8441 : : {
8442 : 0 : if (length)
8443 : 0 : *length = 0;
8444 : 0 : return FALSE;
8445 : : }
8446 : :
8447 : 3 : data = g_task_get_task_data (task);
8448 : :
8449 : 3 : if (length)
8450 : 3 : *length = data->pos;
8451 : :
8452 : 3 : if (etag_out)
8453 : : {
8454 : 1 : *etag_out = data->etag;
8455 : 1 : data->etag = NULL;
8456 : : }
8457 : :
8458 : : /* Zero terminate */
8459 : 3 : load_contents_data_ensure_space (data, 1);
8460 : 3 : data->data[data->pos] = 0;
8461 : :
8462 : 3 : *contents = g_steal_pointer (&data->data);
8463 : :
8464 : 3 : return TRUE;
8465 : : }
8466 : :
8467 : : /**
8468 : : * g_file_load_contents_async:
8469 : : * @file: input #GFile
8470 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8471 : : * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8472 : : * @user_data: the data to pass to callback function
8473 : : *
8474 : : * Starts an asynchronous load of the @file's contents.
8475 : : *
8476 : : * For more details, see g_file_load_contents() which is
8477 : : * the synchronous version of this call.
8478 : : *
8479 : : * When the load operation has completed, @callback will be called
8480 : : * with @user data. To finish the operation, call
8481 : : * g_file_load_contents_finish() with the #GAsyncResult returned by
8482 : : * the @callback.
8483 : : *
8484 : : * If @cancellable is not %NULL, then the operation can be cancelled by
8485 : : * triggering the cancellable object from another thread. If the operation
8486 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8487 : : */
8488 : : void
8489 : 3 : g_file_load_contents_async (GFile *file,
8490 : : GCancellable *cancellable,
8491 : : GAsyncReadyCallback callback,
8492 : : gpointer user_data)
8493 : : {
8494 : 3 : g_file_load_partial_contents_async (file,
8495 : : cancellable,
8496 : : NULL,
8497 : : callback, user_data);
8498 : 3 : }
8499 : :
8500 : : /**
8501 : : * g_file_load_contents_finish:
8502 : : * @file: input #GFile
8503 : : * @res: a #GAsyncResult
8504 : : * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
8505 : : * @length: (out) (optional): a location to place the length of the contents of the file,
8506 : : * or %NULL if the length is not needed
8507 : : * @etag_out: (out) (optional) (nullable): a location to place the current entity tag for the file,
8508 : : * or %NULL if the entity tag is not needed
8509 : : * @error: a #GError, or %NULL
8510 : : *
8511 : : * Finishes an asynchronous load of the @file's contents.
8512 : : * The contents are placed in @contents, and @length is set to the
8513 : : * size of the @contents string. The @contents should be freed with
8514 : : * g_free() when no longer needed. If @etag_out is present, it will be
8515 : : * set to the new entity tag for the @file.
8516 : : *
8517 : : * Returns: %TRUE if the load was successful. If %FALSE and @error is
8518 : : * present, it will be set appropriately.
8519 : : */
8520 : : gboolean
8521 : 3 : g_file_load_contents_finish (GFile *file,
8522 : : GAsyncResult *res,
8523 : : char **contents,
8524 : : gsize *length,
8525 : : char **etag_out,
8526 : : GError **error)
8527 : : {
8528 : 3 : return g_file_load_partial_contents_finish (file,
8529 : : res,
8530 : : contents,
8531 : : length,
8532 : : etag_out,
8533 : : error);
8534 : : }
8535 : :
8536 : : /**
8537 : : * g_file_replace_contents:
8538 : : * @file: input #GFile
8539 : : * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
8540 : : * @length: the length of @contents in bytes
8541 : : * @etag: (nullable): the old [entity-tag](#entity-tags) for the document,
8542 : : * or %NULL
8543 : : * @make_backup: %TRUE if a backup should be created
8544 : : * @flags: a set of #GFileCreateFlags
8545 : : * @new_etag: (out) (optional) (nullable): a location to a new [entity tag](#entity-tags)
8546 : : * for the document. This should be freed with g_free() when no longer
8547 : : * needed, or %NULL
8548 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8549 : : * @error: a #GError, or %NULL
8550 : : *
8551 : : * Replaces the contents of @file with @contents of @length bytes.
8552 : : *
8553 : : * If @etag is specified (not %NULL), any existing file must have that etag,
8554 : : * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
8555 : : *
8556 : : * If @make_backup is %TRUE, this function will attempt to make a backup
8557 : : * of @file. Internally, it uses g_file_replace(), so will try to replace the
8558 : : * file contents in the safest way possible. For example, atomic renames are
8559 : : * used when replacing local files’ contents.
8560 : : *
8561 : : * If @cancellable is not %NULL, then the operation can be cancelled by
8562 : : * triggering the cancellable object from another thread. If the operation
8563 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8564 : : *
8565 : : * The returned @new_etag can be used to verify that the file hasn't
8566 : : * changed the next time it is saved over.
8567 : : *
8568 : : * Returns: %TRUE if successful. If an error has occurred, this function
8569 : : * will return %FALSE and set @error appropriately if present.
8570 : : */
8571 : : gboolean
8572 : 75 : g_file_replace_contents (GFile *file,
8573 : : const char *contents,
8574 : : gsize length,
8575 : : const char *etag,
8576 : : gboolean make_backup,
8577 : : GFileCreateFlags flags,
8578 : : char **new_etag,
8579 : : GCancellable *cancellable,
8580 : : GError **error)
8581 : : {
8582 : : GFileOutputStream *out;
8583 : : gsize pos, remainder;
8584 : 75 : gssize res = -1;
8585 : : gboolean ret;
8586 : :
8587 : 75 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
8588 : 75 : g_return_val_if_fail (contents != NULL, FALSE);
8589 : :
8590 : 75 : out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
8591 : 75 : if (out == NULL)
8592 : 0 : return FALSE;
8593 : :
8594 : 75 : pos = 0;
8595 : 75 : remainder = length;
8596 : 215 : while (remainder > 0 &&
8597 : 70 : (res = g_output_stream_write (G_OUTPUT_STREAM (out),
8598 : 70 : contents + pos,
8599 : : MIN (remainder, GET_CONTENT_BLOCK_SIZE),
8600 : : cancellable,
8601 : : error)) > 0)
8602 : : {
8603 : 70 : pos += res;
8604 : 70 : remainder -= res;
8605 : : }
8606 : :
8607 : 75 : if (remainder > 0 && res < 0)
8608 : : {
8609 : : /* Ignore errors on close */
8610 : 0 : g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
8611 : 0 : g_object_unref (out);
8612 : :
8613 : : /* error is set already */
8614 : 0 : return FALSE;
8615 : : }
8616 : :
8617 : 75 : ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
8618 : :
8619 : 75 : if (new_etag)
8620 : 0 : *new_etag = g_file_output_stream_get_etag (out);
8621 : :
8622 : 75 : g_object_unref (out);
8623 : :
8624 : 75 : return ret;
8625 : : }
8626 : :
8627 : : typedef struct {
8628 : : GTask *task;
8629 : : GBytes *content;
8630 : : gsize pos;
8631 : : char *etag;
8632 : : gboolean failed;
8633 : : } ReplaceContentsData;
8634 : :
8635 : : static void
8636 : 2 : replace_contents_data_free (ReplaceContentsData *data)
8637 : : {
8638 : 2 : g_bytes_unref (data->content);
8639 : 2 : g_free (data->etag);
8640 : 2 : g_free (data);
8641 : 2 : }
8642 : :
8643 : : static void
8644 : 2 : replace_contents_close_callback (GObject *obj,
8645 : : GAsyncResult *close_res,
8646 : : gpointer user_data)
8647 : : {
8648 : 2 : GOutputStream *stream = G_OUTPUT_STREAM (obj);
8649 : 2 : ReplaceContentsData *data = user_data;
8650 : :
8651 : : /* Ignore errors here, we're only reading anyway */
8652 : 2 : g_output_stream_close_finish (stream, close_res, NULL);
8653 : :
8654 : 2 : if (!data->failed)
8655 : : {
8656 : 2 : data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
8657 : 2 : g_task_return_boolean (data->task, TRUE);
8658 : : }
8659 : 2 : g_object_unref (data->task);
8660 : 2 : }
8661 : :
8662 : : static void
8663 : 2 : replace_contents_write_callback (GObject *obj,
8664 : : GAsyncResult *read_res,
8665 : : gpointer user_data)
8666 : : {
8667 : 2 : GOutputStream *stream = G_OUTPUT_STREAM (obj);
8668 : 2 : ReplaceContentsData *data = user_data;
8669 : 2 : GError *error = NULL;
8670 : : gssize write_size;
8671 : :
8672 : 2 : write_size = g_output_stream_write_finish (stream, read_res, &error);
8673 : :
8674 : 2 : if (write_size <= 0)
8675 : : {
8676 : : /* Error or EOF, close the file */
8677 : 0 : if (write_size < 0)
8678 : : {
8679 : 0 : data->failed = TRUE;
8680 : 0 : g_task_return_error (data->task, error);
8681 : : }
8682 : 0 : g_output_stream_close_async (stream, 0,
8683 : : g_task_get_cancellable (data->task),
8684 : : replace_contents_close_callback, data);
8685 : : }
8686 : 2 : else if (write_size > 0)
8687 : : {
8688 : : const gchar *content;
8689 : : gsize length;
8690 : :
8691 : 2 : content = g_bytes_get_data (data->content, &length);
8692 : 2 : data->pos += write_size;
8693 : :
8694 : 2 : if (data->pos >= length)
8695 : 2 : g_output_stream_close_async (stream, 0,
8696 : : g_task_get_cancellable (data->task),
8697 : : replace_contents_close_callback, data);
8698 : : else
8699 : 0 : g_output_stream_write_async (stream,
8700 : 0 : content + data->pos,
8701 : 0 : length - data->pos,
8702 : : 0,
8703 : : g_task_get_cancellable (data->task),
8704 : : replace_contents_write_callback,
8705 : : data);
8706 : : }
8707 : 2 : }
8708 : :
8709 : : static void
8710 : 2 : replace_contents_open_callback (GObject *obj,
8711 : : GAsyncResult *open_res,
8712 : : gpointer user_data)
8713 : : {
8714 : 2 : GFile *file = G_FILE (obj);
8715 : : GFileOutputStream *stream;
8716 : 2 : ReplaceContentsData *data = user_data;
8717 : 2 : GError *error = NULL;
8718 : :
8719 : 2 : stream = g_file_replace_finish (file, open_res, &error);
8720 : :
8721 : 2 : if (stream)
8722 : : {
8723 : : const gchar *content;
8724 : : gsize length;
8725 : :
8726 : 2 : content = g_bytes_get_data (data->content, &length);
8727 : 4 : g_output_stream_write_async (G_OUTPUT_STREAM (stream),
8728 : 2 : content + data->pos,
8729 : 2 : length - data->pos,
8730 : : 0,
8731 : : g_task_get_cancellable (data->task),
8732 : : replace_contents_write_callback,
8733 : : data);
8734 : 2 : g_object_unref (stream); /* ownership is transferred to the write_async() call above */
8735 : : }
8736 : : else
8737 : : {
8738 : 0 : g_task_return_error (data->task, error);
8739 : 0 : g_object_unref (data->task);
8740 : : }
8741 : 2 : }
8742 : :
8743 : : /**
8744 : : * g_file_replace_contents_async:
8745 : : * @file: input #GFile
8746 : : * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
8747 : : * @length: the length of @contents in bytes
8748 : : * @etag: (nullable): a new [entity tag](#entity-tags) for the @file, or %NULL
8749 : : * @make_backup: %TRUE if a backup should be created
8750 : : * @flags: a set of #GFileCreateFlags
8751 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8752 : : * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8753 : : * @user_data: the data to pass to callback function
8754 : : *
8755 : : * Starts an asynchronous replacement of @file with the given
8756 : : * @contents of @length bytes. @etag will replace the document's
8757 : : * current entity tag.
8758 : : *
8759 : : * When this operation has completed, @callback will be called with
8760 : : * @user_user data, and the operation can be finalized with
8761 : : * g_file_replace_contents_finish().
8762 : : *
8763 : : * If @cancellable is not %NULL, then the operation can be cancelled by
8764 : : * triggering the cancellable object from another thread. If the operation
8765 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8766 : : *
8767 : : * If @make_backup is %TRUE, this function will attempt to
8768 : : * make a backup of @file.
8769 : : *
8770 : : * Note that no copy of @contents will be made, so it must stay valid
8771 : : * until @callback is called. See g_file_replace_contents_bytes_async()
8772 : : * for a #GBytes version that will automatically hold a reference to the
8773 : : * contents (without copying) for the duration of the call.
8774 : : */
8775 : : void
8776 : 2 : g_file_replace_contents_async (GFile *file,
8777 : : const char *contents,
8778 : : gsize length,
8779 : : const char *etag,
8780 : : gboolean make_backup,
8781 : : GFileCreateFlags flags,
8782 : : GCancellable *cancellable,
8783 : : GAsyncReadyCallback callback,
8784 : : gpointer user_data)
8785 : : {
8786 : : GBytes *bytes;
8787 : :
8788 : 2 : bytes = g_bytes_new_static (contents, length);
8789 : 2 : g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
8790 : : cancellable, callback, user_data);
8791 : 2 : g_bytes_unref (bytes);
8792 : 2 : }
8793 : :
8794 : : /**
8795 : : * g_file_replace_contents_bytes_async:
8796 : : * @file: input #GFile
8797 : : * @contents: a #GBytes
8798 : : * @etag: (nullable): a new [entity tag](#entity-tags) for the @file, or %NULL
8799 : : * @make_backup: %TRUE if a backup should be created
8800 : : * @flags: a set of #GFileCreateFlags
8801 : : * @cancellable: optional #GCancellable object, %NULL to ignore
8802 : : * @callback: a #GAsyncReadyCallback to call when the request is satisfied
8803 : : * @user_data: the data to pass to callback function
8804 : : *
8805 : : * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
8806 : : * This function will keep a ref on @contents until the operation is done.
8807 : : * Unlike g_file_replace_contents_async() this allows forgetting about the
8808 : : * content without waiting for the callback.
8809 : : *
8810 : : * When this operation has completed, @callback will be called with
8811 : : * @user_user data, and the operation can be finalized with
8812 : : * g_file_replace_contents_finish().
8813 : : *
8814 : : * Since: 2.40
8815 : : */
8816 : : void
8817 : 2 : g_file_replace_contents_bytes_async (GFile *file,
8818 : : GBytes *contents,
8819 : : const char *etag,
8820 : : gboolean make_backup,
8821 : : GFileCreateFlags flags,
8822 : : GCancellable *cancellable,
8823 : : GAsyncReadyCallback callback,
8824 : : gpointer user_data)
8825 : : {
8826 : : ReplaceContentsData *data;
8827 : :
8828 : 2 : g_return_if_fail (G_IS_FILE (file));
8829 : 2 : g_return_if_fail (contents != NULL);
8830 : :
8831 : 2 : data = g_new0 (ReplaceContentsData, 1);
8832 : :
8833 : 2 : data->content = g_bytes_ref (contents);
8834 : :
8835 : 2 : data->task = g_task_new (file, cancellable, callback, user_data);
8836 : 2 : g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
8837 : 2 : g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
8838 : :
8839 : 2 : g_file_replace_async (file,
8840 : : etag,
8841 : : make_backup,
8842 : : flags,
8843 : : 0,
8844 : : g_task_get_cancellable (data->task),
8845 : : replace_contents_open_callback,
8846 : : data);
8847 : : }
8848 : :
8849 : : /**
8850 : : * g_file_replace_contents_finish:
8851 : : * @file: input #GFile
8852 : : * @res: a #GAsyncResult
8853 : : * @new_etag: (out) (optional) (nullable): a location of a new [entity tag](#entity-tags)
8854 : : * for the document. This should be freed with g_free() when it is no
8855 : : * longer needed, or %NULL
8856 : : * @error: a #GError, or %NULL
8857 : : *
8858 : : * Finishes an asynchronous replace of the given @file. See
8859 : : * g_file_replace_contents_async(). Sets @new_etag to the new entity
8860 : : * tag for the document, if present.
8861 : : *
8862 : : * Returns: %TRUE on success, %FALSE on failure.
8863 : : */
8864 : : gboolean
8865 : 2 : g_file_replace_contents_finish (GFile *file,
8866 : : GAsyncResult *res,
8867 : : char **new_etag,
8868 : : GError **error)
8869 : : {
8870 : : GTask *task;
8871 : : ReplaceContentsData *data;
8872 : :
8873 : 2 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
8874 : 2 : g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8875 : :
8876 : 2 : task = G_TASK (res);
8877 : :
8878 : 2 : if (!g_task_propagate_boolean (task, error))
8879 : 0 : return FALSE;
8880 : :
8881 : 2 : data = g_task_get_task_data (task);
8882 : :
8883 : 2 : if (new_etag)
8884 : : {
8885 : 0 : *new_etag = data->etag;
8886 : 0 : data->etag = NULL; /* Take ownership */
8887 : : }
8888 : :
8889 : 2 : return TRUE;
8890 : : }
8891 : :
8892 : : gboolean
8893 : 0 : g_file_real_measure_disk_usage (GFile *file,
8894 : : GFileMeasureFlags flags,
8895 : : GCancellable *cancellable,
8896 : : GFileMeasureProgressCallback progress_callback,
8897 : : gpointer progress_data,
8898 : : guint64 *disk_usage,
8899 : : guint64 *num_dirs,
8900 : : guint64 *num_files,
8901 : : GError **error)
8902 : : {
8903 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8904 : : "Operation not supported for the current backend.");
8905 : 0 : return FALSE;
8906 : : }
8907 : :
8908 : : typedef struct
8909 : : {
8910 : : GFileMeasureFlags flags;
8911 : : GFileMeasureProgressCallback progress_callback;
8912 : : gpointer progress_data;
8913 : : } MeasureTaskData;
8914 : :
8915 : : typedef struct
8916 : : {
8917 : : guint64 disk_usage;
8918 : : guint64 num_dirs;
8919 : : guint64 num_files;
8920 : : } MeasureResult;
8921 : :
8922 : : typedef struct
8923 : : {
8924 : : GFileMeasureProgressCallback callback;
8925 : : gpointer user_data;
8926 : : gboolean reporting;
8927 : : guint64 current_size;
8928 : : guint64 num_dirs;
8929 : : guint64 num_files;
8930 : : } MeasureProgress;
8931 : :
8932 : : static gboolean
8933 : 1 : measure_disk_usage_invoke_progress (gpointer user_data)
8934 : : {
8935 : 1 : MeasureProgress *progress = user_data;
8936 : :
8937 : 1 : (* progress->callback) (progress->reporting,
8938 : : progress->current_size, progress->num_dirs, progress->num_files,
8939 : : progress->user_data);
8940 : :
8941 : 1 : return FALSE;
8942 : : }
8943 : :
8944 : : static void
8945 : 1 : measure_disk_usage_progress (gboolean reporting,
8946 : : guint64 current_size,
8947 : : guint64 num_dirs,
8948 : : guint64 num_files,
8949 : : gpointer user_data)
8950 : : {
8951 : : MeasureProgress progress;
8952 : 1 : GTask *task = user_data;
8953 : : MeasureTaskData *data;
8954 : :
8955 : 1 : data = g_task_get_task_data (task);
8956 : :
8957 : 1 : progress.callback = data->progress_callback;
8958 : 1 : progress.user_data = data->progress_data;
8959 : 1 : progress.reporting = reporting;
8960 : 1 : progress.current_size = current_size;
8961 : 1 : progress.num_dirs = num_dirs;
8962 : 1 : progress.num_files = num_files;
8963 : :
8964 : 1 : g_main_context_invoke_full (g_task_get_context (task),
8965 : : g_task_get_priority (task),
8966 : : measure_disk_usage_invoke_progress,
8967 : : g_memdup2 (&progress, sizeof progress),
8968 : : g_free);
8969 : 1 : }
8970 : :
8971 : : static void
8972 : 1 : measure_disk_usage_thread (GTask *task,
8973 : : gpointer source_object,
8974 : : gpointer task_data,
8975 : : GCancellable *cancellable)
8976 : : {
8977 : 1 : MeasureTaskData *data = task_data;
8978 : 1 : GError *error = NULL;
8979 : 1 : MeasureResult result = { 0, };
8980 : :
8981 : 1 : if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
8982 : 1 : data->progress_callback ? measure_disk_usage_progress : NULL, task,
8983 : : &result.disk_usage, &result.num_dirs, &result.num_files,
8984 : : &error))
8985 : 1 : g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
8986 : : else
8987 : 0 : g_task_return_error (task, error);
8988 : 1 : }
8989 : :
8990 : : static void
8991 : 1 : g_file_real_measure_disk_usage_async (GFile *file,
8992 : : GFileMeasureFlags flags,
8993 : : gint io_priority,
8994 : : GCancellable *cancellable,
8995 : : GFileMeasureProgressCallback progress_callback,
8996 : : gpointer progress_data,
8997 : : GAsyncReadyCallback callback,
8998 : : gpointer user_data)
8999 : : {
9000 : : MeasureTaskData data;
9001 : : GTask *task;
9002 : :
9003 : 1 : data.flags = flags;
9004 : 1 : data.progress_callback = progress_callback;
9005 : 1 : data.progress_data = progress_data;
9006 : :
9007 : 1 : task = g_task_new (file, cancellable, callback, user_data);
9008 : 1 : g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
9009 : 1 : g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
9010 : 1 : g_task_set_priority (task, io_priority);
9011 : :
9012 : 1 : g_task_run_in_thread (task, measure_disk_usage_thread);
9013 : 1 : g_object_unref (task);
9014 : 1 : }
9015 : :
9016 : : static gboolean
9017 : 1 : g_file_real_measure_disk_usage_finish (GFile *file,
9018 : : GAsyncResult *result,
9019 : : guint64 *disk_usage,
9020 : : guint64 *num_dirs,
9021 : : guint64 *num_files,
9022 : : GError **error)
9023 : : {
9024 : : MeasureResult *measure_result;
9025 : :
9026 : 1 : g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
9027 : :
9028 : 1 : measure_result = g_task_propagate_pointer (G_TASK (result), error);
9029 : :
9030 : 1 : if (measure_result == NULL)
9031 : 0 : return FALSE;
9032 : :
9033 : 1 : if (disk_usage)
9034 : 1 : *disk_usage = measure_result->disk_usage;
9035 : :
9036 : 1 : if (num_dirs)
9037 : 1 : *num_dirs = measure_result->num_dirs;
9038 : :
9039 : 1 : if (num_files)
9040 : 1 : *num_files = measure_result->num_files;
9041 : :
9042 : 1 : g_free (measure_result);
9043 : :
9044 : 1 : return TRUE;
9045 : : }
9046 : :
9047 : : /**
9048 : : * g_file_measure_disk_usage:
9049 : : * @file: a #GFile
9050 : : * @flags: #GFileMeasureFlags
9051 : : * @cancellable: (nullable): optional #GCancellable
9052 : : * @progress_callback: (nullable) (scope call): a #GFileMeasureProgressCallback
9053 : : * @progress_data: user_data for @progress_callback
9054 : : * @disk_usage: (out) (optional): the number of bytes of disk space used
9055 : : * @num_dirs: (out) (optional): the number of directories encountered
9056 : : * @num_files: (out) (optional): the number of non-directories encountered
9057 : : * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
9058 : : *
9059 : : * Recursively measures the disk usage of @file.
9060 : : *
9061 : : * This is essentially an analog of the 'du' command, but it also
9062 : : * reports the number of directories and non-directory files encountered
9063 : : * (including things like symbolic links).
9064 : : *
9065 : : * By default, errors are only reported against the toplevel file
9066 : : * itself. Errors found while recursing are silently ignored, unless
9067 : : * %G_FILE_MEASURE_REPORT_ANY_ERROR is given in @flags.
9068 : : *
9069 : : * The returned size, @disk_usage, is in bytes and should be formatted
9070 : : * with g_format_size() in order to get something reasonable for showing
9071 : : * in a user interface.
9072 : : *
9073 : : * @progress_callback and @progress_data can be given to request
9074 : : * periodic progress updates while scanning. See the documentation for
9075 : : * #GFileMeasureProgressCallback for information about when and how the
9076 : : * callback will be invoked.
9077 : : *
9078 : : * Returns: %TRUE if successful, with the out parameters set.
9079 : : * %FALSE otherwise, with @error set.
9080 : : *
9081 : : * Since: 2.38
9082 : : **/
9083 : : gboolean
9084 : 2 : g_file_measure_disk_usage (GFile *file,
9085 : : GFileMeasureFlags flags,
9086 : : GCancellable *cancellable,
9087 : : GFileMeasureProgressCallback progress_callback,
9088 : : gpointer progress_data,
9089 : : guint64 *disk_usage,
9090 : : guint64 *num_dirs,
9091 : : guint64 *num_files,
9092 : : GError **error)
9093 : : {
9094 : 2 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9095 : 2 : g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
9096 : 2 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
9097 : :
9098 : 2 : return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
9099 : : progress_callback, progress_data,
9100 : : disk_usage, num_dirs, num_files,
9101 : : error);
9102 : : }
9103 : :
9104 : : /**
9105 : : * g_file_measure_disk_usage_async:
9106 : : * @file: a #GFile
9107 : : * @flags: #GFileMeasureFlags
9108 : : * @io_priority: the [I/O priority](iface.AsyncResult.html#io-priority) of the request
9109 : : * @cancellable: (nullable): optional #GCancellable
9110 : : * @progress_callback: (nullable): a #GFileMeasureProgressCallback
9111 : : * @progress_data: user_data for @progress_callback
9112 : : * @callback: (nullable): a #GAsyncReadyCallback to call when complete
9113 : : * @user_data: the data to pass to callback function
9114 : : *
9115 : : * Recursively measures the disk usage of @file.
9116 : : *
9117 : : * This is the asynchronous version of g_file_measure_disk_usage(). See
9118 : : * there for more information.
9119 : : *
9120 : : * Since: 2.38
9121 : : **/
9122 : : void
9123 : 1 : g_file_measure_disk_usage_async (GFile *file,
9124 : : GFileMeasureFlags flags,
9125 : : gint io_priority,
9126 : : GCancellable *cancellable,
9127 : : GFileMeasureProgressCallback progress_callback,
9128 : : gpointer progress_data,
9129 : : GAsyncReadyCallback callback,
9130 : : gpointer user_data)
9131 : : {
9132 : 1 : g_return_if_fail (G_IS_FILE (file));
9133 : 1 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
9134 : :
9135 : 1 : G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
9136 : : progress_callback, progress_data,
9137 : : callback, user_data);
9138 : : }
9139 : :
9140 : : /**
9141 : : * g_file_measure_disk_usage_finish:
9142 : : * @file: a #GFile
9143 : : * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
9144 : : * @disk_usage: (out) (optional): the number of bytes of disk space used
9145 : : * @num_dirs: (out) (optional): the number of directories encountered
9146 : : * @num_files: (out) (optional): the number of non-directories encountered
9147 : : * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
9148 : : *
9149 : : * Collects the results from an earlier call to
9150 : : * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
9151 : : * more information.
9152 : : *
9153 : : * Returns: %TRUE if successful, with the out parameters set.
9154 : : * %FALSE otherwise, with @error set.
9155 : : *
9156 : : * Since: 2.38
9157 : : **/
9158 : : gboolean
9159 : 1 : g_file_measure_disk_usage_finish (GFile *file,
9160 : : GAsyncResult *result,
9161 : : guint64 *disk_usage,
9162 : : guint64 *num_dirs,
9163 : : guint64 *num_files,
9164 : : GError **error)
9165 : : {
9166 : 1 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9167 : 1 : g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
9168 : :
9169 : 1 : return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
9170 : : }
9171 : :
9172 : : /**
9173 : : * g_file_start_mountable:
9174 : : * @file: input #GFile
9175 : : * @flags: flags affecting the operation
9176 : : * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
9177 : : * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
9178 : : * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
9179 : : * @user_data: the data to pass to callback function
9180 : : *
9181 : : * Starts a file of type %G_FILE_TYPE_MOUNTABLE.
9182 : : * Using @start_operation, you can request callbacks when, for instance,
9183 : : * passwords are needed during authentication.
9184 : : *
9185 : : * If @cancellable is not %NULL, then the operation can be cancelled by
9186 : : * triggering the cancellable object from another thread. If the operation
9187 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
9188 : : *
9189 : : * When the operation is finished, @callback will be called.
9190 : : * You can then call g_file_mount_mountable_finish() to get
9191 : : * the result of the operation.
9192 : : *
9193 : : * Since: 2.22
9194 : : */
9195 : : void
9196 : 0 : g_file_start_mountable (GFile *file,
9197 : : GDriveStartFlags flags,
9198 : : GMountOperation *start_operation,
9199 : : GCancellable *cancellable,
9200 : : GAsyncReadyCallback callback,
9201 : : gpointer user_data)
9202 : : {
9203 : : GFileIface *iface;
9204 : :
9205 : 0 : g_return_if_fail (G_IS_FILE (file));
9206 : :
9207 : 0 : iface = G_FILE_GET_IFACE (file);
9208 : :
9209 : 0 : if (iface->start_mountable == NULL)
9210 : : {
9211 : 0 : g_task_report_new_error (file, callback, user_data,
9212 : : g_file_start_mountable,
9213 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9214 : 0 : _("Operation not supported"));
9215 : 0 : return;
9216 : : }
9217 : :
9218 : 0 : (* iface->start_mountable) (file,
9219 : : flags,
9220 : : start_operation,
9221 : : cancellable,
9222 : : callback,
9223 : : user_data);
9224 : : }
9225 : :
9226 : : /**
9227 : : * g_file_start_mountable_finish:
9228 : : * @file: input #GFile
9229 : : * @result: a #GAsyncResult
9230 : : * @error: a #GError, or %NULL
9231 : : *
9232 : : * Finishes a start operation. See g_file_start_mountable() for details.
9233 : : *
9234 : : * Finish an asynchronous start operation that was started
9235 : : * with g_file_start_mountable().
9236 : : *
9237 : : * Returns: %TRUE if the operation finished successfully. %FALSE
9238 : : * otherwise.
9239 : : *
9240 : : * Since: 2.22
9241 : : */
9242 : : gboolean
9243 : 0 : g_file_start_mountable_finish (GFile *file,
9244 : : GAsyncResult *result,
9245 : : GError **error)
9246 : : {
9247 : : GFileIface *iface;
9248 : :
9249 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9250 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9251 : :
9252 : 0 : if (g_async_result_legacy_propagate_error (result, error))
9253 : 0 : return FALSE;
9254 : 0 : else if (g_async_result_is_tagged (result, g_file_start_mountable))
9255 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
9256 : :
9257 : 0 : iface = G_FILE_GET_IFACE (file);
9258 : 0 : return (* iface->start_mountable_finish) (file, result, error);
9259 : : }
9260 : :
9261 : : /**
9262 : : * g_file_stop_mountable:
9263 : : * @file: input #GFile
9264 : : * @flags: flags affecting the operation
9265 : : * @mount_operation: (nullable): a #GMountOperation,
9266 : : * or %NULL to avoid user interaction.
9267 : : * @cancellable: (nullable): optional #GCancellable object,
9268 : : * %NULL to ignore
9269 : : * @callback: (nullable): a #GAsyncReadyCallback to call
9270 : : * when the request is satisfied, or %NULL
9271 : : * @user_data: the data to pass to callback function
9272 : : *
9273 : : * Stops a file of type %G_FILE_TYPE_MOUNTABLE.
9274 : : *
9275 : : * If @cancellable is not %NULL, then the operation can be cancelled by
9276 : : * triggering the cancellable object from another thread. If the operation
9277 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
9278 : : *
9279 : : * When the operation is finished, @callback will be called.
9280 : : * You can then call g_file_stop_mountable_finish() to get
9281 : : * the result of the operation.
9282 : : *
9283 : : * Since: 2.22
9284 : : */
9285 : : void
9286 : 0 : g_file_stop_mountable (GFile *file,
9287 : : GMountUnmountFlags flags,
9288 : : GMountOperation *mount_operation,
9289 : : GCancellable *cancellable,
9290 : : GAsyncReadyCallback callback,
9291 : : gpointer user_data)
9292 : : {
9293 : : GFileIface *iface;
9294 : :
9295 : 0 : g_return_if_fail (G_IS_FILE (file));
9296 : :
9297 : 0 : iface = G_FILE_GET_IFACE (file);
9298 : :
9299 : 0 : if (iface->stop_mountable == NULL)
9300 : : {
9301 : 0 : g_task_report_new_error (file, callback, user_data,
9302 : : g_file_stop_mountable,
9303 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9304 : 0 : _("Operation not supported"));
9305 : 0 : return;
9306 : : }
9307 : :
9308 : 0 : (* iface->stop_mountable) (file,
9309 : : flags,
9310 : : mount_operation,
9311 : : cancellable,
9312 : : callback,
9313 : : user_data);
9314 : : }
9315 : :
9316 : : /**
9317 : : * g_file_stop_mountable_finish:
9318 : : * @file: input #GFile
9319 : : * @result: a #GAsyncResult
9320 : : * @error: a #GError, or %NULL
9321 : : *
9322 : : * Finishes a stop operation, see g_file_stop_mountable() for details.
9323 : : *
9324 : : * Finish an asynchronous stop operation that was started
9325 : : * with g_file_stop_mountable().
9326 : : *
9327 : : * Returns: %TRUE if the operation finished successfully.
9328 : : * %FALSE otherwise.
9329 : : *
9330 : : * Since: 2.22
9331 : : */
9332 : : gboolean
9333 : 0 : g_file_stop_mountable_finish (GFile *file,
9334 : : GAsyncResult *result,
9335 : : GError **error)
9336 : : {
9337 : : GFileIface *iface;
9338 : :
9339 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9340 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9341 : :
9342 : 0 : if (g_async_result_legacy_propagate_error (result, error))
9343 : 0 : return FALSE;
9344 : 0 : else if (g_async_result_is_tagged (result, g_file_stop_mountable))
9345 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
9346 : :
9347 : 0 : iface = G_FILE_GET_IFACE (file);
9348 : 0 : return (* iface->stop_mountable_finish) (file, result, error);
9349 : : }
9350 : :
9351 : : /**
9352 : : * g_file_poll_mountable:
9353 : : * @file: input #GFile
9354 : : * @cancellable: optional #GCancellable object, %NULL to ignore
9355 : : * @callback: (nullable): a #GAsyncReadyCallback to call
9356 : : * when the request is satisfied, or %NULL
9357 : : * @user_data: the data to pass to callback function
9358 : : *
9359 : : * Polls a file of type %G_FILE_TYPE_MOUNTABLE.
9360 : : *
9361 : : * If @cancellable is not %NULL, then the operation can be cancelled by
9362 : : * triggering the cancellable object from another thread. If the operation
9363 : : * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
9364 : : *
9365 : : * When the operation is finished, @callback will be called.
9366 : : * You can then call g_file_mount_mountable_finish() to get
9367 : : * the result of the operation.
9368 : : *
9369 : : * Since: 2.22
9370 : : */
9371 : : void
9372 : 0 : g_file_poll_mountable (GFile *file,
9373 : : GCancellable *cancellable,
9374 : : GAsyncReadyCallback callback,
9375 : : gpointer user_data)
9376 : : {
9377 : : GFileIface *iface;
9378 : :
9379 : 0 : g_return_if_fail (G_IS_FILE (file));
9380 : :
9381 : 0 : iface = G_FILE_GET_IFACE (file);
9382 : :
9383 : 0 : if (iface->poll_mountable == NULL)
9384 : : {
9385 : 0 : g_task_report_new_error (file, callback, user_data,
9386 : : g_file_poll_mountable,
9387 : : G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9388 : 0 : _("Operation not supported"));
9389 : 0 : return;
9390 : : }
9391 : :
9392 : 0 : (* iface->poll_mountable) (file,
9393 : : cancellable,
9394 : : callback,
9395 : : user_data);
9396 : : }
9397 : :
9398 : : /**
9399 : : * g_file_poll_mountable_finish:
9400 : : * @file: input #GFile
9401 : : * @result: a #GAsyncResult
9402 : : * @error: a #GError, or %NULL
9403 : : *
9404 : : * Finishes a poll operation. See g_file_poll_mountable() for details.
9405 : : *
9406 : : * Finish an asynchronous poll operation that was polled
9407 : : * with g_file_poll_mountable().
9408 : : *
9409 : : * Returns: %TRUE if the operation finished successfully. %FALSE
9410 : : * otherwise.
9411 : : *
9412 : : * Since: 2.22
9413 : : */
9414 : : gboolean
9415 : 0 : g_file_poll_mountable_finish (GFile *file,
9416 : : GAsyncResult *result,
9417 : : GError **error)
9418 : : {
9419 : : GFileIface *iface;
9420 : :
9421 : 0 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9422 : 0 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
9423 : :
9424 : 0 : if (g_async_result_legacy_propagate_error (result, error))
9425 : 0 : return FALSE;
9426 : 0 : else if (g_async_result_is_tagged (result, g_file_poll_mountable))
9427 : 0 : return g_task_propagate_boolean (G_TASK (result), error);
9428 : :
9429 : 0 : iface = G_FILE_GET_IFACE (file);
9430 : 0 : return (* iface->poll_mountable_finish) (file, result, error);
9431 : : }
9432 : :
9433 : : /**
9434 : : * g_file_supports_thread_contexts:
9435 : : * @file: a #GFile
9436 : : *
9437 : : * Checks if @file supports thread-default main contexts
9438 : : * (see [method@GLib.MainContext.push_thread_default])
9439 : : * If this returns %FALSE, you cannot perform asynchronous operations on
9440 : : * @file in a thread that has a thread-default context.
9441 : : *
9442 : : * Returns: Whether or not @file supports thread-default contexts.
9443 : : *
9444 : : * Since: 2.22
9445 : : */
9446 : : gboolean
9447 : 2 : g_file_supports_thread_contexts (GFile *file)
9448 : : {
9449 : : GFileIface *iface;
9450 : :
9451 : 2 : g_return_val_if_fail (G_IS_FILE (file), FALSE);
9452 : :
9453 : 2 : iface = G_FILE_GET_IFACE (file);
9454 : 2 : return iface->supports_thread_contexts;
9455 : : }
9456 : :
9457 : : /**
9458 : : * g_file_load_bytes:
9459 : : * @file: a #GFile
9460 : : * @cancellable: (nullable): a #GCancellable or %NULL
9461 : : * @etag_out: (out) (nullable) (optional): a location to place the current
9462 : : * entity tag for the file, or %NULL if the entity tag is not needed
9463 : : * @error: a location for a #GError or %NULL
9464 : : *
9465 : : * Loads the contents of @file and returns it as #GBytes.
9466 : : *
9467 : : * If @file is a resource:// based URI, the resulting bytes will reference the
9468 : : * embedded resource instead of a copy. Otherwise, this is equivalent to calling
9469 : : * g_file_load_contents() and g_bytes_new_take().
9470 : : *
9471 : : * For resources, @etag_out will be set to %NULL.
9472 : : *
9473 : : * The data contained in the resulting #GBytes is always zero-terminated, but
9474 : : * this is not included in the #GBytes length. The resulting #GBytes should be
9475 : : * freed with g_bytes_unref() when no longer in use.
9476 : : *
9477 : : * Returns: (transfer full): a #GBytes or %NULL and @error is set
9478 : : *
9479 : : * Since: 2.56
9480 : : */
9481 : : GBytes *
9482 : 2 : g_file_load_bytes (GFile *file,
9483 : : GCancellable *cancellable,
9484 : : gchar **etag_out,
9485 : : GError **error)
9486 : : {
9487 : : gchar *contents;
9488 : : gsize len;
9489 : :
9490 : 2 : g_return_val_if_fail (G_IS_FILE (file), NULL);
9491 : 2 : g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
9492 : 2 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9493 : :
9494 : 2 : if (etag_out != NULL)
9495 : 0 : *etag_out = NULL;
9496 : :
9497 : 2 : if (g_file_has_uri_scheme (file, "resource"))
9498 : : {
9499 : : GBytes *bytes;
9500 : : gchar *uri, *unescaped;
9501 : :
9502 : 0 : uri = g_file_get_uri (file);
9503 : 0 : unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
9504 : 0 : g_free (uri);
9505 : :
9506 : 0 : bytes = g_resources_lookup_data (unescaped, G_RESOURCE_LOOKUP_FLAGS_NONE, error);
9507 : 0 : g_free (unescaped);
9508 : :
9509 : 0 : return bytes;
9510 : : }
9511 : :
9512 : : /* contents is guaranteed to be \0 terminated */
9513 : 2 : if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
9514 : 2 : return g_bytes_new_take (g_steal_pointer (&contents), len);
9515 : :
9516 : 0 : return NULL;
9517 : : }
9518 : :
9519 : : static void
9520 : 1 : g_file_load_bytes_cb (GObject *object,
9521 : : GAsyncResult *result,
9522 : : gpointer user_data)
9523 : : {
9524 : 1 : GFile *file = G_FILE (object);
9525 : 1 : GTask *task = user_data;
9526 : 1 : GError *error = NULL;
9527 : 1 : gchar *etag = NULL;
9528 : 1 : gchar *contents = NULL;
9529 : 1 : gsize len = 0;
9530 : :
9531 : 1 : g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
9532 : 1 : g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
9533 : :
9534 : 1 : if (error != NULL)
9535 : 0 : g_task_return_error (task, g_steal_pointer (&error));
9536 : : else
9537 : 1 : g_task_return_pointer (task,
9538 : 1 : g_bytes_new_take (g_steal_pointer (&contents), len),
9539 : : (GDestroyNotify)g_bytes_unref);
9540 : :
9541 : 1 : g_object_unref (task);
9542 : 1 : }
9543 : :
9544 : : /**
9545 : : * g_file_load_bytes_async:
9546 : : * @file: a #GFile
9547 : : * @cancellable: (nullable): a #GCancellable or %NULL
9548 : : * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
9549 : : * to call when the request is satisfied
9550 : : * @user_data: the data to pass to callback function
9551 : : *
9552 : : * Asynchronously loads the contents of @file as #GBytes.
9553 : : *
9554 : : * If @file is a resource:// based URI, the resulting bytes will reference the
9555 : : * embedded resource instead of a copy. Otherwise, this is equivalent to calling
9556 : : * g_file_load_contents_async() and g_bytes_new_take().
9557 : : *
9558 : : * @callback should call g_file_load_bytes_finish() to get the result of this
9559 : : * asynchronous operation.
9560 : : *
9561 : : * See g_file_load_bytes() for more information.
9562 : : *
9563 : : * Since: 2.56
9564 : : */
9565 : : void
9566 : 1 : g_file_load_bytes_async (GFile *file,
9567 : : GCancellable *cancellable,
9568 : : GAsyncReadyCallback callback,
9569 : : gpointer user_data)
9570 : : {
9571 : 1 : GError *error = NULL;
9572 : : GBytes *bytes;
9573 : : GTask *task;
9574 : :
9575 : 2 : g_return_if_fail (G_IS_FILE (file));
9576 : 1 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
9577 : :
9578 : 1 : task = g_task_new (file, cancellable, callback, user_data);
9579 : 1 : g_task_set_source_tag (task, g_file_load_bytes_async);
9580 : :
9581 : 1 : if (!g_file_has_uri_scheme (file, "resource"))
9582 : : {
9583 : 1 : g_file_load_contents_async (file,
9584 : : cancellable,
9585 : : g_file_load_bytes_cb,
9586 : : g_steal_pointer (&task));
9587 : 1 : return;
9588 : : }
9589 : :
9590 : 0 : bytes = g_file_load_bytes (file, cancellable, NULL, &error);
9591 : :
9592 : 0 : if (bytes == NULL)
9593 : 0 : g_task_return_error (task, g_steal_pointer (&error));
9594 : : else
9595 : 0 : g_task_return_pointer (task,
9596 : : g_steal_pointer (&bytes),
9597 : : (GDestroyNotify)g_bytes_unref);
9598 : :
9599 : 0 : g_object_unref (task);
9600 : : }
9601 : :
9602 : : /**
9603 : : * g_file_load_bytes_finish:
9604 : : * @file: a #GFile
9605 : : * @result: a #GAsyncResult provided to the callback
9606 : : * @etag_out: (out) (nullable) (optional): a location to place the current
9607 : : * entity tag for the file, or %NULL if the entity tag is not needed
9608 : : * @error: a location for a #GError, or %NULL
9609 : : *
9610 : : * Completes an asynchronous request to g_file_load_bytes_async().
9611 : : *
9612 : : * For resources, @etag_out will be set to %NULL.
9613 : : *
9614 : : * The data contained in the resulting #GBytes is always zero-terminated, but
9615 : : * this is not included in the #GBytes length. The resulting #GBytes should be
9616 : : * freed with g_bytes_unref() when no longer in use.
9617 : : *
9618 : : * See g_file_load_bytes() for more information.
9619 : : *
9620 : : * Returns: (transfer full): a #GBytes or %NULL and @error is set
9621 : : *
9622 : : * Since: 2.56
9623 : : */
9624 : : GBytes *
9625 : 1 : g_file_load_bytes_finish (GFile *file,
9626 : : GAsyncResult *result,
9627 : : gchar **etag_out,
9628 : : GError **error)
9629 : : {
9630 : : GBytes *bytes;
9631 : :
9632 : 1 : g_return_val_if_fail (G_IS_FILE (file), NULL);
9633 : 1 : g_return_val_if_fail (G_IS_TASK (result), NULL);
9634 : 1 : g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
9635 : 1 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9636 : :
9637 : 1 : bytes = g_task_propagate_pointer (G_TASK (result), error);
9638 : :
9639 : 1 : if (etag_out != NULL)
9640 : 0 : *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));
9641 : :
9642 : 1 : return bytes;
9643 : : }
|