1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <sys/ioctl.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
43 #include <sys/types.h>
49 #include "glib/gstdio.h"
51 #include "glib-unix.h"
55 #include "gfileattribute-priv.h"
56 #include "gfiledescriptorbased.h"
57 #include "gpollfilemonitor.h"
59 #include "gfileinputstream.h"
60 #include "gfileoutputstream.h"
61 #include "glocalfileoutputstream.h"
62 #include "glocalfileiostream.h"
63 #include "gcancellable.h"
64 #include "gasyncresult.h"
71 * @short_description: File and Directory Handling
73 * @see_also: #GFileInfo, #GFileEnumerator
75 * #GFile is a high level abstraction for manipulating files on a
76 * virtual file system. #GFiles are lightweight, immutable objects
77 * that do no I/O upon creation. It is necessary to understand that
78 * #GFile objects do not represent files, merely an identifier for a
79 * file. All file content I/O is implemented as streaming operations
80 * (see #GInputStream and #GOutputStream).
82 * To construct a #GFile, you can use:
84 * <member>g_file_new_for_path() if you have a path.</member>
85 * <member>g_file_new_for_uri() if you have a URI.</member>
86 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
87 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
88 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
91 * One way to think of a #GFile is as an abstraction of a pathname. For
92 * normal files the system pathname is what is stored internally, but as
93 * #GFiles are extensible it could also be something else that corresponds
94 * to a pathname in a userspace implementation of a filesystem.
96 * #GFiles make up hierarchies of directories and files that correspond to
97 * the files on a filesystem. You can move through the file system with
98 * #GFile using g_file_get_parent() to get an identifier for the parent
99 * directory, g_file_get_child() to get a child within a directory,
100 * g_file_resolve_relative_path() to resolve a relative path between two
101 * #GFiles. There can be multiple hierarchies, so you may not end up at
102 * the same root if you repeatedly call g_file_get_parent() on two different
105 * All #GFiles have a basename (get with g_file_get_basename()). These names
106 * are byte strings that are used to identify the file on the filesystem
107 * (relative to its parent directory) and there is no guarantees that they
108 * have any particular charset encoding or even make any sense at all. If
109 * you want to use filenames in a user interface you should use the display
110 * name that you can get by requesting the
111 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
112 * This is guaranteed to be in UTF-8 and can be used in a user interface.
113 * But always store the real basename or the #GFile to use to actually
114 * access the file, because there is no way to go from a display name to
117 * Using #GFile as an identifier has the same weaknesses as using a path
118 * in that there may be multiple aliases for the same file. For instance,
119 * hard or soft links may cause two different #GFiles to refer to the same
120 * file. Other possible causes for aliases are: case insensitive filesystems,
121 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
122 * check if two #GFiles point to the same file you can query for the
123 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
124 * canonicalization of pathnames passed in, so that trivial differences in
125 * the path string used at creation (duplicated slashes, slash at end of
126 * path, "." or ".." path segments, etc) does not create different #GFiles.
128 * Many #GFile operations have both synchronous and asynchronous versions
129 * to suit your application. Asynchronous versions of synchronous functions
130 * simply have _async() appended to their function names. The asynchronous
131 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
132 * the operation, producing a GAsyncResult which is then passed to the
133 * function's matching _finish() operation.
135 * Some #GFile operations do not have synchronous analogs, as they may
136 * take a very long time to finish, and blocking may leave an application
137 * unusable. Notable cases include:
139 * <member>g_file_mount_mountable() to mount a mountable file.</member>
140 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
141 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
144 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
145 * One notable feature of #GFiles are entity tags, or "etags" for
146 * short. Entity tags are somewhat like a more abstract version of the
147 * traditional mtime, and can be used to quickly determine if the file has
148 * been modified from the version on the file system. See the HTTP 1.1
149 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
150 * for HTTP Etag headers, which are a very similar concept.
154 static void g_file_real_query_info_async (GFile *file,
155 const char *attributes,
156 GFileQueryInfoFlags flags,
158 GCancellable *cancellable,
159 GAsyncReadyCallback callback,
161 static GFileInfo * g_file_real_query_info_finish (GFile *file,
164 static void g_file_real_query_filesystem_info_async (GFile *file,
165 const char *attributes,
167 GCancellable *cancellable,
168 GAsyncReadyCallback callback,
170 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
173 static void g_file_real_enumerate_children_async (GFile *file,
174 const char *attributes,
175 GFileQueryInfoFlags flags,
177 GCancellable *cancellable,
178 GAsyncReadyCallback callback,
180 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
183 static void g_file_real_read_async (GFile *file,
185 GCancellable *cancellable,
186 GAsyncReadyCallback callback,
188 static GFileInputStream * g_file_real_read_finish (GFile *file,
191 static void g_file_real_append_to_async (GFile *file,
192 GFileCreateFlags flags,
194 GCancellable *cancellable,
195 GAsyncReadyCallback callback,
197 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
200 static void g_file_real_create_async (GFile *file,
201 GFileCreateFlags flags,
203 GCancellable *cancellable,
204 GAsyncReadyCallback callback,
206 static GFileOutputStream *g_file_real_create_finish (GFile *file,
209 static void g_file_real_replace_async (GFile *file,
211 gboolean make_backup,
212 GFileCreateFlags flags,
214 GCancellable *cancellable,
215 GAsyncReadyCallback callback,
217 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
220 static void g_file_real_delete_async (GFile *file,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
225 static gboolean g_file_real_delete_finish (GFile *file,
228 static void g_file_real_trash_async (GFile *file,
230 GCancellable *cancellable,
231 GAsyncReadyCallback callback,
233 static gboolean g_file_real_trash_finish (GFile *file,
236 static void g_file_real_open_readwrite_async (GFile *file,
238 GCancellable *cancellable,
239 GAsyncReadyCallback callback,
241 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
244 static void g_file_real_create_readwrite_async (GFile *file,
245 GFileCreateFlags flags,
247 GCancellable *cancellable,
248 GAsyncReadyCallback callback,
250 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
253 static void g_file_real_replace_readwrite_async (GFile *file,
255 gboolean make_backup,
256 GFileCreateFlags flags,
258 GCancellable *cancellable,
259 GAsyncReadyCallback callback,
261 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
264 static gboolean g_file_real_set_attributes_from_info (GFile *file,
266 GFileQueryInfoFlags flags,
267 GCancellable *cancellable,
269 static void g_file_real_set_display_name_async (GFile *file,
270 const char *display_name,
272 GCancellable *cancellable,
273 GAsyncReadyCallback callback,
275 static GFile * g_file_real_set_display_name_finish (GFile *file,
278 static void g_file_real_set_attributes_async (GFile *file,
280 GFileQueryInfoFlags flags,
282 GCancellable *cancellable,
283 GAsyncReadyCallback callback,
285 static gboolean g_file_real_set_attributes_finish (GFile *file,
289 static void g_file_real_find_enclosing_mount_async (GFile *file,
291 GCancellable *cancellable,
292 GAsyncReadyCallback callback,
294 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
297 static void g_file_real_copy_async (GFile *source,
299 GFileCopyFlags flags,
301 GCancellable *cancellable,
302 GFileProgressCallback progress_callback,
303 gpointer progress_callback_data,
304 GAsyncReadyCallback callback,
306 static gboolean g_file_real_copy_finish (GFile *file,
310 typedef GFileIface GFileInterface;
311 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
314 g_file_default_init (GFileIface *iface)
316 iface->enumerate_children_async = g_file_real_enumerate_children_async;
317 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
318 iface->set_display_name_async = g_file_real_set_display_name_async;
319 iface->set_display_name_finish = g_file_real_set_display_name_finish;
320 iface->query_info_async = g_file_real_query_info_async;
321 iface->query_info_finish = g_file_real_query_info_finish;
322 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
323 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
324 iface->set_attributes_async = g_file_real_set_attributes_async;
325 iface->set_attributes_finish = g_file_real_set_attributes_finish;
326 iface->read_async = g_file_real_read_async;
327 iface->read_finish = g_file_real_read_finish;
328 iface->append_to_async = g_file_real_append_to_async;
329 iface->append_to_finish = g_file_real_append_to_finish;
330 iface->create_async = g_file_real_create_async;
331 iface->create_finish = g_file_real_create_finish;
332 iface->replace_async = g_file_real_replace_async;
333 iface->replace_finish = g_file_real_replace_finish;
334 iface->delete_file_async = g_file_real_delete_async;
335 iface->delete_file_finish = g_file_real_delete_finish;
336 iface->trash_async = g_file_real_trash_async;
337 iface->trash_finish = g_file_real_trash_finish;
338 iface->open_readwrite_async = g_file_real_open_readwrite_async;
339 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
340 iface->create_readwrite_async = g_file_real_create_readwrite_async;
341 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
342 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
343 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
344 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
345 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
346 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
347 iface->copy_async = g_file_real_copy_async;
348 iface->copy_finish = g_file_real_copy_finish;
354 * @file: input #GFile
356 * Checks to see if a file is native to the platform.
358 * A native file s one expressed in the platform-native filename format,
359 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
360 * as it might be on a locally mounted remote filesystem.
362 * On some systems non-native files may be available using the native
363 * filesystem via a userspace filesystem (FUSE), in these cases this call
364 * will return %FALSE, but g_file_get_path() will still return a native path.
366 * This call does no blocking I/O.
368 * Returns: %TRUE if @file is native
371 g_file_is_native (GFile *file)
375 g_return_val_if_fail (G_IS_FILE (file), FALSE);
377 iface = G_FILE_GET_IFACE (file);
379 return (* iface->is_native) (file);
384 * g_file_has_uri_scheme:
385 * @file: input #GFile
386 * @uri_scheme: a string containing a URI scheme
388 * Checks to see if a #GFile has a given URI scheme.
390 * This call does no blocking I/O.
392 * Returns: %TRUE if #GFile's backend supports the
393 * given URI scheme, %FALSE if URI scheme is %NULL,
394 * not supported, or #GFile is invalid.
397 g_file_has_uri_scheme (GFile *file,
398 const char *uri_scheme)
402 g_return_val_if_fail (G_IS_FILE (file), FALSE);
403 g_return_val_if_fail (uri_scheme != NULL, FALSE);
405 iface = G_FILE_GET_IFACE (file);
407 return (* iface->has_uri_scheme) (file, uri_scheme);
412 * g_file_get_uri_scheme:
413 * @file: input #GFile
415 * Gets the URI scheme for a #GFile.
416 * RFC 3986 decodes the scheme as:
418 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
420 * Common schemes include "file", "http", "ftp", etc.
422 * This call does no blocking I/O.
424 * Returns: a string containing the URI scheme for the given
425 * #GFile. The returned string should be freed with g_free()
426 * when no longer needed.
429 g_file_get_uri_scheme (GFile *file)
433 g_return_val_if_fail (G_IS_FILE (file), NULL);
435 iface = G_FILE_GET_IFACE (file);
437 return (* iface->get_uri_scheme) (file);
442 * g_file_get_basename:
443 * @file: input #GFile
445 * Gets the base name (the last component of the path) for a given #GFile.
447 * If called for the top level of a system (such as the filesystem root
448 * or a uri like sftp://host/) it will return a single directory separator
449 * (and on Windows, possibly a drive letter).
451 * The base name is a byte string (not UTF-8). It has no defined encoding
452 * or rules other than it may not contain zero bytes. If you want to use
453 * filenames in a user interface you should use the display name that you
454 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
455 * attribute with g_file_query_info().
457 * This call does no blocking I/O.
459 * Returns: string containing the #GFile's base name, or %NULL
460 * if given #GFile is invalid. The returned string should be
461 * freed with g_free() when no longer needed.
464 g_file_get_basename (GFile *file)
468 g_return_val_if_fail (G_IS_FILE (file), NULL);
470 iface = G_FILE_GET_IFACE (file);
472 return (* iface->get_basename) (file);
477 * @file: input #GFile
479 * Gets the local pathname for #GFile, if one exists.
481 * This call does no blocking I/O.
483 * Returns: string containing the #GFile's path, or %NULL if
484 * no such path exists. The returned string should be
485 * freed with g_free() when no longer needed.
488 g_file_get_path (GFile *file)
492 g_return_val_if_fail (G_IS_FILE (file), NULL);
494 iface = G_FILE_GET_IFACE (file);
496 return (* iface->get_path) (file);
501 * @file: input #GFile
503 * Gets the URI for the @file.
505 * This call does no blocking I/O.
507 * Returns: a string containing the #GFile's URI.
508 * The returned string should be freed with g_free()
509 * when no longer needed.
512 g_file_get_uri (GFile *file)
516 g_return_val_if_fail (G_IS_FILE (file), NULL);
518 iface = G_FILE_GET_IFACE (file);
520 return (* iface->get_uri) (file);
524 * g_file_get_parse_name:
525 * @file: input #GFile
527 * Gets the parse name of the @file.
528 * A parse name is a UTF-8 string that describes the
529 * file such that one can get the #GFile back using
530 * g_file_parse_name().
532 * This is generally used to show the #GFile as a nice
533 * full-pathname kind of string in a user interface,
534 * like in a location entry.
536 * For local files with names that can safely be converted
537 * to UTF-8 the pathname is used, otherwise the IRI is used
538 * (a form of URI that allows UTF-8 characters unescaped).
540 * This call does no blocking I/O.
542 * Returns: a string containing the #GFile's parse name.
543 * The returned string should be freed with g_free()
544 * when no longer needed.
547 g_file_get_parse_name (GFile *file)
551 g_return_val_if_fail (G_IS_FILE (file), NULL);
553 iface = G_FILE_GET_IFACE (file);
555 return (* iface->get_parse_name) (file);
560 * @file: input #GFile
562 * Duplicates a #GFile handle. This operation does not duplicate
563 * the actual file or directory represented by the #GFile; see
564 * g_file_copy() if attempting to copy a file.
566 * This call does no blocking I/O.
568 * Returns: (transfer full): a new #GFile that is a duplicate
569 * of the given #GFile.
572 g_file_dup (GFile *file)
576 g_return_val_if_fail (G_IS_FILE (file), NULL);
578 iface = G_FILE_GET_IFACE (file);
580 return (* iface->dup) (file);
585 * @file: (type GFile): #gconstpointer to a #GFile
587 * Creates a hash value for a #GFile.
589 * This call does no blocking I/O.
592 * Returns: 0 if @file is not a valid #GFile, otherwise an
593 * integer that can be used as hash value for the #GFile.
594 * This function is intended for easily hashing a #GFile to
595 * add to a #GHashTable or similar data structure.
598 g_file_hash (gconstpointer file)
602 g_return_val_if_fail (G_IS_FILE (file), 0);
604 iface = G_FILE_GET_IFACE (file);
606 return (* iface->hash) ((GFile *)file);
611 * @file1: the first #GFile
612 * @file2: the second #GFile
614 * Checks equality of two given #GFiles.
616 * Note that two #GFiles that differ can still refer to the same
617 * file on the filesystem due to various forms of filename
620 * This call does no blocking I/O.
622 * Returns: %TRUE if @file1 and @file2 are equal.
623 * %FALSE if either is not a #GFile.
626 g_file_equal (GFile *file1,
631 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
632 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
634 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
637 iface = G_FILE_GET_IFACE (file1);
639 return (* iface->equal) (file1, file2);
645 * @file: input #GFile
647 * Gets the parent directory for the @file.
648 * If the @file represents the root directory of the
649 * file system, then %NULL will be returned.
651 * This call does no blocking I/O.
653 * Returns: (transfer full): a #GFile structure to the
654 * parent of the given #GFile or %NULL if there is
655 * no parent. Free the returned object with g_object_unref().
658 g_file_get_parent (GFile *file)
662 g_return_val_if_fail (G_IS_FILE (file), NULL);
664 iface = G_FILE_GET_IFACE (file);
666 return (* iface->get_parent) (file);
671 * @file: input #GFile
672 * @parent: (allow-none): the parent to check for, or %NULL
674 * Checks if @file has a parent, and optionally, if it is @parent.
676 * If @parent is %NULL then this function returns %TRUE if @file has any
677 * parent at all. If @parent is non-%NULL then %TRUE is only returned
678 * if @file is a child of @parent.
680 * Returns: %TRUE if @file is a child of @parent (or any parent in the
681 * case that @parent is %NULL).
686 g_file_has_parent (GFile *file,
689 GFile *actual_parent;
692 g_return_val_if_fail (G_IS_FILE (file), FALSE);
693 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
695 actual_parent = g_file_get_parent (file);
697 if (actual_parent != NULL)
700 result = g_file_equal (parent, actual_parent);
704 g_object_unref (actual_parent);
714 * @file: input #GFile
715 * @name: string containing the child's basename
717 * Gets a child of @file with basename equal to @name.
719 * Note that the file with that specific name might not exist, but
720 * you can still have a #GFile that points to it. You can use this
721 * for instance to create that file.
723 * This call does no blocking I/O.
725 * Returns: (transfer full): a #GFile to a child specified by @name.
726 * Free the returned object with g_object_unref().
729 g_file_get_child (GFile *file,
732 g_return_val_if_fail (G_IS_FILE (file), NULL);
733 g_return_val_if_fail (name != NULL, NULL);
735 return g_file_resolve_relative_path (file, name);
739 * g_file_get_child_for_display_name:
740 * @file: input #GFile
741 * @display_name: string to a possible child
742 * @error: return location for an error
744 * Gets the child of @file for a given @display_name (i.e. a UTF-8
745 * version of the name). If this function fails, it returns %NULL
746 * and @error will be set. This is very useful when constructing a
747 * #GFile for a new file and the user entered the filename in the
748 * user interface, for instance when you select a directory and
749 * type a filename in the file selector.
751 * This call does no blocking I/O.
753 * Returns: (transfer full): a #GFile to the specified child, or
754 * %NULL if the display name couldn't be converted.
755 * Free the returned object with g_object_unref().
758 g_file_get_child_for_display_name (GFile *file,
759 const char *display_name,
764 g_return_val_if_fail (G_IS_FILE (file), NULL);
765 g_return_val_if_fail (display_name != NULL, NULL);
767 iface = G_FILE_GET_IFACE (file);
769 return (* iface->get_child_for_display_name) (file, display_name, error);
774 * @file: input #GFile
775 * @prefix: input #GFile
777 * Checks whether @file has the prefix specified by @prefix.
779 * In other words, if the names of initial elements of @file's
780 * pathname match @prefix. Only full pathname elements are matched,
781 * so a path like /foo is not considered a prefix of /foobar, only
784 * This call does no I/O, as it works purely on names. As such it can
785 * sometimes return %FALSE even if @file is inside a @prefix (from a
786 * filesystem point of view), because the prefix of @file is an alias
789 * Virtual: prefix_matches
790 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
794 g_file_has_prefix (GFile *file,
799 g_return_val_if_fail (G_IS_FILE (file), FALSE);
800 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
802 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
805 iface = G_FILE_GET_IFACE (file);
807 /* The vtable function differs in arg order since
808 * we're using the old contains_file call
810 return (* iface->prefix_matches) (prefix, file);
814 * g_file_get_relative_path:
815 * @parent: input #GFile
816 * @descendant: input #GFile
818 * Gets the path for @descendant relative to @parent.
820 * This call does no blocking I/O.
822 * Returns: string with the relative path from @descendant
823 * to @parent, or %NULL if @descendant doesn't have @parent
824 * as prefix. The returned string should be freed with g_free()
825 * when no longer needed.
828 g_file_get_relative_path (GFile *parent,
833 g_return_val_if_fail (G_IS_FILE (parent), NULL);
834 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
836 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
839 iface = G_FILE_GET_IFACE (parent);
841 return (* iface->get_relative_path) (parent, descendant);
845 * g_file_resolve_relative_path:
846 * @file: input #GFile
847 * @relative_path: a given relative path string
849 * Resolves a relative path for @file to an absolute path.
851 * This call does no blocking I/O.
853 * Returns: (transfer full): #GFile to the resolved path.
854 * %NULL if @relative_path is %NULL or if @file is invalid.
855 * Free the returned object with g_object_unref().
858 g_file_resolve_relative_path (GFile *file,
859 const char *relative_path)
863 g_return_val_if_fail (G_IS_FILE (file), NULL);
864 g_return_val_if_fail (relative_path != NULL, NULL);
866 iface = G_FILE_GET_IFACE (file);
868 return (* iface->resolve_relative_path) (file, relative_path);
872 * g_file_enumerate_children:
873 * @file: input #GFile
874 * @attributes: an attribute query string
875 * @flags: a set of #GFileQueryInfoFlags
876 * @cancellable: (allow-none): optional #GCancellable object,
878 * @error: #GError for error reporting
880 * Gets the requested information about the files in a directory.
881 * The result is a #GFileEnumerator object that will give out
882 * #GFileInfo objects for all the files in the directory.
884 * The @attributes value is a string that specifies the file
885 * attributes that should be gathered. It is not an error if
886 * it's not possible to read a particular requested attribute
887 * from a file - it just won't be set. @attributes should
888 * be a comma-separated list of attributes or attribute wildcards.
889 * The wildcard "*" means all attributes, and a wildcard like
890 * "standard::*" means all attributes in the standard namespace.
891 * An example attribute query be "standard::*,owner::user".
892 * The standard attributes are available as defines, like
893 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
895 * If @cancellable is not %NULL, then the operation can be cancelled
896 * by triggering the cancellable object from another thread. If the
897 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
900 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
901 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
902 * error will be returned. Other errors are possible too.
904 * Returns: (transfer full): A #GFileEnumerator if successful,
905 * %NULL on error. Free the returned object with g_object_unref().
908 g_file_enumerate_children (GFile *file,
909 const char *attributes,
910 GFileQueryInfoFlags flags,
911 GCancellable *cancellable,
916 g_return_val_if_fail (G_IS_FILE (file), NULL);
918 if (g_cancellable_set_error_if_cancelled (cancellable, error))
921 iface = G_FILE_GET_IFACE (file);
923 if (iface->enumerate_children == NULL)
925 g_set_error_literal (error, G_IO_ERROR,
926 G_IO_ERROR_NOT_SUPPORTED,
927 _("Operation not supported"));
931 return (* iface->enumerate_children) (file, attributes, flags,
936 * g_file_enumerate_children_async:
937 * @file: input #GFile
938 * @attributes: an attribute query string
939 * @flags: a set of #GFileQueryInfoFlags
940 * @io_priority: the <link linkend="io-priority">I/O priority</link>
942 * @cancellable: (allow-none): optional #GCancellable object,
944 * @callback: (scope async): a #GAsyncReadyCallback to call when the
945 * request is satisfied
946 * @user_data: (closure): the data to pass to callback function
948 * Asynchronously gets the requested information about the files
949 * in a directory. The result is a #GFileEnumerator object that will
950 * give out #GFileInfo objects for all the files in the directory.
952 * For more details, see g_file_enumerate_children() which is
953 * the synchronous version of this call.
955 * When the operation is finished, @callback will be called. You can
956 * then call g_file_enumerate_children_finish() to get the result of
960 g_file_enumerate_children_async (GFile *file,
961 const char *attributes,
962 GFileQueryInfoFlags flags,
964 GCancellable *cancellable,
965 GAsyncReadyCallback callback,
970 g_return_if_fail (G_IS_FILE (file));
972 iface = G_FILE_GET_IFACE (file);
973 (* iface->enumerate_children_async) (file,
983 * g_file_enumerate_children_finish:
984 * @file: input #GFile
985 * @res: a #GAsyncResult
988 * Finishes an async enumerate children operation.
989 * See g_file_enumerate_children_async().
991 * Returns: (transfer full): a #GFileEnumerator or %NULL
992 * if an error occurred.
993 * Free the returned object with g_object_unref().
996 g_file_enumerate_children_finish (GFile *file,
1002 g_return_val_if_fail (G_IS_FILE (file), NULL);
1003 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1005 if (g_async_result_legacy_propagate_error (res, error))
1008 iface = G_FILE_GET_IFACE (file);
1009 return (* iface->enumerate_children_finish) (file, res, error);
1013 * g_file_query_exists:
1014 * @file: input #GFile
1015 * @cancellable: (allow-none): optional #GCancellable object,
1018 * Utility function to check if a particular file exists. This is
1019 * implemented using g_file_query_info() and as such does blocking I/O.
1021 * Note that in many cases it is racy to first check for file existence
1022 * and then execute something based on the outcome of that, because the
1023 * file might have been created or removed in between the operations. The
1024 * general approach to handling that is to not check, but just do the
1025 * operation and handle the errors as they come.
1027 * As an example of race-free checking, take the case of reading a file,
1028 * and if it doesn't exist, creating it. There are two racy versions: read
1029 * it, and on error create it; and: check if it exists, if not create it.
1030 * These can both result in two processes creating the file (with perhaps
1031 * a partially written file as the result). The correct approach is to
1032 * always try to create the file with g_file_create() which will either
1033 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1035 * However, in many cases an existence check is useful in a user interface,
1036 * for instance to make a menu item sensitive/insensitive, so that you don't
1037 * have to fool users that something is possible and then just show an error
1038 * dialog. If you do this, you should make sure to also handle the errors
1039 * that can happen due to races when you execute the operation.
1041 * Returns: %TRUE if the file exists (and can be detected without error),
1042 * %FALSE otherwise (or if cancelled).
1045 g_file_query_exists (GFile *file,
1046 GCancellable *cancellable)
1050 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1052 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1053 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1056 g_object_unref (info);
1064 * g_file_query_file_type:
1065 * @file: input #GFile
1066 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1067 * @cancellable: (allow-none): optional #GCancellable object,
1070 * Utility function to inspect the #GFileType of a file. This is
1071 * implemented using g_file_query_info() and as such does blocking I/O.
1073 * The primary use case of this method is to check if a file is
1074 * a regular file, directory, or symlink.
1076 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1077 * if the file does not exist
1082 g_file_query_file_type (GFile *file,
1083 GFileQueryInfoFlags flags,
1084 GCancellable *cancellable)
1087 GFileType file_type;
1089 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1090 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1094 file_type = g_file_info_get_file_type (info);
1095 g_object_unref (info);
1098 file_type = G_FILE_TYPE_UNKNOWN;
1104 * g_file_query_info:
1105 * @file: input #GFile
1106 * @attributes: an attribute query string
1107 * @flags: a set of #GFileQueryInfoFlags
1108 * @cancellable: (allow-none): optional #GCancellable object,
1112 * Gets the requested information about specified @file.
1113 * The result is a #GFileInfo object that contains key-value
1114 * attributes (such as the type or size of the file).
1116 * The @attributes value is a string that specifies the file
1117 * attributes that should be gathered. It is not an error if
1118 * it's not possible to read a particular requested attribute
1119 * from a file - it just won't be set. @attributes should be a
1120 * comma-separated list of attributes or attribute wildcards.
1121 * The wildcard "*" means all attributes, and a wildcard like
1122 * "standard::*" means all attributes in the standard namespace.
1123 * An example attribute query be "standard::*,owner::user".
1124 * The standard attributes are available as defines, like
1125 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1127 * If @cancellable is not %NULL, then the operation can be cancelled
1128 * by triggering the cancellable object from another thread. If the
1129 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1132 * For symlinks, normally the information about the target of the
1133 * symlink is returned, rather than information about the symlink
1134 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1135 * in @flags the information about the symlink itself will be returned.
1136 * Also, for symlinks that point to non-existing files the information
1137 * about the symlink itself will be returned.
1139 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1140 * returned. Other errors are possible too, and depend on what kind of
1141 * filesystem the file is on.
1143 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1144 * on error. Free the returned object with g_object_unref().
1147 g_file_query_info (GFile *file,
1148 const char *attributes,
1149 GFileQueryInfoFlags flags,
1150 GCancellable *cancellable,
1155 g_return_val_if_fail (G_IS_FILE (file), NULL);
1157 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1160 iface = G_FILE_GET_IFACE (file);
1162 if (iface->query_info == NULL)
1164 g_set_error_literal (error, G_IO_ERROR,
1165 G_IO_ERROR_NOT_SUPPORTED,
1166 _("Operation not supported"));
1170 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1174 * g_file_query_info_async:
1175 * @file: input #GFile
1176 * @attributes: an attribute query string
1177 * @flags: a set of #GFileQueryInfoFlags
1178 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1180 * @cancellable: (allow-none): optional #GCancellable object,
1182 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1183 * request is satisfied
1184 * @user_data: (closure): the data to pass to callback function
1186 * Asynchronously gets the requested information about specified @file.
1187 * The result is a #GFileInfo object that contains key-value attributes
1188 * (such as type or size for the file).
1190 * For more details, see g_file_query_info() which is the synchronous
1191 * version of this call.
1193 * When the operation is finished, @callback will be called. You can
1194 * then call g_file_query_info_finish() to get the result of the operation.
1197 g_file_query_info_async (GFile *file,
1198 const char *attributes,
1199 GFileQueryInfoFlags flags,
1201 GCancellable *cancellable,
1202 GAsyncReadyCallback callback,
1207 g_return_if_fail (G_IS_FILE (file));
1209 iface = G_FILE_GET_IFACE (file);
1210 (* iface->query_info_async) (file,
1220 * g_file_query_info_finish:
1221 * @file: input #GFile
1222 * @res: a #GAsyncResult
1225 * Finishes an asynchronous file info query.
1226 * See g_file_query_info_async().
1228 * Returns: (transfer full): #GFileInfo for given @file
1229 * or %NULL on error. Free the returned object with
1233 g_file_query_info_finish (GFile *file,
1239 g_return_val_if_fail (G_IS_FILE (file), NULL);
1240 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1242 if (g_async_result_legacy_propagate_error (res, error))
1245 iface = G_FILE_GET_IFACE (file);
1246 return (* iface->query_info_finish) (file, res, error);
1250 * g_file_query_filesystem_info:
1251 * @file: input #GFile
1252 * @attributes: an attribute query string
1253 * @cancellable: (allow-none): optional #GCancellable object,
1257 * Similar to g_file_query_info(), but obtains information
1258 * about the filesystem the @file is on, rather than the file itself.
1259 * For instance the amount of space available and the type of
1262 * The @attributes value is a string that specifies the attributes
1263 * that should be gathered. It is not an error if it's not possible
1264 * to read a particular requested attribute from a file - it just
1265 * won't be set. @attributes should be a comma-separated list of
1266 * attributes or attribute wildcards. The wildcard "*" means all
1267 * attributes, and a wildcard like "filesystem::*" means all attributes
1268 * in the filesystem namespace. The standard namespace for filesystem
1269 * attributes is "filesystem". Common attributes of interest are
1270 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1271 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1272 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1274 * If @cancellable is not %NULL, then the operation can be cancelled
1275 * by triggering the cancellable object from another thread. If the
1276 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1279 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1280 * be returned. Other errors are possible too, and depend on what
1281 * kind of filesystem the file is on.
1283 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1284 * Free the returned object with g_object_unref().
1287 g_file_query_filesystem_info (GFile *file,
1288 const char *attributes,
1289 GCancellable *cancellable,
1294 g_return_val_if_fail (G_IS_FILE (file), NULL);
1296 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1299 iface = G_FILE_GET_IFACE (file);
1301 if (iface->query_filesystem_info == NULL)
1303 g_set_error_literal (error, G_IO_ERROR,
1304 G_IO_ERROR_NOT_SUPPORTED,
1305 _("Operation not supported"));
1309 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1313 * g_file_query_filesystem_info_async:
1314 * @file: input #GFile
1315 * @attributes: an attribute query string
1316 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1318 * @cancellable: (allow-none): optional #GCancellable object,
1320 * @callback: (scope async): a #GAsyncReadyCallback to call
1321 * when the request is satisfied
1322 * @user_data: (closure): the data to pass to callback function
1324 * Asynchronously gets the requested information about the filesystem
1325 * that the specified @file is on. The result is a #GFileInfo object
1326 * that contains key-value attributes (such as type or size for the
1329 * For more details, see g_file_query_filesystem_info() which is the
1330 * synchronous version of this call.
1332 * When the operation is finished, @callback will be called. You can
1333 * then call g_file_query_info_finish() to get the result of the
1337 g_file_query_filesystem_info_async (GFile *file,
1338 const char *attributes,
1340 GCancellable *cancellable,
1341 GAsyncReadyCallback callback,
1346 g_return_if_fail (G_IS_FILE (file));
1348 iface = G_FILE_GET_IFACE (file);
1349 (* iface->query_filesystem_info_async) (file,
1358 * g_file_query_filesystem_info_finish:
1359 * @file: input #GFile
1360 * @res: a #GAsyncResult
1363 * Finishes an asynchronous filesystem info query.
1364 * See g_file_query_filesystem_info_async().
1366 * Returns: (transfer full): #GFileInfo for given @file
1367 * or %NULL on error.
1368 * Free the returned object with g_object_unref().
1371 g_file_query_filesystem_info_finish (GFile *file,
1377 g_return_val_if_fail (G_IS_FILE (file), NULL);
1378 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1380 if (g_async_result_legacy_propagate_error (res, error))
1383 iface = G_FILE_GET_IFACE (file);
1384 return (* iface->query_filesystem_info_finish) (file, res, error);
1388 * g_file_find_enclosing_mount:
1389 * @file: input #GFile
1390 * @cancellable: (allow-none): optional #GCancellable object,
1394 * Gets a #GMount for the #GFile.
1396 * If the #GFileIface for @file does not have a mount (e.g.
1397 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1398 * and %NULL will be returned.
1400 * If @cancellable is not %NULL, then the operation can be cancelled by
1401 * triggering the cancellable object from another thread. If the operation
1402 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1404 * Returns: (transfer full): a #GMount where the @file is located
1405 * or %NULL on error.
1406 * Free the returned object with g_object_unref().
1409 g_file_find_enclosing_mount (GFile *file,
1410 GCancellable *cancellable,
1415 g_return_val_if_fail (G_IS_FILE (file), NULL);
1417 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1420 iface = G_FILE_GET_IFACE (file);
1421 if (iface->find_enclosing_mount == NULL)
1424 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1425 /* Translators: This is an error message when
1426 * trying to find the enclosing (user visible)
1427 * mount of a file, but none exists.
1429 _("Containing mount does not exist"));
1433 return (* iface->find_enclosing_mount) (file, cancellable, error);
1437 * g_file_find_enclosing_mount_async:
1439 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1441 * @cancellable: (allow-none): optional #GCancellable object,
1443 * @callback: (scope async): a #GAsyncReadyCallback to call
1444 * when the request is satisfied
1445 * @user_data: (closure): the data to pass to callback function
1447 * Asynchronously gets the mount for the file.
1449 * For more details, see g_file_find_enclosing_mount() which is
1450 * the synchronous version of this call.
1452 * When the operation is finished, @callback will be called.
1453 * You can then call g_file_find_enclosing_mount_finish() to
1454 * get the result of the operation.
1457 g_file_find_enclosing_mount_async (GFile *file,
1459 GCancellable *cancellable,
1460 GAsyncReadyCallback callback,
1465 g_return_if_fail (G_IS_FILE (file));
1467 iface = G_FILE_GET_IFACE (file);
1468 (* iface->find_enclosing_mount_async) (file,
1476 * g_file_find_enclosing_mount_finish:
1478 * @res: a #GAsyncResult
1481 * Finishes an asynchronous find mount request.
1482 * See g_file_find_enclosing_mount_async().
1484 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1485 * Free the returned object with g_object_unref().
1488 g_file_find_enclosing_mount_finish (GFile *file,
1494 g_return_val_if_fail (G_IS_FILE (file), NULL);
1495 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1497 if (g_async_result_legacy_propagate_error (res, error))
1500 iface = G_FILE_GET_IFACE (file);
1501 return (* iface->find_enclosing_mount_finish) (file, res, error);
1507 * @file: #GFile to read
1508 * @cancellable: (allow-none): a #GCancellable
1509 * @error: a #GError, or %NULL
1511 * Opens a file for reading. The result is a #GFileInputStream that
1512 * can be used to read the contents of the file.
1514 * If @cancellable is not %NULL, then the operation can be cancelled by
1515 * triggering the cancellable object from another thread. If the operation
1516 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1518 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1519 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1520 * error will be returned. Other errors are possible too, and depend
1521 * on what kind of filesystem the file is on.
1524 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1525 * Free the returned object with g_object_unref().
1528 g_file_read (GFile *file,
1529 GCancellable *cancellable,
1534 g_return_val_if_fail (G_IS_FILE (file), NULL);
1536 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1539 iface = G_FILE_GET_IFACE (file);
1541 if (iface->read_fn == NULL)
1543 g_set_error_literal (error, G_IO_ERROR,
1544 G_IO_ERROR_NOT_SUPPORTED,
1545 _("Operation not supported"));
1549 return (* iface->read_fn) (file, cancellable, error);
1554 * @file: input #GFile
1555 * @flags: a set of #GFileCreateFlags
1556 * @cancellable: (allow-none): optional #GCancellable object,
1558 * @error: a #GError, or %NULL
1560 * Gets an output stream for appending data to the file.
1561 * If the file doesn't already exist it is created.
1563 * By default files created are generally readable by everyone,
1564 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1565 * will be made readable only to the current user, to the level that
1566 * is supported on the target filesystem.
1568 * If @cancellable is not %NULL, then the operation can be cancelled
1569 * by triggering the cancellable object from another thread. If the
1570 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1573 * Some file systems don't allow all file names, and may return an
1574 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1575 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1576 * possible too, and depend on what kind of filesystem the file is on.
1578 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1579 * Free the returned object with g_object_unref().
1582 g_file_append_to (GFile *file,
1583 GFileCreateFlags flags,
1584 GCancellable *cancellable,
1589 g_return_val_if_fail (G_IS_FILE (file), NULL);
1591 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1594 iface = G_FILE_GET_IFACE (file);
1596 if (iface->append_to == NULL)
1598 g_set_error_literal (error, G_IO_ERROR,
1599 G_IO_ERROR_NOT_SUPPORTED,
1600 _("Operation not supported"));
1604 return (* iface->append_to) (file, flags, cancellable, error);
1609 * @file: input #GFile
1610 * @flags: a set of #GFileCreateFlags
1611 * @cancellable: (allow-none): optional #GCancellable object,
1613 * @error: a #GError, or %NULL
1615 * Creates a new file and returns an output stream for writing to it.
1616 * The file must not already exist.
1618 * By default files created are generally readable by everyone,
1619 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1620 * will be made readable only to the current user, to the level
1621 * that is supported on the target filesystem.
1623 * If @cancellable is not %NULL, then the operation can be cancelled
1624 * by triggering the cancellable object from another thread. If the
1625 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1628 * If a file or directory with this name already exists the
1629 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1630 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1631 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1632 * be returned. Other errors are possible too, and depend on what kind
1633 * of filesystem the file is on.
1635 * Returns: (transfer full): a #GFileOutputStream for the newly created
1636 * file, or %NULL on error.
1637 * Free the returned object with g_object_unref().
1640 g_file_create (GFile *file,
1641 GFileCreateFlags flags,
1642 GCancellable *cancellable,
1647 g_return_val_if_fail (G_IS_FILE (file), NULL);
1649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1652 iface = G_FILE_GET_IFACE (file);
1654 if (iface->create == NULL)
1656 g_set_error_literal (error, G_IO_ERROR,
1657 G_IO_ERROR_NOT_SUPPORTED,
1658 _("Operation not supported"));
1662 return (* iface->create) (file, flags, cancellable, error);
1667 * @file: input #GFile
1668 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1669 * for the current #GFile, or #NULL to ignore
1670 * @make_backup: %TRUE if a backup should be created
1671 * @flags: a set of #GFileCreateFlags
1672 * @cancellable: (allow-none): optional #GCancellable object,
1674 * @error: a #GError, or %NULL
1676 * Returns an output stream for overwriting the file, possibly
1677 * creating a backup copy of the file first. If the file doesn't exist,
1678 * it will be created.
1680 * This will try to replace the file in the safest way possible so
1681 * that any errors during the writing will not affect an already
1682 * existing copy of the file. For instance, for local files it
1683 * may write to a temporary file and then atomically rename over
1684 * the destination when the stream is closed.
1686 * By default files created are generally readable by everyone,
1687 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1688 * will be made readable only to the current user, to the level that
1689 * is supported on the target filesystem.
1691 * If @cancellable is not %NULL, then the operation can be cancelled
1692 * by triggering the cancellable object from another thread. If the
1693 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1696 * If you pass in a non-%NULL @etag value, then this value is
1697 * compared to the current entity tag of the file, and if they differ
1698 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1699 * that the file has been changed since you last read it. You can get
1700 * the new etag from g_file_output_stream_get_etag() after you've
1701 * finished writing and closed the #GFileOutputStream. When you load
1702 * a new file you can use g_file_input_stream_query_info() to get
1703 * the etag of the file.
1705 * If @make_backup is %TRUE, this function will attempt to make a
1706 * backup of the current file before overwriting it. If this fails
1707 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1708 * want to replace anyway, try again with @make_backup set to %FALSE.
1710 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1711 * be returned, and if the file is some other form of non-regular file
1712 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1713 * file systems don't allow all file names, and may return an
1714 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1715 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1716 * possible too, and depend on what kind of filesystem the file is on.
1718 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1719 * Free the returned object with g_object_unref().
1722 g_file_replace (GFile *file,
1724 gboolean make_backup,
1725 GFileCreateFlags flags,
1726 GCancellable *cancellable,
1731 g_return_val_if_fail (G_IS_FILE (file), NULL);
1733 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1736 iface = G_FILE_GET_IFACE (file);
1738 if (iface->replace == NULL)
1740 g_set_error_literal (error, G_IO_ERROR,
1741 G_IO_ERROR_NOT_SUPPORTED,
1742 _("Operation not supported"));
1746 /* Handle empty tag string as NULL in consistent way. */
1747 if (etag && *etag == 0)
1750 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1754 * g_file_open_readwrite:
1755 * @file: #GFile to open
1756 * @cancellable: (allow-none): a #GCancellable
1757 * @error: a #GError, or %NULL
1759 * Opens an existing file for reading and writing. The result is
1760 * a #GFileIOStream that can be used to read and write the contents
1763 * If @cancellable is not %NULL, then the operation can be cancelled
1764 * by triggering the cancellable object from another thread. If the
1765 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1768 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1769 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1770 * error will be returned. Other errors are possible too, and depend on
1771 * what kind of filesystem the file is on. Note that in many non-local
1772 * file cases read and write streams are not supported, so make sure you
1773 * really need to do read and write streaming, rather than just opening
1774 * for reading or writing.
1776 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1777 * Free the returned object with g_object_unref().
1782 g_file_open_readwrite (GFile *file,
1783 GCancellable *cancellable,
1788 g_return_val_if_fail (G_IS_FILE (file), NULL);
1790 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1793 iface = G_FILE_GET_IFACE (file);
1795 if (iface->open_readwrite == NULL)
1797 g_set_error_literal (error, G_IO_ERROR,
1798 G_IO_ERROR_NOT_SUPPORTED,
1799 _("Operation not supported"));
1803 return (* iface->open_readwrite) (file, cancellable, error);
1807 * g_file_create_readwrite:
1809 * @flags: a set of #GFileCreateFlags
1810 * @cancellable: (allow-none): optional #GCancellable object,
1812 * @error: return location for a #GError, or %NULL
1814 * Creates a new file and returns a stream for reading and
1815 * writing to it. The file must not already exist.
1817 * By default files created are generally readable by everyone,
1818 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1819 * will be made readable only to the current user, to the level
1820 * that is supported on the target filesystem.
1822 * If @cancellable is not %NULL, then the operation can be cancelled
1823 * by triggering the cancellable object from another thread. If the
1824 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1827 * If a file or directory with this name already exists, the
1828 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1829 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1830 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1831 * will be returned. Other errors are possible too, and depend on what
1832 * kind of filesystem the file is on.
1834 * Note that in many non-local file cases read and write streams are
1835 * not supported, so make sure you really need to do read and write
1836 * streaming, rather than just opening for reading or writing.
1838 * Returns: (transfer full): a #GFileIOStream for the newly created
1839 * file, or %NULL on error.
1840 * Free the returned object with g_object_unref().
1845 g_file_create_readwrite (GFile *file,
1846 GFileCreateFlags flags,
1847 GCancellable *cancellable,
1852 g_return_val_if_fail (G_IS_FILE (file), NULL);
1854 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1857 iface = G_FILE_GET_IFACE (file);
1859 if (iface->create_readwrite == NULL)
1861 g_set_error_literal (error, G_IO_ERROR,
1862 G_IO_ERROR_NOT_SUPPORTED,
1863 _("Operation not supported"));
1867 return (* iface->create_readwrite) (file, flags, cancellable, error);
1871 * g_file_replace_readwrite:
1873 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1874 * for the current #GFile, or #NULL to ignore
1875 * @make_backup: %TRUE if a backup should be created
1876 * @flags: a set of #GFileCreateFlags
1877 * @cancellable: (allow-none): optional #GCancellable object,
1879 * @error: return location for a #GError, or %NULL
1881 * Returns an output stream for overwriting the file in readwrite mode,
1882 * possibly creating a backup copy of the file first. If the file doesn't
1883 * exist, it will be created.
1885 * For details about the behaviour, see g_file_replace() which does the
1886 * same thing but returns an output stream only.
1888 * Note that in many non-local file cases read and write streams are not
1889 * supported, so make sure you really need to do read and write streaming,
1890 * rather than just opening for reading or writing.
1892 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1893 * Free the returned object with g_object_unref().
1898 g_file_replace_readwrite (GFile *file,
1900 gboolean make_backup,
1901 GFileCreateFlags flags,
1902 GCancellable *cancellable,
1907 g_return_val_if_fail (G_IS_FILE (file), NULL);
1909 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1912 iface = G_FILE_GET_IFACE (file);
1914 if (iface->replace_readwrite == NULL)
1916 g_set_error_literal (error, G_IO_ERROR,
1917 G_IO_ERROR_NOT_SUPPORTED,
1918 _("Operation not supported"));
1922 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1926 * g_file_read_async:
1927 * @file: input #GFile
1928 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1930 * @cancellable: (allow-none): optional #GCancellable object,
1932 * @callback: (scope async): a #GAsyncReadyCallback to call
1933 * when the request is satisfied
1934 * @user_data: (closure): the data to pass to callback function
1936 * Asynchronously opens @file for reading.
1938 * For more details, see g_file_read() which is
1939 * the synchronous version of this call.
1941 * When the operation is finished, @callback will be called.
1942 * You can then call g_file_read_finish() to get the result
1946 g_file_read_async (GFile *file,
1948 GCancellable *cancellable,
1949 GAsyncReadyCallback callback,
1954 g_return_if_fail (G_IS_FILE (file));
1956 iface = G_FILE_GET_IFACE (file);
1957 (* iface->read_async) (file,
1965 * g_file_read_finish:
1966 * @file: input #GFile
1967 * @res: a #GAsyncResult
1968 * @error: a #GError, or %NULL
1970 * Finishes an asynchronous file read operation started with
1971 * g_file_read_async().
1973 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1974 * Free the returned object with g_object_unref().
1977 g_file_read_finish (GFile *file,
1983 g_return_val_if_fail (G_IS_FILE (file), NULL);
1984 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1986 if (g_async_result_legacy_propagate_error (res, error))
1989 iface = G_FILE_GET_IFACE (file);
1990 return (* iface->read_finish) (file, res, error);
1994 * g_file_append_to_async:
1995 * @file: input #GFile
1996 * @flags: a set of #GFileCreateFlags
1997 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1999 * @cancellable: (allow-none): optional #GCancellable object,
2001 * @callback: (scope async): a #GAsyncReadyCallback to call
2002 * when the request is satisfied
2003 * @user_data: (closure): the data to pass to callback function
2005 * Asynchronously opens @file for appending.
2007 * For more details, see g_file_append_to() which is
2008 * the synchronous version of this call.
2010 * When the operation is finished, @callback will be called.
2011 * You can then call g_file_append_to_finish() to get the result
2015 g_file_append_to_async (GFile *file,
2016 GFileCreateFlags flags,
2018 GCancellable *cancellable,
2019 GAsyncReadyCallback callback,
2024 g_return_if_fail (G_IS_FILE (file));
2026 iface = G_FILE_GET_IFACE (file);
2027 (* iface->append_to_async) (file,
2036 * g_file_append_to_finish:
2037 * @file: input #GFile
2038 * @res: #GAsyncResult
2039 * @error: a #GError, or %NULL
2041 * Finishes an asynchronous file append operation started with
2042 * g_file_append_to_async().
2044 * Returns: (transfer full): a valid #GFileOutputStream
2045 * or %NULL on error.
2046 * Free the returned object with g_object_unref().
2049 g_file_append_to_finish (GFile *file,
2055 g_return_val_if_fail (G_IS_FILE (file), NULL);
2056 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2058 if (g_async_result_legacy_propagate_error (res, error))
2061 iface = G_FILE_GET_IFACE (file);
2062 return (* iface->append_to_finish) (file, res, error);
2066 * g_file_create_async:
2067 * @file: input #GFile
2068 * @flags: a set of #GFileCreateFlags
2069 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2071 * @cancellable: (allow-none): optional #GCancellable object,
2073 * @callback: (scope async): a #GAsyncReadyCallback to call
2074 * when the request is satisfied
2075 * @user_data: (closure): the data to pass to callback function
2077 * Asynchronously creates a new file and returns an output stream
2078 * for writing to it. The file must not already exist.
2080 * For more details, see g_file_create() which is
2081 * the synchronous version of this call.
2083 * When the operation is finished, @callback will be called.
2084 * You can then call g_file_create_finish() to get the result
2088 g_file_create_async (GFile *file,
2089 GFileCreateFlags flags,
2091 GCancellable *cancellable,
2092 GAsyncReadyCallback callback,
2097 g_return_if_fail (G_IS_FILE (file));
2099 iface = G_FILE_GET_IFACE (file);
2100 (* iface->create_async) (file,
2109 * g_file_create_finish:
2110 * @file: input #GFile
2111 * @res: a #GAsyncResult
2112 * @error: a #GError, or %NULL
2114 * Finishes an asynchronous file create operation started with
2115 * g_file_create_async().
2117 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2118 * Free the returned object with g_object_unref().
2121 g_file_create_finish (GFile *file,
2127 g_return_val_if_fail (G_IS_FILE (file), NULL);
2128 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2130 if (g_async_result_legacy_propagate_error (res, error))
2133 iface = G_FILE_GET_IFACE (file);
2134 return (* iface->create_finish) (file, res, error);
2138 * g_file_replace_async:
2139 * @file: input #GFile
2140 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2141 * for the current #GFile, or NULL to ignore
2142 * @make_backup: %TRUE if a backup should be created
2143 * @flags: a set of #GFileCreateFlags
2144 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2146 * @cancellable: (allow-none): optional #GCancellable object,
2148 * @callback: (scope async): a #GAsyncReadyCallback to call
2149 * when the request is satisfied
2150 * @user_data: (closure): the data to pass to callback function
2152 * Asynchronously overwrites the file, replacing the contents,
2153 * possibly creating a backup copy of the file first.
2155 * For more details, see g_file_replace() which is
2156 * the synchronous version of this call.
2158 * When the operation is finished, @callback will be called.
2159 * You can then call g_file_replace_finish() to get the result
2163 g_file_replace_async (GFile *file,
2165 gboolean make_backup,
2166 GFileCreateFlags flags,
2168 GCancellable *cancellable,
2169 GAsyncReadyCallback callback,
2174 g_return_if_fail (G_IS_FILE (file));
2176 iface = G_FILE_GET_IFACE (file);
2177 (* iface->replace_async) (file,
2188 * g_file_replace_finish:
2189 * @file: input #GFile
2190 * @res: a #GAsyncResult
2191 * @error: a #GError, or %NULL
2193 * Finishes an asynchronous file replace operation started with
2194 * g_file_replace_async().
2196 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2197 * Free the returned object with g_object_unref().
2200 g_file_replace_finish (GFile *file,
2206 g_return_val_if_fail (G_IS_FILE (file), NULL);
2207 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2209 if (g_async_result_legacy_propagate_error (res, error))
2212 iface = G_FILE_GET_IFACE (file);
2213 return (* iface->replace_finish) (file, res, error);
2217 * g_file_open_readwrite_async
2218 * @file: input #GFile
2219 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2221 * @cancellable: (allow-none): optional #GCancellable object,
2223 * @callback: (scope async): a #GAsyncReadyCallback to call
2224 * when the request is satisfied
2225 * @user_data: (closure): the data to pass to callback function
2227 * Asynchronously opens @file for reading and writing.
2229 * For more details, see g_file_open_readwrite() which is
2230 * the synchronous version of this call.
2232 * When the operation is finished, @callback will be called.
2233 * You can then call g_file_open_readwrite_finish() to get
2234 * the result of the operation.
2239 g_file_open_readwrite_async (GFile *file,
2241 GCancellable *cancellable,
2242 GAsyncReadyCallback callback,
2247 g_return_if_fail (G_IS_FILE (file));
2249 iface = G_FILE_GET_IFACE (file);
2250 (* iface->open_readwrite_async) (file,
2258 * g_file_open_readwrite_finish:
2259 * @file: input #GFile
2260 * @res: a #GAsyncResult
2261 * @error: a #GError, or %NULL
2263 * Finishes an asynchronous file read operation started with
2264 * g_file_open_readwrite_async().
2266 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2267 * Free the returned object with g_object_unref().
2272 g_file_open_readwrite_finish (GFile *file,
2278 g_return_val_if_fail (G_IS_FILE (file), NULL);
2279 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2281 if (g_async_result_legacy_propagate_error (res, error))
2284 iface = G_FILE_GET_IFACE (file);
2285 return (* iface->open_readwrite_finish) (file, res, error);
2289 * g_file_create_readwrite_async:
2290 * @file: input #GFile
2291 * @flags: a set of #GFileCreateFlags
2292 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2294 * @cancellable: (allow-none): optional #GCancellable object,
2296 * @callback: (scope async): a #GAsyncReadyCallback to call
2297 * when the request is satisfied
2298 * @user_data: (closure): the data to pass to callback function
2300 * Asynchronously creates a new file and returns a stream
2301 * for reading and writing to it. The file must not already exist.
2303 * For more details, see g_file_create_readwrite() which is
2304 * the synchronous version of this call.
2306 * When the operation is finished, @callback will be called.
2307 * You can then call g_file_create_readwrite_finish() to get
2308 * the result of the operation.
2313 g_file_create_readwrite_async (GFile *file,
2314 GFileCreateFlags flags,
2316 GCancellable *cancellable,
2317 GAsyncReadyCallback callback,
2322 g_return_if_fail (G_IS_FILE (file));
2324 iface = G_FILE_GET_IFACE (file);
2325 (* iface->create_readwrite_async) (file,
2334 * g_file_create_readwrite_finish:
2335 * @file: input #GFile
2336 * @res: a #GAsyncResult
2337 * @error: a #GError, or %NULL
2339 * Finishes an asynchronous file create operation started with
2340 * g_file_create_readwrite_async().
2342 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2343 * Free the returned object with g_object_unref().
2348 g_file_create_readwrite_finish (GFile *file,
2354 g_return_val_if_fail (G_IS_FILE (file), NULL);
2355 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2357 if (g_async_result_legacy_propagate_error (res, error))
2360 iface = G_FILE_GET_IFACE (file);
2361 return (* iface->create_readwrite_finish) (file, res, error);
2365 * g_file_replace_readwrite_async:
2366 * @file: input #GFile
2367 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2368 * for the current #GFile, or NULL to ignore
2369 * @make_backup: %TRUE if a backup should be created
2370 * @flags: a set of #GFileCreateFlags
2371 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2373 * @cancellable: (allow-none): optional #GCancellable object,
2375 * @callback: (scope async): a #GAsyncReadyCallback to call
2376 * when the request is satisfied
2377 * @user_data: (closure): the data to pass to callback function
2379 * Asynchronously overwrites the file in read-write mode,
2380 * replacing the contents, possibly creating a backup copy
2381 * of the file first.
2383 * For more details, see g_file_replace_readwrite() which is
2384 * the synchronous version of this call.
2386 * When the operation is finished, @callback will be called.
2387 * You can then call g_file_replace_readwrite_finish() to get
2388 * the result of the operation.
2393 g_file_replace_readwrite_async (GFile *file,
2395 gboolean make_backup,
2396 GFileCreateFlags flags,
2398 GCancellable *cancellable,
2399 GAsyncReadyCallback callback,
2404 g_return_if_fail (G_IS_FILE (file));
2406 iface = G_FILE_GET_IFACE (file);
2407 (* iface->replace_readwrite_async) (file,
2418 * g_file_replace_readwrite_finish:
2419 * @file: input #GFile
2420 * @res: a #GAsyncResult
2421 * @error: a #GError, or %NULL
2423 * Finishes an asynchronous file replace operation started with
2424 * g_file_replace_readwrite_async().
2426 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2427 * Free the returned object with g_object_unref().
2432 g_file_replace_readwrite_finish (GFile *file,
2438 g_return_val_if_fail (G_IS_FILE (file), NULL);
2439 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2441 if (g_async_result_legacy_propagate_error (res, error))
2444 iface = G_FILE_GET_IFACE (file);
2445 return (* iface->replace_readwrite_finish) (file, res, error);
2449 copy_symlink (GFile *destination,
2450 GFileCopyFlags flags,
2451 GCancellable *cancellable,
2456 gboolean tried_delete;
2458 GFileType file_type;
2460 tried_delete = FALSE;
2464 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2466 /* Maybe it already existed, and we want to overwrite? */
2467 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2468 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2470 g_error_free (my_error);
2472 /* Don't overwrite if the destination is a directory */
2473 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2474 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2475 cancellable, &my_error);
2478 file_type = g_file_info_get_file_type (info);
2479 g_object_unref (info);
2481 if (file_type == G_FILE_TYPE_DIRECTORY)
2483 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2484 _("Can't copy over directory"));
2489 if (!g_file_delete (destination, cancellable, error))
2492 tried_delete = TRUE;
2496 g_propagate_error (error, my_error);
2503 static GInputStream *
2504 open_source_for_copy (GFile *source,
2506 GFileCopyFlags flags,
2507 GCancellable *cancellable,
2513 GFileType file_type;
2516 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2520 /* There was an error opening the source, try to set a good error for it: */
2521 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2523 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2524 * as that is less useful to the app. Better check for errors on the
2527 g_error_free (my_error);
2530 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2531 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2532 cancellable, &my_error);
2534 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2536 file_type = g_file_info_get_file_type (info);
2537 g_object_unref (info);
2539 if (flags & G_FILE_COPY_OVERWRITE)
2541 if (file_type == G_FILE_TYPE_DIRECTORY)
2543 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2544 _("Can't copy directory over directory"));
2547 /* continue to would_recurse error */
2551 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2552 _("Target file exists"));
2558 /* Error getting info from target, return that error
2559 * (except for NOT_FOUND, which is no error here)
2561 g_clear_object (&info);
2562 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2564 g_propagate_error (error, my_error);
2567 g_clear_error (&my_error);
2570 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2571 _("Can't recursively copy directory"));
2575 g_propagate_error (error, my_error);
2580 should_copy (GFileAttributeInfo *info,
2582 gboolean skip_perms)
2584 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2588 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2589 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2593 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2594 GFileAttributeInfoList *namespaces,
2596 gboolean skip_perms)
2603 s = g_string_new ("");
2607 for (i = 0; i < attributes->n_infos; i++)
2609 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2614 g_string_append_c (s, ',');
2616 g_string_append (s, attributes->infos[i].name);
2623 for (i = 0; i < namespaces->n_infos; i++)
2625 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2630 g_string_append_c (s, ',');
2632 g_string_append (s, namespaces->infos[i].name);
2633 g_string_append (s, "::*");
2638 return g_string_free (s, FALSE);
2642 * g_file_copy_attributes:
2643 * @source: a #GFile with attributes
2644 * @destination: a #GFile to copy attributes to
2645 * @flags: a set of #GFileCopyFlags
2646 * @cancellable: (allow-none): optional #GCancellable object,
2648 * @error: a #GError, %NULL to ignore
2650 * Copies the file attributes from @source to @destination.
2652 * Normally only a subset of the file attributes are copied,
2653 * those that are copies in a normal file copy operation
2654 * (which for instance does not include e.g. owner). However
2655 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2656 * all the metadata that is possible to copy is copied. This
2657 * is useful when implementing move by copy + delete source.
2659 * Returns: %TRUE if the attributes were copied successfully,
2663 g_file_copy_attributes (GFile *source,
2665 GFileCopyFlags flags,
2666 GCancellable *cancellable,
2669 GFileAttributeInfoList *attributes, *namespaces;
2670 char *attrs_to_read;
2674 gboolean source_nofollow_symlinks;
2675 gboolean skip_perms;
2677 as_move = flags & G_FILE_COPY_ALL_METADATA;
2678 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2679 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2681 /* Ignore errors here, if the target supports no attributes there is
2684 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2685 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2687 if (attributes == NULL && namespaces == NULL)
2690 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2692 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2693 * we just don't copy it.
2695 info = g_file_query_info (source, attrs_to_read,
2696 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2700 g_free (attrs_to_read);
2705 res = g_file_set_attributes_from_info (destination,
2707 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2710 g_object_unref (info);
2713 g_file_attribute_info_list_unref (attributes);
2714 g_file_attribute_info_list_unref (namespaces);
2720 copy_stream_with_progress (GInputStream *in,
2723 GCancellable *cancellable,
2724 GFileProgressCallback progress_callback,
2725 gpointer progress_callback_data,
2728 gssize n_read, n_written;
2729 goffset current_size;
2730 char buffer[1024*64], *p;
2736 /* avoid performance impact of querying total size when it's not needed */
2737 if (progress_callback)
2739 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2740 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2744 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2745 total_size = g_file_info_get_size (info);
2746 g_object_unref (info);
2749 if (total_size == -1)
2751 info = g_file_query_info (source,
2752 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2753 G_FILE_QUERY_INFO_NONE,
2757 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2758 total_size = g_file_info_get_size (info);
2759 g_object_unref (info);
2764 if (total_size == -1)
2771 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2781 current_size += n_read;
2786 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2787 if (n_written == -1)
2794 n_read -= n_written;
2800 if (progress_callback)
2801 progress_callback (current_size, total_size, progress_callback_data);
2804 /* Make sure we send full copied size */
2805 if (progress_callback)
2806 progress_callback (current_size, total_size, progress_callback_data);
2814 do_splice (int fd_in,
2819 long *bytes_transferd,
2825 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2833 else if (errsv == ENOSYS || errsv == EINVAL)
2834 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2835 _("Splice not supported"));
2837 g_set_error (error, G_IO_ERROR,
2838 g_io_error_from_errno (errsv),
2839 _("Error splicing file: %s"),
2840 g_strerror (errsv));
2845 *bytes_transferd = result;
2850 splice_stream_with_progress (GInputStream *in,
2852 GCancellable *cancellable,
2853 GFileProgressCallback progress_callback,
2854 gpointer progress_callback_data,
2857 int buffer[2] = { -1, -1 };
2864 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2865 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2867 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2871 /* avoid performance impact of querying total size when it's not needed */
2872 if (progress_callback)
2876 if (fstat (fd_in, &sbuf) == 0)
2877 total_size = sbuf.st_size;
2880 if (total_size == -1)
2883 offset_in = offset_out = 0;
2890 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2893 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2904 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2907 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2910 n_read -= n_written;
2913 if (progress_callback)
2914 progress_callback (offset_in, total_size, progress_callback_data);
2917 /* Make sure we send full copied size */
2918 if (progress_callback)
2919 progress_callback (offset_in, total_size, progress_callback_data);
2921 if (!g_close (buffer[0], error))
2924 if (!g_close (buffer[1], error))
2928 if (buffer[0] != -1)
2929 (void) g_close (buffer[0], NULL);
2930 if (buffer[1] != -1)
2931 (void) g_close (buffer[1], NULL);
2939 btrfs_reflink_with_progress (GInputStream *in,
2942 GCancellable *cancellable,
2943 GFileProgressCallback progress_callback,
2944 gpointer progress_callback_data,
2947 goffset source_size;
2951 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2952 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2954 if (progress_callback)
2955 source_size = g_file_info_get_size (info);
2957 /* Btrfs clone ioctl properties:
2958 * - Works at the inode level
2959 * - Doesn't work with directories
2960 * - Always follows symlinks (source and destination)
2962 * By the time we get here, *in and *out are both regular files */
2963 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
2968 g_set_error_literal (error, G_IO_ERROR,
2969 G_IO_ERROR_NOT_SUPPORTED,
2970 _("Copy (reflink/clone) between mounts is not supported"));
2971 else if (errno == EINVAL)
2972 g_set_error_literal (error, G_IO_ERROR,
2973 G_IO_ERROR_NOT_SUPPORTED,
2974 _("Copy (reflink/clone) is not supported or invalid"));
2976 /* Most probably something odd happened; retry with fallback */
2977 g_set_error_literal (error, G_IO_ERROR,
2978 G_IO_ERROR_NOT_SUPPORTED,
2979 _("Copy (reflink/clone) is not supported or didn't work"));
2980 /* We retry with fallback for all error cases because Btrfs is currently
2981 * unstable, and so we can't trust it to do clone properly.
2982 * In addition, any hard errors here would cause the same failure in the
2983 * fallback manual copy as well. */
2987 /* Make sure we send full copied size */
2988 if (progress_callback)
2989 progress_callback (source_size, source_size, progress_callback_data);
2996 file_copy_fallback (GFile *source,
2998 GFileCopyFlags flags,
2999 GCancellable *cancellable,
3000 GFileProgressCallback progress_callback,
3001 gpointer progress_callback_data,
3004 gboolean ret = FALSE;
3005 GInputStream *in = NULL;
3006 GOutputStream *out = NULL;
3007 GFileInfo *info = NULL;
3010 /* need to know the file type */
3011 info = g_file_query_info (source,
3012 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3013 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3019 /* Maybe copy the symlink? */
3020 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3021 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3023 target = g_file_info_get_symlink_target (info);
3026 if (!copy_symlink (destination, flags, cancellable, target, error))
3032 /* ... else fall back on a regular file copy */
3034 /* Handle "special" files (pipes, device nodes, ...)? */
3035 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3037 /* FIXME: could try to recreate device nodes and others? */
3038 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3039 _("Can't copy special file"));
3043 /* Everything else should just fall back on a regular copy. */
3045 in = open_source_for_copy (source, destination, flags, cancellable, error);
3049 if (flags & G_FILE_COPY_OVERWRITE)
3051 out = (GOutputStream *)g_file_replace (destination,
3053 flags & G_FILE_COPY_BACKUP,
3054 G_FILE_CREATE_REPLACE_DESTINATION,
3055 cancellable, error);
3059 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3066 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3068 GError *reflink_err = NULL;
3070 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3071 progress_callback, progress_callback_data,
3074 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3076 g_clear_error (&reflink_err);
3080 g_propagate_error (error, reflink_err);
3093 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3095 GError *splice_err = NULL;
3097 if (!splice_stream_with_progress (in, out, cancellable,
3098 progress_callback, progress_callback_data,
3101 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3103 g_clear_error (&splice_err);
3107 g_propagate_error (error, splice_err);
3120 /* A plain read/write loop */
3121 if (!copy_stream_with_progress (in, out, source, cancellable,
3122 progress_callback, progress_callback_data,
3130 /* Don't care about errors in source here */
3131 (void) g_input_stream_close (in, cancellable, NULL);
3132 g_object_unref (in);
3137 /* But write errors on close are bad! */
3138 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3140 g_object_unref (out);
3143 /* Ignore errors here. Failure to copy metadata is not a hard error */
3145 (void) g_file_copy_attributes (source, destination,
3146 flags, cancellable, NULL);
3149 g_clear_object (&info);
3156 * @source: input #GFile
3157 * @destination: destination #GFile
3158 * @flags: set of #GFileCopyFlags
3159 * @cancellable: (allow-none): optional #GCancellable object,
3161 * @progress_callback: (allow-none) (scope call): function to callback with
3162 * progress information, or %NULL if progress information is not needed
3163 * @progress_callback_data: (closure): user data to pass to @progress_callback
3164 * @error: #GError to set on error, or %NULL
3166 * Copies the file @source to the location specified by @destination.
3167 * Can not handle recursive copies of directories.
3169 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3170 * existing @destination file is overwritten.
3172 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3173 * will be copied as symlinks, otherwise the target of the
3174 * @source symlink will be copied.
3176 * If @cancellable is not %NULL, then the operation can be cancelled by
3177 * triggering the cancellable object from another thread. If the operation
3178 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3180 * If @progress_callback is not %NULL, then the operation can be monitored
3181 * by setting this to a #GFileProgressCallback function.
3182 * @progress_callback_data will be passed to this function. It is guaranteed
3183 * that this callback will be called after all data has been transferred with
3184 * the total number of bytes copied during the operation.
3186 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3187 * is returned, independent on the status of the @destination.
3189 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3190 * the error %G_IO_ERROR_EXISTS is returned.
3192 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3193 * error is returned. If trying to overwrite a directory with a directory the
3194 * %G_IO_ERROR_WOULD_MERGE error is returned.
3196 * If the source is a directory and the target does not exist, or
3197 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3198 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3200 * If you are interested in copying the #GFile object itself (not the on-disk
3201 * file), see g_file_dup().
3203 * Returns: %TRUE on success, %FALSE otherwise.
3206 g_file_copy (GFile *source,
3208 GFileCopyFlags flags,
3209 GCancellable *cancellable,
3210 GFileProgressCallback progress_callback,
3211 gpointer progress_callback_data,
3218 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3219 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3221 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3224 iface = G_FILE_GET_IFACE (destination);
3228 res = (* iface->copy) (source, destination,
3230 progress_callback, progress_callback_data,
3236 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3238 g_propagate_error (error, my_error);
3242 g_clear_error (&my_error);
3245 /* If the types are different, and the destination method failed
3246 * also try the source method
3248 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3250 iface = G_FILE_GET_IFACE (source);
3255 res = (* iface->copy) (source, destination,
3257 progress_callback, progress_callback_data,
3263 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3265 g_propagate_error (error, my_error);
3269 g_clear_error (&my_error);
3273 return file_copy_fallback (source, destination, flags, cancellable,
3274 progress_callback, progress_callback_data,
3279 * g_file_copy_async: (skip)
3280 * @source: input #GFile
3281 * @destination: destination #GFile
3282 * @flags: set of #GFileCopyFlags
3283 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3285 * @cancellable: (allow-none): optional #GCancellable object,
3287 * @progress_callback: (allow-none): function to callback with progress
3288 * information, or %NULL if progress information is not needed
3289 * @progress_callback_data: (closure): user data to pass to @progress_callback
3290 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3291 * @user_data: the data to pass to callback function
3293 * Copies the file @source to the location specified by @destination
3294 * asynchronously. For details of the behaviour, see g_file_copy().
3296 * If @progress_callback is not %NULL, then that function that will be called
3297 * just like in g_file_copy(), however the callback will run in the main loop,
3298 * not in the thread that is doing the I/O operation.
3300 * When the operation is finished, @callback will be called. You can then call
3301 * g_file_copy_finish() to get the result of the operation.
3304 g_file_copy_async (GFile *source,
3306 GFileCopyFlags flags,
3308 GCancellable *cancellable,
3309 GFileProgressCallback progress_callback,
3310 gpointer progress_callback_data,
3311 GAsyncReadyCallback callback,
3316 g_return_if_fail (G_IS_FILE (source));
3317 g_return_if_fail (G_IS_FILE (destination));
3319 iface = G_FILE_GET_IFACE (source);
3320 (* iface->copy_async) (source,
3326 progress_callback_data,
3332 * g_file_copy_finish:
3333 * @file: input #GFile
3334 * @res: a #GAsyncResult
3335 * @error: a #GError, or %NULL
3337 * Finishes copying the file started with g_file_copy_async().
3339 * Returns: a %TRUE on success, %FALSE on error.
3342 g_file_copy_finish (GFile *file,
3348 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3349 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3351 if (g_async_result_legacy_propagate_error (res, error))
3354 iface = G_FILE_GET_IFACE (file);
3355 return (* iface->copy_finish) (file, res, error);
3360 * @source: #GFile pointing to the source location
3361 * @destination: #GFile pointing to the destination location
3362 * @flags: set of #GFileCopyFlags
3363 * @cancellable: (allow-none): optional #GCancellable object,
3365 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3366 * function for updates
3367 * @progress_callback_data: (closure): gpointer to user data for
3368 * the callback function
3369 * @error: #GError for returning error conditions, or %NULL
3371 * Tries to move the file or directory @source to the location specified
3372 * by @destination. If native move operations are supported then this is
3373 * used, otherwise a copy + delete fallback is used. The native
3374 * implementation may support moving directories (for instance on moves
3375 * inside the same filesystem), but the fallback code does not.
3377 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3378 * existing @destination file is overwritten.
3380 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3381 * will be copied as symlinks, otherwise the target of the
3382 * @source symlink will be copied.
3384 * If @cancellable is not %NULL, then the operation can be cancelled by
3385 * triggering the cancellable object from another thread. If the operation
3386 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3388 * If @progress_callback is not %NULL, then the operation can be monitored
3389 * by setting this to a #GFileProgressCallback function.
3390 * @progress_callback_data will be passed to this function. It is
3391 * guaranteed that this callback will be called after all data has been
3392 * transferred with the total number of bytes copied during the operation.
3394 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3395 * error is returned, independent on the status of the @destination.
3397 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3398 * then the error %G_IO_ERROR_EXISTS is returned.
3400 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3401 * error is returned. If trying to overwrite a directory with a directory the
3402 * %G_IO_ERROR_WOULD_MERGE error is returned.
3404 * If the source is a directory and the target does not exist, or
3405 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3406 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3407 * move operation isn't available).
3409 * Returns: %TRUE on successful move, %FALSE otherwise.
3412 g_file_move (GFile *source,
3414 GFileCopyFlags flags,
3415 GCancellable *cancellable,
3416 GFileProgressCallback progress_callback,
3417 gpointer progress_callback_data,
3424 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3425 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3427 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3430 iface = G_FILE_GET_IFACE (destination);
3434 res = (* iface->move) (source, destination,
3436 progress_callback, progress_callback_data,
3442 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3444 g_propagate_error (error, my_error);
3449 /* If the types are different, and the destination method failed
3450 * also try the source method
3452 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3454 iface = G_FILE_GET_IFACE (source);
3459 res = (* iface->move) (source, destination,
3461 progress_callback, progress_callback_data,
3467 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3469 g_propagate_error (error, my_error);
3475 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3477 g_set_error_literal (error, G_IO_ERROR,
3478 G_IO_ERROR_NOT_SUPPORTED,
3479 _("Operation not supported"));
3483 flags |= G_FILE_COPY_ALL_METADATA;
3484 if (!g_file_copy (source, destination, flags, cancellable,
3485 progress_callback, progress_callback_data,
3489 return g_file_delete (source, cancellable, error);
3493 * g_file_make_directory:
3494 * @file: input #GFile
3495 * @cancellable: (allow-none): optional #GCancellable object,
3497 * @error: a #GError, or %NULL
3499 * Creates a directory. Note that this will only create a child directory
3500 * of the immediate parent directory of the path or URI given by the #GFile.
3501 * To recursively create directories, see g_file_make_directory_with_parents().
3502 * This function will fail if the parent directory does not exist, setting
3503 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3504 * creating directories, this function will fail, setting @error to
3505 * %G_IO_ERROR_NOT_SUPPORTED.
3507 * For a local #GFile the newly created directory will have the default
3508 * (current) ownership and permissions of the current process.
3510 * If @cancellable is not %NULL, then the operation can be cancelled by
3511 * triggering the cancellable object from another thread. If the operation
3512 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3514 * Returns: %TRUE on successful creation, %FALSE otherwise.
3517 g_file_make_directory (GFile *file,
3518 GCancellable *cancellable,
3523 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3525 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3528 iface = G_FILE_GET_IFACE (file);
3530 if (iface->make_directory == NULL)
3532 g_set_error_literal (error, G_IO_ERROR,
3533 G_IO_ERROR_NOT_SUPPORTED,
3534 _("Operation not supported"));
3538 return (* iface->make_directory) (file, cancellable, error);
3542 * g_file_make_directory_with_parents:
3543 * @file: input #GFile
3544 * @cancellable: (allow-none): optional #GCancellable object,
3546 * @error: a #GError, or %NULL
3548 * Creates a directory and any parent directories that may not
3549 * exist similar to 'mkdir -p'. If the file system does not support
3550 * creating directories, this function will fail, setting @error to
3551 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3552 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3553 * the similar g_mkdir_with_parents().
3555 * For a local #GFile the newly created directories will have the default
3556 * (current) ownership and permissions of the current process.
3558 * If @cancellable is not %NULL, then the operation can be cancelled by
3559 * triggering the cancellable object from another thread. If the operation
3560 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3562 * Returns: %TRUE if all directories have been successfully created, %FALSE
3568 g_file_make_directory_with_parents (GFile *file,
3569 GCancellable *cancellable,
3572 GFile *work_file = NULL;
3573 GList *list = NULL, *l;
3574 GError *my_error = NULL;
3576 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3578 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3581 g_file_make_directory (file, cancellable, &my_error);
3582 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3585 g_propagate_error (error, my_error);
3586 return my_error == NULL;
3589 work_file = g_object_ref (file);
3591 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3595 parent_file = g_file_get_parent (work_file);
3596 if (parent_file == NULL)
3599 g_clear_error (&my_error);
3600 g_file_make_directory (parent_file, cancellable, &my_error);
3602 g_object_unref (work_file);
3603 work_file = g_object_ref (parent_file);
3605 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3606 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3608 g_object_unref (parent_file);
3611 for (l = list; my_error == NULL && l; l = l->next)
3613 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3617 g_object_unref (work_file);
3620 while (list != NULL)
3622 g_object_unref ((GFile *) list->data);
3623 list = g_list_remove (list, list->data);
3626 if (my_error != NULL)
3628 g_propagate_error (error, my_error);
3632 return g_file_make_directory (file, cancellable, error);
3636 * g_file_make_symbolic_link:
3637 * @file: a #GFile with the name of the symlink to create
3638 * @symlink_value: a string with the path for the target of the new symlink
3639 * @cancellable: (allow-none): optional #GCancellable object,
3643 * Creates a symbolic link named @file which contains the string
3646 * If @cancellable is not %NULL, then the operation can be cancelled by
3647 * triggering the cancellable object from another thread. If the operation
3648 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3650 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3653 g_file_make_symbolic_link (GFile *file,
3654 const char *symlink_value,
3655 GCancellable *cancellable,
3660 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3661 g_return_val_if_fail (symlink_value != NULL, FALSE);
3663 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3666 if (*symlink_value == '\0')
3668 g_set_error_literal (error, G_IO_ERROR,
3669 G_IO_ERROR_INVALID_ARGUMENT,
3670 _("Invalid symlink value given"));
3674 iface = G_FILE_GET_IFACE (file);
3676 if (iface->make_symbolic_link == NULL)
3678 g_set_error_literal (error, G_IO_ERROR,
3679 G_IO_ERROR_NOT_SUPPORTED,
3680 _("Operation not supported"));
3684 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3689 * @file: input #GFile
3690 * @cancellable: (allow-none): optional #GCancellable object,
3692 * @error: a #GError, or %NULL
3694 * Deletes a file. If the @file is a directory, it will only be
3695 * deleted if it is empty. This has the same semantics as g_unlink().
3697 * If @cancellable is not %NULL, then the operation can be cancelled by
3698 * triggering the cancellable object from another thread. If the operation
3699 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3701 * Virtual: delete_file
3702 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3705 g_file_delete (GFile *file,
3706 GCancellable *cancellable,
3711 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3713 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3716 iface = G_FILE_GET_IFACE (file);
3718 if (iface->delete_file == NULL)
3720 g_set_error_literal (error, G_IO_ERROR,
3721 G_IO_ERROR_NOT_SUPPORTED,
3722 _("Operation not supported"));
3726 return (* iface->delete_file) (file, cancellable, error);
3730 * g_file_delete_async:
3731 * @file: input #GFile
3732 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3734 * @cancellable: (allow-none): optional #GCancellable object,
3736 * @callback: a #GAsyncReadyCallback to call
3737 * when the request is satisfied
3738 * @user_data: the data to pass to callback function
3740 * Asynchronously delete a file. If the @file is a directory, it will
3741 * only be deleted if it is empty. This has the same semantics as
3744 * Virtual: delete_file_async
3748 g_file_delete_async (GFile *file,
3750 GCancellable *cancellable,
3751 GAsyncReadyCallback callback,
3756 g_return_if_fail (G_IS_FILE (file));
3758 iface = G_FILE_GET_IFACE (file);
3759 (* iface->delete_file_async) (file,
3767 * g_file_delete_finish:
3768 * @file: input #GFile
3769 * @result: a #GAsyncResult
3770 * @error: a #GError, or %NULL
3772 * Finishes deleting a file started with g_file_delete_async().
3774 * Virtual: delete_file_finish
3775 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3779 g_file_delete_finish (GFile *file,
3780 GAsyncResult *result,
3785 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3786 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3788 if (g_async_result_legacy_propagate_error (result, error))
3791 iface = G_FILE_GET_IFACE (file);
3792 return (* iface->delete_file_finish) (file, result, error);
3797 * @file: #GFile to send to trash
3798 * @cancellable: (allow-none): optional #GCancellable object,
3800 * @error: a #GError, or %NULL
3802 * Sends @file to the "Trashcan", if possible. This is similar to
3803 * deleting it, but the user can recover it before emptying the trashcan.
3804 * Not all file systems support trashing, so this call can return the
3805 * %G_IO_ERROR_NOT_SUPPORTED error.
3807 * If @cancellable is not %NULL, then the operation can be cancelled by
3808 * triggering the cancellable object from another thread. If the operation
3809 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3812 * Returns: %TRUE on successful trash, %FALSE otherwise.
3815 g_file_trash (GFile *file,
3816 GCancellable *cancellable,
3821 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3823 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3826 iface = G_FILE_GET_IFACE (file);
3828 if (iface->trash == NULL)
3830 g_set_error_literal (error,
3831 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3832 _("Trash not supported"));
3836 return (* iface->trash) (file, cancellable, error);
3840 * g_file_trash_async:
3841 * @file: input #GFile
3842 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3844 * @cancellable: (allow-none): optional #GCancellable object,
3846 * @callback: a #GAsyncReadyCallback to call
3847 * when the request is satisfied
3848 * @user_data: the data to pass to callback function
3850 * Asynchronously sends @file to the Trash location, if possible.
3852 * Virtual: trash_async
3856 g_file_trash_async (GFile *file,
3858 GCancellable *cancellable,
3859 GAsyncReadyCallback callback,
3864 g_return_if_fail (G_IS_FILE (file));
3866 iface = G_FILE_GET_IFACE (file);
3867 (* iface->trash_async) (file,
3875 * g_file_trash_finish:
3876 * @file: input #GFile
3877 * @result: a #GAsyncResult
3878 * @error: a #GError, or %NULL
3880 * Finishes an asynchronous file trashing operation, started with
3881 * g_file_trash_async().
3883 * Virtual: trash_finish
3884 * Returns: %TRUE on successful trash, %FALSE otherwise.
3888 g_file_trash_finish (GFile *file,
3889 GAsyncResult *result,
3894 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3895 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3897 iface = G_FILE_GET_IFACE (file);
3898 return (* iface->trash_finish) (file, result, error);
3902 * g_file_set_display_name:
3903 * @file: input #GFile
3904 * @display_name: a string
3905 * @cancellable: (allow-none): optional #GCancellable object,
3907 * @error: a #GError, or %NULL
3909 * Renames @file to the specified display name.
3911 * The display name is converted from UTF-8 to the correct encoding
3912 * for the target filesystem if possible and the @file is renamed to this.
3914 * If you want to implement a rename operation in the user interface the
3915 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3916 * initial value in the rename widget, and then the result after editing
3917 * should be passed to g_file_set_display_name().
3919 * On success the resulting converted filename is returned.
3921 * If @cancellable is not %NULL, then the operation can be cancelled by
3922 * triggering the cancellable object from another thread. If the operation
3923 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3925 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3926 * or %NULL if there was an error.
3927 * Free the returned object with g_object_unref().
3930 g_file_set_display_name (GFile *file,
3931 const gchar *display_name,
3932 GCancellable *cancellable,
3937 g_return_val_if_fail (G_IS_FILE (file), NULL);
3938 g_return_val_if_fail (display_name != NULL, NULL);
3940 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3944 G_IO_ERROR_INVALID_ARGUMENT,
3945 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3949 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3952 iface = G_FILE_GET_IFACE (file);
3954 return (* iface->set_display_name) (file, display_name, cancellable, error);
3958 * g_file_set_display_name_async:
3959 * @file: input #GFile
3960 * @display_name: a string
3961 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3963 * @cancellable: (allow-none): optional #GCancellable object,
3965 * @callback: (scope async): a #GAsyncReadyCallback to call
3966 * when the request is satisfied
3967 * @user_data: (closure): the data to pass to callback function
3969 * Asynchronously sets the display name for a given #GFile.
3971 * For more details, see g_file_set_display_name() which is
3972 * the synchronous version of this call.
3974 * When the operation is finished, @callback will be called.
3975 * You can then call g_file_set_display_name_finish() to get
3976 * the result of the operation.
3979 g_file_set_display_name_async (GFile *file,
3980 const gchar *display_name,
3982 GCancellable *cancellable,
3983 GAsyncReadyCallback callback,
3988 g_return_if_fail (G_IS_FILE (file));
3989 g_return_if_fail (display_name != NULL);
3991 iface = G_FILE_GET_IFACE (file);
3992 (* iface->set_display_name_async) (file,
4001 * g_file_set_display_name_finish:
4002 * @file: input #GFile
4003 * @res: a #GAsyncResult
4004 * @error: a #GError, or %NULL
4006 * Finishes setting a display name started with
4007 * g_file_set_display_name_async().
4009 * Returns: (transfer full): a #GFile or %NULL on error.
4010 * Free the returned object with g_object_unref().
4013 g_file_set_display_name_finish (GFile *file,
4019 g_return_val_if_fail (G_IS_FILE (file), NULL);
4020 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4022 if (g_async_result_legacy_propagate_error (res, error))
4025 iface = G_FILE_GET_IFACE (file);
4026 return (* iface->set_display_name_finish) (file, res, error);
4030 * g_file_query_settable_attributes:
4031 * @file: input #GFile
4032 * @cancellable: (allow-none): optional #GCancellable object,
4034 * @error: a #GError, or %NULL
4036 * Obtain the list of settable attributes for the file.
4038 * Returns the type and full attribute name of all the attributes
4039 * that can be set on this file. This doesn't mean setting it will
4040 * always succeed though, you might get an access failure, or some
4041 * specific file may not support a specific attribute.
4043 * If @cancellable is not %NULL, then the operation can be cancelled by
4044 * triggering the cancellable object from another thread. If the operation
4045 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4047 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4048 * When you are done with it, release it with
4049 * g_file_attribute_info_list_unref()
4051 GFileAttributeInfoList *
4052 g_file_query_settable_attributes (GFile *file,
4053 GCancellable *cancellable,
4058 GFileAttributeInfoList *list;
4060 g_return_val_if_fail (G_IS_FILE (file), NULL);
4062 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4065 iface = G_FILE_GET_IFACE (file);
4067 if (iface->query_settable_attributes == NULL)
4068 return g_file_attribute_info_list_new ();
4071 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4075 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4077 list = g_file_attribute_info_list_new ();
4078 g_error_free (my_error);
4081 g_propagate_error (error, my_error);
4088 * g_file_query_writable_namespaces:
4089 * @file: input #GFile
4090 * @cancellable: (allow-none): optional #GCancellable object,
4092 * @error: a #GError, or %NULL
4094 * Obtain the list of attribute namespaces where new attributes
4095 * can be created by a user. An example of this is extended
4096 * attributes (in the "xattr" namespace).
4098 * If @cancellable is not %NULL, then the operation can be cancelled by
4099 * triggering the cancellable object from another thread. If the operation
4100 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4102 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4103 * When you are done with it, release it with
4104 * g_file_attribute_info_list_unref()
4106 GFileAttributeInfoList *
4107 g_file_query_writable_namespaces (GFile *file,
4108 GCancellable *cancellable,
4113 GFileAttributeInfoList *list;
4115 g_return_val_if_fail (G_IS_FILE (file), NULL);
4117 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4120 iface = G_FILE_GET_IFACE (file);
4122 if (iface->query_writable_namespaces == NULL)
4123 return g_file_attribute_info_list_new ();
4126 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4130 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4132 list = g_file_attribute_info_list_new ();
4133 g_error_free (my_error);
4136 g_propagate_error (error, my_error);
4143 * g_file_set_attribute:
4144 * @file: input #GFile
4145 * @attribute: a string containing the attribute's name
4146 * @type: The type of the attribute
4147 * @value_p: (allow-none): a pointer to the value (or the pointer
4148 * itself if the type is a pointer type)
4149 * @flags: a set of #GFileQueryInfoFlags
4150 * @cancellable: (allow-none): optional #GCancellable object,
4152 * @error: a #GError, or %NULL
4154 * Sets an attribute in the file with attribute name @attribute to @value.
4156 * Some attributes can be unset by setting @attribute to
4157 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4159 * If @cancellable is not %NULL, then the operation can be cancelled by
4160 * triggering the cancellable object from another thread. If the operation
4161 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4163 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4166 g_file_set_attribute (GFile *file,
4167 const gchar *attribute,
4168 GFileAttributeType type,
4170 GFileQueryInfoFlags flags,
4171 GCancellable *cancellable,
4176 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4177 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4179 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4182 iface = G_FILE_GET_IFACE (file);
4184 if (iface->set_attribute == NULL)
4186 g_set_error_literal (error, G_IO_ERROR,
4187 G_IO_ERROR_NOT_SUPPORTED,
4188 _("Operation not supported"));
4192 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4196 * g_file_set_attributes_from_info:
4197 * @file: input #GFile
4198 * @info: a #GFileInfo
4199 * @flags: #GFileQueryInfoFlags
4200 * @cancellable: (allow-none): optional #GCancellable object,
4202 * @error: a #GError, or %NULL
4204 * Tries to set all attributes in the #GFileInfo on the target
4205 * values, not stopping on the first error.
4207 * If there is any error during this operation then @error will
4208 * be set to the first error. Error on particular fields are flagged
4209 * by setting the "status" field in the attribute value to
4210 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4211 * also detect further errors.
4213 * If @cancellable is not %NULL, then the operation can be cancelled by
4214 * triggering the cancellable object from another thread. If the operation
4215 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4217 * Returns: %FALSE if there was any error, %TRUE otherwise.
4220 g_file_set_attributes_from_info (GFile *file,
4222 GFileQueryInfoFlags flags,
4223 GCancellable *cancellable,
4228 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4229 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4234 g_file_info_clear_status (info);
4236 iface = G_FILE_GET_IFACE (file);
4238 return (* iface->set_attributes_from_info) (file,
4246 g_file_real_set_attributes_from_info (GFile *file,
4248 GFileQueryInfoFlags flags,
4249 GCancellable *cancellable,
4255 GFileAttributeValue *value;
4259 attributes = g_file_info_list_attributes (info, NULL);
4261 for (i = 0; attributes[i] != NULL; i++)
4263 value = _g_file_info_get_attribute_value (info, attributes[i]);
4265 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4268 if (!g_file_set_attribute (file, attributes[i],
4269 value->type, _g_file_attribute_value_peek_as_pointer (value),
4270 flags, cancellable, error))
4272 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4274 /* Don't set error multiple times */
4278 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4281 g_strfreev (attributes);
4287 * g_file_set_attributes_async:
4288 * @file: input #GFile
4289 * @info: a #GFileInfo
4290 * @flags: a #GFileQueryInfoFlags
4291 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4293 * @cancellable: (allow-none): optional #GCancellable object,
4295 * @callback: (scope async): a #GAsyncReadyCallback
4296 * @user_data: (closure): a #gpointer
4298 * Asynchronously sets the attributes of @file with @info.
4300 * For more details, see g_file_set_attributes_from_info(),
4301 * which is the synchronous version of this call.
4303 * When the operation is finished, @callback will be called.
4304 * You can then call g_file_set_attributes_finish() to get
4305 * the result of the operation.
4308 g_file_set_attributes_async (GFile *file,
4310 GFileQueryInfoFlags flags,
4312 GCancellable *cancellable,
4313 GAsyncReadyCallback callback,
4318 g_return_if_fail (G_IS_FILE (file));
4319 g_return_if_fail (G_IS_FILE_INFO (info));
4321 iface = G_FILE_GET_IFACE (file);
4322 (* iface->set_attributes_async) (file,
4332 * g_file_set_attributes_finish:
4333 * @file: input #GFile
4334 * @result: a #GAsyncResult
4335 * @info: (out) (transfer full): a #GFileInfo
4336 * @error: a #GError, or %NULL
4338 * Finishes setting an attribute started in g_file_set_attributes_async().
4340 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4343 g_file_set_attributes_finish (GFile *file,
4344 GAsyncResult *result,
4350 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4351 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4353 /* No standard handling of errors here, as we must set info even
4356 iface = G_FILE_GET_IFACE (file);
4357 return (* iface->set_attributes_finish) (file, result, info, error);
4361 * g_file_set_attribute_string:
4362 * @file: input #GFile
4363 * @attribute: a string containing the attribute's name
4364 * @value: a string containing the attribute's value
4365 * @flags: #GFileQueryInfoFlags
4366 * @cancellable: (allow-none): optional #GCancellable object,
4368 * @error: a #GError, or %NULL
4370 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4371 * If @attribute is of a different type, this operation will fail.
4373 * If @cancellable is not %NULL, then the operation can be cancelled by
4374 * triggering the cancellable object from another thread. If the operation
4375 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4377 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4380 g_file_set_attribute_string (GFile *file,
4381 const char *attribute,
4383 GFileQueryInfoFlags flags,
4384 GCancellable *cancellable,
4387 return g_file_set_attribute (file, attribute,
4388 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4389 flags, cancellable, error);
4393 * g_file_set_attribute_byte_string:
4394 * @file: input #GFile
4395 * @attribute: a string containing the attribute's name
4396 * @value: a string containing the attribute's new value
4397 * @flags: a #GFileQueryInfoFlags
4398 * @cancellable: (allow-none): optional #GCancellable object,
4400 * @error: a #GError, or %NULL
4402 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4403 * If @attribute is of a different type, this operation will fail,
4406 * If @cancellable is not %NULL, then the operation can be cancelled by
4407 * triggering the cancellable object from another thread. If the operation
4408 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4410 * Returns: %TRUE if the @attribute was successfully set to @value
4411 * in the @file, %FALSE otherwise.
4414 g_file_set_attribute_byte_string (GFile *file,
4415 const gchar *attribute,
4417 GFileQueryInfoFlags flags,
4418 GCancellable *cancellable,
4421 return g_file_set_attribute (file, attribute,
4422 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4423 flags, cancellable, error);
4427 * g_file_set_attribute_uint32:
4428 * @file: input #GFile
4429 * @attribute: a string containing the attribute's name
4430 * @value: a #guint32 containing the attribute's new value
4431 * @flags: a #GFileQueryInfoFlags
4432 * @cancellable: (allow-none): optional #GCancellable object,
4434 * @error: a #GError, or %NULL
4436 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4437 * If @attribute is of a different type, this operation will fail.
4439 * If @cancellable is not %NULL, then the operation can be cancelled by
4440 * triggering the cancellable object from another thread. If the operation
4441 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4443 * Returns: %TRUE if the @attribute was successfully set to @value
4444 * in the @file, %FALSE otherwise.
4447 g_file_set_attribute_uint32 (GFile *file,
4448 const gchar *attribute,
4450 GFileQueryInfoFlags flags,
4451 GCancellable *cancellable,
4454 return g_file_set_attribute (file, attribute,
4455 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4456 flags, cancellable, error);
4460 * g_file_set_attribute_int32:
4461 * @file: input #GFile
4462 * @attribute: a string containing the attribute's name
4463 * @value: a #gint32 containing the attribute's new value
4464 * @flags: a #GFileQueryInfoFlags
4465 * @cancellable: (allow-none): optional #GCancellable object,
4467 * @error: a #GError, or %NULL
4469 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4470 * If @attribute is of a different type, this operation will fail.
4472 * If @cancellable is not %NULL, then the operation can be cancelled by
4473 * triggering the cancellable object from another thread. If the operation
4474 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4476 * Returns: %TRUE if the @attribute was successfully set to @value
4477 * in the @file, %FALSE otherwise.
4480 g_file_set_attribute_int32 (GFile *file,
4481 const gchar *attribute,
4483 GFileQueryInfoFlags flags,
4484 GCancellable *cancellable,
4487 return g_file_set_attribute (file, attribute,
4488 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4489 flags, cancellable, error);
4493 * g_file_set_attribute_uint64:
4494 * @file: input #GFile
4495 * @attribute: a string containing the attribute's name
4496 * @value: a #guint64 containing the attribute's new value
4497 * @flags: a #GFileQueryInfoFlags
4498 * @cancellable: (allow-none): optional #GCancellable object,
4500 * @error: a #GError, or %NULL
4502 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4503 * If @attribute is of a different type, this operation will fail.
4505 * If @cancellable is not %NULL, then the operation can be cancelled by
4506 * triggering the cancellable object from another thread. If the operation
4507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4509 * Returns: %TRUE if the @attribute was successfully set to @value
4510 * in the @file, %FALSE otherwise.
4513 g_file_set_attribute_uint64 (GFile *file,
4514 const gchar *attribute,
4516 GFileQueryInfoFlags flags,
4517 GCancellable *cancellable,
4520 return g_file_set_attribute (file, attribute,
4521 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4522 flags, cancellable, error);
4526 * g_file_set_attribute_int64:
4527 * @file: input #GFile
4528 * @attribute: a string containing the attribute's name
4529 * @value: a #guint64 containing the attribute's new value
4530 * @flags: a #GFileQueryInfoFlags
4531 * @cancellable: (allow-none): optional #GCancellable object,
4533 * @error: a #GError, or %NULL
4535 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4536 * If @attribute is of a different type, this operation will fail.
4538 * If @cancellable is not %NULL, then the operation can be cancelled by
4539 * triggering the cancellable object from another thread. If the operation
4540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4542 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4545 g_file_set_attribute_int64 (GFile *file,
4546 const gchar *attribute,
4548 GFileQueryInfoFlags flags,
4549 GCancellable *cancellable,
4552 return g_file_set_attribute (file, attribute,
4553 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4554 flags, cancellable, error);
4558 * g_file_mount_mountable:
4559 * @file: input #GFile
4560 * @flags: flags affecting the operation
4561 * @mount_operation: (allow-none): a #GMountOperation,
4562 * or %NULL to avoid user interaction
4563 * @cancellable: (allow-none): optional #GCancellable object,
4565 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4566 * when the request is satisfied, or %NULL
4567 * @user_data: (closure): the data to pass to callback function
4569 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4570 * Using @mount_operation, you can request callbacks when, for instance,
4571 * passwords are needed during authentication.
4573 * If @cancellable is not %NULL, then the operation can be cancelled by
4574 * triggering the cancellable object from another thread. If the operation
4575 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4577 * When the operation is finished, @callback will be called.
4578 * You can then call g_file_mount_mountable_finish() to get
4579 * the result of the operation.
4582 g_file_mount_mountable (GFile *file,
4583 GMountMountFlags flags,
4584 GMountOperation *mount_operation,
4585 GCancellable *cancellable,
4586 GAsyncReadyCallback callback,
4591 g_return_if_fail (G_IS_FILE (file));
4593 iface = G_FILE_GET_IFACE (file);
4595 if (iface->mount_mountable == NULL)
4597 g_task_report_new_error (file, callback, user_data,
4598 g_file_mount_mountable,
4599 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4600 _("Operation not supported"));
4604 (* iface->mount_mountable) (file,
4613 * g_file_mount_mountable_finish:
4614 * @file: input #GFile
4615 * @result: a #GAsyncResult
4616 * @error: a #GError, or %NULL
4618 * Finishes a mount operation. See g_file_mount_mountable() for details.
4620 * Finish an asynchronous mount operation that was started
4621 * with g_file_mount_mountable().
4623 * Returns: (transfer full): a #GFile or %NULL on error.
4624 * Free the returned object with g_object_unref().
4627 g_file_mount_mountable_finish (GFile *file,
4628 GAsyncResult *result,
4633 g_return_val_if_fail (G_IS_FILE (file), NULL);
4634 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4636 if (g_async_result_legacy_propagate_error (result, error))
4638 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4639 return g_task_propagate_pointer (G_TASK (result), error);
4641 iface = G_FILE_GET_IFACE (file);
4642 return (* iface->mount_mountable_finish) (file, result, error);
4646 * g_file_unmount_mountable:
4647 * @file: input #GFile
4648 * @flags: flags affecting the operation
4649 * @cancellable: (allow-none): optional #GCancellable object,
4651 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4652 * when the request is satisfied, or %NULL
4653 * @user_data: (closure): the data to pass to callback function
4655 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4657 * If @cancellable is not %NULL, then the operation can be cancelled by
4658 * triggering the cancellable object from another thread. If the operation
4659 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4661 * When the operation is finished, @callback will be called.
4662 * You can then call g_file_unmount_mountable_finish() to get
4663 * the result of the operation.
4665 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4668 g_file_unmount_mountable (GFile *file,
4669 GMountUnmountFlags flags,
4670 GCancellable *cancellable,
4671 GAsyncReadyCallback callback,
4676 g_return_if_fail (G_IS_FILE (file));
4678 iface = G_FILE_GET_IFACE (file);
4680 if (iface->unmount_mountable == NULL)
4682 g_task_report_new_error (file, callback, user_data,
4683 g_file_unmount_mountable_with_operation,
4684 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4685 _("Operation not supported"));
4689 (* iface->unmount_mountable) (file,
4697 * g_file_unmount_mountable_finish:
4698 * @file: input #GFile
4699 * @result: a #GAsyncResult
4700 * @error: a #GError, or %NULL
4702 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4704 * Finish an asynchronous unmount operation that was started
4705 * with g_file_unmount_mountable().
4707 * Returns: %TRUE if the operation finished successfully.
4710 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4714 g_file_unmount_mountable_finish (GFile *file,
4715 GAsyncResult *result,
4720 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4721 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4723 if (g_async_result_legacy_propagate_error (result, error))
4725 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4726 return g_task_propagate_boolean (G_TASK (result), error);
4728 iface = G_FILE_GET_IFACE (file);
4729 return (* iface->unmount_mountable_finish) (file, result, error);
4733 * g_file_unmount_mountable_with_operation:
4734 * @file: input #GFile
4735 * @flags: flags affecting the operation
4736 * @mount_operation: (allow-none): a #GMountOperation,
4737 * or %NULL to avoid user interaction
4738 * @cancellable: (allow-none): optional #GCancellable object,
4740 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4741 * when the request is satisfied, or %NULL
4742 * @user_data: (closure): the data to pass to callback function
4744 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4746 * If @cancellable is not %NULL, then the operation can be cancelled by
4747 * triggering the cancellable object from another thread. If the operation
4748 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4750 * When the operation is finished, @callback will be called.
4751 * You can then call g_file_unmount_mountable_finish() to get
4752 * the result of the operation.
4757 g_file_unmount_mountable_with_operation (GFile *file,
4758 GMountUnmountFlags flags,
4759 GMountOperation *mount_operation,
4760 GCancellable *cancellable,
4761 GAsyncReadyCallback callback,
4766 g_return_if_fail (G_IS_FILE (file));
4768 iface = G_FILE_GET_IFACE (file);
4770 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4772 g_task_report_new_error (file, callback, user_data,
4773 g_file_unmount_mountable_with_operation,
4774 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4775 _("Operation not supported"));
4779 if (iface->unmount_mountable_with_operation != NULL)
4780 (* iface->unmount_mountable_with_operation) (file,
4787 (* iface->unmount_mountable) (file,
4795 * g_file_unmount_mountable_with_operation_finish:
4796 * @file: input #GFile
4797 * @result: a #GAsyncResult
4798 * @error: a #GError, or %NULL
4800 * Finishes an unmount operation,
4801 * see g_file_unmount_mountable_with_operation() for details.
4803 * Finish an asynchronous unmount operation that was started
4804 * with g_file_unmount_mountable_with_operation().
4806 * Returns: %TRUE if the operation finished successfully.
4812 g_file_unmount_mountable_with_operation_finish (GFile *file,
4813 GAsyncResult *result,
4818 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4819 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4821 if (g_async_result_legacy_propagate_error (result, error))
4823 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4824 return g_task_propagate_boolean (G_TASK (result), error);
4826 iface = G_FILE_GET_IFACE (file);
4827 if (iface->unmount_mountable_with_operation_finish != NULL)
4828 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4830 return (* iface->unmount_mountable_finish) (file, result, error);
4834 * g_file_eject_mountable:
4835 * @file: input #GFile
4836 * @flags: flags affecting the operation
4837 * @cancellable: (allow-none): optional #GCancellable object,
4839 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4840 * when the request is satisfied, or %NULL
4841 * @user_data: (closure): the data to pass to callback function
4843 * Starts an asynchronous eject on a mountable.
4844 * When this operation has completed, @callback will be called with
4845 * @user_user data, and the operation can be finalized with
4846 * g_file_eject_mountable_finish().
4848 * If @cancellable is not %NULL, then the operation can be cancelled by
4849 * triggering the cancellable object from another thread. If the operation
4850 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4852 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4855 g_file_eject_mountable (GFile *file,
4856 GMountUnmountFlags flags,
4857 GCancellable *cancellable,
4858 GAsyncReadyCallback callback,
4863 g_return_if_fail (G_IS_FILE (file));
4865 iface = G_FILE_GET_IFACE (file);
4867 if (iface->eject_mountable == NULL)
4869 g_task_report_new_error (file, callback, user_data,
4870 g_file_eject_mountable_with_operation,
4871 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4872 _("Operation not supported"));
4876 (* iface->eject_mountable) (file,
4884 * g_file_eject_mountable_finish:
4885 * @file: input #GFile
4886 * @result: a #GAsyncResult
4887 * @error: a #GError, or %NULL
4889 * Finishes an asynchronous eject operation started by
4890 * g_file_eject_mountable().
4892 * Returns: %TRUE if the @file was ejected successfully.
4895 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4899 g_file_eject_mountable_finish (GFile *file,
4900 GAsyncResult *result,
4905 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4906 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4908 if (g_async_result_legacy_propagate_error (result, error))
4910 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4911 return g_task_propagate_boolean (G_TASK (result), error);
4913 iface = G_FILE_GET_IFACE (file);
4914 return (* iface->eject_mountable_finish) (file, result, error);
4918 * g_file_eject_mountable_with_operation:
4919 * @file: input #GFile
4920 * @flags: flags affecting the operation
4921 * @mount_operation: (allow-none): a #GMountOperation,
4922 * or %NULL to avoid user interaction
4923 * @cancellable: (allow-none): optional #GCancellable object,
4925 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4926 * when the request is satisfied, or %NULL
4927 * @user_data: (closure): the data to pass to callback function
4929 * Starts an asynchronous eject on a mountable.
4930 * When this operation has completed, @callback will be called with
4931 * @user_user data, and the operation can be finalized with
4932 * g_file_eject_mountable_with_operation_finish().
4934 * If @cancellable is not %NULL, then the operation can be cancelled by
4935 * triggering the cancellable object from another thread. If the operation
4936 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4941 g_file_eject_mountable_with_operation (GFile *file,
4942 GMountUnmountFlags flags,
4943 GMountOperation *mount_operation,
4944 GCancellable *cancellable,
4945 GAsyncReadyCallback callback,
4950 g_return_if_fail (G_IS_FILE (file));
4952 iface = G_FILE_GET_IFACE (file);
4954 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4956 g_task_report_new_error (file, callback, user_data,
4957 g_file_eject_mountable_with_operation,
4958 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4959 _("Operation not supported"));
4963 if (iface->eject_mountable_with_operation != NULL)
4964 (* iface->eject_mountable_with_operation) (file,
4971 (* iface->eject_mountable) (file,
4979 * g_file_eject_mountable_with_operation_finish:
4980 * @file: input #GFile
4981 * @result: a #GAsyncResult
4982 * @error: a #GError, or %NULL
4984 * Finishes an asynchronous eject operation started by
4985 * g_file_eject_mountable_with_operation().
4987 * Returns: %TRUE if the @file was ejected successfully.
4993 g_file_eject_mountable_with_operation_finish (GFile *file,
4994 GAsyncResult *result,
4999 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5000 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5002 if (g_async_result_legacy_propagate_error (result, error))
5004 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5005 return g_task_propagate_boolean (G_TASK (result), error);
5007 iface = G_FILE_GET_IFACE (file);
5008 if (iface->eject_mountable_with_operation_finish != NULL)
5009 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5011 return (* iface->eject_mountable_finish) (file, result, error);
5015 * g_file_monitor_directory:
5016 * @file: input #GFile
5017 * @flags: a set of #GFileMonitorFlags
5018 * @cancellable: (allow-none): optional #GCancellable object,
5020 * @error: a #GError, or %NULL
5022 * Obtains a directory monitor for the given file.
5023 * This may fail if directory monitoring is not supported.
5025 * If @cancellable is not %NULL, then the operation can be cancelled by
5026 * triggering the cancellable object from another thread. If the operation
5027 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5029 * It does not make sense for @flags to contain
5030 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5031 * directories. It is not possible to monitor all the files in a
5032 * directory for changes made via hard links; if you want to do this then
5033 * you must register individual watches with g_file_monitor().
5035 * Virtual: monitor_dir
5036 * Returns: (transfer full): a #GFileMonitor for the given @file,
5037 * or %NULL on error.
5038 * Free the returned object with g_object_unref().
5041 g_file_monitor_directory (GFile *file,
5042 GFileMonitorFlags flags,
5043 GCancellable *cancellable,
5048 g_return_val_if_fail (G_IS_FILE (file), NULL);
5049 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5051 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5054 iface = G_FILE_GET_IFACE (file);
5056 if (iface->monitor_dir == NULL)
5058 g_set_error_literal (error, G_IO_ERROR,
5059 G_IO_ERROR_NOT_SUPPORTED,
5060 _("Operation not supported"));
5064 return (* iface->monitor_dir) (file, flags, cancellable, error);
5068 * g_file_monitor_file:
5069 * @file: input #GFile
5070 * @flags: a set of #GFileMonitorFlags
5071 * @cancellable: (allow-none): optional #GCancellable object,
5073 * @error: a #GError, or %NULL
5075 * Obtains a file monitor for the given file. If no file notification
5076 * mechanism exists, then regular polling of the file is used.
5078 * If @cancellable is not %NULL, then the operation can be cancelled by
5079 * triggering the cancellable object from another thread. If the operation
5080 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5082 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5083 * will also attempt to report changes made to the file via another
5084 * filename (ie, a hard link). Without this flag, you can only rely on
5085 * changes made through the filename contained in @file to be
5086 * reported. Using this flag may result in an increase in resource
5087 * usage, and may not have any effect depending on the #GFileMonitor
5088 * backend and/or filesystem type.
5090 * Returns: (transfer full): a #GFileMonitor for the given @file,
5091 * or %NULL on error.
5092 * Free the returned object with g_object_unref().
5095 g_file_monitor_file (GFile *file,
5096 GFileMonitorFlags flags,
5097 GCancellable *cancellable,
5101 GFileMonitor *monitor;
5103 g_return_val_if_fail (G_IS_FILE (file), NULL);
5105 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5108 iface = G_FILE_GET_IFACE (file);
5112 if (iface->monitor_file)
5113 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5115 /* Fallback to polling */
5116 if (monitor == NULL)
5117 monitor = _g_poll_file_monitor_new (file);
5124 * @file: input #GFile
5125 * @flags: a set of #GFileMonitorFlags
5126 * @cancellable: (allow-none): optional #GCancellable object,
5128 * @error: a #GError, or %NULL
5130 * Obtains a file or directory monitor for the given file,
5131 * depending on the type of the file.
5133 * If @cancellable is not %NULL, then the operation can be cancelled by
5134 * triggering the cancellable object from another thread. If the operation
5135 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5137 * Returns: (transfer full): a #GFileMonitor for the given @file,
5138 * or %NULL on error.
5139 * Free the returned object with g_object_unref().
5144 g_file_monitor (GFile *file,
5145 GFileMonitorFlags flags,
5146 GCancellable *cancellable,
5149 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5150 return g_file_monitor_directory (file,
5151 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5152 cancellable, error);
5154 return g_file_monitor_file (file, flags, cancellable, error);
5157 /********************************************
5158 * Default implementation of async ops *
5159 ********************************************/
5163 GFileQueryInfoFlags flags;
5164 } QueryInfoAsyncData;
5167 query_info_data_free (QueryInfoAsyncData *data)
5169 g_free (data->attributes);
5174 query_info_async_thread (GTask *task,
5177 GCancellable *cancellable)
5179 QueryInfoAsyncData *data = task_data;
5181 GError *error = NULL;
5183 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5185 g_task_return_pointer (task, info, g_object_unref);
5187 g_task_return_error (task, error);
5191 g_file_real_query_info_async (GFile *file,
5192 const char *attributes,
5193 GFileQueryInfoFlags flags,
5195 GCancellable *cancellable,
5196 GAsyncReadyCallback callback,
5200 QueryInfoAsyncData *data;
5202 data = g_new0 (QueryInfoAsyncData, 1);
5203 data->attributes = g_strdup (attributes);
5204 data->flags = flags;
5206 task = g_task_new (file, cancellable, callback, user_data);
5207 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5208 g_task_set_priority (task, io_priority);
5209 g_task_run_in_thread (task, query_info_async_thread);
5210 g_object_unref (task);
5214 g_file_real_query_info_finish (GFile *file,
5218 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5220 return g_task_propagate_pointer (G_TASK (res), error);
5224 query_filesystem_info_async_thread (GTask *task,
5227 GCancellable *cancellable)
5229 const char *attributes = task_data;
5231 GError *error = NULL;
5233 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5235 g_task_return_pointer (task, info, g_object_unref);
5237 g_task_return_error (task, error);
5241 g_file_real_query_filesystem_info_async (GFile *file,
5242 const char *attributes,
5244 GCancellable *cancellable,
5245 GAsyncReadyCallback callback,
5250 task = g_task_new (file, cancellable, callback, user_data);
5251 g_task_set_task_data (task, g_strdup (attributes), g_free);
5252 g_task_set_priority (task, io_priority);
5253 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5254 g_object_unref (task);
5258 g_file_real_query_filesystem_info_finish (GFile *file,
5262 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5264 return g_task_propagate_pointer (G_TASK (res), error);
5268 enumerate_children_async_thread (GTask *task,
5271 GCancellable *cancellable)
5273 QueryInfoAsyncData *data = task_data;
5274 GFileEnumerator *enumerator;
5275 GError *error = NULL;
5277 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5279 g_task_return_error (task, error);
5281 g_task_return_pointer (task, enumerator, g_object_unref);
5285 g_file_real_enumerate_children_async (GFile *file,
5286 const char *attributes,
5287 GFileQueryInfoFlags flags,
5289 GCancellable *cancellable,
5290 GAsyncReadyCallback callback,
5294 QueryInfoAsyncData *data;
5296 data = g_new0 (QueryInfoAsyncData, 1);
5297 data->attributes = g_strdup (attributes);
5298 data->flags = flags;
5300 task = g_task_new (file, cancellable, callback, user_data);
5301 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5302 g_task_set_priority (task, io_priority);
5303 g_task_run_in_thread (task, enumerate_children_async_thread);
5304 g_object_unref (task);
5307 static GFileEnumerator *
5308 g_file_real_enumerate_children_finish (GFile *file,
5312 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5314 return g_task_propagate_pointer (G_TASK (res), error);
5318 open_read_async_thread (GTask *task,
5321 GCancellable *cancellable)
5323 GFileInputStream *stream;
5324 GError *error = NULL;
5326 stream = g_file_read (G_FILE (object), cancellable, &error);
5328 g_task_return_pointer (task, stream, g_object_unref);
5330 g_task_return_error (task, error);
5334 g_file_real_read_async (GFile *file,
5336 GCancellable *cancellable,
5337 GAsyncReadyCallback callback,
5342 task = g_task_new (file, cancellable, callback, user_data);
5343 g_task_set_priority (task, io_priority);
5344 g_task_run_in_thread (task, open_read_async_thread);
5345 g_object_unref (task);
5348 static GFileInputStream *
5349 g_file_real_read_finish (GFile *file,
5353 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5355 return g_task_propagate_pointer (G_TASK (res), error);
5359 append_to_async_thread (GTask *task,
5360 gpointer source_object,
5362 GCancellable *cancellable)
5364 GFileCreateFlags *data = task_data;
5365 GFileOutputStream *stream;
5366 GError *error = NULL;
5368 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5370 g_task_return_pointer (task, stream, g_object_unref);
5372 g_task_return_error (task, error);
5376 g_file_real_append_to_async (GFile *file,
5377 GFileCreateFlags flags,
5379 GCancellable *cancellable,
5380 GAsyncReadyCallback callback,
5383 GFileCreateFlags *data;
5386 data = g_new0 (GFileCreateFlags, 1);
5389 task = g_task_new (file, cancellable, callback, user_data);
5390 g_task_set_task_data (task, data, g_free);
5391 g_task_set_priority (task, io_priority);
5393 g_task_run_in_thread (task, append_to_async_thread);
5394 g_object_unref (task);
5397 static GFileOutputStream *
5398 g_file_real_append_to_finish (GFile *file,
5402 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5404 return g_task_propagate_pointer (G_TASK (res), error);
5408 create_async_thread (GTask *task,
5409 gpointer source_object,
5411 GCancellable *cancellable)
5413 GFileCreateFlags *data = task_data;
5414 GFileOutputStream *stream;
5415 GError *error = NULL;
5417 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5419 g_task_return_pointer (task, stream, g_object_unref);
5421 g_task_return_error (task, error);
5425 g_file_real_create_async (GFile *file,
5426 GFileCreateFlags flags,
5428 GCancellable *cancellable,
5429 GAsyncReadyCallback callback,
5432 GFileCreateFlags *data;
5435 data = g_new0 (GFileCreateFlags, 1);
5438 task = g_task_new (file, cancellable, callback, user_data);
5439 g_task_set_task_data (task, data, g_free);
5440 g_task_set_priority (task, io_priority);
5442 g_task_run_in_thread (task, create_async_thread);
5443 g_object_unref (task);
5446 static GFileOutputStream *
5447 g_file_real_create_finish (GFile *file,
5451 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5453 return g_task_propagate_pointer (G_TASK (res), error);
5457 GFileOutputStream *stream;
5459 gboolean make_backup;
5460 GFileCreateFlags flags;
5464 replace_async_data_free (ReplaceAsyncData *data)
5467 g_object_unref (data->stream);
5468 g_free (data->etag);
5473 replace_async_thread (GTask *task,
5474 gpointer source_object,
5476 GCancellable *cancellable)
5478 GFileOutputStream *stream;
5479 ReplaceAsyncData *data = task_data;
5480 GError *error = NULL;
5482 stream = g_file_replace (G_FILE (source_object),
5490 g_task_return_pointer (task, stream, g_object_unref);
5492 g_task_return_error (task, error);
5496 g_file_real_replace_async (GFile *file,
5498 gboolean make_backup,
5499 GFileCreateFlags flags,
5501 GCancellable *cancellable,
5502 GAsyncReadyCallback callback,
5506 ReplaceAsyncData *data;
5508 data = g_new0 (ReplaceAsyncData, 1);
5509 data->etag = g_strdup (etag);
5510 data->make_backup = make_backup;
5511 data->flags = flags;
5513 task = g_task_new (file, cancellable, callback, user_data);
5514 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5515 g_task_set_priority (task, io_priority);
5517 g_task_run_in_thread (task, replace_async_thread);
5518 g_object_unref (task);
5521 static GFileOutputStream *
5522 g_file_real_replace_finish (GFile *file,
5526 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5528 return g_task_propagate_pointer (G_TASK (res), error);
5532 delete_async_thread (GTask *task,
5535 GCancellable *cancellable)
5537 GError *error = NULL;
5539 if (g_file_delete (G_FILE (object), cancellable, &error))
5540 g_task_return_boolean (task, TRUE);
5542 g_task_return_error (task, error);
5546 g_file_real_delete_async (GFile *file,
5548 GCancellable *cancellable,
5549 GAsyncReadyCallback callback,
5554 task = g_task_new (file, cancellable, callback, user_data);
5555 g_task_set_priority (task, io_priority);
5556 g_task_run_in_thread (task, delete_async_thread);
5557 g_object_unref (task);
5561 g_file_real_delete_finish (GFile *file,
5565 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5567 return g_task_propagate_boolean (G_TASK (res), error);
5571 trash_async_thread (GTask *task,
5574 GCancellable *cancellable)
5576 GError *error = NULL;
5578 if (g_file_trash (G_FILE (object), cancellable, &error))
5579 g_task_return_boolean (task, TRUE);
5581 g_task_return_error (task, error);
5585 g_file_real_trash_async (GFile *file,
5587 GCancellable *cancellable,
5588 GAsyncReadyCallback callback,
5593 task = g_task_new (file, cancellable, callback, user_data);
5594 g_task_set_priority (task, io_priority);
5595 g_task_run_in_thread (task, trash_async_thread);
5596 g_object_unref (task);
5600 g_file_real_trash_finish (GFile *file,
5604 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5606 return g_task_propagate_boolean (G_TASK (res), error);
5610 open_readwrite_async_thread (GTask *task,
5613 GCancellable *cancellable)
5615 GFileIOStream *stream;
5616 GError *error = NULL;
5618 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5621 g_task_return_error (task, error);
5623 g_task_return_pointer (task, stream, g_object_unref);
5627 g_file_real_open_readwrite_async (GFile *file,
5629 GCancellable *cancellable,
5630 GAsyncReadyCallback callback,
5635 task = g_task_new (file, cancellable, callback, user_data);
5636 g_task_set_priority (task, io_priority);
5638 g_task_run_in_thread (task, open_readwrite_async_thread);
5639 g_object_unref (task);
5642 static GFileIOStream *
5643 g_file_real_open_readwrite_finish (GFile *file,
5647 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5649 return g_task_propagate_pointer (G_TASK (res), error);
5653 create_readwrite_async_thread (GTask *task,
5656 GCancellable *cancellable)
5658 GFileCreateFlags *data = task_data;
5659 GFileIOStream *stream;
5660 GError *error = NULL;
5662 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5665 g_task_return_error (task, error);
5667 g_task_return_pointer (task, stream, g_object_unref);
5671 g_file_real_create_readwrite_async (GFile *file,
5672 GFileCreateFlags flags,
5674 GCancellable *cancellable,
5675 GAsyncReadyCallback callback,
5678 GFileCreateFlags *data;
5681 data = g_new0 (GFileCreateFlags, 1);
5684 task = g_task_new (file, cancellable, callback, user_data);
5685 g_task_set_task_data (task, data, g_free);
5686 g_task_set_priority (task, io_priority);
5688 g_task_run_in_thread (task, create_readwrite_async_thread);
5689 g_object_unref (task);
5692 static GFileIOStream *
5693 g_file_real_create_readwrite_finish (GFile *file,
5697 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5699 return g_task_propagate_pointer (G_TASK (res), error);
5704 gboolean make_backup;
5705 GFileCreateFlags flags;
5706 } ReplaceRWAsyncData;
5709 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5711 g_free (data->etag);
5716 replace_readwrite_async_thread (GTask *task,
5719 GCancellable *cancellable)
5721 GFileIOStream *stream;
5722 GError *error = NULL;
5723 ReplaceRWAsyncData *data = task_data;
5725 stream = g_file_replace_readwrite (G_FILE (object),
5733 g_task_return_error (task, error);
5735 g_task_return_pointer (task, stream, g_object_unref);
5739 g_file_real_replace_readwrite_async (GFile *file,
5741 gboolean make_backup,
5742 GFileCreateFlags flags,
5744 GCancellable *cancellable,
5745 GAsyncReadyCallback callback,
5749 ReplaceRWAsyncData *data;
5751 data = g_new0 (ReplaceRWAsyncData, 1);
5752 data->etag = g_strdup (etag);
5753 data->make_backup = make_backup;
5754 data->flags = flags;
5756 task = g_task_new (file, cancellable, callback, user_data);
5757 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5758 g_task_set_priority (task, io_priority);
5760 g_task_run_in_thread (task, replace_readwrite_async_thread);
5761 g_object_unref (task);
5764 static GFileIOStream *
5765 g_file_real_replace_readwrite_finish (GFile *file,
5769 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5771 return g_task_propagate_pointer (G_TASK (res), error);
5775 set_display_name_async_thread (GTask *task,
5778 GCancellable *cancellable)
5780 GError *error = NULL;
5781 char *name = task_data;
5784 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5787 g_task_return_error (task, error);
5789 g_task_return_pointer (task, file, g_object_unref);
5793 g_file_real_set_display_name_async (GFile *file,
5794 const char *display_name,
5796 GCancellable *cancellable,
5797 GAsyncReadyCallback callback,
5802 task = g_task_new (file, cancellable, callback, user_data);
5803 g_task_set_task_data (task, g_strdup (display_name), g_free);
5804 g_task_set_priority (task, io_priority);
5806 g_task_run_in_thread (task, set_display_name_async_thread);
5807 g_object_unref (task);
5811 g_file_real_set_display_name_finish (GFile *file,
5815 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5817 return g_task_propagate_pointer (G_TASK (res), error);
5821 GFileQueryInfoFlags flags;
5828 set_info_data_free (SetInfoAsyncData *data)
5831 g_object_unref (data->info);
5833 g_error_free (data->error);
5838 set_info_async_thread (GTask *task,
5841 GCancellable *cancellable)
5843 SetInfoAsyncData *data = task_data;
5846 data->res = g_file_set_attributes_from_info (G_FILE (object),
5854 g_file_real_set_attributes_async (GFile *file,
5856 GFileQueryInfoFlags flags,
5858 GCancellable *cancellable,
5859 GAsyncReadyCallback callback,
5863 SetInfoAsyncData *data;
5865 data = g_new0 (SetInfoAsyncData, 1);
5866 data->info = g_file_info_dup (info);
5867 data->flags = flags;
5869 task = g_task_new (file, cancellable, callback, user_data);
5870 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5871 g_task_set_priority (task, io_priority);
5873 g_task_run_in_thread (task, set_info_async_thread);
5874 g_object_unref (task);
5878 g_file_real_set_attributes_finish (GFile *file,
5883 SetInfoAsyncData *data;
5885 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5887 data = g_task_get_task_data (G_TASK (res));
5890 *info = g_object_ref (data->info);
5892 if (error != NULL && data->error)
5893 *error = g_error_copy (data->error);
5899 find_enclosing_mount_async_thread (GTask *task,
5902 GCancellable *cancellable)
5904 GError *error = NULL;
5907 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5910 g_task_return_error (task, error);
5912 g_task_return_pointer (task, mount, g_object_unref);
5916 g_file_real_find_enclosing_mount_async (GFile *file,
5918 GCancellable *cancellable,
5919 GAsyncReadyCallback callback,
5924 task = g_task_new (file, cancellable, callback, user_data);
5925 g_task_set_priority (task, io_priority);
5927 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5928 g_object_unref (task);
5932 g_file_real_find_enclosing_mount_finish (GFile *file,
5936 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5938 return g_task_propagate_pointer (G_TASK (res), error);
5945 GFileCopyFlags flags;
5946 GFileProgressCallback progress_cb;
5947 gpointer progress_cb_data;
5951 copy_async_data_free (CopyAsyncData *data)
5953 g_object_unref (data->source);
5954 g_object_unref (data->destination);
5955 g_slice_free (CopyAsyncData, data);
5959 CopyAsyncData *data;
5960 goffset current_num_bytes;
5961 goffset total_num_bytes;
5965 copy_async_progress_in_main (gpointer user_data)
5967 ProgressData *progress = user_data;
5968 CopyAsyncData *data = progress->data;
5970 data->progress_cb (progress->current_num_bytes,
5971 progress->total_num_bytes,
5972 data->progress_cb_data);
5978 copy_async_progress_callback (goffset current_num_bytes,
5979 goffset total_num_bytes,
5982 GTask *task = user_data;
5983 CopyAsyncData *data = g_task_get_task_data (task);
5984 ProgressData *progress;
5986 progress = g_new (ProgressData, 1);
5987 progress->data = data;
5988 progress->current_num_bytes = current_num_bytes;
5989 progress->total_num_bytes = total_num_bytes;
5991 g_main_context_invoke_full (g_task_get_context (task),
5992 g_task_get_priority (task),
5993 copy_async_progress_in_main,
5999 copy_async_thread (GTask *task,
6002 GCancellable *cancellable)
6004 CopyAsyncData *data = task_data;
6006 GError *error = NULL;
6008 result = g_file_copy (data->source,
6012 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6016 g_task_return_boolean (task, TRUE);
6018 g_task_return_error (task, error);
6022 g_file_real_copy_async (GFile *source,
6024 GFileCopyFlags flags,
6026 GCancellable *cancellable,
6027 GFileProgressCallback progress_callback,
6028 gpointer progress_callback_data,
6029 GAsyncReadyCallback callback,
6033 CopyAsyncData *data;
6035 data = g_slice_new (CopyAsyncData);
6036 data->source = g_object_ref (source);
6037 data->destination = g_object_ref (destination);
6038 data->flags = flags;
6039 data->progress_cb = progress_callback;
6040 data->progress_cb_data = progress_callback_data;
6042 task = g_task_new (source, cancellable, callback, user_data);
6043 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6044 g_task_set_priority (task, io_priority);
6045 g_task_run_in_thread (task, copy_async_thread);
6046 g_object_unref (task);
6050 g_file_real_copy_finish (GFile *file,
6054 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6056 return g_task_propagate_boolean (G_TASK (res), error);
6060 /********************************************
6061 * Default VFS operations *
6062 ********************************************/
6065 * g_file_new_for_path:
6066 * @path: a string containing a relative or absolute path.
6067 * The string must be encoded in the glib filename encoding.
6069 * Constructs a #GFile for a given path. This operation never
6070 * fails, but the returned object might not support any I/O
6071 * operation if @path is malformed.
6073 * Returns: (transfer full): a new #GFile for the given @path.
6074 * Free the returned object with g_object_unref().
6077 g_file_new_for_path (const char *path)
6079 g_return_val_if_fail (path != NULL, NULL);
6081 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6085 * g_file_new_for_uri:
6086 * @uri: a UTF-8 string containing a URI
6088 * Constructs a #GFile for a given URI. This operation never
6089 * fails, but the returned object might not support any I/O
6090 * operation if @uri is malformed or if the uri type is
6093 * Returns: (transfer full): a new #GFile for the given @uri.
6094 * Free the returned object with g_object_unref().
6097 g_file_new_for_uri (const char *uri)
6099 g_return_val_if_fail (uri != NULL, NULL);
6101 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6106 * @tmpl: (type filename) (allow-none): Template for the file
6107 * name, as in g_file_open_tmp(), or %NULL for a default template
6108 * @iostream: (out): on return, a #GFileIOStream for the created file
6109 * @error: a #GError, or %NULL
6111 * Opens a file in the preferred directory for temporary files (as
6112 * returned by g_get_tmp_dir()) and returns a #GFile and
6113 * #GFileIOStream pointing to it.
6115 * @tmpl should be a string in the GLib file name encoding
6116 * containing a sequence of six 'X' characters, and containing no
6117 * directory components. If it is %NULL, a default template is used.
6119 * Unlike the other #GFile constructors, this will return %NULL if
6120 * a temporary file could not be created.
6122 * Returns: (transfer full): a new #GFile.
6123 * Free the returned object with g_object_unref().
6128 g_file_new_tmp (const char *tmpl,
6129 GFileIOStream **iostream,
6135 GFileOutputStream *output;
6137 g_return_val_if_fail (iostream != NULL, NULL);
6139 fd = g_file_open_tmp (tmpl, &path, error);
6143 file = g_file_new_for_path (path);
6145 output = _g_local_file_output_stream_new (fd);
6146 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6148 g_object_unref (output);
6155 * g_file_parse_name:
6156 * @parse_name: a file name or path to be parsed
6158 * Constructs a #GFile with the given @parse_name (i.e. something
6159 * given by g_file_get_parse_name()). This operation never fails,
6160 * but the returned object might not support any I/O operation if
6161 * the @parse_name cannot be parsed.
6163 * Returns: (transfer full): a new #GFile.
6166 g_file_parse_name (const char *parse_name)
6168 g_return_val_if_fail (parse_name != NULL, NULL);
6170 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6174 is_valid_scheme_character (char c)
6176 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6179 /* Following RFC 2396, valid schemes are built like:
6180 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6183 has_valid_scheme (const char *uri)
6189 if (!g_ascii_isalpha (*p))
6194 } while (is_valid_scheme_character (*p));
6200 new_for_cmdline_arg (const gchar *arg,
6206 if (g_path_is_absolute (arg))
6207 return g_file_new_for_path (arg);
6209 if (has_valid_scheme (arg))
6210 return g_file_new_for_uri (arg);
6216 current_dir = g_get_current_dir ();
6217 filename = g_build_filename (current_dir, arg, NULL);
6218 g_free (current_dir);
6221 filename = g_build_filename (cwd, arg, NULL);
6223 file = g_file_new_for_path (filename);
6230 * g_file_new_for_commandline_arg:
6231 * @arg: a command line string
6233 * Creates a #GFile with the given argument from the command line.
6234 * The value of @arg can be either a URI, an absolute path or a
6235 * relative path resolved relative to the current working directory.
6236 * This operation never fails, but the returned object might not
6237 * support any I/O operation if @arg points to a malformed path.
6239 * Returns: (transfer full): a new #GFile.
6240 * Free the returned object with g_object_unref().
6243 g_file_new_for_commandline_arg (const char *arg)
6245 g_return_val_if_fail (arg != NULL, NULL);
6247 return new_for_cmdline_arg (arg, NULL);
6251 * g_file_new_for_commandline_arg_and_cwd:
6252 * @arg: a command line string
6253 * @cwd: the current working directory of the commandline
6255 * Creates a #GFile with the given argument from the command line.
6257 * This function is similar to g_file_new_for_commandline_arg() except
6258 * that it allows for passing the current working directory as an
6259 * argument instead of using the current working directory of the
6262 * This is useful if the commandline argument was given in a context
6263 * other than the invocation of the current process.
6265 * See also g_application_command_line_create_file_for_arg().
6267 * Returns: (transfer full): a new #GFile
6272 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6275 g_return_val_if_fail (arg != NULL, NULL);
6276 g_return_val_if_fail (cwd != NULL, NULL);
6278 return new_for_cmdline_arg (arg, cwd);
6282 * g_file_mount_enclosing_volume:
6283 * @location: input #GFile
6284 * @flags: flags affecting the operation
6285 * @mount_operation: (allow-none): a #GMountOperation
6286 * or %NULL to avoid user interaction
6287 * @cancellable: (allow-none): optional #GCancellable object,
6289 * @callback: (allow-none): a #GAsyncReadyCallback to call
6290 * when the request is satisfied, or %NULL
6291 * @user_data: the data to pass to callback function
6293 * Starts a @mount_operation, mounting the volume that contains
6294 * the file @location.
6296 * When this operation has completed, @callback will be called with
6297 * @user_user data, and the operation can be finalized with
6298 * g_file_mount_enclosing_volume_finish().
6300 * If @cancellable is not %NULL, then the operation can be cancelled by
6301 * triggering the cancellable object from another thread. If the operation
6302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6305 g_file_mount_enclosing_volume (GFile *location,
6306 GMountMountFlags flags,
6307 GMountOperation *mount_operation,
6308 GCancellable *cancellable,
6309 GAsyncReadyCallback callback,
6314 g_return_if_fail (G_IS_FILE (location));
6316 iface = G_FILE_GET_IFACE (location);
6318 if (iface->mount_enclosing_volume == NULL)
6320 g_task_report_new_error (location, callback, user_data,
6321 g_file_mount_enclosing_volume,
6322 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6323 _("volume doesn't implement mount"));
6327 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6332 * g_file_mount_enclosing_volume_finish:
6333 * @location: input #GFile
6334 * @result: a #GAsyncResult
6335 * @error: a #GError, or %NULL
6337 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6339 * Returns: %TRUE if successful. If an error has occurred,
6340 * this function will return %FALSE and set @error
6341 * appropriately if present.
6344 g_file_mount_enclosing_volume_finish (GFile *location,
6345 GAsyncResult *result,
6350 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6351 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6353 if (g_async_result_legacy_propagate_error (result, error))
6355 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6356 return g_task_propagate_boolean (G_TASK (result), error);
6358 iface = G_FILE_GET_IFACE (location);
6360 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6363 /********************************************
6364 * Utility functions *
6365 ********************************************/
6368 * g_file_query_default_handler:
6369 * @file: a #GFile to open
6370 * @cancellable: optional #GCancellable object, %NULL to ignore
6371 * @error: a #GError, or %NULL
6373 * Returns the #GAppInfo that is registered as the default
6374 * application to handle the file specified by @file.
6376 * If @cancellable is not %NULL, then the operation can be cancelled by
6377 * triggering the cancellable object from another thread. If the operation
6378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6380 * Returns: (transfer full): a #GAppInfo if the handle was found,
6381 * %NULL if there were errors.
6382 * When you are done with it, release it with g_object_unref()
6385 g_file_query_default_handler (GFile *file,
6386 GCancellable *cancellable,
6390 const char *content_type;
6395 uri_scheme = g_file_get_uri_scheme (file);
6396 if (uri_scheme && uri_scheme[0] != '\0')
6398 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6399 g_free (uri_scheme);
6401 if (appinfo != NULL)
6405 info = g_file_query_info (file,
6406 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6415 content_type = g_file_info_get_content_type (info);
6418 /* Don't use is_native(), as we want to support fuse paths if available */
6419 path = g_file_get_path (file);
6420 appinfo = g_app_info_get_default_for_type (content_type,
6425 g_object_unref (info);
6427 if (appinfo != NULL)
6430 g_set_error_literal (error, G_IO_ERROR,
6431 G_IO_ERROR_NOT_SUPPORTED,
6432 _("No application is registered as handling this file"));
6436 #define GET_CONTENT_BLOCK_SIZE 8192
6439 * g_file_load_contents:
6440 * @file: input #GFile
6441 * @cancellable: optional #GCancellable object, %NULL to ignore
6442 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6443 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6444 * or %NULL if the length is not needed
6445 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6446 * or %NULL if the entity tag is not needed
6447 * @error: a #GError, or %NULL
6449 * Loads the content of the file into memory. The data is always
6450 * zero-terminated, but this is not included in the resultant @length.
6451 * The returned @content should be freed with g_free() when no longer
6454 * If @cancellable is not %NULL, then the operation can be cancelled by
6455 * triggering the cancellable object from another thread. If the operation
6456 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6458 * Returns: %TRUE if the @file's contents were successfully loaded.
6459 * %FALSE if there were errors.
6462 g_file_load_contents (GFile *file,
6463 GCancellable *cancellable,
6469 GFileInputStream *in;
6470 GByteArray *content;
6475 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6476 g_return_val_if_fail (contents != NULL, FALSE);
6478 in = g_file_read (file, cancellable, error);
6482 content = g_byte_array_new ();
6485 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6486 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6487 content->data + pos,
6488 GET_CONTENT_BLOCK_SIZE,
6489 cancellable, error)) > 0)
6492 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6499 info = g_file_input_stream_query_info (in,
6500 G_FILE_ATTRIBUTE_ETAG_VALUE,
6505 *etag_out = g_strdup (g_file_info_get_etag (info));
6506 g_object_unref (info);
6510 /* Ignore errors on close */
6511 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6512 g_object_unref (in);
6516 /* error is set already */
6517 g_byte_array_free (content, TRUE);
6524 /* Zero terminate (we got an extra byte allocated for this */
6525 content->data[pos] = 0;
6527 *contents = (char *)g_byte_array_free (content, FALSE);
6534 GFileReadMoreCallback read_more_callback;
6535 GByteArray *content;
6542 load_contents_data_free (LoadContentsData *data)
6545 g_byte_array_free (data->content, TRUE);
6546 g_free (data->etag);
6551 load_contents_close_callback (GObject *obj,
6552 GAsyncResult *close_res,
6555 GInputStream *stream = G_INPUT_STREAM (obj);
6556 LoadContentsData *data = user_data;
6558 /* Ignore errors here, we're only reading anyway */
6559 g_input_stream_close_finish (stream, close_res, NULL);
6560 g_object_unref (stream);
6562 g_task_return_boolean (data->task, TRUE);
6563 g_object_unref (data->task);
6567 load_contents_fstat_callback (GObject *obj,
6568 GAsyncResult *stat_res,
6571 GInputStream *stream = G_INPUT_STREAM (obj);
6572 LoadContentsData *data = user_data;
6575 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6579 data->etag = g_strdup (g_file_info_get_etag (info));
6580 g_object_unref (info);
6583 g_input_stream_close_async (stream, 0,
6584 g_task_get_cancellable (data->task),
6585 load_contents_close_callback, data);
6589 load_contents_read_callback (GObject *obj,
6590 GAsyncResult *read_res,
6593 GInputStream *stream = G_INPUT_STREAM (obj);
6594 LoadContentsData *data = user_data;
6595 GError *error = NULL;
6598 read_size = g_input_stream_read_finish (stream, read_res, &error);
6602 g_task_return_error (data->task, error);
6603 g_object_unref (data->task);
6605 /* Close the file ignoring any error */
6606 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6607 g_object_unref (stream);
6609 else if (read_size == 0)
6611 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6612 G_FILE_ATTRIBUTE_ETAG_VALUE,
6614 g_task_get_cancellable (data->task),
6615 load_contents_fstat_callback,
6618 else if (read_size > 0)
6620 data->pos += read_size;
6622 g_byte_array_set_size (data->content,
6623 data->pos + GET_CONTENT_BLOCK_SIZE);
6626 if (data->read_more_callback &&
6627 !data->read_more_callback ((char *)data->content->data, data->pos,
6628 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6629 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6630 G_FILE_ATTRIBUTE_ETAG_VALUE,
6632 g_task_get_cancellable (data->task),
6633 load_contents_fstat_callback,
6636 g_input_stream_read_async (stream,
6637 data->content->data + data->pos,
6638 GET_CONTENT_BLOCK_SIZE,
6640 g_task_get_cancellable (data->task),
6641 load_contents_read_callback,
6647 load_contents_open_callback (GObject *obj,
6648 GAsyncResult *open_res,
6651 GFile *file = G_FILE (obj);
6652 GFileInputStream *stream;
6653 LoadContentsData *data = user_data;
6654 GError *error = NULL;
6656 stream = g_file_read_finish (file, open_res, &error);
6660 g_byte_array_set_size (data->content,
6661 data->pos + GET_CONTENT_BLOCK_SIZE);
6662 g_input_stream_read_async (G_INPUT_STREAM (stream),
6663 data->content->data + data->pos,
6664 GET_CONTENT_BLOCK_SIZE,
6666 g_task_get_cancellable (data->task),
6667 load_contents_read_callback,
6672 g_task_return_error (data->task, error);
6673 g_object_unref (data->task);
6678 * g_file_load_partial_contents_async: (skip)
6679 * @file: input #GFile
6680 * @cancellable: optional #GCancellable object, %NULL to ignore
6681 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6682 * and to specify whether further data should be read
6683 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6684 * @user_data: the data to pass to the callback functions
6686 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6687 * be used to stop reading from the file when appropriate, else this
6688 * function will behave exactly as g_file_load_contents_async(). This
6689 * operation can be finished by g_file_load_partial_contents_finish().
6691 * Users of this function should be aware that @user_data is passed to
6692 * both the @read_more_callback and the @callback.
6694 * If @cancellable is not %NULL, then the operation can be cancelled by
6695 * triggering the cancellable object from another thread. If the operation
6696 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6699 g_file_load_partial_contents_async (GFile *file,
6700 GCancellable *cancellable,
6701 GFileReadMoreCallback read_more_callback,
6702 GAsyncReadyCallback callback,
6705 LoadContentsData *data;
6707 g_return_if_fail (G_IS_FILE (file));
6709 data = g_new0 (LoadContentsData, 1);
6710 data->read_more_callback = read_more_callback;
6711 data->content = g_byte_array_new ();
6713 data->task = g_task_new (file, cancellable, callback, user_data);
6714 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6716 g_file_read_async (file,
6718 g_task_get_cancellable (data->task),
6719 load_contents_open_callback,
6724 * g_file_load_partial_contents_finish:
6725 * @file: input #GFile
6726 * @res: a #GAsyncResult
6727 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6728 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6729 * or %NULL if the length is not needed
6730 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6731 * or %NULL if the entity tag is not needed
6732 * @error: a #GError, or %NULL
6734 * Finishes an asynchronous partial load operation that was started
6735 * with g_file_load_partial_contents_async(). The data is always
6736 * zero-terminated, but this is not included in the resultant @length.
6737 * The returned @content should be freed with g_free() when no longer
6740 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6741 * present, it will be set appropriately.
6744 g_file_load_partial_contents_finish (GFile *file,
6752 LoadContentsData *data;
6754 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6755 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6756 g_return_val_if_fail (contents != NULL, FALSE);
6758 task = G_TASK (res);
6760 if (!g_task_propagate_boolean (task, error))
6767 data = g_task_get_task_data (task);
6770 *length = data->pos;
6774 *etag_out = data->etag;
6778 /* Zero terminate */
6779 g_byte_array_set_size (data->content, data->pos + 1);
6780 data->content->data[data->pos] = 0;
6782 *contents = (char *)g_byte_array_free (data->content, FALSE);
6783 data->content = NULL;
6789 * g_file_load_contents_async:
6790 * @file: input #GFile
6791 * @cancellable: optional #GCancellable object, %NULL to ignore
6792 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6793 * @user_data: the data to pass to callback function
6795 * Starts an asynchronous load of the @file's contents.
6797 * For more details, see g_file_load_contents() which is
6798 * the synchronous version of this call.
6800 * When the load operation has completed, @callback will be called
6801 * with @user data. To finish the operation, call
6802 * g_file_load_contents_finish() with the #GAsyncResult returned by
6805 * If @cancellable is not %NULL, then the operation can be cancelled by
6806 * triggering the cancellable object from another thread. If the operation
6807 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6810 g_file_load_contents_async (GFile *file,
6811 GCancellable *cancellable,
6812 GAsyncReadyCallback callback,
6815 g_file_load_partial_contents_async (file,
6818 callback, user_data);
6822 * g_file_load_contents_finish:
6823 * @file: input #GFile
6824 * @res: a #GAsyncResult
6825 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6826 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6827 * or %NULL if the length is not needed
6828 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6829 * or %NULL if the entity tag is not needed
6830 * @error: a #GError, or %NULL
6832 * Finishes an asynchronous load of the @file's contents.
6833 * The contents are placed in @contents, and @length is set to the
6834 * size of the @contents string. The @content should be freed with
6835 * g_free() when no longer needed. If @etag_out is present, it will be
6836 * set to the new entity tag for the @file.
6838 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6839 * present, it will be set appropriately.
6842 g_file_load_contents_finish (GFile *file,
6849 return g_file_load_partial_contents_finish (file,
6858 * g_file_replace_contents:
6859 * @file: input #GFile
6860 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6861 * @length: the length of @contents in bytes
6862 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6863 * for the document, or %NULL
6864 * @make_backup: %TRUE if a backup should be created
6865 * @flags: a set of #GFileCreateFlags
6866 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6867 * for the document. This should be freed with g_free() when no longer
6869 * @cancellable: optional #GCancellable object, %NULL to ignore
6870 * @error: a #GError, or %NULL
6872 * Replaces the contents of @file with @contents of @length bytes.
6874 * If @etag is specified (not %NULL), any existing file must have that etag,
6875 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6877 * If @make_backup is %TRUE, this function will attempt to make a backup
6880 * If @cancellable is not %NULL, then the operation can be cancelled by
6881 * triggering the cancellable object from another thread. If the operation
6882 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6884 * The returned @new_etag can be used to verify that the file hasn't
6885 * changed the next time it is saved over.
6887 * Returns: %TRUE if successful. If an error has occurred, this function
6888 * will return %FALSE and set @error appropriately if present.
6891 g_file_replace_contents (GFile *file,
6892 const char *contents,
6895 gboolean make_backup,
6896 GFileCreateFlags flags,
6898 GCancellable *cancellable,
6901 GFileOutputStream *out;
6902 gsize pos, remainder;
6906 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6907 g_return_val_if_fail (contents != NULL, FALSE);
6909 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6915 while (remainder > 0 &&
6916 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6918 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6926 if (remainder > 0 && res < 0)
6928 /* Ignore errors on close */
6929 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6930 g_object_unref (out);
6932 /* error is set already */
6936 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6939 *new_etag = g_file_output_stream_get_etag (out);
6941 g_object_unref (out);
6948 const char *content;
6953 } ReplaceContentsData;
6956 replace_contents_data_free (ReplaceContentsData *data)
6958 g_free (data->etag);
6963 replace_contents_close_callback (GObject *obj,
6964 GAsyncResult *close_res,
6967 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6968 ReplaceContentsData *data = user_data;
6970 /* Ignore errors here, we're only reading anyway */
6971 g_output_stream_close_finish (stream, close_res, NULL);
6972 g_object_unref (stream);
6976 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6977 g_task_return_boolean (data->task, TRUE);
6979 g_object_unref (data->task);
6983 replace_contents_write_callback (GObject *obj,
6984 GAsyncResult *read_res,
6987 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6988 ReplaceContentsData *data = user_data;
6989 GError *error = NULL;
6992 write_size = g_output_stream_write_finish (stream, read_res, &error);
6994 if (write_size <= 0)
6996 /* Error or EOF, close the file */
6999 data->failed = TRUE;
7000 g_task_return_error (data->task, error);
7002 g_output_stream_close_async (stream, 0,
7003 g_task_get_cancellable (data->task),
7004 replace_contents_close_callback, data);
7006 else if (write_size > 0)
7008 data->pos += write_size;
7010 if (data->pos >= data->length)
7011 g_output_stream_close_async (stream, 0,
7012 g_task_get_cancellable (data->task),
7013 replace_contents_close_callback, data);
7015 g_output_stream_write_async (stream,
7016 data->content + data->pos,
7017 data->length - data->pos,
7019 g_task_get_cancellable (data->task),
7020 replace_contents_write_callback,
7026 replace_contents_open_callback (GObject *obj,
7027 GAsyncResult *open_res,
7030 GFile *file = G_FILE (obj);
7031 GFileOutputStream *stream;
7032 ReplaceContentsData *data = user_data;
7033 GError *error = NULL;
7035 stream = g_file_replace_finish (file, open_res, &error);
7039 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7040 data->content + data->pos,
7041 data->length - data->pos,
7043 g_task_get_cancellable (data->task),
7044 replace_contents_write_callback,
7049 g_task_return_error (data->task, error);
7050 g_object_unref (data->task);
7055 * g_file_replace_contents_async:
7056 * @file: input #GFile
7057 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7058 * @length: the length of @contents in bytes
7059 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7060 * @make_backup: %TRUE if a backup should be created
7061 * @flags: a set of #GFileCreateFlags
7062 * @cancellable: optional #GCancellable object, %NULL to ignore
7063 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7064 * @user_data: the data to pass to callback function
7066 * Starts an asynchronous replacement of @file with the given
7067 * @contents of @length bytes. @etag will replace the document's
7068 * current entity tag.
7070 * When this operation has completed, @callback will be called with
7071 * @user_user data, and the operation can be finalized with
7072 * g_file_replace_contents_finish().
7074 * If @cancellable is not %NULL, then the operation can be cancelled by
7075 * triggering the cancellable object from another thread. If the operation
7076 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7078 * If @make_backup is %TRUE, this function will attempt to
7079 * make a backup of @file.
7082 g_file_replace_contents_async (GFile *file,
7083 const char *contents,
7086 gboolean make_backup,
7087 GFileCreateFlags flags,
7088 GCancellable *cancellable,
7089 GAsyncReadyCallback callback,
7092 ReplaceContentsData *data;
7094 g_return_if_fail (G_IS_FILE (file));
7095 g_return_if_fail (contents != NULL);
7097 data = g_new0 (ReplaceContentsData, 1);
7099 data->content = contents;
7100 data->length = length;
7102 data->task = g_task_new (file, cancellable, callback, user_data);
7103 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7105 g_file_replace_async (file,
7110 g_task_get_cancellable (data->task),
7111 replace_contents_open_callback,
7116 * g_file_replace_contents_finish:
7117 * @file: input #GFile
7118 * @res: a #GAsyncResult
7119 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7120 * for the document. This should be freed with g_free() when it is no
7121 * longer needed, or %NULL
7122 * @error: a #GError, or %NULL
7124 * Finishes an asynchronous replace of the given @file. See
7125 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7126 * tag for the document, if present.
7128 * Returns: %TRUE on success, %FALSE on failure.
7131 g_file_replace_contents_finish (GFile *file,
7137 ReplaceContentsData *data;
7139 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7140 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7142 task = G_TASK (res);
7144 if (!g_task_propagate_boolean (task, error))
7147 data = g_task_get_task_data (task);
7151 *new_etag = data->etag;
7152 data->etag = NULL; /* Take ownership */
7159 * g_file_start_mountable:
7160 * @file: input #GFile
7161 * @flags: flags affecting the operation
7162 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7163 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7164 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7165 * @user_data: the data to pass to callback function
7167 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7168 * Using @start_operation, you can request callbacks when, for instance,
7169 * passwords are needed during authentication.
7171 * If @cancellable is not %NULL, then the operation can be cancelled by
7172 * triggering the cancellable object from another thread. If the operation
7173 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7175 * When the operation is finished, @callback will be called.
7176 * You can then call g_file_mount_mountable_finish() to get
7177 * the result of the operation.
7182 g_file_start_mountable (GFile *file,
7183 GDriveStartFlags flags,
7184 GMountOperation *start_operation,
7185 GCancellable *cancellable,
7186 GAsyncReadyCallback callback,
7191 g_return_if_fail (G_IS_FILE (file));
7193 iface = G_FILE_GET_IFACE (file);
7195 if (iface->start_mountable == NULL)
7197 g_task_report_new_error (file, callback, user_data,
7198 g_file_start_mountable,
7199 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7200 _("Operation not supported"));
7204 (* iface->start_mountable) (file,
7213 * g_file_start_mountable_finish:
7214 * @file: input #GFile
7215 * @result: a #GAsyncResult
7216 * @error: a #GError, or %NULL
7218 * Finishes a start operation. See g_file_start_mountable() for details.
7220 * Finish an asynchronous start operation that was started
7221 * with g_file_start_mountable().
7223 * Returns: %TRUE if the operation finished successfully. %FALSE
7229 g_file_start_mountable_finish (GFile *file,
7230 GAsyncResult *result,
7235 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7236 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7238 if (g_async_result_legacy_propagate_error (result, error))
7240 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7241 return g_task_propagate_boolean (G_TASK (result), error);
7243 iface = G_FILE_GET_IFACE (file);
7244 return (* iface->start_mountable_finish) (file, result, error);
7248 * g_file_stop_mountable:
7249 * @file: input #GFile
7250 * @flags: flags affecting the operation
7251 * @mount_operation: (allow-none): a #GMountOperation,
7252 * or %NULL to avoid user interaction.
7253 * @cancellable: (allow-none): optional #GCancellable object,
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 * Stops 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_stop_mountable_finish() to get
7267 * the result of the operation.
7272 g_file_stop_mountable (GFile *file,
7273 GMountUnmountFlags flags,
7274 GMountOperation *mount_operation,
7275 GCancellable *cancellable,
7276 GAsyncReadyCallback callback,
7281 g_return_if_fail (G_IS_FILE (file));
7283 iface = G_FILE_GET_IFACE (file);
7285 if (iface->stop_mountable == NULL)
7287 g_task_report_new_error (file, callback, user_data,
7288 g_file_stop_mountable,
7289 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7290 _("Operation not supported"));
7294 (* iface->stop_mountable) (file,
7303 * g_file_stop_mountable_finish:
7304 * @file: input #GFile
7305 * @result: a #GAsyncResult
7306 * @error: a #GError, or %NULL
7308 * Finishes an stop operation, see g_file_stop_mountable() for details.
7310 * Finish an asynchronous stop operation that was started
7311 * with g_file_stop_mountable().
7313 * Returns: %TRUE if the operation finished successfully.
7319 g_file_stop_mountable_finish (GFile *file,
7320 GAsyncResult *result,
7325 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7326 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7328 if (g_async_result_legacy_propagate_error (result, error))
7330 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7331 return g_task_propagate_boolean (G_TASK (result), error);
7333 iface = G_FILE_GET_IFACE (file);
7334 return (* iface->stop_mountable_finish) (file, result, error);
7338 * g_file_poll_mountable:
7339 * @file: input #GFile
7340 * @cancellable: optional #GCancellable object, %NULL to ignore
7341 * @callback: (allow-none): a #GAsyncReadyCallback to call
7342 * when the request is satisfied, or %NULL
7343 * @user_data: the data to pass to callback function
7345 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7347 * If @cancellable is not %NULL, then the operation can be cancelled by
7348 * triggering the cancellable object from another thread. If the operation
7349 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7351 * When the operation is finished, @callback will be called.
7352 * You can then call g_file_mount_mountable_finish() to get
7353 * the result of the operation.
7358 g_file_poll_mountable (GFile *file,
7359 GCancellable *cancellable,
7360 GAsyncReadyCallback callback,
7365 g_return_if_fail (G_IS_FILE (file));
7367 iface = G_FILE_GET_IFACE (file);
7369 if (iface->poll_mountable == NULL)
7371 g_task_report_new_error (file, callback, user_data,
7372 g_file_poll_mountable,
7373 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7374 _("Operation not supported"));
7378 (* iface->poll_mountable) (file,
7385 * g_file_poll_mountable_finish:
7386 * @file: input #GFile
7387 * @result: a #GAsyncResult
7388 * @error: a #GError, or %NULL
7390 * Finishes a poll operation. See g_file_poll_mountable() for details.
7392 * Finish an asynchronous poll operation that was polled
7393 * with g_file_poll_mountable().
7395 * Returns: %TRUE if the operation finished successfully. %FALSE
7401 g_file_poll_mountable_finish (GFile *file,
7402 GAsyncResult *result,
7407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7408 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7410 if (g_async_result_legacy_propagate_error (result, error))
7412 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7413 return g_task_propagate_boolean (G_TASK (result), error);
7415 iface = G_FILE_GET_IFACE (file);
7416 return (* iface->poll_mountable_finish) (file, result, error);
7420 * g_file_supports_thread_contexts:
7423 * Checks if @file supports <link
7424 * linkend="g-main-context-push-thread-default-context">thread-default
7425 * contexts</link>. If this returns %FALSE, you cannot perform
7426 * asynchronous operations on @file in a thread that has a
7427 * thread-default context.
7429 * Returns: Whether or not @file supports thread-default contexts.
7434 g_file_supports_thread_contexts (GFile *file)
7438 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7440 iface = G_FILE_GET_IFACE (file);
7441 return iface->supports_thread_contexts;