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>
50 #include "glib-unix.h"
54 #include "gfileattribute-priv.h"
55 #include "gfiledescriptorbased.h"
56 #include "gpollfilemonitor.h"
58 #include "gfileinputstream.h"
59 #include "gfileoutputstream.h"
60 #include "glocalfileoutputstream.h"
61 #include "glocalfileiostream.h"
62 #include "gcancellable.h"
63 #include "gasyncresult.h"
70 * @short_description: File and Directory Handling
72 * @see_also: #GFileInfo, #GFileEnumerator
74 * #GFile is a high level abstraction for manipulating files on a
75 * virtual file system. #GFiles are lightweight, immutable objects
76 * that do no I/O upon creation. It is necessary to understand that
77 * #GFile objects do not represent files, merely an identifier for a
78 * file. All file content I/O is implemented as streaming operations
79 * (see #GInputStream and #GOutputStream).
81 * To construct a #GFile, you can use:
83 * <member>g_file_new_for_path() if you have a path.</member>
84 * <member>g_file_new_for_uri() if you have a URI.</member>
85 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
86 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
87 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
90 * One way to think of a #GFile is as an abstraction of a pathname. For
91 * normal files the system pathname is what is stored internally, but as
92 * #GFiles are extensible it could also be something else that corresponds
93 * to a pathname in a userspace implementation of a filesystem.
95 * #GFiles make up hierarchies of directories and files that correspond to
96 * the files on a filesystem. You can move through the file system with
97 * #GFile using g_file_get_parent() to get an identifier for the parent
98 * directory, g_file_get_child() to get a child within a directory,
99 * g_file_resolve_relative_path() to resolve a relative path between two
100 * #GFiles. There can be multiple hierarchies, so you may not end up at
101 * the same root if you repeatedly call g_file_get_parent() on two different
104 * All #GFiles have a basename (get with g_file_get_basename()). These names
105 * are byte strings that are used to identify the file on the filesystem
106 * (relative to its parent directory) and there is no guarantees that they
107 * have any particular charset encoding or even make any sense at all. If
108 * you want to use filenames in a user interface you should use the display
109 * name that you can get by requesting the
110 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
111 * This is guaranteed to be in UTF-8 and can be used in a user interface.
112 * But always store the real basename or the #GFile to use to actually
113 * access the file, because there is no way to go from a display name to
116 * Using #GFile as an identifier has the same weaknesses as using a path
117 * in that there may be multiple aliases for the same file. For instance,
118 * hard or soft links may cause two different #GFiles to refer to the same
119 * file. Other possible causes for aliases are: case insensitive filesystems,
120 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
121 * check if two #GFiles point to the same file you can query for the
122 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
123 * canonicalization of pathnames passed in, so that trivial differences in
124 * the path string used at creation (duplicated slashes, slash at end of
125 * path, "." or ".." path segments, etc) does not create different #GFiles.
127 * Many #GFile operations have both synchronous and asynchronous versions
128 * to suit your application. Asynchronous versions of synchronous functions
129 * simply have _async() appended to their function names. The asynchronous
130 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
131 * the operation, producing a GAsyncResult which is then passed to the
132 * function's matching _finish() operation.
134 * Some #GFile operations do not have synchronous analogs, as they may
135 * take a very long time to finish, and blocking may leave an application
136 * unusable. Notable cases include:
138 * <member>g_file_mount_mountable() to mount a mountable file.</member>
139 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
140 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
143 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
144 * One notable feature of #GFiles are entity tags, or "etags" for
145 * short. Entity tags are somewhat like a more abstract version of the
146 * traditional mtime, and can be used to quickly determine if the file has
147 * been modified from the version on the file system. See the HTTP 1.1
148 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
149 * for HTTP Etag headers, which are a very similar concept.
153 static void g_file_real_query_info_async (GFile *file,
154 const char *attributes,
155 GFileQueryInfoFlags flags,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
160 static GFileInfo * g_file_real_query_info_finish (GFile *file,
163 static void g_file_real_query_filesystem_info_async (GFile *file,
164 const char *attributes,
166 GCancellable *cancellable,
167 GAsyncReadyCallback callback,
169 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
172 static void g_file_real_enumerate_children_async (GFile *file,
173 const char *attributes,
174 GFileQueryInfoFlags flags,
176 GCancellable *cancellable,
177 GAsyncReadyCallback callback,
179 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
182 static void g_file_real_read_async (GFile *file,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
187 static GFileInputStream * g_file_real_read_finish (GFile *file,
190 static void g_file_real_append_to_async (GFile *file,
191 GFileCreateFlags flags,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
196 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
199 static void g_file_real_create_async (GFile *file,
200 GFileCreateFlags flags,
202 GCancellable *cancellable,
203 GAsyncReadyCallback callback,
205 static GFileOutputStream *g_file_real_create_finish (GFile *file,
208 static void g_file_real_replace_async (GFile *file,
210 gboolean make_backup,
211 GFileCreateFlags flags,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
216 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
219 static void g_file_real_delete_async (GFile *file,
221 GCancellable *cancellable,
222 GAsyncReadyCallback callback,
224 static gboolean g_file_real_delete_finish (GFile *file,
227 static void g_file_real_open_readwrite_async (GFile *file,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
232 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
235 static void g_file_real_create_readwrite_async (GFile *file,
236 GFileCreateFlags flags,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
241 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
244 static void g_file_real_replace_readwrite_async (GFile *file,
246 gboolean make_backup,
247 GFileCreateFlags flags,
249 GCancellable *cancellable,
250 GAsyncReadyCallback callback,
252 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
255 static gboolean g_file_real_set_attributes_from_info (GFile *file,
257 GFileQueryInfoFlags flags,
258 GCancellable *cancellable,
260 static void g_file_real_set_display_name_async (GFile *file,
261 const char *display_name,
263 GCancellable *cancellable,
264 GAsyncReadyCallback callback,
266 static GFile * g_file_real_set_display_name_finish (GFile *file,
269 static void g_file_real_set_attributes_async (GFile *file,
271 GFileQueryInfoFlags flags,
273 GCancellable *cancellable,
274 GAsyncReadyCallback callback,
276 static gboolean g_file_real_set_attributes_finish (GFile *file,
280 static void g_file_real_find_enclosing_mount_async (GFile *file,
282 GCancellable *cancellable,
283 GAsyncReadyCallback callback,
285 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
288 static void g_file_real_copy_async (GFile *source,
290 GFileCopyFlags flags,
292 GCancellable *cancellable,
293 GFileProgressCallback progress_callback,
294 gpointer progress_callback_data,
295 GAsyncReadyCallback callback,
297 static gboolean g_file_real_copy_finish (GFile *file,
301 typedef GFileIface GFileInterface;
302 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
305 g_file_default_init (GFileIface *iface)
307 iface->enumerate_children_async = g_file_real_enumerate_children_async;
308 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
309 iface->set_display_name_async = g_file_real_set_display_name_async;
310 iface->set_display_name_finish = g_file_real_set_display_name_finish;
311 iface->query_info_async = g_file_real_query_info_async;
312 iface->query_info_finish = g_file_real_query_info_finish;
313 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
314 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
315 iface->set_attributes_async = g_file_real_set_attributes_async;
316 iface->set_attributes_finish = g_file_real_set_attributes_finish;
317 iface->read_async = g_file_real_read_async;
318 iface->read_finish = g_file_real_read_finish;
319 iface->append_to_async = g_file_real_append_to_async;
320 iface->append_to_finish = g_file_real_append_to_finish;
321 iface->create_async = g_file_real_create_async;
322 iface->create_finish = g_file_real_create_finish;
323 iface->replace_async = g_file_real_replace_async;
324 iface->replace_finish = g_file_real_replace_finish;
325 iface->delete_file_async = g_file_real_delete_async;
326 iface->delete_file_finish = g_file_real_delete_finish;
327 iface->open_readwrite_async = g_file_real_open_readwrite_async;
328 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
329 iface->create_readwrite_async = g_file_real_create_readwrite_async;
330 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
331 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
332 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
333 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
334 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
335 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
336 iface->copy_async = g_file_real_copy_async;
337 iface->copy_finish = g_file_real_copy_finish;
343 * @file: input #GFile
345 * Checks to see if a file is native to the platform.
347 * A native file s one expressed in the platform-native filename format,
348 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
349 * as it might be on a locally mounted remote filesystem.
351 * On some systems non-native files may be available using the native
352 * filesystem via a userspace filesystem (FUSE), in these cases this call
353 * will return %FALSE, but g_file_get_path() will still return a native path.
355 * This call does no blocking I/O.
357 * Returns: %TRUE if @file is native
360 g_file_is_native (GFile *file)
364 g_return_val_if_fail (G_IS_FILE (file), FALSE);
366 iface = G_FILE_GET_IFACE (file);
368 return (* iface->is_native) (file);
373 * g_file_has_uri_scheme:
374 * @file: input #GFile
375 * @uri_scheme: a string containing a URI scheme
377 * Checks to see if a #GFile has a given URI scheme.
379 * This call does no blocking I/O.
381 * Returns: %TRUE if #GFile's backend supports the
382 * given URI scheme, %FALSE if URI scheme is %NULL,
383 * not supported, or #GFile is invalid.
386 g_file_has_uri_scheme (GFile *file,
387 const char *uri_scheme)
391 g_return_val_if_fail (G_IS_FILE (file), FALSE);
392 g_return_val_if_fail (uri_scheme != NULL, FALSE);
394 iface = G_FILE_GET_IFACE (file);
396 return (* iface->has_uri_scheme) (file, uri_scheme);
401 * g_file_get_uri_scheme:
402 * @file: input #GFile
404 * Gets the URI scheme for a #GFile.
405 * RFC 3986 decodes the scheme as:
407 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
409 * Common schemes include "file", "http", "ftp", etc.
411 * This call does no blocking I/O.
413 * Returns: a string containing the URI scheme for the given
414 * #GFile. The returned string should be freed with g_free()
415 * when no longer needed.
418 g_file_get_uri_scheme (GFile *file)
422 g_return_val_if_fail (G_IS_FILE (file), NULL);
424 iface = G_FILE_GET_IFACE (file);
426 return (* iface->get_uri_scheme) (file);
431 * g_file_get_basename:
432 * @file: input #GFile
434 * Gets the base name (the last component of the path) for a given #GFile.
436 * If called for the top level of a system (such as the filesystem root
437 * or a uri like sftp://host/) it will return a single directory separator
438 * (and on Windows, possibly a drive letter).
440 * The base name is a byte string (not UTF-8). It has no defined encoding
441 * or rules other than it may not contain zero bytes. If you want to use
442 * filenames in a user interface you should use the display name that you
443 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
444 * attribute with g_file_query_info().
446 * This call does no blocking I/O.
448 * Returns: string containing the #GFile's base name, or %NULL
449 * if given #GFile is invalid. The returned string should be
450 * freed with g_free() when no longer needed.
453 g_file_get_basename (GFile *file)
457 g_return_val_if_fail (G_IS_FILE (file), NULL);
459 iface = G_FILE_GET_IFACE (file);
461 return (* iface->get_basename) (file);
466 * @file: input #GFile
468 * Gets the local pathname for #GFile, if one exists.
470 * This call does no blocking I/O.
472 * Returns: string containing the #GFile's path, or %NULL if
473 * no such path exists. The returned string should be
474 * freed with g_free() when no longer needed.
477 g_file_get_path (GFile *file)
481 g_return_val_if_fail (G_IS_FILE (file), NULL);
483 iface = G_FILE_GET_IFACE (file);
485 return (* iface->get_path) (file);
490 * @file: input #GFile
492 * Gets the URI for the @file.
494 * This call does no blocking I/O.
496 * Returns: a string containing the #GFile's URI.
497 * The returned string should be freed with g_free()
498 * when no longer needed.
501 g_file_get_uri (GFile *file)
505 g_return_val_if_fail (G_IS_FILE (file), NULL);
507 iface = G_FILE_GET_IFACE (file);
509 return (* iface->get_uri) (file);
513 * g_file_get_parse_name:
514 * @file: input #GFile
516 * Gets the parse name of the @file.
517 * A parse name is a UTF-8 string that describes the
518 * file such that one can get the #GFile back using
519 * g_file_parse_name().
521 * This is generally used to show the #GFile as a nice
522 * full-pathname kind of string in a user interface,
523 * like in a location entry.
525 * For local files with names that can safely be converted
526 * to UTF-8 the pathname is used, otherwise the IRI is used
527 * (a form of URI that allows UTF-8 characters unescaped).
529 * This call does no blocking I/O.
531 * Returns: a string containing the #GFile's parse name.
532 * The returned string should be freed with g_free()
533 * when no longer needed.
536 g_file_get_parse_name (GFile *file)
540 g_return_val_if_fail (G_IS_FILE (file), NULL);
542 iface = G_FILE_GET_IFACE (file);
544 return (* iface->get_parse_name) (file);
549 * @file: input #GFile
551 * Duplicates a #GFile handle. This operation does not duplicate
552 * the actual file or directory represented by the #GFile; see
553 * g_file_copy() if attempting to copy a file.
555 * This call does no blocking I/O.
557 * Returns: (transfer full): a new #GFile that is a duplicate
558 * of the given #GFile.
561 g_file_dup (GFile *file)
565 g_return_val_if_fail (G_IS_FILE (file), NULL);
567 iface = G_FILE_GET_IFACE (file);
569 return (* iface->dup) (file);
574 * @file: (type GFile): #gconstpointer to a #GFile
576 * Creates a hash value for a #GFile.
578 * This call does no blocking I/O.
581 * Returns: 0 if @file is not a valid #GFile, otherwise an
582 * integer that can be used as hash value for the #GFile.
583 * This function is intended for easily hashing a #GFile to
584 * add to a #GHashTable or similar data structure.
587 g_file_hash (gconstpointer file)
591 g_return_val_if_fail (G_IS_FILE (file), 0);
593 iface = G_FILE_GET_IFACE (file);
595 return (* iface->hash) ((GFile *)file);
600 * @file1: the first #GFile
601 * @file2: the second #GFile
603 * Checks equality of two given #GFiles.
605 * Note that two #GFiles that differ can still refer to the same
606 * file on the filesystem due to various forms of filename
609 * This call does no blocking I/O.
611 * Returns: %TRUE if @file1 and @file2 are equal.
612 * %FALSE if either is not a #GFile.
615 g_file_equal (GFile *file1,
620 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
621 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
623 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
626 iface = G_FILE_GET_IFACE (file1);
628 return (* iface->equal) (file1, file2);
634 * @file: input #GFile
636 * Gets the parent directory for the @file.
637 * If the @file represents the root directory of the
638 * file system, then %NULL will be returned.
640 * This call does no blocking I/O.
642 * Returns: (transfer full): a #GFile structure to the
643 * parent of the given #GFile or %NULL if there is
644 * no parent. Free the returned object with g_object_unref().
647 g_file_get_parent (GFile *file)
651 g_return_val_if_fail (G_IS_FILE (file), NULL);
653 iface = G_FILE_GET_IFACE (file);
655 return (* iface->get_parent) (file);
660 * @file: input #GFile
661 * @parent: (allow-none): the parent to check for, or %NULL
663 * Checks if @file has a parent, and optionally, if it is @parent.
665 * If @parent is %NULL then this function returns %TRUE if @file has any
666 * parent at all. If @parent is non-%NULL then %TRUE is only returned
667 * if @file is a child of @parent.
669 * Returns: %TRUE if @file is a child of @parent (or any parent in the
670 * case that @parent is %NULL).
675 g_file_has_parent (GFile *file,
678 GFile *actual_parent;
681 g_return_val_if_fail (G_IS_FILE (file), FALSE);
682 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
684 actual_parent = g_file_get_parent (file);
686 if (actual_parent != NULL)
689 result = g_file_equal (parent, actual_parent);
693 g_object_unref (actual_parent);
703 * @file: input #GFile
704 * @name: string containing the child's basename
706 * Gets a child of @file with basename equal to @name.
708 * Note that the file with that specific name might not exist, but
709 * you can still have a #GFile that points to it. You can use this
710 * for instance to create that file.
712 * This call does no blocking I/O.
714 * Returns: (transfer full): a #GFile to a child specified by @name.
715 * Free the returned object with g_object_unref().
718 g_file_get_child (GFile *file,
721 g_return_val_if_fail (G_IS_FILE (file), NULL);
722 g_return_val_if_fail (name != NULL, NULL);
724 return g_file_resolve_relative_path (file, name);
728 * g_file_get_child_for_display_name:
729 * @file: input #GFile
730 * @display_name: string to a possible child
731 * @error: return location for an error
733 * Gets the child of @file for a given @display_name (i.e. a UTF-8
734 * version of the name). If this function fails, it returns %NULL
735 * and @error will be set. This is very useful when constructing a
736 * #GFile for a new file and the user entered the filename in the
737 * user interface, for instance when you select a directory and
738 * type a filename in the file selector.
740 * This call does no blocking I/O.
742 * Returns: (transfer full): a #GFile to the specified child, or
743 * %NULL if the display name couldn't be converted.
744 * Free the returned object with g_object_unref().
747 g_file_get_child_for_display_name (GFile *file,
748 const char *display_name,
753 g_return_val_if_fail (G_IS_FILE (file), NULL);
754 g_return_val_if_fail (display_name != NULL, NULL);
756 iface = G_FILE_GET_IFACE (file);
758 return (* iface->get_child_for_display_name) (file, display_name, error);
763 * @file: input #GFile
764 * @prefix: input #GFile
766 * Checks whether @file has the prefix specified by @prefix.
768 * In other words, if the names of initial elements of @file's
769 * pathname match @prefix. Only full pathname elements are matched,
770 * so a path like /foo is not considered a prefix of /foobar, only
773 * This call does no I/O, as it works purely on names. As such it can
774 * sometimes return %FALSE even if @file is inside a @prefix (from a
775 * filesystem point of view), because the prefix of @file is an alias
778 * Virtual: prefix_matches
779 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
783 g_file_has_prefix (GFile *file,
788 g_return_val_if_fail (G_IS_FILE (file), FALSE);
789 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
791 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
794 iface = G_FILE_GET_IFACE (file);
796 /* The vtable function differs in arg order since
797 * we're using the old contains_file call
799 return (* iface->prefix_matches) (prefix, file);
803 * g_file_get_relative_path:
804 * @parent: input #GFile
805 * @descendant: input #GFile
807 * Gets the path for @descendant relative to @parent.
809 * This call does no blocking I/O.
811 * Returns: string with the relative path from @descendant
812 * to @parent, or %NULL if @descendant doesn't have @parent
813 * as prefix. The returned string should be freed with g_free()
814 * when no longer needed.
817 g_file_get_relative_path (GFile *parent,
822 g_return_val_if_fail (G_IS_FILE (parent), NULL);
823 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
825 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
828 iface = G_FILE_GET_IFACE (parent);
830 return (* iface->get_relative_path) (parent, descendant);
834 * g_file_resolve_relative_path:
835 * @file: input #GFile
836 * @relative_path: a given relative path string
838 * Resolves a relative path for @file to an absolute path.
840 * This call does no blocking I/O.
842 * Returns: (transfer full): #GFile to the resolved path.
843 * %NULL if @relative_path is %NULL or if @file is invalid.
844 * Free the returned object with g_object_unref().
847 g_file_resolve_relative_path (GFile *file,
848 const char *relative_path)
852 g_return_val_if_fail (G_IS_FILE (file), NULL);
853 g_return_val_if_fail (relative_path != NULL, NULL);
855 iface = G_FILE_GET_IFACE (file);
857 return (* iface->resolve_relative_path) (file, relative_path);
861 * g_file_enumerate_children:
862 * @file: input #GFile
863 * @attributes: an attribute query string
864 * @flags: a set of #GFileQueryInfoFlags
865 * @cancellable: (allow-none): optional #GCancellable object,
867 * @error: #GError for error reporting
869 * Gets the requested information about the files in a directory.
870 * The result is a #GFileEnumerator object that will give out
871 * #GFileInfo objects for all the files in the directory.
873 * The @attributes value is a string that specifies the file
874 * attributes that should be gathered. It is not an error if
875 * it's not possible to read a particular requested attribute
876 * from a file - it just won't be set. @attributes should
877 * be a comma-separated list of attributes or attribute wildcards.
878 * The wildcard "*" means all attributes, and a wildcard like
879 * "standard::*" means all attributes in the standard namespace.
880 * An example attribute query be "standard::*,owner::user".
881 * The standard attributes are available as defines, like
882 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
884 * If @cancellable is not %NULL, then the operation can be cancelled
885 * by triggering the cancellable object from another thread. If the
886 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
889 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
890 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
891 * error will be returned. Other errors are possible too.
893 * Returns: (transfer full): A #GFileEnumerator if successful,
894 * %NULL on error. Free the returned object with g_object_unref().
897 g_file_enumerate_children (GFile *file,
898 const char *attributes,
899 GFileQueryInfoFlags flags,
900 GCancellable *cancellable,
905 g_return_val_if_fail (G_IS_FILE (file), NULL);
907 if (g_cancellable_set_error_if_cancelled (cancellable, error))
910 iface = G_FILE_GET_IFACE (file);
912 if (iface->enumerate_children == NULL)
914 g_set_error_literal (error, G_IO_ERROR,
915 G_IO_ERROR_NOT_SUPPORTED,
916 _("Operation not supported"));
920 return (* iface->enumerate_children) (file, attributes, flags,
925 * g_file_enumerate_children_async:
926 * @file: input #GFile
927 * @attributes: an attribute query string
928 * @flags: a set of #GFileQueryInfoFlags
929 * @io_priority: the <link linkend="io-priority">I/O priority</link>
931 * @cancellable: (allow-none): optional #GCancellable object,
933 * @callback: (scope async): a #GAsyncReadyCallback to call when the
934 * request is satisfied
935 * @user_data: (closure): the data to pass to callback function
937 * Asynchronously gets the requested information about the files
938 * in a directory. The result is a #GFileEnumerator object that will
939 * give out #GFileInfo objects for all the files in the directory.
941 * For more details, see g_file_enumerate_children() which is
942 * the synchronous version of this call.
944 * When the operation is finished, @callback will be called. You can
945 * then call g_file_enumerate_children_finish() to get the result of
949 g_file_enumerate_children_async (GFile *file,
950 const char *attributes,
951 GFileQueryInfoFlags flags,
953 GCancellable *cancellable,
954 GAsyncReadyCallback callback,
959 g_return_if_fail (G_IS_FILE (file));
961 iface = G_FILE_GET_IFACE (file);
962 (* iface->enumerate_children_async) (file,
972 * g_file_enumerate_children_finish:
973 * @file: input #GFile
974 * @res: a #GAsyncResult
977 * Finishes an async enumerate children operation.
978 * See g_file_enumerate_children_async().
980 * Returns: (transfer full): a #GFileEnumerator or %NULL
981 * if an error occurred.
982 * Free the returned object with g_object_unref().
985 g_file_enumerate_children_finish (GFile *file,
991 g_return_val_if_fail (G_IS_FILE (file), NULL);
992 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
994 if (g_async_result_legacy_propagate_error (res, error))
997 iface = G_FILE_GET_IFACE (file);
998 return (* iface->enumerate_children_finish) (file, res, error);
1002 * g_file_query_exists:
1003 * @file: input #GFile
1004 * @cancellable: (allow-none): optional #GCancellable object,
1007 * Utility function to check if a particular file exists. This is
1008 * implemented using g_file_query_info() and as such does blocking I/O.
1010 * Note that in many cases it is racy to first check for file existence
1011 * and then execute something based on the outcome of that, because the
1012 * file might have been created or removed in between the operations. The
1013 * general approach to handling that is to not check, but just do the
1014 * operation and handle the errors as they come.
1016 * As an example of race-free checking, take the case of reading a file,
1017 * and if it doesn't exist, creating it. There are two racy versions: read
1018 * it, and on error create it; and: check if it exists, if not create it.
1019 * These can both result in two processes creating the file (with perhaps
1020 * a partially written file as the result). The correct approach is to
1021 * always try to create the file with g_file_create() which will either
1022 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1024 * However, in many cases an existence check is useful in a user interface,
1025 * for instance to make a menu item sensitive/insensitive, so that you don't
1026 * have to fool users that something is possible and then just show an error
1027 * dialog. If you do this, you should make sure to also handle the errors
1028 * that can happen due to races when you execute the operation.
1030 * Returns: %TRUE if the file exists (and can be detected without error),
1031 * %FALSE otherwise (or if cancelled).
1034 g_file_query_exists (GFile *file,
1035 GCancellable *cancellable)
1039 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1041 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1042 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1045 g_object_unref (info);
1053 * g_file_query_file_type:
1054 * @file: input #GFile
1055 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1056 * @cancellable: (allow-none): optional #GCancellable object,
1059 * Utility function to inspect the #GFileType of a file. This is
1060 * implemented using g_file_query_info() and as such does blocking I/O.
1062 * The primary use case of this method is to check if a file is
1063 * a regular file, directory, or symlink.
1065 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1066 * if the file does not exist
1071 g_file_query_file_type (GFile *file,
1072 GFileQueryInfoFlags flags,
1073 GCancellable *cancellable)
1076 GFileType file_type;
1078 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1079 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1083 file_type = g_file_info_get_file_type (info);
1084 g_object_unref (info);
1087 file_type = G_FILE_TYPE_UNKNOWN;
1093 * g_file_query_info:
1094 * @file: input #GFile
1095 * @attributes: an attribute query string
1096 * @flags: a set of #GFileQueryInfoFlags
1097 * @cancellable: (allow-none): optional #GCancellable object,
1101 * Gets the requested information about specified @file.
1102 * The result is a #GFileInfo object that contains key-value
1103 * attributes (such as the type or size of the file).
1105 * The @attributes value is a string that specifies the file
1106 * attributes that should be gathered. It is not an error if
1107 * it's not possible to read a particular requested attribute
1108 * from a file - it just won't be set. @attributes should be a
1109 * comma-separated list of attributes or attribute wildcards.
1110 * The wildcard "*" means all attributes, and a wildcard like
1111 * "standard::*" means all attributes in the standard namespace.
1112 * An example attribute query be "standard::*,owner::user".
1113 * The standard attributes are available as defines, like
1114 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1116 * If @cancellable is not %NULL, then the operation can be cancelled
1117 * by triggering the cancellable object from another thread. If the
1118 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1121 * For symlinks, normally the information about the target of the
1122 * symlink is returned, rather than information about the symlink
1123 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1124 * in @flags the information about the symlink itself will be returned.
1125 * Also, for symlinks that point to non-existing files the information
1126 * about the symlink itself will be returned.
1128 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1129 * returned. Other errors are possible too, and depend on what kind of
1130 * filesystem the file is on.
1132 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1133 * on error. Free the returned object with g_object_unref().
1136 g_file_query_info (GFile *file,
1137 const char *attributes,
1138 GFileQueryInfoFlags flags,
1139 GCancellable *cancellable,
1144 g_return_val_if_fail (G_IS_FILE (file), NULL);
1146 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1149 iface = G_FILE_GET_IFACE (file);
1151 if (iface->query_info == NULL)
1153 g_set_error_literal (error, G_IO_ERROR,
1154 G_IO_ERROR_NOT_SUPPORTED,
1155 _("Operation not supported"));
1159 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1163 * g_file_query_info_async:
1164 * @file: input #GFile
1165 * @attributes: an attribute query string
1166 * @flags: a set of #GFileQueryInfoFlags
1167 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1169 * @cancellable: (allow-none): optional #GCancellable object,
1171 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1172 * request is satisfied
1173 * @user_data: (closure): the data to pass to callback function
1175 * Asynchronously gets the requested information about specified @file.
1176 * The result is a #GFileInfo object that contains key-value attributes
1177 * (such as type or size for the file).
1179 * For more details, see g_file_query_info() which is the synchronous
1180 * version of this call.
1182 * When the operation is finished, @callback will be called. You can
1183 * then call g_file_query_info_finish() to get the result of the operation.
1186 g_file_query_info_async (GFile *file,
1187 const char *attributes,
1188 GFileQueryInfoFlags flags,
1190 GCancellable *cancellable,
1191 GAsyncReadyCallback callback,
1196 g_return_if_fail (G_IS_FILE (file));
1198 iface = G_FILE_GET_IFACE (file);
1199 (* iface->query_info_async) (file,
1209 * g_file_query_info_finish:
1210 * @file: input #GFile
1211 * @res: a #GAsyncResult
1214 * Finishes an asynchronous file info query.
1215 * See g_file_query_info_async().
1217 * Returns: (transfer full): #GFileInfo for given @file
1218 * or %NULL on error. Free the returned object with
1222 g_file_query_info_finish (GFile *file,
1228 g_return_val_if_fail (G_IS_FILE (file), NULL);
1229 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1231 if (g_async_result_legacy_propagate_error (res, error))
1234 iface = G_FILE_GET_IFACE (file);
1235 return (* iface->query_info_finish) (file, res, error);
1239 * g_file_query_filesystem_info:
1240 * @file: input #GFile
1241 * @attributes: an attribute query string
1242 * @cancellable: (allow-none): optional #GCancellable object,
1246 * Similar to g_file_query_info(), but obtains information
1247 * about the filesystem the @file is on, rather than the file itself.
1248 * For instance the amount of space available and the type of
1251 * The @attributes value is a string that specifies the attributes
1252 * that should be gathered. It is not an error if it's not possible
1253 * to read a particular requested attribute from a file - it just
1254 * won't be set. @attributes should be a comma-separated list of
1255 * attributes or attribute wildcards. The wildcard "*" means all
1256 * attributes, and a wildcard like "filesystem::*" means all attributes
1257 * in the filesystem namespace. The standard namespace for filesystem
1258 * attributes is "filesystem". Common attributes of interest are
1259 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1260 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1261 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1263 * If @cancellable is not %NULL, then the operation can be cancelled
1264 * by triggering the cancellable object from another thread. If the
1265 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1268 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1269 * be returned. Other errors are possible too, and depend on what
1270 * kind of filesystem the file is on.
1272 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1273 * Free the returned object with g_object_unref().
1276 g_file_query_filesystem_info (GFile *file,
1277 const char *attributes,
1278 GCancellable *cancellable,
1283 g_return_val_if_fail (G_IS_FILE (file), NULL);
1285 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1288 iface = G_FILE_GET_IFACE (file);
1290 if (iface->query_filesystem_info == NULL)
1292 g_set_error_literal (error, G_IO_ERROR,
1293 G_IO_ERROR_NOT_SUPPORTED,
1294 _("Operation not supported"));
1298 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1302 * g_file_query_filesystem_info_async:
1303 * @file: input #GFile
1304 * @attributes: an attribute query string
1305 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1307 * @cancellable: (allow-none): optional #GCancellable object,
1309 * @callback: (scope async): a #GAsyncReadyCallback to call
1310 * when the request is satisfied
1311 * @user_data: (closure): the data to pass to callback function
1313 * Asynchronously gets the requested information about the filesystem
1314 * that the specified @file is on. The result is a #GFileInfo object
1315 * that contains key-value attributes (such as type or size for the
1318 * For more details, see g_file_query_filesystem_info() which is the
1319 * synchronous version of this call.
1321 * When the operation is finished, @callback will be called. You can
1322 * then call g_file_query_info_finish() to get the result of the
1326 g_file_query_filesystem_info_async (GFile *file,
1327 const char *attributes,
1329 GCancellable *cancellable,
1330 GAsyncReadyCallback callback,
1335 g_return_if_fail (G_IS_FILE (file));
1337 iface = G_FILE_GET_IFACE (file);
1338 (* iface->query_filesystem_info_async) (file,
1347 * g_file_query_filesystem_info_finish:
1348 * @file: input #GFile
1349 * @res: a #GAsyncResult
1352 * Finishes an asynchronous filesystem info query.
1353 * See g_file_query_filesystem_info_async().
1355 * Returns: (transfer full): #GFileInfo for given @file
1356 * or %NULL on error.
1357 * Free the returned object with g_object_unref().
1360 g_file_query_filesystem_info_finish (GFile *file,
1366 g_return_val_if_fail (G_IS_FILE (file), NULL);
1367 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1369 if (g_async_result_legacy_propagate_error (res, error))
1372 iface = G_FILE_GET_IFACE (file);
1373 return (* iface->query_filesystem_info_finish) (file, res, error);
1377 * g_file_find_enclosing_mount:
1378 * @file: input #GFile
1379 * @cancellable: (allow-none): optional #GCancellable object,
1383 * Gets a #GMount for the #GFile.
1385 * If the #GFileIface for @file does not have a mount (e.g.
1386 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1387 * and %NULL will be returned.
1389 * If @cancellable is not %NULL, then the operation can be cancelled by
1390 * triggering the cancellable object from another thread. If the operation
1391 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1393 * Returns: (transfer full): a #GMount where the @file is located
1394 * or %NULL on error.
1395 * Free the returned object with g_object_unref().
1398 g_file_find_enclosing_mount (GFile *file,
1399 GCancellable *cancellable,
1404 g_return_val_if_fail (G_IS_FILE (file), NULL);
1406 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1409 iface = G_FILE_GET_IFACE (file);
1410 if (iface->find_enclosing_mount == NULL)
1413 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1414 /* Translators: This is an error message when
1415 * trying to find the enclosing (user visible)
1416 * mount of a file, but none exists.
1418 _("Containing mount does not exist"));
1422 return (* iface->find_enclosing_mount) (file, cancellable, error);
1426 * g_file_find_enclosing_mount_async:
1428 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1430 * @cancellable: (allow-none): optional #GCancellable object,
1432 * @callback: (scope async): a #GAsyncReadyCallback to call
1433 * when the request is satisfied
1434 * @user_data: (closure): the data to pass to callback function
1436 * Asynchronously gets the mount for the file.
1438 * For more details, see g_file_find_enclosing_mount() which is
1439 * the synchronous version of this call.
1441 * When the operation is finished, @callback will be called.
1442 * You can then call g_file_find_enclosing_mount_finish() to
1443 * get the result of the operation.
1446 g_file_find_enclosing_mount_async (GFile *file,
1448 GCancellable *cancellable,
1449 GAsyncReadyCallback callback,
1454 g_return_if_fail (G_IS_FILE (file));
1456 iface = G_FILE_GET_IFACE (file);
1457 (* iface->find_enclosing_mount_async) (file,
1465 * g_file_find_enclosing_mount_finish:
1467 * @res: a #GAsyncResult
1470 * Finishes an asynchronous find mount request.
1471 * See g_file_find_enclosing_mount_async().
1473 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1474 * Free the returned object with g_object_unref().
1477 g_file_find_enclosing_mount_finish (GFile *file,
1483 g_return_val_if_fail (G_IS_FILE (file), NULL);
1484 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1486 if (g_async_result_legacy_propagate_error (res, error))
1489 iface = G_FILE_GET_IFACE (file);
1490 return (* iface->find_enclosing_mount_finish) (file, res, error);
1496 * @file: #GFile to read
1497 * @cancellable: (allow-none): a #GCancellable
1498 * @error: a #GError, or %NULL
1500 * Opens a file for reading. The result is a #GFileInputStream that
1501 * can be used to read the contents of the file.
1503 * If @cancellable is not %NULL, then the operation can be cancelled by
1504 * triggering the cancellable object from another thread. If the operation
1505 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1507 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1508 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1509 * error will be returned. Other errors are possible too, and depend
1510 * on what kind of filesystem the file is on.
1513 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1514 * Free the returned object with g_object_unref().
1517 g_file_read (GFile *file,
1518 GCancellable *cancellable,
1523 g_return_val_if_fail (G_IS_FILE (file), NULL);
1525 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1528 iface = G_FILE_GET_IFACE (file);
1530 if (iface->read_fn == NULL)
1532 g_set_error_literal (error, G_IO_ERROR,
1533 G_IO_ERROR_NOT_SUPPORTED,
1534 _("Operation not supported"));
1538 return (* iface->read_fn) (file, cancellable, error);
1543 * @file: input #GFile
1544 * @flags: a set of #GFileCreateFlags
1545 * @cancellable: (allow-none): optional #GCancellable object,
1547 * @error: a #GError, or %NULL
1549 * Gets an output stream for appending data to the file.
1550 * If the file doesn't already exist it is created.
1552 * By default files created are generally readable by everyone,
1553 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1554 * will be made readable only to the current user, to the level that
1555 * is supported on the target filesystem.
1557 * If @cancellable is not %NULL, then the operation can be cancelled
1558 * by triggering the cancellable object from another thread. If the
1559 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1562 * Some file systems don't allow all file names, and may return an
1563 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1564 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1565 * possible too, and depend on what kind of filesystem the file is on.
1567 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1568 * Free the returned object with g_object_unref().
1571 g_file_append_to (GFile *file,
1572 GFileCreateFlags flags,
1573 GCancellable *cancellable,
1578 g_return_val_if_fail (G_IS_FILE (file), NULL);
1580 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1583 iface = G_FILE_GET_IFACE (file);
1585 if (iface->append_to == NULL)
1587 g_set_error_literal (error, G_IO_ERROR,
1588 G_IO_ERROR_NOT_SUPPORTED,
1589 _("Operation not supported"));
1593 return (* iface->append_to) (file, flags, cancellable, error);
1598 * @file: input #GFile
1599 * @flags: a set of #GFileCreateFlags
1600 * @cancellable: (allow-none): optional #GCancellable object,
1602 * @error: a #GError, or %NULL
1604 * Creates a new file and returns an output stream for writing to it.
1605 * The file must not already exist.
1607 * By default files created are generally readable by everyone,
1608 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1609 * will be made readable only to the current user, to the level
1610 * that is supported on the target filesystem.
1612 * If @cancellable is not %NULL, then the operation can be cancelled
1613 * by triggering the cancellable object from another thread. If the
1614 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1617 * If a file or directory with this name already exists the
1618 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1619 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1620 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1621 * be returned. Other errors are possible too, and depend on what kind
1622 * of filesystem the file is on.
1624 * Returns: (transfer full): a #GFileOutputStream for the newly created
1625 * file, or %NULL on error.
1626 * Free the returned object with g_object_unref().
1629 g_file_create (GFile *file,
1630 GFileCreateFlags flags,
1631 GCancellable *cancellable,
1636 g_return_val_if_fail (G_IS_FILE (file), NULL);
1638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1641 iface = G_FILE_GET_IFACE (file);
1643 if (iface->create == NULL)
1645 g_set_error_literal (error, G_IO_ERROR,
1646 G_IO_ERROR_NOT_SUPPORTED,
1647 _("Operation not supported"));
1651 return (* iface->create) (file, flags, cancellable, error);
1656 * @file: input #GFile
1657 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1658 * for the current #GFile, or #NULL to ignore
1659 * @make_backup: %TRUE if a backup should be created
1660 * @flags: a set of #GFileCreateFlags
1661 * @cancellable: (allow-none): optional #GCancellable object,
1663 * @error: a #GError, or %NULL
1665 * Returns an output stream for overwriting the file, possibly
1666 * creating a backup copy of the file first. If the file doesn't exist,
1667 * it will be created.
1669 * This will try to replace the file in the safest way possible so
1670 * that any errors during the writing will not affect an already
1671 * existing copy of the file. For instance, for local files it
1672 * may write to a temporary file and then atomically rename over
1673 * the destination when the stream is closed.
1675 * By default files created are generally readable by everyone,
1676 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1677 * will be made readable only to the current user, to the level that
1678 * is supported on the target filesystem.
1680 * If @cancellable is not %NULL, then the operation can be cancelled
1681 * by triggering the cancellable object from another thread. If the
1682 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1685 * If you pass in a non-%NULL @etag value, then this value is
1686 * compared to the current entity tag of the file, and if they differ
1687 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1688 * that the file has been changed since you last read it. You can get
1689 * the new etag from g_file_output_stream_get_etag() after you've
1690 * finished writing and closed the #GFileOutputStream. When you load
1691 * a new file you can use g_file_input_stream_query_info() to get
1692 * the etag of the file.
1694 * If @make_backup is %TRUE, this function will attempt to make a
1695 * backup of the current file before overwriting it. If this fails
1696 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1697 * want to replace anyway, try again with @make_backup set to %FALSE.
1699 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1700 * be returned, and if the file is some other form of non-regular file
1701 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1702 * file systems don't allow all file names, and may return an
1703 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1704 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1705 * possible too, and depend on what kind of filesystem the file is on.
1707 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1708 * Free the returned object with g_object_unref().
1711 g_file_replace (GFile *file,
1713 gboolean make_backup,
1714 GFileCreateFlags flags,
1715 GCancellable *cancellable,
1720 g_return_val_if_fail (G_IS_FILE (file), NULL);
1722 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1725 iface = G_FILE_GET_IFACE (file);
1727 if (iface->replace == NULL)
1729 g_set_error_literal (error, G_IO_ERROR,
1730 G_IO_ERROR_NOT_SUPPORTED,
1731 _("Operation not supported"));
1735 /* Handle empty tag string as NULL in consistent way. */
1736 if (etag && *etag == 0)
1739 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1743 * g_file_open_readwrite:
1744 * @file: #GFile to open
1745 * @cancellable: (allow-none): a #GCancellable
1746 * @error: a #GError, or %NULL
1748 * Opens an existing file for reading and writing. The result is
1749 * a #GFileIOStream that can be used to read and write the contents
1752 * If @cancellable is not %NULL, then the operation can be cancelled
1753 * by triggering the cancellable object from another thread. If the
1754 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1757 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1758 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1759 * error will be returned. Other errors are possible too, and depend on
1760 * what kind of filesystem the file is on. Note that in many non-local
1761 * file cases read and write streams are not supported, so make sure you
1762 * really need to do read and write streaming, rather than just opening
1763 * for reading or writing.
1765 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1766 * Free the returned object with g_object_unref().
1771 g_file_open_readwrite (GFile *file,
1772 GCancellable *cancellable,
1777 g_return_val_if_fail (G_IS_FILE (file), NULL);
1779 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1782 iface = G_FILE_GET_IFACE (file);
1784 if (iface->open_readwrite == NULL)
1786 g_set_error_literal (error, G_IO_ERROR,
1787 G_IO_ERROR_NOT_SUPPORTED,
1788 _("Operation not supported"));
1792 return (* iface->open_readwrite) (file, cancellable, error);
1796 * g_file_create_readwrite:
1798 * @flags: a set of #GFileCreateFlags
1799 * @cancellable: (allow-none): optional #GCancellable object,
1801 * @error: return location for a #GError, or %NULL
1803 * Creates a new file and returns a stream for reading and
1804 * writing to it. The file must not already exist.
1806 * By default files created are generally readable by everyone,
1807 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1808 * will be made readable only to the current user, to the level
1809 * that is supported on the target filesystem.
1811 * If @cancellable is not %NULL, then the operation can be cancelled
1812 * by triggering the cancellable object from another thread. If the
1813 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1816 * If a file or directory with this name already exists, the
1817 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1818 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1819 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1820 * will be returned. Other errors are possible too, and depend on what
1821 * kind of filesystem the file is on.
1823 * Note that in many non-local file cases read and write streams are
1824 * not supported, so make sure you really need to do read and write
1825 * streaming, rather than just opening for reading or writing.
1827 * Returns: (transfer full): a #GFileIOStream for the newly created
1828 * file, or %NULL on error.
1829 * Free the returned object with g_object_unref().
1834 g_file_create_readwrite (GFile *file,
1835 GFileCreateFlags flags,
1836 GCancellable *cancellable,
1841 g_return_val_if_fail (G_IS_FILE (file), NULL);
1843 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1846 iface = G_FILE_GET_IFACE (file);
1848 if (iface->create_readwrite == NULL)
1850 g_set_error_literal (error, G_IO_ERROR,
1851 G_IO_ERROR_NOT_SUPPORTED,
1852 _("Operation not supported"));
1856 return (* iface->create_readwrite) (file, flags, cancellable, error);
1860 * g_file_replace_readwrite:
1862 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1863 * for the current #GFile, or #NULL to ignore
1864 * @make_backup: %TRUE if a backup should be created
1865 * @flags: a set of #GFileCreateFlags
1866 * @cancellable: (allow-none): optional #GCancellable object,
1868 * @error: return location for a #GError, or %NULL
1870 * Returns an output stream for overwriting the file in readwrite mode,
1871 * possibly creating a backup copy of the file first. If the file doesn't
1872 * exist, it will be created.
1874 * For details about the behaviour, see g_file_replace() which does the
1875 * same thing but returns an output stream only.
1877 * Note that in many non-local file cases read and write streams are not
1878 * supported, so make sure you really need to do read and write streaming,
1879 * rather than just opening for reading or writing.
1881 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1882 * Free the returned object with g_object_unref().
1887 g_file_replace_readwrite (GFile *file,
1889 gboolean make_backup,
1890 GFileCreateFlags flags,
1891 GCancellable *cancellable,
1896 g_return_val_if_fail (G_IS_FILE (file), NULL);
1898 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1901 iface = G_FILE_GET_IFACE (file);
1903 if (iface->replace_readwrite == NULL)
1905 g_set_error_literal (error, G_IO_ERROR,
1906 G_IO_ERROR_NOT_SUPPORTED,
1907 _("Operation not supported"));
1911 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1915 * g_file_read_async:
1916 * @file: input #GFile
1917 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1919 * @cancellable: (allow-none): optional #GCancellable object,
1921 * @callback: (scope async): a #GAsyncReadyCallback to call
1922 * when the request is satisfied
1923 * @user_data: (closure): the data to pass to callback function
1925 * Asynchronously opens @file for reading.
1927 * For more details, see g_file_read() which is
1928 * the synchronous version of this call.
1930 * When the operation is finished, @callback will be called.
1931 * You can then call g_file_read_finish() to get the result
1935 g_file_read_async (GFile *file,
1937 GCancellable *cancellable,
1938 GAsyncReadyCallback callback,
1943 g_return_if_fail (G_IS_FILE (file));
1945 iface = G_FILE_GET_IFACE (file);
1946 (* iface->read_async) (file,
1954 * g_file_read_finish:
1955 * @file: input #GFile
1956 * @res: a #GAsyncResult
1957 * @error: a #GError, or %NULL
1959 * Finishes an asynchronous file read operation started with
1960 * g_file_read_async().
1962 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1963 * Free the returned object with g_object_unref().
1966 g_file_read_finish (GFile *file,
1972 g_return_val_if_fail (G_IS_FILE (file), NULL);
1973 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1975 if (g_async_result_legacy_propagate_error (res, error))
1978 iface = G_FILE_GET_IFACE (file);
1979 return (* iface->read_finish) (file, res, error);
1983 * g_file_append_to_async:
1984 * @file: input #GFile
1985 * @flags: a set of #GFileCreateFlags
1986 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1988 * @cancellable: (allow-none): optional #GCancellable object,
1990 * @callback: (scope async): a #GAsyncReadyCallback to call
1991 * when the request is satisfied
1992 * @user_data: (closure): the data to pass to callback function
1994 * Asynchronously opens @file for appending.
1996 * For more details, see g_file_append_to() which is
1997 * the synchronous version of this call.
1999 * When the operation is finished, @callback will be called.
2000 * You can then call g_file_append_to_finish() to get the result
2004 g_file_append_to_async (GFile *file,
2005 GFileCreateFlags flags,
2007 GCancellable *cancellable,
2008 GAsyncReadyCallback callback,
2013 g_return_if_fail (G_IS_FILE (file));
2015 iface = G_FILE_GET_IFACE (file);
2016 (* iface->append_to_async) (file,
2025 * g_file_append_to_finish:
2026 * @file: input #GFile
2027 * @res: #GAsyncResult
2028 * @error: a #GError, or %NULL
2030 * Finishes an asynchronous file append operation started with
2031 * g_file_append_to_async().
2033 * Returns: (transfer full): a valid #GFileOutputStream
2034 * or %NULL on error.
2035 * Free the returned object with g_object_unref().
2038 g_file_append_to_finish (GFile *file,
2044 g_return_val_if_fail (G_IS_FILE (file), NULL);
2045 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2047 if (g_async_result_legacy_propagate_error (res, error))
2050 iface = G_FILE_GET_IFACE (file);
2051 return (* iface->append_to_finish) (file, res, error);
2055 * g_file_create_async:
2056 * @file: input #GFile
2057 * @flags: a set of #GFileCreateFlags
2058 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2060 * @cancellable: (allow-none): optional #GCancellable object,
2062 * @callback: (scope async): a #GAsyncReadyCallback to call
2063 * when the request is satisfied
2064 * @user_data: (closure): the data to pass to callback function
2066 * Asynchronously creates a new file and returns an output stream
2067 * for writing to it. The file must not already exist.
2069 * For more details, see g_file_create() which is
2070 * the synchronous version of this call.
2072 * When the operation is finished, @callback will be called.
2073 * You can then call g_file_create_finish() to get the result
2077 g_file_create_async (GFile *file,
2078 GFileCreateFlags flags,
2080 GCancellable *cancellable,
2081 GAsyncReadyCallback callback,
2086 g_return_if_fail (G_IS_FILE (file));
2088 iface = G_FILE_GET_IFACE (file);
2089 (* iface->create_async) (file,
2098 * g_file_create_finish:
2099 * @file: input #GFile
2100 * @res: a #GAsyncResult
2101 * @error: a #GError, or %NULL
2103 * Finishes an asynchronous file create operation started with
2104 * g_file_create_async().
2106 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2107 * Free the returned object with g_object_unref().
2110 g_file_create_finish (GFile *file,
2116 g_return_val_if_fail (G_IS_FILE (file), NULL);
2117 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2119 if (g_async_result_legacy_propagate_error (res, error))
2122 iface = G_FILE_GET_IFACE (file);
2123 return (* iface->create_finish) (file, res, error);
2127 * g_file_replace_async:
2128 * @file: input #GFile
2129 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2130 * for the current #GFile, or NULL to ignore
2131 * @make_backup: %TRUE if a backup should be created
2132 * @flags: a set of #GFileCreateFlags
2133 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2135 * @cancellable: (allow-none): optional #GCancellable object,
2137 * @callback: (scope async): a #GAsyncReadyCallback to call
2138 * when the request is satisfied
2139 * @user_data: (closure): the data to pass to callback function
2141 * Asynchronously overwrites the file, replacing the contents,
2142 * possibly creating a backup copy of the file first.
2144 * For more details, see g_file_replace() which is
2145 * the synchronous version of this call.
2147 * When the operation is finished, @callback will be called.
2148 * You can then call g_file_replace_finish() to get the result
2152 g_file_replace_async (GFile *file,
2154 gboolean make_backup,
2155 GFileCreateFlags flags,
2157 GCancellable *cancellable,
2158 GAsyncReadyCallback callback,
2163 g_return_if_fail (G_IS_FILE (file));
2165 iface = G_FILE_GET_IFACE (file);
2166 (* iface->replace_async) (file,
2177 * g_file_replace_finish:
2178 * @file: input #GFile
2179 * @res: a #GAsyncResult
2180 * @error: a #GError, or %NULL
2182 * Finishes an asynchronous file replace operation started with
2183 * g_file_replace_async().
2185 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2186 * Free the returned object with g_object_unref().
2189 g_file_replace_finish (GFile *file,
2195 g_return_val_if_fail (G_IS_FILE (file), NULL);
2196 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2198 if (g_async_result_legacy_propagate_error (res, error))
2201 iface = G_FILE_GET_IFACE (file);
2202 return (* iface->replace_finish) (file, res, error);
2206 * g_file_open_readwrite_async
2207 * @file: input #GFile
2208 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2210 * @cancellable: (allow-none): optional #GCancellable object,
2212 * @callback: (scope async): a #GAsyncReadyCallback to call
2213 * when the request is satisfied
2214 * @user_data: (closure): the data to pass to callback function
2216 * Asynchronously opens @file for reading and writing.
2218 * For more details, see g_file_open_readwrite() which is
2219 * the synchronous version of this call.
2221 * When the operation is finished, @callback will be called.
2222 * You can then call g_file_open_readwrite_finish() to get
2223 * the result of the operation.
2228 g_file_open_readwrite_async (GFile *file,
2230 GCancellable *cancellable,
2231 GAsyncReadyCallback callback,
2236 g_return_if_fail (G_IS_FILE (file));
2238 iface = G_FILE_GET_IFACE (file);
2239 (* iface->open_readwrite_async) (file,
2247 * g_file_open_readwrite_finish:
2248 * @file: input #GFile
2249 * @res: a #GAsyncResult
2250 * @error: a #GError, or %NULL
2252 * Finishes an asynchronous file read operation started with
2253 * g_file_open_readwrite_async().
2255 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2256 * Free the returned object with g_object_unref().
2261 g_file_open_readwrite_finish (GFile *file,
2267 g_return_val_if_fail (G_IS_FILE (file), NULL);
2268 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2270 if (g_async_result_legacy_propagate_error (res, error))
2273 iface = G_FILE_GET_IFACE (file);
2274 return (* iface->open_readwrite_finish) (file, res, error);
2278 * g_file_create_readwrite_async:
2279 * @file: input #GFile
2280 * @flags: a set of #GFileCreateFlags
2281 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2283 * @cancellable: (allow-none): optional #GCancellable object,
2285 * @callback: (scope async): a #GAsyncReadyCallback to call
2286 * when the request is satisfied
2287 * @user_data: (closure): the data to pass to callback function
2289 * Asynchronously creates a new file and returns a stream
2290 * for reading and writing to it. The file must not already exist.
2292 * For more details, see g_file_create_readwrite() which is
2293 * the synchronous version of this call.
2295 * When the operation is finished, @callback will be called.
2296 * You can then call g_file_create_readwrite_finish() to get
2297 * the result of the operation.
2302 g_file_create_readwrite_async (GFile *file,
2303 GFileCreateFlags flags,
2305 GCancellable *cancellable,
2306 GAsyncReadyCallback callback,
2311 g_return_if_fail (G_IS_FILE (file));
2313 iface = G_FILE_GET_IFACE (file);
2314 (* iface->create_readwrite_async) (file,
2323 * g_file_create_readwrite_finish:
2324 * @file: input #GFile
2325 * @res: a #GAsyncResult
2326 * @error: a #GError, or %NULL
2328 * Finishes an asynchronous file create operation started with
2329 * g_file_create_readwrite_async().
2331 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2332 * Free the returned object with g_object_unref().
2337 g_file_create_readwrite_finish (GFile *file,
2343 g_return_val_if_fail (G_IS_FILE (file), NULL);
2344 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2346 if (g_async_result_legacy_propagate_error (res, error))
2349 iface = G_FILE_GET_IFACE (file);
2350 return (* iface->create_readwrite_finish) (file, res, error);
2354 * g_file_replace_readwrite_async:
2355 * @file: input #GFile
2356 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2357 * for the current #GFile, or NULL to ignore
2358 * @make_backup: %TRUE if a backup should be created
2359 * @flags: a set of #GFileCreateFlags
2360 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2362 * @cancellable: (allow-none): optional #GCancellable object,
2364 * @callback: (scope async): a #GAsyncReadyCallback to call
2365 * when the request is satisfied
2366 * @user_data: (closure): the data to pass to callback function
2368 * Asynchronously overwrites the file in read-write mode,
2369 * replacing the contents, possibly creating a backup copy
2370 * of the file first.
2372 * For more details, see g_file_replace_readwrite() which is
2373 * the synchronous version of this call.
2375 * When the operation is finished, @callback will be called.
2376 * You can then call g_file_replace_readwrite_finish() to get
2377 * the result of the operation.
2382 g_file_replace_readwrite_async (GFile *file,
2384 gboolean make_backup,
2385 GFileCreateFlags flags,
2387 GCancellable *cancellable,
2388 GAsyncReadyCallback callback,
2393 g_return_if_fail (G_IS_FILE (file));
2395 iface = G_FILE_GET_IFACE (file);
2396 (* iface->replace_readwrite_async) (file,
2407 * g_file_replace_readwrite_finish:
2408 * @file: input #GFile
2409 * @res: a #GAsyncResult
2410 * @error: a #GError, or %NULL
2412 * Finishes an asynchronous file replace operation started with
2413 * g_file_replace_readwrite_async().
2415 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2416 * Free the returned object with g_object_unref().
2421 g_file_replace_readwrite_finish (GFile *file,
2427 g_return_val_if_fail (G_IS_FILE (file), NULL);
2428 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2430 if (g_async_result_legacy_propagate_error (res, error))
2433 iface = G_FILE_GET_IFACE (file);
2434 return (* iface->replace_readwrite_finish) (file, res, error);
2438 copy_symlink (GFile *destination,
2439 GFileCopyFlags flags,
2440 GCancellable *cancellable,
2445 gboolean tried_delete;
2447 GFileType file_type;
2449 tried_delete = FALSE;
2453 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2455 /* Maybe it already existed, and we want to overwrite? */
2456 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2457 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2459 g_error_free (my_error);
2461 /* Don't overwrite if the destination is a directory */
2462 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2463 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2464 cancellable, &my_error);
2467 file_type = g_file_info_get_file_type (info);
2468 g_object_unref (info);
2470 if (file_type == G_FILE_TYPE_DIRECTORY)
2472 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2473 _("Can't copy over directory"));
2478 if (!g_file_delete (destination, cancellable, error))
2481 tried_delete = TRUE;
2485 g_propagate_error (error, my_error);
2492 static GInputStream *
2493 open_source_for_copy (GFile *source,
2495 GFileCopyFlags flags,
2496 GCancellable *cancellable,
2502 GFileType file_type;
2505 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2509 /* There was an error opening the source, try to set a good error for it: */
2510 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2512 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2513 * as that is less useful to the app. Better check for errors on the
2516 g_error_free (my_error);
2519 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2520 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2521 cancellable, &my_error);
2523 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2525 file_type = g_file_info_get_file_type (info);
2526 g_object_unref (info);
2528 if (flags & G_FILE_COPY_OVERWRITE)
2530 if (file_type == G_FILE_TYPE_DIRECTORY)
2532 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2533 _("Can't copy directory over directory"));
2536 /* continue to would_recurse error */
2540 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2541 _("Target file exists"));
2547 /* Error getting info from target, return that error
2548 * (except for NOT_FOUND, which is no error here)
2550 g_clear_object (&info);
2551 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2553 g_propagate_error (error, my_error);
2556 g_clear_error (&my_error);
2559 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2560 _("Can't recursively copy directory"));
2564 g_propagate_error (error, my_error);
2569 should_copy (GFileAttributeInfo *info,
2571 gboolean skip_perms)
2573 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2577 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2578 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2582 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2583 GFileAttributeInfoList *namespaces,
2585 gboolean skip_perms)
2592 s = g_string_new ("");
2596 for (i = 0; i < attributes->n_infos; i++)
2598 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2603 g_string_append_c (s, ',');
2605 g_string_append (s, attributes->infos[i].name);
2612 for (i = 0; i < namespaces->n_infos; i++)
2614 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2619 g_string_append_c (s, ',');
2621 g_string_append (s, namespaces->infos[i].name);
2622 g_string_append (s, "::*");
2627 return g_string_free (s, FALSE);
2631 * g_file_copy_attributes:
2632 * @source: a #GFile with attributes
2633 * @destination: a #GFile to copy attributes to
2634 * @flags: a set of #GFileCopyFlags
2635 * @cancellable: (allow-none): optional #GCancellable object,
2637 * @error: a #GError, %NULL to ignore
2639 * Copies the file attributes from @source to @destination.
2641 * Normally only a subset of the file attributes are copied,
2642 * those that are copies in a normal file copy operation
2643 * (which for instance does not include e.g. owner). However
2644 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2645 * all the metadata that is possible to copy is copied. This
2646 * is useful when implementing move by copy + delete source.
2648 * Returns: %TRUE if the attributes were copied successfully,
2652 g_file_copy_attributes (GFile *source,
2654 GFileCopyFlags flags,
2655 GCancellable *cancellable,
2658 GFileAttributeInfoList *attributes, *namespaces;
2659 char *attrs_to_read;
2663 gboolean source_nofollow_symlinks;
2664 gboolean skip_perms;
2666 as_move = flags & G_FILE_COPY_ALL_METADATA;
2667 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2668 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2670 /* Ignore errors here, if the target supports no attributes there is
2673 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2674 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2676 if (attributes == NULL && namespaces == NULL)
2679 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2681 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2682 * we just don't copy it.
2684 info = g_file_query_info (source, attrs_to_read,
2685 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2689 g_free (attrs_to_read);
2694 res = g_file_set_attributes_from_info (destination,
2696 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2699 g_object_unref (info);
2702 g_file_attribute_info_list_unref (attributes);
2703 g_file_attribute_info_list_unref (namespaces);
2709 copy_stream_with_progress (GInputStream *in,
2712 GCancellable *cancellable,
2713 GFileProgressCallback progress_callback,
2714 gpointer progress_callback_data,
2717 gssize n_read, n_written;
2718 goffset current_size;
2719 char buffer[1024*64], *p;
2725 /* avoid performance impact of querying total size when it's not needed */
2726 if (progress_callback)
2728 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2729 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2733 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2734 total_size = g_file_info_get_size (info);
2735 g_object_unref (info);
2738 if (total_size == -1)
2740 info = g_file_query_info (source,
2741 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2742 G_FILE_QUERY_INFO_NONE,
2746 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2747 total_size = g_file_info_get_size (info);
2748 g_object_unref (info);
2753 if (total_size == -1)
2760 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2770 current_size += n_read;
2775 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2776 if (n_written == -1)
2783 n_read -= n_written;
2789 if (progress_callback)
2790 progress_callback (current_size, total_size, progress_callback_data);
2793 /* Make sure we send full copied size */
2794 if (progress_callback)
2795 progress_callback (current_size, total_size, progress_callback_data);
2803 do_splice (int fd_in,
2808 long *bytes_transferd,
2814 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2822 else if (errsv == ENOSYS || errsv == EINVAL)
2823 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2824 _("Splice not supported"));
2826 g_set_error (error, G_IO_ERROR,
2827 g_io_error_from_errno (errsv),
2828 _("Error splicing file: %s"),
2829 g_strerror (errsv));
2834 *bytes_transferd = result;
2839 splice_stream_with_progress (GInputStream *in,
2841 GCancellable *cancellable,
2842 GFileProgressCallback progress_callback,
2843 gpointer progress_callback_data,
2853 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2854 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2856 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2860 /* avoid performance impact of querying total size when it's not needed */
2861 if (progress_callback)
2865 if (fstat (fd_in, &sbuf) == 0)
2866 total_size = sbuf.st_size;
2869 if (total_size == -1)
2872 offset_in = offset_out = 0;
2879 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2882 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2896 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2899 n_read -= n_written;
2902 if (progress_callback)
2903 progress_callback (offset_in, total_size, progress_callback_data);
2906 /* Make sure we send full copied size */
2907 if (progress_callback)
2908 progress_callback (offset_in, total_size, progress_callback_data);
2918 #ifdef HAVE_SYS_IOCTL_H
2920 btrfs_reflink_with_progress (GInputStream *in,
2923 GCancellable *cancellable,
2924 GFileProgressCallback progress_callback,
2925 gpointer progress_callback_data,
2928 goffset source_size;
2932 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2933 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2935 if (progress_callback)
2936 source_size = g_file_info_get_size (info);
2938 /* Btrfs clone ioctl properties:
2939 * - Works at the inode level
2940 * - Doesn't work with directories
2941 * - Always follows symlinks (source and destination)
2943 * By the time we get here, *in and *out are both regular files */
2944 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
2949 g_set_error_literal (error, G_IO_ERROR,
2950 G_IO_ERROR_NOT_SUPPORTED,
2951 _("Copy (reflink/clone) between mounts is not supported"));
2952 else if (errno == EINVAL)
2953 g_set_error_literal (error, G_IO_ERROR,
2954 G_IO_ERROR_NOT_SUPPORTED,
2955 _("Copy (reflink/clone) is not supported or invalid"));
2957 /* Most probably something odd happened; retry with fallback */
2958 g_set_error_literal (error, G_IO_ERROR,
2959 G_IO_ERROR_NOT_SUPPORTED,
2960 _("Copy (reflink/clone) is not supported or didn't work"));
2961 /* We retry with fallback for all error cases because Btrfs is currently
2962 * unstable, and so we can't trust it to do clone properly.
2963 * In addition, any hard errors here would cause the same failure in the
2964 * fallback manual copy as well. */
2968 /* Make sure we send full copied size */
2969 if (progress_callback)
2970 progress_callback (source_size, source_size, progress_callback_data);
2977 file_copy_fallback (GFile *source,
2979 GFileCopyFlags flags,
2980 GCancellable *cancellable,
2981 GFileProgressCallback progress_callback,
2982 gpointer progress_callback_data,
2985 gboolean ret = FALSE;
2986 GInputStream *in = NULL;
2987 GOutputStream *out = NULL;
2988 GFileInfo *info = NULL;
2991 /* need to know the file type */
2992 info = g_file_query_info (source,
2993 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2994 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3000 /* Maybe copy the symlink? */
3001 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3002 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3004 target = g_file_info_get_symlink_target (info);
3007 if (!copy_symlink (destination, flags, cancellable, target, error))
3013 /* ... else fall back on a regular file copy */
3015 /* Handle "special" files (pipes, device nodes, ...)? */
3016 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3018 /* FIXME: could try to recreate device nodes and others? */
3019 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3020 _("Can't copy special file"));
3024 /* Everything else should just fall back on a regular copy. */
3026 in = open_source_for_copy (source, destination, flags, cancellable, error);
3030 if (flags & G_FILE_COPY_OVERWRITE)
3032 out = (GOutputStream *)g_file_replace (destination,
3034 flags & G_FILE_COPY_BACKUP,
3035 G_FILE_CREATE_REPLACE_DESTINATION,
3036 cancellable, error);
3040 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3046 #ifdef HAVE_SYS_IOCTL_H
3047 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3049 GError *reflink_err = NULL;
3051 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3052 progress_callback, progress_callback_data,
3055 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3057 g_clear_error (&reflink_err);
3061 g_propagate_error (error, reflink_err);
3074 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3076 GError *splice_err = NULL;
3078 if (!splice_stream_with_progress (in, out, cancellable,
3079 progress_callback, progress_callback_data,
3082 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3084 g_clear_error (&splice_err);
3088 g_propagate_error (error, splice_err);
3101 /* A plain read/write loop */
3102 if (!copy_stream_with_progress (in, out, source, cancellable,
3103 progress_callback, progress_callback_data,
3109 /* Ignore errors here. Failure to copy metadata is not a hard error */
3111 (void) g_file_copy_attributes (source, destination,
3112 flags, cancellable, NULL);
3116 /* Don't care about errors in source here */
3117 (void) g_input_stream_close (in, cancellable, NULL);
3118 g_object_unref (in);
3123 /* But write errors on close are bad! */
3124 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3126 g_object_unref (out);
3129 g_clear_object (&info);
3136 * @source: input #GFile
3137 * @destination: destination #GFile
3138 * @flags: set of #GFileCopyFlags
3139 * @cancellable: (allow-none): optional #GCancellable object,
3141 * @progress_callback: (allow-none) (scope call): function to callback with
3142 * progress information, or %NULL if progress information is not needed
3143 * @progress_callback_data: (closure): user data to pass to @progress_callback
3144 * @error: #GError to set on error, or %NULL
3146 * Copies the file @source to the location specified by @destination.
3147 * Can not handle recursive copies of directories.
3149 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3150 * existing @destination file is overwritten.
3152 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3153 * will be copied as symlinks, otherwise the target of the
3154 * @source symlink will be copied.
3156 * If @cancellable is not %NULL, then the operation can be cancelled by
3157 * triggering the cancellable object from another thread. If the operation
3158 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3160 * If @progress_callback is not %NULL, then the operation can be monitored
3161 * by setting this to a #GFileProgressCallback function.
3162 * @progress_callback_data will be passed to this function. It is guaranteed
3163 * that this callback will be called after all data has been transferred with
3164 * the total number of bytes copied during the operation.
3166 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3167 * is returned, independent on the status of the @destination.
3169 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3170 * the error %G_IO_ERROR_EXISTS is returned.
3172 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3173 * error is returned. If trying to overwrite a directory with a directory the
3174 * %G_IO_ERROR_WOULD_MERGE error is returned.
3176 * If the source is a directory and the target does not exist, or
3177 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3178 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3180 * If you are interested in copying the #GFile object itself (not the on-disk
3181 * file), see g_file_dup().
3183 * Returns: %TRUE on success, %FALSE otherwise.
3186 g_file_copy (GFile *source,
3188 GFileCopyFlags flags,
3189 GCancellable *cancellable,
3190 GFileProgressCallback progress_callback,
3191 gpointer progress_callback_data,
3198 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3199 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3201 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3204 iface = G_FILE_GET_IFACE (destination);
3208 res = (* iface->copy) (source, destination,
3210 progress_callback, progress_callback_data,
3216 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3218 g_propagate_error (error, my_error);
3222 g_clear_error (&my_error);
3225 /* If the types are different, and the destination method failed
3226 * also try the source method
3228 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3230 iface = G_FILE_GET_IFACE (source);
3235 res = (* iface->copy) (source, destination,
3237 progress_callback, progress_callback_data,
3243 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3245 g_propagate_error (error, my_error);
3249 g_clear_error (&my_error);
3253 return file_copy_fallback (source, destination, flags, cancellable,
3254 progress_callback, progress_callback_data,
3259 * g_file_copy_async: (skip)
3260 * @source: input #GFile
3261 * @destination: destination #GFile
3262 * @flags: set of #GFileCopyFlags
3263 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3265 * @cancellable: (allow-none): optional #GCancellable object,
3267 * @progress_callback: (allow-none): function to callback with progress
3268 * information, or %NULL if progress information is not needed
3269 * @progress_callback_data: (closure): user data to pass to @progress_callback
3270 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3271 * @user_data: the data to pass to callback function
3273 * Copies the file @source to the location specified by @destination
3274 * asynchronously. For details of the behaviour, see g_file_copy().
3276 * If @progress_callback is not %NULL, then that function that will be called
3277 * just like in g_file_copy(), however the callback will run in the main loop,
3278 * not in the thread that is doing the I/O operation.
3280 * When the operation is finished, @callback will be called. You can then call
3281 * g_file_copy_finish() to get the result of the operation.
3284 g_file_copy_async (GFile *source,
3286 GFileCopyFlags flags,
3288 GCancellable *cancellable,
3289 GFileProgressCallback progress_callback,
3290 gpointer progress_callback_data,
3291 GAsyncReadyCallback callback,
3296 g_return_if_fail (G_IS_FILE (source));
3297 g_return_if_fail (G_IS_FILE (destination));
3299 iface = G_FILE_GET_IFACE (source);
3300 (* iface->copy_async) (source,
3306 progress_callback_data,
3312 * g_file_copy_finish:
3313 * @file: input #GFile
3314 * @res: a #GAsyncResult
3315 * @error: a #GError, or %NULL
3317 * Finishes copying the file started with g_file_copy_async().
3319 * Returns: a %TRUE on success, %FALSE on error.
3322 g_file_copy_finish (GFile *file,
3328 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3329 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3331 if (g_async_result_legacy_propagate_error (res, error))
3334 iface = G_FILE_GET_IFACE (file);
3335 return (* iface->copy_finish) (file, res, error);
3340 * @source: #GFile pointing to the source location
3341 * @destination: #GFile pointing to the destination location
3342 * @flags: set of #GFileCopyFlags
3343 * @cancellable: (allow-none): optional #GCancellable object,
3345 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3346 * function for updates
3347 * @progress_callback_data: (closure): gpointer to user data for
3348 * the callback function
3349 * @error: #GError for returning error conditions, or %NULL
3351 * Tries to move the file or directory @source to the location specified
3352 * by @destination. If native move operations are supported then this is
3353 * used, otherwise a copy + delete fallback is used. The native
3354 * implementation may support moving directories (for instance on moves
3355 * inside the same filesystem), but the fallback code does not.
3357 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3358 * existing @destination file is overwritten.
3360 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3361 * will be copied as symlinks, otherwise the target of the
3362 * @source symlink will be copied.
3364 * If @cancellable is not %NULL, then the operation can be cancelled by
3365 * triggering the cancellable object from another thread. If the operation
3366 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3368 * If @progress_callback is not %NULL, then the operation can be monitored
3369 * by setting this to a #GFileProgressCallback function.
3370 * @progress_callback_data will be passed to this function. It is
3371 * guaranteed that this callback will be called after all data has been
3372 * transferred with the total number of bytes copied during the operation.
3374 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3375 * error is returned, independent on the status of the @destination.
3377 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3378 * then the error %G_IO_ERROR_EXISTS is returned.
3380 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3381 * error is returned. If trying to overwrite a directory with a directory the
3382 * %G_IO_ERROR_WOULD_MERGE error is returned.
3384 * If the source is a directory and the target does not exist, or
3385 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3386 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3387 * move operation isn't available).
3389 * Returns: %TRUE on successful move, %FALSE otherwise.
3392 g_file_move (GFile *source,
3394 GFileCopyFlags flags,
3395 GCancellable *cancellable,
3396 GFileProgressCallback progress_callback,
3397 gpointer progress_callback_data,
3404 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3405 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3407 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3410 iface = G_FILE_GET_IFACE (destination);
3414 res = (* iface->move) (source, destination,
3416 progress_callback, progress_callback_data,
3422 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3424 g_propagate_error (error, my_error);
3429 /* If the types are different, and the destination method failed
3430 * also try the source method
3432 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3434 iface = G_FILE_GET_IFACE (source);
3439 res = (* iface->move) (source, destination,
3441 progress_callback, progress_callback_data,
3447 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3449 g_propagate_error (error, my_error);
3455 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3457 g_set_error_literal (error, G_IO_ERROR,
3458 G_IO_ERROR_NOT_SUPPORTED,
3459 _("Operation not supported"));
3463 flags |= G_FILE_COPY_ALL_METADATA;
3464 if (!g_file_copy (source, destination, flags, cancellable,
3465 progress_callback, progress_callback_data,
3469 return g_file_delete (source, cancellable, error);
3473 * g_file_make_directory:
3474 * @file: input #GFile
3475 * @cancellable: (allow-none): optional #GCancellable object,
3477 * @error: a #GError, or %NULL
3479 * Creates a directory. Note that this will only create a child directory
3480 * of the immediate parent directory of the path or URI given by the #GFile.
3481 * To recursively create directories, see g_file_make_directory_with_parents().
3482 * This function will fail if the parent directory does not exist, setting
3483 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3484 * creating directories, this function will fail, setting @error to
3485 * %G_IO_ERROR_NOT_SUPPORTED.
3487 * For a local #GFile the newly created directory will have the default
3488 * (current) ownership and permissions of the current process.
3490 * If @cancellable is not %NULL, then the operation can be cancelled by
3491 * triggering the cancellable object from another thread. If the operation
3492 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3494 * Returns: %TRUE on successful creation, %FALSE otherwise.
3497 g_file_make_directory (GFile *file,
3498 GCancellable *cancellable,
3503 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3505 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3508 iface = G_FILE_GET_IFACE (file);
3510 if (iface->make_directory == NULL)
3512 g_set_error_literal (error, G_IO_ERROR,
3513 G_IO_ERROR_NOT_SUPPORTED,
3514 _("Operation not supported"));
3518 return (* iface->make_directory) (file, cancellable, error);
3522 * g_file_make_directory_with_parents:
3523 * @file: input #GFile
3524 * @cancellable: (allow-none): optional #GCancellable object,
3526 * @error: a #GError, or %NULL
3528 * Creates a directory and any parent directories that may not
3529 * exist similar to 'mkdir -p'. If the file system does not support
3530 * creating directories, this function will fail, setting @error to
3531 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3532 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3533 * the similar g_mkdir_with_parents().
3535 * For a local #GFile the newly created directories will have the default
3536 * (current) ownership and permissions of the current process.
3538 * If @cancellable is not %NULL, then the operation can be cancelled by
3539 * triggering the cancellable object from another thread. If the operation
3540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3542 * Returns: %TRUE if all directories have been successfully created, %FALSE
3548 g_file_make_directory_with_parents (GFile *file,
3549 GCancellable *cancellable,
3552 GFile *work_file = NULL;
3553 GList *list = NULL, *l;
3554 GError *my_error = NULL;
3556 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3558 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3561 g_file_make_directory (file, cancellable, &my_error);
3562 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3565 g_propagate_error (error, my_error);
3566 return my_error == NULL;
3569 work_file = g_object_ref (file);
3571 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3575 parent_file = g_file_get_parent (work_file);
3576 if (parent_file == NULL)
3579 g_clear_error (&my_error);
3580 g_file_make_directory (parent_file, cancellable, &my_error);
3582 g_object_unref (work_file);
3583 work_file = g_object_ref (parent_file);
3585 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3586 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3588 g_object_unref (parent_file);
3591 for (l = list; my_error == NULL && l; l = l->next)
3593 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3597 g_object_unref (work_file);
3600 while (list != NULL)
3602 g_object_unref ((GFile *) list->data);
3603 list = g_list_remove (list, list->data);
3606 if (my_error != NULL)
3608 g_propagate_error (error, my_error);
3612 return g_file_make_directory (file, cancellable, error);
3616 * g_file_make_symbolic_link:
3617 * @file: a #GFile with the name of the symlink to create
3618 * @symlink_value: a string with the path for the target of the new symlink
3619 * @cancellable: (allow-none): optional #GCancellable object,
3623 * Creates a symbolic link named @file which contains the string
3626 * If @cancellable is not %NULL, then the operation can be cancelled by
3627 * triggering the cancellable object from another thread. If the operation
3628 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3630 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3633 g_file_make_symbolic_link (GFile *file,
3634 const char *symlink_value,
3635 GCancellable *cancellable,
3640 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3641 g_return_val_if_fail (symlink_value != NULL, FALSE);
3643 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3646 if (*symlink_value == '\0')
3648 g_set_error_literal (error, G_IO_ERROR,
3649 G_IO_ERROR_INVALID_ARGUMENT,
3650 _("Invalid symlink value given"));
3654 iface = G_FILE_GET_IFACE (file);
3656 if (iface->make_symbolic_link == NULL)
3658 g_set_error_literal (error, G_IO_ERROR,
3659 G_IO_ERROR_NOT_SUPPORTED,
3660 _("Operation not supported"));
3664 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3669 * @file: input #GFile
3670 * @cancellable: (allow-none): optional #GCancellable object,
3672 * @error: a #GError, or %NULL
3674 * Deletes a file. If the @file is a directory, it will only be
3675 * deleted if it is empty. This has the same semantics as g_unlink().
3677 * If @cancellable is not %NULL, then the operation can be cancelled by
3678 * triggering the cancellable object from another thread. If the operation
3679 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3681 * Virtual: delete_file
3682 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3685 g_file_delete (GFile *file,
3686 GCancellable *cancellable,
3691 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3693 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3696 iface = G_FILE_GET_IFACE (file);
3698 if (iface->delete_file == NULL)
3700 g_set_error_literal (error, G_IO_ERROR,
3701 G_IO_ERROR_NOT_SUPPORTED,
3702 _("Operation not supported"));
3706 return (* iface->delete_file) (file, cancellable, error);
3710 * g_file_delete_async:
3711 * @file: input #GFile
3712 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3714 * @cancellable: (allow-none): optional #GCancellable object,
3716 * @callback: a #GAsyncReadyCallback to call
3717 * when the request is satisfied
3718 * @user_data: the data to pass to callback function
3720 * Asynchronously delete a file. If the @file is a directory, it will
3721 * only be deleted if it is empty. This has the same semantics as
3724 * Virtual: delete_file_async
3728 g_file_delete_async (GFile *file,
3730 GCancellable *cancellable,
3731 GAsyncReadyCallback callback,
3736 g_return_if_fail (G_IS_FILE (file));
3738 iface = G_FILE_GET_IFACE (file);
3739 (* iface->delete_file_async) (file,
3747 * g_file_delete_finish:
3748 * @file: input #GFile
3749 * @result: a #GAsyncResult
3750 * @error: a #GError, or %NULL
3752 * Finishes deleting a file started with g_file_delete_async().
3754 * Virtual: delete_file_finish
3758 g_file_delete_finish (GFile *file,
3759 GAsyncResult *result,
3764 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3765 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3767 if (g_async_result_legacy_propagate_error (result, error))
3770 iface = G_FILE_GET_IFACE (file);
3771 return (* iface->delete_file_finish) (file, result, error);
3776 * @file: #GFile to send to trash
3777 * @cancellable: (allow-none): optional #GCancellable object,
3779 * @error: a #GError, or %NULL
3781 * Sends @file to the "Trashcan", if possible. This is similar to
3782 * deleting it, but the user can recover it before emptying the trashcan.
3783 * Not all file systems support trashing, so this call can return the
3784 * %G_IO_ERROR_NOT_SUPPORTED error.
3786 * If @cancellable is not %NULL, then the operation can be cancelled by
3787 * triggering the cancellable object from another thread. If the operation
3788 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3790 * Returns: %TRUE on successful trash, %FALSE otherwise.
3793 g_file_trash (GFile *file,
3794 GCancellable *cancellable,
3799 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3801 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3804 iface = G_FILE_GET_IFACE (file);
3806 if (iface->trash == NULL)
3808 g_set_error_literal (error,
3809 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3810 _("Trash not supported"));
3814 return (* iface->trash) (file, cancellable, error);
3818 * g_file_set_display_name:
3819 * @file: input #GFile
3820 * @display_name: a string
3821 * @cancellable: (allow-none): optional #GCancellable object,
3823 * @error: a #GError, or %NULL
3825 * Renames @file to the specified display name.
3827 * The display name is converted from UTF-8 to the correct encoding
3828 * for the target filesystem if possible and the @file is renamed to this.
3830 * If you want to implement a rename operation in the user interface the
3831 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3832 * initial value in the rename widget, and then the result after editing
3833 * should be passed to g_file_set_display_name().
3835 * On success the resulting converted filename is returned.
3837 * If @cancellable is not %NULL, then the operation can be cancelled by
3838 * triggering the cancellable object from another thread. If the operation
3839 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3841 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3842 * or %NULL if there was an error.
3843 * Free the returned object with g_object_unref().
3846 g_file_set_display_name (GFile *file,
3847 const gchar *display_name,
3848 GCancellable *cancellable,
3853 g_return_val_if_fail (G_IS_FILE (file), NULL);
3854 g_return_val_if_fail (display_name != NULL, NULL);
3856 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3860 G_IO_ERROR_INVALID_ARGUMENT,
3861 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3865 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3868 iface = G_FILE_GET_IFACE (file);
3870 return (* iface->set_display_name) (file, display_name, cancellable, error);
3874 * g_file_set_display_name_async:
3875 * @file: input #GFile
3876 * @display_name: a string
3877 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3879 * @cancellable: (allow-none): optional #GCancellable object,
3881 * @callback: (scope async): a #GAsyncReadyCallback to call
3882 * when the request is satisfied
3883 * @user_data: (closure): the data to pass to callback function
3885 * Asynchronously sets the display name for a given #GFile.
3887 * For more details, see g_file_set_display_name() which is
3888 * the synchronous version of this call.
3890 * When the operation is finished, @callback will be called.
3891 * You can then call g_file_set_display_name_finish() to get
3892 * the result of the operation.
3895 g_file_set_display_name_async (GFile *file,
3896 const gchar *display_name,
3898 GCancellable *cancellable,
3899 GAsyncReadyCallback callback,
3904 g_return_if_fail (G_IS_FILE (file));
3905 g_return_if_fail (display_name != NULL);
3907 iface = G_FILE_GET_IFACE (file);
3908 (* iface->set_display_name_async) (file,
3917 * g_file_set_display_name_finish:
3918 * @file: input #GFile
3919 * @res: a #GAsyncResult
3920 * @error: a #GError, or %NULL
3922 * Finishes setting a display name started with
3923 * g_file_set_display_name_async().
3925 * Returns: (transfer full): a #GFile or %NULL on error.
3926 * Free the returned object with g_object_unref().
3929 g_file_set_display_name_finish (GFile *file,
3935 g_return_val_if_fail (G_IS_FILE (file), NULL);
3936 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3938 if (g_async_result_legacy_propagate_error (res, error))
3941 iface = G_FILE_GET_IFACE (file);
3942 return (* iface->set_display_name_finish) (file, res, error);
3946 * g_file_query_settable_attributes:
3947 * @file: input #GFile
3948 * @cancellable: (allow-none): optional #GCancellable object,
3950 * @error: a #GError, or %NULL
3952 * Obtain the list of settable attributes for the file.
3954 * Returns the type and full attribute name of all the attributes
3955 * that can be set on this file. This doesn't mean setting it will
3956 * always succeed though, you might get an access failure, or some
3957 * specific file may not support a specific attribute.
3959 * If @cancellable is not %NULL, then the operation can be cancelled by
3960 * triggering the cancellable object from another thread. If the operation
3961 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3963 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3964 * When you are done with it, release it with
3965 * g_file_attribute_info_list_unref()
3967 GFileAttributeInfoList *
3968 g_file_query_settable_attributes (GFile *file,
3969 GCancellable *cancellable,
3974 GFileAttributeInfoList *list;
3976 g_return_val_if_fail (G_IS_FILE (file), NULL);
3978 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3981 iface = G_FILE_GET_IFACE (file);
3983 if (iface->query_settable_attributes == NULL)
3984 return g_file_attribute_info_list_new ();
3987 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3991 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3993 list = g_file_attribute_info_list_new ();
3994 g_error_free (my_error);
3997 g_propagate_error (error, my_error);
4004 * g_file_query_writable_namespaces:
4005 * @file: input #GFile
4006 * @cancellable: (allow-none): optional #GCancellable object,
4008 * @error: a #GError, or %NULL
4010 * Obtain the list of attribute namespaces where new attributes
4011 * can be created by a user. An example of this is extended
4012 * attributes (in the "xattr" namespace).
4014 * If @cancellable is not %NULL, then the operation can be cancelled by
4015 * triggering the cancellable object from another thread. If the operation
4016 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4018 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4019 * When you are done with it, release it with
4020 * g_file_attribute_info_list_unref()
4022 GFileAttributeInfoList *
4023 g_file_query_writable_namespaces (GFile *file,
4024 GCancellable *cancellable,
4029 GFileAttributeInfoList *list;
4031 g_return_val_if_fail (G_IS_FILE (file), NULL);
4033 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4036 iface = G_FILE_GET_IFACE (file);
4038 if (iface->query_writable_namespaces == NULL)
4039 return g_file_attribute_info_list_new ();
4042 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4046 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4048 list = g_file_attribute_info_list_new ();
4049 g_error_free (my_error);
4052 g_propagate_error (error, my_error);
4059 * g_file_set_attribute:
4060 * @file: input #GFile
4061 * @attribute: a string containing the attribute's name
4062 * @type: The type of the attribute
4063 * @value_p: (allow-none): a pointer to the value (or the pointer
4064 * itself if the type is a pointer type)
4065 * @flags: a set of #GFileQueryInfoFlags
4066 * @cancellable: (allow-none): optional #GCancellable object,
4068 * @error: a #GError, or %NULL
4070 * Sets an attribute in the file with attribute name @attribute to @value.
4072 * Some attributes can be unset by setting @attribute to
4073 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4075 * If @cancellable is not %NULL, then the operation can be cancelled by
4076 * triggering the cancellable object from another thread. If the operation
4077 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4079 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4082 g_file_set_attribute (GFile *file,
4083 const gchar *attribute,
4084 GFileAttributeType type,
4086 GFileQueryInfoFlags flags,
4087 GCancellable *cancellable,
4092 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4093 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4095 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4098 iface = G_FILE_GET_IFACE (file);
4100 if (iface->set_attribute == NULL)
4102 g_set_error_literal (error, G_IO_ERROR,
4103 G_IO_ERROR_NOT_SUPPORTED,
4104 _("Operation not supported"));
4108 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4112 * g_file_set_attributes_from_info:
4113 * @file: input #GFile
4114 * @info: a #GFileInfo
4115 * @flags: #GFileQueryInfoFlags
4116 * @cancellable: (allow-none): optional #GCancellable object,
4118 * @error: a #GError, or %NULL
4120 * Tries to set all attributes in the #GFileInfo on the target
4121 * values, not stopping on the first error.
4123 * If there is any error during this operation then @error will
4124 * be set to the first error. Error on particular fields are flagged
4125 * by setting the "status" field in the attribute value to
4126 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4127 * also detect further errors.
4129 * If @cancellable is not %NULL, then the operation can be cancelled by
4130 * triggering the cancellable object from another thread. If the operation
4131 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4133 * Returns: %FALSE if there was any error, %TRUE otherwise.
4136 g_file_set_attributes_from_info (GFile *file,
4138 GFileQueryInfoFlags flags,
4139 GCancellable *cancellable,
4144 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4145 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4147 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4150 g_file_info_clear_status (info);
4152 iface = G_FILE_GET_IFACE (file);
4154 return (* iface->set_attributes_from_info) (file,
4162 g_file_real_set_attributes_from_info (GFile *file,
4164 GFileQueryInfoFlags flags,
4165 GCancellable *cancellable,
4171 GFileAttributeValue *value;
4175 attributes = g_file_info_list_attributes (info, NULL);
4177 for (i = 0; attributes[i] != NULL; i++)
4179 value = _g_file_info_get_attribute_value (info, attributes[i]);
4181 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4184 if (!g_file_set_attribute (file, attributes[i],
4185 value->type, _g_file_attribute_value_peek_as_pointer (value),
4186 flags, cancellable, error))
4188 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4190 /* Don't set error multiple times */
4194 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4197 g_strfreev (attributes);
4203 * g_file_set_attributes_async:
4204 * @file: input #GFile
4205 * @info: a #GFileInfo
4206 * @flags: a #GFileQueryInfoFlags
4207 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4209 * @cancellable: (allow-none): optional #GCancellable object,
4211 * @callback: (scope async): a #GAsyncReadyCallback
4212 * @user_data: (closure): a #gpointer
4214 * Asynchronously sets the attributes of @file with @info.
4216 * For more details, see g_file_set_attributes_from_info(),
4217 * which is the synchronous version of this call.
4219 * When the operation is finished, @callback will be called.
4220 * You can then call g_file_set_attributes_finish() to get
4221 * the result of the operation.
4224 g_file_set_attributes_async (GFile *file,
4226 GFileQueryInfoFlags flags,
4228 GCancellable *cancellable,
4229 GAsyncReadyCallback callback,
4234 g_return_if_fail (G_IS_FILE (file));
4235 g_return_if_fail (G_IS_FILE_INFO (info));
4237 iface = G_FILE_GET_IFACE (file);
4238 (* iface->set_attributes_async) (file,
4248 * g_file_set_attributes_finish:
4249 * @file: input #GFile
4250 * @result: a #GAsyncResult
4251 * @info: (out) (transfer full): a #GFileInfo
4252 * @error: a #GError, or %NULL
4254 * Finishes setting an attribute started in g_file_set_attributes_async().
4256 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4259 g_file_set_attributes_finish (GFile *file,
4260 GAsyncResult *result,
4266 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4267 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4269 /* No standard handling of errors here, as we must set info even
4272 iface = G_FILE_GET_IFACE (file);
4273 return (* iface->set_attributes_finish) (file, result, info, error);
4277 * g_file_set_attribute_string:
4278 * @file: input #GFile
4279 * @attribute: a string containing the attribute's name
4280 * @value: a string containing the attribute's value
4281 * @flags: #GFileQueryInfoFlags
4282 * @cancellable: (allow-none): optional #GCancellable object,
4284 * @error: a #GError, or %NULL
4286 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4287 * If @attribute is of a different type, this operation will fail.
4289 * If @cancellable is not %NULL, then the operation can be cancelled by
4290 * triggering the cancellable object from another thread. If the operation
4291 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4293 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4296 g_file_set_attribute_string (GFile *file,
4297 const char *attribute,
4299 GFileQueryInfoFlags flags,
4300 GCancellable *cancellable,
4303 return g_file_set_attribute (file, attribute,
4304 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4305 flags, cancellable, error);
4309 * g_file_set_attribute_byte_string:
4310 * @file: input #GFile
4311 * @attribute: a string containing the attribute's name
4312 * @value: a string containing the attribute's new value
4313 * @flags: a #GFileQueryInfoFlags
4314 * @cancellable: (allow-none): optional #GCancellable object,
4316 * @error: a #GError, or %NULL
4318 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4319 * If @attribute is of a different type, this operation will fail,
4322 * If @cancellable is not %NULL, then the operation can be cancelled by
4323 * triggering the cancellable object from another thread. If the operation
4324 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4326 * Returns: %TRUE if the @attribute was successfully set to @value
4327 * in the @file, %FALSE otherwise.
4330 g_file_set_attribute_byte_string (GFile *file,
4331 const gchar *attribute,
4333 GFileQueryInfoFlags flags,
4334 GCancellable *cancellable,
4337 return g_file_set_attribute (file, attribute,
4338 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4339 flags, cancellable, error);
4343 * g_file_set_attribute_uint32:
4344 * @file: input #GFile
4345 * @attribute: a string containing the attribute's name
4346 * @value: a #guint32 containing the attribute's new value
4347 * @flags: a #GFileQueryInfoFlags
4348 * @cancellable: (allow-none): optional #GCancellable object,
4350 * @error: a #GError, or %NULL
4352 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4353 * If @attribute is of a different type, this operation will fail.
4355 * If @cancellable is not %NULL, then the operation can be cancelled by
4356 * triggering the cancellable object from another thread. If the operation
4357 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4359 * Returns: %TRUE if the @attribute was successfully set to @value
4360 * in the @file, %FALSE otherwise.
4363 g_file_set_attribute_uint32 (GFile *file,
4364 const gchar *attribute,
4366 GFileQueryInfoFlags flags,
4367 GCancellable *cancellable,
4370 return g_file_set_attribute (file, attribute,
4371 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4372 flags, cancellable, error);
4376 * g_file_set_attribute_int32:
4377 * @file: input #GFile
4378 * @attribute: a string containing the attribute's name
4379 * @value: a #gint32 containing the attribute's new value
4380 * @flags: a #GFileQueryInfoFlags
4381 * @cancellable: (allow-none): optional #GCancellable object,
4383 * @error: a #GError, or %NULL
4385 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4386 * If @attribute is of a different type, this operation will fail.
4388 * If @cancellable is not %NULL, then the operation can be cancelled by
4389 * triggering the cancellable object from another thread. If the operation
4390 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4392 * Returns: %TRUE if the @attribute was successfully set to @value
4393 * in the @file, %FALSE otherwise.
4396 g_file_set_attribute_int32 (GFile *file,
4397 const gchar *attribute,
4399 GFileQueryInfoFlags flags,
4400 GCancellable *cancellable,
4403 return g_file_set_attribute (file, attribute,
4404 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4405 flags, cancellable, error);
4409 * g_file_set_attribute_uint64:
4410 * @file: input #GFile
4411 * @attribute: a string containing the attribute's name
4412 * @value: a #guint64 containing the attribute's new value
4413 * @flags: a #GFileQueryInfoFlags
4414 * @cancellable: (allow-none): optional #GCancellable object,
4416 * @error: a #GError, or %NULL
4418 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4419 * If @attribute is of a different type, this operation will fail.
4421 * If @cancellable is not %NULL, then the operation can be cancelled by
4422 * triggering the cancellable object from another thread. If the operation
4423 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4425 * Returns: %TRUE if the @attribute was successfully set to @value
4426 * in the @file, %FALSE otherwise.
4429 g_file_set_attribute_uint64 (GFile *file,
4430 const gchar *attribute,
4432 GFileQueryInfoFlags flags,
4433 GCancellable *cancellable,
4436 return g_file_set_attribute (file, attribute,
4437 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4438 flags, cancellable, error);
4442 * g_file_set_attribute_int64:
4443 * @file: input #GFile
4444 * @attribute: a string containing the attribute's name
4445 * @value: a #guint64 containing the attribute's new value
4446 * @flags: a #GFileQueryInfoFlags
4447 * @cancellable: (allow-none): optional #GCancellable object,
4449 * @error: a #GError, or %NULL
4451 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4452 * If @attribute is of a different type, this operation will fail.
4454 * If @cancellable is not %NULL, then the operation can be cancelled by
4455 * triggering the cancellable object from another thread. If the operation
4456 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4458 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4461 g_file_set_attribute_int64 (GFile *file,
4462 const gchar *attribute,
4464 GFileQueryInfoFlags flags,
4465 GCancellable *cancellable,
4468 return g_file_set_attribute (file, attribute,
4469 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4470 flags, cancellable, error);
4474 * g_file_mount_mountable:
4475 * @file: input #GFile
4476 * @flags: flags affecting the operation
4477 * @mount_operation: (allow-none): a #GMountOperation,
4478 * or %NULL to avoid user interaction
4479 * @cancellable: (allow-none): optional #GCancellable object,
4481 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4482 * when the request is satisfied, or %NULL
4483 * @user_data: (closure): the data to pass to callback function
4485 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4486 * Using @mount_operation, you can request callbacks when, for instance,
4487 * passwords are needed during authentication.
4489 * If @cancellable is not %NULL, then the operation can be cancelled by
4490 * triggering the cancellable object from another thread. If the operation
4491 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4493 * When the operation is finished, @callback will be called.
4494 * You can then call g_file_mount_mountable_finish() to get
4495 * the result of the operation.
4498 g_file_mount_mountable (GFile *file,
4499 GMountMountFlags flags,
4500 GMountOperation *mount_operation,
4501 GCancellable *cancellable,
4502 GAsyncReadyCallback callback,
4507 g_return_if_fail (G_IS_FILE (file));
4509 iface = G_FILE_GET_IFACE (file);
4511 if (iface->mount_mountable == NULL)
4513 g_task_report_new_error (file, callback, user_data,
4514 g_file_mount_mountable,
4515 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4516 _("Operation not supported"));
4520 (* iface->mount_mountable) (file,
4529 * g_file_mount_mountable_finish:
4530 * @file: input #GFile
4531 * @result: a #GAsyncResult
4532 * @error: a #GError, or %NULL
4534 * Finishes a mount operation. See g_file_mount_mountable() for details.
4536 * Finish an asynchronous mount operation that was started
4537 * with g_file_mount_mountable().
4539 * Returns: (transfer full): a #GFile or %NULL on error.
4540 * Free the returned object with g_object_unref().
4543 g_file_mount_mountable_finish (GFile *file,
4544 GAsyncResult *result,
4549 g_return_val_if_fail (G_IS_FILE (file), NULL);
4550 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4552 if (g_async_result_legacy_propagate_error (result, error))
4554 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4555 return g_task_propagate_pointer (G_TASK (result), error);
4557 iface = G_FILE_GET_IFACE (file);
4558 return (* iface->mount_mountable_finish) (file, result, error);
4562 * g_file_unmount_mountable:
4563 * @file: input #GFile
4564 * @flags: flags affecting the operation
4565 * @cancellable: (allow-none): optional #GCancellable object,
4567 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4568 * when the request is satisfied, or %NULL
4569 * @user_data: (closure): the data to pass to callback function
4571 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4573 * If @cancellable is not %NULL, then the operation can be cancelled by
4574 * triggering the cancellable object from another thread. If the operation
4575 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4577 * When the operation is finished, @callback will be called.
4578 * You can then call g_file_unmount_mountable_finish() to get
4579 * the result of the operation.
4581 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4584 g_file_unmount_mountable (GFile *file,
4585 GMountUnmountFlags flags,
4586 GCancellable *cancellable,
4587 GAsyncReadyCallback callback,
4592 g_return_if_fail (G_IS_FILE (file));
4594 iface = G_FILE_GET_IFACE (file);
4596 if (iface->unmount_mountable == NULL)
4598 g_task_report_new_error (file, callback, user_data,
4599 g_file_unmount_mountable_with_operation,
4600 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4601 _("Operation not supported"));
4605 (* iface->unmount_mountable) (file,
4613 * g_file_unmount_mountable_finish:
4614 * @file: input #GFile
4615 * @result: a #GAsyncResult
4616 * @error: a #GError, or %NULL
4618 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4620 * Finish an asynchronous unmount operation that was started
4621 * with g_file_unmount_mountable().
4623 * Returns: %TRUE if the operation finished successfully.
4626 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4630 g_file_unmount_mountable_finish (GFile *file,
4631 GAsyncResult *result,
4636 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4637 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4639 if (g_async_result_legacy_propagate_error (result, error))
4641 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4642 return g_task_propagate_boolean (G_TASK (result), error);
4644 iface = G_FILE_GET_IFACE (file);
4645 return (* iface->unmount_mountable_finish) (file, result, error);
4649 * g_file_unmount_mountable_with_operation:
4650 * @file: input #GFile
4651 * @flags: flags affecting the operation
4652 * @mount_operation: (allow-none): a #GMountOperation,
4653 * or %NULL to avoid user interaction
4654 * @cancellable: (allow-none): optional #GCancellable object,
4656 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4657 * when the request is satisfied, or %NULL
4658 * @user_data: (closure): the data to pass to callback function
4660 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4662 * If @cancellable is not %NULL, then the operation can be cancelled by
4663 * triggering the cancellable object from another thread. If the operation
4664 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4666 * When the operation is finished, @callback will be called.
4667 * You can then call g_file_unmount_mountable_finish() to get
4668 * the result of the operation.
4673 g_file_unmount_mountable_with_operation (GFile *file,
4674 GMountUnmountFlags flags,
4675 GMountOperation *mount_operation,
4676 GCancellable *cancellable,
4677 GAsyncReadyCallback callback,
4682 g_return_if_fail (G_IS_FILE (file));
4684 iface = G_FILE_GET_IFACE (file);
4686 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4688 g_task_report_new_error (file, callback, user_data,
4689 g_file_unmount_mountable_with_operation,
4690 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4691 _("Operation not supported"));
4695 if (iface->unmount_mountable_with_operation != NULL)
4696 (* iface->unmount_mountable_with_operation) (file,
4703 (* iface->unmount_mountable) (file,
4711 * g_file_unmount_mountable_with_operation_finish:
4712 * @file: input #GFile
4713 * @result: a #GAsyncResult
4714 * @error: a #GError, or %NULL
4716 * Finishes an unmount operation,
4717 * see g_file_unmount_mountable_with_operation() for details.
4719 * Finish an asynchronous unmount operation that was started
4720 * with g_file_unmount_mountable_with_operation().
4722 * Returns: %TRUE if the operation finished successfully.
4728 g_file_unmount_mountable_with_operation_finish (GFile *file,
4729 GAsyncResult *result,
4734 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4735 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4737 if (g_async_result_legacy_propagate_error (result, error))
4739 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4740 return g_task_propagate_boolean (G_TASK (result), error);
4742 iface = G_FILE_GET_IFACE (file);
4743 if (iface->unmount_mountable_with_operation_finish != NULL)
4744 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4746 return (* iface->unmount_mountable_finish) (file, result, error);
4750 * g_file_eject_mountable:
4751 * @file: input #GFile
4752 * @flags: flags affecting the operation
4753 * @cancellable: (allow-none): optional #GCancellable object,
4755 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4756 * when the request is satisfied, or %NULL
4757 * @user_data: (closure): the data to pass to callback function
4759 * Starts an asynchronous eject on a mountable.
4760 * When this operation has completed, @callback will be called with
4761 * @user_user data, and the operation can be finalized with
4762 * g_file_eject_mountable_finish().
4764 * If @cancellable is not %NULL, then the operation can be cancelled by
4765 * triggering the cancellable object from another thread. If the operation
4766 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4768 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4771 g_file_eject_mountable (GFile *file,
4772 GMountUnmountFlags flags,
4773 GCancellable *cancellable,
4774 GAsyncReadyCallback callback,
4779 g_return_if_fail (G_IS_FILE (file));
4781 iface = G_FILE_GET_IFACE (file);
4783 if (iface->eject_mountable == NULL)
4785 g_task_report_new_error (file, callback, user_data,
4786 g_file_eject_mountable_with_operation,
4787 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4788 _("Operation not supported"));
4792 (* iface->eject_mountable) (file,
4800 * g_file_eject_mountable_finish:
4801 * @file: input #GFile
4802 * @result: a #GAsyncResult
4803 * @error: a #GError, or %NULL
4805 * Finishes an asynchronous eject operation started by
4806 * g_file_eject_mountable().
4808 * Returns: %TRUE if the @file was ejected successfully.
4811 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4815 g_file_eject_mountable_finish (GFile *file,
4816 GAsyncResult *result,
4821 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4822 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4824 if (g_async_result_legacy_propagate_error (result, error))
4826 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4827 return g_task_propagate_boolean (G_TASK (result), error);
4829 iface = G_FILE_GET_IFACE (file);
4830 return (* iface->eject_mountable_finish) (file, result, error);
4834 * g_file_eject_mountable_with_operation:
4835 * @file: input #GFile
4836 * @flags: flags affecting the operation
4837 * @mount_operation: (allow-none): a #GMountOperation,
4838 * or %NULL to avoid user interaction
4839 * @cancellable: (allow-none): optional #GCancellable object,
4841 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4842 * when the request is satisfied, or %NULL
4843 * @user_data: (closure): the data to pass to callback function
4845 * Starts an asynchronous eject on a mountable.
4846 * When this operation has completed, @callback will be called with
4847 * @user_user data, and the operation can be finalized with
4848 * g_file_eject_mountable_with_operation_finish().
4850 * If @cancellable is not %NULL, then the operation can be cancelled by
4851 * triggering the cancellable object from another thread. If the operation
4852 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4857 g_file_eject_mountable_with_operation (GFile *file,
4858 GMountUnmountFlags flags,
4859 GMountOperation *mount_operation,
4860 GCancellable *cancellable,
4861 GAsyncReadyCallback callback,
4866 g_return_if_fail (G_IS_FILE (file));
4868 iface = G_FILE_GET_IFACE (file);
4870 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4872 g_task_report_new_error (file, callback, user_data,
4873 g_file_eject_mountable_with_operation,
4874 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4875 _("Operation not supported"));
4879 if (iface->eject_mountable_with_operation != NULL)
4880 (* iface->eject_mountable_with_operation) (file,
4887 (* iface->eject_mountable) (file,
4895 * g_file_eject_mountable_with_operation_finish:
4896 * @file: input #GFile
4897 * @result: a #GAsyncResult
4898 * @error: a #GError, or %NULL
4900 * Finishes an asynchronous eject operation started by
4901 * g_file_eject_mountable_with_operation().
4903 * Returns: %TRUE if the @file was ejected successfully.
4909 g_file_eject_mountable_with_operation_finish (GFile *file,
4910 GAsyncResult *result,
4915 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4916 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4918 if (g_async_result_legacy_propagate_error (result, error))
4920 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4921 return g_task_propagate_boolean (G_TASK (result), error);
4923 iface = G_FILE_GET_IFACE (file);
4924 if (iface->eject_mountable_with_operation_finish != NULL)
4925 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4927 return (* iface->eject_mountable_finish) (file, result, error);
4931 * g_file_monitor_directory:
4932 * @file: input #GFile
4933 * @flags: a set of #GFileMonitorFlags
4934 * @cancellable: (allow-none): optional #GCancellable object,
4936 * @error: a #GError, or %NULL
4938 * Obtains a directory monitor for the given file.
4939 * This may fail if directory monitoring is not supported.
4941 * If @cancellable is not %NULL, then the operation can be cancelled by
4942 * triggering the cancellable object from another thread. If the operation
4943 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4945 * It does not make sense for @flags to contain
4946 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4947 * directories. It is not possible to monitor all the files in a
4948 * directory for changes made via hard links; if you want to do this then
4949 * you must register individual watches with g_file_monitor().
4951 * Virtual: monitor_dir
4952 * Returns: (transfer full): a #GFileMonitor for the given @file,
4953 * or %NULL on error.
4954 * Free the returned object with g_object_unref().
4957 g_file_monitor_directory (GFile *file,
4958 GFileMonitorFlags flags,
4959 GCancellable *cancellable,
4964 g_return_val_if_fail (G_IS_FILE (file), NULL);
4965 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4967 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4970 iface = G_FILE_GET_IFACE (file);
4972 if (iface->monitor_dir == NULL)
4974 g_set_error_literal (error, G_IO_ERROR,
4975 G_IO_ERROR_NOT_SUPPORTED,
4976 _("Operation not supported"));
4980 return (* iface->monitor_dir) (file, flags, cancellable, error);
4984 * g_file_monitor_file:
4985 * @file: input #GFile
4986 * @flags: a set of #GFileMonitorFlags
4987 * @cancellable: (allow-none): optional #GCancellable object,
4989 * @error: a #GError, or %NULL
4991 * Obtains a file monitor for the given file. If no file notification
4992 * mechanism exists, then regular polling of the file is used.
4994 * If @cancellable is not %NULL, then the operation can be cancelled by
4995 * triggering the cancellable object from another thread. If the operation
4996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4998 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
4999 * will also attempt to report changes made to the file via another
5000 * filename (ie, a hard link). Without this flag, you can only rely on
5001 * changes made through the filename contained in @file to be
5002 * reported. Using this flag may result in an increase in resource
5003 * usage, and may not have any effect depending on the #GFileMonitor
5004 * backend and/or filesystem type.
5006 * Returns: (transfer full): a #GFileMonitor for the given @file,
5007 * or %NULL on error.
5008 * Free the returned object with g_object_unref().
5011 g_file_monitor_file (GFile *file,
5012 GFileMonitorFlags flags,
5013 GCancellable *cancellable,
5017 GFileMonitor *monitor;
5019 g_return_val_if_fail (G_IS_FILE (file), NULL);
5021 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5024 iface = G_FILE_GET_IFACE (file);
5028 if (iface->monitor_file)
5029 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5031 /* Fallback to polling */
5032 if (monitor == NULL)
5033 monitor = _g_poll_file_monitor_new (file);
5040 * @file: input #GFile
5041 * @flags: a set of #GFileMonitorFlags
5042 * @cancellable: (allow-none): optional #GCancellable object,
5044 * @error: a #GError, or %NULL
5046 * Obtains a file or directory monitor for the given file,
5047 * depending on the type of the file.
5049 * If @cancellable is not %NULL, then the operation can be cancelled by
5050 * triggering the cancellable object from another thread. If the operation
5051 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5053 * Returns: (transfer full): a #GFileMonitor for the given @file,
5054 * or %NULL on error.
5055 * Free the returned object with g_object_unref().
5060 g_file_monitor (GFile *file,
5061 GFileMonitorFlags flags,
5062 GCancellable *cancellable,
5065 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5066 return g_file_monitor_directory (file,
5067 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5068 cancellable, error);
5070 return g_file_monitor_file (file, flags, cancellable, error);
5073 /********************************************
5074 * Default implementation of async ops *
5075 ********************************************/
5079 GFileQueryInfoFlags flags;
5080 } QueryInfoAsyncData;
5083 query_info_data_free (QueryInfoAsyncData *data)
5085 g_free (data->attributes);
5090 query_info_async_thread (GTask *task,
5093 GCancellable *cancellable)
5095 QueryInfoAsyncData *data = task_data;
5097 GError *error = NULL;
5099 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5101 g_task_return_pointer (task, info, g_object_unref);
5103 g_task_return_error (task, error);
5107 g_file_real_query_info_async (GFile *file,
5108 const char *attributes,
5109 GFileQueryInfoFlags flags,
5111 GCancellable *cancellable,
5112 GAsyncReadyCallback callback,
5116 QueryInfoAsyncData *data;
5118 data = g_new0 (QueryInfoAsyncData, 1);
5119 data->attributes = g_strdup (attributes);
5120 data->flags = flags;
5122 task = g_task_new (file, cancellable, callback, user_data);
5123 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5124 g_task_set_priority (task, io_priority);
5125 g_task_run_in_thread (task, query_info_async_thread);
5126 g_object_unref (task);
5130 g_file_real_query_info_finish (GFile *file,
5134 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5136 return g_task_propagate_pointer (G_TASK (res), error);
5140 query_filesystem_info_async_thread (GTask *task,
5143 GCancellable *cancellable)
5145 const char *attributes = task_data;
5147 GError *error = NULL;
5149 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5151 g_task_return_pointer (task, info, g_object_unref);
5153 g_task_return_error (task, error);
5157 g_file_real_query_filesystem_info_async (GFile *file,
5158 const char *attributes,
5160 GCancellable *cancellable,
5161 GAsyncReadyCallback callback,
5166 task = g_task_new (file, cancellable, callback, user_data);
5167 g_task_set_task_data (task, g_strdup (attributes), g_free);
5168 g_task_set_priority (task, io_priority);
5169 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5170 g_object_unref (task);
5174 g_file_real_query_filesystem_info_finish (GFile *file,
5178 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5180 return g_task_propagate_pointer (G_TASK (res), error);
5184 enumerate_children_async_thread (GTask *task,
5187 GCancellable *cancellable)
5189 QueryInfoAsyncData *data = task_data;
5190 GFileEnumerator *enumerator;
5191 GError *error = NULL;
5193 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5195 g_task_return_error (task, error);
5197 g_task_return_pointer (task, enumerator, g_object_unref);
5201 g_file_real_enumerate_children_async (GFile *file,
5202 const char *attributes,
5203 GFileQueryInfoFlags flags,
5205 GCancellable *cancellable,
5206 GAsyncReadyCallback callback,
5210 QueryInfoAsyncData *data;
5212 data = g_new0 (QueryInfoAsyncData, 1);
5213 data->attributes = g_strdup (attributes);
5214 data->flags = flags;
5216 task = g_task_new (file, cancellable, callback, user_data);
5217 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5218 g_task_set_priority (task, io_priority);
5219 g_task_run_in_thread (task, enumerate_children_async_thread);
5220 g_object_unref (task);
5223 static GFileEnumerator *
5224 g_file_real_enumerate_children_finish (GFile *file,
5228 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5230 return g_task_propagate_pointer (G_TASK (res), error);
5234 open_read_async_thread (GTask *task,
5237 GCancellable *cancellable)
5240 GFileInputStream *stream;
5241 GError *error = NULL;
5243 iface = G_FILE_GET_IFACE (object);
5245 if (iface->read_fn == NULL)
5247 g_task_return_new_error (task, G_IO_ERROR,
5248 G_IO_ERROR_NOT_SUPPORTED,
5249 _("Operation not supported"));
5253 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5255 g_task_return_pointer (task, stream, g_object_unref);
5257 g_task_return_error (task, error);
5261 g_file_real_read_async (GFile *file,
5263 GCancellable *cancellable,
5264 GAsyncReadyCallback callback,
5269 task = g_task_new (file, cancellable, callback, user_data);
5270 g_task_set_priority (task, io_priority);
5271 g_task_run_in_thread (task, open_read_async_thread);
5272 g_object_unref (task);
5275 static GFileInputStream *
5276 g_file_real_read_finish (GFile *file,
5280 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5282 return g_task_propagate_pointer (G_TASK (res), error);
5286 append_to_async_thread (GTask *task,
5287 gpointer source_object,
5289 GCancellable *cancellable)
5292 GFileCreateFlags *data = task_data;
5293 GFileOutputStream *stream;
5294 GError *error = NULL;
5296 iface = G_FILE_GET_IFACE (source_object);
5298 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5300 g_task_return_pointer (task, stream, g_object_unref);
5302 g_task_return_error (task, error);
5306 g_file_real_append_to_async (GFile *file,
5307 GFileCreateFlags flags,
5309 GCancellable *cancellable,
5310 GAsyncReadyCallback callback,
5313 GFileCreateFlags *data;
5316 data = g_new0 (GFileCreateFlags, 1);
5319 task = g_task_new (file, cancellable, callback, user_data);
5320 g_task_set_task_data (task, data, g_free);
5321 g_task_set_priority (task, io_priority);
5323 g_task_run_in_thread (task, append_to_async_thread);
5324 g_object_unref (task);
5327 static GFileOutputStream *
5328 g_file_real_append_to_finish (GFile *file,
5332 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5334 return g_task_propagate_pointer (G_TASK (res), error);
5338 create_async_thread (GTask *task,
5339 gpointer source_object,
5341 GCancellable *cancellable)
5344 GFileCreateFlags *data = task_data;
5345 GFileOutputStream *stream;
5346 GError *error = NULL;
5348 iface = G_FILE_GET_IFACE (source_object);
5350 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5352 g_task_return_pointer (task, stream, g_object_unref);
5354 g_task_return_error (task, error);
5358 g_file_real_create_async (GFile *file,
5359 GFileCreateFlags flags,
5361 GCancellable *cancellable,
5362 GAsyncReadyCallback callback,
5365 GFileCreateFlags *data;
5368 data = g_new0 (GFileCreateFlags, 1);
5371 task = g_task_new (file, cancellable, callback, user_data);
5372 g_task_set_task_data (task, data, g_free);
5373 g_task_set_priority (task, io_priority);
5375 g_task_run_in_thread (task, create_async_thread);
5376 g_object_unref (task);
5379 static GFileOutputStream *
5380 g_file_real_create_finish (GFile *file,
5384 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5386 return g_task_propagate_pointer (G_TASK (res), error);
5390 GFileOutputStream *stream;
5392 gboolean make_backup;
5393 GFileCreateFlags flags;
5397 replace_async_data_free (ReplaceAsyncData *data)
5400 g_object_unref (data->stream);
5401 g_free (data->etag);
5406 replace_async_thread (GTask *task,
5407 gpointer source_object,
5409 GCancellable *cancellable)
5412 GFileOutputStream *stream;
5413 ReplaceAsyncData *data = task_data;
5414 GError *error = NULL;
5416 iface = G_FILE_GET_IFACE (source_object);
5418 stream = iface->replace (G_FILE (source_object),
5426 g_task_return_pointer (task, stream, g_object_unref);
5428 g_task_return_error (task, error);
5432 g_file_real_replace_async (GFile *file,
5434 gboolean make_backup,
5435 GFileCreateFlags flags,
5437 GCancellable *cancellable,
5438 GAsyncReadyCallback callback,
5442 ReplaceAsyncData *data;
5444 data = g_new0 (ReplaceAsyncData, 1);
5445 data->etag = g_strdup (etag);
5446 data->make_backup = make_backup;
5447 data->flags = flags;
5449 task = g_task_new (file, cancellable, callback, user_data);
5450 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5451 g_task_set_priority (task, io_priority);
5453 g_task_run_in_thread (task, replace_async_thread);
5454 g_object_unref (task);
5457 static GFileOutputStream *
5458 g_file_real_replace_finish (GFile *file,
5462 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5464 return g_task_propagate_pointer (G_TASK (res), error);
5468 delete_async_thread (GTask *task,
5471 GCancellable *cancellable)
5473 GFile *file = object;
5475 GError *error = NULL;
5477 iface = G_FILE_GET_IFACE (object);
5479 if (iface->delete_file (file,
5482 g_task_return_boolean (task, TRUE);
5484 g_task_return_error (task, error);
5488 g_file_real_delete_async (GFile *file,
5490 GCancellable *cancellable,
5491 GAsyncReadyCallback callback,
5496 task = g_task_new (file, cancellable, callback, user_data);
5497 g_task_set_priority (task, io_priority);
5498 g_task_run_in_thread (task, delete_async_thread);
5499 g_object_unref (task);
5503 g_file_real_delete_finish (GFile *file,
5507 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5509 return g_task_propagate_boolean (G_TASK (res), error);
5513 open_readwrite_async_thread (GTask *task,
5516 GCancellable *cancellable)
5519 GFileIOStream *stream;
5520 GError *error = NULL;
5522 iface = G_FILE_GET_IFACE (object);
5524 if (iface->open_readwrite == NULL)
5526 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5527 _("Operation not supported"));
5531 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5534 g_task_return_error (task, error);
5536 g_task_return_pointer (task, stream, g_object_unref);
5540 g_file_real_open_readwrite_async (GFile *file,
5542 GCancellable *cancellable,
5543 GAsyncReadyCallback callback,
5548 task = g_task_new (file, cancellable, callback, user_data);
5549 g_task_set_priority (task, io_priority);
5551 g_task_run_in_thread (task, open_readwrite_async_thread);
5552 g_object_unref (task);
5555 static GFileIOStream *
5556 g_file_real_open_readwrite_finish (GFile *file,
5560 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5562 return g_task_propagate_pointer (G_TASK (res), error);
5566 create_readwrite_async_thread (GTask *task,
5569 GCancellable *cancellable)
5572 GFileCreateFlags *data = task_data;
5573 GFileIOStream *stream;
5574 GError *error = NULL;
5576 iface = G_FILE_GET_IFACE (object);
5578 if (iface->create_readwrite == NULL)
5580 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5581 _("Operation not supported"));
5585 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5588 g_task_return_error (task, error);
5590 g_task_return_pointer (task, stream, g_object_unref);
5594 g_file_real_create_readwrite_async (GFile *file,
5595 GFileCreateFlags flags,
5597 GCancellable *cancellable,
5598 GAsyncReadyCallback callback,
5601 GFileCreateFlags *data;
5604 data = g_new0 (GFileCreateFlags, 1);
5607 task = g_task_new (file, cancellable, callback, user_data);
5608 g_task_set_task_data (task, data, g_free);
5609 g_task_set_priority (task, io_priority);
5611 g_task_run_in_thread (task, create_readwrite_async_thread);
5612 g_object_unref (task);
5615 static GFileIOStream *
5616 g_file_real_create_readwrite_finish (GFile *file,
5620 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5622 return g_task_propagate_pointer (G_TASK (res), error);
5627 gboolean make_backup;
5628 GFileCreateFlags flags;
5629 } ReplaceRWAsyncData;
5632 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5634 g_free (data->etag);
5639 replace_readwrite_async_thread (GTask *task,
5642 GCancellable *cancellable)
5645 GFileIOStream *stream;
5646 GError *error = NULL;
5647 ReplaceRWAsyncData *data = task_data;
5649 iface = G_FILE_GET_IFACE (object);
5651 stream = iface->replace_readwrite (G_FILE (object),
5659 g_task_return_error (task, error);
5661 g_task_return_pointer (task, stream, g_object_unref);
5665 g_file_real_replace_readwrite_async (GFile *file,
5667 gboolean make_backup,
5668 GFileCreateFlags flags,
5670 GCancellable *cancellable,
5671 GAsyncReadyCallback callback,
5675 ReplaceRWAsyncData *data;
5677 data = g_new0 (ReplaceRWAsyncData, 1);
5678 data->etag = g_strdup (etag);
5679 data->make_backup = make_backup;
5680 data->flags = flags;
5682 task = g_task_new (file, cancellable, callback, user_data);
5683 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5684 g_task_set_priority (task, io_priority);
5686 g_task_run_in_thread (task, replace_readwrite_async_thread);
5687 g_object_unref (task);
5690 static GFileIOStream *
5691 g_file_real_replace_readwrite_finish (GFile *file,
5695 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5697 return g_task_propagate_pointer (G_TASK (res), error);
5701 set_display_name_async_thread (GTask *task,
5704 GCancellable *cancellable)
5706 GError *error = NULL;
5707 char *name = task_data;
5710 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5713 g_task_return_error (task, error);
5715 g_task_return_pointer (task, file, g_object_unref);
5719 g_file_real_set_display_name_async (GFile *file,
5720 const char *display_name,
5722 GCancellable *cancellable,
5723 GAsyncReadyCallback callback,
5728 task = g_task_new (file, cancellable, callback, user_data);
5729 g_task_set_task_data (task, g_strdup (display_name), g_free);
5730 g_task_set_priority (task, io_priority);
5732 g_task_run_in_thread (task, set_display_name_async_thread);
5733 g_object_unref (task);
5737 g_file_real_set_display_name_finish (GFile *file,
5741 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5743 return g_task_propagate_pointer (G_TASK (res), error);
5747 GFileQueryInfoFlags flags;
5754 set_info_data_free (SetInfoAsyncData *data)
5757 g_object_unref (data->info);
5759 g_error_free (data->error);
5764 set_info_async_thread (GTask *task,
5767 GCancellable *cancellable)
5769 SetInfoAsyncData *data = task_data;
5772 data->res = g_file_set_attributes_from_info (G_FILE (object),
5780 g_file_real_set_attributes_async (GFile *file,
5782 GFileQueryInfoFlags flags,
5784 GCancellable *cancellable,
5785 GAsyncReadyCallback callback,
5789 SetInfoAsyncData *data;
5791 data = g_new0 (SetInfoAsyncData, 1);
5792 data->info = g_file_info_dup (info);
5793 data->flags = flags;
5795 task = g_task_new (file, cancellable, callback, user_data);
5796 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5797 g_task_set_priority (task, io_priority);
5799 g_task_run_in_thread (task, set_info_async_thread);
5800 g_object_unref (task);
5804 g_file_real_set_attributes_finish (GFile *file,
5809 SetInfoAsyncData *data;
5811 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5813 data = g_task_get_task_data (G_TASK (res));
5816 *info = g_object_ref (data->info);
5818 if (error != NULL && data->error)
5819 *error = g_error_copy (data->error);
5825 find_enclosing_mount_async_thread (GTask *task,
5828 GCancellable *cancellable)
5830 GError *error = NULL;
5833 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5836 g_task_return_error (task, error);
5838 g_task_return_pointer (task, mount, g_object_unref);
5842 g_file_real_find_enclosing_mount_async (GFile *file,
5844 GCancellable *cancellable,
5845 GAsyncReadyCallback callback,
5850 task = g_task_new (file, cancellable, callback, user_data);
5851 g_task_set_priority (task, io_priority);
5853 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5854 g_object_unref (task);
5858 g_file_real_find_enclosing_mount_finish (GFile *file,
5862 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5864 return g_task_propagate_pointer (G_TASK (res), error);
5871 GFileCopyFlags flags;
5872 GFileProgressCallback progress_cb;
5873 gpointer progress_cb_data;
5877 copy_async_data_free (CopyAsyncData *data)
5879 g_object_unref (data->source);
5880 g_object_unref (data->destination);
5881 g_slice_free (CopyAsyncData, data);
5885 CopyAsyncData *data;
5886 goffset current_num_bytes;
5887 goffset total_num_bytes;
5891 copy_async_progress_in_main (gpointer user_data)
5893 ProgressData *progress = user_data;
5894 CopyAsyncData *data = progress->data;
5896 data->progress_cb (progress->current_num_bytes,
5897 progress->total_num_bytes,
5898 data->progress_cb_data);
5904 copy_async_progress_callback (goffset current_num_bytes,
5905 goffset total_num_bytes,
5908 GTask *task = user_data;
5909 CopyAsyncData *data = g_task_get_task_data (task);
5910 ProgressData *progress;
5912 progress = g_new (ProgressData, 1);
5913 progress->data = data;
5914 progress->current_num_bytes = current_num_bytes;
5915 progress->total_num_bytes = total_num_bytes;
5917 g_main_context_invoke_full (g_task_get_context (task),
5918 g_task_get_priority (task),
5919 copy_async_progress_in_main,
5925 copy_async_thread (GTask *task,
5928 GCancellable *cancellable)
5930 CopyAsyncData *data = task_data;
5932 GError *error = NULL;
5934 result = g_file_copy (data->source,
5938 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5942 g_task_return_boolean (task, TRUE);
5944 g_task_return_error (task, error);
5948 g_file_real_copy_async (GFile *source,
5950 GFileCopyFlags flags,
5952 GCancellable *cancellable,
5953 GFileProgressCallback progress_callback,
5954 gpointer progress_callback_data,
5955 GAsyncReadyCallback callback,
5959 CopyAsyncData *data;
5961 data = g_slice_new (CopyAsyncData);
5962 data->source = g_object_ref (source);
5963 data->destination = g_object_ref (destination);
5964 data->flags = flags;
5965 data->progress_cb = progress_callback;
5966 data->progress_cb_data = progress_callback_data;
5968 task = g_task_new (source, cancellable, callback, user_data);
5969 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5970 g_task_set_priority (task, io_priority);
5971 g_task_run_in_thread (task, copy_async_thread);
5972 g_object_unref (task);
5976 g_file_real_copy_finish (GFile *file,
5980 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5982 return g_task_propagate_boolean (G_TASK (res), error);
5986 /********************************************
5987 * Default VFS operations *
5988 ********************************************/
5991 * g_file_new_for_path:
5992 * @path: a string containing a relative or absolute path.
5993 * The string must be encoded in the glib filename encoding.
5995 * Constructs a #GFile for a given path. This operation never
5996 * fails, but the returned object might not support any I/O
5997 * operation if @path is malformed.
5999 * Returns: (transfer full): a new #GFile for the given @path.
6000 * Free the returned object with g_object_unref().
6003 g_file_new_for_path (const char *path)
6005 g_return_val_if_fail (path != NULL, NULL);
6007 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6011 * g_file_new_for_uri:
6012 * @uri: a UTF-8 string containing a URI
6014 * Constructs a #GFile for a given URI. This operation never
6015 * fails, but the returned object might not support any I/O
6016 * operation if @uri is malformed or if the uri type is
6019 * Returns: (transfer full): a new #GFile for the given @uri.
6020 * Free the returned object with g_object_unref().
6023 g_file_new_for_uri (const char *uri)
6025 g_return_val_if_fail (uri != NULL, NULL);
6027 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6032 * @tmpl: (type filename) (allow-none): Template for the file
6033 * name, as in g_file_open_tmp(), or %NULL for a default template
6034 * @iostream: (out): on return, a #GFileIOStream for the created file
6035 * @error: a #GError, or %NULL
6037 * Opens a file in the preferred directory for temporary files (as
6038 * returned by g_get_tmp_dir()) and returns a #GFile and
6039 * #GFileIOStream pointing to it.
6041 * @tmpl should be a string in the GLib file name encoding
6042 * containing a sequence of six 'X' characters, and containing no
6043 * directory components. If it is %NULL, a default template is used.
6045 * Unlike the other #GFile constructors, this will return %NULL if
6046 * a temporary file could not be created.
6048 * Returns: (transfer full): a new #GFile.
6049 * Free the returned object with g_object_unref().
6054 g_file_new_tmp (const char *tmpl,
6055 GFileIOStream **iostream,
6061 GFileOutputStream *output;
6063 g_return_val_if_fail (iostream != NULL, NULL);
6065 fd = g_file_open_tmp (tmpl, &path, error);
6069 file = g_file_new_for_path (path);
6071 output = _g_local_file_output_stream_new (fd);
6072 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6074 g_object_unref (output);
6081 * g_file_parse_name:
6082 * @parse_name: a file name or path to be parsed
6084 * Constructs a #GFile with the given @parse_name (i.e. something
6085 * given by g_file_get_parse_name()). This operation never fails,
6086 * but the returned object might not support any I/O operation if
6087 * the @parse_name cannot be parsed.
6089 * Returns: (transfer full): a new #GFile.
6092 g_file_parse_name (const char *parse_name)
6094 g_return_val_if_fail (parse_name != NULL, NULL);
6096 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6100 is_valid_scheme_character (char c)
6102 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6105 /* Following RFC 2396, valid schemes are built like:
6106 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6109 has_valid_scheme (const char *uri)
6115 if (!g_ascii_isalpha (*p))
6120 } while (is_valid_scheme_character (*p));
6126 new_for_cmdline_arg (const gchar *arg,
6132 if (g_path_is_absolute (arg))
6133 return g_file_new_for_path (arg);
6135 if (has_valid_scheme (arg))
6136 return g_file_new_for_uri (arg);
6142 current_dir = g_get_current_dir ();
6143 filename = g_build_filename (current_dir, arg, NULL);
6144 g_free (current_dir);
6147 filename = g_build_filename (cwd, arg, NULL);
6149 file = g_file_new_for_path (filename);
6156 * g_file_new_for_commandline_arg:
6157 * @arg: a command line string
6159 * Creates a #GFile with the given argument from the command line.
6160 * The value of @arg can be either a URI, an absolute path or a
6161 * relative path resolved relative to the current working directory.
6162 * This operation never fails, but the returned object might not
6163 * support any I/O operation if @arg points to a malformed path.
6165 * Returns: (transfer full): a new #GFile.
6166 * Free the returned object with g_object_unref().
6169 g_file_new_for_commandline_arg (const char *arg)
6171 g_return_val_if_fail (arg != NULL, NULL);
6173 return new_for_cmdline_arg (arg, NULL);
6177 * g_file_new_for_commandline_arg_and_cwd:
6178 * @arg: a command line string
6179 * @cwd: the current working directory of the commandline
6181 * Creates a #GFile with the given argument from the command line.
6183 * This function is similar to g_file_new_for_commandline_arg() except
6184 * that it allows for passing the current working directory as an
6185 * argument instead of using the current working directory of the
6188 * This is useful if the commandline argument was given in a context
6189 * other than the invocation of the current process.
6191 * See also g_application_command_line_create_file_for_arg().
6193 * Returns: (transfer full): a new #GFile
6198 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6201 g_return_val_if_fail (arg != NULL, NULL);
6202 g_return_val_if_fail (cwd != NULL, NULL);
6204 return new_for_cmdline_arg (arg, cwd);
6208 * g_file_mount_enclosing_volume:
6209 * @location: input #GFile
6210 * @flags: flags affecting the operation
6211 * @mount_operation: (allow-none): a #GMountOperation
6212 * or %NULL to avoid user interaction
6213 * @cancellable: (allow-none): optional #GCancellable object,
6215 * @callback: (allow-none): a #GAsyncReadyCallback to call
6216 * when the request is satisfied, or %NULL
6217 * @user_data: the data to pass to callback function
6219 * Starts a @mount_operation, mounting the volume that contains
6220 * the file @location.
6222 * When this operation has completed, @callback will be called with
6223 * @user_user data, and the operation can be finalized with
6224 * g_file_mount_enclosing_volume_finish().
6226 * If @cancellable is not %NULL, then the operation can be cancelled by
6227 * triggering the cancellable object from another thread. If the operation
6228 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6231 g_file_mount_enclosing_volume (GFile *location,
6232 GMountMountFlags flags,
6233 GMountOperation *mount_operation,
6234 GCancellable *cancellable,
6235 GAsyncReadyCallback callback,
6240 g_return_if_fail (G_IS_FILE (location));
6242 iface = G_FILE_GET_IFACE (location);
6244 if (iface->mount_enclosing_volume == NULL)
6246 g_task_report_new_error (location, callback, user_data,
6247 g_file_mount_enclosing_volume,
6248 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6249 _("volume doesn't implement mount"));
6253 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6258 * g_file_mount_enclosing_volume_finish:
6259 * @location: input #GFile
6260 * @result: a #GAsyncResult
6261 * @error: a #GError, or %NULL
6263 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6265 * Returns: %TRUE if successful. If an error has occurred,
6266 * this function will return %FALSE and set @error
6267 * appropriately if present.
6270 g_file_mount_enclosing_volume_finish (GFile *location,
6271 GAsyncResult *result,
6276 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6277 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6279 if (g_async_result_legacy_propagate_error (result, error))
6281 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6282 return g_task_propagate_boolean (G_TASK (result), error);
6284 iface = G_FILE_GET_IFACE (location);
6286 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6289 /********************************************
6290 * Utility functions *
6291 ********************************************/
6294 * g_file_query_default_handler:
6295 * @file: a #GFile to open
6296 * @cancellable: optional #GCancellable object, %NULL to ignore
6297 * @error: a #GError, or %NULL
6299 * Returns the #GAppInfo that is registered as the default
6300 * application to handle the file specified by @file.
6302 * If @cancellable is not %NULL, then the operation can be cancelled by
6303 * triggering the cancellable object from another thread. If the operation
6304 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6306 * Returns: (transfer full): a #GAppInfo if the handle was found,
6307 * %NULL if there were errors.
6308 * When you are done with it, release it with g_object_unref()
6311 g_file_query_default_handler (GFile *file,
6312 GCancellable *cancellable,
6316 const char *content_type;
6321 uri_scheme = g_file_get_uri_scheme (file);
6322 if (uri_scheme && uri_scheme[0] != '\0')
6324 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6325 g_free (uri_scheme);
6327 if (appinfo != NULL)
6331 info = g_file_query_info (file,
6332 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6341 content_type = g_file_info_get_content_type (info);
6344 /* Don't use is_native(), as we want to support fuse paths if available */
6345 path = g_file_get_path (file);
6346 appinfo = g_app_info_get_default_for_type (content_type,
6351 g_object_unref (info);
6353 if (appinfo != NULL)
6356 g_set_error_literal (error, G_IO_ERROR,
6357 G_IO_ERROR_NOT_SUPPORTED,
6358 _("No application is registered as handling this file"));
6362 #define GET_CONTENT_BLOCK_SIZE 8192
6365 * g_file_load_contents:
6366 * @file: input #GFile
6367 * @cancellable: optional #GCancellable object, %NULL to ignore
6368 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6369 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6370 * or %NULL if the length is not needed
6371 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6372 * or %NULL if the entity tag is not needed
6373 * @error: a #GError, or %NULL
6375 * Loads the content of the file into memory. The data is always
6376 * zero-terminated, but this is not included in the resultant @length.
6377 * The returned @content should be freed with g_free() when no longer
6380 * If @cancellable is not %NULL, then the operation can be cancelled by
6381 * triggering the cancellable object from another thread. If the operation
6382 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6384 * Returns: %TRUE if the @file's contents were successfully loaded.
6385 * %FALSE if there were errors.
6388 g_file_load_contents (GFile *file,
6389 GCancellable *cancellable,
6395 GFileInputStream *in;
6396 GByteArray *content;
6401 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6402 g_return_val_if_fail (contents != NULL, FALSE);
6404 in = g_file_read (file, cancellable, error);
6408 content = g_byte_array_new ();
6411 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6412 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6413 content->data + pos,
6414 GET_CONTENT_BLOCK_SIZE,
6415 cancellable, error)) > 0)
6418 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6425 info = g_file_input_stream_query_info (in,
6426 G_FILE_ATTRIBUTE_ETAG_VALUE,
6431 *etag_out = g_strdup (g_file_info_get_etag (info));
6432 g_object_unref (info);
6436 /* Ignore errors on close */
6437 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6438 g_object_unref (in);
6442 /* error is set already */
6443 g_byte_array_free (content, TRUE);
6450 /* Zero terminate (we got an extra byte allocated for this */
6451 content->data[pos] = 0;
6453 *contents = (char *)g_byte_array_free (content, FALSE);
6460 GFileReadMoreCallback read_more_callback;
6461 GByteArray *content;
6468 load_contents_data_free (LoadContentsData *data)
6471 g_byte_array_free (data->content, TRUE);
6472 g_free (data->etag);
6477 load_contents_close_callback (GObject *obj,
6478 GAsyncResult *close_res,
6481 GInputStream *stream = G_INPUT_STREAM (obj);
6482 LoadContentsData *data = user_data;
6484 /* Ignore errors here, we're only reading anyway */
6485 g_input_stream_close_finish (stream, close_res, NULL);
6486 g_object_unref (stream);
6488 g_task_return_boolean (data->task, TRUE);
6489 g_object_unref (data->task);
6493 load_contents_fstat_callback (GObject *obj,
6494 GAsyncResult *stat_res,
6497 GInputStream *stream = G_INPUT_STREAM (obj);
6498 LoadContentsData *data = user_data;
6501 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6505 data->etag = g_strdup (g_file_info_get_etag (info));
6506 g_object_unref (info);
6509 g_input_stream_close_async (stream, 0,
6510 g_task_get_cancellable (data->task),
6511 load_contents_close_callback, data);
6515 load_contents_read_callback (GObject *obj,
6516 GAsyncResult *read_res,
6519 GInputStream *stream = G_INPUT_STREAM (obj);
6520 LoadContentsData *data = user_data;
6521 GError *error = NULL;
6524 read_size = g_input_stream_read_finish (stream, read_res, &error);
6528 g_task_return_error (data->task, error);
6529 g_object_unref (data->task);
6531 /* Close the file ignoring any error */
6532 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6533 g_object_unref (stream);
6535 else if (read_size == 0)
6537 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6538 G_FILE_ATTRIBUTE_ETAG_VALUE,
6540 g_task_get_cancellable (data->task),
6541 load_contents_fstat_callback,
6544 else if (read_size > 0)
6546 data->pos += read_size;
6548 g_byte_array_set_size (data->content,
6549 data->pos + GET_CONTENT_BLOCK_SIZE);
6552 if (data->read_more_callback &&
6553 !data->read_more_callback ((char *)data->content->data, data->pos,
6554 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6555 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6556 G_FILE_ATTRIBUTE_ETAG_VALUE,
6558 g_task_get_cancellable (data->task),
6559 load_contents_fstat_callback,
6562 g_input_stream_read_async (stream,
6563 data->content->data + data->pos,
6564 GET_CONTENT_BLOCK_SIZE,
6566 g_task_get_cancellable (data->task),
6567 load_contents_read_callback,
6573 load_contents_open_callback (GObject *obj,
6574 GAsyncResult *open_res,
6577 GFile *file = G_FILE (obj);
6578 GFileInputStream *stream;
6579 LoadContentsData *data = user_data;
6580 GError *error = NULL;
6582 stream = g_file_read_finish (file, open_res, &error);
6586 g_byte_array_set_size (data->content,
6587 data->pos + GET_CONTENT_BLOCK_SIZE);
6588 g_input_stream_read_async (G_INPUT_STREAM (stream),
6589 data->content->data + data->pos,
6590 GET_CONTENT_BLOCK_SIZE,
6592 g_task_get_cancellable (data->task),
6593 load_contents_read_callback,
6598 g_task_return_error (data->task, error);
6599 g_object_unref (data->task);
6604 * g_file_load_partial_contents_async: (skip)
6605 * @file: input #GFile
6606 * @cancellable: optional #GCancellable object, %NULL to ignore
6607 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6608 * and to specify whether further data should be read
6609 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6610 * @user_data: the data to pass to the callback functions
6612 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6613 * be used to stop reading from the file when appropriate, else this
6614 * function will behave exactly as g_file_load_contents_async(). This
6615 * operation can be finished by g_file_load_partial_contents_finish().
6617 * Users of this function should be aware that @user_data is passed to
6618 * both the @read_more_callback and the @callback.
6620 * If @cancellable is not %NULL, then the operation can be cancelled by
6621 * triggering the cancellable object from another thread. If the operation
6622 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6625 g_file_load_partial_contents_async (GFile *file,
6626 GCancellable *cancellable,
6627 GFileReadMoreCallback read_more_callback,
6628 GAsyncReadyCallback callback,
6631 LoadContentsData *data;
6633 g_return_if_fail (G_IS_FILE (file));
6635 data = g_new0 (LoadContentsData, 1);
6636 data->read_more_callback = read_more_callback;
6637 data->content = g_byte_array_new ();
6639 data->task = g_task_new (file, cancellable, callback, user_data);
6640 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6642 g_file_read_async (file,
6644 g_task_get_cancellable (data->task),
6645 load_contents_open_callback,
6650 * g_file_load_partial_contents_finish:
6651 * @file: input #GFile
6652 * @res: a #GAsyncResult
6653 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6654 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6655 * or %NULL if the length is not needed
6656 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6657 * or %NULL if the entity tag is not needed
6658 * @error: a #GError, or %NULL
6660 * Finishes an asynchronous partial load operation that was started
6661 * with g_file_load_partial_contents_async(). The data is always
6662 * zero-terminated, but this is not included in the resultant @length.
6663 * The returned @content should be freed with g_free() when no longer
6666 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6667 * present, it will be set appropriately.
6670 g_file_load_partial_contents_finish (GFile *file,
6678 LoadContentsData *data;
6680 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6681 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6682 g_return_val_if_fail (contents != NULL, FALSE);
6684 task = G_TASK (res);
6686 if (!g_task_propagate_boolean (task, error))
6693 data = g_task_get_task_data (task);
6696 *length = data->pos;
6700 *etag_out = data->etag;
6704 /* Zero terminate */
6705 g_byte_array_set_size (data->content, data->pos + 1);
6706 data->content->data[data->pos] = 0;
6708 *contents = (char *)g_byte_array_free (data->content, FALSE);
6709 data->content = NULL;
6715 * g_file_load_contents_async:
6716 * @file: input #GFile
6717 * @cancellable: optional #GCancellable object, %NULL to ignore
6718 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6719 * @user_data: the data to pass to callback function
6721 * Starts an asynchronous load of the @file's contents.
6723 * For more details, see g_file_load_contents() which is
6724 * the synchronous version of this call.
6726 * When the load operation has completed, @callback will be called
6727 * with @user data. To finish the operation, call
6728 * g_file_load_contents_finish() with the #GAsyncResult returned by
6731 * If @cancellable is not %NULL, then the operation can be cancelled by
6732 * triggering the cancellable object from another thread. If the operation
6733 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6736 g_file_load_contents_async (GFile *file,
6737 GCancellable *cancellable,
6738 GAsyncReadyCallback callback,
6741 g_file_load_partial_contents_async (file,
6744 callback, user_data);
6748 * g_file_load_contents_finish:
6749 * @file: input #GFile
6750 * @res: a #GAsyncResult
6751 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6752 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6753 * or %NULL if the length is not needed
6754 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6755 * or %NULL if the entity tag is not needed
6756 * @error: a #GError, or %NULL
6758 * Finishes an asynchronous load of the @file's contents.
6759 * The contents are placed in @contents, and @length is set to the
6760 * size of the @contents string. The @content should be freed with
6761 * g_free() when no longer needed. If @etag_out is present, it will be
6762 * set to the new entity tag for the @file.
6764 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6765 * present, it will be set appropriately.
6768 g_file_load_contents_finish (GFile *file,
6775 return g_file_load_partial_contents_finish (file,
6784 * g_file_replace_contents:
6785 * @file: input #GFile
6786 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6787 * @length: the length of @contents in bytes
6788 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6789 * for the document, or %NULL
6790 * @make_backup: %TRUE if a backup should be created
6791 * @flags: a set of #GFileCreateFlags
6792 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6793 * for the document. This should be freed with g_free() when no longer
6795 * @cancellable: optional #GCancellable object, %NULL to ignore
6796 * @error: a #GError, or %NULL
6798 * Replaces the contents of @file with @contents of @length bytes.
6800 * If @etag is specified (not %NULL), any existing file must have that etag,
6801 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6803 * If @make_backup is %TRUE, this function will attempt to make a backup
6806 * If @cancellable is not %NULL, then the operation can be cancelled by
6807 * triggering the cancellable object from another thread. If the operation
6808 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6810 * The returned @new_etag can be used to verify that the file hasn't
6811 * changed the next time it is saved over.
6813 * Returns: %TRUE if successful. If an error has occurred, this function
6814 * will return %FALSE and set @error appropriately if present.
6817 g_file_replace_contents (GFile *file,
6818 const char *contents,
6821 gboolean make_backup,
6822 GFileCreateFlags flags,
6824 GCancellable *cancellable,
6827 GFileOutputStream *out;
6828 gsize pos, remainder;
6832 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6833 g_return_val_if_fail (contents != NULL, FALSE);
6835 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6841 while (remainder > 0 &&
6842 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6844 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6852 if (remainder > 0 && res < 0)
6854 /* Ignore errors on close */
6855 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6856 g_object_unref (out);
6858 /* error is set already */
6862 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6865 *new_etag = g_file_output_stream_get_etag (out);
6867 g_object_unref (out);
6874 const char *content;
6879 } ReplaceContentsData;
6882 replace_contents_data_free (ReplaceContentsData *data)
6884 g_free (data->etag);
6889 replace_contents_close_callback (GObject *obj,
6890 GAsyncResult *close_res,
6893 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6894 ReplaceContentsData *data = user_data;
6896 /* Ignore errors here, we're only reading anyway */
6897 g_output_stream_close_finish (stream, close_res, NULL);
6898 g_object_unref (stream);
6902 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6903 g_task_return_boolean (data->task, TRUE);
6905 g_object_unref (data->task);
6909 replace_contents_write_callback (GObject *obj,
6910 GAsyncResult *read_res,
6913 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6914 ReplaceContentsData *data = user_data;
6915 GError *error = NULL;
6918 write_size = g_output_stream_write_finish (stream, read_res, &error);
6920 if (write_size <= 0)
6922 /* Error or EOF, close the file */
6925 data->failed = TRUE;
6926 g_task_return_error (data->task, error);
6928 g_output_stream_close_async (stream, 0,
6929 g_task_get_cancellable (data->task),
6930 replace_contents_close_callback, data);
6932 else if (write_size > 0)
6934 data->pos += write_size;
6936 if (data->pos >= data->length)
6937 g_output_stream_close_async (stream, 0,
6938 g_task_get_cancellable (data->task),
6939 replace_contents_close_callback, data);
6941 g_output_stream_write_async (stream,
6942 data->content + data->pos,
6943 data->length - data->pos,
6945 g_task_get_cancellable (data->task),
6946 replace_contents_write_callback,
6952 replace_contents_open_callback (GObject *obj,
6953 GAsyncResult *open_res,
6956 GFile *file = G_FILE (obj);
6957 GFileOutputStream *stream;
6958 ReplaceContentsData *data = user_data;
6959 GError *error = NULL;
6961 stream = g_file_replace_finish (file, open_res, &error);
6965 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6966 data->content + data->pos,
6967 data->length - data->pos,
6969 g_task_get_cancellable (data->task),
6970 replace_contents_write_callback,
6975 g_task_return_error (data->task, error);
6976 g_object_unref (data->task);
6981 * g_file_replace_contents_async:
6982 * @file: input #GFile
6983 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6984 * @length: the length of @contents in bytes
6985 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6986 * @make_backup: %TRUE if a backup should be created
6987 * @flags: a set of #GFileCreateFlags
6988 * @cancellable: optional #GCancellable object, %NULL to ignore
6989 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6990 * @user_data: the data to pass to callback function
6992 * Starts an asynchronous replacement of @file with the given
6993 * @contents of @length bytes. @etag will replace the document's
6994 * current entity tag.
6996 * When this operation has completed, @callback will be called with
6997 * @user_user data, and the operation can be finalized with
6998 * g_file_replace_contents_finish().
7000 * If @cancellable is not %NULL, then the operation can be cancelled by
7001 * triggering the cancellable object from another thread. If the operation
7002 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7004 * If @make_backup is %TRUE, this function will attempt to
7005 * make a backup of @file.
7008 g_file_replace_contents_async (GFile *file,
7009 const char *contents,
7012 gboolean make_backup,
7013 GFileCreateFlags flags,
7014 GCancellable *cancellable,
7015 GAsyncReadyCallback callback,
7018 ReplaceContentsData *data;
7020 g_return_if_fail (G_IS_FILE (file));
7021 g_return_if_fail (contents != NULL);
7023 data = g_new0 (ReplaceContentsData, 1);
7025 data->content = contents;
7026 data->length = length;
7028 data->task = g_task_new (file, cancellable, callback, user_data);
7029 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7031 g_file_replace_async (file,
7036 g_task_get_cancellable (data->task),
7037 replace_contents_open_callback,
7042 * g_file_replace_contents_finish:
7043 * @file: input #GFile
7044 * @res: a #GAsyncResult
7045 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7046 * for the document. This should be freed with g_free() when it is no
7047 * longer needed, or %NULL
7048 * @error: a #GError, or %NULL
7050 * Finishes an asynchronous replace of the given @file. See
7051 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7052 * tag for the document, if present.
7054 * Returns: %TRUE on success, %FALSE on failure.
7057 g_file_replace_contents_finish (GFile *file,
7063 ReplaceContentsData *data;
7065 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7066 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7068 task = G_TASK (res);
7070 if (!g_task_propagate_boolean (task, error))
7073 data = g_task_get_task_data (task);
7077 *new_etag = data->etag;
7078 data->etag = NULL; /* Take ownership */
7085 * g_file_start_mountable:
7086 * @file: input #GFile
7087 * @flags: flags affecting the operation
7088 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7089 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7090 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7091 * @user_data: the data to pass to callback function
7093 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7094 * Using @start_operation, you can request callbacks when, for instance,
7095 * passwords are needed during authentication.
7097 * If @cancellable is not %NULL, then the operation can be cancelled by
7098 * triggering the cancellable object from another thread. If the operation
7099 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7101 * When the operation is finished, @callback will be called.
7102 * You can then call g_file_mount_mountable_finish() to get
7103 * the result of the operation.
7108 g_file_start_mountable (GFile *file,
7109 GDriveStartFlags flags,
7110 GMountOperation *start_operation,
7111 GCancellable *cancellable,
7112 GAsyncReadyCallback callback,
7117 g_return_if_fail (G_IS_FILE (file));
7119 iface = G_FILE_GET_IFACE (file);
7121 if (iface->start_mountable == NULL)
7123 g_task_report_new_error (file, callback, user_data,
7124 g_file_start_mountable,
7125 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7126 _("Operation not supported"));
7130 (* iface->start_mountable) (file,
7139 * g_file_start_mountable_finish:
7140 * @file: input #GFile
7141 * @result: a #GAsyncResult
7142 * @error: a #GError, or %NULL
7144 * Finishes a start operation. See g_file_start_mountable() for details.
7146 * Finish an asynchronous start operation that was started
7147 * with g_file_start_mountable().
7149 * Returns: %TRUE if the operation finished successfully. %FALSE
7155 g_file_start_mountable_finish (GFile *file,
7156 GAsyncResult *result,
7161 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7162 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7164 if (g_async_result_legacy_propagate_error (result, error))
7166 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7167 return g_task_propagate_boolean (G_TASK (result), error);
7169 iface = G_FILE_GET_IFACE (file);
7170 return (* iface->start_mountable_finish) (file, result, error);
7174 * g_file_stop_mountable:
7175 * @file: input #GFile
7176 * @flags: flags affecting the operation
7177 * @mount_operation: (allow-none): a #GMountOperation,
7178 * or %NULL to avoid user interaction.
7179 * @cancellable: (allow-none): optional #GCancellable object,
7181 * @callback: (allow-none): a #GAsyncReadyCallback to call
7182 * when the request is satisfied, or %NULL
7183 * @user_data: the data to pass to callback function
7185 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7187 * If @cancellable is not %NULL, then the operation can be cancelled by
7188 * triggering the cancellable object from another thread. If the operation
7189 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7191 * When the operation is finished, @callback will be called.
7192 * You can then call g_file_stop_mountable_finish() to get
7193 * the result of the operation.
7198 g_file_stop_mountable (GFile *file,
7199 GMountUnmountFlags flags,
7200 GMountOperation *mount_operation,
7201 GCancellable *cancellable,
7202 GAsyncReadyCallback callback,
7207 g_return_if_fail (G_IS_FILE (file));
7209 iface = G_FILE_GET_IFACE (file);
7211 if (iface->stop_mountable == NULL)
7213 g_task_report_new_error (file, callback, user_data,
7214 g_file_stop_mountable,
7215 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7216 _("Operation not supported"));
7220 (* iface->stop_mountable) (file,
7229 * g_file_stop_mountable_finish:
7230 * @file: input #GFile
7231 * @result: a #GAsyncResult
7232 * @error: a #GError, or %NULL
7234 * Finishes an stop operation, see g_file_stop_mountable() for details.
7236 * Finish an asynchronous stop operation that was started
7237 * with g_file_stop_mountable().
7239 * Returns: %TRUE if the operation finished successfully.
7245 g_file_stop_mountable_finish (GFile *file,
7246 GAsyncResult *result,
7251 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7252 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7254 if (g_async_result_legacy_propagate_error (result, error))
7256 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7257 return g_task_propagate_boolean (G_TASK (result), error);
7259 iface = G_FILE_GET_IFACE (file);
7260 return (* iface->stop_mountable_finish) (file, result, error);
7264 * g_file_poll_mountable:
7265 * @file: input #GFile
7266 * @cancellable: optional #GCancellable object, %NULL to ignore
7267 * @callback: (allow-none): a #GAsyncReadyCallback to call
7268 * when the request is satisfied, or %NULL
7269 * @user_data: the data to pass to callback function
7271 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7273 * If @cancellable is not %NULL, then the operation can be cancelled by
7274 * triggering the cancellable object from another thread. If the operation
7275 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7277 * When the operation is finished, @callback will be called.
7278 * You can then call g_file_mount_mountable_finish() to get
7279 * the result of the operation.
7284 g_file_poll_mountable (GFile *file,
7285 GCancellable *cancellable,
7286 GAsyncReadyCallback callback,
7291 g_return_if_fail (G_IS_FILE (file));
7293 iface = G_FILE_GET_IFACE (file);
7295 if (iface->poll_mountable == NULL)
7297 g_task_report_new_error (file, callback, user_data,
7298 g_file_poll_mountable,
7299 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7300 _("Operation not supported"));
7304 (* iface->poll_mountable) (file,
7311 * g_file_poll_mountable_finish:
7312 * @file: input #GFile
7313 * @result: a #GAsyncResult
7314 * @error: a #GError, or %NULL
7316 * Finishes a poll operation. See g_file_poll_mountable() for details.
7318 * Finish an asynchronous poll operation that was polled
7319 * with g_file_poll_mountable().
7321 * Returns: %TRUE if the operation finished successfully. %FALSE
7327 g_file_poll_mountable_finish (GFile *file,
7328 GAsyncResult *result,
7333 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7334 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7336 if (g_async_result_legacy_propagate_error (result, error))
7338 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7339 return g_task_propagate_boolean (G_TASK (result), error);
7341 iface = G_FILE_GET_IFACE (file);
7342 return (* iface->poll_mountable_finish) (file, result, error);
7346 * g_file_supports_thread_contexts:
7349 * Checks if @file supports <link
7350 * linkend="g-main-context-push-thread-default-context">thread-default
7351 * contexts</link>. If this returns %FALSE, you cannot perform
7352 * asynchronous operations on @file in a thread that has a
7353 * thread-default context.
7355 * Returns: Whether or not @file supports thread-default contexts.
7360 g_file_supports_thread_contexts (GFile *file)
7364 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7366 iface = G_FILE_GET_IFACE (file);
7367 return iface->supports_thread_contexts;