1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
33 #include <sys/types.h>
40 #include "gfileattribute-priv.h"
41 #include "gfiledescriptorbased.h"
42 #include "gpollfilemonitor.h"
44 #include "gfileinputstream.h"
45 #include "gfileoutputstream.h"
46 #include "glocalfileoutputstream.h"
47 #include "glocalfileiostream.h"
48 #include "gcancellable.h"
49 #include "gasyncresult.h"
56 * @short_description: File and Directory Handling
58 * @see_also: #GFileInfo, #GFileEnumerator
60 * #GFile is a high level abstraction for manipulating files on a
61 * virtual file system. #GFiles are lightweight, immutable objects
62 * that do no I/O upon creation. It is necessary to understand that
63 * #GFile objects do not represent files, merely an identifier for a
64 * file. All file content I/O is implemented as streaming operations
65 * (see #GInputStream and #GOutputStream).
67 * To construct a #GFile, you can use:
69 * <member>g_file_new_for_path() if you have a path.</member>
70 * <member>g_file_new_for_uri() if you have a URI.</member>
71 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
72 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
73 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
76 * One way to think of a #GFile is as an abstraction of a pathname. For
77 * normal files the system pathname is what is stored internally, but as
78 * #GFiles are extensible it could also be something else that corresponds
79 * to a pathname in a userspace implementation of a filesystem.
81 * #GFiles make up hierarchies of directories and files that correspond to
82 * the files on a filesystem. You can move through the file system with
83 * #GFile using g_file_get_parent() to get an identifier for the parent
84 * directory, g_file_get_child() to get a child within a directory,
85 * g_file_resolve_relative_path() to resolve a relative path between two
86 * #GFiles. There can be multiple hierarchies, so you may not end up at
87 * the same root if you repeatedly call g_file_get_parent() on two different
90 * All #GFiles have a basename (get with g_file_get_basename()). These names
91 * are byte strings that are used to identify the file on the filesystem
92 * (relative to its parent directory) and there is no guarantees that they
93 * have any particular charset encoding or even make any sense at all. If
94 * you want to use filenames in a user interface you should use the display
95 * name that you can get by requesting the
96 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
97 * This is guaranteed to be in UTF-8 and can be used in a user interface.
98 * But always store the real basename or the #GFile to use to actually
99 * access the file, because there is no way to go from a display name to
102 * Using #GFile as an identifier has the same weaknesses as using a path
103 * in that there may be multiple aliases for the same file. For instance,
104 * hard or soft links may cause two different #GFiles to refer to the same
105 * file. Other possible causes for aliases are: case insensitive filesystems,
106 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
107 * check if two #GFiles point to the same file you can query for the
108 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
109 * canonicalization of pathnames passed in, so that trivial differences in
110 * the path string used at creation (duplicated slashes, slash at end of
111 * path, "." or ".." path segments, etc) does not create different #GFiles.
113 * Many #GFile operations have both synchronous and asynchronous versions
114 * to suit your application. Asynchronous versions of synchronous functions
115 * simply have _async() appended to their function names. The asynchronous
116 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
117 * the operation, producing a GAsyncResult which is then passed to the
118 * function's matching _finish() operation.
120 * Some #GFile operations do not have synchronous analogs, as they may
121 * take a very long time to finish, and blocking may leave an application
122 * unusable. Notable cases include:
124 * <member>g_file_mount_mountable() to mount a mountable file.</member>
125 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
126 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
129 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
130 * One notable feature of #GFiles are entity tags, or "etags" for
131 * short. Entity tags are somewhat like a more abstract version of the
132 * traditional mtime, and can be used to quickly determine if the file has
133 * been modified from the version on the file system. See the HTTP 1.1
134 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
135 * for HTTP Etag headers, which are a very similar concept.
139 static void g_file_real_query_info_async (GFile *file,
140 const char *attributes,
141 GFileQueryInfoFlags flags,
143 GCancellable *cancellable,
144 GAsyncReadyCallback callback,
146 static GFileInfo * g_file_real_query_info_finish (GFile *file,
149 static void g_file_real_query_filesystem_info_async (GFile *file,
150 const char *attributes,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
155 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
158 static void g_file_real_enumerate_children_async (GFile *file,
159 const char *attributes,
160 GFileQueryInfoFlags flags,
162 GCancellable *cancellable,
163 GAsyncReadyCallback callback,
165 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
168 static void g_file_real_read_async (GFile *file,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
173 static GFileInputStream * g_file_real_read_finish (GFile *file,
176 static void g_file_real_append_to_async (GFile *file,
177 GFileCreateFlags flags,
179 GCancellable *cancellable,
180 GAsyncReadyCallback callback,
182 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
185 static void g_file_real_create_async (GFile *file,
186 GFileCreateFlags flags,
188 GCancellable *cancellable,
189 GAsyncReadyCallback callback,
191 static GFileOutputStream *g_file_real_create_finish (GFile *file,
194 static void g_file_real_replace_async (GFile *file,
196 gboolean make_backup,
197 GFileCreateFlags flags,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
202 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
205 static void g_file_real_delete_async (GFile *file,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
210 static gboolean g_file_real_delete_finish (GFile *file,
213 static void g_file_real_open_readwrite_async (GFile *file,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
221 static void g_file_real_create_readwrite_async (GFile *file,
222 GFileCreateFlags flags,
224 GCancellable *cancellable,
225 GAsyncReadyCallback callback,
227 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
230 static void g_file_real_replace_readwrite_async (GFile *file,
232 gboolean make_backup,
233 GFileCreateFlags flags,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
238 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
241 static gboolean g_file_real_set_attributes_from_info (GFile *file,
243 GFileQueryInfoFlags flags,
244 GCancellable *cancellable,
246 static void g_file_real_set_display_name_async (GFile *file,
247 const char *display_name,
249 GCancellable *cancellable,
250 GAsyncReadyCallback callback,
252 static GFile * g_file_real_set_display_name_finish (GFile *file,
255 static void g_file_real_set_attributes_async (GFile *file,
257 GFileQueryInfoFlags flags,
259 GCancellable *cancellable,
260 GAsyncReadyCallback callback,
262 static gboolean g_file_real_set_attributes_finish (GFile *file,
266 static void g_file_real_find_enclosing_mount_async (GFile *file,
268 GCancellable *cancellable,
269 GAsyncReadyCallback callback,
271 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
274 static void g_file_real_copy_async (GFile *source,
276 GFileCopyFlags flags,
278 GCancellable *cancellable,
279 GFileProgressCallback progress_callback,
280 gpointer progress_callback_data,
281 GAsyncReadyCallback callback,
283 static gboolean g_file_real_copy_finish (GFile *file,
287 typedef GFileIface GFileInterface;
288 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
291 g_file_default_init (GFileIface *iface)
293 iface->enumerate_children_async = g_file_real_enumerate_children_async;
294 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
295 iface->set_display_name_async = g_file_real_set_display_name_async;
296 iface->set_display_name_finish = g_file_real_set_display_name_finish;
297 iface->query_info_async = g_file_real_query_info_async;
298 iface->query_info_finish = g_file_real_query_info_finish;
299 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
300 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
301 iface->set_attributes_async = g_file_real_set_attributes_async;
302 iface->set_attributes_finish = g_file_real_set_attributes_finish;
303 iface->read_async = g_file_real_read_async;
304 iface->read_finish = g_file_real_read_finish;
305 iface->append_to_async = g_file_real_append_to_async;
306 iface->append_to_finish = g_file_real_append_to_finish;
307 iface->create_async = g_file_real_create_async;
308 iface->create_finish = g_file_real_create_finish;
309 iface->replace_async = g_file_real_replace_async;
310 iface->replace_finish = g_file_real_replace_finish;
311 iface->delete_file_async = g_file_real_delete_async;
312 iface->delete_file_finish = g_file_real_delete_finish;
313 iface->open_readwrite_async = g_file_real_open_readwrite_async;
314 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
315 iface->create_readwrite_async = g_file_real_create_readwrite_async;
316 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
317 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
318 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
319 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
320 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
321 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
322 iface->copy_async = g_file_real_copy_async;
323 iface->copy_finish = g_file_real_copy_finish;
329 * @file: input #GFile
331 * Checks to see if a file is native to the platform.
333 * A native file s one expressed in the platform-native filename format,
334 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
335 * as it might be on a locally mounted remote filesystem.
337 * On some systems non-native files may be available using the native
338 * filesystem via a userspace filesystem (FUSE), in these cases this call
339 * will return %FALSE, but g_file_get_path() will still return a native path.
341 * This call does no blocking I/O.
343 * Returns: %TRUE if @file is native
346 g_file_is_native (GFile *file)
350 g_return_val_if_fail (G_IS_FILE (file), FALSE);
352 iface = G_FILE_GET_IFACE (file);
354 return (* iface->is_native) (file);
359 * g_file_has_uri_scheme:
360 * @file: input #GFile
361 * @uri_scheme: a string containing a URI scheme
363 * Checks to see if a #GFile has a given URI scheme.
365 * This call does no blocking I/O.
367 * Returns: %TRUE if #GFile's backend supports the
368 * given URI scheme, %FALSE if URI scheme is %NULL,
369 * not supported, or #GFile is invalid.
372 g_file_has_uri_scheme (GFile *file,
373 const char *uri_scheme)
377 g_return_val_if_fail (G_IS_FILE (file), FALSE);
378 g_return_val_if_fail (uri_scheme != NULL, FALSE);
380 iface = G_FILE_GET_IFACE (file);
382 return (* iface->has_uri_scheme) (file, uri_scheme);
387 * g_file_get_uri_scheme:
388 * @file: input #GFile
390 * Gets the URI scheme for a #GFile.
391 * RFC 3986 decodes the scheme as:
393 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
395 * Common schemes include "file", "http", "ftp", etc.
397 * This call does no blocking I/O.
399 * Returns: a string containing the URI scheme for the given
400 * #GFile. The returned string should be freed with g_free()
401 * when no longer needed.
404 g_file_get_uri_scheme (GFile *file)
408 g_return_val_if_fail (G_IS_FILE (file), NULL);
410 iface = G_FILE_GET_IFACE (file);
412 return (* iface->get_uri_scheme) (file);
417 * g_file_get_basename:
418 * @file: input #GFile
420 * Gets the base name (the last component of the path) for a given #GFile.
422 * If called for the top level of a system (such as the filesystem root
423 * or a uri like sftp://host/) it will return a single directory separator
424 * (and on Windows, possibly a drive letter).
426 * The base name is a byte string (not UTF-8). It has no defined encoding
427 * or rules other than it may not contain zero bytes. If you want to use
428 * filenames in a user interface you should use the display name that you
429 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
430 * attribute with g_file_query_info().
432 * This call does no blocking I/O.
434 * Returns: string containing the #GFile's base name, or %NULL
435 * if given #GFile is invalid. The returned string should be
436 * freed with g_free() when no longer needed.
439 g_file_get_basename (GFile *file)
443 g_return_val_if_fail (G_IS_FILE (file), NULL);
445 iface = G_FILE_GET_IFACE (file);
447 return (* iface->get_basename) (file);
452 * @file: input #GFile
454 * Gets the local pathname for #GFile, if one exists.
456 * This call does no blocking I/O.
458 * Returns: string containing the #GFile's path, or %NULL if
459 * no such path exists. The returned string should be
460 * freed with g_free() when no longer needed.
463 g_file_get_path (GFile *file)
467 g_return_val_if_fail (G_IS_FILE (file), NULL);
469 iface = G_FILE_GET_IFACE (file);
471 return (* iface->get_path) (file);
476 * @file: input #GFile
478 * Gets the URI for the @file.
480 * This call does no blocking I/O.
482 * Returns: a string containing the #GFile's URI.
483 * The returned string should be freed with g_free()
484 * when no longer needed.
487 g_file_get_uri (GFile *file)
491 g_return_val_if_fail (G_IS_FILE (file), NULL);
493 iface = G_FILE_GET_IFACE (file);
495 return (* iface->get_uri) (file);
499 * g_file_get_parse_name:
500 * @file: input #GFile
502 * Gets the parse name of the @file.
503 * A parse name is a UTF-8 string that describes the
504 * file such that one can get the #GFile back using
505 * g_file_parse_name().
507 * This is generally used to show the #GFile as a nice
508 * full-pathname kind of string in a user interface,
509 * like in a location entry.
511 * For local files with names that can safely be converted
512 * to UTF-8 the pathname is used, otherwise the IRI is used
513 * (a form of URI that allows UTF-8 characters unescaped).
515 * This call does no blocking I/O.
517 * Returns: a string containing the #GFile's parse name.
518 * The returned string should be freed with g_free()
519 * when no longer needed.
522 g_file_get_parse_name (GFile *file)
526 g_return_val_if_fail (G_IS_FILE (file), NULL);
528 iface = G_FILE_GET_IFACE (file);
530 return (* iface->get_parse_name) (file);
535 * @file: input #GFile
537 * Duplicates a #GFile handle. This operation does not duplicate
538 * the actual file or directory represented by the #GFile; see
539 * g_file_copy() if attempting to copy a file.
541 * This call does no blocking I/O.
543 * Returns: (transfer full): a new #GFile that is a duplicate
544 * of the given #GFile.
547 g_file_dup (GFile *file)
551 g_return_val_if_fail (G_IS_FILE (file), NULL);
553 iface = G_FILE_GET_IFACE (file);
555 return (* iface->dup) (file);
560 * @file: (type GFile): #gconstpointer to a #GFile
562 * Creates a hash value for a #GFile.
564 * This call does no blocking I/O.
567 * Returns: 0 if @file is not a valid #GFile, otherwise an
568 * integer that can be used as hash value for the #GFile.
569 * This function is intended for easily hashing a #GFile to
570 * add to a #GHashTable or similar data structure.
573 g_file_hash (gconstpointer file)
577 g_return_val_if_fail (G_IS_FILE (file), 0);
579 iface = G_FILE_GET_IFACE (file);
581 return (* iface->hash) ((GFile *)file);
586 * @file1: the first #GFile
587 * @file2: the second #GFile
589 * Checks equality of two given #GFiles.
591 * Note that two #GFiles that differ can still refer to the same
592 * file on the filesystem due to various forms of filename
595 * This call does no blocking I/O.
597 * Returns: %TRUE if @file1 and @file2 are equal.
598 * %FALSE if either is not a #GFile.
601 g_file_equal (GFile *file1,
606 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
607 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
609 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
612 iface = G_FILE_GET_IFACE (file1);
614 return (* iface->equal) (file1, file2);
620 * @file: input #GFile
622 * Gets the parent directory for the @file.
623 * If the @file represents the root directory of the
624 * file system, then %NULL will be returned.
626 * This call does no blocking I/O.
628 * Returns: (transfer full): a #GFile structure to the
629 * parent of the given #GFile or %NULL if there is
630 * no parent. Free the returned object with g_object_unref().
633 g_file_get_parent (GFile *file)
637 g_return_val_if_fail (G_IS_FILE (file), NULL);
639 iface = G_FILE_GET_IFACE (file);
641 return (* iface->get_parent) (file);
646 * @file: input #GFile
647 * @parent: (allow-none): the parent to check for, or %NULL
649 * Checks if @file has a parent, and optionally, if it is @parent.
651 * If @parent is %NULL then this function returns %TRUE if @file has any
652 * parent at all. If @parent is non-%NULL then %TRUE is only returned
653 * if @file is a child of @parent.
655 * Returns: %TRUE if @file is a child of @parent (or any parent in the
656 * case that @parent is %NULL).
661 g_file_has_parent (GFile *file,
664 GFile *actual_parent;
667 g_return_val_if_fail (G_IS_FILE (file), FALSE);
668 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
670 actual_parent = g_file_get_parent (file);
672 if (actual_parent != NULL)
675 result = g_file_equal (parent, actual_parent);
679 g_object_unref (actual_parent);
689 * @file: input #GFile
690 * @name: string containing the child's basename
692 * Gets a child of @file with basename equal to @name.
694 * Note that the file with that specific name might not exist, but
695 * you can still have a #GFile that points to it. You can use this
696 * for instance to create that file.
698 * This call does no blocking I/O.
700 * Returns: (transfer full): a #GFile to a child specified by @name.
701 * Free the returned object with g_object_unref().
704 g_file_get_child (GFile *file,
707 g_return_val_if_fail (G_IS_FILE (file), NULL);
708 g_return_val_if_fail (name != NULL, NULL);
710 return g_file_resolve_relative_path (file, name);
714 * g_file_get_child_for_display_name:
715 * @file: input #GFile
716 * @display_name: string to a possible child
717 * @error: return location for an error
719 * Gets the child of @file for a given @display_name (i.e. a UTF-8
720 * version of the name). If this function fails, it returns %NULL
721 * and @error will be set. This is very useful when constructing a
722 * #GFile for a new file and the user entered the filename in the
723 * user interface, for instance when you select a directory and
724 * type a filename in the file selector.
726 * This call does no blocking I/O.
728 * Returns: (transfer full): a #GFile to the specified child, or
729 * %NULL if the display name couldn't be converted.
730 * Free the returned object with g_object_unref().
733 g_file_get_child_for_display_name (GFile *file,
734 const char *display_name,
739 g_return_val_if_fail (G_IS_FILE (file), NULL);
740 g_return_val_if_fail (display_name != NULL, NULL);
742 iface = G_FILE_GET_IFACE (file);
744 return (* iface->get_child_for_display_name) (file, display_name, error);
749 * @file: input #GFile
750 * @prefix: input #GFile
752 * Checks whether @file has the prefix specified by @prefix.
754 * In other words, if the names of initial elements of @file's
755 * pathname match @prefix. Only full pathname elements are matched,
756 * so a path like /foo is not considered a prefix of /foobar, only
759 * This call does no I/O, as it works purely on names. As such it can
760 * sometimes return %FALSE even if @file is inside a @prefix (from a
761 * filesystem point of view), because the prefix of @file is an alias
764 * Virtual: prefix_matches
765 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
769 g_file_has_prefix (GFile *file,
774 g_return_val_if_fail (G_IS_FILE (file), FALSE);
775 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
777 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
780 iface = G_FILE_GET_IFACE (file);
782 /* The vtable function differs in arg order since
783 * we're using the old contains_file call
785 return (* iface->prefix_matches) (prefix, file);
789 * g_file_get_relative_path:
790 * @parent: input #GFile
791 * @descendant: input #GFile
793 * Gets the path for @descendant relative to @parent.
795 * This call does no blocking I/O.
797 * Returns: string with the relative path from @descendant
798 * to @parent, or %NULL if @descendant doesn't have @parent
799 * as prefix. The returned string should be freed with g_free()
800 * when no longer needed.
803 g_file_get_relative_path (GFile *parent,
808 g_return_val_if_fail (G_IS_FILE (parent), NULL);
809 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
811 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
814 iface = G_FILE_GET_IFACE (parent);
816 return (* iface->get_relative_path) (parent, descendant);
820 * g_file_resolve_relative_path:
821 * @file: input #GFile
822 * @relative_path: a given relative path string
824 * Resolves a relative path for @file to an absolute path.
826 * This call does no blocking I/O.
828 * Returns: (transfer full): #GFile to the resolved path.
829 * %NULL if @relative_path is %NULL or if @file is invalid.
830 * Free the returned object with g_object_unref().
833 g_file_resolve_relative_path (GFile *file,
834 const char *relative_path)
838 g_return_val_if_fail (G_IS_FILE (file), NULL);
839 g_return_val_if_fail (relative_path != NULL, NULL);
841 iface = G_FILE_GET_IFACE (file);
843 return (* iface->resolve_relative_path) (file, relative_path);
847 * g_file_enumerate_children:
848 * @file: input #GFile
849 * @attributes: an attribute query string
850 * @flags: a set of #GFileQueryInfoFlags
851 * @cancellable: (allow-none): optional #GCancellable object,
853 * @error: #GError for error reporting
855 * Gets the requested information about the files in a directory.
856 * The result is a #GFileEnumerator object that will give out
857 * #GFileInfo objects for all the files in the directory.
859 * The @attributes value is a string that specifies the file
860 * attributes that should be gathered. It is not an error if
861 * it's not possible to read a particular requested attribute
862 * from a file - it just won't be set. @attributes should
863 * be a comma-separated list of attributes or attribute wildcards.
864 * The wildcard "*" means all attributes, and a wildcard like
865 * "standard::*" means all attributes in the standard namespace.
866 * An example attribute query be "standard::*,owner::user".
867 * The standard attributes are available as defines, like
868 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
870 * If @cancellable is not %NULL, then the operation can be cancelled
871 * by triggering the cancellable object from another thread. If the
872 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
875 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
876 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
877 * error will be returned. Other errors are possible too.
879 * Returns: (transfer full): A #GFileEnumerator if successful,
880 * %NULL on error. Free the returned object with g_object_unref().
883 g_file_enumerate_children (GFile *file,
884 const char *attributes,
885 GFileQueryInfoFlags flags,
886 GCancellable *cancellable,
891 g_return_val_if_fail (G_IS_FILE (file), NULL);
893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
896 iface = G_FILE_GET_IFACE (file);
898 if (iface->enumerate_children == NULL)
900 g_set_error_literal (error, G_IO_ERROR,
901 G_IO_ERROR_NOT_SUPPORTED,
902 _("Operation not supported"));
906 return (* iface->enumerate_children) (file, attributes, flags,
911 * g_file_enumerate_children_async:
912 * @file: input #GFile
913 * @attributes: an attribute query string
914 * @flags: a set of #GFileQueryInfoFlags
915 * @io_priority: the <link linkend="io-priority">I/O priority</link>
917 * @cancellable: (allow-none): optional #GCancellable object,
919 * @callback: (scope async): a #GAsyncReadyCallback to call when the
920 * request is satisfied
921 * @user_data: (closure): the data to pass to callback function
923 * Asynchronously gets the requested information about the files
924 * in a directory. The result is a #GFileEnumerator object that will
925 * give out #GFileInfo objects for all the files in the directory.
927 * For more details, see g_file_enumerate_children() which is
928 * the synchronous version of this call.
930 * When the operation is finished, @callback will be called. You can
931 * then call g_file_enumerate_children_finish() to get the result of
935 g_file_enumerate_children_async (GFile *file,
936 const char *attributes,
937 GFileQueryInfoFlags flags,
939 GCancellable *cancellable,
940 GAsyncReadyCallback callback,
945 g_return_if_fail (G_IS_FILE (file));
947 iface = G_FILE_GET_IFACE (file);
948 (* iface->enumerate_children_async) (file,
958 * g_file_enumerate_children_finish:
959 * @file: input #GFile
960 * @res: a #GAsyncResult
963 * Finishes an async enumerate children operation.
964 * See g_file_enumerate_children_async().
966 * Returns: (transfer full): a #GFileEnumerator or %NULL
967 * if an error occurred.
968 * Free the returned object with g_object_unref().
971 g_file_enumerate_children_finish (GFile *file,
977 g_return_val_if_fail (G_IS_FILE (file), NULL);
978 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
980 if (g_async_result_legacy_propagate_error (res, error))
983 iface = G_FILE_GET_IFACE (file);
984 return (* iface->enumerate_children_finish) (file, res, error);
988 * g_file_query_exists:
989 * @file: input #GFile
990 * @cancellable: (allow-none): optional #GCancellable object,
993 * Utility function to check if a particular file exists. This is
994 * implemented using g_file_query_info() and as such does blocking I/O.
996 * Note that in many cases it is racy to first check for file existence
997 * and then execute something based on the outcome of that, because the
998 * file might have been created or removed in between the operations. The
999 * general approach to handling that is to not check, but just do the
1000 * operation and handle the errors as they come.
1002 * As an example of race-free checking, take the case of reading a file,
1003 * and if it doesn't exist, creating it. There are two racy versions: read
1004 * it, and on error create it; and: check if it exists, if not create it.
1005 * These can both result in two processes creating the file (with perhaps
1006 * a partially written file as the result). The correct approach is to
1007 * always try to create the file with g_file_create() which will either
1008 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1010 * However, in many cases an existence check is useful in a user interface,
1011 * for instance to make a menu item sensitive/insensitive, so that you don't
1012 * have to fool users that something is possible and then just show an error
1013 * dialog. If you do this, you should make sure to also handle the errors
1014 * that can happen due to races when you execute the operation.
1016 * Returns: %TRUE if the file exists (and can be detected without error),
1017 * %FALSE otherwise (or if cancelled).
1020 g_file_query_exists (GFile *file,
1021 GCancellable *cancellable)
1025 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1027 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1028 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1031 g_object_unref (info);
1039 * g_file_query_file_type:
1040 * @file: input #GFile
1041 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1042 * @cancellable: (allow-none): optional #GCancellable object,
1045 * Utility function to inspect the #GFileType of a file. This is
1046 * implemented using g_file_query_info() and as such does blocking I/O.
1048 * The primary use case of this method is to check if a file is
1049 * a regular file, directory, or symlink.
1051 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1052 * if the file does not exist
1057 g_file_query_file_type (GFile *file,
1058 GFileQueryInfoFlags flags,
1059 GCancellable *cancellable)
1062 GFileType file_type;
1064 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1065 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1069 file_type = g_file_info_get_file_type (info);
1070 g_object_unref (info);
1073 file_type = G_FILE_TYPE_UNKNOWN;
1079 * g_file_query_info:
1080 * @file: input #GFile
1081 * @attributes: an attribute query string
1082 * @flags: a set of #GFileQueryInfoFlags
1083 * @cancellable: (allow-none): optional #GCancellable object,
1087 * Gets the requested information about specified @file.
1088 * The result is a #GFileInfo object that contains key-value
1089 * attributes (such as the type or size of the file).
1091 * The @attributes value is a string that specifies the file
1092 * attributes that should be gathered. It is not an error if
1093 * it's not possible to read a particular requested attribute
1094 * from a file - it just won't be set. @attributes should be a
1095 * comma-separated list of attributes or attribute wildcards.
1096 * The wildcard "*" means all attributes, and a wildcard like
1097 * "standard::*" means all attributes in the standard namespace.
1098 * An example attribute query be "standard::*,owner::user".
1099 * The standard attributes are available as defines, like
1100 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1102 * If @cancellable is not %NULL, then the operation can be cancelled
1103 * by triggering the cancellable object from another thread. If the
1104 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1107 * For symlinks, normally the information about the target of the
1108 * symlink is returned, rather than information about the symlink
1109 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1110 * in @flags the information about the symlink itself will be returned.
1111 * Also, for symlinks that point to non-existing files the information
1112 * about the symlink itself will be returned.
1114 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1115 * returned. Other errors are possible too, and depend on what kind of
1116 * filesystem the file is on.
1118 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1119 * on error. Free the returned object with g_object_unref().
1122 g_file_query_info (GFile *file,
1123 const char *attributes,
1124 GFileQueryInfoFlags flags,
1125 GCancellable *cancellable,
1130 g_return_val_if_fail (G_IS_FILE (file), NULL);
1132 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1135 iface = G_FILE_GET_IFACE (file);
1137 if (iface->query_info == NULL)
1139 g_set_error_literal (error, G_IO_ERROR,
1140 G_IO_ERROR_NOT_SUPPORTED,
1141 _("Operation not supported"));
1145 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1149 * g_file_query_info_async:
1150 * @file: input #GFile
1151 * @attributes: an attribute query string
1152 * @flags: a set of #GFileQueryInfoFlags
1153 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1155 * @cancellable: (allow-none): optional #GCancellable object,
1157 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1158 * request is satisfied
1159 * @user_data: (closure): the data to pass to callback function
1161 * Asynchronously gets the requested information about specified @file.
1162 * The result is a #GFileInfo object that contains key-value attributes
1163 * (such as type or size for the file).
1165 * For more details, see g_file_query_info() which is the synchronous
1166 * version of this call.
1168 * When the operation is finished, @callback will be called. You can
1169 * then call g_file_query_info_finish() to get the result of the operation.
1172 g_file_query_info_async (GFile *file,
1173 const char *attributes,
1174 GFileQueryInfoFlags flags,
1176 GCancellable *cancellable,
1177 GAsyncReadyCallback callback,
1182 g_return_if_fail (G_IS_FILE (file));
1184 iface = G_FILE_GET_IFACE (file);
1185 (* iface->query_info_async) (file,
1195 * g_file_query_info_finish:
1196 * @file: input #GFile
1197 * @res: a #GAsyncResult
1200 * Finishes an asynchronous file info query.
1201 * See g_file_query_info_async().
1203 * Returns: (transfer full): #GFileInfo for given @file
1204 * or %NULL on error. Free the returned object with
1208 g_file_query_info_finish (GFile *file,
1214 g_return_val_if_fail (G_IS_FILE (file), NULL);
1215 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1217 if (g_async_result_legacy_propagate_error (res, error))
1220 iface = G_FILE_GET_IFACE (file);
1221 return (* iface->query_info_finish) (file, res, error);
1225 * g_file_query_filesystem_info:
1226 * @file: input #GFile
1227 * @attributes: an attribute query string
1228 * @cancellable: (allow-none): optional #GCancellable object,
1232 * Similar to g_file_query_info(), but obtains information
1233 * about the filesystem the @file is on, rather than the file itself.
1234 * For instance the amount of space available and the type of
1237 * The @attributes value is a string that specifies the attributes
1238 * that should be gathered. It is not an error if it's not possible
1239 * to read a particular requested attribute from a file - it just
1240 * won't be set. @attributes should be a comma-separated list of
1241 * attributes or attribute wildcards. The wildcard "*" means all
1242 * attributes, and a wildcard like "filesystem::*" means all attributes
1243 * in the filesystem namespace. The standard namespace for filesystem
1244 * attributes is "filesystem". Common attributes of interest are
1245 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1246 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1247 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1249 * If @cancellable is not %NULL, then the operation can be cancelled
1250 * by triggering the cancellable object from another thread. If the
1251 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1254 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1255 * be returned. Other errors are possible too, and depend on what
1256 * kind of filesystem the file is on.
1258 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1259 * Free the returned object with g_object_unref().
1262 g_file_query_filesystem_info (GFile *file,
1263 const char *attributes,
1264 GCancellable *cancellable,
1269 g_return_val_if_fail (G_IS_FILE (file), NULL);
1271 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1274 iface = G_FILE_GET_IFACE (file);
1276 if (iface->query_filesystem_info == NULL)
1278 g_set_error_literal (error, G_IO_ERROR,
1279 G_IO_ERROR_NOT_SUPPORTED,
1280 _("Operation not supported"));
1284 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1288 * g_file_query_filesystem_info_async:
1289 * @file: input #GFile
1290 * @attributes: an attribute query string
1291 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1293 * @cancellable: (allow-none): optional #GCancellable object,
1295 * @callback: (scope async): a #GAsyncReadyCallback to call
1296 * when the request is satisfied
1297 * @user_data: (closure): the data to pass to callback function
1299 * Asynchronously gets the requested information about the filesystem
1300 * that the specified @file is on. The result is a #GFileInfo object
1301 * that contains key-value attributes (such as type or size for the
1304 * For more details, see g_file_query_filesystem_info() which is the
1305 * synchronous version of this call.
1307 * When the operation is finished, @callback will be called. You can
1308 * then call g_file_query_info_finish() to get the result of the
1312 g_file_query_filesystem_info_async (GFile *file,
1313 const char *attributes,
1315 GCancellable *cancellable,
1316 GAsyncReadyCallback callback,
1321 g_return_if_fail (G_IS_FILE (file));
1323 iface = G_FILE_GET_IFACE (file);
1324 (* iface->query_filesystem_info_async) (file,
1333 * g_file_query_filesystem_info_finish:
1334 * @file: input #GFile
1335 * @res: a #GAsyncResult
1338 * Finishes an asynchronous filesystem info query.
1339 * See g_file_query_filesystem_info_async().
1341 * Returns: (transfer full): #GFileInfo for given @file
1342 * or %NULL on error.
1343 * Free the returned object with g_object_unref().
1346 g_file_query_filesystem_info_finish (GFile *file,
1352 g_return_val_if_fail (G_IS_FILE (file), NULL);
1353 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1355 if (g_async_result_legacy_propagate_error (res, error))
1358 iface = G_FILE_GET_IFACE (file);
1359 return (* iface->query_filesystem_info_finish) (file, res, error);
1363 * g_file_find_enclosing_mount:
1364 * @file: input #GFile
1365 * @cancellable: (allow-none): optional #GCancellable object,
1369 * Gets a #GMount for the #GFile.
1371 * If the #GFileIface for @file does not have a mount (e.g.
1372 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1373 * and %NULL will be returned.
1375 * If @cancellable is not %NULL, then the operation can be cancelled by
1376 * triggering the cancellable object from another thread. If the operation
1377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1379 * Returns: (transfer full): a #GMount where the @file is located
1380 * or %NULL on error.
1381 * Free the returned object with g_object_unref().
1384 g_file_find_enclosing_mount (GFile *file,
1385 GCancellable *cancellable,
1390 g_return_val_if_fail (G_IS_FILE (file), NULL);
1392 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1395 iface = G_FILE_GET_IFACE (file);
1396 if (iface->find_enclosing_mount == NULL)
1399 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1400 /* Translators: This is an error message when
1401 * trying to find the enclosing (user visible)
1402 * mount of a file, but none exists.
1404 _("Containing mount does not exist"));
1408 return (* iface->find_enclosing_mount) (file, cancellable, error);
1412 * g_file_find_enclosing_mount_async:
1414 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1416 * @cancellable: (allow-none): optional #GCancellable object,
1418 * @callback: (scope async): a #GAsyncReadyCallback to call
1419 * when the request is satisfied
1420 * @user_data: (closure): the data to pass to callback function
1422 * Asynchronously gets the mount for the file.
1424 * For more details, see g_file_find_enclosing_mount() which is
1425 * the synchronous version of this call.
1427 * When the operation is finished, @callback will be called.
1428 * You can then call g_file_find_enclosing_mount_finish() to
1429 * get the result of the operation.
1432 g_file_find_enclosing_mount_async (GFile *file,
1434 GCancellable *cancellable,
1435 GAsyncReadyCallback callback,
1440 g_return_if_fail (G_IS_FILE (file));
1442 iface = G_FILE_GET_IFACE (file);
1443 (* iface->find_enclosing_mount_async) (file,
1451 * g_file_find_enclosing_mount_finish:
1453 * @res: a #GAsyncResult
1456 * Finishes an asynchronous find mount request.
1457 * See g_file_find_enclosing_mount_async().
1459 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1460 * Free the returned object with g_object_unref().
1463 g_file_find_enclosing_mount_finish (GFile *file,
1469 g_return_val_if_fail (G_IS_FILE (file), NULL);
1470 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1472 if (g_async_result_legacy_propagate_error (res, error))
1475 iface = G_FILE_GET_IFACE (file);
1476 return (* iface->find_enclosing_mount_finish) (file, res, error);
1482 * @file: #GFile to read
1483 * @cancellable: (allow-none): a #GCancellable
1484 * @error: a #GError, or %NULL
1486 * Opens a file for reading. The result is a #GFileInputStream that
1487 * can be used to read the contents of the file.
1489 * If @cancellable is not %NULL, then the operation can be cancelled by
1490 * triggering the cancellable object from another thread. If the operation
1491 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1493 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1494 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1495 * error will be returned. Other errors are possible too, and depend
1496 * on what kind of filesystem the file is on.
1499 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1500 * Free the returned object with g_object_unref().
1503 g_file_read (GFile *file,
1504 GCancellable *cancellable,
1509 g_return_val_if_fail (G_IS_FILE (file), NULL);
1511 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1514 iface = G_FILE_GET_IFACE (file);
1516 if (iface->read_fn == NULL)
1518 g_set_error_literal (error, G_IO_ERROR,
1519 G_IO_ERROR_NOT_SUPPORTED,
1520 _("Operation not supported"));
1524 return (* iface->read_fn) (file, cancellable, error);
1529 * @file: input #GFile
1530 * @flags: a set of #GFileCreateFlags
1531 * @cancellable: (allow-none): optional #GCancellable object,
1533 * @error: a #GError, or %NULL
1535 * Gets an output stream for appending data to the file.
1536 * If the file doesn't already exist it is created.
1538 * By default files created are generally readable by everyone,
1539 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1540 * will be made readable only to the current user, to the level that
1541 * is supported on the target filesystem.
1543 * If @cancellable is not %NULL, then the operation can be cancelled
1544 * by triggering the cancellable object from another thread. If the
1545 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1548 * Some file systems don't allow all file names, and may return an
1549 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1550 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1551 * possible too, and depend on what kind of filesystem the file is on.
1553 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1554 * Free the returned object with g_object_unref().
1557 g_file_append_to (GFile *file,
1558 GFileCreateFlags flags,
1559 GCancellable *cancellable,
1564 g_return_val_if_fail (G_IS_FILE (file), NULL);
1566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1569 iface = G_FILE_GET_IFACE (file);
1571 if (iface->append_to == NULL)
1573 g_set_error_literal (error, G_IO_ERROR,
1574 G_IO_ERROR_NOT_SUPPORTED,
1575 _("Operation not supported"));
1579 return (* iface->append_to) (file, flags, cancellable, error);
1584 * @file: input #GFile
1585 * @flags: a set of #GFileCreateFlags
1586 * @cancellable: (allow-none): optional #GCancellable object,
1588 * @error: a #GError, or %NULL
1590 * Creates a new file and returns an output stream for writing to it.
1591 * The file must not already exist.
1593 * By default files created are generally readable by everyone,
1594 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1595 * will be made readable only to the current user, to the level
1596 * that is supported on the target filesystem.
1598 * If @cancellable is not %NULL, then the operation can be cancelled
1599 * by triggering the cancellable object from another thread. If the
1600 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1603 * If a file or directory with this name already exists the
1604 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1605 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1606 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1607 * be returned. Other errors are possible too, and depend on what kind
1608 * of filesystem the file is on.
1610 * Returns: (transfer full): a #GFileOutputStream for the newly created
1611 * file, or %NULL on error.
1612 * Free the returned object with g_object_unref().
1615 g_file_create (GFile *file,
1616 GFileCreateFlags flags,
1617 GCancellable *cancellable,
1622 g_return_val_if_fail (G_IS_FILE (file), NULL);
1624 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1627 iface = G_FILE_GET_IFACE (file);
1629 if (iface->create == NULL)
1631 g_set_error_literal (error, G_IO_ERROR,
1632 G_IO_ERROR_NOT_SUPPORTED,
1633 _("Operation not supported"));
1637 return (* iface->create) (file, flags, cancellable, error);
1642 * @file: input #GFile
1643 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1644 * for the current #GFile, or #NULL to ignore
1645 * @make_backup: %TRUE if a backup should be created
1646 * @flags: a set of #GFileCreateFlags
1647 * @cancellable: (allow-none): optional #GCancellable object,
1649 * @error: a #GError, or %NULL
1651 * Returns an output stream for overwriting the file, possibly
1652 * creating a backup copy of the file first. If the file doesn't exist,
1653 * it will be created.
1655 * This will try to replace the file in the safest way possible so
1656 * that any errors during the writing will not affect an already
1657 * existing copy of the file. For instance, for local files it
1658 * may write to a temporary file and then atomically rename over
1659 * the destination when the stream is closed.
1661 * By default files created are generally readable by everyone,
1662 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1663 * will be made readable only to the current user, to the level that
1664 * is supported on the target filesystem.
1666 * If @cancellable is not %NULL, then the operation can be cancelled
1667 * by triggering the cancellable object from another thread. If the
1668 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1671 * If you pass in a non-%NULL @etag value, then this value is
1672 * compared to the current entity tag of the file, and if they differ
1673 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1674 * that the file has been changed since you last read it. You can get
1675 * the new etag from g_file_output_stream_get_etag() after you've
1676 * finished writing and closed the #GFileOutputStream. When you load
1677 * a new file you can use g_file_input_stream_query_info() to get
1678 * the etag of the file.
1680 * If @make_backup is %TRUE, this function will attempt to make a
1681 * backup of the current file before overwriting it. If this fails
1682 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1683 * want to replace anyway, try again with @make_backup set to %FALSE.
1685 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1686 * be returned, and if the file is some other form of non-regular file
1687 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1688 * file systems don't allow all file names, and may return an
1689 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1690 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1691 * possible too, and depend on what kind of filesystem the file is on.
1693 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1694 * Free the returned object with g_object_unref().
1697 g_file_replace (GFile *file,
1699 gboolean make_backup,
1700 GFileCreateFlags flags,
1701 GCancellable *cancellable,
1706 g_return_val_if_fail (G_IS_FILE (file), NULL);
1708 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1711 iface = G_FILE_GET_IFACE (file);
1713 if (iface->replace == NULL)
1715 g_set_error_literal (error, G_IO_ERROR,
1716 G_IO_ERROR_NOT_SUPPORTED,
1717 _("Operation not supported"));
1721 /* Handle empty tag string as NULL in consistent way. */
1722 if (etag && *etag == 0)
1725 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1729 * g_file_open_readwrite:
1730 * @file: #GFile to open
1731 * @cancellable: (allow-none): a #GCancellable
1732 * @error: a #GError, or %NULL
1734 * Opens an existing file for reading and writing. The result is
1735 * a #GFileIOStream that can be used to read and write the contents
1738 * If @cancellable is not %NULL, then the operation can be cancelled
1739 * by triggering the cancellable object from another thread. If the
1740 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1743 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1744 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1745 * error will be returned. Other errors are possible too, and depend on
1746 * what kind of filesystem the file is on. Note that in many non-local
1747 * file cases read and write streams are not supported, so make sure you
1748 * really need to do read and write streaming, rather than just opening
1749 * for reading or writing.
1751 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1752 * Free the returned object with g_object_unref().
1757 g_file_open_readwrite (GFile *file,
1758 GCancellable *cancellable,
1763 g_return_val_if_fail (G_IS_FILE (file), NULL);
1765 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1768 iface = G_FILE_GET_IFACE (file);
1770 if (iface->open_readwrite == NULL)
1772 g_set_error_literal (error, G_IO_ERROR,
1773 G_IO_ERROR_NOT_SUPPORTED,
1774 _("Operation not supported"));
1778 return (* iface->open_readwrite) (file, cancellable, error);
1782 * g_file_create_readwrite:
1784 * @flags: a set of #GFileCreateFlags
1785 * @cancellable: (allow-none): optional #GCancellable object,
1787 * @error: return location for a #GError, or %NULL
1789 * Creates a new file and returns a stream for reading and
1790 * writing to it. The file must not already exist.
1792 * By default files created are generally readable by everyone,
1793 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1794 * will be made readable only to the current user, to the level
1795 * that is supported on the target filesystem.
1797 * If @cancellable is not %NULL, then the operation can be cancelled
1798 * by triggering the cancellable object from another thread. If the
1799 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1802 * If a file or directory with this name already exists, the
1803 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1804 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1805 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1806 * will be returned. Other errors are possible too, and depend on what
1807 * kind of filesystem the file is on.
1809 * Note that in many non-local file cases read and write streams are
1810 * not supported, so make sure you really need to do read and write
1811 * streaming, rather than just opening for reading or writing.
1813 * Returns: (transfer full): a #GFileIOStream for the newly created
1814 * file, or %NULL on error.
1815 * Free the returned object with g_object_unref().
1820 g_file_create_readwrite (GFile *file,
1821 GFileCreateFlags flags,
1822 GCancellable *cancellable,
1827 g_return_val_if_fail (G_IS_FILE (file), NULL);
1829 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1832 iface = G_FILE_GET_IFACE (file);
1834 if (iface->create_readwrite == NULL)
1836 g_set_error_literal (error, G_IO_ERROR,
1837 G_IO_ERROR_NOT_SUPPORTED,
1838 _("Operation not supported"));
1842 return (* iface->create_readwrite) (file, flags, cancellable, error);
1846 * g_file_replace_readwrite:
1848 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1849 * for the current #GFile, or #NULL to ignore
1850 * @make_backup: %TRUE if a backup should be created
1851 * @flags: a set of #GFileCreateFlags
1852 * @cancellable: (allow-none): optional #GCancellable object,
1854 * @error: return location for a #GError, or %NULL
1856 * Returns an output stream for overwriting the file in readwrite mode,
1857 * possibly creating a backup copy of the file first. If the file doesn't
1858 * exist, it will be created.
1860 * For details about the behaviour, see g_file_replace() which does the
1861 * same thing but returns an output stream only.
1863 * Note that in many non-local file cases read and write streams are not
1864 * supported, so make sure you really need to do read and write streaming,
1865 * rather than just opening for reading or writing.
1867 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1868 * Free the returned object with g_object_unref().
1873 g_file_replace_readwrite (GFile *file,
1875 gboolean make_backup,
1876 GFileCreateFlags flags,
1877 GCancellable *cancellable,
1882 g_return_val_if_fail (G_IS_FILE (file), NULL);
1884 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1887 iface = G_FILE_GET_IFACE (file);
1889 if (iface->replace_readwrite == NULL)
1891 g_set_error_literal (error, G_IO_ERROR,
1892 G_IO_ERROR_NOT_SUPPORTED,
1893 _("Operation not supported"));
1897 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1901 * g_file_read_async:
1902 * @file: input #GFile
1903 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1905 * @cancellable: (allow-none): optional #GCancellable object,
1907 * @callback: (scope async): a #GAsyncReadyCallback to call
1908 * when the request is satisfied
1909 * @user_data: (closure): the data to pass to callback function
1911 * Asynchronously opens @file for reading.
1913 * For more details, see g_file_read() which is
1914 * the synchronous version of this call.
1916 * When the operation is finished, @callback will be called.
1917 * You can then call g_file_read_finish() to get the result
1921 g_file_read_async (GFile *file,
1923 GCancellable *cancellable,
1924 GAsyncReadyCallback callback,
1929 g_return_if_fail (G_IS_FILE (file));
1931 iface = G_FILE_GET_IFACE (file);
1932 (* iface->read_async) (file,
1940 * g_file_read_finish:
1941 * @file: input #GFile
1942 * @res: a #GAsyncResult
1943 * @error: a #GError, or %NULL
1945 * Finishes an asynchronous file read operation started with
1946 * g_file_read_async().
1948 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1949 * Free the returned object with g_object_unref().
1952 g_file_read_finish (GFile *file,
1958 g_return_val_if_fail (G_IS_FILE (file), NULL);
1959 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1961 if (g_async_result_legacy_propagate_error (res, error))
1964 iface = G_FILE_GET_IFACE (file);
1965 return (* iface->read_finish) (file, res, error);
1969 * g_file_append_to_async:
1970 * @file: input #GFile
1971 * @flags: a set of #GFileCreateFlags
1972 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1974 * @cancellable: (allow-none): optional #GCancellable object,
1976 * @callback: (scope async): a #GAsyncReadyCallback to call
1977 * when the request is satisfied
1978 * @user_data: (closure): the data to pass to callback function
1980 * Asynchronously opens @file for appending.
1982 * For more details, see g_file_append_to() which is
1983 * the synchronous version of this call.
1985 * When the operation is finished, @callback will be called.
1986 * You can then call g_file_append_to_finish() to get the result
1990 g_file_append_to_async (GFile *file,
1991 GFileCreateFlags flags,
1993 GCancellable *cancellable,
1994 GAsyncReadyCallback callback,
1999 g_return_if_fail (G_IS_FILE (file));
2001 iface = G_FILE_GET_IFACE (file);
2002 (* iface->append_to_async) (file,
2011 * g_file_append_to_finish:
2012 * @file: input #GFile
2013 * @res: #GAsyncResult
2014 * @error: a #GError, or %NULL
2016 * Finishes an asynchronous file append operation started with
2017 * g_file_append_to_async().
2019 * Returns: (transfer full): a valid #GFileOutputStream
2020 * or %NULL on error.
2021 * Free the returned object with g_object_unref().
2024 g_file_append_to_finish (GFile *file,
2030 g_return_val_if_fail (G_IS_FILE (file), NULL);
2031 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2033 if (g_async_result_legacy_propagate_error (res, error))
2036 iface = G_FILE_GET_IFACE (file);
2037 return (* iface->append_to_finish) (file, res, error);
2041 * g_file_create_async:
2042 * @file: input #GFile
2043 * @flags: a set of #GFileCreateFlags
2044 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2046 * @cancellable: (allow-none): optional #GCancellable object,
2048 * @callback: (scope async): a #GAsyncReadyCallback to call
2049 * when the request is satisfied
2050 * @user_data: (closure): the data to pass to callback function
2052 * Asynchronously creates a new file and returns an output stream
2053 * for writing to it. The file must not already exist.
2055 * For more details, see g_file_create() which is
2056 * the synchronous version of this call.
2058 * When the operation is finished, @callback will be called.
2059 * You can then call g_file_create_finish() to get the result
2063 g_file_create_async (GFile *file,
2064 GFileCreateFlags flags,
2066 GCancellable *cancellable,
2067 GAsyncReadyCallback callback,
2072 g_return_if_fail (G_IS_FILE (file));
2074 iface = G_FILE_GET_IFACE (file);
2075 (* iface->create_async) (file,
2084 * g_file_create_finish:
2085 * @file: input #GFile
2086 * @res: a #GAsyncResult
2087 * @error: a #GError, or %NULL
2089 * Finishes an asynchronous file create operation started with
2090 * g_file_create_async().
2092 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2093 * Free the returned object with g_object_unref().
2096 g_file_create_finish (GFile *file,
2102 g_return_val_if_fail (G_IS_FILE (file), NULL);
2103 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2105 if (g_async_result_legacy_propagate_error (res, error))
2108 iface = G_FILE_GET_IFACE (file);
2109 return (* iface->create_finish) (file, res, error);
2113 * g_file_replace_async:
2114 * @file: input #GFile
2115 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2116 * for the current #GFile, or NULL to ignore
2117 * @make_backup: %TRUE if a backup should be created
2118 * @flags: a set of #GFileCreateFlags
2119 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2121 * @cancellable: (allow-none): optional #GCancellable object,
2123 * @callback: (scope async): a #GAsyncReadyCallback to call
2124 * when the request is satisfied
2125 * @user_data: (closure): the data to pass to callback function
2127 * Asynchronously overwrites the file, replacing the contents,
2128 * possibly creating a backup copy of the file first.
2130 * For more details, see g_file_replace() which is
2131 * the synchronous version of this call.
2133 * When the operation is finished, @callback will be called.
2134 * You can then call g_file_replace_finish() to get the result
2138 g_file_replace_async (GFile *file,
2140 gboolean make_backup,
2141 GFileCreateFlags flags,
2143 GCancellable *cancellable,
2144 GAsyncReadyCallback callback,
2149 g_return_if_fail (G_IS_FILE (file));
2151 iface = G_FILE_GET_IFACE (file);
2152 (* iface->replace_async) (file,
2163 * g_file_replace_finish:
2164 * @file: input #GFile
2165 * @res: a #GAsyncResult
2166 * @error: a #GError, or %NULL
2168 * Finishes an asynchronous file replace operation started with
2169 * g_file_replace_async().
2171 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2172 * Free the returned object with g_object_unref().
2175 g_file_replace_finish (GFile *file,
2181 g_return_val_if_fail (G_IS_FILE (file), NULL);
2182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2184 if (g_async_result_legacy_propagate_error (res, error))
2187 iface = G_FILE_GET_IFACE (file);
2188 return (* iface->replace_finish) (file, res, error);
2192 * g_file_open_readwrite_async
2193 * @file: input #GFile
2194 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2196 * @cancellable: (allow-none): optional #GCancellable object,
2198 * @callback: (scope async): a #GAsyncReadyCallback to call
2199 * when the request is satisfied
2200 * @user_data: (closure): the data to pass to callback function
2202 * Asynchronously opens @file for reading and writing.
2204 * For more details, see g_file_open_readwrite() which is
2205 * the synchronous version of this call.
2207 * When the operation is finished, @callback will be called.
2208 * You can then call g_file_open_readwrite_finish() to get
2209 * the result of the operation.
2214 g_file_open_readwrite_async (GFile *file,
2216 GCancellable *cancellable,
2217 GAsyncReadyCallback callback,
2222 g_return_if_fail (G_IS_FILE (file));
2224 iface = G_FILE_GET_IFACE (file);
2225 (* iface->open_readwrite_async) (file,
2233 * g_file_open_readwrite_finish:
2234 * @file: input #GFile
2235 * @res: a #GAsyncResult
2236 * @error: a #GError, or %NULL
2238 * Finishes an asynchronous file read operation started with
2239 * g_file_open_readwrite_async().
2241 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2242 * Free the returned object with g_object_unref().
2247 g_file_open_readwrite_finish (GFile *file,
2253 g_return_val_if_fail (G_IS_FILE (file), NULL);
2254 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2256 if (g_async_result_legacy_propagate_error (res, error))
2259 iface = G_FILE_GET_IFACE (file);
2260 return (* iface->open_readwrite_finish) (file, res, error);
2264 * g_file_create_readwrite_async:
2265 * @file: input #GFile
2266 * @flags: a set of #GFileCreateFlags
2267 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2269 * @cancellable: (allow-none): optional #GCancellable object,
2271 * @callback: (scope async): a #GAsyncReadyCallback to call
2272 * when the request is satisfied
2273 * @user_data: (closure): the data to pass to callback function
2275 * Asynchronously creates a new file and returns a stream
2276 * for reading and writing to it. The file must not already exist.
2278 * For more details, see g_file_create_readwrite() which is
2279 * the synchronous version of this call.
2281 * When the operation is finished, @callback will be called.
2282 * You can then call g_file_create_readwrite_finish() to get
2283 * the result of the operation.
2288 g_file_create_readwrite_async (GFile *file,
2289 GFileCreateFlags flags,
2291 GCancellable *cancellable,
2292 GAsyncReadyCallback callback,
2297 g_return_if_fail (G_IS_FILE (file));
2299 iface = G_FILE_GET_IFACE (file);
2300 (* iface->create_readwrite_async) (file,
2309 * g_file_create_readwrite_finish:
2310 * @file: input #GFile
2311 * @res: a #GAsyncResult
2312 * @error: a #GError, or %NULL
2314 * Finishes an asynchronous file create operation started with
2315 * g_file_create_readwrite_async().
2317 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2318 * Free the returned object with g_object_unref().
2323 g_file_create_readwrite_finish (GFile *file,
2329 g_return_val_if_fail (G_IS_FILE (file), NULL);
2330 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2332 if (g_async_result_legacy_propagate_error (res, error))
2335 iface = G_FILE_GET_IFACE (file);
2336 return (* iface->create_readwrite_finish) (file, res, error);
2340 * g_file_replace_readwrite_async:
2341 * @file: input #GFile
2342 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2343 * for the current #GFile, or NULL to ignore
2344 * @make_backup: %TRUE if a backup should be created
2345 * @flags: a set of #GFileCreateFlags
2346 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2348 * @cancellable: (allow-none): optional #GCancellable object,
2350 * @callback: (scope async): a #GAsyncReadyCallback to call
2351 * when the request is satisfied
2352 * @user_data: (closure): the data to pass to callback function
2354 * Asynchronously overwrites the file in read-write mode,
2355 * replacing the contents, possibly creating a backup copy
2356 * of the file first.
2358 * For more details, see g_file_replace_readwrite() which is
2359 * the synchronous version of this call.
2361 * When the operation is finished, @callback will be called.
2362 * You can then call g_file_replace_readwrite_finish() to get
2363 * the result of the operation.
2368 g_file_replace_readwrite_async (GFile *file,
2370 gboolean make_backup,
2371 GFileCreateFlags flags,
2373 GCancellable *cancellable,
2374 GAsyncReadyCallback callback,
2379 g_return_if_fail (G_IS_FILE (file));
2381 iface = G_FILE_GET_IFACE (file);
2382 (* iface->replace_readwrite_async) (file,
2393 * g_file_replace_readwrite_finish:
2394 * @file: input #GFile
2395 * @res: a #GAsyncResult
2396 * @error: a #GError, or %NULL
2398 * Finishes an asynchronous file replace operation started with
2399 * g_file_replace_readwrite_async().
2401 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2402 * Free the returned object with g_object_unref().
2407 g_file_replace_readwrite_finish (GFile *file,
2413 g_return_val_if_fail (G_IS_FILE (file), NULL);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2416 if (g_async_result_legacy_propagate_error (res, error))
2419 iface = G_FILE_GET_IFACE (file);
2420 return (* iface->replace_readwrite_finish) (file, res, error);
2424 copy_symlink (GFile *destination,
2425 GFileCopyFlags flags,
2426 GCancellable *cancellable,
2431 gboolean tried_delete;
2433 GFileType file_type;
2435 tried_delete = FALSE;
2439 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2441 /* Maybe it already existed, and we want to overwrite? */
2442 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2443 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2445 g_error_free (my_error);
2447 /* Don't overwrite if the destination is a directory */
2448 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2449 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2450 cancellable, &my_error);
2453 file_type = g_file_info_get_file_type (info);
2454 g_object_unref (info);
2456 if (file_type == G_FILE_TYPE_DIRECTORY)
2458 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2459 _("Can't copy over directory"));
2464 if (!g_file_delete (destination, cancellable, error))
2467 tried_delete = TRUE;
2471 g_propagate_error (error, my_error);
2478 static GInputStream *
2479 open_source_for_copy (GFile *source,
2481 GFileCopyFlags flags,
2482 GCancellable *cancellable,
2488 GFileType file_type;
2491 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2495 /* There was an error opening the source, try to set a good error for it: */
2496 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2498 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2499 * as that is less useful to the app. Better check for errors on the
2502 g_error_free (my_error);
2505 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2506 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2507 cancellable, &my_error);
2509 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2511 file_type = g_file_info_get_file_type (info);
2512 g_object_unref (info);
2514 if (flags & G_FILE_COPY_OVERWRITE)
2516 if (file_type == G_FILE_TYPE_DIRECTORY)
2518 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2519 _("Can't copy directory over directory"));
2522 /* continue to would_recurse error */
2526 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2527 _("Target file exists"));
2533 /* Error getting info from target, return that error
2534 * (except for NOT_FOUND, which is no error here)
2536 g_clear_object (&info);
2537 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2539 g_propagate_error (error, my_error);
2542 g_clear_error (&my_error);
2545 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2546 _("Can't recursively copy directory"));
2550 g_propagate_error (error, my_error);
2555 should_copy (GFileAttributeInfo *info,
2557 gboolean skip_perms)
2559 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2563 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2564 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2568 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2569 GFileAttributeInfoList *namespaces,
2571 gboolean skip_perms)
2578 s = g_string_new ("");
2582 for (i = 0; i < attributes->n_infos; i++)
2584 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2589 g_string_append_c (s, ',');
2591 g_string_append (s, attributes->infos[i].name);
2598 for (i = 0; i < namespaces->n_infos; i++)
2600 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2605 g_string_append_c (s, ',');
2607 g_string_append (s, namespaces->infos[i].name);
2608 g_string_append (s, "::*");
2613 return g_string_free (s, FALSE);
2617 * g_file_copy_attributes:
2618 * @source: a #GFile with attributes
2619 * @destination: a #GFile to copy attributes to
2620 * @flags: a set of #GFileCopyFlags
2621 * @cancellable: (allow-none): optional #GCancellable object,
2623 * @error: a #GError, %NULL to ignore
2625 * Copies the file attributes from @source to @destination.
2627 * Normally only a subset of the file attributes are copied,
2628 * those that are copies in a normal file copy operation
2629 * (which for instance does not include e.g. owner). However
2630 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2631 * all the metadata that is possible to copy is copied. This
2632 * is useful when implementing move by copy + delete source.
2634 * Returns: %TRUE if the attributes were copied successfully,
2638 g_file_copy_attributes (GFile *source,
2640 GFileCopyFlags flags,
2641 GCancellable *cancellable,
2644 GFileAttributeInfoList *attributes, *namespaces;
2645 char *attrs_to_read;
2649 gboolean source_nofollow_symlinks;
2650 gboolean skip_perms;
2652 as_move = flags & G_FILE_COPY_ALL_METADATA;
2653 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2654 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2656 /* Ignore errors here, if the target supports no attributes there is
2659 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2660 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2662 if (attributes == NULL && namespaces == NULL)
2665 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2667 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2668 * we just don't copy it.
2670 info = g_file_query_info (source, attrs_to_read,
2671 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2675 g_free (attrs_to_read);
2680 res = g_file_set_attributes_from_info (destination,
2682 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2685 g_object_unref (info);
2688 g_file_attribute_info_list_unref (attributes);
2689 g_file_attribute_info_list_unref (namespaces);
2695 copy_stream_with_progress (GInputStream *in,
2698 GCancellable *cancellable,
2699 GFileProgressCallback progress_callback,
2700 gpointer progress_callback_data,
2703 gssize n_read, n_written;
2704 goffset current_size;
2705 char buffer[1024*64], *p;
2711 /* avoid performance impact of querying total size when it's not needed */
2712 if (progress_callback)
2714 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2715 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2719 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2720 total_size = g_file_info_get_size (info);
2721 g_object_unref (info);
2724 if (total_size == -1)
2726 info = g_file_query_info (source,
2727 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2728 G_FILE_QUERY_INFO_NONE,
2732 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2733 total_size = g_file_info_get_size (info);
2734 g_object_unref (info);
2739 if (total_size == -1)
2746 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2756 current_size += n_read;
2761 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2762 if (n_written == -1)
2769 n_read -= n_written;
2775 if (progress_callback)
2776 progress_callback (current_size, total_size, progress_callback_data);
2779 /* Make sure we send full copied size */
2780 if (progress_callback)
2781 progress_callback (current_size, total_size, progress_callback_data);
2789 do_splice (int fd_in,
2794 long *bytes_transferd,
2800 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2808 else if (errsv == ENOSYS || errsv == EINVAL)
2809 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2810 _("Splice not supported"));
2812 g_set_error (error, G_IO_ERROR,
2813 g_io_error_from_errno (errsv),
2814 _("Error splicing file: %s"),
2815 g_strerror (errsv));
2820 *bytes_transferd = result;
2825 splice_stream_with_progress (GInputStream *in,
2827 GCancellable *cancellable,
2828 GFileProgressCallback progress_callback,
2829 gpointer progress_callback_data,
2839 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2840 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2842 if (pipe (buffer) != 0)
2844 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2845 "Pipe creation failed");
2850 /* avoid performance impact of querying total size when it's not needed */
2851 if (progress_callback)
2855 if (fstat (fd_in, &sbuf) == 0)
2856 total_size = sbuf.st_size;
2859 if (total_size == -1)
2862 offset_in = offset_out = 0;
2869 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2872 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2883 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2886 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2889 n_read -= n_written;
2892 if (progress_callback)
2893 progress_callback (offset_in, total_size, progress_callback_data);
2896 /* Make sure we send full copied size */
2897 if (progress_callback)
2898 progress_callback (offset_in, total_size, progress_callback_data);
2909 file_copy_fallback (GFile *source,
2911 GFileCopyFlags flags,
2912 GCancellable *cancellable,
2913 GFileProgressCallback progress_callback,
2914 gpointer progress_callback_data,
2923 gboolean fallback = TRUE;
2926 /* need to know the file type */
2927 info = g_file_query_info (source,
2928 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2929 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2936 /* Maybe copy the symlink? */
2937 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2938 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2940 target = g_file_info_get_symlink_target (info);
2943 if (!copy_symlink (destination, flags, cancellable, target, error))
2945 g_object_unref (info);
2949 g_object_unref (info);
2952 /* ... else fall back on a regular file copy */
2953 g_object_unref (info);
2955 /* Handle "special" files (pipes, device nodes, ...)? */
2956 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2958 /* FIXME: could try to recreate device nodes and others? */
2959 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2960 _("Can't copy special file"));
2961 g_object_unref (info);
2964 /* Everything else should just fall back on a regular copy. */
2966 g_object_unref (info);
2968 in = open_source_for_copy (source, destination, flags, cancellable, error);
2972 if (flags & G_FILE_COPY_OVERWRITE)
2974 out = (GOutputStream *)g_file_replace (destination,
2976 flags & G_FILE_COPY_BACKUP,
2977 G_FILE_CREATE_REPLACE_DESTINATION,
2978 cancellable, error);
2982 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2987 g_object_unref (in);
2992 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2994 GError *splice_err = NULL;
2996 result = splice_stream_with_progress (in, out, cancellable,
2997 progress_callback, progress_callback_data,
3000 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3004 g_propagate_error (error, splice_err);
3007 g_clear_error (&splice_err);
3012 result = copy_stream_with_progress (in, out, source, cancellable,
3013 progress_callback, progress_callback_data,
3016 /* Don't care about errors in source here */
3017 g_input_stream_close (in, cancellable, NULL);
3019 /* But write errors on close are bad! */
3020 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
3023 g_object_unref (in);
3024 g_object_unref (out);
3026 if (result == FALSE)
3030 /* Ignore errors here. Failure to copy metadata is not a hard error */
3031 g_file_copy_attributes (source, destination,
3032 flags, cancellable, NULL);
3039 * @source: input #GFile
3040 * @destination: destination #GFile
3041 * @flags: set of #GFileCopyFlags
3042 * @cancellable: (allow-none): optional #GCancellable object,
3044 * @progress_callback: (allow-none) (scope call): function to callback with
3045 * progress information, or %NULL if progress information is not needed
3046 * @progress_callback_data: (closure): user data to pass to @progress_callback
3047 * @error: #GError to set on error, or %NULL
3049 * Copies the file @source to the location specified by @destination.
3050 * Can not handle recursive copies of directories.
3052 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3053 * existing @destination file is overwritten.
3055 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3056 * will be copied as symlinks, otherwise the target of the
3057 * @source symlink will be copied.
3059 * If @cancellable is not %NULL, then the operation can be cancelled by
3060 * triggering the cancellable object from another thread. If the operation
3061 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3063 * If @progress_callback is not %NULL, then the operation can be monitored
3064 * by setting this to a #GFileProgressCallback function.
3065 * @progress_callback_data will be passed to this function. It is guaranteed
3066 * that this callback will be called after all data has been transferred with
3067 * the total number of bytes copied during the operation.
3069 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3070 * is returned, independent on the status of the @destination.
3072 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3073 * the error %G_IO_ERROR_EXISTS is returned.
3075 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3076 * error is returned. If trying to overwrite a directory with a directory the
3077 * %G_IO_ERROR_WOULD_MERGE error is returned.
3079 * If the source is a directory and the target does not exist, or
3080 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3081 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3083 * If you are interested in copying the #GFile object itself (not the on-disk
3084 * file), see g_file_dup().
3086 * Returns: %TRUE on success, %FALSE otherwise.
3089 g_file_copy (GFile *source,
3091 GFileCopyFlags flags,
3092 GCancellable *cancellable,
3093 GFileProgressCallback progress_callback,
3094 gpointer progress_callback_data,
3101 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3102 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3104 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3107 iface = G_FILE_GET_IFACE (destination);
3111 res = (* iface->copy) (source, destination,
3113 progress_callback, progress_callback_data,
3119 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3121 g_propagate_error (error, my_error);
3125 g_clear_error (&my_error);
3128 /* If the types are different, and the destination method failed
3129 * also try the source method
3131 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3133 iface = G_FILE_GET_IFACE (source);
3138 res = (* iface->copy) (source, destination,
3140 progress_callback, progress_callback_data,
3146 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3148 g_propagate_error (error, my_error);
3152 g_clear_error (&my_error);
3156 return file_copy_fallback (source, destination, flags, cancellable,
3157 progress_callback, progress_callback_data,
3162 * g_file_copy_async: (skip)
3163 * @source: input #GFile
3164 * @destination: destination #GFile
3165 * @flags: set of #GFileCopyFlags
3166 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3168 * @cancellable: (allow-none): optional #GCancellable object,
3170 * @progress_callback: (allow-none): function to callback with progress
3171 * information, or %NULL if progress information is not needed
3172 * @progress_callback_data: (closure): user data to pass to @progress_callback
3173 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3174 * @user_data: the data to pass to callback function
3176 * Copies the file @source to the location specified by @destination
3177 * asynchronously. For details of the behaviour, see g_file_copy().
3179 * If @progress_callback is not %NULL, then that function that will be called
3180 * just like in g_file_copy(), however the callback will run in the main loop,
3181 * not in the thread that is doing the I/O operation.
3183 * When the operation is finished, @callback will be called. You can then call
3184 * g_file_copy_finish() to get the result of the operation.
3187 g_file_copy_async (GFile *source,
3189 GFileCopyFlags flags,
3191 GCancellable *cancellable,
3192 GFileProgressCallback progress_callback,
3193 gpointer progress_callback_data,
3194 GAsyncReadyCallback callback,
3199 g_return_if_fail (G_IS_FILE (source));
3200 g_return_if_fail (G_IS_FILE (destination));
3202 iface = G_FILE_GET_IFACE (source);
3203 (* iface->copy_async) (source,
3209 progress_callback_data,
3215 * g_file_copy_finish:
3216 * @file: input #GFile
3217 * @res: a #GAsyncResult
3218 * @error: a #GError, or %NULL
3220 * Finishes copying the file started with g_file_copy_async().
3222 * Returns: a %TRUE on success, %FALSE on error.
3225 g_file_copy_finish (GFile *file,
3231 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3232 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3234 if (g_async_result_legacy_propagate_error (res, error))
3237 iface = G_FILE_GET_IFACE (file);
3238 return (* iface->copy_finish) (file, res, error);
3243 * @source: #GFile pointing to the source location
3244 * @destination: #GFile pointing to the destination location
3245 * @flags: set of #GFileCopyFlags
3246 * @cancellable: (allow-none): optional #GCancellable object,
3248 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3249 * function for updates
3250 * @progress_callback_data: (closure): gpointer to user data for
3251 * the callback function
3252 * @error: #GError for returning error conditions, or %NULL
3254 * Tries to move the file or directory @source to the location specified
3255 * by @destination. If native move operations are supported then this is
3256 * used, otherwise a copy + delete fallback is used. The native
3257 * implementation may support moving directories (for instance on moves
3258 * inside the same filesystem), but the fallback code does not.
3260 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3261 * existing @destination file is overwritten.
3263 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3264 * will be copied as symlinks, otherwise the target of the
3265 * @source symlink will be copied.
3267 * If @cancellable is not %NULL, then the operation can be cancelled by
3268 * triggering the cancellable object from another thread. If the operation
3269 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3271 * If @progress_callback is not %NULL, then the operation can be monitored
3272 * by setting this to a #GFileProgressCallback function.
3273 * @progress_callback_data will be passed to this function. It is
3274 * guaranteed that this callback will be called after all data has been
3275 * transferred with the total number of bytes copied during the operation.
3277 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3278 * error is returned, independent on the status of the @destination.
3280 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3281 * then the error %G_IO_ERROR_EXISTS is returned.
3283 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3284 * error is returned. If trying to overwrite a directory with a directory the
3285 * %G_IO_ERROR_WOULD_MERGE error is returned.
3287 * If the source is a directory and the target does not exist, or
3288 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3289 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3290 * move operation isn't available).
3292 * Returns: %TRUE on successful move, %FALSE otherwise.
3295 g_file_move (GFile *source,
3297 GFileCopyFlags flags,
3298 GCancellable *cancellable,
3299 GFileProgressCallback progress_callback,
3300 gpointer progress_callback_data,
3307 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3308 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3310 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3313 iface = G_FILE_GET_IFACE (destination);
3317 res = (* iface->move) (source, destination,
3319 progress_callback, progress_callback_data,
3325 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3327 g_propagate_error (error, my_error);
3332 /* If the types are different, and the destination method failed
3333 * also try the source method
3335 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3337 iface = G_FILE_GET_IFACE (source);
3342 res = (* iface->move) (source, destination,
3344 progress_callback, progress_callback_data,
3350 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3352 g_propagate_error (error, my_error);
3358 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3360 g_set_error_literal (error, G_IO_ERROR,
3361 G_IO_ERROR_NOT_SUPPORTED,
3362 _("Operation not supported"));
3366 flags |= G_FILE_COPY_ALL_METADATA;
3367 if (!g_file_copy (source, destination, flags, cancellable,
3368 progress_callback, progress_callback_data,
3372 return g_file_delete (source, cancellable, error);
3376 * g_file_make_directory:
3377 * @file: input #GFile
3378 * @cancellable: (allow-none): optional #GCancellable object,
3380 * @error: a #GError, or %NULL
3382 * Creates a directory. Note that this will only create a child directory
3383 * of the immediate parent directory of the path or URI given by the #GFile.
3384 * To recursively create directories, see g_file_make_directory_with_parents().
3385 * This function will fail if the parent directory does not exist, setting
3386 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3387 * creating directories, this function will fail, setting @error to
3388 * %G_IO_ERROR_NOT_SUPPORTED.
3390 * For a local #GFile the newly created directory will have the default
3391 * (current) ownership and permissions of the current process.
3393 * If @cancellable is not %NULL, then the operation can be cancelled by
3394 * triggering the cancellable object from another thread. If the operation
3395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3397 * Returns: %TRUE on successful creation, %FALSE otherwise.
3400 g_file_make_directory (GFile *file,
3401 GCancellable *cancellable,
3406 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3411 iface = G_FILE_GET_IFACE (file);
3413 if (iface->make_directory == NULL)
3415 g_set_error_literal (error, G_IO_ERROR,
3416 G_IO_ERROR_NOT_SUPPORTED,
3417 _("Operation not supported"));
3421 return (* iface->make_directory) (file, cancellable, error);
3425 * g_file_make_directory_with_parents:
3426 * @file: input #GFile
3427 * @cancellable: (allow-none): optional #GCancellable object,
3429 * @error: a #GError, or %NULL
3431 * Creates a directory and any parent directories that may not
3432 * exist similar to 'mkdir -p'. If the file system does not support
3433 * creating directories, this function will fail, setting @error to
3434 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3435 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3436 * the similar g_mkdir_with_parents().
3438 * For a local #GFile the newly created directories will have the default
3439 * (current) ownership and permissions of the current process.
3441 * If @cancellable is not %NULL, then the operation can be cancelled by
3442 * triggering the cancellable object from another thread. If the operation
3443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3445 * Returns: %TRUE if all directories have been successfully created, %FALSE
3451 g_file_make_directory_with_parents (GFile *file,
3452 GCancellable *cancellable,
3455 GFile *work_file = NULL;
3456 GList *list = NULL, *l;
3457 GError *my_error = NULL;
3459 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3461 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3464 g_file_make_directory (file, cancellable, &my_error);
3465 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3468 g_propagate_error (error, my_error);
3469 return my_error == NULL;
3472 work_file = g_object_ref (file);
3474 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3478 parent_file = g_file_get_parent (work_file);
3479 if (parent_file == NULL)
3482 g_clear_error (&my_error);
3483 g_file_make_directory (parent_file, cancellable, &my_error);
3485 g_object_unref (work_file);
3486 work_file = g_object_ref (parent_file);
3488 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3489 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3491 g_object_unref (parent_file);
3494 for (l = list; my_error == NULL && l; l = l->next)
3496 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3500 g_object_unref (work_file);
3503 while (list != NULL)
3505 g_object_unref ((GFile *) list->data);
3506 list = g_list_remove (list, list->data);
3509 if (my_error != NULL)
3511 g_propagate_error (error, my_error);
3515 return g_file_make_directory (file, cancellable, error);
3519 * g_file_make_symbolic_link:
3520 * @file: a #GFile with the name of the symlink to create
3521 * @symlink_value: a string with the path for the target of the new symlink
3522 * @cancellable: (allow-none): optional #GCancellable object,
3526 * Creates a symbolic link named @file which contains the string
3529 * If @cancellable is not %NULL, then the operation can be cancelled by
3530 * triggering the cancellable object from another thread. If the operation
3531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3533 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3536 g_file_make_symbolic_link (GFile *file,
3537 const char *symlink_value,
3538 GCancellable *cancellable,
3543 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3544 g_return_val_if_fail (symlink_value != NULL, FALSE);
3546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3549 if (*symlink_value == '\0')
3551 g_set_error_literal (error, G_IO_ERROR,
3552 G_IO_ERROR_INVALID_ARGUMENT,
3553 _("Invalid symlink value given"));
3557 iface = G_FILE_GET_IFACE (file);
3559 if (iface->make_symbolic_link == NULL)
3561 g_set_error_literal (error, G_IO_ERROR,
3562 G_IO_ERROR_NOT_SUPPORTED,
3563 _("Operation not supported"));
3567 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3572 * @file: input #GFile
3573 * @cancellable: (allow-none): optional #GCancellable object,
3575 * @error: a #GError, or %NULL
3577 * Deletes a file. If the @file is a directory, it will only be
3578 * deleted if it is empty. This has the same semantics as g_unlink().
3580 * If @cancellable is not %NULL, then the operation can be cancelled by
3581 * triggering the cancellable object from another thread. If the operation
3582 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3584 * Virtual: delete_file
3585 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3588 g_file_delete (GFile *file,
3589 GCancellable *cancellable,
3594 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3596 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3599 iface = G_FILE_GET_IFACE (file);
3601 if (iface->delete_file == NULL)
3603 g_set_error_literal (error, G_IO_ERROR,
3604 G_IO_ERROR_NOT_SUPPORTED,
3605 _("Operation not supported"));
3609 return (* iface->delete_file) (file, cancellable, error);
3613 * g_file_delete_async:
3614 * @file: input #GFile
3615 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3617 * @cancellable: (allow-none): optional #GCancellable object,
3619 * @callback: a #GAsyncReadyCallback to call
3620 * when the request is satisfied
3621 * @user_data: the data to pass to callback function
3623 * Asynchronously delete a file. If the @file is a directory, it will
3624 * only be deleted if it is empty. This has the same semantics as
3627 * Virtual: delete_file_async
3631 g_file_delete_async (GFile *file,
3633 GCancellable *cancellable,
3634 GAsyncReadyCallback callback,
3639 g_return_if_fail (G_IS_FILE (file));
3641 iface = G_FILE_GET_IFACE (file);
3642 (* iface->delete_file_async) (file,
3650 * g_file_delete_finish:
3651 * @file: input #GFile
3652 * @result: a #GAsyncResult
3653 * @error: a #GError, or %NULL
3655 * Finishes deleting a file started with g_file_delete_async().
3657 * Virtual: delete_file_finish
3661 g_file_delete_finish (GFile *file,
3662 GAsyncResult *result,
3667 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3668 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3670 if (g_async_result_legacy_propagate_error (result, error))
3673 iface = G_FILE_GET_IFACE (file);
3674 return (* iface->delete_file_finish) (file, result, error);
3679 * @file: #GFile to send to trash
3680 * @cancellable: (allow-none): optional #GCancellable object,
3682 * @error: a #GError, or %NULL
3684 * Sends @file to the "Trashcan", if possible. This is similar to
3685 * deleting it, but the user can recover it before emptying the trashcan.
3686 * Not all file systems support trashing, so this call can return the
3687 * %G_IO_ERROR_NOT_SUPPORTED error.
3689 * If @cancellable is not %NULL, then the operation can be cancelled by
3690 * triggering the cancellable object from another thread. If the operation
3691 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3693 * Returns: %TRUE on successful trash, %FALSE otherwise.
3696 g_file_trash (GFile *file,
3697 GCancellable *cancellable,
3702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3704 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3707 iface = G_FILE_GET_IFACE (file);
3709 if (iface->trash == NULL)
3711 g_set_error_literal (error,
3712 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3713 _("Trash not supported"));
3717 return (* iface->trash) (file, cancellable, error);
3721 * g_file_set_display_name:
3722 * @file: input #GFile
3723 * @display_name: a string
3724 * @cancellable: (allow-none): optional #GCancellable object,
3726 * @error: a #GError, or %NULL
3728 * Renames @file to the specified display name.
3730 * The display name is converted from UTF-8 to the correct encoding
3731 * for the target filesystem if possible and the @file is renamed to this.
3733 * If you want to implement a rename operation in the user interface the
3734 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3735 * initial value in the rename widget, and then the result after editing
3736 * should be passed to g_file_set_display_name().
3738 * On success the resulting converted filename is returned.
3740 * If @cancellable is not %NULL, then the operation can be cancelled by
3741 * triggering the cancellable object from another thread. If the operation
3742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3744 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3745 * or %NULL if there was an error.
3746 * Free the returned object with g_object_unref().
3749 g_file_set_display_name (GFile *file,
3750 const gchar *display_name,
3751 GCancellable *cancellable,
3756 g_return_val_if_fail (G_IS_FILE (file), NULL);
3757 g_return_val_if_fail (display_name != NULL, NULL);
3759 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3763 G_IO_ERROR_INVALID_ARGUMENT,
3764 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3771 iface = G_FILE_GET_IFACE (file);
3773 return (* iface->set_display_name) (file, display_name, cancellable, error);
3777 * g_file_set_display_name_async:
3778 * @file: input #GFile
3779 * @display_name: a string
3780 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3782 * @cancellable: (allow-none): optional #GCancellable object,
3784 * @callback: (scope async): a #GAsyncReadyCallback to call
3785 * when the request is satisfied
3786 * @user_data: (closure): the data to pass to callback function
3788 * Asynchronously sets the display name for a given #GFile.
3790 * For more details, see g_file_set_display_name() which is
3791 * the synchronous version of this call.
3793 * When the operation is finished, @callback will be called.
3794 * You can then call g_file_set_display_name_finish() to get
3795 * the result of the operation.
3798 g_file_set_display_name_async (GFile *file,
3799 const gchar *display_name,
3801 GCancellable *cancellable,
3802 GAsyncReadyCallback callback,
3807 g_return_if_fail (G_IS_FILE (file));
3808 g_return_if_fail (display_name != NULL);
3810 iface = G_FILE_GET_IFACE (file);
3811 (* iface->set_display_name_async) (file,
3820 * g_file_set_display_name_finish:
3821 * @file: input #GFile
3822 * @res: a #GAsyncResult
3823 * @error: a #GError, or %NULL
3825 * Finishes setting a display name started with
3826 * g_file_set_display_name_async().
3828 * Returns: (transfer full): a #GFile or %NULL on error.
3829 * Free the returned object with g_object_unref().
3832 g_file_set_display_name_finish (GFile *file,
3838 g_return_val_if_fail (G_IS_FILE (file), NULL);
3839 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3841 if (g_async_result_legacy_propagate_error (res, error))
3844 iface = G_FILE_GET_IFACE (file);
3845 return (* iface->set_display_name_finish) (file, res, error);
3849 * g_file_query_settable_attributes:
3850 * @file: input #GFile
3851 * @cancellable: (allow-none): optional #GCancellable object,
3853 * @error: a #GError, or %NULL
3855 * Obtain the list of settable attributes for the file.
3857 * Returns the type and full attribute name of all the attributes
3858 * that can be set on this file. This doesn't mean setting it will
3859 * always succeed though, you might get an access failure, or some
3860 * specific file may not support a specific attribute.
3862 * If @cancellable is not %NULL, then the operation can be cancelled by
3863 * triggering the cancellable object from another thread. If the operation
3864 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3866 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3867 * When you are done with it, release it with
3868 * g_file_attribute_info_list_unref()
3870 GFileAttributeInfoList *
3871 g_file_query_settable_attributes (GFile *file,
3872 GCancellable *cancellable,
3877 GFileAttributeInfoList *list;
3879 g_return_val_if_fail (G_IS_FILE (file), NULL);
3881 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3884 iface = G_FILE_GET_IFACE (file);
3886 if (iface->query_settable_attributes == NULL)
3887 return g_file_attribute_info_list_new ();
3890 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3894 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3896 list = g_file_attribute_info_list_new ();
3897 g_error_free (my_error);
3900 g_propagate_error (error, my_error);
3907 * g_file_query_writable_namespaces:
3908 * @file: input #GFile
3909 * @cancellable: (allow-none): optional #GCancellable object,
3911 * @error: a #GError, or %NULL
3913 * Obtain the list of attribute namespaces where new attributes
3914 * can be created by a user. An example of this is extended
3915 * attributes (in the "xattr" namespace).
3917 * If @cancellable is not %NULL, then the operation can be cancelled by
3918 * triggering the cancellable object from another thread. If the operation
3919 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3921 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3922 * When you are done with it, release it with
3923 * g_file_attribute_info_list_unref()
3925 GFileAttributeInfoList *
3926 g_file_query_writable_namespaces (GFile *file,
3927 GCancellable *cancellable,
3932 GFileAttributeInfoList *list;
3934 g_return_val_if_fail (G_IS_FILE (file), NULL);
3936 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3939 iface = G_FILE_GET_IFACE (file);
3941 if (iface->query_writable_namespaces == NULL)
3942 return g_file_attribute_info_list_new ();
3945 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3949 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3951 list = g_file_attribute_info_list_new ();
3952 g_error_free (my_error);
3955 g_propagate_error (error, my_error);
3962 * g_file_set_attribute:
3963 * @file: input #GFile
3964 * @attribute: a string containing the attribute's name
3965 * @type: The type of the attribute
3966 * @value_p: (allow-none): a pointer to the value (or the pointer
3967 * itself if the type is a pointer type)
3968 * @flags: a set of #GFileQueryInfoFlags
3969 * @cancellable: (allow-none): optional #GCancellable object,
3971 * @error: a #GError, or %NULL
3973 * Sets an attribute in the file with attribute name @attribute to @value.
3975 * Some attributes can be unset by setting @attribute to
3976 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3978 * If @cancellable is not %NULL, then the operation can be cancelled by
3979 * triggering the cancellable object from another thread. If the operation
3980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3982 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3985 g_file_set_attribute (GFile *file,
3986 const gchar *attribute,
3987 GFileAttributeType type,
3989 GFileQueryInfoFlags flags,
3990 GCancellable *cancellable,
3995 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3996 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3998 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4001 iface = G_FILE_GET_IFACE (file);
4003 if (iface->set_attribute == NULL)
4005 g_set_error_literal (error, G_IO_ERROR,
4006 G_IO_ERROR_NOT_SUPPORTED,
4007 _("Operation not supported"));
4011 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4015 * g_file_set_attributes_from_info:
4016 * @file: input #GFile
4017 * @info: a #GFileInfo
4018 * @flags: #GFileQueryInfoFlags
4019 * @cancellable: (allow-none): optional #GCancellable object,
4021 * @error: a #GError, or %NULL
4023 * Tries to set all attributes in the #GFileInfo on the target
4024 * values, not stopping on the first error.
4026 * If there is any error during this operation then @error will
4027 * be set to the first error. Error on particular fields are flagged
4028 * by setting the "status" field in the attribute value to
4029 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4030 * also detect further errors.
4032 * If @cancellable is not %NULL, then the operation can be cancelled by
4033 * triggering the cancellable object from another thread. If the operation
4034 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4036 * Returns: %FALSE if there was any error, %TRUE otherwise.
4039 g_file_set_attributes_from_info (GFile *file,
4041 GFileQueryInfoFlags flags,
4042 GCancellable *cancellable,
4047 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4048 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4050 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4053 g_file_info_clear_status (info);
4055 iface = G_FILE_GET_IFACE (file);
4057 return (* iface->set_attributes_from_info) (file,
4065 g_file_real_set_attributes_from_info (GFile *file,
4067 GFileQueryInfoFlags flags,
4068 GCancellable *cancellable,
4074 GFileAttributeValue *value;
4078 attributes = g_file_info_list_attributes (info, NULL);
4080 for (i = 0; attributes[i] != NULL; i++)
4082 value = _g_file_info_get_attribute_value (info, attributes[i]);
4084 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4087 if (!g_file_set_attribute (file, attributes[i],
4088 value->type, _g_file_attribute_value_peek_as_pointer (value),
4089 flags, cancellable, error))
4091 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4093 /* Don't set error multiple times */
4097 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4100 g_strfreev (attributes);
4106 * g_file_set_attributes_async:
4107 * @file: input #GFile
4108 * @info: a #GFileInfo
4109 * @flags: a #GFileQueryInfoFlags
4110 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4112 * @cancellable: (allow-none): optional #GCancellable object,
4114 * @callback: (scope async): a #GAsyncReadyCallback
4115 * @user_data: (closure): a #gpointer
4117 * Asynchronously sets the attributes of @file with @info.
4119 * For more details, see g_file_set_attributes_from_info(),
4120 * which is the synchronous version of this call.
4122 * When the operation is finished, @callback will be called.
4123 * You can then call g_file_set_attributes_finish() to get
4124 * the result of the operation.
4127 g_file_set_attributes_async (GFile *file,
4129 GFileQueryInfoFlags flags,
4131 GCancellable *cancellable,
4132 GAsyncReadyCallback callback,
4137 g_return_if_fail (G_IS_FILE (file));
4138 g_return_if_fail (G_IS_FILE_INFO (info));
4140 iface = G_FILE_GET_IFACE (file);
4141 (* iface->set_attributes_async) (file,
4151 * g_file_set_attributes_finish:
4152 * @file: input #GFile
4153 * @result: a #GAsyncResult
4154 * @info: (out) (transfer full): a #GFileInfo
4155 * @error: a #GError, or %NULL
4157 * Finishes setting an attribute started in g_file_set_attributes_async().
4159 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4162 g_file_set_attributes_finish (GFile *file,
4163 GAsyncResult *result,
4169 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4170 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4172 /* No standard handling of errors here, as we must set info even
4175 iface = G_FILE_GET_IFACE (file);
4176 return (* iface->set_attributes_finish) (file, result, info, error);
4180 * g_file_set_attribute_string:
4181 * @file: input #GFile
4182 * @attribute: a string containing the attribute's name
4183 * @value: a string containing the attribute's value
4184 * @flags: #GFileQueryInfoFlags
4185 * @cancellable: (allow-none): optional #GCancellable object,
4187 * @error: a #GError, or %NULL
4189 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4190 * If @attribute is of a different type, this operation will fail.
4192 * If @cancellable is not %NULL, then the operation can be cancelled by
4193 * triggering the cancellable object from another thread. If the operation
4194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4196 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4199 g_file_set_attribute_string (GFile *file,
4200 const char *attribute,
4202 GFileQueryInfoFlags flags,
4203 GCancellable *cancellable,
4206 return g_file_set_attribute (file, attribute,
4207 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4208 flags, cancellable, error);
4212 * g_file_set_attribute_byte_string:
4213 * @file: input #GFile
4214 * @attribute: a string containing the attribute's name
4215 * @value: a string containing the attribute's new value
4216 * @flags: a #GFileQueryInfoFlags
4217 * @cancellable: (allow-none): optional #GCancellable object,
4219 * @error: a #GError, or %NULL
4221 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4222 * If @attribute is of a different type, this operation will fail,
4225 * If @cancellable is not %NULL, then the operation can be cancelled by
4226 * triggering the cancellable object from another thread. If the operation
4227 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4229 * Returns: %TRUE if the @attribute was successfully set to @value
4230 * in the @file, %FALSE otherwise.
4233 g_file_set_attribute_byte_string (GFile *file,
4234 const gchar *attribute,
4236 GFileQueryInfoFlags flags,
4237 GCancellable *cancellable,
4240 return g_file_set_attribute (file, attribute,
4241 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4242 flags, cancellable, error);
4246 * g_file_set_attribute_uint32:
4247 * @file: input #GFile
4248 * @attribute: a string containing the attribute's name
4249 * @value: a #guint32 containing the attribute's new value
4250 * @flags: a #GFileQueryInfoFlags
4251 * @cancellable: (allow-none): optional #GCancellable object,
4253 * @error: a #GError, or %NULL
4255 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4256 * If @attribute is of a different type, this operation will fail.
4258 * If @cancellable is not %NULL, then the operation can be cancelled by
4259 * triggering the cancellable object from another thread. If the operation
4260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4262 * Returns: %TRUE if the @attribute was successfully set to @value
4263 * in the @file, %FALSE otherwise.
4266 g_file_set_attribute_uint32 (GFile *file,
4267 const gchar *attribute,
4269 GFileQueryInfoFlags flags,
4270 GCancellable *cancellable,
4273 return g_file_set_attribute (file, attribute,
4274 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4275 flags, cancellable, error);
4279 * g_file_set_attribute_int32:
4280 * @file: input #GFile
4281 * @attribute: a string containing the attribute's name
4282 * @value: a #gint32 containing the attribute's new value
4283 * @flags: a #GFileQueryInfoFlags
4284 * @cancellable: (allow-none): optional #GCancellable object,
4286 * @error: a #GError, or %NULL
4288 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4289 * If @attribute is of a different type, this operation will fail.
4291 * If @cancellable is not %NULL, then the operation can be cancelled by
4292 * triggering the cancellable object from another thread. If the operation
4293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4295 * Returns: %TRUE if the @attribute was successfully set to @value
4296 * in the @file, %FALSE otherwise.
4299 g_file_set_attribute_int32 (GFile *file,
4300 const gchar *attribute,
4302 GFileQueryInfoFlags flags,
4303 GCancellable *cancellable,
4306 return g_file_set_attribute (file, attribute,
4307 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4308 flags, cancellable, error);
4312 * g_file_set_attribute_uint64:
4313 * @file: input #GFile
4314 * @attribute: a string containing the attribute's name
4315 * @value: a #guint64 containing the attribute's new value
4316 * @flags: a #GFileQueryInfoFlags
4317 * @cancellable: (allow-none): optional #GCancellable object,
4319 * @error: a #GError, or %NULL
4321 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4322 * If @attribute is of a different type, this operation will fail.
4324 * If @cancellable is not %NULL, then the operation can be cancelled by
4325 * triggering the cancellable object from another thread. If the operation
4326 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4328 * Returns: %TRUE if the @attribute was successfully set to @value
4329 * in the @file, %FALSE otherwise.
4332 g_file_set_attribute_uint64 (GFile *file,
4333 const gchar *attribute,
4335 GFileQueryInfoFlags flags,
4336 GCancellable *cancellable,
4339 return g_file_set_attribute (file, attribute,
4340 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4341 flags, cancellable, error);
4345 * g_file_set_attribute_int64:
4346 * @file: input #GFile
4347 * @attribute: a string containing the attribute's name
4348 * @value: a #guint64 containing the attribute's new value
4349 * @flags: a #GFileQueryInfoFlags
4350 * @cancellable: (allow-none): optional #GCancellable object,
4352 * @error: a #GError, or %NULL
4354 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4355 * If @attribute is of a different type, this operation will fail.
4357 * If @cancellable is not %NULL, then the operation can be cancelled by
4358 * triggering the cancellable object from another thread. If the operation
4359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4361 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4364 g_file_set_attribute_int64 (GFile *file,
4365 const gchar *attribute,
4367 GFileQueryInfoFlags flags,
4368 GCancellable *cancellable,
4371 return g_file_set_attribute (file, attribute,
4372 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4373 flags, cancellable, error);
4377 * g_file_mount_mountable:
4378 * @file: input #GFile
4379 * @flags: flags affecting the operation
4380 * @mount_operation: (allow-none): a #GMountOperation,
4381 * or %NULL to avoid user interaction
4382 * @cancellable: (allow-none): optional #GCancellable object,
4384 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4385 * when the request is satisfied, or %NULL
4386 * @user_data: (closure): the data to pass to callback function
4388 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4389 * Using @mount_operation, you can request callbacks when, for instance,
4390 * passwords are needed during authentication.
4392 * If @cancellable is not %NULL, then the operation can be cancelled by
4393 * triggering the cancellable object from another thread. If the operation
4394 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4396 * When the operation is finished, @callback will be called.
4397 * You can then call g_file_mount_mountable_finish() to get
4398 * the result of the operation.
4401 g_file_mount_mountable (GFile *file,
4402 GMountMountFlags flags,
4403 GMountOperation *mount_operation,
4404 GCancellable *cancellable,
4405 GAsyncReadyCallback callback,
4410 g_return_if_fail (G_IS_FILE (file));
4412 iface = G_FILE_GET_IFACE (file);
4414 if (iface->mount_mountable == NULL)
4416 g_task_report_new_error (file, callback, user_data,
4417 g_file_mount_mountable,
4418 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4419 _("Operation not supported"));
4423 (* iface->mount_mountable) (file,
4432 * g_file_mount_mountable_finish:
4433 * @file: input #GFile
4434 * @result: a #GAsyncResult
4435 * @error: a #GError, or %NULL
4437 * Finishes a mount operation. See g_file_mount_mountable() for details.
4439 * Finish an asynchronous mount operation that was started
4440 * with g_file_mount_mountable().
4442 * Returns: (transfer full): a #GFile or %NULL on error.
4443 * Free the returned object with g_object_unref().
4446 g_file_mount_mountable_finish (GFile *file,
4447 GAsyncResult *result,
4452 g_return_val_if_fail (G_IS_FILE (file), NULL);
4453 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4455 if (g_async_result_legacy_propagate_error (result, error))
4457 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4458 return g_task_propagate_pointer (G_TASK (result), error);
4460 iface = G_FILE_GET_IFACE (file);
4461 return (* iface->mount_mountable_finish) (file, result, error);
4465 * g_file_unmount_mountable:
4466 * @file: input #GFile
4467 * @flags: flags affecting the operation
4468 * @cancellable: (allow-none): optional #GCancellable object,
4470 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4471 * when the request is satisfied, or %NULL
4472 * @user_data: (closure): the data to pass to callback function
4474 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4476 * If @cancellable is not %NULL, then the operation can be cancelled by
4477 * triggering the cancellable object from another thread. If the operation
4478 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4480 * When the operation is finished, @callback will be called.
4481 * You can then call g_file_unmount_mountable_finish() to get
4482 * the result of the operation.
4484 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4487 g_file_unmount_mountable (GFile *file,
4488 GMountUnmountFlags flags,
4489 GCancellable *cancellable,
4490 GAsyncReadyCallback callback,
4495 g_return_if_fail (G_IS_FILE (file));
4497 iface = G_FILE_GET_IFACE (file);
4499 if (iface->unmount_mountable == NULL)
4501 g_task_report_new_error (file, callback, user_data,
4502 g_file_unmount_mountable_with_operation,
4503 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4504 _("Operation not supported"));
4508 (* iface->unmount_mountable) (file,
4516 * g_file_unmount_mountable_finish:
4517 * @file: input #GFile
4518 * @result: a #GAsyncResult
4519 * @error: a #GError, or %NULL
4521 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4523 * Finish an asynchronous unmount operation that was started
4524 * with g_file_unmount_mountable().
4526 * Returns: %TRUE if the operation finished successfully.
4529 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4533 g_file_unmount_mountable_finish (GFile *file,
4534 GAsyncResult *result,
4539 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4540 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4542 if (g_async_result_legacy_propagate_error (result, error))
4544 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4545 return g_task_propagate_boolean (G_TASK (result), error);
4547 iface = G_FILE_GET_IFACE (file);
4548 return (* iface->unmount_mountable_finish) (file, result, error);
4552 * g_file_unmount_mountable_with_operation:
4553 * @file: input #GFile
4554 * @flags: flags affecting the operation
4555 * @mount_operation: (allow-none): a #GMountOperation,
4556 * or %NULL to avoid user interaction
4557 * @cancellable: (allow-none): optional #GCancellable object,
4559 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4560 * when the request is satisfied, or %NULL
4561 * @user_data: (closure): the data to pass to callback function
4563 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4565 * If @cancellable is not %NULL, then the operation can be cancelled by
4566 * triggering the cancellable object from another thread. If the operation
4567 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4569 * When the operation is finished, @callback will be called.
4570 * You can then call g_file_unmount_mountable_finish() to get
4571 * the result of the operation.
4576 g_file_unmount_mountable_with_operation (GFile *file,
4577 GMountUnmountFlags flags,
4578 GMountOperation *mount_operation,
4579 GCancellable *cancellable,
4580 GAsyncReadyCallback callback,
4585 g_return_if_fail (G_IS_FILE (file));
4587 iface = G_FILE_GET_IFACE (file);
4589 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4591 g_task_report_new_error (file, callback, user_data,
4592 g_file_unmount_mountable_with_operation,
4593 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4594 _("Operation not supported"));
4598 if (iface->unmount_mountable_with_operation != NULL)
4599 (* iface->unmount_mountable_with_operation) (file,
4606 (* iface->unmount_mountable) (file,
4614 * g_file_unmount_mountable_with_operation_finish:
4615 * @file: input #GFile
4616 * @result: a #GAsyncResult
4617 * @error: a #GError, or %NULL
4619 * Finishes an unmount operation,
4620 * see g_file_unmount_mountable_with_operation() for details.
4622 * Finish an asynchronous unmount operation that was started
4623 * with g_file_unmount_mountable_with_operation().
4625 * Returns: %TRUE if the operation finished successfully.
4631 g_file_unmount_mountable_with_operation_finish (GFile *file,
4632 GAsyncResult *result,
4637 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4638 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4640 if (g_async_result_legacy_propagate_error (result, error))
4642 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4643 return g_task_propagate_boolean (G_TASK (result), error);
4645 iface = G_FILE_GET_IFACE (file);
4646 if (iface->unmount_mountable_with_operation_finish != NULL)
4647 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4649 return (* iface->unmount_mountable_finish) (file, result, error);
4653 * g_file_eject_mountable:
4654 * @file: input #GFile
4655 * @flags: flags affecting the operation
4656 * @cancellable: (allow-none): optional #GCancellable object,
4658 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4659 * when the request is satisfied, or %NULL
4660 * @user_data: (closure): the data to pass to callback function
4662 * Starts an asynchronous eject on a mountable.
4663 * When this operation has completed, @callback will be called with
4664 * @user_user data, and the operation can be finalized with
4665 * g_file_eject_mountable_finish().
4667 * If @cancellable is not %NULL, then the operation can be cancelled by
4668 * triggering the cancellable object from another thread. If the operation
4669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4671 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4674 g_file_eject_mountable (GFile *file,
4675 GMountUnmountFlags flags,
4676 GCancellable *cancellable,
4677 GAsyncReadyCallback callback,
4682 g_return_if_fail (G_IS_FILE (file));
4684 iface = G_FILE_GET_IFACE (file);
4686 if (iface->eject_mountable == NULL)
4688 g_task_report_new_error (file, callback, user_data,
4689 g_file_eject_mountable_with_operation,
4690 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4691 _("Operation not supported"));
4695 (* iface->eject_mountable) (file,
4703 * g_file_eject_mountable_finish:
4704 * @file: input #GFile
4705 * @result: a #GAsyncResult
4706 * @error: a #GError, or %NULL
4708 * Finishes an asynchronous eject operation started by
4709 * g_file_eject_mountable().
4711 * Returns: %TRUE if the @file was ejected successfully.
4714 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4718 g_file_eject_mountable_finish (GFile *file,
4719 GAsyncResult *result,
4724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4725 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4727 if (g_async_result_legacy_propagate_error (result, error))
4729 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4730 return g_task_propagate_boolean (G_TASK (result), error);
4732 iface = G_FILE_GET_IFACE (file);
4733 return (* iface->eject_mountable_finish) (file, result, error);
4737 * g_file_eject_mountable_with_operation:
4738 * @file: input #GFile
4739 * @flags: flags affecting the operation
4740 * @mount_operation: (allow-none): a #GMountOperation,
4741 * or %NULL to avoid user interaction
4742 * @cancellable: (allow-none): optional #GCancellable object,
4744 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4745 * when the request is satisfied, or %NULL
4746 * @user_data: (closure): the data to pass to callback function
4748 * Starts an asynchronous eject on a mountable.
4749 * When this operation has completed, @callback will be called with
4750 * @user_user data, and the operation can be finalized with
4751 * g_file_eject_mountable_with_operation_finish().
4753 * If @cancellable is not %NULL, then the operation can be cancelled by
4754 * triggering the cancellable object from another thread. If the operation
4755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4760 g_file_eject_mountable_with_operation (GFile *file,
4761 GMountUnmountFlags flags,
4762 GMountOperation *mount_operation,
4763 GCancellable *cancellable,
4764 GAsyncReadyCallback callback,
4769 g_return_if_fail (G_IS_FILE (file));
4771 iface = G_FILE_GET_IFACE (file);
4773 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4775 g_task_report_new_error (file, callback, user_data,
4776 g_file_eject_mountable_with_operation,
4777 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4778 _("Operation not supported"));
4782 if (iface->eject_mountable_with_operation != NULL)
4783 (* iface->eject_mountable_with_operation) (file,
4790 (* iface->eject_mountable) (file,
4798 * g_file_eject_mountable_with_operation_finish:
4799 * @file: input #GFile
4800 * @result: a #GAsyncResult
4801 * @error: a #GError, or %NULL
4803 * Finishes an asynchronous eject operation started by
4804 * g_file_eject_mountable_with_operation().
4806 * Returns: %TRUE if the @file was ejected successfully.
4812 g_file_eject_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_eject_mountable_with_operation))
4824 return g_task_propagate_boolean (G_TASK (result), error);
4826 iface = G_FILE_GET_IFACE (file);
4827 if (iface->eject_mountable_with_operation_finish != NULL)
4828 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4830 return (* iface->eject_mountable_finish) (file, result, error);
4834 * g_file_monitor_directory:
4835 * @file: input #GFile
4836 * @flags: a set of #GFileMonitorFlags
4837 * @cancellable: (allow-none): optional #GCancellable object,
4839 * @error: a #GError, or %NULL
4841 * Obtains a directory monitor for the given file.
4842 * This may fail if directory monitoring is not supported.
4844 * If @cancellable is not %NULL, then the operation can be cancelled by
4845 * triggering the cancellable object from another thread. If the operation
4846 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4848 * It does not make sense for @flags to contain
4849 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4850 * directories. It is not possible to monitor all the files in a
4851 * directory for changes made via hard links; if you want to do this then
4852 * you must register individual watches with g_file_monitor().
4854 * Virtual: monitor_dir
4855 * Returns: (transfer full): a #GFileMonitor for the given @file,
4856 * or %NULL on error.
4857 * Free the returned object with g_object_unref().
4860 g_file_monitor_directory (GFile *file,
4861 GFileMonitorFlags flags,
4862 GCancellable *cancellable,
4867 g_return_val_if_fail (G_IS_FILE (file), NULL);
4868 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4870 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4873 iface = G_FILE_GET_IFACE (file);
4875 if (iface->monitor_dir == NULL)
4877 g_set_error_literal (error, G_IO_ERROR,
4878 G_IO_ERROR_NOT_SUPPORTED,
4879 _("Operation not supported"));
4883 return (* iface->monitor_dir) (file, flags, cancellable, error);
4887 * g_file_monitor_file:
4888 * @file: input #GFile
4889 * @flags: a set of #GFileMonitorFlags
4890 * @cancellable: (allow-none): optional #GCancellable object,
4892 * @error: a #GError, or %NULL
4894 * Obtains a file monitor for the given file. If no file notification
4895 * mechanism exists, then regular polling of the file is used.
4897 * If @cancellable is not %NULL, then the operation can be cancelled by
4898 * triggering the cancellable object from another thread. If the operation
4899 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4901 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
4902 * will also attempt to report changes made to the file via another
4903 * filename (ie, a hard link). Without this flag, you can only rely on
4904 * changes made through the filename contained in @file to be
4905 * reported. Using this flag may result in an increase in resource
4906 * usage, and may not have any effect depending on the #GFileMonitor
4907 * backend and/or filesystem type.
4909 * Returns: (transfer full): a #GFileMonitor for the given @file,
4910 * or %NULL on error.
4911 * Free the returned object with g_object_unref().
4914 g_file_monitor_file (GFile *file,
4915 GFileMonitorFlags flags,
4916 GCancellable *cancellable,
4920 GFileMonitor *monitor;
4922 g_return_val_if_fail (G_IS_FILE (file), NULL);
4924 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4927 iface = G_FILE_GET_IFACE (file);
4931 if (iface->monitor_file)
4932 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4934 /* Fallback to polling */
4935 if (monitor == NULL)
4936 monitor = _g_poll_file_monitor_new (file);
4943 * @file: input #GFile
4944 * @flags: a set of #GFileMonitorFlags
4945 * @cancellable: (allow-none): optional #GCancellable object,
4947 * @error: a #GError, or %NULL
4949 * Obtains a file or directory monitor for the given file,
4950 * depending on the type of the file.
4952 * If @cancellable is not %NULL, then the operation can be cancelled by
4953 * triggering the cancellable object from another thread. If the operation
4954 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4956 * Returns: (transfer full): a #GFileMonitor for the given @file,
4957 * or %NULL on error.
4958 * Free the returned object with g_object_unref().
4963 g_file_monitor (GFile *file,
4964 GFileMonitorFlags flags,
4965 GCancellable *cancellable,
4968 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4969 return g_file_monitor_directory (file,
4970 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
4971 cancellable, error);
4973 return g_file_monitor_file (file, flags, cancellable, error);
4976 /********************************************
4977 * Default implementation of async ops *
4978 ********************************************/
4982 GFileQueryInfoFlags flags;
4983 } QueryInfoAsyncData;
4986 query_info_data_free (QueryInfoAsyncData *data)
4988 g_free (data->attributes);
4993 query_info_async_thread (GTask *task,
4996 GCancellable *cancellable)
4998 QueryInfoAsyncData *data = task_data;
5000 GError *error = NULL;
5002 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5004 g_task_return_pointer (task, info, g_object_unref);
5006 g_task_return_error (task, error);
5010 g_file_real_query_info_async (GFile *file,
5011 const char *attributes,
5012 GFileQueryInfoFlags flags,
5014 GCancellable *cancellable,
5015 GAsyncReadyCallback callback,
5019 QueryInfoAsyncData *data;
5021 data = g_new0 (QueryInfoAsyncData, 1);
5022 data->attributes = g_strdup (attributes);
5023 data->flags = flags;
5025 task = g_task_new (file, cancellable, callback, user_data);
5026 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5027 g_task_set_priority (task, io_priority);
5028 g_task_run_in_thread (task, query_info_async_thread);
5029 g_object_unref (task);
5033 g_file_real_query_info_finish (GFile *file,
5037 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5039 return g_task_propagate_pointer (G_TASK (res), error);
5043 query_filesystem_info_async_thread (GTask *task,
5046 GCancellable *cancellable)
5048 const char *attributes = task_data;
5050 GError *error = NULL;
5052 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5054 g_task_return_pointer (task, info, g_object_unref);
5056 g_task_return_error (task, error);
5060 g_file_real_query_filesystem_info_async (GFile *file,
5061 const char *attributes,
5063 GCancellable *cancellable,
5064 GAsyncReadyCallback callback,
5069 task = g_task_new (file, cancellable, callback, user_data);
5070 g_task_set_task_data (task, g_strdup (attributes), g_free);
5071 g_task_set_priority (task, io_priority);
5072 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5073 g_object_unref (task);
5077 g_file_real_query_filesystem_info_finish (GFile *file,
5081 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5083 return g_task_propagate_pointer (G_TASK (res), error);
5087 enumerate_children_async_thread (GTask *task,
5090 GCancellable *cancellable)
5092 QueryInfoAsyncData *data = task_data;
5093 GFileEnumerator *enumerator;
5094 GError *error = NULL;
5096 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5098 g_task_return_error (task, error);
5100 g_task_return_pointer (task, enumerator, g_object_unref);
5104 g_file_real_enumerate_children_async (GFile *file,
5105 const char *attributes,
5106 GFileQueryInfoFlags flags,
5108 GCancellable *cancellable,
5109 GAsyncReadyCallback callback,
5113 QueryInfoAsyncData *data;
5115 data = g_new0 (QueryInfoAsyncData, 1);
5116 data->attributes = g_strdup (attributes);
5117 data->flags = flags;
5119 task = g_task_new (file, cancellable, callback, user_data);
5120 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5121 g_task_set_priority (task, io_priority);
5122 g_task_run_in_thread (task, enumerate_children_async_thread);
5123 g_object_unref (task);
5126 static GFileEnumerator *
5127 g_file_real_enumerate_children_finish (GFile *file,
5131 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5133 return g_task_propagate_pointer (G_TASK (res), error);
5137 open_read_async_thread (GTask *task,
5140 GCancellable *cancellable)
5143 GFileInputStream *stream;
5144 GError *error = NULL;
5146 iface = G_FILE_GET_IFACE (object);
5148 if (iface->read_fn == NULL)
5150 g_task_return_new_error (task, G_IO_ERROR,
5151 G_IO_ERROR_NOT_SUPPORTED,
5152 _("Operation not supported"));
5156 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5158 g_task_return_pointer (task, stream, g_object_unref);
5160 g_task_return_error (task, error);
5164 g_file_real_read_async (GFile *file,
5166 GCancellable *cancellable,
5167 GAsyncReadyCallback callback,
5172 task = g_task_new (file, cancellable, callback, user_data);
5173 g_task_set_priority (task, io_priority);
5174 g_task_run_in_thread (task, open_read_async_thread);
5175 g_object_unref (task);
5178 static GFileInputStream *
5179 g_file_real_read_finish (GFile *file,
5183 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5185 return g_task_propagate_pointer (G_TASK (res), error);
5189 append_to_async_thread (GTask *task,
5190 gpointer source_object,
5192 GCancellable *cancellable)
5195 GFileCreateFlags *data = task_data;
5196 GFileOutputStream *stream;
5197 GError *error = NULL;
5199 iface = G_FILE_GET_IFACE (source_object);
5201 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5203 g_task_return_pointer (task, stream, g_object_unref);
5205 g_task_return_error (task, error);
5209 g_file_real_append_to_async (GFile *file,
5210 GFileCreateFlags flags,
5212 GCancellable *cancellable,
5213 GAsyncReadyCallback callback,
5216 GFileCreateFlags *data;
5219 data = g_new0 (GFileCreateFlags, 1);
5222 task = g_task_new (file, cancellable, callback, user_data);
5223 g_task_set_task_data (task, data, g_free);
5224 g_task_set_priority (task, io_priority);
5226 g_task_run_in_thread (task, append_to_async_thread);
5227 g_object_unref (task);
5230 static GFileOutputStream *
5231 g_file_real_append_to_finish (GFile *file,
5235 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5237 return g_task_propagate_pointer (G_TASK (res), error);
5241 create_async_thread (GTask *task,
5242 gpointer source_object,
5244 GCancellable *cancellable)
5247 GFileCreateFlags *data = task_data;
5248 GFileOutputStream *stream;
5249 GError *error = NULL;
5251 iface = G_FILE_GET_IFACE (source_object);
5253 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5255 g_task_return_pointer (task, stream, g_object_unref);
5257 g_task_return_error (task, error);
5261 g_file_real_create_async (GFile *file,
5262 GFileCreateFlags flags,
5264 GCancellable *cancellable,
5265 GAsyncReadyCallback callback,
5268 GFileCreateFlags *data;
5271 data = g_new0 (GFileCreateFlags, 1);
5274 task = g_task_new (file, cancellable, callback, user_data);
5275 g_task_set_task_data (task, data, g_free);
5276 g_task_set_priority (task, io_priority);
5278 g_task_run_in_thread (task, create_async_thread);
5279 g_object_unref (task);
5282 static GFileOutputStream *
5283 g_file_real_create_finish (GFile *file,
5287 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5289 return g_task_propagate_pointer (G_TASK (res), error);
5293 GFileOutputStream *stream;
5295 gboolean make_backup;
5296 GFileCreateFlags flags;
5300 replace_async_data_free (ReplaceAsyncData *data)
5303 g_object_unref (data->stream);
5304 g_free (data->etag);
5309 replace_async_thread (GTask *task,
5310 gpointer source_object,
5312 GCancellable *cancellable)
5315 GFileOutputStream *stream;
5316 ReplaceAsyncData *data = task_data;
5317 GError *error = NULL;
5319 iface = G_FILE_GET_IFACE (source_object);
5321 stream = iface->replace (G_FILE (source_object),
5329 g_task_return_pointer (task, stream, g_object_unref);
5331 g_task_return_error (task, error);
5335 g_file_real_replace_async (GFile *file,
5337 gboolean make_backup,
5338 GFileCreateFlags flags,
5340 GCancellable *cancellable,
5341 GAsyncReadyCallback callback,
5345 ReplaceAsyncData *data;
5347 data = g_new0 (ReplaceAsyncData, 1);
5348 data->etag = g_strdup (etag);
5349 data->make_backup = make_backup;
5350 data->flags = flags;
5352 task = g_task_new (file, cancellable, callback, user_data);
5353 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5354 g_task_set_priority (task, io_priority);
5356 g_task_run_in_thread (task, replace_async_thread);
5357 g_object_unref (task);
5360 static GFileOutputStream *
5361 g_file_real_replace_finish (GFile *file,
5365 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5367 return g_task_propagate_pointer (G_TASK (res), error);
5371 delete_async_thread (GTask *task,
5374 GCancellable *cancellable)
5376 GFile *file = object;
5378 GError *error = NULL;
5380 iface = G_FILE_GET_IFACE (object);
5382 if (iface->delete_file (file,
5385 g_task_return_boolean (task, TRUE);
5387 g_task_return_error (task, error);
5391 g_file_real_delete_async (GFile *file,
5393 GCancellable *cancellable,
5394 GAsyncReadyCallback callback,
5399 task = g_task_new (file, cancellable, callback, user_data);
5400 g_task_set_priority (task, io_priority);
5401 g_task_run_in_thread (task, delete_async_thread);
5402 g_object_unref (task);
5406 g_file_real_delete_finish (GFile *file,
5410 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5412 return g_task_propagate_boolean (G_TASK (res), error);
5416 open_readwrite_async_thread (GTask *task,
5419 GCancellable *cancellable)
5422 GFileIOStream *stream;
5423 GError *error = NULL;
5425 iface = G_FILE_GET_IFACE (object);
5427 if (iface->open_readwrite == NULL)
5429 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5430 _("Operation not supported"));
5434 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5437 g_task_return_error (task, error);
5439 g_task_return_pointer (task, stream, g_object_unref);
5443 g_file_real_open_readwrite_async (GFile *file,
5445 GCancellable *cancellable,
5446 GAsyncReadyCallback callback,
5451 task = g_task_new (file, cancellable, callback, user_data);
5452 g_task_set_priority (task, io_priority);
5454 g_task_run_in_thread (task, open_readwrite_async_thread);
5455 g_object_unref (task);
5458 static GFileIOStream *
5459 g_file_real_open_readwrite_finish (GFile *file,
5463 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5465 return g_task_propagate_pointer (G_TASK (res), error);
5469 create_readwrite_async_thread (GTask *task,
5472 GCancellable *cancellable)
5475 GFileCreateFlags *data = task_data;
5476 GFileIOStream *stream;
5477 GError *error = NULL;
5479 iface = G_FILE_GET_IFACE (object);
5481 if (iface->create_readwrite == NULL)
5483 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5484 _("Operation not supported"));
5488 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5491 g_task_return_error (task, error);
5493 g_task_return_pointer (task, stream, g_object_unref);
5497 g_file_real_create_readwrite_async (GFile *file,
5498 GFileCreateFlags flags,
5500 GCancellable *cancellable,
5501 GAsyncReadyCallback callback,
5504 GFileCreateFlags *data;
5507 data = g_new0 (GFileCreateFlags, 1);
5510 task = g_task_new (file, cancellable, callback, user_data);
5511 g_task_set_task_data (task, data, g_free);
5512 g_task_set_priority (task, io_priority);
5514 g_task_run_in_thread (task, create_readwrite_async_thread);
5515 g_object_unref (task);
5518 static GFileIOStream *
5519 g_file_real_create_readwrite_finish (GFile *file,
5523 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5525 return g_task_propagate_pointer (G_TASK (res), error);
5530 gboolean make_backup;
5531 GFileCreateFlags flags;
5532 } ReplaceRWAsyncData;
5535 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5537 g_free (data->etag);
5542 replace_readwrite_async_thread (GTask *task,
5545 GCancellable *cancellable)
5548 GFileIOStream *stream;
5549 GError *error = NULL;
5550 ReplaceRWAsyncData *data = task_data;
5552 iface = G_FILE_GET_IFACE (object);
5554 stream = iface->replace_readwrite (G_FILE (object),
5562 g_task_return_error (task, error);
5564 g_task_return_pointer (task, stream, g_object_unref);
5568 g_file_real_replace_readwrite_async (GFile *file,
5570 gboolean make_backup,
5571 GFileCreateFlags flags,
5573 GCancellable *cancellable,
5574 GAsyncReadyCallback callback,
5578 ReplaceRWAsyncData *data;
5580 data = g_new0 (ReplaceRWAsyncData, 1);
5581 data->etag = g_strdup (etag);
5582 data->make_backup = make_backup;
5583 data->flags = flags;
5585 task = g_task_new (file, cancellable, callback, user_data);
5586 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5587 g_task_set_priority (task, io_priority);
5589 g_task_run_in_thread (task, replace_readwrite_async_thread);
5590 g_object_unref (task);
5593 static GFileIOStream *
5594 g_file_real_replace_readwrite_finish (GFile *file,
5598 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5600 return g_task_propagate_pointer (G_TASK (res), error);
5604 set_display_name_async_thread (GTask *task,
5607 GCancellable *cancellable)
5609 GError *error = NULL;
5610 char *name = task_data;
5613 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5616 g_task_return_error (task, error);
5618 g_task_return_pointer (task, file, g_object_unref);
5622 g_file_real_set_display_name_async (GFile *file,
5623 const char *display_name,
5625 GCancellable *cancellable,
5626 GAsyncReadyCallback callback,
5631 task = g_task_new (file, cancellable, callback, user_data);
5632 g_task_set_task_data (task, g_strdup (display_name), g_free);
5633 g_task_set_priority (task, io_priority);
5635 g_task_run_in_thread (task, set_display_name_async_thread);
5636 g_object_unref (task);
5640 g_file_real_set_display_name_finish (GFile *file,
5644 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5646 return g_task_propagate_pointer (G_TASK (res), error);
5650 GFileQueryInfoFlags flags;
5657 set_info_data_free (SetInfoAsyncData *data)
5660 g_object_unref (data->info);
5662 g_error_free (data->error);
5667 set_info_async_thread (GTask *task,
5670 GCancellable *cancellable)
5672 SetInfoAsyncData *data = task_data;
5675 data->res = g_file_set_attributes_from_info (G_FILE (object),
5683 g_file_real_set_attributes_async (GFile *file,
5685 GFileQueryInfoFlags flags,
5687 GCancellable *cancellable,
5688 GAsyncReadyCallback callback,
5692 SetInfoAsyncData *data;
5694 data = g_new0 (SetInfoAsyncData, 1);
5695 data->info = g_file_info_dup (info);
5696 data->flags = flags;
5698 task = g_task_new (file, cancellable, callback, user_data);
5699 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5700 g_task_set_priority (task, io_priority);
5702 g_task_run_in_thread (task, set_info_async_thread);
5703 g_object_unref (task);
5707 g_file_real_set_attributes_finish (GFile *file,
5712 SetInfoAsyncData *data;
5714 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5716 data = g_task_get_task_data (G_TASK (res));
5719 *info = g_object_ref (data->info);
5721 if (error != NULL && data->error)
5722 *error = g_error_copy (data->error);
5728 find_enclosing_mount_async_thread (GTask *task,
5731 GCancellable *cancellable)
5733 GError *error = NULL;
5736 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5739 g_task_return_error (task, error);
5741 g_task_return_pointer (task, mount, g_object_unref);
5745 g_file_real_find_enclosing_mount_async (GFile *file,
5747 GCancellable *cancellable,
5748 GAsyncReadyCallback callback,
5753 task = g_task_new (file, cancellable, callback, user_data);
5754 g_task_set_priority (task, io_priority);
5756 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5757 g_object_unref (task);
5761 g_file_real_find_enclosing_mount_finish (GFile *file,
5765 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5767 return g_task_propagate_pointer (G_TASK (res), error);
5774 GFileCopyFlags flags;
5775 GFileProgressCallback progress_cb;
5776 gpointer progress_cb_data;
5780 copy_async_data_free (CopyAsyncData *data)
5782 g_object_unref (data->source);
5783 g_object_unref (data->destination);
5784 g_slice_free (CopyAsyncData, data);
5788 CopyAsyncData *data;
5789 goffset current_num_bytes;
5790 goffset total_num_bytes;
5794 copy_async_progress_in_main (gpointer user_data)
5796 ProgressData *progress = user_data;
5797 CopyAsyncData *data = progress->data;
5799 data->progress_cb (progress->current_num_bytes,
5800 progress->total_num_bytes,
5801 data->progress_cb_data);
5807 copy_async_progress_callback (goffset current_num_bytes,
5808 goffset total_num_bytes,
5811 GTask *task = user_data;
5812 CopyAsyncData *data = g_task_get_task_data (task);
5813 ProgressData *progress;
5815 progress = g_new (ProgressData, 1);
5816 progress->data = data;
5817 progress->current_num_bytes = current_num_bytes;
5818 progress->total_num_bytes = total_num_bytes;
5820 g_main_context_invoke_full (g_task_get_context (task),
5821 g_task_get_priority (task),
5822 copy_async_progress_in_main,
5828 copy_async_thread (GTask *task,
5831 GCancellable *cancellable)
5833 CopyAsyncData *data = task_data;
5835 GError *error = NULL;
5837 result = g_file_copy (data->source,
5841 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5845 g_task_return_boolean (task, TRUE);
5847 g_task_return_error (task, error);
5851 g_file_real_copy_async (GFile *source,
5853 GFileCopyFlags flags,
5855 GCancellable *cancellable,
5856 GFileProgressCallback progress_callback,
5857 gpointer progress_callback_data,
5858 GAsyncReadyCallback callback,
5862 CopyAsyncData *data;
5864 data = g_slice_new (CopyAsyncData);
5865 data->source = g_object_ref (source);
5866 data->destination = g_object_ref (destination);
5867 data->flags = flags;
5868 data->progress_cb = progress_callback;
5869 data->progress_cb_data = progress_callback_data;
5871 task = g_task_new (source, cancellable, callback, user_data);
5872 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5873 g_task_set_priority (task, io_priority);
5874 g_task_run_in_thread (task, copy_async_thread);
5875 g_object_unref (task);
5879 g_file_real_copy_finish (GFile *file,
5883 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5885 return g_task_propagate_boolean (G_TASK (res), error);
5889 /********************************************
5890 * Default VFS operations *
5891 ********************************************/
5894 * g_file_new_for_path:
5895 * @path: a string containing a relative or absolute path.
5896 * The string must be encoded in the glib filename encoding.
5898 * Constructs a #GFile for a given path. This operation never
5899 * fails, but the returned object might not support any I/O
5900 * operation if @path is malformed.
5902 * Returns: (transfer full): a new #GFile for the given @path.
5903 * Free the returned object with g_object_unref().
5906 g_file_new_for_path (const char *path)
5908 g_return_val_if_fail (path != NULL, NULL);
5910 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5914 * g_file_new_for_uri:
5915 * @uri: a UTF-8 string containing a URI
5917 * Constructs a #GFile for a given URI. This operation never
5918 * fails, but the returned object might not support any I/O
5919 * operation if @uri is malformed or if the uri type is
5922 * Returns: (transfer full): a new #GFile for the given @uri.
5923 * Free the returned object with g_object_unref().
5926 g_file_new_for_uri (const char *uri)
5928 g_return_val_if_fail (uri != NULL, NULL);
5930 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5935 * @tmpl: (type filename) (allow-none): Template for the file
5936 * name, as in g_file_open_tmp(), or %NULL for a default template
5937 * @iostream: (out): on return, a #GFileIOStream for the created file
5938 * @error: a #GError, or %NULL
5940 * Opens a file in the preferred directory for temporary files (as
5941 * returned by g_get_tmp_dir()) and returns a #GFile and
5942 * #GFileIOStream pointing to it.
5944 * @tmpl should be a string in the GLib file name encoding
5945 * containing a sequence of six 'X' characters, and containing no
5946 * directory components. If it is %NULL, a default template is used.
5948 * Unlike the other #GFile constructors, this will return %NULL if
5949 * a temporary file could not be created.
5951 * Returns: (transfer full): a new #GFile.
5952 * Free the returned object with g_object_unref().
5957 g_file_new_tmp (const char *tmpl,
5958 GFileIOStream **iostream,
5964 GFileOutputStream *output;
5966 g_return_val_if_fail (iostream != NULL, NULL);
5968 fd = g_file_open_tmp (tmpl, &path, error);
5972 file = g_file_new_for_path (path);
5974 output = _g_local_file_output_stream_new (fd);
5975 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5977 g_object_unref (output);
5984 * g_file_parse_name:
5985 * @parse_name: a file name or path to be parsed
5987 * Constructs a #GFile with the given @parse_name (i.e. something
5988 * given by g_file_get_parse_name()). This operation never fails,
5989 * but the returned object might not support any I/O operation if
5990 * the @parse_name cannot be parsed.
5992 * Returns: (transfer full): a new #GFile.
5995 g_file_parse_name (const char *parse_name)
5997 g_return_val_if_fail (parse_name != NULL, NULL);
5999 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6003 is_valid_scheme_character (char c)
6005 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6008 /* Following RFC 2396, valid schemes are built like:
6009 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6012 has_valid_scheme (const char *uri)
6018 if (!g_ascii_isalpha (*p))
6023 } while (is_valid_scheme_character (*p));
6029 new_for_cmdline_arg (const gchar *arg,
6035 if (g_path_is_absolute (arg))
6036 return g_file_new_for_path (arg);
6038 if (has_valid_scheme (arg))
6039 return g_file_new_for_uri (arg);
6045 current_dir = g_get_current_dir ();
6046 filename = g_build_filename (current_dir, arg, NULL);
6047 g_free (current_dir);
6050 filename = g_build_filename (cwd, arg, NULL);
6052 file = g_file_new_for_path (filename);
6059 * g_file_new_for_commandline_arg:
6060 * @arg: a command line string
6062 * Creates a #GFile with the given argument from the command line.
6063 * The value of @arg can be either a URI, an absolute path or a
6064 * relative path resolved relative to the current working directory.
6065 * This operation never fails, but the returned object might not
6066 * support any I/O operation if @arg points to a malformed path.
6068 * Returns: (transfer full): a new #GFile.
6069 * Free the returned object with g_object_unref().
6072 g_file_new_for_commandline_arg (const char *arg)
6074 g_return_val_if_fail (arg != NULL, NULL);
6076 return new_for_cmdline_arg (arg, NULL);
6080 * g_file_new_for_commandline_arg_and_cwd:
6081 * @arg: a command line string
6082 * @cwd: the current working directory of the commandline
6084 * Creates a #GFile with the given argument from the command line.
6086 * This function is similar to g_file_new_for_commandline_arg() except
6087 * that it allows for passing the current working directory as an
6088 * argument instead of using the current working directory of the
6091 * This is useful if the commandline argument was given in a context
6092 * other than the invocation of the current process.
6094 * See also g_application_command_line_create_file_for_arg().
6096 * Returns: (transfer full): a new #GFile
6101 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6104 g_return_val_if_fail (arg != NULL, NULL);
6105 g_return_val_if_fail (cwd != NULL, NULL);
6107 return new_for_cmdline_arg (arg, cwd);
6111 * g_file_mount_enclosing_volume:
6112 * @location: input #GFile
6113 * @flags: flags affecting the operation
6114 * @mount_operation: (allow-none): a #GMountOperation
6115 * or %NULL to avoid user interaction
6116 * @cancellable: (allow-none): optional #GCancellable object,
6118 * @callback: (allow-none): a #GAsyncReadyCallback to call
6119 * when the request is satisfied, or %NULL
6120 * @user_data: the data to pass to callback function
6122 * Starts a @mount_operation, mounting the volume that contains
6123 * the file @location.
6125 * When this operation has completed, @callback will be called with
6126 * @user_user data, and the operation can be finalized with
6127 * g_file_mount_enclosing_volume_finish().
6129 * If @cancellable is not %NULL, then the operation can be cancelled by
6130 * triggering the cancellable object from another thread. If the operation
6131 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6134 g_file_mount_enclosing_volume (GFile *location,
6135 GMountMountFlags flags,
6136 GMountOperation *mount_operation,
6137 GCancellable *cancellable,
6138 GAsyncReadyCallback callback,
6143 g_return_if_fail (G_IS_FILE (location));
6145 iface = G_FILE_GET_IFACE (location);
6147 if (iface->mount_enclosing_volume == NULL)
6149 g_task_report_new_error (location, callback, user_data,
6150 g_file_mount_enclosing_volume,
6151 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6152 _("volume doesn't implement mount"));
6156 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6161 * g_file_mount_enclosing_volume_finish:
6162 * @location: input #GFile
6163 * @result: a #GAsyncResult
6164 * @error: a #GError, or %NULL
6166 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6168 * Returns: %TRUE if successful. If an error has occurred,
6169 * this function will return %FALSE and set @error
6170 * appropriately if present.
6173 g_file_mount_enclosing_volume_finish (GFile *location,
6174 GAsyncResult *result,
6179 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6180 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6182 if (g_async_result_legacy_propagate_error (result, error))
6184 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6185 return g_task_propagate_boolean (G_TASK (result), error);
6187 iface = G_FILE_GET_IFACE (location);
6189 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6192 /********************************************
6193 * Utility functions *
6194 ********************************************/
6197 * g_file_query_default_handler:
6198 * @file: a #GFile to open
6199 * @cancellable: optional #GCancellable object, %NULL to ignore
6200 * @error: a #GError, or %NULL
6202 * Returns the #GAppInfo that is registered as the default
6203 * application to handle the file specified by @file.
6205 * If @cancellable is not %NULL, then the operation can be cancelled by
6206 * triggering the cancellable object from another thread. If the operation
6207 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6209 * Returns: (transfer full): a #GAppInfo if the handle was found,
6210 * %NULL if there were errors.
6211 * When you are done with it, release it with g_object_unref()
6214 g_file_query_default_handler (GFile *file,
6215 GCancellable *cancellable,
6219 const char *content_type;
6224 uri_scheme = g_file_get_uri_scheme (file);
6225 if (uri_scheme && uri_scheme[0] != '\0')
6227 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6228 g_free (uri_scheme);
6230 if (appinfo != NULL)
6234 info = g_file_query_info (file,
6235 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6244 content_type = g_file_info_get_content_type (info);
6247 /* Don't use is_native(), as we want to support fuse paths if available */
6248 path = g_file_get_path (file);
6249 appinfo = g_app_info_get_default_for_type (content_type,
6254 g_object_unref (info);
6256 if (appinfo != NULL)
6259 g_set_error_literal (error, G_IO_ERROR,
6260 G_IO_ERROR_NOT_SUPPORTED,
6261 _("No application is registered as handling this file"));
6265 #define GET_CONTENT_BLOCK_SIZE 8192
6268 * g_file_load_contents:
6269 * @file: input #GFile
6270 * @cancellable: optional #GCancellable object, %NULL to ignore
6271 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6272 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6273 * or %NULL if the length is not needed
6274 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6275 * or %NULL if the entity tag is not needed
6276 * @error: a #GError, or %NULL
6278 * Loads the content of the file into memory. The data is always
6279 * zero-terminated, but this is not included in the resultant @length.
6280 * The returned @content should be freed with g_free() when no longer
6283 * If @cancellable is not %NULL, then the operation can be cancelled by
6284 * triggering the cancellable object from another thread. If the operation
6285 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6287 * Returns: %TRUE if the @file's contents were successfully loaded.
6288 * %FALSE if there were errors.
6291 g_file_load_contents (GFile *file,
6292 GCancellable *cancellable,
6298 GFileInputStream *in;
6299 GByteArray *content;
6304 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6305 g_return_val_if_fail (contents != NULL, FALSE);
6307 in = g_file_read (file, cancellable, error);
6311 content = g_byte_array_new ();
6314 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6315 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6316 content->data + pos,
6317 GET_CONTENT_BLOCK_SIZE,
6318 cancellable, error)) > 0)
6321 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6328 info = g_file_input_stream_query_info (in,
6329 G_FILE_ATTRIBUTE_ETAG_VALUE,
6334 *etag_out = g_strdup (g_file_info_get_etag (info));
6335 g_object_unref (info);
6339 /* Ignore errors on close */
6340 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6341 g_object_unref (in);
6345 /* error is set already */
6346 g_byte_array_free (content, TRUE);
6353 /* Zero terminate (we got an extra byte allocated for this */
6354 content->data[pos] = 0;
6356 *contents = (char *)g_byte_array_free (content, FALSE);
6363 GFileReadMoreCallback read_more_callback;
6364 GByteArray *content;
6371 load_contents_data_free (LoadContentsData *data)
6374 g_byte_array_free (data->content, TRUE);
6375 g_free (data->etag);
6380 load_contents_close_callback (GObject *obj,
6381 GAsyncResult *close_res,
6384 GInputStream *stream = G_INPUT_STREAM (obj);
6385 LoadContentsData *data = user_data;
6387 /* Ignore errors here, we're only reading anyway */
6388 g_input_stream_close_finish (stream, close_res, NULL);
6389 g_object_unref (stream);
6391 g_task_return_boolean (data->task, TRUE);
6392 g_object_unref (data->task);
6396 load_contents_fstat_callback (GObject *obj,
6397 GAsyncResult *stat_res,
6400 GInputStream *stream = G_INPUT_STREAM (obj);
6401 LoadContentsData *data = user_data;
6404 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6408 data->etag = g_strdup (g_file_info_get_etag (info));
6409 g_object_unref (info);
6412 g_input_stream_close_async (stream, 0,
6413 g_task_get_cancellable (data->task),
6414 load_contents_close_callback, data);
6418 load_contents_read_callback (GObject *obj,
6419 GAsyncResult *read_res,
6422 GInputStream *stream = G_INPUT_STREAM (obj);
6423 LoadContentsData *data = user_data;
6424 GError *error = NULL;
6427 read_size = g_input_stream_read_finish (stream, read_res, &error);
6431 /* EOF, close the file */
6432 g_task_return_error (data->task, error);
6433 g_input_stream_close_async (stream, 0,
6434 g_task_get_cancellable (data->task),
6435 load_contents_close_callback, data);
6437 else if (read_size == 0)
6439 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6440 G_FILE_ATTRIBUTE_ETAG_VALUE,
6442 g_task_get_cancellable (data->task),
6443 load_contents_fstat_callback,
6446 else if (read_size > 0)
6448 data->pos += read_size;
6450 g_byte_array_set_size (data->content,
6451 data->pos + GET_CONTENT_BLOCK_SIZE);
6454 if (data->read_more_callback &&
6455 !data->read_more_callback ((char *)data->content->data, data->pos,
6456 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6457 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6458 G_FILE_ATTRIBUTE_ETAG_VALUE,
6460 g_task_get_cancellable (data->task),
6461 load_contents_fstat_callback,
6464 g_input_stream_read_async (stream,
6465 data->content->data + data->pos,
6466 GET_CONTENT_BLOCK_SIZE,
6468 g_task_get_cancellable (data->task),
6469 load_contents_read_callback,
6475 load_contents_open_callback (GObject *obj,
6476 GAsyncResult *open_res,
6479 GFile *file = G_FILE (obj);
6480 GFileInputStream *stream;
6481 LoadContentsData *data = user_data;
6482 GError *error = NULL;
6484 stream = g_file_read_finish (file, open_res, &error);
6488 g_byte_array_set_size (data->content,
6489 data->pos + GET_CONTENT_BLOCK_SIZE);
6490 g_input_stream_read_async (G_INPUT_STREAM (stream),
6491 data->content->data + data->pos,
6492 GET_CONTENT_BLOCK_SIZE,
6494 g_task_get_cancellable (data->task),
6495 load_contents_read_callback,
6500 g_task_return_error (data->task, error);
6501 g_object_unref (data->task);
6506 * g_file_load_partial_contents_async: (skip)
6507 * @file: input #GFile
6508 * @cancellable: optional #GCancellable object, %NULL to ignore
6509 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6510 * and to specify whether further data should be read
6511 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6512 * @user_data: the data to pass to the callback functions
6514 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6515 * be used to stop reading from the file when appropriate, else this
6516 * function will behave exactly as g_file_load_contents_async(). This
6517 * operation can be finished by g_file_load_partial_contents_finish().
6519 * Users of this function should be aware that @user_data is passed to
6520 * both the @read_more_callback and the @callback.
6522 * If @cancellable is not %NULL, then the operation can be cancelled by
6523 * triggering the cancellable object from another thread. If the operation
6524 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6527 g_file_load_partial_contents_async (GFile *file,
6528 GCancellable *cancellable,
6529 GFileReadMoreCallback read_more_callback,
6530 GAsyncReadyCallback callback,
6533 LoadContentsData *data;
6535 g_return_if_fail (G_IS_FILE (file));
6537 data = g_new0 (LoadContentsData, 1);
6538 data->read_more_callback = read_more_callback;
6539 data->content = g_byte_array_new ();
6541 data->task = g_task_new (file, cancellable, callback, user_data);
6542 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6544 g_file_read_async (file,
6546 g_task_get_cancellable (data->task),
6547 load_contents_open_callback,
6552 * g_file_load_partial_contents_finish:
6553 * @file: input #GFile
6554 * @res: a #GAsyncResult
6555 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6556 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6557 * or %NULL if the length is not needed
6558 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6559 * or %NULL if the entity tag is not needed
6560 * @error: a #GError, or %NULL
6562 * Finishes an asynchronous partial load operation that was started
6563 * with g_file_load_partial_contents_async(). The data is always
6564 * zero-terminated, but this is not included in the resultant @length.
6565 * The returned @content should be freed with g_free() when no longer
6568 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6569 * present, it will be set appropriately.
6572 g_file_load_partial_contents_finish (GFile *file,
6580 LoadContentsData *data;
6582 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6583 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6584 g_return_val_if_fail (contents != NULL, FALSE);
6586 task = G_TASK (res);
6588 if (!g_task_propagate_boolean (task, error))
6595 data = g_task_get_task_data (task);
6598 *length = data->pos;
6602 *etag_out = data->etag;
6606 /* Zero terminate */
6607 g_byte_array_set_size (data->content, data->pos + 1);
6608 data->content->data[data->pos] = 0;
6610 *contents = (char *)g_byte_array_free (data->content, FALSE);
6611 data->content = NULL;
6617 * g_file_load_contents_async:
6618 * @file: input #GFile
6619 * @cancellable: optional #GCancellable object, %NULL to ignore
6620 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6621 * @user_data: the data to pass to callback function
6623 * Starts an asynchronous load of the @file's contents.
6625 * For more details, see g_file_load_contents() which is
6626 * the synchronous version of this call.
6628 * When the load operation has completed, @callback will be called
6629 * with @user data. To finish the operation, call
6630 * g_file_load_contents_finish() with the #GAsyncResult returned by
6633 * If @cancellable is not %NULL, then the operation can be cancelled by
6634 * triggering the cancellable object from another thread. If the operation
6635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6638 g_file_load_contents_async (GFile *file,
6639 GCancellable *cancellable,
6640 GAsyncReadyCallback callback,
6643 g_file_load_partial_contents_async (file,
6646 callback, user_data);
6650 * g_file_load_contents_finish:
6651 * @file: input #GFile
6652 * @res: a #GAsyncResult
6653 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6654 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6655 * or %NULL if the length is not needed
6656 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6657 * or %NULL if the entity tag is not needed
6658 * @error: a #GError, or %NULL
6660 * Finishes an asynchronous load of the @file's contents.
6661 * The contents are placed in @contents, and @length is set to the
6662 * size of the @contents string. The @content should be freed with
6663 * g_free() when no longer needed. If @etag_out is present, it will be
6664 * set to the new entity tag for the @file.
6666 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6667 * present, it will be set appropriately.
6670 g_file_load_contents_finish (GFile *file,
6677 return g_file_load_partial_contents_finish (file,
6686 * g_file_replace_contents:
6687 * @file: input #GFile
6688 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6689 * @length: the length of @contents in bytes
6690 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6691 * for the document, or %NULL
6692 * @make_backup: %TRUE if a backup should be created
6693 * @flags: a set of #GFileCreateFlags
6694 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6695 * for the document. This should be freed with g_free() when no longer
6697 * @cancellable: optional #GCancellable object, %NULL to ignore
6698 * @error: a #GError, or %NULL
6700 * Replaces the contents of @file with @contents of @length bytes.
6702 * If @etag is specified (not %NULL), any existing file must have that etag,
6703 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6705 * If @make_backup is %TRUE, this function will attempt to make a backup
6708 * If @cancellable is not %NULL, then the operation can be cancelled by
6709 * triggering the cancellable object from another thread. If the operation
6710 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6712 * The returned @new_etag can be used to verify that the file hasn't
6713 * changed the next time it is saved over.
6715 * Returns: %TRUE if successful. If an error has occurred, this function
6716 * will return %FALSE and set @error appropriately if present.
6719 g_file_replace_contents (GFile *file,
6720 const char *contents,
6723 gboolean make_backup,
6724 GFileCreateFlags flags,
6726 GCancellable *cancellable,
6729 GFileOutputStream *out;
6730 gsize pos, remainder;
6734 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6735 g_return_val_if_fail (contents != NULL, FALSE);
6737 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6743 while (remainder > 0 &&
6744 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6746 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6754 if (remainder > 0 && res < 0)
6756 /* Ignore errors on close */
6757 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6758 g_object_unref (out);
6760 /* error is set already */
6764 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6767 *new_etag = g_file_output_stream_get_etag (out);
6769 g_object_unref (out);
6776 const char *content;
6781 } ReplaceContentsData;
6784 replace_contents_data_free (ReplaceContentsData *data)
6786 g_free (data->etag);
6791 replace_contents_close_callback (GObject *obj,
6792 GAsyncResult *close_res,
6795 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6796 ReplaceContentsData *data = user_data;
6798 /* Ignore errors here, we're only reading anyway */
6799 g_output_stream_close_finish (stream, close_res, NULL);
6800 g_object_unref (stream);
6804 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6805 g_task_return_boolean (data->task, TRUE);
6807 g_object_unref (data->task);
6811 replace_contents_write_callback (GObject *obj,
6812 GAsyncResult *read_res,
6815 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6816 ReplaceContentsData *data = user_data;
6817 GError *error = NULL;
6820 write_size = g_output_stream_write_finish (stream, read_res, &error);
6822 if (write_size <= 0)
6824 /* Error or EOF, close the file */
6827 data->failed = TRUE;
6828 g_task_return_error (data->task, error);
6830 g_output_stream_close_async (stream, 0,
6831 g_task_get_cancellable (data->task),
6832 replace_contents_close_callback, data);
6834 else if (write_size > 0)
6836 data->pos += write_size;
6838 if (data->pos >= data->length)
6839 g_output_stream_close_async (stream, 0,
6840 g_task_get_cancellable (data->task),
6841 replace_contents_close_callback, data);
6843 g_output_stream_write_async (stream,
6844 data->content + data->pos,
6845 data->length - data->pos,
6847 g_task_get_cancellable (data->task),
6848 replace_contents_write_callback,
6854 replace_contents_open_callback (GObject *obj,
6855 GAsyncResult *open_res,
6858 GFile *file = G_FILE (obj);
6859 GFileOutputStream *stream;
6860 ReplaceContentsData *data = user_data;
6861 GError *error = NULL;
6863 stream = g_file_replace_finish (file, open_res, &error);
6867 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6868 data->content + data->pos,
6869 data->length - data->pos,
6871 g_task_get_cancellable (data->task),
6872 replace_contents_write_callback,
6877 g_task_return_error (data->task, error);
6878 g_object_unref (data->task);
6883 * g_file_replace_contents_async:
6884 * @file: input #GFile
6885 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6886 * @length: the length of @contents in bytes
6887 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6888 * @make_backup: %TRUE if a backup should be created
6889 * @flags: a set of #GFileCreateFlags
6890 * @cancellable: optional #GCancellable object, %NULL to ignore
6891 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6892 * @user_data: the data to pass to callback function
6894 * Starts an asynchronous replacement of @file with the given
6895 * @contents of @length bytes. @etag will replace the document's
6896 * current entity tag.
6898 * When this operation has completed, @callback will be called with
6899 * @user_user data, and the operation can be finalized with
6900 * g_file_replace_contents_finish().
6902 * If @cancellable is not %NULL, then the operation can be cancelled by
6903 * triggering the cancellable object from another thread. If the operation
6904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6906 * If @make_backup is %TRUE, this function will attempt to
6907 * make a backup of @file.
6910 g_file_replace_contents_async (GFile *file,
6911 const char *contents,
6914 gboolean make_backup,
6915 GFileCreateFlags flags,
6916 GCancellable *cancellable,
6917 GAsyncReadyCallback callback,
6920 ReplaceContentsData *data;
6922 g_return_if_fail (G_IS_FILE (file));
6923 g_return_if_fail (contents != NULL);
6925 data = g_new0 (ReplaceContentsData, 1);
6927 data->content = contents;
6928 data->length = length;
6930 data->task = g_task_new (file, cancellable, callback, user_data);
6931 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
6933 g_file_replace_async (file,
6938 g_task_get_cancellable (data->task),
6939 replace_contents_open_callback,
6944 * g_file_replace_contents_finish:
6945 * @file: input #GFile
6946 * @res: a #GAsyncResult
6947 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6948 * for the document. This should be freed with g_free() when it is no
6949 * longer needed, or %NULL
6950 * @error: a #GError, or %NULL
6952 * Finishes an asynchronous replace of the given @file. See
6953 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6954 * tag for the document, if present.
6956 * Returns: %TRUE on success, %FALSE on failure.
6959 g_file_replace_contents_finish (GFile *file,
6965 ReplaceContentsData *data;
6967 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6968 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6970 task = G_TASK (res);
6972 if (!g_task_propagate_boolean (task, error))
6975 data = g_task_get_task_data (task);
6979 *new_etag = data->etag;
6980 data->etag = NULL; /* Take ownership */
6987 * g_file_start_mountable:
6988 * @file: input #GFile
6989 * @flags: flags affecting the operation
6990 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
6991 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
6992 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
6993 * @user_data: the data to pass to callback function
6995 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
6996 * Using @start_operation, you can request callbacks when, for instance,
6997 * passwords are needed during authentication.
6999 * If @cancellable is not %NULL, then the operation can be cancelled by
7000 * triggering the cancellable object from another thread. If the operation
7001 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7003 * When the operation is finished, @callback will be called.
7004 * You can then call g_file_mount_mountable_finish() to get
7005 * the result of the operation.
7010 g_file_start_mountable (GFile *file,
7011 GDriveStartFlags flags,
7012 GMountOperation *start_operation,
7013 GCancellable *cancellable,
7014 GAsyncReadyCallback callback,
7019 g_return_if_fail (G_IS_FILE (file));
7021 iface = G_FILE_GET_IFACE (file);
7023 if (iface->start_mountable == NULL)
7025 g_task_report_new_error (file, callback, user_data,
7026 g_file_start_mountable,
7027 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7028 _("Operation not supported"));
7032 (* iface->start_mountable) (file,
7041 * g_file_start_mountable_finish:
7042 * @file: input #GFile
7043 * @result: a #GAsyncResult
7044 * @error: a #GError, or %NULL
7046 * Finishes a start operation. See g_file_start_mountable() for details.
7048 * Finish an asynchronous start operation that was started
7049 * with g_file_start_mountable().
7051 * Returns: %TRUE if the operation finished successfully. %FALSE
7057 g_file_start_mountable_finish (GFile *file,
7058 GAsyncResult *result,
7063 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7064 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7066 if (g_async_result_legacy_propagate_error (result, error))
7068 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7069 return g_task_propagate_boolean (G_TASK (result), error);
7071 iface = G_FILE_GET_IFACE (file);
7072 return (* iface->start_mountable_finish) (file, result, error);
7076 * g_file_stop_mountable:
7077 * @file: input #GFile
7078 * @flags: flags affecting the operation
7079 * @mount_operation: (allow-none): a #GMountOperation,
7080 * or %NULL to avoid user interaction.
7081 * @cancellable: (allow-none): optional #GCancellable object,
7083 * @callback: (allow-none): a #GAsyncReadyCallback to call
7084 * when the request is satisfied, or %NULL
7085 * @user_data: the data to pass to callback function
7087 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7089 * If @cancellable is not %NULL, then the operation can be cancelled by
7090 * triggering the cancellable object from another thread. If the operation
7091 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7093 * When the operation is finished, @callback will be called.
7094 * You can then call g_file_stop_mountable_finish() to get
7095 * the result of the operation.
7100 g_file_stop_mountable (GFile *file,
7101 GMountUnmountFlags flags,
7102 GMountOperation *mount_operation,
7103 GCancellable *cancellable,
7104 GAsyncReadyCallback callback,
7109 g_return_if_fail (G_IS_FILE (file));
7111 iface = G_FILE_GET_IFACE (file);
7113 if (iface->stop_mountable == NULL)
7115 g_task_report_new_error (file, callback, user_data,
7116 g_file_stop_mountable,
7117 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7118 _("Operation not supported"));
7122 (* iface->stop_mountable) (file,
7131 * g_file_stop_mountable_finish:
7132 * @file: input #GFile
7133 * @result: a #GAsyncResult
7134 * @error: a #GError, or %NULL
7136 * Finishes an stop operation, see g_file_stop_mountable() for details.
7138 * Finish an asynchronous stop operation that was started
7139 * with g_file_stop_mountable().
7141 * Returns: %TRUE if the operation finished successfully.
7147 g_file_stop_mountable_finish (GFile *file,
7148 GAsyncResult *result,
7153 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7154 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7156 if (g_async_result_legacy_propagate_error (result, error))
7158 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7159 return g_task_propagate_boolean (G_TASK (result), error);
7161 iface = G_FILE_GET_IFACE (file);
7162 return (* iface->stop_mountable_finish) (file, result, error);
7166 * g_file_poll_mountable:
7167 * @file: input #GFile
7168 * @cancellable: optional #GCancellable object, %NULL to ignore
7169 * @callback: (allow-none): a #GAsyncReadyCallback to call
7170 * when the request is satisfied, or %NULL
7171 * @user_data: the data to pass to callback function
7173 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7175 * If @cancellable is not %NULL, then the operation can be cancelled by
7176 * triggering the cancellable object from another thread. If the operation
7177 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7179 * When the operation is finished, @callback will be called.
7180 * You can then call g_file_mount_mountable_finish() to get
7181 * the result of the operation.
7186 g_file_poll_mountable (GFile *file,
7187 GCancellable *cancellable,
7188 GAsyncReadyCallback callback,
7193 g_return_if_fail (G_IS_FILE (file));
7195 iface = G_FILE_GET_IFACE (file);
7197 if (iface->poll_mountable == NULL)
7199 g_task_report_new_error (file, callback, user_data,
7200 g_file_poll_mountable,
7201 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7202 _("Operation not supported"));
7206 (* iface->poll_mountable) (file,
7213 * g_file_poll_mountable_finish:
7214 * @file: input #GFile
7215 * @result: a #GAsyncResult
7216 * @error: a #GError, or %NULL
7218 * Finishes a poll operation. See g_file_poll_mountable() for details.
7220 * Finish an asynchronous poll operation that was polled
7221 * with g_file_poll_mountable().
7223 * Returns: %TRUE if the operation finished successfully. %FALSE
7229 g_file_poll_mountable_finish (GFile *file,
7230 GAsyncResult *result,
7235 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7236 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7238 if (g_async_result_legacy_propagate_error (result, error))
7240 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7241 return g_task_propagate_boolean (G_TASK (result), error);
7243 iface = G_FILE_GET_IFACE (file);
7244 return (* iface->poll_mountable_finish) (file, result, error);
7248 * g_file_supports_thread_contexts:
7251 * Checks if @file supports <link
7252 * linkend="g-main-context-push-thread-default-context">thread-default
7253 * contexts</link>. If this returns %FALSE, you cannot perform
7254 * asynchronous operations on @file in a thread that has a
7255 * thread-default context.
7257 * Returns: Whether or not @file supports thread-default contexts.
7262 g_file_supports_thread_contexts (GFile *file)
7266 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7268 iface = G_FILE_GET_IFACE (file);
7269 return iface->supports_thread_contexts;