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 is 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 #if defined(F_SETPIPE_SZ) && defined(F_GETPIPE_SZ)
3016 /* Try a 1MiB buffer for improved throughput. If that fails, use the default
3017 * pipe size. See: https://bugzilla.gnome.org/791457 */
3018 buffer_size
= fcntl (buffer
[1], F_SETPIPE_SZ
, 1024 * 1024);
3019 if (buffer_size
<= 0)
3022 buffer_size
= fcntl (buffer
[1], F_GETPIPE_SZ
);
3025 if (buffer_size
<= 0)
3027 g_set_error (error
, G_IO_ERROR
, g_io_error_from_errno (errsv
),
3028 _("Error splicing file: %s"), g_strerror (errsv
));
3034 /* If #F_GETPIPE_SZ isn’t available, assume we’re on Linux < 2.6.35,
3035 * but ≥ 2.6.11, meaning the pipe capacity is 64KiB. Ignore the possibility of
3036 * running on Linux < 2.6.11 (where the capacity was the system page size,
3037 * typically 4KiB) because it’s ancient. See pipe(7). */
3038 buffer_size
= 1024 * 64;
3041 g_assert (buffer_size
> 0);
3044 /* avoid performance impact of querying total size when it's not needed */
3045 if (progress_callback
)
3049 if (fstat (fd_in
, &sbuf
) == 0)
3050 total_size
= sbuf
.st_size
;
3053 if (total_size
== -1)
3056 offset_in
= offset_out
= 0;
3063 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3066 if (!do_splice (fd_in
, &offset_in
, buffer
[1], NULL
, buffer_size
, &n_read
, error
))
3077 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3080 if (!do_splice (buffer
[0], NULL
, fd_out
, &offset_out
, n_read
, &n_written
, error
))
3083 n_read
-= n_written
;
3086 if (progress_callback
)
3087 progress_callback (offset_in
, total_size
, progress_callback_data
);
3090 /* Make sure we send full copied size */
3091 if (progress_callback
)
3092 progress_callback (offset_in
, total_size
, progress_callback_data
);
3094 if (!g_close (buffer
[0], error
))
3097 if (!g_close (buffer
[1], error
))
3101 if (buffer
[0] != -1)
3102 (void) g_close (buffer
[0], NULL
);
3103 if (buffer
[1] != -1)
3104 (void) g_close (buffer
[1], NULL
);
3112 btrfs_reflink_with_progress (GInputStream
*in
,
3115 GCancellable
*cancellable
,
3116 GFileProgressCallback progress_callback
,
3117 gpointer progress_callback_data
,
3120 goffset source_size
;
3124 fd_in
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in
));
3125 fd_out
= g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out
));
3127 if (progress_callback
)
3128 source_size
= g_file_info_get_size (info
);
3130 /* Btrfs clone ioctl properties:
3131 * - Works at the inode level
3132 * - Doesn't work with directories
3133 * - Always follows symlinks (source and destination)
3135 * By the time we get here, *in and *out are both regular files */
3136 ret
= ioctl (fd_out
, BTRFS_IOC_CLONE
, fd_in
);
3142 g_set_error_literal (error
, G_IO_ERROR
,
3143 G_IO_ERROR_NOT_SUPPORTED
,
3144 _("Copy (reflink/clone) between mounts is not supported"));
3145 else if (errsv
== EINVAL
)
3146 g_set_error_literal (error
, G_IO_ERROR
,
3147 G_IO_ERROR_NOT_SUPPORTED
,
3148 _("Copy (reflink/clone) is not supported or invalid"));
3150 /* Most probably something odd happened; retry with fallback */
3151 g_set_error_literal (error
, G_IO_ERROR
,
3152 G_IO_ERROR_NOT_SUPPORTED
,
3153 _("Copy (reflink/clone) is not supported or didn’t work"));
3154 /* We retry with fallback for all error cases because Btrfs is currently
3155 * unstable, and so we can't trust it to do clone properly.
3156 * In addition, any hard errors here would cause the same failure in the
3157 * fallback manual copy as well. */
3161 /* Make sure we send full copied size */
3162 if (progress_callback
)
3163 progress_callback (source_size
, source_size
, progress_callback_data
);
3170 file_copy_fallback (GFile
*source
,
3172 GFileCopyFlags flags
,
3173 GCancellable
*cancellable
,
3174 GFileProgressCallback progress_callback
,
3175 gpointer progress_callback_data
,
3178 gboolean ret
= FALSE
;
3179 GFileInputStream
*file_in
= NULL
;
3180 GInputStream
*in
= NULL
;
3181 GOutputStream
*out
= NULL
;
3182 GFileInfo
*info
= NULL
;
3184 char *attrs_to_read
;
3185 gboolean do_set_attributes
= FALSE
;
3187 /* need to know the file type */
3188 info
= g_file_query_info (source
,
3189 G_FILE_ATTRIBUTE_STANDARD_TYPE
"," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET
,
3190 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3196 /* Maybe copy the symlink? */
3197 if ((flags
& G_FILE_COPY_NOFOLLOW_SYMLINKS
) &&
3198 g_file_info_get_file_type (info
) == G_FILE_TYPE_SYMBOLIC_LINK
)
3200 target
= g_file_info_get_symlink_target (info
);
3203 if (!copy_symlink (destination
, flags
, cancellable
, target
, error
))
3209 /* ... else fall back on a regular file copy */
3211 /* Handle "special" files (pipes, device nodes, ...)? */
3212 else if (g_file_info_get_file_type (info
) == G_FILE_TYPE_SPECIAL
)
3214 /* FIXME: could try to recreate device nodes and others? */
3215 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
3216 _("Can’t copy special file"));
3220 /* Everything else should just fall back on a regular copy. */
3222 file_in
= open_source_for_copy (source
, destination
, flags
, cancellable
, error
);
3225 in
= G_INPUT_STREAM (file_in
);
3227 if (!build_attribute_list_for_copy (destination
, flags
, &attrs_to_read
,
3228 cancellable
, error
))
3231 if (attrs_to_read
!= NULL
)
3233 GError
*tmp_error
= NULL
;
3235 /* Ok, ditch the previous lightweight info (on Unix we just
3236 * called lstat()); at this point we gather all the information
3237 * we need about the source from the opened file descriptor.
3239 g_object_unref (info
);
3241 info
= g_file_input_stream_query_info (file_in
, attrs_to_read
,
3242 cancellable
, &tmp_error
);
3245 /* Not all gvfs backends implement query_info_on_read(), we
3246 * can just fall back to the pathname again.
3247 * https://bugzilla.gnome.org/706254
3249 if (g_error_matches (tmp_error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3251 g_clear_error (&tmp_error
);
3252 info
= g_file_query_info (source
, attrs_to_read
, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3253 cancellable
, error
);
3257 g_free (attrs_to_read
);
3258 g_propagate_error (error
, tmp_error
);
3262 g_free (attrs_to_read
);
3266 do_set_attributes
= TRUE
;
3269 /* In the local file path, we pass down the source info which
3270 * includes things like unix::mode, to ensure that the target file
3271 * is not created with different permissions from the source file.
3273 * If a future API like g_file_replace_with_info() is added, switch
3274 * this code to use that.
3276 if (G_IS_LOCAL_FILE (destination
))
3278 if (flags
& G_FILE_COPY_OVERWRITE
)
3279 out
= (GOutputStream
*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination
)),
3281 flags
& G_FILE_COPY_BACKUP
,
3282 G_FILE_CREATE_REPLACE_DESTINATION
,
3284 cancellable
, error
);
3286 out
= (GOutputStream
*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination
)),
3288 cancellable
, error
);
3290 else if (flags
& G_FILE_COPY_OVERWRITE
)
3292 out
= (GOutputStream
*)g_file_replace (destination
,
3294 flags
& G_FILE_COPY_BACKUP
,
3295 G_FILE_CREATE_REPLACE_DESTINATION
,
3296 cancellable
, error
);
3300 out
= (GOutputStream
*)g_file_create (destination
, 0, cancellable
, error
);
3307 if (G_IS_FILE_DESCRIPTOR_BASED (in
) && G_IS_FILE_DESCRIPTOR_BASED (out
))
3309 GError
*reflink_err
= NULL
;
3311 if (!btrfs_reflink_with_progress (in
, out
, info
, cancellable
,
3312 progress_callback
, progress_callback_data
,
3315 if (g_error_matches (reflink_err
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3317 g_clear_error (&reflink_err
);
3321 g_propagate_error (error
, reflink_err
);
3334 if (G_IS_FILE_DESCRIPTOR_BASED (in
) && G_IS_FILE_DESCRIPTOR_BASED (out
))
3336 GError
*splice_err
= NULL
;
3338 if (!splice_stream_with_progress (in
, out
, cancellable
,
3339 progress_callback
, progress_callback_data
,
3342 if (g_error_matches (splice_err
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
))
3344 g_clear_error (&splice_err
);
3348 g_propagate_error (error
, splice_err
);
3361 /* A plain read/write loop */
3362 if (!copy_stream_with_progress (in
, out
, source
, cancellable
,
3363 progress_callback
, progress_callback_data
,
3371 /* Don't care about errors in source here */
3372 (void) g_input_stream_close (in
, cancellable
, NULL
);
3373 g_object_unref (in
);
3378 /* But write errors on close are bad! */
3379 if (!g_output_stream_close (out
, cancellable
, ret
? error
: NULL
))
3381 g_object_unref (out
);
3384 /* Ignore errors here. Failure to copy metadata is not a hard error */
3385 /* TODO: set these attributes /before/ we do the rename() on Unix */
3386 if (ret
&& do_set_attributes
)
3388 g_file_set_attributes_from_info (destination
,
3390 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
,
3395 g_clear_object (&info
);
3402 * @source: input #GFile
3403 * @destination: destination #GFile
3404 * @flags: set of #GFileCopyFlags
3405 * @cancellable: (nullable): optional #GCancellable object,
3407 * @progress_callback: (nullable) (scope call): function to callback with
3408 * progress information, or %NULL if progress information is not needed
3409 * @progress_callback_data: (closure): user data to pass to @progress_callback
3410 * @error: #GError to set on error, or %NULL
3412 * Copies the file @source to the location specified by @destination.
3413 * Can not handle recursive copies of directories.
3415 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3416 * existing @destination file is overwritten.
3418 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3419 * will be copied as symlinks, otherwise the target of the
3420 * @source symlink will be copied.
3422 * If the flag #G_FILE_COPY_ALL_METADATA is specified then all the metadata
3423 * that is possible to copy is copied, not just the default subset (which,
3424 * for instance, does not include the owner, see #GFileInfo).
3426 * If @cancellable is not %NULL, then the operation can be cancelled by
3427 * triggering the cancellable object from another thread. If the operation
3428 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3430 * If @progress_callback is not %NULL, then the operation can be monitored
3431 * by setting this to a #GFileProgressCallback function.
3432 * @progress_callback_data will be passed to this function. It is guaranteed
3433 * that this callback will be called after all data has been transferred with
3434 * the total number of bytes copied during the operation.
3436 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3437 * is returned, independent on the status of the @destination.
3439 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3440 * the error %G_IO_ERROR_EXISTS is returned.
3442 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3443 * error is returned. If trying to overwrite a directory with a directory the
3444 * %G_IO_ERROR_WOULD_MERGE error is returned.
3446 * If the source is a directory and the target does not exist, or
3447 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3448 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3450 * If you are interested in copying the #GFile object itself (not the on-disk
3451 * file), see g_file_dup().
3453 * Returns: %TRUE on success, %FALSE otherwise.
3456 g_file_copy (GFile
*source
,
3458 GFileCopyFlags flags
,
3459 GCancellable
*cancellable
,
3460 GFileProgressCallback progress_callback
,
3461 gpointer progress_callback_data
,
3468 g_return_val_if_fail (G_IS_FILE (source
), FALSE
);
3469 g_return_val_if_fail (G_IS_FILE (destination
), FALSE
);
3471 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3474 iface
= G_FILE_GET_IFACE (destination
);
3478 res
= (* iface
->copy
) (source
, destination
,
3480 progress_callback
, progress_callback_data
,
3486 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3488 g_propagate_error (error
, my_error
);
3492 g_clear_error (&my_error
);
3495 /* If the types are different, and the destination method failed
3496 * also try the source method
3498 if (G_OBJECT_TYPE (source
) != G_OBJECT_TYPE (destination
))
3500 iface
= G_FILE_GET_IFACE (source
);
3505 res
= (* iface
->copy
) (source
, destination
,
3507 progress_callback
, progress_callback_data
,
3513 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3515 g_propagate_error (error
, my_error
);
3519 g_clear_error (&my_error
);
3523 return file_copy_fallback (source
, destination
, flags
, cancellable
,
3524 progress_callback
, progress_callback_data
,
3529 * g_file_copy_async:
3530 * @source: input #GFile
3531 * @destination: destination #GFile
3532 * @flags: set of #GFileCopyFlags
3533 * @io_priority: the [I/O priority][io-priority] of the request
3534 * @cancellable: (nullable): optional #GCancellable object,
3536 * @progress_callback: (nullable) (scope notified): function to callback with progress
3537 * information, or %NULL if progress information is not needed
3538 * @progress_callback_data: (closure progress_callback) (nullable): user data to pass to @progress_callback
3539 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
3540 * @user_data: (closure callback): the data to pass to callback function
3542 * Copies the file @source to the location specified by @destination
3543 * asynchronously. For details of the behaviour, see g_file_copy().
3545 * If @progress_callback is not %NULL, then that function that will be called
3546 * just like in g_file_copy(). The callback will run in the default main context
3547 * of the thread calling g_file_copy_async() — the same context as @callback is
3550 * When the operation is finished, @callback will be called. You can then call
3551 * g_file_copy_finish() to get the result of the operation.
3554 g_file_copy_async (GFile
*source
,
3556 GFileCopyFlags flags
,
3558 GCancellable
*cancellable
,
3559 GFileProgressCallback progress_callback
,
3560 gpointer progress_callback_data
,
3561 GAsyncReadyCallback callback
,
3566 g_return_if_fail (G_IS_FILE (source
));
3567 g_return_if_fail (G_IS_FILE (destination
));
3569 iface
= G_FILE_GET_IFACE (source
);
3570 (* iface
->copy_async
) (source
,
3576 progress_callback_data
,
3582 * g_file_copy_finish:
3583 * @file: input #GFile
3584 * @res: a #GAsyncResult
3585 * @error: a #GError, or %NULL
3587 * Finishes copying the file started with g_file_copy_async().
3589 * Returns: a %TRUE on success, %FALSE on error.
3592 g_file_copy_finish (GFile
*file
,
3598 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3599 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), FALSE
);
3601 if (g_async_result_legacy_propagate_error (res
, error
))
3604 iface
= G_FILE_GET_IFACE (file
);
3605 return (* iface
->copy_finish
) (file
, res
, error
);
3610 * @source: #GFile pointing to the source location
3611 * @destination: #GFile pointing to the destination location
3612 * @flags: set of #GFileCopyFlags
3613 * @cancellable: (nullable): optional #GCancellable object,
3615 * @progress_callback: (nullable) (scope call): #GFileProgressCallback
3616 * function for updates
3617 * @progress_callback_data: (closure): gpointer to user data for
3618 * the callback function
3619 * @error: #GError for returning error conditions, or %NULL
3621 * Tries to move the file or directory @source to the location specified
3622 * by @destination. If native move operations are supported then this is
3623 * used, otherwise a copy + delete fallback is used. The native
3624 * implementation may support moving directories (for instance on moves
3625 * inside the same filesystem), but the fallback code does not.
3627 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3628 * existing @destination file is overwritten.
3630 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3631 * will be copied as symlinks, otherwise the target of the
3632 * @source symlink will be copied.
3634 * If @cancellable is not %NULL, then the operation can be cancelled by
3635 * triggering the cancellable object from another thread. If the operation
3636 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3638 * If @progress_callback is not %NULL, then the operation can be monitored
3639 * by setting this to a #GFileProgressCallback function.
3640 * @progress_callback_data will be passed to this function. It is
3641 * guaranteed that this callback will be called after all data has been
3642 * transferred with the total number of bytes copied during the operation.
3644 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3645 * error is returned, independent on the status of the @destination.
3647 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3648 * then the error %G_IO_ERROR_EXISTS is returned.
3650 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3651 * error is returned. If trying to overwrite a directory with a directory the
3652 * %G_IO_ERROR_WOULD_MERGE error is returned.
3654 * If the source is a directory and the target does not exist, or
3655 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3656 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3657 * move operation isn't available).
3659 * Returns: %TRUE on successful move, %FALSE otherwise.
3662 g_file_move (GFile
*source
,
3664 GFileCopyFlags flags
,
3665 GCancellable
*cancellable
,
3666 GFileProgressCallback progress_callback
,
3667 gpointer progress_callback_data
,
3674 g_return_val_if_fail (G_IS_FILE (source
), FALSE
);
3675 g_return_val_if_fail (G_IS_FILE (destination
), FALSE
);
3677 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3680 iface
= G_FILE_GET_IFACE (destination
);
3684 res
= (* iface
->move
) (source
, destination
,
3686 progress_callback
, progress_callback_data
,
3692 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3694 g_propagate_error (error
, my_error
);
3698 g_clear_error (&my_error
);
3701 /* If the types are different, and the destination method failed
3702 * also try the source method
3704 if (G_OBJECT_TYPE (source
) != G_OBJECT_TYPE (destination
))
3706 iface
= G_FILE_GET_IFACE (source
);
3711 res
= (* iface
->move
) (source
, destination
,
3713 progress_callback
, progress_callback_data
,
3719 if (my_error
->domain
!= G_IO_ERROR
|| my_error
->code
!= G_IO_ERROR_NOT_SUPPORTED
)
3721 g_propagate_error (error
, my_error
);
3725 g_clear_error (&my_error
);
3729 if (flags
& G_FILE_COPY_NO_FALLBACK_FOR_MOVE
)
3731 g_set_error_literal (error
, G_IO_ERROR
,
3732 G_IO_ERROR_NOT_SUPPORTED
,
3733 _("Operation not supported"));
3737 flags
|= G_FILE_COPY_ALL_METADATA
;
3738 if (!g_file_copy (source
, destination
, flags
, cancellable
,
3739 progress_callback
, progress_callback_data
,
3743 return g_file_delete (source
, cancellable
, error
);
3747 * g_file_make_directory:
3748 * @file: input #GFile
3749 * @cancellable: (nullable): optional #GCancellable object,
3751 * @error: a #GError, or %NULL
3753 * Creates a directory. Note that this will only create a child directory
3754 * of the immediate parent directory of the path or URI given by the #GFile.
3755 * To recursively create directories, see g_file_make_directory_with_parents().
3756 * This function will fail if the parent directory does not exist, setting
3757 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3758 * creating directories, this function will fail, setting @error to
3759 * %G_IO_ERROR_NOT_SUPPORTED.
3761 * For a local #GFile the newly created directory will have the default
3762 * (current) ownership and permissions of the current process.
3764 * If @cancellable is not %NULL, then the operation can be cancelled by
3765 * triggering the cancellable object from another thread. If the operation
3766 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3768 * Returns: %TRUE on successful creation, %FALSE otherwise.
3771 g_file_make_directory (GFile
*file
,
3772 GCancellable
*cancellable
,
3777 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3779 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3782 iface
= G_FILE_GET_IFACE (file
);
3784 if (iface
->make_directory
== NULL
)
3786 g_set_error_literal (error
, G_IO_ERROR
,
3787 G_IO_ERROR_NOT_SUPPORTED
,
3788 _("Operation not supported"));
3792 return (* iface
->make_directory
) (file
, cancellable
, error
);
3796 * g_file_make_directory_async:
3797 * @file: input #GFile
3798 * @io_priority: the [I/O priority][io-priority] of the request
3799 * @cancellable: (nullable): optional #GCancellable object,
3801 * @callback: a #GAsyncReadyCallback to call
3802 * when the request is satisfied
3803 * @user_data: the data to pass to callback function
3805 * Asynchronously creates a directory.
3807 * Virtual: make_directory_async
3811 g_file_make_directory_async (GFile
*file
,
3813 GCancellable
*cancellable
,
3814 GAsyncReadyCallback callback
,
3819 g_return_if_fail (G_IS_FILE (file
));
3821 iface
= G_FILE_GET_IFACE (file
);
3822 (* iface
->make_directory_async
) (file
,
3830 * g_file_make_directory_finish:
3831 * @file: input #GFile
3832 * @result: a #GAsyncResult
3833 * @error: a #GError, or %NULL
3835 * Finishes an asynchronous directory creation, started with
3836 * g_file_make_directory_async().
3838 * Virtual: make_directory_finish
3839 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3843 g_file_make_directory_finish (GFile
*file
,
3844 GAsyncResult
*result
,
3849 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3850 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
3852 iface
= G_FILE_GET_IFACE (file
);
3853 return (* iface
->make_directory_finish
) (file
, result
, error
);
3857 * g_file_make_directory_with_parents:
3858 * @file: input #GFile
3859 * @cancellable: (nullable): optional #GCancellable object,
3861 * @error: a #GError, or %NULL
3863 * Creates a directory and any parent directories that may not
3864 * exist similar to 'mkdir -p'. If the file system does not support
3865 * creating directories, this function will fail, setting @error to
3866 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3867 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3868 * the similar g_mkdir_with_parents().
3870 * For a local #GFile the newly created directories will have the default
3871 * (current) ownership and permissions of the current process.
3873 * If @cancellable is not %NULL, then the operation can be cancelled by
3874 * triggering the cancellable object from another thread. If the operation
3875 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3877 * Returns: %TRUE if all directories have been successfully created, %FALSE
3883 g_file_make_directory_with_parents (GFile
*file
,
3884 GCancellable
*cancellable
,
3887 GFile
*work_file
= NULL
;
3888 GList
*list
= NULL
, *l
;
3889 GError
*my_error
= NULL
;
3891 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
3893 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
3896 /* Try for the simple case of not having to create any parent
3897 * directories. If any parent directory needs to be created, this
3898 * call will fail with NOT_FOUND. If that happens, then that value of
3899 * my_error persists into the while loop below.
3901 g_file_make_directory (file
, cancellable
, &my_error
);
3902 if (!g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3905 g_propagate_error (error
, my_error
);
3906 return my_error
== NULL
;
3909 work_file
= g_object_ref (file
);
3911 /* Creates the parent directories as needed. In case any particular
3912 * creation operation fails for lack of other parent directories
3913 * (NOT_FOUND), the directory is added to a list of directories to
3914 * create later, and the value of my_error is retained until the next
3915 * iteration of the loop. After the loop my_error should either be
3916 * empty or contain a real failure condition.
3918 while (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3922 parent_file
= g_file_get_parent (work_file
);
3923 if (parent_file
== NULL
)
3926 g_clear_error (&my_error
);
3927 g_file_make_directory (parent_file
, cancellable
, &my_error
);
3928 /* Another process may have created the directory in between the
3929 * G_IO_ERROR_NOT_FOUND and now
3931 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
))
3932 g_clear_error (&my_error
);
3934 g_object_unref (work_file
);
3935 work_file
= g_object_ref (parent_file
);
3937 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_NOT_FOUND
))
3938 list
= g_list_prepend (list
, parent_file
); /* Transfer ownership of ref */
3940 g_object_unref (parent_file
);
3943 /* All directories should be able to be created now, so an error at
3944 * this point means the whole operation must fail -- except an EXISTS
3945 * error, which means that another process already created the
3946 * directory in between the previous failure and now.
3948 for (l
= list
; my_error
== NULL
&& l
; l
= l
->next
)
3950 g_file_make_directory ((GFile
*) l
->data
, cancellable
, &my_error
);
3951 if (g_error_matches (my_error
, G_IO_ERROR
, G_IO_ERROR_EXISTS
))
3952 g_clear_error (&my_error
);
3956 g_object_unref (work_file
);
3959 while (list
!= NULL
)
3961 g_object_unref ((GFile
*) list
->data
);
3962 list
= g_list_remove (list
, list
->data
);
3965 /* At this point an error in my_error means a that something
3966 * unexpected failed in either of the loops above, so the whole
3967 * operation must fail.
3969 if (my_error
!= NULL
)
3971 g_propagate_error (error
, my_error
);
3975 return g_file_make_directory (file
, cancellable
, error
);
3979 * g_file_make_symbolic_link:
3980 * @file: a #GFile with the name of the symlink to create
3981 * @symlink_value: (type filename): a string with the path for the target
3982 * of the new symlink
3983 * @cancellable: (nullable): optional #GCancellable object,
3987 * Creates a symbolic link named @file which contains the string
3990 * If @cancellable is not %NULL, then the operation can be cancelled by
3991 * triggering the cancellable object from another thread. If the operation
3992 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3994 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3997 g_file_make_symbolic_link (GFile
*file
,
3998 const char *symlink_value
,
3999 GCancellable
*cancellable
,
4004 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4005 g_return_val_if_fail (symlink_value
!= NULL
, FALSE
);
4007 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4010 if (*symlink_value
== '\0')
4012 g_set_error_literal (error
, G_IO_ERROR
,
4013 G_IO_ERROR_INVALID_ARGUMENT
,
4014 _("Invalid symlink value given"));
4018 iface
= G_FILE_GET_IFACE (file
);
4020 if (iface
->make_symbolic_link
== NULL
)
4022 g_set_error_literal (error
, G_IO_ERROR
,
4023 G_IO_ERROR_NOT_SUPPORTED
,
4024 _("Operation not supported"));
4028 return (* iface
->make_symbolic_link
) (file
, symlink_value
, cancellable
, error
);
4033 * @file: input #GFile
4034 * @cancellable: (nullable): optional #GCancellable object,
4036 * @error: a #GError, or %NULL
4038 * Deletes a file. If the @file is a directory, it will only be
4039 * deleted if it is empty. This has the same semantics as g_unlink().
4041 * If @cancellable is not %NULL, then the operation can be cancelled by
4042 * triggering the cancellable object from another thread. If the operation
4043 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4045 * Virtual: delete_file
4046 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4049 g_file_delete (GFile
*file
,
4050 GCancellable
*cancellable
,
4055 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4057 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4060 iface
= G_FILE_GET_IFACE (file
);
4062 if (iface
->delete_file
== NULL
)
4064 g_set_error_literal (error
, G_IO_ERROR
,
4065 G_IO_ERROR_NOT_SUPPORTED
,
4066 _("Operation not supported"));
4070 return (* iface
->delete_file
) (file
, cancellable
, error
);
4074 * g_file_delete_async:
4075 * @file: input #GFile
4076 * @io_priority: the [I/O priority][io-priority] of the request
4077 * @cancellable: (nullable): optional #GCancellable object,
4079 * @callback: a #GAsyncReadyCallback to call
4080 * when the request is satisfied
4081 * @user_data: the data to pass to callback function
4083 * Asynchronously delete a file. If the @file is a directory, it will
4084 * only be deleted if it is empty. This has the same semantics as
4087 * Virtual: delete_file_async
4091 g_file_delete_async (GFile
*file
,
4093 GCancellable
*cancellable
,
4094 GAsyncReadyCallback callback
,
4099 g_return_if_fail (G_IS_FILE (file
));
4101 iface
= G_FILE_GET_IFACE (file
);
4102 (* iface
->delete_file_async
) (file
,
4110 * g_file_delete_finish:
4111 * @file: input #GFile
4112 * @result: a #GAsyncResult
4113 * @error: a #GError, or %NULL
4115 * Finishes deleting a file started with g_file_delete_async().
4117 * Virtual: delete_file_finish
4118 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
4122 g_file_delete_finish (GFile
*file
,
4123 GAsyncResult
*result
,
4128 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4129 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4131 if (g_async_result_legacy_propagate_error (result
, error
))
4134 iface
= G_FILE_GET_IFACE (file
);
4135 return (* iface
->delete_file_finish
) (file
, result
, error
);
4140 * @file: #GFile to send to trash
4141 * @cancellable: (nullable): optional #GCancellable object,
4143 * @error: a #GError, or %NULL
4145 * Sends @file to the "Trashcan", if possible. This is similar to
4146 * deleting it, but the user can recover it before emptying the trashcan.
4147 * Not all file systems support trashing, so this call can return the
4148 * %G_IO_ERROR_NOT_SUPPORTED error.
4150 * If @cancellable is not %NULL, then the operation can be cancelled by
4151 * triggering the cancellable object from another thread. If the operation
4152 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4155 * Returns: %TRUE on successful trash, %FALSE otherwise.
4158 g_file_trash (GFile
*file
,
4159 GCancellable
*cancellable
,
4164 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4166 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4169 iface
= G_FILE_GET_IFACE (file
);
4171 if (iface
->trash
== NULL
)
4173 g_set_error_literal (error
,
4174 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
4175 _("Trash not supported"));
4179 return (* iface
->trash
) (file
, cancellable
, error
);
4183 * g_file_trash_async:
4184 * @file: input #GFile
4185 * @io_priority: the [I/O priority][io-priority] of the request
4186 * @cancellable: (nullable): optional #GCancellable object,
4188 * @callback: a #GAsyncReadyCallback to call
4189 * when the request is satisfied
4190 * @user_data: the data to pass to callback function
4192 * Asynchronously sends @file to the Trash location, if possible.
4194 * Virtual: trash_async
4198 g_file_trash_async (GFile
*file
,
4200 GCancellable
*cancellable
,
4201 GAsyncReadyCallback callback
,
4206 g_return_if_fail (G_IS_FILE (file
));
4208 iface
= G_FILE_GET_IFACE (file
);
4209 (* iface
->trash_async
) (file
,
4217 * g_file_trash_finish:
4218 * @file: input #GFile
4219 * @result: a #GAsyncResult
4220 * @error: a #GError, or %NULL
4222 * Finishes an asynchronous file trashing operation, started with
4223 * g_file_trash_async().
4225 * Virtual: trash_finish
4226 * Returns: %TRUE on successful trash, %FALSE otherwise.
4230 g_file_trash_finish (GFile
*file
,
4231 GAsyncResult
*result
,
4236 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4237 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4239 iface
= G_FILE_GET_IFACE (file
);
4240 return (* iface
->trash_finish
) (file
, result
, error
);
4244 * g_file_set_display_name:
4245 * @file: input #GFile
4246 * @display_name: a string
4247 * @cancellable: (nullable): optional #GCancellable object,
4249 * @error: a #GError, or %NULL
4251 * Renames @file to the specified display name.
4253 * The display name is converted from UTF-8 to the correct encoding
4254 * for the target filesystem if possible and the @file is renamed to this.
4256 * If you want to implement a rename operation in the user interface the
4257 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4258 * initial value in the rename widget, and then the result after editing
4259 * should be passed to g_file_set_display_name().
4261 * On success the resulting converted filename is returned.
4263 * If @cancellable is not %NULL, then the operation can be cancelled by
4264 * triggering the cancellable object from another thread. If the operation
4265 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4267 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4268 * or %NULL if there was an error.
4269 * Free the returned object with g_object_unref().
4272 g_file_set_display_name (GFile
*file
,
4273 const gchar
*display_name
,
4274 GCancellable
*cancellable
,
4279 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4280 g_return_val_if_fail (display_name
!= NULL
, NULL
);
4282 if (strchr (display_name
, G_DIR_SEPARATOR
) != NULL
)
4286 G_IO_ERROR_INVALID_ARGUMENT
,
4287 _("File names cannot contain “%c”"), G_DIR_SEPARATOR
);
4291 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4294 iface
= G_FILE_GET_IFACE (file
);
4296 return (* iface
->set_display_name
) (file
, display_name
, cancellable
, error
);
4300 * g_file_set_display_name_async:
4301 * @file: input #GFile
4302 * @display_name: a string
4303 * @io_priority: the [I/O priority][io-priority] of the request
4304 * @cancellable: (nullable): optional #GCancellable object,
4306 * @callback: (scope async): a #GAsyncReadyCallback to call
4307 * when the request is satisfied
4308 * @user_data: (closure): the data to pass to callback function
4310 * Asynchronously sets the display name for a given #GFile.
4312 * For more details, see g_file_set_display_name() which is
4313 * the synchronous version of this call.
4315 * When the operation is finished, @callback will be called.
4316 * You can then call g_file_set_display_name_finish() to get
4317 * the result of the operation.
4320 g_file_set_display_name_async (GFile
*file
,
4321 const gchar
*display_name
,
4323 GCancellable
*cancellable
,
4324 GAsyncReadyCallback callback
,
4329 g_return_if_fail (G_IS_FILE (file
));
4330 g_return_if_fail (display_name
!= NULL
);
4332 iface
= G_FILE_GET_IFACE (file
);
4333 (* iface
->set_display_name_async
) (file
,
4342 * g_file_set_display_name_finish:
4343 * @file: input #GFile
4344 * @res: a #GAsyncResult
4345 * @error: a #GError, or %NULL
4347 * Finishes setting a display name started with
4348 * g_file_set_display_name_async().
4350 * Returns: (transfer full): a #GFile or %NULL on error.
4351 * Free the returned object with g_object_unref().
4354 g_file_set_display_name_finish (GFile
*file
,
4360 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4361 g_return_val_if_fail (G_IS_ASYNC_RESULT (res
), NULL
);
4363 if (g_async_result_legacy_propagate_error (res
, error
))
4366 iface
= G_FILE_GET_IFACE (file
);
4367 return (* iface
->set_display_name_finish
) (file
, res
, error
);
4371 * g_file_query_settable_attributes:
4372 * @file: input #GFile
4373 * @cancellable: (nullable): optional #GCancellable object,
4375 * @error: a #GError, or %NULL
4377 * Obtain the list of settable attributes for the file.
4379 * Returns the type and full attribute name of all the attributes
4380 * that can be set on this file. This doesn't mean setting it will
4381 * always succeed though, you might get an access failure, or some
4382 * specific file may not support a specific attribute.
4384 * If @cancellable is not %NULL, then the operation can be cancelled by
4385 * triggering the cancellable object from another thread. If the operation
4386 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4388 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4389 * When you are done with it, release it with
4390 * g_file_attribute_info_list_unref()
4392 GFileAttributeInfoList
*
4393 g_file_query_settable_attributes (GFile
*file
,
4394 GCancellable
*cancellable
,
4399 GFileAttributeInfoList
*list
;
4401 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4403 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4406 iface
= G_FILE_GET_IFACE (file
);
4408 if (iface
->query_settable_attributes
== NULL
)
4409 return g_file_attribute_info_list_new ();
4412 list
= (* iface
->query_settable_attributes
) (file
, cancellable
, &my_error
);
4416 if (my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_NOT_SUPPORTED
)
4418 list
= g_file_attribute_info_list_new ();
4419 g_error_free (my_error
);
4422 g_propagate_error (error
, my_error
);
4429 * g_file_query_writable_namespaces:
4430 * @file: input #GFile
4431 * @cancellable: (nullable): optional #GCancellable object,
4433 * @error: a #GError, or %NULL
4435 * Obtain the list of attribute namespaces where new attributes
4436 * can be created by a user. An example of this is extended
4437 * attributes (in the "xattr" namespace).
4439 * If @cancellable is not %NULL, then the operation can be cancelled by
4440 * triggering the cancellable object from another thread. If the operation
4441 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4443 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4444 * When you are done with it, release it with
4445 * g_file_attribute_info_list_unref()
4447 GFileAttributeInfoList
*
4448 g_file_query_writable_namespaces (GFile
*file
,
4449 GCancellable
*cancellable
,
4454 GFileAttributeInfoList
*list
;
4456 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4458 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4461 iface
= G_FILE_GET_IFACE (file
);
4463 if (iface
->query_writable_namespaces
== NULL
)
4464 return g_file_attribute_info_list_new ();
4467 list
= (* iface
->query_writable_namespaces
) (file
, cancellable
, &my_error
);
4471 g_warn_if_reached();
4472 list
= g_file_attribute_info_list_new ();
4475 if (my_error
!= NULL
)
4477 if (my_error
->domain
== G_IO_ERROR
&& my_error
->code
== G_IO_ERROR_NOT_SUPPORTED
)
4479 g_error_free (my_error
);
4482 g_propagate_error (error
, my_error
);
4489 * g_file_set_attribute:
4490 * @file: input #GFile
4491 * @attribute: a string containing the attribute's name
4492 * @type: The type of the attribute
4493 * @value_p: (nullable): a pointer to the value (or the pointer
4494 * itself if the type is a pointer type)
4495 * @flags: a set of #GFileQueryInfoFlags
4496 * @cancellable: (nullable): optional #GCancellable object,
4498 * @error: a #GError, or %NULL
4500 * Sets an attribute in the file with attribute name @attribute to @value.
4502 * Some attributes can be unset by setting @type to
4503 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4505 * If @cancellable is not %NULL, then the operation can be cancelled by
4506 * triggering the cancellable object from another thread. If the operation
4507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4509 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4512 g_file_set_attribute (GFile
*file
,
4513 const gchar
*attribute
,
4514 GFileAttributeType type
,
4516 GFileQueryInfoFlags flags
,
4517 GCancellable
*cancellable
,
4522 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4523 g_return_val_if_fail (attribute
!= NULL
&& *attribute
!= '\0', FALSE
);
4525 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4528 iface
= G_FILE_GET_IFACE (file
);
4530 if (iface
->set_attribute
== NULL
)
4532 g_set_error_literal (error
, G_IO_ERROR
,
4533 G_IO_ERROR_NOT_SUPPORTED
,
4534 _("Operation not supported"));
4538 return (* iface
->set_attribute
) (file
, attribute
, type
, value_p
, flags
, cancellable
, error
);
4542 * g_file_set_attributes_from_info:
4543 * @file: input #GFile
4544 * @info: a #GFileInfo
4545 * @flags: #GFileQueryInfoFlags
4546 * @cancellable: (nullable): optional #GCancellable object,
4548 * @error: a #GError, or %NULL
4550 * Tries to set all attributes in the #GFileInfo on the target
4551 * values, not stopping on the first error.
4553 * If there is any error during this operation then @error will
4554 * be set to the first error. Error on particular fields are flagged
4555 * by setting the "status" field in the attribute value to
4556 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4557 * also detect further errors.
4559 * If @cancellable is not %NULL, then the operation can be cancelled by
4560 * triggering the cancellable object from another thread. If the operation
4561 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4563 * Returns: %FALSE if there was any error, %TRUE otherwise.
4566 g_file_set_attributes_from_info (GFile
*file
,
4568 GFileQueryInfoFlags flags
,
4569 GCancellable
*cancellable
,
4574 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4575 g_return_val_if_fail (G_IS_FILE_INFO (info
), FALSE
);
4577 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
4580 g_file_info_clear_status (info
);
4582 iface
= G_FILE_GET_IFACE (file
);
4584 return (* iface
->set_attributes_from_info
) (file
,
4592 g_file_real_set_attributes_from_info (GFile
*file
,
4594 GFileQueryInfoFlags flags
,
4595 GCancellable
*cancellable
,
4601 GFileAttributeValue
*value
;
4605 attributes
= g_file_info_list_attributes (info
, NULL
);
4607 for (i
= 0; attributes
[i
] != NULL
; i
++)
4609 value
= _g_file_info_get_attribute_value (info
, attributes
[i
]);
4611 if (value
->status
!= G_FILE_ATTRIBUTE_STATUS_UNSET
)
4614 if (!g_file_set_attribute (file
, attributes
[i
],
4615 value
->type
, _g_file_attribute_value_peek_as_pointer (value
),
4616 flags
, cancellable
, error
))
4618 value
->status
= G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING
;
4620 /* Don't set error multiple times */
4624 value
->status
= G_FILE_ATTRIBUTE_STATUS_SET
;
4627 g_strfreev (attributes
);
4633 * g_file_set_attributes_async:
4634 * @file: input #GFile
4635 * @info: a #GFileInfo
4636 * @flags: a #GFileQueryInfoFlags
4637 * @io_priority: the [I/O priority][io-priority] of the request
4638 * @cancellable: (nullable): optional #GCancellable object,
4640 * @callback: (scope async): a #GAsyncReadyCallback
4641 * @user_data: (closure): a #gpointer
4643 * Asynchronously sets the attributes of @file with @info.
4645 * For more details, see g_file_set_attributes_from_info(),
4646 * which is the synchronous version of this call.
4648 * When the operation is finished, @callback will be called.
4649 * You can then call g_file_set_attributes_finish() to get
4650 * the result of the operation.
4653 g_file_set_attributes_async (GFile
*file
,
4655 GFileQueryInfoFlags flags
,
4657 GCancellable
*cancellable
,
4658 GAsyncReadyCallback callback
,
4663 g_return_if_fail (G_IS_FILE (file
));
4664 g_return_if_fail (G_IS_FILE_INFO (info
));
4666 iface
= G_FILE_GET_IFACE (file
);
4667 (* iface
->set_attributes_async
) (file
,
4677 * g_file_set_attributes_finish:
4678 * @file: input #GFile
4679 * @result: a #GAsyncResult
4680 * @info: (out) (transfer full): a #GFileInfo
4681 * @error: a #GError, or %NULL
4683 * Finishes setting an attribute started in g_file_set_attributes_async().
4685 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4688 g_file_set_attributes_finish (GFile
*file
,
4689 GAsyncResult
*result
,
4695 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
4696 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
4698 /* No standard handling of errors here, as we must set info even
4701 iface
= G_FILE_GET_IFACE (file
);
4702 return (* iface
->set_attributes_finish
) (file
, result
, info
, error
);
4706 * g_file_set_attribute_string:
4707 * @file: input #GFile
4708 * @attribute: a string containing the attribute's name
4709 * @value: a string containing the attribute's value
4710 * @flags: #GFileQueryInfoFlags
4711 * @cancellable: (nullable): optional #GCancellable object,
4713 * @error: a #GError, or %NULL
4715 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4716 * If @attribute is of a different type, this operation will fail.
4718 * If @cancellable is not %NULL, then the operation can be cancelled by
4719 * triggering the cancellable object from another thread. If the operation
4720 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4722 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4725 g_file_set_attribute_string (GFile
*file
,
4726 const char *attribute
,
4728 GFileQueryInfoFlags flags
,
4729 GCancellable
*cancellable
,
4732 return g_file_set_attribute (file
, attribute
,
4733 G_FILE_ATTRIBUTE_TYPE_STRING
, (gpointer
)value
,
4734 flags
, cancellable
, error
);
4738 * g_file_set_attribute_byte_string:
4739 * @file: input #GFile
4740 * @attribute: a string containing the attribute's name
4741 * @value: a string containing the attribute's new value
4742 * @flags: a #GFileQueryInfoFlags
4743 * @cancellable: (nullable): optional #GCancellable object,
4745 * @error: a #GError, or %NULL
4747 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4748 * If @attribute is of a different type, this operation will fail,
4751 * If @cancellable is not %NULL, then the operation can be cancelled by
4752 * triggering the cancellable object from another thread. If the operation
4753 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4755 * Returns: %TRUE if the @attribute was successfully set to @value
4756 * in the @file, %FALSE otherwise.
4759 g_file_set_attribute_byte_string (GFile
*file
,
4760 const gchar
*attribute
,
4762 GFileQueryInfoFlags flags
,
4763 GCancellable
*cancellable
,
4766 return g_file_set_attribute (file
, attribute
,
4767 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
, (gpointer
)value
,
4768 flags
, cancellable
, error
);
4772 * g_file_set_attribute_uint32:
4773 * @file: input #GFile
4774 * @attribute: a string containing the attribute's name
4775 * @value: a #guint32 containing the attribute's new value
4776 * @flags: a #GFileQueryInfoFlags
4777 * @cancellable: (nullable): optional #GCancellable object,
4779 * @error: a #GError, or %NULL
4781 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4782 * If @attribute is of a different type, this operation will fail.
4784 * If @cancellable is not %NULL, then the operation can be cancelled by
4785 * triggering the cancellable object from another thread. If the operation
4786 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4788 * Returns: %TRUE if the @attribute was successfully set to @value
4789 * in the @file, %FALSE otherwise.
4792 g_file_set_attribute_uint32 (GFile
*file
,
4793 const gchar
*attribute
,
4795 GFileQueryInfoFlags flags
,
4796 GCancellable
*cancellable
,
4799 return g_file_set_attribute (file
, attribute
,
4800 G_FILE_ATTRIBUTE_TYPE_UINT32
, &value
,
4801 flags
, cancellable
, error
);
4805 * g_file_set_attribute_int32:
4806 * @file: input #GFile
4807 * @attribute: a string containing the attribute's name
4808 * @value: a #gint32 containing the attribute's new value
4809 * @flags: a #GFileQueryInfoFlags
4810 * @cancellable: (nullable): optional #GCancellable object,
4812 * @error: a #GError, or %NULL
4814 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4815 * If @attribute is of a different type, this operation will fail.
4817 * If @cancellable is not %NULL, then the operation can be cancelled by
4818 * triggering the cancellable object from another thread. If the operation
4819 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4821 * Returns: %TRUE if the @attribute was successfully set to @value
4822 * in the @file, %FALSE otherwise.
4825 g_file_set_attribute_int32 (GFile
*file
,
4826 const gchar
*attribute
,
4828 GFileQueryInfoFlags flags
,
4829 GCancellable
*cancellable
,
4832 return g_file_set_attribute (file
, attribute
,
4833 G_FILE_ATTRIBUTE_TYPE_INT32
, &value
,
4834 flags
, cancellable
, error
);
4838 * g_file_set_attribute_uint64:
4839 * @file: input #GFile
4840 * @attribute: a string containing the attribute's name
4841 * @value: a #guint64 containing the attribute's new value
4842 * @flags: a #GFileQueryInfoFlags
4843 * @cancellable: (nullable): optional #GCancellable object,
4845 * @error: a #GError, or %NULL
4847 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4848 * If @attribute is of a different type, this operation will fail.
4850 * If @cancellable is not %NULL, then the operation can be cancelled by
4851 * triggering the cancellable object from another thread. If the operation
4852 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4854 * Returns: %TRUE if the @attribute was successfully set to @value
4855 * in the @file, %FALSE otherwise.
4858 g_file_set_attribute_uint64 (GFile
*file
,
4859 const gchar
*attribute
,
4861 GFileQueryInfoFlags flags
,
4862 GCancellable
*cancellable
,
4865 return g_file_set_attribute (file
, attribute
,
4866 G_FILE_ATTRIBUTE_TYPE_UINT64
, &value
,
4867 flags
, cancellable
, error
);
4871 * g_file_set_attribute_int64:
4872 * @file: input #GFile
4873 * @attribute: a string containing the attribute's name
4874 * @value: a #guint64 containing the attribute's new value
4875 * @flags: a #GFileQueryInfoFlags
4876 * @cancellable: (nullable): optional #GCancellable object,
4878 * @error: a #GError, or %NULL
4880 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4881 * If @attribute is of a different type, this operation will fail.
4883 * If @cancellable is not %NULL, then the operation can be cancelled by
4884 * triggering the cancellable object from another thread. If the operation
4885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4887 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4890 g_file_set_attribute_int64 (GFile
*file
,
4891 const gchar
*attribute
,
4893 GFileQueryInfoFlags flags
,
4894 GCancellable
*cancellable
,
4897 return g_file_set_attribute (file
, attribute
,
4898 G_FILE_ATTRIBUTE_TYPE_INT64
, &value
,
4899 flags
, cancellable
, error
);
4903 * g_file_mount_mountable:
4904 * @file: input #GFile
4905 * @flags: flags affecting the operation
4906 * @mount_operation: (nullable): a #GMountOperation,
4907 * or %NULL to avoid user interaction
4908 * @cancellable: (nullable): optional #GCancellable object,
4910 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4911 * when the request is satisfied, or %NULL
4912 * @user_data: (closure): the data to pass to callback function
4914 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4915 * Using @mount_operation, you can request callbacks when, for instance,
4916 * passwords are needed during authentication.
4918 * If @cancellable is not %NULL, then the operation can be cancelled by
4919 * triggering the cancellable object from another thread. If the operation
4920 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4922 * When the operation is finished, @callback will be called.
4923 * You can then call g_file_mount_mountable_finish() to get
4924 * the result of the operation.
4927 g_file_mount_mountable (GFile
*file
,
4928 GMountMountFlags flags
,
4929 GMountOperation
*mount_operation
,
4930 GCancellable
*cancellable
,
4931 GAsyncReadyCallback callback
,
4936 g_return_if_fail (G_IS_FILE (file
));
4938 iface
= G_FILE_GET_IFACE (file
);
4940 if (iface
->mount_mountable
== NULL
)
4942 g_task_report_new_error (file
, callback
, user_data
,
4943 g_file_mount_mountable
,
4944 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
4945 _("Operation not supported"));
4949 (* iface
->mount_mountable
) (file
,
4958 * g_file_mount_mountable_finish:
4959 * @file: input #GFile
4960 * @result: a #GAsyncResult
4961 * @error: a #GError, or %NULL
4963 * Finishes a mount operation. See g_file_mount_mountable() for details.
4965 * Finish an asynchronous mount operation that was started
4966 * with g_file_mount_mountable().
4968 * Returns: (transfer full): a #GFile or %NULL on error.
4969 * Free the returned object with g_object_unref().
4972 g_file_mount_mountable_finish (GFile
*file
,
4973 GAsyncResult
*result
,
4978 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
4979 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), NULL
);
4981 if (g_async_result_legacy_propagate_error (result
, error
))
4983 else if (g_async_result_is_tagged (result
, g_file_mount_mountable
))
4984 return g_task_propagate_pointer (G_TASK (result
), error
);
4986 iface
= G_FILE_GET_IFACE (file
);
4987 return (* iface
->mount_mountable_finish
) (file
, result
, error
);
4991 * g_file_unmount_mountable:
4992 * @file: input #GFile
4993 * @flags: flags affecting the operation
4994 * @cancellable: (nullable): optional #GCancellable object,
4996 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
4997 * when the request is satisfied, or %NULL
4998 * @user_data: (closure): the data to pass to callback function
5000 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
5002 * If @cancellable is not %NULL, then the operation can be cancelled by
5003 * triggering the cancellable object from another thread. If the operation
5004 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5006 * When the operation is finished, @callback will be called.
5007 * You can then call g_file_unmount_mountable_finish() to get
5008 * the result of the operation.
5010 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
5013 g_file_unmount_mountable (GFile
*file
,
5014 GMountUnmountFlags flags
,
5015 GCancellable
*cancellable
,
5016 GAsyncReadyCallback callback
,
5021 g_return_if_fail (G_IS_FILE (file
));
5023 iface
= G_FILE_GET_IFACE (file
);
5025 if (iface
->unmount_mountable
== NULL
)
5027 g_task_report_new_error (file
, callback
, user_data
,
5028 g_file_unmount_mountable_with_operation
,
5029 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5030 _("Operation not supported"));
5034 (* iface
->unmount_mountable
) (file
,
5042 * g_file_unmount_mountable_finish:
5043 * @file: input #GFile
5044 * @result: a #GAsyncResult
5045 * @error: a #GError, or %NULL
5047 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
5049 * Finish an asynchronous unmount operation that was started
5050 * with g_file_unmount_mountable().
5052 * Returns: %TRUE if the operation finished successfully.
5055 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
5059 g_file_unmount_mountable_finish (GFile
*file
,
5060 GAsyncResult
*result
,
5065 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5066 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5068 if (g_async_result_legacy_propagate_error (result
, error
))
5070 else if (g_async_result_is_tagged (result
, g_file_unmount_mountable_with_operation
))
5071 return g_task_propagate_boolean (G_TASK (result
), error
);
5073 iface
= G_FILE_GET_IFACE (file
);
5074 return (* iface
->unmount_mountable_finish
) (file
, result
, error
);
5078 * g_file_unmount_mountable_with_operation:
5079 * @file: input #GFile
5080 * @flags: flags affecting the operation
5081 * @mount_operation: (nullable): a #GMountOperation,
5082 * or %NULL to avoid user interaction
5083 * @cancellable: (nullable): optional #GCancellable object,
5085 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5086 * when the request is satisfied, or %NULL
5087 * @user_data: (closure): the data to pass to callback function
5089 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
5091 * If @cancellable is not %NULL, then the operation can be cancelled by
5092 * triggering the cancellable object from another thread. If the operation
5093 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5095 * When the operation is finished, @callback will be called.
5096 * You can then call g_file_unmount_mountable_finish() to get
5097 * the result of the operation.
5102 g_file_unmount_mountable_with_operation (GFile
*file
,
5103 GMountUnmountFlags flags
,
5104 GMountOperation
*mount_operation
,
5105 GCancellable
*cancellable
,
5106 GAsyncReadyCallback callback
,
5111 g_return_if_fail (G_IS_FILE (file
));
5113 iface
= G_FILE_GET_IFACE (file
);
5115 if (iface
->unmount_mountable
== NULL
&& iface
->unmount_mountable_with_operation
== NULL
)
5117 g_task_report_new_error (file
, callback
, user_data
,
5118 g_file_unmount_mountable_with_operation
,
5119 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5120 _("Operation not supported"));
5124 if (iface
->unmount_mountable_with_operation
!= NULL
)
5125 (* iface
->unmount_mountable_with_operation
) (file
,
5132 (* iface
->unmount_mountable
) (file
,
5140 * g_file_unmount_mountable_with_operation_finish:
5141 * @file: input #GFile
5142 * @result: a #GAsyncResult
5143 * @error: a #GError, or %NULL
5145 * Finishes an unmount operation,
5146 * see g_file_unmount_mountable_with_operation() for details.
5148 * Finish an asynchronous unmount operation that was started
5149 * with g_file_unmount_mountable_with_operation().
5151 * Returns: %TRUE if the operation finished successfully.
5157 g_file_unmount_mountable_with_operation_finish (GFile
*file
,
5158 GAsyncResult
*result
,
5163 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5164 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5166 if (g_async_result_legacy_propagate_error (result
, error
))
5168 else if (g_async_result_is_tagged (result
, g_file_unmount_mountable_with_operation
))
5169 return g_task_propagate_boolean (G_TASK (result
), error
);
5171 iface
= G_FILE_GET_IFACE (file
);
5172 if (iface
->unmount_mountable_with_operation_finish
!= NULL
)
5173 return (* iface
->unmount_mountable_with_operation_finish
) (file
, result
, error
);
5175 return (* iface
->unmount_mountable_finish
) (file
, result
, error
);
5179 * g_file_eject_mountable:
5180 * @file: input #GFile
5181 * @flags: flags affecting the operation
5182 * @cancellable: (nullable): optional #GCancellable object,
5184 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5185 * when the request is satisfied, or %NULL
5186 * @user_data: (closure): the data to pass to callback function
5188 * Starts an asynchronous eject on a mountable.
5189 * When this operation has completed, @callback will be called with
5190 * @user_user data, and the operation can be finalized with
5191 * g_file_eject_mountable_finish().
5193 * If @cancellable is not %NULL, then the operation can be cancelled by
5194 * triggering the cancellable object from another thread. If the operation
5195 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5197 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5200 g_file_eject_mountable (GFile
*file
,
5201 GMountUnmountFlags flags
,
5202 GCancellable
*cancellable
,
5203 GAsyncReadyCallback callback
,
5208 g_return_if_fail (G_IS_FILE (file
));
5210 iface
= G_FILE_GET_IFACE (file
);
5212 if (iface
->eject_mountable
== NULL
)
5214 g_task_report_new_error (file
, callback
, user_data
,
5215 g_file_eject_mountable_with_operation
,
5216 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5217 _("Operation not supported"));
5221 (* iface
->eject_mountable
) (file
,
5229 * g_file_eject_mountable_finish:
5230 * @file: input #GFile
5231 * @result: a #GAsyncResult
5232 * @error: a #GError, or %NULL
5234 * Finishes an asynchronous eject operation started by
5235 * g_file_eject_mountable().
5237 * Returns: %TRUE if the @file was ejected successfully.
5240 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5244 g_file_eject_mountable_finish (GFile
*file
,
5245 GAsyncResult
*result
,
5250 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5251 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5253 if (g_async_result_legacy_propagate_error (result
, error
))
5255 else if (g_async_result_is_tagged (result
, g_file_eject_mountable_with_operation
))
5256 return g_task_propagate_boolean (G_TASK (result
), error
);
5258 iface
= G_FILE_GET_IFACE (file
);
5259 return (* iface
->eject_mountable_finish
) (file
, result
, error
);
5263 * g_file_eject_mountable_with_operation:
5264 * @file: input #GFile
5265 * @flags: flags affecting the operation
5266 * @mount_operation: (nullable): a #GMountOperation,
5267 * or %NULL to avoid user interaction
5268 * @cancellable: (nullable): optional #GCancellable object,
5270 * @callback: (scope async) (nullable): a #GAsyncReadyCallback to call
5271 * when the request is satisfied, or %NULL
5272 * @user_data: (closure): the data to pass to callback function
5274 * Starts an asynchronous eject on a mountable.
5275 * When this operation has completed, @callback will be called with
5276 * @user_user data, and the operation can be finalized with
5277 * g_file_eject_mountable_with_operation_finish().
5279 * If @cancellable is not %NULL, then the operation can be cancelled by
5280 * triggering the cancellable object from another thread. If the operation
5281 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5286 g_file_eject_mountable_with_operation (GFile
*file
,
5287 GMountUnmountFlags flags
,
5288 GMountOperation
*mount_operation
,
5289 GCancellable
*cancellable
,
5290 GAsyncReadyCallback callback
,
5295 g_return_if_fail (G_IS_FILE (file
));
5297 iface
= G_FILE_GET_IFACE (file
);
5299 if (iface
->eject_mountable
== NULL
&& iface
->eject_mountable_with_operation
== NULL
)
5301 g_task_report_new_error (file
, callback
, user_data
,
5302 g_file_eject_mountable_with_operation
,
5303 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
5304 _("Operation not supported"));
5308 if (iface
->eject_mountable_with_operation
!= NULL
)
5309 (* iface
->eject_mountable_with_operation
) (file
,
5316 (* iface
->eject_mountable
) (file
,
5324 * g_file_eject_mountable_with_operation_finish:
5325 * @file: input #GFile
5326 * @result: a #GAsyncResult
5327 * @error: a #GError, or %NULL
5329 * Finishes an asynchronous eject operation started by
5330 * g_file_eject_mountable_with_operation().
5332 * Returns: %TRUE if the @file was ejected successfully.
5338 g_file_eject_mountable_with_operation_finish (GFile
*file
,
5339 GAsyncResult
*result
,
5344 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
5345 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
5347 if (g_async_result_legacy_propagate_error (result
, error
))
5349 else if (g_async_result_is_tagged (result
, g_file_eject_mountable_with_operation
))
5350 return g_task_propagate_boolean (G_TASK (result
), error
);
5352 iface
= G_FILE_GET_IFACE (file
);
5353 if (iface
->eject_mountable_with_operation_finish
!= NULL
)
5354 return (* iface
->eject_mountable_with_operation_finish
) (file
, result
, error
);
5356 return (* iface
->eject_mountable_finish
) (file
, result
, error
);
5360 * g_file_monitor_directory:
5361 * @file: input #GFile
5362 * @flags: a set of #GFileMonitorFlags
5363 * @cancellable: (nullable): optional #GCancellable object,
5365 * @error: a #GError, or %NULL
5367 * Obtains a directory monitor for the given file.
5368 * This may fail if directory monitoring is not supported.
5370 * If @cancellable is not %NULL, then the operation can be cancelled by
5371 * triggering the cancellable object from another thread. If the operation
5372 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5374 * It does not make sense for @flags to contain
5375 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5376 * directories. It is not possible to monitor all the files in a
5377 * directory for changes made via hard links; if you want to do this then
5378 * you must register individual watches with g_file_monitor().
5380 * Virtual: monitor_dir
5381 * Returns: (transfer full): a #GFileMonitor for the given @file,
5382 * or %NULL on error.
5383 * Free the returned object with g_object_unref().
5386 g_file_monitor_directory (GFile
*file
,
5387 GFileMonitorFlags flags
,
5388 GCancellable
*cancellable
,
5393 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
5394 g_return_val_if_fail (~flags
& G_FILE_MONITOR_WATCH_HARD_LINKS
, NULL
);
5396 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
5399 iface
= G_FILE_GET_IFACE (file
);
5401 if (iface
->monitor_dir
== NULL
)
5403 g_set_error_literal (error
, G_IO_ERROR
,
5404 G_IO_ERROR_NOT_SUPPORTED
,
5405 _("Operation not supported"));
5409 return (* iface
->monitor_dir
) (file
, flags
, cancellable
, error
);
5413 * g_file_monitor_file:
5414 * @file: input #GFile
5415 * @flags: a set of #GFileMonitorFlags
5416 * @cancellable: (nullable): optional #GCancellable object,
5418 * @error: a #GError, or %NULL
5420 * Obtains a file monitor for the given file. If no file notification
5421 * mechanism exists, then regular polling of the file is used.
5423 * If @cancellable is not %NULL, then the operation can be cancelled by
5424 * triggering the cancellable object from another thread. If the operation
5425 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5427 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5428 * will also attempt to report changes made to the file via another
5429 * filename (ie, a hard link). Without this flag, you can only rely on
5430 * changes made through the filename contained in @file to be
5431 * reported. Using this flag may result in an increase in resource
5432 * usage, and may not have any effect depending on the #GFileMonitor
5433 * backend and/or filesystem type.
5435 * Returns: (transfer full): a #GFileMonitor for the given @file,
5436 * or %NULL on error.
5437 * Free the returned object with g_object_unref().
5440 g_file_monitor_file (GFile
*file
,
5441 GFileMonitorFlags flags
,
5442 GCancellable
*cancellable
,
5446 GFileMonitor
*monitor
;
5448 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
5450 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
5453 iface
= G_FILE_GET_IFACE (file
);
5457 if (iface
->monitor_file
)
5458 monitor
= (* iface
->monitor_file
) (file
, flags
, cancellable
, NULL
);
5460 /* Fallback to polling */
5461 if (monitor
== NULL
)
5462 monitor
= _g_poll_file_monitor_new (file
);
5469 * @file: input #GFile
5470 * @flags: a set of #GFileMonitorFlags
5471 * @cancellable: (nullable): optional #GCancellable object,
5473 * @error: a #GError, or %NULL
5475 * Obtains a file or directory monitor for the given file,
5476 * depending on the type of the file.
5478 * If @cancellable is not %NULL, then the operation can be cancelled by
5479 * triggering the cancellable object from another thread. If the operation
5480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5482 * Returns: (transfer full): a #GFileMonitor for the given @file,
5483 * or %NULL on error.
5484 * Free the returned object with g_object_unref().
5489 g_file_monitor (GFile
*file
,
5490 GFileMonitorFlags flags
,
5491 GCancellable
*cancellable
,
5494 if (g_file_query_file_type (file
, 0, cancellable
) == G_FILE_TYPE_DIRECTORY
)
5495 return g_file_monitor_directory (file
,
5496 flags
& ~G_FILE_MONITOR_WATCH_HARD_LINKS
,
5497 cancellable
, error
);
5499 return g_file_monitor_file (file
, flags
, cancellable
, error
);
5502 /********************************************
5503 * Default implementation of async ops *
5504 ********************************************/
5508 GFileQueryInfoFlags flags
;
5509 } QueryInfoAsyncData
;
5512 query_info_data_free (QueryInfoAsyncData
*data
)
5514 g_free (data
->attributes
);
5519 query_info_async_thread (GTask
*task
,
5522 GCancellable
*cancellable
)
5524 QueryInfoAsyncData
*data
= task_data
;
5526 GError
*error
= NULL
;
5528 info
= g_file_query_info (G_FILE (object
), data
->attributes
, data
->flags
, cancellable
, &error
);
5530 g_task_return_pointer (task
, info
, g_object_unref
);
5532 g_task_return_error (task
, error
);
5536 g_file_real_query_info_async (GFile
*file
,
5537 const char *attributes
,
5538 GFileQueryInfoFlags flags
,
5540 GCancellable
*cancellable
,
5541 GAsyncReadyCallback callback
,
5545 QueryInfoAsyncData
*data
;
5547 data
= g_new0 (QueryInfoAsyncData
, 1);
5548 data
->attributes
= g_strdup (attributes
);
5549 data
->flags
= flags
;
5551 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5552 g_task_set_source_tag (task
, g_file_real_query_info_async
);
5553 g_task_set_task_data (task
, data
, (GDestroyNotify
)query_info_data_free
);
5554 g_task_set_priority (task
, io_priority
);
5555 g_task_run_in_thread (task
, query_info_async_thread
);
5556 g_object_unref (task
);
5560 g_file_real_query_info_finish (GFile
*file
,
5564 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5566 return g_task_propagate_pointer (G_TASK (res
), error
);
5570 query_filesystem_info_async_thread (GTask
*task
,
5573 GCancellable
*cancellable
)
5575 const char *attributes
= task_data
;
5577 GError
*error
= NULL
;
5579 info
= g_file_query_filesystem_info (G_FILE (object
), attributes
, cancellable
, &error
);
5581 g_task_return_pointer (task
, info
, g_object_unref
);
5583 g_task_return_error (task
, error
);
5587 g_file_real_query_filesystem_info_async (GFile
*file
,
5588 const char *attributes
,
5590 GCancellable
*cancellable
,
5591 GAsyncReadyCallback callback
,
5596 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5597 g_task_set_source_tag (task
, g_file_real_query_filesystem_info_async
);
5598 g_task_set_task_data (task
, g_strdup (attributes
), g_free
);
5599 g_task_set_priority (task
, io_priority
);
5600 g_task_run_in_thread (task
, query_filesystem_info_async_thread
);
5601 g_object_unref (task
);
5605 g_file_real_query_filesystem_info_finish (GFile
*file
,
5609 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5611 return g_task_propagate_pointer (G_TASK (res
), error
);
5615 enumerate_children_async_thread (GTask
*task
,
5618 GCancellable
*cancellable
)
5620 QueryInfoAsyncData
*data
= task_data
;
5621 GFileEnumerator
*enumerator
;
5622 GError
*error
= NULL
;
5624 enumerator
= g_file_enumerate_children (G_FILE (object
), data
->attributes
, data
->flags
, cancellable
, &error
);
5626 g_task_return_error (task
, error
);
5628 g_task_return_pointer (task
, enumerator
, g_object_unref
);
5632 g_file_real_enumerate_children_async (GFile
*file
,
5633 const char *attributes
,
5634 GFileQueryInfoFlags flags
,
5636 GCancellable
*cancellable
,
5637 GAsyncReadyCallback callback
,
5641 QueryInfoAsyncData
*data
;
5643 data
= g_new0 (QueryInfoAsyncData
, 1);
5644 data
->attributes
= g_strdup (attributes
);
5645 data
->flags
= flags
;
5647 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5648 g_task_set_source_tag (task
, g_file_real_enumerate_children_async
);
5649 g_task_set_task_data (task
, data
, (GDestroyNotify
)query_info_data_free
);
5650 g_task_set_priority (task
, io_priority
);
5651 g_task_run_in_thread (task
, enumerate_children_async_thread
);
5652 g_object_unref (task
);
5655 static GFileEnumerator
*
5656 g_file_real_enumerate_children_finish (GFile
*file
,
5660 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5662 return g_task_propagate_pointer (G_TASK (res
), error
);
5666 open_read_async_thread (GTask
*task
,
5669 GCancellable
*cancellable
)
5671 GFileInputStream
*stream
;
5672 GError
*error
= NULL
;
5674 stream
= g_file_read (G_FILE (object
), cancellable
, &error
);
5676 g_task_return_pointer (task
, stream
, g_object_unref
);
5678 g_task_return_error (task
, error
);
5682 g_file_real_read_async (GFile
*file
,
5684 GCancellable
*cancellable
,
5685 GAsyncReadyCallback callback
,
5690 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5691 g_task_set_source_tag (task
, g_file_real_read_async
);
5692 g_task_set_priority (task
, io_priority
);
5693 g_task_run_in_thread (task
, open_read_async_thread
);
5694 g_object_unref (task
);
5697 static GFileInputStream
*
5698 g_file_real_read_finish (GFile
*file
,
5702 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5704 return g_task_propagate_pointer (G_TASK (res
), error
);
5708 append_to_async_thread (GTask
*task
,
5709 gpointer source_object
,
5711 GCancellable
*cancellable
)
5713 GFileCreateFlags
*data
= task_data
;
5714 GFileOutputStream
*stream
;
5715 GError
*error
= NULL
;
5717 stream
= g_file_append_to (G_FILE (source_object
), *data
, cancellable
, &error
);
5719 g_task_return_pointer (task
, stream
, g_object_unref
);
5721 g_task_return_error (task
, error
);
5725 g_file_real_append_to_async (GFile
*file
,
5726 GFileCreateFlags flags
,
5728 GCancellable
*cancellable
,
5729 GAsyncReadyCallback callback
,
5732 GFileCreateFlags
*data
;
5735 data
= g_new0 (GFileCreateFlags
, 1);
5738 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5739 g_task_set_source_tag (task
, g_file_real_append_to_async
);
5740 g_task_set_task_data (task
, data
, g_free
);
5741 g_task_set_priority (task
, io_priority
);
5743 g_task_run_in_thread (task
, append_to_async_thread
);
5744 g_object_unref (task
);
5747 static GFileOutputStream
*
5748 g_file_real_append_to_finish (GFile
*file
,
5752 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5754 return g_task_propagate_pointer (G_TASK (res
), error
);
5758 create_async_thread (GTask
*task
,
5759 gpointer source_object
,
5761 GCancellable
*cancellable
)
5763 GFileCreateFlags
*data
= task_data
;
5764 GFileOutputStream
*stream
;
5765 GError
*error
= NULL
;
5767 stream
= g_file_create (G_FILE (source_object
), *data
, cancellable
, &error
);
5769 g_task_return_pointer (task
, stream
, g_object_unref
);
5771 g_task_return_error (task
, error
);
5775 g_file_real_create_async (GFile
*file
,
5776 GFileCreateFlags flags
,
5778 GCancellable
*cancellable
,
5779 GAsyncReadyCallback callback
,
5782 GFileCreateFlags
*data
;
5785 data
= g_new0 (GFileCreateFlags
, 1);
5788 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5789 g_task_set_source_tag (task
, g_file_real_create_async
);
5790 g_task_set_task_data (task
, data
, g_free
);
5791 g_task_set_priority (task
, io_priority
);
5793 g_task_run_in_thread (task
, create_async_thread
);
5794 g_object_unref (task
);
5797 static GFileOutputStream
*
5798 g_file_real_create_finish (GFile
*file
,
5802 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5804 return g_task_propagate_pointer (G_TASK (res
), error
);
5808 GFileOutputStream
*stream
;
5810 gboolean make_backup
;
5811 GFileCreateFlags flags
;
5815 replace_async_data_free (ReplaceAsyncData
*data
)
5818 g_object_unref (data
->stream
);
5819 g_free (data
->etag
);
5824 replace_async_thread (GTask
*task
,
5825 gpointer source_object
,
5827 GCancellable
*cancellable
)
5829 GFileOutputStream
*stream
;
5830 ReplaceAsyncData
*data
= task_data
;
5831 GError
*error
= NULL
;
5833 stream
= g_file_replace (G_FILE (source_object
),
5841 g_task_return_pointer (task
, stream
, g_object_unref
);
5843 g_task_return_error (task
, error
);
5847 g_file_real_replace_async (GFile
*file
,
5849 gboolean make_backup
,
5850 GFileCreateFlags flags
,
5852 GCancellable
*cancellable
,
5853 GAsyncReadyCallback callback
,
5857 ReplaceAsyncData
*data
;
5859 data
= g_new0 (ReplaceAsyncData
, 1);
5860 data
->etag
= g_strdup (etag
);
5861 data
->make_backup
= make_backup
;
5862 data
->flags
= flags
;
5864 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5865 g_task_set_source_tag (task
, g_file_real_replace_async
);
5866 g_task_set_task_data (task
, data
, (GDestroyNotify
)replace_async_data_free
);
5867 g_task_set_priority (task
, io_priority
);
5869 g_task_run_in_thread (task
, replace_async_thread
);
5870 g_object_unref (task
);
5873 static GFileOutputStream
*
5874 g_file_real_replace_finish (GFile
*file
,
5878 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
5880 return g_task_propagate_pointer (G_TASK (res
), error
);
5884 delete_async_thread (GTask
*task
,
5887 GCancellable
*cancellable
)
5889 GError
*error
= NULL
;
5891 if (g_file_delete (G_FILE (object
), cancellable
, &error
))
5892 g_task_return_boolean (task
, TRUE
);
5894 g_task_return_error (task
, error
);
5898 g_file_real_delete_async (GFile
*file
,
5900 GCancellable
*cancellable
,
5901 GAsyncReadyCallback callback
,
5906 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5907 g_task_set_source_tag (task
, g_file_real_delete_async
);
5908 g_task_set_priority (task
, io_priority
);
5909 g_task_run_in_thread (task
, delete_async_thread
);
5910 g_object_unref (task
);
5914 g_file_real_delete_finish (GFile
*file
,
5918 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
5920 return g_task_propagate_boolean (G_TASK (res
), error
);
5924 trash_async_thread (GTask
*task
,
5927 GCancellable
*cancellable
)
5929 GError
*error
= NULL
;
5931 if (g_file_trash (G_FILE (object
), cancellable
, &error
))
5932 g_task_return_boolean (task
, TRUE
);
5934 g_task_return_error (task
, error
);
5938 g_file_real_trash_async (GFile
*file
,
5940 GCancellable
*cancellable
,
5941 GAsyncReadyCallback callback
,
5946 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5947 g_task_set_source_tag (task
, g_file_real_trash_async
);
5948 g_task_set_priority (task
, io_priority
);
5949 g_task_run_in_thread (task
, trash_async_thread
);
5950 g_object_unref (task
);
5954 g_file_real_trash_finish (GFile
*file
,
5958 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
5960 return g_task_propagate_boolean (G_TASK (res
), error
);
5964 make_directory_async_thread (GTask
*task
,
5967 GCancellable
*cancellable
)
5969 GError
*error
= NULL
;
5971 if (g_file_make_directory (G_FILE (object
), cancellable
, &error
))
5972 g_task_return_boolean (task
, TRUE
);
5974 g_task_return_error (task
, error
);
5978 g_file_real_make_directory_async (GFile
*file
,
5980 GCancellable
*cancellable
,
5981 GAsyncReadyCallback callback
,
5986 task
= g_task_new (file
, cancellable
, callback
, user_data
);
5987 g_task_set_source_tag (task
, g_file_real_make_directory_async
);
5988 g_task_set_priority (task
, io_priority
);
5989 g_task_run_in_thread (task
, make_directory_async_thread
);
5990 g_object_unref (task
);
5994 g_file_real_make_directory_finish (GFile
*file
,
5998 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
6000 return g_task_propagate_boolean (G_TASK (res
), error
);
6004 open_readwrite_async_thread (GTask
*task
,
6007 GCancellable
*cancellable
)
6009 GFileIOStream
*stream
;
6010 GError
*error
= NULL
;
6012 stream
= g_file_open_readwrite (G_FILE (object
), cancellable
, &error
);
6015 g_task_return_error (task
, error
);
6017 g_task_return_pointer (task
, stream
, g_object_unref
);
6021 g_file_real_open_readwrite_async (GFile
*file
,
6023 GCancellable
*cancellable
,
6024 GAsyncReadyCallback callback
,
6029 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6030 g_task_set_source_tag (task
, g_file_real_open_readwrite_async
);
6031 g_task_set_priority (task
, io_priority
);
6033 g_task_run_in_thread (task
, open_readwrite_async_thread
);
6034 g_object_unref (task
);
6037 static GFileIOStream
*
6038 g_file_real_open_readwrite_finish (GFile
*file
,
6042 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6044 return g_task_propagate_pointer (G_TASK (res
), error
);
6048 create_readwrite_async_thread (GTask
*task
,
6051 GCancellable
*cancellable
)
6053 GFileCreateFlags
*data
= task_data
;
6054 GFileIOStream
*stream
;
6055 GError
*error
= NULL
;
6057 stream
= g_file_create_readwrite (G_FILE (object
), *data
, cancellable
, &error
);
6060 g_task_return_error (task
, error
);
6062 g_task_return_pointer (task
, stream
, g_object_unref
);
6066 g_file_real_create_readwrite_async (GFile
*file
,
6067 GFileCreateFlags flags
,
6069 GCancellable
*cancellable
,
6070 GAsyncReadyCallback callback
,
6073 GFileCreateFlags
*data
;
6076 data
= g_new0 (GFileCreateFlags
, 1);
6079 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6080 g_task_set_source_tag (task
, g_file_real_create_readwrite_async
);
6081 g_task_set_task_data (task
, data
, g_free
);
6082 g_task_set_priority (task
, io_priority
);
6084 g_task_run_in_thread (task
, create_readwrite_async_thread
);
6085 g_object_unref (task
);
6088 static GFileIOStream
*
6089 g_file_real_create_readwrite_finish (GFile
*file
,
6093 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6095 return g_task_propagate_pointer (G_TASK (res
), error
);
6100 gboolean make_backup
;
6101 GFileCreateFlags flags
;
6102 } ReplaceRWAsyncData
;
6105 replace_rw_async_data_free (ReplaceRWAsyncData
*data
)
6107 g_free (data
->etag
);
6112 replace_readwrite_async_thread (GTask
*task
,
6115 GCancellable
*cancellable
)
6117 GFileIOStream
*stream
;
6118 GError
*error
= NULL
;
6119 ReplaceRWAsyncData
*data
= task_data
;
6121 stream
= g_file_replace_readwrite (G_FILE (object
),
6129 g_task_return_error (task
, error
);
6131 g_task_return_pointer (task
, stream
, g_object_unref
);
6135 g_file_real_replace_readwrite_async (GFile
*file
,
6137 gboolean make_backup
,
6138 GFileCreateFlags flags
,
6140 GCancellable
*cancellable
,
6141 GAsyncReadyCallback callback
,
6145 ReplaceRWAsyncData
*data
;
6147 data
= g_new0 (ReplaceRWAsyncData
, 1);
6148 data
->etag
= g_strdup (etag
);
6149 data
->make_backup
= make_backup
;
6150 data
->flags
= flags
;
6152 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6153 g_task_set_source_tag (task
, g_file_real_replace_readwrite_async
);
6154 g_task_set_task_data (task
, data
, (GDestroyNotify
)replace_rw_async_data_free
);
6155 g_task_set_priority (task
, io_priority
);
6157 g_task_run_in_thread (task
, replace_readwrite_async_thread
);
6158 g_object_unref (task
);
6161 static GFileIOStream
*
6162 g_file_real_replace_readwrite_finish (GFile
*file
,
6166 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6168 return g_task_propagate_pointer (G_TASK (res
), error
);
6172 set_display_name_async_thread (GTask
*task
,
6175 GCancellable
*cancellable
)
6177 GError
*error
= NULL
;
6178 char *name
= task_data
;
6181 file
= g_file_set_display_name (G_FILE (object
), name
, cancellable
, &error
);
6184 g_task_return_error (task
, error
);
6186 g_task_return_pointer (task
, file
, g_object_unref
);
6190 g_file_real_set_display_name_async (GFile
*file
,
6191 const char *display_name
,
6193 GCancellable
*cancellable
,
6194 GAsyncReadyCallback callback
,
6199 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6200 g_task_set_source_tag (task
, g_file_real_set_display_name_async
);
6201 g_task_set_task_data (task
, g_strdup (display_name
), g_free
);
6202 g_task_set_priority (task
, io_priority
);
6204 g_task_run_in_thread (task
, set_display_name_async_thread
);
6205 g_object_unref (task
);
6209 g_file_real_set_display_name_finish (GFile
*file
,
6213 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6215 return g_task_propagate_pointer (G_TASK (res
), error
);
6219 GFileQueryInfoFlags flags
;
6226 set_info_data_free (SetInfoAsyncData
*data
)
6229 g_object_unref (data
->info
);
6231 g_error_free (data
->error
);
6236 set_info_async_thread (GTask
*task
,
6239 GCancellable
*cancellable
)
6241 SetInfoAsyncData
*data
= task_data
;
6244 data
->res
= g_file_set_attributes_from_info (G_FILE (object
),
6252 g_file_real_set_attributes_async (GFile
*file
,
6254 GFileQueryInfoFlags flags
,
6256 GCancellable
*cancellable
,
6257 GAsyncReadyCallback callback
,
6261 SetInfoAsyncData
*data
;
6263 data
= g_new0 (SetInfoAsyncData
, 1);
6264 data
->info
= g_file_info_dup (info
);
6265 data
->flags
= flags
;
6267 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6268 g_task_set_source_tag (task
, g_file_real_set_attributes_async
);
6269 g_task_set_task_data (task
, data
, (GDestroyNotify
)set_info_data_free
);
6270 g_task_set_priority (task
, io_priority
);
6272 g_task_run_in_thread (task
, set_info_async_thread
);
6273 g_object_unref (task
);
6277 g_file_real_set_attributes_finish (GFile
*file
,
6282 SetInfoAsyncData
*data
;
6284 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
6286 data
= g_task_get_task_data (G_TASK (res
));
6289 *info
= g_object_ref (data
->info
);
6291 if (error
!= NULL
&& data
->error
)
6292 *error
= g_error_copy (data
->error
);
6298 find_enclosing_mount_async_thread (GTask
*task
,
6301 GCancellable
*cancellable
)
6303 GError
*error
= NULL
;
6306 mount
= g_file_find_enclosing_mount (G_FILE (object
), cancellable
, &error
);
6309 g_task_return_error (task
, error
);
6311 g_task_return_pointer (task
, mount
, g_object_unref
);
6315 g_file_real_find_enclosing_mount_async (GFile
*file
,
6317 GCancellable
*cancellable
,
6318 GAsyncReadyCallback callback
,
6323 task
= g_task_new (file
, cancellable
, callback
, user_data
);
6324 g_task_set_source_tag (task
, g_file_real_find_enclosing_mount_async
);
6325 g_task_set_priority (task
, io_priority
);
6327 g_task_run_in_thread (task
, find_enclosing_mount_async_thread
);
6328 g_object_unref (task
);
6332 g_file_real_find_enclosing_mount_finish (GFile
*file
,
6336 g_return_val_if_fail (g_task_is_valid (res
, file
), NULL
);
6338 return g_task_propagate_pointer (G_TASK (res
), error
);
6345 GFileCopyFlags flags
;
6346 GFileProgressCallback progress_cb
;
6347 gpointer progress_cb_data
;
6351 copy_async_data_free (CopyAsyncData
*data
)
6353 g_object_unref (data
->source
);
6354 g_object_unref (data
->destination
);
6355 g_slice_free (CopyAsyncData
, data
);
6359 CopyAsyncData
*data
;
6360 goffset current_num_bytes
;
6361 goffset total_num_bytes
;
6365 copy_async_progress_in_main (gpointer user_data
)
6367 ProgressData
*progress
= user_data
;
6368 CopyAsyncData
*data
= progress
->data
;
6370 data
->progress_cb (progress
->current_num_bytes
,
6371 progress
->total_num_bytes
,
6372 data
->progress_cb_data
);
6378 copy_async_progress_callback (goffset current_num_bytes
,
6379 goffset total_num_bytes
,
6382 GTask
*task
= user_data
;
6383 CopyAsyncData
*data
= g_task_get_task_data (task
);
6384 ProgressData
*progress
;
6386 progress
= g_new (ProgressData
, 1);
6387 progress
->data
= data
;
6388 progress
->current_num_bytes
= current_num_bytes
;
6389 progress
->total_num_bytes
= total_num_bytes
;
6391 g_main_context_invoke_full (g_task_get_context (task
),
6392 g_task_get_priority (task
),
6393 copy_async_progress_in_main
,
6399 copy_async_thread (GTask
*task
,
6402 GCancellable
*cancellable
)
6404 CopyAsyncData
*data
= task_data
;
6406 GError
*error
= NULL
;
6408 result
= g_file_copy (data
->source
,
6412 (data
->progress_cb
!= NULL
) ? copy_async_progress_callback
: NULL
,
6416 g_task_return_boolean (task
, TRUE
);
6418 g_task_return_error (task
, error
);
6422 g_file_real_copy_async (GFile
*source
,
6424 GFileCopyFlags flags
,
6426 GCancellable
*cancellable
,
6427 GFileProgressCallback progress_callback
,
6428 gpointer progress_callback_data
,
6429 GAsyncReadyCallback callback
,
6433 CopyAsyncData
*data
;
6435 data
= g_slice_new (CopyAsyncData
);
6436 data
->source
= g_object_ref (source
);
6437 data
->destination
= g_object_ref (destination
);
6438 data
->flags
= flags
;
6439 data
->progress_cb
= progress_callback
;
6440 data
->progress_cb_data
= progress_callback_data
;
6442 task
= g_task_new (source
, cancellable
, callback
, user_data
);
6443 g_task_set_source_tag (task
, g_file_real_copy_async
);
6444 g_task_set_task_data (task
, data
, (GDestroyNotify
)copy_async_data_free
);
6445 g_task_set_priority (task
, io_priority
);
6446 g_task_run_in_thread (task
, copy_async_thread
);
6447 g_object_unref (task
);
6451 g_file_real_copy_finish (GFile
*file
,
6455 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
6457 return g_task_propagate_boolean (G_TASK (res
), error
);
6461 /********************************************
6462 * Default VFS operations *
6463 ********************************************/
6466 * g_file_new_for_path:
6467 * @path: (type filename): a string containing a relative or absolute path.
6468 * The string must be encoded in the glib filename encoding.
6470 * Constructs a #GFile for a given path. This operation never
6471 * fails, but the returned object might not support any I/O
6472 * operation if @path is malformed.
6474 * Returns: (transfer full): a new #GFile for the given @path.
6475 * Free the returned object with g_object_unref().
6478 g_file_new_for_path (const char *path
)
6480 g_return_val_if_fail (path
!= NULL
, NULL
);
6482 return g_vfs_get_file_for_path (g_vfs_get_default (), path
);
6486 * g_file_new_for_uri:
6487 * @uri: a UTF-8 string containing a URI
6489 * Constructs a #GFile for a given URI. This operation never
6490 * fails, but the returned object might not support any I/O
6491 * operation if @uri is malformed or if the uri type is
6494 * Returns: (transfer full): a new #GFile for the given @uri.
6495 * Free the returned object with g_object_unref().
6498 g_file_new_for_uri (const char *uri
)
6500 g_return_val_if_fail (uri
!= NULL
, NULL
);
6502 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri
);
6507 * @tmpl: (type filename) (nullable): Template for the file
6508 * name, as in g_file_open_tmp(), or %NULL for a default template
6509 * @iostream: (out): on return, a #GFileIOStream for the created file
6510 * @error: a #GError, or %NULL
6512 * Opens a file in the preferred directory for temporary files (as
6513 * returned by g_get_tmp_dir()) and returns a #GFile and
6514 * #GFileIOStream pointing to it.
6516 * @tmpl should be a string in the GLib file name encoding
6517 * containing a sequence of six 'X' characters, and containing no
6518 * directory components. If it is %NULL, a default template is used.
6520 * Unlike the other #GFile constructors, this will return %NULL if
6521 * a temporary file could not be created.
6523 * Returns: (transfer full): a new #GFile.
6524 * Free the returned object with g_object_unref().
6529 g_file_new_tmp (const char *tmpl
,
6530 GFileIOStream
**iostream
,
6536 GFileOutputStream
*output
;
6538 g_return_val_if_fail (iostream
!= NULL
, NULL
);
6540 fd
= g_file_open_tmp (tmpl
, &path
, error
);
6544 file
= g_file_new_for_path (path
);
6546 output
= _g_local_file_output_stream_new (fd
);
6547 *iostream
= _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output
));
6549 g_object_unref (output
);
6556 * g_file_parse_name:
6557 * @parse_name: a file name or path to be parsed
6559 * Constructs a #GFile with the given @parse_name (i.e. something
6560 * given by g_file_get_parse_name()). This operation never fails,
6561 * but the returned object might not support any I/O operation if
6562 * the @parse_name cannot be parsed.
6564 * Returns: (transfer full): a new #GFile.
6567 g_file_parse_name (const char *parse_name
)
6569 g_return_val_if_fail (parse_name
!= NULL
, NULL
);
6571 return g_vfs_parse_name (g_vfs_get_default (), parse_name
);
6575 * g_file_new_build_filename:
6576 * @first_element: (type filename): the first element in the path
6577 * @...: remaining elements in path, terminated by %NULL
6579 * Constructs a #GFile from a series of elements using the correct
6580 * separator for filenames.
6582 * Using this function is equivalent to calling g_build_filename(),
6583 * followed by g_file_new_for_path() on the result.
6585 * Returns: (transfer full): a new #GFile
6590 g_file_new_build_filename (const gchar
*first_element
,
6597 g_return_val_if_fail (first_element
!= NULL
, NULL
);
6599 va_start (args
, first_element
);
6600 str
= g_build_filename_valist (first_element
, &args
);
6603 file
= g_file_new_for_path (str
);
6610 is_valid_scheme_character (char c
)
6612 return g_ascii_isalnum (c
) || c
== '+' || c
== '-' || c
== '.';
6615 /* Following RFC 2396, valid schemes are built like:
6616 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6619 has_valid_scheme (const char *uri
)
6625 if (!g_ascii_isalpha (*p
))
6630 } while (is_valid_scheme_character (*p
));
6636 new_for_cmdline_arg (const gchar
*arg
,
6642 if (g_path_is_absolute (arg
))
6643 return g_file_new_for_path (arg
);
6645 if (has_valid_scheme (arg
))
6646 return g_file_new_for_uri (arg
);
6652 current_dir
= g_get_current_dir ();
6653 filename
= g_build_filename (current_dir
, arg
, NULL
);
6654 g_free (current_dir
);
6657 filename
= g_build_filename (cwd
, arg
, NULL
);
6659 file
= g_file_new_for_path (filename
);
6666 * g_file_new_for_commandline_arg:
6667 * @arg: (type filename): a command line string
6669 * Creates a #GFile with the given argument from the command line.
6670 * The value of @arg can be either a URI, an absolute path or a
6671 * relative path resolved relative to the current working directory.
6672 * This operation never fails, but the returned object might not
6673 * support any I/O operation if @arg points to a malformed path.
6675 * Note that on Windows, this function expects its argument to be in
6676 * UTF-8 -- not the system code page. This means that you
6677 * should not use this function with string from argv as it is passed
6678 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6679 * the commandline. #GApplication also uses UTF-8 but
6680 * g_application_command_line_create_file_for_arg() may be more useful
6681 * for you there. It is also always possible to use this function with
6682 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6684 * Returns: (transfer full): a new #GFile.
6685 * Free the returned object with g_object_unref().
6688 g_file_new_for_commandline_arg (const char *arg
)
6690 g_return_val_if_fail (arg
!= NULL
, NULL
);
6692 return new_for_cmdline_arg (arg
, NULL
);
6696 * g_file_new_for_commandline_arg_and_cwd:
6697 * @arg: (type filename): a command line string
6698 * @cwd: (type filename): the current working directory of the commandline
6700 * Creates a #GFile with the given argument from the command line.
6702 * This function is similar to g_file_new_for_commandline_arg() except
6703 * that it allows for passing the current working directory as an
6704 * argument instead of using the current working directory of the
6707 * This is useful if the commandline argument was given in a context
6708 * other than the invocation of the current process.
6710 * See also g_application_command_line_create_file_for_arg().
6712 * Returns: (transfer full): a new #GFile
6717 g_file_new_for_commandline_arg_and_cwd (const gchar
*arg
,
6720 g_return_val_if_fail (arg
!= NULL
, NULL
);
6721 g_return_val_if_fail (cwd
!= NULL
, NULL
);
6723 return new_for_cmdline_arg (arg
, cwd
);
6727 * g_file_mount_enclosing_volume:
6728 * @location: input #GFile
6729 * @flags: flags affecting the operation
6730 * @mount_operation: (nullable): a #GMountOperation
6731 * or %NULL to avoid user interaction
6732 * @cancellable: (nullable): optional #GCancellable object,
6734 * @callback: (nullable): a #GAsyncReadyCallback to call
6735 * when the request is satisfied, or %NULL
6736 * @user_data: the data to pass to callback function
6738 * Starts a @mount_operation, mounting the volume that contains
6739 * the file @location.
6741 * When this operation has completed, @callback will be called with
6742 * @user_user data, and the operation can be finalized with
6743 * g_file_mount_enclosing_volume_finish().
6745 * If @cancellable is not %NULL, then the operation can be cancelled by
6746 * triggering the cancellable object from another thread. If the operation
6747 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6750 g_file_mount_enclosing_volume (GFile
*location
,
6751 GMountMountFlags flags
,
6752 GMountOperation
*mount_operation
,
6753 GCancellable
*cancellable
,
6754 GAsyncReadyCallback callback
,
6759 g_return_if_fail (G_IS_FILE (location
));
6761 iface
= G_FILE_GET_IFACE (location
);
6763 if (iface
->mount_enclosing_volume
== NULL
)
6765 g_task_report_new_error (location
, callback
, user_data
,
6766 g_file_mount_enclosing_volume
,
6767 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
6768 _("volume doesn’t implement mount"));
6772 (* iface
->mount_enclosing_volume
) (location
, flags
, mount_operation
, cancellable
, callback
, user_data
);
6777 * g_file_mount_enclosing_volume_finish:
6778 * @location: input #GFile
6779 * @result: a #GAsyncResult
6780 * @error: a #GError, or %NULL
6782 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6784 * Returns: %TRUE if successful. If an error has occurred,
6785 * this function will return %FALSE and set @error
6786 * appropriately if present.
6789 g_file_mount_enclosing_volume_finish (GFile
*location
,
6790 GAsyncResult
*result
,
6795 g_return_val_if_fail (G_IS_FILE (location
), FALSE
);
6796 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
6798 if (g_async_result_legacy_propagate_error (result
, error
))
6800 else if (g_async_result_is_tagged (result
, g_file_mount_enclosing_volume
))
6801 return g_task_propagate_boolean (G_TASK (result
), error
);
6803 iface
= G_FILE_GET_IFACE (location
);
6805 return (* iface
->mount_enclosing_volume_finish
) (location
, result
, error
);
6808 /********************************************
6809 * Utility functions *
6810 ********************************************/
6813 * g_file_query_default_handler:
6814 * @file: a #GFile to open
6815 * @cancellable: optional #GCancellable object, %NULL to ignore
6816 * @error: a #GError, or %NULL
6818 * Returns the #GAppInfo that is registered as the default
6819 * application to handle the file specified by @file.
6821 * If @cancellable is not %NULL, then the operation can be cancelled by
6822 * triggering the cancellable object from another thread. If the operation
6823 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6825 * Returns: (transfer full): a #GAppInfo if the handle was found,
6826 * %NULL if there were errors.
6827 * When you are done with it, release it with g_object_unref()
6830 g_file_query_default_handler (GFile
*file
,
6831 GCancellable
*cancellable
,
6835 const char *content_type
;
6840 uri_scheme
= g_file_get_uri_scheme (file
);
6841 if (uri_scheme
&& uri_scheme
[0] != '\0')
6843 appinfo
= g_app_info_get_default_for_uri_scheme (uri_scheme
);
6844 g_free (uri_scheme
);
6846 if (appinfo
!= NULL
)
6850 info
= g_file_query_info (file
,
6851 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
,
6860 content_type
= g_file_info_get_content_type (info
);
6863 /* Don't use is_native(), as we want to support fuse paths if available */
6864 path
= g_file_get_path (file
);
6865 appinfo
= g_app_info_get_default_for_type (content_type
,
6870 g_object_unref (info
);
6872 if (appinfo
!= NULL
)
6875 g_set_error_literal (error
, G_IO_ERROR
,
6876 G_IO_ERROR_NOT_SUPPORTED
,
6877 _("No application is registered as handling this file"));
6881 #define GET_CONTENT_BLOCK_SIZE 8192
6884 * g_file_load_contents:
6885 * @file: input #GFile
6886 * @cancellable: optional #GCancellable object, %NULL to ignore
6887 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6888 * @length: (out) (optional): a location to place the length of the contents of the file,
6889 * or %NULL if the length is not needed
6890 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
6891 * or %NULL if the entity tag is not needed
6892 * @error: a #GError, or %NULL
6894 * Loads the content of the file into memory. The data is always
6895 * zero-terminated, but this is not included in the resultant @length.
6896 * The returned @content should be freed with g_free() when no longer
6899 * If @cancellable is not %NULL, then the operation can be cancelled by
6900 * triggering the cancellable object from another thread. If the operation
6901 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6903 * Returns: %TRUE if the @file's contents were successfully loaded.
6904 * %FALSE if there were errors.
6907 g_file_load_contents (GFile
*file
,
6908 GCancellable
*cancellable
,
6914 GFileInputStream
*in
;
6915 GByteArray
*content
;
6920 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
6921 g_return_val_if_fail (contents
!= NULL
, FALSE
);
6923 in
= g_file_read (file
, cancellable
, error
);
6927 content
= g_byte_array_new ();
6930 g_byte_array_set_size (content
, pos
+ GET_CONTENT_BLOCK_SIZE
+ 1);
6931 while ((res
= g_input_stream_read (G_INPUT_STREAM (in
),
6932 content
->data
+ pos
,
6933 GET_CONTENT_BLOCK_SIZE
,
6934 cancellable
, error
)) > 0)
6937 g_byte_array_set_size (content
, pos
+ GET_CONTENT_BLOCK_SIZE
+ 1);
6944 info
= g_file_input_stream_query_info (in
,
6945 G_FILE_ATTRIBUTE_ETAG_VALUE
,
6950 *etag_out
= g_strdup (g_file_info_get_etag (info
));
6951 g_object_unref (info
);
6955 /* Ignore errors on close */
6956 g_input_stream_close (G_INPUT_STREAM (in
), cancellable
, NULL
);
6957 g_object_unref (in
);
6961 /* error is set already */
6962 g_byte_array_free (content
, TRUE
);
6969 /* Zero terminate (we got an extra byte allocated for this */
6970 content
->data
[pos
] = 0;
6972 *contents
= (char *)g_byte_array_free (content
, FALSE
);
6979 GFileReadMoreCallback read_more_callback
;
6980 GByteArray
*content
;
6987 load_contents_data_free (LoadContentsData
*data
)
6990 g_byte_array_free (data
->content
, TRUE
);
6991 g_free (data
->etag
);
6996 load_contents_close_callback (GObject
*obj
,
6997 GAsyncResult
*close_res
,
7000 GInputStream
*stream
= G_INPUT_STREAM (obj
);
7001 LoadContentsData
*data
= user_data
;
7003 /* Ignore errors here, we're only reading anyway */
7004 g_input_stream_close_finish (stream
, close_res
, NULL
);
7005 g_object_unref (stream
);
7007 g_task_return_boolean (data
->task
, TRUE
);
7008 g_object_unref (data
->task
);
7012 load_contents_fstat_callback (GObject
*obj
,
7013 GAsyncResult
*stat_res
,
7016 GInputStream
*stream
= G_INPUT_STREAM (obj
);
7017 LoadContentsData
*data
= user_data
;
7020 info
= g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream
),
7024 data
->etag
= g_strdup (g_file_info_get_etag (info
));
7025 g_object_unref (info
);
7028 g_input_stream_close_async (stream
, 0,
7029 g_task_get_cancellable (data
->task
),
7030 load_contents_close_callback
, data
);
7034 load_contents_read_callback (GObject
*obj
,
7035 GAsyncResult
*read_res
,
7038 GInputStream
*stream
= G_INPUT_STREAM (obj
);
7039 LoadContentsData
*data
= user_data
;
7040 GError
*error
= NULL
;
7043 read_size
= g_input_stream_read_finish (stream
, read_res
, &error
);
7047 g_task_return_error (data
->task
, error
);
7048 g_object_unref (data
->task
);
7050 /* Close the file ignoring any error */
7051 g_input_stream_close_async (stream
, 0, NULL
, NULL
, NULL
);
7052 g_object_unref (stream
);
7054 else if (read_size
== 0)
7056 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream
),
7057 G_FILE_ATTRIBUTE_ETAG_VALUE
,
7059 g_task_get_cancellable (data
->task
),
7060 load_contents_fstat_callback
,
7063 else if (read_size
> 0)
7065 data
->pos
+= read_size
;
7067 g_byte_array_set_size (data
->content
,
7068 data
->pos
+ GET_CONTENT_BLOCK_SIZE
);
7071 if (data
->read_more_callback
&&
7072 !data
->read_more_callback ((char *)data
->content
->data
, data
->pos
,
7073 g_async_result_get_user_data (G_ASYNC_RESULT (data
->task
))))
7074 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream
),
7075 G_FILE_ATTRIBUTE_ETAG_VALUE
,
7077 g_task_get_cancellable (data
->task
),
7078 load_contents_fstat_callback
,
7081 g_input_stream_read_async (stream
,
7082 data
->content
->data
+ data
->pos
,
7083 GET_CONTENT_BLOCK_SIZE
,
7085 g_task_get_cancellable (data
->task
),
7086 load_contents_read_callback
,
7092 load_contents_open_callback (GObject
*obj
,
7093 GAsyncResult
*open_res
,
7096 GFile
*file
= G_FILE (obj
);
7097 GFileInputStream
*stream
;
7098 LoadContentsData
*data
= user_data
;
7099 GError
*error
= NULL
;
7101 stream
= g_file_read_finish (file
, open_res
, &error
);
7105 g_byte_array_set_size (data
->content
,
7106 data
->pos
+ GET_CONTENT_BLOCK_SIZE
);
7107 g_input_stream_read_async (G_INPUT_STREAM (stream
),
7108 data
->content
->data
+ data
->pos
,
7109 GET_CONTENT_BLOCK_SIZE
,
7111 g_task_get_cancellable (data
->task
),
7112 load_contents_read_callback
,
7117 g_task_return_error (data
->task
, error
);
7118 g_object_unref (data
->task
);
7123 * g_file_load_partial_contents_async: (skip)
7124 * @file: input #GFile
7125 * @cancellable: optional #GCancellable object, %NULL to ignore
7126 * @read_more_callback: (scope call) (closure user_data): a
7127 * #GFileReadMoreCallback to receive partial data
7128 * and to specify whether further data should be read
7129 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback to call
7130 * when the request is satisfied
7131 * @user_data: the data to pass to the callback functions
7133 * Reads the partial contents of a file. A #GFileReadMoreCallback should
7134 * be used to stop reading from the file when appropriate, else this
7135 * function will behave exactly as g_file_load_contents_async(). This
7136 * operation can be finished by g_file_load_partial_contents_finish().
7138 * Users of this function should be aware that @user_data is passed to
7139 * both the @read_more_callback and the @callback.
7141 * If @cancellable is not %NULL, then the operation can be cancelled by
7142 * triggering the cancellable object from another thread. If the operation
7143 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7146 g_file_load_partial_contents_async (GFile
*file
,
7147 GCancellable
*cancellable
,
7148 GFileReadMoreCallback read_more_callback
,
7149 GAsyncReadyCallback callback
,
7152 LoadContentsData
*data
;
7154 g_return_if_fail (G_IS_FILE (file
));
7156 data
= g_new0 (LoadContentsData
, 1);
7157 data
->read_more_callback
= read_more_callback
;
7158 data
->content
= g_byte_array_new ();
7160 data
->task
= g_task_new (file
, cancellable
, callback
, user_data
);
7161 g_task_set_source_tag (data
->task
, g_file_load_partial_contents_async
);
7162 g_task_set_task_data (data
->task
, data
, (GDestroyNotify
)load_contents_data_free
);
7164 g_file_read_async (file
,
7166 g_task_get_cancellable (data
->task
),
7167 load_contents_open_callback
,
7172 * g_file_load_partial_contents_finish:
7173 * @file: input #GFile
7174 * @res: a #GAsyncResult
7175 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7176 * @length: (out) (optional): a location to place the length of the contents of the file,
7177 * or %NULL if the length is not needed
7178 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7179 * or %NULL if the entity tag is not needed
7180 * @error: a #GError, or %NULL
7182 * Finishes an asynchronous partial load operation that was started
7183 * with g_file_load_partial_contents_async(). The data is always
7184 * zero-terminated, but this is not included in the resultant @length.
7185 * The returned @content should be freed with g_free() when no longer
7188 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7189 * present, it will be set appropriately.
7192 g_file_load_partial_contents_finish (GFile
*file
,
7200 LoadContentsData
*data
;
7202 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7203 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
7204 g_return_val_if_fail (contents
!= NULL
, FALSE
);
7206 task
= G_TASK (res
);
7208 if (!g_task_propagate_boolean (task
, error
))
7215 data
= g_task_get_task_data (task
);
7218 *length
= data
->pos
;
7222 *etag_out
= data
->etag
;
7226 /* Zero terminate */
7227 g_byte_array_set_size (data
->content
, data
->pos
+ 1);
7228 data
->content
->data
[data
->pos
] = 0;
7230 *contents
= (char *)g_byte_array_free (data
->content
, FALSE
);
7231 data
->content
= NULL
;
7237 * g_file_load_contents_async:
7238 * @file: input #GFile
7239 * @cancellable: optional #GCancellable object, %NULL to ignore
7240 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7241 * @user_data: the data to pass to callback function
7243 * Starts an asynchronous load of the @file's contents.
7245 * For more details, see g_file_load_contents() which is
7246 * the synchronous version of this call.
7248 * When the load operation has completed, @callback will be called
7249 * with @user data. To finish the operation, call
7250 * g_file_load_contents_finish() with the #GAsyncResult returned by
7253 * If @cancellable is not %NULL, then the operation can be cancelled by
7254 * triggering the cancellable object from another thread. If the operation
7255 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7258 g_file_load_contents_async (GFile
*file
,
7259 GCancellable
*cancellable
,
7260 GAsyncReadyCallback callback
,
7263 g_file_load_partial_contents_async (file
,
7266 callback
, user_data
);
7270 * g_file_load_contents_finish:
7271 * @file: input #GFile
7272 * @res: a #GAsyncResult
7273 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7274 * @length: (out) (optional): a location to place the length of the contents of the file,
7275 * or %NULL if the length is not needed
7276 * @etag_out: (out) (optional): a location to place the current entity tag for the file,
7277 * or %NULL if the entity tag is not needed
7278 * @error: a #GError, or %NULL
7280 * Finishes an asynchronous load of the @file's contents.
7281 * The contents are placed in @contents, and @length is set to the
7282 * size of the @contents string. The @content should be freed with
7283 * g_free() when no longer needed. If @etag_out is present, it will be
7284 * set to the new entity tag for the @file.
7286 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7287 * present, it will be set appropriately.
7290 g_file_load_contents_finish (GFile
*file
,
7297 return g_file_load_partial_contents_finish (file
,
7306 * g_file_replace_contents:
7307 * @file: input #GFile
7308 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7309 * @length: the length of @contents in bytes
7310 * @etag: (nullable): the old [entity-tag][gfile-etag] for the document,
7312 * @make_backup: %TRUE if a backup should be created
7313 * @flags: a set of #GFileCreateFlags
7314 * @new_etag: (out) (optional): a location to a new [entity tag][gfile-etag]
7315 * for the document. This should be freed with g_free() when no longer
7317 * @cancellable: optional #GCancellable object, %NULL to ignore
7318 * @error: a #GError, or %NULL
7320 * Replaces the contents of @file with @contents of @length bytes.
7322 * If @etag is specified (not %NULL), any existing file must have that etag,
7323 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7325 * If @make_backup is %TRUE, this function will attempt to make a backup
7326 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7327 * file contents in the safest way possible. For example, atomic renames are
7328 * used when replacing local files’ contents.
7330 * If @cancellable is not %NULL, then the operation can be cancelled by
7331 * triggering the cancellable object from another thread. If the operation
7332 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7334 * The returned @new_etag can be used to verify that the file hasn't
7335 * changed the next time it is saved over.
7337 * Returns: %TRUE if successful. If an error has occurred, this function
7338 * will return %FALSE and set @error appropriately if present.
7341 g_file_replace_contents (GFile
*file
,
7342 const char *contents
,
7345 gboolean make_backup
,
7346 GFileCreateFlags flags
,
7348 GCancellable
*cancellable
,
7351 GFileOutputStream
*out
;
7352 gsize pos
, remainder
;
7356 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7357 g_return_val_if_fail (contents
!= NULL
, FALSE
);
7359 out
= g_file_replace (file
, etag
, make_backup
, flags
, cancellable
, error
);
7365 while (remainder
> 0 &&
7366 (res
= g_output_stream_write (G_OUTPUT_STREAM (out
),
7368 MIN (remainder
, GET_CONTENT_BLOCK_SIZE
),
7376 if (remainder
> 0 && res
< 0)
7378 /* Ignore errors on close */
7379 g_output_stream_close (G_OUTPUT_STREAM (out
), cancellable
, NULL
);
7380 g_object_unref (out
);
7382 /* error is set already */
7386 ret
= g_output_stream_close (G_OUTPUT_STREAM (out
), cancellable
, error
);
7389 *new_etag
= g_file_output_stream_get_etag (out
);
7391 g_object_unref (out
);
7402 } ReplaceContentsData
;
7405 replace_contents_data_free (ReplaceContentsData
*data
)
7407 g_bytes_unref (data
->content
);
7408 g_free (data
->etag
);
7413 replace_contents_close_callback (GObject
*obj
,
7414 GAsyncResult
*close_res
,
7417 GOutputStream
*stream
= G_OUTPUT_STREAM (obj
);
7418 ReplaceContentsData
*data
= user_data
;
7420 /* Ignore errors here, we're only reading anyway */
7421 g_output_stream_close_finish (stream
, close_res
, NULL
);
7422 g_object_unref (stream
);
7426 data
->etag
= g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream
));
7427 g_task_return_boolean (data
->task
, TRUE
);
7429 g_object_unref (data
->task
);
7433 replace_contents_write_callback (GObject
*obj
,
7434 GAsyncResult
*read_res
,
7437 GOutputStream
*stream
= G_OUTPUT_STREAM (obj
);
7438 ReplaceContentsData
*data
= user_data
;
7439 GError
*error
= NULL
;
7442 write_size
= g_output_stream_write_finish (stream
, read_res
, &error
);
7444 if (write_size
<= 0)
7446 /* Error or EOF, close the file */
7449 data
->failed
= TRUE
;
7450 g_task_return_error (data
->task
, error
);
7452 g_output_stream_close_async (stream
, 0,
7453 g_task_get_cancellable (data
->task
),
7454 replace_contents_close_callback
, data
);
7456 else if (write_size
> 0)
7458 const gchar
*content
;
7461 content
= g_bytes_get_data (data
->content
, &length
);
7462 data
->pos
+= write_size
;
7464 if (data
->pos
>= length
)
7465 g_output_stream_close_async (stream
, 0,
7466 g_task_get_cancellable (data
->task
),
7467 replace_contents_close_callback
, data
);
7469 g_output_stream_write_async (stream
,
7470 content
+ data
->pos
,
7473 g_task_get_cancellable (data
->task
),
7474 replace_contents_write_callback
,
7480 replace_contents_open_callback (GObject
*obj
,
7481 GAsyncResult
*open_res
,
7484 GFile
*file
= G_FILE (obj
);
7485 GFileOutputStream
*stream
;
7486 ReplaceContentsData
*data
= user_data
;
7487 GError
*error
= NULL
;
7489 stream
= g_file_replace_finish (file
, open_res
, &error
);
7493 const gchar
*content
;
7496 content
= g_bytes_get_data (data
->content
, &length
);
7497 g_output_stream_write_async (G_OUTPUT_STREAM (stream
),
7498 content
+ data
->pos
,
7501 g_task_get_cancellable (data
->task
),
7502 replace_contents_write_callback
,
7507 g_task_return_error (data
->task
, error
);
7508 g_object_unref (data
->task
);
7513 * g_file_replace_contents_async:
7514 * @file: input #GFile
7515 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7516 * @length: the length of @contents in bytes
7517 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7518 * @make_backup: %TRUE if a backup should be created
7519 * @flags: a set of #GFileCreateFlags
7520 * @cancellable: optional #GCancellable object, %NULL to ignore
7521 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7522 * @user_data: the data to pass to callback function
7524 * Starts an asynchronous replacement of @file with the given
7525 * @contents of @length bytes. @etag will replace the document's
7526 * current entity tag.
7528 * When this operation has completed, @callback will be called with
7529 * @user_user data, and the operation can be finalized with
7530 * g_file_replace_contents_finish().
7532 * If @cancellable is not %NULL, then the operation can be cancelled by
7533 * triggering the cancellable object from another thread. If the operation
7534 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7536 * If @make_backup is %TRUE, this function will attempt to
7537 * make a backup of @file.
7539 * Note that no copy of @content will be made, so it must stay valid
7540 * until @callback is called. See g_file_replace_contents_bytes_async()
7541 * for a #GBytes version that will automatically hold a reference to the
7542 * contents (without copying) for the duration of the call.
7545 g_file_replace_contents_async (GFile
*file
,
7546 const char *contents
,
7549 gboolean make_backup
,
7550 GFileCreateFlags flags
,
7551 GCancellable
*cancellable
,
7552 GAsyncReadyCallback callback
,
7557 bytes
= g_bytes_new_static (contents
, length
);
7558 g_file_replace_contents_bytes_async (file
, bytes
, etag
, make_backup
, flags
,
7559 cancellable
, callback
, user_data
);
7560 g_bytes_unref (bytes
);
7564 * g_file_replace_contents_bytes_async:
7565 * @file: input #GFile
7566 * @contents: a #GBytes
7567 * @etag: (nullable): a new [entity tag][gfile-etag] for the @file, or %NULL
7568 * @make_backup: %TRUE if a backup should be created
7569 * @flags: a set of #GFileCreateFlags
7570 * @cancellable: optional #GCancellable object, %NULL to ignore
7571 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7572 * @user_data: the data to pass to callback function
7574 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7575 * This function will keep a ref on @contents until the operation is done.
7576 * Unlike g_file_replace_contents_async() this allows forgetting about the
7577 * content without waiting for the callback.
7579 * When this operation has completed, @callback will be called with
7580 * @user_user data, and the operation can be finalized with
7581 * g_file_replace_contents_finish().
7586 g_file_replace_contents_bytes_async (GFile
*file
,
7589 gboolean make_backup
,
7590 GFileCreateFlags flags
,
7591 GCancellable
*cancellable
,
7592 GAsyncReadyCallback callback
,
7595 ReplaceContentsData
*data
;
7597 g_return_if_fail (G_IS_FILE (file
));
7598 g_return_if_fail (contents
!= NULL
);
7600 data
= g_new0 (ReplaceContentsData
, 1);
7602 data
->content
= g_bytes_ref (contents
);
7604 data
->task
= g_task_new (file
, cancellable
, callback
, user_data
);
7605 g_task_set_source_tag (data
->task
, g_file_replace_contents_bytes_async
);
7606 g_task_set_task_data (data
->task
, data
, (GDestroyNotify
)replace_contents_data_free
);
7608 g_file_replace_async (file
,
7613 g_task_get_cancellable (data
->task
),
7614 replace_contents_open_callback
,
7619 * g_file_replace_contents_finish:
7620 * @file: input #GFile
7621 * @res: a #GAsyncResult
7622 * @new_etag: (out) (optional): a location of a new [entity tag][gfile-etag]
7623 * for the document. This should be freed with g_free() when it is no
7624 * longer needed, or %NULL
7625 * @error: a #GError, or %NULL
7627 * Finishes an asynchronous replace of the given @file. See
7628 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7629 * tag for the document, if present.
7631 * Returns: %TRUE on success, %FALSE on failure.
7634 g_file_replace_contents_finish (GFile
*file
,
7640 ReplaceContentsData
*data
;
7642 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7643 g_return_val_if_fail (g_task_is_valid (res
, file
), FALSE
);
7645 task
= G_TASK (res
);
7647 if (!g_task_propagate_boolean (task
, error
))
7650 data
= g_task_get_task_data (task
);
7654 *new_etag
= data
->etag
;
7655 data
->etag
= NULL
; /* Take ownership */
7662 g_file_real_measure_disk_usage (GFile
*file
,
7663 GFileMeasureFlags flags
,
7664 GCancellable
*cancellable
,
7665 GFileMeasureProgressCallback progress_callback
,
7666 gpointer progress_data
,
7667 guint64
*disk_usage
,
7672 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
7673 "Operation not supported for the current backend.");
7679 GFileMeasureFlags flags
;
7680 GFileMeasureProgressCallback progress_callback
;
7681 gpointer progress_data
;
7693 GFileMeasureProgressCallback callback
;
7696 guint64 current_size
;
7702 measure_disk_usage_invoke_progress (gpointer user_data
)
7704 MeasureProgress
*progress
= user_data
;
7706 (* progress
->callback
) (progress
->reporting
,
7707 progress
->current_size
, progress
->num_dirs
, progress
->num_files
,
7708 progress
->user_data
);
7714 measure_disk_usage_progress (gboolean reporting
,
7715 guint64 current_size
,
7720 MeasureProgress progress
;
7721 GTask
*task
= user_data
;
7722 MeasureTaskData
*data
;
7724 data
= g_task_get_task_data (task
);
7726 progress
.callback
= data
->progress_callback
;
7727 progress
.user_data
= data
->progress_data
;
7728 progress
.reporting
= reporting
;
7729 progress
.current_size
= current_size
;
7730 progress
.num_dirs
= num_dirs
;
7731 progress
.num_files
= num_files
;
7733 g_main_context_invoke_full (g_task_get_context (task
),
7734 g_task_get_priority (task
),
7735 measure_disk_usage_invoke_progress
,
7736 g_memdup (&progress
, sizeof progress
),
7741 measure_disk_usage_thread (GTask
*task
,
7742 gpointer source_object
,
7744 GCancellable
*cancellable
)
7746 MeasureTaskData
*data
= task_data
;
7747 GError
*error
= NULL
;
7748 MeasureResult result
= { 0, };
7750 if (g_file_measure_disk_usage (source_object
, data
->flags
, cancellable
,
7751 data
->progress_callback
? measure_disk_usage_progress
: NULL
, task
,
7752 &result
.disk_usage
, &result
.num_dirs
, &result
.num_files
,
7754 g_task_return_pointer (task
, g_memdup (&result
, sizeof result
), g_free
);
7756 g_task_return_error (task
, error
);
7760 g_file_real_measure_disk_usage_async (GFile
*file
,
7761 GFileMeasureFlags flags
,
7763 GCancellable
*cancellable
,
7764 GFileMeasureProgressCallback progress_callback
,
7765 gpointer progress_data
,
7766 GAsyncReadyCallback callback
,
7769 MeasureTaskData data
;
7773 data
.progress_callback
= progress_callback
;
7774 data
.progress_data
= progress_data
;
7776 task
= g_task_new (file
, cancellable
, callback
, user_data
);
7777 g_task_set_source_tag (task
, g_file_real_measure_disk_usage_async
);
7778 g_task_set_task_data (task
, g_memdup (&data
, sizeof data
), g_free
);
7779 g_task_set_priority (task
, io_priority
);
7781 g_task_run_in_thread (task
, measure_disk_usage_thread
);
7782 g_object_unref (task
);
7786 g_file_real_measure_disk_usage_finish (GFile
*file
,
7787 GAsyncResult
*result
,
7788 guint64
*disk_usage
,
7793 MeasureResult
*measure_result
;
7795 g_return_val_if_fail (g_task_is_valid (result
, file
), FALSE
);
7797 measure_result
= g_task_propagate_pointer (G_TASK (result
), error
);
7799 if (measure_result
== NULL
)
7803 *disk_usage
= measure_result
->disk_usage
;
7806 *num_dirs
= measure_result
->num_dirs
;
7809 *num_files
= measure_result
->num_files
;
7811 g_free (measure_result
);
7817 * g_file_measure_disk_usage:
7819 * @flags: #GFileMeasureFlags
7820 * @cancellable: (nullable): optional #GCancellable
7821 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7822 * @progress_data: user_data for @progress_callback
7823 * @disk_usage: (out) (optional): the number of bytes of disk space used
7824 * @num_dirs: (out) (optional): the number of directories encountered
7825 * @num_files: (out) (optional): the number of non-directories encountered
7826 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7828 * Recursively measures the disk usage of @file.
7830 * This is essentially an analog of the 'du' command, but it also
7831 * reports the number of directories and non-directory files encountered
7832 * (including things like symbolic links).
7834 * By default, errors are only reported against the toplevel file
7835 * itself. Errors found while recursing are silently ignored, unless
7836 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7838 * The returned size, @disk_usage, is in bytes and should be formatted
7839 * with g_format_size() in order to get something reasonable for showing
7840 * in a user interface.
7842 * @progress_callback and @progress_data can be given to request
7843 * periodic progress updates while scanning. See the documentation for
7844 * #GFileMeasureProgressCallback for information about when and how the
7845 * callback will be invoked.
7847 * Returns: %TRUE if successful, with the out parameters set.
7848 * %FALSE otherwise, with @error set.
7853 g_file_measure_disk_usage (GFile
*file
,
7854 GFileMeasureFlags flags
,
7855 GCancellable
*cancellable
,
7856 GFileMeasureProgressCallback progress_callback
,
7857 gpointer progress_data
,
7858 guint64
*disk_usage
,
7863 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7864 g_return_val_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
), FALSE
);
7865 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
7867 return G_FILE_GET_IFACE (file
)->measure_disk_usage (file
, flags
, cancellable
,
7868 progress_callback
, progress_data
,
7869 disk_usage
, num_dirs
, num_files
,
7874 * g_file_measure_disk_usage_async:
7876 * @flags: #GFileMeasureFlags
7877 * @io_priority: the [I/O priority][io-priority] of the request
7878 * @cancellable: (nullable): optional #GCancellable
7879 * @progress_callback: (nullable): a #GFileMeasureProgressCallback
7880 * @progress_data: user_data for @progress_callback
7881 * @callback: (nullable): a #GAsyncReadyCallback to call when complete
7882 * @user_data: the data to pass to callback function
7884 * Recursively measures the disk usage of @file.
7886 * This is the asynchronous version of g_file_measure_disk_usage(). See
7887 * there for more information.
7892 g_file_measure_disk_usage_async (GFile
*file
,
7893 GFileMeasureFlags flags
,
7895 GCancellable
*cancellable
,
7896 GFileMeasureProgressCallback progress_callback
,
7897 gpointer progress_data
,
7898 GAsyncReadyCallback callback
,
7901 g_return_if_fail (G_IS_FILE (file
));
7902 g_return_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
));
7904 G_FILE_GET_IFACE (file
)->measure_disk_usage_async (file
, flags
, io_priority
, cancellable
,
7905 progress_callback
, progress_data
,
7906 callback
, user_data
);
7910 * g_file_measure_disk_usage_finish:
7912 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7913 * @disk_usage: (out) (optional): the number of bytes of disk space used
7914 * @num_dirs: (out) (optional): the number of directories encountered
7915 * @num_files: (out) (optional): the number of non-directories encountered
7916 * @error: (nullable): %NULL, or a pointer to a %NULL #GError pointer
7918 * Collects the results from an earlier call to
7919 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7922 * Returns: %TRUE if successful, with the out parameters set.
7923 * %FALSE otherwise, with @error set.
7928 g_file_measure_disk_usage_finish (GFile
*file
,
7929 GAsyncResult
*result
,
7930 guint64
*disk_usage
,
7935 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
7936 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, FALSE
);
7938 return G_FILE_GET_IFACE (file
)->measure_disk_usage_finish (file
, result
, disk_usage
, num_dirs
, num_files
, error
);
7942 * g_file_start_mountable:
7943 * @file: input #GFile
7944 * @flags: flags affecting the operation
7945 * @start_operation: (nullable): a #GMountOperation, or %NULL to avoid user interaction
7946 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
7947 * @callback: (nullable): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7948 * @user_data: the data to pass to callback function
7950 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7951 * Using @start_operation, you can request callbacks when, for instance,
7952 * passwords are needed during authentication.
7954 * If @cancellable is not %NULL, then the operation can be cancelled by
7955 * triggering the cancellable object from another thread. If the operation
7956 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7958 * When the operation is finished, @callback will be called.
7959 * You can then call g_file_mount_mountable_finish() to get
7960 * the result of the operation.
7965 g_file_start_mountable (GFile
*file
,
7966 GDriveStartFlags flags
,
7967 GMountOperation
*start_operation
,
7968 GCancellable
*cancellable
,
7969 GAsyncReadyCallback callback
,
7974 g_return_if_fail (G_IS_FILE (file
));
7976 iface
= G_FILE_GET_IFACE (file
);
7978 if (iface
->start_mountable
== NULL
)
7980 g_task_report_new_error (file
, callback
, user_data
,
7981 g_file_start_mountable
,
7982 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
7983 _("Operation not supported"));
7987 (* iface
->start_mountable
) (file
,
7996 * g_file_start_mountable_finish:
7997 * @file: input #GFile
7998 * @result: a #GAsyncResult
7999 * @error: a #GError, or %NULL
8001 * Finishes a start operation. See g_file_start_mountable() for details.
8003 * Finish an asynchronous start operation that was started
8004 * with g_file_start_mountable().
8006 * Returns: %TRUE if the operation finished successfully. %FALSE
8012 g_file_start_mountable_finish (GFile
*file
,
8013 GAsyncResult
*result
,
8018 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8019 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8021 if (g_async_result_legacy_propagate_error (result
, error
))
8023 else if (g_async_result_is_tagged (result
, g_file_start_mountable
))
8024 return g_task_propagate_boolean (G_TASK (result
), error
);
8026 iface
= G_FILE_GET_IFACE (file
);
8027 return (* iface
->start_mountable_finish
) (file
, result
, error
);
8031 * g_file_stop_mountable:
8032 * @file: input #GFile
8033 * @flags: flags affecting the operation
8034 * @mount_operation: (nullable): a #GMountOperation,
8035 * or %NULL to avoid user interaction.
8036 * @cancellable: (nullable): optional #GCancellable object,
8038 * @callback: (nullable): a #GAsyncReadyCallback to call
8039 * when the request is satisfied, or %NULL
8040 * @user_data: the data to pass to callback function
8042 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
8044 * If @cancellable is not %NULL, then the operation can be cancelled by
8045 * triggering the cancellable object from another thread. If the operation
8046 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8048 * When the operation is finished, @callback will be called.
8049 * You can then call g_file_stop_mountable_finish() to get
8050 * the result of the operation.
8055 g_file_stop_mountable (GFile
*file
,
8056 GMountUnmountFlags flags
,
8057 GMountOperation
*mount_operation
,
8058 GCancellable
*cancellable
,
8059 GAsyncReadyCallback callback
,
8064 g_return_if_fail (G_IS_FILE (file
));
8066 iface
= G_FILE_GET_IFACE (file
);
8068 if (iface
->stop_mountable
== NULL
)
8070 g_task_report_new_error (file
, callback
, user_data
,
8071 g_file_stop_mountable
,
8072 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
8073 _("Operation not supported"));
8077 (* iface
->stop_mountable
) (file
,
8086 * g_file_stop_mountable_finish:
8087 * @file: input #GFile
8088 * @result: a #GAsyncResult
8089 * @error: a #GError, or %NULL
8091 * Finishes an stop operation, see g_file_stop_mountable() for details.
8093 * Finish an asynchronous stop operation that was started
8094 * with g_file_stop_mountable().
8096 * Returns: %TRUE if the operation finished successfully.
8102 g_file_stop_mountable_finish (GFile
*file
,
8103 GAsyncResult
*result
,
8108 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8109 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8111 if (g_async_result_legacy_propagate_error (result
, error
))
8113 else if (g_async_result_is_tagged (result
, g_file_stop_mountable
))
8114 return g_task_propagate_boolean (G_TASK (result
), error
);
8116 iface
= G_FILE_GET_IFACE (file
);
8117 return (* iface
->stop_mountable_finish
) (file
, result
, error
);
8121 * g_file_poll_mountable:
8122 * @file: input #GFile
8123 * @cancellable: optional #GCancellable object, %NULL to ignore
8124 * @callback: (nullable): a #GAsyncReadyCallback to call
8125 * when the request is satisfied, or %NULL
8126 * @user_data: the data to pass to callback function
8128 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
8130 * If @cancellable is not %NULL, then the operation can be cancelled by
8131 * triggering the cancellable object from another thread. If the operation
8132 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
8134 * When the operation is finished, @callback will be called.
8135 * You can then call g_file_mount_mountable_finish() to get
8136 * the result of the operation.
8141 g_file_poll_mountable (GFile
*file
,
8142 GCancellable
*cancellable
,
8143 GAsyncReadyCallback callback
,
8148 g_return_if_fail (G_IS_FILE (file
));
8150 iface
= G_FILE_GET_IFACE (file
);
8152 if (iface
->poll_mountable
== NULL
)
8154 g_task_report_new_error (file
, callback
, user_data
,
8155 g_file_poll_mountable
,
8156 G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
8157 _("Operation not supported"));
8161 (* iface
->poll_mountable
) (file
,
8168 * g_file_poll_mountable_finish:
8169 * @file: input #GFile
8170 * @result: a #GAsyncResult
8171 * @error: a #GError, or %NULL
8173 * Finishes a poll operation. See g_file_poll_mountable() for details.
8175 * Finish an asynchronous poll operation that was polled
8176 * with g_file_poll_mountable().
8178 * Returns: %TRUE if the operation finished successfully. %FALSE
8184 g_file_poll_mountable_finish (GFile
*file
,
8185 GAsyncResult
*result
,
8190 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8191 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
8193 if (g_async_result_legacy_propagate_error (result
, error
))
8195 else if (g_async_result_is_tagged (result
, g_file_poll_mountable
))
8196 return g_task_propagate_boolean (G_TASK (result
), error
);
8198 iface
= G_FILE_GET_IFACE (file
);
8199 return (* iface
->poll_mountable_finish
) (file
, result
, error
);
8203 * g_file_supports_thread_contexts:
8206 * Checks if @file supports
8207 * [thread-default contexts][g-main-context-push-thread-default-context].
8208 * If this returns %FALSE, you cannot perform asynchronous operations on
8209 * @file in a thread that has a thread-default context.
8211 * Returns: Whether or not @file supports thread-default contexts.
8216 g_file_supports_thread_contexts (GFile
*file
)
8220 g_return_val_if_fail (G_IS_FILE (file
), FALSE
);
8222 iface
= G_FILE_GET_IFACE (file
);
8223 return iface
->supports_thread_contexts
;
8227 * g_file_load_bytes:
8229 * @cancellable: (nullable): a #GCancellable or %NULL
8230 * @etag_out: (out) (nullable) (optional): a location to place the current
8231 * entity tag for the file, or %NULL if the entity tag is not needed
8232 * @error: a location for a #GError or %NULL
8234 * Loads the contents of @file and returns it as #GBytes.
8236 * If @file is a resource:// based URI, the resulting bytes will reference the
8237 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8238 * g_file_load_contents() and g_bytes_new_take().
8240 * For resources, @etag_out will be set to %NULL.
8242 * The data contained in the resulting #GBytes is always zero-terminated, but
8243 * this is not included in the #GBytes length. The resulting #GBytes should be
8244 * freed with g_bytes_unref() when no longer in use.
8246 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8251 g_file_load_bytes (GFile
*file
,
8252 GCancellable
*cancellable
,
8259 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
8260 g_return_val_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
), NULL
);
8261 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
8263 if (etag_out
!= NULL
)
8266 if (g_file_has_uri_scheme (file
, "resource"))
8269 gchar
*uri
, *unescaped
;
8271 uri
= g_file_get_uri (file
);
8272 unescaped
= g_uri_unescape_string (uri
+ strlen ("resource://"), NULL
);
8275 bytes
= g_resources_lookup_data (unescaped
, G_RESOURCE_LOOKUP_FLAGS_NONE
, error
);
8281 /* contents is guaranteed to be \0 terminated */
8282 if (g_file_load_contents (file
, cancellable
, &contents
, &len
, etag_out
, error
))
8283 return g_bytes_new_take (g_steal_pointer (&contents
), len
);
8289 g_file_load_bytes_cb (GObject
*object
,
8290 GAsyncResult
*result
,
8293 GFile
*file
= G_FILE (object
);
8294 GTask
*task
= user_data
;
8295 GError
*error
= NULL
;
8297 gchar
*contents
= NULL
;
8300 g_file_load_contents_finish (file
, result
, &contents
, &len
, &etag
, &error
);
8301 g_task_set_task_data (task
, g_steal_pointer (&etag
), g_free
);
8304 g_task_return_error (task
, g_steal_pointer (&error
));
8306 g_task_return_pointer (task
,
8307 g_bytes_new_take (g_steal_pointer (&contents
), len
),
8308 (GDestroyNotify
)g_bytes_unref
);
8310 g_object_unref (task
);
8314 * g_file_load_bytes_async:
8316 * @cancellable: (nullable): a #GCancellable or %NULL
8317 * @callback: (scope async): a #GAsyncReadyCallback to call when the
8318 * request is satisfied
8319 * @user_data: (closure): the data to pass to callback function
8321 * Asynchronously loads the contents of @file as #GBytes.
8323 * If @file is a resource:// based URI, the resulting bytes will reference the
8324 * embedded resource instead of a copy. Otherwise, this is equivalent to calling
8325 * g_file_load_contents_async() and g_bytes_new_take().
8327 * @callback should call g_file_load_bytes_finish() to get the result of this
8328 * asynchronous operation.
8330 * See g_file_load_bytes() for more information.
8335 g_file_load_bytes_async (GFile
*file
,
8336 GCancellable
*cancellable
,
8337 GAsyncReadyCallback callback
,
8340 GError
*error
= NULL
;
8344 g_return_if_fail (G_IS_FILE (file
));
8345 g_return_if_fail (cancellable
== NULL
|| G_IS_CANCELLABLE (cancellable
));
8347 task
= g_task_new (file
, cancellable
, callback
, user_data
);
8348 g_task_set_source_tag (task
, g_file_load_bytes_async
);
8350 if (!g_file_has_uri_scheme (file
, "resource"))
8352 g_file_load_contents_async (file
,
8354 g_file_load_bytes_cb
,
8355 g_steal_pointer (&task
));
8359 bytes
= g_file_load_bytes (file
, cancellable
, NULL
, &error
);
8362 g_task_return_error (task
, g_steal_pointer (&error
));
8364 g_task_return_pointer (task
,
8365 g_steal_pointer (&bytes
),
8366 (GDestroyNotify
)g_bytes_unref
);
8368 g_object_unref (task
);
8372 * g_file_load_bytes_finish:
8374 * @result: a #GAsyncResult provided to the callback
8375 * @etag_out: (out) (nullable) (optional): a location to place the current
8376 * entity tag for the file, or %NULL if the entity tag is not needed
8377 * @error: a location for a #GError, or %NULL
8379 * Completes an asynchronous request to g_file_load_bytes_async().
8381 * For resources, @etag_out will be set to %NULL.
8383 * The data contained in the resulting #GBytes is always zero-terminated, but
8384 * this is not included in the #GBytes length. The resulting #GBytes should be
8385 * freed with g_bytes_unref() when no longer in use.
8387 * See g_file_load_bytes() for more information.
8389 * Returns: (transfer full): a #GBytes or %NULL and @error is set
8394 g_file_load_bytes_finish (GFile
*file
,
8395 GAsyncResult
*result
,
8401 g_return_val_if_fail (G_IS_FILE (file
), NULL
);
8402 g_return_val_if_fail (G_IS_TASK (result
), NULL
);
8403 g_return_val_if_fail (g_task_is_valid (G_TASK (result
), file
), NULL
);
8404 g_return_val_if_fail (error
== NULL
|| *error
== NULL
, NULL
);
8406 bytes
= g_task_propagate_pointer (G_TASK (result
), error
);
8408 if (etag_out
!= NULL
)
8409 *etag_out
= g_strdup (g_task_get_task_data (G_TASK (result
)));