1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <sys/ioctl.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
43 #include <sys/types.h>
46 #include "glib/gstdio.h"
48 #include "glib-unix.h"
52 #include "gfileattribute-priv.h"
53 #include "gfiledescriptorbased.h"
54 #include "gpollfilemonitor.h"
56 #include "gfileinputstream.h"
57 #include "gfileoutputstream.h"
58 #include "glocalfileoutputstream.h"
59 #include "glocalfileiostream.h"
60 #include "glocalfile.h"
61 #include "gcancellable.h"
62 #include "gasyncresult.h"
69 * @short_description: File and Directory Handling
71 * @see_also: #GFileInfo, #GFileEnumerator
73 * #GFile is a high level abstraction for manipulating files on a
74 * virtual file system. #GFiles are lightweight, immutable objects
75 * that do no I/O upon creation. It is necessary to understand that
76 * #GFile objects do not represent files, merely an identifier for a
77 * file. All file content I/O is implemented as streaming operations
78 * (see #GInputStream and #GOutputStream).
80 * To construct a #GFile, you can use:
82 * <member>g_file_new_for_path() if you have a path.</member>
83 * <member>g_file_new_for_uri() if you have a URI.</member>
84 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
85 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
86 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
89 * One way to think of a #GFile is as an abstraction of a pathname. For
90 * normal files the system pathname is what is stored internally, but as
91 * #GFiles are extensible it could also be something else that corresponds
92 * to a pathname in a userspace implementation of a filesystem.
94 * #GFiles make up hierarchies of directories and files that correspond to
95 * the files on a filesystem. You can move through the file system with
96 * #GFile using g_file_get_parent() to get an identifier for the parent
97 * directory, g_file_get_child() to get a child within a directory,
98 * g_file_resolve_relative_path() to resolve a relative path between two
99 * #GFiles. There can be multiple hierarchies, so you may not end up at
100 * the same root if you repeatedly call g_file_get_parent() on two different
103 * All #GFiles have a basename (get with g_file_get_basename()). These names
104 * are byte strings that are used to identify the file on the filesystem
105 * (relative to its parent directory) and there is no guarantees that they
106 * have any particular charset encoding or even make any sense at all. If
107 * you want to use filenames in a user interface you should use the display
108 * name that you can get by requesting the
109 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
110 * This is guaranteed to be in UTF-8 and can be used in a user interface.
111 * But always store the real basename or the #GFile to use to actually
112 * access the file, because there is no way to go from a display name to
115 * Using #GFile as an identifier has the same weaknesses as using a path
116 * in that there may be multiple aliases for the same file. For instance,
117 * hard or soft links may cause two different #GFiles to refer to the same
118 * file. Other possible causes for aliases are: case insensitive filesystems,
119 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
120 * check if two #GFiles point to the same file you can query for the
121 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
122 * canonicalization of pathnames passed in, so that trivial differences in
123 * the path string used at creation (duplicated slashes, slash at end of
124 * path, "." or ".." path segments, etc) does not create different #GFiles.
126 * Many #GFile operations have both synchronous and asynchronous versions
127 * to suit your application. Asynchronous versions of synchronous functions
128 * simply have _async() appended to their function names. The asynchronous
129 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
130 * the operation, producing a GAsyncResult which is then passed to the
131 * function's matching _finish() operation.
133 * Some #GFile operations do not have synchronous analogs, as they may
134 * take a very long time to finish, and blocking may leave an application
135 * unusable. Notable cases include:
137 * <member>g_file_mount_mountable() to mount a mountable file.</member>
138 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
139 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
142 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
143 * One notable feature of #GFiles are entity tags, or "etags" for
144 * short. Entity tags are somewhat like a more abstract version of the
145 * traditional mtime, and can be used to quickly determine if the file has
146 * been modified from the version on the file system. See the HTTP 1.1
147 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
148 * for HTTP Etag headers, which are a very similar concept.
152 static void g_file_real_query_info_async (GFile *file,
153 const char *attributes,
154 GFileQueryInfoFlags flags,
156 GCancellable *cancellable,
157 GAsyncReadyCallback callback,
159 static GFileInfo * g_file_real_query_info_finish (GFile *file,
162 static void g_file_real_query_filesystem_info_async (GFile *file,
163 const char *attributes,
165 GCancellable *cancellable,
166 GAsyncReadyCallback callback,
168 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
171 static void g_file_real_enumerate_children_async (GFile *file,
172 const char *attributes,
173 GFileQueryInfoFlags flags,
175 GCancellable *cancellable,
176 GAsyncReadyCallback callback,
178 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
181 static void g_file_real_read_async (GFile *file,
183 GCancellable *cancellable,
184 GAsyncReadyCallback callback,
186 static GFileInputStream * g_file_real_read_finish (GFile *file,
189 static void g_file_real_append_to_async (GFile *file,
190 GFileCreateFlags flags,
192 GCancellable *cancellable,
193 GAsyncReadyCallback callback,
195 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
198 static void g_file_real_create_async (GFile *file,
199 GFileCreateFlags flags,
201 GCancellable *cancellable,
202 GAsyncReadyCallback callback,
204 static GFileOutputStream *g_file_real_create_finish (GFile *file,
207 static void g_file_real_replace_async (GFile *file,
209 gboolean make_backup,
210 GFileCreateFlags flags,
212 GCancellable *cancellable,
213 GAsyncReadyCallback callback,
215 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
218 static void g_file_real_delete_async (GFile *file,
220 GCancellable *cancellable,
221 GAsyncReadyCallback callback,
223 static gboolean g_file_real_delete_finish (GFile *file,
226 static void g_file_real_trash_async (GFile *file,
228 GCancellable *cancellable,
229 GAsyncReadyCallback callback,
231 static gboolean g_file_real_trash_finish (GFile *file,
234 static void g_file_real_make_directory_async (GFile *file,
236 GCancellable *cancellable,
237 GAsyncReadyCallback callback,
239 static gboolean g_file_real_make_directory_finish (GFile *file,
242 static void g_file_real_open_readwrite_async (GFile *file,
244 GCancellable *cancellable,
245 GAsyncReadyCallback callback,
247 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
250 static void g_file_real_create_readwrite_async (GFile *file,
251 GFileCreateFlags flags,
253 GCancellable *cancellable,
254 GAsyncReadyCallback callback,
256 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
259 static void g_file_real_replace_readwrite_async (GFile *file,
261 gboolean make_backup,
262 GFileCreateFlags flags,
264 GCancellable *cancellable,
265 GAsyncReadyCallback callback,
267 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
270 static gboolean g_file_real_set_attributes_from_info (GFile *file,
272 GFileQueryInfoFlags flags,
273 GCancellable *cancellable,
275 static void g_file_real_set_display_name_async (GFile *file,
276 const char *display_name,
278 GCancellable *cancellable,
279 GAsyncReadyCallback callback,
281 static GFile * g_file_real_set_display_name_finish (GFile *file,
284 static void g_file_real_set_attributes_async (GFile *file,
286 GFileQueryInfoFlags flags,
288 GCancellable *cancellable,
289 GAsyncReadyCallback callback,
291 static gboolean g_file_real_set_attributes_finish (GFile *file,
295 static void g_file_real_find_enclosing_mount_async (GFile *file,
297 GCancellable *cancellable,
298 GAsyncReadyCallback callback,
300 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
303 static void g_file_real_copy_async (GFile *source,
305 GFileCopyFlags flags,
307 GCancellable *cancellable,
308 GFileProgressCallback progress_callback,
309 gpointer progress_callback_data,
310 GAsyncReadyCallback callback,
312 static gboolean g_file_real_copy_finish (GFile *file,
316 static gboolean g_file_real_measure_disk_usage (GFile *file,
317 GFileMeasureFlags flags,
318 GCancellable *cancellable,
319 GFileMeasureProgressCallback progress_callback,
320 gpointer progress_data,
325 static void g_file_real_measure_disk_usage_async (GFile *file,
326 GFileMeasureFlags flags,
328 GCancellable *cancellable,
329 GFileMeasureProgressCallback progress_callback,
330 gpointer progress_data,
331 GAsyncReadyCallback callback,
333 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
334 GAsyncResult *result,
340 typedef GFileIface GFileInterface;
341 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
344 g_file_default_init (GFileIface *iface)
346 iface->enumerate_children_async = g_file_real_enumerate_children_async;
347 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
348 iface->set_display_name_async = g_file_real_set_display_name_async;
349 iface->set_display_name_finish = g_file_real_set_display_name_finish;
350 iface->query_info_async = g_file_real_query_info_async;
351 iface->query_info_finish = g_file_real_query_info_finish;
352 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
353 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
354 iface->set_attributes_async = g_file_real_set_attributes_async;
355 iface->set_attributes_finish = g_file_real_set_attributes_finish;
356 iface->read_async = g_file_real_read_async;
357 iface->read_finish = g_file_real_read_finish;
358 iface->append_to_async = g_file_real_append_to_async;
359 iface->append_to_finish = g_file_real_append_to_finish;
360 iface->create_async = g_file_real_create_async;
361 iface->create_finish = g_file_real_create_finish;
362 iface->replace_async = g_file_real_replace_async;
363 iface->replace_finish = g_file_real_replace_finish;
364 iface->delete_file_async = g_file_real_delete_async;
365 iface->delete_file_finish = g_file_real_delete_finish;
366 iface->trash_async = g_file_real_trash_async;
367 iface->trash_finish = g_file_real_trash_finish;
368 iface->make_directory_async = g_file_real_make_directory_async;
369 iface->make_directory_finish = g_file_real_make_directory_finish;
370 iface->open_readwrite_async = g_file_real_open_readwrite_async;
371 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
372 iface->create_readwrite_async = g_file_real_create_readwrite_async;
373 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
374 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
375 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
376 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
377 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
378 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
379 iface->copy_async = g_file_real_copy_async;
380 iface->copy_finish = g_file_real_copy_finish;
381 iface->measure_disk_usage = g_file_real_measure_disk_usage;
382 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
383 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
389 * @file: input #GFile
391 * Checks to see if a file is native to the platform.
393 * A native file s one expressed in the platform-native filename format,
394 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
395 * as it might be on a locally mounted remote filesystem.
397 * On some systems non-native files may be available using the native
398 * filesystem via a userspace filesystem (FUSE), in these cases this call
399 * will return %FALSE, but g_file_get_path() will still return a native path.
401 * This call does no blocking I/O.
403 * Returns: %TRUE if @file is native
406 g_file_is_native (GFile *file)
410 g_return_val_if_fail (G_IS_FILE (file), FALSE);
412 iface = G_FILE_GET_IFACE (file);
414 return (* iface->is_native) (file);
419 * g_file_has_uri_scheme:
420 * @file: input #GFile
421 * @uri_scheme: a string containing a URI scheme
423 * Checks to see if a #GFile has a given URI scheme.
425 * This call does no blocking I/O.
427 * Returns: %TRUE if #GFile's backend supports the
428 * given URI scheme, %FALSE if URI scheme is %NULL,
429 * not supported, or #GFile is invalid.
432 g_file_has_uri_scheme (GFile *file,
433 const char *uri_scheme)
437 g_return_val_if_fail (G_IS_FILE (file), FALSE);
438 g_return_val_if_fail (uri_scheme != NULL, FALSE);
440 iface = G_FILE_GET_IFACE (file);
442 return (* iface->has_uri_scheme) (file, uri_scheme);
447 * g_file_get_uri_scheme:
448 * @file: input #GFile
450 * Gets the URI scheme for a #GFile.
451 * RFC 3986 decodes the scheme as:
453 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
455 * Common schemes include "file", "http", "ftp", etc.
457 * This call does no blocking I/O.
459 * Returns: a string containing the URI scheme for the given
460 * #GFile. The returned string should be freed with g_free()
461 * when no longer needed.
464 g_file_get_uri_scheme (GFile *file)
468 g_return_val_if_fail (G_IS_FILE (file), NULL);
470 iface = G_FILE_GET_IFACE (file);
472 return (* iface->get_uri_scheme) (file);
477 * g_file_get_basename:
478 * @file: input #GFile
480 * Gets the base name (the last component of the path) for a given #GFile.
482 * If called for the top level of a system (such as the filesystem root
483 * or a uri like sftp://host/) it will return a single directory separator
484 * (and on Windows, possibly a drive letter).
486 * The base name is a byte string (not UTF-8). It has no defined encoding
487 * or rules other than it may not contain zero bytes. If you want to use
488 * filenames in a user interface you should use the display name that you
489 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
490 * attribute with g_file_query_info().
492 * This call does no blocking I/O.
494 * Returns: string containing the #GFile's base name, or %NULL
495 * if given #GFile is invalid. The returned string should be
496 * freed with g_free() when no longer needed.
499 g_file_get_basename (GFile *file)
503 g_return_val_if_fail (G_IS_FILE (file), NULL);
505 iface = G_FILE_GET_IFACE (file);
507 return (* iface->get_basename) (file);
512 * @file: input #GFile
514 * Gets the local pathname for #GFile, if one exists.
516 * This call does no blocking I/O.
518 * Returns: string containing the #GFile's path, or %NULL if
519 * no such path exists. The returned string should be
520 * freed with g_free() when no longer needed.
523 g_file_get_path (GFile *file)
527 g_return_val_if_fail (G_IS_FILE (file), NULL);
529 iface = G_FILE_GET_IFACE (file);
531 return (* iface->get_path) (file);
536 * @file: input #GFile
538 * Gets the URI for the @file.
540 * This call does no blocking I/O.
542 * Returns: a string containing the #GFile's URI.
543 * The returned string should be freed with g_free()
544 * when no longer needed.
547 g_file_get_uri (GFile *file)
551 g_return_val_if_fail (G_IS_FILE (file), NULL);
553 iface = G_FILE_GET_IFACE (file);
555 return (* iface->get_uri) (file);
559 * g_file_get_parse_name:
560 * @file: input #GFile
562 * Gets the parse name of the @file.
563 * A parse name is a UTF-8 string that describes the
564 * file such that one can get the #GFile back using
565 * g_file_parse_name().
567 * This is generally used to show the #GFile as a nice
568 * full-pathname kind of string in a user interface,
569 * like in a location entry.
571 * For local files with names that can safely be converted
572 * to UTF-8 the pathname is used, otherwise the IRI is used
573 * (a form of URI that allows UTF-8 characters unescaped).
575 * This call does no blocking I/O.
577 * Returns: a string containing the #GFile's parse name.
578 * The returned string should be freed with g_free()
579 * when no longer needed.
582 g_file_get_parse_name (GFile *file)
586 g_return_val_if_fail (G_IS_FILE (file), NULL);
588 iface = G_FILE_GET_IFACE (file);
590 return (* iface->get_parse_name) (file);
595 * @file: input #GFile
597 * Duplicates a #GFile handle. This operation does not duplicate
598 * the actual file or directory represented by the #GFile; see
599 * g_file_copy() if attempting to copy a file.
601 * This call does no blocking I/O.
603 * Returns: (transfer full): a new #GFile that is a duplicate
604 * of the given #GFile.
607 g_file_dup (GFile *file)
611 g_return_val_if_fail (G_IS_FILE (file), NULL);
613 iface = G_FILE_GET_IFACE (file);
615 return (* iface->dup) (file);
620 * @file: (type GFile): #gconstpointer to a #GFile
622 * Creates a hash value for a #GFile.
624 * This call does no blocking I/O.
627 * Returns: 0 if @file is not a valid #GFile, otherwise an
628 * integer that can be used as hash value for the #GFile.
629 * This function is intended for easily hashing a #GFile to
630 * add to a #GHashTable or similar data structure.
633 g_file_hash (gconstpointer file)
637 g_return_val_if_fail (G_IS_FILE (file), 0);
639 iface = G_FILE_GET_IFACE (file);
641 return (* iface->hash) ((GFile *)file);
646 * @file1: the first #GFile
647 * @file2: the second #GFile
649 * Checks equality of two given #GFiles.
651 * Note that two #GFiles that differ can still refer to the same
652 * file on the filesystem due to various forms of filename
655 * This call does no blocking I/O.
657 * Returns: %TRUE if @file1 and @file2 are equal.
658 * %FALSE if either is not a #GFile.
661 g_file_equal (GFile *file1,
666 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
667 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
669 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
672 iface = G_FILE_GET_IFACE (file1);
674 return (* iface->equal) (file1, file2);
680 * @file: input #GFile
682 * Gets the parent directory for the @file.
683 * If the @file represents the root directory of the
684 * file system, then %NULL will be returned.
686 * This call does no blocking I/O.
688 * Returns: (transfer full): a #GFile structure to the
689 * parent of the given #GFile or %NULL if there is
690 * no parent. Free the returned object with g_object_unref().
693 g_file_get_parent (GFile *file)
697 g_return_val_if_fail (G_IS_FILE (file), NULL);
699 iface = G_FILE_GET_IFACE (file);
701 return (* iface->get_parent) (file);
706 * @file: input #GFile
707 * @parent: (allow-none): the parent to check for, or %NULL
709 * Checks if @file has a parent, and optionally, if it is @parent.
711 * If @parent is %NULL then this function returns %TRUE if @file has any
712 * parent at all. If @parent is non-%NULL then %TRUE is only returned
713 * if @file is a child of @parent.
715 * Returns: %TRUE if @file is a child of @parent (or any parent in the
716 * case that @parent is %NULL).
721 g_file_has_parent (GFile *file,
724 GFile *actual_parent;
727 g_return_val_if_fail (G_IS_FILE (file), FALSE);
728 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
730 actual_parent = g_file_get_parent (file);
732 if (actual_parent != NULL)
735 result = g_file_equal (parent, actual_parent);
739 g_object_unref (actual_parent);
749 * @file: input #GFile
750 * @name: string containing the child's basename
752 * Gets a child of @file with basename equal to @name.
754 * Note that the file with that specific name might not exist, but
755 * you can still have a #GFile that points to it. You can use this
756 * for instance to create that file.
758 * This call does no blocking I/O.
760 * Returns: (transfer full): a #GFile to a child specified by @name.
761 * Free the returned object with g_object_unref().
764 g_file_get_child (GFile *file,
767 g_return_val_if_fail (G_IS_FILE (file), NULL);
768 g_return_val_if_fail (name != NULL, NULL);
770 return g_file_resolve_relative_path (file, name);
774 * g_file_get_child_for_display_name:
775 * @file: input #GFile
776 * @display_name: string to a possible child
777 * @error: return location for an error
779 * Gets the child of @file for a given @display_name (i.e. a UTF-8
780 * version of the name). If this function fails, it returns %NULL
781 * and @error will be set. This is very useful when constructing a
782 * #GFile for a new file and the user entered the filename in the
783 * user interface, for instance when you select a directory and
784 * type a filename in the file selector.
786 * This call does no blocking I/O.
788 * Returns: (transfer full): a #GFile to the specified child, or
789 * %NULL if the display name couldn't be converted.
790 * Free the returned object with g_object_unref().
793 g_file_get_child_for_display_name (GFile *file,
794 const char *display_name,
799 g_return_val_if_fail (G_IS_FILE (file), NULL);
800 g_return_val_if_fail (display_name != NULL, NULL);
802 iface = G_FILE_GET_IFACE (file);
804 return (* iface->get_child_for_display_name) (file, display_name, error);
809 * @file: input #GFile
810 * @prefix: input #GFile
812 * Checks whether @file has the prefix specified by @prefix.
814 * In other words, if the names of initial elements of @file's
815 * pathname match @prefix. Only full pathname elements are matched,
816 * so a path like /foo is not considered a prefix of /foobar, only
819 * This call does no I/O, as it works purely on names. As such it can
820 * sometimes return %FALSE even if @file is inside a @prefix (from a
821 * filesystem point of view), because the prefix of @file is an alias
824 * Virtual: prefix_matches
825 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
829 g_file_has_prefix (GFile *file,
834 g_return_val_if_fail (G_IS_FILE (file), FALSE);
835 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
837 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
840 iface = G_FILE_GET_IFACE (file);
842 /* The vtable function differs in arg order since
843 * we're using the old contains_file call
845 return (* iface->prefix_matches) (prefix, file);
849 * g_file_get_relative_path:
850 * @parent: input #GFile
851 * @descendant: input #GFile
853 * Gets the path for @descendant relative to @parent.
855 * This call does no blocking I/O.
857 * Returns: string with the relative path from @descendant
858 * to @parent, or %NULL if @descendant doesn't have @parent
859 * as prefix. The returned string should be freed with g_free()
860 * when no longer needed.
863 g_file_get_relative_path (GFile *parent,
868 g_return_val_if_fail (G_IS_FILE (parent), NULL);
869 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
871 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
874 iface = G_FILE_GET_IFACE (parent);
876 return (* iface->get_relative_path) (parent, descendant);
880 * g_file_resolve_relative_path:
881 * @file: input #GFile
882 * @relative_path: a given relative path string
884 * Resolves a relative path for @file to an absolute path.
886 * This call does no blocking I/O.
888 * Returns: (transfer full): #GFile to the resolved path.
889 * %NULL if @relative_path is %NULL or if @file is invalid.
890 * Free the returned object with g_object_unref().
893 g_file_resolve_relative_path (GFile *file,
894 const char *relative_path)
898 g_return_val_if_fail (G_IS_FILE (file), NULL);
899 g_return_val_if_fail (relative_path != NULL, NULL);
901 iface = G_FILE_GET_IFACE (file);
903 return (* iface->resolve_relative_path) (file, relative_path);
907 * g_file_enumerate_children:
908 * @file: input #GFile
909 * @attributes: an attribute query string
910 * @flags: a set of #GFileQueryInfoFlags
911 * @cancellable: (allow-none): optional #GCancellable object,
913 * @error: #GError for error reporting
915 * Gets the requested information about the files in a directory.
916 * The result is a #GFileEnumerator object that will give out
917 * #GFileInfo objects for all the files in the directory.
919 * The @attributes value is a string that specifies the file
920 * attributes that should be gathered. It is not an error if
921 * it's not possible to read a particular requested attribute
922 * from a file - it just won't be set. @attributes should
923 * be a comma-separated list of attributes or attribute wildcards.
924 * The wildcard "*" means all attributes, and a wildcard like
925 * "standard::*" means all attributes in the standard namespace.
926 * An example attribute query be "standard::*,owner::user".
927 * The standard attributes are available as defines, like
928 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
930 * If @cancellable is not %NULL, then the operation can be cancelled
931 * by triggering the cancellable object from another thread. If the
932 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
935 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
936 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
937 * error will be returned. Other errors are possible too.
939 * Returns: (transfer full): A #GFileEnumerator if successful,
940 * %NULL on error. Free the returned object with g_object_unref().
943 g_file_enumerate_children (GFile *file,
944 const char *attributes,
945 GFileQueryInfoFlags flags,
946 GCancellable *cancellable,
951 g_return_val_if_fail (G_IS_FILE (file), NULL);
953 if (g_cancellable_set_error_if_cancelled (cancellable, error))
956 iface = G_FILE_GET_IFACE (file);
958 if (iface->enumerate_children == NULL)
960 g_set_error_literal (error, G_IO_ERROR,
961 G_IO_ERROR_NOT_SUPPORTED,
962 _("Operation not supported"));
966 return (* iface->enumerate_children) (file, attributes, flags,
971 * g_file_enumerate_children_async:
972 * @file: input #GFile
973 * @attributes: an attribute query string
974 * @flags: a set of #GFileQueryInfoFlags
975 * @io_priority: the <link linkend="io-priority">I/O priority</link>
977 * @cancellable: (allow-none): optional #GCancellable object,
979 * @callback: (scope async): a #GAsyncReadyCallback to call when the
980 * request is satisfied
981 * @user_data: (closure): the data to pass to callback function
983 * Asynchronously gets the requested information about the files
984 * in a directory. The result is a #GFileEnumerator object that will
985 * give out #GFileInfo objects for all the files in the directory.
987 * For more details, see g_file_enumerate_children() which is
988 * the synchronous version of this call.
990 * When the operation is finished, @callback will be called. You can
991 * then call g_file_enumerate_children_finish() to get the result of
995 g_file_enumerate_children_async (GFile *file,
996 const char *attributes,
997 GFileQueryInfoFlags flags,
999 GCancellable *cancellable,
1000 GAsyncReadyCallback callback,
1005 g_return_if_fail (G_IS_FILE (file));
1007 iface = G_FILE_GET_IFACE (file);
1008 (* iface->enumerate_children_async) (file,
1018 * g_file_enumerate_children_finish:
1019 * @file: input #GFile
1020 * @res: a #GAsyncResult
1023 * Finishes an async enumerate children operation.
1024 * See g_file_enumerate_children_async().
1026 * Returns: (transfer full): a #GFileEnumerator or %NULL
1027 * if an error occurred.
1028 * Free the returned object with g_object_unref().
1031 g_file_enumerate_children_finish (GFile *file,
1037 g_return_val_if_fail (G_IS_FILE (file), NULL);
1038 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1040 if (g_async_result_legacy_propagate_error (res, error))
1043 iface = G_FILE_GET_IFACE (file);
1044 return (* iface->enumerate_children_finish) (file, res, error);
1048 * g_file_query_exists:
1049 * @file: input #GFile
1050 * @cancellable: (allow-none): optional #GCancellable object,
1053 * Utility function to check if a particular file exists. This is
1054 * implemented using g_file_query_info() and as such does blocking I/O.
1056 * Note that in many cases it is racy to first check for file existence
1057 * and then execute something based on the outcome of that, because the
1058 * file might have been created or removed in between the operations. The
1059 * general approach to handling that is to not check, but just do the
1060 * operation and handle the errors as they come.
1062 * As an example of race-free checking, take the case of reading a file,
1063 * and if it doesn't exist, creating it. There are two racy versions: read
1064 * it, and on error create it; and: check if it exists, if not create it.
1065 * These can both result in two processes creating the file (with perhaps
1066 * a partially written file as the result). The correct approach is to
1067 * always try to create the file with g_file_create() which will either
1068 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1070 * However, in many cases an existence check is useful in a user interface,
1071 * for instance to make a menu item sensitive/insensitive, so that you don't
1072 * have to fool users that something is possible and then just show an error
1073 * dialog. If you do this, you should make sure to also handle the errors
1074 * that can happen due to races when you execute the operation.
1076 * Returns: %TRUE if the file exists (and can be detected without error),
1077 * %FALSE otherwise (or if cancelled).
1080 g_file_query_exists (GFile *file,
1081 GCancellable *cancellable)
1085 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1087 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1088 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1091 g_object_unref (info);
1099 * g_file_query_file_type:
1100 * @file: input #GFile
1101 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1102 * @cancellable: (allow-none): optional #GCancellable object,
1105 * Utility function to inspect the #GFileType of a file. This is
1106 * implemented using g_file_query_info() and as such does blocking I/O.
1108 * The primary use case of this method is to check if a file is
1109 * a regular file, directory, or symlink.
1111 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1112 * if the file does not exist
1117 g_file_query_file_type (GFile *file,
1118 GFileQueryInfoFlags flags,
1119 GCancellable *cancellable)
1122 GFileType file_type;
1124 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1125 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1129 file_type = g_file_info_get_file_type (info);
1130 g_object_unref (info);
1133 file_type = G_FILE_TYPE_UNKNOWN;
1139 * g_file_query_info:
1140 * @file: input #GFile
1141 * @attributes: an attribute query string
1142 * @flags: a set of #GFileQueryInfoFlags
1143 * @cancellable: (allow-none): optional #GCancellable object,
1147 * Gets the requested information about specified @file.
1148 * The result is a #GFileInfo object that contains key-value
1149 * attributes (such as the type or size of the file).
1151 * The @attributes value is a string that specifies the file
1152 * attributes that should be gathered. It is not an error if
1153 * it's not possible to read a particular requested attribute
1154 * from a file - it just won't be set. @attributes should be a
1155 * comma-separated list of attributes or attribute wildcards.
1156 * The wildcard "*" means all attributes, and a wildcard like
1157 * "standard::*" means all attributes in the standard namespace.
1158 * An example attribute query be "standard::*,owner::user".
1159 * The standard attributes are available as defines, like
1160 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1162 * If @cancellable is not %NULL, then the operation can be cancelled
1163 * by triggering the cancellable object from another thread. If the
1164 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1167 * For symlinks, normally the information about the target of the
1168 * symlink is returned, rather than information about the symlink
1169 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1170 * in @flags the information about the symlink itself will be returned.
1171 * Also, for symlinks that point to non-existing files the information
1172 * about the symlink itself will be returned.
1174 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1175 * returned. Other errors are possible too, and depend on what kind of
1176 * filesystem the file is on.
1178 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1179 * on error. Free the returned object with g_object_unref().
1182 g_file_query_info (GFile *file,
1183 const char *attributes,
1184 GFileQueryInfoFlags flags,
1185 GCancellable *cancellable,
1190 g_return_val_if_fail (G_IS_FILE (file), NULL);
1192 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1195 iface = G_FILE_GET_IFACE (file);
1197 if (iface->query_info == NULL)
1199 g_set_error_literal (error, G_IO_ERROR,
1200 G_IO_ERROR_NOT_SUPPORTED,
1201 _("Operation not supported"));
1205 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1209 * g_file_query_info_async:
1210 * @file: input #GFile
1211 * @attributes: an attribute query string
1212 * @flags: a set of #GFileQueryInfoFlags
1213 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1215 * @cancellable: (allow-none): optional #GCancellable object,
1217 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1218 * request is satisfied
1219 * @user_data: (closure): the data to pass to callback function
1221 * Asynchronously gets the requested information about specified @file.
1222 * The result is a #GFileInfo object that contains key-value attributes
1223 * (such as type or size for the file).
1225 * For more details, see g_file_query_info() which is the synchronous
1226 * version of this call.
1228 * When the operation is finished, @callback will be called. You can
1229 * then call g_file_query_info_finish() to get the result of the operation.
1232 g_file_query_info_async (GFile *file,
1233 const char *attributes,
1234 GFileQueryInfoFlags flags,
1236 GCancellable *cancellable,
1237 GAsyncReadyCallback callback,
1242 g_return_if_fail (G_IS_FILE (file));
1244 iface = G_FILE_GET_IFACE (file);
1245 (* iface->query_info_async) (file,
1255 * g_file_query_info_finish:
1256 * @file: input #GFile
1257 * @res: a #GAsyncResult
1260 * Finishes an asynchronous file info query.
1261 * See g_file_query_info_async().
1263 * Returns: (transfer full): #GFileInfo for given @file
1264 * or %NULL on error. Free the returned object with
1268 g_file_query_info_finish (GFile *file,
1274 g_return_val_if_fail (G_IS_FILE (file), NULL);
1275 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1277 if (g_async_result_legacy_propagate_error (res, error))
1280 iface = G_FILE_GET_IFACE (file);
1281 return (* iface->query_info_finish) (file, res, error);
1285 * g_file_query_filesystem_info:
1286 * @file: input #GFile
1287 * @attributes: an attribute query string
1288 * @cancellable: (allow-none): optional #GCancellable object,
1292 * Similar to g_file_query_info(), but obtains information
1293 * about the filesystem the @file is on, rather than the file itself.
1294 * For instance the amount of space available and the type of
1297 * The @attributes value is a string that specifies the attributes
1298 * that should be gathered. It is not an error if it's not possible
1299 * to read a particular requested attribute from a file - it just
1300 * won't be set. @attributes should be a comma-separated list of
1301 * attributes or attribute wildcards. The wildcard "*" means all
1302 * attributes, and a wildcard like "filesystem::*" means all attributes
1303 * in the filesystem namespace. The standard namespace for filesystem
1304 * attributes is "filesystem". Common attributes of interest are
1305 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1306 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1307 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1309 * If @cancellable is not %NULL, then the operation can be cancelled
1310 * by triggering the cancellable object from another thread. If the
1311 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1314 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1315 * be returned. Other errors are possible too, and depend on what
1316 * kind of filesystem the file is on.
1318 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1319 * Free the returned object with g_object_unref().
1322 g_file_query_filesystem_info (GFile *file,
1323 const char *attributes,
1324 GCancellable *cancellable,
1329 g_return_val_if_fail (G_IS_FILE (file), NULL);
1331 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1334 iface = G_FILE_GET_IFACE (file);
1336 if (iface->query_filesystem_info == NULL)
1338 g_set_error_literal (error, G_IO_ERROR,
1339 G_IO_ERROR_NOT_SUPPORTED,
1340 _("Operation not supported"));
1344 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1348 * g_file_query_filesystem_info_async:
1349 * @file: input #GFile
1350 * @attributes: an attribute query string
1351 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1353 * @cancellable: (allow-none): optional #GCancellable object,
1355 * @callback: (scope async): a #GAsyncReadyCallback to call
1356 * when the request is satisfied
1357 * @user_data: (closure): the data to pass to callback function
1359 * Asynchronously gets the requested information about the filesystem
1360 * that the specified @file is on. The result is a #GFileInfo object
1361 * that contains key-value attributes (such as type or size for the
1364 * For more details, see g_file_query_filesystem_info() which is the
1365 * synchronous version of this call.
1367 * When the operation is finished, @callback will be called. You can
1368 * then call g_file_query_info_finish() to get the result of the
1372 g_file_query_filesystem_info_async (GFile *file,
1373 const char *attributes,
1375 GCancellable *cancellable,
1376 GAsyncReadyCallback callback,
1381 g_return_if_fail (G_IS_FILE (file));
1383 iface = G_FILE_GET_IFACE (file);
1384 (* iface->query_filesystem_info_async) (file,
1393 * g_file_query_filesystem_info_finish:
1394 * @file: input #GFile
1395 * @res: a #GAsyncResult
1398 * Finishes an asynchronous filesystem info query.
1399 * See g_file_query_filesystem_info_async().
1401 * Returns: (transfer full): #GFileInfo for given @file
1402 * or %NULL on error.
1403 * Free the returned object with g_object_unref().
1406 g_file_query_filesystem_info_finish (GFile *file,
1412 g_return_val_if_fail (G_IS_FILE (file), NULL);
1413 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1415 if (g_async_result_legacy_propagate_error (res, error))
1418 iface = G_FILE_GET_IFACE (file);
1419 return (* iface->query_filesystem_info_finish) (file, res, error);
1423 * g_file_find_enclosing_mount:
1424 * @file: input #GFile
1425 * @cancellable: (allow-none): optional #GCancellable object,
1429 * Gets a #GMount for the #GFile.
1431 * If the #GFileIface for @file does not have a mount (e.g.
1432 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1433 * and %NULL will be returned.
1435 * If @cancellable is not %NULL, then the operation can be cancelled by
1436 * triggering the cancellable object from another thread. If the operation
1437 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1439 * Returns: (transfer full): a #GMount where the @file is located
1440 * or %NULL on error.
1441 * Free the returned object with g_object_unref().
1444 g_file_find_enclosing_mount (GFile *file,
1445 GCancellable *cancellable,
1450 g_return_val_if_fail (G_IS_FILE (file), NULL);
1452 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1455 iface = G_FILE_GET_IFACE (file);
1456 if (iface->find_enclosing_mount == NULL)
1459 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1460 /* Translators: This is an error message when
1461 * trying to find the enclosing (user visible)
1462 * mount of a file, but none exists.
1464 _("Containing mount does not exist"));
1468 return (* iface->find_enclosing_mount) (file, cancellable, error);
1472 * g_file_find_enclosing_mount_async:
1474 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1476 * @cancellable: (allow-none): optional #GCancellable object,
1478 * @callback: (scope async): a #GAsyncReadyCallback to call
1479 * when the request is satisfied
1480 * @user_data: (closure): the data to pass to callback function
1482 * Asynchronously gets the mount for the file.
1484 * For more details, see g_file_find_enclosing_mount() which is
1485 * the synchronous version of this call.
1487 * When the operation is finished, @callback will be called.
1488 * You can then call g_file_find_enclosing_mount_finish() to
1489 * get the result of the operation.
1492 g_file_find_enclosing_mount_async (GFile *file,
1494 GCancellable *cancellable,
1495 GAsyncReadyCallback callback,
1500 g_return_if_fail (G_IS_FILE (file));
1502 iface = G_FILE_GET_IFACE (file);
1503 (* iface->find_enclosing_mount_async) (file,
1511 * g_file_find_enclosing_mount_finish:
1513 * @res: a #GAsyncResult
1516 * Finishes an asynchronous find mount request.
1517 * See g_file_find_enclosing_mount_async().
1519 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1520 * Free the returned object with g_object_unref().
1523 g_file_find_enclosing_mount_finish (GFile *file,
1529 g_return_val_if_fail (G_IS_FILE (file), NULL);
1530 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1532 if (g_async_result_legacy_propagate_error (res, error))
1535 iface = G_FILE_GET_IFACE (file);
1536 return (* iface->find_enclosing_mount_finish) (file, res, error);
1542 * @file: #GFile to read
1543 * @cancellable: (allow-none): a #GCancellable
1544 * @error: a #GError, or %NULL
1546 * Opens a file for reading. The result is a #GFileInputStream that
1547 * can be used to read the contents of the file.
1549 * If @cancellable is not %NULL, then the operation can be cancelled by
1550 * triggering the cancellable object from another thread. If the operation
1551 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1553 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1554 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1555 * error will be returned. Other errors are possible too, and depend
1556 * on what kind of filesystem the file is on.
1559 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1560 * Free the returned object with g_object_unref().
1563 g_file_read (GFile *file,
1564 GCancellable *cancellable,
1569 g_return_val_if_fail (G_IS_FILE (file), NULL);
1571 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1574 iface = G_FILE_GET_IFACE (file);
1576 if (iface->read_fn == NULL)
1578 g_set_error_literal (error, G_IO_ERROR,
1579 G_IO_ERROR_NOT_SUPPORTED,
1580 _("Operation not supported"));
1584 return (* iface->read_fn) (file, cancellable, error);
1589 * @file: input #GFile
1590 * @flags: a set of #GFileCreateFlags
1591 * @cancellable: (allow-none): optional #GCancellable object,
1593 * @error: a #GError, or %NULL
1595 * Gets an output stream for appending data to the file.
1596 * If the file doesn't already exist it is created.
1598 * By default files created are generally readable by everyone,
1599 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1600 * will be made readable only to the current user, to the level that
1601 * is supported on the target filesystem.
1603 * If @cancellable is not %NULL, then the operation can be cancelled
1604 * by triggering the cancellable object from another thread. If the
1605 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1608 * Some file systems don't allow all file names, and may return an
1609 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1610 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1611 * possible too, and depend on what kind of filesystem the file is on.
1613 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1614 * Free the returned object with g_object_unref().
1617 g_file_append_to (GFile *file,
1618 GFileCreateFlags flags,
1619 GCancellable *cancellable,
1624 g_return_val_if_fail (G_IS_FILE (file), NULL);
1626 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1629 iface = G_FILE_GET_IFACE (file);
1631 if (iface->append_to == NULL)
1633 g_set_error_literal (error, G_IO_ERROR,
1634 G_IO_ERROR_NOT_SUPPORTED,
1635 _("Operation not supported"));
1639 return (* iface->append_to) (file, flags, cancellable, error);
1644 * @file: input #GFile
1645 * @flags: a set of #GFileCreateFlags
1646 * @cancellable: (allow-none): optional #GCancellable object,
1648 * @error: a #GError, or %NULL
1650 * Creates a new file and returns an output stream for writing to it.
1651 * The file must not already exist.
1653 * By default files created are generally readable by everyone,
1654 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1655 * will be made readable only to the current user, to the level
1656 * that is supported on the target filesystem.
1658 * If @cancellable is not %NULL, then the operation can be cancelled
1659 * by triggering the cancellable object from another thread. If the
1660 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1663 * If a file or directory with this name already exists the
1664 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1665 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1666 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1667 * be returned. Other errors are possible too, and depend on what kind
1668 * of filesystem the file is on.
1670 * Returns: (transfer full): a #GFileOutputStream for the newly created
1671 * file, or %NULL on error.
1672 * Free the returned object with g_object_unref().
1675 g_file_create (GFile *file,
1676 GFileCreateFlags flags,
1677 GCancellable *cancellable,
1682 g_return_val_if_fail (G_IS_FILE (file), NULL);
1684 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1687 iface = G_FILE_GET_IFACE (file);
1689 if (iface->create == NULL)
1691 g_set_error_literal (error, G_IO_ERROR,
1692 G_IO_ERROR_NOT_SUPPORTED,
1693 _("Operation not supported"));
1697 return (* iface->create) (file, flags, cancellable, error);
1702 * @file: input #GFile
1703 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1704 * for the current #GFile, or #NULL to ignore
1705 * @make_backup: %TRUE if a backup should be created
1706 * @flags: a set of #GFileCreateFlags
1707 * @cancellable: (allow-none): optional #GCancellable object,
1709 * @error: a #GError, or %NULL
1711 * Returns an output stream for overwriting the file, possibly
1712 * creating a backup copy of the file first. If the file doesn't exist,
1713 * it will be created.
1715 * This will try to replace the file in the safest way possible so
1716 * that any errors during the writing will not affect an already
1717 * existing copy of the file. For instance, for local files it
1718 * may write to a temporary file and then atomically rename over
1719 * the destination when the stream is closed.
1721 * By default files created are generally readable by everyone,
1722 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1723 * will be made readable only to the current user, to the level that
1724 * is supported on the target filesystem.
1726 * If @cancellable is not %NULL, then the operation can be cancelled
1727 * by triggering the cancellable object from another thread. If the
1728 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1731 * If you pass in a non-%NULL @etag value, then this value is
1732 * compared to the current entity tag of the file, and if they differ
1733 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1734 * that the file has been changed since you last read it. You can get
1735 * the new etag from g_file_output_stream_get_etag() after you've
1736 * finished writing and closed the #GFileOutputStream. When you load
1737 * a new file you can use g_file_input_stream_query_info() to get
1738 * the etag of the file.
1740 * If @make_backup is %TRUE, this function will attempt to make a
1741 * backup of the current file before overwriting it. If this fails
1742 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1743 * want to replace anyway, try again with @make_backup set to %FALSE.
1745 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1746 * be returned, and if the file is some other form of non-regular file
1747 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1748 * file systems don't allow all file names, and may return an
1749 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1750 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1751 * possible too, and depend on what kind of filesystem the file is on.
1753 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1754 * Free the returned object with g_object_unref().
1757 g_file_replace (GFile *file,
1759 gboolean make_backup,
1760 GFileCreateFlags flags,
1761 GCancellable *cancellable,
1766 g_return_val_if_fail (G_IS_FILE (file), NULL);
1768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1771 iface = G_FILE_GET_IFACE (file);
1773 if (iface->replace == NULL)
1775 g_set_error_literal (error, G_IO_ERROR,
1776 G_IO_ERROR_NOT_SUPPORTED,
1777 _("Operation not supported"));
1781 /* Handle empty tag string as NULL in consistent way. */
1782 if (etag && *etag == 0)
1785 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1789 * g_file_open_readwrite:
1790 * @file: #GFile to open
1791 * @cancellable: (allow-none): a #GCancellable
1792 * @error: a #GError, or %NULL
1794 * Opens an existing file for reading and writing. The result is
1795 * a #GFileIOStream that can be used to read and write the contents
1798 * If @cancellable is not %NULL, then the operation can be cancelled
1799 * by triggering the cancellable object from another thread. If the
1800 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1803 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1804 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1805 * error will be returned. Other errors are possible too, and depend on
1806 * what kind of filesystem the file is on. Note that in many non-local
1807 * file cases read and write streams are not supported, so make sure you
1808 * really need to do read and write streaming, rather than just opening
1809 * for reading or writing.
1811 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1812 * Free the returned object with g_object_unref().
1817 g_file_open_readwrite (GFile *file,
1818 GCancellable *cancellable,
1823 g_return_val_if_fail (G_IS_FILE (file), NULL);
1825 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1828 iface = G_FILE_GET_IFACE (file);
1830 if (iface->open_readwrite == NULL)
1832 g_set_error_literal (error, G_IO_ERROR,
1833 G_IO_ERROR_NOT_SUPPORTED,
1834 _("Operation not supported"));
1838 return (* iface->open_readwrite) (file, cancellable, error);
1842 * g_file_create_readwrite:
1844 * @flags: a set of #GFileCreateFlags
1845 * @cancellable: (allow-none): optional #GCancellable object,
1847 * @error: return location for a #GError, or %NULL
1849 * Creates a new file and returns a stream for reading and
1850 * writing to it. The file must not already exist.
1852 * By default files created are generally readable by everyone,
1853 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1854 * will be made readable only to the current user, to the level
1855 * that is supported on the target filesystem.
1857 * If @cancellable is not %NULL, then the operation can be cancelled
1858 * by triggering the cancellable object from another thread. If the
1859 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1862 * If a file or directory with this name already exists, the
1863 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1864 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1865 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1866 * will be returned. Other errors are possible too, and depend on what
1867 * kind of filesystem the file is on.
1869 * Note that in many non-local file cases read and write streams are
1870 * not supported, so make sure you really need to do read and write
1871 * streaming, rather than just opening for reading or writing.
1873 * Returns: (transfer full): a #GFileIOStream for the newly created
1874 * file, or %NULL on error.
1875 * Free the returned object with g_object_unref().
1880 g_file_create_readwrite (GFile *file,
1881 GFileCreateFlags flags,
1882 GCancellable *cancellable,
1887 g_return_val_if_fail (G_IS_FILE (file), NULL);
1889 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1892 iface = G_FILE_GET_IFACE (file);
1894 if (iface->create_readwrite == NULL)
1896 g_set_error_literal (error, G_IO_ERROR,
1897 G_IO_ERROR_NOT_SUPPORTED,
1898 _("Operation not supported"));
1902 return (* iface->create_readwrite) (file, flags, cancellable, error);
1906 * g_file_replace_readwrite:
1908 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1909 * for the current #GFile, or #NULL to ignore
1910 * @make_backup: %TRUE if a backup should be created
1911 * @flags: a set of #GFileCreateFlags
1912 * @cancellable: (allow-none): optional #GCancellable object,
1914 * @error: return location for a #GError, or %NULL
1916 * Returns an output stream for overwriting the file in readwrite mode,
1917 * possibly creating a backup copy of the file first. If the file doesn't
1918 * exist, it will be created.
1920 * For details about the behaviour, see g_file_replace() which does the
1921 * same thing but returns an output stream only.
1923 * Note that in many non-local file cases read and write streams are not
1924 * supported, so make sure you really need to do read and write streaming,
1925 * rather than just opening for reading or writing.
1927 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1928 * Free the returned object with g_object_unref().
1933 g_file_replace_readwrite (GFile *file,
1935 gboolean make_backup,
1936 GFileCreateFlags flags,
1937 GCancellable *cancellable,
1942 g_return_val_if_fail (G_IS_FILE (file), NULL);
1944 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1947 iface = G_FILE_GET_IFACE (file);
1949 if (iface->replace_readwrite == NULL)
1951 g_set_error_literal (error, G_IO_ERROR,
1952 G_IO_ERROR_NOT_SUPPORTED,
1953 _("Operation not supported"));
1957 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1961 * g_file_read_async:
1962 * @file: input #GFile
1963 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1965 * @cancellable: (allow-none): optional #GCancellable object,
1967 * @callback: (scope async): a #GAsyncReadyCallback to call
1968 * when the request is satisfied
1969 * @user_data: (closure): the data to pass to callback function
1971 * Asynchronously opens @file for reading.
1973 * For more details, see g_file_read() which is
1974 * the synchronous version of this call.
1976 * When the operation is finished, @callback will be called.
1977 * You can then call g_file_read_finish() to get the result
1981 g_file_read_async (GFile *file,
1983 GCancellable *cancellable,
1984 GAsyncReadyCallback callback,
1989 g_return_if_fail (G_IS_FILE (file));
1991 iface = G_FILE_GET_IFACE (file);
1992 (* iface->read_async) (file,
2000 * g_file_read_finish:
2001 * @file: input #GFile
2002 * @res: a #GAsyncResult
2003 * @error: a #GError, or %NULL
2005 * Finishes an asynchronous file read operation started with
2006 * g_file_read_async().
2008 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2009 * Free the returned object with g_object_unref().
2012 g_file_read_finish (GFile *file,
2018 g_return_val_if_fail (G_IS_FILE (file), NULL);
2019 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2021 if (g_async_result_legacy_propagate_error (res, error))
2024 iface = G_FILE_GET_IFACE (file);
2025 return (* iface->read_finish) (file, res, error);
2029 * g_file_append_to_async:
2030 * @file: input #GFile
2031 * @flags: a set of #GFileCreateFlags
2032 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2034 * @cancellable: (allow-none): optional #GCancellable object,
2036 * @callback: (scope async): a #GAsyncReadyCallback to call
2037 * when the request is satisfied
2038 * @user_data: (closure): the data to pass to callback function
2040 * Asynchronously opens @file for appending.
2042 * For more details, see g_file_append_to() which is
2043 * the synchronous version of this call.
2045 * When the operation is finished, @callback will be called.
2046 * You can then call g_file_append_to_finish() to get the result
2050 g_file_append_to_async (GFile *file,
2051 GFileCreateFlags flags,
2053 GCancellable *cancellable,
2054 GAsyncReadyCallback callback,
2059 g_return_if_fail (G_IS_FILE (file));
2061 iface = G_FILE_GET_IFACE (file);
2062 (* iface->append_to_async) (file,
2071 * g_file_append_to_finish:
2072 * @file: input #GFile
2073 * @res: #GAsyncResult
2074 * @error: a #GError, or %NULL
2076 * Finishes an asynchronous file append operation started with
2077 * g_file_append_to_async().
2079 * Returns: (transfer full): a valid #GFileOutputStream
2080 * or %NULL on error.
2081 * Free the returned object with g_object_unref().
2084 g_file_append_to_finish (GFile *file,
2090 g_return_val_if_fail (G_IS_FILE (file), NULL);
2091 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2093 if (g_async_result_legacy_propagate_error (res, error))
2096 iface = G_FILE_GET_IFACE (file);
2097 return (* iface->append_to_finish) (file, res, error);
2101 * g_file_create_async:
2102 * @file: input #GFile
2103 * @flags: a set of #GFileCreateFlags
2104 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2106 * @cancellable: (allow-none): optional #GCancellable object,
2108 * @callback: (scope async): a #GAsyncReadyCallback to call
2109 * when the request is satisfied
2110 * @user_data: (closure): the data to pass to callback function
2112 * Asynchronously creates a new file and returns an output stream
2113 * for writing to it. The file must not already exist.
2115 * For more details, see g_file_create() which is
2116 * the synchronous version of this call.
2118 * When the operation is finished, @callback will be called.
2119 * You can then call g_file_create_finish() to get the result
2123 g_file_create_async (GFile *file,
2124 GFileCreateFlags flags,
2126 GCancellable *cancellable,
2127 GAsyncReadyCallback callback,
2132 g_return_if_fail (G_IS_FILE (file));
2134 iface = G_FILE_GET_IFACE (file);
2135 (* iface->create_async) (file,
2144 * g_file_create_finish:
2145 * @file: input #GFile
2146 * @res: a #GAsyncResult
2147 * @error: a #GError, or %NULL
2149 * Finishes an asynchronous file create operation started with
2150 * g_file_create_async().
2152 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2153 * Free the returned object with g_object_unref().
2156 g_file_create_finish (GFile *file,
2162 g_return_val_if_fail (G_IS_FILE (file), NULL);
2163 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2165 if (g_async_result_legacy_propagate_error (res, error))
2168 iface = G_FILE_GET_IFACE (file);
2169 return (* iface->create_finish) (file, res, error);
2173 * g_file_replace_async:
2174 * @file: input #GFile
2175 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2176 * for the current #GFile, or NULL to ignore
2177 * @make_backup: %TRUE if a backup should be created
2178 * @flags: a set of #GFileCreateFlags
2179 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2181 * @cancellable: (allow-none): optional #GCancellable object,
2183 * @callback: (scope async): a #GAsyncReadyCallback to call
2184 * when the request is satisfied
2185 * @user_data: (closure): the data to pass to callback function
2187 * Asynchronously overwrites the file, replacing the contents,
2188 * possibly creating a backup copy of the file first.
2190 * For more details, see g_file_replace() which is
2191 * the synchronous version of this call.
2193 * When the operation is finished, @callback will be called.
2194 * You can then call g_file_replace_finish() to get the result
2198 g_file_replace_async (GFile *file,
2200 gboolean make_backup,
2201 GFileCreateFlags flags,
2203 GCancellable *cancellable,
2204 GAsyncReadyCallback callback,
2209 g_return_if_fail (G_IS_FILE (file));
2211 iface = G_FILE_GET_IFACE (file);
2212 (* iface->replace_async) (file,
2223 * g_file_replace_finish:
2224 * @file: input #GFile
2225 * @res: a #GAsyncResult
2226 * @error: a #GError, or %NULL
2228 * Finishes an asynchronous file replace operation started with
2229 * g_file_replace_async().
2231 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2232 * Free the returned object with g_object_unref().
2235 g_file_replace_finish (GFile *file,
2241 g_return_val_if_fail (G_IS_FILE (file), NULL);
2242 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2244 if (g_async_result_legacy_propagate_error (res, error))
2247 iface = G_FILE_GET_IFACE (file);
2248 return (* iface->replace_finish) (file, res, error);
2252 * g_file_open_readwrite_async
2253 * @file: input #GFile
2254 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2256 * @cancellable: (allow-none): optional #GCancellable object,
2258 * @callback: (scope async): a #GAsyncReadyCallback to call
2259 * when the request is satisfied
2260 * @user_data: (closure): the data to pass to callback function
2262 * Asynchronously opens @file for reading and writing.
2264 * For more details, see g_file_open_readwrite() which is
2265 * the synchronous version of this call.
2267 * When the operation is finished, @callback will be called.
2268 * You can then call g_file_open_readwrite_finish() to get
2269 * the result of the operation.
2274 g_file_open_readwrite_async (GFile *file,
2276 GCancellable *cancellable,
2277 GAsyncReadyCallback callback,
2282 g_return_if_fail (G_IS_FILE (file));
2284 iface = G_FILE_GET_IFACE (file);
2285 (* iface->open_readwrite_async) (file,
2293 * g_file_open_readwrite_finish:
2294 * @file: input #GFile
2295 * @res: a #GAsyncResult
2296 * @error: a #GError, or %NULL
2298 * Finishes an asynchronous file read operation started with
2299 * g_file_open_readwrite_async().
2301 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2302 * Free the returned object with g_object_unref().
2307 g_file_open_readwrite_finish (GFile *file,
2313 g_return_val_if_fail (G_IS_FILE (file), NULL);
2314 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2316 if (g_async_result_legacy_propagate_error (res, error))
2319 iface = G_FILE_GET_IFACE (file);
2320 return (* iface->open_readwrite_finish) (file, res, error);
2324 * g_file_create_readwrite_async:
2325 * @file: input #GFile
2326 * @flags: a set of #GFileCreateFlags
2327 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2329 * @cancellable: (allow-none): optional #GCancellable object,
2331 * @callback: (scope async): a #GAsyncReadyCallback to call
2332 * when the request is satisfied
2333 * @user_data: (closure): the data to pass to callback function
2335 * Asynchronously creates a new file and returns a stream
2336 * for reading and writing to it. The file must not already exist.
2338 * For more details, see g_file_create_readwrite() which is
2339 * the synchronous version of this call.
2341 * When the operation is finished, @callback will be called.
2342 * You can then call g_file_create_readwrite_finish() to get
2343 * the result of the operation.
2348 g_file_create_readwrite_async (GFile *file,
2349 GFileCreateFlags flags,
2351 GCancellable *cancellable,
2352 GAsyncReadyCallback callback,
2357 g_return_if_fail (G_IS_FILE (file));
2359 iface = G_FILE_GET_IFACE (file);
2360 (* iface->create_readwrite_async) (file,
2369 * g_file_create_readwrite_finish:
2370 * @file: input #GFile
2371 * @res: a #GAsyncResult
2372 * @error: a #GError, or %NULL
2374 * Finishes an asynchronous file create operation started with
2375 * g_file_create_readwrite_async().
2377 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2378 * Free the returned object with g_object_unref().
2383 g_file_create_readwrite_finish (GFile *file,
2389 g_return_val_if_fail (G_IS_FILE (file), NULL);
2390 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2392 if (g_async_result_legacy_propagate_error (res, error))
2395 iface = G_FILE_GET_IFACE (file);
2396 return (* iface->create_readwrite_finish) (file, res, error);
2400 * g_file_replace_readwrite_async:
2401 * @file: input #GFile
2402 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2403 * for the current #GFile, or NULL to ignore
2404 * @make_backup: %TRUE if a backup should be created
2405 * @flags: a set of #GFileCreateFlags
2406 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2408 * @cancellable: (allow-none): optional #GCancellable object,
2410 * @callback: (scope async): a #GAsyncReadyCallback to call
2411 * when the request is satisfied
2412 * @user_data: (closure): the data to pass to callback function
2414 * Asynchronously overwrites the file in read-write mode,
2415 * replacing the contents, possibly creating a backup copy
2416 * of the file first.
2418 * For more details, see g_file_replace_readwrite() which is
2419 * the synchronous version of this call.
2421 * When the operation is finished, @callback will be called.
2422 * You can then call g_file_replace_readwrite_finish() to get
2423 * the result of the operation.
2428 g_file_replace_readwrite_async (GFile *file,
2430 gboolean make_backup,
2431 GFileCreateFlags flags,
2433 GCancellable *cancellable,
2434 GAsyncReadyCallback callback,
2439 g_return_if_fail (G_IS_FILE (file));
2441 iface = G_FILE_GET_IFACE (file);
2442 (* iface->replace_readwrite_async) (file,
2453 * g_file_replace_readwrite_finish:
2454 * @file: input #GFile
2455 * @res: a #GAsyncResult
2456 * @error: a #GError, or %NULL
2458 * Finishes an asynchronous file replace operation started with
2459 * g_file_replace_readwrite_async().
2461 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2462 * Free the returned object with g_object_unref().
2467 g_file_replace_readwrite_finish (GFile *file,
2473 g_return_val_if_fail (G_IS_FILE (file), NULL);
2474 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2476 if (g_async_result_legacy_propagate_error (res, error))
2479 iface = G_FILE_GET_IFACE (file);
2480 return (* iface->replace_readwrite_finish) (file, res, error);
2484 copy_symlink (GFile *destination,
2485 GFileCopyFlags flags,
2486 GCancellable *cancellable,
2491 gboolean tried_delete;
2493 GFileType file_type;
2495 tried_delete = FALSE;
2499 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2501 /* Maybe it already existed, and we want to overwrite? */
2502 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2503 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2505 g_clear_error (&my_error);
2507 /* Don't overwrite if the destination is a directory */
2508 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2509 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2510 cancellable, &my_error);
2513 file_type = g_file_info_get_file_type (info);
2514 g_object_unref (info);
2516 if (file_type == G_FILE_TYPE_DIRECTORY)
2518 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2519 _("Can't copy over directory"));
2524 if (!g_file_delete (destination, cancellable, error))
2527 tried_delete = TRUE;
2531 g_propagate_error (error, my_error);
2538 static GFileInputStream *
2539 open_source_for_copy (GFile *source,
2541 GFileCopyFlags flags,
2542 GCancellable *cancellable,
2546 GFileInputStream *ret;
2548 GFileType file_type;
2551 ret = g_file_read (source, cancellable, &my_error);
2555 /* There was an error opening the source, try to set a good error for it: */
2556 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2558 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2559 * as that is less useful to the app. Better check for errors on the
2562 g_error_free (my_error);
2565 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2566 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2567 cancellable, &my_error);
2569 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2571 file_type = g_file_info_get_file_type (info);
2572 g_object_unref (info);
2574 if (flags & G_FILE_COPY_OVERWRITE)
2576 if (file_type == G_FILE_TYPE_DIRECTORY)
2578 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2579 _("Can't copy directory over directory"));
2582 /* continue to would_recurse error */
2586 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2587 _("Target file exists"));
2593 /* Error getting info from target, return that error
2594 * (except for NOT_FOUND, which is no error here)
2596 g_clear_object (&info);
2597 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2599 g_propagate_error (error, my_error);
2602 g_clear_error (&my_error);
2605 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2606 _("Can't recursively copy directory"));
2610 g_propagate_error (error, my_error);
2615 should_copy (GFileAttributeInfo *info,
2616 gboolean copy_all_attributes,
2617 gboolean skip_perms)
2619 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2622 if (copy_all_attributes)
2623 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2624 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2628 build_attribute_list_for_copy (GFile *file,
2629 GFileCopyFlags flags,
2630 char **out_attributes,
2631 GCancellable *cancellable,
2634 gboolean ret = FALSE;
2635 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2639 gboolean copy_all_attributes;
2640 gboolean skip_perms;
2642 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2643 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2645 /* Ignore errors here, if the target supports no attributes there is
2646 * nothing to copy. We still honor the cancellable though.
2648 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2652 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2653 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2656 if (attributes == NULL && namespaces == NULL)
2660 s = g_string_new ("");
2664 for (i = 0; i < attributes->n_infos; i++)
2666 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2671 g_string_append_c (s, ',');
2673 g_string_append (s, attributes->infos[i].name);
2680 for (i = 0; i < namespaces->n_infos; i++)
2682 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2687 g_string_append_c (s, ',');
2689 g_string_append (s, namespaces->infos[i].name);
2690 g_string_append (s, "::*");
2696 *out_attributes = g_string_free (s, FALSE);
2700 g_string_free (s, TRUE);
2702 g_file_attribute_info_list_unref (attributes);
2704 g_file_attribute_info_list_unref (namespaces);
2710 * g_file_copy_attributes:
2711 * @source: a #GFile with attributes
2712 * @destination: a #GFile to copy attributes to
2713 * @flags: a set of #GFileCopyFlags
2714 * @cancellable: (allow-none): optional #GCancellable object,
2716 * @error: a #GError, %NULL to ignore
2718 * Copies the file attributes from @source to @destination.
2720 * Normally only a subset of the file attributes are copied,
2721 * those that are copies in a normal file copy operation
2722 * (which for instance does not include e.g. owner). However
2723 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2724 * all the metadata that is possible to copy is copied. This
2725 * is useful when implementing move by copy + delete source.
2727 * Returns: %TRUE if the attributes were copied successfully,
2731 g_file_copy_attributes (GFile *source,
2733 GFileCopyFlags flags,
2734 GCancellable *cancellable,
2737 char *attrs_to_read;
2740 gboolean source_nofollow_symlinks;
2742 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2743 cancellable, error))
2746 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2748 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2749 * we just don't copy it.
2751 info = g_file_query_info (source, attrs_to_read,
2752 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2756 g_free (attrs_to_read);
2761 res = g_file_set_attributes_from_info (destination,
2763 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2766 g_object_unref (info);
2773 copy_stream_with_progress (GInputStream *in,
2776 GCancellable *cancellable,
2777 GFileProgressCallback progress_callback,
2778 gpointer progress_callback_data,
2781 gssize n_read, n_written;
2782 goffset current_size;
2783 char buffer[1024*64], *p;
2789 /* avoid performance impact of querying total size when it's not needed */
2790 if (progress_callback)
2792 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2793 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2797 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2798 total_size = g_file_info_get_size (info);
2799 g_object_unref (info);
2802 if (total_size == -1)
2804 info = g_file_query_info (source,
2805 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2806 G_FILE_QUERY_INFO_NONE,
2810 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2811 total_size = g_file_info_get_size (info);
2812 g_object_unref (info);
2817 if (total_size == -1)
2824 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2834 current_size += n_read;
2839 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2840 if (n_written == -1)
2847 n_read -= n_written;
2853 if (progress_callback)
2854 progress_callback (current_size, total_size, progress_callback_data);
2857 /* Make sure we send full copied size */
2858 if (progress_callback)
2859 progress_callback (current_size, total_size, progress_callback_data);
2867 do_splice (int fd_in,
2872 long *bytes_transferd,
2878 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2886 else if (errsv == ENOSYS || errsv == EINVAL)
2887 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2888 _("Splice not supported"));
2890 g_set_error (error, G_IO_ERROR,
2891 g_io_error_from_errno (errsv),
2892 _("Error splicing file: %s"),
2893 g_strerror (errsv));
2898 *bytes_transferd = result;
2903 splice_stream_with_progress (GInputStream *in,
2905 GCancellable *cancellable,
2906 GFileProgressCallback progress_callback,
2907 gpointer progress_callback_data,
2910 int buffer[2] = { -1, -1 };
2917 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2918 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2920 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2924 /* avoid performance impact of querying total size when it's not needed */
2925 if (progress_callback)
2929 if (fstat (fd_in, &sbuf) == 0)
2930 total_size = sbuf.st_size;
2933 if (total_size == -1)
2936 offset_in = offset_out = 0;
2943 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2946 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2957 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2960 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2963 n_read -= n_written;
2966 if (progress_callback)
2967 progress_callback (offset_in, total_size, progress_callback_data);
2970 /* Make sure we send full copied size */
2971 if (progress_callback)
2972 progress_callback (offset_in, total_size, progress_callback_data);
2974 if (!g_close (buffer[0], error))
2977 if (!g_close (buffer[1], error))
2981 if (buffer[0] != -1)
2982 (void) g_close (buffer[0], NULL);
2983 if (buffer[1] != -1)
2984 (void) g_close (buffer[1], NULL);
2992 btrfs_reflink_with_progress (GInputStream *in,
2995 GCancellable *cancellable,
2996 GFileProgressCallback progress_callback,
2997 gpointer progress_callback_data,
3000 goffset source_size;
3004 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3005 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3007 if (progress_callback)
3008 source_size = g_file_info_get_size (info);
3010 /* Btrfs clone ioctl properties:
3011 * - Works at the inode level
3012 * - Doesn't work with directories
3013 * - Always follows symlinks (source and destination)
3015 * By the time we get here, *in and *out are both regular files */
3016 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3021 g_set_error_literal (error, G_IO_ERROR,
3022 G_IO_ERROR_NOT_SUPPORTED,
3023 _("Copy (reflink/clone) between mounts is not supported"));
3024 else if (errno == EINVAL)
3025 g_set_error_literal (error, G_IO_ERROR,
3026 G_IO_ERROR_NOT_SUPPORTED,
3027 _("Copy (reflink/clone) is not supported or invalid"));
3029 /* Most probably something odd happened; retry with fallback */
3030 g_set_error_literal (error, G_IO_ERROR,
3031 G_IO_ERROR_NOT_SUPPORTED,
3032 _("Copy (reflink/clone) is not supported or didn't work"));
3033 /* We retry with fallback for all error cases because Btrfs is currently
3034 * unstable, and so we can't trust it to do clone properly.
3035 * In addition, any hard errors here would cause the same failure in the
3036 * fallback manual copy as well. */
3040 /* Make sure we send full copied size */
3041 if (progress_callback)
3042 progress_callback (source_size, source_size, progress_callback_data);
3049 file_copy_fallback (GFile *source,
3051 GFileCopyFlags flags,
3052 GCancellable *cancellable,
3053 GFileProgressCallback progress_callback,
3054 gpointer progress_callback_data,
3057 gboolean ret = FALSE;
3058 GFileInputStream *file_in = NULL;
3059 GInputStream *in = NULL;
3060 GOutputStream *out = NULL;
3061 GFileInfo *info = NULL;
3063 char *attrs_to_read;
3064 gboolean do_set_attributes = FALSE;
3066 /* need to know the file type */
3067 info = g_file_query_info (source,
3068 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3069 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3075 /* Maybe copy the symlink? */
3076 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3077 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3079 target = g_file_info_get_symlink_target (info);
3082 if (!copy_symlink (destination, flags, cancellable, target, error))
3088 /* ... else fall back on a regular file copy */
3090 /* Handle "special" files (pipes, device nodes, ...)? */
3091 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3093 /* FIXME: could try to recreate device nodes and others? */
3094 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3095 _("Can't copy special file"));
3099 /* Everything else should just fall back on a regular copy. */
3101 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3104 in = G_INPUT_STREAM (file_in);
3106 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3107 cancellable, error))
3110 if (attrs_to_read != NULL)
3112 GError *tmp_error = NULL;
3114 /* Ok, ditch the previous lightweight info (on Unix we just
3115 * called lstat()); at this point we gather all the information
3116 * we need about the source from the opened file descriptor.
3118 g_object_unref (info);
3120 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3121 cancellable, &tmp_error);
3124 /* Not all gvfs backends implement query_info_on_read(), we
3125 * can just fall back to the pathname again.
3126 * https://bugzilla.gnome.org/706254
3128 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3130 g_clear_error (&tmp_error);
3131 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3132 cancellable, error);
3136 g_free (attrs_to_read);
3137 g_propagate_error (error, tmp_error);
3141 g_free (attrs_to_read);
3145 do_set_attributes = TRUE;
3148 /* In the local file path, we pass down the source info which
3149 * includes things like unix::mode, to ensure that the target file
3150 * is not created with different permissions from the source file.
3152 * If a future API like g_file_replace_with_info() is added, switch
3153 * this code to use that.
3155 if (G_IS_LOCAL_FILE (destination))
3157 if (flags & G_FILE_COPY_OVERWRITE)
3158 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3160 flags & G_FILE_COPY_BACKUP,
3161 G_FILE_CREATE_REPLACE_DESTINATION,
3163 cancellable, error);
3165 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3167 cancellable, error);
3169 else if (flags & G_FILE_COPY_OVERWRITE)
3171 out = (GOutputStream *)g_file_replace (destination,
3173 flags & G_FILE_COPY_BACKUP,
3174 G_FILE_CREATE_REPLACE_DESTINATION,
3175 cancellable, error);
3179 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3186 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3188 GError *reflink_err = NULL;
3190 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3191 progress_callback, progress_callback_data,
3194 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3196 g_clear_error (&reflink_err);
3200 g_propagate_error (error, reflink_err);
3213 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3215 GError *splice_err = NULL;
3217 if (!splice_stream_with_progress (in, out, cancellable,
3218 progress_callback, progress_callback_data,
3221 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3223 g_clear_error (&splice_err);
3227 g_propagate_error (error, splice_err);
3240 /* A plain read/write loop */
3241 if (!copy_stream_with_progress (in, out, source, cancellable,
3242 progress_callback, progress_callback_data,
3250 /* Don't care about errors in source here */
3251 (void) g_input_stream_close (in, cancellable, NULL);
3252 g_object_unref (in);
3257 /* But write errors on close are bad! */
3258 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3260 g_object_unref (out);
3263 /* Ignore errors here. Failure to copy metadata is not a hard error */
3264 /* TODO: set these attributes /before/ we do the rename() on Unix */
3265 if (ret && do_set_attributes)
3267 g_file_set_attributes_from_info (destination,
3269 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3274 g_clear_object (&info);
3281 * @source: input #GFile
3282 * @destination: destination #GFile
3283 * @flags: set of #GFileCopyFlags
3284 * @cancellable: (allow-none): optional #GCancellable object,
3286 * @progress_callback: (allow-none) (scope call): function to callback with
3287 * progress information, or %NULL if progress information is not needed
3288 * @progress_callback_data: (closure): user data to pass to @progress_callback
3289 * @error: #GError to set on error, or %NULL
3291 * Copies the file @source to the location specified by @destination.
3292 * Can not handle recursive copies of directories.
3294 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3295 * existing @destination file is overwritten.
3297 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3298 * will be copied as symlinks, otherwise the target of the
3299 * @source symlink will be copied.
3301 * If @cancellable is not %NULL, then the operation can be cancelled by
3302 * triggering the cancellable object from another thread. If the operation
3303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3305 * If @progress_callback is not %NULL, then the operation can be monitored
3306 * by setting this to a #GFileProgressCallback function.
3307 * @progress_callback_data will be passed to this function. It is guaranteed
3308 * that this callback will be called after all data has been transferred with
3309 * the total number of bytes copied during the operation.
3311 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3312 * is returned, independent on the status of the @destination.
3314 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3315 * the error %G_IO_ERROR_EXISTS is returned.
3317 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3318 * error is returned. If trying to overwrite a directory with a directory the
3319 * %G_IO_ERROR_WOULD_MERGE error is returned.
3321 * If the source is a directory and the target does not exist, or
3322 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3323 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3325 * If you are interested in copying the #GFile object itself (not the on-disk
3326 * file), see g_file_dup().
3328 * Returns: %TRUE on success, %FALSE otherwise.
3331 g_file_copy (GFile *source,
3333 GFileCopyFlags flags,
3334 GCancellable *cancellable,
3335 GFileProgressCallback progress_callback,
3336 gpointer progress_callback_data,
3343 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3344 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3346 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3349 iface = G_FILE_GET_IFACE (destination);
3353 res = (* iface->copy) (source, destination,
3355 progress_callback, progress_callback_data,
3361 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3363 g_propagate_error (error, my_error);
3367 g_clear_error (&my_error);
3370 /* If the types are different, and the destination method failed
3371 * also try the source method
3373 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3375 iface = G_FILE_GET_IFACE (source);
3380 res = (* iface->copy) (source, destination,
3382 progress_callback, progress_callback_data,
3388 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3390 g_propagate_error (error, my_error);
3394 g_clear_error (&my_error);
3398 return file_copy_fallback (source, destination, flags, cancellable,
3399 progress_callback, progress_callback_data,
3404 * g_file_copy_async: (skip)
3405 * @source: input #GFile
3406 * @destination: destination #GFile
3407 * @flags: set of #GFileCopyFlags
3408 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3410 * @cancellable: (allow-none): optional #GCancellable object,
3412 * @progress_callback: (allow-none): function to callback with progress
3413 * information, or %NULL if progress information is not needed
3414 * @progress_callback_data: (closure): user data to pass to @progress_callback
3415 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3416 * @user_data: the data to pass to callback function
3418 * Copies the file @source to the location specified by @destination
3419 * asynchronously. For details of the behaviour, see g_file_copy().
3421 * If @progress_callback is not %NULL, then that function that will be called
3422 * just like in g_file_copy(), however the callback will run in the main loop,
3423 * not in the thread that is doing the I/O operation.
3425 * When the operation is finished, @callback will be called. You can then call
3426 * g_file_copy_finish() to get the result of the operation.
3429 g_file_copy_async (GFile *source,
3431 GFileCopyFlags flags,
3433 GCancellable *cancellable,
3434 GFileProgressCallback progress_callback,
3435 gpointer progress_callback_data,
3436 GAsyncReadyCallback callback,
3441 g_return_if_fail (G_IS_FILE (source));
3442 g_return_if_fail (G_IS_FILE (destination));
3444 iface = G_FILE_GET_IFACE (source);
3445 (* iface->copy_async) (source,
3451 progress_callback_data,
3457 * g_file_copy_finish:
3458 * @file: input #GFile
3459 * @res: a #GAsyncResult
3460 * @error: a #GError, or %NULL
3462 * Finishes copying the file started with g_file_copy_async().
3464 * Returns: a %TRUE on success, %FALSE on error.
3467 g_file_copy_finish (GFile *file,
3473 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3474 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3476 if (g_async_result_legacy_propagate_error (res, error))
3479 iface = G_FILE_GET_IFACE (file);
3480 return (* iface->copy_finish) (file, res, error);
3485 * @source: #GFile pointing to the source location
3486 * @destination: #GFile pointing to the destination location
3487 * @flags: set of #GFileCopyFlags
3488 * @cancellable: (allow-none): optional #GCancellable object,
3490 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3491 * function for updates
3492 * @progress_callback_data: (closure): gpointer to user data for
3493 * the callback function
3494 * @error: #GError for returning error conditions, or %NULL
3496 * Tries to move the file or directory @source to the location specified
3497 * by @destination. If native move operations are supported then this is
3498 * used, otherwise a copy + delete fallback is used. The native
3499 * implementation may support moving directories (for instance on moves
3500 * inside the same filesystem), but the fallback code does not.
3502 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3503 * existing @destination file is overwritten.
3505 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3506 * will be copied as symlinks, otherwise the target of the
3507 * @source symlink will be copied.
3509 * If @cancellable is not %NULL, then the operation can be cancelled by
3510 * triggering the cancellable object from another thread. If the operation
3511 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3513 * If @progress_callback is not %NULL, then the operation can be monitored
3514 * by setting this to a #GFileProgressCallback function.
3515 * @progress_callback_data will be passed to this function. It is
3516 * guaranteed that this callback will be called after all data has been
3517 * transferred with the total number of bytes copied during the operation.
3519 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3520 * error is returned, independent on the status of the @destination.
3522 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3523 * then the error %G_IO_ERROR_EXISTS is returned.
3525 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3526 * error is returned. If trying to overwrite a directory with a directory the
3527 * %G_IO_ERROR_WOULD_MERGE error is returned.
3529 * If the source is a directory and the target does not exist, or
3530 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3531 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3532 * move operation isn't available).
3534 * Returns: %TRUE on successful move, %FALSE otherwise.
3537 g_file_move (GFile *source,
3539 GFileCopyFlags flags,
3540 GCancellable *cancellable,
3541 GFileProgressCallback progress_callback,
3542 gpointer progress_callback_data,
3549 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3550 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3552 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3555 iface = G_FILE_GET_IFACE (destination);
3559 res = (* iface->move) (source, destination,
3561 progress_callback, progress_callback_data,
3567 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3569 g_propagate_error (error, my_error);
3574 /* If the types are different, and the destination method failed
3575 * also try the source method
3577 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3579 iface = G_FILE_GET_IFACE (source);
3584 res = (* iface->move) (source, destination,
3586 progress_callback, progress_callback_data,
3592 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3594 g_propagate_error (error, my_error);
3600 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3602 g_set_error_literal (error, G_IO_ERROR,
3603 G_IO_ERROR_NOT_SUPPORTED,
3604 _("Operation not supported"));
3608 flags |= G_FILE_COPY_ALL_METADATA;
3609 if (!g_file_copy (source, destination, flags, cancellable,
3610 progress_callback, progress_callback_data,
3614 return g_file_delete (source, cancellable, error);
3618 * g_file_make_directory:
3619 * @file: input #GFile
3620 * @cancellable: (allow-none): optional #GCancellable object,
3622 * @error: a #GError, or %NULL
3624 * Creates a directory. Note that this will only create a child directory
3625 * of the immediate parent directory of the path or URI given by the #GFile.
3626 * To recursively create directories, see g_file_make_directory_with_parents().
3627 * This function will fail if the parent directory does not exist, setting
3628 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3629 * creating directories, this function will fail, setting @error to
3630 * %G_IO_ERROR_NOT_SUPPORTED.
3632 * For a local #GFile the newly created directory will have the default
3633 * (current) ownership and permissions of the current process.
3635 * If @cancellable is not %NULL, then the operation can be cancelled by
3636 * triggering the cancellable object from another thread. If the operation
3637 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3639 * Returns: %TRUE on successful creation, %FALSE otherwise.
3642 g_file_make_directory (GFile *file,
3643 GCancellable *cancellable,
3648 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3650 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3653 iface = G_FILE_GET_IFACE (file);
3655 if (iface->make_directory == NULL)
3657 g_set_error_literal (error, G_IO_ERROR,
3658 G_IO_ERROR_NOT_SUPPORTED,
3659 _("Operation not supported"));
3663 return (* iface->make_directory) (file, cancellable, error);
3667 * g_file_make_directory_async:
3668 * @file: input #GFile
3669 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3671 * @cancellable: (allow-none): optional #GCancellable object,
3673 * @callback: a #GAsyncReadyCallback to call
3674 * when the request is satisfied
3675 * @user_data: the data to pass to callback function
3677 * Asynchronously creates a directory.
3679 * Virtual: make_directory_async
3683 g_file_make_directory_async (GFile *file,
3685 GCancellable *cancellable,
3686 GAsyncReadyCallback callback,
3691 g_return_if_fail (G_IS_FILE (file));
3693 iface = G_FILE_GET_IFACE (file);
3694 (* iface->make_directory_async) (file,
3702 * g_file_make_directory_finish:
3703 * @file: input #GFile
3704 * @result: a #GAsyncResult
3705 * @error: a #GError, or %NULL
3707 * Finishes an asynchronous directory creation, started with
3708 * g_file_make_directory_async().
3710 * Virtual: make_directory_finish
3711 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3715 g_file_make_directory_finish (GFile *file,
3716 GAsyncResult *result,
3721 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3722 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3724 iface = G_FILE_GET_IFACE (file);
3725 return (* iface->make_directory_finish) (file, result, error);
3729 * g_file_make_directory_with_parents:
3730 * @file: input #GFile
3731 * @cancellable: (allow-none): optional #GCancellable object,
3733 * @error: a #GError, or %NULL
3735 * Creates a directory and any parent directories that may not
3736 * exist similar to 'mkdir -p'. If the file system does not support
3737 * creating directories, this function will fail, setting @error to
3738 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3739 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3740 * the similar g_mkdir_with_parents().
3742 * For a local #GFile the newly created directories will have the default
3743 * (current) ownership and permissions of the current process.
3745 * If @cancellable is not %NULL, then the operation can be cancelled by
3746 * triggering the cancellable object from another thread. If the operation
3747 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3749 * Returns: %TRUE if all directories have been successfully created, %FALSE
3755 g_file_make_directory_with_parents (GFile *file,
3756 GCancellable *cancellable,
3759 GFile *work_file = NULL;
3760 GList *list = NULL, *l;
3761 GError *my_error = NULL;
3763 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3765 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3768 g_file_make_directory (file, cancellable, &my_error);
3769 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3772 g_propagate_error (error, my_error);
3773 return my_error == NULL;
3776 work_file = g_object_ref (file);
3778 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3782 parent_file = g_file_get_parent (work_file);
3783 if (parent_file == NULL)
3786 g_clear_error (&my_error);
3787 g_file_make_directory (parent_file, cancellable, &my_error);
3789 g_object_unref (work_file);
3790 work_file = g_object_ref (parent_file);
3792 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3793 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3795 g_object_unref (parent_file);
3798 for (l = list; my_error == NULL && l; l = l->next)
3800 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3804 g_object_unref (work_file);
3807 while (list != NULL)
3809 g_object_unref ((GFile *) list->data);
3810 list = g_list_remove (list, list->data);
3813 if (my_error != NULL)
3815 g_propagate_error (error, my_error);
3819 return g_file_make_directory (file, cancellable, error);
3823 * g_file_make_symbolic_link:
3824 * @file: a #GFile with the name of the symlink to create
3825 * @symlink_value: a string with the path for the target of the new symlink
3826 * @cancellable: (allow-none): optional #GCancellable object,
3830 * Creates a symbolic link named @file which contains the string
3833 * If @cancellable is not %NULL, then the operation can be cancelled by
3834 * triggering the cancellable object from another thread. If the operation
3835 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3837 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3840 g_file_make_symbolic_link (GFile *file,
3841 const char *symlink_value,
3842 GCancellable *cancellable,
3847 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3848 g_return_val_if_fail (symlink_value != NULL, FALSE);
3850 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3853 if (*symlink_value == '\0')
3855 g_set_error_literal (error, G_IO_ERROR,
3856 G_IO_ERROR_INVALID_ARGUMENT,
3857 _("Invalid symlink value given"));
3861 iface = G_FILE_GET_IFACE (file);
3863 if (iface->make_symbolic_link == NULL)
3865 g_set_error_literal (error, G_IO_ERROR,
3866 G_IO_ERROR_NOT_SUPPORTED,
3867 _("Operation not supported"));
3871 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3876 * @file: input #GFile
3877 * @cancellable: (allow-none): optional #GCancellable object,
3879 * @error: a #GError, or %NULL
3881 * Deletes a file. If the @file is a directory, it will only be
3882 * deleted if it is empty. This has the same semantics as g_unlink().
3884 * If @cancellable is not %NULL, then the operation can be cancelled by
3885 * triggering the cancellable object from another thread. If the operation
3886 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3888 * Virtual: delete_file
3889 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3892 g_file_delete (GFile *file,
3893 GCancellable *cancellable,
3898 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3900 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3903 iface = G_FILE_GET_IFACE (file);
3905 if (iface->delete_file == NULL)
3907 g_set_error_literal (error, G_IO_ERROR,
3908 G_IO_ERROR_NOT_SUPPORTED,
3909 _("Operation not supported"));
3913 return (* iface->delete_file) (file, cancellable, error);
3917 * g_file_delete_async:
3918 * @file: input #GFile
3919 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3921 * @cancellable: (allow-none): optional #GCancellable object,
3923 * @callback: a #GAsyncReadyCallback to call
3924 * when the request is satisfied
3925 * @user_data: the data to pass to callback function
3927 * Asynchronously delete a file. If the @file is a directory, it will
3928 * only be deleted if it is empty. This has the same semantics as
3931 * Virtual: delete_file_async
3935 g_file_delete_async (GFile *file,
3937 GCancellable *cancellable,
3938 GAsyncReadyCallback callback,
3943 g_return_if_fail (G_IS_FILE (file));
3945 iface = G_FILE_GET_IFACE (file);
3946 (* iface->delete_file_async) (file,
3954 * g_file_delete_finish:
3955 * @file: input #GFile
3956 * @result: a #GAsyncResult
3957 * @error: a #GError, or %NULL
3959 * Finishes deleting a file started with g_file_delete_async().
3961 * Virtual: delete_file_finish
3962 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3966 g_file_delete_finish (GFile *file,
3967 GAsyncResult *result,
3972 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3973 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3975 if (g_async_result_legacy_propagate_error (result, error))
3978 iface = G_FILE_GET_IFACE (file);
3979 return (* iface->delete_file_finish) (file, result, error);
3984 * @file: #GFile to send to trash
3985 * @cancellable: (allow-none): optional #GCancellable object,
3987 * @error: a #GError, or %NULL
3989 * Sends @file to the "Trashcan", if possible. This is similar to
3990 * deleting it, but the user can recover it before emptying the trashcan.
3991 * Not all file systems support trashing, so this call can return the
3992 * %G_IO_ERROR_NOT_SUPPORTED error.
3994 * If @cancellable is not %NULL, then the operation can be cancelled by
3995 * triggering the cancellable object from another thread. If the operation
3996 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3999 * Returns: %TRUE on successful trash, %FALSE otherwise.
4002 g_file_trash (GFile *file,
4003 GCancellable *cancellable,
4008 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4010 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4013 iface = G_FILE_GET_IFACE (file);
4015 if (iface->trash == NULL)
4017 g_set_error_literal (error,
4018 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4019 _("Trash not supported"));
4023 return (* iface->trash) (file, cancellable, error);
4027 * g_file_trash_async:
4028 * @file: input #GFile
4029 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4031 * @cancellable: (allow-none): optional #GCancellable object,
4033 * @callback: a #GAsyncReadyCallback to call
4034 * when the request is satisfied
4035 * @user_data: the data to pass to callback function
4037 * Asynchronously sends @file to the Trash location, if possible.
4039 * Virtual: trash_async
4043 g_file_trash_async (GFile *file,
4045 GCancellable *cancellable,
4046 GAsyncReadyCallback callback,
4051 g_return_if_fail (G_IS_FILE (file));
4053 iface = G_FILE_GET_IFACE (file);
4054 (* iface->trash_async) (file,
4062 * g_file_trash_finish:
4063 * @file: input #GFile
4064 * @result: a #GAsyncResult
4065 * @error: a #GError, or %NULL
4067 * Finishes an asynchronous file trashing operation, started with
4068 * g_file_trash_async().
4070 * Virtual: trash_finish
4071 * Returns: %TRUE on successful trash, %FALSE otherwise.
4075 g_file_trash_finish (GFile *file,
4076 GAsyncResult *result,
4081 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4082 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4084 iface = G_FILE_GET_IFACE (file);
4085 return (* iface->trash_finish) (file, result, error);
4089 * g_file_set_display_name:
4090 * @file: input #GFile
4091 * @display_name: a string
4092 * @cancellable: (allow-none): optional #GCancellable object,
4094 * @error: a #GError, or %NULL
4096 * Renames @file to the specified display name.
4098 * The display name is converted from UTF-8 to the correct encoding
4099 * for the target filesystem if possible and the @file is renamed to this.
4101 * If you want to implement a rename operation in the user interface the
4102 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4103 * initial value in the rename widget, and then the result after editing
4104 * should be passed to g_file_set_display_name().
4106 * On success the resulting converted filename is returned.
4108 * If @cancellable is not %NULL, then the operation can be cancelled by
4109 * triggering the cancellable object from another thread. If the operation
4110 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4112 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4113 * or %NULL if there was an error.
4114 * Free the returned object with g_object_unref().
4117 g_file_set_display_name (GFile *file,
4118 const gchar *display_name,
4119 GCancellable *cancellable,
4124 g_return_val_if_fail (G_IS_FILE (file), NULL);
4125 g_return_val_if_fail (display_name != NULL, NULL);
4127 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4131 G_IO_ERROR_INVALID_ARGUMENT,
4132 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4136 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4139 iface = G_FILE_GET_IFACE (file);
4141 return (* iface->set_display_name) (file, display_name, cancellable, error);
4145 * g_file_set_display_name_async:
4146 * @file: input #GFile
4147 * @display_name: a string
4148 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4150 * @cancellable: (allow-none): optional #GCancellable object,
4152 * @callback: (scope async): a #GAsyncReadyCallback to call
4153 * when the request is satisfied
4154 * @user_data: (closure): the data to pass to callback function
4156 * Asynchronously sets the display name for a given #GFile.
4158 * For more details, see g_file_set_display_name() which is
4159 * the synchronous version of this call.
4161 * When the operation is finished, @callback will be called.
4162 * You can then call g_file_set_display_name_finish() to get
4163 * the result of the operation.
4166 g_file_set_display_name_async (GFile *file,
4167 const gchar *display_name,
4169 GCancellable *cancellable,
4170 GAsyncReadyCallback callback,
4175 g_return_if_fail (G_IS_FILE (file));
4176 g_return_if_fail (display_name != NULL);
4178 iface = G_FILE_GET_IFACE (file);
4179 (* iface->set_display_name_async) (file,
4188 * g_file_set_display_name_finish:
4189 * @file: input #GFile
4190 * @res: a #GAsyncResult
4191 * @error: a #GError, or %NULL
4193 * Finishes setting a display name started with
4194 * g_file_set_display_name_async().
4196 * Returns: (transfer full): a #GFile or %NULL on error.
4197 * Free the returned object with g_object_unref().
4200 g_file_set_display_name_finish (GFile *file,
4206 g_return_val_if_fail (G_IS_FILE (file), NULL);
4207 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4209 if (g_async_result_legacy_propagate_error (res, error))
4212 iface = G_FILE_GET_IFACE (file);
4213 return (* iface->set_display_name_finish) (file, res, error);
4217 * g_file_query_settable_attributes:
4218 * @file: input #GFile
4219 * @cancellable: (allow-none): optional #GCancellable object,
4221 * @error: a #GError, or %NULL
4223 * Obtain the list of settable attributes for the file.
4225 * Returns the type and full attribute name of all the attributes
4226 * that can be set on this file. This doesn't mean setting it will
4227 * always succeed though, you might get an access failure, or some
4228 * specific file may not support a specific attribute.
4230 * If @cancellable is not %NULL, then the operation can be cancelled by
4231 * triggering the cancellable object from another thread. If the operation
4232 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4234 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4235 * When you are done with it, release it with
4236 * g_file_attribute_info_list_unref()
4238 GFileAttributeInfoList *
4239 g_file_query_settable_attributes (GFile *file,
4240 GCancellable *cancellable,
4245 GFileAttributeInfoList *list;
4247 g_return_val_if_fail (G_IS_FILE (file), NULL);
4249 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4252 iface = G_FILE_GET_IFACE (file);
4254 if (iface->query_settable_attributes == NULL)
4255 return g_file_attribute_info_list_new ();
4258 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4262 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4264 list = g_file_attribute_info_list_new ();
4265 g_error_free (my_error);
4268 g_propagate_error (error, my_error);
4275 * g_file_query_writable_namespaces:
4276 * @file: input #GFile
4277 * @cancellable: (allow-none): optional #GCancellable object,
4279 * @error: a #GError, or %NULL
4281 * Obtain the list of attribute namespaces where new attributes
4282 * can be created by a user. An example of this is extended
4283 * attributes (in the "xattr" namespace).
4285 * If @cancellable is not %NULL, then the operation can be cancelled by
4286 * triggering the cancellable object from another thread. If the operation
4287 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4289 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4290 * When you are done with it, release it with
4291 * g_file_attribute_info_list_unref()
4293 GFileAttributeInfoList *
4294 g_file_query_writable_namespaces (GFile *file,
4295 GCancellable *cancellable,
4300 GFileAttributeInfoList *list;
4302 g_return_val_if_fail (G_IS_FILE (file), NULL);
4304 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4307 iface = G_FILE_GET_IFACE (file);
4309 if (iface->query_writable_namespaces == NULL)
4310 return g_file_attribute_info_list_new ();
4313 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4317 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4319 list = g_file_attribute_info_list_new ();
4320 g_error_free (my_error);
4323 g_propagate_error (error, my_error);
4330 * g_file_set_attribute:
4331 * @file: input #GFile
4332 * @attribute: a string containing the attribute's name
4333 * @type: The type of the attribute
4334 * @value_p: (allow-none): a pointer to the value (or the pointer
4335 * itself if the type is a pointer type)
4336 * @flags: a set of #GFileQueryInfoFlags
4337 * @cancellable: (allow-none): optional #GCancellable object,
4339 * @error: a #GError, or %NULL
4341 * Sets an attribute in the file with attribute name @attribute to @value.
4343 * Some attributes can be unset by setting @attribute to
4344 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4346 * If @cancellable is not %NULL, then the operation can be cancelled by
4347 * triggering the cancellable object from another thread. If the operation
4348 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4350 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4353 g_file_set_attribute (GFile *file,
4354 const gchar *attribute,
4355 GFileAttributeType type,
4357 GFileQueryInfoFlags flags,
4358 GCancellable *cancellable,
4363 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4364 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4366 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4369 iface = G_FILE_GET_IFACE (file);
4371 if (iface->set_attribute == NULL)
4373 g_set_error_literal (error, G_IO_ERROR,
4374 G_IO_ERROR_NOT_SUPPORTED,
4375 _("Operation not supported"));
4379 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4383 * g_file_set_attributes_from_info:
4384 * @file: input #GFile
4385 * @info: a #GFileInfo
4386 * @flags: #GFileQueryInfoFlags
4387 * @cancellable: (allow-none): optional #GCancellable object,
4389 * @error: a #GError, or %NULL
4391 * Tries to set all attributes in the #GFileInfo on the target
4392 * values, not stopping on the first error.
4394 * If there is any error during this operation then @error will
4395 * be set to the first error. Error on particular fields are flagged
4396 * by setting the "status" field in the attribute value to
4397 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4398 * also detect further errors.
4400 * If @cancellable is not %NULL, then the operation can be cancelled by
4401 * triggering the cancellable object from another thread. If the operation
4402 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4404 * Returns: %FALSE if there was any error, %TRUE otherwise.
4407 g_file_set_attributes_from_info (GFile *file,
4409 GFileQueryInfoFlags flags,
4410 GCancellable *cancellable,
4415 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4416 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4418 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4421 g_file_info_clear_status (info);
4423 iface = G_FILE_GET_IFACE (file);
4425 return (* iface->set_attributes_from_info) (file,
4433 g_file_real_set_attributes_from_info (GFile *file,
4435 GFileQueryInfoFlags flags,
4436 GCancellable *cancellable,
4442 GFileAttributeValue *value;
4446 attributes = g_file_info_list_attributes (info, NULL);
4448 for (i = 0; attributes[i] != NULL; i++)
4450 value = _g_file_info_get_attribute_value (info, attributes[i]);
4452 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4455 if (!g_file_set_attribute (file, attributes[i],
4456 value->type, _g_file_attribute_value_peek_as_pointer (value),
4457 flags, cancellable, error))
4459 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4461 /* Don't set error multiple times */
4465 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4468 g_strfreev (attributes);
4474 * g_file_set_attributes_async:
4475 * @file: input #GFile
4476 * @info: a #GFileInfo
4477 * @flags: a #GFileQueryInfoFlags
4478 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4480 * @cancellable: (allow-none): optional #GCancellable object,
4482 * @callback: (scope async): a #GAsyncReadyCallback
4483 * @user_data: (closure): a #gpointer
4485 * Asynchronously sets the attributes of @file with @info.
4487 * For more details, see g_file_set_attributes_from_info(),
4488 * which is the synchronous version of this call.
4490 * When the operation is finished, @callback will be called.
4491 * You can then call g_file_set_attributes_finish() to get
4492 * the result of the operation.
4495 g_file_set_attributes_async (GFile *file,
4497 GFileQueryInfoFlags flags,
4499 GCancellable *cancellable,
4500 GAsyncReadyCallback callback,
4505 g_return_if_fail (G_IS_FILE (file));
4506 g_return_if_fail (G_IS_FILE_INFO (info));
4508 iface = G_FILE_GET_IFACE (file);
4509 (* iface->set_attributes_async) (file,
4519 * g_file_set_attributes_finish:
4520 * @file: input #GFile
4521 * @result: a #GAsyncResult
4522 * @info: (out) (transfer full): a #GFileInfo
4523 * @error: a #GError, or %NULL
4525 * Finishes setting an attribute started in g_file_set_attributes_async().
4527 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4530 g_file_set_attributes_finish (GFile *file,
4531 GAsyncResult *result,
4537 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4538 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4540 /* No standard handling of errors here, as we must set info even
4543 iface = G_FILE_GET_IFACE (file);
4544 return (* iface->set_attributes_finish) (file, result, info, error);
4548 * g_file_set_attribute_string:
4549 * @file: input #GFile
4550 * @attribute: a string containing the attribute's name
4551 * @value: a string containing the attribute's value
4552 * @flags: #GFileQueryInfoFlags
4553 * @cancellable: (allow-none): optional #GCancellable object,
4555 * @error: a #GError, or %NULL
4557 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4558 * If @attribute is of a different type, this operation will fail.
4560 * If @cancellable is not %NULL, then the operation can be cancelled by
4561 * triggering the cancellable object from another thread. If the operation
4562 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4564 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4567 g_file_set_attribute_string (GFile *file,
4568 const char *attribute,
4570 GFileQueryInfoFlags flags,
4571 GCancellable *cancellable,
4574 return g_file_set_attribute (file, attribute,
4575 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4576 flags, cancellable, error);
4580 * g_file_set_attribute_byte_string:
4581 * @file: input #GFile
4582 * @attribute: a string containing the attribute's name
4583 * @value: a string containing the attribute's new value
4584 * @flags: a #GFileQueryInfoFlags
4585 * @cancellable: (allow-none): optional #GCancellable object,
4587 * @error: a #GError, or %NULL
4589 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4590 * If @attribute is of a different type, this operation will fail,
4593 * If @cancellable is not %NULL, then the operation can be cancelled by
4594 * triggering the cancellable object from another thread. If the operation
4595 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4597 * Returns: %TRUE if the @attribute was successfully set to @value
4598 * in the @file, %FALSE otherwise.
4601 g_file_set_attribute_byte_string (GFile *file,
4602 const gchar *attribute,
4604 GFileQueryInfoFlags flags,
4605 GCancellable *cancellable,
4608 return g_file_set_attribute (file, attribute,
4609 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4610 flags, cancellable, error);
4614 * g_file_set_attribute_uint32:
4615 * @file: input #GFile
4616 * @attribute: a string containing the attribute's name
4617 * @value: a #guint32 containing the attribute's new value
4618 * @flags: a #GFileQueryInfoFlags
4619 * @cancellable: (allow-none): optional #GCancellable object,
4621 * @error: a #GError, or %NULL
4623 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4624 * If @attribute is of a different type, this operation will fail.
4626 * If @cancellable is not %NULL, then the operation can be cancelled by
4627 * triggering the cancellable object from another thread. If the operation
4628 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4630 * Returns: %TRUE if the @attribute was successfully set to @value
4631 * in the @file, %FALSE otherwise.
4634 g_file_set_attribute_uint32 (GFile *file,
4635 const gchar *attribute,
4637 GFileQueryInfoFlags flags,
4638 GCancellable *cancellable,
4641 return g_file_set_attribute (file, attribute,
4642 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4643 flags, cancellable, error);
4647 * g_file_set_attribute_int32:
4648 * @file: input #GFile
4649 * @attribute: a string containing the attribute's name
4650 * @value: a #gint32 containing the attribute's new value
4651 * @flags: a #GFileQueryInfoFlags
4652 * @cancellable: (allow-none): optional #GCancellable object,
4654 * @error: a #GError, or %NULL
4656 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4657 * If @attribute is of a different type, this operation will fail.
4659 * If @cancellable is not %NULL, then the operation can be cancelled by
4660 * triggering the cancellable object from another thread. If the operation
4661 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4663 * Returns: %TRUE if the @attribute was successfully set to @value
4664 * in the @file, %FALSE otherwise.
4667 g_file_set_attribute_int32 (GFile *file,
4668 const gchar *attribute,
4670 GFileQueryInfoFlags flags,
4671 GCancellable *cancellable,
4674 return g_file_set_attribute (file, attribute,
4675 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4676 flags, cancellable, error);
4680 * g_file_set_attribute_uint64:
4681 * @file: input #GFile
4682 * @attribute: a string containing the attribute's name
4683 * @value: a #guint64 containing the attribute's new value
4684 * @flags: a #GFileQueryInfoFlags
4685 * @cancellable: (allow-none): optional #GCancellable object,
4687 * @error: a #GError, or %NULL
4689 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4690 * If @attribute is of a different type, this operation will fail.
4692 * If @cancellable is not %NULL, then the operation can be cancelled by
4693 * triggering the cancellable object from another thread. If the operation
4694 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4696 * Returns: %TRUE if the @attribute was successfully set to @value
4697 * in the @file, %FALSE otherwise.
4700 g_file_set_attribute_uint64 (GFile *file,
4701 const gchar *attribute,
4703 GFileQueryInfoFlags flags,
4704 GCancellable *cancellable,
4707 return g_file_set_attribute (file, attribute,
4708 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4709 flags, cancellable, error);
4713 * g_file_set_attribute_int64:
4714 * @file: input #GFile
4715 * @attribute: a string containing the attribute's name
4716 * @value: a #guint64 containing the attribute's new value
4717 * @flags: a #GFileQueryInfoFlags
4718 * @cancellable: (allow-none): optional #GCancellable object,
4720 * @error: a #GError, or %NULL
4722 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4723 * If @attribute is of a different type, this operation will fail.
4725 * If @cancellable is not %NULL, then the operation can be cancelled by
4726 * triggering the cancellable object from another thread. If the operation
4727 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4729 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4732 g_file_set_attribute_int64 (GFile *file,
4733 const gchar *attribute,
4735 GFileQueryInfoFlags flags,
4736 GCancellable *cancellable,
4739 return g_file_set_attribute (file, attribute,
4740 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4741 flags, cancellable, error);
4745 * g_file_mount_mountable:
4746 * @file: input #GFile
4747 * @flags: flags affecting the operation
4748 * @mount_operation: (allow-none): a #GMountOperation,
4749 * or %NULL to avoid user interaction
4750 * @cancellable: (allow-none): optional #GCancellable object,
4752 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4753 * when the request is satisfied, or %NULL
4754 * @user_data: (closure): the data to pass to callback function
4756 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4757 * Using @mount_operation, you can request callbacks when, for instance,
4758 * passwords are needed during authentication.
4760 * If @cancellable is not %NULL, then the operation can be cancelled by
4761 * triggering the cancellable object from another thread. If the operation
4762 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4764 * When the operation is finished, @callback will be called.
4765 * You can then call g_file_mount_mountable_finish() to get
4766 * the result of the operation.
4769 g_file_mount_mountable (GFile *file,
4770 GMountMountFlags flags,
4771 GMountOperation *mount_operation,
4772 GCancellable *cancellable,
4773 GAsyncReadyCallback callback,
4778 g_return_if_fail (G_IS_FILE (file));
4780 iface = G_FILE_GET_IFACE (file);
4782 if (iface->mount_mountable == NULL)
4784 g_task_report_new_error (file, callback, user_data,
4785 g_file_mount_mountable,
4786 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4787 _("Operation not supported"));
4791 (* iface->mount_mountable) (file,
4800 * g_file_mount_mountable_finish:
4801 * @file: input #GFile
4802 * @result: a #GAsyncResult
4803 * @error: a #GError, or %NULL
4805 * Finishes a mount operation. See g_file_mount_mountable() for details.
4807 * Finish an asynchronous mount operation that was started
4808 * with g_file_mount_mountable().
4810 * Returns: (transfer full): a #GFile or %NULL on error.
4811 * Free the returned object with g_object_unref().
4814 g_file_mount_mountable_finish (GFile *file,
4815 GAsyncResult *result,
4820 g_return_val_if_fail (G_IS_FILE (file), NULL);
4821 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4823 if (g_async_result_legacy_propagate_error (result, error))
4825 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4826 return g_task_propagate_pointer (G_TASK (result), error);
4828 iface = G_FILE_GET_IFACE (file);
4829 return (* iface->mount_mountable_finish) (file, result, error);
4833 * g_file_unmount_mountable:
4834 * @file: input #GFile
4835 * @flags: flags affecting the operation
4836 * @cancellable: (allow-none): optional #GCancellable object,
4838 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4839 * when the request is satisfied, or %NULL
4840 * @user_data: (closure): the data to pass to callback function
4842 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
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 * When the operation is finished, @callback will be called.
4849 * You can then call g_file_unmount_mountable_finish() to get
4850 * the result of the operation.
4852 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4855 g_file_unmount_mountable (GFile *file,
4856 GMountUnmountFlags flags,
4857 GCancellable *cancellable,
4858 GAsyncReadyCallback callback,
4863 g_return_if_fail (G_IS_FILE (file));
4865 iface = G_FILE_GET_IFACE (file);
4867 if (iface->unmount_mountable == NULL)
4869 g_task_report_new_error (file, callback, user_data,
4870 g_file_unmount_mountable_with_operation,
4871 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4872 _("Operation not supported"));
4876 (* iface->unmount_mountable) (file,
4884 * g_file_unmount_mountable_finish:
4885 * @file: input #GFile
4886 * @result: a #GAsyncResult
4887 * @error: a #GError, or %NULL
4889 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4891 * Finish an asynchronous unmount operation that was started
4892 * with g_file_unmount_mountable().
4894 * Returns: %TRUE if the operation finished successfully.
4897 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4901 g_file_unmount_mountable_finish (GFile *file,
4902 GAsyncResult *result,
4907 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4908 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4910 if (g_async_result_legacy_propagate_error (result, error))
4912 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4913 return g_task_propagate_boolean (G_TASK (result), error);
4915 iface = G_FILE_GET_IFACE (file);
4916 return (* iface->unmount_mountable_finish) (file, result, error);
4920 * g_file_unmount_mountable_with_operation:
4921 * @file: input #GFile
4922 * @flags: flags affecting the operation
4923 * @mount_operation: (allow-none): a #GMountOperation,
4924 * or %NULL to avoid user interaction
4925 * @cancellable: (allow-none): optional #GCancellable object,
4927 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4928 * when the request is satisfied, or %NULL
4929 * @user_data: (closure): the data to pass to callback function
4931 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4933 * If @cancellable is not %NULL, then the operation can be cancelled by
4934 * triggering the cancellable object from another thread. If the operation
4935 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4937 * When the operation is finished, @callback will be called.
4938 * You can then call g_file_unmount_mountable_finish() to get
4939 * the result of the operation.
4944 g_file_unmount_mountable_with_operation (GFile *file,
4945 GMountUnmountFlags flags,
4946 GMountOperation *mount_operation,
4947 GCancellable *cancellable,
4948 GAsyncReadyCallback callback,
4953 g_return_if_fail (G_IS_FILE (file));
4955 iface = G_FILE_GET_IFACE (file);
4957 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4959 g_task_report_new_error (file, callback, user_data,
4960 g_file_unmount_mountable_with_operation,
4961 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4962 _("Operation not supported"));
4966 if (iface->unmount_mountable_with_operation != NULL)
4967 (* iface->unmount_mountable_with_operation) (file,
4974 (* iface->unmount_mountable) (file,
4982 * g_file_unmount_mountable_with_operation_finish:
4983 * @file: input #GFile
4984 * @result: a #GAsyncResult
4985 * @error: a #GError, or %NULL
4987 * Finishes an unmount operation,
4988 * see g_file_unmount_mountable_with_operation() for details.
4990 * Finish an asynchronous unmount operation that was started
4991 * with g_file_unmount_mountable_with_operation().
4993 * Returns: %TRUE if the operation finished successfully.
4999 g_file_unmount_mountable_with_operation_finish (GFile *file,
5000 GAsyncResult *result,
5005 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5006 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5008 if (g_async_result_legacy_propagate_error (result, error))
5010 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5011 return g_task_propagate_boolean (G_TASK (result), error);
5013 iface = G_FILE_GET_IFACE (file);
5014 if (iface->unmount_mountable_with_operation_finish != NULL)
5015 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5017 return (* iface->unmount_mountable_finish) (file, result, error);
5021 * g_file_eject_mountable:
5022 * @file: input #GFile
5023 * @flags: flags affecting the operation
5024 * @cancellable: (allow-none): optional #GCancellable object,
5026 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5027 * when the request is satisfied, or %NULL
5028 * @user_data: (closure): the data to pass to callback function
5030 * Starts an asynchronous eject on a mountable.
5031 * When this operation has completed, @callback will be called with
5032 * @user_user data, and the operation can be finalized with
5033 * g_file_eject_mountable_finish().
5035 * If @cancellable is not %NULL, then the operation can be cancelled by
5036 * triggering the cancellable object from another thread. If the operation
5037 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5039 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5042 g_file_eject_mountable (GFile *file,
5043 GMountUnmountFlags flags,
5044 GCancellable *cancellable,
5045 GAsyncReadyCallback callback,
5050 g_return_if_fail (G_IS_FILE (file));
5052 iface = G_FILE_GET_IFACE (file);
5054 if (iface->eject_mountable == NULL)
5056 g_task_report_new_error (file, callback, user_data,
5057 g_file_eject_mountable_with_operation,
5058 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5059 _("Operation not supported"));
5063 (* iface->eject_mountable) (file,
5071 * g_file_eject_mountable_finish:
5072 * @file: input #GFile
5073 * @result: a #GAsyncResult
5074 * @error: a #GError, or %NULL
5076 * Finishes an asynchronous eject operation started by
5077 * g_file_eject_mountable().
5079 * Returns: %TRUE if the @file was ejected successfully.
5082 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5086 g_file_eject_mountable_finish (GFile *file,
5087 GAsyncResult *result,
5092 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5093 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5095 if (g_async_result_legacy_propagate_error (result, error))
5097 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5098 return g_task_propagate_boolean (G_TASK (result), error);
5100 iface = G_FILE_GET_IFACE (file);
5101 return (* iface->eject_mountable_finish) (file, result, error);
5105 * g_file_eject_mountable_with_operation:
5106 * @file: input #GFile
5107 * @flags: flags affecting the operation
5108 * @mount_operation: (allow-none): a #GMountOperation,
5109 * or %NULL to avoid user interaction
5110 * @cancellable: (allow-none): optional #GCancellable object,
5112 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5113 * when the request is satisfied, or %NULL
5114 * @user_data: (closure): the data to pass to callback function
5116 * Starts an asynchronous eject on a mountable.
5117 * When this operation has completed, @callback will be called with
5118 * @user_user data, and the operation can be finalized with
5119 * g_file_eject_mountable_with_operation_finish().
5121 * If @cancellable is not %NULL, then the operation can be cancelled by
5122 * triggering the cancellable object from another thread. If the operation
5123 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5128 g_file_eject_mountable_with_operation (GFile *file,
5129 GMountUnmountFlags flags,
5130 GMountOperation *mount_operation,
5131 GCancellable *cancellable,
5132 GAsyncReadyCallback callback,
5137 g_return_if_fail (G_IS_FILE (file));
5139 iface = G_FILE_GET_IFACE (file);
5141 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5143 g_task_report_new_error (file, callback, user_data,
5144 g_file_eject_mountable_with_operation,
5145 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5146 _("Operation not supported"));
5150 if (iface->eject_mountable_with_operation != NULL)
5151 (* iface->eject_mountable_with_operation) (file,
5158 (* iface->eject_mountable) (file,
5166 * g_file_eject_mountable_with_operation_finish:
5167 * @file: input #GFile
5168 * @result: a #GAsyncResult
5169 * @error: a #GError, or %NULL
5171 * Finishes an asynchronous eject operation started by
5172 * g_file_eject_mountable_with_operation().
5174 * Returns: %TRUE if the @file was ejected successfully.
5180 g_file_eject_mountable_with_operation_finish (GFile *file,
5181 GAsyncResult *result,
5186 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5187 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5189 if (g_async_result_legacy_propagate_error (result, error))
5191 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5192 return g_task_propagate_boolean (G_TASK (result), error);
5194 iface = G_FILE_GET_IFACE (file);
5195 if (iface->eject_mountable_with_operation_finish != NULL)
5196 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5198 return (* iface->eject_mountable_finish) (file, result, error);
5202 * g_file_monitor_directory:
5203 * @file: input #GFile
5204 * @flags: a set of #GFileMonitorFlags
5205 * @cancellable: (allow-none): optional #GCancellable object,
5207 * @error: a #GError, or %NULL
5209 * Obtains a directory monitor for the given file.
5210 * This may fail if directory monitoring is not supported.
5212 * If @cancellable is not %NULL, then the operation can be cancelled by
5213 * triggering the cancellable object from another thread. If the operation
5214 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5216 * It does not make sense for @flags to contain
5217 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5218 * directories. It is not possible to monitor all the files in a
5219 * directory for changes made via hard links; if you want to do this then
5220 * you must register individual watches with g_file_monitor().
5222 * Virtual: monitor_dir
5223 * Returns: (transfer full): a #GFileMonitor for the given @file,
5224 * or %NULL on error.
5225 * Free the returned object with g_object_unref().
5228 g_file_monitor_directory (GFile *file,
5229 GFileMonitorFlags flags,
5230 GCancellable *cancellable,
5235 g_return_val_if_fail (G_IS_FILE (file), NULL);
5236 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5238 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5241 iface = G_FILE_GET_IFACE (file);
5243 if (iface->monitor_dir == NULL)
5245 g_set_error_literal (error, G_IO_ERROR,
5246 G_IO_ERROR_NOT_SUPPORTED,
5247 _("Operation not supported"));
5251 return (* iface->monitor_dir) (file, flags, cancellable, error);
5255 * g_file_monitor_file:
5256 * @file: input #GFile
5257 * @flags: a set of #GFileMonitorFlags
5258 * @cancellable: (allow-none): optional #GCancellable object,
5260 * @error: a #GError, or %NULL
5262 * Obtains a file monitor for the given file. If no file notification
5263 * mechanism exists, then regular polling of the file is used.
5265 * If @cancellable is not %NULL, then the operation can be cancelled by
5266 * triggering the cancellable object from another thread. If the operation
5267 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5269 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5270 * will also attempt to report changes made to the file via another
5271 * filename (ie, a hard link). Without this flag, you can only rely on
5272 * changes made through the filename contained in @file to be
5273 * reported. Using this flag may result in an increase in resource
5274 * usage, and may not have any effect depending on the #GFileMonitor
5275 * backend and/or filesystem type.
5277 * Returns: (transfer full): a #GFileMonitor for the given @file,
5278 * or %NULL on error.
5279 * Free the returned object with g_object_unref().
5282 g_file_monitor_file (GFile *file,
5283 GFileMonitorFlags flags,
5284 GCancellable *cancellable,
5288 GFileMonitor *monitor;
5290 g_return_val_if_fail (G_IS_FILE (file), NULL);
5292 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5295 iface = G_FILE_GET_IFACE (file);
5299 if (iface->monitor_file)
5300 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5302 /* Fallback to polling */
5303 if (monitor == NULL)
5304 monitor = _g_poll_file_monitor_new (file);
5311 * @file: input #GFile
5312 * @flags: a set of #GFileMonitorFlags
5313 * @cancellable: (allow-none): optional #GCancellable object,
5315 * @error: a #GError, or %NULL
5317 * Obtains a file or directory monitor for the given file,
5318 * depending on the type of the file.
5320 * If @cancellable is not %NULL, then the operation can be cancelled by
5321 * triggering the cancellable object from another thread. If the operation
5322 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5324 * Returns: (transfer full): a #GFileMonitor for the given @file,
5325 * or %NULL on error.
5326 * Free the returned object with g_object_unref().
5331 g_file_monitor (GFile *file,
5332 GFileMonitorFlags flags,
5333 GCancellable *cancellable,
5336 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5337 return g_file_monitor_directory (file,
5338 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5339 cancellable, error);
5341 return g_file_monitor_file (file, flags, cancellable, error);
5344 /********************************************
5345 * Default implementation of async ops *
5346 ********************************************/
5350 GFileQueryInfoFlags flags;
5351 } QueryInfoAsyncData;
5354 query_info_data_free (QueryInfoAsyncData *data)
5356 g_free (data->attributes);
5361 query_info_async_thread (GTask *task,
5364 GCancellable *cancellable)
5366 QueryInfoAsyncData *data = task_data;
5368 GError *error = NULL;
5370 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5372 g_task_return_pointer (task, info, g_object_unref);
5374 g_task_return_error (task, error);
5378 g_file_real_query_info_async (GFile *file,
5379 const char *attributes,
5380 GFileQueryInfoFlags flags,
5382 GCancellable *cancellable,
5383 GAsyncReadyCallback callback,
5387 QueryInfoAsyncData *data;
5389 data = g_new0 (QueryInfoAsyncData, 1);
5390 data->attributes = g_strdup (attributes);
5391 data->flags = flags;
5393 task = g_task_new (file, cancellable, callback, user_data);
5394 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5395 g_task_set_priority (task, io_priority);
5396 g_task_run_in_thread (task, query_info_async_thread);
5397 g_object_unref (task);
5401 g_file_real_query_info_finish (GFile *file,
5405 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5407 return g_task_propagate_pointer (G_TASK (res), error);
5411 query_filesystem_info_async_thread (GTask *task,
5414 GCancellable *cancellable)
5416 const char *attributes = task_data;
5418 GError *error = NULL;
5420 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5422 g_task_return_pointer (task, info, g_object_unref);
5424 g_task_return_error (task, error);
5428 g_file_real_query_filesystem_info_async (GFile *file,
5429 const char *attributes,
5431 GCancellable *cancellable,
5432 GAsyncReadyCallback callback,
5437 task = g_task_new (file, cancellable, callback, user_data);
5438 g_task_set_task_data (task, g_strdup (attributes), g_free);
5439 g_task_set_priority (task, io_priority);
5440 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5441 g_object_unref (task);
5445 g_file_real_query_filesystem_info_finish (GFile *file,
5449 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5451 return g_task_propagate_pointer (G_TASK (res), error);
5455 enumerate_children_async_thread (GTask *task,
5458 GCancellable *cancellable)
5460 QueryInfoAsyncData *data = task_data;
5461 GFileEnumerator *enumerator;
5462 GError *error = NULL;
5464 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5466 g_task_return_error (task, error);
5468 g_task_return_pointer (task, enumerator, g_object_unref);
5472 g_file_real_enumerate_children_async (GFile *file,
5473 const char *attributes,
5474 GFileQueryInfoFlags flags,
5476 GCancellable *cancellable,
5477 GAsyncReadyCallback callback,
5481 QueryInfoAsyncData *data;
5483 data = g_new0 (QueryInfoAsyncData, 1);
5484 data->attributes = g_strdup (attributes);
5485 data->flags = flags;
5487 task = g_task_new (file, cancellable, callback, user_data);
5488 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5489 g_task_set_priority (task, io_priority);
5490 g_task_run_in_thread (task, enumerate_children_async_thread);
5491 g_object_unref (task);
5494 static GFileEnumerator *
5495 g_file_real_enumerate_children_finish (GFile *file,
5499 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5501 return g_task_propagate_pointer (G_TASK (res), error);
5505 open_read_async_thread (GTask *task,
5508 GCancellable *cancellable)
5510 GFileInputStream *stream;
5511 GError *error = NULL;
5513 stream = g_file_read (G_FILE (object), cancellable, &error);
5515 g_task_return_pointer (task, stream, g_object_unref);
5517 g_task_return_error (task, error);
5521 g_file_real_read_async (GFile *file,
5523 GCancellable *cancellable,
5524 GAsyncReadyCallback callback,
5529 task = g_task_new (file, cancellable, callback, user_data);
5530 g_task_set_priority (task, io_priority);
5531 g_task_run_in_thread (task, open_read_async_thread);
5532 g_object_unref (task);
5535 static GFileInputStream *
5536 g_file_real_read_finish (GFile *file,
5540 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5542 return g_task_propagate_pointer (G_TASK (res), error);
5546 append_to_async_thread (GTask *task,
5547 gpointer source_object,
5549 GCancellable *cancellable)
5551 GFileCreateFlags *data = task_data;
5552 GFileOutputStream *stream;
5553 GError *error = NULL;
5555 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5557 g_task_return_pointer (task, stream, g_object_unref);
5559 g_task_return_error (task, error);
5563 g_file_real_append_to_async (GFile *file,
5564 GFileCreateFlags flags,
5566 GCancellable *cancellable,
5567 GAsyncReadyCallback callback,
5570 GFileCreateFlags *data;
5573 data = g_new0 (GFileCreateFlags, 1);
5576 task = g_task_new (file, cancellable, callback, user_data);
5577 g_task_set_task_data (task, data, g_free);
5578 g_task_set_priority (task, io_priority);
5580 g_task_run_in_thread (task, append_to_async_thread);
5581 g_object_unref (task);
5584 static GFileOutputStream *
5585 g_file_real_append_to_finish (GFile *file,
5589 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5591 return g_task_propagate_pointer (G_TASK (res), error);
5595 create_async_thread (GTask *task,
5596 gpointer source_object,
5598 GCancellable *cancellable)
5600 GFileCreateFlags *data = task_data;
5601 GFileOutputStream *stream;
5602 GError *error = NULL;
5604 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5606 g_task_return_pointer (task, stream, g_object_unref);
5608 g_task_return_error (task, error);
5612 g_file_real_create_async (GFile *file,
5613 GFileCreateFlags flags,
5615 GCancellable *cancellable,
5616 GAsyncReadyCallback callback,
5619 GFileCreateFlags *data;
5622 data = g_new0 (GFileCreateFlags, 1);
5625 task = g_task_new (file, cancellable, callback, user_data);
5626 g_task_set_task_data (task, data, g_free);
5627 g_task_set_priority (task, io_priority);
5629 g_task_run_in_thread (task, create_async_thread);
5630 g_object_unref (task);
5633 static GFileOutputStream *
5634 g_file_real_create_finish (GFile *file,
5638 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5640 return g_task_propagate_pointer (G_TASK (res), error);
5644 GFileOutputStream *stream;
5646 gboolean make_backup;
5647 GFileCreateFlags flags;
5651 replace_async_data_free (ReplaceAsyncData *data)
5654 g_object_unref (data->stream);
5655 g_free (data->etag);
5660 replace_async_thread (GTask *task,
5661 gpointer source_object,
5663 GCancellable *cancellable)
5665 GFileOutputStream *stream;
5666 ReplaceAsyncData *data = task_data;
5667 GError *error = NULL;
5669 stream = g_file_replace (G_FILE (source_object),
5677 g_task_return_pointer (task, stream, g_object_unref);
5679 g_task_return_error (task, error);
5683 g_file_real_replace_async (GFile *file,
5685 gboolean make_backup,
5686 GFileCreateFlags flags,
5688 GCancellable *cancellable,
5689 GAsyncReadyCallback callback,
5693 ReplaceAsyncData *data;
5695 data = g_new0 (ReplaceAsyncData, 1);
5696 data->etag = g_strdup (etag);
5697 data->make_backup = make_backup;
5698 data->flags = flags;
5700 task = g_task_new (file, cancellable, callback, user_data);
5701 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5702 g_task_set_priority (task, io_priority);
5704 g_task_run_in_thread (task, replace_async_thread);
5705 g_object_unref (task);
5708 static GFileOutputStream *
5709 g_file_real_replace_finish (GFile *file,
5713 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5715 return g_task_propagate_pointer (G_TASK (res), error);
5719 delete_async_thread (GTask *task,
5722 GCancellable *cancellable)
5724 GError *error = NULL;
5726 if (g_file_delete (G_FILE (object), cancellable, &error))
5727 g_task_return_boolean (task, TRUE);
5729 g_task_return_error (task, error);
5733 g_file_real_delete_async (GFile *file,
5735 GCancellable *cancellable,
5736 GAsyncReadyCallback callback,
5741 task = g_task_new (file, cancellable, callback, user_data);
5742 g_task_set_priority (task, io_priority);
5743 g_task_run_in_thread (task, delete_async_thread);
5744 g_object_unref (task);
5748 g_file_real_delete_finish (GFile *file,
5752 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5754 return g_task_propagate_boolean (G_TASK (res), error);
5758 trash_async_thread (GTask *task,
5761 GCancellable *cancellable)
5763 GError *error = NULL;
5765 if (g_file_trash (G_FILE (object), cancellable, &error))
5766 g_task_return_boolean (task, TRUE);
5768 g_task_return_error (task, error);
5772 g_file_real_trash_async (GFile *file,
5774 GCancellable *cancellable,
5775 GAsyncReadyCallback callback,
5780 task = g_task_new (file, cancellable, callback, user_data);
5781 g_task_set_priority (task, io_priority);
5782 g_task_run_in_thread (task, trash_async_thread);
5783 g_object_unref (task);
5787 g_file_real_trash_finish (GFile *file,
5791 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5793 return g_task_propagate_boolean (G_TASK (res), error);
5797 make_directory_async_thread (GTask *task,
5800 GCancellable *cancellable)
5802 GError *error = NULL;
5804 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5805 g_task_return_boolean (task, TRUE);
5807 g_task_return_error (task, error);
5811 g_file_real_make_directory_async (GFile *file,
5813 GCancellable *cancellable,
5814 GAsyncReadyCallback callback,
5819 task = g_task_new (file, cancellable, callback, user_data);
5820 g_task_set_priority (task, io_priority);
5821 g_task_run_in_thread (task, make_directory_async_thread);
5822 g_object_unref (task);
5826 g_file_real_make_directory_finish (GFile *file,
5830 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5832 return g_task_propagate_boolean (G_TASK (res), error);
5836 open_readwrite_async_thread (GTask *task,
5839 GCancellable *cancellable)
5841 GFileIOStream *stream;
5842 GError *error = NULL;
5844 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5847 g_task_return_error (task, error);
5849 g_task_return_pointer (task, stream, g_object_unref);
5853 g_file_real_open_readwrite_async (GFile *file,
5855 GCancellable *cancellable,
5856 GAsyncReadyCallback callback,
5861 task = g_task_new (file, cancellable, callback, user_data);
5862 g_task_set_priority (task, io_priority);
5864 g_task_run_in_thread (task, open_readwrite_async_thread);
5865 g_object_unref (task);
5868 static GFileIOStream *
5869 g_file_real_open_readwrite_finish (GFile *file,
5873 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5875 return g_task_propagate_pointer (G_TASK (res), error);
5879 create_readwrite_async_thread (GTask *task,
5882 GCancellable *cancellable)
5884 GFileCreateFlags *data = task_data;
5885 GFileIOStream *stream;
5886 GError *error = NULL;
5888 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5891 g_task_return_error (task, error);
5893 g_task_return_pointer (task, stream, g_object_unref);
5897 g_file_real_create_readwrite_async (GFile *file,
5898 GFileCreateFlags flags,
5900 GCancellable *cancellable,
5901 GAsyncReadyCallback callback,
5904 GFileCreateFlags *data;
5907 data = g_new0 (GFileCreateFlags, 1);
5910 task = g_task_new (file, cancellable, callback, user_data);
5911 g_task_set_task_data (task, data, g_free);
5912 g_task_set_priority (task, io_priority);
5914 g_task_run_in_thread (task, create_readwrite_async_thread);
5915 g_object_unref (task);
5918 static GFileIOStream *
5919 g_file_real_create_readwrite_finish (GFile *file,
5923 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5925 return g_task_propagate_pointer (G_TASK (res), error);
5930 gboolean make_backup;
5931 GFileCreateFlags flags;
5932 } ReplaceRWAsyncData;
5935 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5937 g_free (data->etag);
5942 replace_readwrite_async_thread (GTask *task,
5945 GCancellable *cancellable)
5947 GFileIOStream *stream;
5948 GError *error = NULL;
5949 ReplaceRWAsyncData *data = task_data;
5951 stream = g_file_replace_readwrite (G_FILE (object),
5959 g_task_return_error (task, error);
5961 g_task_return_pointer (task, stream, g_object_unref);
5965 g_file_real_replace_readwrite_async (GFile *file,
5967 gboolean make_backup,
5968 GFileCreateFlags flags,
5970 GCancellable *cancellable,
5971 GAsyncReadyCallback callback,
5975 ReplaceRWAsyncData *data;
5977 data = g_new0 (ReplaceRWAsyncData, 1);
5978 data->etag = g_strdup (etag);
5979 data->make_backup = make_backup;
5980 data->flags = flags;
5982 task = g_task_new (file, cancellable, callback, user_data);
5983 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5984 g_task_set_priority (task, io_priority);
5986 g_task_run_in_thread (task, replace_readwrite_async_thread);
5987 g_object_unref (task);
5990 static GFileIOStream *
5991 g_file_real_replace_readwrite_finish (GFile *file,
5995 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5997 return g_task_propagate_pointer (G_TASK (res), error);
6001 set_display_name_async_thread (GTask *task,
6004 GCancellable *cancellable)
6006 GError *error = NULL;
6007 char *name = task_data;
6010 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6013 g_task_return_error (task, error);
6015 g_task_return_pointer (task, file, g_object_unref);
6019 g_file_real_set_display_name_async (GFile *file,
6020 const char *display_name,
6022 GCancellable *cancellable,
6023 GAsyncReadyCallback callback,
6028 task = g_task_new (file, cancellable, callback, user_data);
6029 g_task_set_task_data (task, g_strdup (display_name), g_free);
6030 g_task_set_priority (task, io_priority);
6032 g_task_run_in_thread (task, set_display_name_async_thread);
6033 g_object_unref (task);
6037 g_file_real_set_display_name_finish (GFile *file,
6041 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6043 return g_task_propagate_pointer (G_TASK (res), error);
6047 GFileQueryInfoFlags flags;
6054 set_info_data_free (SetInfoAsyncData *data)
6057 g_object_unref (data->info);
6059 g_error_free (data->error);
6064 set_info_async_thread (GTask *task,
6067 GCancellable *cancellable)
6069 SetInfoAsyncData *data = task_data;
6072 data->res = g_file_set_attributes_from_info (G_FILE (object),
6080 g_file_real_set_attributes_async (GFile *file,
6082 GFileQueryInfoFlags flags,
6084 GCancellable *cancellable,
6085 GAsyncReadyCallback callback,
6089 SetInfoAsyncData *data;
6091 data = g_new0 (SetInfoAsyncData, 1);
6092 data->info = g_file_info_dup (info);
6093 data->flags = flags;
6095 task = g_task_new (file, cancellable, callback, user_data);
6096 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6097 g_task_set_priority (task, io_priority);
6099 g_task_run_in_thread (task, set_info_async_thread);
6100 g_object_unref (task);
6104 g_file_real_set_attributes_finish (GFile *file,
6109 SetInfoAsyncData *data;
6111 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6113 data = g_task_get_task_data (G_TASK (res));
6116 *info = g_object_ref (data->info);
6118 if (error != NULL && data->error)
6119 *error = g_error_copy (data->error);
6125 find_enclosing_mount_async_thread (GTask *task,
6128 GCancellable *cancellable)
6130 GError *error = NULL;
6133 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6136 g_task_return_error (task, error);
6138 g_task_return_pointer (task, mount, g_object_unref);
6142 g_file_real_find_enclosing_mount_async (GFile *file,
6144 GCancellable *cancellable,
6145 GAsyncReadyCallback callback,
6150 task = g_task_new (file, cancellable, callback, user_data);
6151 g_task_set_priority (task, io_priority);
6153 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6154 g_object_unref (task);
6158 g_file_real_find_enclosing_mount_finish (GFile *file,
6162 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6164 return g_task_propagate_pointer (G_TASK (res), error);
6171 GFileCopyFlags flags;
6172 GFileProgressCallback progress_cb;
6173 gpointer progress_cb_data;
6177 copy_async_data_free (CopyAsyncData *data)
6179 g_object_unref (data->source);
6180 g_object_unref (data->destination);
6181 g_slice_free (CopyAsyncData, data);
6185 CopyAsyncData *data;
6186 goffset current_num_bytes;
6187 goffset total_num_bytes;
6191 copy_async_progress_in_main (gpointer user_data)
6193 ProgressData *progress = user_data;
6194 CopyAsyncData *data = progress->data;
6196 data->progress_cb (progress->current_num_bytes,
6197 progress->total_num_bytes,
6198 data->progress_cb_data);
6204 copy_async_progress_callback (goffset current_num_bytes,
6205 goffset total_num_bytes,
6208 GTask *task = user_data;
6209 CopyAsyncData *data = g_task_get_task_data (task);
6210 ProgressData *progress;
6212 progress = g_new (ProgressData, 1);
6213 progress->data = data;
6214 progress->current_num_bytes = current_num_bytes;
6215 progress->total_num_bytes = total_num_bytes;
6217 g_main_context_invoke_full (g_task_get_context (task),
6218 g_task_get_priority (task),
6219 copy_async_progress_in_main,
6225 copy_async_thread (GTask *task,
6228 GCancellable *cancellable)
6230 CopyAsyncData *data = task_data;
6232 GError *error = NULL;
6234 result = g_file_copy (data->source,
6238 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6242 g_task_return_boolean (task, TRUE);
6244 g_task_return_error (task, error);
6248 g_file_real_copy_async (GFile *source,
6250 GFileCopyFlags flags,
6252 GCancellable *cancellable,
6253 GFileProgressCallback progress_callback,
6254 gpointer progress_callback_data,
6255 GAsyncReadyCallback callback,
6259 CopyAsyncData *data;
6261 data = g_slice_new (CopyAsyncData);
6262 data->source = g_object_ref (source);
6263 data->destination = g_object_ref (destination);
6264 data->flags = flags;
6265 data->progress_cb = progress_callback;
6266 data->progress_cb_data = progress_callback_data;
6268 task = g_task_new (source, cancellable, callback, user_data);
6269 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6270 g_task_set_priority (task, io_priority);
6271 g_task_run_in_thread (task, copy_async_thread);
6272 g_object_unref (task);
6276 g_file_real_copy_finish (GFile *file,
6280 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6282 return g_task_propagate_boolean (G_TASK (res), error);
6286 /********************************************
6287 * Default VFS operations *
6288 ********************************************/
6291 * g_file_new_for_path:
6292 * @path: a string containing a relative or absolute path.
6293 * The string must be encoded in the glib filename encoding.
6295 * Constructs a #GFile for a given path. This operation never
6296 * fails, but the returned object might not support any I/O
6297 * operation if @path is malformed.
6299 * Returns: (transfer full): a new #GFile for the given @path.
6300 * Free the returned object with g_object_unref().
6303 g_file_new_for_path (const char *path)
6305 g_return_val_if_fail (path != NULL, NULL);
6307 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6311 * g_file_new_for_uri:
6312 * @uri: a UTF-8 string containing a URI
6314 * Constructs a #GFile for a given URI. This operation never
6315 * fails, but the returned object might not support any I/O
6316 * operation if @uri is malformed or if the uri type is
6319 * Returns: (transfer full): a new #GFile for the given @uri.
6320 * Free the returned object with g_object_unref().
6323 g_file_new_for_uri (const char *uri)
6325 g_return_val_if_fail (uri != NULL, NULL);
6327 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6332 * @tmpl: (type filename) (allow-none): Template for the file
6333 * name, as in g_file_open_tmp(), or %NULL for a default template
6334 * @iostream: (out): on return, a #GFileIOStream for the created file
6335 * @error: a #GError, or %NULL
6337 * Opens a file in the preferred directory for temporary files (as
6338 * returned by g_get_tmp_dir()) and returns a #GFile and
6339 * #GFileIOStream pointing to it.
6341 * @tmpl should be a string in the GLib file name encoding
6342 * containing a sequence of six 'X' characters, and containing no
6343 * directory components. If it is %NULL, a default template is used.
6345 * Unlike the other #GFile constructors, this will return %NULL if
6346 * a temporary file could not be created.
6348 * Returns: (transfer full): a new #GFile.
6349 * Free the returned object with g_object_unref().
6354 g_file_new_tmp (const char *tmpl,
6355 GFileIOStream **iostream,
6361 GFileOutputStream *output;
6363 g_return_val_if_fail (iostream != NULL, NULL);
6365 fd = g_file_open_tmp (tmpl, &path, error);
6369 file = g_file_new_for_path (path);
6371 output = _g_local_file_output_stream_new (fd);
6372 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6374 g_object_unref (output);
6381 * g_file_parse_name:
6382 * @parse_name: a file name or path to be parsed
6384 * Constructs a #GFile with the given @parse_name (i.e. something
6385 * given by g_file_get_parse_name()). This operation never fails,
6386 * but the returned object might not support any I/O operation if
6387 * the @parse_name cannot be parsed.
6389 * Returns: (transfer full): a new #GFile.
6392 g_file_parse_name (const char *parse_name)
6394 g_return_val_if_fail (parse_name != NULL, NULL);
6396 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6400 is_valid_scheme_character (char c)
6402 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6405 /* Following RFC 2396, valid schemes are built like:
6406 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6409 has_valid_scheme (const char *uri)
6415 if (!g_ascii_isalpha (*p))
6420 } while (is_valid_scheme_character (*p));
6426 new_for_cmdline_arg (const gchar *arg,
6432 if (g_path_is_absolute (arg))
6433 return g_file_new_for_path (arg);
6435 if (has_valid_scheme (arg))
6436 return g_file_new_for_uri (arg);
6442 current_dir = g_get_current_dir ();
6443 filename = g_build_filename (current_dir, arg, NULL);
6444 g_free (current_dir);
6447 filename = g_build_filename (cwd, arg, NULL);
6449 file = g_file_new_for_path (filename);
6456 * g_file_new_for_commandline_arg:
6457 * @arg: a command line string
6459 * Creates a #GFile with the given argument from the command line.
6460 * The value of @arg can be either a URI, an absolute path or a
6461 * relative path resolved relative to the current working directory.
6462 * This operation never fails, but the returned object might not
6463 * support any I/O operation if @arg points to a malformed path.
6465 * Returns: (transfer full): a new #GFile.
6466 * Free the returned object with g_object_unref().
6469 g_file_new_for_commandline_arg (const char *arg)
6471 g_return_val_if_fail (arg != NULL, NULL);
6473 return new_for_cmdline_arg (arg, NULL);
6477 * g_file_new_for_commandline_arg_and_cwd:
6478 * @arg: a command line string
6479 * @cwd: the current working directory of the commandline
6481 * Creates a #GFile with the given argument from the command line.
6483 * This function is similar to g_file_new_for_commandline_arg() except
6484 * that it allows for passing the current working directory as an
6485 * argument instead of using the current working directory of the
6488 * This is useful if the commandline argument was given in a context
6489 * other than the invocation of the current process.
6491 * See also g_application_command_line_create_file_for_arg().
6493 * Returns: (transfer full): a new #GFile
6498 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6501 g_return_val_if_fail (arg != NULL, NULL);
6502 g_return_val_if_fail (cwd != NULL, NULL);
6504 return new_for_cmdline_arg (arg, cwd);
6508 * g_file_mount_enclosing_volume:
6509 * @location: input #GFile
6510 * @flags: flags affecting the operation
6511 * @mount_operation: (allow-none): a #GMountOperation
6512 * or %NULL to avoid user interaction
6513 * @cancellable: (allow-none): optional #GCancellable object,
6515 * @callback: (allow-none): a #GAsyncReadyCallback to call
6516 * when the request is satisfied, or %NULL
6517 * @user_data: the data to pass to callback function
6519 * Starts a @mount_operation, mounting the volume that contains
6520 * the file @location.
6522 * When this operation has completed, @callback will be called with
6523 * @user_user data, and the operation can be finalized with
6524 * g_file_mount_enclosing_volume_finish().
6526 * If @cancellable is not %NULL, then the operation can be cancelled by
6527 * triggering the cancellable object from another thread. If the operation
6528 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6531 g_file_mount_enclosing_volume (GFile *location,
6532 GMountMountFlags flags,
6533 GMountOperation *mount_operation,
6534 GCancellable *cancellable,
6535 GAsyncReadyCallback callback,
6540 g_return_if_fail (G_IS_FILE (location));
6542 iface = G_FILE_GET_IFACE (location);
6544 if (iface->mount_enclosing_volume == NULL)
6546 g_task_report_new_error (location, callback, user_data,
6547 g_file_mount_enclosing_volume,
6548 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6549 _("volume doesn't implement mount"));
6553 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6558 * g_file_mount_enclosing_volume_finish:
6559 * @location: input #GFile
6560 * @result: a #GAsyncResult
6561 * @error: a #GError, or %NULL
6563 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6565 * Returns: %TRUE if successful. If an error has occurred,
6566 * this function will return %FALSE and set @error
6567 * appropriately if present.
6570 g_file_mount_enclosing_volume_finish (GFile *location,
6571 GAsyncResult *result,
6576 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6577 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6579 if (g_async_result_legacy_propagate_error (result, error))
6581 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6582 return g_task_propagate_boolean (G_TASK (result), error);
6584 iface = G_FILE_GET_IFACE (location);
6586 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6589 /********************************************
6590 * Utility functions *
6591 ********************************************/
6594 * g_file_query_default_handler:
6595 * @file: a #GFile to open
6596 * @cancellable: optional #GCancellable object, %NULL to ignore
6597 * @error: a #GError, or %NULL
6599 * Returns the #GAppInfo that is registered as the default
6600 * application to handle the file specified by @file.
6602 * If @cancellable is not %NULL, then the operation can be cancelled by
6603 * triggering the cancellable object from another thread. If the operation
6604 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6606 * Returns: (transfer full): a #GAppInfo if the handle was found,
6607 * %NULL if there were errors.
6608 * When you are done with it, release it with g_object_unref()
6611 g_file_query_default_handler (GFile *file,
6612 GCancellable *cancellable,
6616 const char *content_type;
6621 uri_scheme = g_file_get_uri_scheme (file);
6622 if (uri_scheme && uri_scheme[0] != '\0')
6624 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6625 g_free (uri_scheme);
6627 if (appinfo != NULL)
6631 info = g_file_query_info (file,
6632 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6641 content_type = g_file_info_get_content_type (info);
6644 /* Don't use is_native(), as we want to support fuse paths if available */
6645 path = g_file_get_path (file);
6646 appinfo = g_app_info_get_default_for_type (content_type,
6651 g_object_unref (info);
6653 if (appinfo != NULL)
6656 g_set_error_literal (error, G_IO_ERROR,
6657 G_IO_ERROR_NOT_SUPPORTED,
6658 _("No application is registered as handling this file"));
6662 #define GET_CONTENT_BLOCK_SIZE 8192
6665 * g_file_load_contents:
6666 * @file: input #GFile
6667 * @cancellable: optional #GCancellable object, %NULL to ignore
6668 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6669 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6670 * or %NULL if the length is not needed
6671 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6672 * or %NULL if the entity tag is not needed
6673 * @error: a #GError, or %NULL
6675 * Loads the content of the file into memory. The data is always
6676 * zero-terminated, but this is not included in the resultant @length.
6677 * The returned @content should be freed with g_free() when no longer
6680 * If @cancellable is not %NULL, then the operation can be cancelled by
6681 * triggering the cancellable object from another thread. If the operation
6682 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6684 * Returns: %TRUE if the @file's contents were successfully loaded.
6685 * %FALSE if there were errors.
6688 g_file_load_contents (GFile *file,
6689 GCancellable *cancellable,
6695 GFileInputStream *in;
6696 GByteArray *content;
6701 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6702 g_return_val_if_fail (contents != NULL, FALSE);
6704 in = g_file_read (file, cancellable, error);
6708 content = g_byte_array_new ();
6711 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6712 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6713 content->data + pos,
6714 GET_CONTENT_BLOCK_SIZE,
6715 cancellable, error)) > 0)
6718 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6725 info = g_file_input_stream_query_info (in,
6726 G_FILE_ATTRIBUTE_ETAG_VALUE,
6731 *etag_out = g_strdup (g_file_info_get_etag (info));
6732 g_object_unref (info);
6736 /* Ignore errors on close */
6737 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6738 g_object_unref (in);
6742 /* error is set already */
6743 g_byte_array_free (content, TRUE);
6750 /* Zero terminate (we got an extra byte allocated for this */
6751 content->data[pos] = 0;
6753 *contents = (char *)g_byte_array_free (content, FALSE);
6760 GFileReadMoreCallback read_more_callback;
6761 GByteArray *content;
6768 load_contents_data_free (LoadContentsData *data)
6771 g_byte_array_free (data->content, TRUE);
6772 g_free (data->etag);
6777 load_contents_close_callback (GObject *obj,
6778 GAsyncResult *close_res,
6781 GInputStream *stream = G_INPUT_STREAM (obj);
6782 LoadContentsData *data = user_data;
6784 /* Ignore errors here, we're only reading anyway */
6785 g_input_stream_close_finish (stream, close_res, NULL);
6786 g_object_unref (stream);
6788 g_task_return_boolean (data->task, TRUE);
6789 g_object_unref (data->task);
6793 load_contents_fstat_callback (GObject *obj,
6794 GAsyncResult *stat_res,
6797 GInputStream *stream = G_INPUT_STREAM (obj);
6798 LoadContentsData *data = user_data;
6801 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6805 data->etag = g_strdup (g_file_info_get_etag (info));
6806 g_object_unref (info);
6809 g_input_stream_close_async (stream, 0,
6810 g_task_get_cancellable (data->task),
6811 load_contents_close_callback, data);
6815 load_contents_read_callback (GObject *obj,
6816 GAsyncResult *read_res,
6819 GInputStream *stream = G_INPUT_STREAM (obj);
6820 LoadContentsData *data = user_data;
6821 GError *error = NULL;
6824 read_size = g_input_stream_read_finish (stream, read_res, &error);
6828 g_task_return_error (data->task, error);
6829 g_object_unref (data->task);
6831 /* Close the file ignoring any error */
6832 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6833 g_object_unref (stream);
6835 else if (read_size == 0)
6837 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6838 G_FILE_ATTRIBUTE_ETAG_VALUE,
6840 g_task_get_cancellable (data->task),
6841 load_contents_fstat_callback,
6844 else if (read_size > 0)
6846 data->pos += read_size;
6848 g_byte_array_set_size (data->content,
6849 data->pos + GET_CONTENT_BLOCK_SIZE);
6852 if (data->read_more_callback &&
6853 !data->read_more_callback ((char *)data->content->data, data->pos,
6854 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6855 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6856 G_FILE_ATTRIBUTE_ETAG_VALUE,
6858 g_task_get_cancellable (data->task),
6859 load_contents_fstat_callback,
6862 g_input_stream_read_async (stream,
6863 data->content->data + data->pos,
6864 GET_CONTENT_BLOCK_SIZE,
6866 g_task_get_cancellable (data->task),
6867 load_contents_read_callback,
6873 load_contents_open_callback (GObject *obj,
6874 GAsyncResult *open_res,
6877 GFile *file = G_FILE (obj);
6878 GFileInputStream *stream;
6879 LoadContentsData *data = user_data;
6880 GError *error = NULL;
6882 stream = g_file_read_finish (file, open_res, &error);
6886 g_byte_array_set_size (data->content,
6887 data->pos + GET_CONTENT_BLOCK_SIZE);
6888 g_input_stream_read_async (G_INPUT_STREAM (stream),
6889 data->content->data + data->pos,
6890 GET_CONTENT_BLOCK_SIZE,
6892 g_task_get_cancellable (data->task),
6893 load_contents_read_callback,
6898 g_task_return_error (data->task, error);
6899 g_object_unref (data->task);
6904 * g_file_load_partial_contents_async: (skip)
6905 * @file: input #GFile
6906 * @cancellable: optional #GCancellable object, %NULL to ignore
6907 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6908 * and to specify whether further data should be read
6909 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6910 * @user_data: the data to pass to the callback functions
6912 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6913 * be used to stop reading from the file when appropriate, else this
6914 * function will behave exactly as g_file_load_contents_async(). This
6915 * operation can be finished by g_file_load_partial_contents_finish().
6917 * Users of this function should be aware that @user_data is passed to
6918 * both the @read_more_callback and the @callback.
6920 * If @cancellable is not %NULL, then the operation can be cancelled by
6921 * triggering the cancellable object from another thread. If the operation
6922 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6925 g_file_load_partial_contents_async (GFile *file,
6926 GCancellable *cancellable,
6927 GFileReadMoreCallback read_more_callback,
6928 GAsyncReadyCallback callback,
6931 LoadContentsData *data;
6933 g_return_if_fail (G_IS_FILE (file));
6935 data = g_new0 (LoadContentsData, 1);
6936 data->read_more_callback = read_more_callback;
6937 data->content = g_byte_array_new ();
6939 data->task = g_task_new (file, cancellable, callback, user_data);
6940 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6942 g_file_read_async (file,
6944 g_task_get_cancellable (data->task),
6945 load_contents_open_callback,
6950 * g_file_load_partial_contents_finish:
6951 * @file: input #GFile
6952 * @res: a #GAsyncResult
6953 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6954 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6955 * or %NULL if the length is not needed
6956 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6957 * or %NULL if the entity tag is not needed
6958 * @error: a #GError, or %NULL
6960 * Finishes an asynchronous partial load operation that was started
6961 * with g_file_load_partial_contents_async(). The data is always
6962 * zero-terminated, but this is not included in the resultant @length.
6963 * The returned @content should be freed with g_free() when no longer
6966 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6967 * present, it will be set appropriately.
6970 g_file_load_partial_contents_finish (GFile *file,
6978 LoadContentsData *data;
6980 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6981 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6982 g_return_val_if_fail (contents != NULL, FALSE);
6984 task = G_TASK (res);
6986 if (!g_task_propagate_boolean (task, error))
6993 data = g_task_get_task_data (task);
6996 *length = data->pos;
7000 *etag_out = data->etag;
7004 /* Zero terminate */
7005 g_byte_array_set_size (data->content, data->pos + 1);
7006 data->content->data[data->pos] = 0;
7008 *contents = (char *)g_byte_array_free (data->content, FALSE);
7009 data->content = NULL;
7015 * g_file_load_contents_async:
7016 * @file: input #GFile
7017 * @cancellable: optional #GCancellable object, %NULL to ignore
7018 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7019 * @user_data: the data to pass to callback function
7021 * Starts an asynchronous load of the @file's contents.
7023 * For more details, see g_file_load_contents() which is
7024 * the synchronous version of this call.
7026 * When the load operation has completed, @callback will be called
7027 * with @user data. To finish the operation, call
7028 * g_file_load_contents_finish() with the #GAsyncResult returned by
7031 * If @cancellable is not %NULL, then the operation can be cancelled by
7032 * triggering the cancellable object from another thread. If the operation
7033 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7036 g_file_load_contents_async (GFile *file,
7037 GCancellable *cancellable,
7038 GAsyncReadyCallback callback,
7041 g_file_load_partial_contents_async (file,
7044 callback, user_data);
7048 * g_file_load_contents_finish:
7049 * @file: input #GFile
7050 * @res: a #GAsyncResult
7051 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7052 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7053 * or %NULL if the length is not needed
7054 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7055 * or %NULL if the entity tag is not needed
7056 * @error: a #GError, or %NULL
7058 * Finishes an asynchronous load of the @file's contents.
7059 * The contents are placed in @contents, and @length is set to the
7060 * size of the @contents string. The @content should be freed with
7061 * g_free() when no longer needed. If @etag_out is present, it will be
7062 * set to the new entity tag for the @file.
7064 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7065 * present, it will be set appropriately.
7068 g_file_load_contents_finish (GFile *file,
7075 return g_file_load_partial_contents_finish (file,
7084 * g_file_replace_contents:
7085 * @file: input #GFile
7086 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7087 * @length: the length of @contents in bytes
7088 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7089 * for the document, or %NULL
7090 * @make_backup: %TRUE if a backup should be created
7091 * @flags: a set of #GFileCreateFlags
7092 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7093 * for the document. This should be freed with g_free() when no longer
7095 * @cancellable: optional #GCancellable object, %NULL to ignore
7096 * @error: a #GError, or %NULL
7098 * Replaces the contents of @file with @contents of @length bytes.
7100 * If @etag is specified (not %NULL), any existing file must have that etag,
7101 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7103 * If @make_backup is %TRUE, this function will attempt to make a backup
7106 * If @cancellable is not %NULL, then the operation can be cancelled by
7107 * triggering the cancellable object from another thread. If the operation
7108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7110 * The returned @new_etag can be used to verify that the file hasn't
7111 * changed the next time it is saved over.
7113 * Returns: %TRUE if successful. If an error has occurred, this function
7114 * will return %FALSE and set @error appropriately if present.
7117 g_file_replace_contents (GFile *file,
7118 const char *contents,
7121 gboolean make_backup,
7122 GFileCreateFlags flags,
7124 GCancellable *cancellable,
7127 GFileOutputStream *out;
7128 gsize pos, remainder;
7132 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7133 g_return_val_if_fail (contents != NULL, FALSE);
7135 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7141 while (remainder > 0 &&
7142 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7144 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7152 if (remainder > 0 && res < 0)
7154 /* Ignore errors on close */
7155 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7156 g_object_unref (out);
7158 /* error is set already */
7162 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7165 *new_etag = g_file_output_stream_get_etag (out);
7167 g_object_unref (out);
7178 } ReplaceContentsData;
7181 replace_contents_data_free (ReplaceContentsData *data)
7183 g_bytes_unref (data->content);
7184 g_free (data->etag);
7189 replace_contents_close_callback (GObject *obj,
7190 GAsyncResult *close_res,
7193 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7194 ReplaceContentsData *data = user_data;
7196 /* Ignore errors here, we're only reading anyway */
7197 g_output_stream_close_finish (stream, close_res, NULL);
7198 g_object_unref (stream);
7202 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7203 g_task_return_boolean (data->task, TRUE);
7205 g_object_unref (data->task);
7209 replace_contents_write_callback (GObject *obj,
7210 GAsyncResult *read_res,
7213 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7214 ReplaceContentsData *data = user_data;
7215 GError *error = NULL;
7218 write_size = g_output_stream_write_finish (stream, read_res, &error);
7220 if (write_size <= 0)
7222 /* Error or EOF, close the file */
7225 data->failed = TRUE;
7226 g_task_return_error (data->task, error);
7228 g_output_stream_close_async (stream, 0,
7229 g_task_get_cancellable (data->task),
7230 replace_contents_close_callback, data);
7232 else if (write_size > 0)
7234 const gchar *content;
7237 content = g_bytes_get_data (data->content, &length);
7238 data->pos += write_size;
7240 if (data->pos >= length)
7241 g_output_stream_close_async (stream, 0,
7242 g_task_get_cancellable (data->task),
7243 replace_contents_close_callback, data);
7245 g_output_stream_write_async (stream,
7246 content + data->pos,
7249 g_task_get_cancellable (data->task),
7250 replace_contents_write_callback,
7256 replace_contents_open_callback (GObject *obj,
7257 GAsyncResult *open_res,
7260 GFile *file = G_FILE (obj);
7261 GFileOutputStream *stream;
7262 ReplaceContentsData *data = user_data;
7263 GError *error = NULL;
7265 stream = g_file_replace_finish (file, open_res, &error);
7269 const gchar *content;
7272 content = g_bytes_get_data (data->content, &length);
7273 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7274 content + data->pos,
7277 g_task_get_cancellable (data->task),
7278 replace_contents_write_callback,
7283 g_task_return_error (data->task, error);
7284 g_object_unref (data->task);
7289 * g_file_replace_contents_async:
7290 * @file: input #GFile
7291 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7292 * @length: the length of @contents in bytes
7293 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7294 * @make_backup: %TRUE if a backup should be created
7295 * @flags: a set of #GFileCreateFlags
7296 * @cancellable: optional #GCancellable object, %NULL to ignore
7297 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7298 * @user_data: the data to pass to callback function
7300 * Starts an asynchronous replacement of @file with the given
7301 * @contents of @length bytes. @etag will replace the document's
7302 * current entity tag.
7304 * When this operation has completed, @callback will be called with
7305 * @user_user data, and the operation can be finalized with
7306 * g_file_replace_contents_finish().
7308 * If @cancellable is not %NULL, then the operation can be cancelled by
7309 * triggering the cancellable object from another thread. If the operation
7310 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7312 * If @make_backup is %TRUE, this function will attempt to
7313 * make a backup of @file.
7315 * <warning><para>No copy of @content will be made, so it must stay valid until
7316 * @callback is called. See g_file_replace_contents_bytes_async() for a #GBytes
7317 * version that will automatically hold a reference to the contents (without
7318 * copying) for the duration of the call.</para></warning>
7321 g_file_replace_contents_async (GFile *file,
7322 const char *contents,
7325 gboolean make_backup,
7326 GFileCreateFlags flags,
7327 GCancellable *cancellable,
7328 GAsyncReadyCallback callback,
7333 bytes = g_bytes_new_static (contents, length);
7334 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7335 cancellable, callback, user_data);
7336 g_bytes_unref (bytes);
7340 * g_file_replace_contents_bytes_async:
7341 * @file: input #GFile
7342 * @contents: a #GBytes
7343 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7344 * @make_backup: %TRUE if a backup should be created
7345 * @flags: a set of #GFileCreateFlags
7346 * @cancellable: optional #GCancellable object, %NULL to ignore
7347 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7348 * @user_data: the data to pass to callback function
7350 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7351 * This function will keep a ref on @contents until the operation is done.
7352 * Unlike g_file_replace_contents_async() this allows forgetting about the
7353 * content without waiting for the callback.
7355 * When this operation has completed, @callback will be called with
7356 * @user_user data, and the operation can be finalized with
7357 * g_file_replace_contents_finish().
7362 g_file_replace_contents_bytes_async (GFile *file,
7365 gboolean make_backup,
7366 GFileCreateFlags flags,
7367 GCancellable *cancellable,
7368 GAsyncReadyCallback callback,
7371 ReplaceContentsData *data;
7373 g_return_if_fail (G_IS_FILE (file));
7374 g_return_if_fail (contents != NULL);
7376 data = g_new0 (ReplaceContentsData, 1);
7378 data->content = g_bytes_ref (contents);
7380 data->task = g_task_new (file, cancellable, callback, user_data);
7381 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7383 g_file_replace_async (file,
7388 g_task_get_cancellable (data->task),
7389 replace_contents_open_callback,
7394 * g_file_replace_contents_finish:
7395 * @file: input #GFile
7396 * @res: a #GAsyncResult
7397 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7398 * for the document. This should be freed with g_free() when it is no
7399 * longer needed, or %NULL
7400 * @error: a #GError, or %NULL
7402 * Finishes an asynchronous replace of the given @file. See
7403 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7404 * tag for the document, if present.
7406 * Returns: %TRUE on success, %FALSE on failure.
7409 g_file_replace_contents_finish (GFile *file,
7415 ReplaceContentsData *data;
7417 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7418 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7420 task = G_TASK (res);
7422 if (!g_task_propagate_boolean (task, error))
7425 data = g_task_get_task_data (task);
7429 *new_etag = data->etag;
7430 data->etag = NULL; /* Take ownership */
7437 g_file_real_measure_disk_usage (GFile *file,
7438 GFileMeasureFlags flags,
7439 GCancellable *cancellable,
7440 GFileMeasureProgressCallback progress_callback,
7441 gpointer progress_data,
7442 guint64 *disk_usage,
7447 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7448 "Operation not supported for the current backend.");
7454 GFileMeasureFlags flags;
7455 GFileMeasureProgressCallback progress_callback;
7456 gpointer progress_data;
7468 GFileMeasureProgressCallback callback;
7471 guint64 current_size;
7477 measure_disk_usage_invoke_progress (gpointer user_data)
7479 MeasureProgress *progress = user_data;
7481 (* progress->callback) (progress->reporting,
7482 progress->current_size, progress->num_dirs, progress->num_files,
7483 progress->user_data);
7489 measure_disk_usage_progress (gboolean reporting,
7490 guint64 current_size,
7495 MeasureProgress progress;
7496 GTask *task = user_data;
7497 MeasureTaskData *data;
7499 data = g_task_get_task_data (task);
7501 progress.callback = data->progress_callback;
7502 progress.user_data = data->progress_data;
7503 progress.reporting = reporting;
7504 progress.current_size = current_size;
7505 progress.num_dirs = num_dirs;
7506 progress.num_files = num_files;
7508 g_main_context_invoke_full (g_task_get_context (task),
7509 g_task_get_priority (task),
7510 measure_disk_usage_invoke_progress,
7511 g_memdup (&progress, sizeof progress),
7516 measure_disk_usage_thread (GTask *task,
7517 gpointer source_object,
7519 GCancellable *cancellable)
7521 MeasureTaskData *data = task_data;
7522 GError *error = NULL;
7523 MeasureResult result;
7525 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7526 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7527 &result.disk_usage, &result.num_dirs, &result.num_files,
7529 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7531 g_task_return_error (task, error);
7535 g_file_real_measure_disk_usage_async (GFile *file,
7536 GFileMeasureFlags flags,
7538 GCancellable *cancellable,
7539 GFileMeasureProgressCallback progress_callback,
7540 gpointer progress_data,
7541 GAsyncReadyCallback callback,
7544 MeasureTaskData data;
7548 data.progress_callback = progress_callback;
7549 data.progress_data = progress_data;
7551 task = g_task_new (file, cancellable, callback, user_data);
7552 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7553 g_task_set_priority (task, io_priority);
7555 g_task_run_in_thread (task, measure_disk_usage_thread);
7556 g_object_unref (task);
7560 g_file_real_measure_disk_usage_finish (GFile *file,
7561 GAsyncResult *result,
7562 guint64 *disk_usage,
7567 MeasureResult *measure_result;
7569 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7571 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7573 if (measure_result == NULL)
7577 *disk_usage = measure_result->disk_usage;
7580 *num_dirs = measure_result->num_dirs;
7583 *num_files = measure_result->num_files;
7585 g_free (measure_result);
7591 * g_file_measure_disk_usage:
7593 * @flags: #GFileMeasureFlags
7594 * @cancellable: (allow-none): optional #GCancellable
7595 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7596 * @progress_data: user_data for @progress_callback
7597 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7598 * @num_dirs: (allow-none) (out): the number of directories encountered
7599 * @num_files: (allow-none) (out): the number of non-directories encountered
7600 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7602 * Recursively measures the disk usage of @file.
7604 * This is essentially an analog of the '<literal>du</literal>' command,
7605 * but it also reports the number of directories and non-directory files
7606 * encountered (including things like symbolic links).
7608 * By default, errors are only reported against the toplevel file
7609 * itself. Errors found while recursing are silently ignored, unless
7610 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7612 * The returned size, @disk_usage, is in bytes and should be formatted
7613 * with g_format_size() in order to get something reasonable for showing
7614 * in a user interface.
7616 * @progress_callback and @progress_data can be given to request
7617 * periodic progress updates while scanning. See the documentation for
7618 * #GFileMeasureProgressCallback for information about when and how the
7619 * callback will be invoked.
7621 * Returns: %TRUE if successful, with the out parameters set.
7622 * %FALSE otherwise, with @error set.
7627 g_file_measure_disk_usage (GFile *file,
7628 GFileMeasureFlags flags,
7629 GCancellable *cancellable,
7630 GFileMeasureProgressCallback progress_callback,
7631 gpointer progress_data,
7632 guint64 *disk_usage,
7637 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7638 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7639 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7641 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7642 progress_callback, progress_data,
7643 disk_usage, num_dirs, num_files,
7648 * g_file_measure_disk_usage_async:
7650 * @flags: #GFileMeasureFlags
7651 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7653 * @cancellable: (allow-none): optional #GCancellable
7654 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7655 * @progress_data: user_data for @progress_callback
7656 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7657 * @user_data: the data to pass to callback function
7659 * Recursively measures the disk usage of @file.
7661 * This is the asynchronous version of g_file_measure_disk_usage(). See
7662 * there for more information.
7667 g_file_measure_disk_usage_async (GFile *file,
7668 GFileMeasureFlags flags,
7670 GCancellable *cancellable,
7671 GFileMeasureProgressCallback progress_callback,
7672 gpointer progress_data,
7673 GAsyncReadyCallback callback,
7676 g_return_if_fail (G_IS_FILE (file));
7677 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7679 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7680 progress_callback, progress_data,
7681 callback, user_data);
7685 * g_file_measure_disk_usage_finish:
7687 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7688 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7689 * @num_dirs: (allow-none) (out): the number of directories encountered
7690 * @num_files: (allow-none) (out): the number of non-directories encountered
7691 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7693 * Collects the results from an earlier call to
7694 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7697 * Returns: %TRUE if successful, with the out parameters set.
7698 * %FALSE otherwise, with @error set.
7703 g_file_measure_disk_usage_finish (GFile *file,
7704 GAsyncResult *result,
7705 guint64 *disk_usage,
7710 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7711 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7713 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7717 * g_file_start_mountable:
7718 * @file: input #GFile
7719 * @flags: flags affecting the operation
7720 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7721 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7722 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7723 * @user_data: the data to pass to callback function
7725 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7726 * Using @start_operation, you can request callbacks when, for instance,
7727 * passwords are needed during authentication.
7729 * If @cancellable is not %NULL, then the operation can be cancelled by
7730 * triggering the cancellable object from another thread. If the operation
7731 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7733 * When the operation is finished, @callback will be called.
7734 * You can then call g_file_mount_mountable_finish() to get
7735 * the result of the operation.
7740 g_file_start_mountable (GFile *file,
7741 GDriveStartFlags flags,
7742 GMountOperation *start_operation,
7743 GCancellable *cancellable,
7744 GAsyncReadyCallback callback,
7749 g_return_if_fail (G_IS_FILE (file));
7751 iface = G_FILE_GET_IFACE (file);
7753 if (iface->start_mountable == NULL)
7755 g_task_report_new_error (file, callback, user_data,
7756 g_file_start_mountable,
7757 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7758 _("Operation not supported"));
7762 (* iface->start_mountable) (file,
7771 * g_file_start_mountable_finish:
7772 * @file: input #GFile
7773 * @result: a #GAsyncResult
7774 * @error: a #GError, or %NULL
7776 * Finishes a start operation. See g_file_start_mountable() for details.
7778 * Finish an asynchronous start operation that was started
7779 * with g_file_start_mountable().
7781 * Returns: %TRUE if the operation finished successfully. %FALSE
7787 g_file_start_mountable_finish (GFile *file,
7788 GAsyncResult *result,
7793 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7794 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7796 if (g_async_result_legacy_propagate_error (result, error))
7798 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7799 return g_task_propagate_boolean (G_TASK (result), error);
7801 iface = G_FILE_GET_IFACE (file);
7802 return (* iface->start_mountable_finish) (file, result, error);
7806 * g_file_stop_mountable:
7807 * @file: input #GFile
7808 * @flags: flags affecting the operation
7809 * @mount_operation: (allow-none): a #GMountOperation,
7810 * or %NULL to avoid user interaction.
7811 * @cancellable: (allow-none): optional #GCancellable object,
7813 * @callback: (allow-none): a #GAsyncReadyCallback to call
7814 * when the request is satisfied, or %NULL
7815 * @user_data: the data to pass to callback function
7817 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7819 * If @cancellable is not %NULL, then the operation can be cancelled by
7820 * triggering the cancellable object from another thread. If the operation
7821 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7823 * When the operation is finished, @callback will be called.
7824 * You can then call g_file_stop_mountable_finish() to get
7825 * the result of the operation.
7830 g_file_stop_mountable (GFile *file,
7831 GMountUnmountFlags flags,
7832 GMountOperation *mount_operation,
7833 GCancellable *cancellable,
7834 GAsyncReadyCallback callback,
7839 g_return_if_fail (G_IS_FILE (file));
7841 iface = G_FILE_GET_IFACE (file);
7843 if (iface->stop_mountable == NULL)
7845 g_task_report_new_error (file, callback, user_data,
7846 g_file_stop_mountable,
7847 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7848 _("Operation not supported"));
7852 (* iface->stop_mountable) (file,
7861 * g_file_stop_mountable_finish:
7862 * @file: input #GFile
7863 * @result: a #GAsyncResult
7864 * @error: a #GError, or %NULL
7866 * Finishes an stop operation, see g_file_stop_mountable() for details.
7868 * Finish an asynchronous stop operation that was started
7869 * with g_file_stop_mountable().
7871 * Returns: %TRUE if the operation finished successfully.
7877 g_file_stop_mountable_finish (GFile *file,
7878 GAsyncResult *result,
7883 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7884 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7886 if (g_async_result_legacy_propagate_error (result, error))
7888 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7889 return g_task_propagate_boolean (G_TASK (result), error);
7891 iface = G_FILE_GET_IFACE (file);
7892 return (* iface->stop_mountable_finish) (file, result, error);
7896 * g_file_poll_mountable:
7897 * @file: input #GFile
7898 * @cancellable: optional #GCancellable object, %NULL to ignore
7899 * @callback: (allow-none): a #GAsyncReadyCallback to call
7900 * when the request is satisfied, or %NULL
7901 * @user_data: the data to pass to callback function
7903 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7905 * If @cancellable is not %NULL, then the operation can be cancelled by
7906 * triggering the cancellable object from another thread. If the operation
7907 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7909 * When the operation is finished, @callback will be called.
7910 * You can then call g_file_mount_mountable_finish() to get
7911 * the result of the operation.
7916 g_file_poll_mountable (GFile *file,
7917 GCancellable *cancellable,
7918 GAsyncReadyCallback callback,
7923 g_return_if_fail (G_IS_FILE (file));
7925 iface = G_FILE_GET_IFACE (file);
7927 if (iface->poll_mountable == NULL)
7929 g_task_report_new_error (file, callback, user_data,
7930 g_file_poll_mountable,
7931 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7932 _("Operation not supported"));
7936 (* iface->poll_mountable) (file,
7943 * g_file_poll_mountable_finish:
7944 * @file: input #GFile
7945 * @result: a #GAsyncResult
7946 * @error: a #GError, or %NULL
7948 * Finishes a poll operation. See g_file_poll_mountable() for details.
7950 * Finish an asynchronous poll operation that was polled
7951 * with g_file_poll_mountable().
7953 * Returns: %TRUE if the operation finished successfully. %FALSE
7959 g_file_poll_mountable_finish (GFile *file,
7960 GAsyncResult *result,
7965 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7966 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7968 if (g_async_result_legacy_propagate_error (result, error))
7970 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7971 return g_task_propagate_boolean (G_TASK (result), error);
7973 iface = G_FILE_GET_IFACE (file);
7974 return (* iface->poll_mountable_finish) (file, result, error);
7978 * g_file_supports_thread_contexts:
7981 * Checks if @file supports <link
7982 * linkend="g-main-context-push-thread-default-context">thread-default
7983 * contexts</link>. If this returns %FALSE, you cannot perform
7984 * asynchronous operations on @file in a thread that has a
7985 * thread-default context.
7987 * Returns: Whether or not @file supports thread-default contexts.
7992 g_file_supports_thread_contexts (GFile *file)
7996 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7998 iface = G_FILE_GET_IFACE (file);
7999 return iface->supports_thread_contexts;