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 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <sys/ioctl.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
43 #include <sys/types.h>
49 #include "glib/gstdio.h"
51 #include "glib-unix.h"
55 #include "gfileattribute-priv.h"
56 #include "gfiledescriptorbased.h"
57 #include "gpollfilemonitor.h"
59 #include "gfileinputstream.h"
60 #include "gfileoutputstream.h"
61 #include "glocalfileoutputstream.h"
62 #include "glocalfileiostream.h"
63 #include "gcancellable.h"
64 #include "gasyncresult.h"
71 * @short_description: File and Directory Handling
73 * @see_also: #GFileInfo, #GFileEnumerator
75 * #GFile is a high level abstraction for manipulating files on a
76 * virtual file system. #GFiles are lightweight, immutable objects
77 * that do no I/O upon creation. It is necessary to understand that
78 * #GFile objects do not represent files, merely an identifier for a
79 * file. All file content I/O is implemented as streaming operations
80 * (see #GInputStream and #GOutputStream).
82 * To construct a #GFile, you can use:
84 * <member>g_file_new_for_path() if you have a path.</member>
85 * <member>g_file_new_for_uri() if you have a URI.</member>
86 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
87 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
88 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
91 * One way to think of a #GFile is as an abstraction of a pathname. For
92 * normal files the system pathname is what is stored internally, but as
93 * #GFiles are extensible it could also be something else that corresponds
94 * to a pathname in a userspace implementation of a filesystem.
96 * #GFiles make up hierarchies of directories and files that correspond to
97 * the files on a filesystem. You can move through the file system with
98 * #GFile using g_file_get_parent() to get an identifier for the parent
99 * directory, g_file_get_child() to get a child within a directory,
100 * g_file_resolve_relative_path() to resolve a relative path between two
101 * #GFiles. There can be multiple hierarchies, so you may not end up at
102 * the same root if you repeatedly call g_file_get_parent() on two different
105 * All #GFiles have a basename (get with g_file_get_basename()). These names
106 * are byte strings that are used to identify the file on the filesystem
107 * (relative to its parent directory) and there is no guarantees that they
108 * have any particular charset encoding or even make any sense at all. If
109 * you want to use filenames in a user interface you should use the display
110 * name that you can get by requesting the
111 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
112 * This is guaranteed to be in UTF-8 and can be used in a user interface.
113 * But always store the real basename or the #GFile to use to actually
114 * access the file, because there is no way to go from a display name to
117 * Using #GFile as an identifier has the same weaknesses as using a path
118 * in that there may be multiple aliases for the same file. For instance,
119 * hard or soft links may cause two different #GFiles to refer to the same
120 * file. Other possible causes for aliases are: case insensitive filesystems,
121 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
122 * check if two #GFiles point to the same file you can query for the
123 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
124 * canonicalization of pathnames passed in, so that trivial differences in
125 * the path string used at creation (duplicated slashes, slash at end of
126 * path, "." or ".." path segments, etc) does not create different #GFiles.
128 * Many #GFile operations have both synchronous and asynchronous versions
129 * to suit your application. Asynchronous versions of synchronous functions
130 * simply have _async() appended to their function names. The asynchronous
131 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
132 * the operation, producing a GAsyncResult which is then passed to the
133 * function's matching _finish() operation.
135 * Some #GFile operations do not have synchronous analogs, as they may
136 * take a very long time to finish, and blocking may leave an application
137 * unusable. Notable cases include:
139 * <member>g_file_mount_mountable() to mount a mountable file.</member>
140 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
141 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
144 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
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 has
148 * been modified from the version on the file system. See the HTTP 1.1
149 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
150 * 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_open_readwrite_async (GFile *file,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
236 static void g_file_real_create_readwrite_async (GFile *file,
237 GFileCreateFlags flags,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
245 static void g_file_real_replace_readwrite_async (GFile *file,
247 gboolean make_backup,
248 GFileCreateFlags flags,
250 GCancellable *cancellable,
251 GAsyncReadyCallback callback,
253 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
256 static gboolean g_file_real_set_attributes_from_info (GFile *file,
258 GFileQueryInfoFlags flags,
259 GCancellable *cancellable,
261 static void g_file_real_set_display_name_async (GFile *file,
262 const char *display_name,
264 GCancellable *cancellable,
265 GAsyncReadyCallback callback,
267 static GFile * g_file_real_set_display_name_finish (GFile *file,
270 static void g_file_real_set_attributes_async (GFile *file,
272 GFileQueryInfoFlags flags,
274 GCancellable *cancellable,
275 GAsyncReadyCallback callback,
277 static gboolean g_file_real_set_attributes_finish (GFile *file,
281 static void g_file_real_find_enclosing_mount_async (GFile *file,
283 GCancellable *cancellable,
284 GAsyncReadyCallback callback,
286 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
289 static void g_file_real_copy_async (GFile *source,
291 GFileCopyFlags flags,
293 GCancellable *cancellable,
294 GFileProgressCallback progress_callback,
295 gpointer progress_callback_data,
296 GAsyncReadyCallback callback,
298 static gboolean g_file_real_copy_finish (GFile *file,
302 typedef GFileIface GFileInterface;
303 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
306 g_file_default_init (GFileIface *iface)
308 iface->enumerate_children_async = g_file_real_enumerate_children_async;
309 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
310 iface->set_display_name_async = g_file_real_set_display_name_async;
311 iface->set_display_name_finish = g_file_real_set_display_name_finish;
312 iface->query_info_async = g_file_real_query_info_async;
313 iface->query_info_finish = g_file_real_query_info_finish;
314 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
315 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
316 iface->set_attributes_async = g_file_real_set_attributes_async;
317 iface->set_attributes_finish = g_file_real_set_attributes_finish;
318 iface->read_async = g_file_real_read_async;
319 iface->read_finish = g_file_real_read_finish;
320 iface->append_to_async = g_file_real_append_to_async;
321 iface->append_to_finish = g_file_real_append_to_finish;
322 iface->create_async = g_file_real_create_async;
323 iface->create_finish = g_file_real_create_finish;
324 iface->replace_async = g_file_real_replace_async;
325 iface->replace_finish = g_file_real_replace_finish;
326 iface->delete_file_async = g_file_real_delete_async;
327 iface->delete_file_finish = g_file_real_delete_finish;
328 iface->open_readwrite_async = g_file_real_open_readwrite_async;
329 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
330 iface->create_readwrite_async = g_file_real_create_readwrite_async;
331 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
332 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
333 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
334 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
335 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
336 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
337 iface->copy_async = g_file_real_copy_async;
338 iface->copy_finish = g_file_real_copy_finish;
344 * @file: input #GFile
346 * Checks to see if a file is native to the platform.
348 * A native file s one expressed in the platform-native filename format,
349 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
350 * as it might be on a locally mounted remote filesystem.
352 * On some systems non-native files may be available using the native
353 * filesystem via a userspace filesystem (FUSE), in these cases this call
354 * will return %FALSE, but g_file_get_path() will still return a native path.
356 * This call does no blocking I/O.
358 * Returns: %TRUE if @file is native
361 g_file_is_native (GFile *file)
365 g_return_val_if_fail (G_IS_FILE (file), FALSE);
367 iface = G_FILE_GET_IFACE (file);
369 return (* iface->is_native) (file);
374 * g_file_has_uri_scheme:
375 * @file: input #GFile
376 * @uri_scheme: a string containing a URI scheme
378 * Checks to see if a #GFile has a given URI scheme.
380 * This call does no blocking I/O.
382 * Returns: %TRUE if #GFile's backend supports the
383 * given URI scheme, %FALSE if URI scheme is %NULL,
384 * not supported, or #GFile is invalid.
387 g_file_has_uri_scheme (GFile *file,
388 const char *uri_scheme)
392 g_return_val_if_fail (G_IS_FILE (file), FALSE);
393 g_return_val_if_fail (uri_scheme != NULL, FALSE);
395 iface = G_FILE_GET_IFACE (file);
397 return (* iface->has_uri_scheme) (file, uri_scheme);
402 * g_file_get_uri_scheme:
403 * @file: input #GFile
405 * Gets the URI scheme for a #GFile.
406 * RFC 3986 decodes the scheme as:
408 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
410 * Common schemes include "file", "http", "ftp", etc.
412 * This call does no blocking I/O.
414 * Returns: a string containing the URI scheme for the given
415 * #GFile. The returned string should be freed with g_free()
416 * when no longer needed.
419 g_file_get_uri_scheme (GFile *file)
423 g_return_val_if_fail (G_IS_FILE (file), NULL);
425 iface = G_FILE_GET_IFACE (file);
427 return (* iface->get_uri_scheme) (file);
432 * g_file_get_basename:
433 * @file: input #GFile
435 * Gets the base name (the last component of the path) for a given #GFile.
437 * If called for the top level of a system (such as the filesystem root
438 * or a uri like sftp://host/) it will return a single directory separator
439 * (and on Windows, possibly a drive letter).
441 * The base name is a byte string (not UTF-8). It has no defined encoding
442 * or rules other than it may not contain zero bytes. If you want to use
443 * filenames in a user interface you should use the display name that you
444 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
445 * attribute with g_file_query_info().
447 * This call does no blocking I/O.
449 * Returns: string containing the #GFile's base name, or %NULL
450 * if given #GFile is invalid. The returned string should be
451 * freed with g_free() when no longer needed.
454 g_file_get_basename (GFile *file)
458 g_return_val_if_fail (G_IS_FILE (file), NULL);
460 iface = G_FILE_GET_IFACE (file);
462 return (* iface->get_basename) (file);
467 * @file: input #GFile
469 * Gets the local pathname for #GFile, if one exists.
471 * This call does no blocking I/O.
473 * Returns: string containing the #GFile's path, or %NULL if
474 * no such path exists. The returned string should be
475 * freed with g_free() when no longer needed.
478 g_file_get_path (GFile *file)
482 g_return_val_if_fail (G_IS_FILE (file), NULL);
484 iface = G_FILE_GET_IFACE (file);
486 return (* iface->get_path) (file);
491 * @file: input #GFile
493 * Gets the URI for the @file.
495 * This call does no blocking I/O.
497 * Returns: a string containing the #GFile's URI.
498 * The returned string should be freed with g_free()
499 * when no longer needed.
502 g_file_get_uri (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_uri) (file);
514 * g_file_get_parse_name:
515 * @file: input #GFile
517 * Gets the parse name of the @file.
518 * A parse name is a UTF-8 string that describes the
519 * file such that one can get the #GFile back using
520 * g_file_parse_name().
522 * This is generally used to show the #GFile as a nice
523 * full-pathname kind of string in a user interface,
524 * like in a location entry.
526 * For local files with names that can safely be converted
527 * to UTF-8 the pathname is used, otherwise the IRI is used
528 * (a form of URI that allows UTF-8 characters unescaped).
530 * This call does no blocking I/O.
532 * Returns: a string containing the #GFile's parse name.
533 * The returned string should be freed with g_free()
534 * when no longer needed.
537 g_file_get_parse_name (GFile *file)
541 g_return_val_if_fail (G_IS_FILE (file), NULL);
543 iface = G_FILE_GET_IFACE (file);
545 return (* iface->get_parse_name) (file);
550 * @file: input #GFile
552 * Duplicates a #GFile handle. This operation does not duplicate
553 * the actual file or directory represented by the #GFile; see
554 * g_file_copy() if attempting to copy a file.
556 * This call does no blocking I/O.
558 * Returns: (transfer full): a new #GFile that is a duplicate
559 * of the given #GFile.
562 g_file_dup (GFile *file)
566 g_return_val_if_fail (G_IS_FILE (file), NULL);
568 iface = G_FILE_GET_IFACE (file);
570 return (* iface->dup) (file);
575 * @file: (type GFile): #gconstpointer to a #GFile
577 * Creates a hash value for a #GFile.
579 * This call does no blocking I/O.
582 * Returns: 0 if @file is not a valid #GFile, otherwise an
583 * integer that can be used as hash value for the #GFile.
584 * This function is intended for easily hashing a #GFile to
585 * add to a #GHashTable or similar data structure.
588 g_file_hash (gconstpointer file)
592 g_return_val_if_fail (G_IS_FILE (file), 0);
594 iface = G_FILE_GET_IFACE (file);
596 return (* iface->hash) ((GFile *)file);
601 * @file1: the first #GFile
602 * @file2: the second #GFile
604 * Checks equality of two given #GFiles.
606 * Note that two #GFiles that differ can still refer to the same
607 * file on the filesystem due to various forms of filename
610 * This call does no blocking I/O.
612 * Returns: %TRUE if @file1 and @file2 are equal.
613 * %FALSE if either is not a #GFile.
616 g_file_equal (GFile *file1,
621 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
622 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
624 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
627 iface = G_FILE_GET_IFACE (file1);
629 return (* iface->equal) (file1, file2);
635 * @file: input #GFile
637 * Gets the parent directory for the @file.
638 * If the @file represents the root directory of the
639 * file system, then %NULL will be returned.
641 * This call does no blocking I/O.
643 * Returns: (transfer full): a #GFile structure to the
644 * parent of the given #GFile or %NULL if there is
645 * no parent. Free the returned object with g_object_unref().
648 g_file_get_parent (GFile *file)
652 g_return_val_if_fail (G_IS_FILE (file), NULL);
654 iface = G_FILE_GET_IFACE (file);
656 return (* iface->get_parent) (file);
661 * @file: input #GFile
662 * @parent: (allow-none): the parent to check for, or %NULL
664 * Checks if @file has a parent, and optionally, if it is @parent.
666 * If @parent is %NULL then this function returns %TRUE if @file has any
667 * parent at all. If @parent is non-%NULL then %TRUE is only returned
668 * if @file is a child of @parent.
670 * Returns: %TRUE if @file is a child of @parent (or any parent in the
671 * case that @parent is %NULL).
676 g_file_has_parent (GFile *file,
679 GFile *actual_parent;
682 g_return_val_if_fail (G_IS_FILE (file), FALSE);
683 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
685 actual_parent = g_file_get_parent (file);
687 if (actual_parent != NULL)
690 result = g_file_equal (parent, actual_parent);
694 g_object_unref (actual_parent);
704 * @file: input #GFile
705 * @name: string containing the child's basename
707 * Gets a child of @file with basename equal to @name.
709 * Note that the file with that specific name might not exist, but
710 * you can still have a #GFile that points to it. You can use this
711 * for instance to create that file.
713 * This call does no blocking I/O.
715 * Returns: (transfer full): a #GFile to a child specified by @name.
716 * Free the returned object with g_object_unref().
719 g_file_get_child (GFile *file,
722 g_return_val_if_fail (G_IS_FILE (file), NULL);
723 g_return_val_if_fail (name != NULL, NULL);
725 return g_file_resolve_relative_path (file, name);
729 * g_file_get_child_for_display_name:
730 * @file: input #GFile
731 * @display_name: string to a possible child
732 * @error: return location for an error
734 * Gets the child of @file for a given @display_name (i.e. a UTF-8
735 * version of the name). If this function fails, it returns %NULL
736 * and @error will be set. This is very useful when constructing a
737 * #GFile for a new file and the user entered the filename in the
738 * user interface, for instance when you select a directory and
739 * type a filename in the file selector.
741 * This call does no blocking I/O.
743 * Returns: (transfer full): a #GFile to the specified child, or
744 * %NULL if the display name couldn't be converted.
745 * Free the returned object with g_object_unref().
748 g_file_get_child_for_display_name (GFile *file,
749 const char *display_name,
754 g_return_val_if_fail (G_IS_FILE (file), NULL);
755 g_return_val_if_fail (display_name != NULL, NULL);
757 iface = G_FILE_GET_IFACE (file);
759 return (* iface->get_child_for_display_name) (file, display_name, error);
764 * @file: input #GFile
765 * @prefix: input #GFile
767 * Checks whether @file has the prefix specified by @prefix.
769 * In other words, if the names of initial elements of @file's
770 * pathname match @prefix. Only full pathname elements are matched,
771 * so a path like /foo is not considered a prefix of /foobar, only
774 * This call does no I/O, as it works purely on names. As such it can
775 * sometimes return %FALSE even if @file is inside a @prefix (from a
776 * filesystem point of view), because the prefix of @file is an alias
779 * Virtual: prefix_matches
780 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
784 g_file_has_prefix (GFile *file,
789 g_return_val_if_fail (G_IS_FILE (file), FALSE);
790 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
792 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
795 iface = G_FILE_GET_IFACE (file);
797 /* The vtable function differs in arg order since
798 * we're using the old contains_file call
800 return (* iface->prefix_matches) (prefix, file);
804 * g_file_get_relative_path:
805 * @parent: input #GFile
806 * @descendant: input #GFile
808 * Gets the path for @descendant relative to @parent.
810 * This call does no blocking I/O.
812 * Returns: string with the relative path from @descendant
813 * to @parent, or %NULL if @descendant doesn't have @parent
814 * as prefix. The returned string should be freed with g_free()
815 * when no longer needed.
818 g_file_get_relative_path (GFile *parent,
823 g_return_val_if_fail (G_IS_FILE (parent), NULL);
824 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
826 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
829 iface = G_FILE_GET_IFACE (parent);
831 return (* iface->get_relative_path) (parent, descendant);
835 * g_file_resolve_relative_path:
836 * @file: input #GFile
837 * @relative_path: a given relative path string
839 * Resolves a relative path for @file to an absolute path.
841 * This call does no blocking I/O.
843 * Returns: (transfer full): #GFile to the resolved path.
844 * %NULL if @relative_path is %NULL or if @file is invalid.
845 * Free the returned object with g_object_unref().
848 g_file_resolve_relative_path (GFile *file,
849 const char *relative_path)
853 g_return_val_if_fail (G_IS_FILE (file), NULL);
854 g_return_val_if_fail (relative_path != NULL, NULL);
856 iface = G_FILE_GET_IFACE (file);
858 return (* iface->resolve_relative_path) (file, relative_path);
862 * g_file_enumerate_children:
863 * @file: input #GFile
864 * @attributes: an attribute query string
865 * @flags: a set of #GFileQueryInfoFlags
866 * @cancellable: (allow-none): optional #GCancellable object,
868 * @error: #GError for error reporting
870 * Gets the requested information about the files in a directory.
871 * The result is a #GFileEnumerator object that will give out
872 * #GFileInfo objects for all the files in the directory.
874 * The @attributes value is a string that specifies the file
875 * attributes that should be gathered. It is not an error if
876 * it's not possible to read a particular requested attribute
877 * from a file - it just won't be set. @attributes should
878 * be a comma-separated list of attributes or attribute wildcards.
879 * The wildcard "*" means all attributes, and a wildcard like
880 * "standard::*" means all attributes in the standard namespace.
881 * An example attribute query be "standard::*,owner::user".
882 * The standard attributes are available as defines, like
883 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
885 * If @cancellable is not %NULL, then the operation can be cancelled
886 * by triggering the cancellable object from another thread. If the
887 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
890 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
891 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
892 * error will be returned. Other errors are possible too.
894 * Returns: (transfer full): A #GFileEnumerator if successful,
895 * %NULL on error. Free the returned object with g_object_unref().
898 g_file_enumerate_children (GFile *file,
899 const char *attributes,
900 GFileQueryInfoFlags flags,
901 GCancellable *cancellable,
906 g_return_val_if_fail (G_IS_FILE (file), NULL);
908 if (g_cancellable_set_error_if_cancelled (cancellable, error))
911 iface = G_FILE_GET_IFACE (file);
913 if (iface->enumerate_children == NULL)
915 g_set_error_literal (error, G_IO_ERROR,
916 G_IO_ERROR_NOT_SUPPORTED,
917 _("Operation not supported"));
921 return (* iface->enumerate_children) (file, attributes, flags,
926 * g_file_enumerate_children_async:
927 * @file: input #GFile
928 * @attributes: an attribute query string
929 * @flags: a set of #GFileQueryInfoFlags
930 * @io_priority: the <link linkend="io-priority">I/O priority</link>
932 * @cancellable: (allow-none): optional #GCancellable object,
934 * @callback: (scope async): a #GAsyncReadyCallback to call when the
935 * request is satisfied
936 * @user_data: (closure): the data to pass to callback function
938 * Asynchronously gets the requested information about the files
939 * in a directory. The result is a #GFileEnumerator object that will
940 * give out #GFileInfo objects for all the files in the directory.
942 * For more details, see g_file_enumerate_children() which is
943 * the synchronous version of this call.
945 * When the operation is finished, @callback will be called. You can
946 * then call g_file_enumerate_children_finish() to get the result of
950 g_file_enumerate_children_async (GFile *file,
951 const char *attributes,
952 GFileQueryInfoFlags flags,
954 GCancellable *cancellable,
955 GAsyncReadyCallback callback,
960 g_return_if_fail (G_IS_FILE (file));
962 iface = G_FILE_GET_IFACE (file);
963 (* iface->enumerate_children_async) (file,
973 * g_file_enumerate_children_finish:
974 * @file: input #GFile
975 * @res: a #GAsyncResult
978 * Finishes an async enumerate children operation.
979 * See g_file_enumerate_children_async().
981 * Returns: (transfer full): a #GFileEnumerator or %NULL
982 * if an error occurred.
983 * Free the returned object with g_object_unref().
986 g_file_enumerate_children_finish (GFile *file,
992 g_return_val_if_fail (G_IS_FILE (file), NULL);
993 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
995 if (g_async_result_legacy_propagate_error (res, error))
998 iface = G_FILE_GET_IFACE (file);
999 return (* iface->enumerate_children_finish) (file, res, error);
1003 * g_file_query_exists:
1004 * @file: input #GFile
1005 * @cancellable: (allow-none): optional #GCancellable object,
1008 * Utility function to check if a particular file exists. This is
1009 * implemented using g_file_query_info() and as such does blocking I/O.
1011 * Note that in many cases it is racy to first check for file existence
1012 * and then execute something based on the outcome of that, because the
1013 * file might have been created or removed in between the operations. The
1014 * general approach to handling that is to not check, but just do the
1015 * operation and handle the errors as they come.
1017 * As an example of race-free checking, take the case of reading a file,
1018 * and if it doesn't exist, creating it. There are two racy versions: read
1019 * it, and on error create it; and: check if it exists, if not create it.
1020 * These can both result in two processes creating the file (with perhaps
1021 * a partially written file as the result). The correct approach is to
1022 * always try to create the file with g_file_create() which will either
1023 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1025 * However, in many cases an existence check is useful in a user interface,
1026 * for instance to make a menu item sensitive/insensitive, so that you don't
1027 * have to fool users that something is possible and then just show an error
1028 * dialog. If you do this, you should make sure to also handle the errors
1029 * that can happen due to races when you execute the operation.
1031 * Returns: %TRUE if the file exists (and can be detected without error),
1032 * %FALSE otherwise (or if cancelled).
1035 g_file_query_exists (GFile *file,
1036 GCancellable *cancellable)
1040 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1042 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1043 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1046 g_object_unref (info);
1054 * g_file_query_file_type:
1055 * @file: input #GFile
1056 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1057 * @cancellable: (allow-none): optional #GCancellable object,
1060 * Utility function to inspect the #GFileType of a file. This is
1061 * implemented using g_file_query_info() and as such does blocking I/O.
1063 * The primary use case of this method is to check if a file is
1064 * a regular file, directory, or symlink.
1066 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1067 * if the file does not exist
1072 g_file_query_file_type (GFile *file,
1073 GFileQueryInfoFlags flags,
1074 GCancellable *cancellable)
1077 GFileType file_type;
1079 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1080 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1084 file_type = g_file_info_get_file_type (info);
1085 g_object_unref (info);
1088 file_type = G_FILE_TYPE_UNKNOWN;
1094 * g_file_query_info:
1095 * @file: input #GFile
1096 * @attributes: an attribute query string
1097 * @flags: a set of #GFileQueryInfoFlags
1098 * @cancellable: (allow-none): optional #GCancellable object,
1102 * Gets the requested information about specified @file.
1103 * The result is a #GFileInfo object that contains key-value
1104 * attributes (such as the type or size of the file).
1106 * The @attributes value is a string that specifies the file
1107 * attributes that should be gathered. It is not an error if
1108 * it's not possible to read a particular requested attribute
1109 * from a file - it just won't be set. @attributes should be a
1110 * comma-separated list of attributes or attribute wildcards.
1111 * The wildcard "*" means all attributes, and a wildcard like
1112 * "standard::*" means all attributes in the standard namespace.
1113 * An example attribute query be "standard::*,owner::user".
1114 * The standard attributes are available as defines, like
1115 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1117 * If @cancellable is not %NULL, then the operation can be cancelled
1118 * by triggering the cancellable object from another thread. If the
1119 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1122 * For symlinks, normally the information about the target of the
1123 * symlink is returned, rather than information about the symlink
1124 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1125 * in @flags the information about the symlink itself will be returned.
1126 * Also, for symlinks that point to non-existing files the information
1127 * about the symlink itself will be returned.
1129 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1130 * returned. Other errors are possible too, and depend on what kind of
1131 * filesystem the file is on.
1133 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1134 * on error. Free the returned object with g_object_unref().
1137 g_file_query_info (GFile *file,
1138 const char *attributes,
1139 GFileQueryInfoFlags flags,
1140 GCancellable *cancellable,
1145 g_return_val_if_fail (G_IS_FILE (file), NULL);
1147 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1150 iface = G_FILE_GET_IFACE (file);
1152 if (iface->query_info == NULL)
1154 g_set_error_literal (error, G_IO_ERROR,
1155 G_IO_ERROR_NOT_SUPPORTED,
1156 _("Operation not supported"));
1160 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1164 * g_file_query_info_async:
1165 * @file: input #GFile
1166 * @attributes: an attribute query string
1167 * @flags: a set of #GFileQueryInfoFlags
1168 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1170 * @cancellable: (allow-none): optional #GCancellable object,
1172 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1173 * request is satisfied
1174 * @user_data: (closure): the data to pass to callback function
1176 * Asynchronously gets the requested information about specified @file.
1177 * The result is a #GFileInfo object that contains key-value attributes
1178 * (such as type or size for the file).
1180 * For more details, see g_file_query_info() which is the synchronous
1181 * version of this call.
1183 * When the operation is finished, @callback will be called. You can
1184 * then call g_file_query_info_finish() to get the result of the operation.
1187 g_file_query_info_async (GFile *file,
1188 const char *attributes,
1189 GFileQueryInfoFlags flags,
1191 GCancellable *cancellable,
1192 GAsyncReadyCallback callback,
1197 g_return_if_fail (G_IS_FILE (file));
1199 iface = G_FILE_GET_IFACE (file);
1200 (* iface->query_info_async) (file,
1210 * g_file_query_info_finish:
1211 * @file: input #GFile
1212 * @res: a #GAsyncResult
1215 * Finishes an asynchronous file info query.
1216 * See g_file_query_info_async().
1218 * Returns: (transfer full): #GFileInfo for given @file
1219 * or %NULL on error. Free the returned object with
1223 g_file_query_info_finish (GFile *file,
1229 g_return_val_if_fail (G_IS_FILE (file), NULL);
1230 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1232 if (g_async_result_legacy_propagate_error (res, error))
1235 iface = G_FILE_GET_IFACE (file);
1236 return (* iface->query_info_finish) (file, res, error);
1240 * g_file_query_filesystem_info:
1241 * @file: input #GFile
1242 * @attributes: an attribute query string
1243 * @cancellable: (allow-none): optional #GCancellable object,
1247 * Similar to g_file_query_info(), but obtains information
1248 * about the filesystem the @file is on, rather than the file itself.
1249 * For instance the amount of space available and the type of
1252 * The @attributes value is a string that specifies the attributes
1253 * that should be gathered. It is not an error if it's not possible
1254 * to read a particular requested attribute from a file - it just
1255 * won't be set. @attributes should be a comma-separated list of
1256 * attributes or attribute wildcards. The wildcard "*" means all
1257 * attributes, and a wildcard like "filesystem::*" means all attributes
1258 * in the filesystem namespace. The standard namespace for filesystem
1259 * attributes is "filesystem". Common attributes of interest are
1260 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1261 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1262 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1264 * If @cancellable is not %NULL, then the operation can be cancelled
1265 * by triggering the cancellable object from another thread. If the
1266 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1269 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1270 * be returned. Other errors are possible too, and depend on what
1271 * kind of filesystem the file is on.
1273 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1274 * Free the returned object with g_object_unref().
1277 g_file_query_filesystem_info (GFile *file,
1278 const char *attributes,
1279 GCancellable *cancellable,
1284 g_return_val_if_fail (G_IS_FILE (file), NULL);
1286 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1289 iface = G_FILE_GET_IFACE (file);
1291 if (iface->query_filesystem_info == NULL)
1293 g_set_error_literal (error, G_IO_ERROR,
1294 G_IO_ERROR_NOT_SUPPORTED,
1295 _("Operation not supported"));
1299 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1303 * g_file_query_filesystem_info_async:
1304 * @file: input #GFile
1305 * @attributes: an attribute query string
1306 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1308 * @cancellable: (allow-none): optional #GCancellable object,
1310 * @callback: (scope async): a #GAsyncReadyCallback to call
1311 * when the request is satisfied
1312 * @user_data: (closure): the data to pass to callback function
1314 * Asynchronously gets the requested information about the filesystem
1315 * that the specified @file is on. The result is a #GFileInfo object
1316 * that contains key-value attributes (such as type or size for the
1319 * For more details, see g_file_query_filesystem_info() which is the
1320 * synchronous version of this call.
1322 * When the operation is finished, @callback will be called. You can
1323 * then call g_file_query_info_finish() to get the result of the
1327 g_file_query_filesystem_info_async (GFile *file,
1328 const char *attributes,
1330 GCancellable *cancellable,
1331 GAsyncReadyCallback callback,
1336 g_return_if_fail (G_IS_FILE (file));
1338 iface = G_FILE_GET_IFACE (file);
1339 (* iface->query_filesystem_info_async) (file,
1348 * g_file_query_filesystem_info_finish:
1349 * @file: input #GFile
1350 * @res: a #GAsyncResult
1353 * Finishes an asynchronous filesystem info query.
1354 * See g_file_query_filesystem_info_async().
1356 * Returns: (transfer full): #GFileInfo for given @file
1357 * or %NULL on error.
1358 * Free the returned object with g_object_unref().
1361 g_file_query_filesystem_info_finish (GFile *file,
1367 g_return_val_if_fail (G_IS_FILE (file), NULL);
1368 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1370 if (g_async_result_legacy_propagate_error (res, error))
1373 iface = G_FILE_GET_IFACE (file);
1374 return (* iface->query_filesystem_info_finish) (file, res, error);
1378 * g_file_find_enclosing_mount:
1379 * @file: input #GFile
1380 * @cancellable: (allow-none): optional #GCancellable object,
1384 * Gets a #GMount for the #GFile.
1386 * If the #GFileIface for @file does not have a mount (e.g.
1387 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1388 * and %NULL will be returned.
1390 * If @cancellable is not %NULL, then the operation can be cancelled by
1391 * triggering the cancellable object from another thread. If the operation
1392 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1394 * Returns: (transfer full): a #GMount where the @file is located
1395 * or %NULL on error.
1396 * Free the returned object with g_object_unref().
1399 g_file_find_enclosing_mount (GFile *file,
1400 GCancellable *cancellable,
1405 g_return_val_if_fail (G_IS_FILE (file), NULL);
1407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1410 iface = G_FILE_GET_IFACE (file);
1411 if (iface->find_enclosing_mount == NULL)
1414 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1415 /* Translators: This is an error message when
1416 * trying to find the enclosing (user visible)
1417 * mount of a file, but none exists.
1419 _("Containing mount does not exist"));
1423 return (* iface->find_enclosing_mount) (file, cancellable, error);
1427 * g_file_find_enclosing_mount_async:
1429 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1431 * @cancellable: (allow-none): optional #GCancellable object,
1433 * @callback: (scope async): a #GAsyncReadyCallback to call
1434 * when the request is satisfied
1435 * @user_data: (closure): the data to pass to callback function
1437 * Asynchronously gets the mount for the file.
1439 * For more details, see g_file_find_enclosing_mount() which is
1440 * the synchronous version of this call.
1442 * When the operation is finished, @callback will be called.
1443 * You can then call g_file_find_enclosing_mount_finish() to
1444 * get the result of the operation.
1447 g_file_find_enclosing_mount_async (GFile *file,
1449 GCancellable *cancellable,
1450 GAsyncReadyCallback callback,
1455 g_return_if_fail (G_IS_FILE (file));
1457 iface = G_FILE_GET_IFACE (file);
1458 (* iface->find_enclosing_mount_async) (file,
1466 * g_file_find_enclosing_mount_finish:
1468 * @res: a #GAsyncResult
1471 * Finishes an asynchronous find mount request.
1472 * See g_file_find_enclosing_mount_async().
1474 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1475 * Free the returned object with g_object_unref().
1478 g_file_find_enclosing_mount_finish (GFile *file,
1484 g_return_val_if_fail (G_IS_FILE (file), NULL);
1485 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1487 if (g_async_result_legacy_propagate_error (res, error))
1490 iface = G_FILE_GET_IFACE (file);
1491 return (* iface->find_enclosing_mount_finish) (file, res, error);
1497 * @file: #GFile to read
1498 * @cancellable: (allow-none): a #GCancellable
1499 * @error: a #GError, or %NULL
1501 * Opens a file for reading. The result is a #GFileInputStream that
1502 * can be used to read the contents of the file.
1504 * If @cancellable is not %NULL, then the operation can be cancelled by
1505 * triggering the cancellable object from another thread. If the operation
1506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1508 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1509 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1510 * error will be returned. Other errors are possible too, and depend
1511 * on what kind of filesystem the file is on.
1514 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1515 * Free the returned object with g_object_unref().
1518 g_file_read (GFile *file,
1519 GCancellable *cancellable,
1524 g_return_val_if_fail (G_IS_FILE (file), NULL);
1526 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1529 iface = G_FILE_GET_IFACE (file);
1531 if (iface->read_fn == NULL)
1533 g_set_error_literal (error, G_IO_ERROR,
1534 G_IO_ERROR_NOT_SUPPORTED,
1535 _("Operation not supported"));
1539 return (* iface->read_fn) (file, cancellable, error);
1544 * @file: input #GFile
1545 * @flags: a set of #GFileCreateFlags
1546 * @cancellable: (allow-none): optional #GCancellable object,
1548 * @error: a #GError, or %NULL
1550 * Gets an output stream for appending data to the file.
1551 * If the file doesn't already exist it is created.
1553 * By default files created are generally readable by everyone,
1554 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1555 * will be made readable only to the current user, to the level that
1556 * is supported on the target filesystem.
1558 * If @cancellable is not %NULL, then the operation can be cancelled
1559 * by triggering the cancellable object from another thread. If the
1560 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1563 * Some file systems don't allow all file names, and may return an
1564 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1565 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1566 * possible too, and depend on what kind of filesystem the file is on.
1568 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1569 * Free the returned object with g_object_unref().
1572 g_file_append_to (GFile *file,
1573 GFileCreateFlags flags,
1574 GCancellable *cancellable,
1579 g_return_val_if_fail (G_IS_FILE (file), NULL);
1581 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1584 iface = G_FILE_GET_IFACE (file);
1586 if (iface->append_to == NULL)
1588 g_set_error_literal (error, G_IO_ERROR,
1589 G_IO_ERROR_NOT_SUPPORTED,
1590 _("Operation not supported"));
1594 return (* iface->append_to) (file, flags, cancellable, error);
1599 * @file: input #GFile
1600 * @flags: a set of #GFileCreateFlags
1601 * @cancellable: (allow-none): optional #GCancellable object,
1603 * @error: a #GError, or %NULL
1605 * Creates a new file and returns an output stream for writing to it.
1606 * The file must not already exist.
1608 * By default files created are generally readable by everyone,
1609 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1610 * will be made readable only to the current user, to the level
1611 * that is supported on the target filesystem.
1613 * If @cancellable is not %NULL, then the operation can be cancelled
1614 * by triggering the cancellable object from another thread. If the
1615 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1618 * If a file or directory with this name already exists the
1619 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1620 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1621 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1622 * be returned. Other errors are possible too, and depend on what kind
1623 * of filesystem the file is on.
1625 * Returns: (transfer full): a #GFileOutputStream for the newly created
1626 * file, or %NULL on error.
1627 * Free the returned object with g_object_unref().
1630 g_file_create (GFile *file,
1631 GFileCreateFlags flags,
1632 GCancellable *cancellable,
1637 g_return_val_if_fail (G_IS_FILE (file), NULL);
1639 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1642 iface = G_FILE_GET_IFACE (file);
1644 if (iface->create == NULL)
1646 g_set_error_literal (error, G_IO_ERROR,
1647 G_IO_ERROR_NOT_SUPPORTED,
1648 _("Operation not supported"));
1652 return (* iface->create) (file, flags, cancellable, error);
1657 * @file: input #GFile
1658 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1659 * for the current #GFile, or #NULL to ignore
1660 * @make_backup: %TRUE if a backup should be created
1661 * @flags: a set of #GFileCreateFlags
1662 * @cancellable: (allow-none): optional #GCancellable object,
1664 * @error: a #GError, or %NULL
1666 * Returns an output stream for overwriting the file, possibly
1667 * creating a backup copy of the file first. If the file doesn't exist,
1668 * it will be created.
1670 * This will try to replace the file in the safest way possible so
1671 * that any errors during the writing will not affect an already
1672 * existing copy of the file. For instance, for local files it
1673 * may write to a temporary file and then atomically rename over
1674 * the destination when the stream is closed.
1676 * By default files created are generally readable by everyone,
1677 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1678 * will be made readable only to the current user, to the level that
1679 * is supported on the target filesystem.
1681 * If @cancellable is not %NULL, then the operation can be cancelled
1682 * by triggering the cancellable object from another thread. If the
1683 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1686 * If you pass in a non-%NULL @etag value, then this value is
1687 * compared to the current entity tag of the file, and if they differ
1688 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1689 * that the file has been changed since you last read it. You can get
1690 * the new etag from g_file_output_stream_get_etag() after you've
1691 * finished writing and closed the #GFileOutputStream. When you load
1692 * a new file you can use g_file_input_stream_query_info() to get
1693 * the etag of the file.
1695 * If @make_backup is %TRUE, this function will attempt to make a
1696 * backup of the current file before overwriting it. If this fails
1697 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1698 * want to replace anyway, try again with @make_backup set to %FALSE.
1700 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1701 * be returned, and if the file is some other form of non-regular file
1702 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1703 * file systems don't allow all file names, and may return an
1704 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1705 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1706 * possible too, and depend on what kind of filesystem the file is on.
1708 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1709 * Free the returned object with g_object_unref().
1712 g_file_replace (GFile *file,
1714 gboolean make_backup,
1715 GFileCreateFlags flags,
1716 GCancellable *cancellable,
1721 g_return_val_if_fail (G_IS_FILE (file), NULL);
1723 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1726 iface = G_FILE_GET_IFACE (file);
1728 if (iface->replace == NULL)
1730 g_set_error_literal (error, G_IO_ERROR,
1731 G_IO_ERROR_NOT_SUPPORTED,
1732 _("Operation not supported"));
1736 /* Handle empty tag string as NULL in consistent way. */
1737 if (etag && *etag == 0)
1740 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1744 * g_file_open_readwrite:
1745 * @file: #GFile to open
1746 * @cancellable: (allow-none): a #GCancellable
1747 * @error: a #GError, or %NULL
1749 * Opens an existing file for reading and writing. The result is
1750 * a #GFileIOStream that can be used to read and write the contents
1753 * If @cancellable is not %NULL, then the operation can be cancelled
1754 * by triggering the cancellable object from another thread. If the
1755 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1758 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1759 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1760 * error will be returned. Other errors are possible too, and depend on
1761 * what kind of filesystem the file is on. Note that in many non-local
1762 * file cases read and write streams are not supported, so make sure you
1763 * really need to do read and write streaming, rather than just opening
1764 * for reading or writing.
1766 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1767 * Free the returned object with g_object_unref().
1772 g_file_open_readwrite (GFile *file,
1773 GCancellable *cancellable,
1778 g_return_val_if_fail (G_IS_FILE (file), NULL);
1780 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1783 iface = G_FILE_GET_IFACE (file);
1785 if (iface->open_readwrite == NULL)
1787 g_set_error_literal (error, G_IO_ERROR,
1788 G_IO_ERROR_NOT_SUPPORTED,
1789 _("Operation not supported"));
1793 return (* iface->open_readwrite) (file, cancellable, error);
1797 * g_file_create_readwrite:
1799 * @flags: a set of #GFileCreateFlags
1800 * @cancellable: (allow-none): optional #GCancellable object,
1802 * @error: return location for a #GError, or %NULL
1804 * Creates a new file and returns a stream for reading and
1805 * writing to it. The file must not already exist.
1807 * By default files created are generally readable by everyone,
1808 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1809 * will be made readable only to the current user, to the level
1810 * that is supported on the target filesystem.
1812 * If @cancellable is not %NULL, then the operation can be cancelled
1813 * by triggering the cancellable object from another thread. If the
1814 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1817 * If a file or directory with this name already exists, the
1818 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1819 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1820 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1821 * will be returned. Other errors are possible too, and depend on what
1822 * kind of filesystem the file is on.
1824 * Note that in many non-local file cases read and write streams are
1825 * not supported, so make sure you really need to do read and write
1826 * streaming, rather than just opening for reading or writing.
1828 * Returns: (transfer full): a #GFileIOStream for the newly created
1829 * file, or %NULL on error.
1830 * Free the returned object with g_object_unref().
1835 g_file_create_readwrite (GFile *file,
1836 GFileCreateFlags flags,
1837 GCancellable *cancellable,
1842 g_return_val_if_fail (G_IS_FILE (file), NULL);
1844 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1847 iface = G_FILE_GET_IFACE (file);
1849 if (iface->create_readwrite == NULL)
1851 g_set_error_literal (error, G_IO_ERROR,
1852 G_IO_ERROR_NOT_SUPPORTED,
1853 _("Operation not supported"));
1857 return (* iface->create_readwrite) (file, flags, cancellable, error);
1861 * g_file_replace_readwrite:
1863 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1864 * for the current #GFile, or #NULL to ignore
1865 * @make_backup: %TRUE if a backup should be created
1866 * @flags: a set of #GFileCreateFlags
1867 * @cancellable: (allow-none): optional #GCancellable object,
1869 * @error: return location for a #GError, or %NULL
1871 * Returns an output stream for overwriting the file in readwrite mode,
1872 * possibly creating a backup copy of the file first. If the file doesn't
1873 * exist, it will be created.
1875 * For details about the behaviour, see g_file_replace() which does the
1876 * same thing but returns an output stream only.
1878 * Note that in many non-local file cases read and write streams are not
1879 * supported, so make sure you really need to do read and write streaming,
1880 * rather than just opening for reading or writing.
1882 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1883 * Free the returned object with g_object_unref().
1888 g_file_replace_readwrite (GFile *file,
1890 gboolean make_backup,
1891 GFileCreateFlags flags,
1892 GCancellable *cancellable,
1897 g_return_val_if_fail (G_IS_FILE (file), NULL);
1899 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1902 iface = G_FILE_GET_IFACE (file);
1904 if (iface->replace_readwrite == NULL)
1906 g_set_error_literal (error, G_IO_ERROR,
1907 G_IO_ERROR_NOT_SUPPORTED,
1908 _("Operation not supported"));
1912 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1916 * g_file_read_async:
1917 * @file: input #GFile
1918 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1920 * @cancellable: (allow-none): optional #GCancellable object,
1922 * @callback: (scope async): a #GAsyncReadyCallback to call
1923 * when the request is satisfied
1924 * @user_data: (closure): the data to pass to callback function
1926 * Asynchronously opens @file for reading.
1928 * For more details, see g_file_read() which is
1929 * the synchronous version of this call.
1931 * When the operation is finished, @callback will be called.
1932 * You can then call g_file_read_finish() to get the result
1936 g_file_read_async (GFile *file,
1938 GCancellable *cancellable,
1939 GAsyncReadyCallback callback,
1944 g_return_if_fail (G_IS_FILE (file));
1946 iface = G_FILE_GET_IFACE (file);
1947 (* iface->read_async) (file,
1955 * g_file_read_finish:
1956 * @file: input #GFile
1957 * @res: a #GAsyncResult
1958 * @error: a #GError, or %NULL
1960 * Finishes an asynchronous file read operation started with
1961 * g_file_read_async().
1963 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1964 * Free the returned object with g_object_unref().
1967 g_file_read_finish (GFile *file,
1973 g_return_val_if_fail (G_IS_FILE (file), NULL);
1974 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1976 if (g_async_result_legacy_propagate_error (res, error))
1979 iface = G_FILE_GET_IFACE (file);
1980 return (* iface->read_finish) (file, res, error);
1984 * g_file_append_to_async:
1985 * @file: input #GFile
1986 * @flags: a set of #GFileCreateFlags
1987 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1989 * @cancellable: (allow-none): optional #GCancellable object,
1991 * @callback: (scope async): a #GAsyncReadyCallback to call
1992 * when the request is satisfied
1993 * @user_data: (closure): the data to pass to callback function
1995 * Asynchronously opens @file for appending.
1997 * For more details, see g_file_append_to() which is
1998 * the synchronous version of this call.
2000 * When the operation is finished, @callback will be called.
2001 * You can then call g_file_append_to_finish() to get the result
2005 g_file_append_to_async (GFile *file,
2006 GFileCreateFlags flags,
2008 GCancellable *cancellable,
2009 GAsyncReadyCallback callback,
2014 g_return_if_fail (G_IS_FILE (file));
2016 iface = G_FILE_GET_IFACE (file);
2017 (* iface->append_to_async) (file,
2026 * g_file_append_to_finish:
2027 * @file: input #GFile
2028 * @res: #GAsyncResult
2029 * @error: a #GError, or %NULL
2031 * Finishes an asynchronous file append operation started with
2032 * g_file_append_to_async().
2034 * Returns: (transfer full): a valid #GFileOutputStream
2035 * or %NULL on error.
2036 * Free the returned object with g_object_unref().
2039 g_file_append_to_finish (GFile *file,
2045 g_return_val_if_fail (G_IS_FILE (file), NULL);
2046 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2048 if (g_async_result_legacy_propagate_error (res, error))
2051 iface = G_FILE_GET_IFACE (file);
2052 return (* iface->append_to_finish) (file, res, error);
2056 * g_file_create_async:
2057 * @file: input #GFile
2058 * @flags: a set of #GFileCreateFlags
2059 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2061 * @cancellable: (allow-none): optional #GCancellable object,
2063 * @callback: (scope async): a #GAsyncReadyCallback to call
2064 * when the request is satisfied
2065 * @user_data: (closure): the data to pass to callback function
2067 * Asynchronously creates a new file and returns an output stream
2068 * for writing to it. The file must not already exist.
2070 * For more details, see g_file_create() which is
2071 * the synchronous version of this call.
2073 * When the operation is finished, @callback will be called.
2074 * You can then call g_file_create_finish() to get the result
2078 g_file_create_async (GFile *file,
2079 GFileCreateFlags flags,
2081 GCancellable *cancellable,
2082 GAsyncReadyCallback callback,
2087 g_return_if_fail (G_IS_FILE (file));
2089 iface = G_FILE_GET_IFACE (file);
2090 (* iface->create_async) (file,
2099 * g_file_create_finish:
2100 * @file: input #GFile
2101 * @res: a #GAsyncResult
2102 * @error: a #GError, or %NULL
2104 * Finishes an asynchronous file create operation started with
2105 * g_file_create_async().
2107 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2108 * Free the returned object with g_object_unref().
2111 g_file_create_finish (GFile *file,
2117 g_return_val_if_fail (G_IS_FILE (file), NULL);
2118 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2120 if (g_async_result_legacy_propagate_error (res, error))
2123 iface = G_FILE_GET_IFACE (file);
2124 return (* iface->create_finish) (file, res, error);
2128 * g_file_replace_async:
2129 * @file: input #GFile
2130 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2131 * for the current #GFile, or NULL to ignore
2132 * @make_backup: %TRUE if a backup should be created
2133 * @flags: a set of #GFileCreateFlags
2134 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2136 * @cancellable: (allow-none): optional #GCancellable object,
2138 * @callback: (scope async): a #GAsyncReadyCallback to call
2139 * when the request is satisfied
2140 * @user_data: (closure): the data to pass to callback function
2142 * Asynchronously overwrites the file, replacing the contents,
2143 * possibly creating a backup copy of the file first.
2145 * For more details, see g_file_replace() 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_replace_finish() to get the result
2153 g_file_replace_async (GFile *file,
2155 gboolean make_backup,
2156 GFileCreateFlags flags,
2158 GCancellable *cancellable,
2159 GAsyncReadyCallback callback,
2164 g_return_if_fail (G_IS_FILE (file));
2166 iface = G_FILE_GET_IFACE (file);
2167 (* iface->replace_async) (file,
2178 * g_file_replace_finish:
2179 * @file: input #GFile
2180 * @res: a #GAsyncResult
2181 * @error: a #GError, or %NULL
2183 * Finishes an asynchronous file replace operation started with
2184 * g_file_replace_async().
2186 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2187 * Free the returned object with g_object_unref().
2190 g_file_replace_finish (GFile *file,
2196 g_return_val_if_fail (G_IS_FILE (file), NULL);
2197 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2199 if (g_async_result_legacy_propagate_error (res, error))
2202 iface = G_FILE_GET_IFACE (file);
2203 return (* iface->replace_finish) (file, res, error);
2207 * g_file_open_readwrite_async
2208 * @file: input #GFile
2209 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2211 * @cancellable: (allow-none): optional #GCancellable object,
2213 * @callback: (scope async): a #GAsyncReadyCallback to call
2214 * when the request is satisfied
2215 * @user_data: (closure): the data to pass to callback function
2217 * Asynchronously opens @file for reading and writing.
2219 * For more details, see g_file_open_readwrite() which is
2220 * the synchronous version of this call.
2222 * When the operation is finished, @callback will be called.
2223 * You can then call g_file_open_readwrite_finish() to get
2224 * the result of the operation.
2229 g_file_open_readwrite_async (GFile *file,
2231 GCancellable *cancellable,
2232 GAsyncReadyCallback callback,
2237 g_return_if_fail (G_IS_FILE (file));
2239 iface = G_FILE_GET_IFACE (file);
2240 (* iface->open_readwrite_async) (file,
2248 * g_file_open_readwrite_finish:
2249 * @file: input #GFile
2250 * @res: a #GAsyncResult
2251 * @error: a #GError, or %NULL
2253 * Finishes an asynchronous file read operation started with
2254 * g_file_open_readwrite_async().
2256 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2257 * Free the returned object with g_object_unref().
2262 g_file_open_readwrite_finish (GFile *file,
2268 g_return_val_if_fail (G_IS_FILE (file), NULL);
2269 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2271 if (g_async_result_legacy_propagate_error (res, error))
2274 iface = G_FILE_GET_IFACE (file);
2275 return (* iface->open_readwrite_finish) (file, res, error);
2279 * g_file_create_readwrite_async:
2280 * @file: input #GFile
2281 * @flags: a set of #GFileCreateFlags
2282 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2284 * @cancellable: (allow-none): optional #GCancellable object,
2286 * @callback: (scope async): a #GAsyncReadyCallback to call
2287 * when the request is satisfied
2288 * @user_data: (closure): the data to pass to callback function
2290 * Asynchronously creates a new file and returns a stream
2291 * for reading and writing to it. The file must not already exist.
2293 * For more details, see g_file_create_readwrite() which is
2294 * the synchronous version of this call.
2296 * When the operation is finished, @callback will be called.
2297 * You can then call g_file_create_readwrite_finish() to get
2298 * the result of the operation.
2303 g_file_create_readwrite_async (GFile *file,
2304 GFileCreateFlags flags,
2306 GCancellable *cancellable,
2307 GAsyncReadyCallback callback,
2312 g_return_if_fail (G_IS_FILE (file));
2314 iface = G_FILE_GET_IFACE (file);
2315 (* iface->create_readwrite_async) (file,
2324 * g_file_create_readwrite_finish:
2325 * @file: input #GFile
2326 * @res: a #GAsyncResult
2327 * @error: a #GError, or %NULL
2329 * Finishes an asynchronous file create operation started with
2330 * g_file_create_readwrite_async().
2332 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2333 * Free the returned object with g_object_unref().
2338 g_file_create_readwrite_finish (GFile *file,
2344 g_return_val_if_fail (G_IS_FILE (file), NULL);
2345 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2347 if (g_async_result_legacy_propagate_error (res, error))
2350 iface = G_FILE_GET_IFACE (file);
2351 return (* iface->create_readwrite_finish) (file, res, error);
2355 * g_file_replace_readwrite_async:
2356 * @file: input #GFile
2357 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2358 * for the current #GFile, or NULL to ignore
2359 * @make_backup: %TRUE if a backup should be created
2360 * @flags: a set of #GFileCreateFlags
2361 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2363 * @cancellable: (allow-none): optional #GCancellable object,
2365 * @callback: (scope async): a #GAsyncReadyCallback to call
2366 * when the request is satisfied
2367 * @user_data: (closure): the data to pass to callback function
2369 * Asynchronously overwrites the file in read-write mode,
2370 * replacing the contents, possibly creating a backup copy
2371 * of the file first.
2373 * For more details, see g_file_replace_readwrite() which is
2374 * the synchronous version of this call.
2376 * When the operation is finished, @callback will be called.
2377 * You can then call g_file_replace_readwrite_finish() to get
2378 * the result of the operation.
2383 g_file_replace_readwrite_async (GFile *file,
2385 gboolean make_backup,
2386 GFileCreateFlags flags,
2388 GCancellable *cancellable,
2389 GAsyncReadyCallback callback,
2394 g_return_if_fail (G_IS_FILE (file));
2396 iface = G_FILE_GET_IFACE (file);
2397 (* iface->replace_readwrite_async) (file,
2408 * g_file_replace_readwrite_finish:
2409 * @file: input #GFile
2410 * @res: a #GAsyncResult
2411 * @error: a #GError, or %NULL
2413 * Finishes an asynchronous file replace operation started with
2414 * g_file_replace_readwrite_async().
2416 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2417 * Free the returned object with g_object_unref().
2422 g_file_replace_readwrite_finish (GFile *file,
2428 g_return_val_if_fail (G_IS_FILE (file), NULL);
2429 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2431 if (g_async_result_legacy_propagate_error (res, error))
2434 iface = G_FILE_GET_IFACE (file);
2435 return (* iface->replace_readwrite_finish) (file, res, error);
2439 copy_symlink (GFile *destination,
2440 GFileCopyFlags flags,
2441 GCancellable *cancellable,
2446 gboolean tried_delete;
2448 GFileType file_type;
2450 tried_delete = FALSE;
2454 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2456 /* Maybe it already existed, and we want to overwrite? */
2457 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2458 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2460 g_error_free (my_error);
2462 /* Don't overwrite if the destination is a directory */
2463 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2464 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2465 cancellable, &my_error);
2468 file_type = g_file_info_get_file_type (info);
2469 g_object_unref (info);
2471 if (file_type == G_FILE_TYPE_DIRECTORY)
2473 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2474 _("Can't copy over directory"));
2479 if (!g_file_delete (destination, cancellable, error))
2482 tried_delete = TRUE;
2486 g_propagate_error (error, my_error);
2493 static GInputStream *
2494 open_source_for_copy (GFile *source,
2496 GFileCopyFlags flags,
2497 GCancellable *cancellable,
2503 GFileType file_type;
2506 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2510 /* There was an error opening the source, try to set a good error for it: */
2511 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2513 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2514 * as that is less useful to the app. Better check for errors on the
2517 g_error_free (my_error);
2520 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2521 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2522 cancellable, &my_error);
2524 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2526 file_type = g_file_info_get_file_type (info);
2527 g_object_unref (info);
2529 if (flags & G_FILE_COPY_OVERWRITE)
2531 if (file_type == G_FILE_TYPE_DIRECTORY)
2533 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2534 _("Can't copy directory over directory"));
2537 /* continue to would_recurse error */
2541 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2542 _("Target file exists"));
2548 /* Error getting info from target, return that error
2549 * (except for NOT_FOUND, which is no error here)
2551 g_clear_object (&info);
2552 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2554 g_propagate_error (error, my_error);
2557 g_clear_error (&my_error);
2560 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2561 _("Can't recursively copy directory"));
2565 g_propagate_error (error, my_error);
2570 should_copy (GFileAttributeInfo *info,
2572 gboolean skip_perms)
2574 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2578 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2579 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2583 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2584 GFileAttributeInfoList *namespaces,
2586 gboolean skip_perms)
2593 s = g_string_new ("");
2597 for (i = 0; i < attributes->n_infos; i++)
2599 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2604 g_string_append_c (s, ',');
2606 g_string_append (s, attributes->infos[i].name);
2613 for (i = 0; i < namespaces->n_infos; i++)
2615 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2620 g_string_append_c (s, ',');
2622 g_string_append (s, namespaces->infos[i].name);
2623 g_string_append (s, "::*");
2628 return g_string_free (s, FALSE);
2632 * g_file_copy_attributes:
2633 * @source: a #GFile with attributes
2634 * @destination: a #GFile to copy attributes to
2635 * @flags: a set of #GFileCopyFlags
2636 * @cancellable: (allow-none): optional #GCancellable object,
2638 * @error: a #GError, %NULL to ignore
2640 * Copies the file attributes from @source to @destination.
2642 * Normally only a subset of the file attributes are copied,
2643 * those that are copies in a normal file copy operation
2644 * (which for instance does not include e.g. owner). However
2645 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2646 * all the metadata that is possible to copy is copied. This
2647 * is useful when implementing move by copy + delete source.
2649 * Returns: %TRUE if the attributes were copied successfully,
2653 g_file_copy_attributes (GFile *source,
2655 GFileCopyFlags flags,
2656 GCancellable *cancellable,
2659 GFileAttributeInfoList *attributes, *namespaces;
2660 char *attrs_to_read;
2664 gboolean source_nofollow_symlinks;
2665 gboolean skip_perms;
2667 as_move = flags & G_FILE_COPY_ALL_METADATA;
2668 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2669 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2671 /* Ignore errors here, if the target supports no attributes there is
2674 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2675 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2677 if (attributes == NULL && namespaces == NULL)
2680 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2682 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2683 * we just don't copy it.
2685 info = g_file_query_info (source, attrs_to_read,
2686 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2690 g_free (attrs_to_read);
2695 res = g_file_set_attributes_from_info (destination,
2697 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2700 g_object_unref (info);
2703 g_file_attribute_info_list_unref (attributes);
2704 g_file_attribute_info_list_unref (namespaces);
2710 copy_stream_with_progress (GInputStream *in,
2713 GCancellable *cancellable,
2714 GFileProgressCallback progress_callback,
2715 gpointer progress_callback_data,
2718 gssize n_read, n_written;
2719 goffset current_size;
2720 char buffer[1024*64], *p;
2726 /* avoid performance impact of querying total size when it's not needed */
2727 if (progress_callback)
2729 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2730 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2734 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2735 total_size = g_file_info_get_size (info);
2736 g_object_unref (info);
2739 if (total_size == -1)
2741 info = g_file_query_info (source,
2742 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2743 G_FILE_QUERY_INFO_NONE,
2747 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2748 total_size = g_file_info_get_size (info);
2749 g_object_unref (info);
2754 if (total_size == -1)
2761 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2771 current_size += n_read;
2776 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2777 if (n_written == -1)
2784 n_read -= n_written;
2790 if (progress_callback)
2791 progress_callback (current_size, total_size, progress_callback_data);
2794 /* Make sure we send full copied size */
2795 if (progress_callback)
2796 progress_callback (current_size, total_size, progress_callback_data);
2804 do_splice (int fd_in,
2809 long *bytes_transferd,
2815 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2823 else if (errsv == ENOSYS || errsv == EINVAL)
2824 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2825 _("Splice not supported"));
2827 g_set_error (error, G_IO_ERROR,
2828 g_io_error_from_errno (errsv),
2829 _("Error splicing file: %s"),
2830 g_strerror (errsv));
2835 *bytes_transferd = result;
2840 splice_stream_with_progress (GInputStream *in,
2842 GCancellable *cancellable,
2843 GFileProgressCallback progress_callback,
2844 gpointer progress_callback_data,
2847 int buffer[2] = { -1, -1 };
2854 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2855 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2857 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2861 /* avoid performance impact of querying total size when it's not needed */
2862 if (progress_callback)
2866 if (fstat (fd_in, &sbuf) == 0)
2867 total_size = sbuf.st_size;
2870 if (total_size == -1)
2873 offset_in = offset_out = 0;
2880 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2883 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2894 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2897 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2900 n_read -= n_written;
2903 if (progress_callback)
2904 progress_callback (offset_in, total_size, progress_callback_data);
2907 /* Make sure we send full copied size */
2908 if (progress_callback)
2909 progress_callback (offset_in, total_size, progress_callback_data);
2911 if (!g_close (buffer[0], error))
2914 if (!g_close (buffer[1], error))
2918 if (buffer[0] != -1)
2919 (void) g_close (buffer[0], NULL);
2920 if (buffer[1] != -1)
2921 (void) g_close (buffer[1], NULL);
2929 btrfs_reflink_with_progress (GInputStream *in,
2932 GCancellable *cancellable,
2933 GFileProgressCallback progress_callback,
2934 gpointer progress_callback_data,
2937 goffset source_size;
2941 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2942 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2944 if (progress_callback)
2945 source_size = g_file_info_get_size (info);
2947 /* Btrfs clone ioctl properties:
2948 * - Works at the inode level
2949 * - Doesn't work with directories
2950 * - Always follows symlinks (source and destination)
2952 * By the time we get here, *in and *out are both regular files */
2953 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
2958 g_set_error_literal (error, G_IO_ERROR,
2959 G_IO_ERROR_NOT_SUPPORTED,
2960 _("Copy (reflink/clone) between mounts is not supported"));
2961 else if (errno == EINVAL)
2962 g_set_error_literal (error, G_IO_ERROR,
2963 G_IO_ERROR_NOT_SUPPORTED,
2964 _("Copy (reflink/clone) is not supported or invalid"));
2966 /* Most probably something odd happened; retry with fallback */
2967 g_set_error_literal (error, G_IO_ERROR,
2968 G_IO_ERROR_NOT_SUPPORTED,
2969 _("Copy (reflink/clone) is not supported or didn't work"));
2970 /* We retry with fallback for all error cases because Btrfs is currently
2971 * unstable, and so we can't trust it to do clone properly.
2972 * In addition, any hard errors here would cause the same failure in the
2973 * fallback manual copy as well. */
2977 /* Make sure we send full copied size */
2978 if (progress_callback)
2979 progress_callback (source_size, source_size, progress_callback_data);
2986 file_copy_fallback (GFile *source,
2988 GFileCopyFlags flags,
2989 GCancellable *cancellable,
2990 GFileProgressCallback progress_callback,
2991 gpointer progress_callback_data,
2994 gboolean ret = FALSE;
2995 GInputStream *in = NULL;
2996 GOutputStream *out = NULL;
2997 GFileInfo *info = NULL;
3000 /* need to know the file type */
3001 info = g_file_query_info (source,
3002 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3003 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3009 /* Maybe copy the symlink? */
3010 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3011 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3013 target = g_file_info_get_symlink_target (info);
3016 if (!copy_symlink (destination, flags, cancellable, target, error))
3022 /* ... else fall back on a regular file copy */
3024 /* Handle "special" files (pipes, device nodes, ...)? */
3025 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3027 /* FIXME: could try to recreate device nodes and others? */
3028 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3029 _("Can't copy special file"));
3033 /* Everything else should just fall back on a regular copy. */
3035 in = open_source_for_copy (source, destination, flags, cancellable, error);
3039 if (flags & G_FILE_COPY_OVERWRITE)
3041 out = (GOutputStream *)g_file_replace (destination,
3043 flags & G_FILE_COPY_BACKUP,
3044 G_FILE_CREATE_REPLACE_DESTINATION,
3045 cancellable, error);
3049 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3056 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3058 GError *reflink_err = NULL;
3060 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3061 progress_callback, progress_callback_data,
3064 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3066 g_clear_error (&reflink_err);
3070 g_propagate_error (error, reflink_err);
3083 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3085 GError *splice_err = NULL;
3087 if (!splice_stream_with_progress (in, out, cancellable,
3088 progress_callback, progress_callback_data,
3091 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3093 g_clear_error (&splice_err);
3097 g_propagate_error (error, splice_err);
3110 /* A plain read/write loop */
3111 if (!copy_stream_with_progress (in, out, source, cancellable,
3112 progress_callback, progress_callback_data,
3118 /* Ignore errors here. Failure to copy metadata is not a hard error */
3120 (void) g_file_copy_attributes (source, destination,
3121 flags, cancellable, NULL);
3125 /* Don't care about errors in source here */
3126 (void) g_input_stream_close (in, cancellable, NULL);
3127 g_object_unref (in);
3132 /* But write errors on close are bad! */
3133 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3135 g_object_unref (out);
3138 g_clear_object (&info);
3145 * @source: input #GFile
3146 * @destination: destination #GFile
3147 * @flags: set of #GFileCopyFlags
3148 * @cancellable: (allow-none): optional #GCancellable object,
3150 * @progress_callback: (allow-none) (scope call): function to callback with
3151 * progress information, or %NULL if progress information is not needed
3152 * @progress_callback_data: (closure): user data to pass to @progress_callback
3153 * @error: #GError to set on error, or %NULL
3155 * Copies the file @source to the location specified by @destination.
3156 * Can not handle recursive copies of directories.
3158 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3159 * existing @destination file is overwritten.
3161 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3162 * will be copied as symlinks, otherwise the target of the
3163 * @source symlink will be copied.
3165 * If @cancellable is not %NULL, then the operation can be cancelled by
3166 * triggering the cancellable object from another thread. If the operation
3167 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3169 * If @progress_callback is not %NULL, then the operation can be monitored
3170 * by setting this to a #GFileProgressCallback function.
3171 * @progress_callback_data will be passed to this function. It is guaranteed
3172 * that this callback will be called after all data has been transferred with
3173 * the total number of bytes copied during the operation.
3175 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3176 * is returned, independent on the status of the @destination.
3178 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3179 * the error %G_IO_ERROR_EXISTS is returned.
3181 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3182 * error is returned. If trying to overwrite a directory with a directory the
3183 * %G_IO_ERROR_WOULD_MERGE error is returned.
3185 * If the source is a directory and the target does not exist, or
3186 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3187 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3189 * If you are interested in copying the #GFile object itself (not the on-disk
3190 * file), see g_file_dup().
3192 * Returns: %TRUE on success, %FALSE otherwise.
3195 g_file_copy (GFile *source,
3197 GFileCopyFlags flags,
3198 GCancellable *cancellable,
3199 GFileProgressCallback progress_callback,
3200 gpointer progress_callback_data,
3207 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3208 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3210 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3213 iface = G_FILE_GET_IFACE (destination);
3217 res = (* iface->copy) (source, destination,
3219 progress_callback, progress_callback_data,
3225 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3227 g_propagate_error (error, my_error);
3231 g_clear_error (&my_error);
3234 /* If the types are different, and the destination method failed
3235 * also try the source method
3237 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3239 iface = G_FILE_GET_IFACE (source);
3244 res = (* iface->copy) (source, destination,
3246 progress_callback, progress_callback_data,
3252 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3254 g_propagate_error (error, my_error);
3258 g_clear_error (&my_error);
3262 return file_copy_fallback (source, destination, flags, cancellable,
3263 progress_callback, progress_callback_data,
3268 * g_file_copy_async: (skip)
3269 * @source: input #GFile
3270 * @destination: destination #GFile
3271 * @flags: set of #GFileCopyFlags
3272 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3274 * @cancellable: (allow-none): optional #GCancellable object,
3276 * @progress_callback: (allow-none): function to callback with progress
3277 * information, or %NULL if progress information is not needed
3278 * @progress_callback_data: (closure): user data to pass to @progress_callback
3279 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3280 * @user_data: the data to pass to callback function
3282 * Copies the file @source to the location specified by @destination
3283 * asynchronously. For details of the behaviour, see g_file_copy().
3285 * If @progress_callback is not %NULL, then that function that will be called
3286 * just like in g_file_copy(), however the callback will run in the main loop,
3287 * not in the thread that is doing the I/O operation.
3289 * When the operation is finished, @callback will be called. You can then call
3290 * g_file_copy_finish() to get the result of the operation.
3293 g_file_copy_async (GFile *source,
3295 GFileCopyFlags flags,
3297 GCancellable *cancellable,
3298 GFileProgressCallback progress_callback,
3299 gpointer progress_callback_data,
3300 GAsyncReadyCallback callback,
3305 g_return_if_fail (G_IS_FILE (source));
3306 g_return_if_fail (G_IS_FILE (destination));
3308 iface = G_FILE_GET_IFACE (source);
3309 (* iface->copy_async) (source,
3315 progress_callback_data,
3321 * g_file_copy_finish:
3322 * @file: input #GFile
3323 * @res: a #GAsyncResult
3324 * @error: a #GError, or %NULL
3326 * Finishes copying the file started with g_file_copy_async().
3328 * Returns: a %TRUE on success, %FALSE on error.
3331 g_file_copy_finish (GFile *file,
3337 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3338 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3340 if (g_async_result_legacy_propagate_error (res, error))
3343 iface = G_FILE_GET_IFACE (file);
3344 return (* iface->copy_finish) (file, res, error);
3349 * @source: #GFile pointing to the source location
3350 * @destination: #GFile pointing to the destination location
3351 * @flags: set of #GFileCopyFlags
3352 * @cancellable: (allow-none): optional #GCancellable object,
3354 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3355 * function for updates
3356 * @progress_callback_data: (closure): gpointer to user data for
3357 * the callback function
3358 * @error: #GError for returning error conditions, or %NULL
3360 * Tries to move the file or directory @source to the location specified
3361 * by @destination. If native move operations are supported then this is
3362 * used, otherwise a copy + delete fallback is used. The native
3363 * implementation may support moving directories (for instance on moves
3364 * inside the same filesystem), but the fallback code does not.
3366 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3367 * existing @destination file is overwritten.
3369 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3370 * will be copied as symlinks, otherwise the target of the
3371 * @source symlink will be copied.
3373 * If @cancellable is not %NULL, then the operation can be cancelled by
3374 * triggering the cancellable object from another thread. If the operation
3375 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3377 * If @progress_callback is not %NULL, then the operation can be monitored
3378 * by setting this to a #GFileProgressCallback function.
3379 * @progress_callback_data will be passed to this function. It is
3380 * guaranteed that this callback will be called after all data has been
3381 * transferred with the total number of bytes copied during the operation.
3383 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3384 * error is returned, independent on the status of the @destination.
3386 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3387 * then the error %G_IO_ERROR_EXISTS is returned.
3389 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3390 * error is returned. If trying to overwrite a directory with a directory the
3391 * %G_IO_ERROR_WOULD_MERGE error is returned.
3393 * If the source is a directory and the target does not exist, or
3394 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3395 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3396 * move operation isn't available).
3398 * Returns: %TRUE on successful move, %FALSE otherwise.
3401 g_file_move (GFile *source,
3403 GFileCopyFlags flags,
3404 GCancellable *cancellable,
3405 GFileProgressCallback progress_callback,
3406 gpointer progress_callback_data,
3413 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3414 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3416 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3419 iface = G_FILE_GET_IFACE (destination);
3423 res = (* iface->move) (source, destination,
3425 progress_callback, progress_callback_data,
3431 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3433 g_propagate_error (error, my_error);
3438 /* If the types are different, and the destination method failed
3439 * also try the source method
3441 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3443 iface = G_FILE_GET_IFACE (source);
3448 res = (* iface->move) (source, destination,
3450 progress_callback, progress_callback_data,
3456 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3458 g_propagate_error (error, my_error);
3464 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3466 g_set_error_literal (error, G_IO_ERROR,
3467 G_IO_ERROR_NOT_SUPPORTED,
3468 _("Operation not supported"));
3472 flags |= G_FILE_COPY_ALL_METADATA;
3473 if (!g_file_copy (source, destination, flags, cancellable,
3474 progress_callback, progress_callback_data,
3478 return g_file_delete (source, cancellable, error);
3482 * g_file_make_directory:
3483 * @file: input #GFile
3484 * @cancellable: (allow-none): optional #GCancellable object,
3486 * @error: a #GError, or %NULL
3488 * Creates a directory. Note that this will only create a child directory
3489 * of the immediate parent directory of the path or URI given by the #GFile.
3490 * To recursively create directories, see g_file_make_directory_with_parents().
3491 * This function will fail if the parent directory does not exist, setting
3492 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3493 * creating directories, this function will fail, setting @error to
3494 * %G_IO_ERROR_NOT_SUPPORTED.
3496 * For a local #GFile the newly created directory will have the default
3497 * (current) ownership and permissions of the current process.
3499 * If @cancellable is not %NULL, then the operation can be cancelled by
3500 * triggering the cancellable object from another thread. If the operation
3501 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3503 * Returns: %TRUE on successful creation, %FALSE otherwise.
3506 g_file_make_directory (GFile *file,
3507 GCancellable *cancellable,
3512 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3514 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3517 iface = G_FILE_GET_IFACE (file);
3519 if (iface->make_directory == NULL)
3521 g_set_error_literal (error, G_IO_ERROR,
3522 G_IO_ERROR_NOT_SUPPORTED,
3523 _("Operation not supported"));
3527 return (* iface->make_directory) (file, cancellable, error);
3531 * g_file_make_directory_with_parents:
3532 * @file: input #GFile
3533 * @cancellable: (allow-none): optional #GCancellable object,
3535 * @error: a #GError, or %NULL
3537 * Creates a directory and any parent directories that may not
3538 * exist similar to 'mkdir -p'. If the file system does not support
3539 * creating directories, this function will fail, setting @error to
3540 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3541 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3542 * the similar g_mkdir_with_parents().
3544 * For a local #GFile the newly created directories will have the default
3545 * (current) ownership and permissions of the current process.
3547 * If @cancellable is not %NULL, then the operation can be cancelled by
3548 * triggering the cancellable object from another thread. If the operation
3549 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3551 * Returns: %TRUE if all directories have been successfully created, %FALSE
3557 g_file_make_directory_with_parents (GFile *file,
3558 GCancellable *cancellable,
3561 GFile *work_file = NULL;
3562 GList *list = NULL, *l;
3563 GError *my_error = NULL;
3565 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3567 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3570 g_file_make_directory (file, cancellable, &my_error);
3571 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3574 g_propagate_error (error, my_error);
3575 return my_error == NULL;
3578 work_file = g_object_ref (file);
3580 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3584 parent_file = g_file_get_parent (work_file);
3585 if (parent_file == NULL)
3588 g_clear_error (&my_error);
3589 g_file_make_directory (parent_file, cancellable, &my_error);
3591 g_object_unref (work_file);
3592 work_file = g_object_ref (parent_file);
3594 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3595 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3597 g_object_unref (parent_file);
3600 for (l = list; my_error == NULL && l; l = l->next)
3602 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3606 g_object_unref (work_file);
3609 while (list != NULL)
3611 g_object_unref ((GFile *) list->data);
3612 list = g_list_remove (list, list->data);
3615 if (my_error != NULL)
3617 g_propagate_error (error, my_error);
3621 return g_file_make_directory (file, cancellable, error);
3625 * g_file_make_symbolic_link:
3626 * @file: a #GFile with the name of the symlink to create
3627 * @symlink_value: a string with the path for the target of the new symlink
3628 * @cancellable: (allow-none): optional #GCancellable object,
3632 * Creates a symbolic link named @file which contains the string
3635 * If @cancellable is not %NULL, then the operation can be cancelled by
3636 * triggering the cancellable object from another thread. If the operation
3637 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3639 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3642 g_file_make_symbolic_link (GFile *file,
3643 const char *symlink_value,
3644 GCancellable *cancellable,
3649 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3650 g_return_val_if_fail (symlink_value != NULL, FALSE);
3652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3655 if (*symlink_value == '\0')
3657 g_set_error_literal (error, G_IO_ERROR,
3658 G_IO_ERROR_INVALID_ARGUMENT,
3659 _("Invalid symlink value given"));
3663 iface = G_FILE_GET_IFACE (file);
3665 if (iface->make_symbolic_link == NULL)
3667 g_set_error_literal (error, G_IO_ERROR,
3668 G_IO_ERROR_NOT_SUPPORTED,
3669 _("Operation not supported"));
3673 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3678 * @file: input #GFile
3679 * @cancellable: (allow-none): optional #GCancellable object,
3681 * @error: a #GError, or %NULL
3683 * Deletes a file. If the @file is a directory, it will only be
3684 * deleted if it is empty. This has the same semantics as g_unlink().
3686 * If @cancellable is not %NULL, then the operation can be cancelled by
3687 * triggering the cancellable object from another thread. If the operation
3688 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3690 * Virtual: delete_file
3691 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3694 g_file_delete (GFile *file,
3695 GCancellable *cancellable,
3700 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3702 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3705 iface = G_FILE_GET_IFACE (file);
3707 if (iface->delete_file == NULL)
3709 g_set_error_literal (error, G_IO_ERROR,
3710 G_IO_ERROR_NOT_SUPPORTED,
3711 _("Operation not supported"));
3715 return (* iface->delete_file) (file, cancellable, error);
3719 * g_file_delete_async:
3720 * @file: input #GFile
3721 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3723 * @cancellable: (allow-none): optional #GCancellable object,
3725 * @callback: a #GAsyncReadyCallback to call
3726 * when the request is satisfied
3727 * @user_data: the data to pass to callback function
3729 * Asynchronously delete a file. If the @file is a directory, it will
3730 * only be deleted if it is empty. This has the same semantics as
3733 * Virtual: delete_file_async
3737 g_file_delete_async (GFile *file,
3739 GCancellable *cancellable,
3740 GAsyncReadyCallback callback,
3745 g_return_if_fail (G_IS_FILE (file));
3747 iface = G_FILE_GET_IFACE (file);
3748 (* iface->delete_file_async) (file,
3756 * g_file_delete_finish:
3757 * @file: input #GFile
3758 * @result: a #GAsyncResult
3759 * @error: a #GError, or %NULL
3761 * Finishes deleting a file started with g_file_delete_async().
3763 * Virtual: delete_file_finish
3767 g_file_delete_finish (GFile *file,
3768 GAsyncResult *result,
3773 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3774 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3776 if (g_async_result_legacy_propagate_error (result, error))
3779 iface = G_FILE_GET_IFACE (file);
3780 return (* iface->delete_file_finish) (file, result, error);
3785 * @file: #GFile to send to trash
3786 * @cancellable: (allow-none): optional #GCancellable object,
3788 * @error: a #GError, or %NULL
3790 * Sends @file to the "Trashcan", if possible. This is similar to
3791 * deleting it, but the user can recover it before emptying the trashcan.
3792 * Not all file systems support trashing, so this call can return the
3793 * %G_IO_ERROR_NOT_SUPPORTED error.
3795 * If @cancellable is not %NULL, then the operation can be cancelled by
3796 * triggering the cancellable object from another thread. If the operation
3797 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3799 * Returns: %TRUE on successful trash, %FALSE otherwise.
3802 g_file_trash (GFile *file,
3803 GCancellable *cancellable,
3808 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3810 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3813 iface = G_FILE_GET_IFACE (file);
3815 if (iface->trash == NULL)
3817 g_set_error_literal (error,
3818 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3819 _("Trash not supported"));
3823 return (* iface->trash) (file, cancellable, error);
3827 * g_file_set_display_name:
3828 * @file: input #GFile
3829 * @display_name: a string
3830 * @cancellable: (allow-none): optional #GCancellable object,
3832 * @error: a #GError, or %NULL
3834 * Renames @file to the specified display name.
3836 * The display name is converted from UTF-8 to the correct encoding
3837 * for the target filesystem if possible and the @file is renamed to this.
3839 * If you want to implement a rename operation in the user interface the
3840 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3841 * initial value in the rename widget, and then the result after editing
3842 * should be passed to g_file_set_display_name().
3844 * On success the resulting converted filename is returned.
3846 * If @cancellable is not %NULL, then the operation can be cancelled by
3847 * triggering the cancellable object from another thread. If the operation
3848 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3850 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3851 * or %NULL if there was an error.
3852 * Free the returned object with g_object_unref().
3855 g_file_set_display_name (GFile *file,
3856 const gchar *display_name,
3857 GCancellable *cancellable,
3862 g_return_val_if_fail (G_IS_FILE (file), NULL);
3863 g_return_val_if_fail (display_name != NULL, NULL);
3865 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3869 G_IO_ERROR_INVALID_ARGUMENT,
3870 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3874 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3877 iface = G_FILE_GET_IFACE (file);
3879 return (* iface->set_display_name) (file, display_name, cancellable, error);
3883 * g_file_set_display_name_async:
3884 * @file: input #GFile
3885 * @display_name: a string
3886 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3888 * @cancellable: (allow-none): optional #GCancellable object,
3890 * @callback: (scope async): a #GAsyncReadyCallback to call
3891 * when the request is satisfied
3892 * @user_data: (closure): the data to pass to callback function
3894 * Asynchronously sets the display name for a given #GFile.
3896 * For more details, see g_file_set_display_name() which is
3897 * the synchronous version of this call.
3899 * When the operation is finished, @callback will be called.
3900 * You can then call g_file_set_display_name_finish() to get
3901 * the result of the operation.
3904 g_file_set_display_name_async (GFile *file,
3905 const gchar *display_name,
3907 GCancellable *cancellable,
3908 GAsyncReadyCallback callback,
3913 g_return_if_fail (G_IS_FILE (file));
3914 g_return_if_fail (display_name != NULL);
3916 iface = G_FILE_GET_IFACE (file);
3917 (* iface->set_display_name_async) (file,
3926 * g_file_set_display_name_finish:
3927 * @file: input #GFile
3928 * @res: a #GAsyncResult
3929 * @error: a #GError, or %NULL
3931 * Finishes setting a display name started with
3932 * g_file_set_display_name_async().
3934 * Returns: (transfer full): a #GFile or %NULL on error.
3935 * Free the returned object with g_object_unref().
3938 g_file_set_display_name_finish (GFile *file,
3944 g_return_val_if_fail (G_IS_FILE (file), NULL);
3945 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3947 if (g_async_result_legacy_propagate_error (res, error))
3950 iface = G_FILE_GET_IFACE (file);
3951 return (* iface->set_display_name_finish) (file, res, error);
3955 * g_file_query_settable_attributes:
3956 * @file: input #GFile
3957 * @cancellable: (allow-none): optional #GCancellable object,
3959 * @error: a #GError, or %NULL
3961 * Obtain the list of settable attributes for the file.
3963 * Returns the type and full attribute name of all the attributes
3964 * that can be set on this file. This doesn't mean setting it will
3965 * always succeed though, you might get an access failure, or some
3966 * specific file may not support a specific attribute.
3968 * If @cancellable is not %NULL, then the operation can be cancelled by
3969 * triggering the cancellable object from another thread. If the operation
3970 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3972 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3973 * When you are done with it, release it with
3974 * g_file_attribute_info_list_unref()
3976 GFileAttributeInfoList *
3977 g_file_query_settable_attributes (GFile *file,
3978 GCancellable *cancellable,
3983 GFileAttributeInfoList *list;
3985 g_return_val_if_fail (G_IS_FILE (file), NULL);
3987 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3990 iface = G_FILE_GET_IFACE (file);
3992 if (iface->query_settable_attributes == NULL)
3993 return g_file_attribute_info_list_new ();
3996 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4000 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4002 list = g_file_attribute_info_list_new ();
4003 g_error_free (my_error);
4006 g_propagate_error (error, my_error);
4013 * g_file_query_writable_namespaces:
4014 * @file: input #GFile
4015 * @cancellable: (allow-none): optional #GCancellable object,
4017 * @error: a #GError, or %NULL
4019 * Obtain the list of attribute namespaces where new attributes
4020 * can be created by a user. An example of this is extended
4021 * attributes (in the "xattr" namespace).
4023 * If @cancellable is not %NULL, then the operation can be cancelled by
4024 * triggering the cancellable object from another thread. If the operation
4025 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4027 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4028 * When you are done with it, release it with
4029 * g_file_attribute_info_list_unref()
4031 GFileAttributeInfoList *
4032 g_file_query_writable_namespaces (GFile *file,
4033 GCancellable *cancellable,
4038 GFileAttributeInfoList *list;
4040 g_return_val_if_fail (G_IS_FILE (file), NULL);
4042 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4045 iface = G_FILE_GET_IFACE (file);
4047 if (iface->query_writable_namespaces == NULL)
4048 return g_file_attribute_info_list_new ();
4051 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4055 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4057 list = g_file_attribute_info_list_new ();
4058 g_error_free (my_error);
4061 g_propagate_error (error, my_error);
4068 * g_file_set_attribute:
4069 * @file: input #GFile
4070 * @attribute: a string containing the attribute's name
4071 * @type: The type of the attribute
4072 * @value_p: (allow-none): a pointer to the value (or the pointer
4073 * itself if the type is a pointer type)
4074 * @flags: a set of #GFileQueryInfoFlags
4075 * @cancellable: (allow-none): optional #GCancellable object,
4077 * @error: a #GError, or %NULL
4079 * Sets an attribute in the file with attribute name @attribute to @value.
4081 * Some attributes can be unset by setting @attribute to
4082 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4084 * If @cancellable is not %NULL, then the operation can be cancelled by
4085 * triggering the cancellable object from another thread. If the operation
4086 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4088 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4091 g_file_set_attribute (GFile *file,
4092 const gchar *attribute,
4093 GFileAttributeType type,
4095 GFileQueryInfoFlags flags,
4096 GCancellable *cancellable,
4101 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4102 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4104 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4107 iface = G_FILE_GET_IFACE (file);
4109 if (iface->set_attribute == NULL)
4111 g_set_error_literal (error, G_IO_ERROR,
4112 G_IO_ERROR_NOT_SUPPORTED,
4113 _("Operation not supported"));
4117 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4121 * g_file_set_attributes_from_info:
4122 * @file: input #GFile
4123 * @info: a #GFileInfo
4124 * @flags: #GFileQueryInfoFlags
4125 * @cancellable: (allow-none): optional #GCancellable object,
4127 * @error: a #GError, or %NULL
4129 * Tries to set all attributes in the #GFileInfo on the target
4130 * values, not stopping on the first error.
4132 * If there is any error during this operation then @error will
4133 * be set to the first error. Error on particular fields are flagged
4134 * by setting the "status" field in the attribute value to
4135 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4136 * also detect further errors.
4138 * If @cancellable is not %NULL, then the operation can be cancelled by
4139 * triggering the cancellable object from another thread. If the operation
4140 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4142 * Returns: %FALSE if there was any error, %TRUE otherwise.
4145 g_file_set_attributes_from_info (GFile *file,
4147 GFileQueryInfoFlags flags,
4148 GCancellable *cancellable,
4153 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4154 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4156 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4159 g_file_info_clear_status (info);
4161 iface = G_FILE_GET_IFACE (file);
4163 return (* iface->set_attributes_from_info) (file,
4171 g_file_real_set_attributes_from_info (GFile *file,
4173 GFileQueryInfoFlags flags,
4174 GCancellable *cancellable,
4180 GFileAttributeValue *value;
4184 attributes = g_file_info_list_attributes (info, NULL);
4186 for (i = 0; attributes[i] != NULL; i++)
4188 value = _g_file_info_get_attribute_value (info, attributes[i]);
4190 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4193 if (!g_file_set_attribute (file, attributes[i],
4194 value->type, _g_file_attribute_value_peek_as_pointer (value),
4195 flags, cancellable, error))
4197 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4199 /* Don't set error multiple times */
4203 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4206 g_strfreev (attributes);
4212 * g_file_set_attributes_async:
4213 * @file: input #GFile
4214 * @info: a #GFileInfo
4215 * @flags: a #GFileQueryInfoFlags
4216 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4218 * @cancellable: (allow-none): optional #GCancellable object,
4220 * @callback: (scope async): a #GAsyncReadyCallback
4221 * @user_data: (closure): a #gpointer
4223 * Asynchronously sets the attributes of @file with @info.
4225 * For more details, see g_file_set_attributes_from_info(),
4226 * which is the synchronous version of this call.
4228 * When the operation is finished, @callback will be called.
4229 * You can then call g_file_set_attributes_finish() to get
4230 * the result of the operation.
4233 g_file_set_attributes_async (GFile *file,
4235 GFileQueryInfoFlags flags,
4237 GCancellable *cancellable,
4238 GAsyncReadyCallback callback,
4243 g_return_if_fail (G_IS_FILE (file));
4244 g_return_if_fail (G_IS_FILE_INFO (info));
4246 iface = G_FILE_GET_IFACE (file);
4247 (* iface->set_attributes_async) (file,
4257 * g_file_set_attributes_finish:
4258 * @file: input #GFile
4259 * @result: a #GAsyncResult
4260 * @info: (out) (transfer full): a #GFileInfo
4261 * @error: a #GError, or %NULL
4263 * Finishes setting an attribute started in g_file_set_attributes_async().
4265 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4268 g_file_set_attributes_finish (GFile *file,
4269 GAsyncResult *result,
4275 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4276 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4278 /* No standard handling of errors here, as we must set info even
4281 iface = G_FILE_GET_IFACE (file);
4282 return (* iface->set_attributes_finish) (file, result, info, error);
4286 * g_file_set_attribute_string:
4287 * @file: input #GFile
4288 * @attribute: a string containing the attribute's name
4289 * @value: a string containing the attribute's value
4290 * @flags: #GFileQueryInfoFlags
4291 * @cancellable: (allow-none): optional #GCancellable object,
4293 * @error: a #GError, or %NULL
4295 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4296 * If @attribute is of a different type, this operation will fail.
4298 * If @cancellable is not %NULL, then the operation can be cancelled by
4299 * triggering the cancellable object from another thread. If the operation
4300 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4302 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4305 g_file_set_attribute_string (GFile *file,
4306 const char *attribute,
4308 GFileQueryInfoFlags flags,
4309 GCancellable *cancellable,
4312 return g_file_set_attribute (file, attribute,
4313 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4314 flags, cancellable, error);
4318 * g_file_set_attribute_byte_string:
4319 * @file: input #GFile
4320 * @attribute: a string containing the attribute's name
4321 * @value: a string containing the attribute's new value
4322 * @flags: a #GFileQueryInfoFlags
4323 * @cancellable: (allow-none): optional #GCancellable object,
4325 * @error: a #GError, or %NULL
4327 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4328 * If @attribute is of a different type, this operation will fail,
4331 * If @cancellable is not %NULL, then the operation can be cancelled by
4332 * triggering the cancellable object from another thread. If the operation
4333 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4335 * Returns: %TRUE if the @attribute was successfully set to @value
4336 * in the @file, %FALSE otherwise.
4339 g_file_set_attribute_byte_string (GFile *file,
4340 const gchar *attribute,
4342 GFileQueryInfoFlags flags,
4343 GCancellable *cancellable,
4346 return g_file_set_attribute (file, attribute,
4347 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4348 flags, cancellable, error);
4352 * g_file_set_attribute_uint32:
4353 * @file: input #GFile
4354 * @attribute: a string containing the attribute's name
4355 * @value: a #guint32 containing the attribute's new value
4356 * @flags: a #GFileQueryInfoFlags
4357 * @cancellable: (allow-none): optional #GCancellable object,
4359 * @error: a #GError, or %NULL
4361 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4362 * If @attribute is of a different type, this operation will fail.
4364 * If @cancellable is not %NULL, then the operation can be cancelled by
4365 * triggering the cancellable object from another thread. If the operation
4366 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4368 * Returns: %TRUE if the @attribute was successfully set to @value
4369 * in the @file, %FALSE otherwise.
4372 g_file_set_attribute_uint32 (GFile *file,
4373 const gchar *attribute,
4375 GFileQueryInfoFlags flags,
4376 GCancellable *cancellable,
4379 return g_file_set_attribute (file, attribute,
4380 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4381 flags, cancellable, error);
4385 * g_file_set_attribute_int32:
4386 * @file: input #GFile
4387 * @attribute: a string containing the attribute's name
4388 * @value: a #gint32 containing the attribute's new value
4389 * @flags: a #GFileQueryInfoFlags
4390 * @cancellable: (allow-none): optional #GCancellable object,
4392 * @error: a #GError, or %NULL
4394 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4395 * If @attribute is of a different type, this operation will fail.
4397 * If @cancellable is not %NULL, then the operation can be cancelled by
4398 * triggering the cancellable object from another thread. If the operation
4399 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4401 * Returns: %TRUE if the @attribute was successfully set to @value
4402 * in the @file, %FALSE otherwise.
4405 g_file_set_attribute_int32 (GFile *file,
4406 const gchar *attribute,
4408 GFileQueryInfoFlags flags,
4409 GCancellable *cancellable,
4412 return g_file_set_attribute (file, attribute,
4413 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4414 flags, cancellable, error);
4418 * g_file_set_attribute_uint64:
4419 * @file: input #GFile
4420 * @attribute: a string containing the attribute's name
4421 * @value: a #guint64 containing the attribute's new value
4422 * @flags: a #GFileQueryInfoFlags
4423 * @cancellable: (allow-none): optional #GCancellable object,
4425 * @error: a #GError, or %NULL
4427 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4428 * If @attribute is of a different type, this operation will fail.
4430 * If @cancellable is not %NULL, then the operation can be cancelled by
4431 * triggering the cancellable object from another thread. If the operation
4432 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4434 * Returns: %TRUE if the @attribute was successfully set to @value
4435 * in the @file, %FALSE otherwise.
4438 g_file_set_attribute_uint64 (GFile *file,
4439 const gchar *attribute,
4441 GFileQueryInfoFlags flags,
4442 GCancellable *cancellable,
4445 return g_file_set_attribute (file, attribute,
4446 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4447 flags, cancellable, error);
4451 * g_file_set_attribute_int64:
4452 * @file: input #GFile
4453 * @attribute: a string containing the attribute's name
4454 * @value: a #guint64 containing the attribute's new value
4455 * @flags: a #GFileQueryInfoFlags
4456 * @cancellable: (allow-none): optional #GCancellable object,
4458 * @error: a #GError, or %NULL
4460 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4461 * If @attribute is of a different type, this operation will fail.
4463 * If @cancellable is not %NULL, then the operation can be cancelled by
4464 * triggering the cancellable object from another thread. If the operation
4465 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4467 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4470 g_file_set_attribute_int64 (GFile *file,
4471 const gchar *attribute,
4473 GFileQueryInfoFlags flags,
4474 GCancellable *cancellable,
4477 return g_file_set_attribute (file, attribute,
4478 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4479 flags, cancellable, error);
4483 * g_file_mount_mountable:
4484 * @file: input #GFile
4485 * @flags: flags affecting the operation
4486 * @mount_operation: (allow-none): a #GMountOperation,
4487 * or %NULL to avoid user interaction
4488 * @cancellable: (allow-none): optional #GCancellable object,
4490 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4491 * when the request is satisfied, or %NULL
4492 * @user_data: (closure): the data to pass to callback function
4494 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4495 * Using @mount_operation, you can request callbacks when, for instance,
4496 * passwords are needed during authentication.
4498 * If @cancellable is not %NULL, then the operation can be cancelled by
4499 * triggering the cancellable object from another thread. If the operation
4500 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4502 * When the operation is finished, @callback will be called.
4503 * You can then call g_file_mount_mountable_finish() to get
4504 * the result of the operation.
4507 g_file_mount_mountable (GFile *file,
4508 GMountMountFlags flags,
4509 GMountOperation *mount_operation,
4510 GCancellable *cancellable,
4511 GAsyncReadyCallback callback,
4516 g_return_if_fail (G_IS_FILE (file));
4518 iface = G_FILE_GET_IFACE (file);
4520 if (iface->mount_mountable == NULL)
4522 g_task_report_new_error (file, callback, user_data,
4523 g_file_mount_mountable,
4524 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4525 _("Operation not supported"));
4529 (* iface->mount_mountable) (file,
4538 * g_file_mount_mountable_finish:
4539 * @file: input #GFile
4540 * @result: a #GAsyncResult
4541 * @error: a #GError, or %NULL
4543 * Finishes a mount operation. See g_file_mount_mountable() for details.
4545 * Finish an asynchronous mount operation that was started
4546 * with g_file_mount_mountable().
4548 * Returns: (transfer full): a #GFile or %NULL on error.
4549 * Free the returned object with g_object_unref().
4552 g_file_mount_mountable_finish (GFile *file,
4553 GAsyncResult *result,
4558 g_return_val_if_fail (G_IS_FILE (file), NULL);
4559 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4561 if (g_async_result_legacy_propagate_error (result, error))
4563 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4564 return g_task_propagate_pointer (G_TASK (result), error);
4566 iface = G_FILE_GET_IFACE (file);
4567 return (* iface->mount_mountable_finish) (file, result, error);
4571 * g_file_unmount_mountable:
4572 * @file: input #GFile
4573 * @flags: flags affecting the operation
4574 * @cancellable: (allow-none): optional #GCancellable object,
4576 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4577 * when the request is satisfied, or %NULL
4578 * @user_data: (closure): the data to pass to callback function
4580 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4582 * If @cancellable is not %NULL, then the operation can be cancelled by
4583 * triggering the cancellable object from another thread. If the operation
4584 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4586 * When the operation is finished, @callback will be called.
4587 * You can then call g_file_unmount_mountable_finish() to get
4588 * the result of the operation.
4590 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4593 g_file_unmount_mountable (GFile *file,
4594 GMountUnmountFlags flags,
4595 GCancellable *cancellable,
4596 GAsyncReadyCallback callback,
4601 g_return_if_fail (G_IS_FILE (file));
4603 iface = G_FILE_GET_IFACE (file);
4605 if (iface->unmount_mountable == NULL)
4607 g_task_report_new_error (file, callback, user_data,
4608 g_file_unmount_mountable_with_operation,
4609 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4610 _("Operation not supported"));
4614 (* iface->unmount_mountable) (file,
4622 * g_file_unmount_mountable_finish:
4623 * @file: input #GFile
4624 * @result: a #GAsyncResult
4625 * @error: a #GError, or %NULL
4627 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4629 * Finish an asynchronous unmount operation that was started
4630 * with g_file_unmount_mountable().
4632 * Returns: %TRUE if the operation finished successfully.
4635 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4639 g_file_unmount_mountable_finish (GFile *file,
4640 GAsyncResult *result,
4645 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4646 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4648 if (g_async_result_legacy_propagate_error (result, error))
4650 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4651 return g_task_propagate_boolean (G_TASK (result), error);
4653 iface = G_FILE_GET_IFACE (file);
4654 return (* iface->unmount_mountable_finish) (file, result, error);
4658 * g_file_unmount_mountable_with_operation:
4659 * @file: input #GFile
4660 * @flags: flags affecting the operation
4661 * @mount_operation: (allow-none): a #GMountOperation,
4662 * or %NULL to avoid user interaction
4663 * @cancellable: (allow-none): optional #GCancellable object,
4665 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4666 * when the request is satisfied, or %NULL
4667 * @user_data: (closure): the data to pass to callback function
4669 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4671 * If @cancellable is not %NULL, then the operation can be cancelled by
4672 * triggering the cancellable object from another thread. If the operation
4673 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4675 * When the operation is finished, @callback will be called.
4676 * You can then call g_file_unmount_mountable_finish() to get
4677 * the result of the operation.
4682 g_file_unmount_mountable_with_operation (GFile *file,
4683 GMountUnmountFlags flags,
4684 GMountOperation *mount_operation,
4685 GCancellable *cancellable,
4686 GAsyncReadyCallback callback,
4691 g_return_if_fail (G_IS_FILE (file));
4693 iface = G_FILE_GET_IFACE (file);
4695 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4697 g_task_report_new_error (file, callback, user_data,
4698 g_file_unmount_mountable_with_operation,
4699 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4700 _("Operation not supported"));
4704 if (iface->unmount_mountable_with_operation != NULL)
4705 (* iface->unmount_mountable_with_operation) (file,
4712 (* iface->unmount_mountable) (file,
4720 * g_file_unmount_mountable_with_operation_finish:
4721 * @file: input #GFile
4722 * @result: a #GAsyncResult
4723 * @error: a #GError, or %NULL
4725 * Finishes an unmount operation,
4726 * see g_file_unmount_mountable_with_operation() for details.
4728 * Finish an asynchronous unmount operation that was started
4729 * with g_file_unmount_mountable_with_operation().
4731 * Returns: %TRUE if the operation finished successfully.
4737 g_file_unmount_mountable_with_operation_finish (GFile *file,
4738 GAsyncResult *result,
4743 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4744 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4746 if (g_async_result_legacy_propagate_error (result, error))
4748 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4749 return g_task_propagate_boolean (G_TASK (result), error);
4751 iface = G_FILE_GET_IFACE (file);
4752 if (iface->unmount_mountable_with_operation_finish != NULL)
4753 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4755 return (* iface->unmount_mountable_finish) (file, result, error);
4759 * g_file_eject_mountable:
4760 * @file: input #GFile
4761 * @flags: flags affecting the operation
4762 * @cancellable: (allow-none): optional #GCancellable object,
4764 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4765 * when the request is satisfied, or %NULL
4766 * @user_data: (closure): the data to pass to callback function
4768 * Starts an asynchronous eject on a mountable.
4769 * When this operation has completed, @callback will be called with
4770 * @user_user data, and the operation can be finalized with
4771 * g_file_eject_mountable_finish().
4773 * If @cancellable is not %NULL, then the operation can be cancelled by
4774 * triggering the cancellable object from another thread. If the operation
4775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4777 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4780 g_file_eject_mountable (GFile *file,
4781 GMountUnmountFlags flags,
4782 GCancellable *cancellable,
4783 GAsyncReadyCallback callback,
4788 g_return_if_fail (G_IS_FILE (file));
4790 iface = G_FILE_GET_IFACE (file);
4792 if (iface->eject_mountable == NULL)
4794 g_task_report_new_error (file, callback, user_data,
4795 g_file_eject_mountable_with_operation,
4796 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4797 _("Operation not supported"));
4801 (* iface->eject_mountable) (file,
4809 * g_file_eject_mountable_finish:
4810 * @file: input #GFile
4811 * @result: a #GAsyncResult
4812 * @error: a #GError, or %NULL
4814 * Finishes an asynchronous eject operation started by
4815 * g_file_eject_mountable().
4817 * Returns: %TRUE if the @file was ejected successfully.
4820 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4824 g_file_eject_mountable_finish (GFile *file,
4825 GAsyncResult *result,
4830 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4831 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4833 if (g_async_result_legacy_propagate_error (result, error))
4835 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4836 return g_task_propagate_boolean (G_TASK (result), error);
4838 iface = G_FILE_GET_IFACE (file);
4839 return (* iface->eject_mountable_finish) (file, result, error);
4843 * g_file_eject_mountable_with_operation:
4844 * @file: input #GFile
4845 * @flags: flags affecting the operation
4846 * @mount_operation: (allow-none): a #GMountOperation,
4847 * or %NULL to avoid user interaction
4848 * @cancellable: (allow-none): optional #GCancellable object,
4850 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4851 * when the request is satisfied, or %NULL
4852 * @user_data: (closure): the data to pass to callback function
4854 * Starts an asynchronous eject on a mountable.
4855 * When this operation has completed, @callback will be called with
4856 * @user_user data, and the operation can be finalized with
4857 * g_file_eject_mountable_with_operation_finish().
4859 * If @cancellable is not %NULL, then the operation can be cancelled by
4860 * triggering the cancellable object from another thread. If the operation
4861 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4866 g_file_eject_mountable_with_operation (GFile *file,
4867 GMountUnmountFlags flags,
4868 GMountOperation *mount_operation,
4869 GCancellable *cancellable,
4870 GAsyncReadyCallback callback,
4875 g_return_if_fail (G_IS_FILE (file));
4877 iface = G_FILE_GET_IFACE (file);
4879 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4881 g_task_report_new_error (file, callback, user_data,
4882 g_file_eject_mountable_with_operation,
4883 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4884 _("Operation not supported"));
4888 if (iface->eject_mountable_with_operation != NULL)
4889 (* iface->eject_mountable_with_operation) (file,
4896 (* iface->eject_mountable) (file,
4904 * g_file_eject_mountable_with_operation_finish:
4905 * @file: input #GFile
4906 * @result: a #GAsyncResult
4907 * @error: a #GError, or %NULL
4909 * Finishes an asynchronous eject operation started by
4910 * g_file_eject_mountable_with_operation().
4912 * Returns: %TRUE if the @file was ejected successfully.
4918 g_file_eject_mountable_with_operation_finish (GFile *file,
4919 GAsyncResult *result,
4924 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4925 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4927 if (g_async_result_legacy_propagate_error (result, error))
4929 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4930 return g_task_propagate_boolean (G_TASK (result), error);
4932 iface = G_FILE_GET_IFACE (file);
4933 if (iface->eject_mountable_with_operation_finish != NULL)
4934 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4936 return (* iface->eject_mountable_finish) (file, result, error);
4940 * g_file_monitor_directory:
4941 * @file: input #GFile
4942 * @flags: a set of #GFileMonitorFlags
4943 * @cancellable: (allow-none): optional #GCancellable object,
4945 * @error: a #GError, or %NULL
4947 * Obtains a directory monitor for the given file.
4948 * This may fail if directory monitoring is not supported.
4950 * If @cancellable is not %NULL, then the operation can be cancelled by
4951 * triggering the cancellable object from another thread. If the operation
4952 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4954 * It does not make sense for @flags to contain
4955 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4956 * directories. It is not possible to monitor all the files in a
4957 * directory for changes made via hard links; if you want to do this then
4958 * you must register individual watches with g_file_monitor().
4960 * Virtual: monitor_dir
4961 * Returns: (transfer full): a #GFileMonitor for the given @file,
4962 * or %NULL on error.
4963 * Free the returned object with g_object_unref().
4966 g_file_monitor_directory (GFile *file,
4967 GFileMonitorFlags flags,
4968 GCancellable *cancellable,
4973 g_return_val_if_fail (G_IS_FILE (file), NULL);
4974 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4976 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4979 iface = G_FILE_GET_IFACE (file);
4981 if (iface->monitor_dir == NULL)
4983 g_set_error_literal (error, G_IO_ERROR,
4984 G_IO_ERROR_NOT_SUPPORTED,
4985 _("Operation not supported"));
4989 return (* iface->monitor_dir) (file, flags, cancellable, error);
4993 * g_file_monitor_file:
4994 * @file: input #GFile
4995 * @flags: a set of #GFileMonitorFlags
4996 * @cancellable: (allow-none): optional #GCancellable object,
4998 * @error: a #GError, or %NULL
5000 * Obtains a file monitor for the given file. If no file notification
5001 * mechanism exists, then regular polling of the file is used.
5003 * If @cancellable is not %NULL, then the operation can be cancelled by
5004 * triggering the cancellable object from another thread. If the operation
5005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5007 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5008 * will also attempt to report changes made to the file via another
5009 * filename (ie, a hard link). Without this flag, you can only rely on
5010 * changes made through the filename contained in @file to be
5011 * reported. Using this flag may result in an increase in resource
5012 * usage, and may not have any effect depending on the #GFileMonitor
5013 * backend and/or filesystem type.
5015 * Returns: (transfer full): a #GFileMonitor for the given @file,
5016 * or %NULL on error.
5017 * Free the returned object with g_object_unref().
5020 g_file_monitor_file (GFile *file,
5021 GFileMonitorFlags flags,
5022 GCancellable *cancellable,
5026 GFileMonitor *monitor;
5028 g_return_val_if_fail (G_IS_FILE (file), NULL);
5030 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5033 iface = G_FILE_GET_IFACE (file);
5037 if (iface->monitor_file)
5038 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5040 /* Fallback to polling */
5041 if (monitor == NULL)
5042 monitor = _g_poll_file_monitor_new (file);
5049 * @file: input #GFile
5050 * @flags: a set of #GFileMonitorFlags
5051 * @cancellable: (allow-none): optional #GCancellable object,
5053 * @error: a #GError, or %NULL
5055 * Obtains a file or directory monitor for the given file,
5056 * depending on the type of the file.
5058 * If @cancellable is not %NULL, then the operation can be cancelled by
5059 * triggering the cancellable object from another thread. If the operation
5060 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5062 * Returns: (transfer full): a #GFileMonitor for the given @file,
5063 * or %NULL on error.
5064 * Free the returned object with g_object_unref().
5069 g_file_monitor (GFile *file,
5070 GFileMonitorFlags flags,
5071 GCancellable *cancellable,
5074 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5075 return g_file_monitor_directory (file,
5076 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5077 cancellable, error);
5079 return g_file_monitor_file (file, flags, cancellable, error);
5082 /********************************************
5083 * Default implementation of async ops *
5084 ********************************************/
5088 GFileQueryInfoFlags flags;
5089 } QueryInfoAsyncData;
5092 query_info_data_free (QueryInfoAsyncData *data)
5094 g_free (data->attributes);
5099 query_info_async_thread (GTask *task,
5102 GCancellable *cancellable)
5104 QueryInfoAsyncData *data = task_data;
5106 GError *error = NULL;
5108 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5110 g_task_return_pointer (task, info, g_object_unref);
5112 g_task_return_error (task, error);
5116 g_file_real_query_info_async (GFile *file,
5117 const char *attributes,
5118 GFileQueryInfoFlags flags,
5120 GCancellable *cancellable,
5121 GAsyncReadyCallback callback,
5125 QueryInfoAsyncData *data;
5127 data = g_new0 (QueryInfoAsyncData, 1);
5128 data->attributes = g_strdup (attributes);
5129 data->flags = flags;
5131 task = g_task_new (file, cancellable, callback, user_data);
5132 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5133 g_task_set_priority (task, io_priority);
5134 g_task_run_in_thread (task, query_info_async_thread);
5135 g_object_unref (task);
5139 g_file_real_query_info_finish (GFile *file,
5143 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5145 return g_task_propagate_pointer (G_TASK (res), error);
5149 query_filesystem_info_async_thread (GTask *task,
5152 GCancellable *cancellable)
5154 const char *attributes = task_data;
5156 GError *error = NULL;
5158 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5160 g_task_return_pointer (task, info, g_object_unref);
5162 g_task_return_error (task, error);
5166 g_file_real_query_filesystem_info_async (GFile *file,
5167 const char *attributes,
5169 GCancellable *cancellable,
5170 GAsyncReadyCallback callback,
5175 task = g_task_new (file, cancellable, callback, user_data);
5176 g_task_set_task_data (task, g_strdup (attributes), g_free);
5177 g_task_set_priority (task, io_priority);
5178 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5179 g_object_unref (task);
5183 g_file_real_query_filesystem_info_finish (GFile *file,
5187 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5189 return g_task_propagate_pointer (G_TASK (res), error);
5193 enumerate_children_async_thread (GTask *task,
5196 GCancellable *cancellable)
5198 QueryInfoAsyncData *data = task_data;
5199 GFileEnumerator *enumerator;
5200 GError *error = NULL;
5202 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5204 g_task_return_error (task, error);
5206 g_task_return_pointer (task, enumerator, g_object_unref);
5210 g_file_real_enumerate_children_async (GFile *file,
5211 const char *attributes,
5212 GFileQueryInfoFlags flags,
5214 GCancellable *cancellable,
5215 GAsyncReadyCallback callback,
5219 QueryInfoAsyncData *data;
5221 data = g_new0 (QueryInfoAsyncData, 1);
5222 data->attributes = g_strdup (attributes);
5223 data->flags = flags;
5225 task = g_task_new (file, cancellable, callback, user_data);
5226 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5227 g_task_set_priority (task, io_priority);
5228 g_task_run_in_thread (task, enumerate_children_async_thread);
5229 g_object_unref (task);
5232 static GFileEnumerator *
5233 g_file_real_enumerate_children_finish (GFile *file,
5237 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5239 return g_task_propagate_pointer (G_TASK (res), error);
5243 open_read_async_thread (GTask *task,
5246 GCancellable *cancellable)
5249 GFileInputStream *stream;
5250 GError *error = NULL;
5252 iface = G_FILE_GET_IFACE (object);
5254 if (iface->read_fn == NULL)
5256 g_task_return_new_error (task, G_IO_ERROR,
5257 G_IO_ERROR_NOT_SUPPORTED,
5258 _("Operation not supported"));
5262 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5264 g_task_return_pointer (task, stream, g_object_unref);
5266 g_task_return_error (task, error);
5270 g_file_real_read_async (GFile *file,
5272 GCancellable *cancellable,
5273 GAsyncReadyCallback callback,
5278 task = g_task_new (file, cancellable, callback, user_data);
5279 g_task_set_priority (task, io_priority);
5280 g_task_run_in_thread (task, open_read_async_thread);
5281 g_object_unref (task);
5284 static GFileInputStream *
5285 g_file_real_read_finish (GFile *file,
5289 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5291 return g_task_propagate_pointer (G_TASK (res), error);
5295 append_to_async_thread (GTask *task,
5296 gpointer source_object,
5298 GCancellable *cancellable)
5301 GFileCreateFlags *data = task_data;
5302 GFileOutputStream *stream;
5303 GError *error = NULL;
5305 iface = G_FILE_GET_IFACE (source_object);
5307 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5309 g_task_return_pointer (task, stream, g_object_unref);
5311 g_task_return_error (task, error);
5315 g_file_real_append_to_async (GFile *file,
5316 GFileCreateFlags flags,
5318 GCancellable *cancellable,
5319 GAsyncReadyCallback callback,
5322 GFileCreateFlags *data;
5325 data = g_new0 (GFileCreateFlags, 1);
5328 task = g_task_new (file, cancellable, callback, user_data);
5329 g_task_set_task_data (task, data, g_free);
5330 g_task_set_priority (task, io_priority);
5332 g_task_run_in_thread (task, append_to_async_thread);
5333 g_object_unref (task);
5336 static GFileOutputStream *
5337 g_file_real_append_to_finish (GFile *file,
5341 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5343 return g_task_propagate_pointer (G_TASK (res), error);
5347 create_async_thread (GTask *task,
5348 gpointer source_object,
5350 GCancellable *cancellable)
5353 GFileCreateFlags *data = task_data;
5354 GFileOutputStream *stream;
5355 GError *error = NULL;
5357 iface = G_FILE_GET_IFACE (source_object);
5359 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5361 g_task_return_pointer (task, stream, g_object_unref);
5363 g_task_return_error (task, error);
5367 g_file_real_create_async (GFile *file,
5368 GFileCreateFlags flags,
5370 GCancellable *cancellable,
5371 GAsyncReadyCallback callback,
5374 GFileCreateFlags *data;
5377 data = g_new0 (GFileCreateFlags, 1);
5380 task = g_task_new (file, cancellable, callback, user_data);
5381 g_task_set_task_data (task, data, g_free);
5382 g_task_set_priority (task, io_priority);
5384 g_task_run_in_thread (task, create_async_thread);
5385 g_object_unref (task);
5388 static GFileOutputStream *
5389 g_file_real_create_finish (GFile *file,
5393 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5395 return g_task_propagate_pointer (G_TASK (res), error);
5399 GFileOutputStream *stream;
5401 gboolean make_backup;
5402 GFileCreateFlags flags;
5406 replace_async_data_free (ReplaceAsyncData *data)
5409 g_object_unref (data->stream);
5410 g_free (data->etag);
5415 replace_async_thread (GTask *task,
5416 gpointer source_object,
5418 GCancellable *cancellable)
5421 GFileOutputStream *stream;
5422 ReplaceAsyncData *data = task_data;
5423 GError *error = NULL;
5425 iface = G_FILE_GET_IFACE (source_object);
5427 stream = iface->replace (G_FILE (source_object),
5435 g_task_return_pointer (task, stream, g_object_unref);
5437 g_task_return_error (task, error);
5441 g_file_real_replace_async (GFile *file,
5443 gboolean make_backup,
5444 GFileCreateFlags flags,
5446 GCancellable *cancellable,
5447 GAsyncReadyCallback callback,
5451 ReplaceAsyncData *data;
5453 data = g_new0 (ReplaceAsyncData, 1);
5454 data->etag = g_strdup (etag);
5455 data->make_backup = make_backup;
5456 data->flags = flags;
5458 task = g_task_new (file, cancellable, callback, user_data);
5459 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5460 g_task_set_priority (task, io_priority);
5462 g_task_run_in_thread (task, replace_async_thread);
5463 g_object_unref (task);
5466 static GFileOutputStream *
5467 g_file_real_replace_finish (GFile *file,
5471 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5473 return g_task_propagate_pointer (G_TASK (res), error);
5477 delete_async_thread (GTask *task,
5480 GCancellable *cancellable)
5482 GFile *file = object;
5484 GError *error = NULL;
5486 iface = G_FILE_GET_IFACE (object);
5488 if (iface->delete_file (file,
5491 g_task_return_boolean (task, TRUE);
5493 g_task_return_error (task, error);
5497 g_file_real_delete_async (GFile *file,
5499 GCancellable *cancellable,
5500 GAsyncReadyCallback callback,
5505 task = g_task_new (file, cancellable, callback, user_data);
5506 g_task_set_priority (task, io_priority);
5507 g_task_run_in_thread (task, delete_async_thread);
5508 g_object_unref (task);
5512 g_file_real_delete_finish (GFile *file,
5516 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5518 return g_task_propagate_boolean (G_TASK (res), error);
5522 open_readwrite_async_thread (GTask *task,
5525 GCancellable *cancellable)
5528 GFileIOStream *stream;
5529 GError *error = NULL;
5531 iface = G_FILE_GET_IFACE (object);
5533 if (iface->open_readwrite == NULL)
5535 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5536 _("Operation not supported"));
5540 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5543 g_task_return_error (task, error);
5545 g_task_return_pointer (task, stream, g_object_unref);
5549 g_file_real_open_readwrite_async (GFile *file,
5551 GCancellable *cancellable,
5552 GAsyncReadyCallback callback,
5557 task = g_task_new (file, cancellable, callback, user_data);
5558 g_task_set_priority (task, io_priority);
5560 g_task_run_in_thread (task, open_readwrite_async_thread);
5561 g_object_unref (task);
5564 static GFileIOStream *
5565 g_file_real_open_readwrite_finish (GFile *file,
5569 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5571 return g_task_propagate_pointer (G_TASK (res), error);
5575 create_readwrite_async_thread (GTask *task,
5578 GCancellable *cancellable)
5581 GFileCreateFlags *data = task_data;
5582 GFileIOStream *stream;
5583 GError *error = NULL;
5585 iface = G_FILE_GET_IFACE (object);
5587 if (iface->create_readwrite == NULL)
5589 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5590 _("Operation not supported"));
5594 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5597 g_task_return_error (task, error);
5599 g_task_return_pointer (task, stream, g_object_unref);
5603 g_file_real_create_readwrite_async (GFile *file,
5604 GFileCreateFlags flags,
5606 GCancellable *cancellable,
5607 GAsyncReadyCallback callback,
5610 GFileCreateFlags *data;
5613 data = g_new0 (GFileCreateFlags, 1);
5616 task = g_task_new (file, cancellable, callback, user_data);
5617 g_task_set_task_data (task, data, g_free);
5618 g_task_set_priority (task, io_priority);
5620 g_task_run_in_thread (task, create_readwrite_async_thread);
5621 g_object_unref (task);
5624 static GFileIOStream *
5625 g_file_real_create_readwrite_finish (GFile *file,
5629 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5631 return g_task_propagate_pointer (G_TASK (res), error);
5636 gboolean make_backup;
5637 GFileCreateFlags flags;
5638 } ReplaceRWAsyncData;
5641 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5643 g_free (data->etag);
5648 replace_readwrite_async_thread (GTask *task,
5651 GCancellable *cancellable)
5654 GFileIOStream *stream;
5655 GError *error = NULL;
5656 ReplaceRWAsyncData *data = task_data;
5658 iface = G_FILE_GET_IFACE (object);
5660 stream = iface->replace_readwrite (G_FILE (object),
5668 g_task_return_error (task, error);
5670 g_task_return_pointer (task, stream, g_object_unref);
5674 g_file_real_replace_readwrite_async (GFile *file,
5676 gboolean make_backup,
5677 GFileCreateFlags flags,
5679 GCancellable *cancellable,
5680 GAsyncReadyCallback callback,
5684 ReplaceRWAsyncData *data;
5686 data = g_new0 (ReplaceRWAsyncData, 1);
5687 data->etag = g_strdup (etag);
5688 data->make_backup = make_backup;
5689 data->flags = flags;
5691 task = g_task_new (file, cancellable, callback, user_data);
5692 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5693 g_task_set_priority (task, io_priority);
5695 g_task_run_in_thread (task, replace_readwrite_async_thread);
5696 g_object_unref (task);
5699 static GFileIOStream *
5700 g_file_real_replace_readwrite_finish (GFile *file,
5704 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5706 return g_task_propagate_pointer (G_TASK (res), error);
5710 set_display_name_async_thread (GTask *task,
5713 GCancellable *cancellable)
5715 GError *error = NULL;
5716 char *name = task_data;
5719 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5722 g_task_return_error (task, error);
5724 g_task_return_pointer (task, file, g_object_unref);
5728 g_file_real_set_display_name_async (GFile *file,
5729 const char *display_name,
5731 GCancellable *cancellable,
5732 GAsyncReadyCallback callback,
5737 task = g_task_new (file, cancellable, callback, user_data);
5738 g_task_set_task_data (task, g_strdup (display_name), g_free);
5739 g_task_set_priority (task, io_priority);
5741 g_task_run_in_thread (task, set_display_name_async_thread);
5742 g_object_unref (task);
5746 g_file_real_set_display_name_finish (GFile *file,
5750 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5752 return g_task_propagate_pointer (G_TASK (res), error);
5756 GFileQueryInfoFlags flags;
5763 set_info_data_free (SetInfoAsyncData *data)
5766 g_object_unref (data->info);
5768 g_error_free (data->error);
5773 set_info_async_thread (GTask *task,
5776 GCancellable *cancellable)
5778 SetInfoAsyncData *data = task_data;
5781 data->res = g_file_set_attributes_from_info (G_FILE (object),
5789 g_file_real_set_attributes_async (GFile *file,
5791 GFileQueryInfoFlags flags,
5793 GCancellable *cancellable,
5794 GAsyncReadyCallback callback,
5798 SetInfoAsyncData *data;
5800 data = g_new0 (SetInfoAsyncData, 1);
5801 data->info = g_file_info_dup (info);
5802 data->flags = flags;
5804 task = g_task_new (file, cancellable, callback, user_data);
5805 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5806 g_task_set_priority (task, io_priority);
5808 g_task_run_in_thread (task, set_info_async_thread);
5809 g_object_unref (task);
5813 g_file_real_set_attributes_finish (GFile *file,
5818 SetInfoAsyncData *data;
5820 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5822 data = g_task_get_task_data (G_TASK (res));
5825 *info = g_object_ref (data->info);
5827 if (error != NULL && data->error)
5828 *error = g_error_copy (data->error);
5834 find_enclosing_mount_async_thread (GTask *task,
5837 GCancellable *cancellable)
5839 GError *error = NULL;
5842 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5845 g_task_return_error (task, error);
5847 g_task_return_pointer (task, mount, g_object_unref);
5851 g_file_real_find_enclosing_mount_async (GFile *file,
5853 GCancellable *cancellable,
5854 GAsyncReadyCallback callback,
5859 task = g_task_new (file, cancellable, callback, user_data);
5860 g_task_set_priority (task, io_priority);
5862 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5863 g_object_unref (task);
5867 g_file_real_find_enclosing_mount_finish (GFile *file,
5871 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5873 return g_task_propagate_pointer (G_TASK (res), error);
5880 GFileCopyFlags flags;
5881 GFileProgressCallback progress_cb;
5882 gpointer progress_cb_data;
5886 copy_async_data_free (CopyAsyncData *data)
5888 g_object_unref (data->source);
5889 g_object_unref (data->destination);
5890 g_slice_free (CopyAsyncData, data);
5894 CopyAsyncData *data;
5895 goffset current_num_bytes;
5896 goffset total_num_bytes;
5900 copy_async_progress_in_main (gpointer user_data)
5902 ProgressData *progress = user_data;
5903 CopyAsyncData *data = progress->data;
5905 data->progress_cb (progress->current_num_bytes,
5906 progress->total_num_bytes,
5907 data->progress_cb_data);
5913 copy_async_progress_callback (goffset current_num_bytes,
5914 goffset total_num_bytes,
5917 GTask *task = user_data;
5918 CopyAsyncData *data = g_task_get_task_data (task);
5919 ProgressData *progress;
5921 progress = g_new (ProgressData, 1);
5922 progress->data = data;
5923 progress->current_num_bytes = current_num_bytes;
5924 progress->total_num_bytes = total_num_bytes;
5926 g_main_context_invoke_full (g_task_get_context (task),
5927 g_task_get_priority (task),
5928 copy_async_progress_in_main,
5934 copy_async_thread (GTask *task,
5937 GCancellable *cancellable)
5939 CopyAsyncData *data = task_data;
5941 GError *error = NULL;
5943 result = g_file_copy (data->source,
5947 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5951 g_task_return_boolean (task, TRUE);
5953 g_task_return_error (task, error);
5957 g_file_real_copy_async (GFile *source,
5959 GFileCopyFlags flags,
5961 GCancellable *cancellable,
5962 GFileProgressCallback progress_callback,
5963 gpointer progress_callback_data,
5964 GAsyncReadyCallback callback,
5968 CopyAsyncData *data;
5970 data = g_slice_new (CopyAsyncData);
5971 data->source = g_object_ref (source);
5972 data->destination = g_object_ref (destination);
5973 data->flags = flags;
5974 data->progress_cb = progress_callback;
5975 data->progress_cb_data = progress_callback_data;
5977 task = g_task_new (source, cancellable, callback, user_data);
5978 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5979 g_task_set_priority (task, io_priority);
5980 g_task_run_in_thread (task, copy_async_thread);
5981 g_object_unref (task);
5985 g_file_real_copy_finish (GFile *file,
5989 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5991 return g_task_propagate_boolean (G_TASK (res), error);
5995 /********************************************
5996 * Default VFS operations *
5997 ********************************************/
6000 * g_file_new_for_path:
6001 * @path: a string containing a relative or absolute path.
6002 * The string must be encoded in the glib filename encoding.
6004 * Constructs a #GFile for a given path. This operation never
6005 * fails, but the returned object might not support any I/O
6006 * operation if @path is malformed.
6008 * Returns: (transfer full): a new #GFile for the given @path.
6009 * Free the returned object with g_object_unref().
6012 g_file_new_for_path (const char *path)
6014 g_return_val_if_fail (path != NULL, NULL);
6016 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6020 * g_file_new_for_uri:
6021 * @uri: a UTF-8 string containing a URI
6023 * Constructs a #GFile for a given URI. This operation never
6024 * fails, but the returned object might not support any I/O
6025 * operation if @uri is malformed or if the uri type is
6028 * Returns: (transfer full): a new #GFile for the given @uri.
6029 * Free the returned object with g_object_unref().
6032 g_file_new_for_uri (const char *uri)
6034 g_return_val_if_fail (uri != NULL, NULL);
6036 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6041 * @tmpl: (type filename) (allow-none): Template for the file
6042 * name, as in g_file_open_tmp(), or %NULL for a default template
6043 * @iostream: (out): on return, a #GFileIOStream for the created file
6044 * @error: a #GError, or %NULL
6046 * Opens a file in the preferred directory for temporary files (as
6047 * returned by g_get_tmp_dir()) and returns a #GFile and
6048 * #GFileIOStream pointing to it.
6050 * @tmpl should be a string in the GLib file name encoding
6051 * containing a sequence of six 'X' characters, and containing no
6052 * directory components. If it is %NULL, a default template is used.
6054 * Unlike the other #GFile constructors, this will return %NULL if
6055 * a temporary file could not be created.
6057 * Returns: (transfer full): a new #GFile.
6058 * Free the returned object with g_object_unref().
6063 g_file_new_tmp (const char *tmpl,
6064 GFileIOStream **iostream,
6070 GFileOutputStream *output;
6072 g_return_val_if_fail (iostream != NULL, NULL);
6074 fd = g_file_open_tmp (tmpl, &path, error);
6078 file = g_file_new_for_path (path);
6080 output = _g_local_file_output_stream_new (fd);
6081 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6083 g_object_unref (output);
6090 * g_file_parse_name:
6091 * @parse_name: a file name or path to be parsed
6093 * Constructs a #GFile with the given @parse_name (i.e. something
6094 * given by g_file_get_parse_name()). This operation never fails,
6095 * but the returned object might not support any I/O operation if
6096 * the @parse_name cannot be parsed.
6098 * Returns: (transfer full): a new #GFile.
6101 g_file_parse_name (const char *parse_name)
6103 g_return_val_if_fail (parse_name != NULL, NULL);
6105 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6109 is_valid_scheme_character (char c)
6111 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6114 /* Following RFC 2396, valid schemes are built like:
6115 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6118 has_valid_scheme (const char *uri)
6124 if (!g_ascii_isalpha (*p))
6129 } while (is_valid_scheme_character (*p));
6135 new_for_cmdline_arg (const gchar *arg,
6141 if (g_path_is_absolute (arg))
6142 return g_file_new_for_path (arg);
6144 if (has_valid_scheme (arg))
6145 return g_file_new_for_uri (arg);
6151 current_dir = g_get_current_dir ();
6152 filename = g_build_filename (current_dir, arg, NULL);
6153 g_free (current_dir);
6156 filename = g_build_filename (cwd, arg, NULL);
6158 file = g_file_new_for_path (filename);
6165 * g_file_new_for_commandline_arg:
6166 * @arg: a command line string
6168 * Creates a #GFile with the given argument from the command line.
6169 * The value of @arg can be either a URI, an absolute path or a
6170 * relative path resolved relative to the current working directory.
6171 * This operation never fails, but the returned object might not
6172 * support any I/O operation if @arg points to a malformed path.
6174 * Returns: (transfer full): a new #GFile.
6175 * Free the returned object with g_object_unref().
6178 g_file_new_for_commandline_arg (const char *arg)
6180 g_return_val_if_fail (arg != NULL, NULL);
6182 return new_for_cmdline_arg (arg, NULL);
6186 * g_file_new_for_commandline_arg_and_cwd:
6187 * @arg: a command line string
6188 * @cwd: the current working directory of the commandline
6190 * Creates a #GFile with the given argument from the command line.
6192 * This function is similar to g_file_new_for_commandline_arg() except
6193 * that it allows for passing the current working directory as an
6194 * argument instead of using the current working directory of the
6197 * This is useful if the commandline argument was given in a context
6198 * other than the invocation of the current process.
6200 * See also g_application_command_line_create_file_for_arg().
6202 * Returns: (transfer full): a new #GFile
6207 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6210 g_return_val_if_fail (arg != NULL, NULL);
6211 g_return_val_if_fail (cwd != NULL, NULL);
6213 return new_for_cmdline_arg (arg, cwd);
6217 * g_file_mount_enclosing_volume:
6218 * @location: input #GFile
6219 * @flags: flags affecting the operation
6220 * @mount_operation: (allow-none): a #GMountOperation
6221 * or %NULL to avoid user interaction
6222 * @cancellable: (allow-none): optional #GCancellable object,
6224 * @callback: (allow-none): a #GAsyncReadyCallback to call
6225 * when the request is satisfied, or %NULL
6226 * @user_data: the data to pass to callback function
6228 * Starts a @mount_operation, mounting the volume that contains
6229 * the file @location.
6231 * When this operation has completed, @callback will be called with
6232 * @user_user data, and the operation can be finalized with
6233 * g_file_mount_enclosing_volume_finish().
6235 * If @cancellable is not %NULL, then the operation can be cancelled by
6236 * triggering the cancellable object from another thread. If the operation
6237 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6240 g_file_mount_enclosing_volume (GFile *location,
6241 GMountMountFlags flags,
6242 GMountOperation *mount_operation,
6243 GCancellable *cancellable,
6244 GAsyncReadyCallback callback,
6249 g_return_if_fail (G_IS_FILE (location));
6251 iface = G_FILE_GET_IFACE (location);
6253 if (iface->mount_enclosing_volume == NULL)
6255 g_task_report_new_error (location, callback, user_data,
6256 g_file_mount_enclosing_volume,
6257 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6258 _("volume doesn't implement mount"));
6262 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6267 * g_file_mount_enclosing_volume_finish:
6268 * @location: input #GFile
6269 * @result: a #GAsyncResult
6270 * @error: a #GError, or %NULL
6272 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6274 * Returns: %TRUE if successful. If an error has occurred,
6275 * this function will return %FALSE and set @error
6276 * appropriately if present.
6279 g_file_mount_enclosing_volume_finish (GFile *location,
6280 GAsyncResult *result,
6285 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6286 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6288 if (g_async_result_legacy_propagate_error (result, error))
6290 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6291 return g_task_propagate_boolean (G_TASK (result), error);
6293 iface = G_FILE_GET_IFACE (location);
6295 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6298 /********************************************
6299 * Utility functions *
6300 ********************************************/
6303 * g_file_query_default_handler:
6304 * @file: a #GFile to open
6305 * @cancellable: optional #GCancellable object, %NULL to ignore
6306 * @error: a #GError, or %NULL
6308 * Returns the #GAppInfo that is registered as the default
6309 * application to handle the file specified by @file.
6311 * If @cancellable is not %NULL, then the operation can be cancelled by
6312 * triggering the cancellable object from another thread. If the operation
6313 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6315 * Returns: (transfer full): a #GAppInfo if the handle was found,
6316 * %NULL if there were errors.
6317 * When you are done with it, release it with g_object_unref()
6320 g_file_query_default_handler (GFile *file,
6321 GCancellable *cancellable,
6325 const char *content_type;
6330 uri_scheme = g_file_get_uri_scheme (file);
6331 if (uri_scheme && uri_scheme[0] != '\0')
6333 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6334 g_free (uri_scheme);
6336 if (appinfo != NULL)
6340 info = g_file_query_info (file,
6341 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6350 content_type = g_file_info_get_content_type (info);
6353 /* Don't use is_native(), as we want to support fuse paths if available */
6354 path = g_file_get_path (file);
6355 appinfo = g_app_info_get_default_for_type (content_type,
6360 g_object_unref (info);
6362 if (appinfo != NULL)
6365 g_set_error_literal (error, G_IO_ERROR,
6366 G_IO_ERROR_NOT_SUPPORTED,
6367 _("No application is registered as handling this file"));
6371 #define GET_CONTENT_BLOCK_SIZE 8192
6374 * g_file_load_contents:
6375 * @file: input #GFile
6376 * @cancellable: optional #GCancellable object, %NULL to ignore
6377 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6378 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6379 * or %NULL if the length is not needed
6380 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6381 * or %NULL if the entity tag is not needed
6382 * @error: a #GError, or %NULL
6384 * Loads the content of the file into memory. The data is always
6385 * zero-terminated, but this is not included in the resultant @length.
6386 * The returned @content should be freed with g_free() when no longer
6389 * If @cancellable is not %NULL, then the operation can be cancelled by
6390 * triggering the cancellable object from another thread. If the operation
6391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6393 * Returns: %TRUE if the @file's contents were successfully loaded.
6394 * %FALSE if there were errors.
6397 g_file_load_contents (GFile *file,
6398 GCancellable *cancellable,
6404 GFileInputStream *in;
6405 GByteArray *content;
6410 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6411 g_return_val_if_fail (contents != NULL, FALSE);
6413 in = g_file_read (file, cancellable, error);
6417 content = g_byte_array_new ();
6420 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6421 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6422 content->data + pos,
6423 GET_CONTENT_BLOCK_SIZE,
6424 cancellable, error)) > 0)
6427 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6434 info = g_file_input_stream_query_info (in,
6435 G_FILE_ATTRIBUTE_ETAG_VALUE,
6440 *etag_out = g_strdup (g_file_info_get_etag (info));
6441 g_object_unref (info);
6445 /* Ignore errors on close */
6446 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6447 g_object_unref (in);
6451 /* error is set already */
6452 g_byte_array_free (content, TRUE);
6459 /* Zero terminate (we got an extra byte allocated for this */
6460 content->data[pos] = 0;
6462 *contents = (char *)g_byte_array_free (content, FALSE);
6469 GFileReadMoreCallback read_more_callback;
6470 GByteArray *content;
6477 load_contents_data_free (LoadContentsData *data)
6480 g_byte_array_free (data->content, TRUE);
6481 g_free (data->etag);
6486 load_contents_close_callback (GObject *obj,
6487 GAsyncResult *close_res,
6490 GInputStream *stream = G_INPUT_STREAM (obj);
6491 LoadContentsData *data = user_data;
6493 /* Ignore errors here, we're only reading anyway */
6494 g_input_stream_close_finish (stream, close_res, NULL);
6495 g_object_unref (stream);
6497 g_task_return_boolean (data->task, TRUE);
6498 g_object_unref (data->task);
6502 load_contents_fstat_callback (GObject *obj,
6503 GAsyncResult *stat_res,
6506 GInputStream *stream = G_INPUT_STREAM (obj);
6507 LoadContentsData *data = user_data;
6510 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6514 data->etag = g_strdup (g_file_info_get_etag (info));
6515 g_object_unref (info);
6518 g_input_stream_close_async (stream, 0,
6519 g_task_get_cancellable (data->task),
6520 load_contents_close_callback, data);
6524 load_contents_read_callback (GObject *obj,
6525 GAsyncResult *read_res,
6528 GInputStream *stream = G_INPUT_STREAM (obj);
6529 LoadContentsData *data = user_data;
6530 GError *error = NULL;
6533 read_size = g_input_stream_read_finish (stream, read_res, &error);
6537 g_task_return_error (data->task, error);
6538 g_object_unref (data->task);
6540 /* Close the file ignoring any error */
6541 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6542 g_object_unref (stream);
6544 else if (read_size == 0)
6546 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6547 G_FILE_ATTRIBUTE_ETAG_VALUE,
6549 g_task_get_cancellable (data->task),
6550 load_contents_fstat_callback,
6553 else if (read_size > 0)
6555 data->pos += read_size;
6557 g_byte_array_set_size (data->content,
6558 data->pos + GET_CONTENT_BLOCK_SIZE);
6561 if (data->read_more_callback &&
6562 !data->read_more_callback ((char *)data->content->data, data->pos,
6563 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6564 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6565 G_FILE_ATTRIBUTE_ETAG_VALUE,
6567 g_task_get_cancellable (data->task),
6568 load_contents_fstat_callback,
6571 g_input_stream_read_async (stream,
6572 data->content->data + data->pos,
6573 GET_CONTENT_BLOCK_SIZE,
6575 g_task_get_cancellable (data->task),
6576 load_contents_read_callback,
6582 load_contents_open_callback (GObject *obj,
6583 GAsyncResult *open_res,
6586 GFile *file = G_FILE (obj);
6587 GFileInputStream *stream;
6588 LoadContentsData *data = user_data;
6589 GError *error = NULL;
6591 stream = g_file_read_finish (file, open_res, &error);
6595 g_byte_array_set_size (data->content,
6596 data->pos + GET_CONTENT_BLOCK_SIZE);
6597 g_input_stream_read_async (G_INPUT_STREAM (stream),
6598 data->content->data + data->pos,
6599 GET_CONTENT_BLOCK_SIZE,
6601 g_task_get_cancellable (data->task),
6602 load_contents_read_callback,
6607 g_task_return_error (data->task, error);
6608 g_object_unref (data->task);
6613 * g_file_load_partial_contents_async: (skip)
6614 * @file: input #GFile
6615 * @cancellable: optional #GCancellable object, %NULL to ignore
6616 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6617 * and to specify whether further data should be read
6618 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6619 * @user_data: the data to pass to the callback functions
6621 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6622 * be used to stop reading from the file when appropriate, else this
6623 * function will behave exactly as g_file_load_contents_async(). This
6624 * operation can be finished by g_file_load_partial_contents_finish().
6626 * Users of this function should be aware that @user_data is passed to
6627 * both the @read_more_callback and the @callback.
6629 * If @cancellable is not %NULL, then the operation can be cancelled by
6630 * triggering the cancellable object from another thread. If the operation
6631 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6634 g_file_load_partial_contents_async (GFile *file,
6635 GCancellable *cancellable,
6636 GFileReadMoreCallback read_more_callback,
6637 GAsyncReadyCallback callback,
6640 LoadContentsData *data;
6642 g_return_if_fail (G_IS_FILE (file));
6644 data = g_new0 (LoadContentsData, 1);
6645 data->read_more_callback = read_more_callback;
6646 data->content = g_byte_array_new ();
6648 data->task = g_task_new (file, cancellable, callback, user_data);
6649 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6651 g_file_read_async (file,
6653 g_task_get_cancellable (data->task),
6654 load_contents_open_callback,
6659 * g_file_load_partial_contents_finish:
6660 * @file: input #GFile
6661 * @res: a #GAsyncResult
6662 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6663 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6664 * or %NULL if the length is not needed
6665 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6666 * or %NULL if the entity tag is not needed
6667 * @error: a #GError, or %NULL
6669 * Finishes an asynchronous partial load operation that was started
6670 * with g_file_load_partial_contents_async(). The data is always
6671 * zero-terminated, but this is not included in the resultant @length.
6672 * The returned @content should be freed with g_free() when no longer
6675 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6676 * present, it will be set appropriately.
6679 g_file_load_partial_contents_finish (GFile *file,
6687 LoadContentsData *data;
6689 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6690 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6691 g_return_val_if_fail (contents != NULL, FALSE);
6693 task = G_TASK (res);
6695 if (!g_task_propagate_boolean (task, error))
6702 data = g_task_get_task_data (task);
6705 *length = data->pos;
6709 *etag_out = data->etag;
6713 /* Zero terminate */
6714 g_byte_array_set_size (data->content, data->pos + 1);
6715 data->content->data[data->pos] = 0;
6717 *contents = (char *)g_byte_array_free (data->content, FALSE);
6718 data->content = NULL;
6724 * g_file_load_contents_async:
6725 * @file: input #GFile
6726 * @cancellable: optional #GCancellable object, %NULL to ignore
6727 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6728 * @user_data: the data to pass to callback function
6730 * Starts an asynchronous load of the @file's contents.
6732 * For more details, see g_file_load_contents() which is
6733 * the synchronous version of this call.
6735 * When the load operation has completed, @callback will be called
6736 * with @user data. To finish the operation, call
6737 * g_file_load_contents_finish() with the #GAsyncResult returned by
6740 * If @cancellable is not %NULL, then the operation can be cancelled by
6741 * triggering the cancellable object from another thread. If the operation
6742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6745 g_file_load_contents_async (GFile *file,
6746 GCancellable *cancellable,
6747 GAsyncReadyCallback callback,
6750 g_file_load_partial_contents_async (file,
6753 callback, user_data);
6757 * g_file_load_contents_finish:
6758 * @file: input #GFile
6759 * @res: a #GAsyncResult
6760 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6761 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6762 * or %NULL if the length is not needed
6763 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6764 * or %NULL if the entity tag is not needed
6765 * @error: a #GError, or %NULL
6767 * Finishes an asynchronous load of the @file's contents.
6768 * The contents are placed in @contents, and @length is set to the
6769 * size of the @contents string. The @content should be freed with
6770 * g_free() when no longer needed. If @etag_out is present, it will be
6771 * set to the new entity tag for the @file.
6773 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6774 * present, it will be set appropriately.
6777 g_file_load_contents_finish (GFile *file,
6784 return g_file_load_partial_contents_finish (file,
6793 * g_file_replace_contents:
6794 * @file: input #GFile
6795 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6796 * @length: the length of @contents in bytes
6797 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6798 * for the document, or %NULL
6799 * @make_backup: %TRUE if a backup should be created
6800 * @flags: a set of #GFileCreateFlags
6801 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6802 * for the document. This should be freed with g_free() when no longer
6804 * @cancellable: optional #GCancellable object, %NULL to ignore
6805 * @error: a #GError, or %NULL
6807 * Replaces the contents of @file with @contents of @length bytes.
6809 * If @etag is specified (not %NULL), any existing file must have that etag,
6810 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6812 * If @make_backup is %TRUE, this function will attempt to make a backup
6815 * If @cancellable is not %NULL, then the operation can be cancelled by
6816 * triggering the cancellable object from another thread. If the operation
6817 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6819 * The returned @new_etag can be used to verify that the file hasn't
6820 * changed the next time it is saved over.
6822 * Returns: %TRUE if successful. If an error has occurred, this function
6823 * will return %FALSE and set @error appropriately if present.
6826 g_file_replace_contents (GFile *file,
6827 const char *contents,
6830 gboolean make_backup,
6831 GFileCreateFlags flags,
6833 GCancellable *cancellable,
6836 GFileOutputStream *out;
6837 gsize pos, remainder;
6841 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6842 g_return_val_if_fail (contents != NULL, FALSE);
6844 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6850 while (remainder > 0 &&
6851 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6853 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6861 if (remainder > 0 && res < 0)
6863 /* Ignore errors on close */
6864 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6865 g_object_unref (out);
6867 /* error is set already */
6871 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6874 *new_etag = g_file_output_stream_get_etag (out);
6876 g_object_unref (out);
6883 const char *content;
6888 } ReplaceContentsData;
6891 replace_contents_data_free (ReplaceContentsData *data)
6893 g_free (data->etag);
6898 replace_contents_close_callback (GObject *obj,
6899 GAsyncResult *close_res,
6902 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6903 ReplaceContentsData *data = user_data;
6905 /* Ignore errors here, we're only reading anyway */
6906 g_output_stream_close_finish (stream, close_res, NULL);
6907 g_object_unref (stream);
6911 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6912 g_task_return_boolean (data->task, TRUE);
6914 g_object_unref (data->task);
6918 replace_contents_write_callback (GObject *obj,
6919 GAsyncResult *read_res,
6922 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6923 ReplaceContentsData *data = user_data;
6924 GError *error = NULL;
6927 write_size = g_output_stream_write_finish (stream, read_res, &error);
6929 if (write_size <= 0)
6931 /* Error or EOF, close the file */
6934 data->failed = TRUE;
6935 g_task_return_error (data->task, error);
6937 g_output_stream_close_async (stream, 0,
6938 g_task_get_cancellable (data->task),
6939 replace_contents_close_callback, data);
6941 else if (write_size > 0)
6943 data->pos += write_size;
6945 if (data->pos >= data->length)
6946 g_output_stream_close_async (stream, 0,
6947 g_task_get_cancellable (data->task),
6948 replace_contents_close_callback, data);
6950 g_output_stream_write_async (stream,
6951 data->content + data->pos,
6952 data->length - data->pos,
6954 g_task_get_cancellable (data->task),
6955 replace_contents_write_callback,
6961 replace_contents_open_callback (GObject *obj,
6962 GAsyncResult *open_res,
6965 GFile *file = G_FILE (obj);
6966 GFileOutputStream *stream;
6967 ReplaceContentsData *data = user_data;
6968 GError *error = NULL;
6970 stream = g_file_replace_finish (file, open_res, &error);
6974 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6975 data->content + data->pos,
6976 data->length - data->pos,
6978 g_task_get_cancellable (data->task),
6979 replace_contents_write_callback,
6984 g_task_return_error (data->task, error);
6985 g_object_unref (data->task);
6990 * g_file_replace_contents_async:
6991 * @file: input #GFile
6992 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6993 * @length: the length of @contents in bytes
6994 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6995 * @make_backup: %TRUE if a backup should be created
6996 * @flags: a set of #GFileCreateFlags
6997 * @cancellable: optional #GCancellable object, %NULL to ignore
6998 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6999 * @user_data: the data to pass to callback function
7001 * Starts an asynchronous replacement of @file with the given
7002 * @contents of @length bytes. @etag will replace the document's
7003 * current entity tag.
7005 * When this operation has completed, @callback will be called with
7006 * @user_user data, and the operation can be finalized with
7007 * g_file_replace_contents_finish().
7009 * If @cancellable is not %NULL, then the operation can be cancelled by
7010 * triggering the cancellable object from another thread. If the operation
7011 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7013 * If @make_backup is %TRUE, this function will attempt to
7014 * make a backup of @file.
7017 g_file_replace_contents_async (GFile *file,
7018 const char *contents,
7021 gboolean make_backup,
7022 GFileCreateFlags flags,
7023 GCancellable *cancellable,
7024 GAsyncReadyCallback callback,
7027 ReplaceContentsData *data;
7029 g_return_if_fail (G_IS_FILE (file));
7030 g_return_if_fail (contents != NULL);
7032 data = g_new0 (ReplaceContentsData, 1);
7034 data->content = contents;
7035 data->length = length;
7037 data->task = g_task_new (file, cancellable, callback, user_data);
7038 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7040 g_file_replace_async (file,
7045 g_task_get_cancellable (data->task),
7046 replace_contents_open_callback,
7051 * g_file_replace_contents_finish:
7052 * @file: input #GFile
7053 * @res: a #GAsyncResult
7054 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7055 * for the document. This should be freed with g_free() when it is no
7056 * longer needed, or %NULL
7057 * @error: a #GError, or %NULL
7059 * Finishes an asynchronous replace of the given @file. See
7060 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7061 * tag for the document, if present.
7063 * Returns: %TRUE on success, %FALSE on failure.
7066 g_file_replace_contents_finish (GFile *file,
7072 ReplaceContentsData *data;
7074 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7075 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7077 task = G_TASK (res);
7079 if (!g_task_propagate_boolean (task, error))
7082 data = g_task_get_task_data (task);
7086 *new_etag = data->etag;
7087 data->etag = NULL; /* Take ownership */
7094 * g_file_start_mountable:
7095 * @file: input #GFile
7096 * @flags: flags affecting the operation
7097 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7098 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7099 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7100 * @user_data: the data to pass to callback function
7102 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7103 * Using @start_operation, you can request callbacks when, for instance,
7104 * passwords are needed during authentication.
7106 * If @cancellable is not %NULL, then the operation can be cancelled by
7107 * triggering the cancellable object from another thread. If the operation
7108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7110 * When the operation is finished, @callback will be called.
7111 * You can then call g_file_mount_mountable_finish() to get
7112 * the result of the operation.
7117 g_file_start_mountable (GFile *file,
7118 GDriveStartFlags flags,
7119 GMountOperation *start_operation,
7120 GCancellable *cancellable,
7121 GAsyncReadyCallback callback,
7126 g_return_if_fail (G_IS_FILE (file));
7128 iface = G_FILE_GET_IFACE (file);
7130 if (iface->start_mountable == NULL)
7132 g_task_report_new_error (file, callback, user_data,
7133 g_file_start_mountable,
7134 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7135 _("Operation not supported"));
7139 (* iface->start_mountable) (file,
7148 * g_file_start_mountable_finish:
7149 * @file: input #GFile
7150 * @result: a #GAsyncResult
7151 * @error: a #GError, or %NULL
7153 * Finishes a start operation. See g_file_start_mountable() for details.
7155 * Finish an asynchronous start operation that was started
7156 * with g_file_start_mountable().
7158 * Returns: %TRUE if the operation finished successfully. %FALSE
7164 g_file_start_mountable_finish (GFile *file,
7165 GAsyncResult *result,
7170 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7171 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7173 if (g_async_result_legacy_propagate_error (result, error))
7175 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7176 return g_task_propagate_boolean (G_TASK (result), error);
7178 iface = G_FILE_GET_IFACE (file);
7179 return (* iface->start_mountable_finish) (file, result, error);
7183 * g_file_stop_mountable:
7184 * @file: input #GFile
7185 * @flags: flags affecting the operation
7186 * @mount_operation: (allow-none): a #GMountOperation,
7187 * or %NULL to avoid user interaction.
7188 * @cancellable: (allow-none): optional #GCancellable object,
7190 * @callback: (allow-none): a #GAsyncReadyCallback to call
7191 * when the request is satisfied, or %NULL
7192 * @user_data: the data to pass to callback function
7194 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7196 * If @cancellable is not %NULL, then the operation can be cancelled by
7197 * triggering the cancellable object from another thread. If the operation
7198 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7200 * When the operation is finished, @callback will be called.
7201 * You can then call g_file_stop_mountable_finish() to get
7202 * the result of the operation.
7207 g_file_stop_mountable (GFile *file,
7208 GMountUnmountFlags flags,
7209 GMountOperation *mount_operation,
7210 GCancellable *cancellable,
7211 GAsyncReadyCallback callback,
7216 g_return_if_fail (G_IS_FILE (file));
7218 iface = G_FILE_GET_IFACE (file);
7220 if (iface->stop_mountable == NULL)
7222 g_task_report_new_error (file, callback, user_data,
7223 g_file_stop_mountable,
7224 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7225 _("Operation not supported"));
7229 (* iface->stop_mountable) (file,
7238 * g_file_stop_mountable_finish:
7239 * @file: input #GFile
7240 * @result: a #GAsyncResult
7241 * @error: a #GError, or %NULL
7243 * Finishes an stop operation, see g_file_stop_mountable() for details.
7245 * Finish an asynchronous stop operation that was started
7246 * with g_file_stop_mountable().
7248 * Returns: %TRUE if the operation finished successfully.
7254 g_file_stop_mountable_finish (GFile *file,
7255 GAsyncResult *result,
7260 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7261 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7263 if (g_async_result_legacy_propagate_error (result, error))
7265 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7266 return g_task_propagate_boolean (G_TASK (result), error);
7268 iface = G_FILE_GET_IFACE (file);
7269 return (* iface->stop_mountable_finish) (file, result, error);
7273 * g_file_poll_mountable:
7274 * @file: input #GFile
7275 * @cancellable: optional #GCancellable object, %NULL to ignore
7276 * @callback: (allow-none): a #GAsyncReadyCallback to call
7277 * when the request is satisfied, or %NULL
7278 * @user_data: the data to pass to callback function
7280 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7282 * If @cancellable is not %NULL, then the operation can be cancelled by
7283 * triggering the cancellable object from another thread. If the operation
7284 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7286 * When the operation is finished, @callback will be called.
7287 * You can then call g_file_mount_mountable_finish() to get
7288 * the result of the operation.
7293 g_file_poll_mountable (GFile *file,
7294 GCancellable *cancellable,
7295 GAsyncReadyCallback callback,
7300 g_return_if_fail (G_IS_FILE (file));
7302 iface = G_FILE_GET_IFACE (file);
7304 if (iface->poll_mountable == NULL)
7306 g_task_report_new_error (file, callback, user_data,
7307 g_file_poll_mountable,
7308 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7309 _("Operation not supported"));
7313 (* iface->poll_mountable) (file,
7320 * g_file_poll_mountable_finish:
7321 * @file: input #GFile
7322 * @result: a #GAsyncResult
7323 * @error: a #GError, or %NULL
7325 * Finishes a poll operation. See g_file_poll_mountable() for details.
7327 * Finish an asynchronous poll operation that was polled
7328 * with g_file_poll_mountable().
7330 * Returns: %TRUE if the operation finished successfully. %FALSE
7336 g_file_poll_mountable_finish (GFile *file,
7337 GAsyncResult *result,
7342 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7343 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7345 if (g_async_result_legacy_propagate_error (result, error))
7347 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7348 return g_task_propagate_boolean (G_TASK (result), error);
7350 iface = G_FILE_GET_IFACE (file);
7351 return (* iface->poll_mountable_finish) (file, result, error);
7355 * g_file_supports_thread_contexts:
7358 * Checks if @file supports <link
7359 * linkend="g-main-context-push-thread-default-context">thread-default
7360 * contexts</link>. If this returns %FALSE, you cannot perform
7361 * asynchronous operations on @file in a thread that has a
7362 * thread-default context.
7364 * Returns: Whether or not @file supports thread-default contexts.
7369 g_file_supports_thread_contexts (GFile *file)
7373 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7375 iface = G_FILE_GET_IFACE (file);
7376 return iface->supports_thread_contexts;