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,
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,
3001 /* Maybe copy the symlink? */
3002 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3003 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3005 target = g_file_info_get_symlink_target (info);
3008 if (!copy_symlink (destination, flags, cancellable, target, error))
3010 g_object_unref (info);
3014 g_object_unref (info);
3017 /* ... else fall back on a regular file copy */
3018 g_object_unref (info);
3020 /* Handle "special" files (pipes, device nodes, ...)? */
3021 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3023 /* FIXME: could try to recreate device nodes and others? */
3024 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3025 _("Can't copy special file"));
3026 g_object_unref (info);
3029 /* Everything else should just fall back on a regular copy. */
3031 g_object_unref (info);
3033 in = open_source_for_copy (source, destination, flags, cancellable, error);
3037 if (flags & G_FILE_COPY_OVERWRITE)
3039 out = (GOutputStream *)g_file_replace (destination,
3041 flags & G_FILE_COPY_BACKUP,
3042 G_FILE_CREATE_REPLACE_DESTINATION,
3043 cancellable, error);
3047 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3052 g_object_unref (in);
3056 #ifdef HAVE_SYS_IOCTL_H
3057 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3059 GError *reflink_err = NULL;
3061 result = btrfs_reflink_with_progress (in, out, info, cancellable,
3062 progress_callback, progress_callback_data,
3065 if (result || !g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3068 g_propagate_error (error, reflink_err);
3072 g_clear_error (&reflink_err);
3077 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3079 GError *splice_err = NULL;
3081 result = splice_stream_with_progress (in, out, cancellable,
3082 progress_callback, progress_callback_data,
3085 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3088 g_propagate_error (error, splice_err);
3092 g_clear_error (&splice_err);
3096 result = copy_stream_with_progress (in, out, source, cancellable,
3097 progress_callback, progress_callback_data,
3101 /* Don't care about errors in source here */
3102 g_input_stream_close (in, cancellable, NULL);
3104 /* But write errors on close are bad! */
3105 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
3108 g_object_unref (in);
3109 g_object_unref (out);
3111 if (result == FALSE)
3115 /* Ignore errors here. Failure to copy metadata is not a hard error */
3116 g_file_copy_attributes (source, destination,
3117 flags, cancellable, NULL);
3124 * @source: input #GFile
3125 * @destination: destination #GFile
3126 * @flags: set of #GFileCopyFlags
3127 * @cancellable: (allow-none): optional #GCancellable object,
3129 * @progress_callback: (allow-none) (scope call): function to callback with
3130 * progress information, or %NULL if progress information is not needed
3131 * @progress_callback_data: (closure): user data to pass to @progress_callback
3132 * @error: #GError to set on error, or %NULL
3134 * Copies the file @source to the location specified by @destination.
3135 * Can not handle recursive copies of directories.
3137 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3138 * existing @destination file is overwritten.
3140 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3141 * will be copied as symlinks, otherwise the target of the
3142 * @source symlink will be copied.
3144 * If @cancellable is not %NULL, then the operation can be cancelled by
3145 * triggering the cancellable object from another thread. If the operation
3146 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3148 * If @progress_callback is not %NULL, then the operation can be monitored
3149 * by setting this to a #GFileProgressCallback function.
3150 * @progress_callback_data will be passed to this function. It is guaranteed
3151 * that this callback will be called after all data has been transferred with
3152 * the total number of bytes copied during the operation.
3154 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3155 * is returned, independent on the status of the @destination.
3157 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3158 * the error %G_IO_ERROR_EXISTS is returned.
3160 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3161 * error is returned. If trying to overwrite a directory with a directory the
3162 * %G_IO_ERROR_WOULD_MERGE error is returned.
3164 * If the source is a directory and the target does not exist, or
3165 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3166 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3168 * If you are interested in copying the #GFile object itself (not the on-disk
3169 * file), see g_file_dup().
3171 * Returns: %TRUE on success, %FALSE otherwise.
3174 g_file_copy (GFile *source,
3176 GFileCopyFlags flags,
3177 GCancellable *cancellable,
3178 GFileProgressCallback progress_callback,
3179 gpointer progress_callback_data,
3186 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3187 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3189 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3192 iface = G_FILE_GET_IFACE (destination);
3196 res = (* iface->copy) (source, destination,
3198 progress_callback, progress_callback_data,
3204 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3206 g_propagate_error (error, my_error);
3210 g_clear_error (&my_error);
3213 /* If the types are different, and the destination method failed
3214 * also try the source method
3216 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3218 iface = G_FILE_GET_IFACE (source);
3223 res = (* iface->copy) (source, destination,
3225 progress_callback, progress_callback_data,
3231 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3233 g_propagate_error (error, my_error);
3237 g_clear_error (&my_error);
3241 return file_copy_fallback (source, destination, flags, cancellable,
3242 progress_callback, progress_callback_data,
3247 * g_file_copy_async: (skip)
3248 * @source: input #GFile
3249 * @destination: destination #GFile
3250 * @flags: set of #GFileCopyFlags
3251 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3253 * @cancellable: (allow-none): optional #GCancellable object,
3255 * @progress_callback: (allow-none): function to callback with progress
3256 * information, or %NULL if progress information is not needed
3257 * @progress_callback_data: (closure): user data to pass to @progress_callback
3258 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3259 * @user_data: the data to pass to callback function
3261 * Copies the file @source to the location specified by @destination
3262 * asynchronously. For details of the behaviour, see g_file_copy().
3264 * If @progress_callback is not %NULL, then that function that will be called
3265 * just like in g_file_copy(), however the callback will run in the main loop,
3266 * not in the thread that is doing the I/O operation.
3268 * When the operation is finished, @callback will be called. You can then call
3269 * g_file_copy_finish() to get the result of the operation.
3272 g_file_copy_async (GFile *source,
3274 GFileCopyFlags flags,
3276 GCancellable *cancellable,
3277 GFileProgressCallback progress_callback,
3278 gpointer progress_callback_data,
3279 GAsyncReadyCallback callback,
3284 g_return_if_fail (G_IS_FILE (source));
3285 g_return_if_fail (G_IS_FILE (destination));
3287 iface = G_FILE_GET_IFACE (source);
3288 (* iface->copy_async) (source,
3294 progress_callback_data,
3300 * g_file_copy_finish:
3301 * @file: input #GFile
3302 * @res: a #GAsyncResult
3303 * @error: a #GError, or %NULL
3305 * Finishes copying the file started with g_file_copy_async().
3307 * Returns: a %TRUE on success, %FALSE on error.
3310 g_file_copy_finish (GFile *file,
3316 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3317 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3319 if (g_async_result_legacy_propagate_error (res, error))
3322 iface = G_FILE_GET_IFACE (file);
3323 return (* iface->copy_finish) (file, res, error);
3328 * @source: #GFile pointing to the source location
3329 * @destination: #GFile pointing to the destination location
3330 * @flags: set of #GFileCopyFlags
3331 * @cancellable: (allow-none): optional #GCancellable object,
3333 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3334 * function for updates
3335 * @progress_callback_data: (closure): gpointer to user data for
3336 * the callback function
3337 * @error: #GError for returning error conditions, or %NULL
3339 * Tries to move the file or directory @source to the location specified
3340 * by @destination. If native move operations are supported then this is
3341 * used, otherwise a copy + delete fallback is used. The native
3342 * implementation may support moving directories (for instance on moves
3343 * inside the same filesystem), but the fallback code does not.
3345 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3346 * existing @destination file is overwritten.
3348 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3349 * will be copied as symlinks, otherwise the target of the
3350 * @source symlink will be copied.
3352 * If @cancellable is not %NULL, then the operation can be cancelled by
3353 * triggering the cancellable object from another thread. If the operation
3354 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3356 * If @progress_callback is not %NULL, then the operation can be monitored
3357 * by setting this to a #GFileProgressCallback function.
3358 * @progress_callback_data will be passed to this function. It is
3359 * guaranteed that this callback will be called after all data has been
3360 * transferred with the total number of bytes copied during the operation.
3362 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3363 * error is returned, independent on the status of the @destination.
3365 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3366 * then the error %G_IO_ERROR_EXISTS is returned.
3368 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3369 * error is returned. If trying to overwrite a directory with a directory the
3370 * %G_IO_ERROR_WOULD_MERGE error is returned.
3372 * If the source is a directory and the target does not exist, or
3373 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3374 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3375 * move operation isn't available).
3377 * Returns: %TRUE on successful move, %FALSE otherwise.
3380 g_file_move (GFile *source,
3382 GFileCopyFlags flags,
3383 GCancellable *cancellable,
3384 GFileProgressCallback progress_callback,
3385 gpointer progress_callback_data,
3392 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3393 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3395 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3398 iface = G_FILE_GET_IFACE (destination);
3402 res = (* iface->move) (source, destination,
3404 progress_callback, progress_callback_data,
3410 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3412 g_propagate_error (error, my_error);
3417 /* If the types are different, and the destination method failed
3418 * also try the source method
3420 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3422 iface = G_FILE_GET_IFACE (source);
3427 res = (* iface->move) (source, destination,
3429 progress_callback, progress_callback_data,
3435 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3437 g_propagate_error (error, my_error);
3443 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3445 g_set_error_literal (error, G_IO_ERROR,
3446 G_IO_ERROR_NOT_SUPPORTED,
3447 _("Operation not supported"));
3451 flags |= G_FILE_COPY_ALL_METADATA;
3452 if (!g_file_copy (source, destination, flags, cancellable,
3453 progress_callback, progress_callback_data,
3457 return g_file_delete (source, cancellable, error);
3461 * g_file_make_directory:
3462 * @file: input #GFile
3463 * @cancellable: (allow-none): optional #GCancellable object,
3465 * @error: a #GError, or %NULL
3467 * Creates a directory. Note that this will only create a child directory
3468 * of the immediate parent directory of the path or URI given by the #GFile.
3469 * To recursively create directories, see g_file_make_directory_with_parents().
3470 * This function will fail if the parent directory does not exist, setting
3471 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3472 * creating directories, this function will fail, setting @error to
3473 * %G_IO_ERROR_NOT_SUPPORTED.
3475 * For a local #GFile the newly created directory will have the default
3476 * (current) ownership and permissions of the current process.
3478 * If @cancellable is not %NULL, then the operation can be cancelled by
3479 * triggering the cancellable object from another thread. If the operation
3480 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3482 * Returns: %TRUE on successful creation, %FALSE otherwise.
3485 g_file_make_directory (GFile *file,
3486 GCancellable *cancellable,
3491 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3493 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3496 iface = G_FILE_GET_IFACE (file);
3498 if (iface->make_directory == NULL)
3500 g_set_error_literal (error, G_IO_ERROR,
3501 G_IO_ERROR_NOT_SUPPORTED,
3502 _("Operation not supported"));
3506 return (* iface->make_directory) (file, cancellable, error);
3510 * g_file_make_directory_with_parents:
3511 * @file: input #GFile
3512 * @cancellable: (allow-none): optional #GCancellable object,
3514 * @error: a #GError, or %NULL
3516 * Creates a directory and any parent directories that may not
3517 * exist similar to 'mkdir -p'. If the file system does not support
3518 * creating directories, this function will fail, setting @error to
3519 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3520 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3521 * the similar g_mkdir_with_parents().
3523 * For a local #GFile the newly created directories will have the default
3524 * (current) ownership and permissions of the current process.
3526 * If @cancellable is not %NULL, then the operation can be cancelled by
3527 * triggering the cancellable object from another thread. If the operation
3528 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3530 * Returns: %TRUE if all directories have been successfully created, %FALSE
3536 g_file_make_directory_with_parents (GFile *file,
3537 GCancellable *cancellable,
3540 GFile *work_file = NULL;
3541 GList *list = NULL, *l;
3542 GError *my_error = NULL;
3544 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549 g_file_make_directory (file, cancellable, &my_error);
3550 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3553 g_propagate_error (error, my_error);
3554 return my_error == NULL;
3557 work_file = g_object_ref (file);
3559 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3563 parent_file = g_file_get_parent (work_file);
3564 if (parent_file == NULL)
3567 g_clear_error (&my_error);
3568 g_file_make_directory (parent_file, cancellable, &my_error);
3570 g_object_unref (work_file);
3571 work_file = g_object_ref (parent_file);
3573 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3574 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3576 g_object_unref (parent_file);
3579 for (l = list; my_error == NULL && l; l = l->next)
3581 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3585 g_object_unref (work_file);
3588 while (list != NULL)
3590 g_object_unref ((GFile *) list->data);
3591 list = g_list_remove (list, list->data);
3594 if (my_error != NULL)
3596 g_propagate_error (error, my_error);
3600 return g_file_make_directory (file, cancellable, error);
3604 * g_file_make_symbolic_link:
3605 * @file: a #GFile with the name of the symlink to create
3606 * @symlink_value: a string with the path for the target of the new symlink
3607 * @cancellable: (allow-none): optional #GCancellable object,
3611 * Creates a symbolic link named @file which contains the string
3614 * If @cancellable is not %NULL, then the operation can be cancelled by
3615 * triggering the cancellable object from another thread. If the operation
3616 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3618 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3621 g_file_make_symbolic_link (GFile *file,
3622 const char *symlink_value,
3623 GCancellable *cancellable,
3628 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3629 g_return_val_if_fail (symlink_value != NULL, FALSE);
3631 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3634 if (*symlink_value == '\0')
3636 g_set_error_literal (error, G_IO_ERROR,
3637 G_IO_ERROR_INVALID_ARGUMENT,
3638 _("Invalid symlink value given"));
3642 iface = G_FILE_GET_IFACE (file);
3644 if (iface->make_symbolic_link == NULL)
3646 g_set_error_literal (error, G_IO_ERROR,
3647 G_IO_ERROR_NOT_SUPPORTED,
3648 _("Operation not supported"));
3652 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3657 * @file: input #GFile
3658 * @cancellable: (allow-none): optional #GCancellable object,
3660 * @error: a #GError, or %NULL
3662 * Deletes a file. If the @file is a directory, it will only be
3663 * deleted if it is empty. This has the same semantics as g_unlink().
3665 * If @cancellable is not %NULL, then the operation can be cancelled by
3666 * triggering the cancellable object from another thread. If the operation
3667 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3669 * Virtual: delete_file
3670 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3673 g_file_delete (GFile *file,
3674 GCancellable *cancellable,
3679 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3681 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3684 iface = G_FILE_GET_IFACE (file);
3686 if (iface->delete_file == NULL)
3688 g_set_error_literal (error, G_IO_ERROR,
3689 G_IO_ERROR_NOT_SUPPORTED,
3690 _("Operation not supported"));
3694 return (* iface->delete_file) (file, cancellable, error);
3698 * g_file_delete_async:
3699 * @file: input #GFile
3700 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3702 * @cancellable: (allow-none): optional #GCancellable object,
3704 * @callback: a #GAsyncReadyCallback to call
3705 * when the request is satisfied
3706 * @user_data: the data to pass to callback function
3708 * Asynchronously delete a file. If the @file is a directory, it will
3709 * only be deleted if it is empty. This has the same semantics as
3712 * Virtual: delete_file_async
3716 g_file_delete_async (GFile *file,
3718 GCancellable *cancellable,
3719 GAsyncReadyCallback callback,
3724 g_return_if_fail (G_IS_FILE (file));
3726 iface = G_FILE_GET_IFACE (file);
3727 (* iface->delete_file_async) (file,
3735 * g_file_delete_finish:
3736 * @file: input #GFile
3737 * @result: a #GAsyncResult
3738 * @error: a #GError, or %NULL
3740 * Finishes deleting a file started with g_file_delete_async().
3742 * Virtual: delete_file_finish
3746 g_file_delete_finish (GFile *file,
3747 GAsyncResult *result,
3752 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3753 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3755 if (g_async_result_legacy_propagate_error (result, error))
3758 iface = G_FILE_GET_IFACE (file);
3759 return (* iface->delete_file_finish) (file, result, error);
3764 * @file: #GFile to send to trash
3765 * @cancellable: (allow-none): optional #GCancellable object,
3767 * @error: a #GError, or %NULL
3769 * Sends @file to the "Trashcan", if possible. This is similar to
3770 * deleting it, but the user can recover it before emptying the trashcan.
3771 * Not all file systems support trashing, so this call can return the
3772 * %G_IO_ERROR_NOT_SUPPORTED error.
3774 * If @cancellable is not %NULL, then the operation can be cancelled by
3775 * triggering the cancellable object from another thread. If the operation
3776 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3778 * Returns: %TRUE on successful trash, %FALSE otherwise.
3781 g_file_trash (GFile *file,
3782 GCancellable *cancellable,
3787 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3789 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3792 iface = G_FILE_GET_IFACE (file);
3794 if (iface->trash == NULL)
3796 g_set_error_literal (error,
3797 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3798 _("Trash not supported"));
3802 return (* iface->trash) (file, cancellable, error);
3806 * g_file_set_display_name:
3807 * @file: input #GFile
3808 * @display_name: a string
3809 * @cancellable: (allow-none): optional #GCancellable object,
3811 * @error: a #GError, or %NULL
3813 * Renames @file to the specified display name.
3815 * The display name is converted from UTF-8 to the correct encoding
3816 * for the target filesystem if possible and the @file is renamed to this.
3818 * If you want to implement a rename operation in the user interface the
3819 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3820 * initial value in the rename widget, and then the result after editing
3821 * should be passed to g_file_set_display_name().
3823 * On success the resulting converted filename is returned.
3825 * If @cancellable is not %NULL, then the operation can be cancelled by
3826 * triggering the cancellable object from another thread. If the operation
3827 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3829 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3830 * or %NULL if there was an error.
3831 * Free the returned object with g_object_unref().
3834 g_file_set_display_name (GFile *file,
3835 const gchar *display_name,
3836 GCancellable *cancellable,
3841 g_return_val_if_fail (G_IS_FILE (file), NULL);
3842 g_return_val_if_fail (display_name != NULL, NULL);
3844 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3848 G_IO_ERROR_INVALID_ARGUMENT,
3849 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3853 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3856 iface = G_FILE_GET_IFACE (file);
3858 return (* iface->set_display_name) (file, display_name, cancellable, error);
3862 * g_file_set_display_name_async:
3863 * @file: input #GFile
3864 * @display_name: a string
3865 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3867 * @cancellable: (allow-none): optional #GCancellable object,
3869 * @callback: (scope async): a #GAsyncReadyCallback to call
3870 * when the request is satisfied
3871 * @user_data: (closure): the data to pass to callback function
3873 * Asynchronously sets the display name for a given #GFile.
3875 * For more details, see g_file_set_display_name() which is
3876 * the synchronous version of this call.
3878 * When the operation is finished, @callback will be called.
3879 * You can then call g_file_set_display_name_finish() to get
3880 * the result of the operation.
3883 g_file_set_display_name_async (GFile *file,
3884 const gchar *display_name,
3886 GCancellable *cancellable,
3887 GAsyncReadyCallback callback,
3892 g_return_if_fail (G_IS_FILE (file));
3893 g_return_if_fail (display_name != NULL);
3895 iface = G_FILE_GET_IFACE (file);
3896 (* iface->set_display_name_async) (file,
3905 * g_file_set_display_name_finish:
3906 * @file: input #GFile
3907 * @res: a #GAsyncResult
3908 * @error: a #GError, or %NULL
3910 * Finishes setting a display name started with
3911 * g_file_set_display_name_async().
3913 * Returns: (transfer full): a #GFile or %NULL on error.
3914 * Free the returned object with g_object_unref().
3917 g_file_set_display_name_finish (GFile *file,
3923 g_return_val_if_fail (G_IS_FILE (file), NULL);
3924 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3926 if (g_async_result_legacy_propagate_error (res, error))
3929 iface = G_FILE_GET_IFACE (file);
3930 return (* iface->set_display_name_finish) (file, res, error);
3934 * g_file_query_settable_attributes:
3935 * @file: input #GFile
3936 * @cancellable: (allow-none): optional #GCancellable object,
3938 * @error: a #GError, or %NULL
3940 * Obtain the list of settable attributes for the file.
3942 * Returns the type and full attribute name of all the attributes
3943 * that can be set on this file. This doesn't mean setting it will
3944 * always succeed though, you might get an access failure, or some
3945 * specific file may not support a specific attribute.
3947 * If @cancellable is not %NULL, then the operation can be cancelled by
3948 * triggering the cancellable object from another thread. If the operation
3949 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3951 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3952 * When you are done with it, release it with
3953 * g_file_attribute_info_list_unref()
3955 GFileAttributeInfoList *
3956 g_file_query_settable_attributes (GFile *file,
3957 GCancellable *cancellable,
3962 GFileAttributeInfoList *list;
3964 g_return_val_if_fail (G_IS_FILE (file), NULL);
3966 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3969 iface = G_FILE_GET_IFACE (file);
3971 if (iface->query_settable_attributes == NULL)
3972 return g_file_attribute_info_list_new ();
3975 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3979 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3981 list = g_file_attribute_info_list_new ();
3982 g_error_free (my_error);
3985 g_propagate_error (error, my_error);
3992 * g_file_query_writable_namespaces:
3993 * @file: input #GFile
3994 * @cancellable: (allow-none): optional #GCancellable object,
3996 * @error: a #GError, or %NULL
3998 * Obtain the list of attribute namespaces where new attributes
3999 * can be created by a user. An example of this is extended
4000 * attributes (in the "xattr" namespace).
4002 * If @cancellable is not %NULL, then the operation can be cancelled by
4003 * triggering the cancellable object from another thread. If the operation
4004 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4006 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4007 * When you are done with it, release it with
4008 * g_file_attribute_info_list_unref()
4010 GFileAttributeInfoList *
4011 g_file_query_writable_namespaces (GFile *file,
4012 GCancellable *cancellable,
4017 GFileAttributeInfoList *list;
4019 g_return_val_if_fail (G_IS_FILE (file), NULL);
4021 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4024 iface = G_FILE_GET_IFACE (file);
4026 if (iface->query_writable_namespaces == NULL)
4027 return g_file_attribute_info_list_new ();
4030 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4034 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4036 list = g_file_attribute_info_list_new ();
4037 g_error_free (my_error);
4040 g_propagate_error (error, my_error);
4047 * g_file_set_attribute:
4048 * @file: input #GFile
4049 * @attribute: a string containing the attribute's name
4050 * @type: The type of the attribute
4051 * @value_p: (allow-none): a pointer to the value (or the pointer
4052 * itself if the type is a pointer type)
4053 * @flags: a set of #GFileQueryInfoFlags
4054 * @cancellable: (allow-none): optional #GCancellable object,
4056 * @error: a #GError, or %NULL
4058 * Sets an attribute in the file with attribute name @attribute to @value.
4060 * Some attributes can be unset by setting @attribute to
4061 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4063 * If @cancellable is not %NULL, then the operation can be cancelled by
4064 * triggering the cancellable object from another thread. If the operation
4065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4067 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4070 g_file_set_attribute (GFile *file,
4071 const gchar *attribute,
4072 GFileAttributeType type,
4074 GFileQueryInfoFlags flags,
4075 GCancellable *cancellable,
4080 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4081 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4083 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4086 iface = G_FILE_GET_IFACE (file);
4088 if (iface->set_attribute == NULL)
4090 g_set_error_literal (error, G_IO_ERROR,
4091 G_IO_ERROR_NOT_SUPPORTED,
4092 _("Operation not supported"));
4096 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4100 * g_file_set_attributes_from_info:
4101 * @file: input #GFile
4102 * @info: a #GFileInfo
4103 * @flags: #GFileQueryInfoFlags
4104 * @cancellable: (allow-none): optional #GCancellable object,
4106 * @error: a #GError, or %NULL
4108 * Tries to set all attributes in the #GFileInfo on the target
4109 * values, not stopping on the first error.
4111 * If there is any error during this operation then @error will
4112 * be set to the first error. Error on particular fields are flagged
4113 * by setting the "status" field in the attribute value to
4114 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4115 * also detect further errors.
4117 * If @cancellable is not %NULL, then the operation can be cancelled by
4118 * triggering the cancellable object from another thread. If the operation
4119 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4121 * Returns: %FALSE if there was any error, %TRUE otherwise.
4124 g_file_set_attributes_from_info (GFile *file,
4126 GFileQueryInfoFlags flags,
4127 GCancellable *cancellable,
4132 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4133 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4135 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4138 g_file_info_clear_status (info);
4140 iface = G_FILE_GET_IFACE (file);
4142 return (* iface->set_attributes_from_info) (file,
4150 g_file_real_set_attributes_from_info (GFile *file,
4152 GFileQueryInfoFlags flags,
4153 GCancellable *cancellable,
4159 GFileAttributeValue *value;
4163 attributes = g_file_info_list_attributes (info, NULL);
4165 for (i = 0; attributes[i] != NULL; i++)
4167 value = _g_file_info_get_attribute_value (info, attributes[i]);
4169 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4172 if (!g_file_set_attribute (file, attributes[i],
4173 value->type, _g_file_attribute_value_peek_as_pointer (value),
4174 flags, cancellable, error))
4176 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4178 /* Don't set error multiple times */
4182 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4185 g_strfreev (attributes);
4191 * g_file_set_attributes_async:
4192 * @file: input #GFile
4193 * @info: a #GFileInfo
4194 * @flags: a #GFileQueryInfoFlags
4195 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4197 * @cancellable: (allow-none): optional #GCancellable object,
4199 * @callback: (scope async): a #GAsyncReadyCallback
4200 * @user_data: (closure): a #gpointer
4202 * Asynchronously sets the attributes of @file with @info.
4204 * For more details, see g_file_set_attributes_from_info(),
4205 * which is the synchronous version of this call.
4207 * When the operation is finished, @callback will be called.
4208 * You can then call g_file_set_attributes_finish() to get
4209 * the result of the operation.
4212 g_file_set_attributes_async (GFile *file,
4214 GFileQueryInfoFlags flags,
4216 GCancellable *cancellable,
4217 GAsyncReadyCallback callback,
4222 g_return_if_fail (G_IS_FILE (file));
4223 g_return_if_fail (G_IS_FILE_INFO (info));
4225 iface = G_FILE_GET_IFACE (file);
4226 (* iface->set_attributes_async) (file,
4236 * g_file_set_attributes_finish:
4237 * @file: input #GFile
4238 * @result: a #GAsyncResult
4239 * @info: (out) (transfer full): a #GFileInfo
4240 * @error: a #GError, or %NULL
4242 * Finishes setting an attribute started in g_file_set_attributes_async().
4244 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4247 g_file_set_attributes_finish (GFile *file,
4248 GAsyncResult *result,
4254 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4255 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4257 /* No standard handling of errors here, as we must set info even
4260 iface = G_FILE_GET_IFACE (file);
4261 return (* iface->set_attributes_finish) (file, result, info, error);
4265 * g_file_set_attribute_string:
4266 * @file: input #GFile
4267 * @attribute: a string containing the attribute's name
4268 * @value: a string containing the attribute's value
4269 * @flags: #GFileQueryInfoFlags
4270 * @cancellable: (allow-none): optional #GCancellable object,
4272 * @error: a #GError, or %NULL
4274 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4275 * If @attribute is of a different type, this operation will fail.
4277 * If @cancellable is not %NULL, then the operation can be cancelled by
4278 * triggering the cancellable object from another thread. If the operation
4279 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4281 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4284 g_file_set_attribute_string (GFile *file,
4285 const char *attribute,
4287 GFileQueryInfoFlags flags,
4288 GCancellable *cancellable,
4291 return g_file_set_attribute (file, attribute,
4292 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4293 flags, cancellable, error);
4297 * g_file_set_attribute_byte_string:
4298 * @file: input #GFile
4299 * @attribute: a string containing the attribute's name
4300 * @value: a string containing the attribute's new value
4301 * @flags: a #GFileQueryInfoFlags
4302 * @cancellable: (allow-none): optional #GCancellable object,
4304 * @error: a #GError, or %NULL
4306 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4307 * If @attribute is of a different type, this operation will fail,
4310 * If @cancellable is not %NULL, then the operation can be cancelled by
4311 * triggering the cancellable object from another thread. If the operation
4312 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4314 * Returns: %TRUE if the @attribute was successfully set to @value
4315 * in the @file, %FALSE otherwise.
4318 g_file_set_attribute_byte_string (GFile *file,
4319 const gchar *attribute,
4321 GFileQueryInfoFlags flags,
4322 GCancellable *cancellable,
4325 return g_file_set_attribute (file, attribute,
4326 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4327 flags, cancellable, error);
4331 * g_file_set_attribute_uint32:
4332 * @file: input #GFile
4333 * @attribute: a string containing the attribute's name
4334 * @value: a #guint32 containing the attribute's new value
4335 * @flags: a #GFileQueryInfoFlags
4336 * @cancellable: (allow-none): optional #GCancellable object,
4338 * @error: a #GError, or %NULL
4340 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4341 * If @attribute is of a different type, this operation will fail.
4343 * If @cancellable is not %NULL, then the operation can be cancelled by
4344 * triggering the cancellable object from another thread. If the operation
4345 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4347 * Returns: %TRUE if the @attribute was successfully set to @value
4348 * in the @file, %FALSE otherwise.
4351 g_file_set_attribute_uint32 (GFile *file,
4352 const gchar *attribute,
4354 GFileQueryInfoFlags flags,
4355 GCancellable *cancellable,
4358 return g_file_set_attribute (file, attribute,
4359 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4360 flags, cancellable, error);
4364 * g_file_set_attribute_int32:
4365 * @file: input #GFile
4366 * @attribute: a string containing the attribute's name
4367 * @value: a #gint32 containing the attribute's new value
4368 * @flags: a #GFileQueryInfoFlags
4369 * @cancellable: (allow-none): optional #GCancellable object,
4371 * @error: a #GError, or %NULL
4373 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4374 * If @attribute is of a different type, this operation will fail.
4376 * If @cancellable is not %NULL, then the operation can be cancelled by
4377 * triggering the cancellable object from another thread. If the operation
4378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4380 * Returns: %TRUE if the @attribute was successfully set to @value
4381 * in the @file, %FALSE otherwise.
4384 g_file_set_attribute_int32 (GFile *file,
4385 const gchar *attribute,
4387 GFileQueryInfoFlags flags,
4388 GCancellable *cancellable,
4391 return g_file_set_attribute (file, attribute,
4392 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4393 flags, cancellable, error);
4397 * g_file_set_attribute_uint64:
4398 * @file: input #GFile
4399 * @attribute: a string containing the attribute's name
4400 * @value: a #guint64 containing the attribute's new value
4401 * @flags: a #GFileQueryInfoFlags
4402 * @cancellable: (allow-none): optional #GCancellable object,
4404 * @error: a #GError, or %NULL
4406 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4407 * If @attribute is of a different type, this operation will fail.
4409 * If @cancellable is not %NULL, then the operation can be cancelled by
4410 * triggering the cancellable object from another thread. If the operation
4411 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4413 * Returns: %TRUE if the @attribute was successfully set to @value
4414 * in the @file, %FALSE otherwise.
4417 g_file_set_attribute_uint64 (GFile *file,
4418 const gchar *attribute,
4420 GFileQueryInfoFlags flags,
4421 GCancellable *cancellable,
4424 return g_file_set_attribute (file, attribute,
4425 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4426 flags, cancellable, error);
4430 * g_file_set_attribute_int64:
4431 * @file: input #GFile
4432 * @attribute: a string containing the attribute's name
4433 * @value: a #guint64 containing the attribute's new value
4434 * @flags: a #GFileQueryInfoFlags
4435 * @cancellable: (allow-none): optional #GCancellable object,
4437 * @error: a #GError, or %NULL
4439 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4440 * If @attribute is of a different type, this operation will fail.
4442 * If @cancellable is not %NULL, then the operation can be cancelled by
4443 * triggering the cancellable object from another thread. If the operation
4444 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4446 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4449 g_file_set_attribute_int64 (GFile *file,
4450 const gchar *attribute,
4452 GFileQueryInfoFlags flags,
4453 GCancellable *cancellable,
4456 return g_file_set_attribute (file, attribute,
4457 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4458 flags, cancellable, error);
4462 * g_file_mount_mountable:
4463 * @file: input #GFile
4464 * @flags: flags affecting the operation
4465 * @mount_operation: (allow-none): a #GMountOperation,
4466 * or %NULL to avoid user interaction
4467 * @cancellable: (allow-none): optional #GCancellable object,
4469 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4470 * when the request is satisfied, or %NULL
4471 * @user_data: (closure): the data to pass to callback function
4473 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4474 * Using @mount_operation, you can request callbacks when, for instance,
4475 * passwords are needed during authentication.
4477 * If @cancellable is not %NULL, then the operation can be cancelled by
4478 * triggering the cancellable object from another thread. If the operation
4479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4481 * When the operation is finished, @callback will be called.
4482 * You can then call g_file_mount_mountable_finish() to get
4483 * the result of the operation.
4486 g_file_mount_mountable (GFile *file,
4487 GMountMountFlags flags,
4488 GMountOperation *mount_operation,
4489 GCancellable *cancellable,
4490 GAsyncReadyCallback callback,
4495 g_return_if_fail (G_IS_FILE (file));
4497 iface = G_FILE_GET_IFACE (file);
4499 if (iface->mount_mountable == NULL)
4501 g_task_report_new_error (file, callback, user_data,
4502 g_file_mount_mountable,
4503 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4504 _("Operation not supported"));
4508 (* iface->mount_mountable) (file,
4517 * g_file_mount_mountable_finish:
4518 * @file: input #GFile
4519 * @result: a #GAsyncResult
4520 * @error: a #GError, or %NULL
4522 * Finishes a mount operation. See g_file_mount_mountable() for details.
4524 * Finish an asynchronous mount operation that was started
4525 * with g_file_mount_mountable().
4527 * Returns: (transfer full): a #GFile or %NULL on error.
4528 * Free the returned object with g_object_unref().
4531 g_file_mount_mountable_finish (GFile *file,
4532 GAsyncResult *result,
4537 g_return_val_if_fail (G_IS_FILE (file), NULL);
4538 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4540 if (g_async_result_legacy_propagate_error (result, error))
4542 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4543 return g_task_propagate_pointer (G_TASK (result), error);
4545 iface = G_FILE_GET_IFACE (file);
4546 return (* iface->mount_mountable_finish) (file, result, error);
4550 * g_file_unmount_mountable:
4551 * @file: input #GFile
4552 * @flags: flags affecting the operation
4553 * @cancellable: (allow-none): optional #GCancellable object,
4555 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4556 * when the request is satisfied, or %NULL
4557 * @user_data: (closure): the data to pass to callback function
4559 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4561 * If @cancellable is not %NULL, then the operation can be cancelled by
4562 * triggering the cancellable object from another thread. If the operation
4563 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4565 * When the operation is finished, @callback will be called.
4566 * You can then call g_file_unmount_mountable_finish() to get
4567 * the result of the operation.
4569 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4572 g_file_unmount_mountable (GFile *file,
4573 GMountUnmountFlags flags,
4574 GCancellable *cancellable,
4575 GAsyncReadyCallback callback,
4580 g_return_if_fail (G_IS_FILE (file));
4582 iface = G_FILE_GET_IFACE (file);
4584 if (iface->unmount_mountable == NULL)
4586 g_task_report_new_error (file, callback, user_data,
4587 g_file_unmount_mountable_with_operation,
4588 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4589 _("Operation not supported"));
4593 (* iface->unmount_mountable) (file,
4601 * g_file_unmount_mountable_finish:
4602 * @file: input #GFile
4603 * @result: a #GAsyncResult
4604 * @error: a #GError, or %NULL
4606 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4608 * Finish an asynchronous unmount operation that was started
4609 * with g_file_unmount_mountable().
4611 * Returns: %TRUE if the operation finished successfully.
4614 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4618 g_file_unmount_mountable_finish (GFile *file,
4619 GAsyncResult *result,
4624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4625 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4627 if (g_async_result_legacy_propagate_error (result, error))
4629 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4630 return g_task_propagate_boolean (G_TASK (result), error);
4632 iface = G_FILE_GET_IFACE (file);
4633 return (* iface->unmount_mountable_finish) (file, result, error);
4637 * g_file_unmount_mountable_with_operation:
4638 * @file: input #GFile
4639 * @flags: flags affecting the operation
4640 * @mount_operation: (allow-none): a #GMountOperation,
4641 * or %NULL to avoid user interaction
4642 * @cancellable: (allow-none): optional #GCancellable object,
4644 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4645 * when the request is satisfied, or %NULL
4646 * @user_data: (closure): the data to pass to callback function
4648 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4650 * If @cancellable is not %NULL, then the operation can be cancelled by
4651 * triggering the cancellable object from another thread. If the operation
4652 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4654 * When the operation is finished, @callback will be called.
4655 * You can then call g_file_unmount_mountable_finish() to get
4656 * the result of the operation.
4661 g_file_unmount_mountable_with_operation (GFile *file,
4662 GMountUnmountFlags flags,
4663 GMountOperation *mount_operation,
4664 GCancellable *cancellable,
4665 GAsyncReadyCallback callback,
4670 g_return_if_fail (G_IS_FILE (file));
4672 iface = G_FILE_GET_IFACE (file);
4674 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4676 g_task_report_new_error (file, callback, user_data,
4677 g_file_unmount_mountable_with_operation,
4678 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4679 _("Operation not supported"));
4683 if (iface->unmount_mountable_with_operation != NULL)
4684 (* iface->unmount_mountable_with_operation) (file,
4691 (* iface->unmount_mountable) (file,
4699 * g_file_unmount_mountable_with_operation_finish:
4700 * @file: input #GFile
4701 * @result: a #GAsyncResult
4702 * @error: a #GError, or %NULL
4704 * Finishes an unmount operation,
4705 * see g_file_unmount_mountable_with_operation() for details.
4707 * Finish an asynchronous unmount operation that was started
4708 * with g_file_unmount_mountable_with_operation().
4710 * Returns: %TRUE if the operation finished successfully.
4716 g_file_unmount_mountable_with_operation_finish (GFile *file,
4717 GAsyncResult *result,
4722 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4723 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4725 if (g_async_result_legacy_propagate_error (result, error))
4727 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4728 return g_task_propagate_boolean (G_TASK (result), error);
4730 iface = G_FILE_GET_IFACE (file);
4731 if (iface->unmount_mountable_with_operation_finish != NULL)
4732 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4734 return (* iface->unmount_mountable_finish) (file, result, error);
4738 * g_file_eject_mountable:
4739 * @file: input #GFile
4740 * @flags: flags affecting the operation
4741 * @cancellable: (allow-none): optional #GCancellable object,
4743 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4744 * when the request is satisfied, or %NULL
4745 * @user_data: (closure): the data to pass to callback function
4747 * Starts an asynchronous eject on a mountable.
4748 * When this operation has completed, @callback will be called with
4749 * @user_user data, and the operation can be finalized with
4750 * g_file_eject_mountable_finish().
4752 * If @cancellable is not %NULL, then the operation can be cancelled by
4753 * triggering the cancellable object from another thread. If the operation
4754 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4756 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4759 g_file_eject_mountable (GFile *file,
4760 GMountUnmountFlags flags,
4761 GCancellable *cancellable,
4762 GAsyncReadyCallback callback,
4767 g_return_if_fail (G_IS_FILE (file));
4769 iface = G_FILE_GET_IFACE (file);
4771 if (iface->eject_mountable == NULL)
4773 g_task_report_new_error (file, callback, user_data,
4774 g_file_eject_mountable_with_operation,
4775 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4776 _("Operation not supported"));
4780 (* iface->eject_mountable) (file,
4788 * g_file_eject_mountable_finish:
4789 * @file: input #GFile
4790 * @result: a #GAsyncResult
4791 * @error: a #GError, or %NULL
4793 * Finishes an asynchronous eject operation started by
4794 * g_file_eject_mountable().
4796 * Returns: %TRUE if the @file was ejected successfully.
4799 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4803 g_file_eject_mountable_finish (GFile *file,
4804 GAsyncResult *result,
4809 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4810 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4812 if (g_async_result_legacy_propagate_error (result, error))
4814 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4815 return g_task_propagate_boolean (G_TASK (result), error);
4817 iface = G_FILE_GET_IFACE (file);
4818 return (* iface->eject_mountable_finish) (file, result, error);
4822 * g_file_eject_mountable_with_operation:
4823 * @file: input #GFile
4824 * @flags: flags affecting the operation
4825 * @mount_operation: (allow-none): a #GMountOperation,
4826 * or %NULL to avoid user interaction
4827 * @cancellable: (allow-none): optional #GCancellable object,
4829 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4830 * when the request is satisfied, or %NULL
4831 * @user_data: (closure): the data to pass to callback function
4833 * Starts an asynchronous eject on a mountable.
4834 * When this operation has completed, @callback will be called with
4835 * @user_user data, and the operation can be finalized with
4836 * g_file_eject_mountable_with_operation_finish().
4838 * If @cancellable is not %NULL, then the operation can be cancelled by
4839 * triggering the cancellable object from another thread. If the operation
4840 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4845 g_file_eject_mountable_with_operation (GFile *file,
4846 GMountUnmountFlags flags,
4847 GMountOperation *mount_operation,
4848 GCancellable *cancellable,
4849 GAsyncReadyCallback callback,
4854 g_return_if_fail (G_IS_FILE (file));
4856 iface = G_FILE_GET_IFACE (file);
4858 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4860 g_task_report_new_error (file, callback, user_data,
4861 g_file_eject_mountable_with_operation,
4862 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4863 _("Operation not supported"));
4867 if (iface->eject_mountable_with_operation != NULL)
4868 (* iface->eject_mountable_with_operation) (file,
4875 (* iface->eject_mountable) (file,
4883 * g_file_eject_mountable_with_operation_finish:
4884 * @file: input #GFile
4885 * @result: a #GAsyncResult
4886 * @error: a #GError, or %NULL
4888 * Finishes an asynchronous eject operation started by
4889 * g_file_eject_mountable_with_operation().
4891 * Returns: %TRUE if the @file was ejected successfully.
4897 g_file_eject_mountable_with_operation_finish (GFile *file,
4898 GAsyncResult *result,
4903 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4904 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4906 if (g_async_result_legacy_propagate_error (result, error))
4908 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4909 return g_task_propagate_boolean (G_TASK (result), error);
4911 iface = G_FILE_GET_IFACE (file);
4912 if (iface->eject_mountable_with_operation_finish != NULL)
4913 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4915 return (* iface->eject_mountable_finish) (file, result, error);
4919 * g_file_monitor_directory:
4920 * @file: input #GFile
4921 * @flags: a set of #GFileMonitorFlags
4922 * @cancellable: (allow-none): optional #GCancellable object,
4924 * @error: a #GError, or %NULL
4926 * Obtains a directory monitor for the given file.
4927 * This may fail if directory monitoring is not supported.
4929 * If @cancellable is not %NULL, then the operation can be cancelled by
4930 * triggering the cancellable object from another thread. If the operation
4931 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4933 * It does not make sense for @flags to contain
4934 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4935 * directories. It is not possible to monitor all the files in a
4936 * directory for changes made via hard links; if you want to do this then
4937 * you must register individual watches with g_file_monitor().
4939 * Virtual: monitor_dir
4940 * Returns: (transfer full): a #GFileMonitor for the given @file,
4941 * or %NULL on error.
4942 * Free the returned object with g_object_unref().
4945 g_file_monitor_directory (GFile *file,
4946 GFileMonitorFlags flags,
4947 GCancellable *cancellable,
4952 g_return_val_if_fail (G_IS_FILE (file), NULL);
4953 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4955 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4958 iface = G_FILE_GET_IFACE (file);
4960 if (iface->monitor_dir == NULL)
4962 g_set_error_literal (error, G_IO_ERROR,
4963 G_IO_ERROR_NOT_SUPPORTED,
4964 _("Operation not supported"));
4968 return (* iface->monitor_dir) (file, flags, cancellable, error);
4972 * g_file_monitor_file:
4973 * @file: input #GFile
4974 * @flags: a set of #GFileMonitorFlags
4975 * @cancellable: (allow-none): optional #GCancellable object,
4977 * @error: a #GError, or %NULL
4979 * Obtains a file monitor for the given file. If no file notification
4980 * mechanism exists, then regular polling of the file is used.
4982 * If @cancellable is not %NULL, then the operation can be cancelled by
4983 * triggering the cancellable object from another thread. If the operation
4984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4986 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
4987 * will also attempt to report changes made to the file via another
4988 * filename (ie, a hard link). Without this flag, you can only rely on
4989 * changes made through the filename contained in @file to be
4990 * reported. Using this flag may result in an increase in resource
4991 * usage, and may not have any effect depending on the #GFileMonitor
4992 * backend and/or filesystem type.
4994 * Returns: (transfer full): a #GFileMonitor for the given @file,
4995 * or %NULL on error.
4996 * Free the returned object with g_object_unref().
4999 g_file_monitor_file (GFile *file,
5000 GFileMonitorFlags flags,
5001 GCancellable *cancellable,
5005 GFileMonitor *monitor;
5007 g_return_val_if_fail (G_IS_FILE (file), NULL);
5009 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5012 iface = G_FILE_GET_IFACE (file);
5016 if (iface->monitor_file)
5017 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5019 /* Fallback to polling */
5020 if (monitor == NULL)
5021 monitor = _g_poll_file_monitor_new (file);
5028 * @file: input #GFile
5029 * @flags: a set of #GFileMonitorFlags
5030 * @cancellable: (allow-none): optional #GCancellable object,
5032 * @error: a #GError, or %NULL
5034 * Obtains a file or directory monitor for the given file,
5035 * depending on the type of the file.
5037 * If @cancellable is not %NULL, then the operation can be cancelled by
5038 * triggering the cancellable object from another thread. If the operation
5039 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5041 * Returns: (transfer full): a #GFileMonitor for the given @file,
5042 * or %NULL on error.
5043 * Free the returned object with g_object_unref().
5048 g_file_monitor (GFile *file,
5049 GFileMonitorFlags flags,
5050 GCancellable *cancellable,
5053 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5054 return g_file_monitor_directory (file,
5055 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5056 cancellable, error);
5058 return g_file_monitor_file (file, flags, cancellable, error);
5061 /********************************************
5062 * Default implementation of async ops *
5063 ********************************************/
5067 GFileQueryInfoFlags flags;
5068 } QueryInfoAsyncData;
5071 query_info_data_free (QueryInfoAsyncData *data)
5073 g_free (data->attributes);
5078 query_info_async_thread (GTask *task,
5081 GCancellable *cancellable)
5083 QueryInfoAsyncData *data = task_data;
5085 GError *error = NULL;
5087 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5089 g_task_return_pointer (task, info, g_object_unref);
5091 g_task_return_error (task, error);
5095 g_file_real_query_info_async (GFile *file,
5096 const char *attributes,
5097 GFileQueryInfoFlags flags,
5099 GCancellable *cancellable,
5100 GAsyncReadyCallback callback,
5104 QueryInfoAsyncData *data;
5106 data = g_new0 (QueryInfoAsyncData, 1);
5107 data->attributes = g_strdup (attributes);
5108 data->flags = flags;
5110 task = g_task_new (file, cancellable, callback, user_data);
5111 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5112 g_task_set_priority (task, io_priority);
5113 g_task_run_in_thread (task, query_info_async_thread);
5114 g_object_unref (task);
5118 g_file_real_query_info_finish (GFile *file,
5122 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5124 return g_task_propagate_pointer (G_TASK (res), error);
5128 query_filesystem_info_async_thread (GTask *task,
5131 GCancellable *cancellable)
5133 const char *attributes = task_data;
5135 GError *error = NULL;
5137 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5139 g_task_return_pointer (task, info, g_object_unref);
5141 g_task_return_error (task, error);
5145 g_file_real_query_filesystem_info_async (GFile *file,
5146 const char *attributes,
5148 GCancellable *cancellable,
5149 GAsyncReadyCallback callback,
5154 task = g_task_new (file, cancellable, callback, user_data);
5155 g_task_set_task_data (task, g_strdup (attributes), g_free);
5156 g_task_set_priority (task, io_priority);
5157 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5158 g_object_unref (task);
5162 g_file_real_query_filesystem_info_finish (GFile *file,
5166 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5168 return g_task_propagate_pointer (G_TASK (res), error);
5172 enumerate_children_async_thread (GTask *task,
5175 GCancellable *cancellable)
5177 QueryInfoAsyncData *data = task_data;
5178 GFileEnumerator *enumerator;
5179 GError *error = NULL;
5181 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5183 g_task_return_error (task, error);
5185 g_task_return_pointer (task, enumerator, g_object_unref);
5189 g_file_real_enumerate_children_async (GFile *file,
5190 const char *attributes,
5191 GFileQueryInfoFlags flags,
5193 GCancellable *cancellable,
5194 GAsyncReadyCallback callback,
5198 QueryInfoAsyncData *data;
5200 data = g_new0 (QueryInfoAsyncData, 1);
5201 data->attributes = g_strdup (attributes);
5202 data->flags = flags;
5204 task = g_task_new (file, cancellable, callback, user_data);
5205 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5206 g_task_set_priority (task, io_priority);
5207 g_task_run_in_thread (task, enumerate_children_async_thread);
5208 g_object_unref (task);
5211 static GFileEnumerator *
5212 g_file_real_enumerate_children_finish (GFile *file,
5216 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5218 return g_task_propagate_pointer (G_TASK (res), error);
5222 open_read_async_thread (GTask *task,
5225 GCancellable *cancellable)
5228 GFileInputStream *stream;
5229 GError *error = NULL;
5231 iface = G_FILE_GET_IFACE (object);
5233 if (iface->read_fn == NULL)
5235 g_task_return_new_error (task, G_IO_ERROR,
5236 G_IO_ERROR_NOT_SUPPORTED,
5237 _("Operation not supported"));
5241 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5243 g_task_return_pointer (task, stream, g_object_unref);
5245 g_task_return_error (task, error);
5249 g_file_real_read_async (GFile *file,
5251 GCancellable *cancellable,
5252 GAsyncReadyCallback callback,
5257 task = g_task_new (file, cancellable, callback, user_data);
5258 g_task_set_priority (task, io_priority);
5259 g_task_run_in_thread (task, open_read_async_thread);
5260 g_object_unref (task);
5263 static GFileInputStream *
5264 g_file_real_read_finish (GFile *file,
5268 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5270 return g_task_propagate_pointer (G_TASK (res), error);
5274 append_to_async_thread (GTask *task,
5275 gpointer source_object,
5277 GCancellable *cancellable)
5280 GFileCreateFlags *data = task_data;
5281 GFileOutputStream *stream;
5282 GError *error = NULL;
5284 iface = G_FILE_GET_IFACE (source_object);
5286 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5288 g_task_return_pointer (task, stream, g_object_unref);
5290 g_task_return_error (task, error);
5294 g_file_real_append_to_async (GFile *file,
5295 GFileCreateFlags flags,
5297 GCancellable *cancellable,
5298 GAsyncReadyCallback callback,
5301 GFileCreateFlags *data;
5304 data = g_new0 (GFileCreateFlags, 1);
5307 task = g_task_new (file, cancellable, callback, user_data);
5308 g_task_set_task_data (task, data, g_free);
5309 g_task_set_priority (task, io_priority);
5311 g_task_run_in_thread (task, append_to_async_thread);
5312 g_object_unref (task);
5315 static GFileOutputStream *
5316 g_file_real_append_to_finish (GFile *file,
5320 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5322 return g_task_propagate_pointer (G_TASK (res), error);
5326 create_async_thread (GTask *task,
5327 gpointer source_object,
5329 GCancellable *cancellable)
5332 GFileCreateFlags *data = task_data;
5333 GFileOutputStream *stream;
5334 GError *error = NULL;
5336 iface = G_FILE_GET_IFACE (source_object);
5338 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5340 g_task_return_pointer (task, stream, g_object_unref);
5342 g_task_return_error (task, error);
5346 g_file_real_create_async (GFile *file,
5347 GFileCreateFlags flags,
5349 GCancellable *cancellable,
5350 GAsyncReadyCallback callback,
5353 GFileCreateFlags *data;
5356 data = g_new0 (GFileCreateFlags, 1);
5359 task = g_task_new (file, cancellable, callback, user_data);
5360 g_task_set_task_data (task, data, g_free);
5361 g_task_set_priority (task, io_priority);
5363 g_task_run_in_thread (task, create_async_thread);
5364 g_object_unref (task);
5367 static GFileOutputStream *
5368 g_file_real_create_finish (GFile *file,
5372 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5374 return g_task_propagate_pointer (G_TASK (res), error);
5378 GFileOutputStream *stream;
5380 gboolean make_backup;
5381 GFileCreateFlags flags;
5385 replace_async_data_free (ReplaceAsyncData *data)
5388 g_object_unref (data->stream);
5389 g_free (data->etag);
5394 replace_async_thread (GTask *task,
5395 gpointer source_object,
5397 GCancellable *cancellable)
5400 GFileOutputStream *stream;
5401 ReplaceAsyncData *data = task_data;
5402 GError *error = NULL;
5404 iface = G_FILE_GET_IFACE (source_object);
5406 stream = iface->replace (G_FILE (source_object),
5414 g_task_return_pointer (task, stream, g_object_unref);
5416 g_task_return_error (task, error);
5420 g_file_real_replace_async (GFile *file,
5422 gboolean make_backup,
5423 GFileCreateFlags flags,
5425 GCancellable *cancellable,
5426 GAsyncReadyCallback callback,
5430 ReplaceAsyncData *data;
5432 data = g_new0 (ReplaceAsyncData, 1);
5433 data->etag = g_strdup (etag);
5434 data->make_backup = make_backup;
5435 data->flags = flags;
5437 task = g_task_new (file, cancellable, callback, user_data);
5438 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5439 g_task_set_priority (task, io_priority);
5441 g_task_run_in_thread (task, replace_async_thread);
5442 g_object_unref (task);
5445 static GFileOutputStream *
5446 g_file_real_replace_finish (GFile *file,
5450 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5452 return g_task_propagate_pointer (G_TASK (res), error);
5456 delete_async_thread (GTask *task,
5459 GCancellable *cancellable)
5461 GFile *file = object;
5463 GError *error = NULL;
5465 iface = G_FILE_GET_IFACE (object);
5467 if (iface->delete_file (file,
5470 g_task_return_boolean (task, TRUE);
5472 g_task_return_error (task, error);
5476 g_file_real_delete_async (GFile *file,
5478 GCancellable *cancellable,
5479 GAsyncReadyCallback callback,
5484 task = g_task_new (file, cancellable, callback, user_data);
5485 g_task_set_priority (task, io_priority);
5486 g_task_run_in_thread (task, delete_async_thread);
5487 g_object_unref (task);
5491 g_file_real_delete_finish (GFile *file,
5495 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5497 return g_task_propagate_boolean (G_TASK (res), error);
5501 open_readwrite_async_thread (GTask *task,
5504 GCancellable *cancellable)
5507 GFileIOStream *stream;
5508 GError *error = NULL;
5510 iface = G_FILE_GET_IFACE (object);
5512 if (iface->open_readwrite == NULL)
5514 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5515 _("Operation not supported"));
5519 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5522 g_task_return_error (task, error);
5524 g_task_return_pointer (task, stream, g_object_unref);
5528 g_file_real_open_readwrite_async (GFile *file,
5530 GCancellable *cancellable,
5531 GAsyncReadyCallback callback,
5536 task = g_task_new (file, cancellable, callback, user_data);
5537 g_task_set_priority (task, io_priority);
5539 g_task_run_in_thread (task, open_readwrite_async_thread);
5540 g_object_unref (task);
5543 static GFileIOStream *
5544 g_file_real_open_readwrite_finish (GFile *file,
5548 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5550 return g_task_propagate_pointer (G_TASK (res), error);
5554 create_readwrite_async_thread (GTask *task,
5557 GCancellable *cancellable)
5560 GFileCreateFlags *data = task_data;
5561 GFileIOStream *stream;
5562 GError *error = NULL;
5564 iface = G_FILE_GET_IFACE (object);
5566 if (iface->create_readwrite == NULL)
5568 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5569 _("Operation not supported"));
5573 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5576 g_task_return_error (task, error);
5578 g_task_return_pointer (task, stream, g_object_unref);
5582 g_file_real_create_readwrite_async (GFile *file,
5583 GFileCreateFlags flags,
5585 GCancellable *cancellable,
5586 GAsyncReadyCallback callback,
5589 GFileCreateFlags *data;
5592 data = g_new0 (GFileCreateFlags, 1);
5595 task = g_task_new (file, cancellable, callback, user_data);
5596 g_task_set_task_data (task, data, g_free);
5597 g_task_set_priority (task, io_priority);
5599 g_task_run_in_thread (task, create_readwrite_async_thread);
5600 g_object_unref (task);
5603 static GFileIOStream *
5604 g_file_real_create_readwrite_finish (GFile *file,
5608 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5610 return g_task_propagate_pointer (G_TASK (res), error);
5615 gboolean make_backup;
5616 GFileCreateFlags flags;
5617 } ReplaceRWAsyncData;
5620 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5622 g_free (data->etag);
5627 replace_readwrite_async_thread (GTask *task,
5630 GCancellable *cancellable)
5633 GFileIOStream *stream;
5634 GError *error = NULL;
5635 ReplaceRWAsyncData *data = task_data;
5637 iface = G_FILE_GET_IFACE (object);
5639 stream = iface->replace_readwrite (G_FILE (object),
5647 g_task_return_error (task, error);
5649 g_task_return_pointer (task, stream, g_object_unref);
5653 g_file_real_replace_readwrite_async (GFile *file,
5655 gboolean make_backup,
5656 GFileCreateFlags flags,
5658 GCancellable *cancellable,
5659 GAsyncReadyCallback callback,
5663 ReplaceRWAsyncData *data;
5665 data = g_new0 (ReplaceRWAsyncData, 1);
5666 data->etag = g_strdup (etag);
5667 data->make_backup = make_backup;
5668 data->flags = flags;
5670 task = g_task_new (file, cancellable, callback, user_data);
5671 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5672 g_task_set_priority (task, io_priority);
5674 g_task_run_in_thread (task, replace_readwrite_async_thread);
5675 g_object_unref (task);
5678 static GFileIOStream *
5679 g_file_real_replace_readwrite_finish (GFile *file,
5683 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5685 return g_task_propagate_pointer (G_TASK (res), error);
5689 set_display_name_async_thread (GTask *task,
5692 GCancellable *cancellable)
5694 GError *error = NULL;
5695 char *name = task_data;
5698 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5701 g_task_return_error (task, error);
5703 g_task_return_pointer (task, file, g_object_unref);
5707 g_file_real_set_display_name_async (GFile *file,
5708 const char *display_name,
5710 GCancellable *cancellable,
5711 GAsyncReadyCallback callback,
5716 task = g_task_new (file, cancellable, callback, user_data);
5717 g_task_set_task_data (task, g_strdup (display_name), g_free);
5718 g_task_set_priority (task, io_priority);
5720 g_task_run_in_thread (task, set_display_name_async_thread);
5721 g_object_unref (task);
5725 g_file_real_set_display_name_finish (GFile *file,
5729 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5731 return g_task_propagate_pointer (G_TASK (res), error);
5735 GFileQueryInfoFlags flags;
5742 set_info_data_free (SetInfoAsyncData *data)
5745 g_object_unref (data->info);
5747 g_error_free (data->error);
5752 set_info_async_thread (GTask *task,
5755 GCancellable *cancellable)
5757 SetInfoAsyncData *data = task_data;
5760 data->res = g_file_set_attributes_from_info (G_FILE (object),
5768 g_file_real_set_attributes_async (GFile *file,
5770 GFileQueryInfoFlags flags,
5772 GCancellable *cancellable,
5773 GAsyncReadyCallback callback,
5777 SetInfoAsyncData *data;
5779 data = g_new0 (SetInfoAsyncData, 1);
5780 data->info = g_file_info_dup (info);
5781 data->flags = flags;
5783 task = g_task_new (file, cancellable, callback, user_data);
5784 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5785 g_task_set_priority (task, io_priority);
5787 g_task_run_in_thread (task, set_info_async_thread);
5788 g_object_unref (task);
5792 g_file_real_set_attributes_finish (GFile *file,
5797 SetInfoAsyncData *data;
5799 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5801 data = g_task_get_task_data (G_TASK (res));
5804 *info = g_object_ref (data->info);
5806 if (error != NULL && data->error)
5807 *error = g_error_copy (data->error);
5813 find_enclosing_mount_async_thread (GTask *task,
5816 GCancellable *cancellable)
5818 GError *error = NULL;
5821 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5824 g_task_return_error (task, error);
5826 g_task_return_pointer (task, mount, g_object_unref);
5830 g_file_real_find_enclosing_mount_async (GFile *file,
5832 GCancellable *cancellable,
5833 GAsyncReadyCallback callback,
5838 task = g_task_new (file, cancellable, callback, user_data);
5839 g_task_set_priority (task, io_priority);
5841 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5842 g_object_unref (task);
5846 g_file_real_find_enclosing_mount_finish (GFile *file,
5850 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5852 return g_task_propagate_pointer (G_TASK (res), error);
5859 GFileCopyFlags flags;
5860 GFileProgressCallback progress_cb;
5861 gpointer progress_cb_data;
5865 copy_async_data_free (CopyAsyncData *data)
5867 g_object_unref (data->source);
5868 g_object_unref (data->destination);
5869 g_slice_free (CopyAsyncData, data);
5873 CopyAsyncData *data;
5874 goffset current_num_bytes;
5875 goffset total_num_bytes;
5879 copy_async_progress_in_main (gpointer user_data)
5881 ProgressData *progress = user_data;
5882 CopyAsyncData *data = progress->data;
5884 data->progress_cb (progress->current_num_bytes,
5885 progress->total_num_bytes,
5886 data->progress_cb_data);
5892 copy_async_progress_callback (goffset current_num_bytes,
5893 goffset total_num_bytes,
5896 GTask *task = user_data;
5897 CopyAsyncData *data = g_task_get_task_data (task);
5898 ProgressData *progress;
5900 progress = g_new (ProgressData, 1);
5901 progress->data = data;
5902 progress->current_num_bytes = current_num_bytes;
5903 progress->total_num_bytes = total_num_bytes;
5905 g_main_context_invoke_full (g_task_get_context (task),
5906 g_task_get_priority (task),
5907 copy_async_progress_in_main,
5913 copy_async_thread (GTask *task,
5916 GCancellable *cancellable)
5918 CopyAsyncData *data = task_data;
5920 GError *error = NULL;
5922 result = g_file_copy (data->source,
5926 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5930 g_task_return_boolean (task, TRUE);
5932 g_task_return_error (task, error);
5936 g_file_real_copy_async (GFile *source,
5938 GFileCopyFlags flags,
5940 GCancellable *cancellable,
5941 GFileProgressCallback progress_callback,
5942 gpointer progress_callback_data,
5943 GAsyncReadyCallback callback,
5947 CopyAsyncData *data;
5949 data = g_slice_new (CopyAsyncData);
5950 data->source = g_object_ref (source);
5951 data->destination = g_object_ref (destination);
5952 data->flags = flags;
5953 data->progress_cb = progress_callback;
5954 data->progress_cb_data = progress_callback_data;
5956 task = g_task_new (source, cancellable, callback, user_data);
5957 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5958 g_task_set_priority (task, io_priority);
5959 g_task_run_in_thread (task, copy_async_thread);
5960 g_object_unref (task);
5964 g_file_real_copy_finish (GFile *file,
5968 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5970 return g_task_propagate_boolean (G_TASK (res), error);
5974 /********************************************
5975 * Default VFS operations *
5976 ********************************************/
5979 * g_file_new_for_path:
5980 * @path: a string containing a relative or absolute path.
5981 * The string must be encoded in the glib filename encoding.
5983 * Constructs a #GFile for a given path. This operation never
5984 * fails, but the returned object might not support any I/O
5985 * operation if @path is malformed.
5987 * Returns: (transfer full): a new #GFile for the given @path.
5988 * Free the returned object with g_object_unref().
5991 g_file_new_for_path (const char *path)
5993 g_return_val_if_fail (path != NULL, NULL);
5995 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5999 * g_file_new_for_uri:
6000 * @uri: a UTF-8 string containing a URI
6002 * Constructs a #GFile for a given URI. This operation never
6003 * fails, but the returned object might not support any I/O
6004 * operation if @uri is malformed or if the uri type is
6007 * Returns: (transfer full): a new #GFile for the given @uri.
6008 * Free the returned object with g_object_unref().
6011 g_file_new_for_uri (const char *uri)
6013 g_return_val_if_fail (uri != NULL, NULL);
6015 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6020 * @tmpl: (type filename) (allow-none): Template for the file
6021 * name, as in g_file_open_tmp(), or %NULL for a default template
6022 * @iostream: (out): on return, a #GFileIOStream for the created file
6023 * @error: a #GError, or %NULL
6025 * Opens a file in the preferred directory for temporary files (as
6026 * returned by g_get_tmp_dir()) and returns a #GFile and
6027 * #GFileIOStream pointing to it.
6029 * @tmpl should be a string in the GLib file name encoding
6030 * containing a sequence of six 'X' characters, and containing no
6031 * directory components. If it is %NULL, a default template is used.
6033 * Unlike the other #GFile constructors, this will return %NULL if
6034 * a temporary file could not be created.
6036 * Returns: (transfer full): a new #GFile.
6037 * Free the returned object with g_object_unref().
6042 g_file_new_tmp (const char *tmpl,
6043 GFileIOStream **iostream,
6049 GFileOutputStream *output;
6051 g_return_val_if_fail (iostream != NULL, NULL);
6053 fd = g_file_open_tmp (tmpl, &path, error);
6057 file = g_file_new_for_path (path);
6059 output = _g_local_file_output_stream_new (fd);
6060 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6062 g_object_unref (output);
6069 * g_file_parse_name:
6070 * @parse_name: a file name or path to be parsed
6072 * Constructs a #GFile with the given @parse_name (i.e. something
6073 * given by g_file_get_parse_name()). This operation never fails,
6074 * but the returned object might not support any I/O operation if
6075 * the @parse_name cannot be parsed.
6077 * Returns: (transfer full): a new #GFile.
6080 g_file_parse_name (const char *parse_name)
6082 g_return_val_if_fail (parse_name != NULL, NULL);
6084 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6088 is_valid_scheme_character (char c)
6090 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6093 /* Following RFC 2396, valid schemes are built like:
6094 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6097 has_valid_scheme (const char *uri)
6103 if (!g_ascii_isalpha (*p))
6108 } while (is_valid_scheme_character (*p));
6114 new_for_cmdline_arg (const gchar *arg,
6120 if (g_path_is_absolute (arg))
6121 return g_file_new_for_path (arg);
6123 if (has_valid_scheme (arg))
6124 return g_file_new_for_uri (arg);
6130 current_dir = g_get_current_dir ();
6131 filename = g_build_filename (current_dir, arg, NULL);
6132 g_free (current_dir);
6135 filename = g_build_filename (cwd, arg, NULL);
6137 file = g_file_new_for_path (filename);
6144 * g_file_new_for_commandline_arg:
6145 * @arg: a command line string
6147 * Creates a #GFile with the given argument from the command line.
6148 * The value of @arg can be either a URI, an absolute path or a
6149 * relative path resolved relative to the current working directory.
6150 * This operation never fails, but the returned object might not
6151 * support any I/O operation if @arg points to a malformed path.
6153 * Returns: (transfer full): a new #GFile.
6154 * Free the returned object with g_object_unref().
6157 g_file_new_for_commandline_arg (const char *arg)
6159 g_return_val_if_fail (arg != NULL, NULL);
6161 return new_for_cmdline_arg (arg, NULL);
6165 * g_file_new_for_commandline_arg_and_cwd:
6166 * @arg: a command line string
6167 * @cwd: the current working directory of the commandline
6169 * Creates a #GFile with the given argument from the command line.
6171 * This function is similar to g_file_new_for_commandline_arg() except
6172 * that it allows for passing the current working directory as an
6173 * argument instead of using the current working directory of the
6176 * This is useful if the commandline argument was given in a context
6177 * other than the invocation of the current process.
6179 * See also g_application_command_line_create_file_for_arg().
6181 * Returns: (transfer full): a new #GFile
6186 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6189 g_return_val_if_fail (arg != NULL, NULL);
6190 g_return_val_if_fail (cwd != NULL, NULL);
6192 return new_for_cmdline_arg (arg, cwd);
6196 * g_file_mount_enclosing_volume:
6197 * @location: input #GFile
6198 * @flags: flags affecting the operation
6199 * @mount_operation: (allow-none): a #GMountOperation
6200 * or %NULL to avoid user interaction
6201 * @cancellable: (allow-none): optional #GCancellable object,
6203 * @callback: (allow-none): a #GAsyncReadyCallback to call
6204 * when the request is satisfied, or %NULL
6205 * @user_data: the data to pass to callback function
6207 * Starts a @mount_operation, mounting the volume that contains
6208 * the file @location.
6210 * When this operation has completed, @callback will be called with
6211 * @user_user data, and the operation can be finalized with
6212 * g_file_mount_enclosing_volume_finish().
6214 * If @cancellable is not %NULL, then the operation can be cancelled by
6215 * triggering the cancellable object from another thread. If the operation
6216 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6219 g_file_mount_enclosing_volume (GFile *location,
6220 GMountMountFlags flags,
6221 GMountOperation *mount_operation,
6222 GCancellable *cancellable,
6223 GAsyncReadyCallback callback,
6228 g_return_if_fail (G_IS_FILE (location));
6230 iface = G_FILE_GET_IFACE (location);
6232 if (iface->mount_enclosing_volume == NULL)
6234 g_task_report_new_error (location, callback, user_data,
6235 g_file_mount_enclosing_volume,
6236 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6237 _("volume doesn't implement mount"));
6241 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6246 * g_file_mount_enclosing_volume_finish:
6247 * @location: input #GFile
6248 * @result: a #GAsyncResult
6249 * @error: a #GError, or %NULL
6251 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6253 * Returns: %TRUE if successful. If an error has occurred,
6254 * this function will return %FALSE and set @error
6255 * appropriately if present.
6258 g_file_mount_enclosing_volume_finish (GFile *location,
6259 GAsyncResult *result,
6264 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6265 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6267 if (g_async_result_legacy_propagate_error (result, error))
6269 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6270 return g_task_propagate_boolean (G_TASK (result), error);
6272 iface = G_FILE_GET_IFACE (location);
6274 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6277 /********************************************
6278 * Utility functions *
6279 ********************************************/
6282 * g_file_query_default_handler:
6283 * @file: a #GFile to open
6284 * @cancellable: optional #GCancellable object, %NULL to ignore
6285 * @error: a #GError, or %NULL
6287 * Returns the #GAppInfo that is registered as the default
6288 * application to handle the file specified by @file.
6290 * If @cancellable is not %NULL, then the operation can be cancelled by
6291 * triggering the cancellable object from another thread. If the operation
6292 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6294 * Returns: (transfer full): a #GAppInfo if the handle was found,
6295 * %NULL if there were errors.
6296 * When you are done with it, release it with g_object_unref()
6299 g_file_query_default_handler (GFile *file,
6300 GCancellable *cancellable,
6304 const char *content_type;
6309 uri_scheme = g_file_get_uri_scheme (file);
6310 if (uri_scheme && uri_scheme[0] != '\0')
6312 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6313 g_free (uri_scheme);
6315 if (appinfo != NULL)
6319 info = g_file_query_info (file,
6320 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6329 content_type = g_file_info_get_content_type (info);
6332 /* Don't use is_native(), as we want to support fuse paths if available */
6333 path = g_file_get_path (file);
6334 appinfo = g_app_info_get_default_for_type (content_type,
6339 g_object_unref (info);
6341 if (appinfo != NULL)
6344 g_set_error_literal (error, G_IO_ERROR,
6345 G_IO_ERROR_NOT_SUPPORTED,
6346 _("No application is registered as handling this file"));
6350 #define GET_CONTENT_BLOCK_SIZE 8192
6353 * g_file_load_contents:
6354 * @file: input #GFile
6355 * @cancellable: optional #GCancellable object, %NULL to ignore
6356 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6357 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6358 * or %NULL if the length is not needed
6359 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6360 * or %NULL if the entity tag is not needed
6361 * @error: a #GError, or %NULL
6363 * Loads the content of the file into memory. The data is always
6364 * zero-terminated, but this is not included in the resultant @length.
6365 * The returned @content should be freed with g_free() when no longer
6368 * If @cancellable is not %NULL, then the operation can be cancelled by
6369 * triggering the cancellable object from another thread. If the operation
6370 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6372 * Returns: %TRUE if the @file's contents were successfully loaded.
6373 * %FALSE if there were errors.
6376 g_file_load_contents (GFile *file,
6377 GCancellable *cancellable,
6383 GFileInputStream *in;
6384 GByteArray *content;
6389 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6390 g_return_val_if_fail (contents != NULL, FALSE);
6392 in = g_file_read (file, cancellable, error);
6396 content = g_byte_array_new ();
6399 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6400 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6401 content->data + pos,
6402 GET_CONTENT_BLOCK_SIZE,
6403 cancellable, error)) > 0)
6406 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6413 info = g_file_input_stream_query_info (in,
6414 G_FILE_ATTRIBUTE_ETAG_VALUE,
6419 *etag_out = g_strdup (g_file_info_get_etag (info));
6420 g_object_unref (info);
6424 /* Ignore errors on close */
6425 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6426 g_object_unref (in);
6430 /* error is set already */
6431 g_byte_array_free (content, TRUE);
6438 /* Zero terminate (we got an extra byte allocated for this */
6439 content->data[pos] = 0;
6441 *contents = (char *)g_byte_array_free (content, FALSE);
6448 GFileReadMoreCallback read_more_callback;
6449 GByteArray *content;
6456 load_contents_data_free (LoadContentsData *data)
6459 g_byte_array_free (data->content, TRUE);
6460 g_free (data->etag);
6465 load_contents_close_callback (GObject *obj,
6466 GAsyncResult *close_res,
6469 GInputStream *stream = G_INPUT_STREAM (obj);
6470 LoadContentsData *data = user_data;
6472 /* Ignore errors here, we're only reading anyway */
6473 g_input_stream_close_finish (stream, close_res, NULL);
6474 g_object_unref (stream);
6476 g_task_return_boolean (data->task, TRUE);
6477 g_object_unref (data->task);
6481 load_contents_fstat_callback (GObject *obj,
6482 GAsyncResult *stat_res,
6485 GInputStream *stream = G_INPUT_STREAM (obj);
6486 LoadContentsData *data = user_data;
6489 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6493 data->etag = g_strdup (g_file_info_get_etag (info));
6494 g_object_unref (info);
6497 g_input_stream_close_async (stream, 0,
6498 g_task_get_cancellable (data->task),
6499 load_contents_close_callback, data);
6503 load_contents_read_callback (GObject *obj,
6504 GAsyncResult *read_res,
6507 GInputStream *stream = G_INPUT_STREAM (obj);
6508 LoadContentsData *data = user_data;
6509 GError *error = NULL;
6512 read_size = g_input_stream_read_finish (stream, read_res, &error);
6516 g_task_return_error (data->task, error);
6517 g_object_unref (data->task);
6519 /* Close the file ignoring any error */
6520 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6521 g_object_unref (stream);
6523 else if (read_size == 0)
6525 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6526 G_FILE_ATTRIBUTE_ETAG_VALUE,
6528 g_task_get_cancellable (data->task),
6529 load_contents_fstat_callback,
6532 else if (read_size > 0)
6534 data->pos += read_size;
6536 g_byte_array_set_size (data->content,
6537 data->pos + GET_CONTENT_BLOCK_SIZE);
6540 if (data->read_more_callback &&
6541 !data->read_more_callback ((char *)data->content->data, data->pos,
6542 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6543 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6544 G_FILE_ATTRIBUTE_ETAG_VALUE,
6546 g_task_get_cancellable (data->task),
6547 load_contents_fstat_callback,
6550 g_input_stream_read_async (stream,
6551 data->content->data + data->pos,
6552 GET_CONTENT_BLOCK_SIZE,
6554 g_task_get_cancellable (data->task),
6555 load_contents_read_callback,
6561 load_contents_open_callback (GObject *obj,
6562 GAsyncResult *open_res,
6565 GFile *file = G_FILE (obj);
6566 GFileInputStream *stream;
6567 LoadContentsData *data = user_data;
6568 GError *error = NULL;
6570 stream = g_file_read_finish (file, open_res, &error);
6574 g_byte_array_set_size (data->content,
6575 data->pos + GET_CONTENT_BLOCK_SIZE);
6576 g_input_stream_read_async (G_INPUT_STREAM (stream),
6577 data->content->data + data->pos,
6578 GET_CONTENT_BLOCK_SIZE,
6580 g_task_get_cancellable (data->task),
6581 load_contents_read_callback,
6586 g_task_return_error (data->task, error);
6587 g_object_unref (data->task);
6592 * g_file_load_partial_contents_async: (skip)
6593 * @file: input #GFile
6594 * @cancellable: optional #GCancellable object, %NULL to ignore
6595 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6596 * and to specify whether further data should be read
6597 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6598 * @user_data: the data to pass to the callback functions
6600 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6601 * be used to stop reading from the file when appropriate, else this
6602 * function will behave exactly as g_file_load_contents_async(). This
6603 * operation can be finished by g_file_load_partial_contents_finish().
6605 * Users of this function should be aware that @user_data is passed to
6606 * both the @read_more_callback and the @callback.
6608 * If @cancellable is not %NULL, then the operation can be cancelled by
6609 * triggering the cancellable object from another thread. If the operation
6610 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6613 g_file_load_partial_contents_async (GFile *file,
6614 GCancellable *cancellable,
6615 GFileReadMoreCallback read_more_callback,
6616 GAsyncReadyCallback callback,
6619 LoadContentsData *data;
6621 g_return_if_fail (G_IS_FILE (file));
6623 data = g_new0 (LoadContentsData, 1);
6624 data->read_more_callback = read_more_callback;
6625 data->content = g_byte_array_new ();
6627 data->task = g_task_new (file, cancellable, callback, user_data);
6628 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6630 g_file_read_async (file,
6632 g_task_get_cancellable (data->task),
6633 load_contents_open_callback,
6638 * g_file_load_partial_contents_finish:
6639 * @file: input #GFile
6640 * @res: a #GAsyncResult
6641 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6642 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6643 * or %NULL if the length is not needed
6644 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6645 * or %NULL if the entity tag is not needed
6646 * @error: a #GError, or %NULL
6648 * Finishes an asynchronous partial load operation that was started
6649 * with g_file_load_partial_contents_async(). The data is always
6650 * zero-terminated, but this is not included in the resultant @length.
6651 * The returned @content should be freed with g_free() when no longer
6654 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6655 * present, it will be set appropriately.
6658 g_file_load_partial_contents_finish (GFile *file,
6666 LoadContentsData *data;
6668 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6669 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6670 g_return_val_if_fail (contents != NULL, FALSE);
6672 task = G_TASK (res);
6674 if (!g_task_propagate_boolean (task, error))
6681 data = g_task_get_task_data (task);
6684 *length = data->pos;
6688 *etag_out = data->etag;
6692 /* Zero terminate */
6693 g_byte_array_set_size (data->content, data->pos + 1);
6694 data->content->data[data->pos] = 0;
6696 *contents = (char *)g_byte_array_free (data->content, FALSE);
6697 data->content = NULL;
6703 * g_file_load_contents_async:
6704 * @file: input #GFile
6705 * @cancellable: optional #GCancellable object, %NULL to ignore
6706 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6707 * @user_data: the data to pass to callback function
6709 * Starts an asynchronous load of the @file's contents.
6711 * For more details, see g_file_load_contents() which is
6712 * the synchronous version of this call.
6714 * When the load operation has completed, @callback will be called
6715 * with @user data. To finish the operation, call
6716 * g_file_load_contents_finish() with the #GAsyncResult returned by
6719 * If @cancellable is not %NULL, then the operation can be cancelled by
6720 * triggering the cancellable object from another thread. If the operation
6721 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6724 g_file_load_contents_async (GFile *file,
6725 GCancellable *cancellable,
6726 GAsyncReadyCallback callback,
6729 g_file_load_partial_contents_async (file,
6732 callback, user_data);
6736 * g_file_load_contents_finish:
6737 * @file: input #GFile
6738 * @res: a #GAsyncResult
6739 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6740 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6741 * or %NULL if the length is not needed
6742 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6743 * or %NULL if the entity tag is not needed
6744 * @error: a #GError, or %NULL
6746 * Finishes an asynchronous load of the @file's contents.
6747 * The contents are placed in @contents, and @length is set to the
6748 * size of the @contents string. The @content should be freed with
6749 * g_free() when no longer needed. If @etag_out is present, it will be
6750 * set to the new entity tag for the @file.
6752 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6753 * present, it will be set appropriately.
6756 g_file_load_contents_finish (GFile *file,
6763 return g_file_load_partial_contents_finish (file,
6772 * g_file_replace_contents:
6773 * @file: input #GFile
6774 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6775 * @length: the length of @contents in bytes
6776 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6777 * for the document, or %NULL
6778 * @make_backup: %TRUE if a backup should be created
6779 * @flags: a set of #GFileCreateFlags
6780 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6781 * for the document. This should be freed with g_free() when no longer
6783 * @cancellable: optional #GCancellable object, %NULL to ignore
6784 * @error: a #GError, or %NULL
6786 * Replaces the contents of @file with @contents of @length bytes.
6788 * If @etag is specified (not %NULL), any existing file must have that etag,
6789 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6791 * If @make_backup is %TRUE, this function will attempt to make a backup
6794 * If @cancellable is not %NULL, then the operation can be cancelled by
6795 * triggering the cancellable object from another thread. If the operation
6796 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6798 * The returned @new_etag can be used to verify that the file hasn't
6799 * changed the next time it is saved over.
6801 * Returns: %TRUE if successful. If an error has occurred, this function
6802 * will return %FALSE and set @error appropriately if present.
6805 g_file_replace_contents (GFile *file,
6806 const char *contents,
6809 gboolean make_backup,
6810 GFileCreateFlags flags,
6812 GCancellable *cancellable,
6815 GFileOutputStream *out;
6816 gsize pos, remainder;
6820 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6821 g_return_val_if_fail (contents != NULL, FALSE);
6823 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6829 while (remainder > 0 &&
6830 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6832 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6840 if (remainder > 0 && res < 0)
6842 /* Ignore errors on close */
6843 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6844 g_object_unref (out);
6846 /* error is set already */
6850 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6853 *new_etag = g_file_output_stream_get_etag (out);
6855 g_object_unref (out);
6862 const char *content;
6867 } ReplaceContentsData;
6870 replace_contents_data_free (ReplaceContentsData *data)
6872 g_free (data->etag);
6877 replace_contents_close_callback (GObject *obj,
6878 GAsyncResult *close_res,
6881 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6882 ReplaceContentsData *data = user_data;
6884 /* Ignore errors here, we're only reading anyway */
6885 g_output_stream_close_finish (stream, close_res, NULL);
6886 g_object_unref (stream);
6890 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6891 g_task_return_boolean (data->task, TRUE);
6893 g_object_unref (data->task);
6897 replace_contents_write_callback (GObject *obj,
6898 GAsyncResult *read_res,
6901 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6902 ReplaceContentsData *data = user_data;
6903 GError *error = NULL;
6906 write_size = g_output_stream_write_finish (stream, read_res, &error);
6908 if (write_size <= 0)
6910 /* Error or EOF, close the file */
6913 data->failed = TRUE;
6914 g_task_return_error (data->task, error);
6916 g_output_stream_close_async (stream, 0,
6917 g_task_get_cancellable (data->task),
6918 replace_contents_close_callback, data);
6920 else if (write_size > 0)
6922 data->pos += write_size;
6924 if (data->pos >= data->length)
6925 g_output_stream_close_async (stream, 0,
6926 g_task_get_cancellable (data->task),
6927 replace_contents_close_callback, data);
6929 g_output_stream_write_async (stream,
6930 data->content + data->pos,
6931 data->length - data->pos,
6933 g_task_get_cancellable (data->task),
6934 replace_contents_write_callback,
6940 replace_contents_open_callback (GObject *obj,
6941 GAsyncResult *open_res,
6944 GFile *file = G_FILE (obj);
6945 GFileOutputStream *stream;
6946 ReplaceContentsData *data = user_data;
6947 GError *error = NULL;
6949 stream = g_file_replace_finish (file, open_res, &error);
6953 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6954 data->content + data->pos,
6955 data->length - data->pos,
6957 g_task_get_cancellable (data->task),
6958 replace_contents_write_callback,
6963 g_task_return_error (data->task, error);
6964 g_object_unref (data->task);
6969 * g_file_replace_contents_async:
6970 * @file: input #GFile
6971 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6972 * @length: the length of @contents in bytes
6973 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6974 * @make_backup: %TRUE if a backup should be created
6975 * @flags: a set of #GFileCreateFlags
6976 * @cancellable: optional #GCancellable object, %NULL to ignore
6977 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6978 * @user_data: the data to pass to callback function
6980 * Starts an asynchronous replacement of @file with the given
6981 * @contents of @length bytes. @etag will replace the document's
6982 * current entity tag.
6984 * When this operation has completed, @callback will be called with
6985 * @user_user data, and the operation can be finalized with
6986 * g_file_replace_contents_finish().
6988 * If @cancellable is not %NULL, then the operation can be cancelled by
6989 * triggering the cancellable object from another thread. If the operation
6990 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6992 * If @make_backup is %TRUE, this function will attempt to
6993 * make a backup of @file.
6996 g_file_replace_contents_async (GFile *file,
6997 const char *contents,
7000 gboolean make_backup,
7001 GFileCreateFlags flags,
7002 GCancellable *cancellable,
7003 GAsyncReadyCallback callback,
7006 ReplaceContentsData *data;
7008 g_return_if_fail (G_IS_FILE (file));
7009 g_return_if_fail (contents != NULL);
7011 data = g_new0 (ReplaceContentsData, 1);
7013 data->content = contents;
7014 data->length = length;
7016 data->task = g_task_new (file, cancellable, callback, user_data);
7017 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7019 g_file_replace_async (file,
7024 g_task_get_cancellable (data->task),
7025 replace_contents_open_callback,
7030 * g_file_replace_contents_finish:
7031 * @file: input #GFile
7032 * @res: a #GAsyncResult
7033 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7034 * for the document. This should be freed with g_free() when it is no
7035 * longer needed, or %NULL
7036 * @error: a #GError, or %NULL
7038 * Finishes an asynchronous replace of the given @file. See
7039 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7040 * tag for the document, if present.
7042 * Returns: %TRUE on success, %FALSE on failure.
7045 g_file_replace_contents_finish (GFile *file,
7051 ReplaceContentsData *data;
7053 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7054 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7056 task = G_TASK (res);
7058 if (!g_task_propagate_boolean (task, error))
7061 data = g_task_get_task_data (task);
7065 *new_etag = data->etag;
7066 data->etag = NULL; /* Take ownership */
7073 * g_file_start_mountable:
7074 * @file: input #GFile
7075 * @flags: flags affecting the operation
7076 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7077 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7078 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7079 * @user_data: the data to pass to callback function
7081 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7082 * Using @start_operation, you can request callbacks when, for instance,
7083 * passwords are needed during authentication.
7085 * If @cancellable is not %NULL, then the operation can be cancelled by
7086 * triggering the cancellable object from another thread. If the operation
7087 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7089 * When the operation is finished, @callback will be called.
7090 * You can then call g_file_mount_mountable_finish() to get
7091 * the result of the operation.
7096 g_file_start_mountable (GFile *file,
7097 GDriveStartFlags flags,
7098 GMountOperation *start_operation,
7099 GCancellable *cancellable,
7100 GAsyncReadyCallback callback,
7105 g_return_if_fail (G_IS_FILE (file));
7107 iface = G_FILE_GET_IFACE (file);
7109 if (iface->start_mountable == NULL)
7111 g_task_report_new_error (file, callback, user_data,
7112 g_file_start_mountable,
7113 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7114 _("Operation not supported"));
7118 (* iface->start_mountable) (file,
7127 * g_file_start_mountable_finish:
7128 * @file: input #GFile
7129 * @result: a #GAsyncResult
7130 * @error: a #GError, or %NULL
7132 * Finishes a start operation. See g_file_start_mountable() for details.
7134 * Finish an asynchronous start operation that was started
7135 * with g_file_start_mountable().
7137 * Returns: %TRUE if the operation finished successfully. %FALSE
7143 g_file_start_mountable_finish (GFile *file,
7144 GAsyncResult *result,
7149 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7150 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7152 if (g_async_result_legacy_propagate_error (result, error))
7154 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7155 return g_task_propagate_boolean (G_TASK (result), error);
7157 iface = G_FILE_GET_IFACE (file);
7158 return (* iface->start_mountable_finish) (file, result, error);
7162 * g_file_stop_mountable:
7163 * @file: input #GFile
7164 * @flags: flags affecting the operation
7165 * @mount_operation: (allow-none): a #GMountOperation,
7166 * or %NULL to avoid user interaction.
7167 * @cancellable: (allow-none): optional #GCancellable object,
7169 * @callback: (allow-none): a #GAsyncReadyCallback to call
7170 * when the request is satisfied, or %NULL
7171 * @user_data: the data to pass to callback function
7173 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7175 * If @cancellable is not %NULL, then the operation can be cancelled by
7176 * triggering the cancellable object from another thread. If the operation
7177 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7179 * When the operation is finished, @callback will be called.
7180 * You can then call g_file_stop_mountable_finish() to get
7181 * the result of the operation.
7186 g_file_stop_mountable (GFile *file,
7187 GMountUnmountFlags flags,
7188 GMountOperation *mount_operation,
7189 GCancellable *cancellable,
7190 GAsyncReadyCallback callback,
7195 g_return_if_fail (G_IS_FILE (file));
7197 iface = G_FILE_GET_IFACE (file);
7199 if (iface->stop_mountable == NULL)
7201 g_task_report_new_error (file, callback, user_data,
7202 g_file_stop_mountable,
7203 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7204 _("Operation not supported"));
7208 (* iface->stop_mountable) (file,
7217 * g_file_stop_mountable_finish:
7218 * @file: input #GFile
7219 * @result: a #GAsyncResult
7220 * @error: a #GError, or %NULL
7222 * Finishes an stop operation, see g_file_stop_mountable() for details.
7224 * Finish an asynchronous stop operation that was started
7225 * with g_file_stop_mountable().
7227 * Returns: %TRUE if the operation finished successfully.
7233 g_file_stop_mountable_finish (GFile *file,
7234 GAsyncResult *result,
7239 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7240 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7242 if (g_async_result_legacy_propagate_error (result, error))
7244 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7245 return g_task_propagate_boolean (G_TASK (result), error);
7247 iface = G_FILE_GET_IFACE (file);
7248 return (* iface->stop_mountable_finish) (file, result, error);
7252 * g_file_poll_mountable:
7253 * @file: input #GFile
7254 * @cancellable: optional #GCancellable object, %NULL to ignore
7255 * @callback: (allow-none): a #GAsyncReadyCallback to call
7256 * when the request is satisfied, or %NULL
7257 * @user_data: the data to pass to callback function
7259 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7261 * If @cancellable is not %NULL, then the operation can be cancelled by
7262 * triggering the cancellable object from another thread. If the operation
7263 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7265 * When the operation is finished, @callback will be called.
7266 * You can then call g_file_mount_mountable_finish() to get
7267 * the result of the operation.
7272 g_file_poll_mountable (GFile *file,
7273 GCancellable *cancellable,
7274 GAsyncReadyCallback callback,
7279 g_return_if_fail (G_IS_FILE (file));
7281 iface = G_FILE_GET_IFACE (file);
7283 if (iface->poll_mountable == NULL)
7285 g_task_report_new_error (file, callback, user_data,
7286 g_file_poll_mountable,
7287 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7288 _("Operation not supported"));
7292 (* iface->poll_mountable) (file,
7299 * g_file_poll_mountable_finish:
7300 * @file: input #GFile
7301 * @result: a #GAsyncResult
7302 * @error: a #GError, or %NULL
7304 * Finishes a poll operation. See g_file_poll_mountable() for details.
7306 * Finish an asynchronous poll operation that was polled
7307 * with g_file_poll_mountable().
7309 * Returns: %TRUE if the operation finished successfully. %FALSE
7315 g_file_poll_mountable_finish (GFile *file,
7316 GAsyncResult *result,
7321 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7322 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7324 if (g_async_result_legacy_propagate_error (result, error))
7326 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7327 return g_task_propagate_boolean (G_TASK (result), error);
7329 iface = G_FILE_GET_IFACE (file);
7330 return (* iface->poll_mountable_finish) (file, result, error);
7334 * g_file_supports_thread_contexts:
7337 * Checks if @file supports <link
7338 * linkend="g-main-context-push-thread-default-context">thread-default
7339 * contexts</link>. If this returns %FALSE, you cannot perform
7340 * asynchronous operations on @file in a thread that has a
7341 * thread-default context.
7343 * Returns: Whether or not @file supports thread-default contexts.
7348 g_file_supports_thread_contexts (GFile *file)
7352 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7354 iface = G_FILE_GET_IFACE (file);
7355 return iface->supports_thread_contexts;