Coverage Report

Created: 2025-07-01 07:09

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