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