Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gfile.c
Line
Count
Source (jump to first uncovered line)
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
0
#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
G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
395
396
static void
397
g_file_default_init (GFileIface *iface)
398
0
{
399
0
  iface->enumerate_children_async = g_file_real_enumerate_children_async;
400
0
  iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
401
0
  iface->set_display_name_async = g_file_real_set_display_name_async;
402
0
  iface->set_display_name_finish = g_file_real_set_display_name_finish;
403
0
  iface->query_info_async = g_file_real_query_info_async;
404
0
  iface->query_info_finish = g_file_real_query_info_finish;
405
0
  iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
406
0
  iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
407
0
  iface->set_attributes_async = g_file_real_set_attributes_async;
408
0
  iface->set_attributes_finish = g_file_real_set_attributes_finish;
409
0
  iface->read_async = g_file_real_read_async;
410
0
  iface->read_finish = g_file_real_read_finish;
411
0
  iface->append_to_async = g_file_real_append_to_async;
412
0
  iface->append_to_finish = g_file_real_append_to_finish;
413
0
  iface->create_async = g_file_real_create_async;
414
0
  iface->create_finish = g_file_real_create_finish;
415
0
  iface->replace_async = g_file_real_replace_async;
416
0
  iface->replace_finish = g_file_real_replace_finish;
417
0
  iface->delete_file_async = g_file_real_delete_async;
418
0
  iface->delete_file_finish = g_file_real_delete_finish;
419
0
  iface->trash_async = g_file_real_trash_async;
420
0
  iface->trash_finish = g_file_real_trash_finish;
421
0
  iface->move_async = g_file_real_move_async;
422
0
  iface->move_finish = g_file_real_move_finish;
423
0
  iface->make_directory_async = g_file_real_make_directory_async;
424
0
  iface->make_directory_finish = g_file_real_make_directory_finish;
425
0
  iface->make_symbolic_link_async = g_file_real_make_symbolic_link_async;
426
0
  iface->make_symbolic_link_finish = g_file_real_make_symbolic_link_finish;
427
0
  iface->open_readwrite_async = g_file_real_open_readwrite_async;
428
0
  iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
429
0
  iface->create_readwrite_async = g_file_real_create_readwrite_async;
430
0
  iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
431
0
  iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
432
0
  iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
433
0
  iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
434
0
  iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
435
0
  iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
436
0
  iface->copy_async = g_file_real_copy_async;
437
0
  iface->copy_finish = g_file_real_copy_finish;
438
0
  iface->measure_disk_usage = g_file_real_measure_disk_usage;
439
0
  iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
440
0
  iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
441
0
}
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
g_file_is_native (GFile *file)
464
0
{
465
0
  GFileIface *iface;
466
467
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
468
469
0
  iface = G_FILE_GET_IFACE (file);
470
471
0
  return (* iface->is_native) (file);
472
0
}
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
g_file_has_uri_scheme (GFile      *file,
490
                       const char *uri_scheme)
491
0
{
492
0
  GFileIface *iface;
493
494
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
495
0
  g_return_val_if_fail (uri_scheme != NULL, FALSE);
496
497
0
  iface = G_FILE_GET_IFACE (file);
498
499
0
  return (* iface->has_uri_scheme) (file, uri_scheme);
500
0
}
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
g_file_get_uri_scheme (GFile *file)
525
0
{
526
0
  GFileIface *iface;
527
528
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
529
530
0
  iface = G_FILE_GET_IFACE (file);
531
532
0
  return (* iface->get_uri_scheme) (file);
533
0
}
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
g_file_get_basename (GFile *file)
560
0
{
561
0
  GFileIface *iface;
562
563
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
564
565
0
  iface = G_FILE_GET_IFACE (file);
566
567
0
  return (* iface->get_basename) (file);
568
0
}
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
g_file_get_path (GFile *file)
585
0
{
586
0
  GFileIface *iface;
587
588
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
589
590
0
  iface = G_FILE_GET_IFACE (file);
591
592
0
  return (* iface->get_path) (file);
593
0
}
594
595
static const char *
596
file_peek_path_generic (GFile *file)
597
0
{
598
0
  const char *path;
599
0
  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
0
  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
0
                                  NULL, (gpointer) new_path,
628
0
                                  (GDestroyNotify) g_free, NULL))
629
0
        {
630
0
          path = new_path;
631
0
          break;
632
0
        }
633
0
      else
634
0
        g_free (new_path);
635
0
    }
636
637
0
  return path;
638
0
}
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
g_file_peek_path (GFile *file)
658
0
{
659
0
  if (G_IS_LOCAL_FILE (file))
660
0
    return _g_local_file_get_filename ((GLocalFile *) file);
661
0
  return file_peek_path_generic (file);
662
0
}
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
g_file_get_uri (GFile *file)
679
0
{
680
0
  GFileIface *iface;
681
682
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
683
684
0
  iface = G_FILE_GET_IFACE (file);
685
686
0
  return (* iface->get_uri) (file);
687
0
}
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
g_file_get_parse_name (GFile *file)
714
0
{
715
0
  GFileIface *iface;
716
717
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
718
719
0
  iface = G_FILE_GET_IFACE (file);
720
721
0
  return (* iface->get_parse_name) (file);
722
0
}
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
g_file_dup (GFile *file)
744
0
{
745
0
  GFileIface *iface;
746
747
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
748
749
0
  iface = G_FILE_GET_IFACE (file);
750
751
0
  return (* iface->dup) (file);
752
0
}
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
g_file_hash (gconstpointer file)
769
0
{
770
0
  GFileIface *iface;
771
772
0
  g_return_val_if_fail (G_IS_FILE (file), 0);
773
774
0
  iface = G_FILE_GET_IFACE (file);
775
776
0
  return (* iface->hash) ((GFile *)file);
777
0
}
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
g_file_equal (GFile *file1,
796
              GFile *file2)
797
0
{
798
0
  GFileIface *iface;
799
800
0
  g_return_val_if_fail (G_IS_FILE (file1), FALSE);
801
0
  g_return_val_if_fail (G_IS_FILE (file2), FALSE);
802
803
0
  if (file1 == file2)
804
0
    return TRUE;
805
806
0
  if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
807
0
    return FALSE;
808
809
0
  iface = G_FILE_GET_IFACE (file1);
810
811
0
  return (* iface->equal) (file1, file2);
812
0
}
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
g_file_get_parent (GFile *file)
831
0
{
832
0
  GFileIface *iface;
833
834
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
835
836
0
  iface = G_FILE_GET_IFACE (file);
837
838
0
  return (* iface->get_parent) (file);
839
0
}
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
g_file_has_parent (GFile *file,
859
                   GFile *parent)
860
0
{
861
0
  GFile *actual_parent;
862
0
  gboolean result;
863
864
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
865
0
  g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
866
867
0
  actual_parent = g_file_get_parent (file);
868
869
0
  if (actual_parent != NULL)
870
0
    {
871
0
      if (parent != NULL)
872
0
        result = g_file_equal (parent, actual_parent);
873
0
      else
874
0
        result = TRUE;
875
876
0
      g_object_unref (actual_parent);
877
0
    }
878
0
  else
879
0
    result = FALSE;
880
881
0
  return result;
882
0
}
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
g_file_get_child (GFile      *file,
902
                  const char *name)
903
0
{
904
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
905
0
  g_return_val_if_fail (name != NULL, NULL);
906
0
  g_return_val_if_fail (!g_path_is_absolute (name), NULL);
907
908
0
  return g_file_resolve_relative_path (file, name);
909
0
}
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
g_file_get_child_for_display_name (GFile      *file,
932
                                   const char *display_name,
933
                                   GError **error)
934
0
{
935
0
  GFileIface *iface;
936
937
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
938
0
  g_return_val_if_fail (display_name != NULL, NULL);
939
940
0
  iface = G_FILE_GET_IFACE (file);
941
942
0
  return (* iface->get_child_for_display_name) (file, display_name, error);
943
0
}
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
g_file_has_prefix (GFile *file,
970
                   GFile *prefix)
971
0
{
972
0
  GFileIface *iface;
973
974
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
975
0
  g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
976
977
0
  if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
978
0
    return FALSE;
979
980
0
  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
0
  return (* iface->prefix_matches) (prefix, file);
986
0
}
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
g_file_get_relative_path (GFile *parent,
1004
                          GFile *descendant)
1005
0
{
1006
0
  GFileIface *iface;
1007
1008
0
  g_return_val_if_fail (G_IS_FILE (parent), NULL);
1009
0
  g_return_val_if_fail (G_IS_FILE (descendant), NULL);
1010
1011
0
  if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
1012
0
    return NULL;
1013
1014
0
  iface = G_FILE_GET_IFACE (parent);
1015
1016
0
  return (* iface->get_relative_path) (parent, descendant);
1017
0
}
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
g_file_resolve_relative_path (GFile      *file,
1035
                              const char *relative_path)
1036
0
{
1037
0
  GFileIface *iface;
1038
1039
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1040
0
  g_return_val_if_fail (relative_path != NULL, NULL);
1041
1042
0
  iface = G_FILE_GET_IFACE (file);
1043
1044
0
  return (* iface->resolve_relative_path) (file, relative_path);
1045
0
}
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
g_file_enumerate_children (GFile                *file,
1087
                           const char           *attributes,
1088
                           GFileQueryInfoFlags   flags,
1089
                           GCancellable         *cancellable,
1090
                           GError              **error)
1091
0
{
1092
0
  GFileIface *iface;
1093
1094
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1095
1096
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1097
0
    return NULL;
1098
1099
0
  iface = G_FILE_GET_IFACE (file);
1100
1101
0
  if (iface->enumerate_children == NULL)
1102
0
    {
1103
0
      g_set_error_literal (error, G_IO_ERROR,
1104
0
                           G_IO_ERROR_NOT_SUPPORTED,
1105
0
                           _("Operation not supported"));
1106
0
      return NULL;
1107
0
    }
1108
1109
0
  return (* iface->enumerate_children) (file, attributes, flags,
1110
0
                                        cancellable, error);
1111
0
}
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
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
0
{
1145
0
  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
0
                                       attributes,
1152
0
                                       flags,
1153
0
                                       io_priority,
1154
0
                                       cancellable,
1155
0
                                       callback,
1156
0
                                       user_data);
1157
0
}
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
g_file_enumerate_children_finish (GFile         *file,
1174
                                  GAsyncResult  *res,
1175
                                  GError       **error)
1176
0
{
1177
0
  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
0
}
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
g_file_query_exists (GFile        *file,
1226
                     GCancellable *cancellable)
1227
0
{
1228
0
  GFileIface *iface;
1229
0
  GFileInfo *info;
1230
1231
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
1232
1233
0
  iface = G_FILE_GET_IFACE (file);
1234
1235
0
  if (iface->query_exists)
1236
0
    return iface->query_exists (file, cancellable);
1237
1238
0
  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1239
0
                            G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1240
0
  if (info != NULL)
1241
0
    {
1242
0
      g_object_unref (info);
1243
0
      return TRUE;
1244
0
    }
1245
1246
0
  return FALSE;
1247
0
}
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
g_file_query_file_type (GFile               *file,
1269
                        GFileQueryInfoFlags  flags,
1270
                        GCancellable        *cancellable)
1271
0
{
1272
0
  GFileInfo *info;
1273
0
  GFileType file_type;
1274
1275
0
  g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1276
0
  info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1277
0
                            cancellable, NULL);
1278
0
  if (info != NULL)
1279
0
    {
1280
0
      file_type = g_file_info_get_file_type (info);
1281
0
      g_object_unref (info);
1282
0
    }
1283
0
  else
1284
0
    file_type = G_FILE_TYPE_UNKNOWN;
1285
1286
0
  return file_type;
1287
0
}
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
g_file_query_info (GFile                *file,
1345
                   const char           *attributes,
1346
                   GFileQueryInfoFlags   flags,
1347
                   GCancellable         *cancellable,
1348
                   GError              **error)
1349
0
{
1350
0
  GFileIface *iface;
1351
1352
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1353
1354
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1355
0
    return NULL;
1356
1357
0
  iface = G_FILE_GET_IFACE (file);
1358
1359
0
  if (iface->query_info == NULL)
1360
0
    {
1361
0
      g_set_error_literal (error, G_IO_ERROR,
1362
0
                           G_IO_ERROR_NOT_SUPPORTED,
1363
0
                           _("Operation not supported"));
1364
0
      return NULL;
1365
0
    }
1366
1367
0
  return (* iface->query_info) (file, attributes, flags, cancellable, error);
1368
0
}
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
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
0
{
1401
0
  GFileIface *iface;
1402
1403
0
  g_return_if_fail (G_IS_FILE (file));
1404
1405
0
  iface = G_FILE_GET_IFACE (file);
1406
0
  (* iface->query_info_async) (file,
1407
0
                               attributes,
1408
0
                               flags,
1409
0
                               io_priority,
1410
0
                               cancellable,
1411
0
                               callback,
1412
0
                               user_data);
1413
0
}
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
g_file_query_info_finish (GFile         *file,
1430
                          GAsyncResult  *res,
1431
                          GError       **error)
1432
0
{
1433
0
  GFileIface *iface;
1434
1435
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1436
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1437
1438
0
  if (g_async_result_legacy_propagate_error (res, error))
1439
0
    return NULL;
1440
1441
0
  iface = G_FILE_GET_IFACE (file);
1442
0
  return (* iface->query_info_finish) (file, res, error);
1443
0
}
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
g_file_query_filesystem_info (GFile         *file,
1484
                              const char    *attributes,
1485
                              GCancellable  *cancellable,
1486
                              GError       **error)
1487
0
{
1488
0
  GFileIface *iface;
1489
1490
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1491
1492
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1493
0
    return NULL;
1494
1495
0
  iface = G_FILE_GET_IFACE (file);
1496
1497
0
  if (iface->query_filesystem_info == NULL)
1498
0
    {
1499
0
      g_set_error_literal (error, G_IO_ERROR,
1500
0
                           G_IO_ERROR_NOT_SUPPORTED,
1501
0
                           _("Operation not supported"));
1502
0
      return NULL;
1503
0
    }
1504
1505
0
  return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1506
0
}
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
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
0
{
1539
0
  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
0
                                          attributes,
1546
0
                                          io_priority,
1547
0
                                          cancellable,
1548
0
                                          callback,
1549
0
                                          user_data);
1550
0
}
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
g_file_query_filesystem_info_finish (GFile         *file,
1567
                                     GAsyncResult  *res,
1568
                                     GError       **error)
1569
0
{
1570
0
  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
0
}
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
g_file_find_enclosing_mount (GFile         *file,
1605
                             GCancellable  *cancellable,
1606
                             GError       **error)
1607
0
{
1608
0
  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
0
    {
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
0
                           _("Containing mount does not exist"));
1625
0
      return NULL;
1626
0
    }
1627
1628
0
  return (* iface->find_enclosing_mount) (file, cancellable, error);
1629
0
}
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
g_file_find_enclosing_mount_async (GFile              *file,
1652
                                   int                   io_priority,
1653
                                   GCancellable         *cancellable,
1654
                                   GAsyncReadyCallback   callback,
1655
                                   gpointer              user_data)
1656
0
{
1657
0
  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
0
                                         io_priority,
1664
0
                                         cancellable,
1665
0
                                         callback,
1666
0
                                         user_data);
1667
0
}
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
g_file_find_enclosing_mount_finish (GFile         *file,
1683
                                    GAsyncResult  *res,
1684
                                    GError       **error)
1685
0
{
1686
0
  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
0
}
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
g_file_read (GFile         *file,
1722
             GCancellable  *cancellable,
1723
             GError       **error)
1724
0
{
1725
0
  GFileIface *iface;
1726
1727
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1728
1729
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1730
0
    return NULL;
1731
1732
0
  iface = G_FILE_GET_IFACE (file);
1733
1734
0
  if (iface->read_fn == NULL)
1735
0
    {
1736
0
      g_set_error_literal (error, G_IO_ERROR,
1737
0
                           G_IO_ERROR_NOT_SUPPORTED,
1738
0
                           _("Operation not supported"));
1739
0
      return NULL;
1740
0
    }
1741
1742
0
  return (* iface->read_fn) (file, cancellable, error);
1743
0
}
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
g_file_append_to (GFile             *file,
1776
                  GFileCreateFlags   flags,
1777
                  GCancellable      *cancellable,
1778
                  GError           **error)
1779
0
{
1780
0
  GFileIface *iface;
1781
1782
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1783
1784
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1785
0
    return NULL;
1786
1787
0
  iface = G_FILE_GET_IFACE (file);
1788
1789
0
  if (iface->append_to == NULL)
1790
0
    {
1791
0
      g_set_error_literal (error, G_IO_ERROR,
1792
0
                           G_IO_ERROR_NOT_SUPPORTED,
1793
0
                           _("Operation not supported"));
1794
0
      return NULL;
1795
0
    }
1796
1797
0
  return (* iface->append_to) (file, flags, cancellable, error);
1798
0
}
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
g_file_create (GFile             *file,
1834
               GFileCreateFlags   flags,
1835
               GCancellable      *cancellable,
1836
               GError           **error)
1837
0
{
1838
0
  GFileIface *iface;
1839
1840
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1841
1842
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1843
0
    return NULL;
1844
1845
0
  iface = G_FILE_GET_IFACE (file);
1846
1847
0
  if (iface->create == NULL)
1848
0
    {
1849
0
      g_set_error_literal (error, G_IO_ERROR,
1850
0
                           G_IO_ERROR_NOT_SUPPORTED,
1851
0
                           _("Operation not supported"));
1852
0
      return NULL;
1853
0
    }
1854
1855
0
  return (* iface->create) (file, flags, cancellable, error);
1856
0
}
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
g_file_replace (GFile             *file,
1916
                const char        *etag,
1917
                gboolean           make_backup,
1918
                GFileCreateFlags   flags,
1919
                GCancellable      *cancellable,
1920
                GError           **error)
1921
0
{
1922
0
  GFileIface *iface;
1923
1924
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1925
1926
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1927
0
    return NULL;
1928
1929
0
  iface = G_FILE_GET_IFACE (file);
1930
1931
0
  if (iface->replace == NULL)
1932
0
    {
1933
0
      g_set_error_literal (error, G_IO_ERROR,
1934
0
                           G_IO_ERROR_NOT_SUPPORTED,
1935
0
                           _("Operation not supported"));
1936
0
      return NULL;
1937
0
    }
1938
1939
  /* Handle empty tag string as NULL in consistent way. */
1940
0
  if (etag && *etag == 0)
1941
0
    etag = NULL;
1942
1943
0
  return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1944
0
}
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
g_file_open_readwrite (GFile         *file,
1976
                       GCancellable  *cancellable,
1977
                       GError       **error)
1978
0
{
1979
0
  GFileIface *iface;
1980
1981
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
1982
1983
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
1984
0
    return NULL;
1985
1986
0
  iface = G_FILE_GET_IFACE (file);
1987
1988
0
  if (iface->open_readwrite == NULL)
1989
0
    {
1990
0
      g_set_error_literal (error, G_IO_ERROR,
1991
0
                           G_IO_ERROR_NOT_SUPPORTED,
1992
0
                           _("Operation not supported"));
1993
0
      return NULL;
1994
0
    }
1995
1996
0
  return (* iface->open_readwrite) (file, cancellable, error);
1997
0
}
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
g_file_create_readwrite (GFile             *file,
2039
                         GFileCreateFlags   flags,
2040
                         GCancellable      *cancellable,
2041
                         GError           **error)
