Coverage Report

Created: 2025-07-12 06:25

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