Coverage Report

Created: 2025-06-13 06:55

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