2042
0
{
2043
0
  GFileIface *iface;
2044
2045
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2046
2047
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
2048
0
    return NULL;
2049
2050
0
  iface = G_FILE_GET_IFACE (file);
2051
2052
0
  if (iface->create_readwrite == NULL)
2053
0
    {
2054
0
      g_set_error_literal (error, G_IO_ERROR,
2055
0
                           G_IO_ERROR_NOT_SUPPORTED,
2056
0
                           _("Operation not supported"));
2057
0
      return NULL;
2058
0
    }
2059
2060
0
  return (* iface->create_readwrite) (file, flags, cancellable, error);
2061
0
}
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
g_file_replace_readwrite (GFile             *file,
2092
                          const char        *etag,
2093
                          gboolean           make_backup,
2094
                          GFileCreateFlags   flags,
2095
                          GCancellable      *cancellable,
2096
                          GError           **error)
2097
0
{
2098
0
  GFileIface *iface;
2099
2100
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2101
2102
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
2103
0
    return NULL;
2104
2105
0
  iface = G_FILE_GET_IFACE (file);
2106
2107
0
  if (iface->replace_readwrite == NULL)
2108
0
    {
2109
0
      g_set_error_literal (error, G_IO_ERROR,
2110
0
                           G_IO_ERROR_NOT_SUPPORTED,
2111
0
                           _("Operation not supported"));
2112
0
      return NULL;
2113
0
    }
2114
2115
0
  return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
2116
0
}
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
g_file_read_async (GFile               *file,
2139
                   int                  io_priority,
2140
                   GCancellable        *cancellable,
2141
                   GAsyncReadyCallback  callback,
2142
                   gpointer             user_data)
2143
0
{
2144
0
  GFileIface *iface;
2145
2146
0
  g_return_if_fail (G_IS_FILE (file));
2147
2148
0
  iface = G_FILE_GET_IFACE (file);
2149
0
  (* iface->read_async) (file,
2150
0
                         io_priority,
2151
0
                         cancellable,
2152
0
                         callback,
2153
0
                         user_data);
2154
0
}
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
g_file_read_finish (GFile         *file,
2170
                    GAsyncResult  *res,
2171
                    GError       **error)
2172
0
{
2173
0
  GFileIface *iface;
2174
2175
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2176
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2177
2178
0
  if (g_async_result_legacy_propagate_error (res, error))
2179
0
    return NULL;
2180
2181
0
  iface = G_FILE_GET_IFACE (file);
2182
0
  return (* iface->read_finish) (file, res, error);
2183
0
}
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
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
0
{
2213
0
  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
0
                              flags,
2220
0
                              io_priority,
2221
0
                              cancellable,
2222
0
                              callback,
2223
0
                              user_data);
2224
0
}
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
g_file_append_to_finish (GFile         *file,
2241
                         GAsyncResult  *res,
2242
                         GError       **error)
2243
0
{
2244
0
  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
0
}
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
g_file_create_async (GFile               *file,
2279
                     GFileCreateFlags     flags,
2280
                     int                  io_priority,
2281
                     GCancellable        *cancellable,
2282
                     GAsyncReadyCallback  callback,
2283
                     gpointer             user_data)
2284
0
{
2285
0
  GFileIface *iface;
2286
2287
0
  g_return_if_fail (G_IS_FILE (file));
2288
2289
0
  iface = G_FILE_GET_IFACE (file);
2290
0
  (* iface->create_async) (file,
2291
0
                           flags,
2292
0
                           io_priority,
2293
0
                           cancellable,
2294
0
                           callback,
2295
0
                           user_data);
2296
0
}
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
g_file_create_finish (GFile         *file,
2312
                      GAsyncResult  *res,
2313
                      GError       **error)
2314
0
{
2315
0
  GFileIface *iface;
2316
2317
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2318
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2319
2320
0
  if (g_async_result_legacy_propagate_error (res, error))
2321
0
    return NULL;
2322
2323
0
  iface = G_FILE_GET_IFACE (file);
2324
0
  return (* iface->create_finish) (file, res, error);
2325
0
}
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
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
0
{
2361
0
  GFileIface *iface;
2362
2363
0
  g_return_if_fail (G_IS_FILE (file));
2364
2365
0
  iface = G_FILE_GET_IFACE (file);
2366
0
  (* iface->replace_async) (file,
2367
0
                            etag,
2368
0
                            make_backup,
2369
0
                            flags,
2370
0
                            io_priority,
2371
0
                            cancellable,
2372
0
                            callback,
2373
0
                            user_data);
2374
0
}
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
g_file_replace_finish (GFile         *file,
2390
                       GAsyncResult  *res,
2391
                       GError       **error)
2392
0
{
2393
0
  GFileIface *iface;
2394
2395
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2396
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2397
2398
0
  if (g_async_result_legacy_propagate_error (res, error))
2399
0
    return NULL;
2400
2401
0
  iface = G_FILE_GET_IFACE (file);
2402
0
  return (* iface->replace_finish) (file, res, error);
2403
0
}
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
g_file_open_readwrite_async (GFile               *file,
2428
                             int                  io_priority,
2429
                             GCancellable        *cancellable,
2430
                             GAsyncReadyCallback  callback,
2431
                             gpointer             user_data)
2432
0
{
2433
0
  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
0
                                   io_priority,
2440
0
                                   cancellable,
2441
0
                                   callback,
2442
0
                                   user_data);
2443
0
}
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
g_file_open_readwrite_finish (GFile         *file,
2461
                              GAsyncResult  *res,
2462
                              GError       **error)
2463
0
{
2464
0
  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
0
}
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
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
0
{
2507
0
  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
0
                                     flags,
2514
0
                                     io_priority,
2515
0
                                     cancellable,
2516
0
                                     callback,
2517
0
                                     user_data);
2518
0
}
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
g_file_create_readwrite_finish (GFile         *file,
2536
                                GAsyncResult  *res,
2537
                                GError       **error)
2538
0
{
2539
0
  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
0
}
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
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
0
{
2588
0
  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
0
                                      etag,
2595
0
                                      make_backup,
2596
0
                                      flags,
2597
0
                                      io_priority,
2598
0
                                      cancellable,
2599
0
                                      callback,
2600
0
                                      user_data);
2601
0
}
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
g_file_replace_readwrite_finish (GFile         *file,
2619
                                 GAsyncResult  *res,
2620
                                 GError       **error)
2621
0
{
2622
0
  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
0
}
2633
2634
static gboolean
2635
copy_symlink (GFile           *destination,
2636
              GFileCopyFlags   flags,
2637
              GCancellable    *cancellable,
2638
              const char      *target,
2639
              GError         **error)
2640
0
{
2641
0
  GError *my_error;
2642
0
  gboolean tried_delete;
2643
0
  GFileInfo *info;
2644
0
  GFileType file_type;
2645
2646
0
  tried_delete = FALSE;
2647
2648
0
 retry:
2649
0
  my_error = NULL;
2650
0
  if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2651
0
    {
2652
      /* Maybe it already existed, and we want to overwrite? */
2653
0
      if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2654
0
          my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2655
0
        {
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
0
                                    G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2661
0
                                    cancellable, &my_error);
2662
0
          if (info != NULL)
2663
0
            {
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
0
                {
2669
0
                  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2670
0
                                       _("Can’t copy over directory"));
2671
0
                  return FALSE;
2672
0
                }
2673
0
            }
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
0
        }
2681
            /* Nah, fail */
2682
0
      g_propagate_error (error, my_error);
2683
0
      return FALSE;
2684
0
    }
2685
2686
0
  return TRUE;
2687
0
}
2688
2689
static GFileInputStream *
2690
open_source_for_copy (GFile           *source,
2691
                      GFile           *destination,
2692
                      GFileCopyFlags   flags,
2693
                      GCancellable    *cancellable,
2694
                      GError         **error)
2695
0
{
2696
0
  GError *my_error;
2697
0
  GFileInputStream *ret;
2698
0
  GFileInfo *info;
2699
0
  GFileType file_type;
2700
2701
0
  my_error = NULL;
2702
0
  ret = g_file_read (source, cancellable, &my_error);
2703
0
  if (ret != NULL)
2704
0
    return ret;
2705
2706
  /* There was an error opening the source, try to set a good error for it: */
2707
0
  if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2708
0
    {
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
0
      g_error_free (my_error);
2714
0
      my_error = NULL;
2715
2716
0
      info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2717
0
                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2718
0
                                cancellable, &my_error);
2719
0
      if (info != NULL &&
2720
0
          g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2721
0
        {
2722
0
          file_type = g_file_info_get_file_type (info);
2723
0
          g_object_unref (info);
2724
2725
0
          if (flags & G_FILE_COPY_OVERWRITE)
2726
0
            {
2727
0
              if (file_type == G_FILE_TYPE_DIRECTORY)
2728
0
                {
2729
0
                  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2730
0
                                       _("Can’t copy directory over directory"));
2731
0
                  return NULL;
2732
0
                }
2733
              /* continue to would_recurse error */
2734
0
            }
2735
0
          else
2736
0
            {
2737
0
              g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2738
0
                                   _("Target file exists"));
2739
0
              return NULL;
2740
0
            }
2741
0
        }
2742
0
      else
2743
0
        {
2744
          /* Error getting info from target, return that error
2745
           * (except for NOT_FOUND, which is no error here)
2746
           */
2747
0
          g_clear_object (&info);
2748
0
          if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2749
0
            {
2750
0
              g_propagate_error (error, my_error);
2751
0
              return NULL;
2752
0
            }
2753
0
          g_clear_error (&my_error);
2754
0
        }
2755
2756
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2757
0
                           _("Can’t recursively copy directory"));
2758
0
      return NULL;
2759
0
    }
2760
2761
0
  g_propagate_error (error, my_error);
2762
0
  return NULL;
2763
0
}
2764
2765
static gboolean
2766
should_copy (GFileAttributeInfo *info,
2767
             gboolean            copy_all_attributes,
2768
             gboolean            skip_perms,
2769
             gboolean            skip_modified_time)
2770
0
{
2771
0
  if ((skip_perms && strcmp(info->name, "unix::mode") == 0) ||
2772
0
      (skip_modified_time && strncmp(info->name, "time::modified", 14) == 0))
2773
0
    return FALSE;
2774
2775
0
  if (copy_all_attributes)
2776
0
    return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2777
0
  return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2778
0
}
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
g_file_build_attribute_list_for_copy (GFile                  *file,
2805
                                      GFileCopyFlags          flags,
2806
                                      GCancellable           *cancellable,
2807
                                      GError                **error)
2808
0
{
2809
0
  char *ret = NULL;
2810
0
  GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2811
0
  GString *s = NULL;
2812
0
  gboolean first;
2813
0
  int i;
2814
0
  gboolean copy_all_attributes;
2815
0
  gboolean skip_perms;
2816
0
  gboolean skip_modified_time;
2817
2818
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
2819
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
2820
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2821
2822
0
  copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2823
0
  skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2824
0
  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
0
  attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2830
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
2831
0
    goto out;
2832
2833
0
  namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2834
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
2835
0
    goto out;
2836
2837
0
  if (attributes == NULL && namespaces == NULL)
2838
0
    goto out;
2839
2840
0
  first = TRUE;
2841
0
  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
0
  first = FALSE;
2846
0
  g_string_append (s, G_FILE_ATTRIBUTE_STANDARD_SIZE);
2847
2848
0
  if (attributes)
2849
0
    {
2850
0
      for (i = 0; i < attributes->n_infos; i++)
2851
0
        {
2852
0
          if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms, skip_modified_time))
2853
0
            {
2854
0
              if (first)
2855
0
                first = FALSE;
2856
0
              else
2857
0
                g_string_append_c (s, ',');
2858
2859
0
              g_string_append (s, attributes->infos[i].name);
2860
0
            }
2861
0
        }
2862
0
    }
2863
2864
0
  if (namespaces)
2865
0
    {
2866
0
      for (i = 0; i < namespaces->n_infos; i++)
2867
0
        {
2868
0
          if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE, FALSE))
2869
0
            {
2870
0
              if (first)
2871
0
                first = FALSE;
2872
0
              else
2873
0
                g_string_append_c (s, ',');
2874
2875
0
              g_string_append (s, namespaces->infos[i].name);
2876
0
              g_string_append (s, "::*");
2877
0
            }
2878
0
        }
2879
0
    }
2880
2881
0
  ret = g_string_free (s, FALSE);
2882
0
  s = NULL;
2883
0
 out:
2884
0
  if (s)
2885
0
    g_string_free (s, TRUE);
2886
0
  if (attributes)
2887
0
    g_file_attribute_info_list_unref (attributes);
2888
0
  if (namespaces)
2889
0
    g_file_attribute_info_list_unref (namespaces);
2890
  
2891
0
  return ret;
2892
0
}
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
g_file_copy_attributes (GFile           *source,
2917
                        GFile           *destination,
2918
                        GFileCopyFlags   flags,
2919
                        GCancellable    *cancellable,
2920
                        GError         **error)
2921
0
{
2922
0
  char *attrs_to_read;
2923
0
  gboolean res;
2924
0
  GFileInfo *info;
2925
0
  gboolean source_nofollow_symlinks;
2926
2927
0
  attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
2928
0
                                                        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
0
                            source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2939
0
                            cancellable,
2940
0
                            NULL);
2941
2942
0
  g_free (attrs_to_read);
2943
2944
0
  res = TRUE;
2945
0
  if  (info)
2946
0
    {
2947
0
      res = g_file_set_attributes_from_info (destination,
2948
0
                                             info,
2949
0
                                             G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2950
0
                                             cancellable,
2951
0
                                             error);
2952
0
      g_object_unref (info);
2953
0
    }
2954
2955
0
  return res;
2956
0
}
2957
2958
/* 256k minus malloc overhead */
2959
0
#define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2960
2961
static gboolean
2962
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
0
{
2970
0
  gssize n_read;
2971
0
  gsize n_written;
2972
0
  goffset current_size;
2973
0
  char *buffer;
2974
0
  gboolean res;
2975
0
  goffset total_size;
2976
0
  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
0
    {
2982
0
      info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2983
0
                                             G_FILE_ATTRIBUTE_STANDARD_SIZE,
2984
0
                                             cancellable, NULL);
2985
0
      if (info)
2986
0
        {
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
0
        }
2991
2992
0
      if (total_size == -1)
2993
0
        {
2994
0
          info = g_file_query_info (source,
2995
0
                                    G_FILE_ATTRIBUTE_STANDARD_SIZE,
2996
0
                                    G_FILE_QUERY_INFO_NONE,
2997
0
                                    cancellable, NULL);
2998
0
          if (info)
2999
0
            {
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
0
            }
3004
0
        }
3005
0
    }
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
0
  while (TRUE)
3014
0
    {
3015
0
      n_read = g_input_stream_read (in, buffer, STREAM_BUFFER_SIZE, cancellable, error);
3016
0
      if (n_read == -1)
3017
0
        {
3018
0
          res = FALSE;
3019
0
          break;
3020
0
        }
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
0
    }
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
0
}
3042
3043
#ifdef HAVE_COPY_FILE_RANGE
3044
static gboolean
3045
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
0
{
3053
0
  ssize_t result;
3054
3055
0
  do
3056
0
    {
3057
0
      result = copy_file_range (fd_in, off_in, fd_out, off_out, len, 0);
3058
3059
0
      if (result == -1)
3060
0
        {
3061
0
          int errsv = errno;
3062
3063
0
          if (errsv == EINTR)
3064
0
            {
3065
0
              continue;
3066
0
            }
3067
0
          else if (errsv == ENOSYS || errsv == EINVAL || errsv == EOPNOTSUPP || errsv == EXDEV)
3068
0
            {
3069
0
              g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3070
0
                                   _("Copy file range not supported"));
3071
0
            }
3072
0
          else
3073
0
            {
3074
0
              g_set_error (error, G_IO_ERROR,
3075
0
                           g_io_error_from_errno (errsv),
3076
0
                           _("Error splicing file: %s"),
3077
0
                           g_strerror (errsv));
3078
0
            }
3079
3080
0
          return FALSE;
3081
0
        }
3082
0
    } while (result == -1);
3083
3084
0
  g_assert (result >= 0);
3085
0
  *bytes_transferred = result;
3086
3087
0
  return TRUE;
3088
0
}
3089
3090
static gboolean
3091
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
0
{
3099
0
  goffset total_size, last_notified_size;
3100
0
  size_t copy_len;
3101
0
  loff_t offset_in;
3102
0
  loff_t offset_out;
3103
0
  int fd_in, fd_out;
3104
3105
0
  fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3106
0
  fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3107
3108
0
  g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3109
0
  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
0
  if (total_size == 0)
3116
0
    {
3117
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3118
0
                   _("Copy file range not supported"));
3119
0
      return FALSE;
3120
0
    }
3121
3122
0
  offset_in = offset_out = 0;
3123
0
  copy_len = total_size;
3124
0
  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
0
  while (copy_len > 0)
3131
0
    {
3132
0
      size_t n_copied;
3133
3134
0
      if (g_cancellable_set_error_if_cancelled (cancellable, error) ||
3135
0
          !do_copy_file_range (fd_in, &offset_in, fd_out, &offset_out, copy_len, &n_copied, error))
3136
0
        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
0
        {
3146
0
          progress_callback (offset_in, total_size, progress_callback_data);
3147
0
          last_notified_size = total_size;
3148
0
        }
3149
0
    }
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
0
}
3157
#endif  /* HAVE_COPY_FILE_RANGE */
3158
3159
#ifdef HAVE_SPLICE
3160
3161
static gboolean
3162
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
0
{
3170
0
  long result;
3171
3172
0
retry:
3173
0
  result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
3174
3175
0
  if (result == -1)
3176
0
    {
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
0
                             _("Splice not supported"));
3184
0
      else
3185
0
        g_set_error (error, G_IO_ERROR,
3186
0
                     g_io_error_from_errno (errsv),
3187
0
                     _("Error splicing file: %s"),
3188
0
                     g_strerror (errsv));
3189
3190
0
      return FALSE;
3191
0
    }
3192
3193
0
  *bytes_transferd = result;
3194
0
  return TRUE;
