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