GSeekable: document seek-past-end semantics
[glib.git] / gio / gfile.c
blobbf936ec93dcc2f5eed6f0a8a68430ac215f3be1b
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
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 of the License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "config.h"
27 #ifdef __linux__
28 #include <sys/ioctl.h>
29 #include <errno.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
33 #endif
35 #ifdef HAVE_SPLICE
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #endif
42 #include <string.h>
43 #include <sys/types.h>
44 #ifdef HAVE_PWD_H
45 #include <pwd.h>
46 #endif
48 #include "gfile.h"
49 #include "glib/gstdio.h"
50 #ifdef G_OS_UNIX
51 #include "glib-unix.h"
52 #endif
53 #include "gvfs.h"
54 #include "gtask.h"
55 #include "gfileattribute-priv.h"
56 #include "gfiledescriptorbased.h"
57 #include "gpollfilemonitor.h"
58 #include "gappinfo.h"
59 #include "gfileinputstream.h"
60 #include "gfileoutputstream.h"
61 #include "glocalfileoutputstream.h"
62 #include "glocalfileiostream.h"
63 #include "glocalfile.h"
64 #include "gcancellable.h"
65 #include "gasyncresult.h"
66 #include "gioerror.h"
67 #include "glibintl.h"
70 /**
71 * SECTION:gfile
72 * @short_description: File and Directory Handling
73 * @include: gio/gio.h
74 * @see_also: #GFileInfo, #GFileEnumerator
76 * #GFile is a high level abstraction for manipulating files on a
77 * virtual file system. #GFiles are lightweight, immutable objects
78 * that do no I/O upon creation. It is necessary to understand that
79 * #GFile objects do not represent files, merely an identifier for a
80 * file. All file content I/O is implemented as streaming operations
81 * (see #GInputStream and #GOutputStream).
83 * To construct a #GFile, you can use:
84 * <simplelist>
85 * <member>g_file_new_for_path() if you have a path.</member>
86 * <member>g_file_new_for_uri() if you have a URI.</member>
87 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
88 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
89 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
90 * </simplelist>
92 * One way to think of a #GFile is as an abstraction of a pathname. For
93 * normal files the system pathname is what is stored internally, but as
94 * #GFiles are extensible it could also be something else that corresponds
95 * to a pathname in a userspace implementation of a filesystem.
97 * #GFiles make up hierarchies of directories and files that correspond to
98 * the files on a filesystem. You can move through the file system with
99 * #GFile using g_file_get_parent() to get an identifier for the parent
100 * directory, g_file_get_child() to get a child within a directory,
101 * g_file_resolve_relative_path() to resolve a relative path between two
102 * #GFiles. There can be multiple hierarchies, so you may not end up at
103 * the same root if you repeatedly call g_file_get_parent() on two different
104 * files.
106 * All #GFiles have a basename (get with g_file_get_basename()). These names
107 * are byte strings that are used to identify the file on the filesystem
108 * (relative to its parent directory) and there is no guarantees that they
109 * have any particular charset encoding or even make any sense at all. If
110 * you want to use filenames in a user interface you should use the display
111 * name that you can get by requesting the
112 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
113 * This is guaranteed to be in UTF-8 and can be used in a user interface.
114 * But always store the real basename or the #GFile to use to actually
115 * access the file, because there is no way to go from a display name to
116 * the actual name.
118 * Using #GFile as an identifier has the same weaknesses as using a path
119 * in that there may be multiple aliases for the same file. For instance,
120 * hard or soft links may cause two different #GFiles to refer to the same
121 * file. Other possible causes for aliases are: case insensitive filesystems,
122 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
123 * check if two #GFiles point to the same file you can query for the
124 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
125 * canonicalization of pathnames passed in, so that trivial differences in
126 * the path string used at creation (duplicated slashes, slash at end of
127 * path, "." or ".." path segments, etc) does not create different #GFiles.
129 * Many #GFile operations have both synchronous and asynchronous versions
130 * to suit your application. Asynchronous versions of synchronous functions
131 * simply have _async() appended to their function names. The asynchronous
132 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
133 * the operation, producing a GAsyncResult which is then passed to the
134 * function's matching _finish() operation.
136 * Some #GFile operations do not have synchronous analogs, as they may
137 * take a very long time to finish, and blocking may leave an application
138 * unusable. Notable cases include:
139 * <simplelist>
140 * <member>g_file_mount_mountable() to mount a mountable file.</member>
141 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
142 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
143 * </simplelist>
145 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
146 * One notable feature of #GFiles are entity tags, or "etags" for
147 * short. Entity tags are somewhat like a more abstract version of the
148 * traditional mtime, and can be used to quickly determine if the file has
149 * been modified from the version on the file system. See the HTTP 1.1
150 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
151 * for HTTP Etag headers, which are a very similar concept.
152 * </para>
155 static void g_file_real_query_info_async (GFile *file,
156 const char *attributes,
157 GFileQueryInfoFlags flags,
158 int io_priority,
159 GCancellable *cancellable,
160 GAsyncReadyCallback callback,
161 gpointer user_data);
162 static GFileInfo * g_file_real_query_info_finish (GFile *file,
163 GAsyncResult *res,
164 GError **error);
165 static void g_file_real_query_filesystem_info_async (GFile *file,
166 const char *attributes,
167 int io_priority,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
170 gpointer user_data);
171 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
172 GAsyncResult *res,
173 GError **error);
174 static void g_file_real_enumerate_children_async (GFile *file,
175 const char *attributes,
176 GFileQueryInfoFlags flags,
177 int io_priority,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
180 gpointer user_data);
181 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
182 GAsyncResult *res,
183 GError **error);
184 static void g_file_real_read_async (GFile *file,
185 int io_priority,
186 GCancellable *cancellable,
187 GAsyncReadyCallback callback,
188 gpointer user_data);
189 static GFileInputStream * g_file_real_read_finish (GFile *file,
190 GAsyncResult *res,
191 GError **error);
192 static void g_file_real_append_to_async (GFile *file,
193 GFileCreateFlags flags,
194 int io_priority,
195 GCancellable *cancellable,
196 GAsyncReadyCallback callback,
197 gpointer user_data);
198 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
199 GAsyncResult *res,
200 GError **error);
201 static void g_file_real_create_async (GFile *file,
202 GFileCreateFlags flags,
203 int io_priority,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
206 gpointer user_data);
207 static GFileOutputStream *g_file_real_create_finish (GFile *file,
208 GAsyncResult *res,
209 GError **error);
210 static void g_file_real_replace_async (GFile *file,
211 const char *etag,
212 gboolean make_backup,
213 GFileCreateFlags flags,
214 int io_priority,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
217 gpointer user_data);
218 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
219 GAsyncResult *res,
220 GError **error);
221 static void g_file_real_delete_async (GFile *file,
222 int io_priority,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
225 gpointer user_data);
226 static gboolean g_file_real_delete_finish (GFile *file,
227 GAsyncResult *res,
228 GError **error);
229 static void g_file_real_trash_async (GFile *file,
230 int io_priority,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
233 gpointer user_data);
234 static gboolean g_file_real_trash_finish (GFile *file,
235 GAsyncResult *res,
236 GError **error);
237 static void g_file_real_make_directory_async (GFile *file,
238 int io_priority,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
241 gpointer user_data);
242 static gboolean g_file_real_make_directory_finish (GFile *file,
243 GAsyncResult *res,
244 GError **error);
245 static void g_file_real_open_readwrite_async (GFile *file,
246 int io_priority,
247 GCancellable *cancellable,
248 GAsyncReadyCallback callback,
249 gpointer user_data);
250 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
251 GAsyncResult *res,
252 GError **error);
253 static void g_file_real_create_readwrite_async (GFile *file,
254 GFileCreateFlags flags,
255 int io_priority,
256 GCancellable *cancellable,
257 GAsyncReadyCallback callback,
258 gpointer user_data);
259 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
260 GAsyncResult *res,
261 GError **error);
262 static void g_file_real_replace_readwrite_async (GFile *file,
263 const char *etag,
264 gboolean make_backup,
265 GFileCreateFlags flags,
266 int io_priority,
267 GCancellable *cancellable,
268 GAsyncReadyCallback callback,
269 gpointer user_data);
270 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
271 GAsyncResult *res,
272 GError **error);
273 static gboolean g_file_real_set_attributes_from_info (GFile *file,
274 GFileInfo *info,
275 GFileQueryInfoFlags flags,
276 GCancellable *cancellable,
277 GError **error);
278 static void g_file_real_set_display_name_async (GFile *file,
279 const char *display_name,
280 int io_priority,
281 GCancellable *cancellable,
282 GAsyncReadyCallback callback,
283 gpointer user_data);
284 static GFile * g_file_real_set_display_name_finish (GFile *file,
285 GAsyncResult *res,
286 GError **error);
287 static void g_file_real_set_attributes_async (GFile *file,
288 GFileInfo *info,
289 GFileQueryInfoFlags flags,
290 int io_priority,
291 GCancellable *cancellable,
292 GAsyncReadyCallback callback,
293 gpointer user_data);
294 static gboolean g_file_real_set_attributes_finish (GFile *file,
295 GAsyncResult *res,
296 GFileInfo **info,
297 GError **error);
298 static void g_file_real_find_enclosing_mount_async (GFile *file,
299 int io_priority,
300 GCancellable *cancellable,
301 GAsyncReadyCallback callback,
302 gpointer user_data);
303 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
304 GAsyncResult *res,
305 GError **error);
306 static void g_file_real_copy_async (GFile *source,
307 GFile *destination,
308 GFileCopyFlags flags,
309 int io_priority,
310 GCancellable *cancellable,
311 GFileProgressCallback progress_callback,
312 gpointer progress_callback_data,
313 GAsyncReadyCallback callback,
314 gpointer user_data);
315 static gboolean g_file_real_copy_finish (GFile *file,
316 GAsyncResult *res,
317 GError **error);
319 static gboolean g_file_real_measure_disk_usage (GFile *file,
320 GFileMeasureFlags flags,
321 GCancellable *cancellable,
322 GFileMeasureProgressCallback progress_callback,
323 gpointer progress_data,
324 guint64 *disk_usage,
325 guint64 *num_dirs,
326 guint64 *num_files,
327 GError **error);
328 static void g_file_real_measure_disk_usage_async (GFile *file,
329 GFileMeasureFlags flags,
330 gint io_priority,
331 GCancellable *cancellable,
332 GFileMeasureProgressCallback progress_callback,
333 gpointer progress_data,
334 GAsyncReadyCallback callback,
335 gpointer user_data);
336 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
337 GAsyncResult *result,
338 guint64 *disk_usage,
339 guint64 *num_dirs,
340 guint64 *num_files,
341 GError **error);
343 typedef GFileIface GFileInterface;
344 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
346 static void
347 g_file_default_init (GFileIface *iface)
349 iface->enumerate_children_async = g_file_real_enumerate_children_async;
350 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
351 iface->set_display_name_async = g_file_real_set_display_name_async;
352 iface->set_display_name_finish = g_file_real_set_display_name_finish;
353 iface->query_info_async = g_file_real_query_info_async;
354 iface->query_info_finish = g_file_real_query_info_finish;
355 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
356 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
357 iface->set_attributes_async = g_file_real_set_attributes_async;
358 iface->set_attributes_finish = g_file_real_set_attributes_finish;
359 iface->read_async = g_file_real_read_async;
360 iface->read_finish = g_file_real_read_finish;
361 iface->append_to_async = g_file_real_append_to_async;
362 iface->append_to_finish = g_file_real_append_to_finish;
363 iface->create_async = g_file_real_create_async;
364 iface->create_finish = g_file_real_create_finish;
365 iface->replace_async = g_file_real_replace_async;
366 iface->replace_finish = g_file_real_replace_finish;
367 iface->delete_file_async = g_file_real_delete_async;
368 iface->delete_file_finish = g_file_real_delete_finish;
369 iface->trash_async = g_file_real_trash_async;
370 iface->trash_finish = g_file_real_trash_finish;
371 iface->make_directory_async = g_file_real_make_directory_async;
372 iface->make_directory_finish = g_file_real_make_directory_finish;
373 iface->open_readwrite_async = g_file_real_open_readwrite_async;
374 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
375 iface->create_readwrite_async = g_file_real_create_readwrite_async;
376 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
377 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
378 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
379 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
380 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
381 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
382 iface->copy_async = g_file_real_copy_async;
383 iface->copy_finish = g_file_real_copy_finish;
384 iface->measure_disk_usage = g_file_real_measure_disk_usage;
385 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
386 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
391 * g_file_is_native:
392 * @file: input #GFile
394 * Checks to see if a file is native to the platform.
396 * A native file s one expressed in the platform-native filename format,
397 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
398 * as it might be on a locally mounted remote filesystem.
400 * On some systems non-native files may be available using the native
401 * filesystem via a userspace filesystem (FUSE), in these cases this call
402 * will return %FALSE, but g_file_get_path() will still return a native path.
404 * This call does no blocking I/O.
406 * Returns: %TRUE if @file is native
408 gboolean
409 g_file_is_native (GFile *file)
411 GFileIface *iface;
413 g_return_val_if_fail (G_IS_FILE (file), FALSE);
415 iface = G_FILE_GET_IFACE (file);
417 return (* iface->is_native) (file);
422 * g_file_has_uri_scheme:
423 * @file: input #GFile
424 * @uri_scheme: a string containing a URI scheme
426 * Checks to see if a #GFile has a given URI scheme.
428 * This call does no blocking I/O.
430 * Returns: %TRUE if #GFile's backend supports the
431 * given URI scheme, %FALSE if URI scheme is %NULL,
432 * not supported, or #GFile is invalid.
434 gboolean
435 g_file_has_uri_scheme (GFile *file,
436 const char *uri_scheme)
438 GFileIface *iface;
440 g_return_val_if_fail (G_IS_FILE (file), FALSE);
441 g_return_val_if_fail (uri_scheme != NULL, FALSE);
443 iface = G_FILE_GET_IFACE (file);
445 return (* iface->has_uri_scheme) (file, uri_scheme);
450 * g_file_get_uri_scheme:
451 * @file: input #GFile
453 * Gets the URI scheme for a #GFile.
454 * RFC 3986 decodes the scheme as:
455 * <programlisting>
456 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
457 * </programlisting>
458 * Common schemes include "file", "http", "ftp", etc.
460 * This call does no blocking I/O.
462 * Returns: a string containing the URI scheme for the given
463 * #GFile. The returned string should be freed with g_free()
464 * when no longer needed.
466 char *
467 g_file_get_uri_scheme (GFile *file)
469 GFileIface *iface;
471 g_return_val_if_fail (G_IS_FILE (file), NULL);
473 iface = G_FILE_GET_IFACE (file);
475 return (* iface->get_uri_scheme) (file);
480 * g_file_get_basename:
481 * @file: input #GFile
483 * Gets the base name (the last component of the path) for a given #GFile.
485 * If called for the top level of a system (such as the filesystem root
486 * or a uri like sftp://host/) it will return a single directory separator
487 * (and on Windows, possibly a drive letter).
489 * The base name is a byte string (not UTF-8). It has no defined encoding
490 * or rules other than it may not contain zero bytes. If you want to use
491 * filenames in a user interface you should use the display name that you
492 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
493 * attribute with g_file_query_info().
495 * This call does no blocking I/O.
497 * Returns: string containing the #GFile's base name, or %NULL
498 * if given #GFile is invalid. The returned string should be
499 * freed with g_free() when no longer needed.
501 char *
502 g_file_get_basename (GFile *file)
504 GFileIface *iface;
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_basename) (file);
514 * g_file_get_path:
515 * @file: input #GFile
517 * Gets the local pathname for #GFile, if one exists.
519 * This call does no blocking I/O.
521 * Returns: string containing the #GFile's path, or %NULL if
522 * no such path exists. The returned string should be
523 * freed with g_free() when no longer needed.
525 char *
526 g_file_get_path (GFile *file)
528 GFileIface *iface;
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
538 * g_file_get_uri:
539 * @file: input #GFile
541 * Gets the URI for the @file.
543 * This call does no blocking I/O.
545 * Returns: a string containing the #GFile's URI.
546 * The returned string should be freed with g_free()
547 * when no longer needed.
549 char *
550 g_file_get_uri (GFile *file)
552 GFileIface *iface;
554 g_return_val_if_fail (G_IS_FILE (file), NULL);
556 iface = G_FILE_GET_IFACE (file);
558 return (* iface->get_uri) (file);
562 * g_file_get_parse_name:
563 * @file: input #GFile
565 * Gets the parse name of the @file.
566 * A parse name is a UTF-8 string that describes the
567 * file such that one can get the #GFile back using
568 * g_file_parse_name().
570 * This is generally used to show the #GFile as a nice
571 * full-pathname kind of string in a user interface,
572 * like in a location entry.
574 * For local files with names that can safely be converted
575 * to UTF-8 the pathname is used, otherwise the IRI is used
576 * (a form of URI that allows UTF-8 characters unescaped).
578 * This call does no blocking I/O.
580 * Returns: a string containing the #GFile's parse name.
581 * The returned string should be freed with g_free()
582 * when no longer needed.
584 char *
585 g_file_get_parse_name (GFile *file)
587 GFileIface *iface;
589 g_return_val_if_fail (G_IS_FILE (file), NULL);
591 iface = G_FILE_GET_IFACE (file);
593 return (* iface->get_parse_name) (file);
597 * g_file_dup:
598 * @file: input #GFile
600 * Duplicates a #GFile handle. This operation does not duplicate
601 * the actual file or directory represented by the #GFile; see
602 * g_file_copy() if attempting to copy a file.
604 * This call does no blocking I/O.
606 * Returns: (transfer full): a new #GFile that is a duplicate
607 * of the given #GFile.
609 GFile *
610 g_file_dup (GFile *file)
612 GFileIface *iface;
614 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 iface = G_FILE_GET_IFACE (file);
618 return (* iface->dup) (file);
622 * g_file_hash:
623 * @file: (type GFile): #gconstpointer to a #GFile
625 * Creates a hash value for a #GFile.
627 * This call does no blocking I/O.
629 * Virtual: hash
630 * Returns: 0 if @file is not a valid #GFile, otherwise an
631 * integer that can be used as hash value for the #GFile.
632 * This function is intended for easily hashing a #GFile to
633 * add to a #GHashTable or similar data structure.
635 guint
636 g_file_hash (gconstpointer file)
638 GFileIface *iface;
640 g_return_val_if_fail (G_IS_FILE (file), 0);
642 iface = G_FILE_GET_IFACE (file);
644 return (* iface->hash) ((GFile *)file);
648 * g_file_equal:
649 * @file1: the first #GFile
650 * @file2: the second #GFile
652 * Checks equality of two given #GFiles.
654 * Note that two #GFiles that differ can still refer to the same
655 * file on the filesystem due to various forms of filename
656 * aliasing.
658 * This call does no blocking I/O.
660 * Returns: %TRUE if @file1 and @file2 are equal.
661 * %FALSE if either is not a #GFile.
663 gboolean
664 g_file_equal (GFile *file1,
665 GFile *file2)
667 GFileIface *iface;
669 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
670 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
672 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
673 return FALSE;
675 iface = G_FILE_GET_IFACE (file1);
677 return (* iface->equal) (file1, file2);
682 * g_file_get_parent:
683 * @file: input #GFile
685 * Gets the parent directory for the @file.
686 * If the @file represents the root directory of the
687 * file system, then %NULL will be returned.
689 * This call does no blocking I/O.
691 * Returns: (transfer full): a #GFile structure to the
692 * parent of the given #GFile or %NULL if there is
693 * no parent. Free the returned object with g_object_unref().
695 GFile *
696 g_file_get_parent (GFile *file)
698 GFileIface *iface;
700 g_return_val_if_fail (G_IS_FILE (file), NULL);
702 iface = G_FILE_GET_IFACE (file);
704 return (* iface->get_parent) (file);
708 * g_file_has_parent:
709 * @file: input #GFile
710 * @parent: (allow-none): the parent to check for, or %NULL
712 * Checks if @file has a parent, and optionally, if it is @parent.
714 * If @parent is %NULL then this function returns %TRUE if @file has any
715 * parent at all. If @parent is non-%NULL then %TRUE is only returned
716 * if @file is a child of @parent.
718 * Returns: %TRUE if @file is a child of @parent (or any parent in the
719 * case that @parent is %NULL).
721 * Since: 2.24
723 gboolean
724 g_file_has_parent (GFile *file,
725 GFile *parent)
727 GFile *actual_parent;
728 gboolean result;
730 g_return_val_if_fail (G_IS_FILE (file), FALSE);
731 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
733 actual_parent = g_file_get_parent (file);
735 if (actual_parent != NULL)
737 if (parent != NULL)
738 result = g_file_equal (parent, actual_parent);
739 else
740 result = TRUE;
742 g_object_unref (actual_parent);
744 else
745 result = FALSE;
747 return result;
751 * g_file_get_child:
752 * @file: input #GFile
753 * @name: string containing the child's basename
755 * Gets a child of @file with basename equal to @name.
757 * Note that the file with that specific name might not exist, but
758 * you can still have a #GFile that points to it. You can use this
759 * for instance to create that file.
761 * This call does no blocking I/O.
763 * Returns: (transfer full): a #GFile to a child specified by @name.
764 * Free the returned object with g_object_unref().
766 GFile *
767 g_file_get_child (GFile *file,
768 const char *name)
770 g_return_val_if_fail (G_IS_FILE (file), NULL);
771 g_return_val_if_fail (name != NULL, NULL);
773 return g_file_resolve_relative_path (file, name);
777 * g_file_get_child_for_display_name:
778 * @file: input #GFile
779 * @display_name: string to a possible child
780 * @error: return location for an error
782 * Gets the child of @file for a given @display_name (i.e. a UTF-8
783 * version of the name). If this function fails, it returns %NULL
784 * and @error will be set. This is very useful when constructing a
785 * #GFile for a new file and the user entered the filename in the
786 * user interface, for instance when you select a directory and
787 * type a filename in the file selector.
789 * This call does no blocking I/O.
791 * Returns: (transfer full): a #GFile to the specified child, or
792 * %NULL if the display name couldn't be converted.
793 * Free the returned object with g_object_unref().
795 GFile *
796 g_file_get_child_for_display_name (GFile *file,
797 const char *display_name,
798 GError **error)
800 GFileIface *iface;
802 g_return_val_if_fail (G_IS_FILE (file), NULL);
803 g_return_val_if_fail (display_name != NULL, NULL);
805 iface = G_FILE_GET_IFACE (file);
807 return (* iface->get_child_for_display_name) (file, display_name, error);
811 * g_file_has_prefix:
812 * @file: input #GFile
813 * @prefix: input #GFile
815 * Checks whether @file has the prefix specified by @prefix.
817 * In other words, if the names of initial elements of @file's
818 * pathname match @prefix. Only full pathname elements are matched,
819 * so a path like /foo is not considered a prefix of /foobar, only
820 * of /foo/bar.
822 * This call does no I/O, as it works purely on names. As such it can
823 * sometimes return %FALSE even if @file is inside a @prefix (from a
824 * filesystem point of view), because the prefix of @file is an alias
825 * of @prefix.
827 * Virtual: prefix_matches
828 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
829 * %FALSE otherwise.
831 gboolean
832 g_file_has_prefix (GFile *file,
833 GFile *prefix)
835 GFileIface *iface;
837 g_return_val_if_fail (G_IS_FILE (file), FALSE);
838 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
840 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
841 return FALSE;
843 iface = G_FILE_GET_IFACE (file);
845 /* The vtable function differs in arg order since
846 * we're using the old contains_file call
848 return (* iface->prefix_matches) (prefix, file);
852 * g_file_get_relative_path:
853 * @parent: input #GFile
854 * @descendant: input #GFile
856 * Gets the path for @descendant relative to @parent.
858 * This call does no blocking I/O.
860 * Returns: string with the relative path from @descendant
861 * to @parent, or %NULL if @descendant doesn't have @parent
862 * as prefix. The returned string should be freed with g_free()
863 * when no longer needed.
865 char *
866 g_file_get_relative_path (GFile *parent,
867 GFile *descendant)
869 GFileIface *iface;
871 g_return_val_if_fail (G_IS_FILE (parent), NULL);
872 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
874 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
875 return NULL;
877 iface = G_FILE_GET_IFACE (parent);
879 return (* iface->get_relative_path) (parent, descendant);
883 * g_file_resolve_relative_path:
884 * @file: input #GFile
885 * @relative_path: a given relative path string
887 * Resolves a relative path for @file to an absolute path.
889 * This call does no blocking I/O.
891 * Returns: (transfer full): #GFile to the resolved path.
892 * %NULL if @relative_path is %NULL or if @file is invalid.
893 * Free the returned object with g_object_unref().
895 GFile *
896 g_file_resolve_relative_path (GFile *file,
897 const char *relative_path)
899 GFileIface *iface;
901 g_return_val_if_fail (G_IS_FILE (file), NULL);
902 g_return_val_if_fail (relative_path != NULL, NULL);
904 iface = G_FILE_GET_IFACE (file);
906 return (* iface->resolve_relative_path) (file, relative_path);
910 * g_file_enumerate_children:
911 * @file: input #GFile
912 * @attributes: an attribute query string
913 * @flags: a set of #GFileQueryInfoFlags
914 * @cancellable: (allow-none): optional #GCancellable object,
915 * %NULL to ignore
916 * @error: #GError for error reporting
918 * Gets the requested information about the files in a directory.
919 * The result is a #GFileEnumerator object that will give out
920 * #GFileInfo objects for all the files in the directory.
922 * The @attributes value is a string that specifies the file
923 * attributes that should be gathered. It is not an error if
924 * it's not possible to read a particular requested attribute
925 * from a file - it just won't be set. @attributes should
926 * be a comma-separated list of attributes or attribute wildcards.
927 * The wildcard "*" means all attributes, and a wildcard like
928 * "standard::*" means all attributes in the standard namespace.
929 * An example attribute query be "standard::*,owner::user".
930 * The standard attributes are available as defines, like
931 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
933 * If @cancellable is not %NULL, then the operation can be cancelled
934 * by triggering the cancellable object from another thread. If the
935 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
936 * returned.
938 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
939 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
940 * error will be returned. Other errors are possible too.
942 * Returns: (transfer full): A #GFileEnumerator if successful,
943 * %NULL on error. Free the returned object with g_object_unref().
945 GFileEnumerator *
946 g_file_enumerate_children (GFile *file,
947 const char *attributes,
948 GFileQueryInfoFlags flags,
949 GCancellable *cancellable,
950 GError **error)
952 GFileIface *iface;
954 g_return_val_if_fail (G_IS_FILE (file), NULL);
956 if (g_cancellable_set_error_if_cancelled (cancellable, error))
957 return NULL;
959 iface = G_FILE_GET_IFACE (file);
961 if (iface->enumerate_children == NULL)
963 g_set_error_literal (error, G_IO_ERROR,
964 G_IO_ERROR_NOT_SUPPORTED,
965 _("Operation not supported"));
966 return NULL;
969 return (* iface->enumerate_children) (file, attributes, flags,
970 cancellable, error);
974 * g_file_enumerate_children_async:
975 * @file: input #GFile
976 * @attributes: an attribute query string
977 * @flags: a set of #GFileQueryInfoFlags
978 * @io_priority: the <link linkend="io-priority">I/O priority</link>
979 * of the request
980 * @cancellable: (allow-none): optional #GCancellable object,
981 * %NULL to ignore
982 * @callback: (scope async): a #GAsyncReadyCallback to call when the
983 * request is satisfied
984 * @user_data: (closure): the data to pass to callback function
986 * Asynchronously gets the requested information about the files
987 * in a directory. The result is a #GFileEnumerator object that will
988 * give out #GFileInfo objects for all the files in the directory.
990 * For more details, see g_file_enumerate_children() which is
991 * the synchronous version of this call.
993 * When the operation is finished, @callback will be called. You can
994 * then call g_file_enumerate_children_finish() to get the result of
995 * the operation.
997 void
998 g_file_enumerate_children_async (GFile *file,
999 const char *attributes,
1000 GFileQueryInfoFlags flags,
1001 int io_priority,
1002 GCancellable *cancellable,
1003 GAsyncReadyCallback callback,
1004 gpointer user_data)
1006 GFileIface *iface;
1008 g_return_if_fail (G_IS_FILE (file));
1010 iface = G_FILE_GET_IFACE (file);
1011 (* iface->enumerate_children_async) (file,
1012 attributes,
1013 flags,
1014 io_priority,
1015 cancellable,
1016 callback,
1017 user_data);
1021 * g_file_enumerate_children_finish:
1022 * @file: input #GFile
1023 * @res: a #GAsyncResult
1024 * @error: a #GError
1026 * Finishes an async enumerate children operation.
1027 * See g_file_enumerate_children_async().
1029 * Returns: (transfer full): a #GFileEnumerator or %NULL
1030 * if an error occurred.
1031 * Free the returned object with g_object_unref().
1033 GFileEnumerator *
1034 g_file_enumerate_children_finish (GFile *file,
1035 GAsyncResult *res,
1036 GError **error)
1038 GFileIface *iface;
1040 g_return_val_if_fail (G_IS_FILE (file), NULL);
1041 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1043 if (g_async_result_legacy_propagate_error (res, error))
1044 return NULL;
1046 iface = G_FILE_GET_IFACE (file);
1047 return (* iface->enumerate_children_finish) (file, res, error);
1051 * g_file_query_exists:
1052 * @file: input #GFile
1053 * @cancellable: (allow-none): optional #GCancellable object,
1054 * %NULL to ignore
1056 * Utility function to check if a particular file exists. This is
1057 * implemented using g_file_query_info() and as such does blocking I/O.
1059 * Note that in many cases it is racy to first check for file existence
1060 * and then execute something based on the outcome of that, because the
1061 * file might have been created or removed in between the operations. The
1062 * general approach to handling that is to not check, but just do the
1063 * operation and handle the errors as they come.
1065 * As an example of race-free checking, take the case of reading a file,
1066 * and if it doesn't exist, creating it. There are two racy versions: read
1067 * it, and on error create it; and: check if it exists, if not create it.
1068 * These can both result in two processes creating the file (with perhaps
1069 * a partially written file as the result). The correct approach is to
1070 * always try to create the file with g_file_create() which will either
1071 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1073 * However, in many cases an existence check is useful in a user interface,
1074 * for instance to make a menu item sensitive/insensitive, so that you don't
1075 * have to fool users that something is possible and then just show an error
1076 * dialog. If you do this, you should make sure to also handle the errors
1077 * that can happen due to races when you execute the operation.
1079 * Returns: %TRUE if the file exists (and can be detected without error),
1080 * %FALSE otherwise (or if cancelled).
1082 gboolean
1083 g_file_query_exists (GFile *file,
1084 GCancellable *cancellable)
1086 GFileInfo *info;
1088 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1090 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1091 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1092 if (info != NULL)
1094 g_object_unref (info);
1095 return TRUE;
1098 return FALSE;
1102 * g_file_query_file_type:
1103 * @file: input #GFile
1104 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1105 * @cancellable: (allow-none): optional #GCancellable object,
1106 * %NULL to ignore
1108 * Utility function to inspect the #GFileType of a file. This is
1109 * implemented using g_file_query_info() and as such does blocking I/O.
1111 * The primary use case of this method is to check if a file is
1112 * a regular file, directory, or symlink.
1114 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1115 * if the file does not exist
1117 * Since: 2.18
1119 GFileType
1120 g_file_query_file_type (GFile *file,
1121 GFileQueryInfoFlags flags,
1122 GCancellable *cancellable)
1124 GFileInfo *info;
1125 GFileType file_type;
1127 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1128 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1129 cancellable, NULL);
1130 if (info != NULL)
1132 file_type = g_file_info_get_file_type (info);
1133 g_object_unref (info);
1135 else
1136 file_type = G_FILE_TYPE_UNKNOWN;
1138 return file_type;
1142 * g_file_query_info:
1143 * @file: input #GFile
1144 * @attributes: an attribute query string
1145 * @flags: a set of #GFileQueryInfoFlags
1146 * @cancellable: (allow-none): optional #GCancellable object,
1147 * %NULL to ignore
1148 * @error: a #GError
1150 * Gets the requested information about specified @file.
1151 * The result is a #GFileInfo object that contains key-value
1152 * attributes (such as the type or size of the file).
1154 * The @attributes value is a string that specifies the file
1155 * attributes that should be gathered. It is not an error if
1156 * it's not possible to read a particular requested attribute
1157 * from a file - it just won't be set. @attributes should be a
1158 * comma-separated list of attributes or attribute wildcards.
1159 * The wildcard "*" means all attributes, and a wildcard like
1160 * "standard::*" means all attributes in the standard namespace.
1161 * An example attribute query be "standard::*,owner::user".
1162 * The standard attributes are available as defines, like
1163 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1165 * If @cancellable is not %NULL, then the operation can be cancelled
1166 * by triggering the cancellable object from another thread. If the
1167 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1168 * returned.
1170 * For symlinks, normally the information about the target of the
1171 * symlink is returned, rather than information about the symlink
1172 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1173 * in @flags the information about the symlink itself will be returned.
1174 * Also, for symlinks that point to non-existing files the information
1175 * about the symlink itself will be returned.
1177 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1178 * returned. Other errors are possible too, and depend on what kind of
1179 * filesystem the file is on.
1181 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1182 * on error. Free the returned object with g_object_unref().
1184 GFileInfo *
1185 g_file_query_info (GFile *file,
1186 const char *attributes,
1187 GFileQueryInfoFlags flags,
1188 GCancellable *cancellable,
1189 GError **error)
1191 GFileIface *iface;
1193 g_return_val_if_fail (G_IS_FILE (file), NULL);
1195 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1196 return NULL;
1198 iface = G_FILE_GET_IFACE (file);
1200 if (iface->query_info == NULL)
1202 g_set_error_literal (error, G_IO_ERROR,
1203 G_IO_ERROR_NOT_SUPPORTED,
1204 _("Operation not supported"));
1205 return NULL;
1208 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1212 * g_file_query_info_async:
1213 * @file: input #GFile
1214 * @attributes: an attribute query string
1215 * @flags: a set of #GFileQueryInfoFlags
1216 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1217 * of the request
1218 * @cancellable: (allow-none): optional #GCancellable object,
1219 * %NULL to ignore
1220 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1221 * request is satisfied
1222 * @user_data: (closure): the data to pass to callback function
1224 * Asynchronously gets the requested information about specified @file.
1225 * The result is a #GFileInfo object that contains key-value attributes
1226 * (such as type or size for the file).
1228 * For more details, see g_file_query_info() which is the synchronous
1229 * version of this call.
1231 * When the operation is finished, @callback will be called. You can
1232 * then call g_file_query_info_finish() to get the result of the operation.
1234 void
1235 g_file_query_info_async (GFile *file,
1236 const char *attributes,
1237 GFileQueryInfoFlags flags,
1238 int io_priority,
1239 GCancellable *cancellable,
1240 GAsyncReadyCallback callback,
1241 gpointer user_data)
1243 GFileIface *iface;
1245 g_return_if_fail (G_IS_FILE (file));
1247 iface = G_FILE_GET_IFACE (file);
1248 (* iface->query_info_async) (file,
1249 attributes,
1250 flags,
1251 io_priority,
1252 cancellable,
1253 callback,
1254 user_data);
1258 * g_file_query_info_finish:
1259 * @file: input #GFile
1260 * @res: a #GAsyncResult
1261 * @error: a #GError
1263 * Finishes an asynchronous file info query.
1264 * See g_file_query_info_async().
1266 * Returns: (transfer full): #GFileInfo for given @file
1267 * or %NULL on error. Free the returned object with
1268 * g_object_unref().
1270 GFileInfo *
1271 g_file_query_info_finish (GFile *file,
1272 GAsyncResult *res,
1273 GError **error)
1275 GFileIface *iface;
1277 g_return_val_if_fail (G_IS_FILE (file), NULL);
1278 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1280 if (g_async_result_legacy_propagate_error (res, error))
1281 return NULL;
1283 iface = G_FILE_GET_IFACE (file);
1284 return (* iface->query_info_finish) (file, res, error);
1288 * g_file_query_filesystem_info:
1289 * @file: input #GFile
1290 * @attributes: an attribute query string
1291 * @cancellable: (allow-none): optional #GCancellable object,
1292 * %NULL to ignore
1293 * @error: a #GError
1295 * Similar to g_file_query_info(), but obtains information
1296 * about the filesystem the @file is on, rather than the file itself.
1297 * For instance the amount of space available and the type of
1298 * the filesystem.
1300 * The @attributes value is a string that specifies the attributes
1301 * that should be gathered. It is not an error if it's not possible
1302 * to read a particular requested attribute from a file - it just
1303 * won't be set. @attributes should be a comma-separated list of
1304 * attributes or attribute wildcards. The wildcard "*" means all
1305 * attributes, and a wildcard like "filesystem::*" means all attributes
1306 * in the filesystem namespace. The standard namespace for filesystem
1307 * attributes is "filesystem". Common attributes of interest are
1308 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1309 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1310 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1312 * If @cancellable is not %NULL, then the operation can be cancelled
1313 * by triggering the cancellable object from another thread. If the
1314 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1315 * returned.
1317 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1318 * be returned. Other errors are possible too, and depend on what
1319 * kind of filesystem the file is on.
1321 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1322 * Free the returned object with g_object_unref().
1324 GFileInfo *
1325 g_file_query_filesystem_info (GFile *file,
1326 const char *attributes,
1327 GCancellable *cancellable,
1328 GError **error)
1330 GFileIface *iface;
1332 g_return_val_if_fail (G_IS_FILE (file), NULL);
1334 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1335 return NULL;
1337 iface = G_FILE_GET_IFACE (file);
1339 if (iface->query_filesystem_info == NULL)
1341 g_set_error_literal (error, G_IO_ERROR,
1342 G_IO_ERROR_NOT_SUPPORTED,
1343 _("Operation not supported"));
1344 return NULL;
1347 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1351 * g_file_query_filesystem_info_async:
1352 * @file: input #GFile
1353 * @attributes: an attribute query string
1354 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1355 * of the request
1356 * @cancellable: (allow-none): optional #GCancellable object,
1357 * %NULL to ignore
1358 * @callback: (scope async): a #GAsyncReadyCallback to call
1359 * when the request is satisfied
1360 * @user_data: (closure): the data to pass to callback function
1362 * Asynchronously gets the requested information about the filesystem
1363 * that the specified @file is on. The result is a #GFileInfo object
1364 * that contains key-value attributes (such as type or size for the
1365 * file).
1367 * For more details, see g_file_query_filesystem_info() which is the
1368 * synchronous version of this call.
1370 * When the operation is finished, @callback will be called. You can
1371 * then call g_file_query_info_finish() to get the result of the
1372 * operation.
1374 void
1375 g_file_query_filesystem_info_async (GFile *file,
1376 const char *attributes,
1377 int io_priority,
1378 GCancellable *cancellable,
1379 GAsyncReadyCallback callback,
1380 gpointer user_data)
1382 GFileIface *iface;
1384 g_return_if_fail (G_IS_FILE (file));
1386 iface = G_FILE_GET_IFACE (file);
1387 (* iface->query_filesystem_info_async) (file,
1388 attributes,
1389 io_priority,
1390 cancellable,
1391 callback,
1392 user_data);
1396 * g_file_query_filesystem_info_finish:
1397 * @file: input #GFile
1398 * @res: a #GAsyncResult
1399 * @error: a #GError
1401 * Finishes an asynchronous filesystem info query.
1402 * See g_file_query_filesystem_info_async().
1404 * Returns: (transfer full): #GFileInfo for given @file
1405 * or %NULL on error.
1406 * Free the returned object with g_object_unref().
1408 GFileInfo *
1409 g_file_query_filesystem_info_finish (GFile *file,
1410 GAsyncResult *res,
1411 GError **error)
1413 GFileIface *iface;
1415 g_return_val_if_fail (G_IS_FILE (file), NULL);
1416 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1418 if (g_async_result_legacy_propagate_error (res, error))
1419 return NULL;
1421 iface = G_FILE_GET_IFACE (file);
1422 return (* iface->query_filesystem_info_finish) (file, res, error);
1426 * g_file_find_enclosing_mount:
1427 * @file: input #GFile
1428 * @cancellable: (allow-none): optional #GCancellable object,
1429 * %NULL to ignore
1430 * @error: a #GError
1432 * Gets a #GMount for the #GFile.
1434 * If the #GFileIface for @file does not have a mount (e.g.
1435 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1436 * and %NULL will be returned.
1438 * If @cancellable is not %NULL, then the operation can be cancelled by
1439 * triggering the cancellable object from another thread. If the operation
1440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1442 * Returns: (transfer full): a #GMount where the @file is located
1443 * or %NULL on error.
1444 * Free the returned object with g_object_unref().
1446 GMount *
1447 g_file_find_enclosing_mount (GFile *file,
1448 GCancellable *cancellable,
1449 GError **error)
1451 GFileIface *iface;
1453 g_return_val_if_fail (G_IS_FILE (file), NULL);
1455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1456 return NULL;
1458 iface = G_FILE_GET_IFACE (file);
1459 if (iface->find_enclosing_mount == NULL)
1462 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1463 /* Translators: This is an error message when
1464 * trying to find the enclosing (user visible)
1465 * mount of a file, but none exists.
1467 _("Containing mount does not exist"));
1468 return NULL;
1471 return (* iface->find_enclosing_mount) (file, cancellable, error);
1475 * g_file_find_enclosing_mount_async:
1476 * @file: a #GFile
1477 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1478 * of the request
1479 * @cancellable: (allow-none): optional #GCancellable object,
1480 * %NULL to ignore
1481 * @callback: (scope async): a #GAsyncReadyCallback to call
1482 * when the request is satisfied
1483 * @user_data: (closure): the data to pass to callback function
1485 * Asynchronously gets the mount for the file.
1487 * For more details, see g_file_find_enclosing_mount() which is
1488 * the synchronous version of this call.
1490 * When the operation is finished, @callback will be called.
1491 * You can then call g_file_find_enclosing_mount_finish() to
1492 * get the result of the operation.
1494 void
1495 g_file_find_enclosing_mount_async (GFile *file,
1496 int io_priority,
1497 GCancellable *cancellable,
1498 GAsyncReadyCallback callback,
1499 gpointer user_data)
1501 GFileIface *iface;
1503 g_return_if_fail (G_IS_FILE (file));
1505 iface = G_FILE_GET_IFACE (file);
1506 (* iface->find_enclosing_mount_async) (file,
1507 io_priority,
1508 cancellable,
1509 callback,
1510 user_data);
1514 * g_file_find_enclosing_mount_finish:
1515 * @file: a #GFile
1516 * @res: a #GAsyncResult
1517 * @error: a #GError
1519 * Finishes an asynchronous find mount request.
1520 * See g_file_find_enclosing_mount_async().
1522 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1523 * Free the returned object with g_object_unref().
1525 GMount *
1526 g_file_find_enclosing_mount_finish (GFile *file,
1527 GAsyncResult *res,
1528 GError **error)
1530 GFileIface *iface;
1532 g_return_val_if_fail (G_IS_FILE (file), NULL);
1533 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1535 if (g_async_result_legacy_propagate_error (res, error))
1536 return NULL;
1538 iface = G_FILE_GET_IFACE (file);
1539 return (* iface->find_enclosing_mount_finish) (file, res, error);
1544 * g_file_read:
1545 * @file: #GFile to read
1546 * @cancellable: (allow-none): a #GCancellable
1547 * @error: a #GError, or %NULL
1549 * Opens a file for reading. The result is a #GFileInputStream that
1550 * can be used to read the contents of the file.
1552 * If @cancellable is not %NULL, then the operation can be cancelled by
1553 * triggering the cancellable object from another thread. If the operation
1554 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1556 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1557 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1558 * error will be returned. Other errors are possible too, and depend
1559 * on what kind of filesystem the file is on.
1561 * Virtual: read_fn
1562 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1563 * Free the returned object with g_object_unref().
1565 GFileInputStream *
1566 g_file_read (GFile *file,
1567 GCancellable *cancellable,
1568 GError **error)
1570 GFileIface *iface;
1572 g_return_val_if_fail (G_IS_FILE (file), NULL);
1574 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1575 return NULL;
1577 iface = G_FILE_GET_IFACE (file);
1579 if (iface->read_fn == NULL)
1581 g_set_error_literal (error, G_IO_ERROR,
1582 G_IO_ERROR_NOT_SUPPORTED,
1583 _("Operation not supported"));
1584 return NULL;
1587 return (* iface->read_fn) (file, cancellable, error);
1591 * g_file_append_to:
1592 * @file: input #GFile
1593 * @flags: a set of #GFileCreateFlags
1594 * @cancellable: (allow-none): optional #GCancellable object,
1595 * %NULL to ignore
1596 * @error: a #GError, or %NULL
1598 * Gets an output stream for appending data to the file.
1599 * If the file doesn't already exist it is created.
1601 * By default files created are generally readable by everyone,
1602 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1603 * will be made readable only to the current user, to the level that
1604 * is supported on the target filesystem.
1606 * If @cancellable is not %NULL, then the operation can be cancelled
1607 * by triggering the cancellable object from another thread. If the
1608 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1609 * returned.
1611 * Some file systems don't allow all file names, and may return an
1612 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1613 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1614 * possible too, and depend on what kind of filesystem the file is on.
1616 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1617 * Free the returned object with g_object_unref().
1619 GFileOutputStream *
1620 g_file_append_to (GFile *file,
1621 GFileCreateFlags flags,
1622 GCancellable *cancellable,
1623 GError **error)
1625 GFileIface *iface;
1627 g_return_val_if_fail (G_IS_FILE (file), NULL);
1629 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1630 return NULL;
1632 iface = G_FILE_GET_IFACE (file);
1634 if (iface->append_to == NULL)
1636 g_set_error_literal (error, G_IO_ERROR,
1637 G_IO_ERROR_NOT_SUPPORTED,
1638 _("Operation not supported"));
1639 return NULL;
1642 return (* iface->append_to) (file, flags, cancellable, error);
1646 * g_file_create:
1647 * @file: input #GFile
1648 * @flags: a set of #GFileCreateFlags
1649 * @cancellable: (allow-none): optional #GCancellable object,
1650 * %NULL to ignore
1651 * @error: a #GError, or %NULL
1653 * Creates a new file and returns an output stream for writing to it.
1654 * The file must not already exist.
1656 * By default files created are generally readable by everyone,
1657 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1658 * will be made readable only to the current user, to the level
1659 * that is supported on the target filesystem.
1661 * If @cancellable is not %NULL, then the operation can be cancelled
1662 * by triggering the cancellable object from another thread. If the
1663 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1664 * returned.
1666 * If a file or directory with this name already exists the
1667 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1668 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1669 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1670 * be returned. Other errors are possible too, and depend on what kind
1671 * of filesystem the file is on.
1673 * Returns: (transfer full): a #GFileOutputStream for the newly created
1674 * file, or %NULL on error.
1675 * Free the returned object with g_object_unref().
1677 GFileOutputStream *
1678 g_file_create (GFile *file,
1679 GFileCreateFlags flags,
1680 GCancellable *cancellable,
1681 GError **error)
1683 GFileIface *iface;
1685 g_return_val_if_fail (G_IS_FILE (file), NULL);
1687 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1688 return NULL;
1690 iface = G_FILE_GET_IFACE (file);
1692 if (iface->create == NULL)
1694 g_set_error_literal (error, G_IO_ERROR,
1695 G_IO_ERROR_NOT_SUPPORTED,
1696 _("Operation not supported"));
1697 return NULL;
1700 return (* iface->create) (file, flags, cancellable, error);
1704 * g_file_replace:
1705 * @file: input #GFile
1706 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1707 * for the current #GFile, or #NULL to ignore
1708 * @make_backup: %TRUE if a backup should be created
1709 * @flags: a set of #GFileCreateFlags
1710 * @cancellable: (allow-none): optional #GCancellable object,
1711 * %NULL to ignore
1712 * @error: a #GError, or %NULL
1714 * Returns an output stream for overwriting the file, possibly
1715 * creating a backup copy of the file first. If the file doesn't exist,
1716 * it will be created.
1718 * This will try to replace the file in the safest way possible so
1719 * that any errors during the writing will not affect an already
1720 * existing copy of the file. For instance, for local files it
1721 * may write to a temporary file and then atomically rename over
1722 * the destination when the stream is closed.
1724 * By default files created are generally readable by everyone,
1725 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1726 * will be made readable only to the current user, to the level that
1727 * is supported on the target filesystem.
1729 * If @cancellable is not %NULL, then the operation can be cancelled
1730 * by triggering the cancellable object from another thread. If the
1731 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1732 * returned.
1734 * If you pass in a non-%NULL @etag value, then this value is
1735 * compared to the current entity tag of the file, and if they differ
1736 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1737 * that the file has been changed since you last read it. You can get
1738 * the new etag from g_file_output_stream_get_etag() after you've
1739 * finished writing and closed the #GFileOutputStream. When you load
1740 * a new file you can use g_file_input_stream_query_info() to get
1741 * the etag of the file.
1743 * If @make_backup is %TRUE, this function will attempt to make a
1744 * backup of the current file before overwriting it. If this fails
1745 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1746 * want to replace anyway, try again with @make_backup set to %FALSE.
1748 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1749 * be returned, and if the file is some other form of non-regular file
1750 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1751 * file systems don't allow all file names, and may return an
1752 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1753 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1754 * possible too, and depend on what kind of filesystem the file is on.
1756 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1757 * Free the returned object with g_object_unref().
1759 GFileOutputStream *
1760 g_file_replace (GFile *file,
1761 const char *etag,
1762 gboolean make_backup,
1763 GFileCreateFlags flags,
1764 GCancellable *cancellable,
1765 GError **error)
1767 GFileIface *iface;
1769 g_return_val_if_fail (G_IS_FILE (file), NULL);
1771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1772 return NULL;
1774 iface = G_FILE_GET_IFACE (file);
1776 if (iface->replace == NULL)
1778 g_set_error_literal (error, G_IO_ERROR,
1779 G_IO_ERROR_NOT_SUPPORTED,
1780 _("Operation not supported"));
1781 return NULL;
1784 /* Handle empty tag string as NULL in consistent way. */
1785 if (etag && *etag == 0)
1786 etag = NULL;
1788 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1792 * g_file_open_readwrite:
1793 * @file: #GFile to open
1794 * @cancellable: (allow-none): a #GCancellable
1795 * @error: a #GError, or %NULL
1797 * Opens an existing file for reading and writing. The result is
1798 * a #GFileIOStream that can be used to read and write the contents
1799 * of the file.
1801 * If @cancellable is not %NULL, then the operation can be cancelled
1802 * by triggering the cancellable object from another thread. If the
1803 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1804 * returned.
1806 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1807 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1808 * error will be returned. Other errors are possible too, and depend on
1809 * what kind of filesystem the file is on. Note that in many non-local
1810 * file cases read and write streams are not supported, so make sure you
1811 * really need to do read and write streaming, rather than just opening
1812 * for reading or writing.
1814 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1815 * Free the returned object with g_object_unref().
1817 * Since: 2.22
1819 GFileIOStream *
1820 g_file_open_readwrite (GFile *file,
1821 GCancellable *cancellable,
1822 GError **error)
1824 GFileIface *iface;
1826 g_return_val_if_fail (G_IS_FILE (file), NULL);
1828 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1829 return NULL;
1831 iface = G_FILE_GET_IFACE (file);
1833 if (iface->open_readwrite == NULL)
1835 g_set_error_literal (error, G_IO_ERROR,
1836 G_IO_ERROR_NOT_SUPPORTED,
1837 _("Operation not supported"));
1838 return NULL;
1841 return (* iface->open_readwrite) (file, cancellable, error);
1845 * g_file_create_readwrite:
1846 * @file: a #GFile
1847 * @flags: a set of #GFileCreateFlags
1848 * @cancellable: (allow-none): optional #GCancellable object,
1849 * %NULL to ignore
1850 * @error: return location for a #GError, or %NULL
1852 * Creates a new file and returns a stream for reading and
1853 * writing to it. The file must not already exist.
1855 * By default files created are generally readable by everyone,
1856 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1857 * will be made readable only to the current user, to the level
1858 * that is supported on the target filesystem.
1860 * If @cancellable is not %NULL, then the operation can be cancelled
1861 * by triggering the cancellable object from another thread. If the
1862 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1863 * returned.
1865 * If a file or directory with this name already exists, the
1866 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1867 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1868 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1869 * will be returned. Other errors are possible too, and depend on what
1870 * kind of filesystem the file is on.
1872 * Note that in many non-local file cases read and write streams are
1873 * not supported, so make sure you really need to do read and write
1874 * streaming, rather than just opening for reading or writing.
1876 * Returns: (transfer full): a #GFileIOStream for the newly created
1877 * file, or %NULL on error.
1878 * Free the returned object with g_object_unref().
1880 * Since: 2.22
1882 GFileIOStream *
1883 g_file_create_readwrite (GFile *file,
1884 GFileCreateFlags flags,
1885 GCancellable *cancellable,
1886 GError **error)
1888 GFileIface *iface;
1890 g_return_val_if_fail (G_IS_FILE (file), NULL);
1892 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1893 return NULL;
1895 iface = G_FILE_GET_IFACE (file);
1897 if (iface->create_readwrite == NULL)
1899 g_set_error_literal (error, G_IO_ERROR,
1900 G_IO_ERROR_NOT_SUPPORTED,
1901 _("Operation not supported"));
1902 return NULL;
1905 return (* iface->create_readwrite) (file, flags, cancellable, error);
1909 * g_file_replace_readwrite:
1910 * @file: a #GFile
1911 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1912 * for the current #GFile, or #NULL to ignore
1913 * @make_backup: %TRUE if a backup should be created
1914 * @flags: a set of #GFileCreateFlags
1915 * @cancellable: (allow-none): optional #GCancellable object,
1916 * %NULL to ignore
1917 * @error: return location for a #GError, or %NULL
1919 * Returns an output stream for overwriting the file in readwrite mode,
1920 * possibly creating a backup copy of the file first. If the file doesn't
1921 * exist, it will be created.
1923 * For details about the behaviour, see g_file_replace() which does the
1924 * same thing but returns an output stream only.
1926 * Note that in many non-local file cases read and write streams are not
1927 * supported, so make sure you really need to do read and write streaming,
1928 * rather than just opening for reading or writing.
1930 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1931 * Free the returned object with g_object_unref().
1933 * Since: 2.22
1935 GFileIOStream *
1936 g_file_replace_readwrite (GFile *file,
1937 const char *etag,
1938 gboolean make_backup,
1939 GFileCreateFlags flags,
1940 GCancellable *cancellable,
1941 GError **error)
1943 GFileIface *iface;
1945 g_return_val_if_fail (G_IS_FILE (file), NULL);
1947 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1948 return NULL;
1950 iface = G_FILE_GET_IFACE (file);
1952 if (iface->replace_readwrite == NULL)
1954 g_set_error_literal (error, G_IO_ERROR,
1955 G_IO_ERROR_NOT_SUPPORTED,
1956 _("Operation not supported"));
1957 return NULL;
1960 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1964 * g_file_read_async:
1965 * @file: input #GFile
1966 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1967 * of the request
1968 * @cancellable: (allow-none): optional #GCancellable object,
1969 * %NULL to ignore
1970 * @callback: (scope async): a #GAsyncReadyCallback to call
1971 * when the request is satisfied
1972 * @user_data: (closure): the data to pass to callback function
1974 * Asynchronously opens @file for reading.
1976 * For more details, see g_file_read() which is
1977 * the synchronous version of this call.
1979 * When the operation is finished, @callback will be called.
1980 * You can then call g_file_read_finish() to get the result
1981 * of the operation.
1983 void
1984 g_file_read_async (GFile *file,
1985 int io_priority,
1986 GCancellable *cancellable,
1987 GAsyncReadyCallback callback,
1988 gpointer user_data)
1990 GFileIface *iface;
1992 g_return_if_fail (G_IS_FILE (file));
1994 iface = G_FILE_GET_IFACE (file);
1995 (* iface->read_async) (file,
1996 io_priority,
1997 cancellable,
1998 callback,
1999 user_data);
2003 * g_file_read_finish:
2004 * @file: input #GFile
2005 * @res: a #GAsyncResult
2006 * @error: a #GError, or %NULL
2008 * Finishes an asynchronous file read operation started with
2009 * g_file_read_async().
2011 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2012 * Free the returned object with g_object_unref().
2014 GFileInputStream *
2015 g_file_read_finish (GFile *file,
2016 GAsyncResult *res,
2017 GError **error)
2019 GFileIface *iface;
2021 g_return_val_if_fail (G_IS_FILE (file), NULL);
2022 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2024 if (g_async_result_legacy_propagate_error (res, error))
2025 return NULL;
2027 iface = G_FILE_GET_IFACE (file);
2028 return (* iface->read_finish) (file, res, error);
2032 * g_file_append_to_async:
2033 * @file: input #GFile
2034 * @flags: a set of #GFileCreateFlags
2035 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2036 * of the request
2037 * @cancellable: (allow-none): optional #GCancellable object,
2038 * %NULL to ignore
2039 * @callback: (scope async): a #GAsyncReadyCallback to call
2040 * when the request is satisfied
2041 * @user_data: (closure): the data to pass to callback function
2043 * Asynchronously opens @file for appending.
2045 * For more details, see g_file_append_to() which is
2046 * the synchronous version of this call.
2048 * When the operation is finished, @callback will be called.
2049 * You can then call g_file_append_to_finish() to get the result
2050 * of the operation.
2052 void
2053 g_file_append_to_async (GFile *file,
2054 GFileCreateFlags flags,
2055 int io_priority,
2056 GCancellable *cancellable,
2057 GAsyncReadyCallback callback,
2058 gpointer user_data)
2060 GFileIface *iface;
2062 g_return_if_fail (G_IS_FILE (file));
2064 iface = G_FILE_GET_IFACE (file);
2065 (* iface->append_to_async) (file,
2066 flags,
2067 io_priority,
2068 cancellable,
2069 callback,
2070 user_data);
2074 * g_file_append_to_finish:
2075 * @file: input #GFile
2076 * @res: #GAsyncResult
2077 * @error: a #GError, or %NULL
2079 * Finishes an asynchronous file append operation started with
2080 * g_file_append_to_async().
2082 * Returns: (transfer full): a valid #GFileOutputStream
2083 * or %NULL on error.
2084 * Free the returned object with g_object_unref().
2086 GFileOutputStream *
2087 g_file_append_to_finish (GFile *file,
2088 GAsyncResult *res,
2089 GError **error)
2091 GFileIface *iface;
2093 g_return_val_if_fail (G_IS_FILE (file), NULL);
2094 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2096 if (g_async_result_legacy_propagate_error (res, error))
2097 return NULL;
2099 iface = G_FILE_GET_IFACE (file);
2100 return (* iface->append_to_finish) (file, res, error);
2104 * g_file_create_async:
2105 * @file: input #GFile
2106 * @flags: a set of #GFileCreateFlags
2107 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2108 * of the request
2109 * @cancellable: (allow-none): optional #GCancellable object,
2110 * %NULL to ignore
2111 * @callback: (scope async): a #GAsyncReadyCallback to call
2112 * when the request is satisfied
2113 * @user_data: (closure): the data to pass to callback function
2115 * Asynchronously creates a new file and returns an output stream
2116 * for writing to it. The file must not already exist.
2118 * For more details, see g_file_create() which is
2119 * the synchronous version of this call.
2121 * When the operation is finished, @callback will be called.
2122 * You can then call g_file_create_finish() to get the result
2123 * of the operation.
2125 void
2126 g_file_create_async (GFile *file,
2127 GFileCreateFlags flags,
2128 int io_priority,
2129 GCancellable *cancellable,
2130 GAsyncReadyCallback callback,
2131 gpointer user_data)
2133 GFileIface *iface;
2135 g_return_if_fail (G_IS_FILE (file));
2137 iface = G_FILE_GET_IFACE (file);
2138 (* iface->create_async) (file,
2139 flags,
2140 io_priority,
2141 cancellable,
2142 callback,
2143 user_data);
2147 * g_file_create_finish:
2148 * @file: input #GFile
2149 * @res: a #GAsyncResult
2150 * @error: a #GError, or %NULL
2152 * Finishes an asynchronous file create operation started with
2153 * g_file_create_async().
2155 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2156 * Free the returned object with g_object_unref().
2158 GFileOutputStream *
2159 g_file_create_finish (GFile *file,
2160 GAsyncResult *res,
2161 GError **error)
2163 GFileIface *iface;
2165 g_return_val_if_fail (G_IS_FILE (file), NULL);
2166 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2168 if (g_async_result_legacy_propagate_error (res, error))
2169 return NULL;
2171 iface = G_FILE_GET_IFACE (file);
2172 return (* iface->create_finish) (file, res, error);
2176 * g_file_replace_async:
2177 * @file: input #GFile
2178 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2179 * for the current #GFile, or NULL to ignore
2180 * @make_backup: %TRUE if a backup should be created
2181 * @flags: a set of #GFileCreateFlags
2182 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2183 * of the request
2184 * @cancellable: (allow-none): optional #GCancellable object,
2185 * %NULL to ignore
2186 * @callback: (scope async): a #GAsyncReadyCallback to call
2187 * when the request is satisfied
2188 * @user_data: (closure): the data to pass to callback function
2190 * Asynchronously overwrites the file, replacing the contents,
2191 * possibly creating a backup copy of the file first.
2193 * For more details, see g_file_replace() which is
2194 * the synchronous version of this call.
2196 * When the operation is finished, @callback will be called.
2197 * You can then call g_file_replace_finish() to get the result
2198 * of the operation.
2200 void
2201 g_file_replace_async (GFile *file,
2202 const char *etag,
2203 gboolean make_backup,
2204 GFileCreateFlags flags,
2205 int io_priority,
2206 GCancellable *cancellable,
2207 GAsyncReadyCallback callback,
2208 gpointer user_data)
2210 GFileIface *iface;
2212 g_return_if_fail (G_IS_FILE (file));
2214 iface = G_FILE_GET_IFACE (file);
2215 (* iface->replace_async) (file,
2216 etag,
2217 make_backup,
2218 flags,
2219 io_priority,
2220 cancellable,
2221 callback,
2222 user_data);
2226 * g_file_replace_finish:
2227 * @file: input #GFile
2228 * @res: a #GAsyncResult
2229 * @error: a #GError, or %NULL
2231 * Finishes an asynchronous file replace operation started with
2232 * g_file_replace_async().
2234 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2235 * Free the returned object with g_object_unref().
2237 GFileOutputStream *
2238 g_file_replace_finish (GFile *file,
2239 GAsyncResult *res,
2240 GError **error)
2242 GFileIface *iface;
2244 g_return_val_if_fail (G_IS_FILE (file), NULL);
2245 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2247 if (g_async_result_legacy_propagate_error (res, error))
2248 return NULL;
2250 iface = G_FILE_GET_IFACE (file);
2251 return (* iface->replace_finish) (file, res, error);
2255 * g_file_open_readwrite_async
2256 * @file: input #GFile
2257 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2258 * of the request
2259 * @cancellable: (allow-none): optional #GCancellable object,
2260 * %NULL to ignore
2261 * @callback: (scope async): a #GAsyncReadyCallback to call
2262 * when the request is satisfied
2263 * @user_data: (closure): the data to pass to callback function
2265 * Asynchronously opens @file for reading and writing.
2267 * For more details, see g_file_open_readwrite() which is
2268 * the synchronous version of this call.
2270 * When the operation is finished, @callback will be called.
2271 * You can then call g_file_open_readwrite_finish() to get
2272 * the result of the operation.
2274 * Since: 2.22
2276 void
2277 g_file_open_readwrite_async (GFile *file,
2278 int io_priority,
2279 GCancellable *cancellable,
2280 GAsyncReadyCallback callback,
2281 gpointer user_data)
2283 GFileIface *iface;
2285 g_return_if_fail (G_IS_FILE (file));
2287 iface = G_FILE_GET_IFACE (file);
2288 (* iface->open_readwrite_async) (file,
2289 io_priority,
2290 cancellable,
2291 callback,
2292 user_data);
2296 * g_file_open_readwrite_finish:
2297 * @file: input #GFile
2298 * @res: a #GAsyncResult
2299 * @error: a #GError, or %NULL
2301 * Finishes an asynchronous file read operation started with
2302 * g_file_open_readwrite_async().
2304 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2305 * Free the returned object with g_object_unref().
2307 * Since: 2.22
2309 GFileIOStream *
2310 g_file_open_readwrite_finish (GFile *file,
2311 GAsyncResult *res,
2312 GError **error)
2314 GFileIface *iface;
2316 g_return_val_if_fail (G_IS_FILE (file), NULL);
2317 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2319 if (g_async_result_legacy_propagate_error (res, error))
2320 return NULL;
2322 iface = G_FILE_GET_IFACE (file);
2323 return (* iface->open_readwrite_finish) (file, res, error);
2327 * g_file_create_readwrite_async:
2328 * @file: input #GFile
2329 * @flags: a set of #GFileCreateFlags
2330 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2331 * of the request
2332 * @cancellable: (allow-none): optional #GCancellable object,
2333 * %NULL to ignore
2334 * @callback: (scope async): a #GAsyncReadyCallback to call
2335 * when the request is satisfied
2336 * @user_data: (closure): the data to pass to callback function
2338 * Asynchronously creates a new file and returns a stream
2339 * for reading and writing to it. The file must not already exist.
2341 * For more details, see g_file_create_readwrite() which is
2342 * the synchronous version of this call.
2344 * When the operation is finished, @callback will be called.
2345 * You can then call g_file_create_readwrite_finish() to get
2346 * the result of the operation.
2348 * Since: 2.22
2350 void
2351 g_file_create_readwrite_async (GFile *file,
2352 GFileCreateFlags flags,
2353 int io_priority,
2354 GCancellable *cancellable,
2355 GAsyncReadyCallback callback,
2356 gpointer user_data)
2358 GFileIface *iface;
2360 g_return_if_fail (G_IS_FILE (file));
2362 iface = G_FILE_GET_IFACE (file);
2363 (* iface->create_readwrite_async) (file,
2364 flags,
2365 io_priority,
2366 cancellable,
2367 callback,
2368 user_data);
2372 * g_file_create_readwrite_finish:
2373 * @file: input #GFile
2374 * @res: a #GAsyncResult
2375 * @error: a #GError, or %NULL
2377 * Finishes an asynchronous file create operation started with
2378 * g_file_create_readwrite_async().
2380 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2381 * Free the returned object with g_object_unref().
2383 * Since: 2.22
2385 GFileIOStream *
2386 g_file_create_readwrite_finish (GFile *file,
2387 GAsyncResult *res,
2388 GError **error)
2390 GFileIface *iface;
2392 g_return_val_if_fail (G_IS_FILE (file), NULL);
2393 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2395 if (g_async_result_legacy_propagate_error (res, error))
2396 return NULL;
2398 iface = G_FILE_GET_IFACE (file);
2399 return (* iface->create_readwrite_finish) (file, res, error);
2403 * g_file_replace_readwrite_async:
2404 * @file: input #GFile
2405 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2406 * for the current #GFile, or NULL to ignore
2407 * @make_backup: %TRUE if a backup should be created
2408 * @flags: a set of #GFileCreateFlags
2409 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2410 * of the request
2411 * @cancellable: (allow-none): optional #GCancellable object,
2412 * %NULL to ignore
2413 * @callback: (scope async): a #GAsyncReadyCallback to call
2414 * when the request is satisfied
2415 * @user_data: (closure): the data to pass to callback function
2417 * Asynchronously overwrites the file in read-write mode,
2418 * replacing the contents, possibly creating a backup copy
2419 * of the file first.
2421 * For more details, see g_file_replace_readwrite() which is
2422 * the synchronous version of this call.
2424 * When the operation is finished, @callback will be called.
2425 * You can then call g_file_replace_readwrite_finish() to get
2426 * the result of the operation.
2428 * Since: 2.22
2430 void
2431 g_file_replace_readwrite_async (GFile *file,
2432 const char *etag,
2433 gboolean make_backup,
2434 GFileCreateFlags flags,
2435 int io_priority,
2436 GCancellable *cancellable,
2437 GAsyncReadyCallback callback,
2438 gpointer user_data)
2440 GFileIface *iface;
2442 g_return_if_fail (G_IS_FILE (file));
2444 iface = G_FILE_GET_IFACE (file);
2445 (* iface->replace_readwrite_async) (file,
2446 etag,
2447 make_backup,
2448 flags,
2449 io_priority,
2450 cancellable,
2451 callback,
2452 user_data);
2456 * g_file_replace_readwrite_finish:
2457 * @file: input #GFile
2458 * @res: a #GAsyncResult
2459 * @error: a #GError, or %NULL
2461 * Finishes an asynchronous file replace operation started with
2462 * g_file_replace_readwrite_async().
2464 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2465 * Free the returned object with g_object_unref().
2467 * Since: 2.22
2469 GFileIOStream *
2470 g_file_replace_readwrite_finish (GFile *file,
2471 GAsyncResult *res,
2472 GError **error)
2474 GFileIface *iface;
2476 g_return_val_if_fail (G_IS_FILE (file), NULL);
2477 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2479 if (g_async_result_legacy_propagate_error (res, error))
2480 return NULL;
2482 iface = G_FILE_GET_IFACE (file);
2483 return (* iface->replace_readwrite_finish) (file, res, error);
2486 static gboolean
2487 copy_symlink (GFile *destination,
2488 GFileCopyFlags flags,
2489 GCancellable *cancellable,
2490 const char *target,
2491 GError **error)
2493 GError *my_error;
2494 gboolean tried_delete;
2495 GFileInfo *info;
2496 GFileType file_type;
2498 tried_delete = FALSE;
2500 retry:
2501 my_error = NULL;
2502 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2504 /* Maybe it already existed, and we want to overwrite? */
2505 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2506 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2508 g_error_free (my_error);
2510 /* Don't overwrite if the destination is a directory */
2511 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2512 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2513 cancellable, &my_error);
2514 if (info != NULL)
2516 file_type = g_file_info_get_file_type (info);
2517 g_object_unref (info);
2519 if (file_type == G_FILE_TYPE_DIRECTORY)
2521 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2522 _("Can't copy over directory"));
2523 return FALSE;
2527 if (!g_file_delete (destination, cancellable, error))
2528 return FALSE;
2530 tried_delete = TRUE;
2531 goto retry;
2533 /* Nah, fail */
2534 g_propagate_error (error, my_error);
2535 return FALSE;
2538 return TRUE;
2541 static GFileInputStream *
2542 open_source_for_copy (GFile *source,
2543 GFile *destination,
2544 GFileCopyFlags flags,
2545 GCancellable *cancellable,
2546 GError **error)
2548 GError *my_error;
2549 GFileInputStream *ret;
2550 GFileInfo *info;
2551 GFileType file_type;
2553 my_error = NULL;
2554 ret = g_file_read (source, cancellable, &my_error);
2555 if (ret != NULL)
2556 return ret;
2558 /* There was an error opening the source, try to set a good error for it: */
2559 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2561 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2562 * as that is less useful to the app. Better check for errors on the
2563 * target instead.
2565 g_error_free (my_error);
2566 my_error = NULL;
2568 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2569 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2570 cancellable, &my_error);
2571 if (info != NULL &&
2572 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2574 file_type = g_file_info_get_file_type (info);
2575 g_object_unref (info);
2577 if (flags & G_FILE_COPY_OVERWRITE)
2579 if (file_type == G_FILE_TYPE_DIRECTORY)
2581 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2582 _("Can't copy directory over directory"));
2583 return NULL;
2585 /* continue to would_recurse error */
2587 else
2589 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2590 _("Target file exists"));
2591 return NULL;
2594 else
2596 /* Error getting info from target, return that error
2597 * (except for NOT_FOUND, which is no error here)
2599 g_clear_object (&info);
2600 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2602 g_propagate_error (error, my_error);
2603 return NULL;
2605 g_clear_error (&my_error);
2608 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2609 _("Can't recursively copy directory"));
2610 return NULL;
2613 g_propagate_error (error, my_error);
2614 return NULL;
2617 static gboolean
2618 should_copy (GFileAttributeInfo *info,
2619 gboolean copy_all_attributes,
2620 gboolean skip_perms)
2622 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2623 return FALSE;
2625 if (copy_all_attributes)
2626 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2627 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2630 static gboolean
2631 build_attribute_list_for_copy (GFile *file,
2632 GFileCopyFlags flags,
2633 char **out_attributes,
2634 GCancellable *cancellable,
2635 GError **error)
2637 gboolean ret = FALSE;
2638 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2639 GString *s;
2640 gboolean first;
2641 int i;
2642 gboolean copy_all_attributes;
2643 gboolean skip_perms;
2645 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2646 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2648 /* Ignore errors here, if the target supports no attributes there is
2649 * nothing to copy. We still honor the cancellable though.
2651 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2653 goto out;
2655 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2656 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2657 goto out;
2659 if (attributes == NULL && namespaces == NULL)
2660 goto out;
2662 first = TRUE;
2663 s = g_string_new ("");
2665 if (attributes)
2667 for (i = 0; i < attributes->n_infos; i++)
2669 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2671 if (first)
2672 first = FALSE;
2673 else
2674 g_string_append_c (s, ',');
2676 g_string_append (s, attributes->infos[i].name);
2681 if (namespaces)
2683 for (i = 0; i < namespaces->n_infos; i++)
2685 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2687 if (first)
2688 first = FALSE;
2689 else
2690 g_string_append_c (s, ',');
2692 g_string_append (s, namespaces->infos[i].name);
2693 g_string_append (s, "::*");
2698 ret = TRUE;
2699 *out_attributes = g_string_free (s, FALSE);
2700 s = NULL;
2701 out:
2702 if (s)
2703 g_string_free (s, TRUE);
2704 if (attributes)
2705 g_file_attribute_info_list_unref (attributes);
2706 if (namespaces)
2707 g_file_attribute_info_list_unref (namespaces);
2709 return ret;
2713 * g_file_copy_attributes:
2714 * @source: a #GFile with attributes
2715 * @destination: a #GFile to copy attributes to
2716 * @flags: a set of #GFileCopyFlags
2717 * @cancellable: (allow-none): optional #GCancellable object,
2718 * %NULL to ignore
2719 * @error: a #GError, %NULL to ignore
2721 * Copies the file attributes from @source to @destination.
2723 * Normally only a subset of the file attributes are copied,
2724 * those that are copies in a normal file copy operation
2725 * (which for instance does not include e.g. owner). However
2726 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2727 * all the metadata that is possible to copy is copied. This
2728 * is useful when implementing move by copy + delete source.
2730 * Returns: %TRUE if the attributes were copied successfully,
2731 * %FALSE otherwise.
2733 gboolean
2734 g_file_copy_attributes (GFile *source,
2735 GFile *destination,
2736 GFileCopyFlags flags,
2737 GCancellable *cancellable,
2738 GError **error)
2740 char *attrs_to_read;
2741 gboolean res;
2742 GFileInfo *info;
2743 gboolean source_nofollow_symlinks;
2745 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2746 cancellable, error))
2747 return FALSE;
2749 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2751 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2752 * we just don't copy it.
2754 info = g_file_query_info (source, attrs_to_read,
2755 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2756 cancellable,
2757 NULL);
2759 g_free (attrs_to_read);
2761 res = TRUE;
2762 if (info)
2764 res = g_file_set_attributes_from_info (destination,
2765 info,
2766 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2767 cancellable,
2768 error);
2769 g_object_unref (info);
2772 return res;
2775 static gboolean
2776 copy_stream_with_progress (GInputStream *in,
2777 GOutputStream *out,
2778 GFile *source,
2779 GCancellable *cancellable,
2780 GFileProgressCallback progress_callback,
2781 gpointer progress_callback_data,
2782 GError **error)
2784 gssize n_read, n_written;
2785 goffset current_size;
2786 char buffer[1024*64], *p;
2787 gboolean res;
2788 goffset total_size;
2789 GFileInfo *info;
2791 total_size = -1;
2792 /* avoid performance impact of querying total size when it's not needed */
2793 if (progress_callback)
2795 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2796 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2797 cancellable, NULL);
2798 if (info)
2800 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2801 total_size = g_file_info_get_size (info);
2802 g_object_unref (info);
2805 if (total_size == -1)
2807 info = g_file_query_info (source,
2808 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2809 G_FILE_QUERY_INFO_NONE,
2810 cancellable, NULL);
2811 if (info)
2813 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2814 total_size = g_file_info_get_size (info);
2815 g_object_unref (info);
2820 if (total_size == -1)
2821 total_size = 0;
2823 current_size = 0;
2824 res = TRUE;
2825 while (TRUE)
2827 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2828 if (n_read == -1)
2830 res = FALSE;
2831 break;
2834 if (n_read == 0)
2835 break;
2837 current_size += n_read;
2839 p = buffer;
2840 while (n_read > 0)
2842 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2843 if (n_written == -1)
2845 res = FALSE;
2846 break;
2849 p += n_written;
2850 n_read -= n_written;
2853 if (!res)
2854 break;
2856 if (progress_callback)
2857 progress_callback (current_size, total_size, progress_callback_data);
2860 /* Make sure we send full copied size */
2861 if (progress_callback)
2862 progress_callback (current_size, total_size, progress_callback_data);
2864 return res;
2867 #ifdef HAVE_SPLICE
2869 static gboolean
2870 do_splice (int fd_in,
2871 loff_t *off_in,
2872 int fd_out,
2873 loff_t *off_out,
2874 size_t len,
2875 long *bytes_transferd,
2876 GError **error)
2878 long result;
2880 retry:
2881 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2883 if (result == -1)
2885 int errsv = errno;
2887 if (errsv == EINTR)
2888 goto retry;
2889 else if (errsv == ENOSYS || errsv == EINVAL)
2890 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2891 _("Splice not supported"));
2892 else
2893 g_set_error (error, G_IO_ERROR,
2894 g_io_error_from_errno (errsv),
2895 _("Error splicing file: %s"),
2896 g_strerror (errsv));
2898 return FALSE;
2901 *bytes_transferd = result;
2902 return TRUE;
2905 static gboolean
2906 splice_stream_with_progress (GInputStream *in,
2907 GOutputStream *out,
2908 GCancellable *cancellable,
2909 GFileProgressCallback progress_callback,
2910 gpointer progress_callback_data,
2911 GError **error)
2913 int buffer[2] = { -1, -1 };
2914 gboolean res;
2915 goffset total_size;
2916 loff_t offset_in;
2917 loff_t offset_out;
2918 int fd_in, fd_out;
2920 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2921 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2923 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2924 return FALSE;
2926 total_size = -1;
2927 /* avoid performance impact of querying total size when it's not needed */
2928 if (progress_callback)
2930 struct stat sbuf;
2932 if (fstat (fd_in, &sbuf) == 0)
2933 total_size = sbuf.st_size;
2936 if (total_size == -1)
2937 total_size = 0;
2939 offset_in = offset_out = 0;
2940 res = FALSE;
2941 while (TRUE)
2943 long n_read;
2944 long n_written;
2946 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2947 break;
2949 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2950 break;
2952 if (n_read == 0)
2954 res = TRUE;
2955 break;
2958 while (n_read > 0)
2960 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2961 goto out;
2963 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2964 goto out;
2966 n_read -= n_written;
2969 if (progress_callback)
2970 progress_callback (offset_in, total_size, progress_callback_data);
2973 /* Make sure we send full copied size */
2974 if (progress_callback)
2975 progress_callback (offset_in, total_size, progress_callback_data);
2977 if (!g_close (buffer[0], error))
2978 goto out;
2979 buffer[0] = -1;
2980 if (!g_close (buffer[1], error))
2981 goto out;
2982 buffer[1] = -1;
2983 out:
2984 if (buffer[0] != -1)
2985 (void) g_close (buffer[0], NULL);
2986 if (buffer[1] != -1)
2987 (void) g_close (buffer[1], NULL);
2989 return res;
2991 #endif
2993 #ifdef __linux__
2994 static gboolean
2995 btrfs_reflink_with_progress (GInputStream *in,
2996 GOutputStream *out,
2997 GFileInfo *info,
2998 GCancellable *cancellable,
2999 GFileProgressCallback progress_callback,
3000 gpointer progress_callback_data,
3001 GError **error)
3003 goffset source_size;
3004 int fd_in, fd_out;
3005 int ret;
3007 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3008 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3010 if (progress_callback)
3011 source_size = g_file_info_get_size (info);
3013 /* Btrfs clone ioctl properties:
3014 * - Works at the inode level
3015 * - Doesn't work with directories
3016 * - Always follows symlinks (source and destination)
3018 * By the time we get here, *in and *out are both regular files */
3019 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3021 if (ret < 0)
3023 if (errno == EXDEV)
3024 g_set_error_literal (error, G_IO_ERROR,
3025 G_IO_ERROR_NOT_SUPPORTED,
3026 _("Copy (reflink/clone) between mounts is not supported"));
3027 else if (errno == EINVAL)
3028 g_set_error_literal (error, G_IO_ERROR,
3029 G_IO_ERROR_NOT_SUPPORTED,
3030 _("Copy (reflink/clone) is not supported or invalid"));
3031 else
3032 /* Most probably something odd happened; retry with fallback */
3033 g_set_error_literal (error, G_IO_ERROR,
3034 G_IO_ERROR_NOT_SUPPORTED,
3035 _("Copy (reflink/clone) is not supported or didn't work"));
3036 /* We retry with fallback for all error cases because Btrfs is currently
3037 * unstable, and so we can't trust it to do clone properly.
3038 * In addition, any hard errors here would cause the same failure in the
3039 * fallback manual copy as well. */
3040 return FALSE;
3043 /* Make sure we send full copied size */
3044 if (progress_callback)
3045 progress_callback (source_size, source_size, progress_callback_data);
3047 return TRUE;
3049 #endif
3051 static gboolean
3052 file_copy_fallback (GFile *source,
3053 GFile *destination,
3054 GFileCopyFlags flags,
3055 GCancellable *cancellable,
3056 GFileProgressCallback progress_callback,
3057 gpointer progress_callback_data,
3058 GError **error)
3060 gboolean ret = FALSE;
3061 GFileInputStream *file_in = NULL;
3062 GInputStream *in = NULL;
3063 GOutputStream *out = NULL;
3064 GFileInfo *info = NULL;
3065 const char *target;
3066 char *attrs_to_read;
3067 gboolean do_set_attributes = FALSE;
3069 /* need to know the file type */
3070 info = g_file_query_info (source,
3071 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3072 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3073 cancellable,
3074 error);
3075 if (!info)
3076 goto out;
3078 /* Maybe copy the symlink? */
3079 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3080 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3082 target = g_file_info_get_symlink_target (info);
3083 if (target)
3085 if (!copy_symlink (destination, flags, cancellable, target, error))
3086 goto out;
3088 ret = TRUE;
3089 goto out;
3091 /* ... else fall back on a regular file copy */
3093 /* Handle "special" files (pipes, device nodes, ...)? */
3094 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3096 /* FIXME: could try to recreate device nodes and others? */
3097 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3098 _("Can't copy special file"));
3099 goto out;
3102 /* Everything else should just fall back on a regular copy. */
3104 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3105 if (!file_in)
3106 goto out;
3107 in = G_INPUT_STREAM (file_in);
3109 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3110 cancellable, error))
3111 goto out;
3113 if (attrs_to_read != NULL)
3115 GError *tmp_error = NULL;
3117 /* Ok, ditch the previous lightweight info (on Unix we just
3118 * called lstat()); at this point we gather all the information
3119 * we need about the source from the opened file descriptor.
3121 g_object_unref (info);
3123 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3124 cancellable, &tmp_error);
3125 if (!info)
3127 /* Not all gvfs backends implement query_info_on_read(), we
3128 * can just fall back to the pathname again.
3129 * https://bugzilla.gnome.org/706254
3131 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3133 g_clear_error (&tmp_error);
3134 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3135 cancellable, error);
3137 else
3139 g_free (attrs_to_read);
3140 g_propagate_error (error, tmp_error);
3141 goto out;
3144 g_free (attrs_to_read);
3145 if (!info)
3146 goto out;
3148 do_set_attributes = TRUE;
3151 /* In the local file path, we pass down the source info which
3152 * includes things like unix::mode, to ensure that the target file
3153 * is not created with different permissions from the source file.
3155 * If a future API like g_file_replace_with_info() is added, switch
3156 * this code to use that.
3158 if (G_IS_LOCAL_FILE (destination))
3160 if (flags & G_FILE_COPY_OVERWRITE)
3161 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3162 FALSE, NULL,
3163 flags & G_FILE_COPY_BACKUP,
3164 G_FILE_CREATE_REPLACE_DESTINATION,
3165 info,
3166 cancellable, error);
3167 else
3168 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3169 FALSE, 0, info,
3170 cancellable, error);
3172 else if (flags & G_FILE_COPY_OVERWRITE)
3174 out = (GOutputStream *)g_file_replace (destination,
3175 NULL,
3176 flags & G_FILE_COPY_BACKUP,
3177 G_FILE_CREATE_REPLACE_DESTINATION,
3178 cancellable, error);
3180 else
3182 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3185 if (!out)
3186 goto out;
3188 #ifdef __linux__
3189 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3191 GError *reflink_err = NULL;
3193 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3194 progress_callback, progress_callback_data,
3195 &reflink_err))
3197 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3199 g_clear_error (&reflink_err);
3201 else
3203 g_propagate_error (error, reflink_err);
3204 goto out;
3207 else
3209 ret = TRUE;
3210 goto out;
3213 #endif
3215 #ifdef HAVE_SPLICE
3216 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3218 GError *splice_err = NULL;
3220 if (!splice_stream_with_progress (in, out, cancellable,
3221 progress_callback, progress_callback_data,
3222 &splice_err))
3224 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3226 g_clear_error (&splice_err);
3228 else
3230 g_propagate_error (error, splice_err);
3231 goto out;
3234 else
3236 ret = TRUE;
3237 goto out;
3241 #endif
3243 /* A plain read/write loop */
3244 if (!copy_stream_with_progress (in, out, source, cancellable,
3245 progress_callback, progress_callback_data,
3246 error))
3247 goto out;
3249 ret = TRUE;
3250 out:
3251 if (in)
3253 /* Don't care about errors in source here */
3254 (void) g_input_stream_close (in, cancellable, NULL);
3255 g_object_unref (in);
3258 if (out)
3260 /* But write errors on close are bad! */
3261 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3262 ret = FALSE;
3263 g_object_unref (out);
3266 /* Ignore errors here. Failure to copy metadata is not a hard error */
3267 /* TODO: set these attributes /before/ we do the rename() on Unix */
3268 if (ret && do_set_attributes)
3270 g_file_set_attributes_from_info (destination,
3271 info,
3272 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3273 cancellable,
3274 error);
3277 g_clear_object (&info);
3279 return ret;
3283 * g_file_copy:
3284 * @source: input #GFile
3285 * @destination: destination #GFile
3286 * @flags: set of #GFileCopyFlags
3287 * @cancellable: (allow-none): optional #GCancellable object,
3288 * %NULL to ignore
3289 * @progress_callback: (allow-none) (scope call): function to callback with
3290 * progress information, or %NULL if progress information is not needed
3291 * @progress_callback_data: (closure): user data to pass to @progress_callback
3292 * @error: #GError to set on error, or %NULL
3294 * Copies the file @source to the location specified by @destination.
3295 * Can not handle recursive copies of directories.
3297 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3298 * existing @destination file is overwritten.
3300 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3301 * will be copied as symlinks, otherwise the target of the
3302 * @source symlink will be copied.
3304 * If @cancellable is not %NULL, then the operation can be cancelled by
3305 * triggering the cancellable object from another thread. If the operation
3306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3308 * If @progress_callback is not %NULL, then the operation can be monitored
3309 * by setting this to a #GFileProgressCallback function.
3310 * @progress_callback_data will be passed to this function. It is guaranteed
3311 * that this callback will be called after all data has been transferred with
3312 * the total number of bytes copied during the operation.
3314 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3315 * is returned, independent on the status of the @destination.
3317 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3318 * the error %G_IO_ERROR_EXISTS is returned.
3320 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3321 * error is returned. If trying to overwrite a directory with a directory the
3322 * %G_IO_ERROR_WOULD_MERGE error is returned.
3324 * If the source is a directory and the target does not exist, or
3325 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3326 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3328 * If you are interested in copying the #GFile object itself (not the on-disk
3329 * file), see g_file_dup().
3331 * Returns: %TRUE on success, %FALSE otherwise.
3333 gboolean
3334 g_file_copy (GFile *source,
3335 GFile *destination,
3336 GFileCopyFlags flags,
3337 GCancellable *cancellable,
3338 GFileProgressCallback progress_callback,
3339 gpointer progress_callback_data,
3340 GError **error)
3342 GFileIface *iface;
3343 GError *my_error;
3344 gboolean res;
3346 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3347 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3350 return FALSE;
3352 iface = G_FILE_GET_IFACE (destination);
3353 if (iface->copy)
3355 my_error = NULL;
3356 res = (* iface->copy) (source, destination,
3357 flags, cancellable,
3358 progress_callback, progress_callback_data,
3359 &my_error);
3361 if (res)
3362 return TRUE;
3364 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3366 g_propagate_error (error, my_error);
3367 return FALSE;
3369 else
3370 g_clear_error (&my_error);
3373 /* If the types are different, and the destination method failed
3374 * also try the source method
3376 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3378 iface = G_FILE_GET_IFACE (source);
3380 if (iface->copy)
3382 my_error = NULL;
3383 res = (* iface->copy) (source, destination,
3384 flags, cancellable,
3385 progress_callback, progress_callback_data,
3386 &my_error);
3388 if (res)
3389 return TRUE;
3391 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3393 g_propagate_error (error, my_error);
3394 return FALSE;
3396 else
3397 g_clear_error (&my_error);
3401 return file_copy_fallback (source, destination, flags, cancellable,
3402 progress_callback, progress_callback_data,
3403 error);
3407 * g_file_copy_async: (skip)
3408 * @source: input #GFile
3409 * @destination: destination #GFile
3410 * @flags: set of #GFileCopyFlags
3411 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3412 * of the request
3413 * @cancellable: (allow-none): optional #GCancellable object,
3414 * %NULL to ignore
3415 * @progress_callback: (allow-none): function to callback with progress
3416 * information, or %NULL if progress information is not needed
3417 * @progress_callback_data: (closure): user data to pass to @progress_callback
3418 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3419 * @user_data: the data to pass to callback function
3421 * Copies the file @source to the location specified by @destination
3422 * asynchronously. For details of the behaviour, see g_file_copy().
3424 * If @progress_callback is not %NULL, then that function that will be called
3425 * just like in g_file_copy(), however the callback will run in the main loop,
3426 * not in the thread that is doing the I/O operation.
3428 * When the operation is finished, @callback will be called. You can then call
3429 * g_file_copy_finish() to get the result of the operation.
3431 void
3432 g_file_copy_async (GFile *source,
3433 GFile *destination,
3434 GFileCopyFlags flags,
3435 int io_priority,
3436 GCancellable *cancellable,
3437 GFileProgressCallback progress_callback,
3438 gpointer progress_callback_data,
3439 GAsyncReadyCallback callback,
3440 gpointer user_data)
3442 GFileIface *iface;
3444 g_return_if_fail (G_IS_FILE (source));
3445 g_return_if_fail (G_IS_FILE (destination));
3447 iface = G_FILE_GET_IFACE (source);
3448 (* iface->copy_async) (source,
3449 destination,
3450 flags,
3451 io_priority,
3452 cancellable,
3453 progress_callback,
3454 progress_callback_data,
3455 callback,
3456 user_data);
3460 * g_file_copy_finish:
3461 * @file: input #GFile
3462 * @res: a #GAsyncResult
3463 * @error: a #GError, or %NULL
3465 * Finishes copying the file started with g_file_copy_async().
3467 * Returns: a %TRUE on success, %FALSE on error.
3469 gboolean
3470 g_file_copy_finish (GFile *file,
3471 GAsyncResult *res,
3472 GError **error)
3474 GFileIface *iface;
3476 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3477 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3479 if (g_async_result_legacy_propagate_error (res, error))
3480 return FALSE;
3482 iface = G_FILE_GET_IFACE (file);
3483 return (* iface->copy_finish) (file, res, error);
3487 * g_file_move:
3488 * @source: #GFile pointing to the source location
3489 * @destination: #GFile pointing to the destination location
3490 * @flags: set of #GFileCopyFlags
3491 * @cancellable: (allow-none): optional #GCancellable object,
3492 * %NULL to ignore
3493 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3494 * function for updates
3495 * @progress_callback_data: (closure): gpointer to user data for
3496 * the callback function
3497 * @error: #GError for returning error conditions, or %NULL
3499 * Tries to move the file or directory @source to the location specified
3500 * by @destination. If native move operations are supported then this is
3501 * used, otherwise a copy + delete fallback is used. The native
3502 * implementation may support moving directories (for instance on moves
3503 * inside the same filesystem), but the fallback code does not.
3505 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3506 * existing @destination file is overwritten.
3508 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3509 * will be copied as symlinks, otherwise the target of the
3510 * @source symlink will be copied.
3512 * If @cancellable is not %NULL, then the operation can be cancelled by
3513 * triggering the cancellable object from another thread. If the operation
3514 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3516 * If @progress_callback is not %NULL, then the operation can be monitored
3517 * by setting this to a #GFileProgressCallback function.
3518 * @progress_callback_data will be passed to this function. It is
3519 * guaranteed that this callback will be called after all data has been
3520 * transferred with the total number of bytes copied during the operation.
3522 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3523 * error is returned, independent on the status of the @destination.
3525 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3526 * then the error %G_IO_ERROR_EXISTS is returned.
3528 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3529 * error is returned. If trying to overwrite a directory with a directory the
3530 * %G_IO_ERROR_WOULD_MERGE error is returned.
3532 * If the source is a directory and the target does not exist, or
3533 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3534 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3535 * move operation isn't available).
3537 * Returns: %TRUE on successful move, %FALSE otherwise.
3539 gboolean
3540 g_file_move (GFile *source,
3541 GFile *destination,
3542 GFileCopyFlags flags,
3543 GCancellable *cancellable,
3544 GFileProgressCallback progress_callback,
3545 gpointer progress_callback_data,
3546 GError **error)
3548 GFileIface *iface;
3549 GError *my_error;
3550 gboolean res;
3552 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3553 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3555 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3556 return FALSE;
3558 iface = G_FILE_GET_IFACE (destination);
3559 if (iface->move)
3561 my_error = NULL;
3562 res = (* iface->move) (source, destination,
3563 flags, cancellable,
3564 progress_callback, progress_callback_data,
3565 &my_error);
3567 if (res)
3568 return TRUE;
3570 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3572 g_propagate_error (error, my_error);
3573 return FALSE;
3577 /* If the types are different, and the destination method failed
3578 * also try the source method
3580 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3582 iface = G_FILE_GET_IFACE (source);
3584 if (iface->move)
3586 my_error = NULL;
3587 res = (* iface->move) (source, destination,
3588 flags, cancellable,
3589 progress_callback, progress_callback_data,
3590 &my_error);
3592 if (res)
3593 return TRUE;
3595 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3597 g_propagate_error (error, my_error);
3598 return FALSE;
3603 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3605 g_set_error_literal (error, G_IO_ERROR,
3606 G_IO_ERROR_NOT_SUPPORTED,
3607 _("Operation not supported"));
3608 return FALSE;
3611 flags |= G_FILE_COPY_ALL_METADATA;
3612 if (!g_file_copy (source, destination, flags, cancellable,
3613 progress_callback, progress_callback_data,
3614 error))
3615 return FALSE;
3617 return g_file_delete (source, cancellable, error);
3621 * g_file_make_directory:
3622 * @file: input #GFile
3623 * @cancellable: (allow-none): optional #GCancellable object,
3624 * %NULL to ignore
3625 * @error: a #GError, or %NULL
3627 * Creates a directory. Note that this will only create a child directory
3628 * of the immediate parent directory of the path or URI given by the #GFile.
3629 * To recursively create directories, see g_file_make_directory_with_parents().
3630 * This function will fail if the parent directory does not exist, setting
3631 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3632 * creating directories, this function will fail, setting @error to
3633 * %G_IO_ERROR_NOT_SUPPORTED.
3635 * For a local #GFile the newly created directory will have the default
3636 * (current) ownership and permissions of the current process.
3638 * If @cancellable is not %NULL, then the operation can be cancelled by
3639 * triggering the cancellable object from another thread. If the operation
3640 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3642 * Returns: %TRUE on successful creation, %FALSE otherwise.
3644 gboolean
3645 g_file_make_directory (GFile *file,
3646 GCancellable *cancellable,
3647 GError **error)
3649 GFileIface *iface;
3651 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3653 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3654 return FALSE;
3656 iface = G_FILE_GET_IFACE (file);
3658 if (iface->make_directory == NULL)
3660 g_set_error_literal (error, G_IO_ERROR,
3661 G_IO_ERROR_NOT_SUPPORTED,
3662 _("Operation not supported"));
3663 return FALSE;
3666 return (* iface->make_directory) (file, cancellable, error);
3670 * g_file_make_directory_async:
3671 * @file: input #GFile
3672 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3673 * of the request
3674 * @cancellable: (allow-none): optional #GCancellable object,
3675 * %NULL to ignore
3676 * @callback: a #GAsyncReadyCallback to call
3677 * when the request is satisfied
3678 * @user_data: the data to pass to callback function
3680 * Asynchronously creates a directory.
3682 * Virtual: make_directory_async
3683 * Since: 2.38
3685 void
3686 g_file_make_directory_async (GFile *file,
3687 int io_priority,
3688 GCancellable *cancellable,
3689 GAsyncReadyCallback callback,
3690 gpointer user_data)
3692 GFileIface *iface;
3694 g_return_if_fail (G_IS_FILE (file));
3696 iface = G_FILE_GET_IFACE (file);
3697 (* iface->make_directory_async) (file,
3698 io_priority,
3699 cancellable,
3700 callback,
3701 user_data);
3705 * g_file_make_directory_finish:
3706 * @file: input #GFile
3707 * @result: a #GAsyncResult
3708 * @error: a #GError, or %NULL
3710 * Finishes an asynchronous directory creation, started with
3711 * g_file_make_directory_async().
3713 * Virtual: make_directory_finish
3714 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3715 * Since: 2.38
3717 gboolean
3718 g_file_make_directory_finish (GFile *file,
3719 GAsyncResult *result,
3720 GError **error)
3722 GFileIface *iface;
3724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3725 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3727 iface = G_FILE_GET_IFACE (file);
3728 return (* iface->make_directory_finish) (file, result, error);
3732 * g_file_make_directory_with_parents:
3733 * @file: input #GFile
3734 * @cancellable: (allow-none): optional #GCancellable object,
3735 * %NULL to ignore
3736 * @error: a #GError, or %NULL
3738 * Creates a directory and any parent directories that may not
3739 * exist similar to 'mkdir -p'. If the file system does not support
3740 * creating directories, this function will fail, setting @error to
3741 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3742 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3743 * the similar g_mkdir_with_parents().
3745 * For a local #GFile the newly created directories will have the default
3746 * (current) ownership and permissions of the current process.
3748 * If @cancellable is not %NULL, then the operation can be cancelled by
3749 * triggering the cancellable object from another thread. If the operation
3750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3752 * Returns: %TRUE if all directories have been successfully created, %FALSE
3753 * otherwise.
3755 * Since: 2.18
3757 gboolean
3758 g_file_make_directory_with_parents (GFile *file,
3759 GCancellable *cancellable,
3760 GError **error)
3762 GFile *work_file = NULL;
3763 GList *list = NULL, *l;
3764 GError *my_error = NULL;
3766 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3769 return FALSE;
3771 g_file_make_directory (file, cancellable, &my_error);
3772 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3774 if (my_error)
3775 g_propagate_error (error, my_error);
3776 return my_error == NULL;
3779 work_file = g_object_ref (file);
3781 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3783 GFile *parent_file;
3785 parent_file = g_file_get_parent (work_file);
3786 if (parent_file == NULL)
3787 break;
3789 g_clear_error (&my_error);
3790 g_file_make_directory (parent_file, cancellable, &my_error);
3792 g_object_unref (work_file);
3793 work_file = g_object_ref (parent_file);
3795 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3796 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3797 else
3798 g_object_unref (parent_file);
3801 for (l = list; my_error == NULL && l; l = l->next)
3803 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3806 if (work_file)
3807 g_object_unref (work_file);
3809 /* Clean up */
3810 while (list != NULL)
3812 g_object_unref ((GFile *) list->data);
3813 list = g_list_remove (list, list->data);
3816 if (my_error != NULL)
3818 g_propagate_error (error, my_error);
3819 return FALSE;
3822 return g_file_make_directory (file, cancellable, error);
3826 * g_file_make_symbolic_link:
3827 * @file: a #GFile with the name of the symlink to create
3828 * @symlink_value: a string with the path for the target of the new symlink
3829 * @cancellable: (allow-none): optional #GCancellable object,
3830 * %NULL to ignore
3831 * @error: a #GError
3833 * Creates a symbolic link named @file which contains the string
3834 * @symlink_value.
3836 * If @cancellable is not %NULL, then the operation can be cancelled by
3837 * triggering the cancellable object from another thread. If the operation
3838 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3840 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3842 gboolean
3843 g_file_make_symbolic_link (GFile *file,
3844 const char *symlink_value,
3845 GCancellable *cancellable,
3846 GError **error)
3848 GFileIface *iface;
3850 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3851 g_return_val_if_fail (symlink_value != NULL, FALSE);
3853 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3854 return FALSE;
3856 if (*symlink_value == '\0')
3858 g_set_error_literal (error, G_IO_ERROR,
3859 G_IO_ERROR_INVALID_ARGUMENT,
3860 _("Invalid symlink value given"));
3861 return FALSE;
3864 iface = G_FILE_GET_IFACE (file);
3866 if (iface->make_symbolic_link == NULL)
3868 g_set_error_literal (error, G_IO_ERROR,
3869 G_IO_ERROR_NOT_SUPPORTED,
3870 _("Operation not supported"));
3871 return FALSE;
3874 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3878 * g_file_delete:
3879 * @file: input #GFile
3880 * @cancellable: (allow-none): optional #GCancellable object,
3881 * %NULL to ignore
3882 * @error: a #GError, or %NULL
3884 * Deletes a file. If the @file is a directory, it will only be
3885 * deleted if it is empty. This has the same semantics as g_unlink().
3887 * If @cancellable is not %NULL, then the operation can be cancelled by
3888 * triggering the cancellable object from another thread. If the operation
3889 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3891 * Virtual: delete_file
3892 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3894 gboolean
3895 g_file_delete (GFile *file,
3896 GCancellable *cancellable,
3897 GError **error)
3899 GFileIface *iface;
3901 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3903 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3904 return FALSE;
3906 iface = G_FILE_GET_IFACE (file);
3908 if (iface->delete_file == NULL)
3910 g_set_error_literal (error, G_IO_ERROR,
3911 G_IO_ERROR_NOT_SUPPORTED,
3912 _("Operation not supported"));
3913 return FALSE;
3916 return (* iface->delete_file) (file, cancellable, error);
3920 * g_file_delete_async:
3921 * @file: input #GFile
3922 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3923 * of the request
3924 * @cancellable: (allow-none): optional #GCancellable object,
3925 * %NULL to ignore
3926 * @callback: a #GAsyncReadyCallback to call
3927 * when the request is satisfied
3928 * @user_data: the data to pass to callback function
3930 * Asynchronously delete a file. If the @file is a directory, it will
3931 * only be deleted if it is empty. This has the same semantics as
3932 * g_unlink().
3934 * Virtual: delete_file_async
3935 * Since: 2.34
3937 void
3938 g_file_delete_async (GFile *file,
3939 int io_priority,
3940 GCancellable *cancellable,
3941 GAsyncReadyCallback callback,
3942 gpointer user_data)
3944 GFileIface *iface;
3946 g_return_if_fail (G_IS_FILE (file));
3948 iface = G_FILE_GET_IFACE (file);
3949 (* iface->delete_file_async) (file,
3950 io_priority,
3951 cancellable,
3952 callback,
3953 user_data);
3957 * g_file_delete_finish:
3958 * @file: input #GFile
3959 * @result: a #GAsyncResult
3960 * @error: a #GError, or %NULL
3962 * Finishes deleting a file started with g_file_delete_async().
3964 * Virtual: delete_file_finish
3965 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3966 * Since: 2.34
3968 gboolean
3969 g_file_delete_finish (GFile *file,
3970 GAsyncResult *result,
3971 GError **error)
3973 GFileIface *iface;
3975 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3976 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3978 if (g_async_result_legacy_propagate_error (result, error))
3979 return FALSE;
3981 iface = G_FILE_GET_IFACE (file);
3982 return (* iface->delete_file_finish) (file, result, error);
3986 * g_file_trash:
3987 * @file: #GFile to send to trash
3988 * @cancellable: (allow-none): optional #GCancellable object,
3989 * %NULL to ignore
3990 * @error: a #GError, or %NULL
3992 * Sends @file to the "Trashcan", if possible. This is similar to
3993 * deleting it, but the user can recover it before emptying the trashcan.
3994 * Not all file systems support trashing, so this call can return the
3995 * %G_IO_ERROR_NOT_SUPPORTED error.
3997 * If @cancellable is not %NULL, then the operation can be cancelled by
3998 * triggering the cancellable object from another thread. If the operation
3999 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4001 * Virtual: trash
4002 * Returns: %TRUE on successful trash, %FALSE otherwise.
4004 gboolean
4005 g_file_trash (GFile *file,
4006 GCancellable *cancellable,
4007 GError **error)
4009 GFileIface *iface;
4011 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4013 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4014 return FALSE;
4016 iface = G_FILE_GET_IFACE (file);
4018 if (iface->trash == NULL)
4020 g_set_error_literal (error,
4021 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4022 _("Trash not supported"));
4023 return FALSE;
4026 return (* iface->trash) (file, cancellable, error);
4030 * g_file_trash_async:
4031 * @file: input #GFile
4032 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4033 * of the request
4034 * @cancellable: (allow-none): optional #GCancellable object,
4035 * %NULL to ignore
4036 * @callback: a #GAsyncReadyCallback to call
4037 * when the request is satisfied
4038 * @user_data: the data to pass to callback function
4040 * Asynchronously sends @file to the Trash location, if possible.
4042 * Virtual: trash_async
4043 * Since: 2.38
4045 void
4046 g_file_trash_async (GFile *file,
4047 int io_priority,
4048 GCancellable *cancellable,
4049 GAsyncReadyCallback callback,
4050 gpointer user_data)
4052 GFileIface *iface;
4054 g_return_if_fail (G_IS_FILE (file));
4056 iface = G_FILE_GET_IFACE (file);
4057 (* iface->trash_async) (file,
4058 io_priority,
4059 cancellable,
4060 callback,
4061 user_data);
4065 * g_file_trash_finish:
4066 * @file: input #GFile
4067 * @result: a #GAsyncResult
4068 * @error: a #GError, or %NULL
4070 * Finishes an asynchronous file trashing operation, started with
4071 * g_file_trash_async().
4073 * Virtual: trash_finish
4074 * Returns: %TRUE on successful trash, %FALSE otherwise.
4075 * Since: 2.38
4077 gboolean
4078 g_file_trash_finish (GFile *file,
4079 GAsyncResult *result,
4080 GError **error)
4082 GFileIface *iface;
4084 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4085 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4087 iface = G_FILE_GET_IFACE (file);
4088 return (* iface->trash_finish) (file, result, error);
4092 * g_file_set_display_name:
4093 * @file: input #GFile
4094 * @display_name: a string
4095 * @cancellable: (allow-none): optional #GCancellable object,
4096 * %NULL to ignore
4097 * @error: a #GError, or %NULL
4099 * Renames @file to the specified display name.
4101 * The display name is converted from UTF-8 to the correct encoding
4102 * for the target filesystem if possible and the @file is renamed to this.
4104 * If you want to implement a rename operation in the user interface the
4105 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4106 * initial value in the rename widget, and then the result after editing
4107 * should be passed to g_file_set_display_name().
4109 * On success the resulting converted filename is returned.
4111 * If @cancellable is not %NULL, then the operation can be cancelled by
4112 * triggering the cancellable object from another thread. If the operation
4113 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4115 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4116 * or %NULL if there was an error.
4117 * Free the returned object with g_object_unref().
4119 GFile *
4120 g_file_set_display_name (GFile *file,
4121 const gchar *display_name,
4122 GCancellable *cancellable,
4123 GError **error)
4125 GFileIface *iface;
4127 g_return_val_if_fail (G_IS_FILE (file), NULL);
4128 g_return_val_if_fail (display_name != NULL, NULL);
4130 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4132 g_set_error (error,
4133 G_IO_ERROR,
4134 G_IO_ERROR_INVALID_ARGUMENT,
4135 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4136 return NULL;
4139 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4140 return NULL;
4142 iface = G_FILE_GET_IFACE (file);
4144 return (* iface->set_display_name) (file, display_name, cancellable, error);
4148 * g_file_set_display_name_async:
4149 * @file: input #GFile
4150 * @display_name: a string
4151 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4152 * of the request
4153 * @cancellable: (allow-none): optional #GCancellable object,
4154 * %NULL to ignore
4155 * @callback: (scope async): a #GAsyncReadyCallback to call
4156 * when the request is satisfied
4157 * @user_data: (closure): the data to pass to callback function
4159 * Asynchronously sets the display name for a given #GFile.
4161 * For more details, see g_file_set_display_name() which is
4162 * the synchronous version of this call.
4164 * When the operation is finished, @callback will be called.
4165 * You can then call g_file_set_display_name_finish() to get
4166 * the result of the operation.
4168 void
4169 g_file_set_display_name_async (GFile *file,
4170 const gchar *display_name,
4171 gint io_priority,
4172 GCancellable *cancellable,
4173 GAsyncReadyCallback callback,
4174 gpointer user_data)
4176 GFileIface *iface;
4178 g_return_if_fail (G_IS_FILE (file));
4179 g_return_if_fail (display_name != NULL);
4181 iface = G_FILE_GET_IFACE (file);
4182 (* iface->set_display_name_async) (file,
4183 display_name,
4184 io_priority,
4185 cancellable,
4186 callback,
4187 user_data);
4191 * g_file_set_display_name_finish:
4192 * @file: input #GFile
4193 * @res: a #GAsyncResult
4194 * @error: a #GError, or %NULL
4196 * Finishes setting a display name started with
4197 * g_file_set_display_name_async().
4199 * Returns: (transfer full): a #GFile or %NULL on error.
4200 * Free the returned object with g_object_unref().
4202 GFile *
4203 g_file_set_display_name_finish (GFile *file,
4204 GAsyncResult *res,
4205 GError **error)
4207 GFileIface *iface;
4209 g_return_val_if_fail (G_IS_FILE (file), NULL);
4210 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4212 if (g_async_result_legacy_propagate_error (res, error))
4213 return NULL;
4215 iface = G_FILE_GET_IFACE (file);
4216 return (* iface->set_display_name_finish) (file, res, error);
4220 * g_file_query_settable_attributes:
4221 * @file: input #GFile
4222 * @cancellable: (allow-none): optional #GCancellable object,
4223 * %NULL to ignore
4224 * @error: a #GError, or %NULL
4226 * Obtain the list of settable attributes for the file.
4228 * Returns the type and full attribute name of all the attributes
4229 * that can be set on this file. This doesn't mean setting it will
4230 * always succeed though, you might get an access failure, or some
4231 * specific file may not support a specific attribute.
4233 * If @cancellable is not %NULL, then the operation can be cancelled by
4234 * triggering the cancellable object from another thread. If the operation
4235 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4237 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4238 * When you are done with it, release it with
4239 * g_file_attribute_info_list_unref()
4241 GFileAttributeInfoList *
4242 g_file_query_settable_attributes (GFile *file,
4243 GCancellable *cancellable,
4244 GError **error)
4246 GFileIface *iface;
4247 GError *my_error;
4248 GFileAttributeInfoList *list;
4250 g_return_val_if_fail (G_IS_FILE (file), NULL);
4252 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4253 return NULL;
4255 iface = G_FILE_GET_IFACE (file);
4257 if (iface->query_settable_attributes == NULL)
4258 return g_file_attribute_info_list_new ();
4260 my_error = NULL;
4261 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4263 if (list == NULL)
4265 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4267 list = g_file_attribute_info_list_new ();
4268 g_error_free (my_error);
4270 else
4271 g_propagate_error (error, my_error);
4274 return list;
4278 * g_file_query_writable_namespaces:
4279 * @file: input #GFile
4280 * @cancellable: (allow-none): optional #GCancellable object,
4281 * %NULL to ignore
4282 * @error: a #GError, or %NULL
4284 * Obtain the list of attribute namespaces where new attributes
4285 * can be created by a user. An example of this is extended
4286 * attributes (in the "xattr" namespace).
4288 * If @cancellable is not %NULL, then the operation can be cancelled by
4289 * triggering the cancellable object from another thread. If the operation
4290 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4292 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4293 * When you are done with it, release it with
4294 * g_file_attribute_info_list_unref()
4296 GFileAttributeInfoList *
4297 g_file_query_writable_namespaces (GFile *file,
4298 GCancellable *cancellable,
4299 GError **error)
4301 GFileIface *iface;
4302 GError *my_error;
4303 GFileAttributeInfoList *list;
4305 g_return_val_if_fail (G_IS_FILE (file), NULL);
4307 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4308 return NULL;
4310 iface = G_FILE_GET_IFACE (file);
4312 if (iface->query_writable_namespaces == NULL)
4313 return g_file_attribute_info_list_new ();
4315 my_error = NULL;
4316 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4318 if (list == NULL)
4320 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4322 list = g_file_attribute_info_list_new ();
4323 g_error_free (my_error);
4325 else
4326 g_propagate_error (error, my_error);
4329 return list;
4333 * g_file_set_attribute:
4334 * @file: input #GFile
4335 * @attribute: a string containing the attribute's name
4336 * @type: The type of the attribute
4337 * @value_p: (allow-none): a pointer to the value (or the pointer
4338 * itself if the type is a pointer type)
4339 * @flags: a set of #GFileQueryInfoFlags
4340 * @cancellable: (allow-none): optional #GCancellable object,
4341 * %NULL to ignore
4342 * @error: a #GError, or %NULL
4344 * Sets an attribute in the file with attribute name @attribute to @value.
4346 * Some attributes can be unset by setting @attribute to
4347 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4349 * If @cancellable is not %NULL, then the operation can be cancelled by
4350 * triggering the cancellable object from another thread. If the operation
4351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4353 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4355 gboolean
4356 g_file_set_attribute (GFile *file,
4357 const gchar *attribute,
4358 GFileAttributeType type,
4359 gpointer value_p,
4360 GFileQueryInfoFlags flags,
4361 GCancellable *cancellable,
4362 GError **error)
4364 GFileIface *iface;
4366 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4367 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4369 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4370 return FALSE;
4372 iface = G_FILE_GET_IFACE (file);
4374 if (iface->set_attribute == NULL)
4376 g_set_error_literal (error, G_IO_ERROR,
4377 G_IO_ERROR_NOT_SUPPORTED,
4378 _("Operation not supported"));
4379 return FALSE;
4382 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4386 * g_file_set_attributes_from_info:
4387 * @file: input #GFile
4388 * @info: a #GFileInfo
4389 * @flags: #GFileQueryInfoFlags
4390 * @cancellable: (allow-none): optional #GCancellable object,
4391 * %NULL to ignore
4392 * @error: a #GError, or %NULL
4394 * Tries to set all attributes in the #GFileInfo on the target
4395 * values, not stopping on the first error.
4397 * If there is any error during this operation then @error will
4398 * be set to the first error. Error on particular fields are flagged
4399 * by setting the "status" field in the attribute value to
4400 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4401 * also detect further errors.
4403 * If @cancellable is not %NULL, then the operation can be cancelled by
4404 * triggering the cancellable object from another thread. If the operation
4405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4407 * Returns: %FALSE if there was any error, %TRUE otherwise.
4409 gboolean
4410 g_file_set_attributes_from_info (GFile *file,
4411 GFileInfo *info,
4412 GFileQueryInfoFlags flags,
4413 GCancellable *cancellable,
4414 GError **error)
4416 GFileIface *iface;
4418 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4419 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4421 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4422 return FALSE;
4424 g_file_info_clear_status (info);
4426 iface = G_FILE_GET_IFACE (file);
4428 return (* iface->set_attributes_from_info) (file,
4429 info,
4430 flags,
4431 cancellable,
4432 error);
4435 static gboolean
4436 g_file_real_set_attributes_from_info (GFile *file,
4437 GFileInfo *info,
4438 GFileQueryInfoFlags flags,
4439 GCancellable *cancellable,
4440 GError **error)
4442 char **attributes;
4443 int i;
4444 gboolean res;
4445 GFileAttributeValue *value;
4447 res = TRUE;
4449 attributes = g_file_info_list_attributes (info, NULL);
4451 for (i = 0; attributes[i] != NULL; i++)
4453 value = _g_file_info_get_attribute_value (info, attributes[i]);
4455 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4456 continue;
4458 if (!g_file_set_attribute (file, attributes[i],
4459 value->type, _g_file_attribute_value_peek_as_pointer (value),
4460 flags, cancellable, error))
4462 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4463 res = FALSE;
4464 /* Don't set error multiple times */
4465 error = NULL;
4467 else
4468 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4471 g_strfreev (attributes);
4473 return res;
4477 * g_file_set_attributes_async:
4478 * @file: input #GFile
4479 * @info: a #GFileInfo
4480 * @flags: a #GFileQueryInfoFlags
4481 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4482 * of the request
4483 * @cancellable: (allow-none): optional #GCancellable object,
4484 * %NULL to ignore
4485 * @callback: (scope async): a #GAsyncReadyCallback
4486 * @user_data: (closure): a #gpointer
4488 * Asynchronously sets the attributes of @file with @info.
4490 * For more details, see g_file_set_attributes_from_info(),
4491 * which is the synchronous version of this call.
4493 * When the operation is finished, @callback will be called.
4494 * You can then call g_file_set_attributes_finish() to get
4495 * the result of the operation.
4497 void
4498 g_file_set_attributes_async (GFile *file,
4499 GFileInfo *info,
4500 GFileQueryInfoFlags flags,
4501 int io_priority,
4502 GCancellable *cancellable,
4503 GAsyncReadyCallback callback,
4504 gpointer user_data)
4506 GFileIface *iface;
4508 g_return_if_fail (G_IS_FILE (file));
4509 g_return_if_fail (G_IS_FILE_INFO (info));
4511 iface = G_FILE_GET_IFACE (file);
4512 (* iface->set_attributes_async) (file,
4513 info,
4514 flags,
4515 io_priority,
4516 cancellable,
4517 callback,
4518 user_data);
4522 * g_file_set_attributes_finish:
4523 * @file: input #GFile
4524 * @result: a #GAsyncResult
4525 * @info: (out) (transfer full): a #GFileInfo
4526 * @error: a #GError, or %NULL
4528 * Finishes setting an attribute started in g_file_set_attributes_async().
4530 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4532 gboolean
4533 g_file_set_attributes_finish (GFile *file,
4534 GAsyncResult *result,
4535 GFileInfo **info,
4536 GError **error)
4538 GFileIface *iface;
4540 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4541 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4543 /* No standard handling of errors here, as we must set info even
4544 * on errors
4546 iface = G_FILE_GET_IFACE (file);
4547 return (* iface->set_attributes_finish) (file, result, info, error);
4551 * g_file_set_attribute_string:
4552 * @file: input #GFile
4553 * @attribute: a string containing the attribute's name
4554 * @value: a string containing the attribute's value
4555 * @flags: #GFileQueryInfoFlags
4556 * @cancellable: (allow-none): optional #GCancellable object,
4557 * %NULL to ignore
4558 * @error: a #GError, or %NULL
4560 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4561 * If @attribute is of a different type, this operation will fail.
4563 * If @cancellable is not %NULL, then the operation can be cancelled by
4564 * triggering the cancellable object from another thread. If the operation
4565 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4567 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4569 gboolean
4570 g_file_set_attribute_string (GFile *file,
4571 const char *attribute,
4572 const char *value,
4573 GFileQueryInfoFlags flags,
4574 GCancellable *cancellable,
4575 GError **error)
4577 return g_file_set_attribute (file, attribute,
4578 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4579 flags, cancellable, error);
4583 * g_file_set_attribute_byte_string:
4584 * @file: input #GFile
4585 * @attribute: a string containing the attribute's name
4586 * @value: a string containing the attribute's new value
4587 * @flags: a #GFileQueryInfoFlags
4588 * @cancellable: (allow-none): optional #GCancellable object,
4589 * %NULL to ignore
4590 * @error: a #GError, or %NULL
4592 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4593 * If @attribute is of a different type, this operation will fail,
4594 * returning %FALSE.
4596 * If @cancellable is not %NULL, then the operation can be cancelled by
4597 * triggering the cancellable object from another thread. If the operation
4598 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4600 * Returns: %TRUE if the @attribute was successfully set to @value
4601 * in the @file, %FALSE otherwise.
4603 gboolean
4604 g_file_set_attribute_byte_string (GFile *file,
4605 const gchar *attribute,
4606 const gchar *value,
4607 GFileQueryInfoFlags flags,
4608 GCancellable *cancellable,
4609 GError **error)
4611 return g_file_set_attribute (file, attribute,
4612 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4613 flags, cancellable, error);
4617 * g_file_set_attribute_uint32:
4618 * @file: input #GFile
4619 * @attribute: a string containing the attribute's name
4620 * @value: a #guint32 containing the attribute's new value
4621 * @flags: a #GFileQueryInfoFlags
4622 * @cancellable: (allow-none): optional #GCancellable object,
4623 * %NULL to ignore
4624 * @error: a #GError, or %NULL
4626 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4627 * If @attribute is of a different type, this operation will fail.
4629 * If @cancellable is not %NULL, then the operation can be cancelled by
4630 * triggering the cancellable object from another thread. If the operation
4631 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4633 * Returns: %TRUE if the @attribute was successfully set to @value
4634 * in the @file, %FALSE otherwise.
4636 gboolean
4637 g_file_set_attribute_uint32 (GFile *file,
4638 const gchar *attribute,
4639 guint32 value,
4640 GFileQueryInfoFlags flags,
4641 GCancellable *cancellable,
4642 GError **error)
4644 return g_file_set_attribute (file, attribute,
4645 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4646 flags, cancellable, error);
4650 * g_file_set_attribute_int32:
4651 * @file: input #GFile
4652 * @attribute: a string containing the attribute's name
4653 * @value: a #gint32 containing the attribute's new value
4654 * @flags: a #GFileQueryInfoFlags
4655 * @cancellable: (allow-none): optional #GCancellable object,
4656 * %NULL to ignore
4657 * @error: a #GError, or %NULL
4659 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4660 * If @attribute is of a different type, this operation will fail.
4662 * If @cancellable is not %NULL, then the operation can be cancelled by
4663 * triggering the cancellable object from another thread. If the operation
4664 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4666 * Returns: %TRUE if the @attribute was successfully set to @value
4667 * in the @file, %FALSE otherwise.
4669 gboolean
4670 g_file_set_attribute_int32 (GFile *file,
4671 const gchar *attribute,
4672 gint32 value,
4673 GFileQueryInfoFlags flags,
4674 GCancellable *cancellable,
4675 GError **error)
4677 return g_file_set_attribute (file, attribute,
4678 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4679 flags, cancellable, error);
4683 * g_file_set_attribute_uint64:
4684 * @file: input #GFile
4685 * @attribute: a string containing the attribute's name
4686 * @value: a #guint64 containing the attribute's new value
4687 * @flags: a #GFileQueryInfoFlags
4688 * @cancellable: (allow-none): optional #GCancellable object,
4689 * %NULL to ignore
4690 * @error: a #GError, or %NULL
4692 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4693 * If @attribute is of a different type, this operation will fail.
4695 * If @cancellable is not %NULL, then the operation can be cancelled by
4696 * triggering the cancellable object from another thread. If the operation
4697 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4699 * Returns: %TRUE if the @attribute was successfully set to @value
4700 * in the @file, %FALSE otherwise.
4702 gboolean
4703 g_file_set_attribute_uint64 (GFile *file,
4704 const gchar *attribute,
4705 guint64 value,
4706 GFileQueryInfoFlags flags,
4707 GCancellable *cancellable,
4708 GError **error)
4710 return g_file_set_attribute (file, attribute,
4711 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4712 flags, cancellable, error);
4716 * g_file_set_attribute_int64:
4717 * @file: input #GFile
4718 * @attribute: a string containing the attribute's name
4719 * @value: a #guint64 containing the attribute's new value
4720 * @flags: a #GFileQueryInfoFlags
4721 * @cancellable: (allow-none): optional #GCancellable object,
4722 * %NULL to ignore
4723 * @error: a #GError, or %NULL
4725 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4726 * If @attribute is of a different type, this operation will fail.
4728 * If @cancellable is not %NULL, then the operation can be cancelled by
4729 * triggering the cancellable object from another thread. If the operation
4730 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4732 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4734 gboolean
4735 g_file_set_attribute_int64 (GFile *file,
4736 const gchar *attribute,
4737 gint64 value,
4738 GFileQueryInfoFlags flags,
4739 GCancellable *cancellable,
4740 GError **error)
4742 return g_file_set_attribute (file, attribute,
4743 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4744 flags, cancellable, error);
4748 * g_file_mount_mountable:
4749 * @file: input #GFile
4750 * @flags: flags affecting the operation
4751 * @mount_operation: (allow-none): a #GMountOperation,
4752 * or %NULL to avoid user interaction
4753 * @cancellable: (allow-none): optional #GCancellable object,
4754 * %NULL to ignore
4755 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4756 * when the request is satisfied, or %NULL
4757 * @user_data: (closure): the data to pass to callback function
4759 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4760 * Using @mount_operation, you can request callbacks when, for instance,
4761 * passwords are needed during authentication.
4763 * If @cancellable is not %NULL, then the operation can be cancelled by
4764 * triggering the cancellable object from another thread. If the operation
4765 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4767 * When the operation is finished, @callback will be called.
4768 * You can then call g_file_mount_mountable_finish() to get
4769 * the result of the operation.
4771 void
4772 g_file_mount_mountable (GFile *file,
4773 GMountMountFlags flags,
4774 GMountOperation *mount_operation,
4775 GCancellable *cancellable,
4776 GAsyncReadyCallback callback,
4777 gpointer user_data)
4779 GFileIface *iface;
4781 g_return_if_fail (G_IS_FILE (file));
4783 iface = G_FILE_GET_IFACE (file);
4785 if (iface->mount_mountable == NULL)
4787 g_task_report_new_error (file, callback, user_data,
4788 g_file_mount_mountable,
4789 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4790 _("Operation not supported"));
4791 return;
4794 (* iface->mount_mountable) (file,
4795 flags,
4796 mount_operation,
4797 cancellable,
4798 callback,
4799 user_data);
4803 * g_file_mount_mountable_finish:
4804 * @file: input #GFile
4805 * @result: a #GAsyncResult
4806 * @error: a #GError, or %NULL
4808 * Finishes a mount operation. See g_file_mount_mountable() for details.
4810 * Finish an asynchronous mount operation that was started
4811 * with g_file_mount_mountable().
4813 * Returns: (transfer full): a #GFile or %NULL on error.
4814 * Free the returned object with g_object_unref().
4816 GFile *
4817 g_file_mount_mountable_finish (GFile *file,
4818 GAsyncResult *result,
4819 GError **error)
4821 GFileIface *iface;
4823 g_return_val_if_fail (G_IS_FILE (file), NULL);
4824 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4826 if (g_async_result_legacy_propagate_error (result, error))
4827 return NULL;
4828 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4829 return g_task_propagate_pointer (G_TASK (result), error);
4831 iface = G_FILE_GET_IFACE (file);
4832 return (* iface->mount_mountable_finish) (file, result, error);
4836 * g_file_unmount_mountable:
4837 * @file: input #GFile
4838 * @flags: flags affecting the operation
4839 * @cancellable: (allow-none): optional #GCancellable object,
4840 * %NULL to ignore
4841 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4842 * when the request is satisfied, or %NULL
4843 * @user_data: (closure): the data to pass to callback function
4845 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4847 * If @cancellable is not %NULL, then the operation can be cancelled by
4848 * triggering the cancellable object from another thread. If the operation
4849 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4851 * When the operation is finished, @callback will be called.
4852 * You can then call g_file_unmount_mountable_finish() to get
4853 * the result of the operation.
4855 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4857 void
4858 g_file_unmount_mountable (GFile *file,
4859 GMountUnmountFlags flags,
4860 GCancellable *cancellable,
4861 GAsyncReadyCallback callback,
4862 gpointer user_data)
4864 GFileIface *iface;
4866 g_return_if_fail (G_IS_FILE (file));
4868 iface = G_FILE_GET_IFACE (file);
4870 if (iface->unmount_mountable == NULL)
4872 g_task_report_new_error (file, callback, user_data,
4873 g_file_unmount_mountable_with_operation,
4874 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4875 _("Operation not supported"));
4876 return;
4879 (* iface->unmount_mountable) (file,
4880 flags,
4881 cancellable,
4882 callback,
4883 user_data);
4887 * g_file_unmount_mountable_finish:
4888 * @file: input #GFile
4889 * @result: a #GAsyncResult
4890 * @error: a #GError, or %NULL
4892 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4894 * Finish an asynchronous unmount operation that was started
4895 * with g_file_unmount_mountable().
4897 * Returns: %TRUE if the operation finished successfully.
4898 * %FALSE otherwise.
4900 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4901 * instead.
4903 gboolean
4904 g_file_unmount_mountable_finish (GFile *file,
4905 GAsyncResult *result,
4906 GError **error)
4908 GFileIface *iface;
4910 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4911 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4913 if (g_async_result_legacy_propagate_error (result, error))
4914 return FALSE;
4915 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4916 return g_task_propagate_boolean (G_TASK (result), error);
4918 iface = G_FILE_GET_IFACE (file);
4919 return (* iface->unmount_mountable_finish) (file, result, error);
4923 * g_file_unmount_mountable_with_operation:
4924 * @file: input #GFile
4925 * @flags: flags affecting the operation
4926 * @mount_operation: (allow-none): a #GMountOperation,
4927 * or %NULL to avoid user interaction
4928 * @cancellable: (allow-none): optional #GCancellable object,
4929 * %NULL to ignore
4930 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4931 * when the request is satisfied, or %NULL
4932 * @user_data: (closure): the data to pass to callback function
4934 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4936 * If @cancellable is not %NULL, then the operation can be cancelled by
4937 * triggering the cancellable object from another thread. If the operation
4938 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4940 * When the operation is finished, @callback will be called.
4941 * You can then call g_file_unmount_mountable_finish() to get
4942 * the result of the operation.
4944 * Since: 2.22
4946 void
4947 g_file_unmount_mountable_with_operation (GFile *file,
4948 GMountUnmountFlags flags,
4949 GMountOperation *mount_operation,
4950 GCancellable *cancellable,
4951 GAsyncReadyCallback callback,
4952 gpointer user_data)
4954 GFileIface *iface;
4956 g_return_if_fail (G_IS_FILE (file));
4958 iface = G_FILE_GET_IFACE (file);
4960 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4962 g_task_report_new_error (file, callback, user_data,
4963 g_file_unmount_mountable_with_operation,
4964 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4965 _("Operation not supported"));
4966 return;
4969 if (iface->unmount_mountable_with_operation != NULL)
4970 (* iface->unmount_mountable_with_operation) (file,
4971 flags,
4972 mount_operation,
4973 cancellable,
4974 callback,
4975 user_data);
4976 else
4977 (* iface->unmount_mountable) (file,
4978 flags,
4979 cancellable,
4980 callback,
4981 user_data);
4985 * g_file_unmount_mountable_with_operation_finish:
4986 * @file: input #GFile
4987 * @result: a #GAsyncResult
4988 * @error: a #GError, or %NULL
4990 * Finishes an unmount operation,
4991 * see g_file_unmount_mountable_with_operation() for details.
4993 * Finish an asynchronous unmount operation that was started
4994 * with g_file_unmount_mountable_with_operation().
4996 * Returns: %TRUE if the operation finished successfully.
4997 * %FALSE otherwise.
4999 * Since: 2.22
5001 gboolean
5002 g_file_unmount_mountable_with_operation_finish (GFile *file,
5003 GAsyncResult *result,
5004 GError **error)
5006 GFileIface *iface;
5008 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5009 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5011 if (g_async_result_legacy_propagate_error (result, error))
5012 return FALSE;
5013 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5014 return g_task_propagate_boolean (G_TASK (result), error);
5016 iface = G_FILE_GET_IFACE (file);
5017 if (iface->unmount_mountable_with_operation_finish != NULL)
5018 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5019 else
5020 return (* iface->unmount_mountable_finish) (file, result, error);
5024 * g_file_eject_mountable:
5025 * @file: input #GFile
5026 * @flags: flags affecting the operation
5027 * @cancellable: (allow-none): optional #GCancellable object,
5028 * %NULL to ignore
5029 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5030 * when the request is satisfied, or %NULL
5031 * @user_data: (closure): the data to pass to callback function
5033 * Starts an asynchronous eject on a mountable.
5034 * When this operation has completed, @callback will be called with
5035 * @user_user data, and the operation can be finalized with
5036 * g_file_eject_mountable_finish().
5038 * If @cancellable is not %NULL, then the operation can be cancelled by
5039 * triggering the cancellable object from another thread. If the operation
5040 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5042 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5044 void
5045 g_file_eject_mountable (GFile *file,
5046 GMountUnmountFlags flags,
5047 GCancellable *cancellable,
5048 GAsyncReadyCallback callback,
5049 gpointer user_data)
5051 GFileIface *iface;
5053 g_return_if_fail (G_IS_FILE (file));
5055 iface = G_FILE_GET_IFACE (file);
5057 if (iface->eject_mountable == NULL)
5059 g_task_report_new_error (file, callback, user_data,
5060 g_file_eject_mountable_with_operation,
5061 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5062 _("Operation not supported"));
5063 return;
5066 (* iface->eject_mountable) (file,
5067 flags,
5068 cancellable,
5069 callback,
5070 user_data);
5074 * g_file_eject_mountable_finish:
5075 * @file: input #GFile
5076 * @result: a #GAsyncResult
5077 * @error: a #GError, or %NULL
5079 * Finishes an asynchronous eject operation started by
5080 * g_file_eject_mountable().
5082 * Returns: %TRUE if the @file was ejected successfully.
5083 * %FALSE otherwise.
5085 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5086 * instead.
5088 gboolean
5089 g_file_eject_mountable_finish (GFile *file,
5090 GAsyncResult *result,
5091 GError **error)
5093 GFileIface *iface;
5095 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5096 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5098 if (g_async_result_legacy_propagate_error (result, error))
5099 return FALSE;
5100 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5101 return g_task_propagate_boolean (G_TASK (result), error);
5103 iface = G_FILE_GET_IFACE (file);
5104 return (* iface->eject_mountable_finish) (file, result, error);
5108 * g_file_eject_mountable_with_operation:
5109 * @file: input #GFile
5110 * @flags: flags affecting the operation
5111 * @mount_operation: (allow-none): a #GMountOperation,
5112 * or %NULL to avoid user interaction
5113 * @cancellable: (allow-none): optional #GCancellable object,
5114 * %NULL to ignore
5115 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5116 * when the request is satisfied, or %NULL
5117 * @user_data: (closure): the data to pass to callback function
5119 * Starts an asynchronous eject on a mountable.
5120 * When this operation has completed, @callback will be called with
5121 * @user_user data, and the operation can be finalized with
5122 * g_file_eject_mountable_with_operation_finish().
5124 * If @cancellable is not %NULL, then the operation can be cancelled by
5125 * triggering the cancellable object from another thread. If the operation
5126 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5128 * Since: 2.22
5130 void
5131 g_file_eject_mountable_with_operation (GFile *file,
5132 GMountUnmountFlags flags,
5133 GMountOperation *mount_operation,
5134 GCancellable *cancellable,
5135 GAsyncReadyCallback callback,
5136 gpointer user_data)
5138 GFileIface *iface;
5140 g_return_if_fail (G_IS_FILE (file));
5142 iface = G_FILE_GET_IFACE (file);
5144 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5146 g_task_report_new_error (file, callback, user_data,
5147 g_file_eject_mountable_with_operation,
5148 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5149 _("Operation not supported"));
5150 return;
5153 if (iface->eject_mountable_with_operation != NULL)
5154 (* iface->eject_mountable_with_operation) (file,
5155 flags,
5156 mount_operation,
5157 cancellable,
5158 callback,
5159 user_data);
5160 else
5161 (* iface->eject_mountable) (file,
5162 flags,
5163 cancellable,
5164 callback,
5165 user_data);
5169 * g_file_eject_mountable_with_operation_finish:
5170 * @file: input #GFile
5171 * @result: a #GAsyncResult
5172 * @error: a #GError, or %NULL
5174 * Finishes an asynchronous eject operation started by
5175 * g_file_eject_mountable_with_operation().
5177 * Returns: %TRUE if the @file was ejected successfully.
5178 * %FALSE otherwise.
5180 * Since: 2.22
5182 gboolean
5183 g_file_eject_mountable_with_operation_finish (GFile *file,
5184 GAsyncResult *result,
5185 GError **error)
5187 GFileIface *iface;
5189 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5190 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5192 if (g_async_result_legacy_propagate_error (result, error))
5193 return FALSE;
5194 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5195 return g_task_propagate_boolean (G_TASK (result), error);
5197 iface = G_FILE_GET_IFACE (file);
5198 if (iface->eject_mountable_with_operation_finish != NULL)
5199 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5200 else
5201 return (* iface->eject_mountable_finish) (file, result, error);
5205 * g_file_monitor_directory:
5206 * @file: input #GFile
5207 * @flags: a set of #GFileMonitorFlags
5208 * @cancellable: (allow-none): optional #GCancellable object,
5209 * %NULL to ignore
5210 * @error: a #GError, or %NULL
5212 * Obtains a directory monitor for the given file.
5213 * This may fail if directory monitoring is not supported.
5215 * If @cancellable is not %NULL, then the operation can be cancelled by
5216 * triggering the cancellable object from another thread. If the operation
5217 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5219 * It does not make sense for @flags to contain
5220 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5221 * directories. It is not possible to monitor all the files in a
5222 * directory for changes made via hard links; if you want to do this then
5223 * you must register individual watches with g_file_monitor().
5225 * Virtual: monitor_dir
5226 * Returns: (transfer full): a #GFileMonitor for the given @file,
5227 * or %NULL on error.
5228 * Free the returned object with g_object_unref().
5230 GFileMonitor *
5231 g_file_monitor_directory (GFile *file,
5232 GFileMonitorFlags flags,
5233 GCancellable *cancellable,
5234 GError **error)
5236 GFileIface *iface;
5238 g_return_val_if_fail (G_IS_FILE (file), NULL);
5239 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5241 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5242 return NULL;
5244 iface = G_FILE_GET_IFACE (file);
5246 if (iface->monitor_dir == NULL)
5248 g_set_error_literal (error, G_IO_ERROR,
5249 G_IO_ERROR_NOT_SUPPORTED,
5250 _("Operation not supported"));
5251 return NULL;
5254 return (* iface->monitor_dir) (file, flags, cancellable, error);
5258 * g_file_monitor_file:
5259 * @file: input #GFile
5260 * @flags: a set of #GFileMonitorFlags
5261 * @cancellable: (allow-none): optional #GCancellable object,
5262 * %NULL to ignore
5263 * @error: a #GError, or %NULL
5265 * Obtains a file monitor for the given file. If no file notification
5266 * mechanism exists, then regular polling of the file is used.
5268 * If @cancellable is not %NULL, then the operation can be cancelled by
5269 * triggering the cancellable object from another thread. If the operation
5270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5272 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5273 * will also attempt to report changes made to the file via another
5274 * filename (ie, a hard link). Without this flag, you can only rely on
5275 * changes made through the filename contained in @file to be
5276 * reported. Using this flag may result in an increase in resource
5277 * usage, and may not have any effect depending on the #GFileMonitor
5278 * backend and/or filesystem type.
5280 * Returns: (transfer full): a #GFileMonitor for the given @file,
5281 * or %NULL on error.
5282 * Free the returned object with g_object_unref().
5284 GFileMonitor *
5285 g_file_monitor_file (GFile *file,
5286 GFileMonitorFlags flags,
5287 GCancellable *cancellable,
5288 GError **error)
5290 GFileIface *iface;
5291 GFileMonitor *monitor;
5293 g_return_val_if_fail (G_IS_FILE (file), NULL);
5295 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5296 return NULL;
5298 iface = G_FILE_GET_IFACE (file);
5300 monitor = NULL;
5302 if (iface->monitor_file)
5303 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5305 /* Fallback to polling */
5306 if (monitor == NULL)
5307 monitor = _g_poll_file_monitor_new (file);
5309 return monitor;
5313 * g_file_monitor:
5314 * @file: input #GFile
5315 * @flags: a set of #GFileMonitorFlags
5316 * @cancellable: (allow-none): optional #GCancellable object,
5317 * %NULL to ignore
5318 * @error: a #GError, or %NULL
5320 * Obtains a file or directory monitor for the given file,
5321 * depending on the type of the file.
5323 * If @cancellable is not %NULL, then the operation can be cancelled by
5324 * triggering the cancellable object from another thread. If the operation
5325 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5327 * Returns: (transfer full): a #GFileMonitor for the given @file,
5328 * or %NULL on error.
5329 * Free the returned object with g_object_unref().
5331 * Since: 2.18
5333 GFileMonitor *
5334 g_file_monitor (GFile *file,
5335 GFileMonitorFlags flags,
5336 GCancellable *cancellable,
5337 GError **error)
5339 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5340 return g_file_monitor_directory (file,
5341 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5342 cancellable, error);
5343 else
5344 return g_file_monitor_file (file, flags, cancellable, error);
5347 /********************************************
5348 * Default implementation of async ops *
5349 ********************************************/
5351 typedef struct {
5352 char *attributes;
5353 GFileQueryInfoFlags flags;
5354 } QueryInfoAsyncData;
5356 static void
5357 query_info_data_free (QueryInfoAsyncData *data)
5359 g_free (data->attributes);
5360 g_free (data);
5363 static void
5364 query_info_async_thread (GTask *task,
5365 gpointer object,
5366 gpointer task_data,
5367 GCancellable *cancellable)
5369 QueryInfoAsyncData *data = task_data;
5370 GFileInfo *info;
5371 GError *error = NULL;
5373 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5374 if (info)
5375 g_task_return_pointer (task, info, g_object_unref);
5376 else
5377 g_task_return_error (task, error);
5380 static void
5381 g_file_real_query_info_async (GFile *file,
5382 const char *attributes,
5383 GFileQueryInfoFlags flags,
5384 int io_priority,
5385 GCancellable *cancellable,
5386 GAsyncReadyCallback callback,
5387 gpointer user_data)
5389 GTask *task;
5390 QueryInfoAsyncData *data;
5392 data = g_new0 (QueryInfoAsyncData, 1);
5393 data->attributes = g_strdup (attributes);
5394 data->flags = flags;
5396 task = g_task_new (file, cancellable, callback, user_data);
5397 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5398 g_task_set_priority (task, io_priority);
5399 g_task_run_in_thread (task, query_info_async_thread);
5400 g_object_unref (task);
5403 static GFileInfo *
5404 g_file_real_query_info_finish (GFile *file,
5405 GAsyncResult *res,
5406 GError **error)
5408 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5410 return g_task_propagate_pointer (G_TASK (res), error);
5413 static void
5414 query_filesystem_info_async_thread (GTask *task,
5415 gpointer object,
5416 gpointer task_data,
5417 GCancellable *cancellable)
5419 const char *attributes = task_data;
5420 GFileInfo *info;
5421 GError *error = NULL;
5423 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5424 if (info)
5425 g_task_return_pointer (task, info, g_object_unref);
5426 else
5427 g_task_return_error (task, error);
5430 static void
5431 g_file_real_query_filesystem_info_async (GFile *file,
5432 const char *attributes,
5433 int io_priority,
5434 GCancellable *cancellable,
5435 GAsyncReadyCallback callback,
5436 gpointer user_data)
5438 GTask *task;
5440 task = g_task_new (file, cancellable, callback, user_data);
5441 g_task_set_task_data (task, g_strdup (attributes), g_free);
5442 g_task_set_priority (task, io_priority);
5443 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5444 g_object_unref (task);
5447 static GFileInfo *
5448 g_file_real_query_filesystem_info_finish (GFile *file,
5449 GAsyncResult *res,
5450 GError **error)
5452 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5454 return g_task_propagate_pointer (G_TASK (res), error);
5457 static void
5458 enumerate_children_async_thread (GTask *task,
5459 gpointer object,
5460 gpointer task_data,
5461 GCancellable *cancellable)
5463 QueryInfoAsyncData *data = task_data;
5464 GFileEnumerator *enumerator;
5465 GError *error = NULL;
5467 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5468 if (error)
5469 g_task_return_error (task, error);
5470 else
5471 g_task_return_pointer (task, enumerator, g_object_unref);
5474 static void
5475 g_file_real_enumerate_children_async (GFile *file,
5476 const char *attributes,
5477 GFileQueryInfoFlags flags,
5478 int io_priority,
5479 GCancellable *cancellable,
5480 GAsyncReadyCallback callback,
5481 gpointer user_data)
5483 GTask *task;
5484 QueryInfoAsyncData *data;
5486 data = g_new0 (QueryInfoAsyncData, 1);
5487 data->attributes = g_strdup (attributes);
5488 data->flags = flags;
5490 task = g_task_new (file, cancellable, callback, user_data);
5491 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5492 g_task_set_priority (task, io_priority);
5493 g_task_run_in_thread (task, enumerate_children_async_thread);
5494 g_object_unref (task);
5497 static GFileEnumerator *
5498 g_file_real_enumerate_children_finish (GFile *file,
5499 GAsyncResult *res,
5500 GError **error)
5502 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5504 return g_task_propagate_pointer (G_TASK (res), error);
5507 static void
5508 open_read_async_thread (GTask *task,
5509 gpointer object,
5510 gpointer task_data,
5511 GCancellable *cancellable)
5513 GFileInputStream *stream;
5514 GError *error = NULL;
5516 stream = g_file_read (G_FILE (object), cancellable, &error);
5517 if (stream)
5518 g_task_return_pointer (task, stream, g_object_unref);
5519 else
5520 g_task_return_error (task, error);
5523 static void
5524 g_file_real_read_async (GFile *file,
5525 int io_priority,
5526 GCancellable *cancellable,
5527 GAsyncReadyCallback callback,
5528 gpointer user_data)
5530 GTask *task;
5532 task = g_task_new (file, cancellable, callback, user_data);
5533 g_task_set_priority (task, io_priority);
5534 g_task_run_in_thread (task, open_read_async_thread);
5535 g_object_unref (task);
5538 static GFileInputStream *
5539 g_file_real_read_finish (GFile *file,
5540 GAsyncResult *res,
5541 GError **error)
5543 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5545 return g_task_propagate_pointer (G_TASK (res), error);
5548 static void
5549 append_to_async_thread (GTask *task,
5550 gpointer source_object,
5551 gpointer task_data,
5552 GCancellable *cancellable)
5554 GFileCreateFlags *data = task_data;
5555 GFileOutputStream *stream;
5556 GError *error = NULL;
5558 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5559 if (stream)
5560 g_task_return_pointer (task, stream, g_object_unref);
5561 else
5562 g_task_return_error (task, error);
5565 static void
5566 g_file_real_append_to_async (GFile *file,
5567 GFileCreateFlags flags,
5568 int io_priority,
5569 GCancellable *cancellable,
5570 GAsyncReadyCallback callback,
5571 gpointer user_data)
5573 GFileCreateFlags *data;
5574 GTask *task;
5576 data = g_new0 (GFileCreateFlags, 1);
5577 *data = flags;
5579 task = g_task_new (file, cancellable, callback, user_data);
5580 g_task_set_task_data (task, data, g_free);
5581 g_task_set_priority (task, io_priority);
5583 g_task_run_in_thread (task, append_to_async_thread);
5584 g_object_unref (task);
5587 static GFileOutputStream *
5588 g_file_real_append_to_finish (GFile *file,
5589 GAsyncResult *res,
5590 GError **error)
5592 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5594 return g_task_propagate_pointer (G_TASK (res), error);
5597 static void
5598 create_async_thread (GTask *task,
5599 gpointer source_object,
5600 gpointer task_data,
5601 GCancellable *cancellable)
5603 GFileCreateFlags *data = task_data;
5604 GFileOutputStream *stream;
5605 GError *error = NULL;
5607 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5608 if (stream)
5609 g_task_return_pointer (task, stream, g_object_unref);
5610 else
5611 g_task_return_error (task, error);
5614 static void
5615 g_file_real_create_async (GFile *file,
5616 GFileCreateFlags flags,
5617 int io_priority,
5618 GCancellable *cancellable,
5619 GAsyncReadyCallback callback,
5620 gpointer user_data)
5622 GFileCreateFlags *data;
5623 GTask *task;
5625 data = g_new0 (GFileCreateFlags, 1);
5626 *data = flags;
5628 task = g_task_new (file, cancellable, callback, user_data);
5629 g_task_set_task_data (task, data, g_free);
5630 g_task_set_priority (task, io_priority);
5632 g_task_run_in_thread (task, create_async_thread);
5633 g_object_unref (task);
5636 static GFileOutputStream *
5637 g_file_real_create_finish (GFile *file,
5638 GAsyncResult *res,
5639 GError **error)
5641 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5643 return g_task_propagate_pointer (G_TASK (res), error);
5646 typedef struct {
5647 GFileOutputStream *stream;
5648 char *etag;
5649 gboolean make_backup;
5650 GFileCreateFlags flags;
5651 } ReplaceAsyncData;
5653 static void
5654 replace_async_data_free (ReplaceAsyncData *data)
5656 if (data->stream)
5657 g_object_unref (data->stream);
5658 g_free (data->etag);
5659 g_free (data);
5662 static void
5663 replace_async_thread (GTask *task,
5664 gpointer source_object,
5665 gpointer task_data,
5666 GCancellable *cancellable)
5668 GFileOutputStream *stream;
5669 ReplaceAsyncData *data = task_data;
5670 GError *error = NULL;
5672 stream = g_file_replace (G_FILE (source_object),
5673 data->etag,
5674 data->make_backup,
5675 data->flags,
5676 cancellable,
5677 &error);
5679 if (stream)
5680 g_task_return_pointer (task, stream, g_object_unref);
5681 else
5682 g_task_return_error (task, error);
5685 static void
5686 g_file_real_replace_async (GFile *file,
5687 const char *etag,
5688 gboolean make_backup,
5689 GFileCreateFlags flags,
5690 int io_priority,
5691 GCancellable *cancellable,
5692 GAsyncReadyCallback callback,
5693 gpointer user_data)
5695 GTask *task;
5696 ReplaceAsyncData *data;
5698 data = g_new0 (ReplaceAsyncData, 1);
5699 data->etag = g_strdup (etag);
5700 data->make_backup = make_backup;
5701 data->flags = flags;
5703 task = g_task_new (file, cancellable, callback, user_data);
5704 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5705 g_task_set_priority (task, io_priority);
5707 g_task_run_in_thread (task, replace_async_thread);
5708 g_object_unref (task);
5711 static GFileOutputStream *
5712 g_file_real_replace_finish (GFile *file,
5713 GAsyncResult *res,
5714 GError **error)
5716 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5718 return g_task_propagate_pointer (G_TASK (res), error);
5721 static void
5722 delete_async_thread (GTask *task,
5723 gpointer object,
5724 gpointer task_data,
5725 GCancellable *cancellable)
5727 GError *error = NULL;
5729 if (g_file_delete (G_FILE (object), cancellable, &error))
5730 g_task_return_boolean (task, TRUE);
5731 else
5732 g_task_return_error (task, error);
5735 static void
5736 g_file_real_delete_async (GFile *file,
5737 int io_priority,
5738 GCancellable *cancellable,
5739 GAsyncReadyCallback callback,
5740 gpointer user_data)
5742 GTask *task;
5744 task = g_task_new (file, cancellable, callback, user_data);
5745 g_task_set_priority (task, io_priority);
5746 g_task_run_in_thread (task, delete_async_thread);
5747 g_object_unref (task);
5750 static gboolean
5751 g_file_real_delete_finish (GFile *file,
5752 GAsyncResult *res,
5753 GError **error)
5755 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5757 return g_task_propagate_boolean (G_TASK (res), error);
5760 static void
5761 trash_async_thread (GTask *task,
5762 gpointer object,
5763 gpointer task_data,
5764 GCancellable *cancellable)
5766 GError *error = NULL;
5768 if (g_file_trash (G_FILE (object), cancellable, &error))
5769 g_task_return_boolean (task, TRUE);
5770 else
5771 g_task_return_error (task, error);
5774 static void
5775 g_file_real_trash_async (GFile *file,
5776 int io_priority,
5777 GCancellable *cancellable,
5778 GAsyncReadyCallback callback,
5779 gpointer user_data)
5781 GTask *task;
5783 task = g_task_new (file, cancellable, callback, user_data);
5784 g_task_set_priority (task, io_priority);
5785 g_task_run_in_thread (task, trash_async_thread);
5786 g_object_unref (task);
5789 static gboolean
5790 g_file_real_trash_finish (GFile *file,
5791 GAsyncResult *res,
5792 GError **error)
5794 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5796 return g_task_propagate_boolean (G_TASK (res), error);
5799 static void
5800 make_directory_async_thread (GTask *task,
5801 gpointer object,
5802 gpointer task_data,
5803 GCancellable *cancellable)
5805 GError *error = NULL;
5807 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5808 g_task_return_boolean (task, TRUE);
5809 else
5810 g_task_return_error (task, error);
5813 static void
5814 g_file_real_make_directory_async (GFile *file,
5815 int io_priority,
5816 GCancellable *cancellable,
5817 GAsyncReadyCallback callback,
5818 gpointer user_data)
5820 GTask *task;
5822 task = g_task_new (file, cancellable, callback, user_data);
5823 g_task_set_priority (task, io_priority);
5824 g_task_run_in_thread (task, make_directory_async_thread);
5825 g_object_unref (task);
5828 static gboolean
5829 g_file_real_make_directory_finish (GFile *file,
5830 GAsyncResult *res,
5831 GError **error)
5833 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5835 return g_task_propagate_boolean (G_TASK (res), error);
5838 static void
5839 open_readwrite_async_thread (GTask *task,
5840 gpointer object,
5841 gpointer task_data,
5842 GCancellable *cancellable)
5844 GFileIOStream *stream;
5845 GError *error = NULL;
5847 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5849 if (stream == NULL)
5850 g_task_return_error (task, error);
5851 else
5852 g_task_return_pointer (task, stream, g_object_unref);
5855 static void
5856 g_file_real_open_readwrite_async (GFile *file,
5857 int io_priority,
5858 GCancellable *cancellable,
5859 GAsyncReadyCallback callback,
5860 gpointer user_data)
5862 GTask *task;
5864 task = g_task_new (file, cancellable, callback, user_data);
5865 g_task_set_priority (task, io_priority);
5867 g_task_run_in_thread (task, open_readwrite_async_thread);
5868 g_object_unref (task);
5871 static GFileIOStream *
5872 g_file_real_open_readwrite_finish (GFile *file,
5873 GAsyncResult *res,
5874 GError **error)
5876 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5878 return g_task_propagate_pointer (G_TASK (res), error);
5881 static void
5882 create_readwrite_async_thread (GTask *task,
5883 gpointer object,
5884 gpointer task_data,
5885 GCancellable *cancellable)
5887 GFileCreateFlags *data = task_data;
5888 GFileIOStream *stream;
5889 GError *error = NULL;
5891 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5893 if (stream == NULL)
5894 g_task_return_error (task, error);
5895 else
5896 g_task_return_pointer (task, stream, g_object_unref);
5899 static void
5900 g_file_real_create_readwrite_async (GFile *file,
5901 GFileCreateFlags flags,
5902 int io_priority,
5903 GCancellable *cancellable,
5904 GAsyncReadyCallback callback,
5905 gpointer user_data)
5907 GFileCreateFlags *data;
5908 GTask *task;
5910 data = g_new0 (GFileCreateFlags, 1);
5911 *data = flags;
5913 task = g_task_new (file, cancellable, callback, user_data);
5914 g_task_set_task_data (task, data, g_free);
5915 g_task_set_priority (task, io_priority);
5917 g_task_run_in_thread (task, create_readwrite_async_thread);
5918 g_object_unref (task);
5921 static GFileIOStream *
5922 g_file_real_create_readwrite_finish (GFile *file,
5923 GAsyncResult *res,
5924 GError **error)
5926 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5928 return g_task_propagate_pointer (G_TASK (res), error);
5931 typedef struct {
5932 char *etag;
5933 gboolean make_backup;
5934 GFileCreateFlags flags;
5935 } ReplaceRWAsyncData;
5937 static void
5938 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5940 g_free (data->etag);
5941 g_free (data);
5944 static void
5945 replace_readwrite_async_thread (GTask *task,
5946 gpointer object,
5947 gpointer task_data,
5948 GCancellable *cancellable)
5950 GFileIOStream *stream;
5951 GError *error = NULL;
5952 ReplaceRWAsyncData *data = task_data;
5954 stream = g_file_replace_readwrite (G_FILE (object),
5955 data->etag,
5956 data->make_backup,
5957 data->flags,
5958 cancellable,
5959 &error);
5961 if (stream == NULL)
5962 g_task_return_error (task, error);
5963 else
5964 g_task_return_pointer (task, stream, g_object_unref);
5967 static void
5968 g_file_real_replace_readwrite_async (GFile *file,
5969 const char *etag,
5970 gboolean make_backup,
5971 GFileCreateFlags flags,
5972 int io_priority,
5973 GCancellable *cancellable,
5974 GAsyncReadyCallback callback,
5975 gpointer user_data)
5977 GTask *task;
5978 ReplaceRWAsyncData *data;
5980 data = g_new0 (ReplaceRWAsyncData, 1);
5981 data->etag = g_strdup (etag);
5982 data->make_backup = make_backup;
5983 data->flags = flags;
5985 task = g_task_new (file, cancellable, callback, user_data);
5986 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5987 g_task_set_priority (task, io_priority);
5989 g_task_run_in_thread (task, replace_readwrite_async_thread);
5990 g_object_unref (task);
5993 static GFileIOStream *
5994 g_file_real_replace_readwrite_finish (GFile *file,
5995 GAsyncResult *res,
5996 GError **error)
5998 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6000 return g_task_propagate_pointer (G_TASK (res), error);
6003 static void
6004 set_display_name_async_thread (GTask *task,
6005 gpointer object,
6006 gpointer task_data,
6007 GCancellable *cancellable)
6009 GError *error = NULL;
6010 char *name = task_data;
6011 GFile *file;
6013 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6015 if (file == NULL)
6016 g_task_return_error (task, error);
6017 else
6018 g_task_return_pointer (task, file, g_object_unref);
6021 static void
6022 g_file_real_set_display_name_async (GFile *file,
6023 const char *display_name,
6024 int io_priority,
6025 GCancellable *cancellable,
6026 GAsyncReadyCallback callback,
6027 gpointer user_data)
6029 GTask *task;
6031 task = g_task_new (file, cancellable, callback, user_data);
6032 g_task_set_task_data (task, g_strdup (display_name), g_free);
6033 g_task_set_priority (task, io_priority);
6035 g_task_run_in_thread (task, set_display_name_async_thread);
6036 g_object_unref (task);
6039 static GFile *
6040 g_file_real_set_display_name_finish (GFile *file,
6041 GAsyncResult *res,
6042 GError **error)
6044 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6046 return g_task_propagate_pointer (G_TASK (res), error);
6049 typedef struct {
6050 GFileQueryInfoFlags flags;
6051 GFileInfo *info;
6052 gboolean res;
6053 GError *error;
6054 } SetInfoAsyncData;
6056 static void
6057 set_info_data_free (SetInfoAsyncData *data)
6059 if (data->info)
6060 g_object_unref (data->info);
6061 if (data->error)
6062 g_error_free (data->error);
6063 g_free (data);
6066 static void
6067 set_info_async_thread (GTask *task,
6068 gpointer object,
6069 gpointer task_data,
6070 GCancellable *cancellable)
6072 SetInfoAsyncData *data = task_data;
6074 data->error = NULL;
6075 data->res = g_file_set_attributes_from_info (G_FILE (object),
6076 data->info,
6077 data->flags,
6078 cancellable,
6079 &data->error);
6082 static void
6083 g_file_real_set_attributes_async (GFile *file,
6084 GFileInfo *info,
6085 GFileQueryInfoFlags flags,
6086 int io_priority,
6087 GCancellable *cancellable,
6088 GAsyncReadyCallback callback,
6089 gpointer user_data)
6091 GTask *task;
6092 SetInfoAsyncData *data;
6094 data = g_new0 (SetInfoAsyncData, 1);
6095 data->info = g_file_info_dup (info);
6096 data->flags = flags;
6098 task = g_task_new (file, cancellable, callback, user_data);
6099 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6100 g_task_set_priority (task, io_priority);
6102 g_task_run_in_thread (task, set_info_async_thread);
6103 g_object_unref (task);
6106 static gboolean
6107 g_file_real_set_attributes_finish (GFile *file,
6108 GAsyncResult *res,
6109 GFileInfo **info,
6110 GError **error)
6112 SetInfoAsyncData *data;
6114 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6116 data = g_task_get_task_data (G_TASK (res));
6118 if (info)
6119 *info = g_object_ref (data->info);
6121 if (error != NULL && data->error)
6122 *error = g_error_copy (data->error);
6124 return data->res;
6127 static void
6128 find_enclosing_mount_async_thread (GTask *task,
6129 gpointer object,
6130 gpointer task_data,
6131 GCancellable *cancellable)
6133 GError *error = NULL;
6134 GMount *mount;
6136 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6138 if (mount == NULL)
6139 g_task_return_error (task, error);
6140 else
6141 g_task_return_pointer (task, mount, g_object_unref);
6144 static void
6145 g_file_real_find_enclosing_mount_async (GFile *file,
6146 int io_priority,
6147 GCancellable *cancellable,
6148 GAsyncReadyCallback callback,
6149 gpointer user_data)
6151 GTask *task;
6153 task = g_task_new (file, cancellable, callback, user_data);
6154 g_task_set_priority (task, io_priority);
6156 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6157 g_object_unref (task);
6160 static GMount *
6161 g_file_real_find_enclosing_mount_finish (GFile *file,
6162 GAsyncResult *res,
6163 GError **error)
6165 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6167 return g_task_propagate_pointer (G_TASK (res), error);
6171 typedef struct {
6172 GFile *source;
6173 GFile *destination;
6174 GFileCopyFlags flags;
6175 GFileProgressCallback progress_cb;
6176 gpointer progress_cb_data;
6177 } CopyAsyncData;
6179 static void
6180 copy_async_data_free (CopyAsyncData *data)
6182 g_object_unref (data->source);
6183 g_object_unref (data->destination);
6184 g_slice_free (CopyAsyncData, data);
6187 typedef struct {
6188 CopyAsyncData *data;
6189 goffset current_num_bytes;
6190 goffset total_num_bytes;
6191 } ProgressData;
6193 static gboolean
6194 copy_async_progress_in_main (gpointer user_data)
6196 ProgressData *progress = user_data;
6197 CopyAsyncData *data = progress->data;
6199 data->progress_cb (progress->current_num_bytes,
6200 progress->total_num_bytes,
6201 data->progress_cb_data);
6203 return FALSE;
6206 static void
6207 copy_async_progress_callback (goffset current_num_bytes,
6208 goffset total_num_bytes,
6209 gpointer user_data)
6211 GTask *task = user_data;
6212 CopyAsyncData *data = g_task_get_task_data (task);
6213 ProgressData *progress;
6215 progress = g_new (ProgressData, 1);
6216 progress->data = data;
6217 progress->current_num_bytes = current_num_bytes;
6218 progress->total_num_bytes = total_num_bytes;
6220 g_main_context_invoke_full (g_task_get_context (task),
6221 g_task_get_priority (task),
6222 copy_async_progress_in_main,
6223 progress,
6224 g_free);
6227 static void
6228 copy_async_thread (GTask *task,
6229 gpointer source,
6230 gpointer task_data,
6231 GCancellable *cancellable)
6233 CopyAsyncData *data = task_data;
6234 gboolean result;
6235 GError *error = NULL;
6237 result = g_file_copy (data->source,
6238 data->destination,
6239 data->flags,
6240 cancellable,
6241 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6242 task,
6243 &error);
6244 if (result)
6245 g_task_return_boolean (task, TRUE);
6246 else
6247 g_task_return_error (task, error);
6250 static void
6251 g_file_real_copy_async (GFile *source,
6252 GFile *destination,
6253 GFileCopyFlags flags,
6254 int io_priority,
6255 GCancellable *cancellable,
6256 GFileProgressCallback progress_callback,
6257 gpointer progress_callback_data,
6258 GAsyncReadyCallback callback,
6259 gpointer user_data)
6261 GTask *task;
6262 CopyAsyncData *data;
6264 data = g_slice_new (CopyAsyncData);
6265 data->source = g_object_ref (source);
6266 data->destination = g_object_ref (destination);
6267 data->flags = flags;
6268 data->progress_cb = progress_callback;
6269 data->progress_cb_data = progress_callback_data;
6271 task = g_task_new (source, cancellable, callback, user_data);
6272 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6273 g_task_set_priority (task, io_priority);
6274 g_task_run_in_thread (task, copy_async_thread);
6275 g_object_unref (task);
6278 static gboolean
6279 g_file_real_copy_finish (GFile *file,
6280 GAsyncResult *res,
6281 GError **error)
6283 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6285 return g_task_propagate_boolean (G_TASK (res), error);
6289 /********************************************
6290 * Default VFS operations *
6291 ********************************************/
6294 * g_file_new_for_path:
6295 * @path: a string containing a relative or absolute path.
6296 * The string must be encoded in the glib filename encoding.
6298 * Constructs a #GFile for a given path. This operation never
6299 * fails, but the returned object might not support any I/O
6300 * operation if @path is malformed.
6302 * Returns: (transfer full): a new #GFile for the given @path.
6303 * Free the returned object with g_object_unref().
6305 GFile *
6306 g_file_new_for_path (const char *path)
6308 g_return_val_if_fail (path != NULL, NULL);
6310 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6314 * g_file_new_for_uri:
6315 * @uri: a UTF-8 string containing a URI
6317 * Constructs a #GFile for a given URI. This operation never
6318 * fails, but the returned object might not support any I/O
6319 * operation if @uri is malformed or if the uri type is
6320 * not supported.
6322 * Returns: (transfer full): a new #GFile for the given @uri.
6323 * Free the returned object with g_object_unref().
6325 GFile *
6326 g_file_new_for_uri (const char *uri)
6328 g_return_val_if_fail (uri != NULL, NULL);
6330 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6334 * g_file_new_tmp:
6335 * @tmpl: (type filename) (allow-none): Template for the file
6336 * name, as in g_file_open_tmp(), or %NULL for a default template
6337 * @iostream: (out): on return, a #GFileIOStream for the created file
6338 * @error: a #GError, or %NULL
6340 * Opens a file in the preferred directory for temporary files (as
6341 * returned by g_get_tmp_dir()) and returns a #GFile and
6342 * #GFileIOStream pointing to it.
6344 * @tmpl should be a string in the GLib file name encoding
6345 * containing a sequence of six 'X' characters, and containing no
6346 * directory components. If it is %NULL, a default template is used.
6348 * Unlike the other #GFile constructors, this will return %NULL if
6349 * a temporary file could not be created.
6351 * Returns: (transfer full): a new #GFile.
6352 * Free the returned object with g_object_unref().
6354 * Since: 2.32
6356 GFile *
6357 g_file_new_tmp (const char *tmpl,
6358 GFileIOStream **iostream,
6359 GError **error)
6361 gint fd;
6362 gchar *path;
6363 GFile *file;
6364 GFileOutputStream *output;
6366 g_return_val_if_fail (iostream != NULL, NULL);
6368 fd = g_file_open_tmp (tmpl, &path, error);
6369 if (fd == -1)
6370 return NULL;
6372 file = g_file_new_for_path (path);
6374 output = _g_local_file_output_stream_new (fd);
6375 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6377 g_object_unref (output);
6378 g_free (path);
6380 return file;
6384 * g_file_parse_name:
6385 * @parse_name: a file name or path to be parsed
6387 * Constructs a #GFile with the given @parse_name (i.e. something
6388 * given by g_file_get_parse_name()). This operation never fails,
6389 * but the returned object might not support any I/O operation if
6390 * the @parse_name cannot be parsed.
6392 * Returns: (transfer full): a new #GFile.
6394 GFile *
6395 g_file_parse_name (const char *parse_name)
6397 g_return_val_if_fail (parse_name != NULL, NULL);
6399 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6402 static gboolean
6403 is_valid_scheme_character (char c)
6405 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6408 /* Following RFC 2396, valid schemes are built like:
6409 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6411 static gboolean
6412 has_valid_scheme (const char *uri)
6414 const char *p;
6416 p = uri;
6418 if (!g_ascii_isalpha (*p))
6419 return FALSE;
6421 do {
6422 p++;
6423 } while (is_valid_scheme_character (*p));
6425 return *p == ':';
6428 static GFile *
6429 new_for_cmdline_arg (const gchar *arg,
6430 const gchar *cwd)
6432 GFile *file;
6433 char *filename;
6435 if (g_path_is_absolute (arg))
6436 return g_file_new_for_path (arg);
6438 if (has_valid_scheme (arg))
6439 return g_file_new_for_uri (arg);
6441 if (cwd == NULL)
6443 char *current_dir;
6445 current_dir = g_get_current_dir ();
6446 filename = g_build_filename (current_dir, arg, NULL);
6447 g_free (current_dir);
6449 else
6450 filename = g_build_filename (cwd, arg, NULL);
6452 file = g_file_new_for_path (filename);
6453 g_free (filename);
6455 return file;
6459 * g_file_new_for_commandline_arg:
6460 * @arg: a command line string
6462 * Creates a #GFile with the given argument from the command line.
6463 * The value of @arg can be either a URI, an absolute path or a
6464 * relative path resolved relative to the current working directory.
6465 * This operation never fails, but the returned object might not
6466 * support any I/O operation if @arg points to a malformed path.
6468 * Returns: (transfer full): a new #GFile.
6469 * Free the returned object with g_object_unref().
6471 GFile *
6472 g_file_new_for_commandline_arg (const char *arg)
6474 g_return_val_if_fail (arg != NULL, NULL);
6476 return new_for_cmdline_arg (arg, NULL);
6480 * g_file_new_for_commandline_arg_and_cwd:
6481 * @arg: a command line string
6482 * @cwd: the current working directory of the commandline
6484 * Creates a #GFile with the given argument from the command line.
6486 * This function is similar to g_file_new_for_commandline_arg() except
6487 * that it allows for passing the current working directory as an
6488 * argument instead of using the current working directory of the
6489 * process.
6491 * This is useful if the commandline argument was given in a context
6492 * other than the invocation of the current process.
6494 * See also g_application_command_line_create_file_for_arg().
6496 * Returns: (transfer full): a new #GFile
6498 * Since: 2.36
6500 GFile *
6501 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6502 const gchar *cwd)
6504 g_return_val_if_fail (arg != NULL, NULL);
6505 g_return_val_if_fail (cwd != NULL, NULL);
6507 return new_for_cmdline_arg (arg, cwd);
6511 * g_file_mount_enclosing_volume:
6512 * @location: input #GFile
6513 * @flags: flags affecting the operation
6514 * @mount_operation: (allow-none): a #GMountOperation
6515 * or %NULL to avoid user interaction
6516 * @cancellable: (allow-none): optional #GCancellable object,
6517 * %NULL to ignore
6518 * @callback: (allow-none): a #GAsyncReadyCallback to call
6519 * when the request is satisfied, or %NULL
6520 * @user_data: the data to pass to callback function
6522 * Starts a @mount_operation, mounting the volume that contains
6523 * the file @location.
6525 * When this operation has completed, @callback will be called with
6526 * @user_user data, and the operation can be finalized with
6527 * g_file_mount_enclosing_volume_finish().
6529 * If @cancellable is not %NULL, then the operation can be cancelled by
6530 * triggering the cancellable object from another thread. If the operation
6531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6533 void
6534 g_file_mount_enclosing_volume (GFile *location,
6535 GMountMountFlags flags,
6536 GMountOperation *mount_operation,
6537 GCancellable *cancellable,
6538 GAsyncReadyCallback callback,
6539 gpointer user_data)
6541 GFileIface *iface;
6543 g_return_if_fail (G_IS_FILE (location));
6545 iface = G_FILE_GET_IFACE (location);
6547 if (iface->mount_enclosing_volume == NULL)
6549 g_task_report_new_error (location, callback, user_data,
6550 g_file_mount_enclosing_volume,
6551 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6552 _("volume doesn't implement mount"));
6553 return;
6556 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6561 * g_file_mount_enclosing_volume_finish:
6562 * @location: input #GFile
6563 * @result: a #GAsyncResult
6564 * @error: a #GError, or %NULL
6566 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6568 * Returns: %TRUE if successful. If an error has occurred,
6569 * this function will return %FALSE and set @error
6570 * appropriately if present.
6572 gboolean
6573 g_file_mount_enclosing_volume_finish (GFile *location,
6574 GAsyncResult *result,
6575 GError **error)
6577 GFileIface *iface;
6579 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6580 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6582 if (g_async_result_legacy_propagate_error (result, error))
6583 return FALSE;
6584 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6585 return g_task_propagate_boolean (G_TASK (result), error);
6587 iface = G_FILE_GET_IFACE (location);
6589 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6592 /********************************************
6593 * Utility functions *
6594 ********************************************/
6597 * g_file_query_default_handler:
6598 * @file: a #GFile to open
6599 * @cancellable: optional #GCancellable object, %NULL to ignore
6600 * @error: a #GError, or %NULL
6602 * Returns the #GAppInfo that is registered as the default
6603 * application to handle the file specified by @file.
6605 * If @cancellable is not %NULL, then the operation can be cancelled by
6606 * triggering the cancellable object from another thread. If the operation
6607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6609 * Returns: (transfer full): a #GAppInfo if the handle was found,
6610 * %NULL if there were errors.
6611 * When you are done with it, release it with g_object_unref()
6613 GAppInfo *
6614 g_file_query_default_handler (GFile *file,
6615 GCancellable *cancellable,
6616 GError **error)
6618 char *uri_scheme;
6619 const char *content_type;
6620 GAppInfo *appinfo;
6621 GFileInfo *info;
6622 char *path;
6624 uri_scheme = g_file_get_uri_scheme (file);
6625 if (uri_scheme && uri_scheme[0] != '\0')
6627 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6628 g_free (uri_scheme);
6630 if (appinfo != NULL)
6631 return appinfo;
6634 info = g_file_query_info (file,
6635 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6637 cancellable,
6638 error);
6639 if (info == NULL)
6640 return NULL;
6642 appinfo = NULL;
6644 content_type = g_file_info_get_content_type (info);
6645 if (content_type)
6647 /* Don't use is_native(), as we want to support fuse paths if available */
6648 path = g_file_get_path (file);
6649 appinfo = g_app_info_get_default_for_type (content_type,
6650 path == NULL);
6651 g_free (path);
6654 g_object_unref (info);
6656 if (appinfo != NULL)
6657 return appinfo;
6659 g_set_error_literal (error, G_IO_ERROR,
6660 G_IO_ERROR_NOT_SUPPORTED,
6661 _("No application is registered as handling this file"));
6662 return NULL;
6665 #define GET_CONTENT_BLOCK_SIZE 8192
6668 * g_file_load_contents:
6669 * @file: input #GFile
6670 * @cancellable: optional #GCancellable object, %NULL to ignore
6671 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6672 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6673 * or %NULL if the length is not needed
6674 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6675 * or %NULL if the entity tag is not needed
6676 * @error: a #GError, or %NULL
6678 * Loads the content of the file into memory. The data is always
6679 * zero-terminated, but this is not included in the resultant @length.
6680 * The returned @content should be freed with g_free() when no longer
6681 * needed.
6683 * If @cancellable is not %NULL, then the operation can be cancelled by
6684 * triggering the cancellable object from another thread. If the operation
6685 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6687 * Returns: %TRUE if the @file's contents were successfully loaded.
6688 * %FALSE if there were errors.
6690 gboolean
6691 g_file_load_contents (GFile *file,
6692 GCancellable *cancellable,
6693 char **contents,
6694 gsize *length,
6695 char **etag_out,
6696 GError **error)
6698 GFileInputStream *in;
6699 GByteArray *content;
6700 gsize pos;
6701 gssize res;
6702 GFileInfo *info;
6704 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6705 g_return_val_if_fail (contents != NULL, FALSE);
6707 in = g_file_read (file, cancellable, error);
6708 if (in == NULL)
6709 return FALSE;
6711 content = g_byte_array_new ();
6712 pos = 0;
6714 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6715 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6716 content->data + pos,
6717 GET_CONTENT_BLOCK_SIZE,
6718 cancellable, error)) > 0)
6720 pos += res;
6721 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6724 if (etag_out)
6726 *etag_out = NULL;
6728 info = g_file_input_stream_query_info (in,
6729 G_FILE_ATTRIBUTE_ETAG_VALUE,
6730 cancellable,
6731 NULL);
6732 if (info)
6734 *etag_out = g_strdup (g_file_info_get_etag (info));
6735 g_object_unref (info);
6739 /* Ignore errors on close */
6740 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6741 g_object_unref (in);
6743 if (res < 0)
6745 /* error is set already */
6746 g_byte_array_free (content, TRUE);
6747 return FALSE;
6750 if (length)
6751 *length = pos;
6753 /* Zero terminate (we got an extra byte allocated for this */
6754 content->data[pos] = 0;
6756 *contents = (char *)g_byte_array_free (content, FALSE);
6758 return TRUE;
6761 typedef struct {
6762 GTask *task;
6763 GFileReadMoreCallback read_more_callback;
6764 GByteArray *content;
6765 gsize pos;
6766 char *etag;
6767 } LoadContentsData;
6770 static void
6771 load_contents_data_free (LoadContentsData *data)
6773 if (data->content)
6774 g_byte_array_free (data->content, TRUE);
6775 g_free (data->etag);
6776 g_free (data);
6779 static void
6780 load_contents_close_callback (GObject *obj,
6781 GAsyncResult *close_res,
6782 gpointer user_data)
6784 GInputStream *stream = G_INPUT_STREAM (obj);
6785 LoadContentsData *data = user_data;
6787 /* Ignore errors here, we're only reading anyway */
6788 g_input_stream_close_finish (stream, close_res, NULL);
6789 g_object_unref (stream);
6791 g_task_return_boolean (data->task, TRUE);
6792 g_object_unref (data->task);
6795 static void
6796 load_contents_fstat_callback (GObject *obj,
6797 GAsyncResult *stat_res,
6798 gpointer user_data)
6800 GInputStream *stream = G_INPUT_STREAM (obj);
6801 LoadContentsData *data = user_data;
6802 GFileInfo *info;
6804 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6805 stat_res, NULL);
6806 if (info)
6808 data->etag = g_strdup (g_file_info_get_etag (info));
6809 g_object_unref (info);
6812 g_input_stream_close_async (stream, 0,
6813 g_task_get_cancellable (data->task),
6814 load_contents_close_callback, data);
6817 static void
6818 load_contents_read_callback (GObject *obj,
6819 GAsyncResult *read_res,
6820 gpointer user_data)
6822 GInputStream *stream = G_INPUT_STREAM (obj);
6823 LoadContentsData *data = user_data;
6824 GError *error = NULL;
6825 gssize read_size;
6827 read_size = g_input_stream_read_finish (stream, read_res, &error);
6829 if (read_size < 0)
6831 g_task_return_error (data->task, error);
6832 g_object_unref (data->task);
6834 /* Close the file ignoring any error */
6835 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6836 g_object_unref (stream);
6838 else if (read_size == 0)
6840 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6841 G_FILE_ATTRIBUTE_ETAG_VALUE,
6843 g_task_get_cancellable (data->task),
6844 load_contents_fstat_callback,
6845 data);
6847 else if (read_size > 0)
6849 data->pos += read_size;
6851 g_byte_array_set_size (data->content,
6852 data->pos + GET_CONTENT_BLOCK_SIZE);
6855 if (data->read_more_callback &&
6856 !data->read_more_callback ((char *)data->content->data, data->pos,
6857 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6858 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6859 G_FILE_ATTRIBUTE_ETAG_VALUE,
6861 g_task_get_cancellable (data->task),
6862 load_contents_fstat_callback,
6863 data);
6864 else
6865 g_input_stream_read_async (stream,
6866 data->content->data + data->pos,
6867 GET_CONTENT_BLOCK_SIZE,
6869 g_task_get_cancellable (data->task),
6870 load_contents_read_callback,
6871 data);
6875 static void
6876 load_contents_open_callback (GObject *obj,
6877 GAsyncResult *open_res,
6878 gpointer user_data)
6880 GFile *file = G_FILE (obj);
6881 GFileInputStream *stream;
6882 LoadContentsData *data = user_data;
6883 GError *error = NULL;
6885 stream = g_file_read_finish (file, open_res, &error);
6887 if (stream)
6889 g_byte_array_set_size (data->content,
6890 data->pos + GET_CONTENT_BLOCK_SIZE);
6891 g_input_stream_read_async (G_INPUT_STREAM (stream),
6892 data->content->data + data->pos,
6893 GET_CONTENT_BLOCK_SIZE,
6895 g_task_get_cancellable (data->task),
6896 load_contents_read_callback,
6897 data);
6899 else
6901 g_task_return_error (data->task, error);
6902 g_object_unref (data->task);
6907 * g_file_load_partial_contents_async: (skip)
6908 * @file: input #GFile
6909 * @cancellable: optional #GCancellable object, %NULL to ignore
6910 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6911 * and to specify whether further data should be read
6912 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6913 * @user_data: the data to pass to the callback functions
6915 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6916 * be used to stop reading from the file when appropriate, else this
6917 * function will behave exactly as g_file_load_contents_async(). This
6918 * operation can be finished by g_file_load_partial_contents_finish().
6920 * Users of this function should be aware that @user_data is passed to
6921 * both the @read_more_callback and the @callback.
6923 * If @cancellable is not %NULL, then the operation can be cancelled by
6924 * triggering the cancellable object from another thread. If the operation
6925 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6927 void
6928 g_file_load_partial_contents_async (GFile *file,
6929 GCancellable *cancellable,
6930 GFileReadMoreCallback read_more_callback,
6931 GAsyncReadyCallback callback,
6932 gpointer user_data)
6934 LoadContentsData *data;
6936 g_return_if_fail (G_IS_FILE (file));
6938 data = g_new0 (LoadContentsData, 1);
6939 data->read_more_callback = read_more_callback;
6940 data->content = g_byte_array_new ();
6942 data->task = g_task_new (file, cancellable, callback, user_data);
6943 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6945 g_file_read_async (file,
6947 g_task_get_cancellable (data->task),
6948 load_contents_open_callback,
6949 data);
6953 * g_file_load_partial_contents_finish:
6954 * @file: input #GFile
6955 * @res: a #GAsyncResult
6956 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6957 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6958 * or %NULL if the length is not needed
6959 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6960 * or %NULL if the entity tag is not needed
6961 * @error: a #GError, or %NULL
6963 * Finishes an asynchronous partial load operation that was started
6964 * with g_file_load_partial_contents_async(). The data is always
6965 * zero-terminated, but this is not included in the resultant @length.
6966 * The returned @content should be freed with g_free() when no longer
6967 * needed.
6969 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6970 * present, it will be set appropriately.
6972 gboolean
6973 g_file_load_partial_contents_finish (GFile *file,
6974 GAsyncResult *res,
6975 char **contents,
6976 gsize *length,
6977 char **etag_out,
6978 GError **error)
6980 GTask *task;
6981 LoadContentsData *data;
6983 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6984 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6985 g_return_val_if_fail (contents != NULL, FALSE);
6987 task = G_TASK (res);
6989 if (!g_task_propagate_boolean (task, error))
6991 if (length)
6992 *length = 0;
6993 return FALSE;
6996 data = g_task_get_task_data (task);
6998 if (length)
6999 *length = data->pos;
7001 if (etag_out)
7003 *etag_out = data->etag;
7004 data->etag = NULL;
7007 /* Zero terminate */
7008 g_byte_array_set_size (data->content, data->pos + 1);
7009 data->content->data[data->pos] = 0;
7011 *contents = (char *)g_byte_array_free (data->content, FALSE);
7012 data->content = NULL;
7014 return TRUE;
7018 * g_file_load_contents_async:
7019 * @file: input #GFile
7020 * @cancellable: optional #GCancellable object, %NULL to ignore
7021 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7022 * @user_data: the data to pass to callback function
7024 * Starts an asynchronous load of the @file's contents.
7026 * For more details, see g_file_load_contents() which is
7027 * the synchronous version of this call.
7029 * When the load operation has completed, @callback will be called
7030 * with @user data. To finish the operation, call
7031 * g_file_load_contents_finish() with the #GAsyncResult returned by
7032 * the @callback.
7034 * If @cancellable is not %NULL, then the operation can be cancelled by
7035 * triggering the cancellable object from another thread. If the operation
7036 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7038 void
7039 g_file_load_contents_async (GFile *file,
7040 GCancellable *cancellable,
7041 GAsyncReadyCallback callback,
7042 gpointer user_data)
7044 g_file_load_partial_contents_async (file,
7045 cancellable,
7046 NULL,
7047 callback, user_data);
7051 * g_file_load_contents_finish:
7052 * @file: input #GFile
7053 * @res: a #GAsyncResult
7054 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7055 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7056 * or %NULL if the length is not needed
7057 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7058 * or %NULL if the entity tag is not needed
7059 * @error: a #GError, or %NULL
7061 * Finishes an asynchronous load of the @file's contents.
7062 * The contents are placed in @contents, and @length is set to the
7063 * size of the @contents string. The @content should be freed with
7064 * g_free() when no longer needed. If @etag_out is present, it will be
7065 * set to the new entity tag for the @file.
7067 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7068 * present, it will be set appropriately.
7070 gboolean
7071 g_file_load_contents_finish (GFile *file,
7072 GAsyncResult *res,
7073 char **contents,
7074 gsize *length,
7075 char **etag_out,
7076 GError **error)
7078 return g_file_load_partial_contents_finish (file,
7079 res,
7080 contents,
7081 length,
7082 etag_out,
7083 error);
7087 * g_file_replace_contents:
7088 * @file: input #GFile
7089 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7090 * @length: the length of @contents in bytes
7091 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7092 * for the document, or %NULL
7093 * @make_backup: %TRUE if a backup should be created
7094 * @flags: a set of #GFileCreateFlags
7095 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7096 * for the document. This should be freed with g_free() when no longer
7097 * needed, or %NULL
7098 * @cancellable: optional #GCancellable object, %NULL to ignore
7099 * @error: a #GError, or %NULL
7101 * Replaces the contents of @file with @contents of @length bytes.
7103 * If @etag is specified (not %NULL), any existing file must have that etag,
7104 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7106 * If @make_backup is %TRUE, this function will attempt to make a backup
7107 * of @file.
7109 * If @cancellable is not %NULL, then the operation can be cancelled by
7110 * triggering the cancellable object from another thread. If the operation
7111 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7113 * The returned @new_etag can be used to verify that the file hasn't
7114 * changed the next time it is saved over.
7116 * Returns: %TRUE if successful. If an error has occurred, this function
7117 * will return %FALSE and set @error appropriately if present.
7119 gboolean
7120 g_file_replace_contents (GFile *file,
7121 const char *contents,
7122 gsize length,
7123 const char *etag,
7124 gboolean make_backup,
7125 GFileCreateFlags flags,
7126 char **new_etag,
7127 GCancellable *cancellable,
7128 GError **error)
7130 GFileOutputStream *out;
7131 gsize pos, remainder;
7132 gssize res;
7133 gboolean ret;
7135 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7136 g_return_val_if_fail (contents != NULL, FALSE);
7138 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7139 if (out == NULL)
7140 return FALSE;
7142 pos = 0;
7143 remainder = length;
7144 while (remainder > 0 &&
7145 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7146 contents + pos,
7147 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7148 cancellable,
7149 error)) > 0)
7151 pos += res;
7152 remainder -= res;
7155 if (remainder > 0 && res < 0)
7157 /* Ignore errors on close */
7158 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7159 g_object_unref (out);
7161 /* error is set already */
7162 return FALSE;
7165 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7167 if (new_etag)
7168 *new_etag = g_file_output_stream_get_etag (out);
7170 g_object_unref (out);
7172 return ret;
7175 typedef struct {
7176 GTask *task;
7177 const char *content;
7178 gsize length;
7179 gsize pos;
7180 char *etag;
7181 gboolean failed;
7182 } ReplaceContentsData;
7184 static void
7185 replace_contents_data_free (ReplaceContentsData *data)
7187 g_free (data->etag);
7188 g_free (data);
7191 static void
7192 replace_contents_close_callback (GObject *obj,
7193 GAsyncResult *close_res,
7194 gpointer user_data)
7196 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7197 ReplaceContentsData *data = user_data;
7199 /* Ignore errors here, we're only reading anyway */
7200 g_output_stream_close_finish (stream, close_res, NULL);
7201 g_object_unref (stream);
7203 if (!data->failed)
7205 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7206 g_task_return_boolean (data->task, TRUE);
7208 g_object_unref (data->task);
7211 static void
7212 replace_contents_write_callback (GObject *obj,
7213 GAsyncResult *read_res,
7214 gpointer user_data)
7216 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7217 ReplaceContentsData *data = user_data;
7218 GError *error = NULL;
7219 gssize write_size;
7221 write_size = g_output_stream_write_finish (stream, read_res, &error);
7223 if (write_size <= 0)
7225 /* Error or EOF, close the file */
7226 if (write_size < 0)
7228 data->failed = TRUE;
7229 g_task_return_error (data->task, error);
7231 g_output_stream_close_async (stream, 0,
7232 g_task_get_cancellable (data->task),
7233 replace_contents_close_callback, data);
7235 else if (write_size > 0)
7237 data->pos += write_size;
7239 if (data->pos >= data->length)
7240 g_output_stream_close_async (stream, 0,
7241 g_task_get_cancellable (data->task),
7242 replace_contents_close_callback, data);
7243 else
7244 g_output_stream_write_async (stream,
7245 data->content + data->pos,
7246 data->length - data->pos,
7248 g_task_get_cancellable (data->task),
7249 replace_contents_write_callback,
7250 data);
7254 static void
7255 replace_contents_open_callback (GObject *obj,
7256 GAsyncResult *open_res,
7257 gpointer user_data)
7259 GFile *file = G_FILE (obj);
7260 GFileOutputStream *stream;
7261 ReplaceContentsData *data = user_data;
7262 GError *error = NULL;
7264 stream = g_file_replace_finish (file, open_res, &error);
7266 if (stream)
7268 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7269 data->content + data->pos,
7270 data->length - data->pos,
7272 g_task_get_cancellable (data->task),
7273 replace_contents_write_callback,
7274 data);
7276 else
7278 g_task_return_error (data->task, error);
7279 g_object_unref (data->task);
7284 * g_file_replace_contents_async:
7285 * @file: input #GFile
7286 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7287 * @length: the length of @contents in bytes
7288 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7289 * @make_backup: %TRUE if a backup should be created
7290 * @flags: a set of #GFileCreateFlags
7291 * @cancellable: optional #GCancellable object, %NULL to ignore
7292 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7293 * @user_data: the data to pass to callback function
7295 * Starts an asynchronous replacement of @file with the given
7296 * @contents of @length bytes. @etag will replace the document's
7297 * current entity tag.
7299 * When this operation has completed, @callback will be called with
7300 * @user_user data, and the operation can be finalized with
7301 * g_file_replace_contents_finish().
7303 * If @cancellable is not %NULL, then the operation can be cancelled by
7304 * triggering the cancellable object from another thread. If the operation
7305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7307 * If @make_backup is %TRUE, this function will attempt to
7308 * make a backup of @file.
7310 void
7311 g_file_replace_contents_async (GFile *file,
7312 const char *contents,
7313 gsize length,
7314 const char *etag,
7315 gboolean make_backup,
7316 GFileCreateFlags flags,
7317 GCancellable *cancellable,
7318 GAsyncReadyCallback callback,
7319 gpointer user_data)
7321 ReplaceContentsData *data;
7323 g_return_if_fail (G_IS_FILE (file));
7324 g_return_if_fail (contents != NULL);
7326 data = g_new0 (ReplaceContentsData, 1);
7328 data->content = contents;
7329 data->length = length;
7331 data->task = g_task_new (file, cancellable, callback, user_data);
7332 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7334 g_file_replace_async (file,
7335 etag,
7336 make_backup,
7337 flags,
7339 g_task_get_cancellable (data->task),
7340 replace_contents_open_callback,
7341 data);
7345 * g_file_replace_contents_finish:
7346 * @file: input #GFile
7347 * @res: a #GAsyncResult
7348 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7349 * for the document. This should be freed with g_free() when it is no
7350 * longer needed, or %NULL
7351 * @error: a #GError, or %NULL
7353 * Finishes an asynchronous replace of the given @file. See
7354 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7355 * tag for the document, if present.
7357 * Returns: %TRUE on success, %FALSE on failure.
7359 gboolean
7360 g_file_replace_contents_finish (GFile *file,
7361 GAsyncResult *res,
7362 char **new_etag,
7363 GError **error)
7365 GTask *task;
7366 ReplaceContentsData *data;
7368 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7369 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7371 task = G_TASK (res);
7373 if (!g_task_propagate_boolean (task, error))
7374 return FALSE;
7376 data = g_task_get_task_data (task);
7378 if (new_etag)
7380 *new_etag = data->etag;
7381 data->etag = NULL; /* Take ownership */
7384 return TRUE;
7387 gboolean
7388 g_file_real_measure_disk_usage (GFile *file,
7389 GFileMeasureFlags flags,
7390 GCancellable *cancellable,
7391 GFileMeasureProgressCallback progress_callback,
7392 gpointer progress_data,
7393 guint64 *disk_usage,
7394 guint64 *num_dirs,
7395 guint64 *num_files,
7396 GError **error)
7398 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7399 "Operation not supported for the current backend.");
7400 return FALSE;
7403 typedef struct
7405 GFileMeasureFlags flags;
7406 GFileMeasureProgressCallback progress_callback;
7407 gpointer progress_data;
7408 } MeasureTaskData;
7410 typedef struct
7412 guint64 disk_usage;
7413 guint64 num_dirs;
7414 guint64 num_files;
7415 } MeasureResult;
7417 typedef struct
7419 GFileMeasureProgressCallback callback;
7420 gpointer user_data;
7421 gboolean reporting;
7422 guint64 current_size;
7423 guint64 num_dirs;
7424 guint64 num_files;
7425 } MeasureProgress;
7427 static gboolean
7428 measure_disk_usage_invoke_progress (gpointer user_data)
7430 MeasureProgress *progress = user_data;
7432 (* progress->callback) (progress->reporting,
7433 progress->current_size, progress->num_dirs, progress->num_files,
7434 progress->user_data);
7436 return FALSE;
7439 static void
7440 measure_disk_usage_progress (gboolean reporting,
7441 guint64 current_size,
7442 guint64 num_dirs,
7443 guint64 num_files,
7444 gpointer user_data)
7446 MeasureProgress progress;
7447 GTask *task = user_data;
7448 MeasureTaskData *data;
7450 data = g_task_get_task_data (task);
7452 progress.callback = data->progress_callback;
7453 progress.user_data = data->progress_data;
7454 progress.reporting = reporting;
7455 progress.current_size = current_size;
7456 progress.num_dirs = num_dirs;
7457 progress.num_files = num_files;
7459 g_main_context_invoke_full (g_task_get_context (task),
7460 g_task_get_priority (task),
7461 measure_disk_usage_invoke_progress,
7462 g_memdup (&progress, sizeof progress),
7463 g_free);
7466 static void
7467 measure_disk_usage_thread (GTask *task,
7468 gpointer source_object,
7469 gpointer task_data,
7470 GCancellable *cancellable)
7472 MeasureTaskData *data = task_data;
7473 GError *error = NULL;
7474 MeasureResult result;
7476 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7477 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7478 &result.disk_usage, &result.num_dirs, &result.num_files,
7479 &error))
7480 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7481 else
7482 g_task_return_error (task, error);
7485 static void
7486 g_file_real_measure_disk_usage_async (GFile *file,
7487 GFileMeasureFlags flags,
7488 gint io_priority,
7489 GCancellable *cancellable,
7490 GFileMeasureProgressCallback progress_callback,
7491 gpointer progress_data,
7492 GAsyncReadyCallback callback,
7493 gpointer user_data)
7495 MeasureTaskData data;
7496 GTask *task;
7498 data.flags = flags;
7499 data.progress_callback = progress_callback;
7500 data.progress_data = progress_data;
7502 task = g_task_new (file, cancellable, callback, user_data);
7503 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7504 g_task_set_priority (task, io_priority);
7506 g_task_run_in_thread (task, measure_disk_usage_thread);
7507 g_object_unref (task);
7510 static gboolean
7511 g_file_real_measure_disk_usage_finish (GFile *file,
7512 GAsyncResult *result,
7513 guint64 *disk_usage,
7514 guint64 *num_dirs,
7515 guint64 *num_files,
7516 GError **error)
7518 MeasureResult *measure_result;
7520 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7522 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7524 if (measure_result == NULL)
7525 return FALSE;
7527 if (disk_usage)
7528 *disk_usage = measure_result->disk_usage;
7530 if (num_dirs)
7531 *num_dirs = measure_result->num_dirs;
7533 if (num_files)
7534 *num_files = measure_result->num_files;
7536 g_free (measure_result);
7538 return TRUE;
7542 * g_file_measure_disk_usage:
7543 * @file: a #GFile
7544 * @flags: #GFileMeasureFlags
7545 * @cancellable: (allow-none): optional #GCancellable
7546 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7547 * @progress_data: user_data for @progress_callback
7548 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7549 * @num_dirs: (allow-none) (out): the number of directories encountered
7550 * @num_files: (allow-none) (out): the number of non-directories encountered
7551 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7553 * Recursively measures the disk usage of @file.
7555 * This is essentially an analog of the '<literal>du</literal>' command,
7556 * but it also reports the number of directories and non-directory files
7557 * encountered (including things like symbolic links).
7559 * By default, errors are only reported against the toplevel file
7560 * itself. Errors found while recursing are silently ignored, unless
7561 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7563 * The returned size, @disk_usage, is in bytes and should be formatted
7564 * with g_format_size() in order to get something reasonable for showing
7565 * in a user interface.
7567 * @progress_callback and @progress_data can be given to request
7568 * periodic progress updates while scanning. See the documentation for
7569 * #GFileMeasureProgressCallback for information about when and how the
7570 * callback will be invoked.
7572 * Returns: %TRUE if successful, with the out parameters set.
7573 * %FALSE otherwise, with @error set.
7575 * Since: 2.38
7577 gboolean
7578 g_file_measure_disk_usage (GFile *file,
7579 GFileMeasureFlags flags,
7580 GCancellable *cancellable,
7581 GFileMeasureProgressCallback progress_callback,
7582 gpointer progress_data,
7583 guint64 *disk_usage,
7584 guint64 *num_dirs,
7585 guint64 *num_files,
7586 GError **error)
7588 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7589 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7590 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7592 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7593 progress_callback, progress_data,
7594 disk_usage, num_dirs, num_files,
7595 error);
7599 * g_file_measure_disk_usage_async:
7600 * @file: a #GFile
7601 * @flags: #GFileMeasureFlags
7602 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7603 * of the request
7604 * @cancellable: (allow-none): optional #GCancellable
7605 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7606 * @progress_data: user_data for @progress_callback
7607 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7608 * @user_data: the data to pass to callback function
7610 * Recursively measures the disk usage of @file.
7612 * This is the asynchronous version of g_file_measure_disk_usage(). See
7613 * there for more information.
7615 * Since: 2.38
7617 void
7618 g_file_measure_disk_usage_async (GFile *file,
7619 GFileMeasureFlags flags,
7620 gint io_priority,
7621 GCancellable *cancellable,
7622 GFileMeasureProgressCallback progress_callback,
7623 gpointer progress_data,
7624 GAsyncReadyCallback callback,
7625 gpointer user_data)
7627 g_return_if_fail (G_IS_FILE (file));
7628 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7630 return G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7631 progress_callback, progress_data,
7632 callback, user_data);
7636 * g_file_measure_disk_usage_finish:
7637 * @file: a #GFile
7638 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7639 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7640 * @num_dirs: (allow-none) (out): the number of directories encountered
7641 * @num_files: (allow-none) (out): the number of non-directories encountered
7642 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7644 * Collects the results from an earlier call to
7645 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7646 * more information.
7648 * Returns: %TRUE if successful, with the out parameters set.
7649 * %FALSE otherwise, with @error set.
7651 * Since: 2.38
7653 gboolean
7654 g_file_measure_disk_usage_finish (GFile *file,
7655 GAsyncResult *result,
7656 guint64 *disk_usage,
7657 guint64 *num_dirs,
7658 guint64 *num_files,
7659 GError **error)
7661 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7662 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7664 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7668 * g_file_start_mountable:
7669 * @file: input #GFile
7670 * @flags: flags affecting the operation
7671 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7672 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7673 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7674 * @user_data: the data to pass to callback function
7676 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7677 * Using @start_operation, you can request callbacks when, for instance,
7678 * passwords are needed during authentication.
7680 * If @cancellable is not %NULL, then the operation can be cancelled by
7681 * triggering the cancellable object from another thread. If the operation
7682 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7684 * When the operation is finished, @callback will be called.
7685 * You can then call g_file_mount_mountable_finish() to get
7686 * the result of the operation.
7688 * Since: 2.22
7690 void
7691 g_file_start_mountable (GFile *file,
7692 GDriveStartFlags flags,
7693 GMountOperation *start_operation,
7694 GCancellable *cancellable,
7695 GAsyncReadyCallback callback,
7696 gpointer user_data)
7698 GFileIface *iface;
7700 g_return_if_fail (G_IS_FILE (file));
7702 iface = G_FILE_GET_IFACE (file);
7704 if (iface->start_mountable == NULL)
7706 g_task_report_new_error (file, callback, user_data,
7707 g_file_start_mountable,
7708 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7709 _("Operation not supported"));
7710 return;
7713 (* iface->start_mountable) (file,
7714 flags,
7715 start_operation,
7716 cancellable,
7717 callback,
7718 user_data);
7722 * g_file_start_mountable_finish:
7723 * @file: input #GFile
7724 * @result: a #GAsyncResult
7725 * @error: a #GError, or %NULL
7727 * Finishes a start operation. See g_file_start_mountable() for details.
7729 * Finish an asynchronous start operation that was started
7730 * with g_file_start_mountable().
7732 * Returns: %TRUE if the operation finished successfully. %FALSE
7733 * otherwise.
7735 * Since: 2.22
7737 gboolean
7738 g_file_start_mountable_finish (GFile *file,
7739 GAsyncResult *result,
7740 GError **error)
7742 GFileIface *iface;
7744 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7745 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7747 if (g_async_result_legacy_propagate_error (result, error))
7748 return FALSE;
7749 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7750 return g_task_propagate_boolean (G_TASK (result), error);
7752 iface = G_FILE_GET_IFACE (file);
7753 return (* iface->start_mountable_finish) (file, result, error);
7757 * g_file_stop_mountable:
7758 * @file: input #GFile
7759 * @flags: flags affecting the operation
7760 * @mount_operation: (allow-none): a #GMountOperation,
7761 * or %NULL to avoid user interaction.
7762 * @cancellable: (allow-none): optional #GCancellable object,
7763 * %NULL to ignore
7764 * @callback: (allow-none): a #GAsyncReadyCallback to call
7765 * when the request is satisfied, or %NULL
7766 * @user_data: the data to pass to callback function
7768 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7770 * If @cancellable is not %NULL, then the operation can be cancelled by
7771 * triggering the cancellable object from another thread. If the operation
7772 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7774 * When the operation is finished, @callback will be called.
7775 * You can then call g_file_stop_mountable_finish() to get
7776 * the result of the operation.
7778 * Since: 2.22
7780 void
7781 g_file_stop_mountable (GFile *file,
7782 GMountUnmountFlags flags,
7783 GMountOperation *mount_operation,
7784 GCancellable *cancellable,
7785 GAsyncReadyCallback callback,
7786 gpointer user_data)
7788 GFileIface *iface;
7790 g_return_if_fail (G_IS_FILE (file));
7792 iface = G_FILE_GET_IFACE (file);
7794 if (iface->stop_mountable == NULL)
7796 g_task_report_new_error (file, callback, user_data,
7797 g_file_stop_mountable,
7798 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7799 _("Operation not supported"));
7800 return;
7803 (* iface->stop_mountable) (file,
7804 flags,
7805 mount_operation,
7806 cancellable,
7807 callback,
7808 user_data);
7812 * g_file_stop_mountable_finish:
7813 * @file: input #GFile
7814 * @result: a #GAsyncResult
7815 * @error: a #GError, or %NULL
7817 * Finishes an stop operation, see g_file_stop_mountable() for details.
7819 * Finish an asynchronous stop operation that was started
7820 * with g_file_stop_mountable().
7822 * Returns: %TRUE if the operation finished successfully.
7823 * %FALSE otherwise.
7825 * Since: 2.22
7827 gboolean
7828 g_file_stop_mountable_finish (GFile *file,
7829 GAsyncResult *result,
7830 GError **error)
7832 GFileIface *iface;
7834 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7835 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7837 if (g_async_result_legacy_propagate_error (result, error))
7838 return FALSE;
7839 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7840 return g_task_propagate_boolean (G_TASK (result), error);
7842 iface = G_FILE_GET_IFACE (file);
7843 return (* iface->stop_mountable_finish) (file, result, error);
7847 * g_file_poll_mountable:
7848 * @file: input #GFile
7849 * @cancellable: optional #GCancellable object, %NULL to ignore
7850 * @callback: (allow-none): a #GAsyncReadyCallback to call
7851 * when the request is satisfied, or %NULL
7852 * @user_data: the data to pass to callback function
7854 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7856 * If @cancellable is not %NULL, then the operation can be cancelled by
7857 * triggering the cancellable object from another thread. If the operation
7858 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7860 * When the operation is finished, @callback will be called.
7861 * You can then call g_file_mount_mountable_finish() to get
7862 * the result of the operation.
7864 * Since: 2.22
7866 void
7867 g_file_poll_mountable (GFile *file,
7868 GCancellable *cancellable,
7869 GAsyncReadyCallback callback,
7870 gpointer user_data)
7872 GFileIface *iface;
7874 g_return_if_fail (G_IS_FILE (file));
7876 iface = G_FILE_GET_IFACE (file);
7878 if (iface->poll_mountable == NULL)
7880 g_task_report_new_error (file, callback, user_data,
7881 g_file_poll_mountable,
7882 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7883 _("Operation not supported"));
7884 return;
7887 (* iface->poll_mountable) (file,
7888 cancellable,
7889 callback,
7890 user_data);
7894 * g_file_poll_mountable_finish:
7895 * @file: input #GFile
7896 * @result: a #GAsyncResult
7897 * @error: a #GError, or %NULL
7899 * Finishes a poll operation. See g_file_poll_mountable() for details.
7901 * Finish an asynchronous poll operation that was polled
7902 * with g_file_poll_mountable().
7904 * Returns: %TRUE if the operation finished successfully. %FALSE
7905 * otherwise.
7907 * Since: 2.22
7909 gboolean
7910 g_file_poll_mountable_finish (GFile *file,
7911 GAsyncResult *result,
7912 GError **error)
7914 GFileIface *iface;
7916 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7917 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7919 if (g_async_result_legacy_propagate_error (result, error))
7920 return FALSE;
7921 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7922 return g_task_propagate_boolean (G_TASK (result), error);
7924 iface = G_FILE_GET_IFACE (file);
7925 return (* iface->poll_mountable_finish) (file, result, error);
7929 * g_file_supports_thread_contexts:
7930 * @file: a #GFile
7932 * Checks if @file supports <link
7933 * linkend="g-main-context-push-thread-default-context">thread-default
7934 * contexts</link>. If this returns %FALSE, you cannot perform
7935 * asynchronous operations on @file in a thread that has a
7936 * thread-default context.
7938 * Returns: Whether or not @file supports thread-default contexts.
7940 * Since: 2.22
7942 gboolean
7943 g_file_supports_thread_contexts (GFile *file)
7945 GFileIface *iface;
7947 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7949 iface = G_FILE_GET_IFACE (file);
7950 return iface->supports_thread_contexts;