3195
0
}
3196
3197
static gboolean
3198
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
0
{
3206
0
  int buffer[2] = { -1, -1 };
3207
0
  int buffer_size;
3208
0
  gboolean res;
3209
0
  goffset total_size;
3210
0
  loff_t offset_in;
3211
0
  loff_t offset_out;
3212
0
  int fd_in, fd_out;
3213
3214
0
  fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3215
0
  fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3216
3217
0
  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
0
  buffer_size = fcntl (buffer[1], F_SETPIPE_SZ, 1024 * 1024);
3223
0
  if (buffer_size <= 0)
3224
0
    {
3225
0
      buffer_size = fcntl (buffer[1], F_GETPIPE_SZ);
3226
0
      if (buffer_size <= 0)
3227
0
        {
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
0
        }
3235
0
    }
3236
3237
0
  g_assert (buffer_size > 0);
3238
3239
0
  total_size = -1;
3240
  /* avoid performance impact of querying total size when it's not needed */
3241
0
  if (progress_callback)
3242
0
    {
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
0
    }
3246
3247
0
  if (total_size == -1)
3248
0
    total_size = 0;
3249
3250
0
  offset_in = offset_out = 0;
3251
0
  res = FALSE;
3252
0
  while (TRUE)
3253
0
    {
3254
0
      long n_read;
3255
0
      long n_written;
3256
3257
0
      if (g_cancellable_set_error_if_cancelled (cancellable, error))
3258
0
        break;
3259
3260
0
      if (!do_splice (fd_in, &offset_in, buffer[1], NULL, buffer_size, &n_read, error))
3261
0
        break;
3262
3263
0
      if (n_read == 0)
3264
0
        {
3265
0
          res = TRUE;
3266
0
          break;
3267
0
        }
3268
3269
0
      while (n_read > 0)
3270
0
        {
3271
0
          if (g_cancellable_set_error_if_cancelled (cancellable, error))
3272
0
            goto out;
3273
3274
0
          if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
3275
0
            goto out;
3276
3277
0
          n_read -= n_written;
3278
0
        }
3279
3280
0
      if (progress_callback)
3281
0
        progress_callback (offset_in, total_size, progress_callback_data);
3282
0
    }
3283
3284
  /* Make sure we send full copied size */
3285
0
  if (progress_callback)
3286
0
    progress_callback (offset_in, total_size, progress_callback_data);
3287
3288
0
  if (!g_close (buffer[0], error))
3289
0
    goto out;
3290
0
  buffer[0] = -1;
3291
0
  if (!g_close (buffer[1], error))
3292
0
    goto out;
3293
0
  buffer[1] = -1;
3294
0
 out:
3295
0
  if (buffer[0] != -1)
3296
0
    (void) g_close (buffer[0], NULL);
3297
0
  if (buffer[1] != -1)
3298
0
    (void) g_close (buffer[1], NULL);
3299
3300
0
  return res;
3301
0
}
3302
#endif
3303
3304
#ifdef __linux__
3305
static gboolean
3306
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
0
{
3315
0
  goffset total_size;
3316
0
  int fd_in, fd_out;
3317
0
  int ret, errsv;
3318
3319
0
  fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3320
0
  fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3321
3322
0
  total_size = -1;
3323
  /* avoid performance impact of querying total size when it's not needed */
3324
0
  if (progress_callback)
3325
0
    {
3326
0
      g_assert (g_file_info_has_attribute (in_info, G_FILE_ATTRIBUTE_STANDARD_SIZE));
3327
0
      total_size = g_file_info_get_size (in_info);
3328
0
    }
3329
3330
0
  if (total_size == -1)
3331
0
    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
0
  ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3340
0
  errsv = errno;
3341
3342
0
  if (ret < 0)
3343
0
    {
3344
0
      if (errsv == EXDEV)
3345
0
  g_set_error_literal (error, G_IO_ERROR,
3346
0
           G_IO_ERROR_NOT_SUPPORTED,
3347
0
           _("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
0
           G_IO_ERROR_NOT_SUPPORTED,
3351
0
           _("Copy (reflink/clone) is not supported or invalid"));
3352
0
      else
3353
  /* Most probably something odd happened; retry with fallback */
3354
0
  g_set_error_literal (error, G_IO_ERROR,
3355
0
           G_IO_ERROR_NOT_SUPPORTED,
3356
0
           _("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
0
      return FALSE;
3362
0
    }
3363
3364
  /* Make sure we send full copied size */
3365
0
  if (progress_callback)
3366
0
    progress_callback (total_size, total_size, progress_callback_data);
3367
3368
0
  return TRUE;
3369
0
}
3370
#endif
3371
3372
static gboolean
3373
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
0
{
3381
0
  gboolean ret = FALSE;
3382
0
  GFileInputStream *file_in = NULL;
3383
0
  GInputStream *in = NULL;
3384
0
  GOutputStream *out = NULL;
3385
0
  GFileInfo *info = NULL;
3386
0
  const char *target;
3387
0
  char *attrs_to_read;
3388
0
  gboolean do_set_attributes = FALSE;
3389
0
  GFileCreateFlags create_flags;
3390
0
  GError *tmp_error = NULL;
3391
3392
  /* need to know the file type */
3393
0
  info = g_file_query_info (source,
3394
0
                            G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3395
0
                            G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3396
0
                            cancellable,
3397
0
                            error);
3398
0
  if (!info)
3399
0
    goto out;
3400
3401
0
  if (!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
3402
0
    {
3403
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3404
0
                   _("Cannot retrieve attribute %s"), G_FILE_ATTRIBUTE_STANDARD_TYPE);
3405
0
      goto out;
3406
0
    }
3407
3408
  /* Maybe copy the symlink? */
3409
0
  if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3410
0
      g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3411
0
    {
3412
0
      if (!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET))
3413
0
        {
3414
0
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3415
0
                       _("Cannot retrieve attribute %s"), G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET);
3416
0
          goto out;
3417
0
        }
3418
3419
0
      target = g_file_info_get_symlink_target (info);
3420
0
      if (target)
3421
0
        {
3422
0
          if (!copy_symlink (destination, flags, cancellable, target, error))
3423
0
            goto out;
3424
3425
0
          ret = TRUE;
3426
0
          goto out;
3427
0
        }
3428
        /* ... else fall back on a regular file copy */
3429
0
    }
3430
  /* Handle "special" files (pipes, device nodes, ...)? */
3431
0
  else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3432
0
    {
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
0
                           _("Can’t copy special file"));
3436
0
      goto out;
3437
0
    }
3438
3439
  /* Everything else should just fall back on a regular copy. */
3440
3441
0
  file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3442
0
  if (!file_in)
3443
0
    goto out;
3444
0
  in = G_INPUT_STREAM (file_in);
3445
3446
0
  attrs_to_read = g_file_build_attribute_list_for_copy (destination, flags,
3447
0
                                                        cancellable, error);
3448
0
  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
0
  g_object_unref (info);
3456
3457
0
  info = g_file_input_stream_query_info (file_in, attrs_to_read,
3458
0
                                         cancellable, &tmp_error);
3459
0
  if (!info)
3460
0
    {
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
0
        {
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
0
                                    cancellable, error);
3470
0
        }
3471
0
      else
3472
0
        {
3473
0
          g_free (attrs_to_read);
3474
0
          g_propagate_error (error, tmp_error);
3475
0
          goto out;
3476
0
        }
3477
0
    }
3478
0
  g_free (attrs_to_read);
3479
0
  if (!info)
3480
0
    goto out;
3481
3482
0
  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
0
  create_flags = G_FILE_CREATE_NONE;
3502
0
  if (!(flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) &&
3503
0
      g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_UNIX_MODE) &&
3504
0
      !G_IS_LOCAL_FILE (destination))
3505
0
    create_flags |= G_FILE_CREATE_PRIVATE;
3506
0
  if (flags & G_FILE_COPY_OVERWRITE)
3507
0
    create_flags |= G_FILE_CREATE_REPLACE_DESTINATION;
3508
3509
0
  if (G_IS_LOCAL_FILE (destination))
3510
0
    {
3511
0
      if (flags & G_FILE_COPY_OVERWRITE)
3512
0
        out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3513
0
                                                                   FALSE, NULL,
3514
0
                                                                   flags & G_FILE_COPY_BACKUP,
3515
0
                                                                   create_flags,
3516
0
                                                                   (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3517
0
                                                                   cancellable, error);
3518
0
      else
3519
0
        out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3520
0
                                                                  FALSE, create_flags,
3521
0
                                                                  (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) ? NULL : info,
3522
0
                                                                  cancellable, error);
3523
0
    }
3524
0
  else if (flags & G_FILE_COPY_OVERWRITE)
3525
0
    {
3526
0
      out = (GOutputStream *)g_file_replace (destination,
3527
0
                                             NULL,
3528
0
                                             flags & G_FILE_COPY_BACKUP,
3529
0
                                             create_flags,
3530
0
                                             cancellable, error);
3531
0
    }
3532
0
  else
3533
0
    {
3534
0
      out = (GOutputStream *)g_file_create (destination, create_flags, cancellable, error);
3535
0
    }
3536
3537
0
  if (!out)
3538
0
    goto out;
3539
3540
0
#ifdef __linux__
3541
0
  if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3542
0
    {
3543
0
      GError *reflink_err = NULL;
3544
3545
0
      if (!btrfs_reflink_with_progress (in, info, out, info, cancellable,
3546
0
                                        progress_callback, progress_callback_data,
3547
0
                                        &reflink_err))
3548
0
        {
3549
0
          if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3550
0
            {
3551
0
              g_clear_error (&reflink_err);
3552
0
            }
3553
0
          else
3554
0
            {
3555
0
              g_propagate_error (error, reflink_err);
3556
0
              goto out;
3557
0
            }
3558
0
        }
3559
0
      else
3560
0
        {
3561
0
          ret = TRUE;
3562
0
          goto out;
3563
0
        }
3564
0
    }
3565
0
#endif
3566
3567
0
#ifdef HAVE_COPY_FILE_RANGE
3568
0
  if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3569
