Coverage Report

Created: 2025-11-16 06:24

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