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>
33 #include <sys/types.h>
39 #include "gioscheduler.h"
40 #include "gsimpleasyncresult.h"
41 #include "gfileattribute-priv.h"
42 #include "gfiledescriptorbased.h"
43 #include "gpollfilemonitor.h"
45 #include "gfileinputstream.h"
46 #include "gfileoutputstream.h"
47 #include "glocalfileoutputstream.h"
48 #include "glocalfileiostream.h"
49 #include "gcancellable.h"
50 #include "gasyncresult.h"
57 * @short_description: File and Directory Handling
59 * @see_also: #GFileInfo, #GFileEnumerator
61 * #GFile is a high level abstraction for manipulating files on a
62 * virtual file system. #GFiles are lightweight, immutable objects
63 * that do no I/O upon creation. It is necessary to understand that
64 * #GFile objects do not represent files, merely an identifier for a
65 * file. All file content I/O is implemented as streaming operations
66 * (see #GInputStream and #GOutputStream).
68 * To construct a #GFile, you can use:
70 * <member>g_file_new_for_path() if you have a path.</member>
71 * <member>g_file_new_for_uri() if you have a URI.</member>
72 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
73 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
74 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
77 * One way to think of a #GFile is as an abstraction of a pathname. For
78 * normal files the system pathname is what is stored internally, but as
79 * #GFiles are extensible it could also be something else that corresponds
80 * to a pathname in a userspace implementation of a filesystem.
82 * #GFiles make up hierarchies of directories and files that correspond to
83 * the files on a filesystem. You can move through the file system with
84 * #GFile using g_file_get_parent() to get an identifier for the parent
85 * directory, g_file_get_child() to get a child within a directory,
86 * g_file_resolve_relative_path() to resolve a relative path between two
87 * #GFiles. There can be multiple hierarchies, so you may not end up at
88 * the same root if you repeatedly call g_file_get_parent() on two different
91 * All #GFiles have a basename (get with g_file_get_basename()). These names
92 * are byte strings that are used to identify the file on the filesystem
93 * (relative to its parent directory) and there is no guarantees that they
94 * have any particular charset encoding or even make any sense at all. If
95 * you want to use filenames in a user interface you should use the display
96 * name that you can get by requesting the
97 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
98 * This is guaranteed to be in UTF-8 and can be used in a user interface.
99 * But always store the real basename or the #GFile to use to actually
100 * access the file, because there is no way to go from a display name to
103 * Using #GFile as an identifier has the same weaknesses as using a path
104 * in that there may be multiple aliases for the same file. For instance,
105 * hard or soft links may cause two different #GFiles to refer to the same
106 * file. Other possible causes for aliases are: case insensitive filesystems,
107 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
108 * check if two #GFiles point to the same file you can query for the
109 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
110 * canonicalization of pathnames passed in, so that trivial differences in
111 * the path string used at creation (duplicated slashes, slash at end of
112 * path, "." or ".." path segments, etc) does not create different #GFiles.
114 * Many #GFile operations have both synchronous and asynchronous versions
115 * to suit your application. Asynchronous versions of synchronous functions
116 * simply have _async() appended to their function names. The asynchronous
117 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
118 * the operation, producing a GAsyncResult which is then passed to the
119 * function's matching _finish() operation.
121 * Some #GFile operations do not have synchronous analogs, as they may
122 * take a very long time to finish, and blocking may leave an application
123 * unusable. Notable cases include:
125 * <member>g_file_mount_mountable() to mount a mountable file.</member>
126 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
127 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
130 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
131 * One notable feature of #GFiles are entity tags, or "etags" for
132 * short. Entity tags are somewhat like a more abstract version of the
133 * traditional mtime, and can be used to quickly determine if the file has
134 * been modified from the version on the file system. See the HTTP 1.1
135 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
136 * for HTTP Etag headers, which are a very similar concept.
140 static void g_file_real_query_info_async (GFile *file,
141 const char *attributes,
142 GFileQueryInfoFlags flags,
144 GCancellable *cancellable,
145 GAsyncReadyCallback callback,
147 static GFileInfo * g_file_real_query_info_finish (GFile *file,
150 static void g_file_real_query_filesystem_info_async (GFile *file,
151 const char *attributes,
153 GCancellable *cancellable,
154 GAsyncReadyCallback callback,
156 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
159 static void g_file_real_enumerate_children_async (GFile *file,
160 const char *attributes,
161 GFileQueryInfoFlags flags,
163 GCancellable *cancellable,
164 GAsyncReadyCallback callback,
166 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
169 static void g_file_real_read_async (GFile *file,
171 GCancellable *cancellable,
172 GAsyncReadyCallback callback,
174 static GFileInputStream * g_file_real_read_finish (GFile *file,
177 static void g_file_real_append_to_async (GFile *file,
178 GFileCreateFlags flags,
180 GCancellable *cancellable,
181 GAsyncReadyCallback callback,
183 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
186 static void g_file_real_create_async (GFile *file,
187 GFileCreateFlags flags,
189 GCancellable *cancellable,
190 GAsyncReadyCallback callback,
192 static GFileOutputStream *g_file_real_create_finish (GFile *file,
195 static void g_file_real_replace_async (GFile *file,
197 gboolean make_backup,
198 GFileCreateFlags flags,
200 GCancellable *cancellable,
201 GAsyncReadyCallback callback,
203 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
206 static void g_file_real_delete_async (GFile *file,
208 GCancellable *cancellable,
209 GAsyncReadyCallback callback,
211 static gboolean g_file_real_delete_finish (GFile *file,
214 static void g_file_real_open_readwrite_async (GFile *file,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
219 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
222 static void g_file_real_create_readwrite_async (GFile *file,
223 GFileCreateFlags flags,
225 GCancellable *cancellable,
226 GAsyncReadyCallback callback,
228 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
231 static void g_file_real_replace_readwrite_async (GFile *file,
233 gboolean make_backup,
234 GFileCreateFlags flags,
236 GCancellable *cancellable,
237 GAsyncReadyCallback callback,
239 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
242 static gboolean g_file_real_set_attributes_from_info (GFile *file,
244 GFileQueryInfoFlags flags,
245 GCancellable *cancellable,
247 static void g_file_real_set_display_name_async (GFile *file,
248 const char *display_name,
250 GCancellable *cancellable,
251 GAsyncReadyCallback callback,
253 static GFile * g_file_real_set_display_name_finish (GFile *file,
256 static void g_file_real_set_attributes_async (GFile *file,
258 GFileQueryInfoFlags flags,
260 GCancellable *cancellable,
261 GAsyncReadyCallback callback,
263 static gboolean g_file_real_set_attributes_finish (GFile *file,
267 static void g_file_real_find_enclosing_mount_async (GFile *file,
269 GCancellable *cancellable,
270 GAsyncReadyCallback callback,
272 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
275 static void g_file_real_copy_async (GFile *source,
277 GFileCopyFlags flags,
279 GCancellable *cancellable,
280 GFileProgressCallback progress_callback,
281 gpointer progress_callback_data,
282 GAsyncReadyCallback callback,
284 static gboolean g_file_real_copy_finish (GFile *file,
288 typedef GFileIface GFileInterface;
289 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
292 g_file_default_init (GFileIface *iface)
294 iface->enumerate_children_async = g_file_real_enumerate_children_async;
295 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
296 iface->set_display_name_async = g_file_real_set_display_name_async;
297 iface->set_display_name_finish = g_file_real_set_display_name_finish;
298 iface->query_info_async = g_file_real_query_info_async;
299 iface->query_info_finish = g_file_real_query_info_finish;
300 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
301 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
302 iface->set_attributes_async = g_file_real_set_attributes_async;
303 iface->set_attributes_finish = g_file_real_set_attributes_finish;
304 iface->read_async = g_file_real_read_async;
305 iface->read_finish = g_file_real_read_finish;
306 iface->append_to_async = g_file_real_append_to_async;
307 iface->append_to_finish = g_file_real_append_to_finish;
308 iface->create_async = g_file_real_create_async;
309 iface->create_finish = g_file_real_create_finish;
310 iface->replace_async = g_file_real_replace_async;
311 iface->replace_finish = g_file_real_replace_finish;
312 iface->delete_file_async = g_file_real_delete_async;
313 iface->delete_file_finish = g_file_real_delete_finish;
314 iface->open_readwrite_async = g_file_real_open_readwrite_async;
315 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
316 iface->create_readwrite_async = g_file_real_create_readwrite_async;
317 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
318 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
319 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
320 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
321 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
322 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
323 iface->copy_async = g_file_real_copy_async;
324 iface->copy_finish = g_file_real_copy_finish;
330 * @file: input #GFile
332 * Checks to see if a file is native to the platform.
334 * A native file s one expressed in the platform-native filename format,
335 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
336 * as it might be on a locally mounted remote filesystem.
338 * On some systems non-native files may be available using the native
339 * filesystem via a userspace filesystem (FUSE), in these cases this call
340 * will return %FALSE, but g_file_get_path() will still return a native path.
342 * This call does no blocking I/O.
344 * Returns: %TRUE if @file is native
347 g_file_is_native (GFile *file)
351 g_return_val_if_fail (G_IS_FILE (file), FALSE);
353 iface = G_FILE_GET_IFACE (file);
355 return (* iface->is_native) (file);
360 * g_file_has_uri_scheme:
361 * @file: input #GFile
362 * @uri_scheme: a string containing a URI scheme
364 * Checks to see if a #GFile has a given URI scheme.
366 * This call does no blocking I/O.
368 * Returns: %TRUE if #GFile's backend supports the
369 * given URI scheme, %FALSE if URI scheme is %NULL,
370 * not supported, or #GFile is invalid.
373 g_file_has_uri_scheme (GFile *file,
374 const char *uri_scheme)
378 g_return_val_if_fail (G_IS_FILE (file), FALSE);
379 g_return_val_if_fail (uri_scheme != NULL, FALSE);
381 iface = G_FILE_GET_IFACE (file);
383 return (* iface->has_uri_scheme) (file, uri_scheme);
388 * g_file_get_uri_scheme:
389 * @file: input #GFile
391 * Gets the URI scheme for a #GFile.
392 * RFC 3986 decodes the scheme as:
394 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
396 * Common schemes include "file", "http", "ftp", etc.
398 * This call does no blocking I/O.
400 * Returns: a string containing the URI scheme for the given
401 * #GFile. The returned string should be freed with g_free()
402 * when no longer needed.
405 g_file_get_uri_scheme (GFile *file)
409 g_return_val_if_fail (G_IS_FILE (file), NULL);
411 iface = G_FILE_GET_IFACE (file);
413 return (* iface->get_uri_scheme) (file);
418 * g_file_get_basename:
419 * @file: input #GFile
421 * Gets the base name (the last component of the path) for a given #GFile.
423 * If called for the top level of a system (such as the filesystem root
424 * or a uri like sftp://host/) it will return a single directory separator
425 * (and on Windows, possibly a drive letter).
427 * The base name is a byte string (not UTF-8). It has no defined encoding
428 * or rules other than it may not contain zero bytes. If you want to use
429 * filenames in a user interface you should use the display name that you
430 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
431 * attribute with g_file_query_info().
433 * This call does no blocking I/O.
435 * Returns: string containing the #GFile's base name, or %NULL
436 * if given #GFile is invalid. The returned string should be
437 * freed with g_free() when no longer needed.
440 g_file_get_basename (GFile *file)
444 g_return_val_if_fail (G_IS_FILE (file), NULL);
446 iface = G_FILE_GET_IFACE (file);
448 return (* iface->get_basename) (file);
453 * @file: input #GFile
455 * Gets the local pathname for #GFile, if one exists.
457 * This call does no blocking I/O.
459 * Returns: string containing the #GFile's path, or %NULL if
460 * no such path exists. The returned string should be
461 * freed with g_free() when no longer needed.
464 g_file_get_path (GFile *file)
468 g_return_val_if_fail (G_IS_FILE (file), NULL);
470 iface = G_FILE_GET_IFACE (file);
472 return (* iface->get_path) (file);
477 * @file: input #GFile
479 * Gets the URI for the @file.
481 * This call does no blocking I/O.
483 * Returns: a string containing the #GFile's URI.
484 * The returned string should be freed with g_free()
485 * when no longer needed.
488 g_file_get_uri (GFile *file)
492 g_return_val_if_fail (G_IS_FILE (file), NULL);
494 iface = G_FILE_GET_IFACE (file);
496 return (* iface->get_uri) (file);
500 * g_file_get_parse_name:
501 * @file: input #GFile
503 * Gets the parse name of the @file.
504 * A parse name is a UTF-8 string that describes the
505 * file such that one can get the #GFile back using
506 * g_file_parse_name().
508 * This is generally used to show the #GFile as a nice
509 * full-pathname kind of string in a user interface,
510 * like in a location entry.
512 * For local files with names that can safely be converted
513 * to UTF-8 the pathname is used, otherwise the IRI is used
514 * (a form of URI that allows UTF-8 characters unescaped).
516 * This call does no blocking I/O.
518 * Returns: a string containing the #GFile's parse name.
519 * The returned string should be freed with g_free()
520 * when no longer needed.
523 g_file_get_parse_name (GFile *file)
527 g_return_val_if_fail (G_IS_FILE (file), NULL);
529 iface = G_FILE_GET_IFACE (file);
531 return (* iface->get_parse_name) (file);
536 * @file: input #GFile
538 * Duplicates a #GFile handle. This operation does not duplicate
539 * the actual file or directory represented by the #GFile; see
540 * g_file_copy() if attempting to copy a file.
542 * This call does no blocking I/O.
544 * Returns: (transfer full): a new #GFile that is a duplicate
545 * of the given #GFile.
548 g_file_dup (GFile *file)
552 g_return_val_if_fail (G_IS_FILE (file), NULL);
554 iface = G_FILE_GET_IFACE (file);
556 return (* iface->dup) (file);
561 * @file: (type GFile): #gconstpointer to a #GFile
563 * Creates a hash value for a #GFile.
565 * This call does no blocking I/O.
568 * Returns: 0 if @file is not a valid #GFile, otherwise an
569 * integer that can be used as hash value for the #GFile.
570 * This function is intended for easily hashing a #GFile to
571 * add to a #GHashTable or similar data structure.
574 g_file_hash (gconstpointer file)
578 g_return_val_if_fail (G_IS_FILE (file), 0);
580 iface = G_FILE_GET_IFACE (file);
582 return (* iface->hash) ((GFile *)file);
587 * @file1: the first #GFile
588 * @file2: the second #GFile
590 * Checks equality of two given #GFiles.
592 * Note that two #GFiles that differ can still refer to the same
593 * file on the filesystem due to various forms of filename
596 * This call does no blocking I/O.
598 * Returns: %TRUE if @file1 and @file2 are equal.
599 * %FALSE if either is not a #GFile.
602 g_file_equal (GFile *file1,
607 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
608 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
610 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
613 iface = G_FILE_GET_IFACE (file1);
615 return (* iface->equal) (file1, file2);
621 * @file: input #GFile
623 * Gets the parent directory for the @file.
624 * If the @file represents the root directory of the
625 * file system, then %NULL will be returned.
627 * This call does no blocking I/O.
629 * Returns: (transfer full): a #GFile structure to the
630 * parent of the given #GFile or %NULL if there is
631 * no parent. Free the returned object with g_object_unref().
634 g_file_get_parent (GFile *file)
638 g_return_val_if_fail (G_IS_FILE (file), NULL);
640 iface = G_FILE_GET_IFACE (file);
642 return (* iface->get_parent) (file);
647 * @file: input #GFile
648 * @parent: (allow-none): the parent to check for, or %NULL
650 * Checks if @file has a parent, and optionally, if it is @parent.
652 * If @parent is %NULL then this function returns %TRUE if @file has any
653 * parent at all. If @parent is non-%NULL then %TRUE is only returned
654 * if @file is a child of @parent.
656 * Returns: %TRUE if @file is a child of @parent (or any parent in the
657 * case that @parent is %NULL).
662 g_file_has_parent (GFile *file,
665 GFile *actual_parent;
668 g_return_val_if_fail (G_IS_FILE (file), FALSE);
669 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
671 actual_parent = g_file_get_parent (file);
673 if (actual_parent != NULL)
676 result = g_file_equal (parent, actual_parent);
680 g_object_unref (actual_parent);
690 * @file: input #GFile
691 * @name: string containing the child's basename
693 * Gets a child of @file with basename equal to @name.
695 * Note that the file with that specific name might not exist, but
696 * you can still have a #GFile that points to it. You can use this
697 * for instance to create that file.
699 * This call does no blocking I/O.
701 * Returns: (transfer full): a #GFile to a child specified by @name.
702 * Free the returned object with g_object_unref().
705 g_file_get_child (GFile *file,
708 g_return_val_if_fail (G_IS_FILE (file), NULL);
709 g_return_val_if_fail (name != NULL, NULL);
711 return g_file_resolve_relative_path (file, name);
715 * g_file_get_child_for_display_name:
716 * @file: input #GFile
717 * @display_name: string to a possible child
718 * @error: return location for an error
720 * Gets the child of @file for a given @display_name (i.e. a UTF-8
721 * version of the name). If this function fails, it returns %NULL
722 * and @error will be set. This is very useful when constructing a
723 * #GFile for a new file and the user entered the filename in the
724 * user interface, for instance when you select a directory and
725 * type a filename in the file selector.
727 * This call does no blocking I/O.
729 * Returns: (transfer full): a #GFile to the specified child, or
730 * %NULL if the display name couldn't be converted.
731 * Free the returned object with g_object_unref().
734 g_file_get_child_for_display_name (GFile *file,
735 const char *display_name,
740 g_return_val_if_fail (G_IS_FILE (file), NULL);
741 g_return_val_if_fail (display_name != NULL, NULL);
743 iface = G_FILE_GET_IFACE (file);
745 return (* iface->get_child_for_display_name) (file, display_name, error);
750 * @file: input #GFile
751 * @prefix: input #GFile
753 * Checks whether @file has the prefix specified by @prefix.
755 * In other words, if the names of initial elements of @file's
756 * pathname match @prefix. Only full pathname elements are matched,
757 * so a path like /foo is not considered a prefix of /foobar, only
760 * This call does no I/O, as it works purely on names. As such it can
761 * sometimes return %FALSE even if @file is inside a @prefix (from a
762 * filesystem point of view), because the prefix of @file is an alias
765 * Virtual: prefix_matches
766 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
770 g_file_has_prefix (GFile *file,
775 g_return_val_if_fail (G_IS_FILE (file), FALSE);
776 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
778 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
781 iface = G_FILE_GET_IFACE (file);
783 /* The vtable function differs in arg order since
784 * we're using the old contains_file call
786 return (* iface->prefix_matches) (prefix, file);
790 * g_file_get_relative_path:
791 * @parent: input #GFile
792 * @descendant: input #GFile
794 * Gets the path for @descendant relative to @parent.
796 * This call does no blocking I/O.
798 * Returns: string with the relative path from @descendant
799 * to @parent, or %NULL if @descendant doesn't have @parent
800 * as prefix. The returned string should be freed with g_free()
801 * when no longer needed.
804 g_file_get_relative_path (GFile *parent,
809 g_return_val_if_fail (G_IS_FILE (parent), NULL);
810 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
812 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
815 iface = G_FILE_GET_IFACE (parent);
817 return (* iface->get_relative_path) (parent, descendant);
821 * g_file_resolve_relative_path:
822 * @file: input #GFile
823 * @relative_path: a given relative path string
825 * Resolves a relative path for @file to an absolute path.
827 * This call does no blocking I/O.
829 * Returns: (transfer full): #GFile to the resolved path.
830 * %NULL if @relative_path is %NULL or if @file is invalid.
831 * Free the returned object with g_object_unref().
834 g_file_resolve_relative_path (GFile *file,
835 const char *relative_path)
839 g_return_val_if_fail (G_IS_FILE (file), NULL);
840 g_return_val_if_fail (relative_path != NULL, NULL);
842 iface = G_FILE_GET_IFACE (file);
844 return (* iface->resolve_relative_path) (file, relative_path);
848 * g_file_enumerate_children:
849 * @file: input #GFile
850 * @attributes: an attribute query string
851 * @flags: a set of #GFileQueryInfoFlags
852 * @cancellable: (allow-none): optional #GCancellable object,
854 * @error: #GError for error reporting
856 * Gets the requested information about the files in a directory.
857 * The result is a #GFileEnumerator object that will give out
858 * #GFileInfo objects for all the files in the directory.
860 * The @attributes value is a string that specifies the file
861 * attributes that should be gathered. It is not an error if
862 * it's not possible to read a particular requested attribute
863 * from a file - it just won't be set. @attributes should
864 * be a comma-separated list of attributes or attribute wildcards.
865 * The wildcard "*" means all attributes, and a wildcard like
866 * "standard::*" means all attributes in the standard namespace.
867 * An example attribute query be "standard::*,owner::user".
868 * The standard attributes are available as defines, like
869 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
871 * If @cancellable is not %NULL, then the operation can be cancelled
872 * by triggering the cancellable object from another thread. If the
873 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
876 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
877 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
878 * error will be returned. Other errors are possible too.
880 * Returns: (transfer full): A #GFileEnumerator if successful,
881 * %NULL on error. Free the returned object with g_object_unref().
884 g_file_enumerate_children (GFile *file,
885 const char *attributes,
886 GFileQueryInfoFlags flags,
887 GCancellable *cancellable,
892 g_return_val_if_fail (G_IS_FILE (file), NULL);
894 if (g_cancellable_set_error_if_cancelled (cancellable, error))
897 iface = G_FILE_GET_IFACE (file);
899 if (iface->enumerate_children == NULL)
901 g_set_error_literal (error, G_IO_ERROR,
902 G_IO_ERROR_NOT_SUPPORTED,
903 _("Operation not supported"));
907 return (* iface->enumerate_children) (file, attributes, flags,
912 * g_file_enumerate_children_async:
913 * @file: input #GFile
914 * @attributes: an attribute query string
915 * @flags: a set of #GFileQueryInfoFlags
916 * @io_priority: the <link linkend="io-priority">I/O priority</link>
918 * @cancellable: (allow-none): optional #GCancellable object,
920 * @callback: (scope async): a #GAsyncReadyCallback to call when the
921 * request is satisfied
922 * @user_data: (closure): the data to pass to callback function
924 * Asynchronously gets the requested information about the files
925 * in a directory. The result is a #GFileEnumerator object that will
926 * give out #GFileInfo objects for all the files in the directory.
928 * For more details, see g_file_enumerate_children() which is
929 * the synchronous version of this call.
931 * When the operation is finished, @callback will be called. You can
932 * then call g_file_enumerate_children_finish() to get the result of
936 g_file_enumerate_children_async (GFile *file,
937 const char *attributes,
938 GFileQueryInfoFlags flags,
940 GCancellable *cancellable,
941 GAsyncReadyCallback callback,
946 g_return_if_fail (G_IS_FILE (file));
948 iface = G_FILE_GET_IFACE (file);
949 (* iface->enumerate_children_async) (file,
959 * g_file_enumerate_children_finish:
960 * @file: input #GFile
961 * @res: a #GAsyncResult
964 * Finishes an async enumerate children operation.
965 * See g_file_enumerate_children_async().
967 * Returns: (transfer full): a #GFileEnumerator or %NULL
968 * if an error occurred.
969 * Free the returned object with g_object_unref().
972 g_file_enumerate_children_finish (GFile *file,
978 g_return_val_if_fail (G_IS_FILE (file), NULL);
979 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
981 if (g_async_result_legacy_propagate_error (res, error))
984 iface = G_FILE_GET_IFACE (file);
985 return (* iface->enumerate_children_finish) (file, res, error);
989 * g_file_query_exists:
990 * @file: input #GFile
991 * @cancellable: (allow-none): optional #GCancellable object,
994 * Utility function to check if a particular file exists. This is
995 * implemented using g_file_query_info() and as such does blocking I/O.
997 * Note that in many cases it is racy to first check for file existence
998 * and then execute something based on the outcome of that, because the
999 * file might have been created or removed in between the operations. The
1000 * general approach to handling that is to not check, but just do the
1001 * operation and handle the errors as they come.
1003 * As an example of race-free checking, take the case of reading a file,
1004 * and if it doesn't exist, creating it. There are two racy versions: read
1005 * it, and on error create it; and: check if it exists, if not create it.
1006 * These can both result in two processes creating the file (with perhaps
1007 * a partially written file as the result). The correct approach is to
1008 * always try to create the file with g_file_create() which will either
1009 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1011 * However, in many cases an existence check is useful in a user interface,
1012 * for instance to make a menu item sensitive/insensitive, so that you don't
1013 * have to fool users that something is possible and then just show an error
1014 * dialog. If you do this, you should make sure to also handle the errors
1015 * that can happen due to races when you execute the operation.
1017 * Returns: %TRUE if the file exists (and can be detected without error),
1018 * %FALSE otherwise (or if cancelled).
1021 g_file_query_exists (GFile *file,
1022 GCancellable *cancellable)
1026 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1028 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1029 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1032 g_object_unref (info);
1040 * g_file_query_file_type:
1041 * @file: input #GFile
1042 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1043 * @cancellable: (allow-none): optional #GCancellable object,
1046 * Utility function to inspect the #GFileType of a file. This is
1047 * implemented using g_file_query_info() and as such does blocking I/O.
1049 * The primary use case of this method is to check if a file is
1050 * a regular file, directory, or symlink.
1052 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1053 * if the file does not exist
1058 g_file_query_file_type (GFile *file,
1059 GFileQueryInfoFlags flags,
1060 GCancellable *cancellable)
1063 GFileType file_type;
1065 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1066 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1070 file_type = g_file_info_get_file_type (info);
1071 g_object_unref (info);
1074 file_type = G_FILE_TYPE_UNKNOWN;
1080 * g_file_query_info:
1081 * @file: input #GFile
1082 * @attributes: an attribute query string
1083 * @flags: a set of #GFileQueryInfoFlags
1084 * @cancellable: (allow-none): optional #GCancellable object,
1088 * Gets the requested information about specified @file.
1089 * The result is a #GFileInfo object that contains key-value
1090 * attributes (such as the type or size of the file).
1092 * The @attributes value is a string that specifies the file
1093 * attributes that should be gathered. It is not an error if
1094 * it's not possible to read a particular requested attribute
1095 * from a file - it just won't be set. @attributes should be a
1096 * comma-separated list of attributes or attribute wildcards.
1097 * The wildcard "*" means all attributes, and a wildcard like
1098 * "standard::*" means all attributes in the standard namespace.
1099 * An example attribute query be "standard::*,owner::user".
1100 * The standard attributes are available as defines, like
1101 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1103 * If @cancellable is not %NULL, then the operation can be cancelled
1104 * by triggering the cancellable object from another thread. If the
1105 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1108 * For symlinks, normally the information about the target of the
1109 * symlink is returned, rather than information about the symlink
1110 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1111 * in @flags the information about the symlink itself will be returned.
1112 * Also, for symlinks that point to non-existing files the information
1113 * about the symlink itself will be returned.
1115 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1116 * returned. Other errors are possible too, and depend on what kind of
1117 * filesystem the file is on.
1119 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1120 * on error. Free the returned object with g_object_unref().
1123 g_file_query_info (GFile *file,
1124 const char *attributes,
1125 GFileQueryInfoFlags flags,
1126 GCancellable *cancellable,
1131 g_return_val_if_fail (G_IS_FILE (file), NULL);
1133 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1136 iface = G_FILE_GET_IFACE (file);
1138 if (iface->query_info == NULL)
1140 g_set_error_literal (error, G_IO_ERROR,
1141 G_IO_ERROR_NOT_SUPPORTED,
1142 _("Operation not supported"));
1146 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1150 * g_file_query_info_async:
1151 * @file: input #GFile
1152 * @attributes: an attribute query string
1153 * @flags: a set of #GFileQueryInfoFlags
1154 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1156 * @cancellable: (allow-none): optional #GCancellable object,
1158 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1159 * request is satisfied
1160 * @user_data: (closure): the data to pass to callback function
1162 * Asynchronously gets the requested information about specified @file.
1163 * The result is a #GFileInfo object that contains key-value attributes
1164 * (such as type or size for the file).
1166 * For more details, see g_file_query_info() which is the synchronous
1167 * version of this call.
1169 * When the operation is finished, @callback will be called. You can
1170 * then call g_file_query_info_finish() to get the result of the operation.
1173 g_file_query_info_async (GFile *file,
1174 const char *attributes,
1175 GFileQueryInfoFlags flags,
1177 GCancellable *cancellable,
1178 GAsyncReadyCallback callback,
1183 g_return_if_fail (G_IS_FILE (file));
1185 iface = G_FILE_GET_IFACE (file);
1186 (* iface->query_info_async) (file,
1196 * g_file_query_info_finish:
1197 * @file: input #GFile
1198 * @res: a #GAsyncResult
1201 * Finishes an asynchronous file info query.
1202 * See g_file_query_info_async().
1204 * Returns: (transfer full): #GFileInfo for given @file
1205 * or %NULL on error. Free the returned object with
1209 g_file_query_info_finish (GFile *file,
1215 g_return_val_if_fail (G_IS_FILE (file), NULL);
1216 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1218 if (g_async_result_legacy_propagate_error (res, error))
1221 iface = G_FILE_GET_IFACE (file);
1222 return (* iface->query_info_finish) (file, res, error);
1226 * g_file_query_filesystem_info:
1227 * @file: input #GFile
1228 * @attributes: an attribute query string
1229 * @cancellable: (allow-none): optional #GCancellable object,
1233 * Similar to g_file_query_info(), but obtains information
1234 * about the filesystem the @file is on, rather than the file itself.
1235 * For instance the amount of space available and the type of
1238 * The @attributes value is a string that specifies the attributes
1239 * that should be gathered. It is not an error if it's not possible
1240 * to read a particular requested attribute from a file - it just
1241 * won't be set. @attributes should be a comma-separated list of
1242 * attributes or attribute wildcards. The wildcard "*" means all
1243 * attributes, and a wildcard like "filesystem::*" means all attributes
1244 * in the filesystem namespace. The standard namespace for filesystem
1245 * attributes is "filesystem". Common attributes of interest are
1246 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1247 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1248 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1250 * If @cancellable is not %NULL, then the operation can be cancelled
1251 * by triggering the cancellable object from another thread. If the
1252 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1255 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1256 * be returned. Other errors are possible too, and depend on what
1257 * kind of filesystem the file is on.
1259 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1260 * Free the returned object with g_object_unref().
1263 g_file_query_filesystem_info (GFile *file,
1264 const char *attributes,
1265 GCancellable *cancellable,
1270 g_return_val_if_fail (G_IS_FILE (file), NULL);
1272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1275 iface = G_FILE_GET_IFACE (file);
1277 if (iface->query_filesystem_info == NULL)
1279 g_set_error_literal (error, G_IO_ERROR,
1280 G_IO_ERROR_NOT_SUPPORTED,
1281 _("Operation not supported"));
1285 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1289 * g_file_query_filesystem_info_async:
1290 * @file: input #GFile
1291 * @attributes: an attribute query string
1292 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1294 * @cancellable: (allow-none): optional #GCancellable object,
1296 * @callback: (scope async): a #GAsyncReadyCallback to call
1297 * when the request is satisfied
1298 * @user_data: (closure): the data to pass to callback function
1300 * Asynchronously gets the requested information about the filesystem
1301 * that the specified @file is on. The result is a #GFileInfo object
1302 * that contains key-value attributes (such as type or size for the
1305 * For more details, see g_file_query_filesystem_info() which is the
1306 * synchronous version of this call.
1308 * When the operation is finished, @callback will be called. You can
1309 * then call g_file_query_info_finish() to get the result of the
1313 g_file_query_filesystem_info_async (GFile *file,
1314 const char *attributes,
1316 GCancellable *cancellable,
1317 GAsyncReadyCallback callback,
1322 g_return_if_fail (G_IS_FILE (file));
1324 iface = G_FILE_GET_IFACE (file);
1325 (* iface->query_filesystem_info_async) (file,
1334 * g_file_query_filesystem_info_finish:
1335 * @file: input #GFile
1336 * @res: a #GAsyncResult
1339 * Finishes an asynchronous filesystem info query.
1340 * See g_file_query_filesystem_info_async().
1342 * Returns: (transfer full): #GFileInfo for given @file
1343 * or %NULL on error.
1344 * Free the returned object with g_object_unref().
1347 g_file_query_filesystem_info_finish (GFile *file,
1353 g_return_val_if_fail (G_IS_FILE (file), NULL);
1354 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1356 if (g_async_result_legacy_propagate_error (res, error))
1359 iface = G_FILE_GET_IFACE (file);
1360 return (* iface->query_filesystem_info_finish) (file, res, error);
1364 * g_file_find_enclosing_mount:
1365 * @file: input #GFile
1366 * @cancellable: (allow-none): optional #GCancellable object,
1370 * Gets a #GMount for the #GFile.
1372 * If the #GFileIface for @file does not have a mount (e.g.
1373 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1374 * and %NULL will be returned.
1376 * If @cancellable is not %NULL, then the operation can be cancelled by
1377 * triggering the cancellable object from another thread. If the operation
1378 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1380 * Returns: (transfer full): a #GMount where the @file is located
1381 * or %NULL on error.
1382 * Free the returned object with g_object_unref().
1385 g_file_find_enclosing_mount (GFile *file,
1386 GCancellable *cancellable,
1391 g_return_val_if_fail (G_IS_FILE (file), NULL);
1393 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1396 iface = G_FILE_GET_IFACE (file);
1397 if (iface->find_enclosing_mount == NULL)
1400 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1401 /* Translators: This is an error message when
1402 * trying to find the enclosing (user visible)
1403 * mount of a file, but none exists.
1405 _("Containing mount does not exist"));
1409 return (* iface->find_enclosing_mount) (file, cancellable, error);
1413 * g_file_find_enclosing_mount_async:
1415 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1417 * @cancellable: (allow-none): optional #GCancellable object,
1419 * @callback: (scope async): a #GAsyncReadyCallback to call
1420 * when the request is satisfied
1421 * @user_data: (closure): the data to pass to callback function
1423 * Asynchronously gets the mount for the file.
1425 * For more details, see g_file_find_enclosing_mount() which is
1426 * the synchronous version of this call.
1428 * When the operation is finished, @callback will be called.
1429 * You can then call g_file_find_enclosing_mount_finish() to
1430 * get the result of the operation.
1433 g_file_find_enclosing_mount_async (GFile *file,
1435 GCancellable *cancellable,
1436 GAsyncReadyCallback callback,
1441 g_return_if_fail (G_IS_FILE (file));
1443 iface = G_FILE_GET_IFACE (file);
1444 (* iface->find_enclosing_mount_async) (file,
1452 * g_file_find_enclosing_mount_finish:
1454 * @res: a #GAsyncResult
1457 * Finishes an asynchronous find mount request.
1458 * See g_file_find_enclosing_mount_async().
1460 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1461 * Free the returned object with g_object_unref().
1464 g_file_find_enclosing_mount_finish (GFile *file,
1470 g_return_val_if_fail (G_IS_FILE (file), NULL);
1471 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1473 if (g_async_result_legacy_propagate_error (res, error))
1476 iface = G_FILE_GET_IFACE (file);
1477 return (* iface->find_enclosing_mount_finish) (file, res, error);
1483 * @file: #GFile to read
1484 * @cancellable: (allow-none): a #GCancellable
1485 * @error: a #GError, or %NULL
1487 * Opens a file for reading. The result is a #GFileInputStream that
1488 * can be used to read the contents of the file.
1490 * If @cancellable is not %NULL, then the operation can be cancelled by
1491 * triggering the cancellable object from another thread. If the operation
1492 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1494 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1495 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1496 * error will be returned. Other errors are possible too, and depend
1497 * on what kind of filesystem the file is on.
1500 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1501 * Free the returned object with g_object_unref().
1504 g_file_read (GFile *file,
1505 GCancellable *cancellable,
1510 g_return_val_if_fail (G_IS_FILE (file), NULL);
1512 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1515 iface = G_FILE_GET_IFACE (file);
1517 if (iface->read_fn == NULL)
1519 g_set_error_literal (error, G_IO_ERROR,
1520 G_IO_ERROR_NOT_SUPPORTED,
1521 _("Operation not supported"));
1525 return (* iface->read_fn) (file, cancellable, error);
1530 * @file: input #GFile
1531 * @flags: a set of #GFileCreateFlags
1532 * @cancellable: (allow-none): optional #GCancellable object,
1534 * @error: a #GError, or %NULL
1536 * Gets an output stream for appending data to the file.
1537 * If the file doesn't already exist it is created.
1539 * By default files created are generally readable by everyone,
1540 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1541 * will be made readable only to the current user, to the level that
1542 * is supported on the target filesystem.
1544 * If @cancellable is not %NULL, then the operation can be cancelled
1545 * by triggering the cancellable object from another thread. If the
1546 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1549 * Some file systems don't allow all file names, and may return an
1550 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1551 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1552 * possible too, and depend on what kind of filesystem the file is on.
1554 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1555 * Free the returned object with g_object_unref().
1558 g_file_append_to (GFile *file,
1559 GFileCreateFlags flags,
1560 GCancellable *cancellable,
1565 g_return_val_if_fail (G_IS_FILE (file), NULL);
1567 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1570 iface = G_FILE_GET_IFACE (file);
1572 if (iface->append_to == NULL)
1574 g_set_error_literal (error, G_IO_ERROR,
1575 G_IO_ERROR_NOT_SUPPORTED,
1576 _("Operation not supported"));
1580 return (* iface->append_to) (file, flags, cancellable, error);
1585 * @file: input #GFile
1586 * @flags: a set of #GFileCreateFlags
1587 * @cancellable: (allow-none): optional #GCancellable object,
1589 * @error: a #GError, or %NULL
1591 * Creates a new file and returns an output stream for writing to it.
1592 * The file must not already exist.
1594 * By default files created are generally readable by everyone,
1595 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1596 * will be made readable only to the current user, to the level
1597 * that is supported on the target filesystem.
1599 * If @cancellable is not %NULL, then the operation can be cancelled
1600 * by triggering the cancellable object from another thread. If the
1601 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1604 * If a file or directory with this name already exists the
1605 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1606 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1607 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1608 * be returned. Other errors are possible too, and depend on what kind
1609 * of filesystem the file is on.
1611 * Returns: (transfer full): a #GFileOutputStream for the newly created
1612 * file, or %NULL on error.
1613 * Free the returned object with g_object_unref().
1616 g_file_create (GFile *file,
1617 GFileCreateFlags flags,
1618 GCancellable *cancellable,
1623 g_return_val_if_fail (G_IS_FILE (file), NULL);
1625 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1628 iface = G_FILE_GET_IFACE (file);
1630 if (iface->create == NULL)
1632 g_set_error_literal (error, G_IO_ERROR,
1633 G_IO_ERROR_NOT_SUPPORTED,
1634 _("Operation not supported"));
1638 return (* iface->create) (file, flags, cancellable, error);
1643 * @file: input #GFile
1644 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1645 * for the current #GFile, or #NULL to ignore
1646 * @make_backup: %TRUE if a backup should be created
1647 * @flags: a set of #GFileCreateFlags
1648 * @cancellable: (allow-none): optional #GCancellable object,
1650 * @error: a #GError, or %NULL
1652 * Returns an output stream for overwriting the file, possibly
1653 * creating a backup copy of the file first. If the file doesn't exist,
1654 * it will be created.
1656 * This will try to replace the file in the safest way possible so
1657 * that any errors during the writing will not affect an already
1658 * existing copy of the file. For instance, for local files it
1659 * may write to a temporary file and then atomically rename over
1660 * the destination when the stream is closed.
1662 * By default files created are generally readable by everyone,
1663 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1664 * will be made readable only to the current user, to the level that
1665 * is supported on the target filesystem.
1667 * If @cancellable is not %NULL, then the operation can be cancelled
1668 * by triggering the cancellable object from another thread. If the
1669 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1672 * If you pass in a non-%NULL @etag value, then this value is
1673 * compared to the current entity tag of the file, and if they differ
1674 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1675 * that the file has been changed since you last read it. You can get
1676 * the new etag from g_file_output_stream_get_etag() after you've
1677 * finished writing and closed the #GFileOutputStream. When you load
1678 * a new file you can use g_file_input_stream_query_info() to get
1679 * the etag of the file.
1681 * If @make_backup is %TRUE, this function will attempt to make a
1682 * backup of the current file before overwriting it. If this fails
1683 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1684 * want to replace anyway, try again with @make_backup set to %FALSE.
1686 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1687 * be returned, and if the file is some other form of non-regular file
1688 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1689 * file systems don't allow all file names, and may return an
1690 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1691 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1692 * possible too, and depend on what kind of filesystem the file is on.
1694 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1695 * Free the returned object with g_object_unref().
1698 g_file_replace (GFile *file,
1700 gboolean make_backup,
1701 GFileCreateFlags flags,
1702 GCancellable *cancellable,
1707 g_return_val_if_fail (G_IS_FILE (file), NULL);
1709 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1712 iface = G_FILE_GET_IFACE (file);
1714 if (iface->replace == NULL)
1716 g_set_error_literal (error, G_IO_ERROR,
1717 G_IO_ERROR_NOT_SUPPORTED,
1718 _("Operation not supported"));
1722 /* Handle empty tag string as NULL in consistent way. */
1723 if (etag && *etag == 0)
1726 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1730 * g_file_open_readwrite:
1731 * @file: #GFile to open
1732 * @cancellable: (allow-none): a #GCancellable
1733 * @error: a #GError, or %NULL
1735 * Opens an existing file for reading and writing. The result is
1736 * a #GFileIOStream that can be used to read and write the contents
1739 * If @cancellable is not %NULL, then the operation can be cancelled
1740 * by triggering the cancellable object from another thread. If the
1741 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1744 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1745 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1746 * error will be returned. Other errors are possible too, and depend on
1747 * what kind of filesystem the file is on. Note that in many non-local
1748 * file cases read and write streams are not supported, so make sure you
1749 * really need to do read and write streaming, rather than just opening
1750 * for reading or writing.
1752 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1753 * Free the returned object with g_object_unref().
1758 g_file_open_readwrite (GFile *file,
1759 GCancellable *cancellable,
1764 g_return_val_if_fail (G_IS_FILE (file), NULL);
1766 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1769 iface = G_FILE_GET_IFACE (file);
1771 if (iface->open_readwrite == NULL)
1773 g_set_error_literal (error, G_IO_ERROR,
1774 G_IO_ERROR_NOT_SUPPORTED,
1775 _("Operation not supported"));
1779 return (* iface->open_readwrite) (file, cancellable, error);
1783 * g_file_create_readwrite:
1785 * @flags: a set of #GFileCreateFlags
1786 * @cancellable: (allow-none): optional #GCancellable object,
1788 * @error: return location for a #GError, or %NULL
1790 * Creates a new file and returns a stream for reading and
1791 * writing to it. The file must not already exist.
1793 * By default files created are generally readable by everyone,
1794 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1795 * will be made readable only to the current user, to the level
1796 * that is supported on the target filesystem.
1798 * If @cancellable is not %NULL, then the operation can be cancelled
1799 * by triggering the cancellable object from another thread. If the
1800 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1803 * If a file or directory with this name already exists, the
1804 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1805 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1806 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1807 * will be returned. Other errors are possible too, and depend on what
1808 * kind of filesystem the file is on.
1810 * Note that in many non-local file cases read and write streams are
1811 * not supported, so make sure you really need to do read and write
1812 * streaming, rather than just opening for reading or writing.
1814 * Returns: (transfer full): a #GFileIOStream for the newly created
1815 * file, or %NULL on error.
1816 * Free the returned object with g_object_unref().
1821 g_file_create_readwrite (GFile *file,
1822 GFileCreateFlags flags,
1823 GCancellable *cancellable,
1828 g_return_val_if_fail (G_IS_FILE (file), NULL);
1830 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1833 iface = G_FILE_GET_IFACE (file);
1835 if (iface->create_readwrite == NULL)
1837 g_set_error_literal (error, G_IO_ERROR,
1838 G_IO_ERROR_NOT_SUPPORTED,
1839 _("Operation not supported"));
1843 return (* iface->create_readwrite) (file, flags, cancellable, error);
1847 * g_file_replace_readwrite:
1849 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1850 * for the current #GFile, or #NULL to ignore
1851 * @make_backup: %TRUE if a backup should be created
1852 * @flags: a set of #GFileCreateFlags
1853 * @cancellable: (allow-none): optional #GCancellable object,
1855 * @error: return location for a #GError, or %NULL
1857 * Returns an output stream for overwriting the file in readwrite mode,
1858 * possibly creating a backup copy of the file first. If the file doesn't
1859 * exist, it will be created.
1861 * For details about the behaviour, see g_file_replace() which does the
1862 * same thing but returns an output stream only.
1864 * Note that in many non-local file cases read and write streams are not
1865 * supported, so make sure you really need to do read and write streaming,
1866 * rather than just opening for reading or writing.
1868 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1869 * Free the returned object with g_object_unref().
1874 g_file_replace_readwrite (GFile *file,
1876 gboolean make_backup,
1877 GFileCreateFlags flags,
1878 GCancellable *cancellable,
1883 g_return_val_if_fail (G_IS_FILE (file), NULL);
1885 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1888 iface = G_FILE_GET_IFACE (file);
1890 if (iface->replace_readwrite == NULL)
1892 g_set_error_literal (error, G_IO_ERROR,
1893 G_IO_ERROR_NOT_SUPPORTED,
1894 _("Operation not supported"));
1898 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1902 * g_file_read_async:
1903 * @file: input #GFile
1904 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1906 * @cancellable: (allow-none): optional #GCancellable object,
1908 * @callback: (scope async): a #GAsyncReadyCallback to call
1909 * when the request is satisfied
1910 * @user_data: (closure): the data to pass to callback function
1912 * Asynchronously opens @file for reading.
1914 * For more details, see g_file_read() which is
1915 * the synchronous version of this call.
1917 * When the operation is finished, @callback will be called.
1918 * You can then call g_file_read_finish() to get the result
1922 g_file_read_async (GFile *file,
1924 GCancellable *cancellable,
1925 GAsyncReadyCallback callback,
1930 g_return_if_fail (G_IS_FILE (file));
1932 iface = G_FILE_GET_IFACE (file);
1933 (* iface->read_async) (file,
1941 * g_file_read_finish:
1942 * @file: input #GFile
1943 * @res: a #GAsyncResult
1944 * @error: a #GError, or %NULL
1946 * Finishes an asynchronous file read operation started with
1947 * g_file_read_async().
1949 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1950 * Free the returned object with g_object_unref().
1953 g_file_read_finish (GFile *file,
1959 g_return_val_if_fail (G_IS_FILE (file), NULL);
1960 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1962 if (g_async_result_legacy_propagate_error (res, error))
1965 iface = G_FILE_GET_IFACE (file);
1966 return (* iface->read_finish) (file, res, error);
1970 * g_file_append_to_async:
1971 * @file: input #GFile
1972 * @flags: a set of #GFileCreateFlags
1973 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1975 * @cancellable: (allow-none): optional #GCancellable object,
1977 * @callback: (scope async): a #GAsyncReadyCallback to call
1978 * when the request is satisfied
1979 * @user_data: (closure): the data to pass to callback function
1981 * Asynchronously opens @file for appending.
1983 * For more details, see g_file_append_to() which is
1984 * the synchronous version of this call.
1986 * When the operation is finished, @callback will be called.
1987 * You can then call g_file_append_to_finish() to get the result
1991 g_file_append_to_async (GFile *file,
1992 GFileCreateFlags flags,
1994 GCancellable *cancellable,
1995 GAsyncReadyCallback callback,
2000 g_return_if_fail (G_IS_FILE (file));
2002 iface = G_FILE_GET_IFACE (file);
2003 (* iface->append_to_async) (file,
2012 * g_file_append_to_finish:
2013 * @file: input #GFile
2014 * @res: #GAsyncResult
2015 * @error: a #GError, or %NULL
2017 * Finishes an asynchronous file append operation started with
2018 * g_file_append_to_async().
2020 * Returns: (transfer full): a valid #GFileOutputStream
2021 * or %NULL on error.
2022 * Free the returned object with g_object_unref().
2025 g_file_append_to_finish (GFile *file,
2031 g_return_val_if_fail (G_IS_FILE (file), NULL);
2032 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2034 if (g_async_result_legacy_propagate_error (res, error))
2037 iface = G_FILE_GET_IFACE (file);
2038 return (* iface->append_to_finish) (file, res, error);
2042 * g_file_create_async:
2043 * @file: input #GFile
2044 * @flags: a set of #GFileCreateFlags
2045 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2047 * @cancellable: (allow-none): optional #GCancellable object,
2049 * @callback: (scope async): a #GAsyncReadyCallback to call
2050 * when the request is satisfied
2051 * @user_data: (closure): the data to pass to callback function
2053 * Asynchronously creates a new file and returns an output stream
2054 * for writing to it. The file must not already exist.
2056 * For more details, see g_file_create() which is
2057 * the synchronous version of this call.
2059 * When the operation is finished, @callback will be called.
2060 * You can then call g_file_create_finish() to get the result
2064 g_file_create_async (GFile *file,
2065 GFileCreateFlags flags,
2067 GCancellable *cancellable,
2068 GAsyncReadyCallback callback,
2073 g_return_if_fail (G_IS_FILE (file));
2075 iface = G_FILE_GET_IFACE (file);
2076 (* iface->create_async) (file,
2085 * g_file_create_finish:
2086 * @file: input #GFile
2087 * @res: a #GAsyncResult
2088 * @error: a #GError, or %NULL
2090 * Finishes an asynchronous file create operation started with
2091 * g_file_create_async().
2093 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2094 * Free the returned object with g_object_unref().
2097 g_file_create_finish (GFile *file,
2103 g_return_val_if_fail (G_IS_FILE (file), NULL);
2104 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2106 if (g_async_result_legacy_propagate_error (res, error))
2109 iface = G_FILE_GET_IFACE (file);
2110 return (* iface->create_finish) (file, res, error);
2114 * g_file_replace_async:
2115 * @file: input #GFile
2116 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2117 * for the current #GFile, or NULL to ignore
2118 * @make_backup: %TRUE if a backup should be created
2119 * @flags: a set of #GFileCreateFlags
2120 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2122 * @cancellable: (allow-none): optional #GCancellable object,
2124 * @callback: (scope async): a #GAsyncReadyCallback to call
2125 * when the request is satisfied
2126 * @user_data: (closure): the data to pass to callback function
2128 * Asynchronously overwrites the file, replacing the contents,
2129 * possibly creating a backup copy of the file first.
2131 * For more details, see g_file_replace() which is
2132 * the synchronous version of this call.
2134 * When the operation is finished, @callback will be called.
2135 * You can then call g_file_replace_finish() to get the result
2139 g_file_replace_async (GFile *file,
2141 gboolean make_backup,
2142 GFileCreateFlags flags,
2144 GCancellable *cancellable,
2145 GAsyncReadyCallback callback,
2150 g_return_if_fail (G_IS_FILE (file));
2152 iface = G_FILE_GET_IFACE (file);
2153 (* iface->replace_async) (file,
2164 * g_file_replace_finish:
2165 * @file: input #GFile
2166 * @res: a #GAsyncResult
2167 * @error: a #GError, or %NULL
2169 * Finishes an asynchronous file replace operation started with
2170 * g_file_replace_async().
2172 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2173 * Free the returned object with g_object_unref().
2176 g_file_replace_finish (GFile *file,
2182 g_return_val_if_fail (G_IS_FILE (file), NULL);
2183 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2185 if (g_async_result_legacy_propagate_error (res, error))
2188 iface = G_FILE_GET_IFACE (file);
2189 return (* iface->replace_finish) (file, res, error);
2193 * g_file_open_readwrite_async
2194 * @file: input #GFile
2195 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2197 * @cancellable: (allow-none): optional #GCancellable object,
2199 * @callback: (scope async): a #GAsyncReadyCallback to call
2200 * when the request is satisfied
2201 * @user_data: (closure): the data to pass to callback function
2203 * Asynchronously opens @file for reading and writing.
2205 * For more details, see g_file_open_readwrite() which is
2206 * the synchronous version of this call.
2208 * When the operation is finished, @callback will be called.
2209 * You can then call g_file_open_readwrite_finish() to get
2210 * the result of the operation.
2215 g_file_open_readwrite_async (GFile *file,
2217 GCancellable *cancellable,
2218 GAsyncReadyCallback callback,
2223 g_return_if_fail (G_IS_FILE (file));
2225 iface = G_FILE_GET_IFACE (file);
2226 (* iface->open_readwrite_async) (file,
2234 * g_file_open_readwrite_finish:
2235 * @file: input #GFile
2236 * @res: a #GAsyncResult
2237 * @error: a #GError, or %NULL
2239 * Finishes an asynchronous file read operation started with
2240 * g_file_open_readwrite_async().
2242 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2243 * Free the returned object with g_object_unref().
2248 g_file_open_readwrite_finish (GFile *file,
2254 g_return_val_if_fail (G_IS_FILE (file), NULL);
2255 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2257 if (g_async_result_legacy_propagate_error (res, error))
2260 iface = G_FILE_GET_IFACE (file);
2261 return (* iface->open_readwrite_finish) (file, res, error);
2265 * g_file_create_readwrite_async:
2266 * @file: input #GFile
2267 * @flags: a set of #GFileCreateFlags
2268 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2270 * @cancellable: (allow-none): optional #GCancellable object,
2272 * @callback: (scope async): a #GAsyncReadyCallback to call
2273 * when the request is satisfied
2274 * @user_data: (closure): the data to pass to callback function
2276 * Asynchronously creates a new file and returns a stream
2277 * for reading and writing to it. The file must not already exist.
2279 * For more details, see g_file_create_readwrite() which is
2280 * the synchronous version of this call.
2282 * When the operation is finished, @callback will be called.
2283 * You can then call g_file_create_readwrite_finish() to get
2284 * the result of the operation.
2289 g_file_create_readwrite_async (GFile *file,
2290 GFileCreateFlags flags,
2292 GCancellable *cancellable,
2293 GAsyncReadyCallback callback,
2298 g_return_if_fail (G_IS_FILE (file));
2300 iface = G_FILE_GET_IFACE (file);
2301 (* iface->create_readwrite_async) (file,
2310 * g_file_create_readwrite_finish:
2311 * @file: input #GFile
2312 * @res: a #GAsyncResult
2313 * @error: a #GError, or %NULL
2315 * Finishes an asynchronous file create operation started with
2316 * g_file_create_readwrite_async().
2318 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2319 * Free the returned object with g_object_unref().
2324 g_file_create_readwrite_finish (GFile *file,
2330 g_return_val_if_fail (G_IS_FILE (file), NULL);
2331 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2333 if (g_async_result_legacy_propagate_error (res, error))
2336 iface = G_FILE_GET_IFACE (file);
2337 return (* iface->create_readwrite_finish) (file, res, error);
2341 * g_file_replace_readwrite_async:
2342 * @file: input #GFile
2343 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2344 * for the current #GFile, or NULL to ignore
2345 * @make_backup: %TRUE if a backup should be created
2346 * @flags: a set of #GFileCreateFlags
2347 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2349 * @cancellable: (allow-none): optional #GCancellable object,
2351 * @callback: (scope async): a #GAsyncReadyCallback to call
2352 * when the request is satisfied
2353 * @user_data: (closure): the data to pass to callback function
2355 * Asynchronously overwrites the file in read-write mode,
2356 * replacing the contents, possibly creating a backup copy
2357 * of the file first.
2359 * For more details, see g_file_replace_readwrite() which is
2360 * the synchronous version of this call.
2362 * When the operation is finished, @callback will be called.
2363 * You can then call g_file_replace_readwrite_finish() to get
2364 * the result of the operation.
2369 g_file_replace_readwrite_async (GFile *file,
2371 gboolean make_backup,
2372 GFileCreateFlags flags,
2374 GCancellable *cancellable,
2375 GAsyncReadyCallback callback,
2380 g_return_if_fail (G_IS_FILE (file));
2382 iface = G_FILE_GET_IFACE (file);
2383 (* iface->replace_readwrite_async) (file,
2394 * g_file_replace_readwrite_finish:
2395 * @file: input #GFile
2396 * @res: a #GAsyncResult
2397 * @error: a #GError, or %NULL
2399 * Finishes an asynchronous file replace operation started with
2400 * g_file_replace_readwrite_async().
2402 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2403 * Free the returned object with g_object_unref().
2408 g_file_replace_readwrite_finish (GFile *file,
2414 g_return_val_if_fail (G_IS_FILE (file), NULL);
2415 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2417 if (g_async_result_legacy_propagate_error (res, error))
2420 iface = G_FILE_GET_IFACE (file);
2421 return (* iface->replace_readwrite_finish) (file, res, error);
2425 copy_symlink (GFile *destination,
2426 GFileCopyFlags flags,
2427 GCancellable *cancellable,
2432 gboolean tried_delete;
2434 GFileType file_type;
2436 tried_delete = FALSE;
2440 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2442 /* Maybe it already existed, and we want to overwrite? */
2443 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2444 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2446 g_error_free (my_error);
2448 /* Don't overwrite if the destination is a directory */
2449 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2450 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2451 cancellable, &my_error);
2454 file_type = g_file_info_get_file_type (info);
2455 g_object_unref (info);
2457 if (file_type == G_FILE_TYPE_DIRECTORY)
2459 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2460 _("Can't copy over directory"));
2465 if (!g_file_delete (destination, cancellable, error))
2468 tried_delete = TRUE;
2472 g_propagate_error (error, my_error);
2479 static GInputStream *
2480 open_source_for_copy (GFile *source,
2482 GFileCopyFlags flags,
2483 GCancellable *cancellable,
2489 GFileType file_type;
2492 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2496 /* There was an error opening the source, try to set a good error for it: */
2497 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2499 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2500 * as that is less useful to the app. Better check for errors on the
2503 g_error_free (my_error);
2506 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2507 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2508 cancellable, &my_error);
2510 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2512 file_type = g_file_info_get_file_type (info);
2513 g_object_unref (info);
2515 if (flags & G_FILE_COPY_OVERWRITE)
2517 if (file_type == G_FILE_TYPE_DIRECTORY)
2519 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2520 _("Can't copy directory over directory"));
2523 /* continue to would_recurse error */
2527 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2528 _("Target file exists"));
2534 /* Error getting info from target, return that error
2535 * (except for NOT_FOUND, which is no error here)
2537 g_clear_object (&info);
2538 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2540 g_propagate_error (error, my_error);
2543 g_clear_error (&my_error);
2546 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2547 _("Can't recursively copy directory"));
2551 g_propagate_error (error, my_error);
2556 should_copy (GFileAttributeInfo *info,
2558 gboolean skip_perms)
2560 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2564 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2565 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2569 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2570 GFileAttributeInfoList *namespaces,
2572 gboolean skip_perms)
2579 s = g_string_new ("");
2583 for (i = 0; i < attributes->n_infos; i++)
2585 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2590 g_string_append_c (s, ',');
2592 g_string_append (s, attributes->infos[i].name);
2599 for (i = 0; i < namespaces->n_infos; i++)
2601 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2606 g_string_append_c (s, ',');
2608 g_string_append (s, namespaces->infos[i].name);
2609 g_string_append (s, "::*");
2614 return g_string_free (s, FALSE);
2618 * g_file_copy_attributes:
2619 * @source: a #GFile with attributes
2620 * @destination: a #GFile to copy attributes to
2621 * @flags: a set of #GFileCopyFlags
2622 * @cancellable: (allow-none): optional #GCancellable object,
2624 * @error: a #GError, %NULL to ignore
2626 * Copies the file attributes from @source to @destination.
2628 * Normally only a subset of the file attributes are copied,
2629 * those that are copies in a normal file copy operation
2630 * (which for instance does not include e.g. owner). However
2631 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2632 * all the metadata that is possible to copy is copied. This
2633 * is useful when implementing move by copy + delete source.
2635 * Returns: %TRUE if the attributes were copied successfully,
2639 g_file_copy_attributes (GFile *source,
2641 GFileCopyFlags flags,
2642 GCancellable *cancellable,
2645 GFileAttributeInfoList *attributes, *namespaces;
2646 char *attrs_to_read;
2650 gboolean source_nofollow_symlinks;
2651 gboolean skip_perms;
2653 as_move = flags & G_FILE_COPY_ALL_METADATA;
2654 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2655 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2657 /* Ignore errors here, if the target supports no attributes there is
2660 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2661 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2663 if (attributes == NULL && namespaces == NULL)
2666 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2668 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2669 * we just don't copy it.
2671 info = g_file_query_info (source, attrs_to_read,
2672 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2676 g_free (attrs_to_read);
2681 res = g_file_set_attributes_from_info (destination,
2683 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2686 g_object_unref (info);
2689 g_file_attribute_info_list_unref (attributes);
2690 g_file_attribute_info_list_unref (namespaces);
2696 copy_stream_with_progress (GInputStream *in,
2699 GCancellable *cancellable,
2700 GFileProgressCallback progress_callback,
2701 gpointer progress_callback_data,
2704 gssize n_read, n_written;
2705 goffset current_size;
2706 char buffer[1024*64], *p;
2712 /* avoid performance impact of querying total size when it's not needed */
2713 if (progress_callback)
2715 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2716 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2720 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2721 total_size = g_file_info_get_size (info);
2722 g_object_unref (info);
2725 if (total_size == -1)
2727 info = g_file_query_info (source,
2728 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2729 G_FILE_QUERY_INFO_NONE,
2733 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2734 total_size = g_file_info_get_size (info);
2735 g_object_unref (info);
2740 if (total_size == -1)
2747 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2757 current_size += n_read;
2762 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2763 if (n_written == -1)
2770 n_read -= n_written;
2776 if (progress_callback)
2777 progress_callback (current_size, total_size, progress_callback_data);
2780 /* Make sure we send full copied size */
2781 if (progress_callback)
2782 progress_callback (current_size, total_size, progress_callback_data);
2790 do_splice (int fd_in,
2795 long *bytes_transferd,
2801 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2809 else if (errsv == ENOSYS || errsv == EINVAL)
2810 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2811 _("Splice not supported"));
2813 g_set_error (error, G_IO_ERROR,
2814 g_io_error_from_errno (errsv),
2815 _("Error splicing file: %s"),
2816 g_strerror (errsv));
2821 *bytes_transferd = result;
2826 splice_stream_with_progress (GInputStream *in,
2828 GCancellable *cancellable,
2829 GFileProgressCallback progress_callback,
2830 gpointer progress_callback_data,
2840 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2841 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2843 if (pipe (buffer) != 0)
2845 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2846 "Pipe creation failed");
2851 /* avoid performance impact of querying total size when it's not needed */
2852 if (progress_callback)
2856 if (fstat (fd_in, &sbuf) == 0)
2857 total_size = sbuf.st_size;
2860 if (total_size == -1)
2863 offset_in = offset_out = 0;
2870 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2873 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2884 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2887 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2890 n_read -= n_written;
2893 if (progress_callback)
2894 progress_callback (offset_in, total_size, progress_callback_data);
2897 /* Make sure we send full copied size */
2898 if (progress_callback)
2899 progress_callback (offset_in, total_size, progress_callback_data);
2910 file_copy_fallback (GFile *source,
2912 GFileCopyFlags flags,
2913 GCancellable *cancellable,
2914 GFileProgressCallback progress_callback,
2915 gpointer progress_callback_data,
2924 gboolean fallback = TRUE;
2927 /* need to know the file type */
2928 info = g_file_query_info (source,
2929 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2930 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2937 /* Maybe copy the symlink? */
2938 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2939 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2941 target = g_file_info_get_symlink_target (info);
2944 if (!copy_symlink (destination, flags, cancellable, target, error))
2946 g_object_unref (info);
2950 g_object_unref (info);
2953 /* ... else fall back on a regular file copy */
2954 g_object_unref (info);
2956 /* Handle "special" files (pipes, device nodes, ...)? */
2957 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2959 /* FIXME: could try to recreate device nodes and others? */
2960 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2961 _("Can't copy special file"));
2962 g_object_unref (info);
2965 /* Everything else should just fall back on a regular copy. */
2967 g_object_unref (info);
2969 in = open_source_for_copy (source, destination, flags, cancellable, error);
2973 if (flags & G_FILE_COPY_OVERWRITE)
2975 out = (GOutputStream *)g_file_replace (destination,
2977 flags & G_FILE_COPY_BACKUP,
2978 G_FILE_CREATE_REPLACE_DESTINATION,
2979 cancellable, error);
2983 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2988 g_object_unref (in);
2993 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2995 GError *splice_err = NULL;
2997 result = splice_stream_with_progress (in, out, cancellable,
2998 progress_callback, progress_callback_data,
3001 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3005 g_propagate_error (error, splice_err);
3008 g_clear_error (&splice_err);
3013 result = copy_stream_with_progress (in, out, source, cancellable,
3014 progress_callback, progress_callback_data,
3017 /* Don't care about errors in source here */
3018 g_input_stream_close (in, cancellable, NULL);
3020 /* But write errors on close are bad! */
3021 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
3024 g_object_unref (in);
3025 g_object_unref (out);
3027 if (result == FALSE)
3031 /* Ignore errors here. Failure to copy metadata is not a hard error */
3032 g_file_copy_attributes (source, destination,
3033 flags, cancellable, NULL);
3040 * @source: input #GFile
3041 * @destination: destination #GFile
3042 * @flags: set of #GFileCopyFlags
3043 * @cancellable: (allow-none): optional #GCancellable object,
3045 * @progress_callback: (allow-none) (scope call): function to callback with
3046 * progress information, or %NULL if progress information is not needed
3047 * @progress_callback_data: (closure): user data to pass to @progress_callback
3048 * @error: #GError to set on error, or %NULL
3050 * Copies the file @source to the location specified by @destination.
3051 * Can not handle recursive copies of directories.
3053 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3054 * existing @destination file is overwritten.
3056 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3057 * will be copied as symlinks, otherwise the target of the
3058 * @source symlink will be copied.
3060 * If @cancellable is not %NULL, then the operation can be cancelled by
3061 * triggering the cancellable object from another thread. If the operation
3062 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3064 * If @progress_callback is not %NULL, then the operation can be monitored
3065 * by setting this to a #GFileProgressCallback function.
3066 * @progress_callback_data will be passed to this function. It is guaranteed
3067 * that this callback will be called after all data has been transferred with
3068 * the total number of bytes copied during the operation.
3070 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3071 * is returned, independent on the status of the @destination.
3073 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3074 * the error %G_IO_ERROR_EXISTS is returned.
3076 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3077 * error is returned. If trying to overwrite a directory with a directory the
3078 * %G_IO_ERROR_WOULD_MERGE error is returned.
3080 * If the source is a directory and the target does not exist, or
3081 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3082 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3084 * If you are interested in copying the #GFile object itself (not the on-disk
3085 * file), see g_file_dup().
3087 * Returns: %TRUE on success, %FALSE otherwise.
3090 g_file_copy (GFile *source,
3092 GFileCopyFlags flags,
3093 GCancellable *cancellable,
3094 GFileProgressCallback progress_callback,
3095 gpointer progress_callback_data,
3102 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3103 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3105 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3108 iface = G_FILE_GET_IFACE (destination);
3112 res = (* iface->copy) (source, destination,
3114 progress_callback, progress_callback_data,
3120 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3122 g_propagate_error (error, my_error);
3126 g_clear_error (&my_error);
3129 /* If the types are different, and the destination method failed
3130 * also try the source method
3132 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3134 iface = G_FILE_GET_IFACE (source);
3139 res = (* iface->copy) (source, destination,
3141 progress_callback, progress_callback_data,
3147 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3149 g_propagate_error (error, my_error);
3153 g_clear_error (&my_error);
3157 return file_copy_fallback (source, destination, flags, cancellable,
3158 progress_callback, progress_callback_data,
3163 * g_file_copy_async: (skip)
3164 * @source: input #GFile
3165 * @destination: destination #GFile
3166 * @flags: set of #GFileCopyFlags
3167 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3169 * @cancellable: (allow-none): optional #GCancellable object,
3171 * @progress_callback: (allow-none): function to callback with progress
3172 * information, or %NULL if progress information is not needed
3173 * @progress_callback_data: (closure): user data to pass to @progress_callback
3174 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3175 * @user_data: the data to pass to callback function
3177 * Copies the file @source to the location specified by @destination
3178 * asynchronously. For details of the behaviour, see g_file_copy().
3180 * If @progress_callback is not %NULL, then that function that will be called
3181 * just like in g_file_copy(), however the callback will run in the main loop,
3182 * not in the thread that is doing the I/O operation.
3184 * When the operation is finished, @callback will be called. You can then call
3185 * g_file_copy_finish() to get the result of the operation.
3188 g_file_copy_async (GFile *source,
3190 GFileCopyFlags flags,
3192 GCancellable *cancellable,
3193 GFileProgressCallback progress_callback,
3194 gpointer progress_callback_data,
3195 GAsyncReadyCallback callback,
3200 g_return_if_fail (G_IS_FILE (source));
3201 g_return_if_fail (G_IS_FILE (destination));
3203 iface = G_FILE_GET_IFACE (source);
3204 (* iface->copy_async) (source,
3210 progress_callback_data,
3216 * g_file_copy_finish:
3217 * @file: input #GFile
3218 * @res: a #GAsyncResult
3219 * @error: a #GError, or %NULL
3221 * Finishes copying the file started with g_file_copy_async().
3223 * Returns: a %TRUE on success, %FALSE on error.
3226 g_file_copy_finish (GFile *file,
3232 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3233 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3235 if (g_async_result_legacy_propagate_error (res, error))
3238 iface = G_FILE_GET_IFACE (file);
3239 return (* iface->copy_finish) (file, res, error);
3244 * @source: #GFile pointing to the source location
3245 * @destination: #GFile pointing to the destination location
3246 * @flags: set of #GFileCopyFlags
3247 * @cancellable: (allow-none): optional #GCancellable object,
3249 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3250 * function for updates
3251 * @progress_callback_data: (closure): gpointer to user data for
3252 * the callback function
3253 * @error: #GError for returning error conditions, or %NULL
3255 * Tries to move the file or directory @source to the location specified
3256 * by @destination. If native move operations are supported then this is
3257 * used, otherwise a copy + delete fallback is used. The native
3258 * implementation may support moving directories (for instance on moves
3259 * inside the same filesystem), but the fallback code does not.
3261 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3262 * existing @destination file is overwritten.
3264 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3265 * will be copied as symlinks, otherwise the target of the
3266 * @source symlink will be copied.
3268 * If @cancellable is not %NULL, then the operation can be cancelled by
3269 * triggering the cancellable object from another thread. If the operation
3270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3272 * If @progress_callback is not %NULL, then the operation can be monitored
3273 * by setting this to a #GFileProgressCallback function.
3274 * @progress_callback_data will be passed to this function. It is
3275 * guaranteed that this callback will be called after all data has been
3276 * transferred with the total number of bytes copied during the operation.
3278 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3279 * error is returned, independent on the status of the @destination.
3281 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3282 * then the error %G_IO_ERROR_EXISTS is returned.
3284 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3285 * error is returned. If trying to overwrite a directory with a directory the
3286 * %G_IO_ERROR_WOULD_MERGE error is returned.
3288 * If the source is a directory and the target does not exist, or
3289 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3290 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3291 * move operation isn't available).
3293 * Returns: %TRUE on successful move, %FALSE otherwise.
3296 g_file_move (GFile *source,
3298 GFileCopyFlags flags,
3299 GCancellable *cancellable,
3300 GFileProgressCallback progress_callback,
3301 gpointer progress_callback_data,
3308 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3309 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3311 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3314 iface = G_FILE_GET_IFACE (destination);
3318 res = (* iface->move) (source, destination,
3320 progress_callback, progress_callback_data,
3326 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3328 g_propagate_error (error, my_error);
3333 /* If the types are different, and the destination method failed
3334 * also try the source method
3336 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3338 iface = G_FILE_GET_IFACE (source);
3343 res = (* iface->move) (source, destination,
3345 progress_callback, progress_callback_data,
3351 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3353 g_propagate_error (error, my_error);
3359 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3361 g_set_error_literal (error, G_IO_ERROR,
3362 G_IO_ERROR_NOT_SUPPORTED,
3363 _("Operation not supported"));
3367 flags |= G_FILE_COPY_ALL_METADATA;
3368 if (!g_file_copy (source, destination, flags, cancellable,
3369 progress_callback, progress_callback_data,
3373 return g_file_delete (source, cancellable, error);
3377 * g_file_make_directory:
3378 * @file: input #GFile
3379 * @cancellable: (allow-none): optional #GCancellable object,
3381 * @error: a #GError, or %NULL
3383 * Creates a directory. Note that this will only create a child directory
3384 * of the immediate parent directory of the path or URI given by the #GFile.
3385 * To recursively create directories, see g_file_make_directory_with_parents().
3386 * This function will fail if the parent directory does not exist, setting
3387 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3388 * creating directories, this function will fail, setting @error to
3389 * %G_IO_ERROR_NOT_SUPPORTED.
3391 * For a local #GFile the newly created directory will have the default
3392 * (current) ownership and permissions of the current process.
3394 * If @cancellable is not %NULL, then the operation can be cancelled by
3395 * triggering the cancellable object from another thread. If the operation
3396 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3398 * Returns: %TRUE on successful creation, %FALSE otherwise.
3401 g_file_make_directory (GFile *file,
3402 GCancellable *cancellable,
3407 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3412 iface = G_FILE_GET_IFACE (file);
3414 if (iface->make_directory == NULL)
3416 g_set_error_literal (error, G_IO_ERROR,
3417 G_IO_ERROR_NOT_SUPPORTED,
3418 _("Operation not supported"));
3422 return (* iface->make_directory) (file, cancellable, error);
3426 * g_file_make_directory_with_parents:
3427 * @file: input #GFile
3428 * @cancellable: (allow-none): optional #GCancellable object,
3430 * @error: a #GError, or %NULL
3432 * Creates a directory and any parent directories that may not
3433 * exist similar to 'mkdir -p'. If the file system does not support
3434 * creating directories, this function will fail, setting @error to
3435 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3436 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3437 * the similar g_mkdir_with_parents().
3439 * For a local #GFile the newly created directories will have the default
3440 * (current) ownership and permissions of the current process.
3442 * If @cancellable is not %NULL, then the operation can be cancelled by
3443 * triggering the cancellable object from another thread. If the operation
3444 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3446 * Returns: %TRUE if all directories have been successfully created, %FALSE
3452 g_file_make_directory_with_parents (GFile *file,
3453 GCancellable *cancellable,
3456 GFile *work_file = NULL;
3457 GList *list = NULL, *l;
3458 GError *my_error = NULL;
3460 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3462 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3465 g_file_make_directory (file, cancellable, &my_error);
3466 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3469 g_propagate_error (error, my_error);
3470 return my_error == NULL;
3473 work_file = g_object_ref (file);
3475 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3479 parent_file = g_file_get_parent (work_file);
3480 if (parent_file == NULL)
3483 g_clear_error (&my_error);
3484 g_file_make_directory (parent_file, cancellable, &my_error);
3486 g_object_unref (work_file);
3487 work_file = g_object_ref (parent_file);
3489 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3490 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3492 g_object_unref (parent_file);
3495 for (l = list; my_error == NULL && l; l = l->next)
3497 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3501 g_object_unref (work_file);
3504 while (list != NULL)
3506 g_object_unref ((GFile *) list->data);
3507 list = g_list_remove (list, list->data);
3510 if (my_error != NULL)
3512 g_propagate_error (error, my_error);
3516 return g_file_make_directory (file, cancellable, error);
3520 * g_file_make_symbolic_link:
3521 * @file: a #GFile with the name of the symlink to create
3522 * @symlink_value: a string with the path for the target of the new symlink
3523 * @cancellable: (allow-none): optional #GCancellable object,
3527 * Creates a symbolic link named @file which contains the string
3530 * If @cancellable is not %NULL, then the operation can be cancelled by
3531 * triggering the cancellable object from another thread. If the operation
3532 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3534 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3537 g_file_make_symbolic_link (GFile *file,
3538 const char *symlink_value,
3539 GCancellable *cancellable,
3544 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3545 g_return_val_if_fail (symlink_value != NULL, FALSE);
3547 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3550 if (*symlink_value == '\0')
3552 g_set_error_literal (error, G_IO_ERROR,
3553 G_IO_ERROR_INVALID_ARGUMENT,
3554 _("Invalid symlink value given"));
3558 iface = G_FILE_GET_IFACE (file);
3560 if (iface->make_symbolic_link == NULL)
3562 g_set_error_literal (error, G_IO_ERROR,
3563 G_IO_ERROR_NOT_SUPPORTED,
3564 _("Operation not supported"));
3568 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3573 * @file: input #GFile
3574 * @cancellable: (allow-none): optional #GCancellable object,
3576 * @error: a #GError, or %NULL
3578 * Deletes a file. If the @file is a directory, it will only be
3579 * deleted if it is empty. This has the same semantics as g_unlink().
3581 * If @cancellable is not %NULL, then the operation can be cancelled by
3582 * triggering the cancellable object from another thread. If the operation
3583 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3585 * Virtual: delete_file
3586 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3589 g_file_delete (GFile *file,
3590 GCancellable *cancellable,
3595 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3597 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3600 iface = G_FILE_GET_IFACE (file);
3602 if (iface->delete_file == NULL)
3604 g_set_error_literal (error, G_IO_ERROR,
3605 G_IO_ERROR_NOT_SUPPORTED,
3606 _("Operation not supported"));
3610 return (* iface->delete_file) (file, cancellable, error);
3614 * g_file_delete_async:
3615 * @file: input #GFile
3616 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3618 * @cancellable: (allow-none): optional #GCancellable object,
3620 * @callback: a #GAsyncReadyCallback to call
3621 * when the request is satisfied
3622 * @user_data: the data to pass to callback function
3624 * Asynchronously delete a file. If the @file is a directory, it will
3625 * only be deleted if it is empty. This has the same semantics as
3628 * Virtual: delete_file_async
3632 g_file_delete_async (GFile *file,
3634 GCancellable *cancellable,
3635 GAsyncReadyCallback callback,
3640 g_return_if_fail (G_IS_FILE (file));
3642 iface = G_FILE_GET_IFACE (file);
3643 (* iface->delete_file_async) (file,
3651 * g_file_delete_finish:
3652 * @file: input #GFile
3653 * @result: a #GAsyncResult
3654 * @error: a #GError, or %NULL
3656 * Finishes deleting a file started with g_file_delete_async().
3658 * Virtual: delete_file_finish
3662 g_file_delete_finish (GFile *file,
3663 GAsyncResult *result,
3668 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3669 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3671 if (g_async_result_legacy_propagate_error (result, error))
3674 iface = G_FILE_GET_IFACE (file);
3675 return (* iface->delete_file_finish) (file, result, error);
3680 * @file: #GFile to send to trash
3681 * @cancellable: (allow-none): optional #GCancellable object,
3683 * @error: a #GError, or %NULL
3685 * Sends @file to the "Trashcan", if possible. This is similar to
3686 * deleting it, but the user can recover it before emptying the trashcan.
3687 * Not all file systems support trashing, so this call can return the
3688 * %G_IO_ERROR_NOT_SUPPORTED error.
3690 * If @cancellable is not %NULL, then the operation can be cancelled by
3691 * triggering the cancellable object from another thread. If the operation
3692 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3694 * Returns: %TRUE on successful trash, %FALSE otherwise.
3697 g_file_trash (GFile *file,
3698 GCancellable *cancellable,
3703 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3705 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3708 iface = G_FILE_GET_IFACE (file);
3710 if (iface->trash == NULL)
3712 g_set_error_literal (error,
3713 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3714 _("Trash not supported"));
3718 return (* iface->trash) (file, cancellable, error);
3722 * g_file_set_display_name:
3723 * @file: input #GFile
3724 * @display_name: a string
3725 * @cancellable: (allow-none): optional #GCancellable object,
3727 * @error: a #GError, or %NULL
3729 * Renames @file to the specified display name.
3731 * The display name is converted from UTF-8 to the correct encoding
3732 * for the target filesystem if possible and the @file is renamed to this.
3734 * If you want to implement a rename operation in the user interface the
3735 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3736 * initial value in the rename widget, and then the result after editing
3737 * should be passed to g_file_set_display_name().
3739 * On success the resulting converted filename is returned.
3741 * If @cancellable is not %NULL, then the operation can be cancelled by
3742 * triggering the cancellable object from another thread. If the operation
3743 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3745 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3746 * or %NULL if there was an error.
3747 * Free the returned object with g_object_unref().
3750 g_file_set_display_name (GFile *file,
3751 const gchar *display_name,
3752 GCancellable *cancellable,
3757 g_return_val_if_fail (G_IS_FILE (file), NULL);
3758 g_return_val_if_fail (display_name != NULL, NULL);
3760 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3764 G_IO_ERROR_INVALID_ARGUMENT,
3765 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3769 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3772 iface = G_FILE_GET_IFACE (file);
3774 return (* iface->set_display_name) (file, display_name, cancellable, error);
3778 * g_file_set_display_name_async:
3779 * @file: input #GFile
3780 * @display_name: a string
3781 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3783 * @cancellable: (allow-none): optional #GCancellable object,
3785 * @callback: (scope async): a #GAsyncReadyCallback to call
3786 * when the request is satisfied
3787 * @user_data: (closure): the data to pass to callback function
3789 * Asynchronously sets the display name for a given #GFile.
3791 * For more details, see g_file_set_display_name() which is
3792 * the synchronous version of this call.
3794 * When the operation is finished, @callback will be called.
3795 * You can then call g_file_set_display_name_finish() to get
3796 * the result of the operation.
3799 g_file_set_display_name_async (GFile *file,
3800 const gchar *display_name,
3802 GCancellable *cancellable,
3803 GAsyncReadyCallback callback,
3808 g_return_if_fail (G_IS_FILE (file));
3809 g_return_if_fail (display_name != NULL);
3811 iface = G_FILE_GET_IFACE (file);
3812 (* iface->set_display_name_async) (file,
3821 * g_file_set_display_name_finish:
3822 * @file: input #GFile
3823 * @res: a #GAsyncResult
3824 * @error: a #GError, or %NULL
3826 * Finishes setting a display name started with
3827 * g_file_set_display_name_async().
3829 * Returns: (transfer full): a #GFile or %NULL on error.
3830 * Free the returned object with g_object_unref().
3833 g_file_set_display_name_finish (GFile *file,
3839 g_return_val_if_fail (G_IS_FILE (file), NULL);
3840 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3842 if (g_async_result_legacy_propagate_error (res, error))
3845 iface = G_FILE_GET_IFACE (file);
3846 return (* iface->set_display_name_finish) (file, res, error);
3850 * g_file_query_settable_attributes:
3851 * @file: input #GFile
3852 * @cancellable: (allow-none): optional #GCancellable object,
3854 * @error: a #GError, or %NULL
3856 * Obtain the list of settable attributes for the file.
3858 * Returns the type and full attribute name of all the attributes
3859 * that can be set on this file. This doesn't mean setting it will
3860 * always succeed though, you might get an access failure, or some
3861 * specific file may not support a specific attribute.
3863 * If @cancellable is not %NULL, then the operation can be cancelled by
3864 * triggering the cancellable object from another thread. If the operation
3865 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3867 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3868 * When you are done with it, release it with
3869 * g_file_attribute_info_list_unref()
3871 GFileAttributeInfoList *
3872 g_file_query_settable_attributes (GFile *file,
3873 GCancellable *cancellable,
3878 GFileAttributeInfoList *list;
3880 g_return_val_if_fail (G_IS_FILE (file), NULL);
3882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3885 iface = G_FILE_GET_IFACE (file);
3887 if (iface->query_settable_attributes == NULL)
3888 return g_file_attribute_info_list_new ();
3891 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3895 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3897 list = g_file_attribute_info_list_new ();
3898 g_error_free (my_error);
3901 g_propagate_error (error, my_error);
3908 * g_file_query_writable_namespaces:
3909 * @file: input #GFile
3910 * @cancellable: (allow-none): optional #GCancellable object,
3912 * @error: a #GError, or %NULL
3914 * Obtain the list of attribute namespaces where new attributes
3915 * can be created by a user. An example of this is extended
3916 * attributes (in the "xattr" namespace).
3918 * If @cancellable is not %NULL, then the operation can be cancelled by
3919 * triggering the cancellable object from another thread. If the operation
3920 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3922 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3923 * When you are done with it, release it with
3924 * g_file_attribute_info_list_unref()
3926 GFileAttributeInfoList *
3927 g_file_query_writable_namespaces (GFile *file,
3928 GCancellable *cancellable,
3933 GFileAttributeInfoList *list;
3935 g_return_val_if_fail (G_IS_FILE (file), NULL);
3937 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3940 iface = G_FILE_GET_IFACE (file);
3942 if (iface->query_writable_namespaces == NULL)
3943 return g_file_attribute_info_list_new ();
3946 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3950 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3952 list = g_file_attribute_info_list_new ();
3953 g_error_free (my_error);
3956 g_propagate_error (error, my_error);
3963 * g_file_set_attribute:
3964 * @file: input #GFile
3965 * @attribute: a string containing the attribute's name
3966 * @type: The type of the attribute
3967 * @value_p: (allow-none): a pointer to the value (or the pointer
3968 * itself if the type is a pointer type)
3969 * @flags: a set of #GFileQueryInfoFlags
3970 * @cancellable: (allow-none): optional #GCancellable object,
3972 * @error: a #GError, or %NULL
3974 * Sets an attribute in the file with attribute name @attribute to @value.
3976 * Some attributes can be unset by setting @attribute to
3977 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3979 * If @cancellable is not %NULL, then the operation can be cancelled by
3980 * triggering the cancellable object from another thread. If the operation
3981 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3983 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3986 g_file_set_attribute (GFile *file,
3987 const gchar *attribute,
3988 GFileAttributeType type,
3990 GFileQueryInfoFlags flags,
3991 GCancellable *cancellable,
3996 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3997 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3999 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4002 iface = G_FILE_GET_IFACE (file);
4004 if (iface->set_attribute == NULL)
4006 g_set_error_literal (error, G_IO_ERROR,
4007 G_IO_ERROR_NOT_SUPPORTED,
4008 _("Operation not supported"));
4012 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4016 * g_file_set_attributes_from_info:
4017 * @file: input #GFile
4018 * @info: a #GFileInfo
4019 * @flags: #GFileQueryInfoFlags
4020 * @cancellable: (allow-none): optional #GCancellable object,
4022 * @error: a #GError, or %NULL
4024 * Tries to set all attributes in the #GFileInfo on the target
4025 * values, not stopping on the first error.
4027 * If there is any error during this operation then @error will
4028 * be set to the first error. Error on particular fields are flagged
4029 * by setting the "status" field in the attribute value to
4030 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4031 * also detect further errors.
4033 * If @cancellable is not %NULL, then the operation can be cancelled by
4034 * triggering the cancellable object from another thread. If the operation
4035 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4037 * Returns: %TRUE if there was any error, %FALSE otherwise.
4040 g_file_set_attributes_from_info (GFile *file,
4042 GFileQueryInfoFlags flags,
4043 GCancellable *cancellable,
4048 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4049 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4051 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4054 g_file_info_clear_status (info);
4056 iface = G_FILE_GET_IFACE (file);
4058 return (* iface->set_attributes_from_info) (file,
4066 g_file_real_set_attributes_from_info (GFile *file,
4068 GFileQueryInfoFlags flags,
4069 GCancellable *cancellable,
4075 GFileAttributeValue *value;
4079 attributes = g_file_info_list_attributes (info, NULL);
4081 for (i = 0; attributes[i] != NULL; i++)
4083 value = _g_file_info_get_attribute_value (info, attributes[i]);
4085 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4088 if (!g_file_set_attribute (file, attributes[i],
4089 value->type, _g_file_attribute_value_peek_as_pointer (value),
4090 flags, cancellable, error))
4092 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4094 /* Don't set error multiple times */
4098 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4101 g_strfreev (attributes);
4107 * g_file_set_attributes_async:
4108 * @file: input #GFile
4109 * @info: a #GFileInfo
4110 * @flags: a #GFileQueryInfoFlags
4111 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4113 * @cancellable: (allow-none): optional #GCancellable object,
4115 * @callback: (scope async): a #GAsyncReadyCallback
4116 * @user_data: (closure): a #gpointer
4118 * Asynchronously sets the attributes of @file with @info.
4120 * For more details, see g_file_set_attributes_from_info(),
4121 * which is the synchronous version of this call.
4123 * When the operation is finished, @callback will be called.
4124 * You can then call g_file_set_attributes_finish() to get
4125 * the result of the operation.
4128 g_file_set_attributes_async (GFile *file,
4130 GFileQueryInfoFlags flags,
4132 GCancellable *cancellable,
4133 GAsyncReadyCallback callback,
4138 g_return_if_fail (G_IS_FILE (file));
4139 g_return_if_fail (G_IS_FILE_INFO (info));
4141 iface = G_FILE_GET_IFACE (file);
4142 (* iface->set_attributes_async) (file,
4152 * g_file_set_attributes_finish:
4153 * @file: input #GFile
4154 * @result: a #GAsyncResult
4155 * @info: (out) (transfer full): a #GFileInfo
4156 * @error: a #GError, or %NULL
4158 * Finishes setting an attribute started in g_file_set_attributes_async().
4160 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4163 g_file_set_attributes_finish (GFile *file,
4164 GAsyncResult *result,
4170 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4171 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4173 /* No standard handling of errors here, as we must set info even
4176 iface = G_FILE_GET_IFACE (file);
4177 return (* iface->set_attributes_finish) (file, result, info, error);
4181 * g_file_set_attribute_string:
4182 * @file: input #GFile
4183 * @attribute: a string containing the attribute's name
4184 * @value: a string containing the attribute's value
4185 * @flags: #GFileQueryInfoFlags
4186 * @cancellable: (allow-none): optional #GCancellable object,
4188 * @error: a #GError, or %NULL
4190 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4191 * If @attribute is of a different type, this operation will fail.
4193 * If @cancellable is not %NULL, then the operation can be cancelled by
4194 * triggering the cancellable object from another thread. If the operation
4195 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4197 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4200 g_file_set_attribute_string (GFile *file,
4201 const char *attribute,
4203 GFileQueryInfoFlags flags,
4204 GCancellable *cancellable,
4207 return g_file_set_attribute (file, attribute,
4208 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4209 flags, cancellable, error);
4213 * g_file_set_attribute_byte_string:
4214 * @file: input #GFile
4215 * @attribute: a string containing the attribute's name
4216 * @value: a string containing the attribute's new value
4217 * @flags: a #GFileQueryInfoFlags
4218 * @cancellable: (allow-none): optional #GCancellable object,
4220 * @error: a #GError, or %NULL
4222 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4223 * If @attribute is of a different type, this operation will fail,
4226 * If @cancellable is not %NULL, then the operation can be cancelled by
4227 * triggering the cancellable object from another thread. If the operation
4228 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4230 * Returns: %TRUE if the @attribute was successfully set to @value
4231 * in the @file, %FALSE otherwise.
4234 g_file_set_attribute_byte_string (GFile *file,
4235 const gchar *attribute,
4237 GFileQueryInfoFlags flags,
4238 GCancellable *cancellable,
4241 return g_file_set_attribute (file, attribute,
4242 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4243 flags, cancellable, error);
4247 * g_file_set_attribute_uint32:
4248 * @file: input #GFile
4249 * @attribute: a string containing the attribute's name
4250 * @value: a #guint32 containing the attribute's new value
4251 * @flags: a #GFileQueryInfoFlags
4252 * @cancellable: (allow-none): optional #GCancellable object,
4254 * @error: a #GError, or %NULL
4256 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4257 * If @attribute is of a different type, this operation will fail.
4259 * If @cancellable is not %NULL, then the operation can be cancelled by
4260 * triggering the cancellable object from another thread. If the operation
4261 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4263 * Returns: %TRUE if the @attribute was successfully set to @value
4264 * in the @file, %FALSE otherwise.
4267 g_file_set_attribute_uint32 (GFile *file,
4268 const gchar *attribute,
4270 GFileQueryInfoFlags flags,
4271 GCancellable *cancellable,
4274 return g_file_set_attribute (file, attribute,
4275 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4276 flags, cancellable, error);
4280 * g_file_set_attribute_int32:
4281 * @file: input #GFile
4282 * @attribute: a string containing the attribute's name
4283 * @value: a #gint32 containing the attribute's new value
4284 * @flags: a #GFileQueryInfoFlags
4285 * @cancellable: (allow-none): optional #GCancellable object,
4287 * @error: a #GError, or %NULL
4289 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4290 * If @attribute is of a different type, this operation will fail.
4292 * If @cancellable is not %NULL, then the operation can be cancelled by
4293 * triggering the cancellable object from another thread. If the operation
4294 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4296 * Returns: %TRUE if the @attribute was successfully set to @value
4297 * in the @file, %FALSE otherwise.
4300 g_file_set_attribute_int32 (GFile *file,
4301 const gchar *attribute,
4303 GFileQueryInfoFlags flags,
4304 GCancellable *cancellable,
4307 return g_file_set_attribute (file, attribute,
4308 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4309 flags, cancellable, error);
4313 * g_file_set_attribute_uint64:
4314 * @file: input #GFile
4315 * @attribute: a string containing the attribute's name
4316 * @value: a #guint64 containing the attribute's new value
4317 * @flags: a #GFileQueryInfoFlags
4318 * @cancellable: (allow-none): optional #GCancellable object,
4320 * @error: a #GError, or %NULL
4322 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4323 * If @attribute is of a different type, this operation will fail.
4325 * If @cancellable is not %NULL, then the operation can be cancelled by
4326 * triggering the cancellable object from another thread. If the operation
4327 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4329 * Returns: %TRUE if the @attribute was successfully set to @value
4330 * in the @file, %FALSE otherwise.
4333 g_file_set_attribute_uint64 (GFile *file,
4334 const gchar *attribute,
4336 GFileQueryInfoFlags flags,
4337 GCancellable *cancellable,
4340 return g_file_set_attribute (file, attribute,
4341 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4342 flags, cancellable, error);
4346 * g_file_set_attribute_int64:
4347 * @file: input #GFile
4348 * @attribute: a string containing the attribute's name
4349 * @value: a #guint64 containing the attribute's new value
4350 * @flags: a #GFileQueryInfoFlags
4351 * @cancellable: (allow-none): optional #GCancellable object,
4353 * @error: a #GError, or %NULL
4355 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4356 * If @attribute is of a different type, this operation will fail.
4358 * If @cancellable is not %NULL, then the operation can be cancelled by
4359 * triggering the cancellable object from another thread. If the operation
4360 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4362 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4365 g_file_set_attribute_int64 (GFile *file,
4366 const gchar *attribute,
4368 GFileQueryInfoFlags flags,
4369 GCancellable *cancellable,
4372 return g_file_set_attribute (file, attribute,
4373 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4374 flags, cancellable, error);
4378 * g_file_mount_mountable:
4379 * @file: input #GFile
4380 * @flags: flags affecting the operation
4381 * @mount_operation: (allow-none): a #GMountOperation,
4382 * or %NULL to avoid user interaction
4383 * @cancellable: (allow-none): optional #GCancellable object,
4385 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4386 * when the request is satisfied, or %NULL
4387 * @user_data: (closure): the data to pass to callback function
4389 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4390 * Using @mount_operation, you can request callbacks when, for instance,
4391 * passwords are needed during authentication.
4393 * If @cancellable is not %NULL, then the operation can be cancelled by
4394 * triggering the cancellable object from another thread. If the operation
4395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4397 * When the operation is finished, @callback will be called.
4398 * You can then call g_file_mount_mountable_finish() to get
4399 * the result of the operation.
4402 g_file_mount_mountable (GFile *file,
4403 GMountMountFlags flags,
4404 GMountOperation *mount_operation,
4405 GCancellable *cancellable,
4406 GAsyncReadyCallback callback,
4411 g_return_if_fail (G_IS_FILE (file));
4413 iface = G_FILE_GET_IFACE (file);
4415 if (iface->mount_mountable == NULL)
4417 g_simple_async_report_error_in_idle (G_OBJECT (file),
4421 G_IO_ERROR_NOT_SUPPORTED,
4422 _("Operation not supported"));
4426 (* iface->mount_mountable) (file,
4435 * g_file_mount_mountable_finish:
4436 * @file: input #GFile
4437 * @result: a #GAsyncResult
4438 * @error: a #GError, or %NULL
4440 * Finishes a mount operation. See g_file_mount_mountable() for details.
4442 * Finish an asynchronous mount operation that was started
4443 * with g_file_mount_mountable().
4445 * Returns: (transfer full): a #GFile or %NULL on error.
4446 * Free the returned object with g_object_unref().
4449 g_file_mount_mountable_finish (GFile *file,
4450 GAsyncResult *result,
4455 g_return_val_if_fail (G_IS_FILE (file), NULL);
4456 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4458 if (g_async_result_legacy_propagate_error (result, error))
4461 iface = G_FILE_GET_IFACE (file);
4462 return (* iface->mount_mountable_finish) (file, result, error);
4466 * g_file_unmount_mountable:
4467 * @file: input #GFile
4468 * @flags: flags affecting the operation
4469 * @cancellable: (allow-none): optional #GCancellable object,
4471 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4472 * when the request is satisfied, or %NULL
4473 * @user_data: (closure): the data to pass to callback function
4475 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4477 * If @cancellable is not %NULL, then the operation can be cancelled by
4478 * triggering the cancellable object from another thread. If the operation
4479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4481 * When the operation is finished, @callback will be called.
4482 * You can then call g_file_unmount_mountable_finish() to get
4483 * the result of the operation.
4485 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4488 g_file_unmount_mountable (GFile *file,
4489 GMountUnmountFlags flags,
4490 GCancellable *cancellable,
4491 GAsyncReadyCallback callback,
4496 g_return_if_fail (G_IS_FILE (file));
4498 iface = G_FILE_GET_IFACE (file);
4500 if (iface->unmount_mountable == NULL)
4502 g_simple_async_report_error_in_idle (G_OBJECT (file),
4506 G_IO_ERROR_NOT_SUPPORTED,
4507 _("Operation not supported"));
4511 (* iface->unmount_mountable) (file,
4519 * g_file_unmount_mountable_finish:
4520 * @file: input #GFile
4521 * @result: a #GAsyncResult
4522 * @error: a #GError, or %NULL
4524 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4526 * Finish an asynchronous unmount operation that was started
4527 * with g_file_unmount_mountable().
4529 * Returns: %TRUE if the operation finished successfully.
4532 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4536 g_file_unmount_mountable_finish (GFile *file,
4537 GAsyncResult *result,
4542 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4543 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4545 if (g_async_result_legacy_propagate_error (result, error))
4548 iface = G_FILE_GET_IFACE (file);
4549 return (* iface->unmount_mountable_finish) (file, result, error);
4553 * g_file_unmount_mountable_with_operation:
4554 * @file: input #GFile
4555 * @flags: flags affecting the operation
4556 * @mount_operation: (allow-none): a #GMountOperation,
4557 * or %NULL to avoid user interaction
4558 * @cancellable: (allow-none): optional #GCancellable object,
4560 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4561 * when the request is satisfied, or %NULL
4562 * @user_data: (closure): the data to pass to callback function
4564 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4566 * If @cancellable is not %NULL, then the operation can be cancelled by
4567 * triggering the cancellable object from another thread. If the operation
4568 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4570 * When the operation is finished, @callback will be called.
4571 * You can then call g_file_unmount_mountable_finish() to get
4572 * the result of the operation.
4577 g_file_unmount_mountable_with_operation (GFile *file,
4578 GMountUnmountFlags flags,
4579 GMountOperation *mount_operation,
4580 GCancellable *cancellable,
4581 GAsyncReadyCallback callback,
4586 g_return_if_fail (G_IS_FILE (file));
4588 iface = G_FILE_GET_IFACE (file);
4590 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4592 g_simple_async_report_error_in_idle (G_OBJECT (file),
4596 G_IO_ERROR_NOT_SUPPORTED,
4597 _("Operation not supported"));
4601 if (iface->unmount_mountable_with_operation != NULL)
4602 (* iface->unmount_mountable_with_operation) (file,
4609 (* iface->unmount_mountable) (file,
4617 * g_file_unmount_mountable_with_operation_finish:
4618 * @file: input #GFile
4619 * @result: a #GAsyncResult
4620 * @error: a #GError, or %NULL
4622 * Finishes an unmount operation,
4623 * see g_file_unmount_mountable_with_operation() for details.
4625 * Finish an asynchronous unmount operation that was started
4626 * with g_file_unmount_mountable_with_operation().
4628 * Returns: %TRUE if the operation finished successfully.
4634 g_file_unmount_mountable_with_operation_finish (GFile *file,
4635 GAsyncResult *result,
4640 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4641 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4643 if (g_async_result_legacy_propagate_error (result, error))
4646 iface = G_FILE_GET_IFACE (file);
4647 if (iface->unmount_mountable_with_operation_finish != NULL)
4648 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4650 return (* iface->unmount_mountable_finish) (file, result, error);
4654 * g_file_eject_mountable:
4655 * @file: input #GFile
4656 * @flags: flags affecting the operation
4657 * @cancellable: (allow-none): optional #GCancellable object,
4659 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4660 * when the request is satisfied, or %NULL
4661 * @user_data: (closure): the data to pass to callback function
4663 * Starts an asynchronous eject on a mountable.
4664 * When this operation has completed, @callback will be called with
4665 * @user_user data, and the operation can be finalized with
4666 * g_file_eject_mountable_finish().
4668 * If @cancellable is not %NULL, then the operation can be cancelled by
4669 * triggering the cancellable object from another thread. If the operation
4670 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4672 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4675 g_file_eject_mountable (GFile *file,
4676 GMountUnmountFlags flags,
4677 GCancellable *cancellable,
4678 GAsyncReadyCallback callback,
4683 g_return_if_fail (G_IS_FILE (file));
4685 iface = G_FILE_GET_IFACE (file);
4687 if (iface->eject_mountable == NULL)
4689 g_simple_async_report_error_in_idle (G_OBJECT (file),
4693 G_IO_ERROR_NOT_SUPPORTED,
4694 _("Operation not supported"));
4698 (* iface->eject_mountable) (file,
4706 * g_file_eject_mountable_finish:
4707 * @file: input #GFile
4708 * @result: a #GAsyncResult
4709 * @error: a #GError, or %NULL
4711 * Finishes an asynchronous eject operation started by
4712 * g_file_eject_mountable().
4714 * Returns: %TRUE if the @file was ejected successfully.
4717 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4721 g_file_eject_mountable_finish (GFile *file,
4722 GAsyncResult *result,
4727 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4728 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4730 if (g_async_result_legacy_propagate_error (result, error))
4733 iface = G_FILE_GET_IFACE (file);
4734 return (* iface->eject_mountable_finish) (file, result, error);
4738 * g_file_eject_mountable_with_operation:
4739 * @file: input #GFile
4740 * @flags: flags affecting the operation
4741 * @mount_operation: (allow-none): a #GMountOperation,
4742 * or %NULL to avoid user interaction
4743 * @cancellable: (allow-none): optional #GCancellable object,
4745 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4746 * when the request is satisfied, or %NULL
4747 * @user_data: (closure): the data to pass to callback function
4749 * Starts an asynchronous eject on a mountable.
4750 * When this operation has completed, @callback will be called with
4751 * @user_user data, and the operation can be finalized with
4752 * g_file_eject_mountable_with_operation_finish().
4754 * If @cancellable is not %NULL, then the operation can be cancelled by
4755 * triggering the cancellable object from another thread. If the operation
4756 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4761 g_file_eject_mountable_with_operation (GFile *file,
4762 GMountUnmountFlags flags,
4763 GMountOperation *mount_operation,
4764 GCancellable *cancellable,
4765 GAsyncReadyCallback callback,
4770 g_return_if_fail (G_IS_FILE (file));
4772 iface = G_FILE_GET_IFACE (file);
4774 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4776 g_simple_async_report_error_in_idle (G_OBJECT (file),
4780 G_IO_ERROR_NOT_SUPPORTED,
4781 _("Operation not supported"));
4785 if (iface->eject_mountable_with_operation != NULL)
4786 (* iface->eject_mountable_with_operation) (file,
4793 (* iface->eject_mountable) (file,
4801 * g_file_eject_mountable_with_operation_finish:
4802 * @file: input #GFile
4803 * @result: a #GAsyncResult
4804 * @error: a #GError, or %NULL
4806 * Finishes an asynchronous eject operation started by
4807 * g_file_eject_mountable_with_operation().
4809 * Returns: %TRUE if the @file was ejected successfully.
4815 g_file_eject_mountable_with_operation_finish (GFile *file,
4816 GAsyncResult *result,
4821 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4822 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4824 if (g_async_result_legacy_propagate_error (result, error))
4827 iface = G_FILE_GET_IFACE (file);
4828 if (iface->eject_mountable_with_operation_finish != NULL)
4829 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4831 return (* iface->eject_mountable_finish) (file, result, error);
4835 * g_file_monitor_directory:
4836 * @file: input #GFile
4837 * @flags: a set of #GFileMonitorFlags
4838 * @cancellable: (allow-none): optional #GCancellable object,
4840 * @error: a #GError, or %NULL
4842 * Obtains a directory monitor for the given file.
4843 * This may fail if directory monitoring is not supported.
4845 * If @cancellable is not %NULL, then the operation can be cancelled by
4846 * triggering the cancellable object from another thread. If the operation
4847 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4849 * Virtual: monitor_dir
4850 * Returns: (transfer full): a #GFileMonitor for the given @file,
4851 * or %NULL on error.
4852 * Free the returned object with g_object_unref().
4855 g_file_monitor_directory (GFile *file,
4856 GFileMonitorFlags flags,
4857 GCancellable *cancellable,
4862 g_return_val_if_fail (G_IS_FILE (file), NULL);
4864 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4867 iface = G_FILE_GET_IFACE (file);
4869 if (iface->monitor_dir == NULL)
4871 g_set_error_literal (error, G_IO_ERROR,
4872 G_IO_ERROR_NOT_SUPPORTED,
4873 _("Operation not supported"));
4877 return (* iface->monitor_dir) (file, flags, cancellable, error);
4881 * g_file_monitor_file:
4882 * @file: input #GFile
4883 * @flags: a set of #GFileMonitorFlags
4884 * @cancellable: (allow-none): optional #GCancellable object,
4886 * @error: a #GError, or %NULL
4888 * Obtains a file monitor for the given file. If no file notification
4889 * mechanism exists, then regular polling of the file is used.
4891 * If @cancellable is not %NULL, then the operation can be cancelled by
4892 * triggering the cancellable object from another thread. If the operation
4893 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4895 * Returns: (transfer full): a #GFileMonitor for the given @file,
4896 * or %NULL on error.
4897 * Free the returned object with g_object_unref().
4900 g_file_monitor_file (GFile *file,
4901 GFileMonitorFlags flags,
4902 GCancellable *cancellable,
4906 GFileMonitor *monitor;
4908 g_return_val_if_fail (G_IS_FILE (file), NULL);
4910 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4913 iface = G_FILE_GET_IFACE (file);
4917 if (iface->monitor_file)
4918 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4920 /* Fallback to polling */
4921 if (monitor == NULL)
4922 monitor = _g_poll_file_monitor_new (file);
4929 * @file: input #GFile
4930 * @flags: a set of #GFileMonitorFlags
4931 * @cancellable: (allow-none): optional #GCancellable object,
4933 * @error: a #GError, or %NULL
4935 * Obtains a file or directory monitor for the given file,
4936 * depending on the type of the file.
4938 * If @cancellable is not %NULL, then the operation can be cancelled by
4939 * triggering the cancellable object from another thread. If the operation
4940 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4942 * Returns: (transfer full): a #GFileMonitor for the given @file,
4943 * or %NULL on error.
4944 * Free the returned object with g_object_unref().
4949 g_file_monitor (GFile *file,
4950 GFileMonitorFlags flags,
4951 GCancellable *cancellable,
4954 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4955 return g_file_monitor_directory (file, flags, cancellable, error);
4957 return g_file_monitor_file (file, flags, cancellable, error);
4960 /********************************************
4961 * Default implementation of async ops *
4962 ********************************************/
4966 GFileQueryInfoFlags flags;
4968 } QueryInfoAsyncData;
4971 query_info_data_free (QueryInfoAsyncData *data)
4974 g_object_unref (data->info);
4975 g_free (data->attributes);
4980 query_info_async_thread (GSimpleAsyncResult *res,
4982 GCancellable *cancellable)
4984 GError *error = NULL;
4985 QueryInfoAsyncData *data;
4988 data = g_simple_async_result_get_op_res_gpointer (res);
4989 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4992 g_simple_async_result_take_error (res, error);
4998 g_file_real_query_info_async (GFile *file,
4999 const char *attributes,
5000 GFileQueryInfoFlags flags,
5002 GCancellable *cancellable,
5003 GAsyncReadyCallback callback,
5006 GSimpleAsyncResult *res;
5007 QueryInfoAsyncData *data;
5009 data = g_new0 (QueryInfoAsyncData, 1);
5010 data->attributes = g_strdup (attributes);
5011 data->flags = flags;
5013 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
5014 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
5016 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
5017 g_object_unref (res);
5021 g_file_real_query_info_finish (GFile *file,
5025 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5026 QueryInfoAsyncData *data;
5028 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
5030 if (g_simple_async_result_propagate_error (simple, error))
5033 data = g_simple_async_result_get_op_res_gpointer (simple);
5035 return g_object_ref (data->info);
5043 } QueryFilesystemInfoAsyncData;
5046 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
5049 g_object_unref (data->info);
5050 g_free (data->attributes);
5055 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
5057 GCancellable *cancellable)
5059 GError *error = NULL;
5060 QueryFilesystemInfoAsyncData *data;
5063 data = g_simple_async_result_get_op_res_gpointer (res);
5065 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
5068 g_simple_async_result_take_error (res, error);
5074 g_file_real_query_filesystem_info_async (GFile *file,
5075 const char *attributes,
5077 GCancellable *cancellable,
5078 GAsyncReadyCallback callback,
5081 GSimpleAsyncResult *res;
5082 QueryFilesystemInfoAsyncData *data;
5084 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
5085 data->attributes = g_strdup (attributes);
5087 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
5088 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
5090 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
5091 g_object_unref (res);
5095 g_file_real_query_filesystem_info_finish (GFile *file,
5099 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5100 QueryFilesystemInfoAsyncData *data;
5102 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
5104 if (g_simple_async_result_propagate_error (simple, error))
5107 data = g_simple_async_result_get_op_res_gpointer (simple);
5109 return g_object_ref (data->info);
5116 GFileQueryInfoFlags flags;
5117 GFileEnumerator *enumerator;
5118 } EnumerateChildrenAsyncData;
5121 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
5123 if (data->enumerator)
5124 g_object_unref (data->enumerator);
5125 g_free (data->attributes);
5130 enumerate_children_async_thread (GSimpleAsyncResult *res,
5132 GCancellable *cancellable)
5134 GError *error = NULL;
5135 EnumerateChildrenAsyncData *data;
5136 GFileEnumerator *enumerator;
5138 data = g_simple_async_result_get_op_res_gpointer (res);
5140 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5142 if (enumerator == NULL)
5143 g_simple_async_result_take_error (res, error);
5145 data->enumerator = enumerator;
5149 g_file_real_enumerate_children_async (GFile *file,
5150 const char *attributes,
5151 GFileQueryInfoFlags flags,
5153 GCancellable *cancellable,
5154 GAsyncReadyCallback callback,
5157 GSimpleAsyncResult *res;
5158 EnumerateChildrenAsyncData *data;
5160 data = g_new0 (EnumerateChildrenAsyncData, 1);
5161 data->attributes = g_strdup (attributes);
5162 data->flags = flags;
5164 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5165 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5167 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5168 g_object_unref (res);
5171 static GFileEnumerator *
5172 g_file_real_enumerate_children_finish (GFile *file,
5176 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5177 EnumerateChildrenAsyncData *data;
5179 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5181 if (g_simple_async_result_propagate_error (simple, error))
5184 data = g_simple_async_result_get_op_res_gpointer (simple);
5185 if (data->enumerator)
5186 return g_object_ref (data->enumerator);
5192 open_read_async_thread (GSimpleAsyncResult *res,
5194 GCancellable *cancellable)
5197 GFileInputStream *stream;
5198 GError *error = NULL;
5200 iface = G_FILE_GET_IFACE (object);
5202 if (iface->read_fn == NULL)
5204 g_set_error_literal (&error, G_IO_ERROR,
5205 G_IO_ERROR_NOT_SUPPORTED,
5206 _("Operation not supported"));
5208 g_simple_async_result_take_error (res, error);
5213 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5216 g_simple_async_result_take_error (res, error);
5218 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5222 g_file_real_read_async (GFile *file,
5224 GCancellable *cancellable,
5225 GAsyncReadyCallback callback,
5228 GSimpleAsyncResult *res;
5230 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5232 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5233 g_object_unref (res);
5236 static GFileInputStream *
5237 g_file_real_read_finish (GFile *file,
5241 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5244 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5246 if (g_simple_async_result_propagate_error (simple, error))
5249 op = g_simple_async_result_get_op_res_gpointer (simple);
5251 return g_object_ref (op);
5257 append_to_async_thread (GSimpleAsyncResult *res,
5259 GCancellable *cancellable)
5262 GFileCreateFlags *data;
5263 GFileOutputStream *stream;
5264 GError *error = NULL;
5266 iface = G_FILE_GET_IFACE (object);
5268 data = g_simple_async_result_get_op_res_gpointer (res);
5270 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5273 g_simple_async_result_take_error (res, error);
5275 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5279 g_file_real_append_to_async (GFile *file,
5280 GFileCreateFlags flags,
5282 GCancellable *cancellable,
5283 GAsyncReadyCallback callback,
5286 GFileCreateFlags *data;
5287 GSimpleAsyncResult *res;
5289 data = g_new0 (GFileCreateFlags, 1);
5292 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5293 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5295 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5296 g_object_unref (res);
5299 static GFileOutputStream *
5300 g_file_real_append_to_finish (GFile *file,
5304 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5307 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5309 if (g_simple_async_result_propagate_error (simple, error))
5312 op = g_simple_async_result_get_op_res_gpointer (simple);
5314 return g_object_ref (op);
5320 create_async_thread (GSimpleAsyncResult *res,
5322 GCancellable *cancellable)
5325 GFileCreateFlags *data;
5326 GFileOutputStream *stream;
5327 GError *error = NULL;
5329 iface = G_FILE_GET_IFACE (object);
5331 data = g_simple_async_result_get_op_res_gpointer (res);
5333 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5336 g_simple_async_result_take_error (res, error);
5338 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5342 g_file_real_create_async (GFile *file,
5343 GFileCreateFlags flags,
5345 GCancellable *cancellable,
5346 GAsyncReadyCallback callback,
5349 GFileCreateFlags *data;
5350 GSimpleAsyncResult *res;
5352 data = g_new0 (GFileCreateFlags, 1);
5355 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5356 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5358 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5359 g_object_unref (res);
5362 static GFileOutputStream *
5363 g_file_real_create_finish (GFile *file,
5367 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5370 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5372 if (g_simple_async_result_propagate_error (simple, error))
5375 op = g_simple_async_result_get_op_res_gpointer (simple);
5377 return g_object_ref (op);
5383 GFileOutputStream *stream;
5385 gboolean make_backup;
5386 GFileCreateFlags flags;
5390 replace_async_data_free (ReplaceAsyncData *data)
5393 g_object_unref (data->stream);
5394 g_free (data->etag);
5399 replace_async_thread (GSimpleAsyncResult *res,
5401 GCancellable *cancellable)
5404 GFileOutputStream *stream;
5405 GError *error = NULL;
5406 ReplaceAsyncData *data;
5408 iface = G_FILE_GET_IFACE (object);
5410 data = g_simple_async_result_get_op_res_gpointer (res);
5412 stream = iface->replace (G_FILE (object),
5420 g_simple_async_result_take_error (res, error);
5422 data->stream = stream;
5426 g_file_real_replace_async (GFile *file,
5428 gboolean make_backup,
5429 GFileCreateFlags flags,
5431 GCancellable *cancellable,
5432 GAsyncReadyCallback callback,
5435 GSimpleAsyncResult *res;
5436 ReplaceAsyncData *data;
5438 data = g_new0 (ReplaceAsyncData, 1);
5439 data->etag = g_strdup (etag);
5440 data->make_backup = make_backup;
5441 data->flags = flags;
5443 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5444 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5446 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5447 g_object_unref (res);
5450 static GFileOutputStream *
5451 g_file_real_replace_finish (GFile *file,
5455 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5456 ReplaceAsyncData *data;
5458 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5460 if (g_simple_async_result_propagate_error (simple, error))
5463 data = g_simple_async_result_get_op_res_gpointer (simple);
5465 return g_object_ref (data->stream);
5471 delete_async_thread (GSimpleAsyncResult *res,
5473 GCancellable *cancellable)
5476 GError *error = NULL;
5478 iface = G_FILE_GET_IFACE (object);
5480 if (!iface->delete_file (G_FILE (object),
5483 g_simple_async_result_take_error (res, error);
5487 g_file_real_delete_async (GFile *file,
5489 GCancellable *cancellable,
5490 GAsyncReadyCallback callback,
5493 GSimpleAsyncResult *res;
5495 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_delete_async);
5496 g_simple_async_result_run_in_thread (res, delete_async_thread, io_priority, cancellable);
5497 g_object_unref (res);
5501 g_file_real_delete_finish (GFile *file,
5505 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5507 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_delete_async);
5509 if (g_simple_async_result_propagate_error (simple, error))
5516 open_readwrite_async_thread (GSimpleAsyncResult *res,
5518 GCancellable *cancellable)
5521 GFileIOStream *stream;
5522 GError *error = NULL;
5524 iface = G_FILE_GET_IFACE (object);
5526 if (iface->open_readwrite == NULL)
5528 g_set_error_literal (&error, G_IO_ERROR,
5529 G_IO_ERROR_NOT_SUPPORTED,
5530 _("Operation not supported"));
5532 g_simple_async_result_take_error (res, error);
5537 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5540 g_simple_async_result_take_error (res, error);
5542 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5546 g_file_real_open_readwrite_async (GFile *file,
5548 GCancellable *cancellable,
5549 GAsyncReadyCallback callback,
5552 GSimpleAsyncResult *res;
5554 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5556 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5557 g_object_unref (res);
5560 static GFileIOStream *
5561 g_file_real_open_readwrite_finish (GFile *file,
5565 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5568 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5570 if (g_simple_async_result_propagate_error (simple, error))
5573 op = g_simple_async_result_get_op_res_gpointer (simple);
5575 return g_object_ref (op);
5581 create_readwrite_async_thread (GSimpleAsyncResult *res,
5583 GCancellable *cancellable)
5586 GFileCreateFlags *data;
5587 GFileIOStream *stream;
5588 GError *error = NULL;
5590 iface = G_FILE_GET_IFACE (object);
5592 data = g_simple_async_result_get_op_res_gpointer (res);
5594 if (iface->create_readwrite == NULL)
5596 g_set_error_literal (&error, G_IO_ERROR,
5597 G_IO_ERROR_NOT_SUPPORTED,
5598 _("Operation not supported"));
5600 g_simple_async_result_take_error (res, error);
5605 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5608 g_simple_async_result_take_error (res, error);
5610 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5614 g_file_real_create_readwrite_async (GFile *file,
5615 GFileCreateFlags flags,
5617 GCancellable *cancellable,
5618 GAsyncReadyCallback callback,
5621 GFileCreateFlags *data;
5622 GSimpleAsyncResult *res;
5624 data = g_new0 (GFileCreateFlags, 1);
5627 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5628 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5630 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5631 g_object_unref (res);
5634 static GFileIOStream *
5635 g_file_real_create_readwrite_finish (GFile *file,
5639 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5642 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5644 if (g_simple_async_result_propagate_error (simple, error))
5647 op = g_simple_async_result_get_op_res_gpointer (simple);
5649 return g_object_ref (op);
5655 GFileIOStream *stream;
5657 gboolean make_backup;
5658 GFileCreateFlags flags;
5659 } ReplaceRWAsyncData;
5662 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5665 g_object_unref (data->stream);
5666 g_free (data->etag);
5671 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5673 GCancellable *cancellable)
5676 GFileIOStream *stream;
5677 GError *error = NULL;
5678 ReplaceRWAsyncData *data;
5680 iface = G_FILE_GET_IFACE (object);
5682 data = g_simple_async_result_get_op_res_gpointer (res);
5684 stream = iface->replace_readwrite (G_FILE (object),
5692 g_simple_async_result_take_error (res, error);
5694 data->stream = stream;
5698 g_file_real_replace_readwrite_async (GFile *file,
5700 gboolean make_backup,
5701 GFileCreateFlags flags,
5703 GCancellable *cancellable,
5704 GAsyncReadyCallback callback,
5707 GSimpleAsyncResult *res;
5708 ReplaceRWAsyncData *data;
5710 data = g_new0 (ReplaceRWAsyncData, 1);
5711 data->etag = g_strdup (etag);
5712 data->make_backup = make_backup;
5713 data->flags = flags;
5715 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5716 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5718 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5719 g_object_unref (res);
5722 static GFileIOStream *
5723 g_file_real_replace_readwrite_finish (GFile *file,
5727 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5728 ReplaceRWAsyncData *data;
5730 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5732 if (g_simple_async_result_propagate_error (simple, error))
5735 data = g_simple_async_result_get_op_res_gpointer (simple);
5737 return g_object_ref (data->stream);
5745 } SetDisplayNameAsyncData;
5748 set_display_name_data_free (SetDisplayNameAsyncData *data)
5750 g_free (data->name);
5752 g_object_unref (data->file);
5757 set_display_name_async_thread (GSimpleAsyncResult *res,
5759 GCancellable *cancellable)
5761 GError *error = NULL;
5762 SetDisplayNameAsyncData *data;
5765 data = g_simple_async_result_get_op_res_gpointer (res);
5767 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5770 g_simple_async_result_take_error (res, error);
5776 g_file_real_set_display_name_async (GFile *file,
5777 const char *display_name,
5779 GCancellable *cancellable,
5780 GAsyncReadyCallback callback,
5783 GSimpleAsyncResult *res;
5784 SetDisplayNameAsyncData *data;
5786 data = g_new0 (SetDisplayNameAsyncData, 1);
5787 data->name = g_strdup (display_name);
5789 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5790 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5792 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5793 g_object_unref (res);
5797 g_file_real_set_display_name_finish (GFile *file,
5801 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5802 SetDisplayNameAsyncData *data;
5804 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5806 if (g_simple_async_result_propagate_error (simple, error))
5809 data = g_simple_async_result_get_op_res_gpointer (simple);
5811 return g_object_ref (data->file);
5817 GFileQueryInfoFlags flags;
5824 set_info_data_free (SetInfoAsyncData *data)
5827 g_object_unref (data->info);
5829 g_error_free (data->error);
5834 set_info_async_thread (GSimpleAsyncResult *res,
5836 GCancellable *cancellable)
5838 SetInfoAsyncData *data;
5840 data = g_simple_async_result_get_op_res_gpointer (res);
5843 data->res = g_file_set_attributes_from_info (G_FILE (object),
5851 g_file_real_set_attributes_async (GFile *file,
5853 GFileQueryInfoFlags flags,
5855 GCancellable *cancellable,
5856 GAsyncReadyCallback callback,
5859 GSimpleAsyncResult *res;
5860 SetInfoAsyncData *data;
5862 data = g_new0 (SetInfoAsyncData, 1);
5863 data->info = g_file_info_dup (info);
5864 data->flags = flags;
5866 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5867 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5869 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5870 g_object_unref (res);
5874 g_file_real_set_attributes_finish (GFile *file,
5879 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5880 SetInfoAsyncData *data;
5882 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5884 data = g_simple_async_result_get_op_res_gpointer (simple);
5887 *info = g_object_ref (data->info);
5889 if (error != NULL && data->error)
5890 *error = g_error_copy (data->error);
5896 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5898 GCancellable *cancellable)
5900 GError *error = NULL;
5903 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5906 g_simple_async_result_take_error (res, error);
5908 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5912 g_file_real_find_enclosing_mount_async (GFile *file,
5914 GCancellable *cancellable,
5915 GAsyncReadyCallback callback,
5918 GSimpleAsyncResult *res;
5920 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5922 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5923 g_object_unref (res);
5927 g_file_real_find_enclosing_mount_finish (GFile *file,
5931 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5934 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5936 if (g_simple_async_result_propagate_error (simple, error))
5939 mount = g_simple_async_result_get_op_res_gpointer (simple);
5940 return g_object_ref (mount);
5947 GFileCopyFlags flags;
5948 GFileProgressCallback progress_cb;
5949 gpointer progress_cb_data;
5953 copy_async_data_free (CopyAsyncData *data)
5955 g_object_unref (data->source);
5956 g_object_unref (data->destination);
5957 g_slice_free (CopyAsyncData, data);
5961 CopyAsyncData *data;
5962 goffset current_num_bytes;
5963 goffset total_num_bytes;
5967 copy_async_progress_in_main (gpointer user_data)
5969 ProgressData *progress = user_data;
5970 CopyAsyncData *data = progress->data;
5972 data->progress_cb (progress->current_num_bytes,
5973 progress->total_num_bytes,
5974 data->progress_cb_data);
5980 copy_async_progress_callback (goffset current_num_bytes,
5981 goffset total_num_bytes,
5984 GTask *task = user_data;
5985 CopyAsyncData *data = g_task_get_task_data (task);
5986 ProgressData *progress;
5988 progress = g_new (ProgressData, 1);
5989 progress->data = data;
5990 progress->current_num_bytes = current_num_bytes;
5991 progress->total_num_bytes = total_num_bytes;
5993 g_main_context_invoke_full (g_task_get_context (task),
5994 g_task_get_priority (task),
5995 copy_async_progress_in_main,
6001 copy_async_thread (GTask *task,
6004 GCancellable *cancellable)
6006 CopyAsyncData *data = task_data;
6008 GError *error = NULL;
6010 result = g_file_copy (data->source,
6014 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6018 g_task_return_boolean (task, TRUE);
6020 g_task_return_error (task, error);
6024 g_file_real_copy_async (GFile *source,
6026 GFileCopyFlags flags,
6028 GCancellable *cancellable,
6029 GFileProgressCallback progress_callback,
6030 gpointer progress_callback_data,
6031 GAsyncReadyCallback callback,
6035 CopyAsyncData *data;
6037 data = g_slice_new (CopyAsyncData);
6038 data->source = g_object_ref (source);
6039 data->destination = g_object_ref (destination);
6040 data->flags = flags;
6041 data->progress_cb = progress_callback;
6042 data->progress_cb_data = progress_callback_data;
6044 task = g_task_new (source, cancellable, callback, user_data);
6045 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6046 g_task_set_priority (task, io_priority);
6047 g_task_run_in_thread (task, copy_async_thread);
6048 g_object_unref (task);
6052 g_file_real_copy_finish (GFile *file,
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_simple_async_report_error_in_idle (G_OBJECT (location),
6321 callback, user_data,
6322 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6323 _("volume doesn't implement mount"));
6328 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6333 * g_file_mount_enclosing_volume_finish:
6334 * @location: input #GFile
6335 * @result: a #GAsyncResult
6336 * @error: a #GError, or %NULL
6338 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6340 * Returns: %TRUE if successful. If an error has occurred,
6341 * this function will return %FALSE and set @error
6342 * appropriately if present.
6345 g_file_mount_enclosing_volume_finish (GFile *location,
6346 GAsyncResult *result,
6351 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6352 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6354 if (g_async_result_legacy_propagate_error (result, error))
6357 iface = G_FILE_GET_IFACE (location);
6359 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6362 /********************************************
6363 * Utility functions *
6364 ********************************************/
6367 * g_file_query_default_handler:
6368 * @file: a #GFile to open
6369 * @cancellable: optional #GCancellable object, %NULL to ignore
6370 * @error: a #GError, or %NULL
6372 * Returns the #GAppInfo that is registered as the default
6373 * application to handle the file specified by @file.
6375 * If @cancellable is not %NULL, then the operation can be cancelled by
6376 * triggering the cancellable object from another thread. If the operation
6377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6379 * Returns: (transfer full): a #GAppInfo if the handle was found,
6380 * %NULL if there were errors.
6381 * When you are done with it, release it with g_object_unref()
6384 g_file_query_default_handler (GFile *file,
6385 GCancellable *cancellable,
6389 const char *content_type;
6394 uri_scheme = g_file_get_uri_scheme (file);
6395 if (uri_scheme && uri_scheme[0] != '\0')
6397 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6398 g_free (uri_scheme);
6400 if (appinfo != NULL)
6404 info = g_file_query_info (file,
6405 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6414 content_type = g_file_info_get_content_type (info);
6417 /* Don't use is_native(), as we want to support fuse paths if available */
6418 path = g_file_get_path (file);
6419 appinfo = g_app_info_get_default_for_type (content_type,
6424 g_object_unref (info);
6426 if (appinfo != NULL)
6429 g_set_error_literal (error, G_IO_ERROR,
6430 G_IO_ERROR_NOT_SUPPORTED,
6431 _("No application is registered as handling this file"));
6435 #define GET_CONTENT_BLOCK_SIZE 8192
6438 * g_file_load_contents:
6439 * @file: input #GFile
6440 * @cancellable: optional #GCancellable object, %NULL to ignore
6441 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6442 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6443 * or %NULL if the length is not needed
6444 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6445 * or %NULL if the entity tag is not needed
6446 * @error: a #GError, or %NULL
6448 * Loads the content of the file into memory. The data is always
6449 * zero-terminated, but this is not included in the resultant @length.
6450 * The returned @content should be freed with g_free() when no longer
6453 * If @cancellable is not %NULL, then the operation can be cancelled by
6454 * triggering the cancellable object from another thread. If the operation
6455 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6457 * Returns: %TRUE if the @file's contents were successfully loaded.
6458 * %FALSE if there were errors.
6461 g_file_load_contents (GFile *file,
6462 GCancellable *cancellable,
6468 GFileInputStream *in;
6469 GByteArray *content;
6474 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6475 g_return_val_if_fail (contents != NULL, FALSE);
6477 in = g_file_read (file, cancellable, error);
6481 content = g_byte_array_new ();
6484 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6485 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6486 content->data + pos,
6487 GET_CONTENT_BLOCK_SIZE,
6488 cancellable, error)) > 0)
6491 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6498 info = g_file_input_stream_query_info (in,
6499 G_FILE_ATTRIBUTE_ETAG_VALUE,
6504 *etag_out = g_strdup (g_file_info_get_etag (info));
6505 g_object_unref (info);
6509 /* Ignore errors on close */
6510 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6511 g_object_unref (in);
6515 /* error is set already */
6516 g_byte_array_free (content, TRUE);
6523 /* Zero terminate (we got an extra byte allocated for this */
6524 content->data[pos] = 0;
6526 *contents = (char *)g_byte_array_free (content, FALSE);
6534 GCancellable *cancellable;
6535 GFileReadMoreCallback read_more_callback;
6536 GAsyncReadyCallback callback;
6538 GByteArray *content;
6545 load_contents_data_free (LoadContentsData *data)
6548 g_error_free (data->error);
6549 if (data->cancellable)
6550 g_object_unref (data->cancellable);
6552 g_byte_array_free (data->content, TRUE);
6553 g_free (data->etag);
6554 g_object_unref (data->file);
6559 load_contents_close_callback (GObject *obj,
6560 GAsyncResult *close_res,
6563 GInputStream *stream = G_INPUT_STREAM (obj);
6564 LoadContentsData *data = user_data;
6565 GSimpleAsyncResult *res;
6567 /* Ignore errors here, we're only reading anyway */
6568 g_input_stream_close_finish (stream, close_res, NULL);
6569 g_object_unref (stream);
6571 res = g_simple_async_result_new (G_OBJECT (data->file),
6574 g_file_load_contents_async);
6575 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6576 g_simple_async_result_complete (res);
6577 g_object_unref (res);
6581 load_contents_fstat_callback (GObject *obj,
6582 GAsyncResult *stat_res,
6585 GInputStream *stream = G_INPUT_STREAM (obj);
6586 LoadContentsData *data = user_data;
6589 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6593 data->etag = g_strdup (g_file_info_get_etag (info));
6594 g_object_unref (info);
6597 g_input_stream_close_async (stream, 0,
6599 load_contents_close_callback, data);
6603 load_contents_read_callback (GObject *obj,
6604 GAsyncResult *read_res,
6607 GInputStream *stream = G_INPUT_STREAM (obj);
6608 LoadContentsData *data = user_data;
6609 GError *error = NULL;
6612 read_size = g_input_stream_read_finish (stream, read_res, &error);
6616 /* Error or EOF, close the file */
6617 data->error = error;
6618 g_input_stream_close_async (stream, 0,
6620 load_contents_close_callback, data);
6622 else if (read_size == 0)
6624 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6625 G_FILE_ATTRIBUTE_ETAG_VALUE,
6628 load_contents_fstat_callback,
6631 else if (read_size > 0)
6633 data->pos += read_size;
6635 g_byte_array_set_size (data->content,
6636 data->pos + GET_CONTENT_BLOCK_SIZE);
6639 if (data->read_more_callback &&
6640 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6641 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6642 G_FILE_ATTRIBUTE_ETAG_VALUE,
6645 load_contents_fstat_callback,
6648 g_input_stream_read_async (stream,
6649 data->content->data + data->pos,
6650 GET_CONTENT_BLOCK_SIZE,
6653 load_contents_read_callback,
6659 load_contents_open_callback (GObject *obj,
6660 GAsyncResult *open_res,
6663 GFile *file = G_FILE (obj);
6664 GFileInputStream *stream;
6665 LoadContentsData *data = user_data;
6666 GError *error = NULL;
6667 GSimpleAsyncResult *res;
6669 stream = g_file_read_finish (file, open_res, &error);
6673 g_byte_array_set_size (data->content,
6674 data->pos + GET_CONTENT_BLOCK_SIZE);
6675 g_input_stream_read_async (G_INPUT_STREAM (stream),
6676 data->content->data + data->pos,
6677 GET_CONTENT_BLOCK_SIZE,
6680 load_contents_read_callback,
6685 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6689 g_simple_async_result_complete (res);
6690 load_contents_data_free (data);
6691 g_object_unref (res);
6696 * g_file_load_partial_contents_async: (skip)
6697 * @file: input #GFile
6698 * @cancellable: optional #GCancellable object, %NULL to ignore
6699 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6700 * and to specify whether further data should be read
6701 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6702 * @user_data: the data to pass to the callback functions
6704 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6705 * be used to stop reading from the file when appropriate, else this
6706 * function will behave exactly as g_file_load_contents_async(). This
6707 * operation can be finished by g_file_load_partial_contents_finish().
6709 * Users of this function should be aware that @user_data is passed to
6710 * both the @read_more_callback and the @callback.
6712 * If @cancellable is not %NULL, then the operation can be cancelled by
6713 * triggering the cancellable object from another thread. If the operation
6714 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6717 g_file_load_partial_contents_async (GFile *file,
6718 GCancellable *cancellable,
6719 GFileReadMoreCallback read_more_callback,
6720 GAsyncReadyCallback callback,
6723 LoadContentsData *data;
6725 g_return_if_fail (G_IS_FILE (file));
6727 data = g_new0 (LoadContentsData, 1);
6730 data->cancellable = g_object_ref (cancellable);
6731 data->read_more_callback = read_more_callback;
6732 data->callback = callback;
6733 data->user_data = user_data;
6734 data->content = g_byte_array_new ();
6735 data->file = g_object_ref (file);
6737 g_file_read_async (file,
6740 load_contents_open_callback,
6745 * g_file_load_partial_contents_finish:
6746 * @file: input #GFile
6747 * @res: a #GAsyncResult
6748 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6749 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6750 * or %NULL if the length is not needed
6751 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6752 * or %NULL if the entity tag is not needed
6753 * @error: a #GError, or %NULL
6755 * Finishes an asynchronous partial load operation that was started
6756 * with g_file_load_partial_contents_async(). The data is always
6757 * zero-terminated, but this is not included in the resultant @length.
6758 * The returned @content should be freed with g_free() when no longer
6761 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6762 * present, it will be set appropriately.
6765 g_file_load_partial_contents_finish (GFile *file,
6772 GSimpleAsyncResult *simple;
6773 LoadContentsData *data;
6775 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6776 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6777 g_return_val_if_fail (contents != NULL, FALSE);
6779 simple = G_SIMPLE_ASYNC_RESULT (res);
6781 if (g_simple_async_result_propagate_error (simple, error))
6784 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6786 data = g_simple_async_result_get_op_res_gpointer (simple);
6790 g_propagate_error (error, data->error);
6799 *length = data->pos;
6803 *etag_out = data->etag;
6807 /* Zero terminate */
6808 g_byte_array_set_size (data->content, data->pos + 1);
6809 data->content->data[data->pos] = 0;
6811 *contents = (char *)g_byte_array_free (data->content, FALSE);
6812 data->content = NULL;
6818 * g_file_load_contents_async:
6819 * @file: input #GFile
6820 * @cancellable: optional #GCancellable object, %NULL to ignore
6821 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6822 * @user_data: the data to pass to callback function
6824 * Starts an asynchronous load of the @file's contents.
6826 * For more details, see g_file_load_contents() which is
6827 * the synchronous version of this call.
6829 * When the load operation has completed, @callback will be called
6830 * with @user data. To finish the operation, call
6831 * g_file_load_contents_finish() with the #GAsyncResult returned by
6834 * If @cancellable is not %NULL, then the operation can be cancelled by
6835 * triggering the cancellable object from another thread. If the operation
6836 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6839 g_file_load_contents_async (GFile *file,
6840 GCancellable *cancellable,
6841 GAsyncReadyCallback callback,
6844 g_file_load_partial_contents_async (file,
6847 callback, user_data);
6851 * g_file_load_contents_finish:
6852 * @file: input #GFile
6853 * @res: a #GAsyncResult
6854 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6855 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6856 * or %NULL if the length is not needed
6857 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6858 * or %NULL if the entity tag is not needed
6859 * @error: a #GError, or %NULL
6861 * Finishes an asynchronous load of the @file's contents.
6862 * The contents are placed in @contents, and @length is set to the
6863 * size of the @contents string. The @content should be freed with
6864 * g_free() when no longer needed. If @etag_out is present, it will be
6865 * set to the new entity tag for the @file.
6867 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6868 * present, it will be set appropriately.
6871 g_file_load_contents_finish (GFile *file,
6878 return g_file_load_partial_contents_finish (file,
6887 * g_file_replace_contents:
6888 * @file: input #GFile
6889 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6890 * @length: the length of @contents in bytes
6891 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6892 * for the document, or %NULL
6893 * @make_backup: %TRUE if a backup should be created
6894 * @flags: a set of #GFileCreateFlags
6895 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6896 * for the document. This should be freed with g_free() when no longer
6898 * @cancellable: optional #GCancellable object, %NULL to ignore
6899 * @error: a #GError, or %NULL
6901 * Replaces the contents of @file with @contents of @length bytes.
6903 * If @etag is specified (not %NULL), any existing file must have that etag,
6904 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6906 * If @make_backup is %TRUE, this function will attempt to make a backup
6909 * If @cancellable is not %NULL, then the operation can be cancelled by
6910 * triggering the cancellable object from another thread. If the operation
6911 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6913 * The returned @new_etag can be used to verify that the file hasn't
6914 * changed the next time it is saved over.
6916 * Returns: %TRUE if successful. If an error has occurred, this function
6917 * will return %FALSE and set @error appropriately if present.
6920 g_file_replace_contents (GFile *file,
6921 const char *contents,
6924 gboolean make_backup,
6925 GFileCreateFlags flags,
6927 GCancellable *cancellable,
6930 GFileOutputStream *out;
6931 gsize pos, remainder;
6935 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6936 g_return_val_if_fail (contents != NULL, FALSE);
6938 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6944 while (remainder > 0 &&
6945 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6947 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6955 if (remainder > 0 && res < 0)
6957 /* Ignore errors on close */
6958 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6959 g_object_unref (out);
6961 /* error is set already */
6965 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6968 *new_etag = g_file_output_stream_get_etag (out);
6970 g_object_unref (out);
6978 GCancellable *cancellable;
6979 GAsyncReadyCallback callback;
6981 const char *content;
6985 } ReplaceContentsData;
6988 replace_contents_data_free (ReplaceContentsData *data)
6991 g_error_free (data->error);
6992 if (data->cancellable)
6993 g_object_unref (data->cancellable);
6994 g_object_unref (data->file);
6995 g_free (data->etag);
7000 replace_contents_close_callback (GObject *obj,
7001 GAsyncResult *close_res,
7004 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7005 ReplaceContentsData *data = user_data;
7006 GSimpleAsyncResult *res;
7008 /* Ignore errors here, we're only reading anyway */
7009 g_output_stream_close_finish (stream, close_res, NULL);
7010 g_object_unref (stream);
7012 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7014 res = g_simple_async_result_new (G_OBJECT (data->file),
7017 g_file_replace_contents_async);
7018 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
7019 g_simple_async_result_complete (res);
7020 g_object_unref (res);
7024 replace_contents_write_callback (GObject *obj,
7025 GAsyncResult *read_res,
7028 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7029 ReplaceContentsData *data = user_data;
7030 GError *error = NULL;
7033 write_size = g_output_stream_write_finish (stream, read_res, &error);
7035 if (write_size <= 0)
7037 /* Error or EOF, close the file */
7039 data->error = error;
7040 g_output_stream_close_async (stream, 0,
7042 replace_contents_close_callback, data);
7044 else if (write_size > 0)
7046 data->pos += write_size;
7048 if (data->pos >= data->length)
7049 g_output_stream_close_async (stream, 0,
7051 replace_contents_close_callback, data);
7053 g_output_stream_write_async (stream,
7054 data->content + data->pos,
7055 data->length - data->pos,
7058 replace_contents_write_callback,
7064 replace_contents_open_callback (GObject *obj,
7065 GAsyncResult *open_res,
7068 GFile *file = G_FILE (obj);
7069 GFileOutputStream *stream;
7070 ReplaceContentsData *data = user_data;
7071 GError *error = NULL;
7072 GSimpleAsyncResult *res;
7074 stream = g_file_replace_finish (file, open_res, &error);
7078 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7079 data->content + data->pos,
7080 data->length - data->pos,
7083 replace_contents_write_callback,
7088 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
7092 g_simple_async_result_complete (res);
7093 replace_contents_data_free (data);
7094 g_object_unref (res);
7099 * g_file_replace_contents_async:
7100 * @file: input #GFile
7101 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7102 * @length: the length of @contents in bytes
7103 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7104 * @make_backup: %TRUE if a backup should be created
7105 * @flags: a set of #GFileCreateFlags
7106 * @cancellable: optional #GCancellable object, %NULL to ignore
7107 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7108 * @user_data: the data to pass to callback function
7110 * Starts an asynchronous replacement of @file with the given
7111 * @contents of @length bytes. @etag will replace the document's
7112 * current entity tag.
7114 * When this operation has completed, @callback will be called with
7115 * @user_user data, and the operation can be finalized with
7116 * g_file_replace_contents_finish().
7118 * If @cancellable is not %NULL, then the operation can be cancelled by
7119 * triggering the cancellable object from another thread. If the operation
7120 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7122 * If @make_backup is %TRUE, this function will attempt to
7123 * make a backup of @file.
7126 g_file_replace_contents_async (GFile *file,
7127 const char *contents,
7130 gboolean make_backup,
7131 GFileCreateFlags flags,
7132 GCancellable *cancellable,
7133 GAsyncReadyCallback callback,
7136 ReplaceContentsData *data;
7138 g_return_if_fail (G_IS_FILE (file));
7139 g_return_if_fail (contents != NULL);
7141 data = g_new0 (ReplaceContentsData, 1);
7144 data->cancellable = g_object_ref (cancellable);
7145 data->callback = callback;
7146 data->user_data = user_data;
7147 data->content = contents;
7148 data->length = length;
7150 data->file = g_object_ref (file);
7152 g_file_replace_async (file,
7158 replace_contents_open_callback,
7163 * g_file_replace_contents_finish:
7164 * @file: input #GFile
7165 * @res: a #GAsyncResult
7166 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7167 * for the document. This should be freed with g_free() when it is no
7168 * longer needed, or %NULL
7169 * @error: a #GError, or %NULL
7171 * Finishes an asynchronous replace of the given @file. See
7172 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7173 * tag for the document, if present.
7175 * Returns: %TRUE on success, %FALSE on failure.
7178 g_file_replace_contents_finish (GFile *file,
7183 GSimpleAsyncResult *simple;
7184 ReplaceContentsData *data;
7186 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7187 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
7189 simple = G_SIMPLE_ASYNC_RESULT (res);
7191 if (g_simple_async_result_propagate_error (simple, error))
7194 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
7196 data = g_simple_async_result_get_op_res_gpointer (simple);
7200 g_propagate_error (error, data->error);
7207 *new_etag = data->etag;
7208 data->etag = NULL; /* Take ownership */
7215 * g_file_start_mountable:
7216 * @file: input #GFile
7217 * @flags: flags affecting the operation
7218 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7219 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7220 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7221 * @user_data: the data to pass to callback function
7223 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7224 * Using @start_operation, you can request callbacks when, for instance,
7225 * passwords are needed during authentication.
7227 * If @cancellable is not %NULL, then the operation can be cancelled by
7228 * triggering the cancellable object from another thread. If the operation
7229 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7231 * When the operation is finished, @callback will be called.
7232 * You can then call g_file_mount_mountable_finish() to get
7233 * the result of the operation.
7238 g_file_start_mountable (GFile *file,
7239 GDriveStartFlags flags,
7240 GMountOperation *start_operation,
7241 GCancellable *cancellable,
7242 GAsyncReadyCallback callback,
7247 g_return_if_fail (G_IS_FILE (file));
7249 iface = G_FILE_GET_IFACE (file);
7251 if (iface->start_mountable == NULL)
7253 g_simple_async_report_error_in_idle (G_OBJECT (file),
7257 G_IO_ERROR_NOT_SUPPORTED,
7258 _("Operation not supported"));
7262 (* iface->start_mountable) (file,
7271 * g_file_start_mountable_finish:
7272 * @file: input #GFile
7273 * @result: a #GAsyncResult
7274 * @error: a #GError, or %NULL
7276 * Finishes a start operation. See g_file_start_mountable() for details.
7278 * Finish an asynchronous start operation that was started
7279 * with g_file_start_mountable().
7281 * Returns: %TRUE if the operation finished successfully. %FALSE
7287 g_file_start_mountable_finish (GFile *file,
7288 GAsyncResult *result,
7293 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7294 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7296 if (g_async_result_legacy_propagate_error (result, error))
7299 iface = G_FILE_GET_IFACE (file);
7300 return (* iface->start_mountable_finish) (file, result, error);
7304 * g_file_stop_mountable:
7305 * @file: input #GFile
7306 * @flags: flags affecting the operation
7307 * @mount_operation: (allow-none): a #GMountOperation,
7308 * or %NULL to avoid user interaction.
7309 * @cancellable: (allow-none): optional #GCancellable object,
7311 * @callback: (allow-none): a #GAsyncReadyCallback to call
7312 * when the request is satisfied, or %NULL
7313 * @user_data: the data to pass to callback function
7315 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7317 * If @cancellable is not %NULL, then the operation can be cancelled by
7318 * triggering the cancellable object from another thread. If the operation
7319 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7321 * When the operation is finished, @callback will be called.
7322 * You can then call g_file_stop_mountable_finish() to get
7323 * the result of the operation.
7328 g_file_stop_mountable (GFile *file,
7329 GMountUnmountFlags flags,
7330 GMountOperation *mount_operation,
7331 GCancellable *cancellable,
7332 GAsyncReadyCallback callback,
7337 g_return_if_fail (G_IS_FILE (file));
7339 iface = G_FILE_GET_IFACE (file);
7341 if (iface->stop_mountable == NULL)
7343 g_simple_async_report_error_in_idle (G_OBJECT (file),
7347 G_IO_ERROR_NOT_SUPPORTED,
7348 _("Operation not supported"));
7352 (* iface->stop_mountable) (file,
7361 * g_file_stop_mountable_finish:
7362 * @file: input #GFile
7363 * @result: a #GAsyncResult
7364 * @error: a #GError, or %NULL
7366 * Finishes an stop operation, see g_file_stop_mountable() for details.
7368 * Finish an asynchronous stop operation that was started
7369 * with g_file_stop_mountable().
7371 * Returns: %TRUE if the operation finished successfully.
7377 g_file_stop_mountable_finish (GFile *file,
7378 GAsyncResult *result,
7383 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7384 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7386 if (g_async_result_legacy_propagate_error (result, error))
7389 iface = G_FILE_GET_IFACE (file);
7390 return (* iface->stop_mountable_finish) (file, result, error);
7394 * g_file_poll_mountable:
7395 * @file: input #GFile
7396 * @cancellable: optional #GCancellable object, %NULL to ignore
7397 * @callback: (allow-none): a #GAsyncReadyCallback to call
7398 * when the request is satisfied, or %NULL
7399 * @user_data: the data to pass to callback function
7401 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7403 * If @cancellable is not %NULL, then the operation can be cancelled by
7404 * triggering the cancellable object from another thread. If the operation
7405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7407 * When the operation is finished, @callback will be called.
7408 * You can then call g_file_mount_mountable_finish() to get
7409 * the result of the operation.
7414 g_file_poll_mountable (GFile *file,
7415 GCancellable *cancellable,
7416 GAsyncReadyCallback callback,
7421 g_return_if_fail (G_IS_FILE (file));
7423 iface = G_FILE_GET_IFACE (file);
7425 if (iface->poll_mountable == NULL)
7427 g_simple_async_report_error_in_idle (G_OBJECT (file),
7431 G_IO_ERROR_NOT_SUPPORTED,
7432 _("Operation not supported"));
7436 (* iface->poll_mountable) (file,
7443 * g_file_poll_mountable_finish:
7444 * @file: input #GFile
7445 * @result: a #GAsyncResult
7446 * @error: a #GError, or %NULL
7448 * Finishes a poll operation. See g_file_poll_mountable() for details.
7450 * Finish an asynchronous poll operation that was polled
7451 * with g_file_poll_mountable().
7453 * Returns: %TRUE if the operation finished successfully. %FALSE
7459 g_file_poll_mountable_finish (GFile *file,
7460 GAsyncResult *result,
7465 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7466 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7468 if (g_async_result_legacy_propagate_error (result, error))
7471 iface = G_FILE_GET_IFACE (file);
7472 return (* iface->poll_mountable_finish) (file, result, error);
7476 * g_file_supports_thread_contexts:
7479 * Checks if @file supports <link
7480 * linkend="g-main-context-push-thread-default-context">thread-default
7481 * contexts</link>. If this returns %FALSE, you cannot perform
7482 * asynchronous operations on @file in a thread that has a
7483 * thread-default context.
7485 * Returns: Whether or not @file supports thread-default contexts.
7490 g_file_supports_thread_contexts (GFile *file)
7494 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7496 iface = G_FILE_GET_IFACE (file);
7497 return iface->supports_thread_contexts;