0
    {
3570
0
      GError *copy_file_range_error = NULL;
3571
3572
0
      if (copy_file_range_with_progress (in, info, out, cancellable,
3573
0
                                         progress_callback, progress_callback_data,
3574
0
                                         &copy_file_range_error))
3575
0
        {
3576
0
          ret = TRUE;
3577
0
          goto out;
3578
0
        }
3579
0
      else if (!g_error_matches (copy_file_range_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3580
0
        {
3581
0
          g_propagate_error (error, g_steal_pointer (&copy_file_range_error));
3582
0
          goto out;
3583
0
        }
3584
0
      else
3585
0
        {
3586
0
          g_clear_error (&copy_file_range_error);
3587
0
        }
3588
0
    }
3589
0
#endif  /* HAVE_COPY_FILE_RANGE */
3590
3591
0
#ifdef HAVE_SPLICE
3592
0
  if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3593
0
    {
3594
0
      GError *splice_err = NULL;
3595
3596
0
      if (!splice_stream_with_progress (in, info, out, cancellable,
3597
0
                                        progress_callback, progress_callback_data,
3598
0
                                        &splice_err))
3599
0
        {
3600
0
          if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3601
0
            {
3602
0
              g_clear_error (&splice_err);
3603
0
            }
3604
0
          else
3605
0
            {
3606
0
              g_propagate_error (error, splice_err);
3607
0
              goto out;
3608
0
            }
3609
0
        }
3610
0
      else
3611
0
        {
3612
0
          ret = TRUE;
3613
0
          goto out;
3614
0
        }
3615
0
    }
3616
3617
0
#endif
3618
3619
  /* A plain read/write loop */
3620
0
  if (!copy_stream_with_progress (in, out, source, cancellable,
3621
0
                                  progress_callback, progress_callback_data,
3622
0
                                  error))
3623
0
    goto out;
3624
3625
0
  ret = TRUE;
3626
0
 out:
3627
0
  if (in)
3628
0
    {
3629
      /* Don't care about errors in source here */
3630
0
      (void) g_input_stream_close (in, cancellable, NULL);
3631
0
      g_object_unref (in);
3632
0
    }
3633
3634
0
  if (out)
3635
0
    {
3636
      /* But write errors on close are bad! */
3637
0
      if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3638
0
        ret = FALSE;
3639
0
      g_object_unref (out);
3640
0
    }
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
0
  if (ret && do_set_attributes)
3645
0
    {
3646
0
      g_file_set_attributes_from_info (destination,
3647
0
                                       info,
3648
0
                                       G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3649
0
                                       cancellable,
3650
0
                                       NULL);
3651
0
    }
3652
3653
0
  g_clear_object (&info);
3654
3655
0
  return ret;
3656
0
}
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
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
0
{
3722
0
  GFileIface *iface;
3723
0
  GError *my_error;
3724
0
  gboolean res;
3725
3726
0
  g_return_val_if_fail (G_IS_FILE (source), FALSE);
3727
0
  g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3728
3729
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
3730
0
    return FALSE;
3731
3732
0
  iface = G_FILE_GET_IFACE (destination);
3733
0
  if (iface->copy)
3734
0
    {
3735
0
      my_error = NULL;
3736
0
      res = (* iface->copy) (source, destination,
3737
0
                             flags, cancellable,
3738
0
                             progress_callback, progress_callback_data,
3739
0
                             &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
0
        {
3746
0
          g_propagate_error (error, my_error);
3747
0
              return FALSE;
3748
0
        }
3749
0
      else
3750
0
        g_clear_error (&my_error);
3751
0
    }
3752
3753
  /* If the types are different, and the destination method failed
3754
   * also try the source method
3755
   */
3756
0
  if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3757
0
    {
3758
0
      iface = G_FILE_GET_IFACE (source);
3759
3760
0
      if (iface->copy)
3761
0
        {
3762
0
          my_error = NULL;
3763
0
          res = (* iface->copy) (source, destination,
3764
0
                                 flags, cancellable,
3765
0
                                 progress_callback, progress_callback_data,
3766
0
                                 &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
0
            {
3773
0
              g_propagate_error (error, my_error);
3774
0
              return FALSE;
3775
0
            }
3776
0
          else
3777
0
            g_clear_error (&my_error);
3778
0
        }
3779
0
    }
3780
3781
0
  return file_copy_fallback (source, destination, flags, cancellable,
3782
0
                             progress_callback, progress_callback_data,
3783
0
                             error);
3784
0
}
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
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
0
{
3824
0
  GFileIface *iface;
3825
3826
0
  g_return_if_fail (G_IS_FILE (source));
3827
0
  g_return_if_fail (G_IS_FILE (destination));
3828
3829
0
  iface = G_FILE_GET_IFACE (source);
3830
0
  (* iface->copy_async) (source,
3831
0
                         destination,
3832
0
                         flags,
3833
0
                         io_priority,
3834
0
                         cancellable,
3835
0
                         progress_callback,
3836
0
                         progress_callback_data,
3837
0
                         callback,
3838
0
                         user_data);
3839
0
}
3840
3841
typedef struct _CopyAsyncClosuresData
3842
{
3843
  GClosure *progress_callback_closure;
3844
  GClosure *ready_callback_closure;
3845
} CopyAsyncClosuresData;
3846
3847
static CopyAsyncClosuresData *
3848
copy_async_closures_data_new (GClosure *progress_callback_closure,
3849
                              GClosure *ready_callback_closure)
3850
0
{
3851
0
  CopyAsyncClosuresData *data;
3852
3853
0
  data = g_new0 (CopyAsyncClosuresData, 1);
3854
3855
0
  if (progress_callback_closure != NULL)
3856
0
    {
3857
0
      data->progress_callback_closure = g_closure_ref (progress_callback_closure);
3858
0
      g_closure_sink (progress_callback_closure);
3859
0
      if (G_CLOSURE_NEEDS_MARSHAL (progress_callback_closure))
3860
0
        g_closure_set_marshal (progress_callback_closure, g_cclosure_marshal_generic);
3861
0
    }
3862
3863
0
  data->ready_callback_closure = g_closure_ref (ready_callback_closure);
3864
0
  g_closure_sink (ready_callback_closure);
3865
0
  if (G_CLOSURE_NEEDS_MARSHAL (ready_callback_closure))
3866
0
    g_closure_set_marshal (ready_callback_closure, g_cclosure_marshal_generic);
3867
3868
0
  return data;
3869
0
}
3870
3871
static void
3872
copy_async_closures_data_free (CopyAsyncClosuresData *data)
3873
0
{
3874
0
  if (data->progress_callback_closure != NULL)
3875
0
    g_closure_unref (data->progress_callback_closure);
3876
3877
0
  g_closure_unref (data->ready_callback_closure);
3878
3879
0
  g_free (data);
3880
0
}
3881
3882
static void
3883
copy_async_invoke_progress (goffset current_num_bytes,
3884
                            goffset total_num_bytes,
3885
                            void *user_data)
3886
0
{
3887
0
  CopyAsyncClosuresData *data = (CopyAsyncClosuresData *) user_data;
3888
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
3889
3890
  /* goffset is 64-bits even on 32-bits platforms */
3891
0
  g_value_init (&params[0], G_TYPE_INT64);
3892
0
  g_value_set_int64 (&params[0], current_num_bytes);
3893
0
  g_value_init (&params[1], G_TYPE_INT64);
3894
0
  g_value_set_int64 (&params[1], total_num_bytes);
3895
3896
0
  g_closure_invoke (data->progress_callback_closure, /* result = */ NULL, 2, params, /* hint = */ NULL);
3897
3898
0
  g_value_unset (&params[0]);
3899
0
  g_value_unset (&params[1]);
3900
0
}
3901
3902
static void
3903
copy_async_invoke_ready (GObject *file,
3904
                         GAsyncResult *result,
3905
                         void *user_data)
3906
0
{
3907
0
  CopyAsyncClosuresData *data = (CopyAsyncClosuresData *) user_data;
3908
0
  GValue params[2] = { G_VALUE_INIT, G_VALUE_INIT };
3909
3910
0
  g_value_init (&params[0], G_TYPE_FILE);
3911
0
  g_value_set_object (&params[0], file);
3912
0
  g_value_init (&params[1], G_TYPE_ASYNC_RESULT);
3913
0
  g_value_set_object (&params[1], result);
3914
3915
0
  g_closure_invoke (data->ready_callback_closure, /* result = */ NULL, 2, params, /* hint = */ NULL);
3916
3917
0
  copy_async_closures_data_free (data);
3918
0
  g_value_unset (&params[0]);
3919
0
  g_value_unset (&params[1]);
3920
0
}
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
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
0
{
3948
0
  CopyAsyncClosuresData *data;
3949
3950
  /* freed in copy_async_invoke_ready */
3951
0
  data = copy_async_closures_data_new (progress_callback_closure, ready_callback_closure);
3952
3953
0
  g_file_copy_async (source, destination, flags, io_priority, cancellable,
3954
0
                     progress_callback_closure == NULL ? NULL : copy_async_invoke_progress, data,
3955
0
                     copy_async_invoke_ready, data);
3956
0
}
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
g_file_copy_finish (GFile         *file,
3970
                    GAsyncResult  *res,
3971
                    GError       **error)
3972
0
{
3973
0
  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
0
}
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
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
0
{
4043
0
  GFileIface *iface;
4044
0
  GError *my_error;
4045
0
  gboolean res;
4046
4047
0
  g_return_val_if_fail (G_IS_FILE (source), FALSE);
4048
0
  g_return_val_if_fail (G_IS_FILE (destination), FALSE);
4049
4050
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4051
0
    return FALSE;
4052
4053
0
  iface = G_FILE_GET_IFACE (destination);
4054
0
  if (iface->move)
4055
0
    {
4056
0
      my_error = NULL;
4057
0
      res = (* iface->move) (source, destination,
4058
0
                             flags, cancellable,
4059
0
                             progress_callback, progress_callback_data,
4060
0
                             &my_error);
4061
4062
0
      if (res)
4063
0
        return TRUE;
4064
4065
0
      if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
4066
0
        {
4067
0
          g_propagate_error (error, my_error);
4068
0
          return FALSE;
4069
0
        }
4070
0
      else
4071
0
        g_clear_error (&my_error);
4072
0
    }
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
0
    {
4079
0
      iface = G_FILE_GET_IFACE (source);
4080
4081
0
      if (iface->move)
4082
0
        {
4083
0
          my_error = NULL;
4084
0
          res = (* iface->move) (source, destination,
4085
0
                                 flags, cancellable,
4086
0
                                 progress_callback, progress_callback_data,
4087
0
                                 &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
0
            {
4094
0
              g_propagate_error (error, my_error);
4095
0
              return FALSE;
4096
0
            }
4097
0
          else
4098
0
            g_clear_error (&my_error);
4099
0
        }
4100
0
    }
4101
4102
0
  if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
4103
0
    {
4104
0
      g_set_error_literal (error, G_IO_ERROR,
4105
0
                           G_IO_ERROR_NOT_SUPPORTED,
4106
0
                           _("Operation not supported"));
4107
0
      return FALSE;
4108
0
    }
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
0
                    progress_callback, progress_callback_data,
4113
0
                    error))
4114
0
    return FALSE;
4115
4116
0
  return g_file_delete (source, cancellable, error);
4117
0
}
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
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
0
{
4157
0
  GFileIface *iface;
4158
4159
0
  g_return_if_fail (G_IS_FILE (source));
4160
0
  g_return_if_fail (G_IS_FILE (destination));
4161
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4162
4163
0
  iface = G_FILE_GET_IFACE (source);
4164
0
  (* iface->move_async) (source,
4165
0
                         destination,
4166
0
                         flags,
4167
0
                         io_priority,
4168
0
                         cancellable,
4169
0
                         progress_callback,
4170
0
                         progress_callback_data,
4171
0
                         callback,
4172
0
                         user_data);
4173
0
}
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
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
0
{
4201
0
  CopyAsyncClosuresData *data;
4202
4203
  /* freed in copy_async_invoke_ready */
4204
0
  data = copy_async_closures_data_new (progress_callback_closure, ready_callback_closure);
4205
4206
0
  g_file_move_async (source, destination, flags, io_priority, cancellable,
4207
0
                     progress_callback_closure == NULL ? NULL : copy_async_invoke_progress, data,
4208
0
                     copy_async_invoke_ready, data);
4209
0
}
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
g_file_move_finish (GFile         *file,
4226
                    GAsyncResult  *result,
4227
                    GError       **error)
4228
0
{
4229
0
  GFileIface *iface;
4230
4231
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4232
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4233
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4234
4235
0
  iface = G_FILE_GET_IFACE (file);
4236
0
  return (* iface->move_finish) (file, result, error);
4237
0
}
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
g_file_make_directory (GFile         *file,
4269
                       GCancellable  *cancellable,
4270
                       GError       **error)
4271
0
{
4272
0
  GFileIface *iface;
4273
4274
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4275
4276
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4277
0
    return FALSE;
4278
4279
0
  iface = G_FILE_GET_IFACE (file);
4280
4281
0
  if (iface->make_directory == NULL)
4282
0
    {
4283
0
      g_set_error_literal (error, G_IO_ERROR,
4284
0
                           G_IO_ERROR_NOT_SUPPORTED,
4285
0
                           _("Operation not supported"));
4286
0
      return FALSE;
4287
0
    }
4288
4289
0
  return (* iface->make_directory) (file, cancellable, error);
4290
0
}
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
g_file_make_directory_async (GFile               *file,
4308
                             int                  io_priority,
4309
                             GCancellable        *cancellable,
4310
                             GAsyncReadyCallback  callback,
4311
                             gpointer             user_data)
4312
0
{
4313
0
  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
0
                                   io_priority,
4320
0
                                   cancellable,
4321
0
                                   callback,
4322
0
                                   user_data);
4323
0
}
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
g_file_make_directory_finish (GFile         *file,
4339
                              GAsyncResult  *result,
4340
                              GError       **error)
4341
0
{
4342
0
  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
0
}
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
g_file_make_directory_with_parents (GFile         *file,
4379
                                    GCancellable  *cancellable,
4380
                                    GError       **error)
4381
0
{
4382
0
  GFile *work_file = NULL;
4383
0
  GList *list = NULL, *l;
4384
0
  GError *my_error = NULL;
4385
4386
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4387
4388
0
  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
0
  g_file_make_directory (file, cancellable, &my_error);
4397
0
  if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4398
0
    {
4399
0
      if (my_error)
4400
0
        g_propagate_error (error, my_error);
4401
0
      return my_error == NULL;
4402
0
    }
4403
4404
0
  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
0
  while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4414
0
    {
4415
0
      GFile *parent_file;
4416
4417
0
      parent_file = g_file_get_parent (work_file);
4418
0
      if (parent_file == NULL)
4419
0
        break;
4420
4421
0
      g_clear_error (&my_error);
4422
0
      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
0
      if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4427
0
        g_clear_error (&my_error);
4428
4429
0
      g_object_unref (work_file);
4430
0
      work_file = g_object_ref (parent_file);
4431
4432
0
      if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
4433
0
        list = g_list_prepend (list, parent_file);  /* Transfer ownership of ref */
4434
0
      else
4435
0
        g_object_unref (parent_file);
4436
0
    }
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
0
  for (l = list; my_error == NULL && l; l = l->next)
4444
0
    {
4445
0
      g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
4446
0
      if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
4447
0
        g_clear_error (&my_error);
4448
0
    }
4449
4450
0
  if (work_file)
4451
0
    g_object_unref (work_file);
4452
4453
  /* Clean up */
4454
0
  while (list != NULL)
4455
0
    {
4456
0
      g_object_unref ((GFile *) list->data);
4457
0
      list = g_list_remove (list, list->data);
4458
0
    }
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
0
  if (my_error != NULL)
4465
0
    {
4466
0
      g_propagate_error (error, my_error);
4467
0
      return FALSE;
4468
0
    }
4469
4470
0
  return g_file_make_directory (file, cancellable, error);
4471
0
}
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
g_file_make_symbolic_link (GFile         *file,
4493
                           const char    *symlink_value,
4494
                           GCancellable  *cancellable,
4495
                           GError       **error)
4496
0
{
4497
0
  GFileIface *iface;
4498
4499
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4500
0
  g_return_val_if_fail (symlink_value != NULL, FALSE);
4501
4502
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4503
0
    return FALSE;
4504
4505
0
  if (*symlink_value == '\0')
4506
0
    {
4507
0
      g_set_error_literal (error, G_IO_ERROR,
4508
0
                           G_IO_ERROR_INVALID_ARGUMENT,
4509
0
                           _("Invalid symlink value given"));
4510
0
      return FALSE;
4511
0
    }
4512
4513
0
  iface = G_FILE_GET_IFACE (file);
4514
4515
0
  if (iface->make_symbolic_link == NULL)
4516
0
    {
4517
0
      g_set_error_literal (error, G_IO_ERROR,
4518
0
                           G_IO_ERROR_NOT_SUPPORTED,
4519
0
                           _("Symbolic links not supported"));
4520
0
      return FALSE;
4521
0
    }
4522
4523
0
  return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
4524
0
}
4525
4526
static void
4527
make_symbolic_link_async_thread (GTask         *task,
4528
                                 gpointer       object,
4529
                                 gpointer       task_data,
4530
                                 GCancellable  *cancellable)
4531
0
{
4532
0
  const char *symlink_value = task_data;
4533
0
  GError *error = NULL;
4534
4535
0
  if (g_file_make_symbolic_link (G_FILE (object), symlink_value, cancellable, &error))
4536
0
    g_task_return_boolean (task, TRUE);
4537
0
  else
4538
0
    g_task_return_error (task, g_steal_pointer (&error));
4539
0
}
4540
4541
static void
4542
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
0
{
4549
0
  GTask *task;
4550
4551
0
  g_return_if_fail (G_IS_FILE (file));
4552
0
  g_return_if_fail (symlink_value != NULL);
4553
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4554
4555
0
  task = g_task_new (file, cancellable, callback, user_data);
4556
0
  g_task_set_source_tag (task, g_file_real_make_symbolic_link_async);
4557
0
  g_task_set_task_data (task, g_strdup (symlink_value), g_free);
4558
0
  g_task_set_priority (task, io_priority);
4559
4560
0
  g_task_run_in_thread (task, make_symbolic_link_async_thread);
4561
0
  g_object_unref (task);
4562
0
}
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
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
0
{
4589
0
  GFileIface *iface;
4590
4591
0
  g_return_if_fail (G_IS_FILE (file));
4592
0
  g_return_if_fail (symlink_value != NULL);
4593
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
4594
4595
0
  iface = G_FILE_GET_IFACE (file);
4596
4597
  /* Default implementation should always be provided by GFileIface */
4598
0
  g_assert (iface->make_symbolic_link_async != NULL);
4599
4600
0
  (* iface->make_symbolic_link_async) (file, symlink_value, io_priority,
4601
0
                                       cancellable, callback, user_data);
4602
0
}
4603
4604
static gboolean
4605
g_file_real_make_symbolic_link_finish (GFile         *file,
4606
                                       GAsyncResult  *result,
4607
                                       GError       **error)
4608
0
{
4609
0
  g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
4610
4611
0
  return g_task_propagate_boolean (G_TASK (result), error);
4612
0
}
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
g_file_make_symbolic_link_finish (GFile         *file,
4628
                                  GAsyncResult  *result,
4629
                                  GError       **error)
4630
0
{
4631
0
  GFileIface *iface;
4632
4633
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4634
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
4635
4636
0
  iface = G_FILE_GET_IFACE (file);
4637
  /* Default implementation should always be provided by GFileIface */
4638
0
  g_assert (iface->make_symbolic_link_finish != NULL);
4639
4640
0
  return (* iface->make_symbolic_link_finish) (file, result, error);
4641
0
}
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
g_file_delete (GFile         *file,
4676
               GCancellable  *cancellable,
4677
               GError       **error)
4678
0
{
4679
0
  GFileIface *iface;
4680
4681
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4682
4683
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4684
0
    return FALSE;
4685
4686
0
  iface = G_FILE_GET_IFACE (file);
4687
4688
0
  if (iface->delete_file == NULL)
4689
0
    {
4690
0
      g_set_error_literal (error, G_IO_ERROR,
4691
0
                           G_IO_ERROR_NOT_SUPPORTED,
4692
0
                           _("Operation not supported"));
4693
0
      return FALSE;
4694
0
    }
4695
4696
0
  return (* iface->delete_file) (file, cancellable, error);
4697
0
}
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
g_file_delete_async (GFile               *file,
4717
                     int                  io_priority,
4718
                     GCancellable        *cancellable,
4719
                     GAsyncReadyCallback  callback,
4720
                     gpointer             user_data)
4721
0
{
4722
0
  GFileIface *iface;
4723
4724
0
  g_return_if_fail (G_IS_FILE (file));
4725
4726
0
  iface = G_FILE_GET_IFACE (file);
4727
0
  (* iface->delete_file_async) (file,
4728
0
                                io_priority,
4729
0
                                cancellable,
4730
0
                                callback,
4731
0
                                user_data);
4732
0
}
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
g_file_delete_finish (GFile         *file,
4747
                      GAsyncResult  *result,
4748
                      GError       **error)
4749
0
{
4750
0
  GFileIface *iface;
4751
4752
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4753
0
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4754
4755
0
  if (g_async_result_legacy_propagate_error (result, error))
4756
0
    return FALSE;
4757
4758
0
  iface = G_FILE_GET_IFACE (file);
4759
0
  return (* iface->delete_file_finish) (file, result, error);
4760
0
}
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
g_file_trash (GFile         *file,
4787
              GCancellable  *cancellable,
4788
              GError       **error)
4789
0
{
4790
0
  GFileIface *iface;
4791
4792
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
4793
4794
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
4795
0
    return FALSE;
4796
4797
0
  iface = G_FILE_GET_IFACE (file);
4798
4799
0
  if (iface->trash == NULL)
4800
0
    {
4801
0
      g_set_error_literal (error,
4802
0
                           G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4803
0
                           _("Trash not supported"));
4804
0
      return FALSE;
4805
0
    }
4806
4807
0
  return (* iface->trash) (file, cancellable, error);
4808
0
}
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
g_file_trash_async (GFile               *file,
4826
                    int                  io_priority,
4827
                    GCancellable        *cancellable,
4828
                    GAsyncReadyCallback  callback,
4829
                    gpointer             user_data)
4830
0
{
4831
0
  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
0
                          io_priority,
4838
0
                          cancellable,
4839
0
                          callback,
4840
0
                          user_data);
4841
0
}
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
g_file_trash_finish (GFile         *file,
4857
                     GAsyncResult  *result,
4858
                     GError       **error)
4859
0
{
4860
0
  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
0
}
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
g_file_set_display_name (GFile         *file,
4899
                         const gchar   *display_name,
4900
                         GCancellable  *cancellable,
4901
                         GError       **error)
4902
0
{
4903
0
  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
0
    {
4910
0
      g_set_error (error,
4911
0
                   G_IO_ERROR,
4912
0
                   G_IO_ERROR_INVALID_ARGUMENT,
4913
0
                   _("File names cannot contain “%c”"), G_DIR_SEPARATOR);
4914
0
      return NULL;
4915
0
    }
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
0
}
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
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
0
{
4953
0
  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
0
                                     display_name,
4961
0
                                     io_priority,
4962
0
                                     cancellable,
4963
0
                                     callback,
4964
0
                                     user_data);
4965
0
}
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
g_file_set_display_name_finish (GFile         *file,
4981
                                GAsyncResult  *res,
4982
                                GError       **error)
4983
0
{
4984
0
  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
0
}
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
g_file_query_settable_attributes (GFile         *file,
5020
                                  GCancellable  *cancellable,
5021
                                  GError       **error)
5022
0
{
5023
0
  GFileIface *iface;
5024
0
  GError *my_error;
5025
0
  GFileAttributeInfoList *list;
5026
5027
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
5028
5029
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5030
0
    return NULL;
5031
5032
0
  iface = G_FILE_GET_IFACE (file);
5033
5034
0
  if (iface->query_settable_attributes == NULL)
5035
0
    return g_file_attribute_info_list_new ();
5036
5037
0
  my_error = NULL;
5038
0
  list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
5039
5040
0
  if (list == NULL)
5041
0
    {
5042
0
      if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
5043
0
        {
5044
0
          list = g_file_attribute_info_list_new ();
5045
0
          g_error_free (my_error);
5046
0
        }
5047
0
      else
5048
0
        g_propagate_error (error, my_error);
5049
0
    }
5050
5051
0
  return list;
5052
0
}
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
g_file_query_writable_namespaces (GFile         *file,
5075
                                  GCancellable  *cancellable,
5076
                                  GError       **error)
5077
0
{
5078
0
  GFileIface *iface;
5079
0
  GError *my_error;
5080
0
  GFileAttributeInfoList *list;
5081
5082
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
5083
5084
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5085
0
    return NULL;
5086
5087
0
  iface = G_FILE_GET_IFACE (file);
5088
5089
0
  if (iface->query_writable_namespaces == NULL)
5090
0
    return g_file_attribute_info_list_new ();
5091
5092
0
  my_error = NULL;
5093
0
  list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
5094
5095
0
  if (list == NULL)
5096
0
    {
5097
0
      g_warn_if_reached();
5098
0
      list = g_file_attribute_info_list_new ();
5099
0
    }
5100
5101
0
  if (my_error != NULL)
5102
0
    {
5103
0
      if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
5104
0
        {
5105
0
          g_error_free (my_error);
5106
0
        }
5107
0
      else
5108
0
        g_propagate_error (error, my_error);
5109
0
    }
5110
5111
0
  return list;
5112
0
}
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
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
0
{
5146
0
  GFileIface *iface;
5147
5148
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
5149
0
  g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
5150
5151
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5152
0
    return FALSE;
5153
5154
0
  iface = G_FILE_GET_IFACE (file);
5155
5156
0
  if (iface->set_attribute == NULL)
5157
0
    {
5158
0
      g_set_error_literal (error, G_IO_ERROR,
5159
0
                           G_IO_ERROR_NOT_SUPPORTED,
5160
0
                           _("Operation not supported"));
5161
0
      return FALSE;
5162
0
    }
5163
5164
0
  return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
5165
0
}
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
g_file_set_attributes_from_info (GFile                *file,
5193
                                 GFileInfo            *info,
5194
                                 GFileQueryInfoFlags   flags,
5195
                                 GCancellable         *cancellable,
5196
                                 GError              **error)
5197
0
{
5198
0
  GFileIface *iface;
5199
5200
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
5201
0
  g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
5202
5203
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
5204
0
    return FALSE;
5205
5206
0
  g_file_info_clear_status (info);
5207
5208
0
  iface = G_FILE_GET_IFACE (file);
5209
5210
0
  return (* iface->set_attributes_from_info) (file,
5211
0
                                              info,
5212
0
                                              flags,
5213
0
                                              cancellable,
5214
0
                                              error);
5215
0
}
5216
5217
static gboolean
5218
g_file_real_set_attributes_from_info (GFile                *file,
5219
                                      GFileInfo            *info,
5220
                                      GFileQueryInfoFlags   flags,
5221
                                      GCancellable         *cancellable,
5222
                                      GError              **error)
5223
0
{
5224
0
  char **attributes;
5225
0
  int i;
5226
0
  gboolean res;
5227
0
  GFileAttributeValue *value;
5228
5229
0
  res = TRUE;
5230
5231
0
  attributes = g_file_info_list_attributes (info, NULL);
5232
5233
0
  for (i = 0; attributes[i] != NULL; i++)
5234
0
    {
5235
0
      value = _g_file_info_get_attribute_value (info, attributes[i]);
5236
5237
0
      if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
5238
0
        continue;
5239
5240
0
      if (!g_file_set_attribute (file, attributes[i],
5241
0
                                 value->type, _g_file_attribute_value_peek_as_pointer (value),
5242
0
                                 flags, cancellable, error))
5243
0
        {
5244
0
          value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
5245
0
          res = FALSE;
5246
          /* Don't set error multiple times */
5247
0
          error = NULL;
5248
0
        }
5249
0
      else
5250
0
        value->status = G_FILE_ATTRIBUTE_STATUS_SET;
5251
0
    }
5252
5253
0
  g_strfreev (attributes);
5254
5255
0
  return res;
5256
0
}
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
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
0
{
5288
0
  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
0
                                   info,
5296
0
                                   flags,
5297
0
                                   io_priority,
5298
0
                                   cancellable,
5299
0
                                   callback,
5300
0
                                   user_data);
5301
0
}
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
g_file_set_attributes_finish (GFile         *file,
5316
                              GAsyncResult  *result,
5317
                              GFileInfo    **info,
5318
                              GError       **error)
5319
0
{
5320
0
  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
0
}
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
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
0
{
5359
0
  return g_file_set_attribute (file, attribute,
5360
0
                               G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
5361
0
                               flags, cancellable, error);
5362
0
}
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
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
0
{
5393
0
  return g_file_set_attribute (file, attribute,
5394
0
                               G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
5395
0
                               flags, cancellable, error);
5396
0
}
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
g_file_set_attribute_uint32 (GFile                *file,
5420
                             const gchar          *attribute,
5421
                             guint32               value,
5422
                             GFileQueryInfoFlags   flags,
5423
                             GCancellable         *cancellable,
5424
                             GError              **error)
5425
0
{
5426
0
  return g_file_set_attribute (file, attribute,
5427
0
                               G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
5428
0
                               flags, cancellable, error);
5429
0
}
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
g_file_set_attribute_int32 (GFile                *file,
5453
                            const gchar          *attribute,
5454
                            gint32                value,
5455
                            GFileQueryInfoFlags   flags,
5456
                            GCancellable         *cancellable,
5457
                            GError              **error)
5458
0
{
5459
0
  return g_file_set_attribute (file, attribute,
5460
0
                               G_FILE_ATTRIBUTE_TYPE_INT32, &value,
5461
0
                               flags, cancellable, error);
5462
0
}
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
g_file_set_attribute_uint64 (GFile                *file,
5486
                             const gchar          *attribute,
5487
                             guint64               value,
5488
                             GFileQueryInfoFlags   flags,
5489
                             GCancellable         *cancellable,
5490
                             GError              **error)
5491
0
 {
5492
0
  return g_file_set_attribute (file, attribute,
5493
0
                               G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
5494
0
                               flags, cancellable, error);
5495
0
}
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
g_file_set_attribute_int64 (GFile                *file,
5518
                            const gchar          *attribute,
5519
                            gint64                value,
5520
                            GFileQueryInfoFlags   flags,
5521
                            GCancellable         *cancellable,
5522
                            GError              **error)
