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 * Virtual: monitor_dir
4849 * Returns: (transfer full): a #GFileMonitor for the given @file,
4850 * or %NULL on error.
4851 * Free the returned object with g_object_unref().
4854 g_file_monitor_directory (GFile *file,
4855 GFileMonitorFlags flags,
4856 GCancellable *cancellable,
4861 g_return_val_if_fail (G_IS_FILE (file), NULL);
4863 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4866 iface = G_FILE_GET_IFACE (file);
4868 if (iface->monitor_dir == NULL)
4870 g_set_error_literal (error, G_IO_ERROR,
4871 G_IO_ERROR_NOT_SUPPORTED,
4872 _("Operation not supported"));
4876 return (* iface->monitor_dir) (file, flags, cancellable, error);
4880 * g_file_monitor_file:
4881 * @file: input #GFile
4882 * @flags: a set of #GFileMonitorFlags
4883 * @cancellable: (allow-none): optional #GCancellable object,
4885 * @error: a #GError, or %NULL
4887 * Obtains a file monitor for the given file. If no file notification
4888 * mechanism exists, then regular polling of the file is used.
4890 * If @cancellable is not %NULL, then the operation can be cancelled by
4891 * triggering the cancellable object from another thread. If the operation
4892 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4894 * Returns: (transfer full): a #GFileMonitor for the given @file,
4895 * or %NULL on error.
4896 * Free the returned object with g_object_unref().
4899 g_file_monitor_file (GFile *file,
4900 GFileMonitorFlags flags,
4901 GCancellable *cancellable,
4905 GFileMonitor *monitor;
4907 g_return_val_if_fail (G_IS_FILE (file), NULL);
4909 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4912 iface = G_FILE_GET_IFACE (file);
4916 if (iface->monitor_file)
4917 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4919 /* Fallback to polling */
4920 if (monitor == NULL)
4921 monitor = _g_poll_file_monitor_new (file);
4928 * @file: input #GFile
4929 * @flags: a set of #GFileMonitorFlags
4930 * @cancellable: (allow-none): optional #GCancellable object,
4932 * @error: a #GError, or %NULL
4934 * Obtains a file or directory monitor for the given file,
4935 * depending on the type of the file.
4937 * If @cancellable is not %NULL, then the operation can be cancelled by
4938 * triggering the cancellable object from another thread. If the operation
4939 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4941 * Returns: (transfer full): a #GFileMonitor for the given @file,
4942 * or %NULL on error.
4943 * Free the returned object with g_object_unref().
4948 g_file_monitor (GFile *file,
4949 GFileMonitorFlags flags,
4950 GCancellable *cancellable,
4953 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4954 return g_file_monitor_directory (file, flags, cancellable, error);
4956 return g_file_monitor_file (file, flags, cancellable, error);
4959 /********************************************
4960 * Default implementation of async ops *
4961 ********************************************/
4965 GFileQueryInfoFlags flags;
4966 } QueryInfoAsyncData;
4969 query_info_data_free (QueryInfoAsyncData *data)
4971 g_free (data->attributes);
4976 query_info_async_thread (GTask *task,
4979 GCancellable *cancellable)
4981 QueryInfoAsyncData *data = task_data;
4983 GError *error = NULL;
4985 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4987 g_task_return_pointer (task, info, g_object_unref);
4989 g_task_return_error (task, error);
4993 g_file_real_query_info_async (GFile *file,
4994 const char *attributes,
4995 GFileQueryInfoFlags flags,
4997 GCancellable *cancellable,
4998 GAsyncReadyCallback callback,
5002 QueryInfoAsyncData *data;
5004 data = g_new0 (QueryInfoAsyncData, 1);
5005 data->attributes = g_strdup (attributes);
5006 data->flags = flags;
5008 task = g_task_new (file, cancellable, callback, user_data);
5009 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5010 g_task_set_priority (task, io_priority);
5011 g_task_run_in_thread (task, query_info_async_thread);
5012 g_object_unref (task);
5016 g_file_real_query_info_finish (GFile *file,
5020 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5022 return g_task_propagate_pointer (G_TASK (res), error);
5026 query_filesystem_info_async_thread (GTask *task,
5029 GCancellable *cancellable)
5031 const char *attributes = task_data;
5033 GError *error = NULL;
5035 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5037 g_task_return_pointer (task, info, g_object_unref);
5039 g_task_return_error (task, error);
5043 g_file_real_query_filesystem_info_async (GFile *file,
5044 const char *attributes,
5046 GCancellable *cancellable,
5047 GAsyncReadyCallback callback,
5052 task = g_task_new (file, cancellable, callback, user_data);
5053 g_task_set_task_data (task, g_strdup (attributes), g_free);
5054 g_task_set_priority (task, io_priority);
5055 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5056 g_object_unref (task);
5060 g_file_real_query_filesystem_info_finish (GFile *file,
5064 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5066 return g_task_propagate_pointer (G_TASK (res), error);
5070 enumerate_children_async_thread (GTask *task,
5073 GCancellable *cancellable)
5075 QueryInfoAsyncData *data = task_data;
5076 GFileEnumerator *enumerator;
5077 GError *error = NULL;
5079 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5081 g_task_return_error (task, error);
5083 g_task_return_pointer (task, enumerator, g_object_unref);
5087 g_file_real_enumerate_children_async (GFile *file,
5088 const char *attributes,
5089 GFileQueryInfoFlags flags,
5091 GCancellable *cancellable,
5092 GAsyncReadyCallback callback,
5096 QueryInfoAsyncData *data;
5098 data = g_new0 (QueryInfoAsyncData, 1);
5099 data->attributes = g_strdup (attributes);
5100 data->flags = flags;
5102 task = g_task_new (file, cancellable, callback, user_data);
5103 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5104 g_task_set_priority (task, io_priority);
5105 g_task_run_in_thread (task, enumerate_children_async_thread);
5106 g_object_unref (task);
5109 static GFileEnumerator *
5110 g_file_real_enumerate_children_finish (GFile *file,
5114 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5116 return g_task_propagate_pointer (G_TASK (res), error);
5120 open_read_async_thread (GTask *task,
5123 GCancellable *cancellable)
5126 GFileInputStream *stream;
5127 GError *error = NULL;
5129 iface = G_FILE_GET_IFACE (object);
5131 if (iface->read_fn == NULL)
5133 g_task_return_new_error (task, G_IO_ERROR,
5134 G_IO_ERROR_NOT_SUPPORTED,
5135 _("Operation not supported"));
5139 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5141 g_task_return_pointer (task, stream, g_object_unref);
5143 g_task_return_error (task, error);
5147 g_file_real_read_async (GFile *file,
5149 GCancellable *cancellable,
5150 GAsyncReadyCallback callback,
5155 task = g_task_new (file, cancellable, callback, user_data);
5156 g_task_set_priority (task, io_priority);
5157 g_task_run_in_thread (task, open_read_async_thread);
5158 g_object_unref (task);
5161 static GFileInputStream *
5162 g_file_real_read_finish (GFile *file,
5166 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5168 return g_task_propagate_pointer (G_TASK (res), error);
5172 append_to_async_thread (GTask *task,
5173 gpointer source_object,
5175 GCancellable *cancellable)
5178 GFileCreateFlags *data = task_data;
5179 GFileOutputStream *stream;
5180 GError *error = NULL;
5182 iface = G_FILE_GET_IFACE (source_object);
5184 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5186 g_task_return_pointer (task, stream, g_object_unref);
5188 g_task_return_error (task, error);
5192 g_file_real_append_to_async (GFile *file,
5193 GFileCreateFlags flags,
5195 GCancellable *cancellable,
5196 GAsyncReadyCallback callback,
5199 GFileCreateFlags *data;
5202 data = g_new0 (GFileCreateFlags, 1);
5205 task = g_task_new (file, cancellable, callback, user_data);
5206 g_task_set_task_data (task, data, g_free);
5207 g_task_set_priority (task, io_priority);
5209 g_task_run_in_thread (task, append_to_async_thread);
5210 g_object_unref (task);
5213 static GFileOutputStream *
5214 g_file_real_append_to_finish (GFile *file,
5218 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5220 return g_task_propagate_pointer (G_TASK (res), error);
5224 create_async_thread (GTask *task,
5225 gpointer source_object,
5227 GCancellable *cancellable)
5230 GFileCreateFlags *data = task_data;
5231 GFileOutputStream *stream;
5232 GError *error = NULL;
5234 iface = G_FILE_GET_IFACE (source_object);
5236 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5238 g_task_return_pointer (task, stream, g_object_unref);
5240 g_task_return_error (task, error);
5244 g_file_real_create_async (GFile *file,
5245 GFileCreateFlags flags,
5247 GCancellable *cancellable,
5248 GAsyncReadyCallback callback,
5251 GFileCreateFlags *data;
5254 data = g_new0 (GFileCreateFlags, 1);
5257 task = g_task_new (file, cancellable, callback, user_data);
5258 g_task_set_task_data (task, data, g_free);
5259 g_task_set_priority (task, io_priority);
5261 g_task_run_in_thread (task, create_async_thread);
5262 g_object_unref (task);
5265 static GFileOutputStream *
5266 g_file_real_create_finish (GFile *file,
5270 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5272 return g_task_propagate_pointer (G_TASK (res), error);
5276 GFileOutputStream *stream;
5278 gboolean make_backup;
5279 GFileCreateFlags flags;
5283 replace_async_data_free (ReplaceAsyncData *data)
5286 g_object_unref (data->stream);
5287 g_free (data->etag);
5292 replace_async_thread (GTask *task,
5293 gpointer source_object,
5295 GCancellable *cancellable)
5298 GFileOutputStream *stream;
5299 ReplaceAsyncData *data = task_data;
5300 GError *error = NULL;
5302 iface = G_FILE_GET_IFACE (source_object);
5304 stream = iface->replace (G_FILE (source_object),
5312 g_task_return_pointer (task, stream, g_object_unref);
5314 g_task_return_error (task, error);
5318 g_file_real_replace_async (GFile *file,
5320 gboolean make_backup,
5321 GFileCreateFlags flags,
5323 GCancellable *cancellable,
5324 GAsyncReadyCallback callback,
5328 ReplaceAsyncData *data;
5330 data = g_new0 (ReplaceAsyncData, 1);
5331 data->etag = g_strdup (etag);
5332 data->make_backup = make_backup;
5333 data->flags = flags;
5335 task = g_task_new (file, cancellable, callback, user_data);
5336 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5337 g_task_set_priority (task, io_priority);
5339 g_task_run_in_thread (task, replace_async_thread);
5340 g_object_unref (task);
5343 static GFileOutputStream *
5344 g_file_real_replace_finish (GFile *file,
5348 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5350 return g_task_propagate_pointer (G_TASK (res), error);
5354 delete_async_thread (GTask *task,
5357 GCancellable *cancellable)
5359 GFile *file = object;
5361 GError *error = NULL;
5363 iface = G_FILE_GET_IFACE (object);
5365 if (iface->delete_file (file,
5368 g_task_return_boolean (task, TRUE);
5370 g_task_return_error (task, error);
5374 g_file_real_delete_async (GFile *file,
5376 GCancellable *cancellable,
5377 GAsyncReadyCallback callback,
5382 task = g_task_new (file, cancellable, callback, user_data);
5383 g_task_set_priority (task, io_priority);
5384 g_task_run_in_thread (task, delete_async_thread);
5385 g_object_unref (task);
5389 g_file_real_delete_finish (GFile *file,
5393 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5395 return g_task_propagate_boolean (G_TASK (res), error);
5399 open_readwrite_async_thread (GTask *task,
5402 GCancellable *cancellable)
5405 GFileIOStream *stream;
5406 GError *error = NULL;
5408 iface = G_FILE_GET_IFACE (object);
5410 if (iface->open_readwrite == NULL)
5412 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5413 _("Operation not supported"));
5417 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5420 g_task_return_error (task, error);
5422 g_task_return_pointer (task, stream, g_object_unref);
5426 g_file_real_open_readwrite_async (GFile *file,
5428 GCancellable *cancellable,
5429 GAsyncReadyCallback callback,
5434 task = g_task_new (file, cancellable, callback, user_data);
5435 g_task_set_priority (task, io_priority);
5437 g_task_run_in_thread (task, open_readwrite_async_thread);
5438 g_object_unref (task);
5441 static GFileIOStream *
5442 g_file_real_open_readwrite_finish (GFile *file,
5446 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5448 return g_task_propagate_pointer (G_TASK (res), error);
5452 create_readwrite_async_thread (GTask *task,
5455 GCancellable *cancellable)
5458 GFileCreateFlags *data = task_data;
5459 GFileIOStream *stream;
5460 GError *error = NULL;
5462 iface = G_FILE_GET_IFACE (object);
5464 if (iface->create_readwrite == NULL)
5466 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5467 _("Operation not supported"));
5471 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5474 g_task_return_error (task, error);
5476 g_task_return_pointer (task, stream, g_object_unref);
5480 g_file_real_create_readwrite_async (GFile *file,
5481 GFileCreateFlags flags,
5483 GCancellable *cancellable,
5484 GAsyncReadyCallback callback,
5487 GFileCreateFlags *data;
5490 data = g_new0 (GFileCreateFlags, 1);
5493 task = g_task_new (file, cancellable, callback, user_data);
5494 g_task_set_task_data (task, data, g_free);
5495 g_task_set_priority (task, io_priority);
5497 g_task_run_in_thread (task, create_readwrite_async_thread);
5498 g_object_unref (task);
5501 static GFileIOStream *
5502 g_file_real_create_readwrite_finish (GFile *file,
5506 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5508 return g_task_propagate_pointer (G_TASK (res), error);
5513 gboolean make_backup;
5514 GFileCreateFlags flags;
5515 } ReplaceRWAsyncData;
5518 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5520 g_free (data->etag);
5525 replace_readwrite_async_thread (GTask *task,
5528 GCancellable *cancellable)
5531 GFileIOStream *stream;
5532 GError *error = NULL;
5533 ReplaceRWAsyncData *data = task_data;
5535 iface = G_FILE_GET_IFACE (object);
5537 stream = iface->replace_readwrite (G_FILE (object),
5545 g_task_return_error (task, error);
5547 g_task_return_pointer (task, stream, g_object_unref);
5551 g_file_real_replace_readwrite_async (GFile *file,
5553 gboolean make_backup,
5554 GFileCreateFlags flags,
5556 GCancellable *cancellable,
5557 GAsyncReadyCallback callback,
5561 ReplaceRWAsyncData *data;
5563 data = g_new0 (ReplaceRWAsyncData, 1);
5564 data->etag = g_strdup (etag);
5565 data->make_backup = make_backup;
5566 data->flags = flags;
5568 task = g_task_new (file, cancellable, callback, user_data);
5569 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5570 g_task_set_priority (task, io_priority);
5572 g_task_run_in_thread (task, replace_readwrite_async_thread);
5573 g_object_unref (task);
5576 static GFileIOStream *
5577 g_file_real_replace_readwrite_finish (GFile *file,
5581 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5583 return g_task_propagate_pointer (G_TASK (res), error);
5587 set_display_name_async_thread (GTask *task,
5590 GCancellable *cancellable)
5592 GError *error = NULL;
5593 char *name = task_data;
5596 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5599 g_task_return_error (task, error);
5601 g_task_return_pointer (task, file, g_object_unref);
5605 g_file_real_set_display_name_async (GFile *file,
5606 const char *display_name,
5608 GCancellable *cancellable,
5609 GAsyncReadyCallback callback,
5614 task = g_task_new (file, cancellable, callback, user_data);
5615 g_task_set_task_data (task, g_strdup (display_name), g_free);
5616 g_task_set_priority (task, io_priority);
5618 g_task_run_in_thread (task, set_display_name_async_thread);
5619 g_object_unref (task);
5623 g_file_real_set_display_name_finish (GFile *file,
5627 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5629 return g_task_propagate_pointer (G_TASK (res), error);
5633 GFileQueryInfoFlags flags;
5640 set_info_data_free (SetInfoAsyncData *data)
5643 g_object_unref (data->info);
5645 g_error_free (data->error);
5650 set_info_async_thread (GTask *task,
5653 GCancellable *cancellable)
5655 SetInfoAsyncData *data = task_data;
5658 data->res = g_file_set_attributes_from_info (G_FILE (object),
5666 g_file_real_set_attributes_async (GFile *file,
5668 GFileQueryInfoFlags flags,
5670 GCancellable *cancellable,
5671 GAsyncReadyCallback callback,
5675 SetInfoAsyncData *data;
5677 data = g_new0 (SetInfoAsyncData, 1);
5678 data->info = g_file_info_dup (info);
5679 data->flags = flags;
5681 task = g_task_new (file, cancellable, callback, user_data);
5682 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5683 g_task_set_priority (task, io_priority);
5685 g_task_run_in_thread (task, set_info_async_thread);
5686 g_object_unref (task);
5690 g_file_real_set_attributes_finish (GFile *file,
5695 SetInfoAsyncData *data;
5697 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5699 data = g_task_get_task_data (G_TASK (res));
5702 *info = g_object_ref (data->info);
5704 if (error != NULL && data->error)
5705 *error = g_error_copy (data->error);
5711 find_enclosing_mount_async_thread (GTask *task,
5714 GCancellable *cancellable)
5716 GError *error = NULL;
5719 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5722 g_task_return_error (task, error);
5724 g_task_return_pointer (task, mount, g_object_unref);
5728 g_file_real_find_enclosing_mount_async (GFile *file,
5730 GCancellable *cancellable,
5731 GAsyncReadyCallback callback,
5736 task = g_task_new (file, cancellable, callback, user_data);
5737 g_task_set_priority (task, io_priority);
5739 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5740 g_object_unref (task);
5744 g_file_real_find_enclosing_mount_finish (GFile *file,
5748 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5750 return g_task_propagate_pointer (G_TASK (res), error);
5757 GFileCopyFlags flags;
5758 GFileProgressCallback progress_cb;
5759 gpointer progress_cb_data;
5763 copy_async_data_free (CopyAsyncData *data)
5765 g_object_unref (data->source);
5766 g_object_unref (data->destination);
5767 g_slice_free (CopyAsyncData, data);
5771 CopyAsyncData *data;
5772 goffset current_num_bytes;
5773 goffset total_num_bytes;
5777 copy_async_progress_in_main (gpointer user_data)
5779 ProgressData *progress = user_data;
5780 CopyAsyncData *data = progress->data;
5782 data->progress_cb (progress->current_num_bytes,
5783 progress->total_num_bytes,
5784 data->progress_cb_data);
5790 copy_async_progress_callback (goffset current_num_bytes,
5791 goffset total_num_bytes,
5794 GTask *task = user_data;
5795 CopyAsyncData *data = g_task_get_task_data (task);
5796 ProgressData *progress;
5798 progress = g_new (ProgressData, 1);
5799 progress->data = data;
5800 progress->current_num_bytes = current_num_bytes;
5801 progress->total_num_bytes = total_num_bytes;
5803 g_main_context_invoke_full (g_task_get_context (task),
5804 g_task_get_priority (task),
5805 copy_async_progress_in_main,
5811 copy_async_thread (GTask *task,
5814 GCancellable *cancellable)
5816 CopyAsyncData *data = task_data;
5818 GError *error = NULL;
5820 result = g_file_copy (data->source,
5824 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5828 g_task_return_boolean (task, TRUE);
5830 g_task_return_error (task, error);
5834 g_file_real_copy_async (GFile *source,
5836 GFileCopyFlags flags,
5838 GCancellable *cancellable,
5839 GFileProgressCallback progress_callback,
5840 gpointer progress_callback_data,
5841 GAsyncReadyCallback callback,
5845 CopyAsyncData *data;
5847 data = g_slice_new (CopyAsyncData);
5848 data->source = g_object_ref (source);
5849 data->destination = g_object_ref (destination);
5850 data->flags = flags;
5851 data->progress_cb = progress_callback;
5852 data->progress_cb_data = progress_callback_data;
5854 task = g_task_new (source, cancellable, callback, user_data);
5855 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5856 g_task_set_priority (task, io_priority);
5857 g_task_run_in_thread (task, copy_async_thread);
5858 g_object_unref (task);
5862 g_file_real_copy_finish (GFile *file,
5866 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5868 return g_task_propagate_boolean (G_TASK (res), error);
5872 /********************************************
5873 * Default VFS operations *
5874 ********************************************/
5877 * g_file_new_for_path:
5878 * @path: a string containing a relative or absolute path.
5879 * The string must be encoded in the glib filename encoding.
5881 * Constructs a #GFile for a given path. This operation never
5882 * fails, but the returned object might not support any I/O
5883 * operation if @path is malformed.
5885 * Returns: (transfer full): a new #GFile for the given @path.
5886 * Free the returned object with g_object_unref().
5889 g_file_new_for_path (const char *path)
5891 g_return_val_if_fail (path != NULL, NULL);
5893 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5897 * g_file_new_for_uri:
5898 * @uri: a UTF-8 string containing a URI
5900 * Constructs a #GFile for a given URI. This operation never
5901 * fails, but the returned object might not support any I/O
5902 * operation if @uri is malformed or if the uri type is
5905 * Returns: (transfer full): a new #GFile for the given @uri.
5906 * Free the returned object with g_object_unref().
5909 g_file_new_for_uri (const char *uri)
5911 g_return_val_if_fail (uri != NULL, NULL);
5913 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5918 * @tmpl: (type filename) (allow-none): Template for the file
5919 * name, as in g_file_open_tmp(), or %NULL for a default template
5920 * @iostream: (out): on return, a #GFileIOStream for the created file
5921 * @error: a #GError, or %NULL
5923 * Opens a file in the preferred directory for temporary files (as
5924 * returned by g_get_tmp_dir()) and returns a #GFile and
5925 * #GFileIOStream pointing to it.
5927 * @tmpl should be a string in the GLib file name encoding
5928 * containing a sequence of six 'X' characters, and containing no
5929 * directory components. If it is %NULL, a default template is used.
5931 * Unlike the other #GFile constructors, this will return %NULL if
5932 * a temporary file could not be created.
5934 * Returns: (transfer full): a new #GFile.
5935 * Free the returned object with g_object_unref().
5940 g_file_new_tmp (const char *tmpl,
5941 GFileIOStream **iostream,
5947 GFileOutputStream *output;
5949 g_return_val_if_fail (iostream != NULL, NULL);
5951 fd = g_file_open_tmp (tmpl, &path, error);
5955 file = g_file_new_for_path (path);
5957 output = _g_local_file_output_stream_new (fd);
5958 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5960 g_object_unref (output);
5967 * g_file_parse_name:
5968 * @parse_name: a file name or path to be parsed
5970 * Constructs a #GFile with the given @parse_name (i.e. something
5971 * given by g_file_get_parse_name()). This operation never fails,
5972 * but the returned object might not support any I/O operation if
5973 * the @parse_name cannot be parsed.
5975 * Returns: (transfer full): a new #GFile.
5978 g_file_parse_name (const char *parse_name)
5980 g_return_val_if_fail (parse_name != NULL, NULL);
5982 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5986 is_valid_scheme_character (char c)
5988 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5991 /* Following RFC 2396, valid schemes are built like:
5992 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5995 has_valid_scheme (const char *uri)
6001 if (!g_ascii_isalpha (*p))
6006 } while (is_valid_scheme_character (*p));
6012 new_for_cmdline_arg (const gchar *arg,
6018 if (g_path_is_absolute (arg))
6019 return g_file_new_for_path (arg);
6021 if (has_valid_scheme (arg))
6022 return g_file_new_for_uri (arg);
6028 current_dir = g_get_current_dir ();
6029 filename = g_build_filename (current_dir, arg, NULL);
6030 g_free (current_dir);
6033 filename = g_build_filename (cwd, arg, NULL);
6035 file = g_file_new_for_path (filename);
6042 * g_file_new_for_commandline_arg:
6043 * @arg: a command line string
6045 * Creates a #GFile with the given argument from the command line.
6046 * The value of @arg can be either a URI, an absolute path or a
6047 * relative path resolved relative to the current working directory.
6048 * This operation never fails, but the returned object might not
6049 * support any I/O operation if @arg points to a malformed path.
6051 * Returns: (transfer full): a new #GFile.
6052 * Free the returned object with g_object_unref().
6055 g_file_new_for_commandline_arg (const char *arg)
6057 g_return_val_if_fail (arg != NULL, NULL);
6059 return new_for_cmdline_arg (arg, NULL);
6063 * g_file_new_for_commandline_arg_and_cwd:
6064 * @arg: a command line string
6065 * @cwd: the current working directory of the commandline
6067 * Creates a #GFile with the given argument from the command line.
6069 * This function is similar to g_file_new_for_commandline_arg() except
6070 * that it allows for passing the current working directory as an
6071 * argument instead of using the current working directory of the
6074 * This is useful if the commandline argument was given in a context
6075 * other than the invocation of the current process.
6077 * See also g_application_command_line_create_file_for_arg().
6079 * Returns: (transfer full): a new #GFile
6084 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6087 g_return_val_if_fail (arg != NULL, NULL);
6088 g_return_val_if_fail (cwd != NULL, NULL);
6090 return new_for_cmdline_arg (arg, cwd);
6094 * g_file_mount_enclosing_volume:
6095 * @location: input #GFile
6096 * @flags: flags affecting the operation
6097 * @mount_operation: (allow-none): a #GMountOperation
6098 * or %NULL to avoid user interaction
6099 * @cancellable: (allow-none): optional #GCancellable object,
6101 * @callback: (allow-none): a #GAsyncReadyCallback to call
6102 * when the request is satisfied, or %NULL
6103 * @user_data: the data to pass to callback function
6105 * Starts a @mount_operation, mounting the volume that contains
6106 * the file @location.
6108 * When this operation has completed, @callback will be called with
6109 * @user_user data, and the operation can be finalized with
6110 * g_file_mount_enclosing_volume_finish().
6112 * If @cancellable is not %NULL, then the operation can be cancelled by
6113 * triggering the cancellable object from another thread. If the operation
6114 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6117 g_file_mount_enclosing_volume (GFile *location,
6118 GMountMountFlags flags,
6119 GMountOperation *mount_operation,
6120 GCancellable *cancellable,
6121 GAsyncReadyCallback callback,
6126 g_return_if_fail (G_IS_FILE (location));
6128 iface = G_FILE_GET_IFACE (location);
6130 if (iface->mount_enclosing_volume == NULL)
6132 g_task_report_new_error (location, callback, user_data,
6133 g_file_mount_enclosing_volume,
6134 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6135 _("volume doesn't implement mount"));
6139 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6144 * g_file_mount_enclosing_volume_finish:
6145 * @location: input #GFile
6146 * @result: a #GAsyncResult
6147 * @error: a #GError, or %NULL
6149 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6151 * Returns: %TRUE if successful. If an error has occurred,
6152 * this function will return %FALSE and set @error
6153 * appropriately if present.
6156 g_file_mount_enclosing_volume_finish (GFile *location,
6157 GAsyncResult *result,
6162 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6163 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6165 if (g_async_result_legacy_propagate_error (result, error))
6167 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6168 return g_task_propagate_boolean (G_TASK (result), error);
6170 iface = G_FILE_GET_IFACE (location);
6172 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6175 /********************************************
6176 * Utility functions *
6177 ********************************************/
6180 * g_file_query_default_handler:
6181 * @file: a #GFile to open
6182 * @cancellable: optional #GCancellable object, %NULL to ignore
6183 * @error: a #GError, or %NULL
6185 * Returns the #GAppInfo that is registered as the default
6186 * application to handle the file specified by @file.
6188 * If @cancellable is not %NULL, then the operation can be cancelled by
6189 * triggering the cancellable object from another thread. If the operation
6190 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6192 * Returns: (transfer full): a #GAppInfo if the handle was found,
6193 * %NULL if there were errors.
6194 * When you are done with it, release it with g_object_unref()
6197 g_file_query_default_handler (GFile *file,
6198 GCancellable *cancellable,
6202 const char *content_type;
6207 uri_scheme = g_file_get_uri_scheme (file);
6208 if (uri_scheme && uri_scheme[0] != '\0')
6210 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6211 g_free (uri_scheme);
6213 if (appinfo != NULL)
6217 info = g_file_query_info (file,
6218 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6227 content_type = g_file_info_get_content_type (info);
6230 /* Don't use is_native(), as we want to support fuse paths if available */
6231 path = g_file_get_path (file);
6232 appinfo = g_app_info_get_default_for_type (content_type,
6237 g_object_unref (info);
6239 if (appinfo != NULL)
6242 g_set_error_literal (error, G_IO_ERROR,
6243 G_IO_ERROR_NOT_SUPPORTED,
6244 _("No application is registered as handling this file"));
6248 #define GET_CONTENT_BLOCK_SIZE 8192
6251 * g_file_load_contents:
6252 * @file: input #GFile
6253 * @cancellable: optional #GCancellable object, %NULL to ignore
6254 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6255 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6256 * or %NULL if the length is not needed
6257 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6258 * or %NULL if the entity tag is not needed
6259 * @error: a #GError, or %NULL
6261 * Loads the content of the file into memory. The data is always
6262 * zero-terminated, but this is not included in the resultant @length.
6263 * The returned @content should be freed with g_free() when no longer
6266 * If @cancellable is not %NULL, then the operation can be cancelled by
6267 * triggering the cancellable object from another thread. If the operation
6268 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6270 * Returns: %TRUE if the @file's contents were successfully loaded.
6271 * %FALSE if there were errors.
6274 g_file_load_contents (GFile *file,
6275 GCancellable *cancellable,
6281 GFileInputStream *in;
6282 GByteArray *content;
6287 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6288 g_return_val_if_fail (contents != NULL, FALSE);
6290 in = g_file_read (file, cancellable, error);
6294 content = g_byte_array_new ();
6297 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6298 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6299 content->data + pos,
6300 GET_CONTENT_BLOCK_SIZE,
6301 cancellable, error)) > 0)
6304 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6311 info = g_file_input_stream_query_info (in,
6312 G_FILE_ATTRIBUTE_ETAG_VALUE,
6317 *etag_out = g_strdup (g_file_info_get_etag (info));
6318 g_object_unref (info);
6322 /* Ignore errors on close */
6323 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6324 g_object_unref (in);
6328 /* error is set already */
6329 g_byte_array_free (content, TRUE);
6336 /* Zero terminate (we got an extra byte allocated for this */
6337 content->data[pos] = 0;
6339 *contents = (char *)g_byte_array_free (content, FALSE);
6346 GFileReadMoreCallback read_more_callback;
6347 GByteArray *content;
6354 load_contents_data_free (LoadContentsData *data)
6357 g_byte_array_free (data->content, TRUE);
6358 g_free (data->etag);
6363 load_contents_close_callback (GObject *obj,
6364 GAsyncResult *close_res,
6367 GInputStream *stream = G_INPUT_STREAM (obj);
6368 LoadContentsData *data = user_data;
6370 /* Ignore errors here, we're only reading anyway */
6371 g_input_stream_close_finish (stream, close_res, NULL);
6372 g_object_unref (stream);
6374 g_task_return_boolean (data->task, TRUE);
6375 g_object_unref (data->task);
6379 load_contents_fstat_callback (GObject *obj,
6380 GAsyncResult *stat_res,
6383 GInputStream *stream = G_INPUT_STREAM (obj);
6384 LoadContentsData *data = user_data;
6387 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6391 data->etag = g_strdup (g_file_info_get_etag (info));
6392 g_object_unref (info);
6395 g_input_stream_close_async (stream, 0,
6396 g_task_get_cancellable (data->task),
6397 load_contents_close_callback, data);
6401 load_contents_read_callback (GObject *obj,
6402 GAsyncResult *read_res,
6405 GInputStream *stream = G_INPUT_STREAM (obj);
6406 LoadContentsData *data = user_data;
6407 GError *error = NULL;
6410 read_size = g_input_stream_read_finish (stream, read_res, &error);
6414 /* EOF, close the file */
6415 g_task_return_error (data->task, error);
6416 g_input_stream_close_async (stream, 0,
6417 g_task_get_cancellable (data->task),
6418 load_contents_close_callback, data);
6420 else if (read_size == 0)
6422 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6423 G_FILE_ATTRIBUTE_ETAG_VALUE,
6425 g_task_get_cancellable (data->task),
6426 load_contents_fstat_callback,
6429 else if (read_size > 0)
6431 data->pos += read_size;
6433 g_byte_array_set_size (data->content,
6434 data->pos + GET_CONTENT_BLOCK_SIZE);
6437 if (data->read_more_callback &&
6438 !data->read_more_callback ((char *)data->content->data, data->pos,
6439 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6440 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6441 G_FILE_ATTRIBUTE_ETAG_VALUE,
6443 g_task_get_cancellable (data->task),
6444 load_contents_fstat_callback,
6447 g_input_stream_read_async (stream,
6448 data->content->data + data->pos,
6449 GET_CONTENT_BLOCK_SIZE,
6451 g_task_get_cancellable (data->task),
6452 load_contents_read_callback,
6458 load_contents_open_callback (GObject *obj,
6459 GAsyncResult *open_res,
6462 GFile *file = G_FILE (obj);
6463 GFileInputStream *stream;
6464 LoadContentsData *data = user_data;
6465 GError *error = NULL;
6467 stream = g_file_read_finish (file, open_res, &error);
6471 g_byte_array_set_size (data->content,
6472 data->pos + GET_CONTENT_BLOCK_SIZE);
6473 g_input_stream_read_async (G_INPUT_STREAM (stream),
6474 data->content->data + data->pos,
6475 GET_CONTENT_BLOCK_SIZE,
6477 g_task_get_cancellable (data->task),
6478 load_contents_read_callback,
6483 g_task_return_error (data->task, error);
6484 g_object_unref (data->task);
6489 * g_file_load_partial_contents_async: (skip)
6490 * @file: input #GFile
6491 * @cancellable: optional #GCancellable object, %NULL to ignore
6492 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6493 * and to specify whether further data should be read
6494 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6495 * @user_data: the data to pass to the callback functions
6497 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6498 * be used to stop reading from the file when appropriate, else this
6499 * function will behave exactly as g_file_load_contents_async(). This
6500 * operation can be finished by g_file_load_partial_contents_finish().
6502 * Users of this function should be aware that @user_data is passed to
6503 * both the @read_more_callback and the @callback.
6505 * If @cancellable is not %NULL, then the operation can be cancelled by
6506 * triggering the cancellable object from another thread. If the operation
6507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6510 g_file_load_partial_contents_async (GFile *file,
6511 GCancellable *cancellable,
6512 GFileReadMoreCallback read_more_callback,
6513 GAsyncReadyCallback callback,
6516 LoadContentsData *data;
6518 g_return_if_fail (G_IS_FILE (file));
6520 data = g_new0 (LoadContentsData, 1);
6521 data->read_more_callback = read_more_callback;
6522 data->content = g_byte_array_new ();
6524 data->task = g_task_new (file, cancellable, callback, user_data);
6525 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6527 g_file_read_async (file,
6529 g_task_get_cancellable (data->task),
6530 load_contents_open_callback,
6535 * g_file_load_partial_contents_finish:
6536 * @file: input #GFile
6537 * @res: a #GAsyncResult
6538 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6539 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6540 * or %NULL if the length is not needed
6541 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6542 * or %NULL if the entity tag is not needed
6543 * @error: a #GError, or %NULL
6545 * Finishes an asynchronous partial load operation that was started
6546 * with g_file_load_partial_contents_async(). The data is always
6547 * zero-terminated, but this is not included in the resultant @length.
6548 * The returned @content should be freed with g_free() when no longer
6551 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6552 * present, it will be set appropriately.
6555 g_file_load_partial_contents_finish (GFile *file,
6563 LoadContentsData *data;
6565 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6566 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6567 g_return_val_if_fail (contents != NULL, FALSE);
6569 task = G_TASK (res);
6571 if (!g_task_propagate_boolean (task, error))
6578 data = g_task_get_task_data (task);
6581 *length = data->pos;
6585 *etag_out = data->etag;
6589 /* Zero terminate */
6590 g_byte_array_set_size (data->content, data->pos + 1);
6591 data->content->data[data->pos] = 0;
6593 *contents = (char *)g_byte_array_free (data->content, FALSE);
6594 data->content = NULL;
6600 * g_file_load_contents_async:
6601 * @file: input #GFile
6602 * @cancellable: optional #GCancellable object, %NULL to ignore
6603 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6604 * @user_data: the data to pass to callback function
6606 * Starts an asynchronous load of the @file's contents.
6608 * For more details, see g_file_load_contents() which is
6609 * the synchronous version of this call.
6611 * When the load operation has completed, @callback will be called
6612 * with @user data. To finish the operation, call
6613 * g_file_load_contents_finish() with the #GAsyncResult returned by
6616 * If @cancellable is not %NULL, then the operation can be cancelled by
6617 * triggering the cancellable object from another thread. If the operation
6618 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6621 g_file_load_contents_async (GFile *file,
6622 GCancellable *cancellable,
6623 GAsyncReadyCallback callback,
6626 g_file_load_partial_contents_async (file,
6629 callback, user_data);
6633 * g_file_load_contents_finish:
6634 * @file: input #GFile
6635 * @res: a #GAsyncResult
6636 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6637 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6638 * or %NULL if the length is not needed
6639 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6640 * or %NULL if the entity tag is not needed
6641 * @error: a #GError, or %NULL
6643 * Finishes an asynchronous load of the @file's contents.
6644 * The contents are placed in @contents, and @length is set to the
6645 * size of the @contents string. The @content should be freed with
6646 * g_free() when no longer needed. If @etag_out is present, it will be
6647 * set to the new entity tag for the @file.
6649 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6650 * present, it will be set appropriately.
6653 g_file_load_contents_finish (GFile *file,
6660 return g_file_load_partial_contents_finish (file,
6669 * g_file_replace_contents:
6670 * @file: input #GFile
6671 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6672 * @length: the length of @contents in bytes
6673 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6674 * for the document, or %NULL
6675 * @make_backup: %TRUE if a backup should be created
6676 * @flags: a set of #GFileCreateFlags
6677 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6678 * for the document. This should be freed with g_free() when no longer
6680 * @cancellable: optional #GCancellable object, %NULL to ignore
6681 * @error: a #GError, or %NULL
6683 * Replaces the contents of @file with @contents of @length bytes.
6685 * If @etag is specified (not %NULL), any existing file must have that etag,
6686 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6688 * If @make_backup is %TRUE, this function will attempt to make a backup
6691 * If @cancellable is not %NULL, then the operation can be cancelled by
6692 * triggering the cancellable object from another thread. If the operation
6693 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6695 * The returned @new_etag can be used to verify that the file hasn't
6696 * changed the next time it is saved over.
6698 * Returns: %TRUE if successful. If an error has occurred, this function
6699 * will return %FALSE and set @error appropriately if present.
6702 g_file_replace_contents (GFile *file,
6703 const char *contents,
6706 gboolean make_backup,
6707 GFileCreateFlags flags,
6709 GCancellable *cancellable,
6712 GFileOutputStream *out;
6713 gsize pos, remainder;
6717 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6718 g_return_val_if_fail (contents != NULL, FALSE);
6720 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6726 while (remainder > 0 &&
6727 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6729 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6737 if (remainder > 0 && res < 0)
6739 /* Ignore errors on close */
6740 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6741 g_object_unref (out);
6743 /* error is set already */
6747 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6750 *new_etag = g_file_output_stream_get_etag (out);
6752 g_object_unref (out);
6759 const char *content;
6764 } ReplaceContentsData;
6767 replace_contents_data_free (ReplaceContentsData *data)
6769 g_free (data->etag);
6774 replace_contents_close_callback (GObject *obj,
6775 GAsyncResult *close_res,
6778 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6779 ReplaceContentsData *data = user_data;
6781 /* Ignore errors here, we're only reading anyway */
6782 g_output_stream_close_finish (stream, close_res, NULL);
6783 g_object_unref (stream);
6787 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6788 g_task_return_boolean (data->task, TRUE);
6790 g_object_unref (data->task);
6794 replace_contents_write_callback (GObject *obj,
6795 GAsyncResult *read_res,
6798 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6799 ReplaceContentsData *data = user_data;
6800 GError *error = NULL;
6803 write_size = g_output_stream_write_finish (stream, read_res, &error);
6805 if (write_size <= 0)
6807 /* Error or EOF, close the file */
6810 data->failed = TRUE;
6811 g_task_return_error (data->task, error);
6813 g_output_stream_close_async (stream, 0,
6814 g_task_get_cancellable (data->task),
6815 replace_contents_close_callback, data);
6817 else if (write_size > 0)
6819 data->pos += write_size;
6821 if (data->pos >= data->length)
6822 g_output_stream_close_async (stream, 0,
6823 g_task_get_cancellable (data->task),
6824 replace_contents_close_callback, data);
6826 g_output_stream_write_async (stream,
6827 data->content + data->pos,
6828 data->length - data->pos,
6830 g_task_get_cancellable (data->task),
6831 replace_contents_write_callback,
6837 replace_contents_open_callback (GObject *obj,
6838 GAsyncResult *open_res,
6841 GFile *file = G_FILE (obj);
6842 GFileOutputStream *stream;
6843 ReplaceContentsData *data = user_data;
6844 GError *error = NULL;
6846 stream = g_file_replace_finish (file, open_res, &error);
6850 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6851 data->content + data->pos,
6852 data->length - data->pos,
6854 g_task_get_cancellable (data->task),
6855 replace_contents_write_callback,
6860 g_task_return_error (data->task, error);
6861 g_object_unref (data->task);
6866 * g_file_replace_contents_async:
6867 * @file: input #GFile
6868 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6869 * @length: the length of @contents in bytes
6870 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6871 * @make_backup: %TRUE if a backup should be created
6872 * @flags: a set of #GFileCreateFlags
6873 * @cancellable: optional #GCancellable object, %NULL to ignore
6874 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6875 * @user_data: the data to pass to callback function
6877 * Starts an asynchronous replacement of @file with the given
6878 * @contents of @length bytes. @etag will replace the document's
6879 * current entity tag.
6881 * When this operation has completed, @callback will be called with
6882 * @user_user data, and the operation can be finalized with
6883 * g_file_replace_contents_finish().
6885 * If @cancellable is not %NULL, then the operation can be cancelled by
6886 * triggering the cancellable object from another thread. If the operation
6887 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6889 * If @make_backup is %TRUE, this function will attempt to
6890 * make a backup of @file.
6893 g_file_replace_contents_async (GFile *file,
6894 const char *contents,
6897 gboolean make_backup,
6898 GFileCreateFlags flags,
6899 GCancellable *cancellable,
6900 GAsyncReadyCallback callback,
6903 ReplaceContentsData *data;
6905 g_return_if_fail (G_IS_FILE (file));
6906 g_return_if_fail (contents != NULL);
6908 data = g_new0 (ReplaceContentsData, 1);
6910 data->content = contents;
6911 data->length = length;
6913 data->task = g_task_new (file, cancellable, callback, user_data);
6914 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
6916 g_file_replace_async (file,
6921 g_task_get_cancellable (data->task),
6922 replace_contents_open_callback,
6927 * g_file_replace_contents_finish:
6928 * @file: input #GFile
6929 * @res: a #GAsyncResult
6930 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6931 * for the document. This should be freed with g_free() when it is no
6932 * longer needed, or %NULL
6933 * @error: a #GError, or %NULL
6935 * Finishes an asynchronous replace of the given @file. See
6936 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6937 * tag for the document, if present.
6939 * Returns: %TRUE on success, %FALSE on failure.
6942 g_file_replace_contents_finish (GFile *file,
6948 ReplaceContentsData *data;
6950 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6951 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6953 task = G_TASK (res);
6955 if (!g_task_propagate_boolean (task, error))
6958 data = g_task_get_task_data (task);
6962 *new_etag = data->etag;
6963 data->etag = NULL; /* Take ownership */
6970 * g_file_start_mountable:
6971 * @file: input #GFile
6972 * @flags: flags affecting the operation
6973 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
6974 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
6975 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
6976 * @user_data: the data to pass to callback function
6978 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
6979 * Using @start_operation, you can request callbacks when, for instance,
6980 * passwords are needed during authentication.
6982 * If @cancellable is not %NULL, then the operation can be cancelled by
6983 * triggering the cancellable object from another thread. If the operation
6984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6986 * When the operation is finished, @callback will be called.
6987 * You can then call g_file_mount_mountable_finish() to get
6988 * the result of the operation.
6993 g_file_start_mountable (GFile *file,
6994 GDriveStartFlags flags,
6995 GMountOperation *start_operation,
6996 GCancellable *cancellable,
6997 GAsyncReadyCallback callback,
7002 g_return_if_fail (G_IS_FILE (file));
7004 iface = G_FILE_GET_IFACE (file);
7006 if (iface->start_mountable == NULL)
7008 g_task_report_new_error (file, callback, user_data,
7009 g_file_start_mountable,
7010 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7011 _("Operation not supported"));
7015 (* iface->start_mountable) (file,
7024 * g_file_start_mountable_finish:
7025 * @file: input #GFile
7026 * @result: a #GAsyncResult
7027 * @error: a #GError, or %NULL
7029 * Finishes a start operation. See g_file_start_mountable() for details.
7031 * Finish an asynchronous start operation that was started
7032 * with g_file_start_mountable().
7034 * Returns: %TRUE if the operation finished successfully. %FALSE
7040 g_file_start_mountable_finish (GFile *file,
7041 GAsyncResult *result,
7046 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7047 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7049 if (g_async_result_legacy_propagate_error (result, error))
7051 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7052 return g_task_propagate_boolean (G_TASK (result), error);
7054 iface = G_FILE_GET_IFACE (file);
7055 return (* iface->start_mountable_finish) (file, result, error);
7059 * g_file_stop_mountable:
7060 * @file: input #GFile
7061 * @flags: flags affecting the operation
7062 * @mount_operation: (allow-none): a #GMountOperation,
7063 * or %NULL to avoid user interaction.
7064 * @cancellable: (allow-none): optional #GCancellable object,
7066 * @callback: (allow-none): a #GAsyncReadyCallback to call
7067 * when the request is satisfied, or %NULL
7068 * @user_data: the data to pass to callback function
7070 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7072 * If @cancellable is not %NULL, then the operation can be cancelled by
7073 * triggering the cancellable object from another thread. If the operation
7074 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7076 * When the operation is finished, @callback will be called.
7077 * You can then call g_file_stop_mountable_finish() to get
7078 * the result of the operation.
7083 g_file_stop_mountable (GFile *file,
7084 GMountUnmountFlags flags,
7085 GMountOperation *mount_operation,
7086 GCancellable *cancellable,
7087 GAsyncReadyCallback callback,
7092 g_return_if_fail (G_IS_FILE (file));
7094 iface = G_FILE_GET_IFACE (file);
7096 if (iface->stop_mountable == NULL)
7098 g_task_report_new_error (file, callback, user_data,
7099 g_file_stop_mountable,
7100 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7101 _("Operation not supported"));
7105 (* iface->stop_mountable) (file,
7114 * g_file_stop_mountable_finish:
7115 * @file: input #GFile
7116 * @result: a #GAsyncResult
7117 * @error: a #GError, or %NULL
7119 * Finishes an stop operation, see g_file_stop_mountable() for details.
7121 * Finish an asynchronous stop operation that was started
7122 * with g_file_stop_mountable().
7124 * Returns: %TRUE if the operation finished successfully.
7130 g_file_stop_mountable_finish (GFile *file,
7131 GAsyncResult *result,
7136 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7137 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7139 if (g_async_result_legacy_propagate_error (result, error))
7141 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7142 return g_task_propagate_boolean (G_TASK (result), error);
7144 iface = G_FILE_GET_IFACE (file);
7145 return (* iface->stop_mountable_finish) (file, result, error);
7149 * g_file_poll_mountable:
7150 * @file: input #GFile
7151 * @cancellable: optional #GCancellable object, %NULL to ignore
7152 * @callback: (allow-none): a #GAsyncReadyCallback to call
7153 * when the request is satisfied, or %NULL
7154 * @user_data: the data to pass to callback function
7156 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7158 * If @cancellable is not %NULL, then the operation can be cancelled by
7159 * triggering the cancellable object from another thread. If the operation
7160 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7162 * When the operation is finished, @callback will be called.
7163 * You can then call g_file_mount_mountable_finish() to get
7164 * the result of the operation.
7169 g_file_poll_mountable (GFile *file,
7170 GCancellable *cancellable,
7171 GAsyncReadyCallback callback,
7176 g_return_if_fail (G_IS_FILE (file));
7178 iface = G_FILE_GET_IFACE (file);
7180 if (iface->poll_mountable == NULL)
7182 g_task_report_new_error (file, callback, user_data,
7183 g_file_poll_mountable,
7184 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7185 _("Operation not supported"));
7189 (* iface->poll_mountable) (file,
7196 * g_file_poll_mountable_finish:
7197 * @file: input #GFile
7198 * @result: a #GAsyncResult
7199 * @error: a #GError, or %NULL
7201 * Finishes a poll operation. See g_file_poll_mountable() for details.
7203 * Finish an asynchronous poll operation that was polled
7204 * with g_file_poll_mountable().
7206 * Returns: %TRUE if the operation finished successfully. %FALSE
7212 g_file_poll_mountable_finish (GFile *file,
7213 GAsyncResult *result,
7218 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7219 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7221 if (g_async_result_legacy_propagate_error (result, error))
7223 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7224 return g_task_propagate_boolean (G_TASK (result), error);
7226 iface = G_FILE_GET_IFACE (file);
7227 return (* iface->poll_mountable_finish) (file, result, error);
7231 * g_file_supports_thread_contexts:
7234 * Checks if @file supports <link
7235 * linkend="g-main-context-push-thread-default-context">thread-default
7236 * contexts</link>. If this returns %FALSE, you cannot perform
7237 * asynchronous operations on @file in a thread that has a
7238 * thread-default context.
7240 * Returns: Whether or not @file supports thread-default contexts.
7245 g_file_supports_thread_contexts (GFile *file)
7249 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7251 iface = G_FILE_GET_IFACE (file);
7252 return iface->supports_thread_contexts;