Coverage Report

Created: 2025-07-18 06:10

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