5523
0
{
5524
0
  return g_file_set_attribute (file, attribute,
5525
0
                               G_FILE_ATTRIBUTE_TYPE_INT64, &value,
5526
0
                               flags, cancellable, error);
5527
0
}
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
g_file_mount_mountable (GFile               *file,
5555
                        GMountMountFlags     flags,
5556
                        GMountOperation     *mount_operation,
5557
                        GCancellable        *cancellable,
5558
                        GAsyncReadyCallback  callback,
5559
                        gpointer             user_data)
5560
0
{
5561
0
  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
0
    {
5569
0
      g_task_report_new_error (file, callback, user_data,
5570
0
                               g_file_mount_mountable,
5571
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5572
0
                               _("Operation not supported"));
5573
0
      return;
5574
0
    }
5575
5576
0
  (* iface->mount_mountable) (file,
5577
0
                              flags,
5578
0
                              mount_operation,
5579
0
                              cancellable,
5580
0
                              callback,
5581
0
                              user_data);
5582
0
}
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
g_file_mount_mountable_finish (GFile         *file,
5600
                               GAsyncResult  *result,
5601
                               GError       **error)
5602
0
{
5603
0
  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
0
}
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
g_file_unmount_mountable (GFile               *file,
5641
                          GMountUnmountFlags   flags,
5642
                          GCancellable        *cancellable,
5643
                          GAsyncReadyCallback  callback,
5644
                          gpointer             user_data)
5645
0
{
5646
0
  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
0
    {
5654
0
      g_task_report_new_error (file, callback, user_data,
5655
0
                               g_file_unmount_mountable_with_operation,
5656
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5657
0
                               _("Operation not supported"));
5658
0
      return;
5659
0
    }
5660
5661
0
  (* iface->unmount_mountable) (file,
5662
0
                                flags,
5663
0
                                cancellable,
5664
0
                                callback,
5665
0
                                user_data);
5666
0
}
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
g_file_unmount_mountable_finish (GFile         *file,
5687
                                 GAsyncResult  *result,
5688
                                 GError       **error)
5689
0
{
5690
0
  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
0
}
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
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
0
{
5736
0
  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
0
    {
5744
0
      g_task_report_new_error (file, callback, user_data,
5745
0
                               g_file_unmount_mountable_with_operation,
5746
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5747
0
                               _("Operation not supported"));
5748
0
      return;
5749
0
    }
5750
5751
0
  if (iface->unmount_mountable_with_operation != NULL)
5752
0
    (* iface->unmount_mountable_with_operation) (file,
5753
0
                                                 flags,
5754
0
                                                 mount_operation,
5755
0
                                                 cancellable,
5756
0
                                                 callback,
5757
0
                                                 user_data);
5758
0
  else
5759
0
    (* iface->unmount_mountable) (file,
5760
0
                                  flags,
5761
0
                                  cancellable,
5762
0
                                  callback,
5763
0
                                  user_data);
5764
0
}
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
g_file_unmount_mountable_with_operation_finish (GFile         *file,
5785
                                                GAsyncResult  *result,
5786
                                                GError       **error)
5787
0
{
5788
0
  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
0
  else
5802
0
    return (* iface->unmount_mountable_finish) (file, result, error);
5803
0
}
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
g_file_eject_mountable (GFile               *file,
5828
                        GMountUnmountFlags   flags,
5829
                        GCancellable        *cancellable,
5830
                        GAsyncReadyCallback  callback,
5831
                        gpointer             user_data)
5832
0
{
5833
0
  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
0
    {
5841
0
      g_task_report_new_error (file, callback, user_data,
5842
0
                               g_file_eject_mountable_with_operation,
5843
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5844
0
                               _("Operation not supported"));
5845
0
      return;
5846
0
    }
5847
5848
0
  (* iface->eject_mountable) (file,
5849
0
                              flags,
5850
0
                              cancellable,
5851
0
                              callback,
5852
0
                              user_data);
5853
0
}
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
g_file_eject_mountable_finish (GFile         *file,
5872
                               GAsyncResult  *result,
5873
                               GError       **error)
5874
0
{
5875
0
  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
0
}
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
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
0
{
5920
0
  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
0
    {
5928
0
      g_task_report_new_error (file, callback, user_data,
5929
0
                               g_file_eject_mountable_with_operation,
5930
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5931
0
                               _("Operation not supported"));
5932
0
      return;
5933
0
    }
5934
5935
0
  if (iface->eject_mountable_with_operation != NULL)
5936
0
    (* iface->eject_mountable_with_operation) (file,
5937
0
                                               flags,
5938
0
                                               mount_operation,
5939
0
                                               cancellable,
5940
0
                                               callback,
5941
0
                                               user_data);
5942
0
  else
5943
0
    (* iface->eject_mountable) (file,
5944
0
                                flags,
5945
0
                                cancellable,
5946
0
                                callback,
5947
0
                                user_data);
5948
0
}
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
g_file_eject_mountable_with_operation_finish (GFile         *file,
5966
                                              GAsyncResult  *result,
5967
                                              GError       **error)
5968
0
{
5969
0
  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
0
  else
5983
0
    return (* iface->eject_mountable_finish) (file, result, error);
5984
0
}
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
g_file_monitor_directory (GFile              *file,
6012
                          GFileMonitorFlags   flags,
6013
                          GCancellable       *cancellable,
6014
                          GError            **error)
6015
0
{
6016
0
  GFileIface *iface;
6017
6018
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
6019
0
  g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
6020
6021
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
6022
0
    return NULL;
6023
6024
0
  iface = G_FILE_GET_IFACE (file);
6025
6026
0
  if (iface->monitor_dir == NULL)
6027
0
    {
6028
0
      g_set_error_literal (error, G_IO_ERROR,
6029
0
                           G_IO_ERROR_NOT_SUPPORTED,
6030
0
                           _("Operation not supported"));
6031
0
      return NULL;
6032
0
    }
6033
6034
0
  return (* iface->monitor_dir) (file, flags, cancellable, error);
6035
0
}
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
g_file_monitor_file (GFile              *file,
6066
                     GFileMonitorFlags   flags,
6067
                     GCancellable       *cancellable,
6068
                     GError            **error)
6069
0
{
6070
0
  GFileIface *iface;
6071
0
  GFileMonitor *monitor;
6072
6073
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
6074
6075
0
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
6076
0
    return NULL;
6077
6078
0
  iface = G_FILE_GET_IFACE (file);
6079
6080
0
  monitor = NULL;
6081
6082
0
  if (iface->monitor_file)
6083
0
    monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
6084
6085
  /* Fallback to polling */
6086
0
  if (monitor == NULL)
6087
0
    monitor = _g_poll_file_monitor_new (file);
6088
6089
0
  return monitor;
6090
0
}
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
g_file_monitor (GFile              *file,
6115
                GFileMonitorFlags   flags,
6116
                GCancellable       *cancellable,
6117
                GError            **error)
6118
0
{
6119
0
  if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
6120
0
    return g_file_monitor_directory (file,
6121
0
                                     flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
6122
0
                                     cancellable, error);
6123
0
  else
6124
0
    return g_file_monitor_file (file, flags, cancellable, error);
6125
0
}
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
query_info_data_free (QueryInfoAsyncData *data)
6138
0
{
6139
0
  g_free (data->attributes);
6140
0
  g_free (data);
6141
0
}
6142
6143
static void
6144
query_info_async_thread (GTask         *task,
6145
                         gpointer       object,
6146
                         gpointer       task_data,
6147
                         GCancellable  *cancellable)
6148
0
{
6149
0
  QueryInfoAsyncData *data = task_data;
6150
0
  GFileInfo *info;
6151
0
  GError *error = NULL;
6152
6153
0
  info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
6154
0
  if (info)
6155
0
    g_task_return_pointer (task, info, g_object_unref);
6156
0
  else
6157
0
    g_task_return_error (task, error);
6158
0
}
6159
6160
static void
6161
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
0
{
6169
0
  GTask *task;
6170
0
  QueryInfoAsyncData *data;
6171
6172
0
  data = g_new0 (QueryInfoAsyncData, 1);
6173
0
  data->attributes = g_strdup (attributes);
6174
0
  data->flags = flags;
6175
6176
0
  task = g_task_new (file, cancellable, callback, user_data);
6177
0
  g_task_set_source_tag (task, g_file_real_query_info_async);
6178
0
  g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
6179
0
  g_task_set_priority (task, io_priority);
6180
0
  g_task_run_in_thread (task, query_info_async_thread);
6181
0
  g_object_unref (task);
6182
0
}
6183
6184
static GFileInfo *
6185
g_file_real_query_info_finish (GFile         *file,
6186
                               GAsyncResult  *res,
6187
                               GError       **error)
6188
0
{
6189
0
  g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6190
6191
0
  return g_task_propagate_pointer (G_TASK (res), error);
6192
0
}
6193
6194
static void
6195
query_filesystem_info_async_thread (GTask         *task,
6196
                                    gpointer       object,
6197
                                    gpointer       task_data,
6198
                                    GCancellable  *cancellable)
