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