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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
80 * <member>g_file_new_for_path() if you have a path.</member>
81 * <member>g_file_new_for_uri() if you have a URI.</member>
82 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
83 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
84 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
87 * One way to think of a #GFile is as an abstraction of a pathname. For
88 * normal files the system pathname is what is stored internally, but as
89 * #GFiles are extensible it could also be something else that corresponds
90 * to a pathname in a userspace implementation of a filesystem.
92 * #GFiles make up hierarchies of directories and files that correspond to
93 * the files on a filesystem. You can move through the file system with
94 * #GFile using g_file_get_parent() to get an identifier for the parent
95 * directory, g_file_get_child() to get a child within a directory,
96 * g_file_resolve_relative_path() to resolve a relative path between two
97 * #GFiles. There can be multiple hierarchies, so you may not end up at
98 * the same root if you repeatedly call g_file_get_parent() on two different
101 * All #GFiles have a basename (get with g_file_get_basename()). These names
102 * are byte strings that are used to identify the file on the filesystem
103 * (relative to its parent directory) and there is no guarantees that they
104 * have any particular charset encoding or even make any sense at all. If
105 * you want to use filenames in a user interface you should use the display
106 * name that you can get by requesting the
107 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
108 * This is guaranteed to be in UTF-8 and can be used in a user interface.
109 * But always store the real basename or the #GFile to use to actually
110 * access the file, because there is no way to go from a display name to
113 * Using #GFile as an identifier has the same weaknesses as using a path
114 * in that there may be multiple aliases for the same file. For instance,
115 * hard or soft links may cause two different #GFiles to refer to the same
116 * file. Other possible causes for aliases are: case insensitive filesystems,
117 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
118 * check if two #GFiles point to the same file you can query for the
119 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
120 * canonicalization of pathnames passed in, so that trivial differences in
121 * the path string used at creation (duplicated slashes, slash at end of
122 * path, "." or ".." path segments, etc) does not create different #GFiles.
124 * Many #GFile operations have both synchronous and asynchronous versions
125 * to suit your application. Asynchronous versions of synchronous functions
126 * simply have _async() appended to their function names. The asynchronous
127 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
128 * the operation, producing a GAsyncResult which is then passed to the
129 * function's matching _finish() operation.
131 * Some #GFile operations do not have synchronous analogs, as they may
132 * take a very long time to finish, and blocking may leave an application
133 * unusable. Notable cases include:
135 * <member>g_file_mount_mountable() to mount a mountable file.</member>
136 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
137 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
140 * ## Entity Tags # {#gfile-etag}
142 * One notable feature of #GFiles are entity tags, or "etags" for
143 * short. Entity tags are somewhat like a more abstract version of the
144 * traditional mtime, and can be used to quickly determine if the file
145 * has been modified from the version on the file system. See the
147 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
148 * for HTTP Etag headers, which are a very similar concept.
151 static void g_file_real_query_info_async (GFile *file,
152 const char *attributes,
153 GFileQueryInfoFlags flags,
155 GCancellable *cancellable,
156 GAsyncReadyCallback callback,
158 static GFileInfo * g_file_real_query_info_finish (GFile *file,
161 static void g_file_real_query_filesystem_info_async (GFile *file,
162 const char *attributes,
164 GCancellable *cancellable,
165 GAsyncReadyCallback callback,
167 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
170 static void g_file_real_enumerate_children_async (GFile *file,
171 const char *attributes,
172 GFileQueryInfoFlags flags,
174 GCancellable *cancellable,
175 GAsyncReadyCallback callback,
177 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
180 static void g_file_real_read_async (GFile *file,
182 GCancellable *cancellable,
183 GAsyncReadyCallback callback,
185 static GFileInputStream * g_file_real_read_finish (GFile *file,
188 static void g_file_real_append_to_async (GFile *file,
189 GFileCreateFlags flags,
191 GCancellable *cancellable,
192 GAsyncReadyCallback callback,
194 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
197 static void g_file_real_create_async (GFile *file,
198 GFileCreateFlags flags,
200 GCancellable *cancellable,
201 GAsyncReadyCallback callback,
203 static GFileOutputStream *g_file_real_create_finish (GFile *file,
206 static void g_file_real_replace_async (GFile *file,
208 gboolean make_backup,
209 GFileCreateFlags flags,
211 GCancellable *cancellable,
212 GAsyncReadyCallback callback,
214 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
217 static void g_file_real_delete_async (GFile *file,
219 GCancellable *cancellable,
220 GAsyncReadyCallback callback,
222 static gboolean g_file_real_delete_finish (GFile *file,
225 static void g_file_real_trash_async (GFile *file,
227 GCancellable *cancellable,
228 GAsyncReadyCallback callback,
230 static gboolean g_file_real_trash_finish (GFile *file,
233 static void g_file_real_make_directory_async (GFile *file,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
238 static gboolean g_file_real_make_directory_finish (GFile *file,
241 static void g_file_real_open_readwrite_async (GFile *file,
243 GCancellable *cancellable,
244 GAsyncReadyCallback callback,
246 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
249 static void g_file_real_create_readwrite_async (GFile *file,
250 GFileCreateFlags flags,
252 GCancellable *cancellable,
253 GAsyncReadyCallback callback,
255 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
258 static void g_file_real_replace_readwrite_async (GFile *file,
260 gboolean make_backup,
261 GFileCreateFlags flags,
263 GCancellable *cancellable,
264 GAsyncReadyCallback callback,
266 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
269 static gboolean g_file_real_set_attributes_from_info (GFile *file,
271 GFileQueryInfoFlags flags,
272 GCancellable *cancellable,
274 static void g_file_real_set_display_name_async (GFile *file,
275 const char *display_name,
277 GCancellable *cancellable,
278 GAsyncReadyCallback callback,
280 static GFile * g_file_real_set_display_name_finish (GFile *file,
283 static void g_file_real_set_attributes_async (GFile *file,
285 GFileQueryInfoFlags flags,
287 GCancellable *cancellable,
288 GAsyncReadyCallback callback,
290 static gboolean g_file_real_set_attributes_finish (GFile *file,
294 static void g_file_real_find_enclosing_mount_async (GFile *file,
296 GCancellable *cancellable,
297 GAsyncReadyCallback callback,
299 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
302 static void g_file_real_copy_async (GFile *source,
304 GFileCopyFlags flags,
306 GCancellable *cancellable,
307 GFileProgressCallback progress_callback,
308 gpointer progress_callback_data,
309 GAsyncReadyCallback callback,
311 static gboolean g_file_real_copy_finish (GFile *file,
315 static gboolean g_file_real_measure_disk_usage (GFile *file,
316 GFileMeasureFlags flags,
317 GCancellable *cancellable,
318 GFileMeasureProgressCallback progress_callback,
319 gpointer progress_data,
324 static void g_file_real_measure_disk_usage_async (GFile *file,
325 GFileMeasureFlags flags,
327 GCancellable *cancellable,
328 GFileMeasureProgressCallback progress_callback,
329 gpointer progress_data,
330 GAsyncReadyCallback callback,
332 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
333 GAsyncResult *result,
339 typedef GFileIface GFileInterface;
340 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
343 g_file_default_init (GFileIface *iface)
345 iface->enumerate_children_async = g_file_real_enumerate_children_async;
346 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
347 iface->set_display_name_async = g_file_real_set_display_name_async;
348 iface->set_display_name_finish = g_file_real_set_display_name_finish;
349 iface->query_info_async = g_file_real_query_info_async;
350 iface->query_info_finish = g_file_real_query_info_finish;
351 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
352 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
353 iface->set_attributes_async = g_file_real_set_attributes_async;
354 iface->set_attributes_finish = g_file_real_set_attributes_finish;
355 iface->read_async = g_file_real_read_async;
356 iface->read_finish = g_file_real_read_finish;
357 iface->append_to_async = g_file_real_append_to_async;
358 iface->append_to_finish = g_file_real_append_to_finish;
359 iface->create_async = g_file_real_create_async;
360 iface->create_finish = g_file_real_create_finish;
361 iface->replace_async = g_file_real_replace_async;
362 iface->replace_finish = g_file_real_replace_finish;
363 iface->delete_file_async = g_file_real_delete_async;
364 iface->delete_file_finish = g_file_real_delete_finish;
365 iface->trash_async = g_file_real_trash_async;
366 iface->trash_finish = g_file_real_trash_finish;
367 iface->make_directory_async = g_file_real_make_directory_async;
368 iface->make_directory_finish = g_file_real_make_directory_finish;
369 iface->open_readwrite_async = g_file_real_open_readwrite_async;
370 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
371 iface->create_readwrite_async = g_file_real_create_readwrite_async;
372 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
373 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
374 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
375 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
376 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
377 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
378 iface->copy_async = g_file_real_copy_async;
379 iface->copy_finish = g_file_real_copy_finish;
380 iface->measure_disk_usage = g_file_real_measure_disk_usage;
381 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
382 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
388 * @file: input #GFile
390 * Checks to see if a file is native to the platform.
392 * A native file s one expressed in the platform-native filename format,
393 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
394 * as it might be on a locally mounted remote filesystem.
396 * On some systems non-native files may be available using the native
397 * filesystem via a userspace filesystem (FUSE), in these cases this call
398 * will return %FALSE, but g_file_get_path() will still return a native path.
400 * This call does no blocking I/O.
402 * Returns: %TRUE if @file is native
405 g_file_is_native (GFile *file)
409 g_return_val_if_fail (G_IS_FILE (file), FALSE);
411 iface = G_FILE_GET_IFACE (file);
413 return (* iface->is_native) (file);
418 * g_file_has_uri_scheme:
419 * @file: input #GFile
420 * @uri_scheme: a string containing a URI scheme
422 * Checks to see if a #GFile has a given URI scheme.
424 * This call does no blocking I/O.
426 * Returns: %TRUE if #GFile's backend supports the
427 * given URI scheme, %FALSE if URI scheme is %NULL,
428 * not supported, or #GFile is invalid.
431 g_file_has_uri_scheme (GFile *file,
432 const char *uri_scheme)
436 g_return_val_if_fail (G_IS_FILE (file), FALSE);
437 g_return_val_if_fail (uri_scheme != NULL, FALSE);
439 iface = G_FILE_GET_IFACE (file);
441 return (* iface->has_uri_scheme) (file, uri_scheme);
446 * g_file_get_uri_scheme:
447 * @file: input #GFile
449 * Gets the URI scheme for a #GFile.
450 * RFC 3986 decodes the scheme as:
452 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
454 * Common schemes include "file", "http", "ftp", etc.
456 * This call does no blocking I/O.
458 * Returns: a string containing the URI scheme for the given
459 * #GFile. The returned string should be freed with g_free()
460 * when no longer needed.
463 g_file_get_uri_scheme (GFile *file)
467 g_return_val_if_fail (G_IS_FILE (file), NULL);
469 iface = G_FILE_GET_IFACE (file);
471 return (* iface->get_uri_scheme) (file);
476 * g_file_get_basename:
477 * @file: input #GFile
479 * Gets the base name (the last component of the path) for a given #GFile.
481 * If called for the top level of a system (such as the filesystem root
482 * or a uri like sftp://host/) it will return a single directory separator
483 * (and on Windows, possibly a drive letter).
485 * The base name is a byte string (not UTF-8). It has no defined encoding
486 * or rules other than it may not contain zero bytes. If you want to use
487 * filenames in a user interface you should use the display name that you
488 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
489 * attribute with g_file_query_info().
491 * This call does no blocking I/O.
493 * Returns: string containing the #GFile's base name, or %NULL
494 * if given #GFile is invalid. The returned string should be
495 * freed with g_free() when no longer needed.
498 g_file_get_basename (GFile *file)
502 g_return_val_if_fail (G_IS_FILE (file), NULL);
504 iface = G_FILE_GET_IFACE (file);
506 return (* iface->get_basename) (file);
511 * @file: input #GFile
513 * Gets the local pathname for #GFile, if one exists.
515 * This call does no blocking I/O.
517 * Returns: string containing the #GFile's path, or %NULL if
518 * no such path exists. The returned string should be
519 * freed with g_free() when no longer needed.
522 g_file_get_path (GFile *file)
526 g_return_val_if_fail (G_IS_FILE (file), NULL);
528 iface = G_FILE_GET_IFACE (file);
530 return (* iface->get_path) (file);
535 * @file: input #GFile
537 * Gets the URI for the @file.
539 * This call does no blocking I/O.
541 * Returns: a string containing the #GFile's URI.
542 * The returned string should be freed with g_free()
543 * when no longer needed.
546 g_file_get_uri (GFile *file)
550 g_return_val_if_fail (G_IS_FILE (file), NULL);
552 iface = G_FILE_GET_IFACE (file);
554 return (* iface->get_uri) (file);
558 * g_file_get_parse_name:
559 * @file: input #GFile
561 * Gets the parse name of the @file.
562 * A parse name is a UTF-8 string that describes the
563 * file such that one can get the #GFile back using
564 * g_file_parse_name().
566 * This is generally used to show the #GFile as a nice
567 * full-pathname kind of string in a user interface,
568 * like in a location entry.
570 * For local files with names that can safely be converted
571 * to UTF-8 the pathname is used, otherwise the IRI is used
572 * (a form of URI that allows UTF-8 characters unescaped).
574 * This call does no blocking I/O.
576 * Returns: a string containing the #GFile's parse name.
577 * The returned string should be freed with g_free()
578 * when no longer needed.
581 g_file_get_parse_name (GFile *file)
585 g_return_val_if_fail (G_IS_FILE (file), NULL);
587 iface = G_FILE_GET_IFACE (file);
589 return (* iface->get_parse_name) (file);
594 * @file: input #GFile
596 * Duplicates a #GFile handle. This operation does not duplicate
597 * the actual file or directory represented by the #GFile; see
598 * g_file_copy() if attempting to copy a file.
600 * This call does no blocking I/O.
602 * Returns: (transfer full): a new #GFile that is a duplicate
603 * of the given #GFile.
606 g_file_dup (GFile *file)
610 g_return_val_if_fail (G_IS_FILE (file), NULL);
612 iface = G_FILE_GET_IFACE (file);
614 return (* iface->dup) (file);
619 * @file: (type GFile): #gconstpointer to a #GFile
621 * Creates a hash value for a #GFile.
623 * This call does no blocking I/O.
626 * Returns: 0 if @file is not a valid #GFile, otherwise an
627 * integer that can be used as hash value for the #GFile.
628 * This function is intended for easily hashing a #GFile to
629 * add to a #GHashTable or similar data structure.
632 g_file_hash (gconstpointer file)
636 g_return_val_if_fail (G_IS_FILE (file), 0);
638 iface = G_FILE_GET_IFACE (file);
640 return (* iface->hash) ((GFile *)file);
645 * @file1: the first #GFile
646 * @file2: the second #GFile
648 * Checks equality of two given #GFiles.
650 * Note that two #GFiles that differ can still refer to the same
651 * file on the filesystem due to various forms of filename
654 * This call does no blocking I/O.
656 * Returns: %TRUE if @file1 and @file2 are equal.
657 * %FALSE if either is not a #GFile.
660 g_file_equal (GFile *file1,
665 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
666 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
668 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
671 iface = G_FILE_GET_IFACE (file1);
673 return (* iface->equal) (file1, file2);
679 * @file: input #GFile
681 * Gets the parent directory for the @file.
682 * If the @file represents the root directory of the
683 * file system, then %NULL will be returned.
685 * This call does no blocking I/O.
687 * Returns: (transfer full): a #GFile structure to the
688 * parent of the given #GFile or %NULL if there is
689 * no parent. Free the returned object with g_object_unref().
692 g_file_get_parent (GFile *file)
696 g_return_val_if_fail (G_IS_FILE (file), NULL);
698 iface = G_FILE_GET_IFACE (file);
700 return (* iface->get_parent) (file);
705 * @file: input #GFile
706 * @parent: (allow-none): the parent to check for, or %NULL
708 * Checks if @file has a parent, and optionally, if it is @parent.
710 * If @parent is %NULL then this function returns %TRUE if @file has any
711 * parent at all. If @parent is non-%NULL then %TRUE is only returned
712 * if @file is a child of @parent.
714 * Returns: %TRUE if @file is a child of @parent (or any parent in the
715 * case that @parent is %NULL).
720 g_file_has_parent (GFile *file,
723 GFile *actual_parent;
726 g_return_val_if_fail (G_IS_FILE (file), FALSE);
727 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
729 actual_parent = g_file_get_parent (file);
731 if (actual_parent != NULL)
734 result = g_file_equal (parent, actual_parent);
738 g_object_unref (actual_parent);
748 * @file: input #GFile
749 * @name: string containing the child's basename
751 * Gets a child of @file with basename equal to @name.
753 * Note that the file with that specific name might not exist, but
754 * you can still have a #GFile that points to it. You can use this
755 * for instance to create that file.
757 * This call does no blocking I/O.
759 * Returns: (transfer full): a #GFile to a child specified by @name.
760 * Free the returned object with g_object_unref().
763 g_file_get_child (GFile *file,
766 g_return_val_if_fail (G_IS_FILE (file), NULL);
767 g_return_val_if_fail (name != NULL, NULL);
769 return g_file_resolve_relative_path (file, name);
773 * g_file_get_child_for_display_name:
774 * @file: input #GFile
775 * @display_name: string to a possible child
776 * @error: return location for an error
778 * Gets the child of @file for a given @display_name (i.e. a UTF-8
779 * version of the name). If this function fails, it returns %NULL
780 * and @error will be set. This is very useful when constructing a
781 * #GFile for a new file and the user entered the filename in the
782 * user interface, for instance when you select a directory and
783 * type a filename in the file selector.
785 * This call does no blocking I/O.
787 * Returns: (transfer full): a #GFile to the specified child, or
788 * %NULL if the display name couldn't be converted.
789 * Free the returned object with g_object_unref().
792 g_file_get_child_for_display_name (GFile *file,
793 const char *display_name,
798 g_return_val_if_fail (G_IS_FILE (file), NULL);
799 g_return_val_if_fail (display_name != NULL, NULL);
801 iface = G_FILE_GET_IFACE (file);
803 return (* iface->get_child_for_display_name) (file, display_name, error);
808 * @file: input #GFile
809 * @prefix: input #GFile
811 * Checks whether @file has the prefix specified by @prefix.
813 * In other words, if the names of initial elements of @file's
814 * pathname match @prefix. Only full pathname elements are matched,
815 * so a path like /foo is not considered a prefix of /foobar, only
818 * This call does no I/O, as it works purely on names. As such it can
819 * sometimes return %FALSE even if @file is inside a @prefix (from a
820 * filesystem point of view), because the prefix of @file is an alias
823 * Virtual: prefix_matches
824 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
828 g_file_has_prefix (GFile *file,
833 g_return_val_if_fail (G_IS_FILE (file), FALSE);
834 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
836 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
839 iface = G_FILE_GET_IFACE (file);
841 /* The vtable function differs in arg order since
842 * we're using the old contains_file call
844 return (* iface->prefix_matches) (prefix, file);
848 * g_file_get_relative_path:
849 * @parent: input #GFile
850 * @descendant: input #GFile
852 * Gets the path for @descendant relative to @parent.
854 * This call does no blocking I/O.
856 * Returns: string with the relative path from @descendant
857 * to @parent, or %NULL if @descendant doesn't have @parent
858 * as prefix. The returned string should be freed with g_free()
859 * when no longer needed.
862 g_file_get_relative_path (GFile *parent,
867 g_return_val_if_fail (G_IS_FILE (parent), NULL);
868 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
870 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
873 iface = G_FILE_GET_IFACE (parent);
875 return (* iface->get_relative_path) (parent, descendant);
879 * g_file_resolve_relative_path:
880 * @file: input #GFile
881 * @relative_path: a given relative path string
883 * Resolves a relative path for @file to an absolute path.
885 * This call does no blocking I/O.
887 * Returns: (transfer full): #GFile to the resolved path.
888 * %NULL if @relative_path is %NULL or if @file is invalid.
889 * Free the returned object with g_object_unref().
892 g_file_resolve_relative_path (GFile *file,
893 const char *relative_path)
897 g_return_val_if_fail (G_IS_FILE (file), NULL);
898 g_return_val_if_fail (relative_path != NULL, NULL);
900 iface = G_FILE_GET_IFACE (file);
902 return (* iface->resolve_relative_path) (file, relative_path);
906 * g_file_enumerate_children:
907 * @file: input #GFile
908 * @attributes: an attribute query string
909 * @flags: a set of #GFileQueryInfoFlags
910 * @cancellable: (allow-none): optional #GCancellable object,
912 * @error: #GError for error reporting
914 * Gets the requested information about the files in a directory.
915 * The result is a #GFileEnumerator object that will give out
916 * #GFileInfo objects for all the files in the directory.
918 * The @attributes value is a string that specifies the file
919 * attributes that should be gathered. It is not an error if
920 * it's not possible to read a particular requested attribute
921 * from a file - it just won't be set. @attributes should
922 * be a comma-separated list of attributes or attribute wildcards.
923 * The wildcard "*" means all attributes, and a wildcard like
924 * "standard::*" means all attributes in the standard namespace.
925 * An example attribute query be "standard::*,owner::user".
926 * The standard attributes are available as defines, like
927 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
929 * If @cancellable is not %NULL, then the operation can be cancelled
930 * by triggering the cancellable object from another thread. If the
931 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
934 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
935 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
936 * error will be returned. Other errors are possible too.
938 * Returns: (transfer full): A #GFileEnumerator if successful,
939 * %NULL on error. Free the returned object with g_object_unref().
942 g_file_enumerate_children (GFile *file,
943 const char *attributes,
944 GFileQueryInfoFlags flags,
945 GCancellable *cancellable,
950 g_return_val_if_fail (G_IS_FILE (file), NULL);
952 if (g_cancellable_set_error_if_cancelled (cancellable, error))
955 iface = G_FILE_GET_IFACE (file);
957 if (iface->enumerate_children == NULL)
959 g_set_error_literal (error, G_IO_ERROR,
960 G_IO_ERROR_NOT_SUPPORTED,
961 _("Operation not supported"));
965 return (* iface->enumerate_children) (file, attributes, flags,
970 * g_file_enumerate_children_async:
971 * @file: input #GFile
972 * @attributes: an attribute query string
973 * @flags: a set of #GFileQueryInfoFlags
974 * @io_priority: the [I/O priority][io-priority] of the request
975 * @cancellable: (allow-none): optional #GCancellable object,
977 * @callback: (scope async): a #GAsyncReadyCallback to call when the
978 * request is satisfied
979 * @user_data: (closure): the data to pass to callback function
981 * Asynchronously gets the requested information about the files
982 * in a directory. The result is a #GFileEnumerator object that will
983 * give out #GFileInfo objects for all the files in the directory.
985 * For more details, see g_file_enumerate_children() which is
986 * the synchronous version of this call.
988 * When the operation is finished, @callback will be called. You can
989 * then call g_file_enumerate_children_finish() to get the result of
993 g_file_enumerate_children_async (GFile *file,
994 const char *attributes,
995 GFileQueryInfoFlags flags,
997 GCancellable *cancellable,
998 GAsyncReadyCallback callback,
1003 g_return_if_fail (G_IS_FILE (file));
1005 iface = G_FILE_GET_IFACE (file);
1006 (* iface->enumerate_children_async) (file,
1016 * g_file_enumerate_children_finish:
1017 * @file: input #GFile
1018 * @res: a #GAsyncResult
1021 * Finishes an async enumerate children operation.
1022 * See g_file_enumerate_children_async().
1024 * Returns: (transfer full): a #GFileEnumerator or %NULL
1025 * if an error occurred.
1026 * Free the returned object with g_object_unref().
1029 g_file_enumerate_children_finish (GFile *file,
1035 g_return_val_if_fail (G_IS_FILE (file), NULL);
1036 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1038 if (g_async_result_legacy_propagate_error (res, error))
1041 iface = G_FILE_GET_IFACE (file);
1042 return (* iface->enumerate_children_finish) (file, res, error);
1046 * g_file_query_exists:
1047 * @file: input #GFile
1048 * @cancellable: (allow-none): optional #GCancellable object,
1051 * Utility function to check if a particular file exists. This is
1052 * implemented using g_file_query_info() and as such does blocking I/O.
1054 * Note that in many cases it is racy to first check for file existence
1055 * and then execute something based on the outcome of that, because the
1056 * file might have been created or removed in between the operations. The
1057 * general approach to handling that is to not check, but just do the
1058 * operation and handle the errors as they come.
1060 * As an example of race-free checking, take the case of reading a file,
1061 * and if it doesn't exist, creating it. There are two racy versions: read
1062 * it, and on error create it; and: check if it exists, if not create it.
1063 * These can both result in two processes creating the file (with perhaps
1064 * a partially written file as the result). The correct approach is to
1065 * always try to create the file with g_file_create() which will either
1066 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1068 * However, in many cases an existence check is useful in a user interface,
1069 * for instance to make a menu item sensitive/insensitive, so that you don't
1070 * have to fool users that something is possible and then just show an error
1071 * dialog. If you do this, you should make sure to also handle the errors
1072 * that can happen due to races when you execute the operation.
1074 * Returns: %TRUE if the file exists (and can be detected without error),
1075 * %FALSE otherwise (or if cancelled).
1078 g_file_query_exists (GFile *file,
1079 GCancellable *cancellable)
1083 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1085 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1086 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1089 g_object_unref (info);
1097 * g_file_query_file_type:
1098 * @file: input #GFile
1099 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1100 * @cancellable: (allow-none): optional #GCancellable object,
1103 * Utility function to inspect the #GFileType of a file. This is
1104 * implemented using g_file_query_info() and as such does blocking I/O.
1106 * The primary use case of this method is to check if a file is
1107 * a regular file, directory, or symlink.
1109 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1110 * if the file does not exist
1115 g_file_query_file_type (GFile *file,
1116 GFileQueryInfoFlags flags,
1117 GCancellable *cancellable)
1120 GFileType file_type;
1122 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1123 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1127 file_type = g_file_info_get_file_type (info);
1128 g_object_unref (info);
1131 file_type = G_FILE_TYPE_UNKNOWN;
1137 * g_file_query_info:
1138 * @file: input #GFile
1139 * @attributes: an attribute query string
1140 * @flags: a set of #GFileQueryInfoFlags
1141 * @cancellable: (allow-none): optional #GCancellable object,
1145 * Gets the requested information about specified @file.
1146 * The result is a #GFileInfo object that contains key-value
1147 * attributes (such as the type or size of the file).
1149 * The @attributes value is a string that specifies the file
1150 * attributes that should be gathered. It is not an error if
1151 * it's not possible to read a particular requested attribute
1152 * from a file - it just won't be set. @attributes should be a
1153 * comma-separated list of attributes or attribute wildcards.
1154 * The wildcard "*" means all attributes, and a wildcard like
1155 * "standard::*" means all attributes in the standard namespace.
1156 * An example attribute query be "standard::*,owner::user".
1157 * The standard attributes are available as defines, like
1158 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1160 * If @cancellable is not %NULL, then the operation can be cancelled
1161 * by triggering the cancellable object from another thread. If the
1162 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1165 * For symlinks, normally the information about the target of the
1166 * symlink is returned, rather than information about the symlink
1167 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1168 * in @flags the information about the symlink itself will be returned.
1169 * Also, for symlinks that point to non-existing files the information
1170 * about the symlink itself will be returned.
1172 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1173 * returned. Other errors are possible too, and depend on what kind of
1174 * filesystem the file is on.
1176 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1177 * on error. Free the returned object with g_object_unref().
1180 g_file_query_info (GFile *file,
1181 const char *attributes,
1182 GFileQueryInfoFlags flags,
1183 GCancellable *cancellable,
1188 g_return_val_if_fail (G_IS_FILE (file), NULL);
1190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1193 iface = G_FILE_GET_IFACE (file);
1195 if (iface->query_info == NULL)
1197 g_set_error_literal (error, G_IO_ERROR,
1198 G_IO_ERROR_NOT_SUPPORTED,
1199 _("Operation not supported"));
1203 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1207 * g_file_query_info_async:
1208 * @file: input #GFile
1209 * @attributes: an attribute query string
1210 * @flags: a set of #GFileQueryInfoFlags
1211 * @io_priority: the [I/O priority][io-priority] of the request
1212 * @cancellable: (allow-none): optional #GCancellable object,
1214 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1215 * request is satisfied
1216 * @user_data: (closure): the data to pass to callback function
1218 * Asynchronously gets the requested information about specified @file.
1219 * The result is a #GFileInfo object that contains key-value attributes
1220 * (such as type or size for the file).
1222 * For more details, see g_file_query_info() which is the synchronous
1223 * version of this call.
1225 * When the operation is finished, @callback will be called. You can
1226 * then call g_file_query_info_finish() to get the result of the operation.
1229 g_file_query_info_async (GFile *file,
1230 const char *attributes,
1231 GFileQueryInfoFlags flags,
1233 GCancellable *cancellable,
1234 GAsyncReadyCallback callback,
1239 g_return_if_fail (G_IS_FILE (file));
1241 iface = G_FILE_GET_IFACE (file);
1242 (* iface->query_info_async) (file,
1252 * g_file_query_info_finish:
1253 * @file: input #GFile
1254 * @res: a #GAsyncResult
1257 * Finishes an asynchronous file info query.
1258 * See g_file_query_info_async().
1260 * Returns: (transfer full): #GFileInfo for given @file
1261 * or %NULL on error. Free the returned object with
1265 g_file_query_info_finish (GFile *file,
1271 g_return_val_if_fail (G_IS_FILE (file), NULL);
1272 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1274 if (g_async_result_legacy_propagate_error (res, error))
1277 iface = G_FILE_GET_IFACE (file);
1278 return (* iface->query_info_finish) (file, res, error);
1282 * g_file_query_filesystem_info:
1283 * @file: input #GFile
1284 * @attributes: an attribute query string
1285 * @cancellable: (allow-none): optional #GCancellable object,
1289 * Similar to g_file_query_info(), but obtains information
1290 * about the filesystem the @file is on, rather than the file itself.
1291 * For instance the amount of space available and the type of
1294 * The @attributes value is a string that specifies the attributes
1295 * that should be gathered. It is not an error if it's not possible
1296 * to read a particular requested attribute from a file - it just
1297 * won't be set. @attributes should be a comma-separated list of
1298 * attributes or attribute wildcards. The wildcard "*" means all
1299 * attributes, and a wildcard like "filesystem::*" means all attributes
1300 * in the filesystem namespace. The standard namespace for filesystem
1301 * attributes is "filesystem". Common attributes of interest are
1302 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1303 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1304 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1306 * If @cancellable is not %NULL, then the operation can be cancelled
1307 * by triggering the cancellable object from another thread. If the
1308 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1311 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1312 * be returned. Other errors are possible too, and depend on what
1313 * kind of filesystem the file is on.
1315 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1316 * Free the returned object with g_object_unref().
1319 g_file_query_filesystem_info (GFile *file,
1320 const char *attributes,
1321 GCancellable *cancellable,
1326 g_return_val_if_fail (G_IS_FILE (file), NULL);
1328 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1331 iface = G_FILE_GET_IFACE (file);
1333 if (iface->query_filesystem_info == NULL)
1335 g_set_error_literal (error, G_IO_ERROR,
1336 G_IO_ERROR_NOT_SUPPORTED,
1337 _("Operation not supported"));
1341 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1345 * g_file_query_filesystem_info_async:
1346 * @file: input #GFile
1347 * @attributes: an attribute query string
1348 * @io_priority: the [I/O priority][io-priority] of the request
1349 * @cancellable: (allow-none): optional #GCancellable object,
1351 * @callback: (scope async): a #GAsyncReadyCallback to call
1352 * when the request is satisfied
1353 * @user_data: (closure): the data to pass to callback function
1355 * Asynchronously gets the requested information about the filesystem
1356 * that the specified @file is on. The result is a #GFileInfo object
1357 * that contains key-value attributes (such as type or size for the
1360 * For more details, see g_file_query_filesystem_info() which is the
1361 * synchronous version of this call.
1363 * When the operation is finished, @callback will be called. You can
1364 * then call g_file_query_info_finish() to get the result of the
1368 g_file_query_filesystem_info_async (GFile *file,
1369 const char *attributes,
1371 GCancellable *cancellable,
1372 GAsyncReadyCallback callback,
1377 g_return_if_fail (G_IS_FILE (file));
1379 iface = G_FILE_GET_IFACE (file);
1380 (* iface->query_filesystem_info_async) (file,
1389 * g_file_query_filesystem_info_finish:
1390 * @file: input #GFile
1391 * @res: a #GAsyncResult
1394 * Finishes an asynchronous filesystem info query.
1395 * See g_file_query_filesystem_info_async().
1397 * Returns: (transfer full): #GFileInfo for given @file
1398 * or %NULL on error.
1399 * Free the returned object with g_object_unref().
1402 g_file_query_filesystem_info_finish (GFile *file,
1408 g_return_val_if_fail (G_IS_FILE (file), NULL);
1409 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1411 if (g_async_result_legacy_propagate_error (res, error))
1414 iface = G_FILE_GET_IFACE (file);
1415 return (* iface->query_filesystem_info_finish) (file, res, error);
1419 * g_file_find_enclosing_mount:
1420 * @file: input #GFile
1421 * @cancellable: (allow-none): optional #GCancellable object,
1425 * Gets a #GMount for the #GFile.
1427 * If the #GFileIface for @file does not have a mount (e.g.
1428 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1429 * and %NULL will be returned.
1431 * If @cancellable is not %NULL, then the operation can be cancelled by
1432 * triggering the cancellable object from another thread. If the operation
1433 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1435 * Returns: (transfer full): a #GMount where the @file is located
1436 * or %NULL on error.
1437 * Free the returned object with g_object_unref().
1440 g_file_find_enclosing_mount (GFile *file,
1441 GCancellable *cancellable,
1446 g_return_val_if_fail (G_IS_FILE (file), NULL);
1448 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1451 iface = G_FILE_GET_IFACE (file);
1452 if (iface->find_enclosing_mount == NULL)
1455 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1456 /* Translators: This is an error message when
1457 * trying to find the enclosing (user visible)
1458 * mount of a file, but none exists.
1460 _("Containing mount does not exist"));
1464 return (* iface->find_enclosing_mount) (file, cancellable, error);
1468 * g_file_find_enclosing_mount_async:
1470 * @io_priority: the [I/O priority][io-priority] of the request
1471 * @cancellable: (allow-none): optional #GCancellable object,
1473 * @callback: (scope async): a #GAsyncReadyCallback to call
1474 * when the request is satisfied
1475 * @user_data: (closure): the data to pass to callback function
1477 * Asynchronously gets the mount for the file.
1479 * For more details, see g_file_find_enclosing_mount() which is
1480 * the synchronous version of this call.
1482 * When the operation is finished, @callback will be called.
1483 * You can then call g_file_find_enclosing_mount_finish() to
1484 * get the result of the operation.
1487 g_file_find_enclosing_mount_async (GFile *file,
1489 GCancellable *cancellable,
1490 GAsyncReadyCallback callback,
1495 g_return_if_fail (G_IS_FILE (file));
1497 iface = G_FILE_GET_IFACE (file);
1498 (* iface->find_enclosing_mount_async) (file,
1506 * g_file_find_enclosing_mount_finish:
1508 * @res: a #GAsyncResult
1511 * Finishes an asynchronous find mount request.
1512 * See g_file_find_enclosing_mount_async().
1514 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1515 * Free the returned object with g_object_unref().
1518 g_file_find_enclosing_mount_finish (GFile *file,
1524 g_return_val_if_fail (G_IS_FILE (file), NULL);
1525 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1527 if (g_async_result_legacy_propagate_error (res, error))
1530 iface = G_FILE_GET_IFACE (file);
1531 return (* iface->find_enclosing_mount_finish) (file, res, error);
1537 * @file: #GFile to read
1538 * @cancellable: (allow-none): a #GCancellable
1539 * @error: a #GError, or %NULL
1541 * Opens a file for reading. The result is a #GFileInputStream that
1542 * can be used to read the contents of the file.
1544 * If @cancellable is not %NULL, then the operation can be cancelled by
1545 * triggering the cancellable object from another thread. If the operation
1546 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1548 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1549 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1550 * error will be returned. Other errors are possible too, and depend
1551 * on what kind of filesystem the file is on.
1554 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1555 * Free the returned object with g_object_unref().
1558 g_file_read (GFile *file,
1559 GCancellable *cancellable,
1564 g_return_val_if_fail (G_IS_FILE (file), NULL);
1566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1569 iface = G_FILE_GET_IFACE (file);
1571 if (iface->read_fn == NULL)
1573 g_set_error_literal (error, G_IO_ERROR,
1574 G_IO_ERROR_NOT_SUPPORTED,
1575 _("Operation not supported"));
1579 return (* iface->read_fn) (file, cancellable, error);
1584 * @file: input #GFile
1585 * @flags: a set of #GFileCreateFlags
1586 * @cancellable: (allow-none): optional #GCancellable object,
1588 * @error: a #GError, or %NULL
1590 * Gets an output stream for appending data to the file.
1591 * If the file doesn't already exist it is created.
1593 * By default files created are generally readable by everyone,
1594 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1595 * will be made readable only to the current user, to the level that
1596 * is supported on the target filesystem.
1598 * If @cancellable is not %NULL, then the operation can be cancelled
1599 * by triggering the cancellable object from another thread. If the
1600 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1603 * Some file systems don't allow all file names, and may return an
1604 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1605 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1606 * possible too, and depend on what kind of filesystem the file is on.
1608 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1609 * Free the returned object with g_object_unref().
1612 g_file_append_to (GFile *file,
1613 GFileCreateFlags flags,
1614 GCancellable *cancellable,
1619 g_return_val_if_fail (G_IS_FILE (file), NULL);
1621 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1624 iface = G_FILE_GET_IFACE (file);
1626 if (iface->append_to == NULL)
1628 g_set_error_literal (error, G_IO_ERROR,
1629 G_IO_ERROR_NOT_SUPPORTED,
1630 _("Operation not supported"));
1634 return (* iface->append_to) (file, flags, cancellable, error);
1639 * @file: input #GFile
1640 * @flags: a set of #GFileCreateFlags
1641 * @cancellable: (allow-none): optional #GCancellable object,
1643 * @error: a #GError, or %NULL
1645 * Creates a new file and returns an output stream for writing to it.
1646 * The file must not already exist.
1648 * By default files created are generally readable by everyone,
1649 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1650 * will be made readable only to the current user, to the level
1651 * that is supported on the target filesystem.
1653 * If @cancellable is not %NULL, then the operation can be cancelled
1654 * by triggering the cancellable object from another thread. If the
1655 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1658 * If a file or directory with this name already exists the
1659 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1660 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1661 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1662 * be returned. Other errors are possible too, and depend on what kind
1663 * of filesystem the file is on.
1665 * Returns: (transfer full): a #GFileOutputStream for the newly created
1666 * file, or %NULL on error.
1667 * Free the returned object with g_object_unref().
1670 g_file_create (GFile *file,
1671 GFileCreateFlags flags,
1672 GCancellable *cancellable,
1677 g_return_val_if_fail (G_IS_FILE (file), NULL);
1679 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1682 iface = G_FILE_GET_IFACE (file);
1684 if (iface->create == NULL)
1686 g_set_error_literal (error, G_IO_ERROR,
1687 G_IO_ERROR_NOT_SUPPORTED,
1688 _("Operation not supported"));
1692 return (* iface->create) (file, flags, cancellable, error);
1697 * @file: input #GFile
1698 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1699 * for the current #GFile, or #NULL to ignore
1700 * @make_backup: %TRUE if a backup should be created
1701 * @flags: a set of #GFileCreateFlags
1702 * @cancellable: (allow-none): optional #GCancellable object,
1704 * @error: a #GError, or %NULL
1706 * Returns an output stream for overwriting the file, possibly
1707 * creating a backup copy of the file first. If the file doesn't exist,
1708 * it will be created.
1710 * This will try to replace the file in the safest way possible so
1711 * that any errors during the writing will not affect an already
1712 * existing copy of the file. For instance, for local files it
1713 * may write to a temporary file and then atomically rename over
1714 * the destination when the stream is closed.
1716 * By default files created are generally readable by everyone,
1717 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1718 * will be made readable only to the current user, to the level that
1719 * is supported on the target filesystem.
1721 * If @cancellable is not %NULL, then the operation can be cancelled
1722 * by triggering the cancellable object from another thread. If the
1723 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1726 * If you pass in a non-%NULL @etag value, then this value is
1727 * compared to the current entity tag of the file, and if they differ
1728 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1729 * that the file has been changed since you last read it. You can get
1730 * the new etag from g_file_output_stream_get_etag() after you've
1731 * finished writing and closed the #GFileOutputStream. When you load
1732 * a new file you can use g_file_input_stream_query_info() to get
1733 * the etag of the file.
1735 * If @make_backup is %TRUE, this function will attempt to make a
1736 * backup of the current file before overwriting it. If this fails
1737 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1738 * want to replace anyway, try again with @make_backup set to %FALSE.
1740 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1741 * be returned, and if the file is some other form of non-regular file
1742 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1743 * file systems don't allow all file names, and may return an
1744 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1745 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1746 * possible too, and depend on what kind of filesystem the file is on.
1748 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1749 * Free the returned object with g_object_unref().
1752 g_file_replace (GFile *file,
1754 gboolean make_backup,
1755 GFileCreateFlags flags,
1756 GCancellable *cancellable,
1761 g_return_val_if_fail (G_IS_FILE (file), NULL);
1763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1766 iface = G_FILE_GET_IFACE (file);
1768 if (iface->replace == NULL)
1770 g_set_error_literal (error, G_IO_ERROR,
1771 G_IO_ERROR_NOT_SUPPORTED,
1772 _("Operation not supported"));
1776 /* Handle empty tag string as NULL in consistent way. */
1777 if (etag && *etag == 0)
1780 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1784 * g_file_open_readwrite:
1785 * @file: #GFile to open
1786 * @cancellable: (allow-none): a #GCancellable
1787 * @error: a #GError, or %NULL
1789 * Opens an existing file for reading and writing. The result is
1790 * a #GFileIOStream that can be used to read and write the contents
1793 * If @cancellable is not %NULL, then the operation can be cancelled
1794 * by triggering the cancellable object from another thread. If the
1795 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1798 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1799 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1800 * error will be returned. Other errors are possible too, and depend on
1801 * what kind of filesystem the file is on. Note that in many non-local
1802 * file cases read and write streams are not supported, so make sure you
1803 * really need to do read and write streaming, rather than just opening
1804 * for reading or writing.
1806 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1807 * Free the returned object with g_object_unref().
1812 g_file_open_readwrite (GFile *file,
1813 GCancellable *cancellable,
1818 g_return_val_if_fail (G_IS_FILE (file), NULL);
1820 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1823 iface = G_FILE_GET_IFACE (file);
1825 if (iface->open_readwrite == NULL)
1827 g_set_error_literal (error, G_IO_ERROR,
1828 G_IO_ERROR_NOT_SUPPORTED,
1829 _("Operation not supported"));
1833 return (* iface->open_readwrite) (file, cancellable, error);
1837 * g_file_create_readwrite:
1839 * @flags: a set of #GFileCreateFlags
1840 * @cancellable: (allow-none): optional #GCancellable object,
1842 * @error: return location for a #GError, or %NULL
1844 * Creates a new file and returns a stream for reading and
1845 * writing to it. The file must not already exist.
1847 * By default files created are generally readable by everyone,
1848 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1849 * will be made readable only to the current user, to the level
1850 * that is supported on the target filesystem.
1852 * If @cancellable is not %NULL, then the operation can be cancelled
1853 * by triggering the cancellable object from another thread. If the
1854 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1857 * If a file or directory with this name already exists, the
1858 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1859 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1860 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1861 * will be returned. Other errors are possible too, and depend on what
1862 * kind of filesystem the file is on.
1864 * Note that in many non-local file cases read and write streams are
1865 * not supported, so make sure you really need to do read and write
1866 * streaming, rather than just opening for reading or writing.
1868 * Returns: (transfer full): a #GFileIOStream for the newly created
1869 * file, or %NULL on error.
1870 * Free the returned object with g_object_unref().
1875 g_file_create_readwrite (GFile *file,
1876 GFileCreateFlags flags,
1877 GCancellable *cancellable,
1882 g_return_val_if_fail (G_IS_FILE (file), NULL);
1884 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1887 iface = G_FILE_GET_IFACE (file);
1889 if (iface->create_readwrite == NULL)
1891 g_set_error_literal (error, G_IO_ERROR,
1892 G_IO_ERROR_NOT_SUPPORTED,
1893 _("Operation not supported"));
1897 return (* iface->create_readwrite) (file, flags, cancellable, error);
1901 * g_file_replace_readwrite:
1903 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1904 * for the current #GFile, or #NULL to ignore
1905 * @make_backup: %TRUE if a backup should be created
1906 * @flags: a set of #GFileCreateFlags
1907 * @cancellable: (allow-none): optional #GCancellable object,
1909 * @error: return location for a #GError, or %NULL
1911 * Returns an output stream for overwriting the file in readwrite mode,
1912 * possibly creating a backup copy of the file first. If the file doesn't
1913 * exist, it will be created.
1915 * For details about the behaviour, see g_file_replace() which does the
1916 * same thing but returns an output stream only.
1918 * Note that in many non-local file cases read and write streams are not
1919 * supported, so make sure you really need to do read and write streaming,
1920 * rather than just opening for reading or writing.
1922 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1923 * Free the returned object with g_object_unref().
1928 g_file_replace_readwrite (GFile *file,
1930 gboolean make_backup,
1931 GFileCreateFlags flags,
1932 GCancellable *cancellable,
1937 g_return_val_if_fail (G_IS_FILE (file), NULL);
1939 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1942 iface = G_FILE_GET_IFACE (file);
1944 if (iface->replace_readwrite == NULL)
1946 g_set_error_literal (error, G_IO_ERROR,
1947 G_IO_ERROR_NOT_SUPPORTED,
1948 _("Operation not supported"));
1952 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1956 * g_file_read_async:
1957 * @file: input #GFile
1958 * @io_priority: the [I/O priority][io-priority] of the request
1959 * @cancellable: (allow-none): optional #GCancellable object,
1961 * @callback: (scope async): a #GAsyncReadyCallback to call
1962 * when the request is satisfied
1963 * @user_data: (closure): the data to pass to callback function
1965 * Asynchronously opens @file for reading.
1967 * For more details, see g_file_read() which is
1968 * the synchronous version of this call.
1970 * When the operation is finished, @callback will be called.
1971 * You can then call g_file_read_finish() to get the result
1975 g_file_read_async (GFile *file,
1977 GCancellable *cancellable,
1978 GAsyncReadyCallback callback,
1983 g_return_if_fail (G_IS_FILE (file));
1985 iface = G_FILE_GET_IFACE (file);
1986 (* iface->read_async) (file,
1994 * g_file_read_finish:
1995 * @file: input #GFile
1996 * @res: a #GAsyncResult
1997 * @error: a #GError, or %NULL
1999 * Finishes an asynchronous file read operation started with
2000 * g_file_read_async().
2002 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2003 * Free the returned object with g_object_unref().
2006 g_file_read_finish (GFile *file,
2012 g_return_val_if_fail (G_IS_FILE (file), NULL);
2013 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2015 if (g_async_result_legacy_propagate_error (res, error))
2018 iface = G_FILE_GET_IFACE (file);
2019 return (* iface->read_finish) (file, res, error);
2023 * g_file_append_to_async:
2024 * @file: input #GFile
2025 * @flags: a set of #GFileCreateFlags
2026 * @io_priority: the [I/O priority][io-priority] of the request
2027 * @cancellable: (allow-none): optional #GCancellable object,
2029 * @callback: (scope async): a #GAsyncReadyCallback to call
2030 * when the request is satisfied
2031 * @user_data: (closure): the data to pass to callback function
2033 * Asynchronously opens @file for appending.
2035 * For more details, see g_file_append_to() which is
2036 * the synchronous version of this call.
2038 * When the operation is finished, @callback will be called.
2039 * You can then call g_file_append_to_finish() to get the result
2043 g_file_append_to_async (GFile *file,
2044 GFileCreateFlags flags,
2046 GCancellable *cancellable,
2047 GAsyncReadyCallback callback,
2052 g_return_if_fail (G_IS_FILE (file));
2054 iface = G_FILE_GET_IFACE (file);
2055 (* iface->append_to_async) (file,
2064 * g_file_append_to_finish:
2065 * @file: input #GFile
2066 * @res: #GAsyncResult
2067 * @error: a #GError, or %NULL
2069 * Finishes an asynchronous file append operation started with
2070 * g_file_append_to_async().
2072 * Returns: (transfer full): a valid #GFileOutputStream
2073 * or %NULL on error.
2074 * Free the returned object with g_object_unref().
2077 g_file_append_to_finish (GFile *file,
2083 g_return_val_if_fail (G_IS_FILE (file), NULL);
2084 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2086 if (g_async_result_legacy_propagate_error (res, error))
2089 iface = G_FILE_GET_IFACE (file);
2090 return (* iface->append_to_finish) (file, res, error);
2094 * g_file_create_async:
2095 * @file: input #GFile
2096 * @flags: a set of #GFileCreateFlags
2097 * @io_priority: the [I/O priority][io-priority] of the request
2098 * @cancellable: (allow-none): optional #GCancellable object,
2100 * @callback: (scope async): a #GAsyncReadyCallback to call
2101 * when the request is satisfied
2102 * @user_data: (closure): the data to pass to callback function
2104 * Asynchronously creates a new file and returns an output stream
2105 * for writing to it. The file must not already exist.
2107 * For more details, see g_file_create() which is
2108 * the synchronous version of this call.
2110 * When the operation is finished, @callback will be called.
2111 * You can then call g_file_create_finish() to get the result
2115 g_file_create_async (GFile *file,
2116 GFileCreateFlags flags,
2118 GCancellable *cancellable,
2119 GAsyncReadyCallback callback,
2124 g_return_if_fail (G_IS_FILE (file));
2126 iface = G_FILE_GET_IFACE (file);
2127 (* iface->create_async) (file,
2136 * g_file_create_finish:
2137 * @file: input #GFile
2138 * @res: a #GAsyncResult
2139 * @error: a #GError, or %NULL
2141 * Finishes an asynchronous file create operation started with
2142 * g_file_create_async().
2144 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2145 * Free the returned object with g_object_unref().
2148 g_file_create_finish (GFile *file,
2154 g_return_val_if_fail (G_IS_FILE (file), NULL);
2155 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2157 if (g_async_result_legacy_propagate_error (res, error))
2160 iface = G_FILE_GET_IFACE (file);
2161 return (* iface->create_finish) (file, res, error);
2165 * g_file_replace_async:
2166 * @file: input #GFile
2167 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2168 * or %NULL to ignore
2169 * @make_backup: %TRUE if a backup should be created
2170 * @flags: a set of #GFileCreateFlags
2171 * @io_priority: the [I/O priority][io-priority] of the request
2172 * @cancellable: (allow-none): optional #GCancellable object,
2174 * @callback: (scope async): a #GAsyncReadyCallback to call
2175 * when the request is satisfied
2176 * @user_data: (closure): the data to pass to callback function
2178 * Asynchronously overwrites the file, replacing the contents,
2179 * possibly creating a backup copy of the file first.
2181 * For more details, see g_file_replace() which is
2182 * the synchronous version of this call.
2184 * When the operation is finished, @callback will be called.
2185 * You can then call g_file_replace_finish() to get the result
2189 g_file_replace_async (GFile *file,
2191 gboolean make_backup,
2192 GFileCreateFlags flags,
2194 GCancellable *cancellable,
2195 GAsyncReadyCallback callback,
2200 g_return_if_fail (G_IS_FILE (file));
2202 iface = G_FILE_GET_IFACE (file);
2203 (* iface->replace_async) (file,
2214 * g_file_replace_finish:
2215 * @file: input #GFile
2216 * @res: a #GAsyncResult
2217 * @error: a #GError, or %NULL
2219 * Finishes an asynchronous file replace operation started with
2220 * g_file_replace_async().
2222 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2223 * Free the returned object with g_object_unref().
2226 g_file_replace_finish (GFile *file,
2232 g_return_val_if_fail (G_IS_FILE (file), NULL);
2233 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2235 if (g_async_result_legacy_propagate_error (res, error))
2238 iface = G_FILE_GET_IFACE (file);
2239 return (* iface->replace_finish) (file, res, error);
2243 * g_file_open_readwrite_async
2244 * @file: input #GFile
2245 * @io_priority: the [I/O priority][io-priority] of the request
2246 * @cancellable: (allow-none): optional #GCancellable object,
2248 * @callback: (scope async): a #GAsyncReadyCallback to call
2249 * when the request is satisfied
2250 * @user_data: (closure): the data to pass to callback function
2252 * Asynchronously opens @file for reading and writing.
2254 * For more details, see g_file_open_readwrite() which is
2255 * the synchronous version of this call.
2257 * When the operation is finished, @callback will be called.
2258 * You can then call g_file_open_readwrite_finish() to get
2259 * the result of the operation.
2264 g_file_open_readwrite_async (GFile *file,
2266 GCancellable *cancellable,
2267 GAsyncReadyCallback callback,
2272 g_return_if_fail (G_IS_FILE (file));
2274 iface = G_FILE_GET_IFACE (file);
2275 (* iface->open_readwrite_async) (file,
2283 * g_file_open_readwrite_finish:
2284 * @file: input #GFile
2285 * @res: a #GAsyncResult
2286 * @error: a #GError, or %NULL
2288 * Finishes an asynchronous file read operation started with
2289 * g_file_open_readwrite_async().
2291 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2292 * Free the returned object with g_object_unref().
2297 g_file_open_readwrite_finish (GFile *file,
2303 g_return_val_if_fail (G_IS_FILE (file), NULL);
2304 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2306 if (g_async_result_legacy_propagate_error (res, error))
2309 iface = G_FILE_GET_IFACE (file);
2310 return (* iface->open_readwrite_finish) (file, res, error);
2314 * g_file_create_readwrite_async:
2315 * @file: input #GFile
2316 * @flags: a set of #GFileCreateFlags
2317 * @io_priority: the [I/O priority][io-priority] of the request
2318 * @cancellable: (allow-none): optional #GCancellable object,
2320 * @callback: (scope async): a #GAsyncReadyCallback to call
2321 * when the request is satisfied
2322 * @user_data: (closure): the data to pass to callback function
2324 * Asynchronously creates a new file and returns a stream
2325 * for reading and writing to it. The file must not already exist.
2327 * For more details, see g_file_create_readwrite() which is
2328 * the synchronous version of this call.
2330 * When the operation is finished, @callback will be called.
2331 * You can then call g_file_create_readwrite_finish() to get
2332 * the result of the operation.
2337 g_file_create_readwrite_async (GFile *file,
2338 GFileCreateFlags flags,
2340 GCancellable *cancellable,
2341 GAsyncReadyCallback callback,
2346 g_return_if_fail (G_IS_FILE (file));
2348 iface = G_FILE_GET_IFACE (file);
2349 (* iface->create_readwrite_async) (file,
2358 * g_file_create_readwrite_finish:
2359 * @file: input #GFile
2360 * @res: a #GAsyncResult
2361 * @error: a #GError, or %NULL
2363 * Finishes an asynchronous file create operation started with
2364 * g_file_create_readwrite_async().
2366 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2367 * Free the returned object with g_object_unref().
2372 g_file_create_readwrite_finish (GFile *file,
2378 g_return_val_if_fail (G_IS_FILE (file), NULL);
2379 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2381 if (g_async_result_legacy_propagate_error (res, error))
2384 iface = G_FILE_GET_IFACE (file);
2385 return (* iface->create_readwrite_finish) (file, res, error);
2389 * g_file_replace_readwrite_async:
2390 * @file: input #GFile
2391 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2392 * or %NULL to ignore
2393 * @make_backup: %TRUE if a backup should be created
2394 * @flags: a set of #GFileCreateFlags
2395 * @io_priority: the [I/O priority][io-priority] of the request
2396 * @cancellable: (allow-none): optional #GCancellable object,
2398 * @callback: (scope async): a #GAsyncReadyCallback to call
2399 * when the request is satisfied
2400 * @user_data: (closure): the data to pass to callback function
2402 * Asynchronously overwrites the file in read-write mode,
2403 * replacing the contents, possibly creating a backup copy
2404 * of the file first.
2406 * For more details, see g_file_replace_readwrite() which is
2407 * the synchronous version of this call.
2409 * When the operation is finished, @callback will be called.
2410 * You can then call g_file_replace_readwrite_finish() to get
2411 * the result of the operation.
2416 g_file_replace_readwrite_async (GFile *file,
2418 gboolean make_backup,
2419 GFileCreateFlags flags,
2421 GCancellable *cancellable,
2422 GAsyncReadyCallback callback,
2427 g_return_if_fail (G_IS_FILE (file));
2429 iface = G_FILE_GET_IFACE (file);
2430 (* iface->replace_readwrite_async) (file,
2441 * g_file_replace_readwrite_finish:
2442 * @file: input #GFile
2443 * @res: a #GAsyncResult
2444 * @error: a #GError, or %NULL
2446 * Finishes an asynchronous file replace operation started with
2447 * g_file_replace_readwrite_async().
2449 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2450 * Free the returned object with g_object_unref().
2455 g_file_replace_readwrite_finish (GFile *file,
2461 g_return_val_if_fail (G_IS_FILE (file), NULL);
2462 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2464 if (g_async_result_legacy_propagate_error (res, error))
2467 iface = G_FILE_GET_IFACE (file);
2468 return (* iface->replace_readwrite_finish) (file, res, error);
2472 copy_symlink (GFile *destination,
2473 GFileCopyFlags flags,
2474 GCancellable *cancellable,
2479 gboolean tried_delete;
2481 GFileType file_type;
2483 tried_delete = FALSE;
2487 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2489 /* Maybe it already existed, and we want to overwrite? */
2490 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2491 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2493 g_clear_error (&my_error);
2495 /* Don't overwrite if the destination is a directory */
2496 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2497 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2498 cancellable, &my_error);
2501 file_type = g_file_info_get_file_type (info);
2502 g_object_unref (info);
2504 if (file_type == G_FILE_TYPE_DIRECTORY)
2506 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2507 _("Can't copy over directory"));
2512 if (!g_file_delete (destination, cancellable, error))
2515 tried_delete = TRUE;
2519 g_propagate_error (error, my_error);
2526 static GFileInputStream *
2527 open_source_for_copy (GFile *source,
2529 GFileCopyFlags flags,
2530 GCancellable *cancellable,
2534 GFileInputStream *ret;
2536 GFileType file_type;
2539 ret = g_file_read (source, cancellable, &my_error);
2543 /* There was an error opening the source, try to set a good error for it: */
2544 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2546 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2547 * as that is less useful to the app. Better check for errors on the
2550 g_error_free (my_error);
2553 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2554 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2555 cancellable, &my_error);
2557 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2559 file_type = g_file_info_get_file_type (info);
2560 g_object_unref (info);
2562 if (flags & G_FILE_COPY_OVERWRITE)
2564 if (file_type == G_FILE_TYPE_DIRECTORY)
2566 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2567 _("Can't copy directory over directory"));
2570 /* continue to would_recurse error */
2574 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2575 _("Target file exists"));
2581 /* Error getting info from target, return that error
2582 * (except for NOT_FOUND, which is no error here)
2584 g_clear_object (&info);
2585 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2587 g_propagate_error (error, my_error);
2590 g_clear_error (&my_error);
2593 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2594 _("Can't recursively copy directory"));
2598 g_propagate_error (error, my_error);
2603 should_copy (GFileAttributeInfo *info,
2604 gboolean copy_all_attributes,
2605 gboolean skip_perms)
2607 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2610 if (copy_all_attributes)
2611 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2612 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2616 build_attribute_list_for_copy (GFile *file,
2617 GFileCopyFlags flags,
2618 char **out_attributes,
2619 GCancellable *cancellable,
2622 gboolean ret = FALSE;
2623 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2627 gboolean copy_all_attributes;
2628 gboolean skip_perms;
2630 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2631 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2633 /* Ignore errors here, if the target supports no attributes there is
2634 * nothing to copy. We still honor the cancellable though.
2636 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2637 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2640 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2641 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2644 if (attributes == NULL && namespaces == NULL)
2648 s = g_string_new ("");
2652 for (i = 0; i < attributes->n_infos; i++)
2654 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2659 g_string_append_c (s, ',');
2661 g_string_append (s, attributes->infos[i].name);
2668 for (i = 0; i < namespaces->n_infos; i++)
2670 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2675 g_string_append_c (s, ',');
2677 g_string_append (s, namespaces->infos[i].name);
2678 g_string_append (s, "::*");
2684 *out_attributes = g_string_free (s, FALSE);
2688 g_string_free (s, TRUE);
2690 g_file_attribute_info_list_unref (attributes);
2692 g_file_attribute_info_list_unref (namespaces);
2698 * g_file_copy_attributes:
2699 * @source: a #GFile with attributes
2700 * @destination: a #GFile to copy attributes to
2701 * @flags: a set of #GFileCopyFlags
2702 * @cancellable: (allow-none): optional #GCancellable object,
2704 * @error: a #GError, %NULL to ignore
2706 * Copies the file attributes from @source to @destination.
2708 * Normally only a subset of the file attributes are copied,
2709 * those that are copies in a normal file copy operation
2710 * (which for instance does not include e.g. owner). However
2711 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2712 * all the metadata that is possible to copy is copied. This
2713 * is useful when implementing move by copy + delete source.
2715 * Returns: %TRUE if the attributes were copied successfully,
2719 g_file_copy_attributes (GFile *source,
2721 GFileCopyFlags flags,
2722 GCancellable *cancellable,
2725 char *attrs_to_read;
2728 gboolean source_nofollow_symlinks;
2730 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2731 cancellable, error))
2734 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2736 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2737 * we just don't copy it.
2739 info = g_file_query_info (source, attrs_to_read,
2740 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2744 g_free (attrs_to_read);
2749 res = g_file_set_attributes_from_info (destination,
2751 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2754 g_object_unref (info);
2761 copy_stream_with_progress (GInputStream *in,
2764 GCancellable *cancellable,
2765 GFileProgressCallback progress_callback,
2766 gpointer progress_callback_data,
2769 gssize n_read, n_written;
2770 goffset current_size;
2771 char buffer[1024*64], *p;
2777 /* avoid performance impact of querying total size when it's not needed */
2778 if (progress_callback)
2780 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2781 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2785 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2786 total_size = g_file_info_get_size (info);
2787 g_object_unref (info);
2790 if (total_size == -1)
2792 info = g_file_query_info (source,
2793 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2794 G_FILE_QUERY_INFO_NONE,
2798 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2799 total_size = g_file_info_get_size (info);
2800 g_object_unref (info);
2805 if (total_size == -1)
2812 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2822 current_size += n_read;
2827 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2828 if (n_written == -1)
2835 n_read -= n_written;
2841 if (progress_callback)
2842 progress_callback (current_size, total_size, progress_callback_data);
2845 /* Make sure we send full copied size */
2846 if (progress_callback)
2847 progress_callback (current_size, total_size, progress_callback_data);
2855 do_splice (int fd_in,
2860 long *bytes_transferd,
2866 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2874 else if (errsv == ENOSYS || errsv == EINVAL)
2875 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2876 _("Splice not supported"));
2878 g_set_error (error, G_IO_ERROR,
2879 g_io_error_from_errno (errsv),
2880 _("Error splicing file: %s"),
2881 g_strerror (errsv));
2886 *bytes_transferd = result;
2891 splice_stream_with_progress (GInputStream *in,
2893 GCancellable *cancellable,
2894 GFileProgressCallback progress_callback,
2895 gpointer progress_callback_data,
2898 int buffer[2] = { -1, -1 };
2905 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2906 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2908 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2912 /* avoid performance impact of querying total size when it's not needed */
2913 if (progress_callback)
2917 if (fstat (fd_in, &sbuf) == 0)
2918 total_size = sbuf.st_size;
2921 if (total_size == -1)
2924 offset_in = offset_out = 0;
2931 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2934 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2945 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2948 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2951 n_read -= n_written;
2954 if (progress_callback)
2955 progress_callback (offset_in, total_size, progress_callback_data);
2958 /* Make sure we send full copied size */
2959 if (progress_callback)
2960 progress_callback (offset_in, total_size, progress_callback_data);
2962 if (!g_close (buffer[0], error))
2965 if (!g_close (buffer[1], error))
2969 if (buffer[0] != -1)
2970 (void) g_close (buffer[0], NULL);
2971 if (buffer[1] != -1)
2972 (void) g_close (buffer[1], NULL);
2980 btrfs_reflink_with_progress (GInputStream *in,
2983 GCancellable *cancellable,
2984 GFileProgressCallback progress_callback,
2985 gpointer progress_callback_data,
2988 goffset source_size;
2992 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2993 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2995 if (progress_callback)
2996 source_size = g_file_info_get_size (info);
2998 /* Btrfs clone ioctl properties:
2999 * - Works at the inode level
3000 * - Doesn't work with directories
3001 * - Always follows symlinks (source and destination)
3003 * By the time we get here, *in and *out are both regular files */
3004 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3009 g_set_error_literal (error, G_IO_ERROR,
3010 G_IO_ERROR_NOT_SUPPORTED,
3011 _("Copy (reflink/clone) between mounts is not supported"));
3012 else if (errno == EINVAL)
3013 g_set_error_literal (error, G_IO_ERROR,
3014 G_IO_ERROR_NOT_SUPPORTED,
3015 _("Copy (reflink/clone) is not supported or invalid"));
3017 /* Most probably something odd happened; retry with fallback */
3018 g_set_error_literal (error, G_IO_ERROR,
3019 G_IO_ERROR_NOT_SUPPORTED,
3020 _("Copy (reflink/clone) is not supported or didn't work"));
3021 /* We retry with fallback for all error cases because Btrfs is currently
3022 * unstable, and so we can't trust it to do clone properly.
3023 * In addition, any hard errors here would cause the same failure in the
3024 * fallback manual copy as well. */
3028 /* Make sure we send full copied size */
3029 if (progress_callback)
3030 progress_callback (source_size, source_size, progress_callback_data);
3037 file_copy_fallback (GFile *source,
3039 GFileCopyFlags flags,
3040 GCancellable *cancellable,
3041 GFileProgressCallback progress_callback,
3042 gpointer progress_callback_data,
3045 gboolean ret = FALSE;
3046 GFileInputStream *file_in = NULL;
3047 GInputStream *in = NULL;
3048 GOutputStream *out = NULL;
3049 GFileInfo *info = NULL;
3051 char *attrs_to_read;
3052 gboolean do_set_attributes = FALSE;
3054 /* need to know the file type */
3055 info = g_file_query_info (source,
3056 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3057 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3063 /* Maybe copy the symlink? */
3064 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3065 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3067 target = g_file_info_get_symlink_target (info);
3070 if (!copy_symlink (destination, flags, cancellable, target, error))
3076 /* ... else fall back on a regular file copy */
3078 /* Handle "special" files (pipes, device nodes, ...)? */
3079 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3081 /* FIXME: could try to recreate device nodes and others? */
3082 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3083 _("Can't copy special file"));
3087 /* Everything else should just fall back on a regular copy. */
3089 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3092 in = G_INPUT_STREAM (file_in);
3094 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3095 cancellable, error))
3098 if (attrs_to_read != NULL)
3100 GError *tmp_error = NULL;
3102 /* Ok, ditch the previous lightweight info (on Unix we just
3103 * called lstat()); at this point we gather all the information
3104 * we need about the source from the opened file descriptor.
3106 g_object_unref (info);
3108 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3109 cancellable, &tmp_error);
3112 /* Not all gvfs backends implement query_info_on_read(), we
3113 * can just fall back to the pathname again.
3114 * https://bugzilla.gnome.org/706254
3116 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3118 g_clear_error (&tmp_error);
3119 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3120 cancellable, error);
3124 g_free (attrs_to_read);
3125 g_propagate_error (error, tmp_error);
3129 g_free (attrs_to_read);
3133 do_set_attributes = TRUE;
3136 /* In the local file path, we pass down the source info which
3137 * includes things like unix::mode, to ensure that the target file
3138 * is not created with different permissions from the source file.
3140 * If a future API like g_file_replace_with_info() is added, switch
3141 * this code to use that.
3143 if (G_IS_LOCAL_FILE (destination))
3145 if (flags & G_FILE_COPY_OVERWRITE)
3146 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3148 flags & G_FILE_COPY_BACKUP,
3149 G_FILE_CREATE_REPLACE_DESTINATION,
3151 cancellable, error);
3153 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3155 cancellable, error);
3157 else if (flags & G_FILE_COPY_OVERWRITE)
3159 out = (GOutputStream *)g_file_replace (destination,
3161 flags & G_FILE_COPY_BACKUP,
3162 G_FILE_CREATE_REPLACE_DESTINATION,
3163 cancellable, error);
3167 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3174 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3176 GError *reflink_err = NULL;
3178 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3179 progress_callback, progress_callback_data,
3182 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3184 g_clear_error (&reflink_err);
3188 g_propagate_error (error, reflink_err);
3201 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3203 GError *splice_err = NULL;
3205 if (!splice_stream_with_progress (in, out, cancellable,
3206 progress_callback, progress_callback_data,
3209 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3211 g_clear_error (&splice_err);
3215 g_propagate_error (error, splice_err);
3228 /* A plain read/write loop */
3229 if (!copy_stream_with_progress (in, out, source, cancellable,
3230 progress_callback, progress_callback_data,
3238 /* Don't care about errors in source here */
3239 (void) g_input_stream_close (in, cancellable, NULL);
3240 g_object_unref (in);
3245 /* But write errors on close are bad! */
3246 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3248 g_object_unref (out);
3251 /* Ignore errors here. Failure to copy metadata is not a hard error */
3252 /* TODO: set these attributes /before/ we do the rename() on Unix */
3253 if (ret && do_set_attributes)
3255 g_file_set_attributes_from_info (destination,
3257 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3262 g_clear_object (&info);
3269 * @source: input #GFile
3270 * @destination: destination #GFile
3271 * @flags: set of #GFileCopyFlags
3272 * @cancellable: (allow-none): optional #GCancellable object,
3274 * @progress_callback: (allow-none) (scope call): function to callback with
3275 * progress information, or %NULL if progress information is not needed
3276 * @progress_callback_data: (closure): user data to pass to @progress_callback
3277 * @error: #GError to set on error, or %NULL
3279 * Copies the file @source to the location specified by @destination.
3280 * Can not handle recursive copies of directories.
3282 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3283 * existing @destination file is overwritten.
3285 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3286 * will be copied as symlinks, otherwise the target of the
3287 * @source symlink will be copied.
3289 * If @cancellable is not %NULL, then the operation can be cancelled by
3290 * triggering the cancellable object from another thread. If the operation
3291 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3293 * If @progress_callback is not %NULL, then the operation can be monitored
3294 * by setting this to a #GFileProgressCallback function.
3295 * @progress_callback_data will be passed to this function. It is guaranteed
3296 * that this callback will be called after all data has been transferred with
3297 * the total number of bytes copied during the operation.
3299 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3300 * is returned, independent on the status of the @destination.
3302 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3303 * the error %G_IO_ERROR_EXISTS is returned.
3305 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3306 * error is returned. If trying to overwrite a directory with a directory the
3307 * %G_IO_ERROR_WOULD_MERGE error is returned.
3309 * If the source is a directory and the target does not exist, or
3310 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3311 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3313 * If you are interested in copying the #GFile object itself (not the on-disk
3314 * file), see g_file_dup().
3316 * Returns: %TRUE on success, %FALSE otherwise.
3319 g_file_copy (GFile *source,
3321 GFileCopyFlags flags,
3322 GCancellable *cancellable,
3323 GFileProgressCallback progress_callback,
3324 gpointer progress_callback_data,
3331 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3332 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3334 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3337 iface = G_FILE_GET_IFACE (destination);
3341 res = (* iface->copy) (source, destination,
3343 progress_callback, progress_callback_data,
3349 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3351 g_propagate_error (error, my_error);
3355 g_clear_error (&my_error);
3358 /* If the types are different, and the destination method failed
3359 * also try the source method
3361 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3363 iface = G_FILE_GET_IFACE (source);
3368 res = (* iface->copy) (source, destination,
3370 progress_callback, progress_callback_data,
3376 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3378 g_propagate_error (error, my_error);
3382 g_clear_error (&my_error);
3386 return file_copy_fallback (source, destination, flags, cancellable,
3387 progress_callback, progress_callback_data,
3392 * g_file_copy_async: (skip)
3393 * @source: input #GFile
3394 * @destination: destination #GFile
3395 * @flags: set of #GFileCopyFlags
3396 * @io_priority: the [I/O priority][io-priority] of the request
3397 * @cancellable: (allow-none): optional #GCancellable object,
3399 * @progress_callback: (allow-none): function to callback with progress
3400 * information, or %NULL if progress information is not needed
3401 * @progress_callback_data: (closure): user data to pass to @progress_callback
3402 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3403 * @user_data: the data to pass to callback function
3405 * Copies the file @source to the location specified by @destination
3406 * asynchronously. For details of the behaviour, see g_file_copy().
3408 * If @progress_callback is not %NULL, then that function that will be called
3409 * just like in g_file_copy(), however the callback will run in the main loop,
3410 * not in the thread that is doing the I/O operation.
3412 * When the operation is finished, @callback will be called. You can then call
3413 * g_file_copy_finish() to get the result of the operation.
3416 g_file_copy_async (GFile *source,
3418 GFileCopyFlags flags,
3420 GCancellable *cancellable,
3421 GFileProgressCallback progress_callback,
3422 gpointer progress_callback_data,
3423 GAsyncReadyCallback callback,
3428 g_return_if_fail (G_IS_FILE (source));
3429 g_return_if_fail (G_IS_FILE (destination));
3431 iface = G_FILE_GET_IFACE (source);
3432 (* iface->copy_async) (source,
3438 progress_callback_data,
3444 * g_file_copy_finish:
3445 * @file: input #GFile
3446 * @res: a #GAsyncResult
3447 * @error: a #GError, or %NULL
3449 * Finishes copying the file started with g_file_copy_async().
3451 * Returns: a %TRUE on success, %FALSE on error.
3454 g_file_copy_finish (GFile *file,
3460 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3461 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3463 if (g_async_result_legacy_propagate_error (res, error))
3466 iface = G_FILE_GET_IFACE (file);
3467 return (* iface->copy_finish) (file, res, error);
3472 * @source: #GFile pointing to the source location
3473 * @destination: #GFile pointing to the destination location
3474 * @flags: set of #GFileCopyFlags
3475 * @cancellable: (allow-none): optional #GCancellable object,
3477 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3478 * function for updates
3479 * @progress_callback_data: (closure): gpointer to user data for
3480 * the callback function
3481 * @error: #GError for returning error conditions, or %NULL
3483 * Tries to move the file or directory @source to the location specified
3484 * by @destination. If native move operations are supported then this is
3485 * used, otherwise a copy + delete fallback is used. The native
3486 * implementation may support moving directories (for instance on moves
3487 * inside the same filesystem), but the fallback code does not.
3489 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3490 * existing @destination file is overwritten.
3492 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3493 * will be copied as symlinks, otherwise the target of the
3494 * @source symlink will be copied.
3496 * If @cancellable is not %NULL, then the operation can be cancelled by
3497 * triggering the cancellable object from another thread. If the operation
3498 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3500 * If @progress_callback is not %NULL, then the operation can be monitored
3501 * by setting this to a #GFileProgressCallback function.
3502 * @progress_callback_data will be passed to this function. It is
3503 * guaranteed that this callback will be called after all data has been
3504 * transferred with the total number of bytes copied during the operation.
3506 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3507 * error is returned, independent on the status of the @destination.
3509 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3510 * then the error %G_IO_ERROR_EXISTS is returned.
3512 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3513 * error is returned. If trying to overwrite a directory with a directory the
3514 * %G_IO_ERROR_WOULD_MERGE error is returned.
3516 * If the source is a directory and the target does not exist, or
3517 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3518 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3519 * move operation isn't available).
3521 * Returns: %TRUE on successful move, %FALSE otherwise.
3524 g_file_move (GFile *source,
3526 GFileCopyFlags flags,
3527 GCancellable *cancellable,
3528 GFileProgressCallback progress_callback,
3529 gpointer progress_callback_data,
3536 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3537 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3539 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3542 iface = G_FILE_GET_IFACE (destination);
3546 res = (* iface->move) (source, destination,
3548 progress_callback, progress_callback_data,
3554 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3556 g_propagate_error (error, my_error);
3561 /* If the types are different, and the destination method failed
3562 * also try the source method
3564 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3566 iface = G_FILE_GET_IFACE (source);
3571 res = (* iface->move) (source, destination,
3573 progress_callback, progress_callback_data,
3579 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3581 g_propagate_error (error, my_error);
3587 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3589 g_set_error_literal (error, G_IO_ERROR,
3590 G_IO_ERROR_NOT_SUPPORTED,
3591 _("Operation not supported"));
3595 flags |= G_FILE_COPY_ALL_METADATA;
3596 if (!g_file_copy (source, destination, flags, cancellable,
3597 progress_callback, progress_callback_data,
3601 return g_file_delete (source, cancellable, error);
3605 * g_file_make_directory:
3606 * @file: input #GFile
3607 * @cancellable: (allow-none): optional #GCancellable object,
3609 * @error: a #GError, or %NULL
3611 * Creates a directory. Note that this will only create a child directory
3612 * of the immediate parent directory of the path or URI given by the #GFile.
3613 * To recursively create directories, see g_file_make_directory_with_parents().
3614 * This function will fail if the parent directory does not exist, setting
3615 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3616 * creating directories, this function will fail, setting @error to
3617 * %G_IO_ERROR_NOT_SUPPORTED.
3619 * For a local #GFile the newly created directory will have the default
3620 * (current) ownership and permissions of the current process.
3622 * If @cancellable is not %NULL, then the operation can be cancelled by
3623 * triggering the cancellable object from another thread. If the operation
3624 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3626 * Returns: %TRUE on successful creation, %FALSE otherwise.
3629 g_file_make_directory (GFile *file,
3630 GCancellable *cancellable,
3635 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3637 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3640 iface = G_FILE_GET_IFACE (file);
3642 if (iface->make_directory == NULL)
3644 g_set_error_literal (error, G_IO_ERROR,
3645 G_IO_ERROR_NOT_SUPPORTED,
3646 _("Operation not supported"));
3650 return (* iface->make_directory) (file, cancellable, error);
3654 * g_file_make_directory_async:
3655 * @file: input #GFile
3656 * @io_priority: the [I/O priority][io-priority] of the request
3657 * @cancellable: (allow-none): optional #GCancellable object,
3659 * @callback: a #GAsyncReadyCallback to call
3660 * when the request is satisfied
3661 * @user_data: the data to pass to callback function
3663 * Asynchronously creates a directory.
3665 * Virtual: make_directory_async
3669 g_file_make_directory_async (GFile *file,
3671 GCancellable *cancellable,
3672 GAsyncReadyCallback callback,
3677 g_return_if_fail (G_IS_FILE (file));
3679 iface = G_FILE_GET_IFACE (file);
3680 (* iface->make_directory_async) (file,
3688 * g_file_make_directory_finish:
3689 * @file: input #GFile
3690 * @result: a #GAsyncResult
3691 * @error: a #GError, or %NULL
3693 * Finishes an asynchronous directory creation, started with
3694 * g_file_make_directory_async().
3696 * Virtual: make_directory_finish
3697 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3701 g_file_make_directory_finish (GFile *file,
3702 GAsyncResult *result,
3707 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3708 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3710 iface = G_FILE_GET_IFACE (file);
3711 return (* iface->make_directory_finish) (file, result, error);
3715 * g_file_make_directory_with_parents:
3716 * @file: input #GFile
3717 * @cancellable: (allow-none): optional #GCancellable object,
3719 * @error: a #GError, or %NULL
3721 * Creates a directory and any parent directories that may not
3722 * exist similar to 'mkdir -p'. If the file system does not support
3723 * creating directories, this function will fail, setting @error to
3724 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3725 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3726 * the similar g_mkdir_with_parents().
3728 * For a local #GFile the newly created directories will have the default
3729 * (current) ownership and permissions of the current process.
3731 * If @cancellable is not %NULL, then the operation can be cancelled by
3732 * triggering the cancellable object from another thread. If the operation
3733 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3735 * Returns: %TRUE if all directories have been successfully created, %FALSE
3741 g_file_make_directory_with_parents (GFile *file,
3742 GCancellable *cancellable,
3745 GFile *work_file = NULL;
3746 GList *list = NULL, *l;
3747 GError *my_error = NULL;
3749 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3751 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3754 g_file_make_directory (file, cancellable, &my_error);
3755 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3758 g_propagate_error (error, my_error);
3759 return my_error == NULL;
3762 work_file = g_object_ref (file);
3764 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3768 parent_file = g_file_get_parent (work_file);
3769 if (parent_file == NULL)
3772 g_clear_error (&my_error);
3773 g_file_make_directory (parent_file, cancellable, &my_error);
3775 g_object_unref (work_file);
3776 work_file = g_object_ref (parent_file);
3778 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3779 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3781 g_object_unref (parent_file);
3784 for (l = list; my_error == NULL && l; l = l->next)
3786 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3790 g_object_unref (work_file);
3793 while (list != NULL)
3795 g_object_unref ((GFile *) list->data);
3796 list = g_list_remove (list, list->data);
3799 if (my_error != NULL)
3801 g_propagate_error (error, my_error);
3805 return g_file_make_directory (file, cancellable, error);
3809 * g_file_make_symbolic_link:
3810 * @file: a #GFile with the name of the symlink to create
3811 * @symlink_value: a string with the path for the target of the new symlink
3812 * @cancellable: (allow-none): optional #GCancellable object,
3816 * Creates a symbolic link named @file which contains the string
3819 * If @cancellable is not %NULL, then the operation can be cancelled by
3820 * triggering the cancellable object from another thread. If the operation
3821 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3823 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3826 g_file_make_symbolic_link (GFile *file,
3827 const char *symlink_value,
3828 GCancellable *cancellable,
3833 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3834 g_return_val_if_fail (symlink_value != NULL, FALSE);
3836 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3839 if (*symlink_value == '\0')
3841 g_set_error_literal (error, G_IO_ERROR,
3842 G_IO_ERROR_INVALID_ARGUMENT,
3843 _("Invalid symlink value given"));
3847 iface = G_FILE_GET_IFACE (file);
3849 if (iface->make_symbolic_link == NULL)
3851 g_set_error_literal (error, G_IO_ERROR,
3852 G_IO_ERROR_NOT_SUPPORTED,
3853 _("Operation not supported"));
3857 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3862 * @file: input #GFile
3863 * @cancellable: (allow-none): optional #GCancellable object,
3865 * @error: a #GError, or %NULL
3867 * Deletes a file. If the @file is a directory, it will only be
3868 * deleted if it is empty. This has the same semantics as g_unlink().
3870 * If @cancellable is not %NULL, then the operation can be cancelled by
3871 * triggering the cancellable object from another thread. If the operation
3872 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3874 * Virtual: delete_file
3875 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3878 g_file_delete (GFile *file,
3879 GCancellable *cancellable,
3884 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3886 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3889 iface = G_FILE_GET_IFACE (file);
3891 if (iface->delete_file == NULL)
3893 g_set_error_literal (error, G_IO_ERROR,
3894 G_IO_ERROR_NOT_SUPPORTED,
3895 _("Operation not supported"));
3899 return (* iface->delete_file) (file, cancellable, error);
3903 * g_file_delete_async:
3904 * @file: input #GFile
3905 * @io_priority: the [I/O priority][io-priority] of the request
3906 * @cancellable: (allow-none): optional #GCancellable object,
3908 * @callback: a #GAsyncReadyCallback to call
3909 * when the request is satisfied
3910 * @user_data: the data to pass to callback function
3912 * Asynchronously delete a file. If the @file is a directory, it will
3913 * only be deleted if it is empty. This has the same semantics as
3916 * Virtual: delete_file_async
3920 g_file_delete_async (GFile *file,
3922 GCancellable *cancellable,
3923 GAsyncReadyCallback callback,
3928 g_return_if_fail (G_IS_FILE (file));
3930 iface = G_FILE_GET_IFACE (file);
3931 (* iface->delete_file_async) (file,
3939 * g_file_delete_finish:
3940 * @file: input #GFile
3941 * @result: a #GAsyncResult
3942 * @error: a #GError, or %NULL
3944 * Finishes deleting a file started with g_file_delete_async().
3946 * Virtual: delete_file_finish
3947 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3951 g_file_delete_finish (GFile *file,
3952 GAsyncResult *result,
3957 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3958 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3960 if (g_async_result_legacy_propagate_error (result, error))
3963 iface = G_FILE_GET_IFACE (file);
3964 return (* iface->delete_file_finish) (file, result, error);
3969 * @file: #GFile to send to trash
3970 * @cancellable: (allow-none): optional #GCancellable object,
3972 * @error: a #GError, or %NULL
3974 * Sends @file to the "Trashcan", if possible. This is similar to
3975 * deleting it, but the user can recover it before emptying the trashcan.
3976 * Not all file systems support trashing, so this call can return the
3977 * %G_IO_ERROR_NOT_SUPPORTED error.
3979 * If @cancellable is not %NULL, then the operation can be cancelled by
3980 * triggering the cancellable object from another thread. If the operation
3981 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3984 * Returns: %TRUE on successful trash, %FALSE otherwise.
3987 g_file_trash (GFile *file,
3988 GCancellable *cancellable,
3993 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3995 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3998 iface = G_FILE_GET_IFACE (file);
4000 if (iface->trash == NULL)
4002 g_set_error_literal (error,
4003 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4004 _("Trash not supported"));
4008 return (* iface->trash) (file, cancellable, error);
4012 * g_file_trash_async:
4013 * @file: input #GFile
4014 * @io_priority: the [I/O priority][io-priority] of the request
4015 * @cancellable: (allow-none): optional #GCancellable object,
4017 * @callback: a #GAsyncReadyCallback to call
4018 * when the request is satisfied
4019 * @user_data: the data to pass to callback function
4021 * Asynchronously sends @file to the Trash location, if possible.
4023 * Virtual: trash_async
4027 g_file_trash_async (GFile *file,
4029 GCancellable *cancellable,
4030 GAsyncReadyCallback callback,
4035 g_return_if_fail (G_IS_FILE (file));
4037 iface = G_FILE_GET_IFACE (file);
4038 (* iface->trash_async) (file,
4046 * g_file_trash_finish:
4047 * @file: input #GFile
4048 * @result: a #GAsyncResult
4049 * @error: a #GError, or %NULL
4051 * Finishes an asynchronous file trashing operation, started with
4052 * g_file_trash_async().
4054 * Virtual: trash_finish
4055 * Returns: %TRUE on successful trash, %FALSE otherwise.
4059 g_file_trash_finish (GFile *file,
4060 GAsyncResult *result,
4065 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4066 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4068 iface = G_FILE_GET_IFACE (file);
4069 return (* iface->trash_finish) (file, result, error);
4073 * g_file_set_display_name:
4074 * @file: input #GFile
4075 * @display_name: a string
4076 * @cancellable: (allow-none): optional #GCancellable object,
4078 * @error: a #GError, or %NULL
4080 * Renames @file to the specified display name.
4082 * The display name is converted from UTF-8 to the correct encoding
4083 * for the target filesystem if possible and the @file is renamed to this.
4085 * If you want to implement a rename operation in the user interface the
4086 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4087 * initial value in the rename widget, and then the result after editing
4088 * should be passed to g_file_set_display_name().
4090 * On success the resulting converted filename is returned.
4092 * If @cancellable is not %NULL, then the operation can be cancelled by
4093 * triggering the cancellable object from another thread. If the operation
4094 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4096 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4097 * or %NULL if there was an error.
4098 * Free the returned object with g_object_unref().
4101 g_file_set_display_name (GFile *file,
4102 const gchar *display_name,
4103 GCancellable *cancellable,
4108 g_return_val_if_fail (G_IS_FILE (file), NULL);
4109 g_return_val_if_fail (display_name != NULL, NULL);
4111 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4115 G_IO_ERROR_INVALID_ARGUMENT,
4116 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4120 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4123 iface = G_FILE_GET_IFACE (file);
4125 return (* iface->set_display_name) (file, display_name, cancellable, error);
4129 * g_file_set_display_name_async:
4130 * @file: input #GFile
4131 * @display_name: a string
4132 * @io_priority: the [I/O priority][io-priority] of the request
4133 * @cancellable: (allow-none): optional #GCancellable object,
4135 * @callback: (scope async): a #GAsyncReadyCallback to call
4136 * when the request is satisfied
4137 * @user_data: (closure): the data to pass to callback function
4139 * Asynchronously sets the display name for a given #GFile.
4141 * For more details, see g_file_set_display_name() which is
4142 * the synchronous version of this call.
4144 * When the operation is finished, @callback will be called.
4145 * You can then call g_file_set_display_name_finish() to get
4146 * the result of the operation.
4149 g_file_set_display_name_async (GFile *file,
4150 const gchar *display_name,
4152 GCancellable *cancellable,
4153 GAsyncReadyCallback callback,
4158 g_return_if_fail (G_IS_FILE (file));
4159 g_return_if_fail (display_name != NULL);
4161 iface = G_FILE_GET_IFACE (file);
4162 (* iface->set_display_name_async) (file,
4171 * g_file_set_display_name_finish:
4172 * @file: input #GFile
4173 * @res: a #GAsyncResult
4174 * @error: a #GError, or %NULL
4176 * Finishes setting a display name started with
4177 * g_file_set_display_name_async().
4179 * Returns: (transfer full): a #GFile or %NULL on error.
4180 * Free the returned object with g_object_unref().
4183 g_file_set_display_name_finish (GFile *file,
4189 g_return_val_if_fail (G_IS_FILE (file), NULL);
4190 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4192 if (g_async_result_legacy_propagate_error (res, error))
4195 iface = G_FILE_GET_IFACE (file);
4196 return (* iface->set_display_name_finish) (file, res, error);
4200 * g_file_query_settable_attributes:
4201 * @file: input #GFile
4202 * @cancellable: (allow-none): optional #GCancellable object,
4204 * @error: a #GError, or %NULL
4206 * Obtain the list of settable attributes for the file.
4208 * Returns the type and full attribute name of all the attributes
4209 * that can be set on this file. This doesn't mean setting it will
4210 * always succeed though, you might get an access failure, or some
4211 * specific file may not support a specific attribute.
4213 * If @cancellable is not %NULL, then the operation can be cancelled by
4214 * triggering the cancellable object from another thread. If the operation
4215 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4217 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4218 * When you are done with it, release it with
4219 * g_file_attribute_info_list_unref()
4221 GFileAttributeInfoList *
4222 g_file_query_settable_attributes (GFile *file,
4223 GCancellable *cancellable,
4228 GFileAttributeInfoList *list;
4230 g_return_val_if_fail (G_IS_FILE (file), NULL);
4232 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4235 iface = G_FILE_GET_IFACE (file);
4237 if (iface->query_settable_attributes == NULL)
4238 return g_file_attribute_info_list_new ();
4241 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4245 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4247 list = g_file_attribute_info_list_new ();
4248 g_error_free (my_error);
4251 g_propagate_error (error, my_error);
4258 * g_file_query_writable_namespaces:
4259 * @file: input #GFile
4260 * @cancellable: (allow-none): optional #GCancellable object,
4262 * @error: a #GError, or %NULL
4264 * Obtain the list of attribute namespaces where new attributes
4265 * can be created by a user. An example of this is extended
4266 * attributes (in the "xattr" namespace).
4268 * If @cancellable is not %NULL, then the operation can be cancelled by
4269 * triggering the cancellable object from another thread. If the operation
4270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4272 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4273 * When you are done with it, release it with
4274 * g_file_attribute_info_list_unref()
4276 GFileAttributeInfoList *
4277 g_file_query_writable_namespaces (GFile *file,
4278 GCancellable *cancellable,
4283 GFileAttributeInfoList *list;
4285 g_return_val_if_fail (G_IS_FILE (file), NULL);
4287 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4290 iface = G_FILE_GET_IFACE (file);
4292 if (iface->query_writable_namespaces == NULL)
4293 return g_file_attribute_info_list_new ();
4296 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4300 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4302 list = g_file_attribute_info_list_new ();
4303 g_error_free (my_error);
4306 g_propagate_error (error, my_error);
4313 * g_file_set_attribute:
4314 * @file: input #GFile
4315 * @attribute: a string containing the attribute's name
4316 * @type: The type of the attribute
4317 * @value_p: (allow-none): a pointer to the value (or the pointer
4318 * itself if the type is a pointer type)
4319 * @flags: a set of #GFileQueryInfoFlags
4320 * @cancellable: (allow-none): optional #GCancellable object,
4322 * @error: a #GError, or %NULL
4324 * Sets an attribute in the file with attribute name @attribute to @value.
4326 * Some attributes can be unset by setting @attribute to
4327 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4329 * If @cancellable is not %NULL, then the operation can be cancelled by
4330 * triggering the cancellable object from another thread. If the operation
4331 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4333 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4336 g_file_set_attribute (GFile *file,
4337 const gchar *attribute,
4338 GFileAttributeType type,
4340 GFileQueryInfoFlags flags,
4341 GCancellable *cancellable,
4346 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4347 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4352 iface = G_FILE_GET_IFACE (file);
4354 if (iface->set_attribute == NULL)
4356 g_set_error_literal (error, G_IO_ERROR,
4357 G_IO_ERROR_NOT_SUPPORTED,
4358 _("Operation not supported"));
4362 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4366 * g_file_set_attributes_from_info:
4367 * @file: input #GFile
4368 * @info: a #GFileInfo
4369 * @flags: #GFileQueryInfoFlags
4370 * @cancellable: (allow-none): optional #GCancellable object,
4372 * @error: a #GError, or %NULL
4374 * Tries to set all attributes in the #GFileInfo on the target
4375 * values, not stopping on the first error.
4377 * If there is any error during this operation then @error will
4378 * be set to the first error. Error on particular fields are flagged
4379 * by setting the "status" field in the attribute value to
4380 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4381 * also detect further errors.
4383 * If @cancellable is not %NULL, then the operation can be cancelled by
4384 * triggering the cancellable object from another thread. If the operation
4385 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4387 * Returns: %FALSE if there was any error, %TRUE otherwise.
4390 g_file_set_attributes_from_info (GFile *file,
4392 GFileQueryInfoFlags flags,
4393 GCancellable *cancellable,
4398 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4399 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4401 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4404 g_file_info_clear_status (info);
4406 iface = G_FILE_GET_IFACE (file);
4408 return (* iface->set_attributes_from_info) (file,
4416 g_file_real_set_attributes_from_info (GFile *file,
4418 GFileQueryInfoFlags flags,
4419 GCancellable *cancellable,
4425 GFileAttributeValue *value;
4429 attributes = g_file_info_list_attributes (info, NULL);
4431 for (i = 0; attributes[i] != NULL; i++)
4433 value = _g_file_info_get_attribute_value (info, attributes[i]);
4435 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4438 if (!g_file_set_attribute (file, attributes[i],
4439 value->type, _g_file_attribute_value_peek_as_pointer (value),
4440 flags, cancellable, error))
4442 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4444 /* Don't set error multiple times */
4448 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4451 g_strfreev (attributes);
4457 * g_file_set_attributes_async:
4458 * @file: input #GFile
4459 * @info: a #GFileInfo
4460 * @flags: a #GFileQueryInfoFlags
4461 * @io_priority: the [I/O priority][io-priority] of the request
4462 * @cancellable: (allow-none): optional #GCancellable object,
4464 * @callback: (scope async): a #GAsyncReadyCallback
4465 * @user_data: (closure): a #gpointer
4467 * Asynchronously sets the attributes of @file with @info.
4469 * For more details, see g_file_set_attributes_from_info(),
4470 * which is the synchronous version of this call.
4472 * When the operation is finished, @callback will be called.
4473 * You can then call g_file_set_attributes_finish() to get
4474 * the result of the operation.
4477 g_file_set_attributes_async (GFile *file,
4479 GFileQueryInfoFlags flags,
4481 GCancellable *cancellable,
4482 GAsyncReadyCallback callback,
4487 g_return_if_fail (G_IS_FILE (file));
4488 g_return_if_fail (G_IS_FILE_INFO (info));
4490 iface = G_FILE_GET_IFACE (file);
4491 (* iface->set_attributes_async) (file,
4501 * g_file_set_attributes_finish:
4502 * @file: input #GFile
4503 * @result: a #GAsyncResult
4504 * @info: (out) (transfer full): a #GFileInfo
4505 * @error: a #GError, or %NULL
4507 * Finishes setting an attribute started in g_file_set_attributes_async().
4509 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4512 g_file_set_attributes_finish (GFile *file,
4513 GAsyncResult *result,
4519 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4520 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4522 /* No standard handling of errors here, as we must set info even
4525 iface = G_FILE_GET_IFACE (file);
4526 return (* iface->set_attributes_finish) (file, result, info, error);
4530 * g_file_set_attribute_string:
4531 * @file: input #GFile
4532 * @attribute: a string containing the attribute's name
4533 * @value: a string containing the attribute's value
4534 * @flags: #GFileQueryInfoFlags
4535 * @cancellable: (allow-none): optional #GCancellable object,
4537 * @error: a #GError, or %NULL
4539 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4540 * If @attribute is of a different type, this operation will fail.
4542 * If @cancellable is not %NULL, then the operation can be cancelled by
4543 * triggering the cancellable object from another thread. If the operation
4544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4546 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4549 g_file_set_attribute_string (GFile *file,
4550 const char *attribute,
4552 GFileQueryInfoFlags flags,
4553 GCancellable *cancellable,
4556 return g_file_set_attribute (file, attribute,
4557 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4558 flags, cancellable, error);
4562 * g_file_set_attribute_byte_string:
4563 * @file: input #GFile
4564 * @attribute: a string containing the attribute's name
4565 * @value: a string containing the attribute's new value
4566 * @flags: a #GFileQueryInfoFlags
4567 * @cancellable: (allow-none): optional #GCancellable object,
4569 * @error: a #GError, or %NULL
4571 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4572 * If @attribute is of a different type, this operation will fail,
4575 * If @cancellable is not %NULL, then the operation can be cancelled by
4576 * triggering the cancellable object from another thread. If the operation
4577 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4579 * Returns: %TRUE if the @attribute was successfully set to @value
4580 * in the @file, %FALSE otherwise.
4583 g_file_set_attribute_byte_string (GFile *file,
4584 const gchar *attribute,
4586 GFileQueryInfoFlags flags,
4587 GCancellable *cancellable,
4590 return g_file_set_attribute (file, attribute,
4591 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4592 flags, cancellable, error);
4596 * g_file_set_attribute_uint32:
4597 * @file: input #GFile
4598 * @attribute: a string containing the attribute's name
4599 * @value: a #guint32 containing the attribute's new value
4600 * @flags: a #GFileQueryInfoFlags
4601 * @cancellable: (allow-none): optional #GCancellable object,
4603 * @error: a #GError, or %NULL
4605 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4606 * If @attribute is of a different type, this operation will fail.
4608 * If @cancellable is not %NULL, then the operation can be cancelled by
4609 * triggering the cancellable object from another thread. If the operation
4610 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4612 * Returns: %TRUE if the @attribute was successfully set to @value
4613 * in the @file, %FALSE otherwise.
4616 g_file_set_attribute_uint32 (GFile *file,
4617 const gchar *attribute,
4619 GFileQueryInfoFlags flags,
4620 GCancellable *cancellable,
4623 return g_file_set_attribute (file, attribute,
4624 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4625 flags, cancellable, error);
4629 * g_file_set_attribute_int32:
4630 * @file: input #GFile
4631 * @attribute: a string containing the attribute's name
4632 * @value: a #gint32 containing the attribute's new value
4633 * @flags: a #GFileQueryInfoFlags
4634 * @cancellable: (allow-none): optional #GCancellable object,
4636 * @error: a #GError, or %NULL
4638 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4639 * If @attribute is of a different type, this operation will fail.
4641 * If @cancellable is not %NULL, then the operation can be cancelled by
4642 * triggering the cancellable object from another thread. If the operation
4643 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4645 * Returns: %TRUE if the @attribute was successfully set to @value
4646 * in the @file, %FALSE otherwise.
4649 g_file_set_attribute_int32 (GFile *file,
4650 const gchar *attribute,
4652 GFileQueryInfoFlags flags,
4653 GCancellable *cancellable,
4656 return g_file_set_attribute (file, attribute,
4657 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4658 flags, cancellable, error);
4662 * g_file_set_attribute_uint64:
4663 * @file: input #GFile
4664 * @attribute: a string containing the attribute's name
4665 * @value: a #guint64 containing the attribute's new value
4666 * @flags: a #GFileQueryInfoFlags
4667 * @cancellable: (allow-none): optional #GCancellable object,
4669 * @error: a #GError, or %NULL
4671 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4672 * If @attribute is of a different type, this operation will fail.
4674 * If @cancellable is not %NULL, then the operation can be cancelled by
4675 * triggering the cancellable object from another thread. If the operation
4676 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4678 * Returns: %TRUE if the @attribute was successfully set to @value
4679 * in the @file, %FALSE otherwise.
4682 g_file_set_attribute_uint64 (GFile *file,
4683 const gchar *attribute,
4685 GFileQueryInfoFlags flags,
4686 GCancellable *cancellable,
4689 return g_file_set_attribute (file, attribute,
4690 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4691 flags, cancellable, error);
4695 * g_file_set_attribute_int64:
4696 * @file: input #GFile
4697 * @attribute: a string containing the attribute's name
4698 * @value: a #guint64 containing the attribute's new value
4699 * @flags: a #GFileQueryInfoFlags
4700 * @cancellable: (allow-none): optional #GCancellable object,
4702 * @error: a #GError, or %NULL
4704 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4705 * If @attribute is of a different type, this operation will fail.
4707 * If @cancellable is not %NULL, then the operation can be cancelled by
4708 * triggering the cancellable object from another thread. If the operation
4709 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4711 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4714 g_file_set_attribute_int64 (GFile *file,
4715 const gchar *attribute,
4717 GFileQueryInfoFlags flags,
4718 GCancellable *cancellable,
4721 return g_file_set_attribute (file, attribute,
4722 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4723 flags, cancellable, error);
4727 * g_file_mount_mountable:
4728 * @file: input #GFile
4729 * @flags: flags affecting the operation
4730 * @mount_operation: (allow-none): a #GMountOperation,
4731 * or %NULL to avoid user interaction
4732 * @cancellable: (allow-none): optional #GCancellable object,
4734 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4735 * when the request is satisfied, or %NULL
4736 * @user_data: (closure): the data to pass to callback function
4738 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4739 * Using @mount_operation, you can request callbacks when, for instance,
4740 * passwords are needed during authentication.
4742 * If @cancellable is not %NULL, then the operation can be cancelled by
4743 * triggering the cancellable object from another thread. If the operation
4744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4746 * When the operation is finished, @callback will be called.
4747 * You can then call g_file_mount_mountable_finish() to get
4748 * the result of the operation.
4751 g_file_mount_mountable (GFile *file,
4752 GMountMountFlags flags,
4753 GMountOperation *mount_operation,
4754 GCancellable *cancellable,
4755 GAsyncReadyCallback callback,
4760 g_return_if_fail (G_IS_FILE (file));
4762 iface = G_FILE_GET_IFACE (file);
4764 if (iface->mount_mountable == NULL)
4766 g_task_report_new_error (file, callback, user_data,
4767 g_file_mount_mountable,
4768 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4769 _("Operation not supported"));
4773 (* iface->mount_mountable) (file,
4782 * g_file_mount_mountable_finish:
4783 * @file: input #GFile
4784 * @result: a #GAsyncResult
4785 * @error: a #GError, or %NULL
4787 * Finishes a mount operation. See g_file_mount_mountable() for details.
4789 * Finish an asynchronous mount operation that was started
4790 * with g_file_mount_mountable().
4792 * Returns: (transfer full): a #GFile or %NULL on error.
4793 * Free the returned object with g_object_unref().
4796 g_file_mount_mountable_finish (GFile *file,
4797 GAsyncResult *result,
4802 g_return_val_if_fail (G_IS_FILE (file), NULL);
4803 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4805 if (g_async_result_legacy_propagate_error (result, error))
4807 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4808 return g_task_propagate_pointer (G_TASK (result), error);
4810 iface = G_FILE_GET_IFACE (file);
4811 return (* iface->mount_mountable_finish) (file, result, error);
4815 * g_file_unmount_mountable:
4816 * @file: input #GFile
4817 * @flags: flags affecting the operation
4818 * @cancellable: (allow-none): optional #GCancellable object,
4820 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4821 * when the request is satisfied, or %NULL
4822 * @user_data: (closure): the data to pass to callback function
4824 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4826 * If @cancellable is not %NULL, then the operation can be cancelled by
4827 * triggering the cancellable object from another thread. If the operation
4828 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4830 * When the operation is finished, @callback will be called.
4831 * You can then call g_file_unmount_mountable_finish() to get
4832 * the result of the operation.
4834 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4837 g_file_unmount_mountable (GFile *file,
4838 GMountUnmountFlags flags,
4839 GCancellable *cancellable,
4840 GAsyncReadyCallback callback,
4845 g_return_if_fail (G_IS_FILE (file));
4847 iface = G_FILE_GET_IFACE (file);
4849 if (iface->unmount_mountable == NULL)
4851 g_task_report_new_error (file, callback, user_data,
4852 g_file_unmount_mountable_with_operation,
4853 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4854 _("Operation not supported"));
4858 (* iface->unmount_mountable) (file,
4866 * g_file_unmount_mountable_finish:
4867 * @file: input #GFile
4868 * @result: a #GAsyncResult
4869 * @error: a #GError, or %NULL
4871 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4873 * Finish an asynchronous unmount operation that was started
4874 * with g_file_unmount_mountable().
4876 * Returns: %TRUE if the operation finished successfully.
4879 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4883 g_file_unmount_mountable_finish (GFile *file,
4884 GAsyncResult *result,
4889 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4890 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4892 if (g_async_result_legacy_propagate_error (result, error))
4894 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4895 return g_task_propagate_boolean (G_TASK (result), error);
4897 iface = G_FILE_GET_IFACE (file);
4898 return (* iface->unmount_mountable_finish) (file, result, error);
4902 * g_file_unmount_mountable_with_operation:
4903 * @file: input #GFile
4904 * @flags: flags affecting the operation
4905 * @mount_operation: (allow-none): a #GMountOperation,
4906 * or %NULL to avoid user interaction
4907 * @cancellable: (allow-none): optional #GCancellable object,
4909 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4910 * when the request is satisfied, or %NULL
4911 * @user_data: (closure): the data to pass to callback function
4913 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4915 * If @cancellable is not %NULL, then the operation can be cancelled by
4916 * triggering the cancellable object from another thread. If the operation
4917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4919 * When the operation is finished, @callback will be called.
4920 * You can then call g_file_unmount_mountable_finish() to get
4921 * the result of the operation.
4926 g_file_unmount_mountable_with_operation (GFile *file,
4927 GMountUnmountFlags flags,
4928 GMountOperation *mount_operation,
4929 GCancellable *cancellable,
4930 GAsyncReadyCallback callback,
4935 g_return_if_fail (G_IS_FILE (file));
4937 iface = G_FILE_GET_IFACE (file);
4939 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4941 g_task_report_new_error (file, callback, user_data,
4942 g_file_unmount_mountable_with_operation,
4943 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4944 _("Operation not supported"));
4948 if (iface->unmount_mountable_with_operation != NULL)
4949 (* iface->unmount_mountable_with_operation) (file,
4956 (* iface->unmount_mountable) (file,
4964 * g_file_unmount_mountable_with_operation_finish:
4965 * @file: input #GFile
4966 * @result: a #GAsyncResult
4967 * @error: a #GError, or %NULL
4969 * Finishes an unmount operation,
4970 * see g_file_unmount_mountable_with_operation() for details.
4972 * Finish an asynchronous unmount operation that was started
4973 * with g_file_unmount_mountable_with_operation().
4975 * Returns: %TRUE if the operation finished successfully.
4981 g_file_unmount_mountable_with_operation_finish (GFile *file,
4982 GAsyncResult *result,
4987 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4988 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4990 if (g_async_result_legacy_propagate_error (result, error))
4992 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4993 return g_task_propagate_boolean (G_TASK (result), error);
4995 iface = G_FILE_GET_IFACE (file);
4996 if (iface->unmount_mountable_with_operation_finish != NULL)
4997 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4999 return (* iface->unmount_mountable_finish) (file, result, error);
5003 * g_file_eject_mountable:
5004 * @file: input #GFile
5005 * @flags: flags affecting the operation
5006 * @cancellable: (allow-none): optional #GCancellable object,
5008 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5009 * when the request is satisfied, or %NULL
5010 * @user_data: (closure): the data to pass to callback function
5012 * Starts an asynchronous eject on a mountable.
5013 * When this operation has completed, @callback will be called with
5014 * @user_user data, and the operation can be finalized with
5015 * g_file_eject_mountable_finish().
5017 * If @cancellable is not %NULL, then the operation can be cancelled by
5018 * triggering the cancellable object from another thread. If the operation
5019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5021 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5024 g_file_eject_mountable (GFile *file,
5025 GMountUnmountFlags flags,
5026 GCancellable *cancellable,
5027 GAsyncReadyCallback callback,
5032 g_return_if_fail (G_IS_FILE (file));
5034 iface = G_FILE_GET_IFACE (file);
5036 if (iface->eject_mountable == NULL)
5038 g_task_report_new_error (file, callback, user_data,
5039 g_file_eject_mountable_with_operation,
5040 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5041 _("Operation not supported"));
5045 (* iface->eject_mountable) (file,
5053 * g_file_eject_mountable_finish:
5054 * @file: input #GFile
5055 * @result: a #GAsyncResult
5056 * @error: a #GError, or %NULL
5058 * Finishes an asynchronous eject operation started by
5059 * g_file_eject_mountable().
5061 * Returns: %TRUE if the @file was ejected successfully.
5064 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5068 g_file_eject_mountable_finish (GFile *file,
5069 GAsyncResult *result,
5074 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5075 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5077 if (g_async_result_legacy_propagate_error (result, error))
5079 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5080 return g_task_propagate_boolean (G_TASK (result), error);
5082 iface = G_FILE_GET_IFACE (file);
5083 return (* iface->eject_mountable_finish) (file, result, error);
5087 * g_file_eject_mountable_with_operation:
5088 * @file: input #GFile
5089 * @flags: flags affecting the operation
5090 * @mount_operation: (allow-none): a #GMountOperation,
5091 * or %NULL to avoid user interaction
5092 * @cancellable: (allow-none): optional #GCancellable object,
5094 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5095 * when the request is satisfied, or %NULL
5096 * @user_data: (closure): the data to pass to callback function
5098 * Starts an asynchronous eject on a mountable.
5099 * When this operation has completed, @callback will be called with
5100 * @user_user data, and the operation can be finalized with
5101 * g_file_eject_mountable_with_operation_finish().
5103 * If @cancellable is not %NULL, then the operation can be cancelled by
5104 * triggering the cancellable object from another thread. If the operation
5105 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5110 g_file_eject_mountable_with_operation (GFile *file,
5111 GMountUnmountFlags flags,
5112 GMountOperation *mount_operation,
5113 GCancellable *cancellable,
5114 GAsyncReadyCallback callback,
5119 g_return_if_fail (G_IS_FILE (file));
5121 iface = G_FILE_GET_IFACE (file);
5123 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5125 g_task_report_new_error (file, callback, user_data,
5126 g_file_eject_mountable_with_operation,
5127 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5128 _("Operation not supported"));
5132 if (iface->eject_mountable_with_operation != NULL)
5133 (* iface->eject_mountable_with_operation) (file,
5140 (* iface->eject_mountable) (file,
5148 * g_file_eject_mountable_with_operation_finish:
5149 * @file: input #GFile
5150 * @result: a #GAsyncResult
5151 * @error: a #GError, or %NULL
5153 * Finishes an asynchronous eject operation started by
5154 * g_file_eject_mountable_with_operation().
5156 * Returns: %TRUE if the @file was ejected successfully.
5162 g_file_eject_mountable_with_operation_finish (GFile *file,
5163 GAsyncResult *result,
5168 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5169 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5171 if (g_async_result_legacy_propagate_error (result, error))
5173 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5174 return g_task_propagate_boolean (G_TASK (result), error);
5176 iface = G_FILE_GET_IFACE (file);
5177 if (iface->eject_mountable_with_operation_finish != NULL)
5178 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5180 return (* iface->eject_mountable_finish) (file, result, error);
5184 * g_file_monitor_directory:
5185 * @file: input #GFile
5186 * @flags: a set of #GFileMonitorFlags
5187 * @cancellable: (allow-none): optional #GCancellable object,
5189 * @error: a #GError, or %NULL
5191 * Obtains a directory monitor for the given file.
5192 * This may fail if directory monitoring is not supported.
5194 * If @cancellable is not %NULL, then the operation can be cancelled by
5195 * triggering the cancellable object from another thread. If the operation
5196 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5198 * It does not make sense for @flags to contain
5199 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5200 * directories. It is not possible to monitor all the files in a
5201 * directory for changes made via hard links; if you want to do this then
5202 * you must register individual watches with g_file_monitor().
5204 * Virtual: monitor_dir
5205 * Returns: (transfer full): a #GFileMonitor for the given @file,
5206 * or %NULL on error.
5207 * Free the returned object with g_object_unref().
5210 g_file_monitor_directory (GFile *file,
5211 GFileMonitorFlags flags,
5212 GCancellable *cancellable,
5217 g_return_val_if_fail (G_IS_FILE (file), NULL);
5218 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5220 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5223 iface = G_FILE_GET_IFACE (file);
5225 if (iface->monitor_dir == NULL)
5227 g_set_error_literal (error, G_IO_ERROR,
5228 G_IO_ERROR_NOT_SUPPORTED,
5229 _("Operation not supported"));
5233 return (* iface->monitor_dir) (file, flags, cancellable, error);
5237 * g_file_monitor_file:
5238 * @file: input #GFile
5239 * @flags: a set of #GFileMonitorFlags
5240 * @cancellable: (allow-none): optional #GCancellable object,
5242 * @error: a #GError, or %NULL
5244 * Obtains a file monitor for the given file. If no file notification
5245 * mechanism exists, then regular polling of the file is used.
5247 * If @cancellable is not %NULL, then the operation can be cancelled by
5248 * triggering the cancellable object from another thread. If the operation
5249 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5251 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5252 * will also attempt to report changes made to the file via another
5253 * filename (ie, a hard link). Without this flag, you can only rely on
5254 * changes made through the filename contained in @file to be
5255 * reported. Using this flag may result in an increase in resource
5256 * usage, and may not have any effect depending on the #GFileMonitor
5257 * backend and/or filesystem type.
5259 * Returns: (transfer full): a #GFileMonitor for the given @file,
5260 * or %NULL on error.
5261 * Free the returned object with g_object_unref().
5264 g_file_monitor_file (GFile *file,
5265 GFileMonitorFlags flags,
5266 GCancellable *cancellable,
5270 GFileMonitor *monitor;
5272 g_return_val_if_fail (G_IS_FILE (file), NULL);
5274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5277 iface = G_FILE_GET_IFACE (file);
5281 if (iface->monitor_file)
5282 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5284 /* Fallback to polling */
5285 if (monitor == NULL)
5286 monitor = _g_poll_file_monitor_new (file);
5293 * @file: input #GFile
5294 * @flags: a set of #GFileMonitorFlags
5295 * @cancellable: (allow-none): optional #GCancellable object,
5297 * @error: a #GError, or %NULL
5299 * Obtains a file or directory monitor for the given file,
5300 * depending on the type of the file.
5302 * If @cancellable is not %NULL, then the operation can be cancelled by
5303 * triggering the cancellable object from another thread. If the operation
5304 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5306 * Returns: (transfer full): a #GFileMonitor for the given @file,
5307 * or %NULL on error.
5308 * Free the returned object with g_object_unref().
5313 g_file_monitor (GFile *file,
5314 GFileMonitorFlags flags,
5315 GCancellable *cancellable,
5318 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5319 return g_file_monitor_directory (file,
5320 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5321 cancellable, error);
5323 return g_file_monitor_file (file, flags, cancellable, error);
5326 /********************************************
5327 * Default implementation of async ops *
5328 ********************************************/
5332 GFileQueryInfoFlags flags;
5333 } QueryInfoAsyncData;
5336 query_info_data_free (QueryInfoAsyncData *data)
5338 g_free (data->attributes);
5343 query_info_async_thread (GTask *task,
5346 GCancellable *cancellable)
5348 QueryInfoAsyncData *data = task_data;
5350 GError *error = NULL;
5352 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5354 g_task_return_pointer (task, info, g_object_unref);
5356 g_task_return_error (task, error);
5360 g_file_real_query_info_async (GFile *file,
5361 const char *attributes,
5362 GFileQueryInfoFlags flags,
5364 GCancellable *cancellable,
5365 GAsyncReadyCallback callback,
5369 QueryInfoAsyncData *data;
5371 data = g_new0 (QueryInfoAsyncData, 1);
5372 data->attributes = g_strdup (attributes);
5373 data->flags = flags;
5375 task = g_task_new (file, cancellable, callback, user_data);
5376 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5377 g_task_set_priority (task, io_priority);
5378 g_task_run_in_thread (task, query_info_async_thread);
5379 g_object_unref (task);
5383 g_file_real_query_info_finish (GFile *file,
5387 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5389 return g_task_propagate_pointer (G_TASK (res), error);
5393 query_filesystem_info_async_thread (GTask *task,
5396 GCancellable *cancellable)
5398 const char *attributes = task_data;
5400 GError *error = NULL;
5402 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5404 g_task_return_pointer (task, info, g_object_unref);
5406 g_task_return_error (task, error);
5410 g_file_real_query_filesystem_info_async (GFile *file,
5411 const char *attributes,
5413 GCancellable *cancellable,
5414 GAsyncReadyCallback callback,
5419 task = g_task_new (file, cancellable, callback, user_data);
5420 g_task_set_task_data (task, g_strdup (attributes), g_free);
5421 g_task_set_priority (task, io_priority);
5422 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5423 g_object_unref (task);
5427 g_file_real_query_filesystem_info_finish (GFile *file,
5431 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5433 return g_task_propagate_pointer (G_TASK (res), error);
5437 enumerate_children_async_thread (GTask *task,
5440 GCancellable *cancellable)
5442 QueryInfoAsyncData *data = task_data;
5443 GFileEnumerator *enumerator;
5444 GError *error = NULL;
5446 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5448 g_task_return_error (task, error);
5450 g_task_return_pointer (task, enumerator, g_object_unref);
5454 g_file_real_enumerate_children_async (GFile *file,
5455 const char *attributes,
5456 GFileQueryInfoFlags flags,
5458 GCancellable *cancellable,
5459 GAsyncReadyCallback callback,
5463 QueryInfoAsyncData *data;
5465 data = g_new0 (QueryInfoAsyncData, 1);
5466 data->attributes = g_strdup (attributes);
5467 data->flags = flags;
5469 task = g_task_new (file, cancellable, callback, user_data);
5470 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5471 g_task_set_priority (task, io_priority);
5472 g_task_run_in_thread (task, enumerate_children_async_thread);
5473 g_object_unref (task);
5476 static GFileEnumerator *
5477 g_file_real_enumerate_children_finish (GFile *file,
5481 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5483 return g_task_propagate_pointer (G_TASK (res), error);
5487 open_read_async_thread (GTask *task,
5490 GCancellable *cancellable)
5492 GFileInputStream *stream;
5493 GError *error = NULL;
5495 stream = g_file_read (G_FILE (object), cancellable, &error);
5497 g_task_return_pointer (task, stream, g_object_unref);
5499 g_task_return_error (task, error);
5503 g_file_real_read_async (GFile *file,
5505 GCancellable *cancellable,
5506 GAsyncReadyCallback callback,
5511 task = g_task_new (file, cancellable, callback, user_data);
5512 g_task_set_priority (task, io_priority);
5513 g_task_run_in_thread (task, open_read_async_thread);
5514 g_object_unref (task);
5517 static GFileInputStream *
5518 g_file_real_read_finish (GFile *file,
5522 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5524 return g_task_propagate_pointer (G_TASK (res), error);
5528 append_to_async_thread (GTask *task,
5529 gpointer source_object,
5531 GCancellable *cancellable)
5533 GFileCreateFlags *data = task_data;
5534 GFileOutputStream *stream;
5535 GError *error = NULL;
5537 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5539 g_task_return_pointer (task, stream, g_object_unref);
5541 g_task_return_error (task, error);
5545 g_file_real_append_to_async (GFile *file,
5546 GFileCreateFlags flags,
5548 GCancellable *cancellable,
5549 GAsyncReadyCallback callback,
5552 GFileCreateFlags *data;
5555 data = g_new0 (GFileCreateFlags, 1);
5558 task = g_task_new (file, cancellable, callback, user_data);
5559 g_task_set_task_data (task, data, g_free);
5560 g_task_set_priority (task, io_priority);
5562 g_task_run_in_thread (task, append_to_async_thread);
5563 g_object_unref (task);
5566 static GFileOutputStream *
5567 g_file_real_append_to_finish (GFile *file,
5571 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5573 return g_task_propagate_pointer (G_TASK (res), error);
5577 create_async_thread (GTask *task,
5578 gpointer source_object,
5580 GCancellable *cancellable)
5582 GFileCreateFlags *data = task_data;
5583 GFileOutputStream *stream;
5584 GError *error = NULL;
5586 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5588 g_task_return_pointer (task, stream, g_object_unref);
5590 g_task_return_error (task, error);
5594 g_file_real_create_async (GFile *file,
5595 GFileCreateFlags flags,
5597 GCancellable *cancellable,
5598 GAsyncReadyCallback callback,
5601 GFileCreateFlags *data;
5604 data = g_new0 (GFileCreateFlags, 1);
5607 task = g_task_new (file, cancellable, callback, user_data);
5608 g_task_set_task_data (task, data, g_free);
5609 g_task_set_priority (task, io_priority);
5611 g_task_run_in_thread (task, create_async_thread);
5612 g_object_unref (task);
5615 static GFileOutputStream *
5616 g_file_real_create_finish (GFile *file,
5620 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5622 return g_task_propagate_pointer (G_TASK (res), error);
5626 GFileOutputStream *stream;
5628 gboolean make_backup;
5629 GFileCreateFlags flags;
5633 replace_async_data_free (ReplaceAsyncData *data)
5636 g_object_unref (data->stream);
5637 g_free (data->etag);
5642 replace_async_thread (GTask *task,
5643 gpointer source_object,
5645 GCancellable *cancellable)
5647 GFileOutputStream *stream;
5648 ReplaceAsyncData *data = task_data;
5649 GError *error = NULL;
5651 stream = g_file_replace (G_FILE (source_object),
5659 g_task_return_pointer (task, stream, g_object_unref);
5661 g_task_return_error (task, error);
5665 g_file_real_replace_async (GFile *file,
5667 gboolean make_backup,
5668 GFileCreateFlags flags,
5670 GCancellable *cancellable,
5671 GAsyncReadyCallback callback,
5675 ReplaceAsyncData *data;
5677 data = g_new0 (ReplaceAsyncData, 1);
5678 data->etag = g_strdup (etag);
5679 data->make_backup = make_backup;
5680 data->flags = flags;
5682 task = g_task_new (file, cancellable, callback, user_data);
5683 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5684 g_task_set_priority (task, io_priority);
5686 g_task_run_in_thread (task, replace_async_thread);
5687 g_object_unref (task);
5690 static GFileOutputStream *
5691 g_file_real_replace_finish (GFile *file,
5695 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5697 return g_task_propagate_pointer (G_TASK (res), error);
5701 delete_async_thread (GTask *task,
5704 GCancellable *cancellable)
5706 GError *error = NULL;
5708 if (g_file_delete (G_FILE (object), cancellable, &error))
5709 g_task_return_boolean (task, TRUE);
5711 g_task_return_error (task, error);
5715 g_file_real_delete_async (GFile *file,
5717 GCancellable *cancellable,
5718 GAsyncReadyCallback callback,
5723 task = g_task_new (file, cancellable, callback, user_data);
5724 g_task_set_priority (task, io_priority);
5725 g_task_run_in_thread (task, delete_async_thread);
5726 g_object_unref (task);
5730 g_file_real_delete_finish (GFile *file,
5734 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5736 return g_task_propagate_boolean (G_TASK (res), error);
5740 trash_async_thread (GTask *task,
5743 GCancellable *cancellable)
5745 GError *error = NULL;
5747 if (g_file_trash (G_FILE (object), cancellable, &error))
5748 g_task_return_boolean (task, TRUE);
5750 g_task_return_error (task, error);
5754 g_file_real_trash_async (GFile *file,
5756 GCancellable *cancellable,
5757 GAsyncReadyCallback callback,
5762 task = g_task_new (file, cancellable, callback, user_data);
5763 g_task_set_priority (task, io_priority);
5764 g_task_run_in_thread (task, trash_async_thread);
5765 g_object_unref (task);
5769 g_file_real_trash_finish (GFile *file,
5773 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5775 return g_task_propagate_boolean (G_TASK (res), error);
5779 make_directory_async_thread (GTask *task,
5782 GCancellable *cancellable)
5784 GError *error = NULL;
5786 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5787 g_task_return_boolean (task, TRUE);
5789 g_task_return_error (task, error);
5793 g_file_real_make_directory_async (GFile *file,
5795 GCancellable *cancellable,
5796 GAsyncReadyCallback callback,
5801 task = g_task_new (file, cancellable, callback, user_data);
5802 g_task_set_priority (task, io_priority);
5803 g_task_run_in_thread (task, make_directory_async_thread);
5804 g_object_unref (task);
5808 g_file_real_make_directory_finish (GFile *file,
5812 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5814 return g_task_propagate_boolean (G_TASK (res), error);
5818 open_readwrite_async_thread (GTask *task,
5821 GCancellable *cancellable)
5823 GFileIOStream *stream;
5824 GError *error = NULL;
5826 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5829 g_task_return_error (task, error);
5831 g_task_return_pointer (task, stream, g_object_unref);
5835 g_file_real_open_readwrite_async (GFile *file,
5837 GCancellable *cancellable,
5838 GAsyncReadyCallback callback,
5843 task = g_task_new (file, cancellable, callback, user_data);
5844 g_task_set_priority (task, io_priority);
5846 g_task_run_in_thread (task, open_readwrite_async_thread);
5847 g_object_unref (task);
5850 static GFileIOStream *
5851 g_file_real_open_readwrite_finish (GFile *file,
5855 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5857 return g_task_propagate_pointer (G_TASK (res), error);
5861 create_readwrite_async_thread (GTask *task,
5864 GCancellable *cancellable)
5866 GFileCreateFlags *data = task_data;
5867 GFileIOStream *stream;
5868 GError *error = NULL;
5870 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5873 g_task_return_error (task, error);
5875 g_task_return_pointer (task, stream, g_object_unref);
5879 g_file_real_create_readwrite_async (GFile *file,
5880 GFileCreateFlags flags,
5882 GCancellable *cancellable,
5883 GAsyncReadyCallback callback,
5886 GFileCreateFlags *data;
5889 data = g_new0 (GFileCreateFlags, 1);
5892 task = g_task_new (file, cancellable, callback, user_data);
5893 g_task_set_task_data (task, data, g_free);
5894 g_task_set_priority (task, io_priority);
5896 g_task_run_in_thread (task, create_readwrite_async_thread);
5897 g_object_unref (task);
5900 static GFileIOStream *
5901 g_file_real_create_readwrite_finish (GFile *file,
5905 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5907 return g_task_propagate_pointer (G_TASK (res), error);
5912 gboolean make_backup;
5913 GFileCreateFlags flags;
5914 } ReplaceRWAsyncData;
5917 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5919 g_free (data->etag);
5924 replace_readwrite_async_thread (GTask *task,
5927 GCancellable *cancellable)
5929 GFileIOStream *stream;
5930 GError *error = NULL;
5931 ReplaceRWAsyncData *data = task_data;
5933 stream = g_file_replace_readwrite (G_FILE (object),
5941 g_task_return_error (task, error);
5943 g_task_return_pointer (task, stream, g_object_unref);
5947 g_file_real_replace_readwrite_async (GFile *file,
5949 gboolean make_backup,
5950 GFileCreateFlags flags,
5952 GCancellable *cancellable,
5953 GAsyncReadyCallback callback,
5957 ReplaceRWAsyncData *data;
5959 data = g_new0 (ReplaceRWAsyncData, 1);
5960 data->etag = g_strdup (etag);
5961 data->make_backup = make_backup;
5962 data->flags = flags;
5964 task = g_task_new (file, cancellable, callback, user_data);
5965 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5966 g_task_set_priority (task, io_priority);
5968 g_task_run_in_thread (task, replace_readwrite_async_thread);
5969 g_object_unref (task);
5972 static GFileIOStream *
5973 g_file_real_replace_readwrite_finish (GFile *file,
5977 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5979 return g_task_propagate_pointer (G_TASK (res), error);
5983 set_display_name_async_thread (GTask *task,
5986 GCancellable *cancellable)
5988 GError *error = NULL;
5989 char *name = task_data;
5992 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5995 g_task_return_error (task, error);
5997 g_task_return_pointer (task, file, g_object_unref);
6001 g_file_real_set_display_name_async (GFile *file,
6002 const char *display_name,
6004 GCancellable *cancellable,
6005 GAsyncReadyCallback callback,
6010 task = g_task_new (file, cancellable, callback, user_data);
6011 g_task_set_task_data (task, g_strdup (display_name), g_free);
6012 g_task_set_priority (task, io_priority);
6014 g_task_run_in_thread (task, set_display_name_async_thread);
6015 g_object_unref (task);
6019 g_file_real_set_display_name_finish (GFile *file,
6023 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6025 return g_task_propagate_pointer (G_TASK (res), error);
6029 GFileQueryInfoFlags flags;
6036 set_info_data_free (SetInfoAsyncData *data)
6039 g_object_unref (data->info);
6041 g_error_free (data->error);
6046 set_info_async_thread (GTask *task,
6049 GCancellable *cancellable)
6051 SetInfoAsyncData *data = task_data;
6054 data->res = g_file_set_attributes_from_info (G_FILE (object),
6062 g_file_real_set_attributes_async (GFile *file,
6064 GFileQueryInfoFlags flags,
6066 GCancellable *cancellable,
6067 GAsyncReadyCallback callback,
6071 SetInfoAsyncData *data;
6073 data = g_new0 (SetInfoAsyncData, 1);
6074 data->info = g_file_info_dup (info);
6075 data->flags = flags;
6077 task = g_task_new (file, cancellable, callback, user_data);
6078 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6079 g_task_set_priority (task, io_priority);
6081 g_task_run_in_thread (task, set_info_async_thread);
6082 g_object_unref (task);
6086 g_file_real_set_attributes_finish (GFile *file,
6091 SetInfoAsyncData *data;
6093 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6095 data = g_task_get_task_data (G_TASK (res));
6098 *info = g_object_ref (data->info);
6100 if (error != NULL && data->error)
6101 *error = g_error_copy (data->error);
6107 find_enclosing_mount_async_thread (GTask *task,
6110 GCancellable *cancellable)
6112 GError *error = NULL;
6115 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6118 g_task_return_error (task, error);
6120 g_task_return_pointer (task, mount, g_object_unref);
6124 g_file_real_find_enclosing_mount_async (GFile *file,
6126 GCancellable *cancellable,
6127 GAsyncReadyCallback callback,
6132 task = g_task_new (file, cancellable, callback, user_data);
6133 g_task_set_priority (task, io_priority);
6135 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6136 g_object_unref (task);
6140 g_file_real_find_enclosing_mount_finish (GFile *file,
6144 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6146 return g_task_propagate_pointer (G_TASK (res), error);
6153 GFileCopyFlags flags;
6154 GFileProgressCallback progress_cb;
6155 gpointer progress_cb_data;
6159 copy_async_data_free (CopyAsyncData *data)
6161 g_object_unref (data->source);
6162 g_object_unref (data->destination);
6163 g_slice_free (CopyAsyncData, data);
6167 CopyAsyncData *data;
6168 goffset current_num_bytes;
6169 goffset total_num_bytes;
6173 copy_async_progress_in_main (gpointer user_data)
6175 ProgressData *progress = user_data;
6176 CopyAsyncData *data = progress->data;
6178 data->progress_cb (progress->current_num_bytes,
6179 progress->total_num_bytes,
6180 data->progress_cb_data);
6186 copy_async_progress_callback (goffset current_num_bytes,
6187 goffset total_num_bytes,
6190 GTask *task = user_data;
6191 CopyAsyncData *data = g_task_get_task_data (task);
6192 ProgressData *progress;
6194 progress = g_new (ProgressData, 1);
6195 progress->data = data;
6196 progress->current_num_bytes = current_num_bytes;
6197 progress->total_num_bytes = total_num_bytes;
6199 g_main_context_invoke_full (g_task_get_context (task),
6200 g_task_get_priority (task),
6201 copy_async_progress_in_main,
6207 copy_async_thread (GTask *task,
6210 GCancellable *cancellable)
6212 CopyAsyncData *data = task_data;
6214 GError *error = NULL;
6216 result = g_file_copy (data->source,
6220 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6224 g_task_return_boolean (task, TRUE);
6226 g_task_return_error (task, error);
6230 g_file_real_copy_async (GFile *source,
6232 GFileCopyFlags flags,
6234 GCancellable *cancellable,
6235 GFileProgressCallback progress_callback,
6236 gpointer progress_callback_data,
6237 GAsyncReadyCallback callback,
6241 CopyAsyncData *data;
6243 data = g_slice_new (CopyAsyncData);
6244 data->source = g_object_ref (source);
6245 data->destination = g_object_ref (destination);
6246 data->flags = flags;
6247 data->progress_cb = progress_callback;
6248 data->progress_cb_data = progress_callback_data;
6250 task = g_task_new (source, cancellable, callback, user_data);
6251 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6252 g_task_set_priority (task, io_priority);
6253 g_task_run_in_thread (task, copy_async_thread);
6254 g_object_unref (task);
6258 g_file_real_copy_finish (GFile *file,
6262 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6264 return g_task_propagate_boolean (G_TASK (res), error);
6268 /********************************************
6269 * Default VFS operations *
6270 ********************************************/
6273 * g_file_new_for_path:
6274 * @path: a string containing a relative or absolute path.
6275 * The string must be encoded in the glib filename encoding.
6277 * Constructs a #GFile for a given path. This operation never
6278 * fails, but the returned object might not support any I/O
6279 * operation if @path is malformed.
6281 * Returns: (transfer full): a new #GFile for the given @path.
6282 * Free the returned object with g_object_unref().
6285 g_file_new_for_path (const char *path)
6287 g_return_val_if_fail (path != NULL, NULL);
6289 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6293 * g_file_new_for_uri:
6294 * @uri: a UTF-8 string containing a URI
6296 * Constructs a #GFile for a given URI. This operation never
6297 * fails, but the returned object might not support any I/O
6298 * operation if @uri is malformed or if the uri type is
6301 * Returns: (transfer full): a new #GFile for the given @uri.
6302 * Free the returned object with g_object_unref().
6305 g_file_new_for_uri (const char *uri)
6307 g_return_val_if_fail (uri != NULL, NULL);
6309 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6314 * @tmpl: (type filename) (allow-none): Template for the file
6315 * name, as in g_file_open_tmp(), or %NULL for a default template
6316 * @iostream: (out): on return, a #GFileIOStream for the created file
6317 * @error: a #GError, or %NULL
6319 * Opens a file in the preferred directory for temporary files (as
6320 * returned by g_get_tmp_dir()) and returns a #GFile and
6321 * #GFileIOStream pointing to it.
6323 * @tmpl should be a string in the GLib file name encoding
6324 * containing a sequence of six 'X' characters, and containing no
6325 * directory components. If it is %NULL, a default template is used.
6327 * Unlike the other #GFile constructors, this will return %NULL if
6328 * a temporary file could not be created.
6330 * Returns: (transfer full): a new #GFile.
6331 * Free the returned object with g_object_unref().
6336 g_file_new_tmp (const char *tmpl,
6337 GFileIOStream **iostream,
6343 GFileOutputStream *output;
6345 g_return_val_if_fail (iostream != NULL, NULL);
6347 fd = g_file_open_tmp (tmpl, &path, error);
6351 file = g_file_new_for_path (path);
6353 output = _g_local_file_output_stream_new (fd);
6354 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6356 g_object_unref (output);
6363 * g_file_parse_name:
6364 * @parse_name: a file name or path to be parsed
6366 * Constructs a #GFile with the given @parse_name (i.e. something
6367 * given by g_file_get_parse_name()). This operation never fails,
6368 * but the returned object might not support any I/O operation if
6369 * the @parse_name cannot be parsed.
6371 * Returns: (transfer full): a new #GFile.
6374 g_file_parse_name (const char *parse_name)
6376 g_return_val_if_fail (parse_name != NULL, NULL);
6378 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6382 is_valid_scheme_character (char c)
6384 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6387 /* Following RFC 2396, valid schemes are built like:
6388 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6391 has_valid_scheme (const char *uri)
6397 if (!g_ascii_isalpha (*p))
6402 } while (is_valid_scheme_character (*p));
6408 new_for_cmdline_arg (const gchar *arg,
6414 if (g_path_is_absolute (arg))
6415 return g_file_new_for_path (arg);
6417 if (has_valid_scheme (arg))
6418 return g_file_new_for_uri (arg);
6424 current_dir = g_get_current_dir ();
6425 filename = g_build_filename (current_dir, arg, NULL);
6426 g_free (current_dir);
6429 filename = g_build_filename (cwd, arg, NULL);
6431 file = g_file_new_for_path (filename);
6438 * g_file_new_for_commandline_arg:
6439 * @arg: a command line string
6441 * Creates a #GFile with the given argument from the command line.
6442 * The value of @arg can be either a URI, an absolute path or a
6443 * relative path resolved relative to the current working directory.
6444 * This operation never fails, but the returned object might not
6445 * support any I/O operation if @arg points to a malformed path.
6447 * Note that on Windows, this function expects its argument to be in
6448 * UTF-8 -- not the system code page. This means that you
6449 * should not use this function with string from argv as it is passed
6450 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6451 * the commandline. #GApplication also uses UTF-8 but
6452 * g_application_command_line_create_file_for_arg() may be more useful
6453 * for you there. It is also always possible to use this function with
6454 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6456 * Returns: (transfer full): a new #GFile.
6457 * Free the returned object with g_object_unref().
6460 g_file_new_for_commandline_arg (const char *arg)
6462 g_return_val_if_fail (arg != NULL, NULL);
6464 return new_for_cmdline_arg (arg, NULL);
6468 * g_file_new_for_commandline_arg_and_cwd:
6469 * @arg: a command line string
6470 * @cwd: the current working directory of the commandline
6472 * Creates a #GFile with the given argument from the command line.
6474 * This function is similar to g_file_new_for_commandline_arg() except
6475 * that it allows for passing the current working directory as an
6476 * argument instead of using the current working directory of the
6479 * This is useful if the commandline argument was given in a context
6480 * other than the invocation of the current process.
6482 * See also g_application_command_line_create_file_for_arg().
6484 * Returns: (transfer full): a new #GFile
6489 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6492 g_return_val_if_fail (arg != NULL, NULL);
6493 g_return_val_if_fail (cwd != NULL, NULL);
6495 return new_for_cmdline_arg (arg, cwd);
6499 * g_file_mount_enclosing_volume:
6500 * @location: input #GFile
6501 * @flags: flags affecting the operation
6502 * @mount_operation: (allow-none): a #GMountOperation
6503 * or %NULL to avoid user interaction
6504 * @cancellable: (allow-none): optional #GCancellable object,
6506 * @callback: (allow-none): a #GAsyncReadyCallback to call
6507 * when the request is satisfied, or %NULL
6508 * @user_data: the data to pass to callback function
6510 * Starts a @mount_operation, mounting the volume that contains
6511 * the file @location.
6513 * When this operation has completed, @callback will be called with
6514 * @user_user data, and the operation can be finalized with
6515 * g_file_mount_enclosing_volume_finish().
6517 * If @cancellable is not %NULL, then the operation can be cancelled by
6518 * triggering the cancellable object from another thread. If the operation
6519 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6522 g_file_mount_enclosing_volume (GFile *location,
6523 GMountMountFlags flags,
6524 GMountOperation *mount_operation,
6525 GCancellable *cancellable,
6526 GAsyncReadyCallback callback,
6531 g_return_if_fail (G_IS_FILE (location));
6533 iface = G_FILE_GET_IFACE (location);
6535 if (iface->mount_enclosing_volume == NULL)
6537 g_task_report_new_error (location, callback, user_data,
6538 g_file_mount_enclosing_volume,
6539 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6540 _("volume doesn't implement mount"));
6544 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6549 * g_file_mount_enclosing_volume_finish:
6550 * @location: input #GFile
6551 * @result: a #GAsyncResult
6552 * @error: a #GError, or %NULL
6554 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6556 * Returns: %TRUE if successful. If an error has occurred,
6557 * this function will return %FALSE and set @error
6558 * appropriately if present.
6561 g_file_mount_enclosing_volume_finish (GFile *location,
6562 GAsyncResult *result,
6567 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6568 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6570 if (g_async_result_legacy_propagate_error (result, error))
6572 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6573 return g_task_propagate_boolean (G_TASK (result), error);
6575 iface = G_FILE_GET_IFACE (location);
6577 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6580 /********************************************
6581 * Utility functions *
6582 ********************************************/
6585 * g_file_query_default_handler:
6586 * @file: a #GFile to open
6587 * @cancellable: optional #GCancellable object, %NULL to ignore
6588 * @error: a #GError, or %NULL
6590 * Returns the #GAppInfo that is registered as the default
6591 * application to handle the file specified by @file.
6593 * If @cancellable is not %NULL, then the operation can be cancelled by
6594 * triggering the cancellable object from another thread. If the operation
6595 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6597 * Returns: (transfer full): a #GAppInfo if the handle was found,
6598 * %NULL if there were errors.
6599 * When you are done with it, release it with g_object_unref()
6602 g_file_query_default_handler (GFile *file,
6603 GCancellable *cancellable,
6607 const char *content_type;
6612 uri_scheme = g_file_get_uri_scheme (file);
6613 if (uri_scheme && uri_scheme[0] != '\0')
6615 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6616 g_free (uri_scheme);
6618 if (appinfo != NULL)
6622 info = g_file_query_info (file,
6623 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6632 content_type = g_file_info_get_content_type (info);
6635 /* Don't use is_native(), as we want to support fuse paths if available */
6636 path = g_file_get_path (file);
6637 appinfo = g_app_info_get_default_for_type (content_type,
6642 g_object_unref (info);
6644 if (appinfo != NULL)
6647 g_set_error_literal (error, G_IO_ERROR,
6648 G_IO_ERROR_NOT_SUPPORTED,
6649 _("No application is registered as handling this file"));
6653 #define GET_CONTENT_BLOCK_SIZE 8192
6656 * g_file_load_contents:
6657 * @file: input #GFile
6658 * @cancellable: optional #GCancellable object, %NULL to ignore
6659 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6660 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6661 * or %NULL if the length is not needed
6662 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6663 * or %NULL if the entity tag is not needed
6664 * @error: a #GError, or %NULL
6666 * Loads the content of the file into memory. The data is always
6667 * zero-terminated, but this is not included in the resultant @length.
6668 * The returned @content should be freed with g_free() when no longer
6671 * If @cancellable is not %NULL, then the operation can be cancelled by
6672 * triggering the cancellable object from another thread. If the operation
6673 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6675 * Returns: %TRUE if the @file's contents were successfully loaded.
6676 * %FALSE if there were errors.
6679 g_file_load_contents (GFile *file,
6680 GCancellable *cancellable,
6686 GFileInputStream *in;
6687 GByteArray *content;
6692 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6693 g_return_val_if_fail (contents != NULL, FALSE);
6695 in = g_file_read (file, cancellable, error);
6699 content = g_byte_array_new ();
6702 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6703 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6704 content->data + pos,
6705 GET_CONTENT_BLOCK_SIZE,
6706 cancellable, error)) > 0)
6709 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6716 info = g_file_input_stream_query_info (in,
6717 G_FILE_ATTRIBUTE_ETAG_VALUE,
6722 *etag_out = g_strdup (g_file_info_get_etag (info));
6723 g_object_unref (info);
6727 /* Ignore errors on close */
6728 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6729 g_object_unref (in);
6733 /* error is set already */
6734 g_byte_array_free (content, TRUE);
6741 /* Zero terminate (we got an extra byte allocated for this */
6742 content->data[pos] = 0;
6744 *contents = (char *)g_byte_array_free (content, FALSE);
6751 GFileReadMoreCallback read_more_callback;
6752 GByteArray *content;
6759 load_contents_data_free (LoadContentsData *data)
6762 g_byte_array_free (data->content, TRUE);
6763 g_free (data->etag);
6768 load_contents_close_callback (GObject *obj,
6769 GAsyncResult *close_res,
6772 GInputStream *stream = G_INPUT_STREAM (obj);
6773 LoadContentsData *data = user_data;
6775 /* Ignore errors here, we're only reading anyway */
6776 g_input_stream_close_finish (stream, close_res, NULL);
6777 g_object_unref (stream);
6779 g_task_return_boolean (data->task, TRUE);
6780 g_object_unref (data->task);
6784 load_contents_fstat_callback (GObject *obj,
6785 GAsyncResult *stat_res,
6788 GInputStream *stream = G_INPUT_STREAM (obj);
6789 LoadContentsData *data = user_data;
6792 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6796 data->etag = g_strdup (g_file_info_get_etag (info));
6797 g_object_unref (info);
6800 g_input_stream_close_async (stream, 0,
6801 g_task_get_cancellable (data->task),
6802 load_contents_close_callback, data);
6806 load_contents_read_callback (GObject *obj,
6807 GAsyncResult *read_res,
6810 GInputStream *stream = G_INPUT_STREAM (obj);
6811 LoadContentsData *data = user_data;
6812 GError *error = NULL;
6815 read_size = g_input_stream_read_finish (stream, read_res, &error);
6819 g_task_return_error (data->task, error);
6820 g_object_unref (data->task);
6822 /* Close the file ignoring any error */
6823 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6824 g_object_unref (stream);
6826 else if (read_size == 0)
6828 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6829 G_FILE_ATTRIBUTE_ETAG_VALUE,
6831 g_task_get_cancellable (data->task),
6832 load_contents_fstat_callback,
6835 else if (read_size > 0)
6837 data->pos += read_size;
6839 g_byte_array_set_size (data->content,
6840 data->pos + GET_CONTENT_BLOCK_SIZE);
6843 if (data->read_more_callback &&
6844 !data->read_more_callback ((char *)data->content->data, data->pos,
6845 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6846 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6847 G_FILE_ATTRIBUTE_ETAG_VALUE,
6849 g_task_get_cancellable (data->task),
6850 load_contents_fstat_callback,
6853 g_input_stream_read_async (stream,
6854 data->content->data + data->pos,
6855 GET_CONTENT_BLOCK_SIZE,
6857 g_task_get_cancellable (data->task),
6858 load_contents_read_callback,
6864 load_contents_open_callback (GObject *obj,
6865 GAsyncResult *open_res,
6868 GFile *file = G_FILE (obj);
6869 GFileInputStream *stream;
6870 LoadContentsData *data = user_data;
6871 GError *error = NULL;
6873 stream = g_file_read_finish (file, open_res, &error);
6877 g_byte_array_set_size (data->content,
6878 data->pos + GET_CONTENT_BLOCK_SIZE);
6879 g_input_stream_read_async (G_INPUT_STREAM (stream),
6880 data->content->data + data->pos,
6881 GET_CONTENT_BLOCK_SIZE,
6883 g_task_get_cancellable (data->task),
6884 load_contents_read_callback,
6889 g_task_return_error (data->task, error);
6890 g_object_unref (data->task);
6895 * g_file_load_partial_contents_async: (skip)
6896 * @file: input #GFile
6897 * @cancellable: optional #GCancellable object, %NULL to ignore
6898 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6899 * and to specify whether further data should be read
6900 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6901 * @user_data: the data to pass to the callback functions
6903 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6904 * be used to stop reading from the file when appropriate, else this
6905 * function will behave exactly as g_file_load_contents_async(). This
6906 * operation can be finished by g_file_load_partial_contents_finish().
6908 * Users of this function should be aware that @user_data is passed to
6909 * both the @read_more_callback and the @callback.
6911 * If @cancellable is not %NULL, then the operation can be cancelled by
6912 * triggering the cancellable object from another thread. If the operation
6913 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6916 g_file_load_partial_contents_async (GFile *file,
6917 GCancellable *cancellable,
6918 GFileReadMoreCallback read_more_callback,
6919 GAsyncReadyCallback callback,
6922 LoadContentsData *data;
6924 g_return_if_fail (G_IS_FILE (file));
6926 data = g_new0 (LoadContentsData, 1);
6927 data->read_more_callback = read_more_callback;
6928 data->content = g_byte_array_new ();
6930 data->task = g_task_new (file, cancellable, callback, user_data);
6931 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6933 g_file_read_async (file,
6935 g_task_get_cancellable (data->task),
6936 load_contents_open_callback,
6941 * g_file_load_partial_contents_finish:
6942 * @file: input #GFile
6943 * @res: a #GAsyncResult
6944 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6945 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6946 * or %NULL if the length is not needed
6947 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6948 * or %NULL if the entity tag is not needed
6949 * @error: a #GError, or %NULL
6951 * Finishes an asynchronous partial load operation that was started
6952 * with g_file_load_partial_contents_async(). The data is always
6953 * zero-terminated, but this is not included in the resultant @length.
6954 * The returned @content should be freed with g_free() when no longer
6957 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6958 * present, it will be set appropriately.
6961 g_file_load_partial_contents_finish (GFile *file,
6969 LoadContentsData *data;
6971 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6972 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6973 g_return_val_if_fail (contents != NULL, FALSE);
6975 task = G_TASK (res);
6977 if (!g_task_propagate_boolean (task, error))
6984 data = g_task_get_task_data (task);
6987 *length = data->pos;
6991 *etag_out = data->etag;
6995 /* Zero terminate */
6996 g_byte_array_set_size (data->content, data->pos + 1);
6997 data->content->data[data->pos] = 0;
6999 *contents = (char *)g_byte_array_free (data->content, FALSE);
7000 data->content = NULL;
7006 * g_file_load_contents_async:
7007 * @file: input #GFile
7008 * @cancellable: optional #GCancellable object, %NULL to ignore
7009 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7010 * @user_data: the data to pass to callback function
7012 * Starts an asynchronous load of the @file's contents.
7014 * For more details, see g_file_load_contents() which is
7015 * the synchronous version of this call.
7017 * When the load operation has completed, @callback will be called
7018 * with @user data. To finish the operation, call
7019 * g_file_load_contents_finish() with the #GAsyncResult returned by
7022 * If @cancellable is not %NULL, then the operation can be cancelled by
7023 * triggering the cancellable object from another thread. If the operation
7024 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7027 g_file_load_contents_async (GFile *file,
7028 GCancellable *cancellable,
7029 GAsyncReadyCallback callback,
7032 g_file_load_partial_contents_async (file,
7035 callback, user_data);
7039 * g_file_load_contents_finish:
7040 * @file: input #GFile
7041 * @res: a #GAsyncResult
7042 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7043 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7044 * or %NULL if the length is not needed
7045 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7046 * or %NULL if the entity tag is not needed
7047 * @error: a #GError, or %NULL
7049 * Finishes an asynchronous load of the @file's contents.
7050 * The contents are placed in @contents, and @length is set to the
7051 * size of the @contents string. The @content should be freed with
7052 * g_free() when no longer needed. If @etag_out is present, it will be
7053 * set to the new entity tag for the @file.
7055 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7056 * present, it will be set appropriately.
7059 g_file_load_contents_finish (GFile *file,
7066 return g_file_load_partial_contents_finish (file,
7075 * g_file_replace_contents:
7076 * @file: input #GFile
7077 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7078 * @length: the length of @contents in bytes
7079 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7081 * @make_backup: %TRUE if a backup should be created
7082 * @flags: a set of #GFileCreateFlags
7083 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7084 * for the document. This should be freed with g_free() when no longer
7086 * @cancellable: optional #GCancellable object, %NULL to ignore
7087 * @error: a #GError, or %NULL
7089 * Replaces the contents of @file with @contents of @length bytes.
7091 * If @etag is specified (not %NULL), any existing file must have that etag,
7092 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7094 * If @make_backup is %TRUE, this function will attempt to make a backup
7097 * If @cancellable is not %NULL, then the operation can be cancelled by
7098 * triggering the cancellable object from another thread. If the operation
7099 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7101 * The returned @new_etag can be used to verify that the file hasn't
7102 * changed the next time it is saved over.
7104 * Returns: %TRUE if successful. If an error has occurred, this function
7105 * will return %FALSE and set @error appropriately if present.
7108 g_file_replace_contents (GFile *file,
7109 const char *contents,
7112 gboolean make_backup,
7113 GFileCreateFlags flags,
7115 GCancellable *cancellable,
7118 GFileOutputStream *out;
7119 gsize pos, remainder;
7123 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7124 g_return_val_if_fail (contents != NULL, FALSE);
7126 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7132 while (remainder > 0 &&
7133 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7135 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7143 if (remainder > 0 && res < 0)
7145 /* Ignore errors on close */
7146 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7147 g_object_unref (out);
7149 /* error is set already */
7153 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7156 *new_etag = g_file_output_stream_get_etag (out);
7158 g_object_unref (out);
7169 } ReplaceContentsData;
7172 replace_contents_data_free (ReplaceContentsData *data)
7174 g_bytes_unref (data->content);
7175 g_free (data->etag);
7180 replace_contents_close_callback (GObject *obj,
7181 GAsyncResult *close_res,
7184 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7185 ReplaceContentsData *data = user_data;
7187 /* Ignore errors here, we're only reading anyway */
7188 g_output_stream_close_finish (stream, close_res, NULL);
7189 g_object_unref (stream);
7193 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7194 g_task_return_boolean (data->task, TRUE);
7196 g_object_unref (data->task);
7200 replace_contents_write_callback (GObject *obj,
7201 GAsyncResult *read_res,
7204 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7205 ReplaceContentsData *data = user_data;
7206 GError *error = NULL;
7209 write_size = g_output_stream_write_finish (stream, read_res, &error);
7211 if (write_size <= 0)
7213 /* Error or EOF, close the file */
7216 data->failed = TRUE;
7217 g_task_return_error (data->task, error);
7219 g_output_stream_close_async (stream, 0,
7220 g_task_get_cancellable (data->task),
7221 replace_contents_close_callback, data);
7223 else if (write_size > 0)
7225 const gchar *content;
7228 content = g_bytes_get_data (data->content, &length);
7229 data->pos += write_size;
7231 if (data->pos >= length)
7232 g_output_stream_close_async (stream, 0,
7233 g_task_get_cancellable (data->task),
7234 replace_contents_close_callback, data);
7236 g_output_stream_write_async (stream,
7237 content + data->pos,
7240 g_task_get_cancellable (data->task),
7241 replace_contents_write_callback,
7247 replace_contents_open_callback (GObject *obj,
7248 GAsyncResult *open_res,
7251 GFile *file = G_FILE (obj);
7252 GFileOutputStream *stream;
7253 ReplaceContentsData *data = user_data;
7254 GError *error = NULL;
7256 stream = g_file_replace_finish (file, open_res, &error);
7260 const gchar *content;
7263 content = g_bytes_get_data (data->content, &length);
7264 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7265 content + data->pos,
7268 g_task_get_cancellable (data->task),
7269 replace_contents_write_callback,
7274 g_task_return_error (data->task, error);
7275 g_object_unref (data->task);
7280 * g_file_replace_contents_async:
7281 * @file: input #GFile
7282 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7283 * @length: the length of @contents in bytes
7284 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7285 * @make_backup: %TRUE if a backup should be created
7286 * @flags: a set of #GFileCreateFlags
7287 * @cancellable: optional #GCancellable object, %NULL to ignore
7288 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7289 * @user_data: the data to pass to callback function
7291 * Starts an asynchronous replacement of @file with the given
7292 * @contents of @length bytes. @etag will replace the document's
7293 * current entity tag.
7295 * When this operation has completed, @callback will be called with
7296 * @user_user data, and the operation can be finalized with
7297 * g_file_replace_contents_finish().
7299 * If @cancellable is not %NULL, then the operation can be cancelled by
7300 * triggering the cancellable object from another thread. If the operation
7301 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7303 * If @make_backup is %TRUE, this function will attempt to
7304 * make a backup of @file.
7306 * Note that no copy of @content will be made, so it must stay valid
7307 * until @callback is called. See g_file_replace_contents_bytes_async()
7308 * for a #GBytes version that will automatically hold a reference to the
7309 * contents (without copying) for the duration of the call.
7312 g_file_replace_contents_async (GFile *file,
7313 const char *contents,
7316 gboolean make_backup,
7317 GFileCreateFlags flags,
7318 GCancellable *cancellable,
7319 GAsyncReadyCallback callback,
7324 bytes = g_bytes_new_static (contents, length);
7325 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7326 cancellable, callback, user_data);
7327 g_bytes_unref (bytes);
7331 * g_file_replace_contents_bytes_async:
7332 * @file: input #GFile
7333 * @contents: a #GBytes
7334 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7335 * @make_backup: %TRUE if a backup should be created
7336 * @flags: a set of #GFileCreateFlags
7337 * @cancellable: optional #GCancellable object, %NULL to ignore
7338 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7339 * @user_data: the data to pass to callback function
7341 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7342 * This function will keep a ref on @contents until the operation is done.
7343 * Unlike g_file_replace_contents_async() this allows forgetting about the
7344 * content without waiting for the callback.
7346 * When this operation has completed, @callback will be called with
7347 * @user_user data, and the operation can be finalized with
7348 * g_file_replace_contents_finish().
7353 g_file_replace_contents_bytes_async (GFile *file,
7356 gboolean make_backup,
7357 GFileCreateFlags flags,
7358 GCancellable *cancellable,
7359 GAsyncReadyCallback callback,
7362 ReplaceContentsData *data;
7364 g_return_if_fail (G_IS_FILE (file));
7365 g_return_if_fail (contents != NULL);
7367 data = g_new0 (ReplaceContentsData, 1);
7369 data->content = g_bytes_ref (contents);
7371 data->task = g_task_new (file, cancellable, callback, user_data);
7372 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7374 g_file_replace_async (file,
7379 g_task_get_cancellable (data->task),
7380 replace_contents_open_callback,
7385 * g_file_replace_contents_finish:
7386 * @file: input #GFile
7387 * @res: a #GAsyncResult
7388 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7389 * for the document. This should be freed with g_free() when it is no
7390 * longer needed, or %NULL
7391 * @error: a #GError, or %NULL
7393 * Finishes an asynchronous replace of the given @file. See
7394 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7395 * tag for the document, if present.
7397 * Returns: %TRUE on success, %FALSE on failure.
7400 g_file_replace_contents_finish (GFile *file,
7406 ReplaceContentsData *data;
7408 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7409 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7411 task = G_TASK (res);
7413 if (!g_task_propagate_boolean (task, error))
7416 data = g_task_get_task_data (task);
7420 *new_etag = data->etag;
7421 data->etag = NULL; /* Take ownership */
7428 g_file_real_measure_disk_usage (GFile *file,
7429 GFileMeasureFlags flags,
7430 GCancellable *cancellable,
7431 GFileMeasureProgressCallback progress_callback,
7432 gpointer progress_data,
7433 guint64 *disk_usage,
7438 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7439 "Operation not supported for the current backend.");
7445 GFileMeasureFlags flags;
7446 GFileMeasureProgressCallback progress_callback;
7447 gpointer progress_data;
7459 GFileMeasureProgressCallback callback;
7462 guint64 current_size;
7468 measure_disk_usage_invoke_progress (gpointer user_data)
7470 MeasureProgress *progress = user_data;
7472 (* progress->callback) (progress->reporting,
7473 progress->current_size, progress->num_dirs, progress->num_files,
7474 progress->user_data);
7480 measure_disk_usage_progress (gboolean reporting,
7481 guint64 current_size,
7486 MeasureProgress progress;
7487 GTask *task = user_data;
7488 MeasureTaskData *data;
7490 data = g_task_get_task_data (task);
7492 progress.callback = data->progress_callback;
7493 progress.user_data = data->progress_data;
7494 progress.reporting = reporting;
7495 progress.current_size = current_size;
7496 progress.num_dirs = num_dirs;
7497 progress.num_files = num_files;
7499 g_main_context_invoke_full (g_task_get_context (task),
7500 g_task_get_priority (task),
7501 measure_disk_usage_invoke_progress,
7502 g_memdup (&progress, sizeof progress),
7507 measure_disk_usage_thread (GTask *task,
7508 gpointer source_object,
7510 GCancellable *cancellable)
7512 MeasureTaskData *data = task_data;
7513 GError *error = NULL;
7514 MeasureResult result;
7516 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7517 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7518 &result.disk_usage, &result.num_dirs, &result.num_files,
7520 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7522 g_task_return_error (task, error);
7526 g_file_real_measure_disk_usage_async (GFile *file,
7527 GFileMeasureFlags flags,
7529 GCancellable *cancellable,
7530 GFileMeasureProgressCallback progress_callback,
7531 gpointer progress_data,
7532 GAsyncReadyCallback callback,
7535 MeasureTaskData data;
7539 data.progress_callback = progress_callback;
7540 data.progress_data = progress_data;
7542 task = g_task_new (file, cancellable, callback, user_data);
7543 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7544 g_task_set_priority (task, io_priority);
7546 g_task_run_in_thread (task, measure_disk_usage_thread);
7547 g_object_unref (task);
7551 g_file_real_measure_disk_usage_finish (GFile *file,
7552 GAsyncResult *result,
7553 guint64 *disk_usage,
7558 MeasureResult *measure_result;
7560 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7562 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7564 if (measure_result == NULL)
7568 *disk_usage = measure_result->disk_usage;
7571 *num_dirs = measure_result->num_dirs;
7574 *num_files = measure_result->num_files;
7576 g_free (measure_result);
7582 * g_file_measure_disk_usage:
7584 * @flags: #GFileMeasureFlags
7585 * @cancellable: (allow-none): optional #GCancellable
7586 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7587 * @progress_data: user_data for @progress_callback
7588 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7589 * @num_dirs: (allow-none) (out): the number of directories encountered
7590 * @num_files: (allow-none) (out): the number of non-directories encountered
7591 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7593 * Recursively measures the disk usage of @file.
7595 * This is essentially an analog of the 'du' command, but it also
7596 * reports the number of directories and non-directory files encountered
7597 * (including things like symbolic links).
7599 * By default, errors are only reported against the toplevel file
7600 * itself. Errors found while recursing are silently ignored, unless
7601 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7603 * The returned size, @disk_usage, is in bytes and should be formatted
7604 * with g_format_size() in order to get something reasonable for showing
7605 * in a user interface.
7607 * @progress_callback and @progress_data can be given to request
7608 * periodic progress updates while scanning. See the documentation for
7609 * #GFileMeasureProgressCallback for information about when and how the
7610 * callback will be invoked.
7612 * Returns: %TRUE if successful, with the out parameters set.
7613 * %FALSE otherwise, with @error set.
7618 g_file_measure_disk_usage (GFile *file,
7619 GFileMeasureFlags flags,
7620 GCancellable *cancellable,
7621 GFileMeasureProgressCallback progress_callback,
7622 gpointer progress_data,
7623 guint64 *disk_usage,
7628 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7629 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7630 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7632 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7633 progress_callback, progress_data,
7634 disk_usage, num_dirs, num_files,
7639 * g_file_measure_disk_usage_async:
7641 * @flags: #GFileMeasureFlags
7642 * @io_priority: the [I/O priority][io-priority] of the request
7643 * @cancellable: (allow-none): optional #GCancellable
7644 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7645 * @progress_data: user_data for @progress_callback
7646 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7647 * @user_data: the data to pass to callback function
7649 * Recursively measures the disk usage of @file.
7651 * This is the asynchronous version of g_file_measure_disk_usage(). See
7652 * there for more information.
7657 g_file_measure_disk_usage_async (GFile *file,
7658 GFileMeasureFlags flags,
7660 GCancellable *cancellable,
7661 GFileMeasureProgressCallback progress_callback,
7662 gpointer progress_data,
7663 GAsyncReadyCallback callback,
7666 g_return_if_fail (G_IS_FILE (file));
7667 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7669 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7670 progress_callback, progress_data,
7671 callback, user_data);
7675 * g_file_measure_disk_usage_finish:
7677 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7678 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7679 * @num_dirs: (allow-none) (out): the number of directories encountered
7680 * @num_files: (allow-none) (out): the number of non-directories encountered
7681 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7683 * Collects the results from an earlier call to
7684 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7687 * Returns: %TRUE if successful, with the out parameters set.
7688 * %FALSE otherwise, with @error set.
7693 g_file_measure_disk_usage_finish (GFile *file,
7694 GAsyncResult *result,
7695 guint64 *disk_usage,
7700 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7701 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7703 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7707 * g_file_start_mountable:
7708 * @file: input #GFile
7709 * @flags: flags affecting the operation
7710 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7711 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7712 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7713 * @user_data: the data to pass to callback function
7715 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7716 * Using @start_operation, you can request callbacks when, for instance,
7717 * passwords are needed during authentication.
7719 * If @cancellable is not %NULL, then the operation can be cancelled by
7720 * triggering the cancellable object from another thread. If the operation
7721 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7723 * When the operation is finished, @callback will be called.
7724 * You can then call g_file_mount_mountable_finish() to get
7725 * the result of the operation.
7730 g_file_start_mountable (GFile *file,
7731 GDriveStartFlags flags,
7732 GMountOperation *start_operation,
7733 GCancellable *cancellable,
7734 GAsyncReadyCallback callback,
7739 g_return_if_fail (G_IS_FILE (file));
7741 iface = G_FILE_GET_IFACE (file);
7743 if (iface->start_mountable == NULL)
7745 g_task_report_new_error (file, callback, user_data,
7746 g_file_start_mountable,
7747 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7748 _("Operation not supported"));
7752 (* iface->start_mountable) (file,
7761 * g_file_start_mountable_finish:
7762 * @file: input #GFile
7763 * @result: a #GAsyncResult
7764 * @error: a #GError, or %NULL
7766 * Finishes a start operation. See g_file_start_mountable() for details.
7768 * Finish an asynchronous start operation that was started
7769 * with g_file_start_mountable().
7771 * Returns: %TRUE if the operation finished successfully. %FALSE
7777 g_file_start_mountable_finish (GFile *file,
7778 GAsyncResult *result,
7783 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7784 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7786 if (g_async_result_legacy_propagate_error (result, error))
7788 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7789 return g_task_propagate_boolean (G_TASK (result), error);
7791 iface = G_FILE_GET_IFACE (file);
7792 return (* iface->start_mountable_finish) (file, result, error);
7796 * g_file_stop_mountable:
7797 * @file: input #GFile
7798 * @flags: flags affecting the operation
7799 * @mount_operation: (allow-none): a #GMountOperation,
7800 * or %NULL to avoid user interaction.
7801 * @cancellable: (allow-none): optional #GCancellable object,
7803 * @callback: (allow-none): a #GAsyncReadyCallback to call
7804 * when the request is satisfied, or %NULL
7805 * @user_data: the data to pass to callback function
7807 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7809 * If @cancellable is not %NULL, then the operation can be cancelled by
7810 * triggering the cancellable object from another thread. If the operation
7811 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7813 * When the operation is finished, @callback will be called.
7814 * You can then call g_file_stop_mountable_finish() to get
7815 * the result of the operation.
7820 g_file_stop_mountable (GFile *file,
7821 GMountUnmountFlags flags,
7822 GMountOperation *mount_operation,
7823 GCancellable *cancellable,
7824 GAsyncReadyCallback callback,
7829 g_return_if_fail (G_IS_FILE (file));
7831 iface = G_FILE_GET_IFACE (file);
7833 if (iface->stop_mountable == NULL)
7835 g_task_report_new_error (file, callback, user_data,
7836 g_file_stop_mountable,
7837 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7838 _("Operation not supported"));
7842 (* iface->stop_mountable) (file,
7851 * g_file_stop_mountable_finish:
7852 * @file: input #GFile
7853 * @result: a #GAsyncResult
7854 * @error: a #GError, or %NULL
7856 * Finishes an stop operation, see g_file_stop_mountable() for details.
7858 * Finish an asynchronous stop operation that was started
7859 * with g_file_stop_mountable().
7861 * Returns: %TRUE if the operation finished successfully.
7867 g_file_stop_mountable_finish (GFile *file,
7868 GAsyncResult *result,
7873 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7874 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7876 if (g_async_result_legacy_propagate_error (result, error))
7878 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7879 return g_task_propagate_boolean (G_TASK (result), error);
7881 iface = G_FILE_GET_IFACE (file);
7882 return (* iface->stop_mountable_finish) (file, result, error);
7886 * g_file_poll_mountable:
7887 * @file: input #GFile
7888 * @cancellable: optional #GCancellable object, %NULL to ignore
7889 * @callback: (allow-none): a #GAsyncReadyCallback to call
7890 * when the request is satisfied, or %NULL
7891 * @user_data: the data to pass to callback function
7893 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7895 * If @cancellable is not %NULL, then the operation can be cancelled by
7896 * triggering the cancellable object from another thread. If the operation
7897 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7899 * When the operation is finished, @callback will be called.
7900 * You can then call g_file_mount_mountable_finish() to get
7901 * the result of the operation.
7906 g_file_poll_mountable (GFile *file,
7907 GCancellable *cancellable,
7908 GAsyncReadyCallback callback,
7913 g_return_if_fail (G_IS_FILE (file));
7915 iface = G_FILE_GET_IFACE (file);
7917 if (iface->poll_mountable == NULL)
7919 g_task_report_new_error (file, callback, user_data,
7920 g_file_poll_mountable,
7921 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7922 _("Operation not supported"));
7926 (* iface->poll_mountable) (file,
7933 * g_file_poll_mountable_finish:
7934 * @file: input #GFile
7935 * @result: a #GAsyncResult
7936 * @error: a #GError, or %NULL
7938 * Finishes a poll operation. See g_file_poll_mountable() for details.
7940 * Finish an asynchronous poll operation that was polled
7941 * with g_file_poll_mountable().
7943 * Returns: %TRUE if the operation finished successfully. %FALSE
7949 g_file_poll_mountable_finish (GFile *file,
7950 GAsyncResult *result,
7955 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7956 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7958 if (g_async_result_legacy_propagate_error (result, error))
7960 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7961 return g_task_propagate_boolean (G_TASK (result), error);
7963 iface = G_FILE_GET_IFACE (file);
7964 return (* iface->poll_mountable_finish) (file, result, error);
7968 * g_file_supports_thread_contexts:
7971 * Checks if @file supports
7972 * [thread-default contexts][g-main-context-push-thread-default-context].
7973 * If this returns %FALSE, you cannot perform asynchronous operations on
7974 * @file in a thread that has a thread-default context.
7976 * Returns: Whether or not @file supports thread-default contexts.
7981 g_file_supports_thread_contexts (GFile *file)
7985 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7987 iface = G_FILE_GET_IFACE (file);
7988 return iface->supports_thread_contexts;