6199
0
{
6200
0
  const char *attributes = task_data;
6201
0
  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
0
  else
6208
0
    g_task_return_error (task, error);
6209
0
}
6210
6211
static void
6212
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
0
{
6219
0
  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
g_file_real_query_filesystem_info_finish (GFile         *file,
6231
                                          GAsyncResult  *res,
6232
                                          GError       **error)
6233
0
{
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
0
}
6238
6239
static void
6240
enumerate_children_async_thread (GTask         *task,
6241
                                 gpointer       object,
6242
                                 gpointer       task_data,
6243
                                 GCancellable  *cancellable)
6244
0
{
6245
0
  QueryInfoAsyncData *data = task_data;
6246
0
  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
0
  else
6253
0
    g_task_return_pointer (task, enumerator, g_object_unref);
6254
0
}
6255
6256
static void
6257
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
0
{
6265
0
  GTask *task;
6266
0
  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
g_file_real_enumerate_children_finish (GFile         *file,
6282
                                       GAsyncResult  *res,
6283
                                       GError       **error)
6284
0
{
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
0
}
6289
6290
static void
6291
open_read_async_thread (GTask         *task,
6292
                        gpointer       object,
6293
                        gpointer       task_data,
6294
                        GCancellable  *cancellable)
6295
0
{
6296
0
  GFileInputStream *stream;
6297
0
  GError *error = NULL;
6298
6299
0
  stream = g_file_read (G_FILE (object), cancellable, &error);
6300
0
  if (stream)
6301
0
    g_task_return_pointer (task, stream, g_object_unref);
6302
0
  else
6303
0
    g_task_return_error (task, error);
6304
0
}
6305
6306
static void
6307
g_file_real_read_async (GFile               *file,
6308
                        int                  io_priority,
6309
                        GCancellable        *cancellable,
6310
                        GAsyncReadyCallback  callback,
6311
                        gpointer             user_data)
6312
0
{
6313
0
  GTask *task;
6314
6315
0
  task = g_task_new (file, cancellable, callback, user_data);
6316
0
  g_task_set_source_tag (task, g_file_real_read_async);
6317
0
  g_task_set_priority (task, io_priority);
6318
0
  g_task_run_in_thread (task, open_read_async_thread);
6319
0
  g_object_unref (task);
6320
0
}
6321
6322
static GFileInputStream *
6323
g_file_real_read_finish (GFile         *file,
6324
                         GAsyncResult  *res,
6325
                         GError       **error)
6326
0
{
6327
0
  g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6328
6329
0
  return g_task_propagate_pointer (G_TASK (res), error);
6330
0
}
6331
6332
static void
6333
append_to_async_thread (GTask         *task,
6334
                        gpointer       source_object,
6335
                        gpointer       task_data,
6336
                        GCancellable  *cancellable)
6337
0
{
6338
0
  GFileCreateFlags *data = task_data;
6339
0
  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
0
  else
6346
0
    g_task_return_error (task, error);
6347
0
}
6348
6349
static void
6350
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
0
{
6357
0
  GFileCreateFlags *data;
6358
0
  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
g_file_real_append_to_finish (GFile         *file,
6374
                              GAsyncResult  *res,
6375
                              GError       **error)
6376
0
{
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
0
}
6381
6382
static void
6383
create_async_thread (GTask         *task,
6384
                     gpointer       source_object,
6385
                     gpointer       task_data,
6386
                     GCancellable  *cancellable)
6387
0
{
6388
0
  GFileCreateFlags *data = task_data;
6389
0
  GFileOutputStream *stream;
6390
0
  GError *error = NULL;
6391
6392
0
  stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
6393
0
  if (stream)
6394
0
    g_task_return_pointer (task, stream, g_object_unref);
6395
0
  else
6396
0
    g_task_return_error (task, error);
6397
0
}
6398
6399
static void
6400
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
0
{
6407
0
  GFileCreateFlags *data;
6408
0
  GTask *task;
6409
6410
0
  data = g_new0 (GFileCreateFlags, 1);
6411
0
  *data = flags;
6412
6413
0
  task = g_task_new (file, cancellable, callback, user_data);
6414
0
  g_task_set_source_tag (task, g_file_real_create_async);
6415
0
  g_task_set_task_data (task, data, g_free);
6416
0
  g_task_set_priority (task, io_priority);
6417
6418
0
  g_task_run_in_thread (task, create_async_thread);
6419
0
  g_object_unref (task);
6420
0
}
6421
6422
static GFileOutputStream *
6423
g_file_real_create_finish (GFile         *file,
6424
                           GAsyncResult  *res,
6425
                           GError       **error)
6426
0
{
6427
0
  g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6428
6429
0
  return g_task_propagate_pointer (G_TASK (res), error);
6430
0
}
6431
6432
typedef struct {
6433
  GFileOutputStream *stream;
6434
  char *etag;
6435
  gboolean make_backup;
6436
  GFileCreateFlags flags;
6437
} ReplaceAsyncData;
6438
6439
static void
6440
replace_async_data_free (ReplaceAsyncData *data)
6441
0
{
6442
0
  if (data->stream)
6443
0
    g_object_unref (data->stream);
6444
0
  g_free (data->etag);
6445
0
  g_free (data);
6446
0
}
6447
6448
static void
6449
replace_async_thread (GTask         *task,
6450
                      gpointer       source_object,
6451
                      gpointer       task_data,
6452
                      GCancellable  *cancellable)
6453
0
{
6454
0
  GFileOutputStream *stream;
6455
0
  ReplaceAsyncData *data = task_data;
6456
0
  GError *error = NULL;
6457
6458
0
  stream = g_file_replace (G_FILE (source_object),
6459
0
                           data->etag,
6460
0
                           data->make_backup,
6461
0
                           data->flags,
6462
0
                           cancellable,
6463
0
                           &error);
6464
6465
0
  if (stream)
6466
0
    g_task_return_pointer (task, stream, g_object_unref);
6467
0
  else
6468
0
    g_task_return_error (task, error);
6469
0
}
6470
6471
static void
6472
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
0
{
6481
0
  GTask *task;
6482
0
  ReplaceAsyncData *data;
6483
6484
0
  data = g_new0 (ReplaceAsyncData, 1);
6485
0
  data->etag = g_strdup (etag);
6486
0
  data->make_backup = make_backup;
6487
0
  data->flags = flags;
6488
6489
0
  task = g_task_new (file, cancellable, callback, user_data);
6490
0
  g_task_set_source_tag (task, g_file_real_replace_async);
6491
0
  g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
6492
0
  g_task_set_priority (task, io_priority);
6493
6494
0
  g_task_run_in_thread (task, replace_async_thread);
6495
0
  g_object_unref (task);
6496
0
}
6497
6498
static GFileOutputStream *
6499
g_file_real_replace_finish (GFile         *file,
6500
                            GAsyncResult  *res,
6501
                            GError       **error)
6502
0
{
6503
0
  g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6504
6505
0
  return g_task_propagate_pointer (G_TASK (res), error);
6506
0
}
6507
6508
static void
6509
delete_async_thread (GTask        *task,
6510
                     gpointer      object,
6511
                     gpointer      task_data,
6512
                     GCancellable *cancellable)
6513
0
{
6514
0
  GError *error = NULL;
6515
6516
0
  if (g_file_delete (G_FILE (object), cancellable, &error))
6517
0
    g_task_return_boolean (task, TRUE);
6518
0
  else
6519
0
    g_task_return_error (task, error);
6520
0
}
6521
6522
static void
6523
g_file_real_delete_async (GFile               *file,
6524
                          int                  io_priority,
6525
                          GCancellable        *cancellable,
6526
                          GAsyncReadyCallback  callback,
6527
                          gpointer             user_data)
6528
0
{
6529
0
  GTask *task;
6530
6531
0
  task = g_task_new (file, cancellable, callback, user_data);
6532
0
  g_task_set_source_tag (task, g_file_real_delete_async);
6533
0
  g_task_set_priority (task, io_priority);
6534
0
  g_task_run_in_thread (task, delete_async_thread);
6535
0
  g_object_unref (task);
6536
0
}
6537
6538
static gboolean
6539
g_file_real_delete_finish (GFile         *file,
6540
                           GAsyncResult  *res,
6541
                           GError       **error)
6542
0
{
6543
0
  g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6544
6545
0
  return g_task_propagate_boolean (G_TASK (res), error);
6546
0
}
6547
6548
static void
6549
trash_async_thread (GTask        *task,
6550
                    gpointer      object,
6551
                    gpointer      task_data,
6552
                    GCancellable *cancellable)
6553
0
{
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
0
  else
6559
0
    g_task_return_error (task, error);
6560
0
}
6561
6562
static void
6563
g_file_real_trash_async (GFile               *file,
6564
                         int                  io_priority,
6565
                         GCancellable        *cancellable,
6566
                         GAsyncReadyCallback  callback,
6567
                         gpointer             user_data)
6568
0
{
6569
0
  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
g_file_real_trash_finish (GFile         *file,
6580
                          GAsyncResult  *res,
6581
                          GError       **error)
6582
0
{
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
0
}
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
move_async_data_free (MoveAsyncData *data)
6599
0
{
6600
0
  g_object_unref (data->source);
6601
0
  g_object_unref (data->destination);
6602
0
  g_slice_free (MoveAsyncData, data);
6603
0
}
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
move_async_progress_in_main (gpointer user_data)
6613
0
{
6614
0
  MoveProgressData *progress = user_data;
6615
0
  MoveAsyncData *data = progress->data;
6616
6617
0
  data->progress_cb (progress->current_num_bytes,
6618
0
                     progress->total_num_bytes,
6619
0
                     data->progress_cb_data);
6620
6621
0
  return G_SOURCE_REMOVE;
6622
0
}
6623
6624
static void
6625
move_async_progress_callback (goffset  current_num_bytes,
6626
                              goffset  total_num_bytes,
6627
                              gpointer user_data)
6628
0
{
6629
0
  GTask *task = user_data;
6630
0
  MoveAsyncData *data = g_task_get_task_data (task);
6631
0
  MoveProgressData *progress;
6632
6633
0
  progress = g_new0 (MoveProgressData, 1);
6634
0
  progress->data = data;
6635
0
  progress->current_num_bytes = current_num_bytes;
6636
0
  progress->total_num_bytes = total_num_bytes;
6637
6638
0
  g_main_context_invoke_full (g_task_get_context (task),
6639
0
                              g_task_get_priority (task),
6640
0
                              move_async_progress_in_main,
6641
0
                              g_steal_pointer (&progress),
6642
0
                              g_free);
6643
0
}
6644
6645
static void
6646
move_async_thread (GTask        *task,
6647
                   gpointer      source,
6648
                   gpointer      task_data,
6649
                   GCancellable *cancellable)
6650
0
{
6651
0
  MoveAsyncData *data = task_data;
6652
0
  gboolean result;
6653
0
  GError *error = NULL;
6654
6655
0
  result = g_file_move (data->source,
6656
0
                        data->destination,
6657
0
                        data->flags,
6658
0
                        cancellable,
6659
0
                        (data->progress_cb != NULL) ? move_async_progress_callback : NULL,
6660
0
                        task,
6661
0
                        &error);
6662
0
  if (result)
6663
0
    g_task_return_boolean (task, TRUE);
6664
0
  else
6665
0
    g_task_return_error (task, g_steal_pointer (&error));
6666
0
}
6667
6668
static void
6669
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
0
{
6679
0
  GTask *task;
6680
0
  MoveAsyncData *data;
6681
6682
0
  data = g_slice_new0 (MoveAsyncData);
6683
0
  data->source = g_object_ref (source);
6684
0
  data->destination = g_object_ref (destination);
6685
0
  data->flags = flags;
6686
0
  data->progress_cb = progress_callback;
6687
0
  data->progress_cb_data = progress_callback_data;
6688
6689
0
  task = g_task_new (source, cancellable, callback, user_data);
6690
0
  g_task_set_source_tag (task, g_file_real_move_async);
6691
0
  g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) move_async_data_free);
6692
0
  g_task_set_priority (task, io_priority);
6693
0
  g_task_run_in_thread (task, move_async_thread);
6694
0
  g_object_unref (task);
6695
0
}
6696
6697
static gboolean
6698
g_file_real_move_finish (GFile        *file,
6699
                         GAsyncResult *result,
6700
                         GError      **error)
6701
0
{
6702
0
  g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
6703
6704
0
  return g_task_propagate_boolean (G_TASK (result), error);
6705
0
}
6706
6707
static void
6708
make_directory_async_thread (GTask        *task,
6709
                             gpointer      object,
6710
                             gpointer      task_data,
6711
                             GCancellable *cancellable)
6712
0
{
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
0
  else
6718
0
    g_task_return_error (task, error);
6719
0
}
6720
6721
static void
6722
g_file_real_make_directory_async (GFile               *file,
6723
                                  int                  io_priority,
6724
                                  GCancellable        *cancellable,
6725
                                  GAsyncReadyCallback  callback,
6726
                                  gpointer             user_data)
6727
0
{
6728
0
  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
g_file_real_make_directory_finish (GFile         *file,
6739
                                   GAsyncResult  *res,
6740
                                   GError       **error)
6741
0
{
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
0
}
6746
6747
static void
6748
open_readwrite_async_thread (GTask        *task,
6749
                             gpointer      object,
6750
                             gpointer      task_data,
6751
                             GCancellable *cancellable)
6752
0
{
6753
0
  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
0
  else
6761
0
    g_task_return_pointer (task, stream, g_object_unref);
6762
0
}
6763
6764
static void
6765
g_file_real_open_readwrite_async (GFile               *file,
6766
                                  int                  io_priority,
6767
                                  GCancellable        *cancellable,
6768
                                  GAsyncReadyCallback  callback,
6769
                                  gpointer             user_data)
6770
0
{
6771
0
  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
g_file_real_open_readwrite_finish (GFile         *file,
6783
                                   GAsyncResult  *res,
6784
                                   GError       **error)
6785
0
{
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
0
}
6790
6791
static void
6792
create_readwrite_async_thread (GTask        *task,
6793
                               gpointer      object,
6794
                               gpointer      task_data,
6795
                               GCancellable *cancellable)
6796
0
{
6797
0
  GFileCreateFlags *data = task_data;
6798
0
  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
0
  else
6806
0
    g_task_return_pointer (task, stream, g_object_unref);
6807
0
}
6808
6809
static void
6810
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
0
{
6817
0
  GFileCreateFlags *data;
6818
0
  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
g_file_real_create_readwrite_finish (GFile         *file,
6834
                                     GAsyncResult  *res,
6835
                                     GError       **error)
6836
0
{
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
0
}
6841
6842
typedef struct {
6843
  char *etag;
6844
  gboolean make_backup;
6845
  GFileCreateFlags flags;
6846
} ReplaceRWAsyncData;
6847
6848
static void
6849
replace_rw_async_data_free (ReplaceRWAsyncData *data)
6850
0
{
6851
0
  g_free (data->etag);
6852
0
  g_free (data);
6853
0
}
6854
6855
static void
6856
replace_readwrite_async_thread (GTask        *task,
6857
                                gpointer      object,
6858
                                gpointer      task_data,
6859
                                GCancellable *cancellable)
6860
0
{
6861
0
  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
0
                                     data->make_backup,
6868
0
                                     data->flags,
6869
0
                                     cancellable,
6870
0
                                     &error);
6871
6872
0
  if (stream == NULL)
6873
0
    g_task_return_error (task, error);
6874
0
  else
6875
0
    g_task_return_pointer (task, stream, g_object_unref);
6876
0
}
6877
6878
static void
6879
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
0
{
6888
0
  GTask *task;
6889
0
  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
g_file_real_replace_readwrite_finish (GFile         *file,
6907
                                      GAsyncResult  *res,
6908
                                      GError       **error)
6909
0
{
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
0
}
6914
6915
static void
6916
set_display_name_async_thread (GTask        *task,
6917
                               gpointer      object,
6918
                               gpointer      task_data,
6919
                               GCancellable *cancellable)
6920
0
{
6921
0
  GError *error = NULL;
6922
0
  char *name = task_data;
6923
0
  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
0
  else
6930
0
    g_task_return_pointer (task, file, g_object_unref);
6931
0
}
6932
6933
static void
6934
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
0
{
6941
0
  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
g_file_real_set_display_name_finish (GFile         *file,
6954
                                     GAsyncResult  *res,
6955
                                     GError       **error)
6956
0
{
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
0
}
6961
6962
typedef struct {
6963
  GFileQueryInfoFlags flags;
6964
  GFileInfo *info;
6965
} SetInfoAsyncData;
6966
6967
static void
6968
set_info_data_free (SetInfoAsyncData *data)
6969
0
{
6970
0
  if (data->info)
6971
0
    g_object_unref (data->info);
6972
0
  g_free (data);
6973
0
}
6974
6975
static void
6976
set_info_async_thread (GTask        *task,
6977
                       gpointer      object,
6978
                       gpointer      task_data,
6979
                       GCancellable *cancellable)
6980
0
{
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
0
                                       data->info,
6986
0
                                       data->flags,
6987
0
                                       cancellable,
6988
0
                                       &error))
6989
0
    g_task_return_boolean (task, TRUE);
6990
0
  else
6991
0
    g_task_return_error (task, error);
6992
0
}
6993
6994
static void
6995
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
0
{
7003
0
  GTask *task;
7004
0
  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
g_file_real_set_attributes_finish (GFile         *file,
7021
                                   GAsyncResult  *res,
7022
                                   GFileInfo    **info,
7023
                                   GError       **error)
7024
0
{
7025
0
  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
0
}
7036
7037
static void
7038
find_enclosing_mount_async_thread (GTask        *task,
7039
                                   gpointer      object,
7040
                                   gpointer      task_data,
7041
                                   GCancellable *cancellable)
7042
0
{
7043
0
  GError *error = NULL;
7044
0
  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
0
  else
7051
0
    g_task_return_pointer (task, mount, g_object_unref);
7052
0
}
7053
7054
static void
7055
g_file_real_find_enclosing_mount_async (GFile               *file,
7056
                                        int                  io_priority,
7057
                                        GCancellable        *cancellable,
7058
                                        GAsyncReadyCallback  callback,
7059
                                        gpointer             user_data)
7060
0
{
7061
0
  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
g_file_real_find_enclosing_mount_finish (GFile         *file,
7073
                                         GAsyncResult  *res,
7074
                                         GError       **error)
7075
0
{
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
0
}
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
copy_async_data_free (CopyAsyncData *data)
7092
0
{
7093
0
  g_object_unref (data->source);
7094
0
  g_object_unref (data->destination);
7095
0
  g_slice_free (CopyAsyncData, data);
7096
0
}
7097
7098
typedef struct {
7099
  CopyAsyncData *data;
7100
  goffset current_num_bytes;
7101
  goffset total_num_bytes;
7102
} CopyProgressData;
7103
7104
static gboolean
7105
copy_async_progress_in_main (gpointer user_data)
7106
0
{
7107
0
  CopyProgressData *progress = user_data;
7108
0
  CopyAsyncData *data = progress->data;
7109
7110
0
  data->progress_cb (progress->current_num_bytes,
7111
0
                     progress->total_num_bytes,
7112
0
                     data->progress_cb_data);
7113
7114
0
  return FALSE;
7115
0
}
7116
7117
static void
7118
copy_async_progress_callback (goffset  current_num_bytes,
7119
                              goffset  total_num_bytes,
7120
                              gpointer user_data)
7121
0
{
7122
0
  GTask *task = user_data;
7123
0
  CopyAsyncData *data = g_task_get_task_data (task);
7124
0
  CopyProgressData *progress;
7125
7126
0
  progress = g_new (CopyProgressData, 1);
7127
0
  progress->data = data;
7128
0
  progress->current_num_bytes = current_num_bytes;
7129
0
  progress->total_num_bytes = total_num_bytes;
7130
7131
0
  g_main_context_invoke_full (g_task_get_context (task),
7132
0
                              g_task_get_priority (task),
7133
0
                              copy_async_progress_in_main,
7134
0
                              progress,
7135
0
                              g_free);
7136
0
}
7137
7138
static void
7139
copy_async_thread (GTask        *task,
7140
                   gpointer      source,
7141
                   gpointer      task_data,
7142
                   GCancellable *cancellable)
7143
0
{
7144
0
  CopyAsyncData *data = task_data;
7145
0
  gboolean result;
7146
0
  GError *error = NULL;
7147
7148
0
  result = g_file_copy (data->source,
7149
0
                        data->destination,
7150
0
                        data->flags,
7151
0
                        cancellable,
7152
0
                        (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
7153
0
                        task,
7154
0
                        &error);
7155
0
  if (result)
7156
0
    g_task_return_boolean (task, TRUE);
7157
0
  else
7158
0
    g_task_return_error (task, error);
7159
0
}
7160
7161
static void
7162
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
0
{
7172
0
  GTask *task;
7173
0
  CopyAsyncData *data;
7174
7175
0
  data = g_slice_new (CopyAsyncData);
7176
0
  data->source = g_object_ref (source);
7177
0
  data->destination = g_object_ref (destination);
7178
0
  data->flags = flags;
7179
0
  data->progress_cb = progress_callback;
7180
0
  data->progress_cb_data = progress_callback_data;
7181
7182
0
  task = g_task_new (source, cancellable, callback, user_data);
7183
0
  g_task_set_source_tag (task, g_file_real_copy_async);
7184
0
  g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
7185
0
  g_task_set_priority (task, io_priority);
7186
0
  g_task_run_in_thread (task, copy_async_thread);
7187
0
  g_object_unref (task);
7188
0
}
7189
7190
static gboolean
7191
g_file_real_copy_finish (GFile        *file,
7192
                         GAsyncResult *res,
7193
                         GError      **error)
7194
0
{
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
0
}
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
g_file_new_for_path (const char *path)
7219
0
{
7220
0
  g_return_val_if_fail (path != NULL, NULL);
7221
7222
0
  return g_vfs_get_file_for_path (g_vfs_get_default (), path);
7223
0
}
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
g_file_new_for_uri (const char *uri)
7239
0
{
7240
0
  g_return_val_if_fail (uri != NULL, NULL);
7241
7242
0
  return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
7243
0
}
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
g_file_new_tmp (const char     *tmpl,
7270
                GFileIOStream **iostream,
7271
                GError        **error)
7272
0
{
7273
0
  gint fd;
7274
0
  gchar *path;
7275
0
  GFile *file;
7276
0
  GFileOutputStream *output;
7277
7278
0
  g_return_val_if_fail (iostream != NULL, NULL);
7279
7280
0
  fd = g_file_open_tmp (tmpl, &path, error);
7281
0
  if (fd == -1)
7282
0
    return NULL;
7283
7284
0
  file = g_file_new_for_path (path);
7285
7286
0
  output = _g_local_file_output_stream_new (fd);
7287
0
  *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
7288
7289
0
  g_object_unref (output);
7290
0
  g_free (path);
7291
7292
0
  return file;
7293
0
}
7294
7295
typedef struct {
7296
  GFile *file;
7297
  GFileIOStream *iostream;
7298
} NewTmpAsyncData;
7299
7300
static void
7301
new_tmp_data_free (NewTmpAsyncData *data)
7302
0
{
7303
0
  g_clear_object (&data->file);
7304
0
  g_clear_object (&data->iostream);
7305
0
  g_free (data);
7306
0
}
7307
7308
static void
7309
new_tmp_async_thread (GTask         *task,
7310
                      gpointer       object,
7311
                      gpointer       task_data,
7312
                      GCancellable  *cancellable)
7313
0
{
7314
0
  GFile *file;
7315
0
  const char *tmpl = task_data;
7316
0
  GFileIOStream *iostream = NULL;
7317
0
  GError *error = NULL;
7318
0
  NewTmpAsyncData *return_data;
7319
7320
0
  if (g_task_return_error_if_cancelled (task))
7321
0
    return;
7322
7323
0
  file = g_file_new_tmp (tmpl, &iostream, &error);
7324
7325
0
  if (!file)
7326
0
    {
7327
0
      int error_code = G_IO_ERROR_FAILED;
7328
7329
0
      if (error->domain == G_IO_ERROR)
7330
0
        {
7331
0
          g_task_return_error (task, g_steal_pointer (&error));
7332
0
          return;
7333
0
        }
7334
7335
0
      if (error->domain == G_FILE_ERROR)
7336
0
        error_code = g_io_error_from_file_error (error->code);
7337
7338
0
      g_task_return_new_error (task, G_IO_ERROR, error_code,
7339
0
                               _("Failed to create a temporary directory for "
7340
0
                                 "template “%s”: %s"),
7341
0
                               tmpl, error->message);
7342
7343
0
      g_clear_error (&error);
7344
0
      return;
7345
0
    }
7346
7347
0
  return_data = g_new0 (NewTmpAsyncData, 1);
7348
0
  return_data->file = g_steal_pointer (&file);
7349
0
  return_data->iostream = g_steal_pointer (&iostream);
7350
7351
0
  g_task_return_pointer (task, g_steal_pointer (&return_data),
7352
0
                         (GDestroyNotify) new_tmp_data_free);
7353
0
}
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
g_file_new_tmp_async (const char          *tmpl,
7375
                      int                  io_priority,
7376
                      GCancellable        *cancellable,
7377
                      GAsyncReadyCallback  callback,
7378
                      gpointer             user_data)
7379
0
{
7380
0
  GTask *task;
7381
7382
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7383
7384
0
  task = g_task_new (NULL, cancellable, callback, user_data);
7385
0
  g_task_set_source_tag (task, g_file_new_tmp_async);
7386
0
  g_task_set_task_data (task, g_strdup (tmpl), g_free);
7387
0
  g_task_set_priority (task, io_priority);
7388
0
  g_task_set_check_cancellable (task, TRUE);
7389
0
  g_task_run_in_thread (task, new_tmp_async_thread);
7390
0
  g_object_unref (task);
7391
0
}
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
g_file_new_tmp_finish (GAsyncResult   *result,
7408
                       GFileIOStream **iostream,
7409
                       GError        **error)
7410
0
{
7411
0
  GFile *file;
7412
0
  NewTmpAsyncData *data;
7413
7414
0
  g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7415
0
  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7416
0
                        g_file_new_tmp_async, NULL);
7417
0
  g_return_val_if_fail (iostream != NULL, NULL);
7418
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7419
7420
0
  data = g_task_propagate_pointer (G_TASK (result), error);
7421
7422
0
  if (!data)
7423
0
    {
7424
0
      *iostream = NULL;
7425
0
      return NULL;
7426
0
    }
7427
7428
0
  file = g_steal_pointer (&data->file);
7429
0
  *iostream = g_steal_pointer (&data->iostream);
7430
7431
0
  new_tmp_data_free (data);
7432
7433
0
  return file;
7434
0
}
7435
7436
static void
7437
new_tmp_dir_async_thread (GTask         *task,
7438
                          gpointer       object,
7439
                          gpointer       task_data,
7440
                          GCancellable  *cancellable)
7441
0
{
7442
0
  gchar *path;
7443
0
  const char *tmpl = task_data;
7444
0
  GError *error = NULL;
7445
7446
0
  if (g_task_return_error_if_cancelled (task))
7447
0
    return;
7448
7449
0
  path = g_dir_make_tmp (tmpl, &error);
7450
7451
0
  if (!path)
7452
0
    {
7453
0
      int error_code = G_IO_ERROR_FAILED;
7454
7455
0
      if (error->domain == G_IO_ERROR)
7456
0
        {
7457
0
          g_task_return_error (task, g_steal_pointer (&error));
7458
0
          return;
7459
0
        }
7460
7461
0
      if (error->domain == G_FILE_ERROR)
7462
0
        error_code = g_io_error_from_file_error (error->code);
7463
7464
0
      g_task_return_new_error (task, G_IO_ERROR, error_code,
7465
0
                               _("Failed to create a temporary directory for "
7466
0
                                 "template “%s”: %s"),
7467
0
                               tmpl, error->message);
7468
7469
0
      g_clear_error (&error);
7470
0
      return;
7471
0
    }
7472
7473
0
  g_task_return_pointer (task, g_file_new_for_path (path), g_object_unref);
7474
7475
0
  g_free (path);
7476
0
}
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
g_file_new_tmp_dir_async (const char          *tmpl,
7498
                          int                  io_priority,
7499
                          GCancellable        *cancellable,
7500
                          GAsyncReadyCallback  callback,
7501
                          gpointer             user_data)
7502
0
{
7503
0
  GTask *task;
7504
7505
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7506
7507
0
  task = g_task_new (NULL, cancellable, callback, user_data);
7508
0
  g_task_set_source_tag (task, g_file_new_tmp_dir_async);
7509
0
  g_task_set_task_data (task, g_strdup (tmpl), g_free);
7510
0
  g_task_set_priority (task, io_priority);
7511
0
  g_task_set_check_cancellable (task, TRUE);
7512
0
  g_task_run_in_thread (task, new_tmp_dir_async_thread);
7513
0
  g_object_unref (task);
7514
0
}
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
g_file_new_tmp_dir_finish (GAsyncResult  *result,
7531
                           GError       **error)
7532
0
{
7533
0
  g_return_val_if_fail (g_task_is_valid (result, NULL), NULL);
7534
0
  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
7535
0
                        g_file_new_tmp_dir_async, NULL);
7536
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
7537
7538
0
  return g_task_propagate_pointer (G_TASK (result), error);
7539
0
}
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
g_file_parse_name (const char *parse_name)
7554
0
{
7555
0
  g_return_val_if_fail (parse_name != NULL, NULL);
7556
7557
0
  return g_vfs_parse_name (g_vfs_get_default (), parse_name);
7558
0
}
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
g_file_new_build_filename (const gchar *first_element,
7577
                           ...)
7578
0
{
7579
0
  gchar *str;
7580
0
  GFile *file;
7581
0
  va_list args;
7582
7583
0
  g_return_val_if_fail (first_element != NULL, NULL);
7584
7585
0
  va_start (args, first_element);
7586
0
  str = g_build_filename_valist (first_element, &args);
7587
0
  va_end (args);
7588
7589
0
  file = g_file_new_for_path (str);
7590
0
  g_free (str);
7591
7592
0
  return file;
7593
0
}
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
g_file_new_build_filenamev (const gchar * const *args)
7613
0
{
7614
0
  gchar *str;
7615
0
  GFile *file;
7616
7617
0
  str = g_build_filenamev ((gchar **) args);
7618
0
  file = g_file_new_for_path (str);
7619
0
  g_free (str);
7620
7621
0
  return file;
7622
0
}
7623
7624
static gboolean
7625
is_valid_scheme_character (char c)
7626
0
{
7627
0
  return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
7628
0
}
7629
7630
/* Following RFC 2396, valid schemes are built like:
7631
 *       scheme        = alpha *( alpha | digit | "+" | "-" | "." )
7632
 */
7633
static gboolean
7634
has_valid_scheme (const char *uri)
7635
0
{
7636
0
  const char *p;
7637
7638
0
  p = uri;
7639
7640
0
  if (!g_ascii_isalpha (*p))
7641
0
    return FALSE;
7642
7643
0
  do {
7644
0
    p++;
7645
0
  } while (is_valid_scheme_character (*p));
7646
7647
0
  return *p == ':';
7648
0
}
7649
7650
static GFile *
7651
new_for_cmdline_arg (const gchar *arg,
7652
                     const gchar *cwd)
7653
0
{
7654
0
  GFile *file;
7655
0
  char *filename;
7656
7657
0
  if (g_path_is_absolute (arg))
7658
0
    return g_file_new_for_path (arg);
7659
7660
0
  if (has_valid_scheme (arg))
7661
0
    return g_file_new_for_uri (arg);
7662
7663
0
  if (cwd == NULL)
7664
0
    {
7665
0
      char *current_dir;
7666
7667
0
      current_dir = g_get_current_dir ();
7668
0
      filename = g_build_filename (current_dir, arg, NULL);
7669
0
      g_free (current_dir);
7670
0
    }
7671
0
  else
7672
0
    filename = g_build_filename (cwd, arg, NULL);
7673
7674
0
  file = g_file_new_for_path (filename);
7675
0
  g_free (filename);
7676
7677
0
  return file;
7678
0
}
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
g_file_new_for_commandline_arg (const char *arg)
7704
0
{
7705
0
  g_return_val_if_fail (arg != NULL, NULL);
7706
7707
0
  return new_for_cmdline_arg (arg, NULL);
7708
0
}
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
g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
7733
                                        const gchar *cwd)
7734
0
{
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
0
}
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
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
0
{
7772
0
  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
0
    {
7780
0
      g_task_report_new_error (location, callback, user_data,
7781
0
                               g_file_mount_enclosing_volume,
7782
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7783
0
                               _("volume doesn’t implement mount"));
7784
0
      return;
7785
0
    }
7786
7787
0
  (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
7788
7789
0
}
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
g_file_mount_enclosing_volume_finish (GFile         *location,
7805
                                      GAsyncResult  *result,
7806
                                      GError       **error)
7807
0
{
7808
0
  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
0
}
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
g_file_query_default_handler (GFile         *file,
7846
                              GCancellable  *cancellable,
7847
                              GError       **error)
7848
0
{
7849
0
  char *uri_scheme;
7850
0
  const char *content_type;
7851
0
  GAppInfo *appinfo;
7852
0
  GFileInfo *info;
7853
0
  char *path;
7854
7855
0
  uri_scheme = g_file_get_uri_scheme (file);
7856
0
  if (uri_scheme && uri_scheme[0] != '\0')
7857
0
    {
7858
0
      appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
7859
0
      g_free (uri_scheme);
7860
7861
0
      if (appinfo != NULL)
7862
0
        return appinfo;
7863
0
    }
7864
0
  else
7865
0
    g_free (uri_scheme);
7866
7867
0
  info = g_file_query_info (file,
7868
0
                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
7869
0
                            G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
7870
0
                            0,
7871
0
                            cancellable,
7872
0
                            error);
7873
0
  if (info == NULL)
7874
0
    return NULL;
7875
7876
0
  appinfo = NULL;
7877
7878
0
  content_type = g_file_info_get_content_type (info);
7879
0
  if (content_type == NULL)
7880
0
    content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7881
0
  if (content_type)
7882
0
    {
7883
      /* Don't use is_native(), as we want to support fuse paths if available */
7884
0
      path = g_file_get_path (file);
7885
0
      appinfo = g_app_info_get_default_for_type (content_type,
7886
0
                                                 path == NULL);
7887
0
      g_free (path);
7888
0
    }
7889
7890
0
  g_object_unref (info);
7891
7892
0
  if (appinfo != NULL)
7893
0
    return appinfo;
7894
7895
0
  g_set_error_literal (error, G_IO_ERROR,
7896
0
                       G_IO_ERROR_NOT_SUPPORTED,
7897
0
                       _("No application is registered as handling this file"));
7898
0
  return NULL;
7899
0
}
7900
7901
static void
7902
query_default_handler_query_app_info_for_type_cb (GObject      *object,
7903
                                                  GAsyncResult *result,
7904
                                                  gpointer      user_data)
7905
0
{
7906
0
  GTask *task = G_TASK (user_data);
7907
0
  GAppInfo *appinfo;
7908
0
  GError *error = NULL;
7909
7910
0
  appinfo = g_app_info_get_default_for_type_finish (result, &error);
7911
7912
0
  if (appinfo != NULL)
7913
0
    {
7914
0
      g_task_return_pointer (task, g_steal_pointer (&appinfo), g_object_unref);
7915
0
    }
7916
0
  else if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
7917
0
    {
7918
0
      g_task_return_new_error_literal (task,
7919
0
                                       G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7920
0
                                       error->message);
7921
0
    }
7922
0
  else
7923
0
    {
7924
0
      g_task_return_error (task, g_steal_pointer (&error));
7925
0
    }
7926
7927
0
  g_clear_error (&error);
7928
0
  g_object_unref (task);
7929
0
}
7930
7931
static void
7932
query_default_handler_query_info_cb (GObject      *object,
7933
                                     GAsyncResult *result,
7934
                                     gpointer      user_data)
7935
0
{
7936
0
  GFile *file = G_FILE (object);
7937
0
  GTask *task = G_TASK (user_data);
7938
0
  GError *error = NULL;
7939
0
  GFileInfo *info;
7940
0
  const char *content_type;
7941
7942
0
  info = g_file_query_info_finish (file, result, &error);
7943
0
  if (info == NULL)
7944
0
    {
7945
0
      g_task_return_error (task, g_steal_pointer (&error));
7946
0
      g_object_unref (task);
7947
0
      return;
7948
0
    }
7949
7950
0
  content_type = g_file_info_get_content_type (info);
7951
0
  if (content_type == NULL)
7952
0
    content_type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
7953
0
  if (content_type)
7954
0
    {
7955
0
      GCancellable *cancellable = g_task_get_cancellable (task);
7956
0
      char *path;
7957
7958
      /* Don't use is_native(), as we want to support fuse paths if available */
7959
0
      path = g_file_get_path (file);
7960
7961
0
      g_app_info_get_default_for_type_async (content_type,
7962
0
                                             path == NULL,
7963
0
                                             cancellable,
7964
0
                                             query_default_handler_query_app_info_for_type_cb,
7965
0
                                             g_steal_pointer (&task));
7966
7967
0
      g_free (path);
7968
0
    }
7969
0
  else
7970
0
    {
7971
0
      g_task_return_new_error_literal (task,
7972
0
                                       G_IO_ERROR,
7973
0
                                       G_IO_ERROR_NOT_SUPPORTED,
7974
0
                                       _("No application is registered as handling this file"));
7975
0
    }
7976
7977
0
  g_object_unref (info);
7978
0
  g_clear_object (&task);
7979
0
}
7980
7981
static void
7982
on_query_default_handler_for_uri_cb (GObject      *object,
7983
                                     GAsyncResult *result,
7984
                                     gpointer      user_data)
7985
0
{
7986
0
  GTask *task = user_data;
7987
0
  GAppInfo *app_info;
7988
7989
0
  app_info = g_app_info_get_default_for_uri_scheme_finish (result, NULL);
7990
7991
0
  if (app_info)
7992
0
    {
7993
0
      g_task_return_pointer (task, g_steal_pointer (&app_info), g_object_unref);
7994
0
      g_object_unref (task);
7995
0
    }
7996
0
  else
7997
0
    {
7998
0
      g_file_query_info_async (g_task_get_source_object (task),
7999
0
                               G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
8000
0
                               G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
8001
0
                               0,
8002
0
                               g_task_get_priority (task),
8003
0
                               g_task_get_cancellable (task),
8004
0
                               query_default_handler_query_info_cb,
8005
0
                               task);
8006
0
    }
8007
0
}
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
g_file_query_default_handler_async (GFile              *file,
8023
                                    int                 io_priority,
8024
                                    GCancellable       *cancellable,
8025
                                    GAsyncReadyCallback callback,
8026
                                    gpointer            user_data)
8027
0
{
8028
0
  GTask *task;
8029
0
  char *uri_scheme;
8030
8031
0
  task = g_task_new (file, cancellable, callback, user_data);
8032
0
  g_task_set_source_tag (task, g_file_query_default_handler_async);
8033
8034
0
  uri_scheme = g_file_get_uri_scheme (file);
8035
0
  if (uri_scheme && uri_scheme[0] != '\0')
8036
0
    {
8037
0
      g_app_info_get_default_for_uri_scheme_async (uri_scheme,
8038
0
                                                   cancellable,
8039
0
                                                   on_query_default_handler_for_uri_cb,
8040
0
                                                   g_steal_pointer (&task));
8041
0
      g_free (uri_scheme);
8042
0
      return;
8043
0
    }
8044
8045
0
  g_file_query_info_async (file,
8046
0
                           G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
8047
0
                           G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
8048
0
                           0,
8049
0
                           io_priority,
8050
0
                           cancellable,
8051
0
                           query_default_handler_query_info_cb,
8052
0
                           g_steal_pointer (&task));
8053
8054
0
  g_free (uri_scheme);
8055
0
}
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
g_file_query_default_handler_finish (GFile        *file,
8073
                                     GAsyncResult *result,
8074
                                     GError      **error)
8075
0
{
8076
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
8077
0
  g_return_val_if_fail (g_task_is_valid (result, file), NULL);
8078
8079
0
  return g_task_propagate_pointer (G_TASK (result), error);
8080
0
}
8081
8082
0
#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
g_file_load_contents (GFile         *file,
8109
                      GCancellable  *cancellable,
8110
                      char         **contents,
8111
                      gsize         *length,
8112
                      char         **etag_out,
8113
                      GError       **error)
8114
0
{
8115
0
  GFileInputStream *in;
8116
0
  char *data;
8117
0
  gsize size;
8118
0
  gsize pos;
8119
0
  gssize res;
8120
0
  GFileInfo *info;
8121
8122
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
8123
0
  g_return_val_if_fail (contents != NULL, FALSE);
8124
8125
0
  in = g_file_read (file, cancellable, error);
8126
0
  if (in == NULL)
8127
0
    return FALSE;
8128
8129
0
  size = GET_CONTENT_BLOCK_SIZE;
8130
0
  data = g_malloc (GET_CONTENT_BLOCK_SIZE);
8131
0
  pos = 0;
8132
8133
0
  while ((res = g_input_stream_read (G_INPUT_STREAM (in),
8134
0
                                     data + pos,
8135
0
                                     GET_CONTENT_BLOCK_SIZE,
8136
0
                                     cancellable, error)) > 0)
8137
0
    {
8138
0
      pos += res;
8139
0
      if (size - pos < GET_CONTENT_BLOCK_SIZE)
8140
0
        {
8141
0
          g_assert (size <= G_MAXSIZE / 2);
8142
0
          size *= 2;
8143
0
          data = g_realloc (data, size);
8144
0
        }
8145
0
    }
8146
8147
0
  if (etag_out)
8148
0
    {
8149
0
      *etag_out = NULL;
8150
8151
0
      info = g_file_input_stream_query_info (in,
8152
0
                                             G_FILE_ATTRIBUTE_ETAG_VALUE,
8153
0
                                             cancellable,
8154
0
                                             NULL);
8155
0
      if (info)
8156
0
        {
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
0
        }
8160
0
    }
8161
8162
  /* Ignore errors on close */
8163
0
  g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
8164
0
  g_object_unref (in);
8165
8166
0
  if (res < 0)
8167
0
    {
8168
      /* error is set already */
8169
0
      g_free (data);
8170
0
      return FALSE;
8171
0
    }
8172
8173
0
  if (length)
8174
0
    *length = pos;
8175
8176
  /* Zero terminate (allocating extra bytes if needed) */
8177
0
  if (pos >= size)
8178
0
    data = g_realloc (data, pos + 1);
8179
0
  data[pos] = 0;
8180
8181
0
  *contents = g_steal_pointer (&data);
8182
8183
0
  return TRUE;
8184
0
}
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
load_contents_data_free (LoadContentsData *data)
8198
0
{
8199
0
  g_clear_pointer (&data->data, g_free);
8200
0
  g_free (data->etag);
8201
0
  g_free (data);
8202
0
}
8203
8204
static void
8205
load_contents_data_ensure_space (LoadContentsData *data,
8206
                                 gsize             space)
8207
0
{
8208
0
  if (data->size - data->pos < space)
8209
0
    {
8210
0
      if (data->data == NULL)
8211
0
        {
8212
0
          data->size = space;
8213
0
          data->data = g_malloc (space);
8214
0
        }
8215
0
      else
8216
0
        {
8217
0
          g_assert (data->size <= G_MAXSIZE / 2);
8218
0
          data->size *= 2;
8219
0
          data->data = g_realloc (data->data, data->size);
8220
0
        }
8221
0
    }
8222
0
}
8223
8224
static void
8225
load_contents_close_callback (GObject      *obj,
8226
                              GAsyncResult *close_res,
8227
                              gpointer      user_data)
8228
0
{
8229
0
  GInputStream *stream = G_INPUT_STREAM (obj);
8230
0
  LoadContentsData *data = user_data;
8231
8232
  /* Ignore errors here, we're only reading anyway */
8233
0
  g_input_stream_close_finish (stream, close_res, NULL);
8234
0
  g_object_unref (stream);
8235
8236
0
  g_task_return_boolean (data->task, TRUE);
8237
0
  g_object_unref (data->task);
8238
0
}
8239
8240
static void
8241
load_contents_fstat_callback (GObject      *obj,
8242
                              GAsyncResult *stat_res,
8243
                              gpointer      user_data)
8244
0
{
8245
0
  GInputStream *stream = G_INPUT_STREAM (obj);
8246
0
  LoadContentsData *data = user_data;
8247
0
  GFileInfo *info;
8248
8249
0
  info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
8250
0
                                                stat_res, NULL);
8251
0
  if (info)
8252
0
    {
8253
0
      data->etag = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_ETAG_VALUE) ? g_strdup (g_file_info_get_etag (info)) : NULL;
8254
0
      g_object_unref (info);
8255
0
    }
8256
8257
0
  g_input_stream_close_async (stream, 0,
8258
0
                              g_task_get_cancellable (data->task),
8259
0
                              load_contents_close_callback, data);
8260
0
}
8261
8262
static void
8263
load_contents_read_callback (GObject      *obj,
8264
                             GAsyncResult *read_res,
8265
                             gpointer      user_data)
8266
0
{
8267
0
  GInputStream *stream = G_INPUT_STREAM (obj);
8268
0
  LoadContentsData *data = user_data;
8269
0
  GError *error = NULL;
8270
0
  gssize read_size;
8271
8272
0
  read_size = g_input_stream_read_finish (stream, read_res, &error);
8273
8274
0
  if (read_size < 0)
8275
0
    {
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
0
    }
8283
0
  else if (read_size == 0)
8284
0
    {
8285
0
      g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
8286
0
                                            G_FILE_ATTRIBUTE_ETAG_VALUE,
8287
0
                                            0,
8288
0
                                            g_task_get_cancellable (data->task),
8289
0
                                            load_contents_fstat_callback,
8290
0
                                            data);
8291
0
    }
8292
0
  else if (read_size > 0)
8293
0
    {
8294
0
      data->pos += read_size;
8295
8296
0
      load_contents_data_ensure_space (data, GET_CONTENT_BLOCK_SIZE);
8297
8298
0
      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
0
                                              G_FILE_ATTRIBUTE_ETAG_VALUE,
8303
0
                                              0,
8304
0
                                              g_task_get_cancellable (data->task),
8305
0
                                              load_contents_fstat_callback,
8306
0
                                              data);
8307
0
      else
8308
0
        g_input_stream_read_async (stream,
8309
0
                                   data->data + data->pos,
8310
0
                                   GET_CONTENT_BLOCK_SIZE,
8311
0
                                   0,
8312
0
                                   g_task_get_cancellable (data->task),
8313
0
                                   load_contents_read_callback,
8314
0
                                   data);
8315
0
    }
8316
0
}
8317
8318
static void
8319
load_contents_open_callback (GObject      *obj,
8320
                             GAsyncResult *open_res,
8321
                             gpointer      user_data)
8322
0
{
8323
0
  GFile *file = G_FILE (obj);
8324
0
  GFileInputStream *stream;
8325
0
  LoadContentsData *data = user_data;
8326
0
  GError *error = NULL;
8327
8328
0
  stream = g_file_read_finish (file, open_res, &error);
8329
8330
0
  if (stream)
8331
0
    {
8332
0
      load_contents_data_ensure_space (data, GET_CONTENT_BLOCK_SIZE);
8333
0
      g_input_stream_read_async (G_INPUT_STREAM (stream),
8334
0
                                 data->data + data->pos,
8335
0
                                 GET_CONTENT_BLOCK_SIZE,
8336
0
                                 0,
8337
0
                                 g_task_get_cancellable (data->task),
8338
0
                                 load_contents_read_callback,
8339
0
                                 data);
8340
0
    }
8341
0
  else
8342
0
    {
8343
0
      g_task_return_error (data->task, error);
8344
0
      g_object_unref (data->task);
8345
0
    }
8346
0
}
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
g_file_load_partial_contents_async (GFile                 *file,
8373
                                    GCancellable          *cancellable,
8374
                                    GFileReadMoreCallback  read_more_callback,
8375
                                    GAsyncReadyCallback    callback,
8376
                                    gpointer               user_data)
8377
0
{
8378
0
  LoadContentsData *data;
8379
8380
0
  g_return_if_fail (G_IS_FILE (file));
8381
8382
0
  data = g_new0 (LoadContentsData, 1);
8383
0
  data->read_more_callback = read_more_callback;
8384
8385
0
  data->task = g_task_new (file, cancellable, callback, user_data);
8386
0
  g_task_set_source_tag (data->task, g_file_load_partial_contents_async);
8387
0
  g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
8388
8389
0
  g_file_read_async (file,
8390
0
                     0,
8391
0
                     g_task_get_cancellable (data->task),
8392
0
                     load_contents_open_callback,
8393
0
                     data);
8394
0
}
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
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
0
{
8424
0
  GTask *task;
8425
0
  LoadContentsData *data;
8426
8427
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
8428
0
  g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8429
0
  g_return_val_if_fail (contents != NULL, FALSE);
8430
8431
0
  task = G_TASK (res);
8432
8433
0
  if (!g_task_propagate_boolean (task, error))
8434
0
    {
8435
0
      if (length)
8436
0
        *length = 0;
8437
0
      return FALSE;
8438
0
    }
8439
8440
0
  data = g_task_get_task_data (task);
8441
8442
0
  if (length)
8443
0
    *length = data->pos;
8444
8445
0
  if (etag_out)
8446
0
    {
8447
0
      *etag_out = data->etag;
8448
0
      data->etag = NULL;
8449
0
    }
8450
8451
  /* Zero terminate */
8452
0
  load_contents_data_ensure_space (data, 1);
8453
0
  data->data[data->pos] = 0;
8454
8455
0
  *contents = g_steal_pointer (&data->data);
8456
8457
0
  return TRUE;
8458
0
}
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
g_file_load_contents_async (GFile               *file,
8483
                           GCancellable        *cancellable,
8484
                           GAsyncReadyCallback  callback,
8485
                           gpointer             user_data)
8486
0
{
8487
0
  g_file_load_partial_contents_async (file,
8488
0
                                      cancellable,
8489
0
                                      NULL,
8490
0
                                      callback, user_data);
8491
0
}
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
g_file_load_contents_finish (GFile         *file,
8515
                             GAsyncResult  *res,
8516
                             char         **contents,
8517
                             gsize         *length,
8518
                             char         **etag_out,
8519
                             GError       **error)
8520
0
{
8521
0
  return g_file_load_partial_contents_finish (file,
8522
0
                                              res,
8523
0
                                              contents,
8524
0
                                              length,
8525
0
                                              etag_out,
8526
0
                                              error);
8527
0
}
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
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
0
{
8575
0
  GFileOutputStream *out;
8576
0
  gsize pos, remainder;
8577
0
  gssize res = -1;
8578
0
  gboolean ret;
8579
8580
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
8581
0
  g_return_val_if_fail (contents != NULL, FALSE);
8582
8583
0
  out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
8584
0
  if (out == NULL)
8585
0
    return FALSE;
8586
8587
0
  pos = 0;
8588
0
  remainder = length;
8589
0
  while (remainder > 0 &&
8590
0
         (res = g_output_stream_write (G_OUTPUT_STREAM (out),
8591
0
                                       contents + pos,
8592
0
                                       MIN (remainder, GET_CONTENT_BLOCK_SIZE),
8593
0
                                       cancellable,
8594
0
                                       error)) > 0)
8595
0
    {
8596
0
      pos += res;
8597
0
      remainder -= res;
8598
0
    }
8599
8600
0
  if (remainder > 0 && res < 0)
8601
0
    {
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
0
    }
8609
8610
0
  ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
8611
8612
0
  if (new_etag)
8613
0
    *new_etag = g_file_output_stream_get_etag (out);
8614
8615
0
  g_object_unref (out);
8616
8617
0
  return ret;
8618
0
}
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
replace_contents_data_free (ReplaceContentsData *data)
8630
0
{
8631
0
  g_bytes_unref (data->content);
8632
0
  g_free (data->etag);
8633
0
  g_free (data);
8634
0
}
8635
8636
static void
8637
replace_contents_close_callback (GObject      *obj,
8638
                                 GAsyncResult *close_res,
8639
                                 gpointer      user_data)
8640
0
{
8641
0
  GOutputStream *stream = G_OUTPUT_STREAM (obj);
8642
0
  ReplaceContentsData *data = user_data;
8643
8644
  /* Ignore errors here, we're only reading anyway */
8645
0
  g_output_stream_close_finish (stream, close_res, NULL);
8646
8647
0
  if (!data->failed)
8648
0
    {
8649
0
      data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
8650
0
      g_task_return_boolean (data->task, TRUE);
8651
0
    }
8652
0
  g_object_unref (data->task);
8653
0
}
8654
8655
static void
8656
replace_contents_write_callback (GObject      *obj,
8657
                                 GAsyncResult *read_res,
8658
                                 gpointer      user_data)
8659
0
{
8660
0
  GOutputStream *stream = G_OUTPUT_STREAM (obj);
8661
0
  ReplaceContentsData *data = user_data;
8662
0
  GError *error = NULL;
8663
0
  gssize write_size;
8664
8665
0
  write_size = g_output_stream_write_finish (stream, read_res, &error);
8666
8667
0
  if (write_size <= 0)
8668
0
    {
8669
      /* Error or EOF, close the file */
8670
0
      if (write_size < 0)
8671
0
        {
8672
0
          data->failed = TRUE;
8673
0
          g_task_return_error (data->task, error);
8674
0
        }
8675
0
      g_output_stream_close_async (stream, 0,
8676
0
                                   g_task_get_cancellable (data->task),
8677
0
                                   replace_contents_close_callback, data);
8678
0
    }
8679
0
  else if (write_size > 0)
8680
0
    {
8681
0
      const gchar *content;
8682
0
      gsize length;
8683
8684
0
      content = g_bytes_get_data (data->content, &length);
8685
0
      data->pos += write_size;
8686
8687
0
      if (data->pos >= length)
8688
0
        g_output_stream_close_async (stream, 0,
8689
0
                                     g_task_get_cancellable (data->task),
8690
0
                                     replace_contents_close_callback, data);
8691
0
      else
8692
0
        g_output_stream_write_async (stream,
8693
0
                                     content + data->pos,
8694
0
                                     length - data->pos,
8695
0
                                     0,
8696
0
                                     g_task_get_cancellable (data->task),
8697
0
                                     replace_contents_write_callback,
8698
0
                                     data);
8699
0
    }
8700
0
}
8701
8702
static void
8703
replace_contents_open_callback (GObject      *obj,
8704
                                GAsyncResult *open_res,
8705
                                gpointer      user_data)
8706
0
{
8707
0
  GFile *file = G_FILE (obj);
8708
0
  GFileOutputStream *stream;
8709
0
  ReplaceContentsData *data = user_data;
8710
0
  GError *error = NULL;
8711
8712
0
  stream = g_file_replace_finish (file, open_res, &error);
8713
8714
0
  if (stream)
8715
0
    {
8716
0
      const gchar *content;
8717
0
      gsize length;
8718
8719
0
      content = g_bytes_get_data (data->content, &length);
8720
0
      g_output_stream_write_async (G_OUTPUT_STREAM (stream),
8721
0
                                   content + data->pos,
8722
0
                                   length - data->pos,
8723
0
                                   0,
8724
0
                                   g_task_get_cancellable (data->task),
8725
0
                                   replace_contents_write_callback,
8726
0
                                   data);
8727
0
      g_object_unref (stream);  /* ownership is transferred to the write_async() call above */
8728
0
    }
8729
0
  else
8730
0
    {
8731
0
      g_task_return_error (data->task, error);
8732
0
      g_object_unref (data->task);
8733
0
    }
8734
0
}
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
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
0
{
8779
0
  GBytes *bytes;
8780
8781
0
  bytes = g_bytes_new_static (contents, length);
8782
0
  g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
8783
0
      cancellable, callback, user_data);
8784
0
  g_bytes_unref (bytes);
8785
0
}
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
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
0
{
8819
0
  ReplaceContentsData *data;
8820
8821
0
  g_return_if_fail (G_IS_FILE (file));
8822
0
  g_return_if_fail (contents != NULL);
8823
8824
0
  data = g_new0 (ReplaceContentsData, 1);
8825
8826
0
  data->content = g_bytes_ref (contents);
8827
8828
0
  data->task = g_task_new (file, cancellable, callback, user_data);
8829
0
  g_task_set_source_tag (data->task, g_file_replace_contents_bytes_async);
8830
0
  g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
8831
8832
0
  g_file_replace_async (file,
8833
0
                        etag,
8834
0
                        make_backup,
8835
0
                        flags,
8836
0
                        0,
8837
0
                        g_task_get_cancellable (data->task),
8838
0
                        replace_contents_open_callback,
8839
0
                        data);
8840
0
}
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
g_file_replace_contents_finish (GFile         *file,
8859
                                GAsyncResult  *res,
8860
                                char         **new_etag,
8861
                                GError       **error)
