Coverage Report

Created: 2025-12-08 06:13

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