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_FILE_ERROR_NOTDIR
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 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2539 g_propagate_error (error, my_error);
2542 g_clear_error (&my_error);
2545 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2546 _("Can't recursively copy directory"));
2550 g_propagate_error (error, my_error);
2555 should_copy (GFileAttributeInfo *info,
2557 gboolean skip_perms)
2559 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2563 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2564 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2568 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2569 GFileAttributeInfoList *namespaces,
2571 gboolean skip_perms)
2578 s = g_string_new ("");
2582 for (i = 0; i < attributes->n_infos; i++)
2584 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2589 g_string_append_c (s, ',');
2591 g_string_append (s, attributes->infos[i].name);
2598 for (i = 0; i < namespaces->n_infos; i++)
2600 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2605 g_string_append_c (s, ',');
2607 g_string_append (s, namespaces->infos[i].name);
2608 g_string_append (s, "::*");
2613 return g_string_free (s, FALSE);
2617 * g_file_copy_attributes:
2618 * @source: a #GFile with attributes
2619 * @destination: a #GFile to copy attributes to
2620 * @flags: a set of #GFileCopyFlags
2621 * @cancellable: (allow-none): optional #GCancellable object,
2623 * @error: a #GError, %NULL to ignore
2625 * Copies the file attributes from @source to @destination.
2627 * Normally only a subset of the file attributes are copied,
2628 * those that are copies in a normal file copy operation
2629 * (which for instance does not include e.g. owner). However
2630 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2631 * all the metadata that is possible to copy is copied. This
2632 * is useful when implementing move by copy + delete source.
2634 * Returns: %TRUE if the attributes were copied successfully,
2638 g_file_copy_attributes (GFile *source,
2640 GFileCopyFlags flags,
2641 GCancellable *cancellable,
2644 GFileAttributeInfoList *attributes, *namespaces;
2645 char *attrs_to_read;
2649 gboolean source_nofollow_symlinks;
2650 gboolean skip_perms;
2652 as_move = flags & G_FILE_COPY_ALL_METADATA;
2653 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2654 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2656 /* Ignore errors here, if the target supports no attributes there is
2659 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2660 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2662 if (attributes == NULL && namespaces == NULL)
2665 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2667 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2668 * we just don't copy it.
2670 info = g_file_query_info (source, attrs_to_read,
2671 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2675 g_free (attrs_to_read);
2680 res = g_file_set_attributes_from_info (destination,
2682 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2685 g_object_unref (info);
2688 g_file_attribute_info_list_unref (attributes);
2689 g_file_attribute_info_list_unref (namespaces);
2695 copy_stream_with_progress (GInputStream *in,
2698 GCancellable *cancellable,
2699 GFileProgressCallback progress_callback,
2700 gpointer progress_callback_data,
2703 gssize n_read, n_written;
2704 goffset current_size;
2705 char buffer[1024*64], *p;
2711 /* avoid performance impact of querying total size when it's not needed */
2712 if (progress_callback)
2714 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2715 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2719 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2720 total_size = g_file_info_get_size (info);
2721 g_object_unref (info);
2724 if (total_size == -1)
2726 info = g_file_query_info (source,
2727 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2728 G_FILE_QUERY_INFO_NONE,
2732 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2733 total_size = g_file_info_get_size (info);
2734 g_object_unref (info);
2739 if (total_size == -1)
2746 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2756 current_size += n_read;
2761 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2762 if (n_written == -1)
2769 n_read -= n_written;
2775 if (progress_callback)
2776 progress_callback (current_size, total_size, progress_callback_data);
2779 /* Make sure we send full copied size */
2780 if (progress_callback)
2781 progress_callback (current_size, total_size, progress_callback_data);
2789 do_splice (int fd_in,
2794 long *bytes_transferd,
2800 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2808 else if (errsv == ENOSYS || errsv == EINVAL)
2809 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2810 _("Splice not supported"));
2812 g_set_error (error, G_IO_ERROR,
2813 g_io_error_from_errno (errsv),
2814 _("Error splicing file: %s"),
2815 g_strerror (errsv));
2820 *bytes_transferd = result;
2825 splice_stream_with_progress (GInputStream *in,
2827 GCancellable *cancellable,
2828 GFileProgressCallback progress_callback,
2829 gpointer progress_callback_data,
2839 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2840 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2842 if (pipe (buffer) != 0)
2844 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2845 "Pipe creation failed");
2850 /* avoid performance impact of querying total size when it's not needed */
2851 if (progress_callback)
2855 if (fstat (fd_in, &sbuf) == 0)
2856 total_size = sbuf.st_size;
2859 if (total_size == -1)
2862 offset_in = offset_out = 0;
2869 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2872 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2883 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2886 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2889 n_read -= n_written;
2892 if (progress_callback)
2893 progress_callback (offset_in, total_size, progress_callback_data);
2896 /* Make sure we send full copied size */
2897 if (progress_callback)
2898 progress_callback (offset_in, total_size, progress_callback_data);
2909 file_copy_fallback (GFile *source,
2911 GFileCopyFlags flags,
2912 GCancellable *cancellable,
2913 GFileProgressCallback progress_callback,
2914 gpointer progress_callback_data,
2923 gboolean fallback = TRUE;
2926 /* need to know the file type */
2927 info = g_file_query_info (source,
2928 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2929 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2936 /* Maybe copy the symlink? */
2937 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2938 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2940 target = g_file_info_get_symlink_target (info);
2943 if (!copy_symlink (destination, flags, cancellable, target, error))
2945 g_object_unref (info);
2949 g_object_unref (info);
2952 /* ... else fall back on a regular file copy */
2953 g_object_unref (info);
2955 /* Handle "special" files (pipes, device nodes, ...)? */
2956 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2958 /* FIXME: could try to recreate device nodes and others? */
2959 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2960 _("Can't copy special file"));
2961 g_object_unref (info);
2964 /* Everything else should just fall back on a regular copy. */
2966 g_object_unref (info);
2968 in = open_source_for_copy (source, destination, flags, cancellable, error);
2972 if (flags & G_FILE_COPY_OVERWRITE)
2974 out = (GOutputStream *)g_file_replace (destination,
2976 flags & G_FILE_COPY_BACKUP,
2977 G_FILE_CREATE_REPLACE_DESTINATION,
2978 cancellable, error);
2982 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2987 g_object_unref (in);
2992 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2994 GError *splice_err = NULL;
2996 result = splice_stream_with_progress (in, out, cancellable,
2997 progress_callback, progress_callback_data,
3000 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3004 g_propagate_error (error, splice_err);
3007 g_clear_error (&splice_err);
3012 result = copy_stream_with_progress (in, out, source, cancellable,
3013 progress_callback, progress_callback_data,
3016 /* Don't care about errors in source here */
3017 g_input_stream_close (in, cancellable, NULL);
3019 /* But write errors on close are bad! */
3020 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
3023 g_object_unref (in);
3024 g_object_unref (out);
3026 if (result == FALSE)
3030 /* Ignore errors here. Failure to copy metadata is not a hard error */
3031 g_file_copy_attributes (source, destination,
3032 flags, cancellable, NULL);
3039 * @source: input #GFile
3040 * @destination: destination #GFile
3041 * @flags: set of #GFileCopyFlags
3042 * @cancellable: (allow-none): optional #GCancellable object,
3044 * @progress_callback: (allow-none) (scope call): function to callback with
3045 * progress information, or %NULL if progress information is not needed
3046 * @progress_callback_data: (closure): user data to pass to @progress_callback
3047 * @error: #GError to set on error, or %NULL
3049 * Copies the file @source to the location specified by @destination.
3050 * Can not handle recursive copies of directories.
3052 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3053 * existing @destination file is overwritten.
3055 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3056 * will be copied as symlinks, otherwise the target of the
3057 * @source symlink will be copied.
3059 * If @cancellable is not %NULL, then the operation can be cancelled by
3060 * triggering the cancellable object from another thread. If the operation
3061 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3063 * If @progress_callback is not %NULL, then the operation can be monitored
3064 * by setting this to a #GFileProgressCallback function.
3065 * @progress_callback_data will be passed to this function. It is guaranteed
3066 * that this callback will be called after all data has been transferred with
3067 * the total number of bytes copied during the operation.
3069 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3070 * is returned, independent on the status of the @destination.
3072 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3073 * the error %G_IO_ERROR_EXISTS is returned.
3075 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3076 * error is returned. If trying to overwrite a directory with a directory the
3077 * %G_IO_ERROR_WOULD_MERGE error is returned.
3079 * If the source is a directory and the target does not exist, or
3080 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3081 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3083 * If you are interested in copying the #GFile object itself (not the on-disk
3084 * file), see g_file_dup().
3086 * Returns: %TRUE on success, %FALSE otherwise.
3089 g_file_copy (GFile *source,
3091 GFileCopyFlags flags,
3092 GCancellable *cancellable,
3093 GFileProgressCallback progress_callback,
3094 gpointer progress_callback_data,
3101 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3102 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3104 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3107 iface = G_FILE_GET_IFACE (destination);
3111 res = (* iface->copy) (source, destination,
3113 progress_callback, progress_callback_data,
3119 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3121 g_propagate_error (error, my_error);
3125 g_clear_error (&my_error);
3128 /* If the types are different, and the destination method failed
3129 * also try the source method
3131 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3133 iface = G_FILE_GET_IFACE (source);
3138 res = (* iface->copy) (source, destination,
3140 progress_callback, progress_callback_data,
3146 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3148 g_propagate_error (error, my_error);
3152 g_clear_error (&my_error);
3156 return file_copy_fallback (source, destination, flags, cancellable,
3157 progress_callback, progress_callback_data,
3162 * g_file_copy_async: (skip)
3163 * @source: input #GFile
3164 * @destination: destination #GFile
3165 * @flags: set of #GFileCopyFlags
3166 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3168 * @cancellable: (allow-none): optional #GCancellable object,
3170 * @progress_callback: (allow-none): function to callback with progress
3171 * information, or %NULL if progress information is not needed
3172 * @progress_callback_data: (closure): user data to pass to @progress_callback
3173 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3174 * @user_data: the data to pass to callback function
3176 * Copies the file @source to the location specified by @destination
3177 * asynchronously. For details of the behaviour, see g_file_copy().
3179 * If @progress_callback is not %NULL, then that function that will be called
3180 * just like in g_file_copy(), however the callback will run in the main loop,
3181 * not in the thread that is doing the I/O operation.
3183 * When the operation is finished, @callback will be called. You can then call
3184 * g_file_copy_finish() to get the result of the operation.
3187 g_file_copy_async (GFile *source,
3189 GFileCopyFlags flags,
3191 GCancellable *cancellable,
3192 GFileProgressCallback progress_callback,
3193 gpointer progress_callback_data,
3194 GAsyncReadyCallback callback,
3199 g_return_if_fail (G_IS_FILE (source));
3200 g_return_if_fail (G_IS_FILE (destination));
3202 iface = G_FILE_GET_IFACE (source);
3203 (* iface->copy_async) (source,
3209 progress_callback_data,
3215 * g_file_copy_finish:
3216 * @file: input #GFile
3217 * @res: a #GAsyncResult
3218 * @error: a #GError, or %NULL
3220 * Finishes copying the file started with g_file_copy_async().
3222 * Returns: a %TRUE on success, %FALSE on error.
3225 g_file_copy_finish (GFile *file,
3231 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3232 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3234 if (g_async_result_legacy_propagate_error (res, error))
3237 iface = G_FILE_GET_IFACE (file);
3238 return (* iface->copy_finish) (file, res, error);
3243 * @source: #GFile pointing to the source location
3244 * @destination: #GFile pointing to the destination location
3245 * @flags: set of #GFileCopyFlags
3246 * @cancellable: (allow-none): optional #GCancellable object,
3248 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3249 * function for updates
3250 * @progress_callback_data: (closure): gpointer to user data for
3251 * the callback function
3252 * @error: #GError for returning error conditions, or %NULL
3254 * Tries to move the file or directory @source to the location specified
3255 * by @destination. If native move operations are supported then this is
3256 * used, otherwise a copy + delete fallback is used. The native
3257 * implementation may support moving directories (for instance on moves
3258 * inside the same filesystem), but the fallback code does not.
3260 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3261 * existing @destination file is overwritten.
3263 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3264 * will be copied as symlinks, otherwise the target of the
3265 * @source symlink will be copied.
3267 * If @cancellable is not %NULL, then the operation can be cancelled by
3268 * triggering the cancellable object from another thread. If the operation
3269 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3271 * If @progress_callback is not %NULL, then the operation can be monitored
3272 * by setting this to a #GFileProgressCallback function.
3273 * @progress_callback_data will be passed to this function. It is
3274 * guaranteed that this callback will be called after all data has been
3275 * transferred with the total number of bytes copied during the operation.
3277 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3278 * error is returned, independent on the status of the @destination.
3280 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3281 * then the error %G_IO_ERROR_EXISTS is returned.
3283 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3284 * error is returned. If trying to overwrite a directory with a directory the
3285 * %G_IO_ERROR_WOULD_MERGE error is returned.
3287 * If the source is a directory and the target does not exist, or
3288 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3289 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3290 * move operation isn't available).
3292 * Returns: %TRUE on successful move, %FALSE otherwise.
3295 g_file_move (GFile *source,
3297 GFileCopyFlags flags,
3298 GCancellable *cancellable,
3299 GFileProgressCallback progress_callback,
3300 gpointer progress_callback_data,
3307 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3308 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3310 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3313 iface = G_FILE_GET_IFACE (destination);
3317 res = (* iface->move) (source, destination,
3319 progress_callback, progress_callback_data,
3325 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3327 g_propagate_error (error, my_error);
3332 /* If the types are different, and the destination method failed
3333 * also try the source method
3335 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3337 iface = G_FILE_GET_IFACE (source);
3342 res = (* iface->move) (source, destination,
3344 progress_callback, progress_callback_data,
3350 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3352 g_propagate_error (error, my_error);
3358 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3360 g_set_error_literal (error, G_IO_ERROR,
3361 G_IO_ERROR_NOT_SUPPORTED,
3362 _("Operation not supported"));
3366 flags |= G_FILE_COPY_ALL_METADATA;
3367 if (!g_file_copy (source, destination, flags, cancellable,
3368 progress_callback, progress_callback_data,
3372 return g_file_delete (source, cancellable, error);
3376 * g_file_make_directory:
3377 * @file: input #GFile
3378 * @cancellable: (allow-none): optional #GCancellable object,
3380 * @error: a #GError, or %NULL
3382 * Creates a directory. Note that this will only create a child directory
3383 * of the immediate parent directory of the path or URI given by the #GFile.
3384 * To recursively create directories, see g_file_make_directory_with_parents().
3385 * This function will fail if the parent directory does not exist, setting
3386 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3387 * creating directories, this function will fail, setting @error to
3388 * %G_IO_ERROR_NOT_SUPPORTED.
3390 * For a local #GFile the newly created directory will have the default
3391 * (current) ownership and permissions of the current process.
3393 * If @cancellable is not %NULL, then the operation can be cancelled by
3394 * triggering the cancellable object from another thread. If the operation
3395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3397 * Returns: %TRUE on successful creation, %FALSE otherwise.
3400 g_file_make_directory (GFile *file,
3401 GCancellable *cancellable,
3406 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3411 iface = G_FILE_GET_IFACE (file);
3413 if (iface->make_directory == NULL)
3415 g_set_error_literal (error, G_IO_ERROR,
3416 G_IO_ERROR_NOT_SUPPORTED,
3417 _("Operation not supported"));
3421 return (* iface->make_directory) (file, cancellable, error);
3425 * g_file_make_directory_with_parents:
3426 * @file: input #GFile
3427 * @cancellable: (allow-none): optional #GCancellable object,
3429 * @error: a #GError, or %NULL
3431 * Creates a directory and any parent directories that may not
3432 * exist similar to 'mkdir -p'. If the file system does not support
3433 * creating directories, this function will fail, setting @error to
3434 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3435 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3436 * the similar g_mkdir_with_parents().
3438 * For a local #GFile the newly created directories will have the default
3439 * (current) ownership and permissions of the current process.
3441 * If @cancellable is not %NULL, then the operation can be cancelled by
3442 * triggering the cancellable object from another thread. If the operation
3443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3445 * Returns: %TRUE if all directories have been successfully created, %FALSE
3451 g_file_make_directory_with_parents (GFile *file,
3452 GCancellable *cancellable,
3455 GFile *work_file = NULL;
3456 GList *list = NULL, *l;
3457 GError *my_error = NULL;
3459 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3461 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3464 g_file_make_directory (file, cancellable, &my_error);
3465 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3468 g_propagate_error (error, my_error);
3469 return my_error == NULL;
3472 work_file = g_object_ref (file);
3474 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3478 parent_file = g_file_get_parent (work_file);
3479 if (parent_file == NULL)
3482 g_clear_error (&my_error);
3483 g_file_make_directory (parent_file, cancellable, &my_error);
3485 g_object_unref (work_file);
3486 work_file = g_object_ref (parent_file);
3488 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3489 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3491 g_object_unref (parent_file);
3494 for (l = list; my_error == NULL && l; l = l->next)
3496 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3500 g_object_unref (work_file);
3503 while (list != NULL)
3505 g_object_unref ((GFile *) list->data);
3506 list = g_list_remove (list, list->data);
3509 if (my_error != NULL)
3511 g_propagate_error (error, my_error);
3515 return g_file_make_directory (file, cancellable, error);
3519 * g_file_make_symbolic_link:
3520 * @file: a #GFile with the name of the symlink to create
3521 * @symlink_value: a string with the path for the target of the new symlink
3522 * @cancellable: (allow-none): optional #GCancellable object,
3526 * Creates a symbolic link named @file which contains the string
3529 * If @cancellable is not %NULL, then the operation can be cancelled by
3530 * triggering the cancellable object from another thread. If the operation
3531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3533 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3536 g_file_make_symbolic_link (GFile *file,
3537 const char *symlink_value,
3538 GCancellable *cancellable,
3543 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3544 g_return_val_if_fail (symlink_value != NULL, FALSE);
3546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549 if (*symlink_value == '\0')
3551 g_set_error_literal (error, G_IO_ERROR,
3552 G_IO_ERROR_INVALID_ARGUMENT,
3553 _("Invalid symlink value given"));
3557 iface = G_FILE_GET_IFACE (file);
3559 if (iface->make_symbolic_link == NULL)
3561 g_set_error_literal (error, G_IO_ERROR,
3562 G_IO_ERROR_NOT_SUPPORTED,
3563 _("Operation not supported"));
3567 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3572 * @file: input #GFile
3573 * @cancellable: (allow-none): optional #GCancellable object,
3575 * @error: a #GError, or %NULL
3577 * Deletes a file. If the @file is a directory, it will only be
3578 * deleted if it is empty. This has the same semantics as g_unlink().
3580 * If @cancellable is not %NULL, then the operation can be cancelled by
3581 * triggering the cancellable object from another thread. If the operation
3582 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3584 * Virtual: delete_file
3585 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3588 g_file_delete (GFile *file,
3589 GCancellable *cancellable,
3594 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3596 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3599 iface = G_FILE_GET_IFACE (file);
3601 if (iface->delete_file == NULL)
3603 g_set_error_literal (error, G_IO_ERROR,
3604 G_IO_ERROR_NOT_SUPPORTED,
3605 _("Operation not supported"));
3609 return (* iface->delete_file) (file, cancellable, error);
3613 * g_file_delete_async:
3614 * @file: input #GFile
3615 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3617 * @cancellable: (allow-none): optional #GCancellable object,
3619 * @callback: a #GAsyncReadyCallback to call
3620 * when the request is satisfied
3621 * @user_data: the data to pass to callback function
3623 * Asynchronously delete a file. If the @file is a directory, it will
3624 * only be deleted if it is empty. This has the same semantics as
3627 * Virtual: delete_file_async
3631 g_file_delete_async (GFile *file,
3633 GCancellable *cancellable,
3634 GAsyncReadyCallback callback,
3639 g_return_if_fail (G_IS_FILE (file));
3641 iface = G_FILE_GET_IFACE (file);
3642 (* iface->delete_file_async) (file,
3650 * g_file_delete_finish:
3651 * @file: input #GFile
3652 * @res: a #GAsyncResult
3653 * @error: a #GError, or %NULL
3655 * Finishes deleting a file started with g_file_delete_async().
3657 * Virtual: delete_file_finish
3661 g_file_delete_finish (GFile *file,
3662 GAsyncResult *result,
3667 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3668 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3670 if (g_async_result_legacy_propagate_error (result, error))
3673 iface = G_FILE_GET_IFACE (file);
3674 return (* iface->delete_file_finish) (file, result, error);
3679 * @file: #GFile to send to trash
3680 * @cancellable: (allow-none): optional #GCancellable object,
3682 * @error: a #GError, or %NULL
3684 * Sends @file to the "Trashcan", if possible. This is similar to
3685 * deleting it, but the user can recover it before emptying the trashcan.
3686 * Not all file systems support trashing, so this call can return the
3687 * %G_IO_ERROR_NOT_SUPPORTED error.
3689 * If @cancellable is not %NULL, then the operation can be cancelled by
3690 * triggering the cancellable object from another thread. If the operation
3691 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3693 * Returns: %TRUE on successful trash, %FALSE otherwise.
3696 g_file_trash (GFile *file,
3697 GCancellable *cancellable,
3702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3704 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3707 iface = G_FILE_GET_IFACE (file);
3709 if (iface->trash == NULL)
3711 g_set_error_literal (error,
3712 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3713 _("Trash not supported"));
3717 return (* iface->trash) (file, cancellable, error);
3721 * g_file_set_display_name:
3722 * @file: input #GFile
3723 * @display_name: a string
3724 * @cancellable: (allow-none): optional #GCancellable object,
3726 * @error: a #GError, or %NULL
3728 * Renames @file to the specified display name.
3730 * The display name is converted from UTF-8 to the correct encoding
3731 * for the target filesystem if possible and the @file is renamed to this.
3733 * If you want to implement a rename operation in the user interface the
3734 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3735 * initial value in the rename widget, and then the result after editing
3736 * should be passed to g_file_set_display_name().
3738 * On success the resulting converted filename is returned.
3740 * If @cancellable is not %NULL, then the operation can be cancelled by
3741 * triggering the cancellable object from another thread. If the operation
3742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3744 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3745 * or %NULL if there was an error.
3746 * Free the returned object with g_object_unref().
3749 g_file_set_display_name (GFile *file,
3750 const gchar *display_name,
3751 GCancellable *cancellable,
3756 g_return_val_if_fail (G_IS_FILE (file), NULL);
3757 g_return_val_if_fail (display_name != NULL, NULL);
3759 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3763 G_IO_ERROR_INVALID_ARGUMENT,
3764 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3771 iface = G_FILE_GET_IFACE (file);
3773 return (* iface->set_display_name) (file, display_name, cancellable, error);
3777 * g_file_set_display_name_async:
3778 * @file: input #GFile
3779 * @display_name: a string
3780 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3782 * @cancellable: (allow-none): optional #GCancellable object,
3784 * @callback: (scope async): a #GAsyncReadyCallback to call
3785 * when the request is satisfied
3786 * @user_data: (closure): the data to pass to callback function
3788 * Asynchronously sets the display name for a given #GFile.
3790 * For more details, see g_file_set_display_name() which is
3791 * the synchronous version of this call.
3793 * When the operation is finished, @callback will be called.
3794 * You can then call g_file_set_display_name_finish() to get
3795 * the result of the operation.
3798 g_file_set_display_name_async (GFile *file,
3799 const gchar *display_name,
3801 GCancellable *cancellable,
3802 GAsyncReadyCallback callback,
3807 g_return_if_fail (G_IS_FILE (file));
3808 g_return_if_fail (display_name != NULL);
3810 iface = G_FILE_GET_IFACE (file);
3811 (* iface->set_display_name_async) (file,
3820 * g_file_set_display_name_finish:
3821 * @file: input #GFile
3822 * @res: a #GAsyncResult
3823 * @error: a #GError, or %NULL
3825 * Finishes setting a display name started with
3826 * g_file_set_display_name_async().
3828 * Returns: (transfer full): a #GFile or %NULL on error.
3829 * Free the returned object with g_object_unref().
3832 g_file_set_display_name_finish (GFile *file,
3838 g_return_val_if_fail (G_IS_FILE (file), NULL);
3839 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3841 if (g_async_result_legacy_propagate_error (res, error))
3844 iface = G_FILE_GET_IFACE (file);
3845 return (* iface->set_display_name_finish) (file, res, error);
3849 * g_file_query_settable_attributes:
3850 * @file: input #GFile
3851 * @cancellable: (allow-none): optional #GCancellable object,
3853 * @error: a #GError, or %NULL
3855 * Obtain the list of settable attributes for the file.
3857 * Returns the type and full attribute name of all the attributes
3858 * that can be set on this file. This doesn't mean setting it will
3859 * always succeed though, you might get an access failure, or some
3860 * specific file may not support a specific attribute.
3862 * If @cancellable is not %NULL, then the operation can be cancelled by
3863 * triggering the cancellable object from another thread. If the operation
3864 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3866 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3867 * When you are done with it, release it with
3868 * g_file_attribute_info_list_unref()
3870 GFileAttributeInfoList *
3871 g_file_query_settable_attributes (GFile *file,
3872 GCancellable *cancellable,
3877 GFileAttributeInfoList *list;
3879 g_return_val_if_fail (G_IS_FILE (file), NULL);
3881 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3884 iface = G_FILE_GET_IFACE (file);
3886 if (iface->query_settable_attributes == NULL)
3887 return g_file_attribute_info_list_new ();
3890 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3894 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3896 list = g_file_attribute_info_list_new ();
3897 g_error_free (my_error);
3900 g_propagate_error (error, my_error);
3907 * g_file_query_writable_namespaces:
3908 * @file: input #GFile
3909 * @cancellable: (allow-none): optional #GCancellable object,
3911 * @error: a #GError, or %NULL
3913 * Obtain the list of attribute namespaces where new attributes
3914 * can be created by a user. An example of this is extended
3915 * attributes (in the "xattr" namespace).
3917 * If @cancellable is not %NULL, then the operation can be cancelled by
3918 * triggering the cancellable object from another thread. If the operation
3919 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3921 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3922 * When you are done with it, release it with
3923 * g_file_attribute_info_list_unref()
3925 GFileAttributeInfoList *
3926 g_file_query_writable_namespaces (GFile *file,
3927 GCancellable *cancellable,
3932 GFileAttributeInfoList *list;
3934 g_return_val_if_fail (G_IS_FILE (file), NULL);
3936 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3939 iface = G_FILE_GET_IFACE (file);
3941 if (iface->query_writable_namespaces == NULL)
3942 return g_file_attribute_info_list_new ();
3945 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3949 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3951 list = g_file_attribute_info_list_new ();
3952 g_error_free (my_error);
3955 g_propagate_error (error, my_error);
3962 * g_file_set_attribute:
3963 * @file: input #GFile
3964 * @attribute: a string containing the attribute's name
3965 * @type: The type of the attribute
3966 * @value_p: (allow-none): a pointer to the value (or the pointer
3967 * itself if the type is a pointer type)
3968 * @flags: a set of #GFileQueryInfoFlags
3969 * @cancellable: (allow-none): optional #GCancellable object,
3971 * @error: a #GError, or %NULL
3973 * Sets an attribute in the file with attribute name @attribute to @value.
3975 * Some attributes can be unset by setting @attribute to
3976 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3978 * If @cancellable is not %NULL, then the operation can be cancelled by
3979 * triggering the cancellable object from another thread. If the operation
3980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3982 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3985 g_file_set_attribute (GFile *file,
3986 const gchar *attribute,
3987 GFileAttributeType type,
3989 GFileQueryInfoFlags flags,
3990 GCancellable *cancellable,
3995 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3996 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3998 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4001 iface = G_FILE_GET_IFACE (file);
4003 if (iface->set_attribute == NULL)
4005 g_set_error_literal (error, G_IO_ERROR,
4006 G_IO_ERROR_NOT_SUPPORTED,
4007 _("Operation not supported"));
4011 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4015 * g_file_set_attributes_from_info:
4016 * @file: input #GFile
4017 * @info: a #GFileInfo
4018 * @flags: #GFileQueryInfoFlags
4019 * @cancellable: (allow-none): optional #GCancellable object,
4021 * @error: a #GError, or %NULL
4023 * Tries to set all attributes in the #GFileInfo on the target
4024 * values, not stopping on the first error.
4026 * If there is any error during this operation then @error will
4027 * be set to the first error. Error on particular fields are flagged
4028 * by setting the "status" field in the attribute value to
4029 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4030 * also detect further errors.
4032 * If @cancellable is not %NULL, then the operation can be cancelled by
4033 * triggering the cancellable object from another thread. If the operation
4034 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4036 * Returns: %TRUE if there was any error, %FALSE otherwise.
4039 g_file_set_attributes_from_info (GFile *file,
4041 GFileQueryInfoFlags flags,
4042 GCancellable *cancellable,
4047 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4048 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4050 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4053 g_file_info_clear_status (info);
4055 iface = G_FILE_GET_IFACE (file);
4057 return (* iface->set_attributes_from_info) (file,
4065 g_file_real_set_attributes_from_info (GFile *file,
4067 GFileQueryInfoFlags flags,
4068 GCancellable *cancellable,
4074 GFileAttributeValue *value;
4078 attributes = g_file_info_list_attributes (info, NULL);
4080 for (i = 0; attributes[i] != NULL; i++)
4082 value = _g_file_info_get_attribute_value (info, attributes[i]);
4084 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4087 if (!g_file_set_attribute (file, attributes[i],
4088 value->type, _g_file_attribute_value_peek_as_pointer (value),
4089 flags, cancellable, error))
4091 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4093 /* Don't set error multiple times */
4097 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4100 g_strfreev (attributes);
4106 * g_file_set_attributes_async:
4107 * @file: input #GFile
4108 * @info: a #GFileInfo
4109 * @flags: a #GFileQueryInfoFlags
4110 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4112 * @cancellable: (allow-none): optional #GCancellable object,
4114 * @callback: (scope async): a #GAsyncReadyCallback
4115 * @user_data: (closure): a #gpointer
4117 * Asynchronously sets the attributes of @file with @info.
4119 * For more details, see g_file_set_attributes_from_info(),
4120 * which is the synchronous version of this call.
4122 * When the operation is finished, @callback will be called.
4123 * You can then call g_file_set_attributes_finish() to get
4124 * the result of the operation.
4127 g_file_set_attributes_async (GFile *file,
4129 GFileQueryInfoFlags flags,
4131 GCancellable *cancellable,
4132 GAsyncReadyCallback callback,
4137 g_return_if_fail (G_IS_FILE (file));
4138 g_return_if_fail (G_IS_FILE_INFO (info));
4140 iface = G_FILE_GET_IFACE (file);
4141 (* iface->set_attributes_async) (file,
4151 * g_file_set_attributes_finish:
4152 * @file: input #GFile
4153 * @result: a #GAsyncResult
4154 * @info: (out) (transfer full): a #GFileInfo
4155 * @error: a #GError, or %NULL
4157 * Finishes setting an attribute started in g_file_set_attributes_async().
4159 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4162 g_file_set_attributes_finish (GFile *file,
4163 GAsyncResult *result,
4169 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4170 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4172 /* No standard handling of errors here, as we must set info even
4175 iface = G_FILE_GET_IFACE (file);
4176 return (* iface->set_attributes_finish) (file, result, info, error);
4180 * g_file_set_attribute_string:
4181 * @file: input #GFile
4182 * @attribute: a string containing the attribute's name
4183 * @value: a string containing the attribute's value
4184 * @flags: #GFileQueryInfoFlags
4185 * @cancellable: (allow-none): optional #GCancellable object,
4187 * @error: a #GError, or %NULL
4189 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4190 * If @attribute is of a different type, this operation will fail.
4192 * If @cancellable is not %NULL, then the operation can be cancelled by
4193 * triggering the cancellable object from another thread. If the operation
4194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4196 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4199 g_file_set_attribute_string (GFile *file,
4200 const char *attribute,
4202 GFileQueryInfoFlags flags,
4203 GCancellable *cancellable,
4206 return g_file_set_attribute (file, attribute,
4207 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4208 flags, cancellable, error);
4212 * g_file_set_attribute_byte_string:
4213 * @file: input #GFile
4214 * @attribute: a string containing the attribute's name
4215 * @value: a string containing the attribute's new value
4216 * @flags: a #GFileQueryInfoFlags
4217 * @cancellable: (allow-none): optional #GCancellable object,
4219 * @error: a #GError, or %NULL
4221 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4222 * If @attribute is of a different type, this operation will fail,
4225 * If @cancellable is not %NULL, then the operation can be cancelled by
4226 * triggering the cancellable object from another thread. If the operation
4227 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4229 * Returns: %TRUE if the @attribute was successfully set to @value
4230 * in the @file, %FALSE otherwise.
4233 g_file_set_attribute_byte_string (GFile *file,
4234 const gchar *attribute,
4236 GFileQueryInfoFlags flags,
4237 GCancellable *cancellable,
4240 return g_file_set_attribute (file, attribute,
4241 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4242 flags, cancellable, error);
4246 * g_file_set_attribute_uint32:
4247 * @file: input #GFile
4248 * @attribute: a string containing the attribute's name
4249 * @value: a #guint32 containing the attribute's new value
4250 * @flags: a #GFileQueryInfoFlags
4251 * @cancellable: (allow-none): optional #GCancellable object,
4253 * @error: a #GError, or %NULL
4255 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4256 * If @attribute is of a different type, this operation will fail.
4258 * If @cancellable is not %NULL, then the operation can be cancelled by
4259 * triggering the cancellable object from another thread. If the operation
4260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4262 * Returns: %TRUE if the @attribute was successfully set to @value
4263 * in the @file, %FALSE otherwise.
4266 g_file_set_attribute_uint32 (GFile *file,
4267 const gchar *attribute,
4269 GFileQueryInfoFlags flags,
4270 GCancellable *cancellable,
4273 return g_file_set_attribute (file, attribute,
4274 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4275 flags, cancellable, error);
4279 * g_file_set_attribute_int32:
4280 * @file: input #GFile
4281 * @attribute: a string containing the attribute's name
4282 * @value: a #gint32 containing the attribute's new value
4283 * @flags: a #GFileQueryInfoFlags
4284 * @cancellable: (allow-none): optional #GCancellable object,
4286 * @error: a #GError, or %NULL
4288 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4289 * If @attribute is of a different type, this operation will fail.
4291 * If @cancellable is not %NULL, then the operation can be cancelled by
4292 * triggering the cancellable object from another thread. If the operation
4293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4295 * Returns: %TRUE if the @attribute was successfully set to @value
4296 * in the @file, %FALSE otherwise.
4299 g_file_set_attribute_int32 (GFile *file,
4300 const gchar *attribute,
4302 GFileQueryInfoFlags flags,
4303 GCancellable *cancellable,
4306 return g_file_set_attribute (file, attribute,
4307 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4308 flags, cancellable, error);
4312 * g_file_set_attribute_uint64:
4313 * @file: input #GFile
4314 * @attribute: a string containing the attribute's name
4315 * @value: a #guint64 containing the attribute's new value
4316 * @flags: a #GFileQueryInfoFlags
4317 * @cancellable: (allow-none): optional #GCancellable object,
4319 * @error: a #GError, or %NULL
4321 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4322 * If @attribute is of a different type, this operation will fail.
4324 * If @cancellable is not %NULL, then the operation can be cancelled by
4325 * triggering the cancellable object from another thread. If the operation
4326 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4328 * Returns: %TRUE if the @attribute was successfully set to @value
4329 * in the @file, %FALSE otherwise.
4332 g_file_set_attribute_uint64 (GFile *file,
4333 const gchar *attribute,
4335 GFileQueryInfoFlags flags,
4336 GCancellable *cancellable,
4339 return g_file_set_attribute (file, attribute,
4340 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4341 flags, cancellable, error);
4345 * g_file_set_attribute_int64:
4346 * @file: input #GFile
4347 * @attribute: a string containing the attribute's name
4348 * @value: a #guint64 containing the attribute's new value
4349 * @flags: a #GFileQueryInfoFlags
4350 * @cancellable: (allow-none): optional #GCancellable object,
4352 * @error: a #GError, or %NULL
4354 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4355 * If @attribute is of a different type, this operation will fail.
4357 * If @cancellable is not %NULL, then the operation can be cancelled by
4358 * triggering the cancellable object from another thread. If the operation
4359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4361 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4364 g_file_set_attribute_int64 (GFile *file,
4365 const gchar *attribute,
4367 GFileQueryInfoFlags flags,
4368 GCancellable *cancellable,
4371 return g_file_set_attribute (file, attribute,
4372 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4373 flags, cancellable, error);
4377 * g_file_mount_mountable:
4378 * @file: input #GFile
4379 * @flags: flags affecting the operation
4380 * @mount_operation: (allow-none): a #GMountOperation,
4381 * or %NULL to avoid user interaction
4382 * @cancellable: (allow-none): optional #GCancellable object,
4384 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4385 * when the request is satisfied, or %NULL
4386 * @user_data: (closure): the data to pass to callback function
4388 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4389 * Using @mount_operation, you can request callbacks when, for instance,
4390 * passwords are needed during authentication.
4392 * If @cancellable is not %NULL, then the operation can be cancelled by
4393 * triggering the cancellable object from another thread. If the operation
4394 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4396 * When the operation is finished, @callback will be called.
4397 * You can then call g_file_mount_mountable_finish() to get
4398 * the result of the operation.
4401 g_file_mount_mountable (GFile *file,
4402 GMountMountFlags flags,
4403 GMountOperation *mount_operation,
4404 GCancellable *cancellable,
4405 GAsyncReadyCallback callback,
4410 g_return_if_fail (G_IS_FILE (file));
4412 iface = G_FILE_GET_IFACE (file);
4414 if (iface->mount_mountable == NULL)
4416 g_simple_async_report_error_in_idle (G_OBJECT (file),
4420 G_IO_ERROR_NOT_SUPPORTED,
4421 _("Operation not supported"));
4425 (* iface->mount_mountable) (file,
4434 * g_file_mount_mountable_finish:
4435 * @file: input #GFile
4436 * @result: a #GAsyncResult
4437 * @error: a #GError, or %NULL
4439 * Finishes a mount operation. See g_file_mount_mountable() for details.
4441 * Finish an asynchronous mount operation that was started
4442 * with g_file_mount_mountable().
4444 * Returns: (transfer full): a #GFile or %NULL on error.
4445 * Free the returned object with g_object_unref().
4448 g_file_mount_mountable_finish (GFile *file,
4449 GAsyncResult *result,
4454 g_return_val_if_fail (G_IS_FILE (file), NULL);
4455 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4457 if (g_async_result_legacy_propagate_error (result, error))
4460 iface = G_FILE_GET_IFACE (file);
4461 return (* iface->mount_mountable_finish) (file, result, error);
4465 * g_file_unmount_mountable:
4466 * @file: input #GFile
4467 * @flags: flags affecting the operation
4468 * @cancellable: (allow-none): optional #GCancellable object,
4470 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4471 * when the request is satisfied, or %NULL
4472 * @user_data: (closure): the data to pass to callback function
4474 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4476 * If @cancellable is not %NULL, then the operation can be cancelled by
4477 * triggering the cancellable object from another thread. If the operation
4478 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4480 * When the operation is finished, @callback will be called.
4481 * You can then call g_file_unmount_mountable_finish() to get
4482 * the result of the operation.
4484 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4487 g_file_unmount_mountable (GFile *file,
4488 GMountUnmountFlags flags,
4489 GCancellable *cancellable,
4490 GAsyncReadyCallback callback,
4495 g_return_if_fail (G_IS_FILE (file));
4497 iface = G_FILE_GET_IFACE (file);
4499 if (iface->unmount_mountable == NULL)
4501 g_simple_async_report_error_in_idle (G_OBJECT (file),
4505 G_IO_ERROR_NOT_SUPPORTED,
4506 _("Operation not supported"));
4510 (* iface->unmount_mountable) (file,
4518 * g_file_unmount_mountable_finish:
4519 * @file: input #GFile
4520 * @result: a #GAsyncResult
4521 * @error: a #GError, or %NULL
4523 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4525 * Finish an asynchronous unmount operation that was started
4526 * with g_file_unmount_mountable().
4528 * Returns: %TRUE if the operation finished successfully.
4531 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4535 g_file_unmount_mountable_finish (GFile *file,
4536 GAsyncResult *result,
4541 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4542 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4544 if (g_async_result_legacy_propagate_error (result, error))
4547 iface = G_FILE_GET_IFACE (file);
4548 return (* iface->unmount_mountable_finish) (file, result, error);
4552 * g_file_unmount_mountable_with_operation:
4553 * @file: input #GFile
4554 * @flags: flags affecting the operation
4555 * @mount_operation: (allow-none): a #GMountOperation,
4556 * or %NULL to avoid user interaction
4557 * @cancellable: (allow-none): optional #GCancellable object,
4559 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4560 * when the request is satisfied, or %NULL
4561 * @user_data: (closure): the data to pass to callback function
4563 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4565 * If @cancellable is not %NULL, then the operation can be cancelled by
4566 * triggering the cancellable object from another thread. If the operation
4567 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4569 * When the operation is finished, @callback will be called.
4570 * You can then call g_file_unmount_mountable_finish() to get
4571 * the result of the operation.
4576 g_file_unmount_mountable_with_operation (GFile *file,
4577 GMountUnmountFlags flags,
4578 GMountOperation *mount_operation,
4579 GCancellable *cancellable,
4580 GAsyncReadyCallback callback,
4585 g_return_if_fail (G_IS_FILE (file));
4587 iface = G_FILE_GET_IFACE (file);
4589 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4591 g_simple_async_report_error_in_idle (G_OBJECT (file),
4595 G_IO_ERROR_NOT_SUPPORTED,
4596 _("Operation not supported"));
4600 if (iface->unmount_mountable_with_operation != NULL)
4601 (* iface->unmount_mountable_with_operation) (file,
4608 (* iface->unmount_mountable) (file,
4616 * g_file_unmount_mountable_with_operation_finish:
4617 * @file: input #GFile
4618 * @result: a #GAsyncResult
4619 * @error: a #GError, or %NULL
4621 * Finishes an unmount operation,
4622 * see g_file_unmount_mountable_with_operation() for details.
4624 * Finish an asynchronous unmount operation that was started
4625 * with g_file_unmount_mountable_with_operation().
4627 * Returns: %TRUE if the operation finished successfully.
4633 g_file_unmount_mountable_with_operation_finish (GFile *file,
4634 GAsyncResult *result,
4639 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4640 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4642 if (g_async_result_legacy_propagate_error (result, error))
4645 iface = G_FILE_GET_IFACE (file);
4646 if (iface->unmount_mountable_with_operation_finish != NULL)
4647 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4649 return (* iface->unmount_mountable_finish) (file, result, error);
4653 * g_file_eject_mountable:
4654 * @file: input #GFile
4655 * @flags: flags affecting the operation
4656 * @cancellable: (allow-none): optional #GCancellable object,
4658 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4659 * when the request is satisfied, or %NULL
4660 * @user_data: (closure): the data to pass to callback function
4662 * Starts an asynchronous eject on a mountable.
4663 * When this operation has completed, @callback will be called with
4664 * @user_user data, and the operation can be finalized with
4665 * g_file_eject_mountable_finish().
4667 * If @cancellable is not %NULL, then the operation can be cancelled by
4668 * triggering the cancellable object from another thread. If the operation
4669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4671 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4674 g_file_eject_mountable (GFile *file,
4675 GMountUnmountFlags flags,
4676 GCancellable *cancellable,
4677 GAsyncReadyCallback callback,
4682 g_return_if_fail (G_IS_FILE (file));
4684 iface = G_FILE_GET_IFACE (file);
4686 if (iface->eject_mountable == NULL)
4688 g_simple_async_report_error_in_idle (G_OBJECT (file),
4692 G_IO_ERROR_NOT_SUPPORTED,
4693 _("Operation not supported"));
4697 (* iface->eject_mountable) (file,
4705 * g_file_eject_mountable_finish:
4706 * @file: input #GFile
4707 * @result: a #GAsyncResult
4708 * @error: a #GError, or %NULL
4710 * Finishes an asynchronous eject operation started by
4711 * g_file_eject_mountable().
4713 * Returns: %TRUE if the @file was ejected successfully.
4716 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4720 g_file_eject_mountable_finish (GFile *file,
4721 GAsyncResult *result,
4726 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4727 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4729 if (g_async_result_legacy_propagate_error (result, error))
4732 iface = G_FILE_GET_IFACE (file);
4733 return (* iface->eject_mountable_finish) (file, result, error);
4737 * g_file_eject_mountable_with_operation:
4738 * @file: input #GFile
4739 * @flags: flags affecting the operation
4740 * @mount_operation: (allow-none): a #GMountOperation,
4741 * or %NULL to avoid user interaction
4742 * @cancellable: (allow-none): optional #GCancellable object,
4744 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4745 * when the request is satisfied, or %NULL
4746 * @user_data: (closure): the data to pass to callback function
4748 * Starts an asynchronous eject on a mountable.
4749 * When this operation has completed, @callback will be called with
4750 * @user_user data, and the operation can be finalized with
4751 * g_file_eject_mountable_with_operation_finish().
4753 * If @cancellable is not %NULL, then the operation can be cancelled by
4754 * triggering the cancellable object from another thread. If the operation
4755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4760 g_file_eject_mountable_with_operation (GFile *file,
4761 GMountUnmountFlags flags,
4762 GMountOperation *mount_operation,
4763 GCancellable *cancellable,
4764 GAsyncReadyCallback callback,
4769 g_return_if_fail (G_IS_FILE (file));
4771 iface = G_FILE_GET_IFACE (file);
4773 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4775 g_simple_async_report_error_in_idle (G_OBJECT (file),
4779 G_IO_ERROR_NOT_SUPPORTED,
4780 _("Operation not supported"));
4784 if (iface->eject_mountable_with_operation != NULL)
4785 (* iface->eject_mountable_with_operation) (file,
4792 (* iface->eject_mountable) (file,
4800 * g_file_eject_mountable_with_operation_finish:
4801 * @file: input #GFile
4802 * @result: a #GAsyncResult
4803 * @error: a #GError, or %NULL
4805 * Finishes an asynchronous eject operation started by
4806 * g_file_eject_mountable_with_operation().
4808 * Returns: %TRUE if the @file was ejected successfully.
4814 g_file_eject_mountable_with_operation_finish (GFile *file,
4815 GAsyncResult *result,
4820 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4821 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4823 if (g_async_result_legacy_propagate_error (result, error))
4826 iface = G_FILE_GET_IFACE (file);
4827 if (iface->eject_mountable_with_operation_finish != NULL)
4828 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4830 return (* iface->eject_mountable_finish) (file, result, error);
4834 * g_file_monitor_directory:
4835 * @file: input #GFile
4836 * @flags: a set of #GFileMonitorFlags
4837 * @cancellable: (allow-none): optional #GCancellable object,
4839 * @error: a #GError, or %NULL
4841 * Obtains a directory monitor for the given file.
4842 * This may fail if directory monitoring is not supported.
4844 * If @cancellable is not %NULL, then the operation can be cancelled by
4845 * triggering the cancellable object from another thread. If the operation
4846 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4848 * Virtual: monitor_dir
4849 * Returns: (transfer full): a #GFileMonitor for the given @file,
4850 * or %NULL on error.
4851 * Free the returned object with g_object_unref().
4854 g_file_monitor_directory (GFile *file,
4855 GFileMonitorFlags flags,
4856 GCancellable *cancellable,
4861 g_return_val_if_fail (G_IS_FILE (file), NULL);
4863 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4866 iface = G_FILE_GET_IFACE (file);
4868 if (iface->monitor_dir == NULL)
4870 g_set_error_literal (error, G_IO_ERROR,
4871 G_IO_ERROR_NOT_SUPPORTED,
4872 _("Operation not supported"));
4876 return (* iface->monitor_dir) (file, flags, cancellable, error);
4880 * g_file_monitor_file:
4881 * @file: input #GFile
4882 * @flags: a set of #GFileMonitorFlags
4883 * @cancellable: (allow-none): optional #GCancellable object,
4885 * @error: a #GError, or %NULL
4887 * Obtains a file monitor for the given file. If no file notification
4888 * mechanism exists, then regular polling of the file is used.
4890 * If @cancellable is not %NULL, then the operation can be cancelled by
4891 * triggering the cancellable object from another thread. If the operation
4892 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4894 * Returns: (transfer full): a #GFileMonitor for the given @file,
4895 * or %NULL on error.
4896 * Free the returned object with g_object_unref().
4899 g_file_monitor_file (GFile *file,
4900 GFileMonitorFlags flags,
4901 GCancellable *cancellable,
4905 GFileMonitor *monitor;
4907 g_return_val_if_fail (G_IS_FILE (file), NULL);
4909 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4912 iface = G_FILE_GET_IFACE (file);
4916 if (iface->monitor_file)
4917 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4919 /* Fallback to polling */
4920 if (monitor == NULL)
4921 monitor = _g_poll_file_monitor_new (file);
4928 * @file: input #GFile
4929 * @flags: a set of #GFileMonitorFlags
4930 * @cancellable: (allow-none): optional #GCancellable object,
4932 * @error: a #GError, or %NULL
4934 * Obtains a file or directory monitor for the given file,
4935 * depending on the type of the file.
4937 * If @cancellable is not %NULL, then the operation can be cancelled by
4938 * triggering the cancellable object from another thread. If the operation
4939 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4941 * Returns: (transfer full): a #GFileMonitor for the given @file,
4942 * or %NULL on error.
4943 * Free the returned object with g_object_unref().
4948 g_file_monitor (GFile *file,
4949 GFileMonitorFlags flags,
4950 GCancellable *cancellable,
4953 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4954 return g_file_monitor_directory (file, flags, cancellable, error);
4956 return g_file_monitor_file (file, flags, cancellable, error);
4959 /********************************************
4960 * Default implementation of async ops *
4961 ********************************************/
4965 GFileQueryInfoFlags flags;
4967 } QueryInfoAsyncData;
4970 query_info_data_free (QueryInfoAsyncData *data)
4973 g_object_unref (data->info);
4974 g_free (data->attributes);
4979 query_info_async_thread (GSimpleAsyncResult *res,
4981 GCancellable *cancellable)
4983 GError *error = NULL;
4984 QueryInfoAsyncData *data;
4987 data = g_simple_async_result_get_op_res_gpointer (res);
4988 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4991 g_simple_async_result_take_error (res, error);
4997 g_file_real_query_info_async (GFile *file,
4998 const char *attributes,
4999 GFileQueryInfoFlags flags,
5001 GCancellable *cancellable,
5002 GAsyncReadyCallback callback,
5005 GSimpleAsyncResult *res;
5006 QueryInfoAsyncData *data;
5008 data = g_new0 (QueryInfoAsyncData, 1);
5009 data->attributes = g_strdup (attributes);
5010 data->flags = flags;
5012 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
5013 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
5015 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
5016 g_object_unref (res);
5020 g_file_real_query_info_finish (GFile *file,
5024 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5025 QueryInfoAsyncData *data;
5027 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
5029 if (g_simple_async_result_propagate_error (simple, error))
5032 data = g_simple_async_result_get_op_res_gpointer (simple);
5034 return g_object_ref (data->info);
5042 } QueryFilesystemInfoAsyncData;
5045 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
5048 g_object_unref (data->info);
5049 g_free (data->attributes);
5054 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
5056 GCancellable *cancellable)
5058 GError *error = NULL;
5059 QueryFilesystemInfoAsyncData *data;
5062 data = g_simple_async_result_get_op_res_gpointer (res);
5064 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
5067 g_simple_async_result_take_error (res, error);
5073 g_file_real_query_filesystem_info_async (GFile *file,
5074 const char *attributes,
5076 GCancellable *cancellable,
5077 GAsyncReadyCallback callback,
5080 GSimpleAsyncResult *res;
5081 QueryFilesystemInfoAsyncData *data;
5083 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
5084 data->attributes = g_strdup (attributes);
5086 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
5087 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
5089 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
5090 g_object_unref (res);
5094 g_file_real_query_filesystem_info_finish (GFile *file,
5098 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5099 QueryFilesystemInfoAsyncData *data;
5101 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
5103 if (g_simple_async_result_propagate_error (simple, error))
5106 data = g_simple_async_result_get_op_res_gpointer (simple);
5108 return g_object_ref (data->info);
5115 GFileQueryInfoFlags flags;
5116 GFileEnumerator *enumerator;
5117 } EnumerateChildrenAsyncData;
5120 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
5122 if (data->enumerator)
5123 g_object_unref (data->enumerator);
5124 g_free (data->attributes);
5129 enumerate_children_async_thread (GSimpleAsyncResult *res,
5131 GCancellable *cancellable)
5133 GError *error = NULL;
5134 EnumerateChildrenAsyncData *data;
5135 GFileEnumerator *enumerator;
5137 data = g_simple_async_result_get_op_res_gpointer (res);
5139 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5141 if (enumerator == NULL)
5142 g_simple_async_result_take_error (res, error);
5144 data->enumerator = enumerator;
5148 g_file_real_enumerate_children_async (GFile *file,
5149 const char *attributes,
5150 GFileQueryInfoFlags flags,
5152 GCancellable *cancellable,
5153 GAsyncReadyCallback callback,
5156 GSimpleAsyncResult *res;
5157 EnumerateChildrenAsyncData *data;
5159 data = g_new0 (EnumerateChildrenAsyncData, 1);
5160 data->attributes = g_strdup (attributes);
5161 data->flags = flags;
5163 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5164 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5166 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5167 g_object_unref (res);
5170 static GFileEnumerator *
5171 g_file_real_enumerate_children_finish (GFile *file,
5175 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5176 EnumerateChildrenAsyncData *data;
5178 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5180 if (g_simple_async_result_propagate_error (simple, error))
5183 data = g_simple_async_result_get_op_res_gpointer (simple);
5184 if (data->enumerator)
5185 return g_object_ref (data->enumerator);
5191 open_read_async_thread (GSimpleAsyncResult *res,
5193 GCancellable *cancellable)
5196 GFileInputStream *stream;
5197 GError *error = NULL;
5199 iface = G_FILE_GET_IFACE (object);
5201 if (iface->read_fn == NULL)
5203 g_set_error_literal (&error, G_IO_ERROR,
5204 G_IO_ERROR_NOT_SUPPORTED,
5205 _("Operation not supported"));
5207 g_simple_async_result_take_error (res, error);
5212 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5215 g_simple_async_result_take_error (res, error);
5217 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5221 g_file_real_read_async (GFile *file,
5223 GCancellable *cancellable,
5224 GAsyncReadyCallback callback,
5227 GSimpleAsyncResult *res;
5229 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5231 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5232 g_object_unref (res);
5235 static GFileInputStream *
5236 g_file_real_read_finish (GFile *file,
5240 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5243 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5245 if (g_simple_async_result_propagate_error (simple, error))
5248 op = g_simple_async_result_get_op_res_gpointer (simple);
5250 return g_object_ref (op);
5256 append_to_async_thread (GSimpleAsyncResult *res,
5258 GCancellable *cancellable)
5261 GFileCreateFlags *data;
5262 GFileOutputStream *stream;
5263 GError *error = NULL;
5265 iface = G_FILE_GET_IFACE (object);
5267 data = g_simple_async_result_get_op_res_gpointer (res);
5269 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5272 g_simple_async_result_take_error (res, error);
5274 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5278 g_file_real_append_to_async (GFile *file,
5279 GFileCreateFlags flags,
5281 GCancellable *cancellable,
5282 GAsyncReadyCallback callback,
5285 GFileCreateFlags *data;
5286 GSimpleAsyncResult *res;
5288 data = g_new0 (GFileCreateFlags, 1);
5291 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5292 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5294 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5295 g_object_unref (res);
5298 static GFileOutputStream *
5299 g_file_real_append_to_finish (GFile *file,
5303 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5306 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5308 if (g_simple_async_result_propagate_error (simple, error))
5311 op = g_simple_async_result_get_op_res_gpointer (simple);
5313 return g_object_ref (op);
5319 create_async_thread (GSimpleAsyncResult *res,
5321 GCancellable *cancellable)
5324 GFileCreateFlags *data;
5325 GFileOutputStream *stream;
5326 GError *error = NULL;
5328 iface = G_FILE_GET_IFACE (object);
5330 data = g_simple_async_result_get_op_res_gpointer (res);
5332 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5335 g_simple_async_result_take_error (res, error);
5337 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5341 g_file_real_create_async (GFile *file,
5342 GFileCreateFlags flags,
5344 GCancellable *cancellable,
5345 GAsyncReadyCallback callback,
5348 GFileCreateFlags *data;
5349 GSimpleAsyncResult *res;
5351 data = g_new0 (GFileCreateFlags, 1);
5354 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5355 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5357 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5358 g_object_unref (res);
5361 static GFileOutputStream *
5362 g_file_real_create_finish (GFile *file,
5366 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5369 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5371 if (g_simple_async_result_propagate_error (simple, error))
5374 op = g_simple_async_result_get_op_res_gpointer (simple);
5376 return g_object_ref (op);
5382 GFileOutputStream *stream;
5384 gboolean make_backup;
5385 GFileCreateFlags flags;
5389 replace_async_data_free (ReplaceAsyncData *data)
5392 g_object_unref (data->stream);
5393 g_free (data->etag);
5398 replace_async_thread (GSimpleAsyncResult *res,
5400 GCancellable *cancellable)
5403 GFileOutputStream *stream;
5404 GError *error = NULL;
5405 ReplaceAsyncData *data;
5407 iface = G_FILE_GET_IFACE (object);
5409 data = g_simple_async_result_get_op_res_gpointer (res);
5411 stream = iface->replace (G_FILE (object),
5419 g_simple_async_result_take_error (res, error);
5421 data->stream = stream;
5425 g_file_real_replace_async (GFile *file,
5427 gboolean make_backup,
5428 GFileCreateFlags flags,
5430 GCancellable *cancellable,
5431 GAsyncReadyCallback callback,
5434 GSimpleAsyncResult *res;
5435 ReplaceAsyncData *data;
5437 data = g_new0 (ReplaceAsyncData, 1);
5438 data->etag = g_strdup (etag);
5439 data->make_backup = make_backup;
5440 data->flags = flags;
5442 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5443 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5445 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5446 g_object_unref (res);
5449 static GFileOutputStream *
5450 g_file_real_replace_finish (GFile *file,
5454 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5455 ReplaceAsyncData *data;
5457 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5459 if (g_simple_async_result_propagate_error (simple, error))
5462 data = g_simple_async_result_get_op_res_gpointer (simple);
5464 return g_object_ref (data->stream);
5470 delete_async_thread (GSimpleAsyncResult *res,
5472 GCancellable *cancellable)
5475 GError *error = NULL;
5477 iface = G_FILE_GET_IFACE (object);
5479 if (!iface->delete_file (G_FILE (object),
5482 g_simple_async_result_take_error (res, error);
5486 g_file_real_delete_async (GFile *file,
5488 GCancellable *cancellable,
5489 GAsyncReadyCallback callback,
5492 GSimpleAsyncResult *res;
5494 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_delete_async);
5495 g_simple_async_result_run_in_thread (res, delete_async_thread, io_priority, cancellable);
5496 g_object_unref (res);
5500 g_file_real_delete_finish (GFile *file,
5504 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5506 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_delete_async);
5508 if (g_simple_async_result_propagate_error (simple, error))
5515 open_readwrite_async_thread (GSimpleAsyncResult *res,
5517 GCancellable *cancellable)
5520 GFileIOStream *stream;
5521 GError *error = NULL;
5523 iface = G_FILE_GET_IFACE (object);
5525 if (iface->open_readwrite == NULL)
5527 g_set_error_literal (&error, G_IO_ERROR,
5528 G_IO_ERROR_NOT_SUPPORTED,
5529 _("Operation not supported"));
5531 g_simple_async_result_take_error (res, error);
5536 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5539 g_simple_async_result_take_error (res, error);
5541 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5545 g_file_real_open_readwrite_async (GFile *file,
5547 GCancellable *cancellable,
5548 GAsyncReadyCallback callback,
5551 GSimpleAsyncResult *res;
5553 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5555 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5556 g_object_unref (res);
5559 static GFileIOStream *
5560 g_file_real_open_readwrite_finish (GFile *file,
5564 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5567 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5569 if (g_simple_async_result_propagate_error (simple, error))
5572 op = g_simple_async_result_get_op_res_gpointer (simple);
5574 return g_object_ref (op);
5580 create_readwrite_async_thread (GSimpleAsyncResult *res,
5582 GCancellable *cancellable)
5585 GFileCreateFlags *data;
5586 GFileIOStream *stream;
5587 GError *error = NULL;
5589 iface = G_FILE_GET_IFACE (object);
5591 data = g_simple_async_result_get_op_res_gpointer (res);
5593 if (iface->create_readwrite == NULL)
5595 g_set_error_literal (&error, G_IO_ERROR,
5596 G_IO_ERROR_NOT_SUPPORTED,
5597 _("Operation not supported"));
5599 g_simple_async_result_take_error (res, error);
5604 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5607 g_simple_async_result_take_error (res, error);
5609 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5613 g_file_real_create_readwrite_async (GFile *file,
5614 GFileCreateFlags flags,
5616 GCancellable *cancellable,
5617 GAsyncReadyCallback callback,
5620 GFileCreateFlags *data;
5621 GSimpleAsyncResult *res;
5623 data = g_new0 (GFileCreateFlags, 1);
5626 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5627 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5629 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5630 g_object_unref (res);
5633 static GFileIOStream *
5634 g_file_real_create_readwrite_finish (GFile *file,
5638 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5641 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5643 if (g_simple_async_result_propagate_error (simple, error))
5646 op = g_simple_async_result_get_op_res_gpointer (simple);
5648 return g_object_ref (op);
5654 GFileIOStream *stream;
5656 gboolean make_backup;
5657 GFileCreateFlags flags;
5658 } ReplaceRWAsyncData;
5661 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5664 g_object_unref (data->stream);
5665 g_free (data->etag);
5670 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5672 GCancellable *cancellable)
5675 GFileIOStream *stream;
5676 GError *error = NULL;
5677 ReplaceRWAsyncData *data;
5679 iface = G_FILE_GET_IFACE (object);
5681 data = g_simple_async_result_get_op_res_gpointer (res);
5683 stream = iface->replace_readwrite (G_FILE (object),
5691 g_simple_async_result_take_error (res, error);
5693 data->stream = stream;
5697 g_file_real_replace_readwrite_async (GFile *file,
5699 gboolean make_backup,
5700 GFileCreateFlags flags,
5702 GCancellable *cancellable,
5703 GAsyncReadyCallback callback,
5706 GSimpleAsyncResult *res;
5707 ReplaceRWAsyncData *data;
5709 data = g_new0 (ReplaceRWAsyncData, 1);
5710 data->etag = g_strdup (etag);
5711 data->make_backup = make_backup;
5712 data->flags = flags;
5714 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5715 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5717 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5718 g_object_unref (res);
5721 static GFileIOStream *
5722 g_file_real_replace_readwrite_finish (GFile *file,
5726 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5727 ReplaceRWAsyncData *data;
5729 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5731 if (g_simple_async_result_propagate_error (simple, error))
5734 data = g_simple_async_result_get_op_res_gpointer (simple);
5736 return g_object_ref (data->stream);
5744 } SetDisplayNameAsyncData;
5747 set_display_name_data_free (SetDisplayNameAsyncData *data)
5749 g_free (data->name);
5751 g_object_unref (data->file);
5756 set_display_name_async_thread (GSimpleAsyncResult *res,
5758 GCancellable *cancellable)
5760 GError *error = NULL;
5761 SetDisplayNameAsyncData *data;
5764 data = g_simple_async_result_get_op_res_gpointer (res);
5766 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5769 g_simple_async_result_take_error (res, error);
5775 g_file_real_set_display_name_async (GFile *file,
5776 const char *display_name,
5778 GCancellable *cancellable,
5779 GAsyncReadyCallback callback,
5782 GSimpleAsyncResult *res;
5783 SetDisplayNameAsyncData *data;
5785 data = g_new0 (SetDisplayNameAsyncData, 1);
5786 data->name = g_strdup (display_name);
5788 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5789 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5791 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5792 g_object_unref (res);
5796 g_file_real_set_display_name_finish (GFile *file,
5800 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5801 SetDisplayNameAsyncData *data;
5803 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5805 if (g_simple_async_result_propagate_error (simple, error))
5808 data = g_simple_async_result_get_op_res_gpointer (simple);
5810 return g_object_ref (data->file);
5816 GFileQueryInfoFlags flags;
5823 set_info_data_free (SetInfoAsyncData *data)
5826 g_object_unref (data->info);
5828 g_error_free (data->error);
5833 set_info_async_thread (GSimpleAsyncResult *res,
5835 GCancellable *cancellable)
5837 SetInfoAsyncData *data;
5839 data = g_simple_async_result_get_op_res_gpointer (res);
5842 data->res = g_file_set_attributes_from_info (G_FILE (object),
5850 g_file_real_set_attributes_async (GFile *file,
5852 GFileQueryInfoFlags flags,
5854 GCancellable *cancellable,
5855 GAsyncReadyCallback callback,
5858 GSimpleAsyncResult *res;
5859 SetInfoAsyncData *data;
5861 data = g_new0 (SetInfoAsyncData, 1);
5862 data->info = g_file_info_dup (info);
5863 data->flags = flags;
5865 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5866 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5868 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5869 g_object_unref (res);
5873 g_file_real_set_attributes_finish (GFile *file,
5878 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5879 SetInfoAsyncData *data;
5881 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5883 data = g_simple_async_result_get_op_res_gpointer (simple);
5886 *info = g_object_ref (data->info);
5888 if (error != NULL && data->error)
5889 *error = g_error_copy (data->error);
5895 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5897 GCancellable *cancellable)
5899 GError *error = NULL;
5902 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5905 g_simple_async_result_take_error (res, error);
5907 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5911 g_file_real_find_enclosing_mount_async (GFile *file,
5913 GCancellable *cancellable,
5914 GAsyncReadyCallback callback,
5917 GSimpleAsyncResult *res;
5919 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5921 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5922 g_object_unref (res);
5926 g_file_real_find_enclosing_mount_finish (GFile *file,
5930 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5933 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5935 if (g_simple_async_result_propagate_error (simple, error))
5938 mount = g_simple_async_result_get_op_res_gpointer (simple);
5939 return g_object_ref (mount);
5946 GFileCopyFlags flags;
5947 GFileProgressCallback progress_cb;
5948 gpointer progress_cb_data;
5949 GIOSchedulerJob *job;
5953 copy_async_data_free (CopyAsyncData *data)
5955 g_object_unref (data->source);
5956 g_object_unref (data->destination);
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 CopyAsyncData *data = user_data;
5985 ProgressData *progress;
5987 progress = g_new (ProgressData, 1);
5988 progress->data = data;
5989 progress->current_num_bytes = current_num_bytes;
5990 progress->total_num_bytes = total_num_bytes;
5992 g_io_scheduler_job_send_to_mainloop_async (data->job,
5993 copy_async_progress_in_main,
5999 copy_async_thread (GIOSchedulerJob *job,
6000 GCancellable *cancellable,
6003 GSimpleAsyncResult *res;
6004 CopyAsyncData *data;
6009 data = g_simple_async_result_get_op_res_gpointer (res);
6013 result = g_file_copy (data->source,
6017 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6022 g_simple_async_result_take_error (res, error);
6024 g_simple_async_result_complete_in_idle (res);
6030 g_file_real_copy_async (GFile *source,
6032 GFileCopyFlags flags,
6034 GCancellable *cancellable,
6035 GFileProgressCallback progress_callback,
6036 gpointer progress_callback_data,
6037 GAsyncReadyCallback callback,
6040 GSimpleAsyncResult *res;
6041 CopyAsyncData *data;
6043 data = g_new0 (CopyAsyncData, 1);
6044 data->source = g_object_ref (source);
6045 data->destination = g_object_ref (destination);
6046 data->flags = flags;
6047 data->progress_cb = progress_callback;
6048 data->progress_cb_data = progress_callback_data;
6050 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
6051 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
6053 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
6057 g_file_real_copy_finish (GFile *file,
6061 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
6063 if (g_simple_async_result_propagate_error (simple, error))
6070 /********************************************
6071 * Default VFS operations *
6072 ********************************************/
6075 * g_file_new_for_path:
6076 * @path: a string containing a relative or absolute path.
6077 * The string must be encoded in the glib filename encoding.
6079 * Constructs a #GFile for a given path. This operation never
6080 * fails, but the returned object might not support any I/O
6081 * operation if @path is malformed.
6083 * Returns: (transfer full): a new #GFile for the given @path.
6084 * Free the returned object with g_object_unref().
6087 g_file_new_for_path (const char *path)
6089 g_return_val_if_fail (path != NULL, NULL);
6091 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6095 * g_file_new_for_uri:
6096 * @uri: a UTF-8 string containing a URI
6098 * Constructs a #GFile for a given URI. This operation never
6099 * fails, but the returned object might not support any I/O
6100 * operation if @uri is malformed or if the uri type is
6103 * Returns: (transfer full): a new #GFile for the given @uri.
6104 * Free the returned object with g_object_unref().
6107 g_file_new_for_uri (const char *uri)
6109 g_return_val_if_fail (uri != NULL, NULL);
6111 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6116 * @tmpl: (type filename) (allow-none): Template for the file
6117 * name, as in g_file_open_tmp(), or %NULL for a default template
6118 * @iostream: (out): on return, a #GFileIOStream for the created file
6119 * @error: a #GError, or %NULL
6121 * Opens a file in the preferred directory for temporary files (as
6122 * returned by g_get_tmp_dir()) and returns a #GFile and
6123 * #GFileIOStream pointing to it.
6125 * @tmpl should be a string in the GLib file name encoding
6126 * containing a sequence of six 'X' characters, and containing no
6127 * directory components. If it is %NULL, a default template is used.
6129 * Unlike the other #GFile constructors, this will return %NULL if
6130 * a temporary file could not be created.
6132 * Returns: (transfer full): a new #GFile.
6133 * Free the returned object with g_object_unref().
6138 g_file_new_tmp (const char *tmpl,
6139 GFileIOStream **iostream,
6145 GFileOutputStream *output;
6147 g_return_val_if_fail (iostream != NULL, NULL);
6149 fd = g_file_open_tmp (tmpl, &path, error);
6153 file = g_file_new_for_path (path);
6155 output = _g_local_file_output_stream_new (fd);
6156 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6158 g_object_unref (output);
6165 * g_file_parse_name:
6166 * @parse_name: a file name or path to be parsed
6168 * Constructs a #GFile with the given @parse_name (i.e. something
6169 * given by g_file_get_parse_name()). This operation never fails,
6170 * but the returned object might not support any I/O operation if
6171 * the @parse_name cannot be parsed.
6173 * Returns: (transfer full): a new #GFile.
6176 g_file_parse_name (const char *parse_name)
6178 g_return_val_if_fail (parse_name != NULL, NULL);
6180 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6184 is_valid_scheme_character (char c)
6186 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6189 /* Following RFC 2396, valid schemes are built like:
6190 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6193 has_valid_scheme (const char *uri)
6199 if (!g_ascii_isalpha (*p))
6204 } while (is_valid_scheme_character (*p));
6210 * g_file_new_for_commandline_arg:
6211 * @arg: a command line string
6213 * Creates a #GFile with the given argument from the command line.
6214 * The value of @arg can be either a URI, an absolute path or a
6215 * relative path resolved relative to the current working directory.
6216 * This operation never fails, but the returned object might not
6217 * support any I/O operation if @arg points to a malformed path.
6219 * Returns: (transfer full): a new #GFile.
6220 * Free the returned object with g_object_unref().
6223 g_file_new_for_commandline_arg (const char *arg)
6229 g_return_val_if_fail (arg != NULL, NULL);
6231 if (g_path_is_absolute (arg))
6232 return g_file_new_for_path (arg);
6234 if (has_valid_scheme (arg))
6235 return g_file_new_for_uri (arg);
6237 current_dir = g_get_current_dir ();
6238 filename = g_build_filename (current_dir, arg, NULL);
6239 g_free (current_dir);
6241 file = g_file_new_for_path (filename);
6248 * g_file_mount_enclosing_volume:
6249 * @location: input #GFile
6250 * @flags: flags affecting the operation
6251 * @mount_operation: (allow-none): a #GMountOperation
6252 * or %NULL to avoid user interaction
6253 * @cancellable: (allow-none): optional #GCancellable object,
6255 * @callback: (allow-none): a #GAsyncReadyCallback to call
6256 * when the request is satisfied, or %NULL
6257 * @user_data: the data to pass to callback function
6259 * Starts a @mount_operation, mounting the volume that contains
6260 * the file @location.
6262 * When this operation has completed, @callback will be called with
6263 * @user_user data, and the operation can be finalized with
6264 * g_file_mount_enclosing_volume_finish().
6266 * If @cancellable is not %NULL, then the operation can be cancelled by
6267 * triggering the cancellable object from another thread. If the operation
6268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6271 g_file_mount_enclosing_volume (GFile *location,
6272 GMountMountFlags flags,
6273 GMountOperation *mount_operation,
6274 GCancellable *cancellable,
6275 GAsyncReadyCallback callback,
6280 g_return_if_fail (G_IS_FILE (location));
6282 iface = G_FILE_GET_IFACE (location);
6284 if (iface->mount_enclosing_volume == NULL)
6286 g_simple_async_report_error_in_idle (G_OBJECT (location),
6287 callback, user_data,
6288 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6289 _("volume doesn't implement mount"));
6294 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6299 * g_file_mount_enclosing_volume_finish:
6300 * @location: input #GFile
6301 * @result: a #GAsyncResult
6302 * @error: a #GError, or %NULL
6304 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6306 * Returns: %TRUE if successful. If an error has occurred,
6307 * this function will return %FALSE and set @error
6308 * appropriately if present.
6311 g_file_mount_enclosing_volume_finish (GFile *location,
6312 GAsyncResult *result,
6317 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6318 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6320 if (g_async_result_legacy_propagate_error (result, error))
6323 iface = G_FILE_GET_IFACE (location);
6325 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6328 /********************************************
6329 * Utility functions *
6330 ********************************************/
6333 * g_file_query_default_handler:
6334 * @file: a #GFile to open
6335 * @cancellable: optional #GCancellable object, %NULL to ignore
6336 * @error: a #GError, or %NULL
6338 * Returns the #GAppInfo that is registered as the default
6339 * application to handle the file specified by @file.
6341 * If @cancellable is not %NULL, then the operation can be cancelled by
6342 * triggering the cancellable object from another thread. If the operation
6343 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6345 * Returns: (transfer full): a #GAppInfo if the handle was found,
6346 * %NULL if there were errors.
6347 * When you are done with it, release it with g_object_unref()
6350 g_file_query_default_handler (GFile *file,
6351 GCancellable *cancellable,
6355 const char *content_type;
6360 uri_scheme = g_file_get_uri_scheme (file);
6361 if (uri_scheme && uri_scheme[0] != '\0')
6363 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6364 g_free (uri_scheme);
6366 if (appinfo != NULL)
6370 info = g_file_query_info (file,
6371 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6380 content_type = g_file_info_get_content_type (info);
6383 /* Don't use is_native(), as we want to support fuse paths if available */
6384 path = g_file_get_path (file);
6385 appinfo = g_app_info_get_default_for_type (content_type,
6390 g_object_unref (info);
6392 if (appinfo != NULL)
6395 g_set_error_literal (error, G_IO_ERROR,
6396 G_IO_ERROR_NOT_SUPPORTED,
6397 _("No application is registered as handling this file"));
6401 #define GET_CONTENT_BLOCK_SIZE 8192
6404 * g_file_load_contents:
6405 * @file: input #GFile
6406 * @cancellable: optional #GCancellable object, %NULL to ignore
6407 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6408 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6409 * or %NULL if the length is not needed
6410 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6411 * or %NULL if the entity tag is not needed
6412 * @error: a #GError, or %NULL
6414 * Loads the content of the file into memory. The data is always
6415 * zero-terminated, but this is not included in the resultant @length.
6416 * The returned @content should be freed with g_free() when no longer
6419 * If @cancellable is not %NULL, then the operation can be cancelled by
6420 * triggering the cancellable object from another thread. If the operation
6421 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6423 * Returns: %TRUE if the @file's contents were successfully loaded.
6424 * %FALSE if there were errors.
6427 g_file_load_contents (GFile *file,
6428 GCancellable *cancellable,
6434 GFileInputStream *in;
6435 GByteArray *content;
6440 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6441 g_return_val_if_fail (contents != NULL, FALSE);
6443 in = g_file_read (file, cancellable, error);
6447 content = g_byte_array_new ();
6450 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6451 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6452 content->data + pos,
6453 GET_CONTENT_BLOCK_SIZE,
6454 cancellable, error)) > 0)
6457 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6464 info = g_file_input_stream_query_info (in,
6465 G_FILE_ATTRIBUTE_ETAG_VALUE,
6470 *etag_out = g_strdup (g_file_info_get_etag (info));
6471 g_object_unref (info);
6475 /* Ignore errors on close */
6476 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6477 g_object_unref (in);
6481 /* error is set already */
6482 g_byte_array_free (content, TRUE);
6489 /* Zero terminate (we got an extra byte allocated for this */
6490 content->data[pos] = 0;
6492 *contents = (char *)g_byte_array_free (content, FALSE);
6500 GCancellable *cancellable;
6501 GFileReadMoreCallback read_more_callback;
6502 GAsyncReadyCallback callback;
6504 GByteArray *content;
6511 load_contents_data_free (LoadContentsData *data)
6514 g_error_free (data->error);
6515 if (data->cancellable)
6516 g_object_unref (data->cancellable);
6518 g_byte_array_free (data->content, TRUE);
6519 g_free (data->etag);
6520 g_object_unref (data->file);
6525 load_contents_close_callback (GObject *obj,
6526 GAsyncResult *close_res,
6529 GInputStream *stream = G_INPUT_STREAM (obj);
6530 LoadContentsData *data = user_data;
6531 GSimpleAsyncResult *res;
6533 /* Ignore errors here, we're only reading anyway */
6534 g_input_stream_close_finish (stream, close_res, NULL);
6535 g_object_unref (stream);
6537 res = g_simple_async_result_new (G_OBJECT (data->file),
6540 g_file_load_contents_async);
6541 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6542 g_simple_async_result_complete (res);
6543 g_object_unref (res);
6547 load_contents_fstat_callback (GObject *obj,
6548 GAsyncResult *stat_res,
6551 GInputStream *stream = G_INPUT_STREAM (obj);
6552 LoadContentsData *data = user_data;
6555 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6559 data->etag = g_strdup (g_file_info_get_etag (info));
6560 g_object_unref (info);
6563 g_input_stream_close_async (stream, 0,
6565 load_contents_close_callback, data);
6569 load_contents_read_callback (GObject *obj,
6570 GAsyncResult *read_res,
6573 GInputStream *stream = G_INPUT_STREAM (obj);
6574 LoadContentsData *data = user_data;
6575 GError *error = NULL;
6578 read_size = g_input_stream_read_finish (stream, read_res, &error);
6582 /* Error or EOF, close the file */
6583 data->error = error;
6584 g_input_stream_close_async (stream, 0,
6586 load_contents_close_callback, data);
6588 else if (read_size == 0)
6590 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6591 G_FILE_ATTRIBUTE_ETAG_VALUE,
6594 load_contents_fstat_callback,
6597 else if (read_size > 0)
6599 data->pos += read_size;
6601 g_byte_array_set_size (data->content,
6602 data->pos + GET_CONTENT_BLOCK_SIZE);
6605 if (data->read_more_callback &&
6606 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6607 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6608 G_FILE_ATTRIBUTE_ETAG_VALUE,
6611 load_contents_fstat_callback,
6614 g_input_stream_read_async (stream,
6615 data->content->data + data->pos,
6616 GET_CONTENT_BLOCK_SIZE,
6619 load_contents_read_callback,
6625 load_contents_open_callback (GObject *obj,
6626 GAsyncResult *open_res,
6629 GFile *file = G_FILE (obj);
6630 GFileInputStream *stream;
6631 LoadContentsData *data = user_data;
6632 GError *error = NULL;
6633 GSimpleAsyncResult *res;
6635 stream = g_file_read_finish (file, open_res, &error);
6639 g_byte_array_set_size (data->content,
6640 data->pos + GET_CONTENT_BLOCK_SIZE);
6641 g_input_stream_read_async (G_INPUT_STREAM (stream),
6642 data->content->data + data->pos,
6643 GET_CONTENT_BLOCK_SIZE,
6646 load_contents_read_callback,
6651 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
6655 g_simple_async_result_complete (res);
6656 load_contents_data_free (data);
6657 g_object_unref (res);
6662 * g_file_load_partial_contents_async: (skip)
6663 * @file: input #GFile
6664 * @cancellable: optional #GCancellable object, %NULL to ignore
6665 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6666 * and to specify whether further data should be read
6667 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6668 * @user_data: the data to pass to the callback functions
6670 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6671 * be used to stop reading from the file when appropriate, else this
6672 * function will behave exactly as g_file_load_contents_async(). This
6673 * operation can be finished by g_file_load_partial_contents_finish().
6675 * Users of this function should be aware that @user_data is passed to
6676 * both the @read_more_callback and the @callback.
6678 * If @cancellable is not %NULL, then the operation can be cancelled by
6679 * triggering the cancellable object from another thread. If the operation
6680 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6683 g_file_load_partial_contents_async (GFile *file,
6684 GCancellable *cancellable,
6685 GFileReadMoreCallback read_more_callback,
6686 GAsyncReadyCallback callback,
6689 LoadContentsData *data;
6691 g_return_if_fail (G_IS_FILE (file));
6693 data = g_new0 (LoadContentsData, 1);
6696 data->cancellable = g_object_ref (cancellable);
6697 data->read_more_callback = read_more_callback;
6698 data->callback = callback;
6699 data->user_data = user_data;
6700 data->content = g_byte_array_new ();
6701 data->file = g_object_ref (file);
6703 g_file_read_async (file,
6706 load_contents_open_callback,
6711 * g_file_load_partial_contents_finish:
6712 * @file: input #GFile
6713 * @res: a #GAsyncResult
6714 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6715 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6716 * or %NULL if the length is not needed
6717 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6718 * or %NULL if the entity tag is not needed
6719 * @error: a #GError, or %NULL
6721 * Finishes an asynchronous partial load operation that was started
6722 * with g_file_load_partial_contents_async(). The data is always
6723 * zero-terminated, but this is not included in the resultant @length.
6724 * The returned @content should be freed with g_free() when no longer
6727 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6728 * present, it will be set appropriately.
6731 g_file_load_partial_contents_finish (GFile *file,
6738 GSimpleAsyncResult *simple;
6739 LoadContentsData *data;
6741 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6742 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6743 g_return_val_if_fail (contents != NULL, FALSE);
6745 simple = G_SIMPLE_ASYNC_RESULT (res);
6747 if (g_simple_async_result_propagate_error (simple, error))
6750 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6752 data = g_simple_async_result_get_op_res_gpointer (simple);
6756 g_propagate_error (error, data->error);
6765 *length = data->pos;
6769 *etag_out = data->etag;
6773 /* Zero terminate */
6774 g_byte_array_set_size (data->content, data->pos + 1);
6775 data->content->data[data->pos] = 0;
6777 *contents = (char *)g_byte_array_free (data->content, FALSE);
6778 data->content = NULL;
6784 * g_file_load_contents_async:
6785 * @file: input #GFile
6786 * @cancellable: optional #GCancellable object, %NULL to ignore
6787 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6788 * @user_data: the data to pass to callback function
6790 * Starts an asynchronous load of the @file's contents.
6792 * For more details, see g_file_load_contents() which is
6793 * the synchronous version of this call.
6795 * When the load operation has completed, @callback will be called
6796 * with @user data. To finish the operation, call
6797 * g_file_load_contents_finish() with the #GAsyncResult returned by
6800 * If @cancellable is not %NULL, then the operation can be cancelled by
6801 * triggering the cancellable object from another thread. If the operation
6802 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6805 g_file_load_contents_async (GFile *file,
6806 GCancellable *cancellable,
6807 GAsyncReadyCallback callback,
6810 g_file_load_partial_contents_async (file,
6813 callback, user_data);
6817 * g_file_load_contents_finish:
6818 * @file: input #GFile
6819 * @res: a #GAsyncResult
6820 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6821 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6822 * or %NULL if the length is not needed
6823 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6824 * or %NULL if the entity tag is not needed
6825 * @error: a #GError, or %NULL
6827 * Finishes an asynchronous load of the @file's contents.
6828 * The contents are placed in @contents, and @length is set to the
6829 * size of the @contents string. The @content should be freed with
6830 * g_free() when no longer needed. If @etag_out is present, it will be
6831 * set to the new entity tag for the @file.
6833 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6834 * present, it will be set appropriately.
6837 g_file_load_contents_finish (GFile *file,
6844 return g_file_load_partial_contents_finish (file,
6853 * g_file_replace_contents:
6854 * @file: input #GFile
6855 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6856 * @length: the length of @contents in bytes
6857 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6858 * for the document, or %NULL
6859 * @make_backup: %TRUE if a backup should be created
6860 * @flags: a set of #GFileCreateFlags
6861 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6862 * for the document. This should be freed with g_free() when no longer
6864 * @cancellable: optional #GCancellable object, %NULL to ignore
6865 * @error: a #GError, or %NULL
6867 * Replaces the contents of @file with @contents of @length bytes.
6869 * If @etag is specified (not %NULL), any existing file must have that etag,
6870 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6872 * If @make_backup is %TRUE, this function will attempt to make a backup
6875 * If @cancellable is not %NULL, then the operation can be cancelled by
6876 * triggering the cancellable object from another thread. If the operation
6877 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6879 * The returned @new_etag can be used to verify that the file hasn't
6880 * changed the next time it is saved over.
6882 * Returns: %TRUE if successful. If an error has occurred, this function
6883 * will return %FALSE and set @error appropriately if present.
6886 g_file_replace_contents (GFile *file,
6887 const char *contents,
6890 gboolean make_backup,
6891 GFileCreateFlags flags,
6893 GCancellable *cancellable,
6896 GFileOutputStream *out;
6897 gsize pos, remainder;
6901 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6902 g_return_val_if_fail (contents != NULL, FALSE);
6904 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6910 while (remainder > 0 &&
6911 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6913 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6921 if (remainder > 0 && res < 0)
6923 /* Ignore errors on close */
6924 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6925 g_object_unref (out);
6927 /* error is set already */
6931 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6934 *new_etag = g_file_output_stream_get_etag (out);
6936 g_object_unref (out);
6944 GCancellable *cancellable;
6945 GAsyncReadyCallback callback;
6947 const char *content;
6951 } ReplaceContentsData;
6954 replace_contents_data_free (ReplaceContentsData *data)
6957 g_error_free (data->error);
6958 if (data->cancellable)
6959 g_object_unref (data->cancellable);
6960 g_object_unref (data->file);
6961 g_free (data->etag);
6966 replace_contents_close_callback (GObject *obj,
6967 GAsyncResult *close_res,
6970 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6971 ReplaceContentsData *data = user_data;
6972 GSimpleAsyncResult *res;
6974 /* Ignore errors here, we're only reading anyway */
6975 g_output_stream_close_finish (stream, close_res, NULL);
6976 g_object_unref (stream);
6978 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6980 res = g_simple_async_result_new (G_OBJECT (data->file),
6983 g_file_replace_contents_async);
6984 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6985 g_simple_async_result_complete (res);
6986 g_object_unref (res);
6990 replace_contents_write_callback (GObject *obj,
6991 GAsyncResult *read_res,
6994 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6995 ReplaceContentsData *data = user_data;
6996 GError *error = NULL;
6999 write_size = g_output_stream_write_finish (stream, read_res, &error);
7001 if (write_size <= 0)
7003 /* Error or EOF, close the file */
7005 data->error = error;
7006 g_output_stream_close_async (stream, 0,
7008 replace_contents_close_callback, data);
7010 else if (write_size > 0)
7012 data->pos += write_size;
7014 if (data->pos >= data->length)
7015 g_output_stream_close_async (stream, 0,
7017 replace_contents_close_callback, data);
7019 g_output_stream_write_async (stream,
7020 data->content + data->pos,
7021 data->length - data->pos,
7024 replace_contents_write_callback,
7030 replace_contents_open_callback (GObject *obj,
7031 GAsyncResult *open_res,
7034 GFile *file = G_FILE (obj);
7035 GFileOutputStream *stream;
7036 ReplaceContentsData *data = user_data;
7037 GError *error = NULL;
7038 GSimpleAsyncResult *res;
7040 stream = g_file_replace_finish (file, open_res, &error);
7044 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7045 data->content + data->pos,
7046 data->length - data->pos,
7049 replace_contents_write_callback,
7054 res = g_simple_async_result_new_take_error (G_OBJECT (data->file),
7058 g_simple_async_result_complete (res);
7059 replace_contents_data_free (data);
7060 g_object_unref (res);
7065 * g_file_replace_contents_async:
7066 * @file: input #GFile
7067 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7068 * @length: the length of @contents in bytes
7069 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7070 * @make_backup: %TRUE if a backup should be created
7071 * @flags: a set of #GFileCreateFlags
7072 * @cancellable: optional #GCancellable object, %NULL to ignore
7073 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7074 * @user_data: the data to pass to callback function
7076 * Starts an asynchronous replacement of @file with the given
7077 * @contents of @length bytes. @etag will replace the document's
7078 * current entity tag.
7080 * When this operation has completed, @callback will be called with
7081 * @user_user data, and the operation can be finalized with
7082 * g_file_replace_contents_finish().
7084 * If @cancellable is not %NULL, then the operation can be cancelled by
7085 * triggering the cancellable object from another thread. If the operation
7086 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7088 * If @make_backup is %TRUE, this function will attempt to
7089 * make a backup of @file.
7092 g_file_replace_contents_async (GFile *file,
7093 const char *contents,
7096 gboolean make_backup,
7097 GFileCreateFlags flags,
7098 GCancellable *cancellable,
7099 GAsyncReadyCallback callback,
7102 ReplaceContentsData *data;
7104 g_return_if_fail (G_IS_FILE (file));
7105 g_return_if_fail (contents != NULL);
7107 data = g_new0 (ReplaceContentsData, 1);
7110 data->cancellable = g_object_ref (cancellable);
7111 data->callback = callback;
7112 data->user_data = user_data;
7113 data->content = contents;
7114 data->length = length;
7116 data->file = g_object_ref (file);
7118 g_file_replace_async (file,
7124 replace_contents_open_callback,
7129 * g_file_replace_contents_finish:
7130 * @file: input #GFile
7131 * @res: a #GAsyncResult
7132 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7133 * for the document. This should be freed with g_free() when it is no
7134 * longer needed, or %NULL
7135 * @error: a #GError, or %NULL
7137 * Finishes an asynchronous replace of the given @file. See
7138 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7139 * tag for the document, if present.
7141 * Returns: %TRUE on success, %FALSE on failure.
7144 g_file_replace_contents_finish (GFile *file,
7149 GSimpleAsyncResult *simple;
7150 ReplaceContentsData *data;
7152 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7153 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
7155 simple = G_SIMPLE_ASYNC_RESULT (res);
7157 if (g_simple_async_result_propagate_error (simple, error))
7160 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
7162 data = g_simple_async_result_get_op_res_gpointer (simple);
7166 g_propagate_error (error, data->error);
7173 *new_etag = data->etag;
7174 data->etag = NULL; /* Take ownership */
7181 * g_file_start_mountable:
7182 * @file: input #GFile
7183 * @flags: flags affecting the operation
7184 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7185 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7186 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7187 * @user_data: the data to pass to callback function
7189 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7190 * Using @start_operation, you can request callbacks when, for instance,
7191 * passwords are needed during authentication.
7193 * If @cancellable is not %NULL, then the operation can be cancelled by
7194 * triggering the cancellable object from another thread. If the operation
7195 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7197 * When the operation is finished, @callback will be called.
7198 * You can then call g_file_mount_mountable_finish() to get
7199 * the result of the operation.
7204 g_file_start_mountable (GFile *file,
7205 GDriveStartFlags flags,
7206 GMountOperation *start_operation,
7207 GCancellable *cancellable,
7208 GAsyncReadyCallback callback,
7213 g_return_if_fail (G_IS_FILE (file));
7215 iface = G_FILE_GET_IFACE (file);
7217 if (iface->start_mountable == NULL)
7219 g_simple_async_report_error_in_idle (G_OBJECT (file),
7223 G_IO_ERROR_NOT_SUPPORTED,
7224 _("Operation not supported"));
7228 (* iface->start_mountable) (file,
7237 * g_file_start_mountable_finish:
7238 * @file: input #GFile
7239 * @result: a #GAsyncResult
7240 * @error: a #GError, or %NULL
7242 * Finishes a start operation. See g_file_start_mountable() for details.
7244 * Finish an asynchronous start operation that was started
7245 * with g_file_start_mountable().
7247 * Returns: %TRUE if the operation finished successfully. %FALSE
7253 g_file_start_mountable_finish (GFile *file,
7254 GAsyncResult *result,
7259 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7260 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7262 if (g_async_result_legacy_propagate_error (result, error))
7265 iface = G_FILE_GET_IFACE (file);
7266 return (* iface->start_mountable_finish) (file, result, error);
7270 * g_file_stop_mountable:
7271 * @file: input #GFile
7272 * @flags: flags affecting the operation
7273 * @mount_operation: (allow-none): a #GMountOperation,
7274 * or %NULL to avoid user interaction.
7275 * @cancellable: (allow-none): optional #GCancellable object,
7277 * @callback: (allow-none): a #GAsyncReadyCallback to call
7278 * when the request is satisfied, or %NULL
7279 * @user_data: the data to pass to callback function
7281 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7283 * If @cancellable is not %NULL, then the operation can be cancelled by
7284 * triggering the cancellable object from another thread. If the operation
7285 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7287 * When the operation is finished, @callback will be called.
7288 * You can then call g_file_stop_mountable_finish() to get
7289 * the result of the operation.
7294 g_file_stop_mountable (GFile *file,
7295 GMountUnmountFlags flags,
7296 GMountOperation *mount_operation,
7297 GCancellable *cancellable,
7298 GAsyncReadyCallback callback,
7303 g_return_if_fail (G_IS_FILE (file));
7305 iface = G_FILE_GET_IFACE (file);
7307 if (iface->stop_mountable == NULL)
7309 g_simple_async_report_error_in_idle (G_OBJECT (file),
7313 G_IO_ERROR_NOT_SUPPORTED,
7314 _("Operation not supported"));
7318 (* iface->stop_mountable) (file,
7327 * g_file_stop_mountable_finish:
7328 * @file: input #GFile
7329 * @result: a #GAsyncResult
7330 * @error: a #GError, or %NULL
7332 * Finishes an stop operation, see g_file_stop_mountable() for details.
7334 * Finish an asynchronous stop operation that was started
7335 * with g_file_stop_mountable().
7337 * Returns: %TRUE if the operation finished successfully.
7343 g_file_stop_mountable_finish (GFile *file,
7344 GAsyncResult *result,
7349 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7350 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7352 if (g_async_result_legacy_propagate_error (result, error))
7355 iface = G_FILE_GET_IFACE (file);
7356 return (* iface->stop_mountable_finish) (file, result, error);
7360 * g_file_poll_mountable:
7361 * @file: input #GFile
7362 * @cancellable: optional #GCancellable object, %NULL to ignore
7363 * @callback: (allow-none): a #GAsyncReadyCallback to call
7364 * when the request is satisfied, or %NULL
7365 * @user_data: the data to pass to callback function
7367 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7369 * If @cancellable is not %NULL, then the operation can be cancelled by
7370 * triggering the cancellable object from another thread. If the operation
7371 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7373 * When the operation is finished, @callback will be called.
7374 * You can then call g_file_mount_mountable_finish() to get
7375 * the result of the operation.
7380 g_file_poll_mountable (GFile *file,
7381 GCancellable *cancellable,
7382 GAsyncReadyCallback callback,
7387 g_return_if_fail (G_IS_FILE (file));
7389 iface = G_FILE_GET_IFACE (file);
7391 if (iface->poll_mountable == NULL)
7393 g_simple_async_report_error_in_idle (G_OBJECT (file),
7397 G_IO_ERROR_NOT_SUPPORTED,
7398 _("Operation not supported"));
7402 (* iface->poll_mountable) (file,
7409 * g_file_poll_mountable_finish:
7410 * @file: input #GFile
7411 * @result: a #GAsyncResult
7412 * @error: a #GError, or %NULL
7414 * Finishes a poll operation. See g_file_poll_mountable() for details.
7416 * Finish an asynchronous poll operation that was polled
7417 * with g_file_poll_mountable().
7419 * Returns: %TRUE if the operation finished successfully. %FALSE
7425 g_file_poll_mountable_finish (GFile *file,
7426 GAsyncResult *result,
7431 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7432 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7434 if (g_async_result_legacy_propagate_error (result, error))
7437 iface = G_FILE_GET_IFACE (file);
7438 return (* iface->poll_mountable_finish) (file, result, error);
7442 * g_file_supports_thread_contexts:
7445 * Checks if @file supports <link
7446 * linkend="g-main-context-push-thread-default-context">thread-default
7447 * contexts</link>. If this returns %FALSE, you cannot perform
7448 * asynchronous operations on @file in a thread that has a
7449 * thread-default context.
7451 * Returns: Whether or not @file supports thread-default contexts.
7456 g_file_supports_thread_contexts (GFile *file)
7460 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7462 iface = G_FILE_GET_IFACE (file);
7463 return iface->supports_thread_contexts;