8862
0
{
8863
0
  GTask *task;
8864
0
  ReplaceContentsData *data;
8865
8866
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
8867
0
  g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
8868
8869
0
  task = G_TASK (res);
8870
8871
0
  if (!g_task_propagate_boolean (task, error))
8872
0
    return FALSE;
8873
8874
0
  data = g_task_get_task_data (task);
8875
8876
0
  if (new_etag)
8877
0
    {
8878
0
      *new_etag = data->etag;
8879
0
      data->etag = NULL; /* Take ownership */
8880
0
    }
8881
8882
0
  return TRUE;
8883
0
}
8884
8885
gboolean
8886
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
0
{
8896
0
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
8897
0
                       "Operation not supported for the current backend.");
8898
0
  return FALSE;
8899
0
}
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
measure_disk_usage_invoke_progress (gpointer user_data)
8927
0
{
8928
0
  MeasureProgress *progress = user_data;
8929
8930
0
  (* progress->callback) (progress->reporting,
8931
0
                          progress->current_size, progress->num_dirs, progress->num_files,
8932
0
                          progress->user_data);
8933
8934
0
  return FALSE;
8935
0
}
8936
8937
static void
8938
measure_disk_usage_progress (gboolean reporting,
8939
                             guint64  current_size,
8940
                             guint64  num_dirs,
8941
                             guint64  num_files,
8942
                             gpointer user_data)
