1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
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.1 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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
84 * - g_file_new_build_filename() to create a file from path elements.
86 * One way to think of a #GFile is as an abstraction of a pathname. For
87 * normal files the system pathname is what is stored internally, but as
88 * #GFiles are extensible it could also be something else that corresponds
89 * to a pathname in a userspace implementation of a filesystem.
91 * #GFiles make up hierarchies of directories and files that correspond to
92 * the files on a filesystem. You can move through the file system with
93 * #GFile using g_file_get_parent() to get an identifier for the parent
94 * directory, g_file_get_child() to get a child within a directory,
95 * g_file_resolve_relative_path() to resolve a relative path between two
96 * #GFiles. There can be multiple hierarchies, so you may not end up at
97 * the same root if you repeatedly call g_file_get_parent() on two different
100 * All #GFiles have a basename (get with g_file_get_basename()). These names
101 * are byte strings that are used to identify the file on the filesystem
102 * (relative to its parent directory) and there is no guarantees that they
103 * have any particular charset encoding or even make any sense at all. If
104 * you want to use filenames in a user interface you should use the display
105 * name that you can get by requesting the
106 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
107 * This is guaranteed to be in UTF-8 and can be used in a user interface.
108 * But always store the real basename or the #GFile to use to actually
109 * access the file, because there is no way to go from a display name to
112 * Using #GFile as an identifier has the same weaknesses as using a path
113 * in that there may be multiple aliases for the same file. For instance,
114 * hard or soft links may cause two different #GFiles to refer to the same
115 * file. Other possible causes for aliases are: case insensitive filesystems,
116 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
117 * check if two #GFiles point to the same file you can query for the
118 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
119 * canonicalization of pathnames passed in, so that trivial differences in
120 * the path string used at creation (duplicated slashes, slash at end of
121 * path, "." or ".." path segments, etc) does not create different #GFiles.
123 * Many #GFile operations have both synchronous and asynchronous versions
124 * to suit your application. Asynchronous versions of synchronous functions
125 * simply have _async() appended to their function names. The asynchronous
126 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
127 * the operation, producing a GAsyncResult which is then passed to the
128 * function's matching _finish() operation.
130 * It is highly recommended to use asynchronous calls when running within a
131 * shared main loop, such as in the main thread of an application. This avoids
132 * I/O operations blocking other sources on the main loop from being dispatched.
133 * Synchronous I/O operations should be performed from worker threads. See the
134 * [introduction to asynchronous programming section][async-programming] for
137 * Some #GFile operations almost always take a noticeable amount of time, and
138 * so do not have synchronous analogs. Notable cases include:
139 * - g_file_mount_mountable() to mount a mountable file.
140 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
141 * - g_file_eject_mountable_with_operation() to eject a mountable file.
143 * ## Entity Tags # {#gfile-etag}
145 * One notable feature of #GFiles are entity tags, or "etags" for
146 * short. Entity tags are somewhat like a more abstract version of the
147 * traditional mtime, and can be used to quickly determine if the file
148 * has been modified from the version on the file system. See the
150 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
151 * for HTTP Etag headers, which are a very similar concept.
154 static void g_file_real_query_info_async (GFile
*file
,
155 const char *attributes
,
156 GFileQueryInfoFlags flags
,
158 GCancellable
*cancellable
,
159 GAsyncReadyCallback callback
,
161 static GFileInfo
* g_file_real_query_info_finish (GFile
*file
,
164 static void g_file_real_query_filesystem_info_async (GFile
*file
,
165 const char *attributes
,
167 GCancellable
*cancellable
,
168 GAsyncReadyCallback callback
,
170 static GFileInfo
* g_file_real_query_filesystem_info_finish (GFile
*file
,
173 static void g_file_real_enumerate_children_async (GFile
*file
,
174 const char *attributes
,
175 GFileQueryInfoFlags flags
,
177 GCancellable
*cancellable
,
178 GAsyncReadyCallback callback
,
180 static GFileEnumerator
* g_file_real_enumerate_children_finish (GFile
*file
,
183 static void g_file_real_read_async (GFile
*file
,
185 GCancellable
*cancellable
,
186 GAsyncReadyCallback callback
,
188 static GFileInputStream
* g_file_real_read_finish (GFile
*file
,
191 static void g_file_real_append_to_async (GFile
*file
,
192 GFileCreateFlags flags
,
194 GCancellable
*cancellable
,
195 GAsyncReadyCallback callback
,
197 static GFileOutputStream
*g_file_real_append_to_finish (GFile
*file
,
200 static void g_file_real_create_async (GFile
*file
,
201 GFileCreateFlags flags
,
203 GCancellable
*cancellable
,
204 GAsyncReadyCallback callback
,
206 static GFileOutputStream
*g_file_real_create_finish (GFile
*file
,
209 static void g_file_real_replace_async (GFile
*file
,
211 gboolean make_backup
,
212 GFileCreateFlags flags
,
214 GCancellable
*cancellable
,
215 GAsyncReadyCallback callback
,
217 static GFileOutputStream
*g_file_real_replace_finish (GFile
*file
,
220 static void g_file_real_delete_async (GFile
*file
,
222 GCancellable
*cancellable
,
223 GAsyncReadyCallback callback
,
225 static gboolean
g_file_real_delete_finish (GFile
*file
,
228 static void g_file_real_trash_async (GFile
*file
,
230 GCancellable
*cancellable
,
231 GAsyncReadyCallback callback
,
233 static gboolean
g_file_real_trash_finish (GFile
*file
,
236 static void g_file_real_make_directory_async (GFile
*file
,
238 GCancellable
*cancellable
,
239 GAsyncReadyCallback callback
,
241 static gboolean
g_file_real_make_directory_finish (GFile
*file
,
244 static void g_file_real_open_readwrite_async (GFile
*file
,
246 GCancellable
*cancellable
,
247 GAsyncReadyCallback callback
,
249 static GFileIOStream
* g_file_real_open_readwrite_finish (GFile
*file
,
252 static void g_file_real_create_readwrite_async (GFile
*file
,
253 GFileCreateFlags flags
,
255 GCancellable
*cancellable
,
256 GAsyncReadyCallback callback
,
258 static GFileIOStream
* g_file_real_create_readwrite_finish (GFile
*file
,
261 static void g_file_real_replace_readwrite_async (GFile
*file
,
263 gboolean make_backup
,
264 GFileCreateFlags flags
,
266 GCancellable
*cancellable
,
267 GAsyncReadyCallback callback
,
269 static GFileIOStream
* g_file_real_replace_readwrite_finish (GFile
*file
,
272 static gboolean
g_file_real_set_attributes_from_info (GFile
*file
,
274 GFileQueryInfoFlags flags
,
275 GCancellable
*cancellable
,
277 static void g_file_real_set_display_name_async (GFile
*file
,
278 const char *display_name
,
280 GCancellable
*cancellable
,
281 GAsyncReadyCallback callback
,
283 static GFile
* g_file_real_set_display_name_finish (GFile
*file
,
286 static void g_file_real_set_attributes_async (GFile
*file
,
288 GFileQueryInfoFlags flags
,
290 GCancellable
*cancellable
,
291 GAsyncReadyCallback callback
,
293 static gboolean
g_file_real_set_attributes_finish (GFile
*file
,
297 static void g_file_real_find_enclosing_mount_async (GFile
*file
,
299 GCancellable
*cancellable
,
300 GAsyncReadyCallback callback
,
302 static GMount
* g_file_real_find_enclosing_mount_finish (GFile
*file
,
305 static void g_file_real_copy_async (GFile
*source
,
307 GFileCopyFlags flags
,
309 GCancellable
*cancellable
,
310 GFileProgressCallback progress_callback
,
311 gpointer progress_callback_data
,
312 GAsyncReadyCallback callback
,
314 static gboolean
g_file_real_copy_finish (GFile
*file
,
318 static gboolean
g_file_real_measure_disk_usage (GFile
*file
,
319 GFileMeasureFlags flags
,
320 GCancellable
*cancellable
,
321 GFileMeasureProgressCallback progress_callback
,
322 gpointer progress_data
,
327 static void g_file_real_measure_disk_usage_async (GFile
*file
,
328 GFileMeasureFlags flags
,
330 GCancellable
*cancellable
,
331 GFileMeasureProgressCallback progress_callback
,
332 gpointer progress_data
,
333 GAsyncReadyCallback callback
,
335 static gboolean
g_file_real_measure_disk_usage_finish (GFile
*file
,
336 GAsyncResult
*result
,
342 typedef GFileIface GFileInterface
;
343 G_DEFINE_INTERFACE (GFile
, g_file
, G_TYPE_OBJECT
)
346 g_file_default_init (GFileIface
*iface
)
348 iface
->enumerate_children_async
= g_file_real_enumerate_children_async
;
349 iface
->enumerate_children_finish
= g_file_real_enumerate_children_finish
;
350 iface
->set_display_name_async
= g_file_real_set_display_name_async
;
351 iface
->set_display_name_finish
= g_file_real_set_display_name_finish
;
352 iface
->query_info_async
= g_file_real_query_info_async
;
353 iface
->query_info_finish
= g_file_real_query_info_finish
;
354 iface
->query_filesystem_info_async
= g_file_real_query_filesystem_info_async
;
355 iface
->query_filesystem_info_finish
= g_file_real_query_filesystem_info_finish
;
356 iface
->set_attributes_async
= g_file_real_set_attributes_async
;
357 iface
->set_attributes_finish
= g_file_real_set_attributes_finish
;
358 iface
->read_async
= g_file_real_read_async
;
359 iface
->read_finish
= g_file_real_read_finish
;
360 iface
->append_to_async
= g_file_real_append_to_async
;
361 iface
->append_to_finish
= g_file_real_append_to_finish
;
362 iface
->create_async
= g_file_real_create_async
;
363 iface
->create_finish
= g_file_real_create_finish
;
364 iface
->replace_async
= g_file_real_replace_async
;
365 iface
->replace_finish
= g_file_real_replace_finish
;
366 iface
->delete_file_async
= g_file_real_delete_async
;
367 iface
->delete_file_finish
= g_file_real_delete_finish
;
368 iface
->trash_async
= g_file_real_trash_async
;
369 iface
->trash_finish
= g_file_real_trash_finish
;
370 iface
->make_directory_async
= g_file_real_make_directory_async
;
371 iface
->make_directory_finish
= g_file_real_make_directory_finish
;
372 iface
->open_readwrite_async
= g_file_real_open_readwrite_async
;
373 iface
->open_readwrite_finish
= g_file_real_open_readwrite_finish
;
374 iface
->create_readwrite_async
= g_file_real_create_readwrite_async
;
375 iface
->create_readwrite_finish
= g_file_real_create_readwrite_finish
;
376 iface
->replace_readwrite_async
= g_file_real_replace_readwrite_async
;
377 iface
->replace_readwrite_finish
= g_file_real_replace_readwrite_finish
;
378 iface
->find_enclosing_mount_async
= g_file_real_find_enclosing_mount_async
;
379 iface
->find_enclosing_mount_finish
= g_file_real_find_enclosing_mount_finish
;
380 iface
->set_attributes_from_info
= g_file_real_set_attributes_from_info
;
381 iface
->copy_async
= g_file_real_copy_async
;
382 iface
->copy_finish
= g_file_real_copy_finish
;
383 iface
->measure_disk_usage
= g_file_real_measure_disk_usage
;
384 iface
->measure_disk_usage_async
= g_file_real_measure_disk_usage_async
;
385 iface
->measure_disk_usage_finish
= g_file_real_measure_disk_usage_finish
;
391 * @file: input #GFile
393 * Checks to see if a file is native to the platform.
395 * A native file s one expressed in the platform-native filename format,
396 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
397 * as it might be on a locally mounted remote filesystem.
399 * On some systems non-native files may be available using the native
400 * filesystem via a userspace filesystem (FUSE), in these cases this call
401 * will return %FALSE, but g_file_get_path() will still return a native path.
403 * This call does no blocking I/O.
405 * Returns: %TRUE if @file is native
408 g_file_is_native (GFile
*file
)
412 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
414 iface
= G_FILE_GET_IFACE (file
);
416 return (* iface
->is_native
) (file
);
421 * g_file_has_uri_scheme:
422 * @file: input #GFile
423 * @uri_scheme: a string containing a URI scheme
425 * Checks to see if a #GFile has a given URI scheme.
427 * This call does no blocking I/O.
429 * Returns: %TRUE if #GFile's backend supports the
430 * given URI scheme, %FALSE if URI scheme is %NULL,
431 * not supported, or #GFile is invalid.
434 g_file_has_uri_scheme (GFile
*file
,
435 const char *uri_scheme
)
439 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
440 g_return_val_if_fail (uri_scheme
!= NULL
, FALSE
);
442 iface
= G_FILE_GET_IFACE (file
);
444 return (* iface
->has_uri_scheme
) (file
, uri_scheme
);
449 * g_file_get_uri_scheme:
450 * @file: input #GFile
452 * Gets the URI scheme for a #GFile.
453 * RFC 3986 decodes the scheme as:
455 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
457 * Common schemes include "file", "http", "ftp", etc.
459 * This call does no blocking I/O.
461 * Returns: a string containing the URI scheme for the given
462 * #GFile. The returned string should be freed with g_free()
463 * when no longer needed.
466 g_file_get_uri_scheme (GFile
*file
)
470 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
472 iface
= G_FILE_GET_IFACE (file
);
474 return (* iface
->get_uri_scheme
) (file
);
479 * g_file_get_basename:
480 * @file: input #GFile
482 * Gets the base name (the last component of the path) for a given #GFile.
484 * If called for the top level of a system (such as the filesystem root
485 * or a uri like sftp://host/) it will return a single directory separator
486 * (and on Windows, possibly a drive letter).
488 * The base name is a byte string (not UTF-8). It has no defined encoding
489 * or rules other than it may not contain zero bytes. If you want to use
490 * filenames in a user interface you should use the display name that you
491 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
492 * attribute with g_file_query_info().
494 * This call does no blocking I/O.
496 * Returns: (type filename) (nullable): string containing the #GFile's
497 * base name, or %NULL if given #GFile is invalid. The returned string
498 * should be freed with g_free() when no longer needed.
501 g_file_get_basename (GFile
*file
)
505 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
507 iface
= G_FILE_GET_IFACE (file
);
509 return (* iface
->get_basename
) (file
);
514 * @file: input #GFile
516 * Gets the local pathname for #GFile, if one exists. If non-%NULL, this is
517 * guaranteed to be an absolute, canonical path. It might contain symlinks.
519 * This call does no blocking I/O.
521 * Returns: (type filename) (nullable): string containing the #GFile's path,
522 * or %NULL if no such path exists. The returned string should be freed
523 * with g_free() when no longer needed.
526 g_file_get_path (GFile
*file
)
530 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
532 iface
= G_FILE_GET_IFACE (file
);
534 return (* iface
->get_path
) (file
);
537 /* Original commit introducing this in libgsystem:
539 * fileutil: Handle recent: and trash: URIs
541 * The gs_file_get_path_cached() was rather brittle in its handling
542 * of URIs. It would assert() when a GFile didn't have a backing path
543 * (such as when handling trash: or recent: URIs), and didn't know
544 * how to get the target URI for those items either.
546 * Make sure that we do not assert() when a backing path cannot be
547 * found, and handle recent: and trash: URIs.
549 * https://bugzilla.gnome.org/show_bug.cgi?id=708435
552 file_get_target_path (GFile
*file
)
558 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI
, G_FILE_QUERY_INFO_NONE
, NULL
, NULL
);
561 target
= g_file_info_get_attribute_string (info
, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI
);
562 path
= g_filename_from_uri (target
, NULL
, NULL
);
563 g_object_unref (info
);
569 file_peek_path_generic (GFile
*file
)
572 static GQuark _file_path_quark
= 0;
574 if (G_UNLIKELY (_file_path_quark
) == 0)
575 _file_path_quark
= g_quark_from_static_string ("gio-file-path");
577 /* We need to be careful about threading, as two threads calling
578 * g_file_peek_path() on the same file could race: both would see
579 * (g_object_get_qdata(…) == NULL) to begin with, both would generate and add
580 * the path, but the second thread to add it would end up freeing the path
581 * set by the first thread. The first thread would still return the pointer
582 * to that freed path, though, resulting an a read-after-free. Handle that
583 * with a compare-and-swap loop. The g_object_*_qdata() functions are atomic. */
587 gchar
*new_path
= NULL
;
589 path
= g_object_get_qdata ((GObject
*)file
, _file_path_quark
);
594 if (g_file_has_uri_scheme (file
, "trash") ||
595 g_file_has_uri_scheme (file
, "recent"))
596 new_path
= file_get_target_path (file
);
598 new_path
= g_file_get_path (file
);
599 if (new_path
== NULL
)
602 /* By passing NULL here, we ensure we never replace existing data: */
603 if (g_object_replace_qdata ((GObject
*) file
, _file_path_quark
,
604 NULL
, (gpointer
) new_path
,
605 (GDestroyNotify
) g_free
, NULL
))
616 * @file: input #GFile
618 * Exactly like g_file_get_path(), but caches the result via
619 * g_object_set_qdata_full(). This is useful for example in C
620 * applications which mix `g_file_*` APIs with native ones. It
621 * also avoids an extra duplicated string when possible, so will be
622 * generally more efficient.
624 * This call does no blocking I/O.
626 * Returns: (type filename) (nullable): string containing the #GFile's path,
627 * or %NULL if no such path exists. The returned string is owned by @file.
631 g_file_peek_path (GFile
*file
)
633 if (G_IS_LOCAL_FILE (file
))
634 return _g_local_file_get_filename ((GLocalFile
*) file
);
635 return file_peek_path_generic (file
);
640 * @file: input #GFile
642 * Gets the URI for the @file.
644 * This call does no blocking I/O.
646 * Returns: a string containing the #GFile's URI.
647 * The returned string should be freed with g_free()
648 * when no longer needed.
651 g_file_get_uri (GFile
*file
)
655 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
657 iface
= G_FILE_GET_IFACE (file
);
659 return (* iface
->get_uri
) (file
);
663 * g_file_get_parse_name:
664 * @file: input #GFile
666 * Gets the parse name of the @file.
667 * A parse name is a UTF-8 string that describes the
668 * file such that one can get the #GFile back using
669 * g_file_parse_name().
671 * This is generally used to show the #GFile as a nice
672 * full-pathname kind of string in a user interface,
673 * like in a location entry.
675 * For local files with names that can safely be converted
676 * to UTF-8 the pathname is used, otherwise the IRI is used
677 * (a form of URI that allows UTF-8 characters unescaped).
679 * This call does no blocking I/O.
681 * Returns: a string containing the #GFile's parse name.
682 * The returned string should be freed with g_free()
683 * when no longer needed.
686 g_file_get_parse_name (GFile
*file
)
690 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
692 iface
= G_FILE_GET_IFACE (file
);
694 return (* iface
->get_parse_name
) (file
);
699 * @file: input #GFile
701 * Duplicates a #GFile handle. This operation does not duplicate
702 * the actual file or directory represented by the #GFile; see
703 * g_file_copy() if attempting to copy a file.
705 * This call does no blocking I/O.
707 * Returns: (transfer full): a new #GFile that is a duplicate
708 * of the given #GFile.
711 g_file_dup (GFile
*file
)
715 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
717 iface
= G_FILE_GET_IFACE (file
);
719 return (* iface
->dup
) (file
);
724 * @file: (type GFile): #gconstpointer to a #GFile
726 * Creates a hash value for a #GFile.
728 * This call does no blocking I/O.
731 * Returns: 0 if @file is not a valid #GFile, otherwise an
732 * integer that can be used as hash value for the #GFile.
733 * This function is intended for easily hashing a #GFile to
734 * add to a #GHashTable or similar data structure.
737 g_file_hash (gconstpointer file
)
741 g_return_val_if_fail (G_IS_FILE (file
), 0);
743 iface
= G_FILE_GET_IFACE (file
);
745 return (* iface
->hash
) ((GFile
*)file
);
750 * @file1: the first #GFile
751 * @file2: the second #GFile
753 * Checks if the two given #GFiles refer to the same file.
755 * Note that two #GFiles that differ can still refer to the same
756 * file on the filesystem due to various forms of filename
759 * This call does no blocking I/O.
761 * Returns: %TRUE if @file1 and @file2 are equal.
764 g_file_equal (GFile
*file1
,
769 g_return_val_if_fail (G_IS_FILE (file1
), FALSE
);
770 g_return_val_if_fail (G_IS_FILE (file2
), FALSE
);
775 if (G_TYPE_FROM_INSTANCE (file1
) != G_TYPE_FROM_INSTANCE (file2
))
778 iface
= G_FILE_GET_IFACE (file1
);
780 return (* iface
->equal
) (file1
, file2
);
786 * @file: input #GFile
788 * Gets the parent directory for the @file.
789 * If the @file represents the root directory of the
790 * file system, then %NULL will be returned.
792 * This call does no blocking I/O.
794 * Returns: (nullable) (transfer full): a #GFile structure to the
795 * parent of the given #GFile or %NULL if there is no parent. Free
796 * the returned object with g_object_unref().
799 g_file_get_parent (GFile
*file
)
803 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
805 iface
= G_FILE_GET_IFACE (file
);
807 return (* iface
->get_parent
) (file
);
812 * @file: input #GFile
813 * @parent: (nullable): the parent to check for, or %NULL
815 * Checks if @file has a parent, and optionally, if it is @parent.
817 * If @parent is %NULL then this function returns %TRUE if @file has any
818 * parent at all. If @parent is non-%NULL then %TRUE is only returned
819 * if @file is an immediate child of @parent.
821 * Returns: %TRUE if @file is an immediate child of @parent (or any parent in
822 * the case that @parent is %NULL).
827 g_file_has_parent (GFile
*file
,
830 GFile
*actual_parent
;
833 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
834 g_return_val_if_fail (parent
== NULL
|| G_IS_FILE (parent
), FALSE
);
836 actual_parent
= g_file_get_parent (file
);
838 if (actual_parent
!= NULL
)
841 result
= g_file_equal (parent
, actual_parent
);
845 g_object_unref (actual_parent
);
855 * @file: input #GFile
856 * @name: (type filename): string containing the child's basename
858 * Gets a child of @file with basename equal to @name.
860 * Note that the file with that specific name might not exist, but
861 * you can still have a #GFile that points to it. You can use this
862 * for instance to create that file.
864 * This call does no blocking I/O.
866 * Returns: (transfer full): a #GFile to a child specified by @name.
867 * Free the returned object with g_object_unref().
870 g_file_get_child (GFile
*file
,
873 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
874 g_return_val_if_fail (name
!= NULL
, NULL
);
876 return g_file_resolve_relative_path (file
, name
);
880 * g_file_get_child_for_display_name:
881 * @file: input #GFile
882 * @display_name: string to a possible child
883 * @error: return location for an error
885 * Gets the child of @file for a given @display_name (i.e. a UTF-8
886 * version of the name). If this function fails, it returns %NULL
887 * and @error will be set. This is very useful when constructing a
888 * #GFile for a new file and the user entered the filename in the
889 * user interface, for instance when you select a directory and
890 * type a filename in the file selector.
892 * This call does no blocking I/O.
894 * Returns: (transfer full): a #GFile to the specified child, or
895 * %NULL if the display name couldn't be converted.
896 * Free the returned object with g_object_unref().
899 g_file_get_child_for_display_name (GFile
*file
,
900 const char *display_name
,
905 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
906 g_return_val_if_fail (display_name
!= NULL
, NULL
);
908 iface
= G_FILE_GET_IFACE (file
);
910 return (* iface
->get_child_for_display_name
) (file
, display_name
, error
);
915 * @file: input #GFile
916 * @prefix: input #GFile
918 * Checks whether @file has the prefix specified by @prefix.
920 * In other words, if the names of initial elements of @file's
921 * pathname match @prefix. Only full pathname elements are matched,
922 * so a path like /foo is not considered a prefix of /foobar, only
925 * A #GFile is not a prefix of itself. If you want to check for
926 * equality, use g_file_equal().
928 * This call does no I/O, as it works purely on names. As such it can
929 * sometimes return %FALSE even if @file is inside a @prefix (from a
930 * filesystem point of view), because the prefix of @file is an alias
933 * Virtual: prefix_matches
934 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
938 g_file_has_prefix (GFile
*file
,
943 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
944 g_return_val_if_fail (G_IS_FILE (prefix
), FALSE
);
946 if (G_TYPE_FROM_INSTANCE (file
) != G_TYPE_FROM_INSTANCE (prefix
))
949 iface
= G_FILE_GET_IFACE (file
);
951 /* The vtable function differs in arg order since
952 * we're using the old contains_file call
954 return (* iface
->prefix_matches
) (prefix
, file
);
958 * g_file_get_relative_path:
959 * @parent: input #GFile
960 * @descendant: input #GFile
962 * Gets the path for @descendant relative to @parent.
964 * This call does no blocking I/O.
966 * Returns: (type filename) (nullable): string with the relative path from
967 * @descendant to @parent, or %NULL if @descendant doesn't have @parent as
968 * prefix. The returned string should be freed with g_free() when
972 g_file_get_relative_path (GFile
*parent
,
977 g_return_val_if_fail (G_IS_FILE (parent
), NULL
);
978 g_return_val_if_fail (G_IS_FILE (descendant
), NULL
);
980 if (G_TYPE_FROM_INSTANCE (parent
) != G_TYPE_FROM_INSTANCE (descendant
))
983 iface
= G_FILE_GET_IFACE (parent
);
985 return (* iface
->get_relative_path
) (parent
, descendant
);
989 * g_file_resolve_relative_path:
990 * @file: input #GFile
991 * @relative_path: (type filename): a given relative path string
993 * Resolves a relative path for @file to an absolute path.
995 * This call does no blocking I/O.
997 * Returns: (transfer full): #GFile to the resolved path.
998 * %NULL if @relative_path is %NULL or if @file is invalid.
999 * Free the returned object with g_object_unref().
1002 g_file_resolve_relative_path (GFile
*file
,
1003 const char *relative_path
)
1007 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1008 g_return_val_if_fail (relative_path
!= NULL
, NULL
);
1010 iface
= G_FILE_GET_IFACE (file
);
1012 return (* iface
->resolve_relative_path
) (file
, relative_path
);
1016 * g_file_enumerate_children:
1017 * @file: input #GFile
1018 * @attributes: an attribute query string
1019 * @flags: a set of #GFileQueryInfoFlags
1020 * @cancellable: (nullable): optional #GCancellable object,
1022 * @error: #GError for error reporting
1024 * Gets the requested information about the files in a directory.
1025 * The result is a #GFileEnumerator object that will give out
1026 * #GFileInfo objects for all the files in the directory.
1028 * The @attributes value is a string that specifies the file
1029 * attributes that should be gathered. It is not an error if
1030 * it's not possible to read a particular requested attribute
1031 * from a file - it just won't be set. @attributes should
1032 * be a comma-separated list of attributes or attribute wildcards.
1033 * The wildcard "*" means all attributes, and a wildcard like
1034 * "standard::*" means all attributes in the standard namespace.
1035 * An example attribute query be "standard::*,owner::user".
1036 * The standard attributes are available as defines, like
1037 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1039 * If @cancellable is not %NULL, then the operation can be cancelled
1040 * by triggering the cancellable object from another thread. If the
1041 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1044 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1045 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
1046 * error will be returned. Other errors are possible too.
1048 * Returns: (transfer full): A #GFileEnumerator if successful,
1049 * %NULL on error. Free the returned object with g_object_unref().
1052 g_file_enumerate_children (GFile
*file
,
1053 const char *attributes
,
1054 GFileQueryInfoFlags flags
,
1055 GCancellable
*cancellable
,
1060 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1062 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1065 iface
= G_FILE_GET_IFACE (file
);
1067 if (iface
->enumerate_children
== NULL
)
1069 g_set_error_literal (error
, G_IO_ERROR
,
1070 G_IO_ERROR_NOT_SUPPORTED
,
1071 _("Operation not supported"));
1075 return (* iface
->enumerate_children
) (file
, attributes
, flags
,
1076 cancellable
, error
);
1080 * g_file_enumerate_children_async:
1081 * @file: input #GFile
1082 * @attributes: an attribute query string
1083 * @flags: a set of #GFileQueryInfoFlags
1084 * @io_priority: the [I/O priority][io-priority] of the request
1085 * @cancellable: (nullable): optional #GCancellable object,
1087 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1088 * request is satisfied
1089 * @user_data: (closure): the data to pass to callback function
1091 * Asynchronously gets the requested information about the files
1092 * in a directory. The result is a #GFileEnumerator object that will
1093 * give out #GFileInfo objects for all the files in the directory.
1095 * For more details, see g_file_enumerate_children() which is
1096 * the synchronous version of this call.
1098 * When the operation is finished, @callback will be called. You can
1099 * then call g_file_enumerate_children_finish() to get the result of
1103 g_file_enumerate_children_async (GFile
*file
,
1104 const char *attributes
,
1105 GFileQueryInfoFlags flags
,
1107 GCancellable
*cancellable
,
1108 GAsyncReadyCallback callback
,
1113 g_return_if_fail (G_IS_FILE (file
));
1115 iface
= G_FILE_GET_IFACE (file
);
1116 (* iface
->enumerate_children_async
) (file
,
1126 * g_file_enumerate_children_finish:
1127 * @file: input #GFile
1128 * @res: a #GAsyncResult
1131 * Finishes an async enumerate children operation.
1132 * See g_file_enumerate_children_async().
1134 * Returns: (transfer full): a #GFileEnumerator or %NULL
1135 * if an error occurred.
1136 * Free the returned object with g_object_unref().
1139 g_file_enumerate_children_finish (GFile
*file
,
1145 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1146 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
1148 if (g_async_result_legacy_propagate_error (res
, error
))
1151 iface
= G_FILE_GET_IFACE (file
);
1152 return (* iface
->enumerate_children_finish
) (file
, res
, error
);
1156 * g_file_query_exists:
1157 * @file: input #GFile
1158 * @cancellable: (nullable): optional #GCancellable object,
1161 * Utility function to check if a particular file exists. This is
1162 * implemented using g_file_query_info() and as such does blocking I/O.
1164 * Note that in many cases it is [racy to first check for file existence](https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use)
1165 * and then execute something based on the outcome of that, because the
1166 * file might have been created or removed in between the operations. The
1167 * general approach to handling that is to not check, but just do the
1168 * operation and handle the errors as they come.
1170 * As an example of race-free checking, take the case of reading a file,
1171 * and if it doesn't exist, creating it. There are two racy versions: read
1172 * it, and on error create it; and: check if it exists, if not create it.
1173 * These can both result in two processes creating the file (with perhaps
1174 * a partially written file as the result). The correct approach is to
1175 * always try to create the file with g_file_create() which will either
1176 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1178 * However, in many cases an existence check is useful in a user interface,
1179 * for instance to make a menu item sensitive/insensitive, so that you don't
1180 * have to fool users that something is possible and then just show an error
1181 * dialog. If you do this, you should make sure to also handle the errors
1182 * that can happen due to races when you execute the operation.
1184 * Returns: %TRUE if the file exists (and can be detected without error),
1185 * %FALSE otherwise (or if cancelled).
1188 g_file_query_exists (GFile
*file
,
1189 GCancellable
*cancellable
)
1193 g_return_val_if_fail (G_IS_FILE(file
), FALSE
);
1195 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_TYPE
,
1196 G_FILE_QUERY_INFO_NONE
, cancellable
, NULL
);
1199 g_object_unref (info
);
1207 * g_file_query_file_type:
1208 * @file: input #GFile
1209 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1210 * @cancellable: (nullable): optional #GCancellable object,
1213 * Utility function to inspect the #GFileType of a file. This is
1214 * implemented using g_file_query_info() and as such does blocking I/O.
1216 * The primary use case of this method is to check if a file is
1217 * a regular file, directory, or symlink.
1219 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1220 * if the file does not exist
1225 g_file_query_file_type (GFile
*file
,
1226 GFileQueryInfoFlags flags
,
1227 GCancellable
*cancellable
)
1230 GFileType file_type
;
1232 g_return_val_if_fail (G_IS_FILE(file
), G_FILE_TYPE_UNKNOWN
);
1233 info
= g_file_query_info (file
, G_FILE_ATTRIBUTE_STANDARD_TYPE
, flags
,
1237 file_type
= g_file_info_get_file_type (info
);
1238 g_object_unref (info
);
1241 file_type
= G_FILE_TYPE_UNKNOWN
;
1247 * g_file_query_info:
1248 * @file: input #GFile
1249 * @attributes: an attribute query string
1250 * @flags: a set of #GFileQueryInfoFlags
1251 * @cancellable: (nullable): optional #GCancellable object,
1255 * Gets the requested information about specified @file.
1256 * The result is a #GFileInfo object that contains key-value
1257 * attributes (such as the type or size of the file).
1259 * The @attributes value is a string that specifies the file
1260 * attributes that should be gathered. It is not an error if
1261 * it's not possible to read a particular requested attribute
1262 * from a file - it just won't be set. @attributes should be a
1263 * comma-separated list of attributes or attribute wildcards.
1264 * The wildcard "*" means all attributes, and a wildcard like
1265 * "standard::*" means all attributes in the standard namespace.
1266 * An example attribute query be "standard::*,owner::user".
1267 * The standard attributes are available as defines, like
1268 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1270 * If @cancellable is not %NULL, then the operation can be cancelled
1271 * by triggering the cancellable object from another thread. If the
1272 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1275 * For symlinks, normally the information about the target of the
1276 * symlink is returned, rather than information about the symlink
1277 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1278 * in @flags the information about the symlink itself will be returned.
1279 * Also, for symlinks that point to non-existing files the information
1280 * about the symlink itself will be returned.
1282 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1283 * returned. Other errors are possible too, and depend on what kind of
1284 * filesystem the file is on.
1286 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1287 * on error. Free the returned object with g_object_unref().
1290 g_file_query_info (GFile
*file
,
1291 const char *attributes
,
1292 GFileQueryInfoFlags flags
,
1293 GCancellable
*cancellable
,
1298 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1300 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1303 iface
= G_FILE_GET_IFACE (file
);
1305 if (iface
->query_info
== NULL
)
1307 g_set_error_literal (error
, G_IO_ERROR
,
1308 G_IO_ERROR_NOT_SUPPORTED
,
1309 _("Operation not supported"));
1313 return (* iface
->query_info
) (file
, attributes
, flags
, cancellable
, error
);
1317 * g_file_query_info_async:
1318 * @file: input #GFile
1319 * @attributes: an attribute query string
1320 * @flags: a set of #GFileQueryInfoFlags
1321 * @io_priority: the [I/O priority][io-priority] of the request
1322 * @cancellable: (nullable): optional #GCancellable object,
1324 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1325 * request is satisfied
1326 * @user_data: (closure): the data to pass to callback function
1328 * Asynchronously gets the requested information about specified @file.
1329 * The result is a #GFileInfo object that contains key-value attributes
1330 * (such as type or size for the file).
1332 * For more details, see g_file_query_info() which is the synchronous
1333 * version of this call.
1335 * When the operation is finished, @callback will be called. You can
1336 * then call g_file_query_info_finish() to get the result of the operation.
1339 g_file_query_info_async (GFile
*file
,
1340 const char *attributes
,
1341 GFileQueryInfoFlags flags
,
1343 GCancellable
*cancellable
,
1344 GAsyncReadyCallback callback
,
1349 g_return_if_fail (G_IS_FILE (file
));
1351 iface
= G_FILE_GET_IFACE (file
);
1352 (* iface
->query_info_async
) (file
,
1362 * g_file_query_info_finish:
1363 * @file: input #GFile
1364 * @res: a #GAsyncResult
1367 * Finishes an asynchronous file info query.
1368 * See g_file_query_info_async().
1370 * Returns: (transfer full): #GFileInfo for given @file
1371 * or %NULL on error. Free the returned object with
1375 g_file_query_info_finish (GFile
*file
,
1381 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1382 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
1384 if (g_async_result_legacy_propagate_error (res
, error
))
1387 iface
= G_FILE_GET_IFACE (file
);
1388 return (* iface
->query_info_finish
) (file
, res
, error
);
1392 * g_file_query_filesystem_info:
1393 * @file: input #GFile
1394 * @attributes: an attribute query string
1395 * @cancellable: (nullable): optional #GCancellable object,
1399 * Similar to g_file_query_info(), but obtains information
1400 * about the filesystem the @file is on, rather than the file itself.
1401 * For instance the amount of space available and the type of
1404 * The @attributes value is a string that specifies the attributes
1405 * that should be gathered. It is not an error if it's not possible
1406 * to read a particular requested attribute from a file - it just
1407 * won't be set. @attributes should be a comma-separated list of
1408 * attributes or attribute wildcards. The wildcard "*" means all
1409 * attributes, and a wildcard like "filesystem::*" means all attributes
1410 * in the filesystem namespace. The standard namespace for filesystem
1411 * attributes is "filesystem". Common attributes of interest are
1412 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1413 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1414 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1416 * If @cancellable is not %NULL, then the operation can be cancelled
1417 * by triggering the cancellable object from another thread. If the
1418 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1421 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1422 * be returned. Other errors are possible too, and depend on what
1423 * kind of filesystem the file is on.
1425 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1426 * Free the returned object with g_object_unref().
1429 g_file_query_filesystem_info (GFile
*file
,
1430 const char *attributes
,
1431 GCancellable
*cancellable
,
1436 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1438 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1441 iface
= G_FILE_GET_IFACE (file
);
1443 if (iface
->query_filesystem_info
== NULL
)
1445 g_set_error_literal (error
, G_IO_ERROR
,
1446 G_IO_ERROR_NOT_SUPPORTED
,
1447 _("Operation not supported"));
1451 return (* iface
->query_filesystem_info
) (file
, attributes
, cancellable
, error
);
1455 * g_file_query_filesystem_info_async:
1456 * @file: input #GFile
1457 * @attributes: an attribute query string
1458 * @io_priority: the [I/O priority][io-priority] of the request
1459 * @cancellable: (nullable): optional #GCancellable object,
1461 * @callback: (scope async): a #GAsyncReadyCallback to call
1462 * when the request is satisfied
1463 * @user_data: (closure): the data to pass to callback function
1465 * Asynchronously gets the requested information about the filesystem
1466 * that the specified @file is on. The result is a #GFileInfo object
1467 * that contains key-value attributes (such as type or size for the
1470 * For more details, see g_file_query_filesystem_info() which is the
1471 * synchronous version of this call.
1473 * When the operation is finished, @callback will be called. You can
1474 * then call g_file_query_info_finish() to get the result of the
1478 g_file_query_filesystem_info_async (GFile
*file
,
1479 const char *attributes
,
1481 GCancellable
*cancellable
,
1482 GAsyncReadyCallback callback
,
1487 g_return_if_fail (G_IS_FILE (file
));
1489 iface
= G_FILE_GET_IFACE (file
);
1490 (* iface
->query_filesystem_info_async
) (file
,
1499 * g_file_query_filesystem_info_finish:
1500 * @file: input #GFile
1501 * @res: a #GAsyncResult
1504 * Finishes an asynchronous filesystem info query.
1505 * See g_file_query_filesystem_info_async().
1507 * Returns: (transfer full): #GFileInfo for given @file
1508 * or %NULL on error.
1509 * Free the returned object with g_object_unref().
1512 g_file_query_filesystem_info_finish (GFile
*file
,
1518 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1519 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
1521 if (g_async_result_legacy_propagate_error (res
, error
))
1524 iface
= G_FILE_GET_IFACE (file
);
1525 return (* iface
->query_filesystem_info_finish
) (file
, res
, error
);
1529 * g_file_find_enclosing_mount:
1530 * @file: input #GFile
1531 * @cancellable: (nullable): optional #GCancellable object,
1535 * Gets a #GMount for the #GFile.
1537 * If the #GFileIface for @file does not have a mount (e.g.
1538 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1539 * and %NULL will be returned.
1541 * If @cancellable is not %NULL, then the operation can be cancelled by
1542 * triggering the cancellable object from another thread. If the operation
1543 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1545 * Returns: (transfer full): a #GMount where the @file is located
1546 * or %NULL on error.
1547 * Free the returned object with g_object_unref().
1550 g_file_find_enclosing_mount (GFile
*file
,
1551 GCancellable
*cancellable
,
1556 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1558 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1561 iface
= G_FILE_GET_IFACE (file
);
1562 if (iface
->find_enclosing_mount
== NULL
)
1565 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
,
1566 /* Translators: This is an error message when
1567 * trying to find the enclosing (user visible)
1568 * mount of a file, but none exists.
1570 _("Containing mount does not exist"));
1574 return (* iface
->find_enclosing_mount
) (file
, cancellable
, error
);
1578 * g_file_find_enclosing_mount_async:
1580 * @io_priority: the [I/O priority][io-priority] of the request
1581 * @cancellable: (nullable): optional #GCancellable object,
1583 * @callback: (scope async): a #GAsyncReadyCallback to call
1584 * when the request is satisfied
1585 * @user_data: (closure): the data to pass to callback function
1587 * Asynchronously gets the mount for the file.
1589 * For more details, see g_file_find_enclosing_mount() which is
1590 * the synchronous version of this call.
1592 * When the operation is finished, @callback will be called.
1593 * You can then call g_file_find_enclosing_mount_finish() to
1594 * get the result of the operation.
1597 g_file_find_enclosing_mount_async (GFile
*file
,
1599 GCancellable
*cancellable
,
1600 GAsyncReadyCallback callback
,
1605 g_return_if_fail (G_IS_FILE (file
));
1607 iface
= G_FILE_GET_IFACE (file
);
1608 (* iface
->find_enclosing_mount_async
) (file
,
1616 * g_file_find_enclosing_mount_finish:
1618 * @res: a #GAsyncResult
1621 * Finishes an asynchronous find mount request.
1622 * See g_file_find_enclosing_mount_async().
1624 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1625 * Free the returned object with g_object_unref().
1628 g_file_find_enclosing_mount_finish (GFile
*file
,
1634 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1635 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
1637 if (g_async_result_legacy_propagate_error (res
, error
))
1640 iface
= G_FILE_GET_IFACE (file
);
1641 return (* iface
->find_enclosing_mount_finish
) (file
, res
, error
);
1647 * @file: #GFile to read
1648 * @cancellable: (nullable): a #GCancellable
1649 * @error: a #GError, or %NULL
1651 * Opens a file for reading. The result is a #GFileInputStream that
1652 * can be used to read the contents of the file.
1654 * If @cancellable is not %NULL, then the operation can be cancelled by
1655 * triggering the cancellable object from another thread. If the operation
1656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1658 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1659 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1660 * error will be returned. Other errors are possible too, and depend
1661 * on what kind of filesystem the file is on.
1664 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1665 * Free the returned object with g_object_unref().
1668 g_file_read (GFile
*file
,
1669 GCancellable
*cancellable
,
1674 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1676 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1679 iface
= G_FILE_GET_IFACE (file
);
1681 if (iface
->read_fn
== NULL
)
1683 g_set_error_literal (error
, G_IO_ERROR
,
1684 G_IO_ERROR_NOT_SUPPORTED
,
1685 _("Operation not supported"));
1689 return (* iface
->read_fn
) (file
, cancellable
, error
);
1694 * @file: input #GFile
1695 * @flags: a set of #GFileCreateFlags
1696 * @cancellable: (nullable): optional #GCancellable object,
1698 * @error: a #GError, or %NULL
1700 * Gets an output stream for appending data to the file.
1701 * If the file doesn't already exist it is created.
1703 * By default files created are generally readable by everyone,
1704 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1705 * will be made readable only to the current user, to the level that
1706 * is supported on the target filesystem.
1708 * If @cancellable is not %NULL, then the operation can be cancelled
1709 * by triggering the cancellable object from another thread. If the
1710 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1713 * Some file systems don't allow all file names, and may return an
1714 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1715 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1716 * possible too, and depend on what kind of filesystem the file is on.
1718 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1719 * Free the returned object with g_object_unref().
1722 g_file_append_to (GFile
*file
,
1723 GFileCreateFlags flags
,
1724 GCancellable
*cancellable
,
1729 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1731 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1734 iface
= G_FILE_GET_IFACE (file
);
1736 if (iface
->append_to
== NULL
)
1738 g_set_error_literal (error
, G_IO_ERROR
,
1739 G_IO_ERROR_NOT_SUPPORTED
,
1740 _("Operation not supported"));
1744 return (* iface
->append_to
) (file
, flags
, cancellable
, error
);
1749 * @file: input #GFile
1750 * @flags: a set of #GFileCreateFlags
1751 * @cancellable: (nullable): optional #GCancellable object,
1753 * @error: a #GError, or %NULL
1755 * Creates a new file and returns an output stream for writing to it.
1756 * The file must not already exist.
1758 * By default files created are generally readable by everyone,
1759 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1760 * will be made readable only to the current user, to the level
1761 * that is supported on the target filesystem.
1763 * If @cancellable is not %NULL, then the operation can be cancelled
1764 * by triggering the cancellable object from another thread. If the
1765 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1768 * If a file or directory with this name already exists the
1769 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1770 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1771 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1772 * be returned. Other errors are possible too, and depend on what kind
1773 * of filesystem the file is on.
1775 * Returns: (transfer full): a #GFileOutputStream for the newly created
1776 * file, or %NULL on error.
1777 * Free the returned object with g_object_unref().
1780 g_file_create (GFile
*file
,
1781 GFileCreateFlags flags
,
1782 GCancellable
*cancellable
,
1787 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1789 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1792 iface
= G_FILE_GET_IFACE (file
);
1794 if (iface
->create
== NULL
)
1796 g_set_error_literal (error
, G_IO_ERROR
,
1797 G_IO_ERROR_NOT_SUPPORTED
,
1798 _("Operation not supported"));
1802 return (* iface
->create
) (file
, flags
, cancellable
, error
);
1807 * @file: input #GFile
1808 * @etag: (nullable): an optional [entity tag][gfile-etag]
1809 * for the current #GFile, or #NULL to ignore
1810 * @make_backup: %TRUE if a backup should be created
1811 * @flags: a set of #GFileCreateFlags
1812 * @cancellable: (nullable): optional #GCancellable object,
1814 * @error: a #GError, or %NULL
1816 * Returns an output stream for overwriting the file, possibly
1817 * creating a backup copy of the file first. If the file doesn't exist,
1818 * it will be created.
1820 * This will try to replace the file in the safest way possible so
1821 * that any errors during the writing will not affect an already
1822 * existing copy of the file. For instance, for local files it
1823 * may write to a temporary file and then atomically rename over
1824 * the destination when the stream is closed.
1826 * By default files created are generally readable by everyone,
1827 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1828 * will be made readable only to the current user, to the level that
1829 * is supported on the target filesystem.
1831 * If @cancellable is not %NULL, then the operation can be cancelled
1832 * by triggering the cancellable object from another thread. If the
1833 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1836 * If you pass in a non-%NULL @etag value and @file already exists, then
1837 * this value is compared to the current entity tag of the file, and if
1838 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1839 * generally means that the file has been changed since you last read
1840 * it. You can get the new etag from g_file_output_stream_get_etag()
1841 * after you've finished writing and closed the #GFileOutputStream. When
1842 * you load a new file you can use g_file_input_stream_query_info() to
1843 * get the etag of the file.
1845 * If @make_backup is %TRUE, this function will attempt to make a
1846 * backup of the current file before overwriting it. If this fails
1847 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1848 * want to replace anyway, try again with @make_backup set to %FALSE.
1850 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1851 * be returned, and if the file is some other form of non-regular file
1852 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1853 * file systems don't allow all file names, and may return an
1854 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1855 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1856 * possible too, and depend on what kind of filesystem the file is on.
1858 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1859 * Free the returned object with g_object_unref().
1862 g_file_replace (GFile
*file
,
1864 gboolean make_backup
,
1865 GFileCreateFlags flags
,
1866 GCancellable
*cancellable
,
1871 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1873 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1876 iface
= G_FILE_GET_IFACE (file
);
1878 if (iface
->replace
== NULL
)
1880 g_set_error_literal (error
, G_IO_ERROR
,
1881 G_IO_ERROR_NOT_SUPPORTED
,
1882 _("Operation not supported"));
1886 /* Handle empty tag string as NULL in consistent way. */
1887 if (etag
&& *etag
== 0)
1890 return (* iface
->replace
) (file
, etag
, make_backup
, flags
, cancellable
, error
);
1894 * g_file_open_readwrite:
1895 * @file: #GFile to open
1896 * @cancellable: (nullable): a #GCancellable
1897 * @error: a #GError, or %NULL
1899 * Opens an existing file for reading and writing. The result is
1900 * a #GFileIOStream that can be used to read and write the contents
1903 * If @cancellable is not %NULL, then the operation can be cancelled
1904 * by triggering the cancellable object from another thread. If the
1905 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1908 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1909 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1910 * error will be returned. Other errors are possible too, and depend on
1911 * what kind of filesystem the file is on. Note that in many non-local
1912 * file cases read and write streams are not supported, so make sure you
1913 * really need to do read and write streaming, rather than just opening
1914 * for reading or writing.
1916 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1917 * Free the returned object with g_object_unref().
1922 g_file_open_readwrite (GFile
*file
,
1923 GCancellable
*cancellable
,
1928 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1930 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1933 iface
= G_FILE_GET_IFACE (file
);
1935 if (iface
->open_readwrite
== NULL
)
1937 g_set_error_literal (error
, G_IO_ERROR
,
1938 G_IO_ERROR_NOT_SUPPORTED
,
1939 _("Operation not supported"));
1943 return (* iface
->open_readwrite
) (file
, cancellable
, error
);
1947 * g_file_create_readwrite:
1949 * @flags: a set of #GFileCreateFlags
1950 * @cancellable: (nullable): optional #GCancellable object,
1952 * @error: return location for a #GError, or %NULL
1954 * Creates a new file and returns a stream for reading and
1955 * writing to it. The file must not already exist.
1957 * By default files created are generally readable by everyone,
1958 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1959 * will be made readable only to the current user, to the level
1960 * that is supported on the target filesystem.
1962 * If @cancellable is not %NULL, then the operation can be cancelled
1963 * by triggering the cancellable object from another thread. If the
1964 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1967 * If a file or directory with this name already exists, the
1968 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1969 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1970 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1971 * will be returned. Other errors are possible too, and depend on what
1972 * kind of filesystem the file is on.
1974 * Note that in many non-local file cases read and write streams are
1975 * not supported, so make sure you really need to do read and write
1976 * streaming, rather than just opening for reading or writing.
1978 * Returns: (transfer full): a #GFileIOStream for the newly created
1979 * file, or %NULL on error.
1980 * Free the returned object with g_object_unref().
1985 g_file_create_readwrite (GFile
*file
,
1986 GFileCreateFlags flags
,
1987 GCancellable
*cancellable
,
1992 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
1994 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
1997 iface
= G_FILE_GET_IFACE (file
);
1999 if (iface
->create_readwrite
== NULL
)
2001 g_set_error_literal (error
, G_IO_ERROR
,
2002 G_IO_ERROR_NOT_SUPPORTED
,
2003 _("Operation not supported"));
2007 return (* iface
->create_readwrite
) (file
, flags
, cancellable
, error
);
2011 * g_file_replace_readwrite:
2013 * @etag: (nullable): an optional [entity tag][gfile-etag]
2014 * for the current #GFile, or #NULL to ignore
2015 * @make_backup: %TRUE if a backup should be created
2016 * @flags: a set of #GFileCreateFlags
2017 * @cancellable: (nullable): optional #GCancellable object,
2019 * @error: return location for a #GError, or %NULL
2021 * Returns an output stream for overwriting the file in readwrite mode,
2022 * possibly creating a backup copy of the file first. If the file doesn't
2023 * exist, it will be created.
2025 * For details about the behaviour, see g_file_replace() which does the
2026 * same thing but returns an output stream only.
2028 * Note that in many non-local file cases read and write streams are not
2029 * supported, so make sure you really need to do read and write streaming,
2030 * rather than just opening for reading or writing.
2032 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2033 * Free the returned object with g_object_unref().
2038 g_file_replace_readwrite (GFile
*file
,
2040 gboolean make_backup
,
2041 GFileCreateFlags flags
,
2042 GCancellable
*cancellable
,
2047 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2049 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
2052 iface
= G_FILE_GET_IFACE (file
);
2054 if (iface
->replace_readwrite
== NULL
)
2056 g_set_error_literal (error
, G_IO_ERROR
,
2057 G_IO_ERROR_NOT_SUPPORTED
,
2058 _("Operation not supported"));
2062 return (* iface
->replace_readwrite
) (file
, etag
, make_backup
, flags
, cancellable
, error
);
2066 * g_file_read_async:
2067 * @file: input #GFile
2068 * @io_priority: the [I/O priority][io-priority] of the request
2069 * @cancellable: (nullable): optional #GCancellable object,
2071 * @callback: (scope async): a #GAsyncReadyCallback to call
2072 * when the request is satisfied
2073 * @user_data: (closure): the data to pass to callback function
2075 * Asynchronously opens @file for reading.
2077 * For more details, see g_file_read() which is
2078 * the synchronous version of this call.
2080 * When the operation is finished, @callback will be called.
2081 * You can then call g_file_read_finish() to get the result
2085 g_file_read_async (GFile
*file
,
2087 GCancellable
*cancellable
,
2088 GAsyncReadyCallback callback
,
2093 g_return_if_fail (G_IS_FILE (file
));
2095 iface
= G_FILE_GET_IFACE (file
);
2096 (* iface
->read_async
) (file
,
2104 * g_file_read_finish:
2105 * @file: input #GFile
2106 * @res: a #GAsyncResult
2107 * @error: a #GError, or %NULL
2109 * Finishes an asynchronous file read operation started with
2110 * g_file_read_async().
2112 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2113 * Free the returned object with g_object_unref().
2116 g_file_read_finish (GFile
*file
,
2122 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2123 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2125 if (g_async_result_legacy_propagate_error (res
, error
))
2128 iface
= G_FILE_GET_IFACE (file
);
2129 return (* iface
->read_finish
) (file
, res
, error
);
2133 * g_file_append_to_async:
2134 * @file: input #GFile
2135 * @flags: a set of #GFileCreateFlags
2136 * @io_priority: the [I/O priority][io-priority] of the request
2137 * @cancellable: (nullable): optional #GCancellable object,
2139 * @callback: (scope async): a #GAsyncReadyCallback to call
2140 * when the request is satisfied
2141 * @user_data: (closure): the data to pass to callback function
2143 * Asynchronously opens @file for appending.
2145 * For more details, see g_file_append_to() which is
2146 * the synchronous version of this call.
2148 * When the operation is finished, @callback will be called.
2149 * You can then call g_file_append_to_finish() to get the result
2153 g_file_append_to_async (GFile
*file
,
2154 GFileCreateFlags flags
,
2156 GCancellable
*cancellable
,
2157 GAsyncReadyCallback callback
,
2162 g_return_if_fail (G_IS_FILE (file
));
2164 iface
= G_FILE_GET_IFACE (file
);
2165 (* iface
->append_to_async
) (file
,
2174 * g_file_append_to_finish:
2175 * @file: input #GFile
2176 * @res: #GAsyncResult
2177 * @error: a #GError, or %NULL
2179 * Finishes an asynchronous file append operation started with
2180 * g_file_append_to_async().
2182 * Returns: (transfer full): a valid #GFileOutputStream
2183 * or %NULL on error.
2184 * Free the returned object with g_object_unref().
2187 g_file_append_to_finish (GFile
*file
,
2193 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2194 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2196 if (g_async_result_legacy_propagate_error (res
, error
))
2199 iface
= G_FILE_GET_IFACE (file
);
2200 return (* iface
->append_to_finish
) (file
, res
, error
);
2204 * g_file_create_async:
2205 * @file: input #GFile
2206 * @flags: a set of #GFileCreateFlags
2207 * @io_priority: the [I/O priority][io-priority] of the request
2208 * @cancellable: (nullable): optional #GCancellable object,
2210 * @callback: (scope async): a #GAsyncReadyCallback to call
2211 * when the request is satisfied
2212 * @user_data: (closure): the data to pass to callback function
2214 * Asynchronously creates a new file and returns an output stream
2215 * for writing to it. The file must not already exist.
2217 * For more details, see g_file_create() which is
2218 * the synchronous version of this call.
2220 * When the operation is finished, @callback will be called.
2221 * You can then call g_file_create_finish() to get the result
2225 g_file_create_async (GFile
*file
,
2226 GFileCreateFlags flags
,
2228 GCancellable
*cancellable
,
2229 GAsyncReadyCallback callback
,
2234 g_return_if_fail (G_IS_FILE (file
));
2236 iface
= G_FILE_GET_IFACE (file
);
2237 (* iface
->create_async
) (file
,
2246 * g_file_create_finish:
2247 * @file: input #GFile
2248 * @res: a #GAsyncResult
2249 * @error: a #GError, or %NULL
2251 * Finishes an asynchronous file create operation started with
2252 * g_file_create_async().
2254 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2255 * Free the returned object with g_object_unref().
2258 g_file_create_finish (GFile
*file
,
2264 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2265 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2267 if (g_async_result_legacy_propagate_error (res
, error
))
2270 iface
= G_FILE_GET_IFACE (file
);
2271 return (* iface
->create_finish
) (file
, res
, error
);
2275 * g_file_replace_async:
2276 * @file: input #GFile
2277 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2278 * or %NULL to ignore
2279 * @make_backup: %TRUE if a backup should be created
2280 * @flags: a set of #GFileCreateFlags
2281 * @io_priority: the [I/O priority][io-priority] of the request
2282 * @cancellable: (nullable): optional #GCancellable object,
2284 * @callback: (scope async): a #GAsyncReadyCallback to call
2285 * when the request is satisfied
2286 * @user_data: (closure): the data to pass to callback function
2288 * Asynchronously overwrites the file, replacing the contents,
2289 * possibly creating a backup copy of the file first.
2291 * For more details, see g_file_replace() which is
2292 * the synchronous version of this call.
2294 * When the operation is finished, @callback will be called.
2295 * You can then call g_file_replace_finish() to get the result
2299 g_file_replace_async (GFile
*file
,
2301 gboolean make_backup
,
2302 GFileCreateFlags flags
,
2304 GCancellable
*cancellable
,
2305 GAsyncReadyCallback callback
,
2310 g_return_if_fail (G_IS_FILE (file
));
2312 iface
= G_FILE_GET_IFACE (file
);
2313 (* iface
->replace_async
) (file
,
2324 * g_file_replace_finish:
2325 * @file: input #GFile
2326 * @res: a #GAsyncResult
2327 * @error: a #GError, or %NULL
2329 * Finishes an asynchronous file replace operation started with
2330 * g_file_replace_async().
2332 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2333 * Free the returned object with g_object_unref().
2336 g_file_replace_finish (GFile
*file
,
2342 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2343 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2345 if (g_async_result_legacy_propagate_error (res
, error
))
2348 iface
= G_FILE_GET_IFACE (file
);
2349 return (* iface
->replace_finish
) (file
, res
, error
);
2353 * g_file_open_readwrite_async
2354 * @file: input #GFile
2355 * @io_priority: the [I/O priority][io-priority] of the request
2356 * @cancellable: (nullable): optional #GCancellable object,
2358 * @callback: (scope async): a #GAsyncReadyCallback to call
2359 * when the request is satisfied
2360 * @user_data: (closure): the data to pass to callback function
2362 * Asynchronously opens @file for reading and writing.
2364 * For more details, see g_file_open_readwrite() which is
2365 * the synchronous version of this call.
2367 * When the operation is finished, @callback will be called.
2368 * You can then call g_file_open_readwrite_finish() to get
2369 * the result of the operation.
2374 g_file_open_readwrite_async (GFile
*file
,
2376 GCancellable
*cancellable
,
2377 GAsyncReadyCallback callback
,
2382 g_return_if_fail (G_IS_FILE (file
));
2384 iface
= G_FILE_GET_IFACE (file
);
2385 (* iface
->open_readwrite_async
) (file
,
2393 * g_file_open_readwrite_finish:
2394 * @file: input #GFile
2395 * @res: a #GAsyncResult
2396 * @error: a #GError, or %NULL
2398 * Finishes an asynchronous file read operation started with
2399 * g_file_open_readwrite_async().
2401 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2402 * Free the returned object with g_object_unref().
2407 g_file_open_readwrite_finish (GFile
*file
,
2413 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2416 if (g_async_result_legacy_propagate_error (res
, error
))
2419 iface
= G_FILE_GET_IFACE (file
);
2420 return (* iface
->open_readwrite_finish
) (file
, res
, error
);
2424 * g_file_create_readwrite_async:
2425 * @file: input #GFile
2426 * @flags: a set of #GFileCreateFlags
2427 * @io_priority: the [I/O priority][io-priority] of the request
2428 * @cancellable: (nullable): optional #GCancellable object,
2430 * @callback: (scope async): a #GAsyncReadyCallback to call
2431 * when the request is satisfied
2432 * @user_data: (closure): the data to pass to callback function
2434 * Asynchronously creates a new file and returns a stream
2435 * for reading and writing to it. The file must not already exist.
2437 * For more details, see g_file_create_readwrite() which is
2438 * the synchronous version of this call.
2440 * When the operation is finished, @callback will be called.
2441 * You can then call g_file_create_readwrite_finish() to get
2442 * the result of the operation.
2447 g_file_create_readwrite_async (GFile
*file
,
2448 GFileCreateFlags flags
,
2450 GCancellable
*cancellable
,
2451 GAsyncReadyCallback callback
,
2456 g_return_if_fail (G_IS_FILE (file
));
2458 iface
= G_FILE_GET_IFACE (file
);
2459 (* iface
->create_readwrite_async
) (file
,
2468 * g_file_create_readwrite_finish:
2469 * @file: input #GFile
2470 * @res: a #GAsyncResult
2471 * @error: a #GError, or %NULL
2473 * Finishes an asynchronous file create operation started with
2474 * g_file_create_readwrite_async().
2476 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2477 * Free the returned object with g_object_unref().
2482 g_file_create_readwrite_finish (GFile
*file
,
2488 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2489 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2491 if (g_async_result_legacy_propagate_error (res
, error
))
2494 iface
= G_FILE_GET_IFACE (file
);
2495 return (* iface
->create_readwrite_finish
) (file
, res
, error
);
2499 * g_file_replace_readwrite_async:
2500 * @file: input #GFile
2501 * @etag: (nullable): an [entity tag][gfile-etag] for the current #GFile,
2502 * or %NULL to ignore
2503 * @make_backup: %TRUE if a backup should be created
2504 * @flags: a set of #GFileCreateFlags
2505 * @io_priority: the [I/O priority][io-priority] of the request
2506 * @cancellable: (nullable): optional #GCancellable object,
2508 * @callback: (scope async): a #GAsyncReadyCallback to call
2509 * when the request is satisfied
2510 * @user_data: (closure): the data to pass to callback function
2512 * Asynchronously overwrites the file in read-write mode,
2513 * replacing the contents, possibly creating a backup copy
2514 * of the file first.
2516 * For more details, see g_file_replace_readwrite() which is
2517 * the synchronous version of this call.
2519 * When the operation is finished, @callback will be called.
2520 * You can then call g_file_replace_readwrite_finish() to get
2521 * the result of the operation.
2526 g_file_replace_readwrite_async (GFile
*file
,
2528 gboolean make_backup
,
2529 GFileCreateFlags flags
,
2531 GCancellable
*cancellable
,
2532 GAsyncReadyCallback callback
,
2537 g_return_if_fail (G_IS_FILE (file
));
2539 iface
= G_FILE_GET_IFACE (file
);
2540 (* iface
->replace_readwrite_async
) (file
,
2551 * g_file_replace_readwrite_finish:
2552 * @file: input #GFile
2553 * @res: a #GAsyncResult
2554 * @error: a #GError, or %NULL
2556 * Finishes an asynchronous file replace operation started with
2557 * g_file_replace_readwrite_async().
2559 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2560 * Free the returned object with g_object_unref().
2565 g_file_replace_readwrite_finish (GFile
*file
,
2571 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
2572 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
2574 if (g_async_result_legacy_propagate_error (res
, error
))
2577 iface
= G_FILE_GET_IFACE (file
);
2578 return (* iface
->replace_readwrite_finish
) (file
, res
, error
);
2582 copy_symlink (GFile
*destination
,
2583 GFileCopyFlags flags
,
2584 GCancellable
*cancellable
,
2589 gboolean tried_delete
;
2591 GFileType file_type
;
2593 tried_delete
= FALSE
;
2597 if (!g_file_make_symbolic_link (destination
, target
, cancellable
, &my_error
))
2599 /* Maybe it already existed, and we want to overwrite? */
2600 if (!tried_delete
&& (flags
& G_FILE_COPY_OVERWRITE
) &&
2601 my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_EXISTS
)
2603 g_clear_error (&my_error
);
2605 /* Don't overwrite if the destination is a directory */
2606 info
= g_file_query_info (destination
, G_FILE_ATTRIBUTE_STANDARD_TYPE
,
2607 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2608 cancellable
, &my_error
);
2611 file_type
= g_file_info_get_file_type (info
);
2612 g_object_unref (info
);
2614 if (file_type
== G_FILE_TYPE_DIRECTORY
)
2616 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_IS_DIRECTORY
,
2617 _("Can’t copy over directory"));
2622 if (!g_file_delete (destination
, cancellable
, error
))
2625 tried_delete
= TRUE
;
2629 g_propagate_error (error
, my_error
);
2636 static GFileInputStream
*
2637 open_source_for_copy (GFile
*source
,
2639 GFileCopyFlags flags
,
2640 GCancellable
*cancellable
,
2644 GFileInputStream
*ret
;
2646 GFileType file_type
;
2649 ret
= g_file_read (source
, cancellable
, &my_error
);
2653 /* There was an error opening the source, try to set a good error for it: */
2654 if (my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_IS_DIRECTORY
)
2656 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2657 * as that is less useful to the app. Better check for errors on the
2660 g_error_free (my_error
);
2663 info
= g_file_query_info (destination
, G_FILE_ATTRIBUTE_STANDARD_TYPE
,
2664 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2665 cancellable
, &my_error
);
2667 g_file_info_has_attribute (info
, G_FILE_ATTRIBUTE_STANDARD_TYPE
))
2669 file_type
= g_file_info_get_file_type (info
);
2670 g_object_unref (info
);
2672 if (flags
& G_FILE_COPY_OVERWRITE
)
2674 if (file_type
== G_FILE_TYPE_DIRECTORY
)
2676 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_WOULD_MERGE
,
2677 _("Can’t copy directory over directory"));
2680 /* continue to would_recurse error */
2684 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
,
2685 _("Target file exists"));
2691 /* Error getting info from target, return that error
2692 * (except for NOT_FOUND, which is no error here)
2694 g_clear_object (&info
);
2695 if (my_error
!= NULL
&& !g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
2697 g_propagate_error (error
, my_error
);
2700 g_clear_error (&my_error
);
2703 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_WOULD_RECURSE
,
2704 _("Can’t recursively copy directory"));
2708 g_propagate_error (error
, my_error
);
2713 should_copy (GFileAttributeInfo
*info
,
2714 gboolean copy_all_attributes
,
2715 gboolean skip_perms
)
2717 if (skip_perms
&& strcmp(info
->name
, "unix::mode") == 0)
2720 if (copy_all_attributes
)
2721 return info
->flags
& G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED
;
2722 return info
->flags
& G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE
;
2726 build_attribute_list_for_copy (GFile
*file
,
2727 GFileCopyFlags flags
,
2728 char **out_attributes
,
2729 GCancellable
*cancellable
,
2732 gboolean ret
= FALSE
;
2733 GFileAttributeInfoList
*attributes
= NULL
, *namespaces
= NULL
;
2737 gboolean copy_all_attributes
;
2738 gboolean skip_perms
;
2740 copy_all_attributes
= flags
& G_FILE_COPY_ALL_METADATA
;
2741 skip_perms
= (flags
& G_FILE_COPY_TARGET_DEFAULT_PERMS
) != 0;
2743 /* Ignore errors here, if the target supports no attributes there is
2744 * nothing to copy. We still honor the cancellable though.
2746 attributes
= g_file_query_settable_attributes (file
, cancellable
, NULL
);
2747 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
2750 namespaces
= g_file_query_writable_namespaces (file
, cancellable
, NULL
);
2751 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
2754 if (attributes
== NULL
&& namespaces
== NULL
)
2758 s
= g_string_new ("");
2762 for (i
= 0; i
< attributes
->n_infos
; i
++)
2764 if (should_copy (&attributes
->infos
[i
], copy_all_attributes
, skip_perms
))
2769 g_string_append_c (s
, ',');
2771 g_string_append (s
, attributes
->infos
[i
].name
);
2778 for (i
= 0; i
< namespaces
->n_infos
; i
++)
2780 if (should_copy (&namespaces
->infos
[i
], copy_all_attributes
, FALSE
))
2785 g_string_append_c (s
, ',');
2787 g_string_append (s
, namespaces
->infos
[i
].name
);
2788 g_string_append (s
, "::*");
2794 *out_attributes
= g_string_free (s
, FALSE
);
2798 g_string_free (s
, TRUE
);
2800 g_file_attribute_info_list_unref (attributes
);
2802 g_file_attribute_info_list_unref (namespaces
);
2808 * g_file_copy_attributes:
2809 * @source: a #GFile with attributes
2810 * @destination: a #GFile to copy attributes to
2811 * @flags: a set of #GFileCopyFlags
2812 * @cancellable: (nullable): optional #GCancellable object,
2814 * @error: a #GError, %NULL to ignore
2816 * Copies the file attributes from @source to @destination.
2818 * Normally only a subset of the file attributes are copied,
2819 * those that are copies in a normal file copy operation
2820 * (which for instance does not include e.g. owner). However
2821 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2822 * all the metadata that is possible to copy is copied. This
2823 * is useful when implementing move by copy + delete source.
2825 * Returns: %TRUE if the attributes were copied successfully,
2829 g_file_copy_attributes (GFile
*source
,
2831 GFileCopyFlags flags
,
2832 GCancellable
*cancellable
,
2835 char *attrs_to_read
;
2838 gboolean source_nofollow_symlinks
;
2840 if (!build_attribute_list_for_copy (destination
, flags
, &attrs_to_read
,
2841 cancellable
, error
))
2844 source_nofollow_symlinks
= flags
& G_FILE_COPY_NOFOLLOW_SYMLINKS
;
2846 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2847 * we just don't copy it.
2849 info
= g_file_query_info (source
, attrs_to_read
,
2850 source_nofollow_symlinks
? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
:0,
2854 g_free (attrs_to_read
);
2859 res
= g_file_set_attributes_from_info (destination
,
2861 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
2864 g_object_unref (info
);
2870 /* 256k minus malloc overhead */
2871 #define STREAM_BUFFER_SIZE (1024*256 - 2 *sizeof(gpointer))
2874 copy_stream_with_progress (GInputStream
*in
,
2877 GCancellable
*cancellable
,
2878 GFileProgressCallback progress_callback
,
2879 gpointer progress_callback_data
,
2884 goffset current_size
;
2891 /* avoid performance impact of querying total size when it's not needed */
2892 if (progress_callback
)
2894 info
= g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in
),
2895 G_FILE_ATTRIBUTE_STANDARD_SIZE
,
2899 if (g_file_info_has_attribute (info
, G_FILE_ATTRIBUTE_STANDARD_SIZE
))
2900 total_size
= g_file_info_get_size (info
);
2901 g_object_unref (info
);
2904 if (total_size
== -1)
2906 info
= g_file_query_info (source
,
2907 G_FILE_ATTRIBUTE_STANDARD_SIZE
,
2908 G_FILE_QUERY_INFO_NONE
,
2912 if (g_file_info_has_attribute (info
, G_FILE_ATTRIBUTE_STANDARD_SIZE
))
2913 total_size
= g_file_info_get_size (info
);
2914 g_object_unref (info
);
2919 if (total_size
== -1)
2922 buffer
= g_malloc0 (STREAM_BUFFER_SIZE
);
2927 n_read
= g_input_stream_read (in
, buffer
, STREAM_BUFFER_SIZE
, cancellable
, error
);
2937 current_size
+= n_read
;
2939 res
= g_output_stream_write_all (out
, buffer
, n_read
, &n_written
, cancellable
, error
);
2943 if (progress_callback
)
2944 progress_callback (current_size
, total_size
, progress_callback_data
);
2948 /* Make sure we send full copied size */
2949 if (progress_callback
)
2950 progress_callback (current_size
, total_size
, progress_callback_data
);
2958 do_splice (int fd_in
,
2963 long *bytes_transferd
,
2969 result
= splice (fd_in
, off_in
, fd_out
, off_out
, len
, SPLICE_F_MORE
);
2977 else if (errsv
== ENOSYS
|| errsv
== EINVAL
)
2978 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
2979 _("Splice not supported"));
2981 g_set_error (error
, G_IO_ERROR
,
2982 g_io_error_from_errno (errsv
),
2983 _("Error splicing file: %s"),
2984 g_strerror (errsv
));
2989 *bytes_transferd
= result
;
2994 splice_stream_with_progress (GInputStream
*in
,
2996 GCancellable
*cancellable
,
2997 GFileProgressCallback progress_callback
,
2998 gpointer progress_callback_data
,
3001 int buffer
[2] = { -1, -1 };
3009 fd_in
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in
));
3010 fd_out
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out
));
3012 if (!g_unix_open_pipe (buffer
, FD_CLOEXEC
, error
))
3015 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3016 * pipe size. See: https://bugzilla.gnome.org/791457 */
3017 buffer_size
= fcntl (buffer
[1], F_SETPIPE_SZ
, 1024 * 1024);
3018 if (buffer_size
<= 0)
3021 buffer_size
= fcntl (buffer
[1], F_GETPIPE_SZ
);
3024 if (buffer_size
<= 0)
3026 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
3027 _("Error splicing file: %s"), g_strerror (errsv
));
3033 g_assert (buffer_size
> 0);
3036 /* avoid performance impact of querying total size when it's not needed */
3037 if (progress_callback
)
3041 if (fstat (fd_in
, &sbuf
) == 0)
3042 total_size
= sbuf
.st_size
;
3045 if (total_size
== -1)
3048 offset_in
= offset_out
= 0;
3055 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3058 if (!do_splice (fd_in
, &offset_in
, buffer
[1], NULL
, buffer_size
, &n_read
, error
))
3069 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3072 if (!do_splice (buffer
[0], NULL
, fd_out
, &offset_out
, n_read
, &n_written
, error
))
3075 n_read
-= n_written
;
3078 if (progress_callback
)
3079 progress_callback (offset_in
, total_size
, progress_callback_data
);
3082 /* Make sure we send full copied size */
3083 if (progress_callback
)
3084 progress_callback (offset_in
, total_size
, progress_callback_data
);
3086 if (!g_close (buffer
[0], error
))
3089 if (!g_close (buffer
[1], error
))
3093 if (buffer
[0] != -1)
3094 (void) g_close (buffer
[0], NULL
);
3095 if (buffer
[1] != -1)
3096 (void) g_close (buffer
[1], NULL
);
3104 btrfs_reflink_with_progress (GInputStream
*in
,
3107 GCancellable
*cancellable
,
3108 GFileProgressCallback progress_callback
,
3109 gpointer progress_callback_data
,
3112 goffset source_size
;
3116 fd_in
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in
));
3117 fd_out
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out
));
3119 if (progress_callback
)
3120 source_size
= g_file_info_get_size (info
);
3122 /* Btrfs clone ioctl properties:
3123 * - Works at the inode level
3124 * - Doesn't work with directories
3125 * - Always follows symlinks (source and destination)
3127 * By the time we get here, *in and *out are both regular files */
3128 ret
= ioctl (fd_out
, BTRFS_IOC_CLONE
, fd_in
);
3134 g_set_error_literal (error
, G_IO_ERROR
,
3135 G_IO_ERROR_NOT_SUPPORTED
,
3136 _("Copy (reflink/clone) between mounts is not supported"));
3137 else if (errsv
== EINVAL
)
3138 g_set_error_literal (error
, G_IO_ERROR
,
3139 G_IO_ERROR_NOT_SUPPORTED
,
3140 _("Copy (reflink/clone) is not supported or invalid"));
3142 /* Most probably something odd happened; retry with fallback */
3143 g_set_error_literal (error
, G_IO_ERROR
,
3144 G_IO_ERROR_NOT_SUPPORTED
,
3145 _("Copy (reflink/clone) is not supported or didn’t work"));
3146 /* We retry with fallback for all error cases because Btrfs is currently
3147 * unstable, and so we can't trust it to do clone properly.
3148 * In addition, any hard errors here would cause the same failure in the
3149 * fallback manual copy as well. */
3153 /* Make sure we send full copied size */
3154 if (progress_callback
)
3155 progress_callback (source_size
, source_size
, progress_callback_data
);
3162 file_copy_fallback (GFile
*source
,
3164 GFileCopyFlags flags
,
3165 GCancellable
*cancellable
,
3166 GFileProgressCallback progress_callback
,
3167 gpointer progress_callback_data
,
3170 gboolean ret
= FALSE
;
3171 GFileInputStream
*file_in
= NULL
;
3172 GInputStream
*in
= NULL
;
3173 GOutputStream
*out
= NULL
;
3174 GFileInfo
*info
= NULL
;
3176 char *attrs_to_read
;
3177 gboolean do_set_attributes
= FALSE
;
3179 /* need to know the file type */
3180 info
= g_file_query_info (source
,
3181 G_FILE_ATTRIBUTE_STANDARD_TYPE
"," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
,
3182 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3188 /* Maybe copy the symlink? */
3189 if ((flags
& G_FILE_COPY_NOFOLLOW_SYMLINKS
) &&
3190 g_file_info_get_file_type (info
) == G_FILE_TYPE_SYMBOLIC_LINK
)
3192 target
= g_file_info_get_symlink_target (info
);
3195 if (!copy_symlink (destination
, flags
, cancellable
, target
, error
))
3201 /* ... else fall back on a regular file copy */
3203 /* Handle "special" files (pipes, device nodes, ...)? */
3204 else if (g_file_info_get_file_type (info
) == G_FILE_TYPE_SPECIAL
)
3206 /* FIXME: could try to recreate device nodes and others? */
3207 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
3208 _("Can’t copy special file"));
3212 /* Everything else should just fall back on a regular copy. */
3214 file_in
= open_source_for_copy (source
, destination
, flags
, cancellable
, error
);
3217 in
= G_INPUT_STREAM (file_in
);
3219 if (!build_attribute_list_for_copy (destination
, flags
, &attrs_to_read
,
3220 cancellable
, error
))
3223 if (attrs_to_read
!= NULL
)
3225 GError
*tmp_error
= NULL
;
3227 /* Ok, ditch the previous lightweight info (on Unix we just
3228 * called lstat()); at this point we gather all the information
3229 * we need about the source from the opened file descriptor.
3231 g_object_unref (info
);
3233 info
= g_file_input_stream_query_info (file_in
, attrs_to_read
,
3234 cancellable
, &tmp_error
);
3237 /* Not all gvfs backends implement query_info_on_read(), we
3238 * can just fall back to the pathname again.
3239 * https://bugzilla.gnome.org/706254
3241 if (g_error_matches (tmp_error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3243 g_clear_error (&tmp_error
);
3244 info
= g_file_query_info (source
, attrs_to_read
, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3245 cancellable
, error
);
3249 g_free (attrs_to_read
);
3250 g_propagate_error (error
, tmp_error
);
3254 g_free (attrs_to_read
);
3258 do_set_attributes
= TRUE
;
3261 /* In the local file path, we pass down the source info which
3262 * includes things like unix::mode, to ensure that the target file
3263 * is not created with different permissions from the source file.
3265 * If a future API like g_file_replace_with_info() is added, switch
3266 * this code to use that.
3268 if (G_IS_LOCAL_FILE (destination
))
3270 if (flags
& G_FILE_COPY_OVERWRITE
)
3271 out
= (GOutputStream
*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination
)),
3273 flags
& G_FILE_COPY_BACKUP
,
3274 G_FILE_CREATE_REPLACE_DESTINATION
,
3276 cancellable
, error
);
3278 out
= (GOutputStream
*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination
)),
3280 cancellable
, error
);
3282 else if (flags
& G_FILE_COPY_OVERWRITE
)
3284 out
= (GOutputStream
*)g_file_replace (destination
,
3286 flags
& G_FILE_COPY_BACKUP
,
3287 G_FILE_CREATE_REPLACE_DESTINATION
,
3288 cancellable
, error
);
3292 out
= (GOutputStream
*)g_file_create (destination
, 0, cancellable
, error
);
3299 if (G_IS_FILE_DESCRIPTOR_BASED (in
) && G_IS_FILE_DESCRIPTOR_BASED (out
))
3301 GError
*reflink_err
= NULL
;
3303 if (!btrfs_reflink_with_progress (in
, out
, info
, cancellable
,
3304 progress_callback
, progress_callback_data
,
3307 if (g_error_matches (reflink_err
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3309 g_clear_error (&reflink_err
);
3313 g_propagate_error (error
, reflink_err
);
3326 if (G_IS_FILE_DESCRIPTOR_BASED (in
) && G_IS_FILE_DESCRIPTOR_BASED (out
))
3328 GError
*splice_err
= NULL
;
3330 if (!splice_stream_with_progress (in
, out
, cancellable
,
3331 progress_callback
, progress_callback_data
,
3334 if (g_error_matches (splice_err
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3336 g_clear_error (&splice_err
);
3340 g_propagate_error (error
, splice_err
);
3353 /* A plain read/write loop */
3354 if (!copy_stream_with_progress (in
, out
, source
, cancellable
,
3355 progress_callback
, progress_callback_data
,
3363 /* Don't care about errors in source here */
3364 (void) g_input_stream_close (in
, cancellable
, NULL
);
3365 g_object_unref (in
);
3370 /* But write errors on close are bad! */
3371 if (!g_output_stream_close (out
, cancellable
, ret
? error
: NULL
))
3373 g_object_unref (out
);
3376 /* Ignore errors here. Failure to copy metadata is not a hard error */
3377 /* TODO: set these attributes /before/ we do the rename() on Unix */
3378 if (ret
&& do_set_attributes
)
3380 g_file_set_attributes_from_info (destination
,
3382 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3387 g_clear_object (&info
);
3394 * @source: input #GFile
3395 * @destination: destination #GFile
3396 * @flags: set of #GFileCopyFlags
3397 * @cancellable: (nullable): optional #GCancellable object,
3399 * @progress_callback: (nullable) (scope call): function to callback with
3400 * progress information, or %NULL if progress information is not needed
3401 * @progress_callback_data: (closure): user data to pass to @progress_callback
3402 * @error: #GError to set on error, or %NULL
3404 * Copies the file @source to the location specified by @destination.
3405 * Can not handle recursive copies of directories.
3407 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3408 * existing @destination file is overwritten.
3410 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3411 * will be copied as symlinks, otherwise the target of the
3412 * @source symlink will be copied.
3414 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3415 * that is possible to copy is copied, not just the default subset (which,
3416 * for instance, does not include the owner, see #GFileInfo).
3418 * If @cancellable is not %NULL, then the operation can be cancelled by
3419 * triggering the cancellable object from another thread. If the operation
3420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3422 * If @progress_callback is not %NULL, then the operation can be monitored
3423 * by setting this to a #GFileProgressCallback function.
3424 * @progress_callback_data will be passed to this function. It is guaranteed
3425 * that this callback will be called after all data has been transferred with
3426 * the total number of bytes copied during the operation.
3428 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3429 * is returned, independent on the status of the @destination.
3431 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3432 * the error %G_IO_ERROR_EXISTS is returned.
3434 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3435 * error is returned. If trying to overwrite a directory with a directory the
3436 * %G_IO_ERROR_WOULD_MERGE error is returned.
3438 * If the source is a directory and the target does not exist, or
3439 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3440 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3442 * If you are interested in copying the #GFile object itself (not the on-disk
3443 * file), see g_file_dup().
3445 * Returns: %TRUE on success, %FALSE otherwise.
3448 g_file_copy (GFile
*source
,
3450 GFileCopyFlags flags
,
3451 GCancellable
*cancellable
,
3452 GFileProgressCallback progress_callback
,
3453 gpointer progress_callback_data
,
3460 g_return_val_if_fail (G_IS_FILE (source
), FALSE
);
3461 g_return_val_if_fail (G_IS_FILE (destination
), FALSE
);
3463 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3466 iface
= G_FILE_GET_IFACE (destination
);
3470 res
= (* iface
->copy
) (source
, destination
,
3472 progress_callback
, progress_callback_data
,
3478 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3480 g_propagate_error (error
, my_error
);
3484 g_clear_error (&my_error
);
3487 /* If the types are different, and the destination method failed
3488 * also try the source method
3490 if (G_OBJECT_TYPE (source
) != G_OBJECT_TYPE (destination
))
3492 iface
= G_FILE_GET_IFACE (source
);
3497 res
= (* iface
->copy
) (source
, destination
,
3499 progress_callback
, progress_callback_data
,
3505 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3507 g_propagate_error (error
, my_error
);
3511 g_clear_error (&my_error
);
3515 return file_copy_fallback (source
, destination
, flags
, cancellable
,
3516 progress_callback
, progress_callback_data
,
3521 * g_file_copy_async:
3522 * @source: input #GFile
3523 * @destination: destination #GFile
3524 * @flags: set of #GFileCopyFlags
3525 * @io_priority: the [I/O priority][io-priority] of the request
3526 * @cancellable: (nullable): optional #GCancellable object,
3528 * @progress_callback: (nullable) (scope notified): function to callback with progress
3529 * information, or %NULL if progress information is not needed
3530 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3531 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3532 * @user_data: (closure callback): the data to pass to callback function
3534 * Copies the file @source to the location specified by @destination
3535 * asynchronously. For details of the behaviour, see g_file_copy().
3537 * If @progress_callback is not %NULL, then that function that will be called
3538 * just like in g_file_copy(). The callback will run in the default main context
3539 * of the thread calling g_file_copy_async() — the same context as @callback is
3542 * When the operation is finished, @callback will be called. You can then call
3543 * g_file_copy_finish() to get the result of the operation.
3546 g_file_copy_async (GFile
*source
,
3548 GFileCopyFlags flags
,
3550 GCancellable
*cancellable
,
3551 GFileProgressCallback progress_callback
,
3552 gpointer progress_callback_data
,
3553 GAsyncReadyCallback callback
,
3558 g_return_if_fail (G_IS_FILE (source
));
3559 g_return_if_fail (G_IS_FILE (destination
));
3561 iface
= G_FILE_GET_IFACE (source
);
3562 (* iface
->copy_async
) (source
,
3568 progress_callback_data
,
3574 * g_file_copy_finish:
3575 * @file: input #GFile
3576 * @res: a #GAsyncResult
3577 * @error: a #GError, or %NULL
3579 * Finishes copying the file started with g_file_copy_async().
3581 * Returns: a %TRUE on success, %FALSE on error.
3584 g_file_copy_finish (GFile
*file
,
3590 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3591 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), FALSE
);
3593 if (g_async_result_legacy_propagate_error (res
, error
))
3596 iface
= G_FILE_GET_IFACE (file
);
3597 return (* iface
->copy_finish
) (file
, res
, error
);
3602 * @source: #GFile pointing to the source location
3603 * @destination: #GFile pointing to the destination location
3604 * @flags: set of #GFileCopyFlags
3605 * @cancellable: (nullable): optional #GCancellable object,
3607 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3608 * function for updates
3609 * @progress_callback_data: (closure): gpointer to user data for
3610 * the callback function
3611 * @error: #GError for returning error conditions, or %NULL
3613 * Tries to move the file or directory @source to the location specified
3614 * by @destination. If native move operations are supported then this is
3615 * used, otherwise a copy + delete fallback is used. The native
3616 * implementation may support moving directories (for instance on moves
3617 * inside the same filesystem), but the fallback code does not.
3619 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3620 * existing @destination file is overwritten.
3622 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3623 * will be copied as symlinks, otherwise the target of the
3624 * @source symlink will be copied.
3626 * If @cancellable is not %NULL, then the operation can be cancelled by
3627 * triggering the cancellable object from another thread. If the operation
3628 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3630 * If @progress_callback is not %NULL, then the operation can be monitored
3631 * by setting this to a #GFileProgressCallback function.
3632 * @progress_callback_data will be passed to this function. It is
3633 * guaranteed that this callback will be called after all data has been
3634 * transferred with the total number of bytes copied during the operation.
3636 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3637 * error is returned, independent on the status of the @destination.
3639 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3640 * then the error %G_IO_ERROR_EXISTS is returned.
3642 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3643 * error is returned. If trying to overwrite a directory with a directory the
3644 * %G_IO_ERROR_WOULD_MERGE error is returned.
3646 * If the source is a directory and the target does not exist, or
3647 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3648 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3649 * move operation isn't available).
3651 * Returns: %TRUE on successful move, %FALSE otherwise.
3654 g_file_move (GFile
*source
,
3656 GFileCopyFlags flags
,
3657 GCancellable
*cancellable
,
3658 GFileProgressCallback progress_callback
,
3659 gpointer progress_callback_data
,
3666 g_return_val_if_fail (G_IS_FILE (source
), FALSE
);
3667 g_return_val_if_fail (G_IS_FILE (destination
), FALSE
);
3669 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3672 iface
= G_FILE_GET_IFACE (destination
);
3676 res
= (* iface
->move
) (source
, destination
,
3678 progress_callback
, progress_callback_data
,
3684 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3686 g_propagate_error (error
, my_error
);
3690 g_clear_error (&my_error
);
3693 /* If the types are different, and the destination method failed
3694 * also try the source method
3696 if (G_OBJECT_TYPE (source
) != G_OBJECT_TYPE (destination
))
3698 iface
= G_FILE_GET_IFACE (source
);
3703 res
= (* iface
->move
) (source
, destination
,
3705 progress_callback
, progress_callback_data
,
3711 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3713 g_propagate_error (error
, my_error
);
3717 g_clear_error (&my_error
);
3721 if (flags
& G_FILE_COPY_NO_FALLBACK_FOR_MOVE
)
3723 g_set_error_literal (error
, G_IO_ERROR
,
3724 G_IO_ERROR_NOT_SUPPORTED
,
3725 _("Operation not supported"));
3729 flags
|= G_FILE_COPY_ALL_METADATA
;
3730 if (!g_file_copy (source
, destination
, flags
, cancellable
,
3731 progress_callback
, progress_callback_data
,
3735 return g_file_delete (source
, cancellable
, error
);
3739 * g_file_make_directory:
3740 * @file: input #GFile
3741 * @cancellable: (nullable): optional #GCancellable object,
3743 * @error: a #GError, or %NULL
3745 * Creates a directory. Note that this will only create a child directory
3746 * of the immediate parent directory of the path or URI given by the #GFile.
3747 * To recursively create directories, see g_file_make_directory_with_parents().
3748 * This function will fail if the parent directory does not exist, setting
3749 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3750 * creating directories, this function will fail, setting @error to
3751 * %G_IO_ERROR_NOT_SUPPORTED.
3753 * For a local #GFile the newly created directory will have the default
3754 * (current) ownership and permissions of the current process.
3756 * If @cancellable is not %NULL, then the operation can be cancelled by
3757 * triggering the cancellable object from another thread. If the operation
3758 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3760 * Returns: %TRUE on successful creation, %FALSE otherwise.
3763 g_file_make_directory (GFile
*file
,
3764 GCancellable
*cancellable
,
3769 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3771 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3774 iface
= G_FILE_GET_IFACE (file
);
3776 if (iface
->make_directory
== NULL
)
3778 g_set_error_literal (error
, G_IO_ERROR
,
3779 G_IO_ERROR_NOT_SUPPORTED
,
3780 _("Operation not supported"));
3784 return (* iface
->make_directory
) (file
, cancellable
, error
);
3788 * g_file_make_directory_async:
3789 * @file: input #GFile
3790 * @io_priority: the [I/O priority][io-priority] of the request
3791 * @cancellable: (nullable): optional #GCancellable object,
3793 * @callback: a #GAsyncReadyCallback to call
3794 * when the request is satisfied
3795 * @user_data: the data to pass to callback function
3797 * Asynchronously creates a directory.
3799 * Virtual: make_directory_async
3803 g_file_make_directory_async (GFile
*file
,
3805 GCancellable
*cancellable
,
3806 GAsyncReadyCallback callback
,
3811 g_return_if_fail (G_IS_FILE (file
));
3813 iface
= G_FILE_GET_IFACE (file
);
3814 (* iface
->make_directory_async
) (file
,
3822 * g_file_make_directory_finish:
3823 * @file: input #GFile
3824 * @result: a #GAsyncResult
3825 * @error: a #GError, or %NULL
3827 * Finishes an asynchronous directory creation, started with
3828 * g_file_make_directory_async().
3830 * Virtual: make_directory_finish
3831 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3835 g_file_make_directory_finish (GFile
*file
,
3836 GAsyncResult
*result
,
3841 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3842 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
3844 iface
= G_FILE_GET_IFACE (file
);
3845 return (* iface
->make_directory_finish
) (file
, result
, error
);
3849 * g_file_make_directory_with_parents:
3850 * @file: input #GFile
3851 * @cancellable: (nullable): optional #GCancellable object,
3853 * @error: a #GError, or %NULL
3855 * Creates a directory and any parent directories that may not
3856 * exist similar to 'mkdir -p'. If the file system does not support
3857 * creating directories, this function will fail, setting @error to
3858 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3859 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3860 * the similar g_mkdir_with_parents().
3862 * For a local #GFile the newly created directories will have the default
3863 * (current) ownership and permissions of the current process.
3865 * If @cancellable is not %NULL, then the operation can be cancelled by
3866 * triggering the cancellable object from another thread. If the operation
3867 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3869 * Returns: %TRUE if all directories have been successfully created, %FALSE
3875 g_file_make_directory_with_parents (GFile
*file
,
3876 GCancellable
*cancellable
,
3879 GFile
*work_file
= NULL
;
3880 GList
*list
= NULL
, *l
;
3881 GError
*my_error
= NULL
;
3883 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3885 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3888 /* Try for the simple case of not having to create any parent
3889 * directories. If any parent directory needs to be created, this
3890 * call will fail with NOT_FOUND. If that happens, then that value of
3891 * my_error persists into the while loop below.
3893 g_file_make_directory (file
, cancellable
, &my_error
);
3894 if (!g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3897 g_propagate_error (error
, my_error
);
3898 return my_error
== NULL
;
3901 work_file
= g_object_ref (file
);
3903 /* Creates the parent directories as needed. In case any particular
3904 * creation operation fails for lack of other parent directories
3905 * (NOT_FOUND), the directory is added to a list of directories to
3906 * create later, and the value of my_error is retained until the next
3907 * iteration of the loop. After the loop my_error should either be
3908 * empty or contain a real failure condition.
3910 while (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3914 parent_file
= g_file_get_parent (work_file
);
3915 if (parent_file
== NULL
)
3918 g_clear_error (&my_error
);
3919 g_file_make_directory (parent_file
, cancellable
, &my_error
);
3920 /* Another process may have created the directory in between the
3921 * G_IO_ERROR_NOT_FOUND and now
3923 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
))
3924 g_clear_error (&my_error
);
3926 g_object_unref (work_file
);
3927 work_file
= g_object_ref (parent_file
);
3929 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3930 list
= g_list_prepend (list
, parent_file
); /* Transfer ownership of ref */
3932 g_object_unref (parent_file
);
3935 /* All directories should be able to be created now, so an error at
3936 * this point means the whole operation must fail -- except an EXISTS
3937 * error, which means that another process already created the
3938 * directory in between the previous failure and now.
3940 for (l
= list
; my_error
== NULL
&& l
; l
= l
->next
)
3942 g_file_make_directory ((GFile
*) l
->data
, cancellable
, &my_error
);
3943 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
))
3944 g_clear_error (&my_error
);
3948 g_object_unref (work_file
);
3951 while (list
!= NULL
)
3953 g_object_unref ((GFile
*) list
->data
);
3954 list
= g_list_remove (list
, list
->data
);
3957 /* At this point an error in my_error means a that something
3958 * unexpected failed in either of the loops above, so the whole
3959 * operation must fail.
3961 if (my_error
!= NULL
)
3963 g_propagate_error (error
, my_error
);
3967 return g_file_make_directory (file
, cancellable
, error
);
3971 * g_file_make_symbolic_link:
3972 * @file: a #GFile with the name of the symlink to create
3973 * @symlink_value: (type filename): a string with the path for the target
3974 * of the new symlink
3975 * @cancellable: (nullable): optional #GCancellable object,
3979 * Creates a symbolic link named @file which contains the string
3982 * If @cancellable is not %NULL, then the operation can be cancelled by
3983 * triggering the cancellable object from another thread. If the operation
3984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3986 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3989 g_file_make_symbolic_link (GFile
*file
,
3990 const char *symlink_value
,
3991 GCancellable
*cancellable
,
3996 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3997 g_return_val_if_fail (symlink_value
!= NULL
, FALSE
);
3999 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4002 if (*symlink_value
== '\0')
4004 g_set_error_literal (error
, G_IO_ERROR
,
4005 G_IO_ERROR_INVALID_ARGUMENT
,
4006 _("Invalid symlink value given"));
4010 iface
= G_FILE_GET_IFACE (file
);
4012 if (iface
->make_symbolic_link
== NULL
)
4014 g_set_error_literal (error
, G_IO_ERROR
,
4015 G_IO_ERROR_NOT_SUPPORTED
,
4016 _("Operation not supported"));
4020 return (* iface
->make_symbolic_link
) (file
, symlink_value
, cancellable
, error
);
4025 * @file: input #GFile
4026 * @cancellable: (nullable): optional #GCancellable object,
4028 * @error: a #GError, or %NULL
4030 * Deletes a file. If the @file is a directory, it will only be
4031 * deleted if it is empty. This has the same semantics as g_unlink().
4033 * If @cancellable is not %NULL, then the operation can be cancelled by
4034 * triggering the cancellable object from another thread. If the operation
4035 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4037 * Virtual: delete_file
4038 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4041 g_file_delete (GFile
*file
,
4042 GCancellable
*cancellable
,
4047 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4049 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4052 iface
= G_FILE_GET_IFACE (file
);
4054 if (iface
->delete_file
== NULL
)
4056 g_set_error_literal (error
, G_IO_ERROR
,
4057 G_IO_ERROR_NOT_SUPPORTED
,
4058 _("Operation not supported"));
4062 return (* iface
->delete_file
) (file
, cancellable
, error
);
4066 * g_file_delete_async:
4067 * @file: input #GFile
4068 * @io_priority: the [I/O priority][io-priority] of the request
4069 * @cancellable: (nullable): optional #GCancellable object,
4071 * @callback: a #GAsyncReadyCallback to call
4072 * when the request is satisfied
4073 * @user_data: the data to pass to callback function
4075 * Asynchronously delete a file. If the @file is a directory, it will
4076 * only be deleted if it is empty. This has the same semantics as
4079 * Virtual: delete_file_async
4083 g_file_delete_async (GFile
*file
,
4085 GCancellable
*cancellable
,
4086 GAsyncReadyCallback callback
,
4091 g_return_if_fail (G_IS_FILE (file
));
4093 iface
= G_FILE_GET_IFACE (file
);
4094 (* iface
->delete_file_async
) (file
,
4102 * g_file_delete_finish:
4103 * @file: input #GFile
4104 * @result: a #GAsyncResult
4105 * @error: a #GError, or %NULL
4107 * Finishes deleting a file started with g_file_delete_async().
4109 * Virtual: delete_file_finish
4110 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4114 g_file_delete_finish (GFile
*file
,
4115 GAsyncResult
*result
,
4120 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4121 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4123 if (g_async_result_legacy_propagate_error (result
, error
))
4126 iface
= G_FILE_GET_IFACE (file
);
4127 return (* iface
->delete_file_finish
) (file
, result
, error
);
4132 * @file: #GFile to send to trash
4133 * @cancellable: (nullable): optional #GCancellable object,
4135 * @error: a #GError, or %NULL
4137 * Sends @file to the "Trashcan", if possible. This is similar to
4138 * deleting it, but the user can recover it before emptying the trashcan.
4139 * Not all file systems support trashing, so this call can return the
4140 * %G_IO_ERROR_NOT_SUPPORTED error.
4142 * If @cancellable is not %NULL, then the operation can be cancelled by
4143 * triggering the cancellable object from another thread. If the operation
4144 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4147 * Returns: %TRUE on successful trash, %FALSE otherwise.
4150 g_file_trash (GFile
*file
,
4151 GCancellable
*cancellable
,
4156 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4158 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4161 iface
= G_FILE_GET_IFACE (file
);
4163 if (iface
->trash
== NULL
)
4165 g_set_error_literal (error
,
4166 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
4167 _("Trash not supported"));
4171 return (* iface
->trash
) (file
, cancellable
, error
);
4175 * g_file_trash_async:
4176 * @file: input #GFile
4177 * @io_priority: the [I/O priority][io-priority] of the request
4178 * @cancellable: (nullable): optional #GCancellable object,
4180 * @callback: a #GAsyncReadyCallback to call
4181 * when the request is satisfied
4182 * @user_data: the data to pass to callback function
4184 * Asynchronously sends @file to the Trash location, if possible.
4186 * Virtual: trash_async
4190 g_file_trash_async (GFile
*file
,
4192 GCancellable
*cancellable
,
4193 GAsyncReadyCallback callback
,
4198 g_return_if_fail (G_IS_FILE (file
));
4200 iface
= G_FILE_GET_IFACE (file
);
4201 (* iface
->trash_async
) (file
,
4209 * g_file_trash_finish:
4210 * @file: input #GFile
4211 * @result: a #GAsyncResult
4212 * @error: a #GError, or %NULL
4214 * Finishes an asynchronous file trashing operation, started with
4215 * g_file_trash_async().
4217 * Virtual: trash_finish
4218 * Returns: %TRUE on successful trash, %FALSE otherwise.
4222 g_file_trash_finish (GFile
*file
,
4223 GAsyncResult
*result
,
4228 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4229 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4231 iface
= G_FILE_GET_IFACE (file
);
4232 return (* iface
->trash_finish
) (file
, result
, error
);
4236 * g_file_set_display_name:
4237 * @file: input #GFile
4238 * @display_name: a string
4239 * @cancellable: (nullable): optional #GCancellable object,
4241 * @error: a #GError, or %NULL
4243 * Renames @file to the specified display name.
4245 * The display name is converted from UTF-8 to the correct encoding
4246 * for the target filesystem if possible and the @file is renamed to this.
4248 * If you want to implement a rename operation in the user interface the
4249 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4250 * initial value in the rename widget, and then the result after editing
4251 * should be passed to g_file_set_display_name().
4253 * On success the resulting converted filename is returned.
4255 * If @cancellable is not %NULL, then the operation can be cancelled by
4256 * triggering the cancellable object from another thread. If the operation
4257 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4259 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4260 * or %NULL if there was an error.
4261 * Free the returned object with g_object_unref().
4264 g_file_set_display_name (GFile
*file
,
4265 const gchar
*display_name
,
4266 GCancellable
*cancellable
,
4271 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4272 g_return_val_if_fail (display_name
!= NULL
, NULL
);
4274 if (strchr (display_name
, G_DIR_SEPARATOR
) != NULL
)
4278 G_IO_ERROR_INVALID_ARGUMENT
,
4279 _("File names cannot contain “%c”"), G_DIR_SEPARATOR
);
4283 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4286 iface
= G_FILE_GET_IFACE (file
);
4288 return (* iface
->set_display_name
) (file
, display_name
, cancellable
, error
);
4292 * g_file_set_display_name_async:
4293 * @file: input #GFile
4294 * @display_name: a string
4295 * @io_priority: the [I/O priority][io-priority] of the request
4296 * @cancellable: (nullable): optional #GCancellable object,
4298 * @callback: (scope async): a #GAsyncReadyCallback to call
4299 * when the request is satisfied
4300 * @user_data: (closure): the data to pass to callback function
4302 * Asynchronously sets the display name for a given #GFile.
4304 * For more details, see g_file_set_display_name() which is
4305 * the synchronous version of this call.
4307 * When the operation is finished, @callback will be called.
4308 * You can then call g_file_set_display_name_finish() to get
4309 * the result of the operation.
4312 g_file_set_display_name_async (GFile
*file
,
4313 const gchar
*display_name
,
4315 GCancellable
*cancellable
,
4316 GAsyncReadyCallback callback
,
4321 g_return_if_fail (G_IS_FILE (file
));
4322 g_return_if_fail (display_name
!= NULL
);
4324 iface
= G_FILE_GET_IFACE (file
);
4325 (* iface
->set_display_name_async
) (file
,
4334 * g_file_set_display_name_finish:
4335 * @file: input #GFile
4336 * @res: a #GAsyncResult
4337 * @error: a #GError, or %NULL
4339 * Finishes setting a display name started with
4340 * g_file_set_display_name_async().
4342 * Returns: (transfer full): a #GFile or %NULL on error.
4343 * Free the returned object with g_object_unref().
4346 g_file_set_display_name_finish (GFile
*file
,
4352 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4353 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
4355 if (g_async_result_legacy_propagate_error (res
, error
))
4358 iface
= G_FILE_GET_IFACE (file
);
4359 return (* iface
->set_display_name_finish
) (file
, res
, error
);
4363 * g_file_query_settable_attributes:
4364 * @file: input #GFile
4365 * @cancellable: (nullable): optional #GCancellable object,
4367 * @error: a #GError, or %NULL
4369 * Obtain the list of settable attributes for the file.
4371 * Returns the type and full attribute name of all the attributes
4372 * that can be set on this file. This doesn't mean setting it will
4373 * always succeed though, you might get an access failure, or some
4374 * specific file may not support a specific attribute.
4376 * If @cancellable is not %NULL, then the operation can be cancelled by
4377 * triggering the cancellable object from another thread. If the operation
4378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4380 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4381 * When you are done with it, release it with
4382 * g_file_attribute_info_list_unref()
4384 GFileAttributeInfoList
*
4385 g_file_query_settable_attributes (GFile
*file
,
4386 GCancellable
*cancellable
,
4391 GFileAttributeInfoList
*list
;
4393 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4395 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4398 iface
= G_FILE_GET_IFACE (file
);
4400 if (iface
->query_settable_attributes
== NULL
)
4401 return g_file_attribute_info_list_new ();
4404 list
= (* iface
->query_settable_attributes
) (file
, cancellable
, &my_error
);
4408 if (my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_NOT_SUPPORTED
)
4410 list
= g_file_attribute_info_list_new ();
4411 g_error_free (my_error
);
4414 g_propagate_error (error
, my_error
);
4421 * g_file_query_writable_namespaces:
4422 * @file: input #GFile
4423 * @cancellable: (nullable): optional #GCancellable object,
4425 * @error: a #GError, or %NULL
4427 * Obtain the list of attribute namespaces where new attributes
4428 * can be created by a user. An example of this is extended
4429 * attributes (in the "xattr" namespace).
4431 * If @cancellable is not %NULL, then the operation can be cancelled by
4432 * triggering the cancellable object from another thread. If the operation
4433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4435 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4436 * When you are done with it, release it with
4437 * g_file_attribute_info_list_unref()
4439 GFileAttributeInfoList
*
4440 g_file_query_writable_namespaces (GFile
*file
,
4441 GCancellable
*cancellable
,
4446 GFileAttributeInfoList
*list
;
4448 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4450 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4453 iface
= G_FILE_GET_IFACE (file
);
4455 if (iface
->query_writable_namespaces
== NULL
)
4456 return g_file_attribute_info_list_new ();
4459 list
= (* iface
->query_writable_namespaces
) (file
, cancellable
, &my_error
);
4463 g_warn_if_reached();
4464 list
= g_file_attribute_info_list_new ();
4467 if (my_error
!= NULL
)
4469 if (my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_NOT_SUPPORTED
)
4471 g_error_free (my_error
);
4474 g_propagate_error (error
, my_error
);
4481 * g_file_set_attribute:
4482 * @file: input #GFile
4483 * @attribute: a string containing the attribute's name
4484 * @type: The type of the attribute
4485 * @value_p: (nullable): a pointer to the value (or the pointer
4486 * itself if the type is a pointer type)
4487 * @flags: a set of #GFileQueryInfoFlags
4488 * @cancellable: (nullable): optional #GCancellable object,
4490 * @error: a #GError, or %NULL
4492 * Sets an attribute in the file with attribute name @attribute to @value.
4494 * Some attributes can be unset by setting @type to
4495 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4497 * If @cancellable is not %NULL, then the operation can be cancelled by
4498 * triggering the cancellable object from another thread. If the operation
4499 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4501 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4504 g_file_set_attribute (GFile
*file
,
4505 const gchar
*attribute
,
4506 GFileAttributeType type
,
4508 GFileQueryInfoFlags flags
,
4509 GCancellable
*cancellable
,
4514 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4515 g_return_val_if_fail (attribute
!= NULL
&& *attribute
!= '\0', FALSE
);
4517 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4520 iface
= G_FILE_GET_IFACE (file
);
4522 if (iface
->set_attribute
== NULL
)
4524 g_set_error_literal (error
, G_IO_ERROR
,
4525 G_IO_ERROR_NOT_SUPPORTED
,
4526 _("Operation not supported"));
4530 return (* iface
->set_attribute
) (file
, attribute
, type
, value_p
, flags
, cancellable
, error
);
4534 * g_file_set_attributes_from_info:
4535 * @file: input #GFile
4536 * @info: a #GFileInfo
4537 * @flags: #GFileQueryInfoFlags
4538 * @cancellable: (nullable): optional #GCancellable object,
4540 * @error: a #GError, or %NULL
4542 * Tries to set all attributes in the #GFileInfo on the target
4543 * values, not stopping on the first error.
4545 * If there is any error during this operation then @error will
4546 * be set to the first error. Error on particular fields are flagged
4547 * by setting the "status" field in the attribute value to
4548 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4549 * also detect further errors.
4551 * If @cancellable is not %NULL, then the operation can be cancelled by
4552 * triggering the cancellable object from another thread. If the operation
4553 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4555 * Returns: %FALSE if there was any error, %TRUE otherwise.
4558 g_file_set_attributes_from_info (GFile
*file
,
4560 GFileQueryInfoFlags flags
,
4561 GCancellable
*cancellable
,
4566 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4567 g_return_val_if_fail (G_IS_FILE_INFO (info
), FALSE
);
4569 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4572 g_file_info_clear_status (info
);
4574 iface
= G_FILE_GET_IFACE (file
);
4576 return (* iface
->set_attributes_from_info
) (file
,
4584 g_file_real_set_attributes_from_info (GFile
*file
,
4586 GFileQueryInfoFlags flags
,
4587 GCancellable
*cancellable
,
4593 GFileAttributeValue
*value
;
4597 attributes
= g_file_info_list_attributes (info
, NULL
);
4599 for (i
= 0; attributes
[i
] != NULL
; i
++)
4601 value
= _g_file_info_get_attribute_value (info
, attributes
[i
]);
4603 if (value
->status
!= G_FILE_ATTRIBUTE_STATUS_UNSET
)
4606 if (!g_file_set_attribute (file
, attributes
[i
],
4607 value
->type
, _g_file_attribute_value_peek_as_pointer (value
),
4608 flags
, cancellable
, error
))
4610 value
->status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
4612 /* Don't set error multiple times */
4616 value
->status
= G_FILE_ATTRIBUTE_STATUS_SET
;
4619 g_strfreev (attributes
);
4625 * g_file_set_attributes_async:
4626 * @file: input #GFile
4627 * @info: a #GFileInfo
4628 * @flags: a #GFileQueryInfoFlags
4629 * @io_priority: the [I/O priority][io-priority] of the request
4630 * @cancellable: (nullable): optional #GCancellable object,
4632 * @callback: (scope async): a #GAsyncReadyCallback
4633 * @user_data: (closure): a #gpointer
4635 * Asynchronously sets the attributes of @file with @info.
4637 * For more details, see g_file_set_attributes_from_info(),
4638 * which is the synchronous version of this call.
4640 * When the operation is finished, @callback will be called.
4641 * You can then call g_file_set_attributes_finish() to get
4642 * the result of the operation.
4645 g_file_set_attributes_async (GFile
*file
,
4647 GFileQueryInfoFlags flags
,
4649 GCancellable
*cancellable
,
4650 GAsyncReadyCallback callback
,
4655 g_return_if_fail (G_IS_FILE (file
));
4656 g_return_if_fail (G_IS_FILE_INFO (info
));
4658 iface
= G_FILE_GET_IFACE (file
);
4659 (* iface
->set_attributes_async
) (file
,
4669 * g_file_set_attributes_finish:
4670 * @file: input #GFile
4671 * @result: a #GAsyncResult
4672 * @info: (out) (transfer full): a #GFileInfo
4673 * @error: a #GError, or %NULL
4675 * Finishes setting an attribute started in g_file_set_attributes_async().
4677 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4680 g_file_set_attributes_finish (GFile
*file
,
4681 GAsyncResult
*result
,
4687 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4688 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4690 /* No standard handling of errors here, as we must set info even
4693 iface
= G_FILE_GET_IFACE (file
);
4694 return (* iface
->set_attributes_finish
) (file
, result
, info
, error
);
4698 * g_file_set_attribute_string:
4699 * @file: input #GFile
4700 * @attribute: a string containing the attribute's name
4701 * @value: a string containing the attribute's value
4702 * @flags: #GFileQueryInfoFlags
4703 * @cancellable: (nullable): optional #GCancellable object,
4705 * @error: a #GError, or %NULL
4707 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4708 * If @attribute is of a different type, this operation will fail.
4710 * If @cancellable is not %NULL, then the operation can be cancelled by
4711 * triggering the cancellable object from another thread. If the operation
4712 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4714 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4717 g_file_set_attribute_string (GFile
*file
,
4718 const char *attribute
,
4720 GFileQueryInfoFlags flags
,
4721 GCancellable
*cancellable
,
4724 return g_file_set_attribute (file
, attribute
,
4725 G_FILE_ATTRIBUTE_TYPE_STRING
, (gpointer
)value
,
4726 flags
, cancellable
, error
);
4730 * g_file_set_attribute_byte_string:
4731 * @file: input #GFile
4732 * @attribute: a string containing the attribute's name
4733 * @value: a string containing the attribute's new value
4734 * @flags: a #GFileQueryInfoFlags
4735 * @cancellable: (nullable): optional #GCancellable object,
4737 * @error: a #GError, or %NULL
4739 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4740 * If @attribute is of a different type, this operation will fail,
4743 * If @cancellable is not %NULL, then the operation can be cancelled by
4744 * triggering the cancellable object from another thread. If the operation
4745 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4747 * Returns: %TRUE if the @attribute was successfully set to @value
4748 * in the @file, %FALSE otherwise.
4751 g_file_set_attribute_byte_string (GFile
*file
,
4752 const gchar
*attribute
,
4754 GFileQueryInfoFlags flags
,
4755 GCancellable
*cancellable
,
4758 return g_file_set_attribute (file
, attribute
,
4759 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
, (gpointer
)value
,
4760 flags
, cancellable
, error
);
4764 * g_file_set_attribute_uint32:
4765 * @file: input #GFile
4766 * @attribute: a string containing the attribute's name
4767 * @value: a #guint32 containing the attribute's new value
4768 * @flags: a #GFileQueryInfoFlags
4769 * @cancellable: (nullable): optional #GCancellable object,
4771 * @error: a #GError, or %NULL
4773 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4774 * If @attribute is of a different type, this operation will fail.
4776 * If @cancellable is not %NULL, then the operation can be cancelled by
4777 * triggering the cancellable object from another thread. If the operation
4778 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4780 * Returns: %TRUE if the @attribute was successfully set to @value
4781 * in the @file, %FALSE otherwise.
4784 g_file_set_attribute_uint32 (GFile
*file
,
4785 const gchar
*attribute
,
4787 GFileQueryInfoFlags flags
,
4788 GCancellable
*cancellable
,
4791 return g_file_set_attribute (file
, attribute
,
4792 G_FILE_ATTRIBUTE_TYPE_UINT32
, &value
,
4793 flags
, cancellable
, error
);
4797 * g_file_set_attribute_int32:
4798 * @file: input #GFile
4799 * @attribute: a string containing the attribute's name
4800 * @value: a #gint32 containing the attribute's new value
4801 * @flags: a #GFileQueryInfoFlags
4802 * @cancellable: (nullable): optional #GCancellable object,
4804 * @error: a #GError, or %NULL
4806 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4807 * If @attribute is of a different type, this operation will fail.
4809 * If @cancellable is not %NULL, then the operation can be cancelled by
4810 * triggering the cancellable object from another thread. If the operation
4811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4813 * Returns: %TRUE if the @attribute was successfully set to @value
4814 * in the @file, %FALSE otherwise.
4817 g_file_set_attribute_int32 (GFile
*file
,
4818 const gchar
*attribute
,
4820 GFileQueryInfoFlags flags
,
4821 GCancellable
*cancellable
,
4824 return g_file_set_attribute (file
, attribute
,
4825 G_FILE_ATTRIBUTE_TYPE_INT32
, &value
,
4826 flags
, cancellable
, error
);
4830 * g_file_set_attribute_uint64:
4831 * @file: input #GFile
4832 * @attribute: a string containing the attribute's name
4833 * @value: a #guint64 containing the attribute's new value
4834 * @flags: a #GFileQueryInfoFlags
4835 * @cancellable: (nullable): optional #GCancellable object,
4837 * @error: a #GError, or %NULL
4839 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4840 * If @attribute is of a different type, this operation will fail.
4842 * If @cancellable is not %NULL, then the operation can be cancelled by
4843 * triggering the cancellable object from another thread. If the operation
4844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4846 * Returns: %TRUE if the @attribute was successfully set to @value
4847 * in the @file, %FALSE otherwise.
4850 g_file_set_attribute_uint64 (GFile
*file
,
4851 const gchar
*attribute
,
4853 GFileQueryInfoFlags flags
,
4854 GCancellable
*cancellable
,
4857 return g_file_set_attribute (file
, attribute
,
4858 G_FILE_ATTRIBUTE_TYPE_UINT64
, &value
,
4859 flags
, cancellable
, error
);
4863 * g_file_set_attribute_int64:
4864 * @file: input #GFile
4865 * @attribute: a string containing the attribute's name
4866 * @value: a #guint64 containing the attribute's new value
4867 * @flags: a #GFileQueryInfoFlags
4868 * @cancellable: (nullable): optional #GCancellable object,
4870 * @error: a #GError, or %NULL
4872 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4873 * If @attribute is of a different type, this operation will fail.
4875 * If @cancellable is not %NULL, then the operation can be cancelled by
4876 * triggering the cancellable object from another thread. If the operation
4877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4879 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4882 g_file_set_attribute_int64 (GFile
*file
,
4883 const gchar
*attribute
,
4885 GFileQueryInfoFlags flags
,
4886 GCancellable
*cancellable
,
4889 return g_file_set_attribute (file
, attribute
,
4890 G_FILE_ATTRIBUTE_TYPE_INT64
, &value
,
4891 flags
, cancellable
, error
);
4895 * g_file_mount_mountable:
4896 * @file: input #GFile
4897 * @flags: flags affecting the operation
4898 * @mount_operation: (nullable): a #GMountOperation,
4899 * or %NULL to avoid user interaction
4900 * @cancellable: (nullable): optional #GCancellable object,
4902 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4903 * when the request is satisfied, or %NULL
4904 * @user_data: (closure): the data to pass to callback function
4906 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4907 * Using @mount_operation, you can request callbacks when, for instance,
4908 * passwords are needed during authentication.
4910 * If @cancellable is not %NULL, then the operation can be cancelled by
4911 * triggering the cancellable object from another thread. If the operation
4912 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4914 * When the operation is finished, @callback will be called.
4915 * You can then call g_file_mount_mountable_finish() to get
4916 * the result of the operation.
4919 g_file_mount_mountable (GFile
*file
,
4920 GMountMountFlags flags
,
4921 GMountOperation
*mount_operation
,
4922 GCancellable
*cancellable
,
4923 GAsyncReadyCallback callback
,
4928 g_return_if_fail (G_IS_FILE (file
));
4930 iface
= G_FILE_GET_IFACE (file
);
4932 if (iface
->mount_mountable
== NULL
)
4934 g_task_report_new_error (file
, callback
, user_data
,
4935 g_file_mount_mountable
,
4936 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
4937 _("Operation not supported"));
4941 (* iface
->mount_mountable
) (file
,
4950 * g_file_mount_mountable_finish:
4951 * @file: input #GFile
4952 * @result: a #GAsyncResult
4953 * @error: a #GError, or %NULL
4955 * Finishes a mount operation. See g_file_mount_mountable() for details.
4957 * Finish an asynchronous mount operation that was started
4958 * with g_file_mount_mountable().
4960 * Returns: (transfer full): a #GFile or %NULL on error.
4961 * Free the returned object with g_object_unref().
4964 g_file_mount_mountable_finish (GFile
*file
,
4965 GAsyncResult
*result
,
4970 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4971 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), NULL
);
4973 if (g_async_result_legacy_propagate_error (result
, error
))
4975 else if (g_async_result_is_tagged (result
, g_file_mount_mountable
))
4976 return g_task_propagate_pointer (G_TASK (result
), error
);
4978 iface
= G_FILE_GET_IFACE (file
);
4979 return (* iface
->mount_mountable_finish
) (file
, result
, error
);
4983 * g_file_unmount_mountable:
4984 * @file: input #GFile
4985 * @flags: flags affecting the operation
4986 * @cancellable: (nullable): optional #GCancellable object,
4988 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4989 * when the request is satisfied, or %NULL
4990 * @user_data: (closure): the data to pass to callback function
4992 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4994 * If @cancellable is not %NULL, then the operation can be cancelled by
4995 * triggering the cancellable object from another thread. If the operation
4996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4998 * When the operation is finished, @callback will be called.
4999 * You can then call g_file_unmount_mountable_finish() to get
5000 * the result of the operation.
5002 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5005 g_file_unmount_mountable (GFile
*file
,
5006 GMountUnmountFlags flags
,
5007 GCancellable
*cancellable
,
5008 GAsyncReadyCallback callback
,
5013 g_return_if_fail (G_IS_FILE (file
));
5015 iface
= G_FILE_GET_IFACE (file
);
5017 if (iface
->unmount_mountable
== NULL
)
5019 g_task_report_new_error (file
, callback
, user_data
,
5020 g_file_unmount_mountable_with_operation
,
5021 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5022 _("Operation not supported"));
5026 (* iface
->unmount_mountable
) (file
,
5034 * g_file_unmount_mountable_finish:
5035 * @file: input #GFile
5036 * @result: a #GAsyncResult
5037 * @error: a #GError, or %NULL
5039 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5041 * Finish an asynchronous unmount operation that was started
5042 * with g_file_unmount_mountable().
5044 * Returns: %TRUE if the operation finished successfully.
5047 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5051 g_file_unmount_mountable_finish (GFile
*file
,
5052 GAsyncResult
*result
,
5057 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5058 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5060 if (g_async_result_legacy_propagate_error (result
, error
))
5062 else if (g_async_result_is_tagged (result
, g_file_unmount_mountable_with_operation
))
5063 return g_task_propagate_boolean (G_TASK (result
), error
);
5065 iface
= G_FILE_GET_IFACE (file
);
5066 return (* iface
->unmount_mountable_finish
) (file
, result
, error
);
5070 * g_file_unmount_mountable_with_operation:
5071 * @file: input #GFile
5072 * @flags: flags affecting the operation
5073 * @mount_operation: (nullable): a #GMountOperation,
5074 * or %NULL to avoid user interaction
5075 * @cancellable: (nullable): optional #GCancellable object,
5077 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5078 * when the request is satisfied, or %NULL
5079 * @user_data: (closure): the data to pass to callback function
5081 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5083 * If @cancellable is not %NULL, then the operation can be cancelled by
5084 * triggering the cancellable object from another thread. If the operation
5085 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5087 * When the operation is finished, @callback will be called.
5088 * You can then call g_file_unmount_mountable_finish() to get
5089 * the result of the operation.
5094 g_file_unmount_mountable_with_operation (GFile
*file
,
5095 GMountUnmountFlags flags
,
5096 GMountOperation
*mount_operation
,
5097 GCancellable
*cancellable
,
5098 GAsyncReadyCallback callback
,
5103 g_return_if_fail (G_IS_FILE (file
));
5105 iface
= G_FILE_GET_IFACE (file
);
5107 if (iface
->unmount_mountable
== NULL
&& iface
->unmount_mountable_with_operation
== NULL
)
5109 g_task_report_new_error (file
, callback
, user_data
,
5110 g_file_unmount_mountable_with_operation
,
5111 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5112 _("Operation not supported"));
5116 if (iface
->unmount_mountable_with_operation
!= NULL
)
5117 (* iface
->unmount_mountable_with_operation
) (file
,
5124 (* iface
->unmount_mountable
) (file
,
5132 * g_file_unmount_mountable_with_operation_finish:
5133 * @file: input #GFile
5134 * @result: a #GAsyncResult
5135 * @error: a #GError, or %NULL
5137 * Finishes an unmount operation,
5138 * see g_file_unmount_mountable_with_operation() for details.
5140 * Finish an asynchronous unmount operation that was started
5141 * with g_file_unmount_mountable_with_operation().
5143 * Returns: %TRUE if the operation finished successfully.
5149 g_file_unmount_mountable_with_operation_finish (GFile
*file
,
5150 GAsyncResult
*result
,
5155 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5156 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5158 if (g_async_result_legacy_propagate_error (result
, error
))
5160 else if (g_async_result_is_tagged (result
, g_file_unmount_mountable_with_operation
))
5161 return g_task_propagate_boolean (G_TASK (result
), error
);
5163 iface
= G_FILE_GET_IFACE (file
);
5164 if (iface
->unmount_mountable_with_operation_finish
!= NULL
)
5165 return (* iface
->unmount_mountable_with_operation_finish
) (file
, result
, error
);
5167 return (* iface
->unmount_mountable_finish
) (file
, result
, error
);
5171 * g_file_eject_mountable:
5172 * @file: input #GFile
5173 * @flags: flags affecting the operation
5174 * @cancellable: (nullable): optional #GCancellable object,
5176 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5177 * when the request is satisfied, or %NULL
5178 * @user_data: (closure): the data to pass to callback function
5180 * Starts an asynchronous eject on a mountable.
5181 * When this operation has completed, @callback will be called with
5182 * @user_user data, and the operation can be finalized with
5183 * g_file_eject_mountable_finish().
5185 * If @cancellable is not %NULL, then the operation can be cancelled by
5186 * triggering the cancellable object from another thread. If the operation
5187 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5189 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5192 g_file_eject_mountable (GFile
*file
,
5193 GMountUnmountFlags flags
,
5194 GCancellable
*cancellable
,
5195 GAsyncReadyCallback callback
,
5200 g_return_if_fail (G_IS_FILE (file
));
5202 iface
= G_FILE_GET_IFACE (file
);
5204 if (iface
->eject_mountable
== NULL
)
5206 g_task_report_new_error (file
, callback
, user_data
,
5207 g_file_eject_mountable_with_operation
,
5208 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5209 _("Operation not supported"));
5213 (* iface
->eject_mountable
) (file
,
5221 * g_file_eject_mountable_finish:
5222 * @file: input #GFile
5223 * @result: a #GAsyncResult
5224 * @error: a #GError, or %NULL
5226 * Finishes an asynchronous eject operation started by
5227 * g_file_eject_mountable().
5229 * Returns: %TRUE if the @file was ejected successfully.
5232 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5236 g_file_eject_mountable_finish (GFile
*file
,
5237 GAsyncResult
*result
,
5242 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5243 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5245 if (g_async_result_legacy_propagate_error (result
, error
))
5247 else if (g_async_result_is_tagged (result
, g_file_eject_mountable_with_operation
))
5248 return g_task_propagate_boolean (G_TASK (result
), error
);
5250 iface
= G_FILE_GET_IFACE (file
);
5251 return (* iface
->eject_mountable_finish
) (file
, result
, error
);
5255 * g_file_eject_mountable_with_operation:
5256 * @file: input #GFile
5257 * @flags: flags affecting the operation
5258 * @mount_operation: (nullable): a #GMountOperation,
5259 * or %NULL to avoid user interaction
5260 * @cancellable: (nullable): optional #GCancellable object,
5262 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5263 * when the request is satisfied, or %NULL
5264 * @user_data: (closure): the data to pass to callback function
5266 * Starts an asynchronous eject on a mountable.
5267 * When this operation has completed, @callback will be called with
5268 * @user_user data, and the operation can be finalized with
5269 * g_file_eject_mountable_with_operation_finish().
5271 * If @cancellable is not %NULL, then the operation can be cancelled by
5272 * triggering the cancellable object from another thread. If the operation
5273 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5278 g_file_eject_mountable_with_operation (GFile
*file
,
5279 GMountUnmountFlags flags
,
5280 GMountOperation
*mount_operation
,
5281 GCancellable
*cancellable
,
5282 GAsyncReadyCallback callback
,
5287 g_return_if_fail (G_IS_FILE (file
));
5289 iface
= G_FILE_GET_IFACE (file
);
5291 if (iface
->eject_mountable
== NULL
&& iface
->eject_mountable_with_operation
== NULL
)
5293 g_task_report_new_error (file
, callback
, user_data
,
5294 g_file_eject_mountable_with_operation
,
5295 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5296 _("Operation not supported"));
5300 if (iface
->eject_mountable_with_operation
!= NULL
)
5301 (* iface
->eject_mountable_with_operation
) (file
,
5308 (* iface
->eject_mountable
) (file
,
5316 * g_file_eject_mountable_with_operation_finish:
5317 * @file: input #GFile
5318 * @result: a #GAsyncResult
5319 * @error: a #GError, or %NULL
5321 * Finishes an asynchronous eject operation started by
5322 * g_file_eject_mountable_with_operation().
5324 * Returns: %TRUE if the @file was ejected successfully.
5330 g_file_eject_mountable_with_operation_finish (GFile
*file
,
5331 GAsyncResult
*result
,
5336 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5337 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5339 if (g_async_result_legacy_propagate_error (result
, error
))
5341 else if (g_async_result_is_tagged (result
, g_file_eject_mountable_with_operation
))
5342 return g_task_propagate_boolean (G_TASK (result
), error
);
5344 iface
= G_FILE_GET_IFACE (file
);
5345 if (iface
->eject_mountable_with_operation_finish
!= NULL
)
5346 return (* iface
->eject_mountable_with_operation_finish
) (file
, result
, error
);
5348 return (* iface
->eject_mountable_finish
) (file
, result
, error
);
5352 * g_file_monitor_directory:
5353 * @file: input #GFile
5354 * @flags: a set of #GFileMonitorFlags
5355 * @cancellable: (nullable): optional #GCancellable object,
5357 * @error: a #GError, or %NULL
5359 * Obtains a directory monitor for the given file.
5360 * This may fail if directory monitoring is not supported.
5362 * If @cancellable is not %NULL, then the operation can be cancelled by
5363 * triggering the cancellable object from another thread. If the operation
5364 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5366 * It does not make sense for @flags to contain
5367 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5368 * directories. It is not possible to monitor all the files in a
5369 * directory for changes made via hard links; if you want to do this then
5370 * you must register individual watches with g_file_monitor().
5372 * Virtual: monitor_dir
5373 * Returns: (transfer full): a #GFileMonitor for the given @file,
5374 * or %NULL on error.
5375 * Free the returned object with g_object_unref().
5378 g_file_monitor_directory (GFile
*file
,
5379 GFileMonitorFlags flags
,
5380 GCancellable
*cancellable
,
5385 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
5386 g_return_val_if_fail (~flags
& G_FILE_MONITOR_WATCH_HARD_LINKS
, NULL
);
5388 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
5391 iface
= G_FILE_GET_IFACE (file
);
5393 if (iface
->monitor_dir
== NULL
)
5395 g_set_error_literal (error
, G_IO_ERROR
,
5396 G_IO_ERROR_NOT_SUPPORTED
,
5397 _("Operation not supported"));
5401 return (* iface
->monitor_dir
) (file
, flags
, cancellable
, error
);
5405 * g_file_monitor_file:
5406 * @file: input #GFile
5407 * @flags: a set of #GFileMonitorFlags
5408 * @cancellable: (nullable): optional #GCancellable object,
5410 * @error: a #GError, or %NULL
5412 * Obtains a file monitor for the given file. If no file notification
5413 * mechanism exists, then regular polling of the file is used.
5415 * If @cancellable is not %NULL, then the operation can be cancelled by
5416 * triggering the cancellable object from another thread. If the operation
5417 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5419 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5420 * will also attempt to report changes made to the file via another
5421 * filename (ie, a hard link). Without this flag, you can only rely on
5422 * changes made through the filename contained in @file to be
5423 * reported. Using this flag may result in an increase in resource
5424 * usage, and may not have any effect depending on the #GFileMonitor
5425 * backend and/or filesystem type.
5427 * Returns: (transfer full): a #GFileMonitor for the given @file,
5428 * or %NULL on error.
5429 * Free the returned object with g_object_unref().
5432 g_file_monitor_file (GFile
*file
,
5433 GFileMonitorFlags flags
,
5434 GCancellable
*cancellable
,
5438 GFileMonitor
*monitor
;
5440 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
5442 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
5445 iface
= G_FILE_GET_IFACE (file
);
5449 if (iface
->monitor_file
)
5450 monitor
= (* iface
->monitor_file
) (file
, flags
, cancellable
, NULL
);
5452 /* Fallback to polling */
5453 if (monitor
== NULL
)
5454 monitor
= _g_poll_file_monitor_new (file
);
5461 * @file: input #GFile
5462 * @flags: a set of #GFileMonitorFlags
5463 * @cancellable: (nullable): optional #GCancellable object,
5465 * @error: a #GError, or %NULL
5467 * Obtains a file or directory monitor for the given file,
5468 * depending on the type of the file.
5470 * If @cancellable is not %NULL, then the operation can be cancelled by
5471 * triggering the cancellable object from another thread. If the operation
5472 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5474 * Returns: (transfer full): a #GFileMonitor for the given @file,
5475 * or %NULL on error.
5476 * Free the returned object with g_object_unref().
5481 g_file_monitor (GFile
*file
,
5482 GFileMonitorFlags flags
,
5483 GCancellable
*cancellable
,
5486 if (g_file_query_file_type (file
, 0, cancellable
) == G_FILE_TYPE_DIRECTORY
)
5487 return g_file_monitor_directory (file
,
5488 flags
& ~G_FILE_MONITOR_WATCH_HARD_LINKS
,
5489 cancellable
, error
);
5491 return g_file_monitor_file (file
, flags
, cancellable
, error
);
5494 /********************************************
5495 * Default implementation of async ops *
5496 ********************************************/
5500 GFileQueryInfoFlags flags
;
5501 } QueryInfoAsyncData
;
5504 query_info_data_free (QueryInfoAsyncData
*data
)
5506 g_free (data
->attributes
);
5511 query_info_async_thread (GTask
*task
,
5514 GCancellable
*cancellable
)
5516 QueryInfoAsyncData
*data
= task_data
;
5518 GError
*error
= NULL
;
5520 info
= g_file_query_info (G_FILE (object
), data
->attributes
, data
->flags
, cancellable
, &error
);
5522 g_task_return_pointer (task
, info
, g_object_unref
);
5524 g_task_return_error (task
, error
);
5528 g_file_real_query_info_async (GFile
*file
,
5529 const char *attributes
,
5530 GFileQueryInfoFlags flags
,
5532 GCancellable
*cancellable
,
5533 GAsyncReadyCallback callback
,
5537 QueryInfoAsyncData
*data
;
5539 data
= g_new0 (QueryInfoAsyncData
, 1);
5540 data
->attributes
= g_strdup (attributes
);
5541 data
->flags
= flags
;
5543 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5544 g_task_set_source_tag (task
, g_file_real_query_info_async
);
5545 g_task_set_task_data (task
, data
, (GDestroyNotify
)query_info_data_free
);
5546 g_task_set_priority (task
, io_priority
);
5547 g_task_run_in_thread (task
, query_info_async_thread
);
5548 g_object_unref (task
);
5552 g_file_real_query_info_finish (GFile
*file
,
5556 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5558 return g_task_propagate_pointer (G_TASK (res
), error
);
5562 query_filesystem_info_async_thread (GTask
*task
,
5565 GCancellable
*cancellable
)
5567 const char *attributes
= task_data
;
5569 GError
*error
= NULL
;
5571 info
= g_file_query_filesystem_info (G_FILE (object
), attributes
, cancellable
, &error
);
5573 g_task_return_pointer (task
, info
, g_object_unref
);
5575 g_task_return_error (task
, error
);
5579 g_file_real_query_filesystem_info_async (GFile
*file
,
5580 const char *attributes
,
5582 GCancellable
*cancellable
,
5583 GAsyncReadyCallback callback
,
5588 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5589 g_task_set_source_tag (task
, g_file_real_query_filesystem_info_async
);
5590 g_task_set_task_data (task
, g_strdup (attributes
), g_free
);
5591 g_task_set_priority (task
, io_priority
);
5592 g_task_run_in_thread (task
, query_filesystem_info_async_thread
);
5593 g_object_unref (task
);
5597 g_file_real_query_filesystem_info_finish (GFile
*file
,
5601 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5603 return g_task_propagate_pointer (G_TASK (res
), error
);
5607 enumerate_children_async_thread (GTask
*task
,
5610 GCancellable
*cancellable
)
5612 QueryInfoAsyncData
*data
= task_data
;
5613 GFileEnumerator
*enumerator
;
5614 GError
*error
= NULL
;
5616 enumerator
= g_file_enumerate_children (G_FILE (object
), data
->attributes
, data
->flags
, cancellable
, &error
);
5618 g_task_return_error (task
, error
);
5620 g_task_return_pointer (task
, enumerator
, g_object_unref
);
5624 g_file_real_enumerate_children_async (GFile
*file
,
5625 const char *attributes
,
5626 GFileQueryInfoFlags flags
,
5628 GCancellable
*cancellable
,
5629 GAsyncReadyCallback callback
,
5633 QueryInfoAsyncData
*data
;
5635 data
= g_new0 (QueryInfoAsyncData
, 1);
5636 data
->attributes
= g_strdup (attributes
);
5637 data
->flags
= flags
;
5639 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5640 g_task_set_source_tag (task
, g_file_real_enumerate_children_async
);
5641 g_task_set_task_data (task
, data
, (GDestroyNotify
)query_info_data_free
);
5642 g_task_set_priority (task
, io_priority
);
5643 g_task_run_in_thread (task
, enumerate_children_async_thread
);
5644 g_object_unref (task
);
5647 static GFileEnumerator
*
5648 g_file_real_enumerate_children_finish (GFile
*file
,
5652 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5654 return g_task_propagate_pointer (G_TASK (res
), error
);
5658 open_read_async_thread (GTask
*task
,
5661 GCancellable
*cancellable
)
5663 GFileInputStream
*stream
;
5664 GError
*error
= NULL
;
5666 stream
= g_file_read (G_FILE (object
), cancellable
, &error
);
5668 g_task_return_pointer (task
, stream
, g_object_unref
);
5670 g_task_return_error (task
, error
);
5674 g_file_real_read_async (GFile
*file
,
5676 GCancellable
*cancellable
,
5677 GAsyncReadyCallback callback
,
5682 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5683 g_task_set_source_tag (task
, g_file_real_read_async
);
5684 g_task_set_priority (task
, io_priority
);
5685 g_task_run_in_thread (task
, open_read_async_thread
);
5686 g_object_unref (task
);
5689 static GFileInputStream
*
5690 g_file_real_read_finish (GFile
*file
,
5694 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5696 return g_task_propagate_pointer (G_TASK (res
), error
);
5700 append_to_async_thread (GTask
*task
,
5701 gpointer source_object
,
5703 GCancellable
*cancellable
)
5705 GFileCreateFlags
*data
= task_data
;
5706 GFileOutputStream
*stream
;
5707 GError
*error
= NULL
;
5709 stream
= g_file_append_to (G_FILE (source_object
), *data
, cancellable
, &error
);
5711 g_task_return_pointer (task
, stream
, g_object_unref
);
5713 g_task_return_error (task
, error
);
5717 g_file_real_append_to_async (GFile
*file
,
5718 GFileCreateFlags flags
,
5720 GCancellable
*cancellable
,
5721 GAsyncReadyCallback callback
,
5724 GFileCreateFlags
*data
;
5727 data
= g_new0 (GFileCreateFlags
, 1);
5730 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5731 g_task_set_source_tag (task
, g_file_real_append_to_async
);
5732 g_task_set_task_data (task
, data
, g_free
);
5733 g_task_set_priority (task
, io_priority
);
5735 g_task_run_in_thread (task
, append_to_async_thread
);
5736 g_object_unref (task
);
5739 static GFileOutputStream
*
5740 g_file_real_append_to_finish (GFile
*file
,
5744 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5746 return g_task_propagate_pointer (G_TASK (res
), error
);
5750 create_async_thread (GTask
*task
,
5751 gpointer source_object
,
5753 GCancellable
*cancellable
)
5755 GFileCreateFlags
*data
= task_data
;
5756 GFileOutputStream
*stream
;
5757 GError
*error
= NULL
;
5759 stream
= g_file_create (G_FILE (source_object
), *data
, cancellable
, &error
);
5761 g_task_return_pointer (task
, stream
, g_object_unref
);
5763 g_task_return_error (task
, error
);
5767 g_file_real_create_async (GFile
*file
,
5768 GFileCreateFlags flags
,
5770 GCancellable
*cancellable
,
5771 GAsyncReadyCallback callback
,
5774 GFileCreateFlags
*data
;
5777 data
= g_new0 (GFileCreateFlags
, 1);
5780 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5781 g_task_set_source_tag (task
, g_file_real_create_async
);
5782 g_task_set_task_data (task
, data
, g_free
);
5783 g_task_set_priority (task
, io_priority
);
5785 g_task_run_in_thread (task
, create_async_thread
);
5786 g_object_unref (task
);
5789 static GFileOutputStream
*
5790 g_file_real_create_finish (GFile
*file
,
5794 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5796 return g_task_propagate_pointer (G_TASK (res
), error
);
5800 GFileOutputStream
*stream
;
5802 gboolean make_backup
;
5803 GFileCreateFlags flags
;
5807 replace_async_data_free (ReplaceAsyncData
*data
)
5810 g_object_unref (data
->stream
);
5811 g_free (data
->etag
);
5816 replace_async_thread (GTask
*task
,
5817 gpointer source_object
,
5819 GCancellable
*cancellable
)
5821 GFileOutputStream
*stream
;
5822 ReplaceAsyncData
*data
= task_data
;
5823 GError
*error
= NULL
;
5825 stream
= g_file_replace (G_FILE (source_object
),
5833 g_task_return_pointer (task
, stream
, g_object_unref
);
5835 g_task_return_error (task
, error
);
5839 g_file_real_replace_async (GFile
*file
,
5841 gboolean make_backup
,
5842 GFileCreateFlags flags
,
5844 GCancellable
*cancellable
,
5845 GAsyncReadyCallback callback
,
5849 ReplaceAsyncData
*data
;
5851 data
= g_new0 (ReplaceAsyncData
, 1);
5852 data
->etag
= g_strdup (etag
);
5853 data
->make_backup
= make_backup
;
5854 data
->flags
= flags
;
5856 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5857 g_task_set_source_tag (task
, g_file_real_replace_async
);
5858 g_task_set_task_data (task
, data
, (GDestroyNotify
)replace_async_data_free
);
5859 g_task_set_priority (task
, io_priority
);
5861 g_task_run_in_thread (task
, replace_async_thread
);
5862 g_object_unref (task
);
5865 static GFileOutputStream
*
5866 g_file_real_replace_finish (GFile
*file
,
5870 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5872 return g_task_propagate_pointer (G_TASK (res
), error
);
5876 delete_async_thread (GTask
*task
,
5879 GCancellable
*cancellable
)
5881 GError
*error
= NULL
;
5883 if (g_file_delete (G_FILE (object
), cancellable
, &error
))
5884 g_task_return_boolean (task
, TRUE
);
5886 g_task_return_error (task
, error
);
5890 g_file_real_delete_async (GFile
*file
,
5892 GCancellable
*cancellable
,
5893 GAsyncReadyCallback callback
,
5898 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5899 g_task_set_source_tag (task
, g_file_real_delete_async
);
5900 g_task_set_priority (task
, io_priority
);
5901 g_task_run_in_thread (task
, delete_async_thread
);
5902 g_object_unref (task
);
5906 g_file_real_delete_finish (GFile
*file
,
5910 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
5912 return g_task_propagate_boolean (G_TASK (res
), error
);
5916 trash_async_thread (GTask
*task
,
5919 GCancellable
*cancellable
)
5921 GError
*error
= NULL
;
5923 if (g_file_trash (G_FILE (object
), cancellable
, &error
))
5924 g_task_return_boolean (task
, TRUE
);
5926 g_task_return_error (task
, error
);
5930 g_file_real_trash_async (GFile
*file
,
5932 GCancellable
*cancellable
,
5933 GAsyncReadyCallback callback
,
5938 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5939 g_task_set_source_tag (task
, g_file_real_trash_async
);
5940 g_task_set_priority (task
, io_priority
);
5941 g_task_run_in_thread (task
, trash_async_thread
);
5942 g_object_unref (task
);
5946 g_file_real_trash_finish (GFile
*file
,
5950 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
5952 return g_task_propagate_boolean (G_TASK (res
), error
);
5956 make_directory_async_thread (GTask
*task
,
5959 GCancellable
*cancellable
)
5961 GError
*error
= NULL
;
5963 if (g_file_make_directory (G_FILE (object
), cancellable
, &error
))
5964 g_task_return_boolean (task
, TRUE
);
5966 g_task_return_error (task
, error
);
5970 g_file_real_make_directory_async (GFile
*file
,
5972 GCancellable
*cancellable
,
5973 GAsyncReadyCallback callback
,
5978 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5979 g_task_set_source_tag (task
, g_file_real_make_directory_async
);
5980 g_task_set_priority (task
, io_priority
);
5981 g_task_run_in_thread (task
, make_directory_async_thread
);
5982 g_object_unref (task
);
5986 g_file_real_make_directory_finish (GFile
*file
,
5990 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
5992 return g_task_propagate_boolean (G_TASK (res
), error
);
5996 open_readwrite_async_thread (GTask
*task
,
5999 GCancellable
*cancellable
)
6001 GFileIOStream
*stream
;
6002 GError
*error
= NULL
;
6004 stream
= g_file_open_readwrite (G_FILE (object
), cancellable
, &error
);
6007 g_task_return_error (task
, error
);
6009 g_task_return_pointer (task
, stream
, g_object_unref
);
6013 g_file_real_open_readwrite_async (GFile
*file
,
6015 GCancellable
*cancellable
,
6016 GAsyncReadyCallback callback
,
6021 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6022 g_task_set_source_tag (task
, g_file_real_open_readwrite_async
);
6023 g_task_set_priority (task
, io_priority
);
6025 g_task_run_in_thread (task
, open_readwrite_async_thread
);
6026 g_object_unref (task
);
6029 static GFileIOStream
*
6030 g_file_real_open_readwrite_finish (GFile
*file
,
6034 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6036 return g_task_propagate_pointer (G_TASK (res
), error
);
6040 create_readwrite_async_thread (GTask
*task
,
6043 GCancellable
*cancellable
)
6045 GFileCreateFlags
*data
= task_data
;
6046 GFileIOStream
*stream
;
6047 GError
*error
= NULL
;
6049 stream
= g_file_create_readwrite (G_FILE (object
), *data
, cancellable
, &error
);
6052 g_task_return_error (task
, error
);
6054 g_task_return_pointer (task
, stream
, g_object_unref
);
6058 g_file_real_create_readwrite_async (GFile
*file
,
6059 GFileCreateFlags flags
,
6061 GCancellable
*cancellable
,
6062 GAsyncReadyCallback callback
,
6065 GFileCreateFlags
*data
;
6068 data
= g_new0 (GFileCreateFlags
, 1);
6071 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6072 g_task_set_source_tag (task
, g_file_real_create_readwrite_async
);
6073 g_task_set_task_data (task
, data
, g_free
);
6074 g_task_set_priority (task
, io_priority
);
6076 g_task_run_in_thread (task
, create_readwrite_async_thread
);
6077 g_object_unref (task
);
6080 static GFileIOStream
*
6081 g_file_real_create_readwrite_finish (GFile
*file
,
6085 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6087 return g_task_propagate_pointer (G_TASK (res
), error
);
6092 gboolean make_backup
;
6093 GFileCreateFlags flags
;
6094 } ReplaceRWAsyncData
;
6097 replace_rw_async_data_free (ReplaceRWAsyncData
*data
)
6099 g_free (data
->etag
);
6104 replace_readwrite_async_thread (GTask
*task
,
6107 GCancellable
*cancellable
)
6109 GFileIOStream
*stream
;
6110 GError
*error
= NULL
;
6111 ReplaceRWAsyncData
*data
= task_data
;
6113 stream
= g_file_replace_readwrite (G_FILE (object
),
6121 g_task_return_error (task
, error
);
6123 g_task_return_pointer (task
, stream
, g_object_unref
);
6127 g_file_real_replace_readwrite_async (GFile
*file
,
6129 gboolean make_backup
,
6130 GFileCreateFlags flags
,
6132 GCancellable
*cancellable
,
6133 GAsyncReadyCallback callback
,
6137 ReplaceRWAsyncData
*data
;
6139 data
= g_new0 (ReplaceRWAsyncData
, 1);
6140 data
->etag
= g_strdup (etag
);
6141 data
->make_backup
= make_backup
;
6142 data
->flags
= flags
;
6144 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6145 g_task_set_source_tag (task
, g_file_real_replace_readwrite_async
);
6146 g_task_set_task_data (task
, data
, (GDestroyNotify
)replace_rw_async_data_free
);
6147 g_task_set_priority (task
, io_priority
);
6149 g_task_run_in_thread (task
, replace_readwrite_async_thread
);
6150 g_object_unref (task
);
6153 static GFileIOStream
*
6154 g_file_real_replace_readwrite_finish (GFile
*file
,
6158 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6160 return g_task_propagate_pointer (G_TASK (res
), error
);
6164 set_display_name_async_thread (GTask
*task
,
6167 GCancellable
*cancellable
)
6169 GError
*error
= NULL
;
6170 char *name
= task_data
;
6173 file
= g_file_set_display_name (G_FILE (object
), name
, cancellable
, &error
);
6176 g_task_return_error (task
, error
);
6178 g_task_return_pointer (task
, file
, g_object_unref
);
6182 g_file_real_set_display_name_async (GFile
*file
,
6183 const char *display_name
,
6185 GCancellable
*cancellable
,
6186 GAsyncReadyCallback callback
,
6191 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6192 g_task_set_source_tag (task
, g_file_real_set_display_name_async
);
6193 g_task_set_task_data (task
, g_strdup (display_name
), g_free
);
6194 g_task_set_priority (task
, io_priority
);
6196 g_task_run_in_thread (task
, set_display_name_async_thread
);
6197 g_object_unref (task
);
6201 g_file_real_set_display_name_finish (GFile
*file
,
6205 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6207 return g_task_propagate_pointer (G_TASK (res
), error
);
6211 GFileQueryInfoFlags flags
;
6218 set_info_data_free (SetInfoAsyncData
*data
)
6221 g_object_unref (data
->info
);
6223 g_error_free (data
->error
);
6228 set_info_async_thread (GTask
*task
,
6231 GCancellable
*cancellable
)
6233 SetInfoAsyncData
*data
= task_data
;
6236 data
->res
= g_file_set_attributes_from_info (G_FILE (object
),
6244 g_file_real_set_attributes_async (GFile
*file
,
6246 GFileQueryInfoFlags flags
,
6248 GCancellable
*cancellable
,
6249 GAsyncReadyCallback callback
,
6253 SetInfoAsyncData
*data
;
6255 data
= g_new0 (SetInfoAsyncData
, 1);
6256 data
->info
= g_file_info_dup (info
);
6257 data
->flags
= flags
;
6259 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6260 g_task_set_source_tag (task
, g_file_real_set_attributes_async
);
6261 g_task_set_task_data (task
, data
, (GDestroyNotify
)set_info_data_free
);
6262 g_task_set_priority (task
, io_priority
);
6264 g_task_run_in_thread (task
, set_info_async_thread
);
6265 g_object_unref (task
);
6269 g_file_real_set_attributes_finish (GFile
*file
,
6274 SetInfoAsyncData
*data
;
6276 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
6278 data
= g_task_get_task_data (G_TASK (res
));
6281 *info
= g_object_ref (data
->info
);
6283 if (error
!= NULL
&& data
->error
)
6284 *error
= g_error_copy (data
->error
);
6290 find_enclosing_mount_async_thread (GTask
*task
,
6293 GCancellable
*cancellable
)
6295 GError
*error
= NULL
;
6298 mount
= g_file_find_enclosing_mount (G_FILE (object
), cancellable
, &error
);
6301 g_task_return_error (task
, error
);
6303 g_task_return_pointer (task
, mount
, g_object_unref
);
6307 g_file_real_find_enclosing_mount_async (GFile
*file
,
6309 GCancellable
*cancellable
,
6310 GAsyncReadyCallback callback
,
6315 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6316 g_task_set_source_tag (task
, g_file_real_find_enclosing_mount_async
);
6317 g_task_set_priority (task
, io_priority
);
6319 g_task_run_in_thread (task
, find_enclosing_mount_async_thread
);
6320 g_object_unref (task
);
6324 g_file_real_find_enclosing_mount_finish (GFile
*file
,
6328 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6330 return g_task_propagate_pointer (G_TASK (res
), error
);
6337 GFileCopyFlags flags
;
6338 GFileProgressCallback progress_cb
;
6339 gpointer progress_cb_data
;
6343 copy_async_data_free (CopyAsyncData
*data
)
6345 g_object_unref (data
->source
);
6346 g_object_unref (data
->destination
);
6347 g_slice_free (CopyAsyncData
, data
);
6351 CopyAsyncData
*data
;
6352 goffset current_num_bytes
;
6353 goffset total_num_bytes
;
6357 copy_async_progress_in_main (gpointer user_data
)
6359 ProgressData
*progress
= user_data
;
6360 CopyAsyncData
*data
= progress
->data
;
6362 data
->progress_cb (progress
->current_num_bytes
,
6363 progress
->total_num_bytes
,
6364 data
->progress_cb_data
);
6370 copy_async_progress_callback (goffset current_num_bytes
,
6371 goffset total_num_bytes
,
6374 GTask
*task
= user_data
;
6375 CopyAsyncData
*data
= g_task_get_task_data (task
);
6376 ProgressData
*progress
;
6378 progress
= g_new (ProgressData
, 1);
6379 progress
->data
= data
;
6380 progress
->current_num_bytes
= current_num_bytes
;
6381 progress
->total_num_bytes
= total_num_bytes
;
6383 g_main_context_invoke_full (g_task_get_context (task
),
6384 g_task_get_priority (task
),
6385 copy_async_progress_in_main
,
6391 copy_async_thread (GTask
*task
,
6394 GCancellable
*cancellable
)
6396 CopyAsyncData
*data
= task_data
;
6398 GError
*error
= NULL
;
6400 result
= g_file_copy (data
->source
,
6404 (data
->progress_cb
!= NULL
) ? copy_async_progress_callback
: NULL
,
6408 g_task_return_boolean (task
, TRUE
);
6410 g_task_return_error (task
, error
);
6414 g_file_real_copy_async (GFile
*source
,
6416 GFileCopyFlags flags
,
6418 GCancellable
*cancellable
,
6419 GFileProgressCallback progress_callback
,
6420 gpointer progress_callback_data
,
6421 GAsyncReadyCallback callback
,
6425 CopyAsyncData
*data
;
6427 data
= g_slice_new (CopyAsyncData
);
6428 data
->source
= g_object_ref (source
);
6429 data
->destination
= g_object_ref (destination
);
6430 data
->flags
= flags
;
6431 data
->progress_cb
= progress_callback
;
6432 data
->progress_cb_data
= progress_callback_data
;
6434 task
= g_task_new (source
, cancellable
, callback
, user_data
);
6435 g_task_set_source_tag (task
, g_file_real_copy_async
);
6436 g_task_set_task_data (task
, data
, (GDestroyNotify
)copy_async_data_free
);
6437 g_task_set_priority (task
, io_priority
);
6438 g_task_run_in_thread (task
, copy_async_thread
);
6439 g_object_unref (task
);
6443 g_file_real_copy_finish (GFile
*file
,
6447 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
6449 return g_task_propagate_boolean (G_TASK (res
), error
);
6453 /********************************************
6454 * Default VFS operations *
6455 ********************************************/
6458 * g_file_new_for_path:
6459 * @path: (type filename): a string containing a relative or absolute path.
6460 * The string must be encoded in the glib filename encoding.
6462 * Constructs a #GFile for a given path. This operation never
6463 * fails, but the returned object might not support any I/O
6464 * operation if @path is malformed.
6466 * Returns: (transfer full): a new #GFile for the given @path.
6467 * Free the returned object with g_object_unref().
6470 g_file_new_for_path (const char *path
)
6472 g_return_val_if_fail (path
!= NULL
, NULL
);
6474 return g_vfs_get_file_for_path (g_vfs_get_default (), path
);
6478 * g_file_new_for_uri:
6479 * @uri: a UTF-8 string containing a URI
6481 * Constructs a #GFile for a given URI. This operation never
6482 * fails, but the returned object might not support any I/O
6483 * operation if @uri is malformed or if the uri type is
6486 * Returns: (transfer full): a new #GFile for the given @uri.
6487 * Free the returned object with g_object_unref().
6490 g_file_new_for_uri (const char *uri
)
6492 g_return_val_if_fail (uri
!= NULL
, NULL
);
6494 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri
);
6499 * @tmpl: (type filename) (nullable): Template for the file
6500 * name, as in g_file_open_tmp(), or %NULL for a default template
6501 * @iostream: (out): on return, a #GFileIOStream for the created file
6502 * @error: a #GError, or %NULL
6504 * Opens a file in the preferred directory for temporary files (as
6505 * returned by g_get_tmp_dir()) and returns a #GFile and
6506 * #GFileIOStream pointing to it.
6508 * @tmpl should be a string in the GLib file name encoding
6509 * containing a sequence of six 'X' characters, and containing no
6510 * directory components. If it is %NULL, a default template is used.
6512 * Unlike the other #GFile constructors, this will return %NULL if
6513 * a temporary file could not be created.
6515 * Returns: (transfer full): a new #GFile.
6516 * Free the returned object with g_object_unref().
6521 g_file_new_tmp (const char *tmpl
,
6522 GFileIOStream
**iostream
,
6528 GFileOutputStream
*output
;
6530 g_return_val_if_fail (iostream
!= NULL
, NULL
);
6532 fd
= g_file_open_tmp (tmpl
, &path
, error
);
6536 file
= g_file_new_for_path (path
);
6538 output
= _g_local_file_output_stream_new (fd
);
6539 *iostream
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
6541 g_object_unref (output
);
6548 * g_file_parse_name:
6549 * @parse_name: a file name or path to be parsed
6551 * Constructs a #GFile with the given @parse_name (i.e. something
6552 * given by g_file_get_parse_name()). This operation never fails,
6553 * but the returned object might not support any I/O operation if
6554 * the @parse_name cannot be parsed.
6556 * Returns: (transfer full): a new #GFile.
6559 g_file_parse_name (const char *parse_name
)
6561 g_return_val_if_fail (parse_name
!= NULL
, NULL
);
6563 return g_vfs_parse_name (g_vfs_get_default (), parse_name
);
6567 * g_file_new_build_filename:
6568 * @first_element: (type filename): the first element in the path
6569 * @...: remaining elements in path, terminated by %NULL
6571 * Constructs a #GFile from a series of elements using the correct
6572 * separator for filenames.
6574 * Using this function is equivalent to calling g_build_filename(),
6575 * followed by g_file_new_for_path() on the result.
6577 * Returns: (transfer full): a new #GFile
6582 g_file_new_build_filename (const gchar
*first_element
,
6589 g_return_val_if_fail (first_element
!= NULL
, NULL
);
6591 va_start (args
, first_element
);
6592 str
= g_build_filename_valist (first_element
, &args
);
6595 file
= g_file_new_for_path (str
);
6602 is_valid_scheme_character (char c
)
6604 return g_ascii_isalnum (c
) || c
== '+' || c
== '-' || c
== '.';
6607 /* Following RFC 2396, valid schemes are built like:
6608 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6611 has_valid_scheme (const char *uri
)
6617 if (!g_ascii_isalpha (*p
))
6622 } while (is_valid_scheme_character (*p
));
6628 new_for_cmdline_arg (const gchar
*arg
,
6634 if (g_path_is_absolute (arg
))
6635 return g_file_new_for_path (arg
);
6637 if (has_valid_scheme (arg
))
6638 return g_file_new_for_uri (arg
);
6644 current_dir
= g_get_current_dir ();
6645 filename
= g_build_filename (current_dir
, arg
, NULL
);
6646 g_free (current_dir
);
6649 filename
= g_build_filename (cwd
, arg
, NULL
);
6651 file
= g_file_new_for_path (filename
);
6658 * g_file_new_for_commandline_arg:
6659 * @arg: (type filename): a command line string
6661 * Creates a #GFile with the given argument from the command line.
6662 * The value of @arg can be either a URI, an absolute path or a
6663 * relative path resolved relative to the current working directory.
6664 * This operation never fails, but the returned object might not
6665 * support any I/O operation if @arg points to a malformed path.
6667 * Note that on Windows, this function expects its argument to be in
6668 * UTF-8 -- not the system code page. This means that you
6669 * should not use this function with string from argv as it is passed
6670 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6671 * the commandline. #GApplication also uses UTF-8 but
6672 * g_application_command_line_create_file_for_arg() may be more useful
6673 * for you there. It is also always possible to use this function with
6674 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6676 * Returns: (transfer full): a new #GFile.
6677 * Free the returned object with g_object_unref().
6680 g_file_new_for_commandline_arg (const char *arg
)
6682 g_return_val_if_fail (arg
!= NULL
, NULL
);
6684 return new_for_cmdline_arg (arg
, NULL
);
6688 * g_file_new_for_commandline_arg_and_cwd:
6689 * @arg: (type filename): a command line string
6690 * @cwd: (type filename): the current working directory of the commandline
6692 * Creates a #GFile with the given argument from the command line.
6694 * This function is similar to g_file_new_for_commandline_arg() except
6695 * that it allows for passing the current working directory as an
6696 * argument instead of using the current working directory of the
6699 * This is useful if the commandline argument was given in a context
6700 * other than the invocation of the current process.
6702 * See also g_application_command_line_create_file_for_arg().
6704 * Returns: (transfer full): a new #GFile
6709 g_file_new_for_commandline_arg_and_cwd (const gchar
*arg
,
6712 g_return_val_if_fail (arg
!= NULL
, NULL
);
6713 g_return_val_if_fail (cwd
!= NULL
, NULL
);
6715 return new_for_cmdline_arg (arg
, cwd
);
6719 * g_file_mount_enclosing_volume:
6720 * @location: input #GFile
6721 * @flags: flags affecting the operation
6722 * @mount_operation: (nullable): a #GMountOperation
6723 * or %NULL to avoid user interaction
6724 * @cancellable: (nullable): optional #GCancellable object,
6726 * @callback: (nullable): a #GAsyncReadyCallback to call
6727 * when the request is satisfied, or %NULL
6728 * @user_data: the data to pass to callback function
6730 * Starts a @mount_operation, mounting the volume that contains
6731 * the file @location.
6733 * When this operation has completed, @callback will be called with
6734 * @user_user data, and the operation can be finalized with
6735 * g_file_mount_enclosing_volume_finish().
6737 * If @cancellable is not %NULL, then the operation can be cancelled by
6738 * triggering the cancellable object from another thread. If the operation
6739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6742 g_file_mount_enclosing_volume (GFile
*location
,
6743 GMountMountFlags flags
,
6744 GMountOperation
*mount_operation
,
6745 GCancellable
*cancellable
,
6746 GAsyncReadyCallback callback
,
6751 g_return_if_fail (G_IS_FILE (location
));
6753 iface
= G_FILE_GET_IFACE (location
);
6755 if (iface
->mount_enclosing_volume
== NULL
)
6757 g_task_report_new_error (location
, callback
, user_data
,
6758 g_file_mount_enclosing_volume
,
6759 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
6760 _("volume doesn’t implement mount"));
6764 (* iface
->mount_enclosing_volume
) (location
, flags
, mount_operation
, cancellable
, callback
, user_data
);
6769 * g_file_mount_enclosing_volume_finish:
6770 * @location: input #GFile
6771 * @result: a #GAsyncResult
6772 * @error: a #GError, or %NULL
6774 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6776 * Returns: %TRUE if successful. If an error has occurred,
6777 * this function will return %FALSE and set @error
6778 * appropriately if present.
6781 g_file_mount_enclosing_volume_finish (GFile
*location
,
6782 GAsyncResult
*result
,
6787 g_return_val_if_fail (G_IS_FILE (location
), FALSE
);
6788 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
6790 if (g_async_result_legacy_propagate_error (result
, error
))
6792 else if (g_async_result_is_tagged (result
, g_file_mount_enclosing_volume
))
6793 return g_task_propagate_boolean (G_TASK (result
), error
);
6795 iface
= G_FILE_GET_IFACE (location
);
6797 return (* iface
->mount_enclosing_volume_finish
) (location
, result
, error
);
6800 /********************************************
6801 * Utility functions *
6802 ********************************************/
6805 * g_file_query_default_handler:
6806 * @file: a #GFile to open
6807 * @cancellable: optional #GCancellable object, %NULL to ignore
6808 * @error: a #GError, or %NULL
6810 * Returns the #GAppInfo that is registered as the default
6811 * application to handle the file specified by @file.
6813 * If @cancellable is not %NULL, then the operation can be cancelled by
6814 * triggering the cancellable object from another thread. If the operation
6815 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6817 * Returns: (transfer full): a #GAppInfo if the handle was found,
6818 * %NULL if there were errors.
6819 * When you are done with it, release it with g_object_unref()
6822 g_file_query_default_handler (GFile
*file
,
6823 GCancellable
*cancellable
,
6827 const char *content_type
;
6832 uri_scheme
= g_file_get_uri_scheme (file
);
6833 if (uri_scheme
&& uri_scheme
[0] != '\0')
6835 appinfo
= g_app_info_get_default_for_uri_scheme (uri_scheme
);
6836 g_free (uri_scheme
);
6838 if (appinfo
!= NULL
)
6842 info
= g_file_query_info (file
,
6843 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
,
6852 content_type
= g_file_info_get_content_type (info
);
6855 /* Don't use is_native(), as we want to support fuse paths if available */
6856 path
= g_file_get_path (file
);
6857 appinfo
= g_app_info_get_default_for_type (content_type
,
6862 g_object_unref (info
);
6864 if (appinfo
!= NULL
)
6867 g_set_error_literal (error
, G_IO_ERROR
,
6868 G_IO_ERROR_NOT_SUPPORTED
,
6869 _("No application is registered as handling this file"));
6873 #define GET_CONTENT_BLOCK_SIZE 8192
6876 * g_file_load_contents:
6877 * @file: input #GFile
6878 * @cancellable: optional #GCancellable object, %NULL to ignore
6879 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6880 * @length: (out) (optional): a location to place the length of the contents of the file,
6881 * or %NULL if the length is not needed
6882 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6883 * or %NULL if the entity tag is not needed
6884 * @error: a #GError, or %NULL
6886 * Loads the content of the file into memory. The data is always
6887 * zero-terminated, but this is not included in the resultant @length.
6888 * The returned @content should be freed with g_free() when no longer
6891 * If @cancellable is not %NULL, then the operation can be cancelled by
6892 * triggering the cancellable object from another thread. If the operation
6893 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6895 * Returns: %TRUE if the @file's contents were successfully loaded.
6896 * %FALSE if there were errors.
6899 g_file_load_contents (GFile
*file
,
6900 GCancellable
*cancellable
,
6906 GFileInputStream
*in
;
6907 GByteArray
*content
;
6912 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
6913 g_return_val_if_fail (contents
!= NULL
, FALSE
);
6915 in
= g_file_read (file
, cancellable
, error
);
6919 content
= g_byte_array_new ();
6922 g_byte_array_set_size (content
, pos
+ GET_CONTENT_BLOCK_SIZE
+ 1);
6923 while ((res
= g_input_stream_read (G_INPUT_STREAM (in
),
6924 content
->data
+ pos
,
6925 GET_CONTENT_BLOCK_SIZE
,
6926 cancellable
, error
)) > 0)
6929 g_byte_array_set_size (content
, pos
+ GET_CONTENT_BLOCK_SIZE
+ 1);
6936 info
= g_file_input_stream_query_info (in
,
6937 G_FILE_ATTRIBUTE_ETAG_VALUE
,
6942 *etag_out
= g_strdup (g_file_info_get_etag (info
));
6943 g_object_unref (info
);
6947 /* Ignore errors on close */
6948 g_input_stream_close (G_INPUT_STREAM (in
), cancellable
, NULL
);
6949 g_object_unref (in
);
6953 /* error is set already */
6954 g_byte_array_free (content
, TRUE
);
6961 /* Zero terminate (we got an extra byte allocated for this */
6962 content
->data
[pos
] = 0;
6964 *contents
= (char *)g_byte_array_free (content
, FALSE
);
6971 GFileReadMoreCallback read_more_callback
;
6972 GByteArray
*content
;
6979 load_contents_data_free (LoadContentsData
*data
)
6982 g_byte_array_free (data
->content
, TRUE
);
6983 g_free (data
->etag
);
6988 load_contents_close_callback (GObject
*obj
,
6989 GAsyncResult
*close_res
,
6992 GInputStream
*stream
= G_INPUT_STREAM (obj
);
6993 LoadContentsData
*data
= user_data
;
6995 /* Ignore errors here, we're only reading anyway */
6996 g_input_stream_close_finish (stream
, close_res
, NULL
);
6997 g_object_unref (stream
);
6999 g_task_return_boolean (data
->task
, TRUE
);
7000 g_object_unref (data
->task
);
7004 load_contents_fstat_callback (GObject
*obj
,
7005 GAsyncResult
*stat_res
,
7008 GInputStream
*stream
= G_INPUT_STREAM (obj
);
7009 LoadContentsData
*data
= user_data
;
7012 info
= g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream
),
7016 data
->etag
= g_strdup (g_file_info_get_etag (info
));
7017 g_object_unref (info
);
7020 g_input_stream_close_async (stream
, 0,
7021 g_task_get_cancellable (data
->task
),
7022 load_contents_close_callback
, data
);
7026 load_contents_read_callback (GObject
*obj
,
7027 GAsyncResult
*read_res
,
7030 GInputStream
*stream
= G_INPUT_STREAM (obj
);
7031 LoadContentsData
*data
= user_data
;
7032 GError
*error
= NULL
;
7035 read_size
= g_input_stream_read_finish (stream
, read_res
, &error
);
7039 g_task_return_error (data
->task
, error
);
7040 g_object_unref (data
->task
);
7042 /* Close the file ignoring any error */
7043 g_input_stream_close_async (stream
, 0, NULL
, NULL
, NULL
);
7044 g_object_unref (stream
);
7046 else if (read_size
== 0)
7048 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream
),
7049 G_FILE_ATTRIBUTE_ETAG_VALUE
,
7051 g_task_get_cancellable (data
->task
),
7052 load_contents_fstat_callback
,
7055 else if (read_size
> 0)
7057 data
->pos
+= read_size
;
7059 g_byte_array_set_size (data
->content
,
7060 data
->pos
+ GET_CONTENT_BLOCK_SIZE
);
7063 if (data
->read_more_callback
&&
7064 !data
->read_more_callback ((char *)data
->content
->data
, data
->pos
,
7065 g_async_result_get_user_data (G_ASYNC_RESULT (data
->task
))))
7066 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream
),
7067 G_FILE_ATTRIBUTE_ETAG_VALUE
,
7069 g_task_get_cancellable (data
->task
),
7070 load_contents_fstat_callback
,
7073 g_input_stream_read_async (stream
,
7074 data
->content
->data
+ data
->pos
,
7075 GET_CONTENT_BLOCK_SIZE
,
7077 g_task_get_cancellable (data
->task
),
7078 load_contents_read_callback
,
7084 load_contents_open_callback (GObject
*obj
,
7085 GAsyncResult
*open_res
,
7088 GFile
*file
= G_FILE (obj
);
7089 GFileInputStream
*stream
;
7090 LoadContentsData
*data
= user_data
;
7091 GError
*error
= NULL
;
7093 stream
= g_file_read_finish (file
, open_res
, &error
);
7097 g_byte_array_set_size (data
->content
,
7098 data
->pos
+ GET_CONTENT_BLOCK_SIZE
);
7099 g_input_stream_read_async (G_INPUT_STREAM (stream
),
7100 data
->content
->data
+ data
->pos
,
7101 GET_CONTENT_BLOCK_SIZE
,
7103 g_task_get_cancellable (data
->task
),
7104 load_contents_read_callback
,
7109 g_task_return_error (data
->task
, error
);
7110 g_object_unref (data
->task
);
7115 * g_file_load_partial_contents_async: (skip)
7116 * @file: input #GFile
7117 * @cancellable: optional #GCancellable object, %NULL to ignore
7118 * @read_more_callback: (scope call) (closure user_data): a
7119 * #GFileReadMoreCallback to receive partial data
7120 * and to specify whether further data should be read
7121 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7122 * when the request is satisfied
7123 * @user_data: the data to pass to the callback functions
7125 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7126 * be used to stop reading from the file when appropriate, else this
7127 * function will behave exactly as g_file_load_contents_async(). This
7128 * operation can be finished by g_file_load_partial_contents_finish().
7130 * Users of this function should be aware that @user_data is passed to
7131 * both the @read_more_callback and the @callback.
7133 * If @cancellable is not %NULL, then the operation can be cancelled by
7134 * triggering the cancellable object from another thread. If the operation
7135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7138 g_file_load_partial_contents_async (GFile
*file
,
7139 GCancellable
*cancellable
,
7140 GFileReadMoreCallback read_more_callback
,
7141 GAsyncReadyCallback callback
,
7144 LoadContentsData
*data
;
7146 g_return_if_fail (G_IS_FILE (file
));
7148 data
= g_new0 (LoadContentsData
, 1);
7149 data
->read_more_callback
= read_more_callback
;
7150 data
->content
= g_byte_array_new ();
7152 data
->task
= g_task_new (file
, cancellable
, callback
, user_data
);
7153 g_task_set_source_tag (data
->task
, g_file_load_partial_contents_async
);
7154 g_task_set_task_data (data
->task
, data
, (GDestroyNotify
)load_contents_data_free
);
7156 g_file_read_async (file
,
7158 g_task_get_cancellable (data
->task
),
7159 load_contents_open_callback
,
7164 * g_file_load_partial_contents_finish:
7165 * @file: input #GFile
7166 * @res: a #GAsyncResult
7167 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7168 * @length: (out) (optional): a location to place the length of the contents of the file,
7169 * or %NULL if the length is not needed
7170 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7171 * or %NULL if the entity tag is not needed
7172 * @error: a #GError, or %NULL
7174 * Finishes an asynchronous partial load operation that was started
7175 * with g_file_load_partial_contents_async(). The data is always
7176 * zero-terminated, but this is not included in the resultant @length.
7177 * The returned @content should be freed with g_free() when no longer
7180 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7181 * present, it will be set appropriately.
7184 g_file_load_partial_contents_finish (GFile
*file
,
7192 LoadContentsData
*data
;
7194 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7195 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
7196 g_return_val_if_fail (contents
!= NULL
, FALSE
);
7198 task
= G_TASK (res
);
7200 if (!g_task_propagate_boolean (task
, error
))
7207 data
= g_task_get_task_data (task
);
7210 *length
= data
->pos
;
7214 *etag_out
= data
->etag
;
7218 /* Zero terminate */
7219 g_byte_array_set_size (data
->content
, data
->pos
+ 1);
7220 data
->content
->data
[data
->pos
] = 0;
7222 *contents
= (char *)g_byte_array_free (data
->content
, FALSE
);
7223 data
->content
= NULL
;
7229 * g_file_load_contents_async:
7230 * @file: input #GFile
7231 * @cancellable: optional #GCancellable object, %NULL to ignore
7232 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7233 * @user_data: the data to pass to callback function
7235 * Starts an asynchronous load of the @file's contents.
7237 * For more details, see g_file_load_contents() which is
7238 * the synchronous version of this call.
7240 * When the load operation has completed, @callback will be called
7241 * with @user data. To finish the operation, call
7242 * g_file_load_contents_finish() with the #GAsyncResult returned by
7245 * If @cancellable is not %NULL, then the operation can be cancelled by
7246 * triggering the cancellable object from another thread. If the operation
7247 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7250 g_file_load_contents_async (GFile
*file
,
7251 GCancellable
*cancellable
,
7252 GAsyncReadyCallback callback
,
7255 g_file_load_partial_contents_async (file
,
7258 callback
, user_data
);
7262 * g_file_load_contents_finish:
7263 * @file: input #GFile
7264 * @res: a #GAsyncResult
7265 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7266 * @length: (out) (optional): a location to place the length of the contents of the file,
7267 * or %NULL if the length is not needed
7268 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7269 * or %NULL if the entity tag is not needed
7270 * @error: a #GError, or %NULL
7272 * Finishes an asynchronous load of the @file's contents.
7273 * The contents are placed in @contents, and @length is set to the
7274 * size of the @contents string. The @content should be freed with
7275 * g_free() when no longer needed. If @etag_out is present, it will be
7276 * set to the new entity tag for the @file.
7278 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7279 * present, it will be set appropriately.
7282 g_file_load_contents_finish (GFile
*file
,
7289 return g_file_load_partial_contents_finish (file
,
7298 * g_file_replace_contents:
7299 * @file: input #GFile
7300 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7301 * @length: the length of @contents in bytes
7302 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7304 * @make_backup: %TRUE if a backup should be created
7305 * @flags: a set of #GFileCreateFlags
7306 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7307 * for the document. This should be freed with g_free() when no longer
7309 * @cancellable: optional #GCancellable object, %NULL to ignore
7310 * @error: a #GError, or %NULL
7312 * Replaces the contents of @file with @contents of @length bytes.
7314 * If @etag is specified (not %NULL), any existing file must have that etag,
7315 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7317 * If @make_backup is %TRUE, this function will attempt to make a backup
7318 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7319 * file contents in the safest way possible. For example, atomic renames are
7320 * used when replacing local files’ contents.
7322 * If @cancellable is not %NULL, then the operation can be cancelled by
7323 * triggering the cancellable object from another thread. If the operation
7324 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7326 * The returned @new_etag can be used to verify that the file hasn't
7327 * changed the next time it is saved over.
7329 * Returns: %TRUE if successful. If an error has occurred, this function
7330 * will return %FALSE and set @error appropriately if present.
7333 g_file_replace_contents (GFile
*file
,
7334 const char *contents
,
7337 gboolean make_backup
,
7338 GFileCreateFlags flags
,
7340 GCancellable
*cancellable
,
7343 GFileOutputStream
*out
;
7344 gsize pos
, remainder
;
7348 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7349 g_return_val_if_fail (contents
!= NULL
, FALSE
);
7351 out
= g_file_replace (file
, etag
, make_backup
, flags
, cancellable
, error
);
7357 while (remainder
> 0 &&
7358 (res
= g_output_stream_write (G_OUTPUT_STREAM (out
),
7360 MIN (remainder
, GET_CONTENT_BLOCK_SIZE
),
7368 if (remainder
> 0 && res
< 0)
7370 /* Ignore errors on close */
7371 g_output_stream_close (G_OUTPUT_STREAM (out
), cancellable
, NULL
);
7372 g_object_unref (out
);
7374 /* error is set already */
7378 ret
= g_output_stream_close (G_OUTPUT_STREAM (out
), cancellable
, error
);
7381 *new_etag
= g_file_output_stream_get_etag (out
);
7383 g_object_unref (out
);
7394 } ReplaceContentsData
;
7397 replace_contents_data_free (ReplaceContentsData
*data
)
7399 g_bytes_unref (data
->content
);
7400 g_free (data
->etag
);
7405 replace_contents_close_callback (GObject
*obj
,
7406 GAsyncResult
*close_res
,
7409 GOutputStream
*stream
= G_OUTPUT_STREAM (obj
);
7410 ReplaceContentsData
*data
= user_data
;
7412 /* Ignore errors here, we're only reading anyway */
7413 g_output_stream_close_finish (stream
, close_res
, NULL
);
7414 g_object_unref (stream
);
7418 data
->etag
= g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream
));
7419 g_task_return_boolean (data
->task
, TRUE
);
7421 g_object_unref (data
->task
);
7425 replace_contents_write_callback (GObject
*obj
,
7426 GAsyncResult
*read_res
,
7429 GOutputStream
*stream
= G_OUTPUT_STREAM (obj
);
7430 ReplaceContentsData
*data
= user_data
;
7431 GError
*error
= NULL
;
7434 write_size
= g_output_stream_write_finish (stream
, read_res
, &error
);
7436 if (write_size
<= 0)
7438 /* Error or EOF, close the file */
7441 data
->failed
= TRUE
;
7442 g_task_return_error (data
->task
, error
);
7444 g_output_stream_close_async (stream
, 0,
7445 g_task_get_cancellable (data
->task
),
7446 replace_contents_close_callback
, data
);
7448 else if (write_size
> 0)
7450 const gchar
*content
;
7453 content
= g_bytes_get_data (data
->content
, &length
);
7454 data
->pos
+= write_size
;
7456 if (data
->pos
>= length
)
7457 g_output_stream_close_async (stream
, 0,
7458 g_task_get_cancellable (data
->task
),
7459 replace_contents_close_callback
, data
);
7461 g_output_stream_write_async (stream
,
7462 content
+ data
->pos
,
7465 g_task_get_cancellable (data
->task
),
7466 replace_contents_write_callback
,
7472 replace_contents_open_callback (GObject
*obj
,
7473 GAsyncResult
*open_res
,
7476 GFile
*file
= G_FILE (obj
);
7477 GFileOutputStream
*stream
;
7478 ReplaceContentsData
*data
= user_data
;
7479 GError
*error
= NULL
;
7481 stream
= g_file_replace_finish (file
, open_res
, &error
);
7485 const gchar
*content
;
7488 content
= g_bytes_get_data (data
->content
, &length
);
7489 g_output_stream_write_async (G_OUTPUT_STREAM (stream
),
7490 content
+ data
->pos
,
7493 g_task_get_cancellable (data
->task
),
7494 replace_contents_write_callback
,
7499 g_task_return_error (data
->task
, error
);
7500 g_object_unref (data
->task
);
7505 * g_file_replace_contents_async:
7506 * @file: input #GFile
7507 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7508 * @length: the length of @contents in bytes
7509 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7510 * @make_backup: %TRUE if a backup should be created
7511 * @flags: a set of #GFileCreateFlags
7512 * @cancellable: optional #GCancellable object, %NULL to ignore
7513 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7514 * @user_data: the data to pass to callback function
7516 * Starts an asynchronous replacement of @file with the given
7517 * @contents of @length bytes. @etag will replace the document's
7518 * current entity tag.
7520 * When this operation has completed, @callback will be called with
7521 * @user_user data, and the operation can be finalized with
7522 * g_file_replace_contents_finish().
7524 * If @cancellable is not %NULL, then the operation can be cancelled by
7525 * triggering the cancellable object from another thread. If the operation
7526 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7528 * If @make_backup is %TRUE, this function will attempt to
7529 * make a backup of @file.
7531 * Note that no copy of @content will be made, so it must stay valid
7532 * until @callback is called. See g_file_replace_contents_bytes_async()
7533 * for a #GBytes version that will automatically hold a reference to the
7534 * contents (without copying) for the duration of the call.
7537 g_file_replace_contents_async (GFile
*file
,
7538 const char *contents
,
7541 gboolean make_backup
,
7542 GFileCreateFlags flags
,
7543 GCancellable
*cancellable
,
7544 GAsyncReadyCallback callback
,
7549 bytes
= g_bytes_new_static (contents
, length
);
7550 g_file_replace_contents_bytes_async (file
, bytes
, etag
, make_backup
, flags
,
7551 cancellable
, callback
, user_data
);
7552 g_bytes_unref (bytes
);
7556 * g_file_replace_contents_bytes_async:
7557 * @file: input #GFile
7558 * @contents: a #GBytes
7559 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7560 * @make_backup: %TRUE if a backup should be created
7561 * @flags: a set of #GFileCreateFlags
7562 * @cancellable: optional #GCancellable object, %NULL to ignore
7563 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7564 * @user_data: the data to pass to callback function
7566 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7567 * This function will keep a ref on @contents until the operation is done.
7568 * Unlike g_file_replace_contents_async() this allows forgetting about the
7569 * content without waiting for the callback.
7571 * When this operation has completed, @callback will be called with
7572 * @user_user data, and the operation can be finalized with
7573 * g_file_replace_contents_finish().
7578 g_file_replace_contents_bytes_async (GFile
*file
,
7581 gboolean make_backup
,
7582 GFileCreateFlags flags
,
7583 GCancellable
*cancellable
,
7584 GAsyncReadyCallback callback
,
7587 ReplaceContentsData
*data
;
7589 g_return_if_fail (G_IS_FILE (file
));
7590 g_return_if_fail (contents
!= NULL
);
7592 data
= g_new0 (ReplaceContentsData
, 1);
7594 data
->content
= g_bytes_ref (contents
);
7596 data
->task
= g_task_new (file
, cancellable
, callback
, user_data
);
7597 g_task_set_source_tag (data
->task
, g_file_replace_contents_bytes_async
);
7598 g_task_set_task_data (data
->task
, data
, (GDestroyNotify
)replace_contents_data_free
);
7600 g_file_replace_async (file
,
7605 g_task_get_cancellable (data
->task
),
7606 replace_contents_open_callback
,
7611 * g_file_replace_contents_finish:
7612 * @file: input #GFile
7613 * @res: a #GAsyncResult
7614 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7615 * for the document. This should be freed with g_free() when it is no
7616 * longer needed, or %NULL
7617 * @error: a #GError, or %NULL
7619 * Finishes an asynchronous replace of the given @file. See
7620 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7621 * tag for the document, if present.
7623 * Returns: %TRUE on success, %FALSE on failure.
7626 g_file_replace_contents_finish (GFile
*file
,
7632 ReplaceContentsData
*data
;
7634 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7635 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
7637 task
= G_TASK (res
);
7639 if (!g_task_propagate_boolean (task
, error
))
7642 data
= g_task_get_task_data (task
);
7646 *new_etag
= data
->etag
;
7647 data
->etag
= NULL
; /* Take ownership */
7654 g_file_real_measure_disk_usage (GFile
*file
,
7655 GFileMeasureFlags flags
,
7656 GCancellable
*cancellable
,
7657 GFileMeasureProgressCallback progress_callback
,
7658 gpointer progress_data
,
7659 guint64
*disk_usage
,
7664 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
7665 "Operation not supported for the current backend.");
7671 GFileMeasureFlags flags
;
7672 GFileMeasureProgressCallback progress_callback
;
7673 gpointer progress_data
;
7685 GFileMeasureProgressCallback callback
;
7688 guint64 current_size
;
7694 measure_disk_usage_invoke_progress (gpointer user_data
)
7696 MeasureProgress
*progress
= user_data
;
7698 (* progress
->callback
) (progress
->reporting
,
7699 progress
->current_size
, progress
->num_dirs
, progress
->num_files
,
7700 progress
->user_data
);
7706 measure_disk_usage_progress (gboolean reporting
,
7707 guint64 current_size
,
7712 MeasureProgress progress
;
7713 GTask
*task
= user_data
;
7714 MeasureTaskData
*data
;
7716 data
= g_task_get_task_data (task
);
7718 progress
.callback
= data
->progress_callback
;
7719 progress
.user_data
= data
->progress_data
;
7720 progress
.reporting
= reporting
;
7721 progress
.current_size
= current_size
;
7722 progress
.num_dirs
= num_dirs
;
7723 progress
.num_files
= num_files
;
7725 g_main_context_invoke_full (g_task_get_context (task
),
7726 g_task_get_priority (task
),
7727 measure_disk_usage_invoke_progress
,
7728 g_memdup (&progress
, sizeof progress
),
7733 measure_disk_usage_thread (GTask
*task
,
7734 gpointer source_object
,
7736 GCancellable
*cancellable
)
7738 MeasureTaskData
*data
= task_data
;
7739 GError
*error
= NULL
;
7740 MeasureResult result
= { 0, };
7742 if (g_file_measure_disk_usage (source_object
, data
->flags
, cancellable
,
7743 data
->progress_callback
? measure_disk_usage_progress
: NULL
, task
,
7744 &result
.disk_usage
, &result
.num_dirs
, &result
.num_files
,
7746 g_task_return_pointer (task
, g_memdup (&result
, sizeof result
), g_free
);
7748 g_task_return_error (task
, error
);
7752 g_file_real_measure_disk_usage_async (GFile
*file
,
7753 GFileMeasureFlags flags
,
7755 GCancellable
*cancellable
,
7756 GFileMeasureProgressCallback progress_callback
,
7757 gpointer progress_data
,
7758 GAsyncReadyCallback callback
,
7761 MeasureTaskData data
;
7765 data
.progress_callback
= progress_callback
;
7766 data
.progress_data
= progress_data
;
7768 task
= g_task_new (file
, cancellable
, callback
, user_data
);
7769 g_task_set_source_tag (task
, g_file_real_measure_disk_usage_async
);
7770 g_task_set_task_data (task
, g_memdup (&data
, sizeof data
), g_free
);
7771 g_task_set_priority (task
, io_priority
);
7773 g_task_run_in_thread (task
, measure_disk_usage_thread
);
7774 g_object_unref (task
);
7778 g_file_real_measure_disk_usage_finish (GFile
*file
,
7779 GAsyncResult
*result
,
7780 guint64
*disk_usage
,
7785 MeasureResult
*measure_result
;
7787 g_return_val_if_fail (g_task_is_valid (result
, file
), FALSE
);
7789 measure_result
= g_task_propagate_pointer (G_TASK (result
), error
);
7791 if (measure_result
== NULL
)
7795 *disk_usage
= measure_result
->disk_usage
;
7798 *num_dirs
= measure_result
->num_dirs
;
7801 *num_files
= measure_result
->num_files
;
7803 g_free (measure_result
);
7809 * g_file_measure_disk_usage:
7811 * @flags: #GFileMeasureFlags
7812 * @cancellable: (nullable): optional #GCancellable
7813 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7814 * @progress_data: user_data for @progress_callback
7815 * @disk_usage: (out) (optional): the number of bytes of disk space used
7816 * @num_dirs: (out) (optional): the number of directories encountered
7817 * @num_files: (out) (optional): the number of non-directories encountered
7818 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7820 * Recursively measures the disk usage of @file.
7822 * This is essentially an analog of the 'du' command, but it also
7823 * reports the number of directories and non-directory files encountered
7824 * (including things like symbolic links).
7826 * By default, errors are only reported against the toplevel file
7827 * itself. Errors found while recursing are silently ignored, unless
7828 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7830 * The returned size, @disk_usage, is in bytes and should be formatted
7831 * with g_format_size() in order to get something reasonable for showing
7832 * in a user interface.
7834 * @progress_callback and @progress_data can be given to request
7835 * periodic progress updates while scanning. See the documentation for
7836 * #GFileMeasureProgressCallback for information about when and how the
7837 * callback will be invoked.
7839 * Returns: %TRUE if successful, with the out parameters set.
7840 * %FALSE otherwise, with @error set.
7845 g_file_measure_disk_usage (GFile
*file
,
7846 GFileMeasureFlags flags
,
7847 GCancellable
*cancellable
,
7848 GFileMeasureProgressCallback progress_callback
,
7849 gpointer progress_data
,
7850 guint64
*disk_usage
,
7855 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7856 g_return_val_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
), FALSE
);
7857 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
7859 return G_FILE_GET_IFACE (file
)->measure_disk_usage (file
, flags
, cancellable
,
7860 progress_callback
, progress_data
,
7861 disk_usage
, num_dirs
, num_files
,
7866 * g_file_measure_disk_usage_async:
7868 * @flags: #GFileMeasureFlags
7869 * @io_priority: the [I/O priority][io-priority] of the request
7870 * @cancellable: (nullable): optional #GCancellable
7871 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7872 * @progress_data: user_data for @progress_callback
7873 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7874 * @user_data: the data to pass to callback function
7876 * Recursively measures the disk usage of @file.
7878 * This is the asynchronous version of g_file_measure_disk_usage(). See
7879 * there for more information.
7884 g_file_measure_disk_usage_async (GFile
*file
,
7885 GFileMeasureFlags flags
,
7887 GCancellable
*cancellable
,
7888 GFileMeasureProgressCallback progress_callback
,
7889 gpointer progress_data
,
7890 GAsyncReadyCallback callback
,
7893 g_return_if_fail (G_IS_FILE (file
));
7894 g_return_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
));
7896 G_FILE_GET_IFACE (file
)->measure_disk_usage_async (file
, flags
, io_priority
, cancellable
,
7897 progress_callback
, progress_data
,
7898 callback
, user_data
);
7902 * g_file_measure_disk_usage_finish:
7904 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7905 * @disk_usage: (out) (optional): the number of bytes of disk space used
7906 * @num_dirs: (out) (optional): the number of directories encountered
7907 * @num_files: (out) (optional): the number of non-directories encountered
7908 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7910 * Collects the results from an earlier call to
7911 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7914 * Returns: %TRUE if successful, with the out parameters set.
7915 * %FALSE otherwise, with @error set.
7920 g_file_measure_disk_usage_finish (GFile
*file
,
7921 GAsyncResult
*result
,
7922 guint64
*disk_usage
,
7927 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7928 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
7930 return G_FILE_GET_IFACE (file
)->measure_disk_usage_finish (file
, result
, disk_usage
, num_dirs
, num_files
, error
);
7934 * g_file_start_mountable:
7935 * @file: input #GFile
7936 * @flags: flags affecting the operation
7937 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7938 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7939 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7940 * @user_data: the data to pass to callback function
7942 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7943 * Using @start_operation, you can request callbacks when, for instance,
7944 * passwords are needed during authentication.
7946 * If @cancellable is not %NULL, then the operation can be cancelled by
7947 * triggering the cancellable object from another thread. If the operation
7948 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7950 * When the operation is finished, @callback will be called.
7951 * You can then call g_file_mount_mountable_finish() to get
7952 * the result of the operation.
7957 g_file_start_mountable (GFile
*file
,
7958 GDriveStartFlags flags
,
7959 GMountOperation
*start_operation
,
7960 GCancellable
*cancellable
,
7961 GAsyncReadyCallback callback
,
7966 g_return_if_fail (G_IS_FILE (file
));
7968 iface
= G_FILE_GET_IFACE (file
);
7970 if (iface
->start_mountable
== NULL
)
7972 g_task_report_new_error (file
, callback
, user_data
,
7973 g_file_start_mountable
,
7974 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
7975 _("Operation not supported"));
7979 (* iface
->start_mountable
) (file
,
7988 * g_file_start_mountable_finish:
7989 * @file: input #GFile
7990 * @result: a #GAsyncResult
7991 * @error: a #GError, or %NULL
7993 * Finishes a start operation. See g_file_start_mountable() for details.
7995 * Finish an asynchronous start operation that was started
7996 * with g_file_start_mountable().
7998 * Returns: %TRUE if the operation finished successfully. %FALSE
8004 g_file_start_mountable_finish (GFile
*file
,
8005 GAsyncResult
*result
,
8010 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8011 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8013 if (g_async_result_legacy_propagate_error (result
, error
))
8015 else if (g_async_result_is_tagged (result
, g_file_start_mountable
))
8016 return g_task_propagate_boolean (G_TASK (result
), error
);
8018 iface
= G_FILE_GET_IFACE (file
);
8019 return (* iface
->start_mountable_finish
) (file
, result
, error
);
8023 * g_file_stop_mountable:
8024 * @file: input #GFile
8025 * @flags: flags affecting the operation
8026 * @mount_operation: (nullable): a #GMountOperation,
8027 * or %NULL to avoid user interaction.
8028 * @cancellable: (nullable): optional #GCancellable object,
8030 * @callback: (nullable): a #GAsyncReadyCallback to call
8031 * when the request is satisfied, or %NULL
8032 * @user_data: the data to pass to callback function
8034 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8036 * If @cancellable is not %NULL, then the operation can be cancelled by
8037 * triggering the cancellable object from another thread. If the operation
8038 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8040 * When the operation is finished, @callback will be called.
8041 * You can then call g_file_stop_mountable_finish() to get
8042 * the result of the operation.
8047 g_file_stop_mountable (GFile
*file
,
8048 GMountUnmountFlags flags
,
8049 GMountOperation
*mount_operation
,
8050 GCancellable
*cancellable
,
8051 GAsyncReadyCallback callback
,
8056 g_return_if_fail (G_IS_FILE (file
));
8058 iface
= G_FILE_GET_IFACE (file
);
8060 if (iface
->stop_mountable
== NULL
)
8062 g_task_report_new_error (file
, callback
, user_data
,
8063 g_file_stop_mountable
,
8064 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
8065 _("Operation not supported"));
8069 (* iface
->stop_mountable
) (file
,
8078 * g_file_stop_mountable_finish:
8079 * @file: input #GFile
8080 * @result: a #GAsyncResult
8081 * @error: a #GError, or %NULL
8083 * Finishes an stop operation, see g_file_stop_mountable() for details.
8085 * Finish an asynchronous stop operation that was started
8086 * with g_file_stop_mountable().
8088 * Returns: %TRUE if the operation finished successfully.
8094 g_file_stop_mountable_finish (GFile
*file
,
8095 GAsyncResult
*result
,
8100 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8101 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8103 if (g_async_result_legacy_propagate_error (result
, error
))
8105 else if (g_async_result_is_tagged (result
, g_file_stop_mountable
))
8106 return g_task_propagate_boolean (G_TASK (result
), error
);
8108 iface
= G_FILE_GET_IFACE (file
);
8109 return (* iface
->stop_mountable_finish
) (file
, result
, error
);
8113 * g_file_poll_mountable:
8114 * @file: input #GFile
8115 * @cancellable: optional #GCancellable object, %NULL to ignore
8116 * @callback: (nullable): a #GAsyncReadyCallback to call
8117 * when the request is satisfied, or %NULL
8118 * @user_data: the data to pass to callback function
8120 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8122 * If @cancellable is not %NULL, then the operation can be cancelled by
8123 * triggering the cancellable object from another thread. If the operation
8124 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8126 * When the operation is finished, @callback will be called.
8127 * You can then call g_file_mount_mountable_finish() to get
8128 * the result of the operation.
8133 g_file_poll_mountable (GFile
*file
,
8134 GCancellable
*cancellable
,
8135 GAsyncReadyCallback callback
,
8140 g_return_if_fail (G_IS_FILE (file
));
8142 iface
= G_FILE_GET_IFACE (file
);
8144 if (iface
->poll_mountable
== NULL
)
8146 g_task_report_new_error (file
, callback
, user_data
,
8147 g_file_poll_mountable
,
8148 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
8149 _("Operation not supported"));
8153 (* iface
->poll_mountable
) (file
,
8160 * g_file_poll_mountable_finish:
8161 * @file: input #GFile
8162 * @result: a #GAsyncResult
8163 * @error: a #GError, or %NULL
8165 * Finishes a poll operation. See g_file_poll_mountable() for details.
8167 * Finish an asynchronous poll operation that was polled
8168 * with g_file_poll_mountable().
8170 * Returns: %TRUE if the operation finished successfully. %FALSE
8176 g_file_poll_mountable_finish (GFile
*file
,
8177 GAsyncResult
*result
,
8182 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8183 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8185 if (g_async_result_legacy_propagate_error (result
, error
))
8187 else if (g_async_result_is_tagged (result
, g_file_poll_mountable
))
8188 return g_task_propagate_boolean (G_TASK (result
), error
);
8190 iface
= G_FILE_GET_IFACE (file
);
8191 return (* iface
->poll_mountable_finish
) (file
, result
, error
);
8195 * g_file_supports_thread_contexts:
8198 * Checks if @file supports
8199 * [thread-default contexts][g-main-context-push-thread-default-context].
8200 * If this returns %FALSE, you cannot perform asynchronous operations on
8201 * @file in a thread that has a thread-default context.
8203 * Returns: Whether or not @file supports thread-default contexts.
8208 g_file_supports_thread_contexts (GFile
*file
)
8212 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8214 iface
= G_FILE_GET_IFACE (file
);
8215 return iface
->supports_thread_contexts
;
8219 * g_file_load_bytes:
8221 * @cancellable: (nullable): a #GCancellable or %NULL
8222 * @etag_out: (out) (nullable) (optional): a location to place the current
8223 * entity tag for the file, or %NULL if the entity tag is not needed
8224 * @error: a location for a #GError or %NULL
8226 * Loads the contents of @file and returns it as #GBytes.
8228 * If @file is a resource:// based URI, the resulting bytes will reference the
8229 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8230 * g_file_load_contents() and g_bytes_new_take().
8232 * For resources, @etag_out will be set to %NULL.
8234 * The data contained in the resulting #GBytes is always zero-terminated, but
8235 * this is not included in the #GBytes length. The resulting #GBytes should be
8236 * freed with g_bytes_unref() when no longer in use.
8238 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8243 g_file_load_bytes (GFile
*file
,
8244 GCancellable
*cancellable
,
8251 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
8252 g_return_val_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
), NULL
);
8253 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
8255 if (etag_out
!= NULL
)
8258 if (g_file_has_uri_scheme (file
, "resource"))
8261 gchar
*uri
, *unescaped
;
8263 uri
= g_file_get_uri (file
);
8264 unescaped
= g_uri_unescape_string (uri
+ strlen ("resource://"), NULL
);
8267 bytes
= g_resources_lookup_data (unescaped
, G_RESOURCE_LOOKUP_FLAGS_NONE
, error
);
8273 /* contents is guaranteed to be \0 terminated */
8274 if (g_file_load_contents (file
, cancellable
, &contents
, &len
, etag_out
, error
))
8275 return g_bytes_new_take (g_steal_pointer (&contents
), len
);
8281 g_file_load_bytes_cb (GObject
*object
,
8282 GAsyncResult
*result
,
8285 GFile
*file
= G_FILE (object
);
8286 GTask
*task
= user_data
;
8287 GError
*error
= NULL
;
8289 gchar
*contents
= NULL
;
8292 g_file_load_contents_finish (file
, result
, &contents
, &len
, &etag
, &error
);
8293 g_task_set_task_data (task
, g_steal_pointer (&etag
), g_free
);
8296 g_task_return_error (task
, g_steal_pointer (&error
));
8298 g_task_return_pointer (task
,
8299 g_bytes_new_take (g_steal_pointer (&contents
), len
),
8300 (GDestroyNotify
)g_bytes_unref
);
8302 g_object_unref (task
);
8306 * g_file_load_bytes_async:
8308 * @cancellable: (nullable): a #GCancellable or %NULL
8309 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8310 * request is satisfied
8311 * @user_data: (closure): the data to pass to callback function
8313 * Asynchronously loads the contents of @file as #GBytes.
8315 * If @file is a resource:// based URI, the resulting bytes will reference the
8316 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8317 * g_file_load_contents_async() and g_bytes_new_take().
8319 * @callback should call g_file_load_bytes_finish() to get the result of this
8320 * asynchronous operation.
8322 * See g_file_load_bytes() for more information.
8327 g_file_load_bytes_async (GFile
*file
,
8328 GCancellable
*cancellable
,
8329 GAsyncReadyCallback callback
,
8332 GError
*error
= NULL
;
8336 g_return_if_fail (G_IS_FILE (file
));
8337 g_return_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
));
8339 task
= g_task_new (file
, cancellable
, callback
, user_data
);
8340 g_task_set_source_tag (task
, g_file_load_bytes_async
);
8342 if (!g_file_has_uri_scheme (file
, "resource"))
8344 g_file_load_contents_async (file
,
8346 g_file_load_bytes_cb
,
8347 g_steal_pointer (&task
));
8351 bytes
= g_file_load_bytes (file
, cancellable
, NULL
, &error
);
8354 g_task_return_error (task
, g_steal_pointer (&error
));
8356 g_task_return_pointer (task
,
8357 g_steal_pointer (&bytes
),
8358 (GDestroyNotify
)g_bytes_unref
);
8360 g_object_unref (task
);
8364 * g_file_load_bytes_finish:
8366 * @result: a #GAsyncResult provided to the callback
8367 * @etag_out: (out) (nullable) (optional): a location to place the current
8368 * entity tag for the file, or %NULL if the entity tag is not needed
8369 * @error: a location for a #GError, or %NULL
8371 * Completes an asynchronous request to g_file_load_bytes_async().
8373 * For resources, @etag_out will be set to %NULL.
8375 * The data contained in the resulting #GBytes is always zero-terminated, but
8376 * this is not included in the #GBytes length. The resulting #GBytes should be
8377 * freed with g_bytes_unref() when no longer in use.
8379 * See g_file_load_bytes() for more information.
8381 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8386 g_file_load_bytes_finish (GFile
*file
,
8387 GAsyncResult
*result
,
8393 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
8394 g_return_val_if_fail (G_IS_TASK (result
), NULL
);
8395 g_return_val_if_fail (g_task_is_valid (G_TASK (result
), file
), NULL
);
8396 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
8398 bytes
= g_task_propagate_pointer (G_TASK (result
), error
);
8400 if (etag_out
!= NULL
)
8401 *etag_out
= g_strdup (g_task_get_task_data (G_TASK (result
)));