8943
0
{
8944
0
  MeasureProgress progress;
8945
0
  GTask *task = user_data;
8946
0
  MeasureTaskData *data;
8947
8948
0
  data = g_task_get_task_data (task);
8949
8950
0
  progress.callback = data->progress_callback;
8951
0
  progress.user_data = data->progress_data;
8952
0
  progress.reporting = reporting;
8953
0
  progress.current_size = current_size;
8954
0
  progress.num_dirs = num_dirs;
8955
0
  progress.num_files = num_files;
8956
8957
0
  g_main_context_invoke_full (g_task_get_context (task),
8958
0
                              g_task_get_priority (task),
8959
0
                              measure_disk_usage_invoke_progress,
8960
0
                              g_memdup2 (&progress, sizeof progress),
8961
0
                              g_free);
8962
0
}
8963
8964
static void
8965
measure_disk_usage_thread (GTask        *task,
8966
                           gpointer      source_object,
8967
                           gpointer      task_data,
8968
                           GCancellable *cancellable)
8969
0
{
8970
0
  MeasureTaskData *data = task_data;
8971
0
  GError *error = NULL;
8972
0
  MeasureResult result = { 0, };
8973
8974
0
  if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
8975
0
                                 data->progress_callback ? measure_disk_usage_progress : NULL, task,
8976
0
                                 &result.disk_usage, &result.num_dirs, &result.num_files,
8977
0
                                 &error))
8978
0
    g_task_return_pointer (task, g_memdup2 (&result, sizeof result), g_free);
8979
0
  else
8980
0
    g_task_return_error (task, error);
8981
0
}
8982
8983
static void
8984
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
0
{
8993
0
  MeasureTaskData data;
8994
0
  GTask *task;
8995
8996
0
  data.flags = flags;
8997
0
  data.progress_callback = progress_callback;
8998
0
  data.progress_data = progress_data;
8999
9000
0
  task = g_task_new (file, cancellable, callback, user_data);
9001
0
  g_task_set_source_tag (task, g_file_real_measure_disk_usage_async);
9002
0
  g_task_set_task_data (task, g_memdup2 (&data, sizeof data), g_free);
9003
0
  g_task_set_priority (task, io_priority);
9004
9005
0
  g_task_run_in_thread (task, measure_disk_usage_thread);
9006
0
  g_object_unref (task);
9007
0
}
9008
9009
static gboolean
9010
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
0
{
9017
0
  MeasureResult *measure_result;
9018
9019
0
  g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
9020
9021
0
  measure_result = g_task_propagate_pointer (G_TASK (result), error);
9022
9023
0
  if (measure_result == NULL)
9024
0
    return FALSE;
9025
9026
0
  if (disk_usage)
9027
0
    *disk_usage = measure_result->disk_usage;
9028
9029
0
  if (num_dirs)
9030
0
    *num_dirs = measure_result->num_dirs;
9031
9032
0
  if (num_files)
9033
0
    *num_files = measure_result->num_files;
9034
9035
0
  g_free (measure_result);
9036
9037
0
  return TRUE;
9038
0
}
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
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
0
{
9087
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
9088
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
9089
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
9090
9091
0
  return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
9092
0
                                                      progress_callback, progress_data,
9093
0
                                                      disk_usage, num_dirs, num_files,
9094
0
                                                      error);
9095
0
}
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
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
0
{
9125
0
  g_return_if_fail (G_IS_FILE (file));
9126
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
9127
9128
0
  G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
9129
0
                                                     progress_callback, progress_data,
9130
0
                                                     callback, user_data);
9131
0
}
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
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
0
{
9159
0
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
9160
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
9161
9162
0
  return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
9163
0
}
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
g_file_start_mountable (GFile               *file,
9190
                        GDriveStartFlags     flags,
9191
                        GMountOperation     *start_operation,
9192
                        GCancellable        *cancellable,
9193
                        GAsyncReadyCallback  callback,
9194
                        gpointer             user_data)
9195
0
{
9196
0
  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
0
    {
9204
0
      g_task_report_new_error (file, callback, user_data,
9205
0
                               g_file_start_mountable,
9206
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9207
0
                               _("Operation not supported"));
9208
0
      return;
9209
0
    }
9210
9211
0
  (* iface->start_mountable) (file,
9212
0
                              flags,
9213
0
                              start_operation,
9214
0
                              cancellable,
9215
0
                              callback,
9216
0
                              user_data);
9217
0
}
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
g_file_start_mountable_finish (GFile         *file,
9237
                               GAsyncResult  *result,
9238
                               GError       **error)
9239
0
{
9240
0
  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
0
}
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
g_file_stop_mountable (GFile               *file,
9280
                       GMountUnmountFlags   flags,
9281
                       GMountOperation     *mount_operation,
9282
                       GCancellable        *cancellable,
9283
                       GAsyncReadyCallback  callback,
9284
                       gpointer             user_data)
9285
0
{
9286
0
  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
0
    {
9294
0
      g_task_report_new_error (file, callback, user_data,
9295
0
                               g_file_stop_mountable,
9296
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9297
0
                               _("Operation not supported"));
9298
0
      return;
9299
0
    }
9300
9301
0
  (* iface->stop_mountable) (file,
9302
0
                             flags,
9303
0
                             mount_operation,
9304
0
                             cancellable,
9305
0
                             callback,
9306
0
                             user_data);
9307
0
}
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
g_file_stop_mountable_finish (GFile         *file,
9327
                              GAsyncResult  *result,
9328
                              GError       **error)
9329
0
{
9330
0
  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
0
}
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
g_file_poll_mountable (GFile               *file,
9366
                       GCancellable        *cancellable,
9367
                       GAsyncReadyCallback  callback,
9368
                       gpointer             user_data)
9369
0
{
9370
0
  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
0
    {
9378
0
      g_task_report_new_error (file, callback, user_data,
9379
0
                               g_file_poll_mountable,
9380
0
                               G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
9381
0
                               _("Operation not supported"));
9382
0
      return;
9383
0
    }
9384
9385
0
  (* iface->poll_mountable) (file,
9386
0
                             cancellable,
9387
0
                             callback,
9388
0
                             user_data);
9389
0
}
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
g_file_poll_mountable_finish (GFile         *file,
9409
                              GAsyncResult  *result,
9410
                              GError       **error)
9411
0
{
9412
0
  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
0
}
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
g_file_supports_thread_contexts (GFile *file)
9441
0
{
9442
0
 GFileIface *iface;
9443
9444
0
 g_return_val_if_fail (G_IS_FILE (file), FALSE);
9445
9446
0
 iface = G_FILE_GET_IFACE (file);
9447
0
 return iface->supports_thread_contexts;
9448
0
}
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
g_file_load_bytes (GFile         *file,
9476
                   GCancellable  *cancellable,
9477
                   gchar        **etag_out,
9478
                   GError       **error)
9479
0
{
9480
0
  gchar *contents;
9481
0
  gsize len;
9482
9483
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
9484
0
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
9485
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9486
9487
0
  if (etag_out != NULL)
9488
0
    *etag_out = NULL;
9489
9490
0
  if (g_file_has_uri_scheme (file, "resource"))
9491
0
    {
9492
0
      GBytes *bytes;
9493
0
      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
0
    }
9504
9505
  /* contents is guaranteed to be \0 terminated */
9506
0
  if (g_file_load_contents (file, cancellable, &contents, &len, etag_out, error))
9507
0
    return g_bytes_new_take (g_steal_pointer (&contents), len);
9508
9509
0
  return NULL;
9510
0
}
9511
9512
static void
9513
g_file_load_bytes_cb (GObject      *object,
9514
                      GAsyncResult *result,
9515
                      gpointer      user_data)
9516
0
{
9517
0
  GFile *file = G_FILE (object);
9518
0
  GTask *task = user_data;
9519
0
  GError *error = NULL;
9520
0
  gchar *etag = NULL;
9521
0
  gchar *contents = NULL;
9522
0
  gsize len = 0;
9523
9524
0
  g_file_load_contents_finish (file, result, &contents, &len, &etag, &error);
9525
0
  g_task_set_task_data (task, g_steal_pointer (&etag), g_free);
9526
9527
0
  if (error != NULL)
9528
0
    g_task_return_error (task, g_steal_pointer (&error));
9529
0
  else
9530
0
    g_task_return_pointer (task,
9531
0
                           g_bytes_new_take (g_steal_pointer (&contents), len),
9532
0
                           (GDestroyNotify)g_bytes_unref);
9533
9534
0
  g_object_unref (task);
9535
0
}
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
g_file_load_bytes_async (GFile               *file,
9560
                         GCancellable        *cancellable,
9561
                         GAsyncReadyCallback  callback,
9562
                         gpointer             user_data)
9563
0
{
9564
0
  GError *error = NULL;
9565
0
  GBytes *bytes;
9566
0
  GTask *task;
9567
9568
0
  g_return_if_fail (G_IS_FILE (file));
9569
0
  g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
9570
9571
0
  task = g_task_new (file, cancellable, callback, user_data);
9572
0
  g_task_set_source_tag (task, g_file_load_bytes_async);
9573
9574
0
  if (!g_file_has_uri_scheme (file, "resource"))
9575
0
    {
9576
0
      g_file_load_contents_async (file,
9577
0
                                  cancellable,
9578
0
                                  g_file_load_bytes_cb,
9579
0
                                  g_steal_pointer (&task));
9580
0
      return;
9581
0
    }
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
0
  else
9588
0
    g_task_return_pointer (task,
9589
0
                           g_steal_pointer (&bytes),
9590
0
                           (GDestroyNotify)g_bytes_unref);
9591
9592
0
  g_object_unref (task);
9593
0
}
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
g_file_load_bytes_finish (GFile         *file,
9619
                          GAsyncResult  *result,
9620
                          gchar        **etag_out,
9621
                          GError       **error)
9622
0
{
9623
0
  GBytes *bytes;
9624
9625
0
  g_return_val_if_fail (G_IS_FILE (file), NULL);
9626
0
  g_return_val_if_fail (G_IS_TASK (result), NULL);
9627
0
  g_return_val_if_fail (g_task_is_valid (G_TASK (result), file), NULL);
9628
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
9629
9630
0
  bytes = g_task_propagate_pointer (G_TASK (result), error);
9631
9632
0
  if (etag_out != NULL)
9633
0
    *etag_out = g_strdup (g_task_get_task_data (G_TASK (result)));
9634
9635
0
  return bytes;
9636
0
}