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 * <para id="gfile-etag">
141 * One notable feature of #GFiles are entity tags, or "etags" for
142 * short. Entity tags are somewhat like a more abstract version of the
143 * traditional mtime, and can be used to quickly determine if the file
144 * has been modified from the version on the file system. See the
146 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
147 * 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 <link linkend="io-priority">I/O priority</link>
976 * @cancellable: (allow-none): optional #GCancellable object,
978 * @callback: (scope async): a #GAsyncReadyCallback to call when the
979 * request is satisfied
980 * @user_data: (closure): the data to pass to callback function
982 * Asynchronously gets the requested information about the files
983 * in a directory. The result is a #GFileEnumerator object that will
984 * give out #GFileInfo objects for all the files in the directory.
986 * For more details, see g_file_enumerate_children() which is
987 * the synchronous version of this call.
989 * When the operation is finished, @callback will be called. You can
990 * then call g_file_enumerate_children_finish() to get the result of
994 g_file_enumerate_children_async (GFile *file,
995 const char *attributes,
996 GFileQueryInfoFlags flags,
998 GCancellable *cancellable,
999 GAsyncReadyCallback callback,
1004 g_return_if_fail (G_IS_FILE (file));
1006 iface = G_FILE_GET_IFACE (file);
1007 (* iface->enumerate_children_async) (file,
1017 * g_file_enumerate_children_finish:
1018 * @file: input #GFile
1019 * @res: a #GAsyncResult
1022 * Finishes an async enumerate children operation.
1023 * See g_file_enumerate_children_async().
1025 * Returns: (transfer full): a #GFileEnumerator or %NULL
1026 * if an error occurred.
1027 * Free the returned object with g_object_unref().
1030 g_file_enumerate_children_finish (GFile *file,
1036 g_return_val_if_fail (G_IS_FILE (file), NULL);
1037 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1039 if (g_async_result_legacy_propagate_error (res, error))
1042 iface = G_FILE_GET_IFACE (file);
1043 return (* iface->enumerate_children_finish) (file, res, error);
1047 * g_file_query_exists:
1048 * @file: input #GFile
1049 * @cancellable: (allow-none): optional #GCancellable object,
1052 * Utility function to check if a particular file exists. This is
1053 * implemented using g_file_query_info() and as such does blocking I/O.
1055 * Note that in many cases it is racy to first check for file existence
1056 * and then execute something based on the outcome of that, because the
1057 * file might have been created or removed in between the operations. The
1058 * general approach to handling that is to not check, but just do the
1059 * operation and handle the errors as they come.
1061 * As an example of race-free checking, take the case of reading a file,
1062 * and if it doesn't exist, creating it. There are two racy versions: read
1063 * it, and on error create it; and: check if it exists, if not create it.
1064 * These can both result in two processes creating the file (with perhaps
1065 * a partially written file as the result). The correct approach is to
1066 * always try to create the file with g_file_create() which will either
1067 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1069 * However, in many cases an existence check is useful in a user interface,
1070 * for instance to make a menu item sensitive/insensitive, so that you don't
1071 * have to fool users that something is possible and then just show an error
1072 * dialog. If you do this, you should make sure to also handle the errors
1073 * that can happen due to races when you execute the operation.
1075 * Returns: %TRUE if the file exists (and can be detected without error),
1076 * %FALSE otherwise (or if cancelled).
1079 g_file_query_exists (GFile *file,
1080 GCancellable *cancellable)
1084 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1086 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1087 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1090 g_object_unref (info);
1098 * g_file_query_file_type:
1099 * @file: input #GFile
1100 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1101 * @cancellable: (allow-none): optional #GCancellable object,
1104 * Utility function to inspect the #GFileType of a file. This is
1105 * implemented using g_file_query_info() and as such does blocking I/O.
1107 * The primary use case of this method is to check if a file is
1108 * a regular file, directory, or symlink.
1110 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1111 * if the file does not exist
1116 g_file_query_file_type (GFile *file,
1117 GFileQueryInfoFlags flags,
1118 GCancellable *cancellable)
1121 GFileType file_type;
1123 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1124 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1128 file_type = g_file_info_get_file_type (info);
1129 g_object_unref (info);
1132 file_type = G_FILE_TYPE_UNKNOWN;
1138 * g_file_query_info:
1139 * @file: input #GFile
1140 * @attributes: an attribute query string
1141 * @flags: a set of #GFileQueryInfoFlags
1142 * @cancellable: (allow-none): optional #GCancellable object,
1146 * Gets the requested information about specified @file.
1147 * The result is a #GFileInfo object that contains key-value
1148 * attributes (such as the type or size of the file).
1150 * The @attributes value is a string that specifies the file
1151 * attributes that should be gathered. It is not an error if
1152 * it's not possible to read a particular requested attribute
1153 * from a file - it just won't be set. @attributes should be a
1154 * comma-separated list of attributes or attribute wildcards.
1155 * The wildcard "*" means all attributes, and a wildcard like
1156 * "standard::*" means all attributes in the standard namespace.
1157 * An example attribute query be "standard::*,owner::user".
1158 * The standard attributes are available as defines, like
1159 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1161 * If @cancellable is not %NULL, then the operation can be cancelled
1162 * by triggering the cancellable object from another thread. If the
1163 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1166 * For symlinks, normally the information about the target of the
1167 * symlink is returned, rather than information about the symlink
1168 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1169 * in @flags the information about the symlink itself will be returned.
1170 * Also, for symlinks that point to non-existing files the information
1171 * about the symlink itself will be returned.
1173 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1174 * returned. Other errors are possible too, and depend on what kind of
1175 * filesystem the file is on.
1177 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1178 * on error. Free the returned object with g_object_unref().
1181 g_file_query_info (GFile *file,
1182 const char *attributes,
1183 GFileQueryInfoFlags flags,
1184 GCancellable *cancellable,
1189 g_return_val_if_fail (G_IS_FILE (file), NULL);
1191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1194 iface = G_FILE_GET_IFACE (file);
1196 if (iface->query_info == NULL)
1198 g_set_error_literal (error, G_IO_ERROR,
1199 G_IO_ERROR_NOT_SUPPORTED,
1200 _("Operation not supported"));
1204 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1208 * g_file_query_info_async:
1209 * @file: input #GFile
1210 * @attributes: an attribute query string
1211 * @flags: a set of #GFileQueryInfoFlags
1212 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1214 * @cancellable: (allow-none): optional #GCancellable object,
1216 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1217 * request is satisfied
1218 * @user_data: (closure): the data to pass to callback function
1220 * Asynchronously gets the requested information about specified @file.
1221 * The result is a #GFileInfo object that contains key-value attributes
1222 * (such as type or size for the file).
1224 * For more details, see g_file_query_info() which is the synchronous
1225 * version of this call.
1227 * When the operation is finished, @callback will be called. You can
1228 * then call g_file_query_info_finish() to get the result of the operation.
1231 g_file_query_info_async (GFile *file,
1232 const char *attributes,
1233 GFileQueryInfoFlags flags,
1235 GCancellable *cancellable,
1236 GAsyncReadyCallback callback,
1241 g_return_if_fail (G_IS_FILE (file));
1243 iface = G_FILE_GET_IFACE (file);
1244 (* iface->query_info_async) (file,
1254 * g_file_query_info_finish:
1255 * @file: input #GFile
1256 * @res: a #GAsyncResult
1259 * Finishes an asynchronous file info query.
1260 * See g_file_query_info_async().
1262 * Returns: (transfer full): #GFileInfo for given @file
1263 * or %NULL on error. Free the returned object with
1267 g_file_query_info_finish (GFile *file,
1273 g_return_val_if_fail (G_IS_FILE (file), NULL);
1274 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1276 if (g_async_result_legacy_propagate_error (res, error))
1279 iface = G_FILE_GET_IFACE (file);
1280 return (* iface->query_info_finish) (file, res, error);
1284 * g_file_query_filesystem_info:
1285 * @file: input #GFile
1286 * @attributes: an attribute query string
1287 * @cancellable: (allow-none): optional #GCancellable object,
1291 * Similar to g_file_query_info(), but obtains information
1292 * about the filesystem the @file is on, rather than the file itself.
1293 * For instance the amount of space available and the type of
1296 * The @attributes value is a string that specifies the attributes
1297 * that should be gathered. It is not an error if it's not possible
1298 * to read a particular requested attribute from a file - it just
1299 * won't be set. @attributes should be a comma-separated list of
1300 * attributes or attribute wildcards. The wildcard "*" means all
1301 * attributes, and a wildcard like "filesystem::*" means all attributes
1302 * in the filesystem namespace. The standard namespace for filesystem
1303 * attributes is "filesystem". Common attributes of interest are
1304 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1305 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1306 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1308 * If @cancellable is not %NULL, then the operation can be cancelled
1309 * by triggering the cancellable object from another thread. If the
1310 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1313 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1314 * be returned. Other errors are possible too, and depend on what
1315 * kind of filesystem the file is on.
1317 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1318 * Free the returned object with g_object_unref().
1321 g_file_query_filesystem_info (GFile *file,
1322 const char *attributes,
1323 GCancellable *cancellable,
1328 g_return_val_if_fail (G_IS_FILE (file), NULL);
1330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1333 iface = G_FILE_GET_IFACE (file);
1335 if (iface->query_filesystem_info == NULL)
1337 g_set_error_literal (error, G_IO_ERROR,
1338 G_IO_ERROR_NOT_SUPPORTED,
1339 _("Operation not supported"));
1343 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1347 * g_file_query_filesystem_info_async:
1348 * @file: input #GFile
1349 * @attributes: an attribute query string
1350 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1352 * @cancellable: (allow-none): optional #GCancellable object,
1354 * @callback: (scope async): a #GAsyncReadyCallback to call
1355 * when the request is satisfied
1356 * @user_data: (closure): the data to pass to callback function
1358 * Asynchronously gets the requested information about the filesystem
1359 * that the specified @file is on. The result is a #GFileInfo object
1360 * that contains key-value attributes (such as type or size for the
1363 * For more details, see g_file_query_filesystem_info() which is the
1364 * synchronous version of this call.
1366 * When the operation is finished, @callback will be called. You can
1367 * then call g_file_query_info_finish() to get the result of the
1371 g_file_query_filesystem_info_async (GFile *file,
1372 const char *attributes,
1374 GCancellable *cancellable,
1375 GAsyncReadyCallback callback,
1380 g_return_if_fail (G_IS_FILE (file));
1382 iface = G_FILE_GET_IFACE (file);
1383 (* iface->query_filesystem_info_async) (file,
1392 * g_file_query_filesystem_info_finish:
1393 * @file: input #GFile
1394 * @res: a #GAsyncResult
1397 * Finishes an asynchronous filesystem info query.
1398 * See g_file_query_filesystem_info_async().
1400 * Returns: (transfer full): #GFileInfo for given @file
1401 * or %NULL on error.
1402 * Free the returned object with g_object_unref().
1405 g_file_query_filesystem_info_finish (GFile *file,
1411 g_return_val_if_fail (G_IS_FILE (file), NULL);
1412 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1414 if (g_async_result_legacy_propagate_error (res, error))
1417 iface = G_FILE_GET_IFACE (file);
1418 return (* iface->query_filesystem_info_finish) (file, res, error);
1422 * g_file_find_enclosing_mount:
1423 * @file: input #GFile
1424 * @cancellable: (allow-none): optional #GCancellable object,
1428 * Gets a #GMount for the #GFile.
1430 * If the #GFileIface for @file does not have a mount (e.g.
1431 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1432 * and %NULL will be returned.
1434 * If @cancellable is not %NULL, then the operation can be cancelled by
1435 * triggering the cancellable object from another thread. If the operation
1436 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1438 * Returns: (transfer full): a #GMount where the @file is located
1439 * or %NULL on error.
1440 * Free the returned object with g_object_unref().
1443 g_file_find_enclosing_mount (GFile *file,
1444 GCancellable *cancellable,
1449 g_return_val_if_fail (G_IS_FILE (file), NULL);
1451 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1454 iface = G_FILE_GET_IFACE (file);
1455 if (iface->find_enclosing_mount == NULL)
1458 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1459 /* Translators: This is an error message when
1460 * trying to find the enclosing (user visible)
1461 * mount of a file, but none exists.
1463 _("Containing mount does not exist"));
1467 return (* iface->find_enclosing_mount) (file, cancellable, error);
1471 * g_file_find_enclosing_mount_async:
1473 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1475 * @cancellable: (allow-none): optional #GCancellable object,
1477 * @callback: (scope async): a #GAsyncReadyCallback to call
1478 * when the request is satisfied
1479 * @user_data: (closure): the data to pass to callback function
1481 * Asynchronously gets the mount for the file.
1483 * For more details, see g_file_find_enclosing_mount() which is
1484 * the synchronous version of this call.
1486 * When the operation is finished, @callback will be called.
1487 * You can then call g_file_find_enclosing_mount_finish() to
1488 * get the result of the operation.
1491 g_file_find_enclosing_mount_async (GFile *file,
1493 GCancellable *cancellable,
1494 GAsyncReadyCallback callback,
1499 g_return_if_fail (G_IS_FILE (file));
1501 iface = G_FILE_GET_IFACE (file);
1502 (* iface->find_enclosing_mount_async) (file,
1510 * g_file_find_enclosing_mount_finish:
1512 * @res: a #GAsyncResult
1515 * Finishes an asynchronous find mount request.
1516 * See g_file_find_enclosing_mount_async().
1518 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1519 * Free the returned object with g_object_unref().
1522 g_file_find_enclosing_mount_finish (GFile *file,
1528 g_return_val_if_fail (G_IS_FILE (file), NULL);
1529 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1531 if (g_async_result_legacy_propagate_error (res, error))
1534 iface = G_FILE_GET_IFACE (file);
1535 return (* iface->find_enclosing_mount_finish) (file, res, error);
1541 * @file: #GFile to read
1542 * @cancellable: (allow-none): a #GCancellable
1543 * @error: a #GError, or %NULL
1545 * Opens a file for reading. The result is a #GFileInputStream that
1546 * can be used to read the contents of the file.
1548 * If @cancellable is not %NULL, then the operation can be cancelled by
1549 * triggering the cancellable object from another thread. If the operation
1550 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1552 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1553 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1554 * error will be returned. Other errors are possible too, and depend
1555 * on what kind of filesystem the file is on.
1558 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1559 * Free the returned object with g_object_unref().
1562 g_file_read (GFile *file,
1563 GCancellable *cancellable,
1568 g_return_val_if_fail (G_IS_FILE (file), NULL);
1570 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1573 iface = G_FILE_GET_IFACE (file);
1575 if (iface->read_fn == NULL)
1577 g_set_error_literal (error, G_IO_ERROR,
1578 G_IO_ERROR_NOT_SUPPORTED,
1579 _("Operation not supported"));
1583 return (* iface->read_fn) (file, cancellable, error);
1588 * @file: input #GFile
1589 * @flags: a set of #GFileCreateFlags
1590 * @cancellable: (allow-none): optional #GCancellable object,
1592 * @error: a #GError, or %NULL
1594 * Gets an output stream for appending data to the file.
1595 * If the file doesn't already exist it is created.
1597 * By default files created are generally readable by everyone,
1598 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1599 * will be made readable only to the current user, to the level that
1600 * is supported on the target filesystem.
1602 * If @cancellable is not %NULL, then the operation can be cancelled
1603 * by triggering the cancellable object from another thread. If the
1604 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1607 * Some file systems don't allow all file names, and may return an
1608 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1609 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1610 * possible too, and depend on what kind of filesystem the file is on.
1612 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1613 * Free the returned object with g_object_unref().
1616 g_file_append_to (GFile *file,
1617 GFileCreateFlags flags,
1618 GCancellable *cancellable,
1623 g_return_val_if_fail (G_IS_FILE (file), NULL);
1625 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1628 iface = G_FILE_GET_IFACE (file);
1630 if (iface->append_to == NULL)
1632 g_set_error_literal (error, G_IO_ERROR,
1633 G_IO_ERROR_NOT_SUPPORTED,
1634 _("Operation not supported"));
1638 return (* iface->append_to) (file, flags, cancellable, error);
1643 * @file: input #GFile
1644 * @flags: a set of #GFileCreateFlags
1645 * @cancellable: (allow-none): optional #GCancellable object,
1647 * @error: a #GError, or %NULL
1649 * Creates a new file and returns an output stream for writing to it.
1650 * The file must not already exist.
1652 * By default files created are generally readable by everyone,
1653 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1654 * will be made readable only to the current user, to the level
1655 * that is supported on the target filesystem.
1657 * If @cancellable is not %NULL, then the operation can be cancelled
1658 * by triggering the cancellable object from another thread. If the
1659 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1662 * If a file or directory with this name already exists the
1663 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1664 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1665 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1666 * be returned. Other errors are possible too, and depend on what kind
1667 * of filesystem the file is on.
1669 * Returns: (transfer full): a #GFileOutputStream for the newly created
1670 * file, or %NULL on error.
1671 * Free the returned object with g_object_unref().
1674 g_file_create (GFile *file,
1675 GFileCreateFlags flags,
1676 GCancellable *cancellable,
1681 g_return_val_if_fail (G_IS_FILE (file), NULL);
1683 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1686 iface = G_FILE_GET_IFACE (file);
1688 if (iface->create == NULL)
1690 g_set_error_literal (error, G_IO_ERROR,
1691 G_IO_ERROR_NOT_SUPPORTED,
1692 _("Operation not supported"));
1696 return (* iface->create) (file, flags, cancellable, error);
1701 * @file: input #GFile
1702 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1703 * for the current #GFile, or #NULL to ignore
1704 * @make_backup: %TRUE if a backup should be created
1705 * @flags: a set of #GFileCreateFlags
1706 * @cancellable: (allow-none): optional #GCancellable object,
1708 * @error: a #GError, or %NULL
1710 * Returns an output stream for overwriting the file, possibly
1711 * creating a backup copy of the file first. If the file doesn't exist,
1712 * it will be created.
1714 * This will try to replace the file in the safest way possible so
1715 * that any errors during the writing will not affect an already
1716 * existing copy of the file. For instance, for local files it
1717 * may write to a temporary file and then atomically rename over
1718 * the destination when the stream is closed.
1720 * By default files created are generally readable by everyone,
1721 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1722 * will be made readable only to the current user, to the level that
1723 * is supported on the target filesystem.
1725 * If @cancellable is not %NULL, then the operation can be cancelled
1726 * by triggering the cancellable object from another thread. If the
1727 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1730 * If you pass in a non-%NULL @etag value, then this value is
1731 * compared to the current entity tag of the file, and if they differ
1732 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1733 * that the file has been changed since you last read it. You can get
1734 * the new etag from g_file_output_stream_get_etag() after you've
1735 * finished writing and closed the #GFileOutputStream. When you load
1736 * a new file you can use g_file_input_stream_query_info() to get
1737 * the etag of the file.
1739 * If @make_backup is %TRUE, this function will attempt to make a
1740 * backup of the current file before overwriting it. If this fails
1741 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1742 * want to replace anyway, try again with @make_backup set to %FALSE.
1744 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1745 * be returned, and if the file is some other form of non-regular file
1746 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1747 * file systems don't allow all file names, and may return an
1748 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1749 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1750 * possible too, and depend on what kind of filesystem the file is on.
1752 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1753 * Free the returned object with g_object_unref().
1756 g_file_replace (GFile *file,
1758 gboolean make_backup,
1759 GFileCreateFlags flags,
1760 GCancellable *cancellable,
1765 g_return_val_if_fail (G_IS_FILE (file), NULL);
1767 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1770 iface = G_FILE_GET_IFACE (file);
1772 if (iface->replace == NULL)
1774 g_set_error_literal (error, G_IO_ERROR,
1775 G_IO_ERROR_NOT_SUPPORTED,
1776 _("Operation not supported"));
1780 /* Handle empty tag string as NULL in consistent way. */
1781 if (etag && *etag == 0)
1784 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1788 * g_file_open_readwrite:
1789 * @file: #GFile to open
1790 * @cancellable: (allow-none): a #GCancellable
1791 * @error: a #GError, or %NULL
1793 * Opens an existing file for reading and writing. The result is
1794 * a #GFileIOStream that can be used to read and write the contents
1797 * If @cancellable is not %NULL, then the operation can be cancelled
1798 * by triggering the cancellable object from another thread. If the
1799 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1802 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1803 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1804 * error will be returned. Other errors are possible too, and depend on
1805 * what kind of filesystem the file is on. Note that in many non-local
1806 * file cases read and write streams are not supported, so make sure you
1807 * really need to do read and write streaming, rather than just opening
1808 * for reading or writing.
1810 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1811 * Free the returned object with g_object_unref().
1816 g_file_open_readwrite (GFile *file,
1817 GCancellable *cancellable,
1822 g_return_val_if_fail (G_IS_FILE (file), NULL);
1824 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1827 iface = G_FILE_GET_IFACE (file);
1829 if (iface->open_readwrite == NULL)
1831 g_set_error_literal (error, G_IO_ERROR,
1832 G_IO_ERROR_NOT_SUPPORTED,
1833 _("Operation not supported"));
1837 return (* iface->open_readwrite) (file, cancellable, error);
1841 * g_file_create_readwrite:
1843 * @flags: a set of #GFileCreateFlags
1844 * @cancellable: (allow-none): optional #GCancellable object,
1846 * @error: return location for a #GError, or %NULL
1848 * Creates a new file and returns a stream for reading and
1849 * writing to it. The file must not already exist.
1851 * By default files created are generally readable by everyone,
1852 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1853 * will be made readable only to the current user, to the level
1854 * that is supported on the target filesystem.
1856 * If @cancellable is not %NULL, then the operation can be cancelled
1857 * by triggering the cancellable object from another thread. If the
1858 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1861 * If a file or directory with this name already exists, the
1862 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1863 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1864 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1865 * will be returned. Other errors are possible too, and depend on what
1866 * kind of filesystem the file is on.
1868 * Note that in many non-local file cases read and write streams are
1869 * not supported, so make sure you really need to do read and write
1870 * streaming, rather than just opening for reading or writing.
1872 * Returns: (transfer full): a #GFileIOStream for the newly created
1873 * file, or %NULL on error.
1874 * Free the returned object with g_object_unref().
1879 g_file_create_readwrite (GFile *file,
1880 GFileCreateFlags flags,
1881 GCancellable *cancellable,
1886 g_return_val_if_fail (G_IS_FILE (file), NULL);
1888 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1891 iface = G_FILE_GET_IFACE (file);
1893 if (iface->create_readwrite == NULL)
1895 g_set_error_literal (error, G_IO_ERROR,
1896 G_IO_ERROR_NOT_SUPPORTED,
1897 _("Operation not supported"));
1901 return (* iface->create_readwrite) (file, flags, cancellable, error);
1905 * g_file_replace_readwrite:
1907 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1908 * for the current #GFile, or #NULL to ignore
1909 * @make_backup: %TRUE if a backup should be created
1910 * @flags: a set of #GFileCreateFlags
1911 * @cancellable: (allow-none): optional #GCancellable object,
1913 * @error: return location for a #GError, or %NULL
1915 * Returns an output stream for overwriting the file in readwrite mode,
1916 * possibly creating a backup copy of the file first. If the file doesn't
1917 * exist, it will be created.
1919 * For details about the behaviour, see g_file_replace() which does the
1920 * same thing but returns an output stream only.
1922 * Note that in many non-local file cases read and write streams are not
1923 * supported, so make sure you really need to do read and write streaming,
1924 * rather than just opening for reading or writing.
1926 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1927 * Free the returned object with g_object_unref().
1932 g_file_replace_readwrite (GFile *file,
1934 gboolean make_backup,
1935 GFileCreateFlags flags,
1936 GCancellable *cancellable,
1941 g_return_val_if_fail (G_IS_FILE (file), NULL);
1943 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1946 iface = G_FILE_GET_IFACE (file);
1948 if (iface->replace_readwrite == NULL)
1950 g_set_error_literal (error, G_IO_ERROR,
1951 G_IO_ERROR_NOT_SUPPORTED,
1952 _("Operation not supported"));
1956 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1960 * g_file_read_async:
1961 * @file: input #GFile
1962 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1964 * @cancellable: (allow-none): optional #GCancellable object,
1966 * @callback: (scope async): a #GAsyncReadyCallback to call
1967 * when the request is satisfied
1968 * @user_data: (closure): the data to pass to callback function
1970 * Asynchronously opens @file for reading.
1972 * For more details, see g_file_read() which is
1973 * the synchronous version of this call.
1975 * When the operation is finished, @callback will be called.
1976 * You can then call g_file_read_finish() to get the result
1980 g_file_read_async (GFile *file,
1982 GCancellable *cancellable,
1983 GAsyncReadyCallback callback,
1988 g_return_if_fail (G_IS_FILE (file));
1990 iface = G_FILE_GET_IFACE (file);
1991 (* iface->read_async) (file,
1999 * g_file_read_finish:
2000 * @file: input #GFile
2001 * @res: a #GAsyncResult
2002 * @error: a #GError, or %NULL
2004 * Finishes an asynchronous file read operation started with
2005 * g_file_read_async().
2007 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2008 * Free the returned object with g_object_unref().
2011 g_file_read_finish (GFile *file,
2017 g_return_val_if_fail (G_IS_FILE (file), NULL);
2018 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2020 if (g_async_result_legacy_propagate_error (res, error))
2023 iface = G_FILE_GET_IFACE (file);
2024 return (* iface->read_finish) (file, res, error);
2028 * g_file_append_to_async:
2029 * @file: input #GFile
2030 * @flags: a set of #GFileCreateFlags
2031 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2033 * @cancellable: (allow-none): optional #GCancellable object,
2035 * @callback: (scope async): a #GAsyncReadyCallback to call
2036 * when the request is satisfied
2037 * @user_data: (closure): the data to pass to callback function
2039 * Asynchronously opens @file for appending.
2041 * For more details, see g_file_append_to() which is
2042 * the synchronous version of this call.
2044 * When the operation is finished, @callback will be called.
2045 * You can then call g_file_append_to_finish() to get the result
2049 g_file_append_to_async (GFile *file,
2050 GFileCreateFlags flags,
2052 GCancellable *cancellable,
2053 GAsyncReadyCallback callback,
2058 g_return_if_fail (G_IS_FILE (file));
2060 iface = G_FILE_GET_IFACE (file);
2061 (* iface->append_to_async) (file,
2070 * g_file_append_to_finish:
2071 * @file: input #GFile
2072 * @res: #GAsyncResult
2073 * @error: a #GError, or %NULL
2075 * Finishes an asynchronous file append operation started with
2076 * g_file_append_to_async().
2078 * Returns: (transfer full): a valid #GFileOutputStream
2079 * or %NULL on error.
2080 * Free the returned object with g_object_unref().
2083 g_file_append_to_finish (GFile *file,
2089 g_return_val_if_fail (G_IS_FILE (file), NULL);
2090 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2092 if (g_async_result_legacy_propagate_error (res, error))
2095 iface = G_FILE_GET_IFACE (file);
2096 return (* iface->append_to_finish) (file, res, error);
2100 * g_file_create_async:
2101 * @file: input #GFile
2102 * @flags: a set of #GFileCreateFlags
2103 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2105 * @cancellable: (allow-none): optional #GCancellable object,
2107 * @callback: (scope async): a #GAsyncReadyCallback to call
2108 * when the request is satisfied
2109 * @user_data: (closure): the data to pass to callback function
2111 * Asynchronously creates a new file and returns an output stream
2112 * for writing to it. The file must not already exist.
2114 * For more details, see g_file_create() which is
2115 * the synchronous version of this call.
2117 * When the operation is finished, @callback will be called.
2118 * You can then call g_file_create_finish() to get the result
2122 g_file_create_async (GFile *file,
2123 GFileCreateFlags flags,
2125 GCancellable *cancellable,
2126 GAsyncReadyCallback callback,
2131 g_return_if_fail (G_IS_FILE (file));
2133 iface = G_FILE_GET_IFACE (file);
2134 (* iface->create_async) (file,
2143 * g_file_create_finish:
2144 * @file: input #GFile
2145 * @res: a #GAsyncResult
2146 * @error: a #GError, or %NULL
2148 * Finishes an asynchronous file create operation started with
2149 * g_file_create_async().
2151 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2152 * Free the returned object with g_object_unref().
2155 g_file_create_finish (GFile *file,
2161 g_return_val_if_fail (G_IS_FILE (file), NULL);
2162 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2164 if (g_async_result_legacy_propagate_error (res, error))
2167 iface = G_FILE_GET_IFACE (file);
2168 return (* iface->create_finish) (file, res, error);
2172 * g_file_replace_async:
2173 * @file: input #GFile
2174 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2175 * for the current #GFile, or NULL to ignore
2176 * @make_backup: %TRUE if a backup should be created
2177 * @flags: a set of #GFileCreateFlags
2178 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2180 * @cancellable: (allow-none): optional #GCancellable object,
2182 * @callback: (scope async): a #GAsyncReadyCallback to call
2183 * when the request is satisfied
2184 * @user_data: (closure): the data to pass to callback function
2186 * Asynchronously overwrites the file, replacing the contents,
2187 * possibly creating a backup copy of the file first.
2189 * For more details, see g_file_replace() which is
2190 * the synchronous version of this call.
2192 * When the operation is finished, @callback will be called.
2193 * You can then call g_file_replace_finish() to get the result
2197 g_file_replace_async (GFile *file,
2199 gboolean make_backup,
2200 GFileCreateFlags flags,
2202 GCancellable *cancellable,
2203 GAsyncReadyCallback callback,
2208 g_return_if_fail (G_IS_FILE (file));
2210 iface = G_FILE_GET_IFACE (file);
2211 (* iface->replace_async) (file,
2222 * g_file_replace_finish:
2223 * @file: input #GFile
2224 * @res: a #GAsyncResult
2225 * @error: a #GError, or %NULL
2227 * Finishes an asynchronous file replace operation started with
2228 * g_file_replace_async().
2230 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2231 * Free the returned object with g_object_unref().
2234 g_file_replace_finish (GFile *file,
2240 g_return_val_if_fail (G_IS_FILE (file), NULL);
2241 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2243 if (g_async_result_legacy_propagate_error (res, error))
2246 iface = G_FILE_GET_IFACE (file);
2247 return (* iface->replace_finish) (file, res, error);
2251 * g_file_open_readwrite_async
2252 * @file: input #GFile
2253 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2255 * @cancellable: (allow-none): optional #GCancellable object,
2257 * @callback: (scope async): a #GAsyncReadyCallback to call
2258 * when the request is satisfied
2259 * @user_data: (closure): the data to pass to callback function
2261 * Asynchronously opens @file for reading and writing.
2263 * For more details, see g_file_open_readwrite() which is
2264 * the synchronous version of this call.
2266 * When the operation is finished, @callback will be called.
2267 * You can then call g_file_open_readwrite_finish() to get
2268 * the result of the operation.
2273 g_file_open_readwrite_async (GFile *file,
2275 GCancellable *cancellable,
2276 GAsyncReadyCallback callback,
2281 g_return_if_fail (G_IS_FILE (file));
2283 iface = G_FILE_GET_IFACE (file);
2284 (* iface->open_readwrite_async) (file,
2292 * g_file_open_readwrite_finish:
2293 * @file: input #GFile
2294 * @res: a #GAsyncResult
2295 * @error: a #GError, or %NULL
2297 * Finishes an asynchronous file read operation started with
2298 * g_file_open_readwrite_async().
2300 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2301 * Free the returned object with g_object_unref().
2306 g_file_open_readwrite_finish (GFile *file,
2312 g_return_val_if_fail (G_IS_FILE (file), NULL);
2313 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2315 if (g_async_result_legacy_propagate_error (res, error))
2318 iface = G_FILE_GET_IFACE (file);
2319 return (* iface->open_readwrite_finish) (file, res, error);
2323 * g_file_create_readwrite_async:
2324 * @file: input #GFile
2325 * @flags: a set of #GFileCreateFlags
2326 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2328 * @cancellable: (allow-none): optional #GCancellable object,
2330 * @callback: (scope async): a #GAsyncReadyCallback to call
2331 * when the request is satisfied
2332 * @user_data: (closure): the data to pass to callback function
2334 * Asynchronously creates a new file and returns a stream
2335 * for reading and writing to it. The file must not already exist.
2337 * For more details, see g_file_create_readwrite() which is
2338 * the synchronous version of this call.
2340 * When the operation is finished, @callback will be called.
2341 * You can then call g_file_create_readwrite_finish() to get
2342 * the result of the operation.
2347 g_file_create_readwrite_async (GFile *file,
2348 GFileCreateFlags flags,
2350 GCancellable *cancellable,
2351 GAsyncReadyCallback callback,
2356 g_return_if_fail (G_IS_FILE (file));
2358 iface = G_FILE_GET_IFACE (file);
2359 (* iface->create_readwrite_async) (file,
2368 * g_file_create_readwrite_finish:
2369 * @file: input #GFile
2370 * @res: a #GAsyncResult
2371 * @error: a #GError, or %NULL
2373 * Finishes an asynchronous file create operation started with
2374 * g_file_create_readwrite_async().
2376 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2377 * Free the returned object with g_object_unref().
2382 g_file_create_readwrite_finish (GFile *file,
2388 g_return_val_if_fail (G_IS_FILE (file), NULL);
2389 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2391 if (g_async_result_legacy_propagate_error (res, error))
2394 iface = G_FILE_GET_IFACE (file);
2395 return (* iface->create_readwrite_finish) (file, res, error);
2399 * g_file_replace_readwrite_async:
2400 * @file: input #GFile
2401 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2402 * for the current #GFile, or NULL to ignore
2403 * @make_backup: %TRUE if a backup should be created
2404 * @flags: a set of #GFileCreateFlags
2405 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2407 * @cancellable: (allow-none): optional #GCancellable object,
2409 * @callback: (scope async): a #GAsyncReadyCallback to call
2410 * when the request is satisfied
2411 * @user_data: (closure): the data to pass to callback function
2413 * Asynchronously overwrites the file in read-write mode,
2414 * replacing the contents, possibly creating a backup copy
2415 * of the file first.
2417 * For more details, see g_file_replace_readwrite() which is
2418 * the synchronous version of this call.
2420 * When the operation is finished, @callback will be called.
2421 * You can then call g_file_replace_readwrite_finish() to get
2422 * the result of the operation.
2427 g_file_replace_readwrite_async (GFile *file,
2429 gboolean make_backup,
2430 GFileCreateFlags flags,
2432 GCancellable *cancellable,
2433 GAsyncReadyCallback callback,
2438 g_return_if_fail (G_IS_FILE (file));
2440 iface = G_FILE_GET_IFACE (file);
2441 (* iface->replace_readwrite_async) (file,
2452 * g_file_replace_readwrite_finish:
2453 * @file: input #GFile
2454 * @res: a #GAsyncResult
2455 * @error: a #GError, or %NULL
2457 * Finishes an asynchronous file replace operation started with
2458 * g_file_replace_readwrite_async().
2460 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2461 * Free the returned object with g_object_unref().
2466 g_file_replace_readwrite_finish (GFile *file,
2472 g_return_val_if_fail (G_IS_FILE (file), NULL);
2473 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2475 if (g_async_result_legacy_propagate_error (res, error))
2478 iface = G_FILE_GET_IFACE (file);
2479 return (* iface->replace_readwrite_finish) (file, res, error);
2483 copy_symlink (GFile *destination,
2484 GFileCopyFlags flags,
2485 GCancellable *cancellable,
2490 gboolean tried_delete;
2492 GFileType file_type;
2494 tried_delete = FALSE;
2498 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2500 /* Maybe it already existed, and we want to overwrite? */
2501 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2502 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2504 g_clear_error (&my_error);
2506 /* Don't overwrite if the destination is a directory */
2507 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2508 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2509 cancellable, &my_error);
2512 file_type = g_file_info_get_file_type (info);
2513 g_object_unref (info);
2515 if (file_type == G_FILE_TYPE_DIRECTORY)
2517 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2518 _("Can't copy over directory"));
2523 if (!g_file_delete (destination, cancellable, error))
2526 tried_delete = TRUE;
2530 g_propagate_error (error, my_error);
2537 static GFileInputStream *
2538 open_source_for_copy (GFile *source,
2540 GFileCopyFlags flags,
2541 GCancellable *cancellable,
2545 GFileInputStream *ret;
2547 GFileType file_type;
2550 ret = g_file_read (source, cancellable, &my_error);
2554 /* There was an error opening the source, try to set a good error for it: */
2555 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2557 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2558 * as that is less useful to the app. Better check for errors on the
2561 g_error_free (my_error);
2564 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2565 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2566 cancellable, &my_error);
2568 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2570 file_type = g_file_info_get_file_type (info);
2571 g_object_unref (info);
2573 if (flags & G_FILE_COPY_OVERWRITE)
2575 if (file_type == G_FILE_TYPE_DIRECTORY)
2577 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2578 _("Can't copy directory over directory"));
2581 /* continue to would_recurse error */
2585 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2586 _("Target file exists"));
2592 /* Error getting info from target, return that error
2593 * (except for NOT_FOUND, which is no error here)
2595 g_clear_object (&info);
2596 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2598 g_propagate_error (error, my_error);
2601 g_clear_error (&my_error);
2604 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2605 _("Can't recursively copy directory"));
2609 g_propagate_error (error, my_error);
2614 should_copy (GFileAttributeInfo *info,
2615 gboolean copy_all_attributes,
2616 gboolean skip_perms)
2618 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2621 if (copy_all_attributes)
2622 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2623 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2627 build_attribute_list_for_copy (GFile *file,
2628 GFileCopyFlags flags,
2629 char **out_attributes,
2630 GCancellable *cancellable,
2633 gboolean ret = FALSE;
2634 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2638 gboolean copy_all_attributes;
2639 gboolean skip_perms;
2641 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2642 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2644 /* Ignore errors here, if the target supports no attributes there is
2645 * nothing to copy. We still honor the cancellable though.
2647 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2651 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2655 if (attributes == NULL && namespaces == NULL)
2659 s = g_string_new ("");
2663 for (i = 0; i < attributes->n_infos; i++)
2665 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2670 g_string_append_c (s, ',');
2672 g_string_append (s, attributes->infos[i].name);
2679 for (i = 0; i < namespaces->n_infos; i++)
2681 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2686 g_string_append_c (s, ',');
2688 g_string_append (s, namespaces->infos[i].name);
2689 g_string_append (s, "::*");
2695 *out_attributes = g_string_free (s, FALSE);
2699 g_string_free (s, TRUE);
2701 g_file_attribute_info_list_unref (attributes);
2703 g_file_attribute_info_list_unref (namespaces);
2709 * g_file_copy_attributes:
2710 * @source: a #GFile with attributes
2711 * @destination: a #GFile to copy attributes to
2712 * @flags: a set of #GFileCopyFlags
2713 * @cancellable: (allow-none): optional #GCancellable object,
2715 * @error: a #GError, %NULL to ignore
2717 * Copies the file attributes from @source to @destination.
2719 * Normally only a subset of the file attributes are copied,
2720 * those that are copies in a normal file copy operation
2721 * (which for instance does not include e.g. owner). However
2722 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2723 * all the metadata that is possible to copy is copied. This
2724 * is useful when implementing move by copy + delete source.
2726 * Returns: %TRUE if the attributes were copied successfully,
2730 g_file_copy_attributes (GFile *source,
2732 GFileCopyFlags flags,
2733 GCancellable *cancellable,
2736 char *attrs_to_read;
2739 gboolean source_nofollow_symlinks;
2741 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2742 cancellable, error))
2745 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2747 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2748 * we just don't copy it.
2750 info = g_file_query_info (source, attrs_to_read,
2751 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2755 g_free (attrs_to_read);
2760 res = g_file_set_attributes_from_info (destination,
2762 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2765 g_object_unref (info);
2772 copy_stream_with_progress (GInputStream *in,
2775 GCancellable *cancellable,
2776 GFileProgressCallback progress_callback,
2777 gpointer progress_callback_data,
2780 gssize n_read, n_written;
2781 goffset current_size;
2782 char buffer[1024*64], *p;
2788 /* avoid performance impact of querying total size when it's not needed */
2789 if (progress_callback)
2791 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2792 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2796 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2797 total_size = g_file_info_get_size (info);
2798 g_object_unref (info);
2801 if (total_size == -1)
2803 info = g_file_query_info (source,
2804 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2805 G_FILE_QUERY_INFO_NONE,
2809 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2810 total_size = g_file_info_get_size (info);
2811 g_object_unref (info);
2816 if (total_size == -1)
2823 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2833 current_size += n_read;
2838 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2839 if (n_written == -1)
2846 n_read -= n_written;
2852 if (progress_callback)
2853 progress_callback (current_size, total_size, progress_callback_data);
2856 /* Make sure we send full copied size */
2857 if (progress_callback)
2858 progress_callback (current_size, total_size, progress_callback_data);
2866 do_splice (int fd_in,
2871 long *bytes_transferd,
2877 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2885 else if (errsv == ENOSYS || errsv == EINVAL)
2886 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2887 _("Splice not supported"));
2889 g_set_error (error, G_IO_ERROR,
2890 g_io_error_from_errno (errsv),
2891 _("Error splicing file: %s"),
2892 g_strerror (errsv));
2897 *bytes_transferd = result;
2902 splice_stream_with_progress (GInputStream *in,
2904 GCancellable *cancellable,
2905 GFileProgressCallback progress_callback,
2906 gpointer progress_callback_data,
2909 int buffer[2] = { -1, -1 };
2916 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2917 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2919 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2923 /* avoid performance impact of querying total size when it's not needed */
2924 if (progress_callback)
2928 if (fstat (fd_in, &sbuf) == 0)
2929 total_size = sbuf.st_size;
2932 if (total_size == -1)
2935 offset_in = offset_out = 0;
2942 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2945 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2956 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2959 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2962 n_read -= n_written;
2965 if (progress_callback)
2966 progress_callback (offset_in, total_size, progress_callback_data);
2969 /* Make sure we send full copied size */
2970 if (progress_callback)
2971 progress_callback (offset_in, total_size, progress_callback_data);
2973 if (!g_close (buffer[0], error))
2976 if (!g_close (buffer[1], error))
2980 if (buffer[0] != -1)
2981 (void) g_close (buffer[0], NULL);
2982 if (buffer[1] != -1)
2983 (void) g_close (buffer[1], NULL);
2991 btrfs_reflink_with_progress (GInputStream *in,
2994 GCancellable *cancellable,
2995 GFileProgressCallback progress_callback,
2996 gpointer progress_callback_data,
2999 goffset source_size;
3003 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3004 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3006 if (progress_callback)
3007 source_size = g_file_info_get_size (info);
3009 /* Btrfs clone ioctl properties:
3010 * - Works at the inode level
3011 * - Doesn't work with directories
3012 * - Always follows symlinks (source and destination)
3014 * By the time we get here, *in and *out are both regular files */
3015 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3020 g_set_error_literal (error, G_IO_ERROR,
3021 G_IO_ERROR_NOT_SUPPORTED,
3022 _("Copy (reflink/clone) between mounts is not supported"));
3023 else if (errno == EINVAL)
3024 g_set_error_literal (error, G_IO_ERROR,
3025 G_IO_ERROR_NOT_SUPPORTED,
3026 _("Copy (reflink/clone) is not supported or invalid"));
3028 /* Most probably something odd happened; retry with fallback */
3029 g_set_error_literal (error, G_IO_ERROR,
3030 G_IO_ERROR_NOT_SUPPORTED,
3031 _("Copy (reflink/clone) is not supported or didn't work"));
3032 /* We retry with fallback for all error cases because Btrfs is currently
3033 * unstable, and so we can't trust it to do clone properly.
3034 * In addition, any hard errors here would cause the same failure in the
3035 * fallback manual copy as well. */
3039 /* Make sure we send full copied size */
3040 if (progress_callback)
3041 progress_callback (source_size, source_size, progress_callback_data);
3048 file_copy_fallback (GFile *source,
3050 GFileCopyFlags flags,
3051 GCancellable *cancellable,
3052 GFileProgressCallback progress_callback,
3053 gpointer progress_callback_data,
3056 gboolean ret = FALSE;
3057 GFileInputStream *file_in = NULL;
3058 GInputStream *in = NULL;
3059 GOutputStream *out = NULL;
3060 GFileInfo *info = NULL;
3062 char *attrs_to_read;
3063 gboolean do_set_attributes = FALSE;
3065 /* need to know the file type */
3066 info = g_file_query_info (source,
3067 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3068 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3074 /* Maybe copy the symlink? */
3075 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3076 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3078 target = g_file_info_get_symlink_target (info);
3081 if (!copy_symlink (destination, flags, cancellable, target, error))
3087 /* ... else fall back on a regular file copy */
3089 /* Handle "special" files (pipes, device nodes, ...)? */
3090 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3092 /* FIXME: could try to recreate device nodes and others? */
3093 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3094 _("Can't copy special file"));
3098 /* Everything else should just fall back on a regular copy. */
3100 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3103 in = G_INPUT_STREAM (file_in);
3105 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3106 cancellable, error))
3109 if (attrs_to_read != NULL)
3111 GError *tmp_error = NULL;
3113 /* Ok, ditch the previous lightweight info (on Unix we just
3114 * called lstat()); at this point we gather all the information
3115 * we need about the source from the opened file descriptor.
3117 g_object_unref (info);
3119 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3120 cancellable, &tmp_error);
3123 /* Not all gvfs backends implement query_info_on_read(), we
3124 * can just fall back to the pathname again.
3125 * https://bugzilla.gnome.org/706254
3127 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3129 g_clear_error (&tmp_error);
3130 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3131 cancellable, error);
3135 g_free (attrs_to_read);
3136 g_propagate_error (error, tmp_error);
3140 g_free (attrs_to_read);
3144 do_set_attributes = TRUE;
3147 /* In the local file path, we pass down the source info which
3148 * includes things like unix::mode, to ensure that the target file
3149 * is not created with different permissions from the source file.
3151 * If a future API like g_file_replace_with_info() is added, switch
3152 * this code to use that.
3154 if (G_IS_LOCAL_FILE (destination))
3156 if (flags & G_FILE_COPY_OVERWRITE)
3157 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3159 flags & G_FILE_COPY_BACKUP,
3160 G_FILE_CREATE_REPLACE_DESTINATION,
3162 cancellable, error);
3164 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3166 cancellable, error);
3168 else if (flags & G_FILE_COPY_OVERWRITE)
3170 out = (GOutputStream *)g_file_replace (destination,
3172 flags & G_FILE_COPY_BACKUP,
3173 G_FILE_CREATE_REPLACE_DESTINATION,
3174 cancellable, error);
3178 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3185 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3187 GError *reflink_err = NULL;
3189 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3190 progress_callback, progress_callback_data,
3193 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3195 g_clear_error (&reflink_err);
3199 g_propagate_error (error, reflink_err);
3212 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3214 GError *splice_err = NULL;
3216 if (!splice_stream_with_progress (in, out, cancellable,
3217 progress_callback, progress_callback_data,
3220 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3222 g_clear_error (&splice_err);
3226 g_propagate_error (error, splice_err);
3239 /* A plain read/write loop */
3240 if (!copy_stream_with_progress (in, out, source, cancellable,
3241 progress_callback, progress_callback_data,
3249 /* Don't care about errors in source here */
3250 (void) g_input_stream_close (in, cancellable, NULL);
3251 g_object_unref (in);
3256 /* But write errors on close are bad! */
3257 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3259 g_object_unref (out);
3262 /* Ignore errors here. Failure to copy metadata is not a hard error */
3263 /* TODO: set these attributes /before/ we do the rename() on Unix */
3264 if (ret && do_set_attributes)
3266 g_file_set_attributes_from_info (destination,
3268 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3273 g_clear_object (&info);
3280 * @source: input #GFile
3281 * @destination: destination #GFile
3282 * @flags: set of #GFileCopyFlags
3283 * @cancellable: (allow-none): optional #GCancellable object,
3285 * @progress_callback: (allow-none) (scope call): function to callback with
3286 * progress information, or %NULL if progress information is not needed
3287 * @progress_callback_data: (closure): user data to pass to @progress_callback
3288 * @error: #GError to set on error, or %NULL
3290 * Copies the file @source to the location specified by @destination.
3291 * Can not handle recursive copies of directories.
3293 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3294 * existing @destination file is overwritten.
3296 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3297 * will be copied as symlinks, otherwise the target of the
3298 * @source symlink will be copied.
3300 * If @cancellable is not %NULL, then the operation can be cancelled by
3301 * triggering the cancellable object from another thread. If the operation
3302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3304 * If @progress_callback is not %NULL, then the operation can be monitored
3305 * by setting this to a #GFileProgressCallback function.
3306 * @progress_callback_data will be passed to this function. It is guaranteed
3307 * that this callback will be called after all data has been transferred with
3308 * the total number of bytes copied during the operation.
3310 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3311 * is returned, independent on the status of the @destination.
3313 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3314 * the error %G_IO_ERROR_EXISTS is returned.
3316 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3317 * error is returned. If trying to overwrite a directory with a directory the
3318 * %G_IO_ERROR_WOULD_MERGE error is returned.
3320 * If the source is a directory and the target does not exist, or
3321 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3322 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3324 * If you are interested in copying the #GFile object itself (not the on-disk
3325 * file), see g_file_dup().
3327 * Returns: %TRUE on success, %FALSE otherwise.
3330 g_file_copy (GFile *source,
3332 GFileCopyFlags flags,
3333 GCancellable *cancellable,
3334 GFileProgressCallback progress_callback,
3335 gpointer progress_callback_data,
3342 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3343 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3345 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3348 iface = G_FILE_GET_IFACE (destination);
3352 res = (* iface->copy) (source, destination,
3354 progress_callback, progress_callback_data,
3360 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3362 g_propagate_error (error, my_error);
3366 g_clear_error (&my_error);
3369 /* If the types are different, and the destination method failed
3370 * also try the source method
3372 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3374 iface = G_FILE_GET_IFACE (source);
3379 res = (* iface->copy) (source, destination,
3381 progress_callback, progress_callback_data,
3387 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3389 g_propagate_error (error, my_error);
3393 g_clear_error (&my_error);
3397 return file_copy_fallback (source, destination, flags, cancellable,
3398 progress_callback, progress_callback_data,
3403 * g_file_copy_async: (skip)
3404 * @source: input #GFile
3405 * @destination: destination #GFile
3406 * @flags: set of #GFileCopyFlags
3407 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3409 * @cancellable: (allow-none): optional #GCancellable object,
3411 * @progress_callback: (allow-none): function to callback with progress
3412 * information, or %NULL if progress information is not needed
3413 * @progress_callback_data: (closure): user data to pass to @progress_callback
3414 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3415 * @user_data: the data to pass to callback function
3417 * Copies the file @source to the location specified by @destination
3418 * asynchronously. For details of the behaviour, see g_file_copy().
3420 * If @progress_callback is not %NULL, then that function that will be called
3421 * just like in g_file_copy(), however the callback will run in the main loop,
3422 * not in the thread that is doing the I/O operation.
3424 * When the operation is finished, @callback will be called. You can then call
3425 * g_file_copy_finish() to get the result of the operation.
3428 g_file_copy_async (GFile *source,
3430 GFileCopyFlags flags,
3432 GCancellable *cancellable,
3433 GFileProgressCallback progress_callback,
3434 gpointer progress_callback_data,
3435 GAsyncReadyCallback callback,
3440 g_return_if_fail (G_IS_FILE (source));
3441 g_return_if_fail (G_IS_FILE (destination));
3443 iface = G_FILE_GET_IFACE (source);
3444 (* iface->copy_async) (source,
3450 progress_callback_data,
3456 * g_file_copy_finish:
3457 * @file: input #GFile
3458 * @res: a #GAsyncResult
3459 * @error: a #GError, or %NULL
3461 * Finishes copying the file started with g_file_copy_async().
3463 * Returns: a %TRUE on success, %FALSE on error.
3466 g_file_copy_finish (GFile *file,
3472 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3473 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3475 if (g_async_result_legacy_propagate_error (res, error))
3478 iface = G_FILE_GET_IFACE (file);
3479 return (* iface->copy_finish) (file, res, error);
3484 * @source: #GFile pointing to the source location
3485 * @destination: #GFile pointing to the destination location
3486 * @flags: set of #GFileCopyFlags
3487 * @cancellable: (allow-none): optional #GCancellable object,
3489 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3490 * function for updates
3491 * @progress_callback_data: (closure): gpointer to user data for
3492 * the callback function
3493 * @error: #GError for returning error conditions, or %NULL
3495 * Tries to move the file or directory @source to the location specified
3496 * by @destination. If native move operations are supported then this is
3497 * used, otherwise a copy + delete fallback is used. The native
3498 * implementation may support moving directories (for instance on moves
3499 * inside the same filesystem), but the fallback code does not.
3501 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3502 * existing @destination file is overwritten.
3504 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3505 * will be copied as symlinks, otherwise the target of the
3506 * @source symlink will be copied.
3508 * If @cancellable is not %NULL, then the operation can be cancelled by
3509 * triggering the cancellable object from another thread. If the operation
3510 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3512 * If @progress_callback is not %NULL, then the operation can be monitored
3513 * by setting this to a #GFileProgressCallback function.
3514 * @progress_callback_data will be passed to this function. It is
3515 * guaranteed that this callback will be called after all data has been
3516 * transferred with the total number of bytes copied during the operation.
3518 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3519 * error is returned, independent on the status of the @destination.
3521 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3522 * then the error %G_IO_ERROR_EXISTS is returned.
3524 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3525 * error is returned. If trying to overwrite a directory with a directory the
3526 * %G_IO_ERROR_WOULD_MERGE error is returned.
3528 * If the source is a directory and the target does not exist, or
3529 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3530 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3531 * move operation isn't available).
3533 * Returns: %TRUE on successful move, %FALSE otherwise.
3536 g_file_move (GFile *source,
3538 GFileCopyFlags flags,
3539 GCancellable *cancellable,
3540 GFileProgressCallback progress_callback,
3541 gpointer progress_callback_data,
3548 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3549 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3551 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3554 iface = G_FILE_GET_IFACE (destination);
3558 res = (* iface->move) (source, destination,
3560 progress_callback, progress_callback_data,
3566 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3568 g_propagate_error (error, my_error);
3573 /* If the types are different, and the destination method failed
3574 * also try the source method
3576 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3578 iface = G_FILE_GET_IFACE (source);
3583 res = (* iface->move) (source, destination,
3585 progress_callback, progress_callback_data,
3591 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3593 g_propagate_error (error, my_error);
3599 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3601 g_set_error_literal (error, G_IO_ERROR,
3602 G_IO_ERROR_NOT_SUPPORTED,
3603 _("Operation not supported"));
3607 flags |= G_FILE_COPY_ALL_METADATA;
3608 if (!g_file_copy (source, destination, flags, cancellable,
3609 progress_callback, progress_callback_data,
3613 return g_file_delete (source, cancellable, error);
3617 * g_file_make_directory:
3618 * @file: input #GFile
3619 * @cancellable: (allow-none): optional #GCancellable object,
3621 * @error: a #GError, or %NULL
3623 * Creates a directory. Note that this will only create a child directory
3624 * of the immediate parent directory of the path or URI given by the #GFile.
3625 * To recursively create directories, see g_file_make_directory_with_parents().
3626 * This function will fail if the parent directory does not exist, setting
3627 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3628 * creating directories, this function will fail, setting @error to
3629 * %G_IO_ERROR_NOT_SUPPORTED.
3631 * For a local #GFile the newly created directory will have the default
3632 * (current) ownership and permissions of the current process.
3634 * If @cancellable is not %NULL, then the operation can be cancelled by
3635 * triggering the cancellable object from another thread. If the operation
3636 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3638 * Returns: %TRUE on successful creation, %FALSE otherwise.
3641 g_file_make_directory (GFile *file,
3642 GCancellable *cancellable,
3647 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3649 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3652 iface = G_FILE_GET_IFACE (file);
3654 if (iface->make_directory == NULL)
3656 g_set_error_literal (error, G_IO_ERROR,
3657 G_IO_ERROR_NOT_SUPPORTED,
3658 _("Operation not supported"));
3662 return (* iface->make_directory) (file, cancellable, error);
3666 * g_file_make_directory_async:
3667 * @file: input #GFile
3668 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3670 * @cancellable: (allow-none): optional #GCancellable object,
3672 * @callback: a #GAsyncReadyCallback to call
3673 * when the request is satisfied
3674 * @user_data: the data to pass to callback function
3676 * Asynchronously creates a directory.
3678 * Virtual: make_directory_async
3682 g_file_make_directory_async (GFile *file,
3684 GCancellable *cancellable,
3685 GAsyncReadyCallback callback,
3690 g_return_if_fail (G_IS_FILE (file));
3692 iface = G_FILE_GET_IFACE (file);
3693 (* iface->make_directory_async) (file,
3701 * g_file_make_directory_finish:
3702 * @file: input #GFile
3703 * @result: a #GAsyncResult
3704 * @error: a #GError, or %NULL
3706 * Finishes an asynchronous directory creation, started with
3707 * g_file_make_directory_async().
3709 * Virtual: make_directory_finish
3710 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3714 g_file_make_directory_finish (GFile *file,
3715 GAsyncResult *result,
3720 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3721 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3723 iface = G_FILE_GET_IFACE (file);
3724 return (* iface->make_directory_finish) (file, result, error);
3728 * g_file_make_directory_with_parents:
3729 * @file: input #GFile
3730 * @cancellable: (allow-none): optional #GCancellable object,
3732 * @error: a #GError, or %NULL
3734 * Creates a directory and any parent directories that may not
3735 * exist similar to 'mkdir -p'. If the file system does not support
3736 * creating directories, this function will fail, setting @error to
3737 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3738 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3739 * the similar g_mkdir_with_parents().
3741 * For a local #GFile the newly created directories will have the default
3742 * (current) ownership and permissions of the current process.
3744 * If @cancellable is not %NULL, then the operation can be cancelled by
3745 * triggering the cancellable object from another thread. If the operation
3746 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3748 * Returns: %TRUE if all directories have been successfully created, %FALSE
3754 g_file_make_directory_with_parents (GFile *file,
3755 GCancellable *cancellable,
3758 GFile *work_file = NULL;
3759 GList *list = NULL, *l;
3760 GError *my_error = NULL;
3762 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3764 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3767 g_file_make_directory (file, cancellable, &my_error);
3768 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3771 g_propagate_error (error, my_error);
3772 return my_error == NULL;
3775 work_file = g_object_ref (file);
3777 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3781 parent_file = g_file_get_parent (work_file);
3782 if (parent_file == NULL)
3785 g_clear_error (&my_error);
3786 g_file_make_directory (parent_file, cancellable, &my_error);
3788 g_object_unref (work_file);
3789 work_file = g_object_ref (parent_file);
3791 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3792 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3794 g_object_unref (parent_file);
3797 for (l = list; my_error == NULL && l; l = l->next)
3799 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3803 g_object_unref (work_file);
3806 while (list != NULL)
3808 g_object_unref ((GFile *) list->data);
3809 list = g_list_remove (list, list->data);
3812 if (my_error != NULL)
3814 g_propagate_error (error, my_error);
3818 return g_file_make_directory (file, cancellable, error);
3822 * g_file_make_symbolic_link:
3823 * @file: a #GFile with the name of the symlink to create
3824 * @symlink_value: a string with the path for the target of the new symlink
3825 * @cancellable: (allow-none): optional #GCancellable object,
3829 * Creates a symbolic link named @file which contains the string
3832 * If @cancellable is not %NULL, then the operation can be cancelled by
3833 * triggering the cancellable object from another thread. If the operation
3834 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3836 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3839 g_file_make_symbolic_link (GFile *file,
3840 const char *symlink_value,
3841 GCancellable *cancellable,
3846 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3847 g_return_val_if_fail (symlink_value != NULL, FALSE);
3849 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3852 if (*symlink_value == '\0')
3854 g_set_error_literal (error, G_IO_ERROR,
3855 G_IO_ERROR_INVALID_ARGUMENT,
3856 _("Invalid symlink value given"));
3860 iface = G_FILE_GET_IFACE (file);
3862 if (iface->make_symbolic_link == NULL)
3864 g_set_error_literal (error, G_IO_ERROR,
3865 G_IO_ERROR_NOT_SUPPORTED,
3866 _("Operation not supported"));
3870 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3875 * @file: input #GFile
3876 * @cancellable: (allow-none): optional #GCancellable object,
3878 * @error: a #GError, or %NULL
3880 * Deletes a file. If the @file is a directory, it will only be
3881 * deleted if it is empty. This has the same semantics as g_unlink().
3883 * If @cancellable is not %NULL, then the operation can be cancelled by
3884 * triggering the cancellable object from another thread. If the operation
3885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3887 * Virtual: delete_file
3888 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3891 g_file_delete (GFile *file,
3892 GCancellable *cancellable,
3897 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3899 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3902 iface = G_FILE_GET_IFACE (file);
3904 if (iface->delete_file == NULL)
3906 g_set_error_literal (error, G_IO_ERROR,
3907 G_IO_ERROR_NOT_SUPPORTED,
3908 _("Operation not supported"));
3912 return (* iface->delete_file) (file, cancellable, error);
3916 * g_file_delete_async:
3917 * @file: input #GFile
3918 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3920 * @cancellable: (allow-none): optional #GCancellable object,
3922 * @callback: a #GAsyncReadyCallback to call
3923 * when the request is satisfied
3924 * @user_data: the data to pass to callback function
3926 * Asynchronously delete a file. If the @file is a directory, it will
3927 * only be deleted if it is empty. This has the same semantics as
3930 * Virtual: delete_file_async
3934 g_file_delete_async (GFile *file,
3936 GCancellable *cancellable,
3937 GAsyncReadyCallback callback,
3942 g_return_if_fail (G_IS_FILE (file));
3944 iface = G_FILE_GET_IFACE (file);
3945 (* iface->delete_file_async) (file,
3953 * g_file_delete_finish:
3954 * @file: input #GFile
3955 * @result: a #GAsyncResult
3956 * @error: a #GError, or %NULL
3958 * Finishes deleting a file started with g_file_delete_async().
3960 * Virtual: delete_file_finish
3961 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3965 g_file_delete_finish (GFile *file,
3966 GAsyncResult *result,
3971 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3972 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3974 if (g_async_result_legacy_propagate_error (result, error))
3977 iface = G_FILE_GET_IFACE (file);
3978 return (* iface->delete_file_finish) (file, result, error);
3983 * @file: #GFile to send to trash
3984 * @cancellable: (allow-none): optional #GCancellable object,
3986 * @error: a #GError, or %NULL
3988 * Sends @file to the "Trashcan", if possible. This is similar to
3989 * deleting it, but the user can recover it before emptying the trashcan.
3990 * Not all file systems support trashing, so this call can return the
3991 * %G_IO_ERROR_NOT_SUPPORTED error.
3993 * If @cancellable is not %NULL, then the operation can be cancelled by
3994 * triggering the cancellable object from another thread. If the operation
3995 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3998 * Returns: %TRUE on successful trash, %FALSE otherwise.
4001 g_file_trash (GFile *file,
4002 GCancellable *cancellable,
4007 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4009 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4012 iface = G_FILE_GET_IFACE (file);
4014 if (iface->trash == NULL)
4016 g_set_error_literal (error,
4017 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4018 _("Trash not supported"));
4022 return (* iface->trash) (file, cancellable, error);
4026 * g_file_trash_async:
4027 * @file: input #GFile
4028 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4030 * @cancellable: (allow-none): optional #GCancellable object,
4032 * @callback: a #GAsyncReadyCallback to call
4033 * when the request is satisfied
4034 * @user_data: the data to pass to callback function
4036 * Asynchronously sends @file to the Trash location, if possible.
4038 * Virtual: trash_async
4042 g_file_trash_async (GFile *file,
4044 GCancellable *cancellable,
4045 GAsyncReadyCallback callback,
4050 g_return_if_fail (G_IS_FILE (file));
4052 iface = G_FILE_GET_IFACE (file);
4053 (* iface->trash_async) (file,
4061 * g_file_trash_finish:
4062 * @file: input #GFile
4063 * @result: a #GAsyncResult
4064 * @error: a #GError, or %NULL
4066 * Finishes an asynchronous file trashing operation, started with
4067 * g_file_trash_async().
4069 * Virtual: trash_finish
4070 * Returns: %TRUE on successful trash, %FALSE otherwise.
4074 g_file_trash_finish (GFile *file,
4075 GAsyncResult *result,
4080 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4081 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4083 iface = G_FILE_GET_IFACE (file);
4084 return (* iface->trash_finish) (file, result, error);
4088 * g_file_set_display_name:
4089 * @file: input #GFile
4090 * @display_name: a string
4091 * @cancellable: (allow-none): optional #GCancellable object,
4093 * @error: a #GError, or %NULL
4095 * Renames @file to the specified display name.
4097 * The display name is converted from UTF-8 to the correct encoding
4098 * for the target filesystem if possible and the @file is renamed to this.
4100 * If you want to implement a rename operation in the user interface the
4101 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4102 * initial value in the rename widget, and then the result after editing
4103 * should be passed to g_file_set_display_name().
4105 * On success the resulting converted filename is returned.
4107 * If @cancellable is not %NULL, then the operation can be cancelled by
4108 * triggering the cancellable object from another thread. If the operation
4109 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4111 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4112 * or %NULL if there was an error.
4113 * Free the returned object with g_object_unref().
4116 g_file_set_display_name (GFile *file,
4117 const gchar *display_name,
4118 GCancellable *cancellable,
4123 g_return_val_if_fail (G_IS_FILE (file), NULL);
4124 g_return_val_if_fail (display_name != NULL, NULL);
4126 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4130 G_IO_ERROR_INVALID_ARGUMENT,
4131 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4135 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4138 iface = G_FILE_GET_IFACE (file);
4140 return (* iface->set_display_name) (file, display_name, cancellable, error);
4144 * g_file_set_display_name_async:
4145 * @file: input #GFile
4146 * @display_name: a string
4147 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4149 * @cancellable: (allow-none): optional #GCancellable object,
4151 * @callback: (scope async): a #GAsyncReadyCallback to call
4152 * when the request is satisfied
4153 * @user_data: (closure): the data to pass to callback function
4155 * Asynchronously sets the display name for a given #GFile.
4157 * For more details, see g_file_set_display_name() which is
4158 * the synchronous version of this call.
4160 * When the operation is finished, @callback will be called.
4161 * You can then call g_file_set_display_name_finish() to get
4162 * the result of the operation.
4165 g_file_set_display_name_async (GFile *file,
4166 const gchar *display_name,
4168 GCancellable *cancellable,
4169 GAsyncReadyCallback callback,
4174 g_return_if_fail (G_IS_FILE (file));
4175 g_return_if_fail (display_name != NULL);
4177 iface = G_FILE_GET_IFACE (file);
4178 (* iface->set_display_name_async) (file,
4187 * g_file_set_display_name_finish:
4188 * @file: input #GFile
4189 * @res: a #GAsyncResult
4190 * @error: a #GError, or %NULL
4192 * Finishes setting a display name started with
4193 * g_file_set_display_name_async().
4195 * Returns: (transfer full): a #GFile or %NULL on error.
4196 * Free the returned object with g_object_unref().
4199 g_file_set_display_name_finish (GFile *file,
4205 g_return_val_if_fail (G_IS_FILE (file), NULL);
4206 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4208 if (g_async_result_legacy_propagate_error (res, error))
4211 iface = G_FILE_GET_IFACE (file);
4212 return (* iface->set_display_name_finish) (file, res, error);
4216 * g_file_query_settable_attributes:
4217 * @file: input #GFile
4218 * @cancellable: (allow-none): optional #GCancellable object,
4220 * @error: a #GError, or %NULL
4222 * Obtain the list of settable attributes for the file.
4224 * Returns the type and full attribute name of all the attributes
4225 * that can be set on this file. This doesn't mean setting it will
4226 * always succeed though, you might get an access failure, or some
4227 * specific file may not support a specific attribute.
4229 * If @cancellable is not %NULL, then the operation can be cancelled by
4230 * triggering the cancellable object from another thread. If the operation
4231 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4233 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4234 * When you are done with it, release it with
4235 * g_file_attribute_info_list_unref()
4237 GFileAttributeInfoList *
4238 g_file_query_settable_attributes (GFile *file,
4239 GCancellable *cancellable,
4244 GFileAttributeInfoList *list;
4246 g_return_val_if_fail (G_IS_FILE (file), NULL);
4248 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4251 iface = G_FILE_GET_IFACE (file);
4253 if (iface->query_settable_attributes == NULL)
4254 return g_file_attribute_info_list_new ();
4257 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4261 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4263 list = g_file_attribute_info_list_new ();
4264 g_error_free (my_error);
4267 g_propagate_error (error, my_error);
4274 * g_file_query_writable_namespaces:
4275 * @file: input #GFile
4276 * @cancellable: (allow-none): optional #GCancellable object,
4278 * @error: a #GError, or %NULL
4280 * Obtain the list of attribute namespaces where new attributes
4281 * can be created by a user. An example of this is extended
4282 * attributes (in the "xattr" namespace).
4284 * If @cancellable is not %NULL, then the operation can be cancelled by
4285 * triggering the cancellable object from another thread. If the operation
4286 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4288 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4289 * When you are done with it, release it with
4290 * g_file_attribute_info_list_unref()
4292 GFileAttributeInfoList *
4293 g_file_query_writable_namespaces (GFile *file,
4294 GCancellable *cancellable,
4299 GFileAttributeInfoList *list;
4301 g_return_val_if_fail (G_IS_FILE (file), NULL);
4303 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4306 iface = G_FILE_GET_IFACE (file);
4308 if (iface->query_writable_namespaces == NULL)
4309 return g_file_attribute_info_list_new ();
4312 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4316 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4318 list = g_file_attribute_info_list_new ();
4319 g_error_free (my_error);
4322 g_propagate_error (error, my_error);
4329 * g_file_set_attribute:
4330 * @file: input #GFile
4331 * @attribute: a string containing the attribute's name
4332 * @type: The type of the attribute
4333 * @value_p: (allow-none): a pointer to the value (or the pointer
4334 * itself if the type is a pointer type)
4335 * @flags: a set of #GFileQueryInfoFlags
4336 * @cancellable: (allow-none): optional #GCancellable object,
4338 * @error: a #GError, or %NULL
4340 * Sets an attribute in the file with attribute name @attribute to @value.
4342 * Some attributes can be unset by setting @attribute to
4343 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4345 * If @cancellable is not %NULL, then the operation can be cancelled by
4346 * triggering the cancellable object from another thread. If the operation
4347 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4349 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4352 g_file_set_attribute (GFile *file,
4353 const gchar *attribute,
4354 GFileAttributeType type,
4356 GFileQueryInfoFlags flags,
4357 GCancellable *cancellable,
4362 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4363 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4365 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4368 iface = G_FILE_GET_IFACE (file);
4370 if (iface->set_attribute == NULL)
4372 g_set_error_literal (error, G_IO_ERROR,
4373 G_IO_ERROR_NOT_SUPPORTED,
4374 _("Operation not supported"));
4378 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4382 * g_file_set_attributes_from_info:
4383 * @file: input #GFile
4384 * @info: a #GFileInfo
4385 * @flags: #GFileQueryInfoFlags
4386 * @cancellable: (allow-none): optional #GCancellable object,
4388 * @error: a #GError, or %NULL
4390 * Tries to set all attributes in the #GFileInfo on the target
4391 * values, not stopping on the first error.
4393 * If there is any error during this operation then @error will
4394 * be set to the first error. Error on particular fields are flagged
4395 * by setting the "status" field in the attribute value to
4396 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4397 * also detect further errors.
4399 * If @cancellable is not %NULL, then the operation can be cancelled by
4400 * triggering the cancellable object from another thread. If the operation
4401 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4403 * Returns: %FALSE if there was any error, %TRUE otherwise.
4406 g_file_set_attributes_from_info (GFile *file,
4408 GFileQueryInfoFlags flags,
4409 GCancellable *cancellable,
4414 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4415 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4417 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4420 g_file_info_clear_status (info);
4422 iface = G_FILE_GET_IFACE (file);
4424 return (* iface->set_attributes_from_info) (file,
4432 g_file_real_set_attributes_from_info (GFile *file,
4434 GFileQueryInfoFlags flags,
4435 GCancellable *cancellable,
4441 GFileAttributeValue *value;
4445 attributes = g_file_info_list_attributes (info, NULL);
4447 for (i = 0; attributes[i] != NULL; i++)
4449 value = _g_file_info_get_attribute_value (info, attributes[i]);
4451 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4454 if (!g_file_set_attribute (file, attributes[i],
4455 value->type, _g_file_attribute_value_peek_as_pointer (value),
4456 flags, cancellable, error))
4458 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4460 /* Don't set error multiple times */
4464 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4467 g_strfreev (attributes);
4473 * g_file_set_attributes_async:
4474 * @file: input #GFile
4475 * @info: a #GFileInfo
4476 * @flags: a #GFileQueryInfoFlags
4477 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4479 * @cancellable: (allow-none): optional #GCancellable object,
4481 * @callback: (scope async): a #GAsyncReadyCallback
4482 * @user_data: (closure): a #gpointer
4484 * Asynchronously sets the attributes of @file with @info.
4486 * For more details, see g_file_set_attributes_from_info(),
4487 * which is the synchronous version of this call.
4489 * When the operation is finished, @callback will be called.
4490 * You can then call g_file_set_attributes_finish() to get
4491 * the result of the operation.
4494 g_file_set_attributes_async (GFile *file,
4496 GFileQueryInfoFlags flags,
4498 GCancellable *cancellable,
4499 GAsyncReadyCallback callback,
4504 g_return_if_fail (G_IS_FILE (file));
4505 g_return_if_fail (G_IS_FILE_INFO (info));
4507 iface = G_FILE_GET_IFACE (file);
4508 (* iface->set_attributes_async) (file,
4518 * g_file_set_attributes_finish:
4519 * @file: input #GFile
4520 * @result: a #GAsyncResult
4521 * @info: (out) (transfer full): a #GFileInfo
4522 * @error: a #GError, or %NULL
4524 * Finishes setting an attribute started in g_file_set_attributes_async().
4526 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4529 g_file_set_attributes_finish (GFile *file,
4530 GAsyncResult *result,
4536 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4537 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4539 /* No standard handling of errors here, as we must set info even
4542 iface = G_FILE_GET_IFACE (file);
4543 return (* iface->set_attributes_finish) (file, result, info, error);
4547 * g_file_set_attribute_string:
4548 * @file: input #GFile
4549 * @attribute: a string containing the attribute's name
4550 * @value: a string containing the attribute's value
4551 * @flags: #GFileQueryInfoFlags
4552 * @cancellable: (allow-none): optional #GCancellable object,
4554 * @error: a #GError, or %NULL
4556 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4557 * If @attribute is of a different type, this operation will fail.
4559 * If @cancellable is not %NULL, then the operation can be cancelled by
4560 * triggering the cancellable object from another thread. If the operation
4561 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4563 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4566 g_file_set_attribute_string (GFile *file,
4567 const char *attribute,
4569 GFileQueryInfoFlags flags,
4570 GCancellable *cancellable,
4573 return g_file_set_attribute (file, attribute,
4574 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4575 flags, cancellable, error);
4579 * g_file_set_attribute_byte_string:
4580 * @file: input #GFile
4581 * @attribute: a string containing the attribute's name
4582 * @value: a string containing the attribute's new value
4583 * @flags: a #GFileQueryInfoFlags
4584 * @cancellable: (allow-none): optional #GCancellable object,
4586 * @error: a #GError, or %NULL
4588 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4589 * If @attribute is of a different type, this operation will fail,
4592 * If @cancellable is not %NULL, then the operation can be cancelled by
4593 * triggering the cancellable object from another thread. If the operation
4594 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4596 * Returns: %TRUE if the @attribute was successfully set to @value
4597 * in the @file, %FALSE otherwise.
4600 g_file_set_attribute_byte_string (GFile *file,
4601 const gchar *attribute,
4603 GFileQueryInfoFlags flags,
4604 GCancellable *cancellable,
4607 return g_file_set_attribute (file, attribute,
4608 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4609 flags, cancellable, error);
4613 * g_file_set_attribute_uint32:
4614 * @file: input #GFile
4615 * @attribute: a string containing the attribute's name
4616 * @value: a #guint32 containing the attribute's new value
4617 * @flags: a #GFileQueryInfoFlags
4618 * @cancellable: (allow-none): optional #GCancellable object,
4620 * @error: a #GError, or %NULL
4622 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4623 * If @attribute is of a different type, this operation will fail.
4625 * If @cancellable is not %NULL, then the operation can be cancelled by
4626 * triggering the cancellable object from another thread. If the operation
4627 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4629 * Returns: %TRUE if the @attribute was successfully set to @value
4630 * in the @file, %FALSE otherwise.
4633 g_file_set_attribute_uint32 (GFile *file,
4634 const gchar *attribute,
4636 GFileQueryInfoFlags flags,
4637 GCancellable *cancellable,
4640 return g_file_set_attribute (file, attribute,
4641 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4642 flags, cancellable, error);
4646 * g_file_set_attribute_int32:
4647 * @file: input #GFile
4648 * @attribute: a string containing the attribute's name
4649 * @value: a #gint32 containing the attribute's new value
4650 * @flags: a #GFileQueryInfoFlags
4651 * @cancellable: (allow-none): optional #GCancellable object,
4653 * @error: a #GError, or %NULL
4655 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4656 * If @attribute is of a different type, this operation will fail.
4658 * If @cancellable is not %NULL, then the operation can be cancelled by
4659 * triggering the cancellable object from another thread. If the operation
4660 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4662 * Returns: %TRUE if the @attribute was successfully set to @value
4663 * in the @file, %FALSE otherwise.
4666 g_file_set_attribute_int32 (GFile *file,
4667 const gchar *attribute,
4669 GFileQueryInfoFlags flags,
4670 GCancellable *cancellable,
4673 return g_file_set_attribute (file, attribute,
4674 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4675 flags, cancellable, error);
4679 * g_file_set_attribute_uint64:
4680 * @file: input #GFile
4681 * @attribute: a string containing the attribute's name
4682 * @value: a #guint64 containing the attribute's new value
4683 * @flags: a #GFileQueryInfoFlags
4684 * @cancellable: (allow-none): optional #GCancellable object,
4686 * @error: a #GError, or %NULL
4688 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4689 * If @attribute is of a different type, this operation will fail.
4691 * If @cancellable is not %NULL, then the operation can be cancelled by
4692 * triggering the cancellable object from another thread. If the operation
4693 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4695 * Returns: %TRUE if the @attribute was successfully set to @value
4696 * in the @file, %FALSE otherwise.
4699 g_file_set_attribute_uint64 (GFile *file,
4700 const gchar *attribute,
4702 GFileQueryInfoFlags flags,
4703 GCancellable *cancellable,
4706 return g_file_set_attribute (file, attribute,
4707 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4708 flags, cancellable, error);
4712 * g_file_set_attribute_int64:
4713 * @file: input #GFile
4714 * @attribute: a string containing the attribute's name
4715 * @value: a #guint64 containing the attribute's new value
4716 * @flags: a #GFileQueryInfoFlags
4717 * @cancellable: (allow-none): optional #GCancellable object,
4719 * @error: a #GError, or %NULL
4721 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4722 * If @attribute is of a different type, this operation will fail.
4724 * If @cancellable is not %NULL, then the operation can be cancelled by
4725 * triggering the cancellable object from another thread. If the operation
4726 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4728 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4731 g_file_set_attribute_int64 (GFile *file,
4732 const gchar *attribute,
4734 GFileQueryInfoFlags flags,
4735 GCancellable *cancellable,
4738 return g_file_set_attribute (file, attribute,
4739 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4740 flags, cancellable, error);
4744 * g_file_mount_mountable:
4745 * @file: input #GFile
4746 * @flags: flags affecting the operation
4747 * @mount_operation: (allow-none): a #GMountOperation,
4748 * or %NULL to avoid user interaction
4749 * @cancellable: (allow-none): optional #GCancellable object,
4751 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4752 * when the request is satisfied, or %NULL
4753 * @user_data: (closure): the data to pass to callback function
4755 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4756 * Using @mount_operation, you can request callbacks when, for instance,
4757 * passwords are needed during authentication.
4759 * If @cancellable is not %NULL, then the operation can be cancelled by
4760 * triggering the cancellable object from another thread. If the operation
4761 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4763 * When the operation is finished, @callback will be called.
4764 * You can then call g_file_mount_mountable_finish() to get
4765 * the result of the operation.
4768 g_file_mount_mountable (GFile *file,
4769 GMountMountFlags flags,
4770 GMountOperation *mount_operation,
4771 GCancellable *cancellable,
4772 GAsyncReadyCallback callback,
4777 g_return_if_fail (G_IS_FILE (file));
4779 iface = G_FILE_GET_IFACE (file);
4781 if (iface->mount_mountable == NULL)
4783 g_task_report_new_error (file, callback, user_data,
4784 g_file_mount_mountable,
4785 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4786 _("Operation not supported"));
4790 (* iface->mount_mountable) (file,
4799 * g_file_mount_mountable_finish:
4800 * @file: input #GFile
4801 * @result: a #GAsyncResult
4802 * @error: a #GError, or %NULL
4804 * Finishes a mount operation. See g_file_mount_mountable() for details.
4806 * Finish an asynchronous mount operation that was started
4807 * with g_file_mount_mountable().
4809 * Returns: (transfer full): a #GFile or %NULL on error.
4810 * Free the returned object with g_object_unref().
4813 g_file_mount_mountable_finish (GFile *file,
4814 GAsyncResult *result,
4819 g_return_val_if_fail (G_IS_FILE (file), NULL);
4820 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4822 if (g_async_result_legacy_propagate_error (result, error))
4824 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4825 return g_task_propagate_pointer (G_TASK (result), error);
4827 iface = G_FILE_GET_IFACE (file);
4828 return (* iface->mount_mountable_finish) (file, result, error);
4832 * g_file_unmount_mountable:
4833 * @file: input #GFile
4834 * @flags: flags affecting the operation
4835 * @cancellable: (allow-none): optional #GCancellable object,
4837 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4838 * when the request is satisfied, or %NULL
4839 * @user_data: (closure): the data to pass to callback function
4841 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4843 * If @cancellable is not %NULL, then the operation can be cancelled by
4844 * triggering the cancellable object from another thread. If the operation
4845 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4847 * When the operation is finished, @callback will be called.
4848 * You can then call g_file_unmount_mountable_finish() to get
4849 * the result of the operation.
4851 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4854 g_file_unmount_mountable (GFile *file,
4855 GMountUnmountFlags flags,
4856 GCancellable *cancellable,
4857 GAsyncReadyCallback callback,
4862 g_return_if_fail (G_IS_FILE (file));
4864 iface = G_FILE_GET_IFACE (file);
4866 if (iface->unmount_mountable == NULL)
4868 g_task_report_new_error (file, callback, user_data,
4869 g_file_unmount_mountable_with_operation,
4870 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4871 _("Operation not supported"));
4875 (* iface->unmount_mountable) (file,
4883 * g_file_unmount_mountable_finish:
4884 * @file: input #GFile
4885 * @result: a #GAsyncResult
4886 * @error: a #GError, or %NULL
4888 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4890 * Finish an asynchronous unmount operation that was started
4891 * with g_file_unmount_mountable().
4893 * Returns: %TRUE if the operation finished successfully.
4896 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4900 g_file_unmount_mountable_finish (GFile *file,
4901 GAsyncResult *result,
4906 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4907 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4909 if (g_async_result_legacy_propagate_error (result, error))
4911 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4912 return g_task_propagate_boolean (G_TASK (result), error);
4914 iface = G_FILE_GET_IFACE (file);
4915 return (* iface->unmount_mountable_finish) (file, result, error);
4919 * g_file_unmount_mountable_with_operation:
4920 * @file: input #GFile
4921 * @flags: flags affecting the operation
4922 * @mount_operation: (allow-none): a #GMountOperation,
4923 * or %NULL to avoid user interaction
4924 * @cancellable: (allow-none): optional #GCancellable object,
4926 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4927 * when the request is satisfied, or %NULL
4928 * @user_data: (closure): the data to pass to callback function
4930 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4932 * If @cancellable is not %NULL, then the operation can be cancelled by
4933 * triggering the cancellable object from another thread. If the operation
4934 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4936 * When the operation is finished, @callback will be called.
4937 * You can then call g_file_unmount_mountable_finish() to get
4938 * the result of the operation.
4943 g_file_unmount_mountable_with_operation (GFile *file,
4944 GMountUnmountFlags flags,
4945 GMountOperation *mount_operation,
4946 GCancellable *cancellable,
4947 GAsyncReadyCallback callback,
4952 g_return_if_fail (G_IS_FILE (file));
4954 iface = G_FILE_GET_IFACE (file);
4956 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4958 g_task_report_new_error (file, callback, user_data,
4959 g_file_unmount_mountable_with_operation,
4960 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4961 _("Operation not supported"));
4965 if (iface->unmount_mountable_with_operation != NULL)
4966 (* iface->unmount_mountable_with_operation) (file,
4973 (* iface->unmount_mountable) (file,
4981 * g_file_unmount_mountable_with_operation_finish:
4982 * @file: input #GFile
4983 * @result: a #GAsyncResult
4984 * @error: a #GError, or %NULL
4986 * Finishes an unmount operation,
4987 * see g_file_unmount_mountable_with_operation() for details.
4989 * Finish an asynchronous unmount operation that was started
4990 * with g_file_unmount_mountable_with_operation().
4992 * Returns: %TRUE if the operation finished successfully.
4998 g_file_unmount_mountable_with_operation_finish (GFile *file,
4999 GAsyncResult *result,
5004 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5005 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5007 if (g_async_result_legacy_propagate_error (result, error))
5009 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5010 return g_task_propagate_boolean (G_TASK (result), error);
5012 iface = G_FILE_GET_IFACE (file);
5013 if (iface->unmount_mountable_with_operation_finish != NULL)
5014 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5016 return (* iface->unmount_mountable_finish) (file, result, error);
5020 * g_file_eject_mountable:
5021 * @file: input #GFile
5022 * @flags: flags affecting the operation
5023 * @cancellable: (allow-none): optional #GCancellable object,
5025 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5026 * when the request is satisfied, or %NULL
5027 * @user_data: (closure): the data to pass to callback function
5029 * Starts an asynchronous eject on a mountable.
5030 * When this operation has completed, @callback will be called with
5031 * @user_user data, and the operation can be finalized with
5032 * g_file_eject_mountable_finish().
5034 * If @cancellable is not %NULL, then the operation can be cancelled by
5035 * triggering the cancellable object from another thread. If the operation
5036 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5038 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5041 g_file_eject_mountable (GFile *file,
5042 GMountUnmountFlags flags,
5043 GCancellable *cancellable,
5044 GAsyncReadyCallback callback,
5049 g_return_if_fail (G_IS_FILE (file));
5051 iface = G_FILE_GET_IFACE (file);
5053 if (iface->eject_mountable == NULL)
5055 g_task_report_new_error (file, callback, user_data,
5056 g_file_eject_mountable_with_operation,
5057 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5058 _("Operation not supported"));
5062 (* iface->eject_mountable) (file,
5070 * g_file_eject_mountable_finish:
5071 * @file: input #GFile
5072 * @result: a #GAsyncResult
5073 * @error: a #GError, or %NULL
5075 * Finishes an asynchronous eject operation started by
5076 * g_file_eject_mountable().
5078 * Returns: %TRUE if the @file was ejected successfully.
5081 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5085 g_file_eject_mountable_finish (GFile *file,
5086 GAsyncResult *result,
5091 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5092 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5094 if (g_async_result_legacy_propagate_error (result, error))
5096 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5097 return g_task_propagate_boolean (G_TASK (result), error);
5099 iface = G_FILE_GET_IFACE (file);
5100 return (* iface->eject_mountable_finish) (file, result, error);
5104 * g_file_eject_mountable_with_operation:
5105 * @file: input #GFile
5106 * @flags: flags affecting the operation
5107 * @mount_operation: (allow-none): a #GMountOperation,
5108 * or %NULL to avoid user interaction
5109 * @cancellable: (allow-none): optional #GCancellable object,
5111 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5112 * when the request is satisfied, or %NULL
5113 * @user_data: (closure): the data to pass to callback function
5115 * Starts an asynchronous eject on a mountable.
5116 * When this operation has completed, @callback will be called with
5117 * @user_user data, and the operation can be finalized with
5118 * g_file_eject_mountable_with_operation_finish().
5120 * If @cancellable is not %NULL, then the operation can be cancelled by
5121 * triggering the cancellable object from another thread. If the operation
5122 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5127 g_file_eject_mountable_with_operation (GFile *file,
5128 GMountUnmountFlags flags,
5129 GMountOperation *mount_operation,
5130 GCancellable *cancellable,
5131 GAsyncReadyCallback callback,
5136 g_return_if_fail (G_IS_FILE (file));
5138 iface = G_FILE_GET_IFACE (file);
5140 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5142 g_task_report_new_error (file, callback, user_data,
5143 g_file_eject_mountable_with_operation,
5144 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5145 _("Operation not supported"));
5149 if (iface->eject_mountable_with_operation != NULL)
5150 (* iface->eject_mountable_with_operation) (file,
5157 (* iface->eject_mountable) (file,
5165 * g_file_eject_mountable_with_operation_finish:
5166 * @file: input #GFile
5167 * @result: a #GAsyncResult
5168 * @error: a #GError, or %NULL
5170 * Finishes an asynchronous eject operation started by
5171 * g_file_eject_mountable_with_operation().
5173 * Returns: %TRUE if the @file was ejected successfully.
5179 g_file_eject_mountable_with_operation_finish (GFile *file,
5180 GAsyncResult *result,
5185 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5186 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5188 if (g_async_result_legacy_propagate_error (result, error))
5190 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5191 return g_task_propagate_boolean (G_TASK (result), error);
5193 iface = G_FILE_GET_IFACE (file);
5194 if (iface->eject_mountable_with_operation_finish != NULL)
5195 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5197 return (* iface->eject_mountable_finish) (file, result, error);
5201 * g_file_monitor_directory:
5202 * @file: input #GFile
5203 * @flags: a set of #GFileMonitorFlags
5204 * @cancellable: (allow-none): optional #GCancellable object,
5206 * @error: a #GError, or %NULL
5208 * Obtains a directory monitor for the given file.
5209 * This may fail if directory monitoring is not supported.
5211 * If @cancellable is not %NULL, then the operation can be cancelled by
5212 * triggering the cancellable object from another thread. If the operation
5213 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5215 * It does not make sense for @flags to contain
5216 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5217 * directories. It is not possible to monitor all the files in a
5218 * directory for changes made via hard links; if you want to do this then
5219 * you must register individual watches with g_file_monitor().
5221 * Virtual: monitor_dir
5222 * Returns: (transfer full): a #GFileMonitor for the given @file,
5223 * or %NULL on error.
5224 * Free the returned object with g_object_unref().
5227 g_file_monitor_directory (GFile *file,
5228 GFileMonitorFlags flags,
5229 GCancellable *cancellable,
5234 g_return_val_if_fail (G_IS_FILE (file), NULL);
5235 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5237 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5240 iface = G_FILE_GET_IFACE (file);
5242 if (iface->monitor_dir == NULL)
5244 g_set_error_literal (error, G_IO_ERROR,
5245 G_IO_ERROR_NOT_SUPPORTED,
5246 _("Operation not supported"));
5250 return (* iface->monitor_dir) (file, flags, cancellable, error);
5254 * g_file_monitor_file:
5255 * @file: input #GFile
5256 * @flags: a set of #GFileMonitorFlags
5257 * @cancellable: (allow-none): optional #GCancellable object,
5259 * @error: a #GError, or %NULL
5261 * Obtains a file monitor for the given file. If no file notification
5262 * mechanism exists, then regular polling of the file is used.
5264 * If @cancellable is not %NULL, then the operation can be cancelled by
5265 * triggering the cancellable object from another thread. If the operation
5266 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5268 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5269 * will also attempt to report changes made to the file via another
5270 * filename (ie, a hard link). Without this flag, you can only rely on
5271 * changes made through the filename contained in @file to be
5272 * reported. Using this flag may result in an increase in resource
5273 * usage, and may not have any effect depending on the #GFileMonitor
5274 * backend and/or filesystem type.
5276 * Returns: (transfer full): a #GFileMonitor for the given @file,
5277 * or %NULL on error.
5278 * Free the returned object with g_object_unref().
5281 g_file_monitor_file (GFile *file,
5282 GFileMonitorFlags flags,
5283 GCancellable *cancellable,
5287 GFileMonitor *monitor;
5289 g_return_val_if_fail (G_IS_FILE (file), NULL);
5291 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5294 iface = G_FILE_GET_IFACE (file);
5298 if (iface->monitor_file)
5299 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5301 /* Fallback to polling */
5302 if (monitor == NULL)
5303 monitor = _g_poll_file_monitor_new (file);
5310 * @file: input #GFile
5311 * @flags: a set of #GFileMonitorFlags
5312 * @cancellable: (allow-none): optional #GCancellable object,
5314 * @error: a #GError, or %NULL
5316 * Obtains a file or directory monitor for the given file,
5317 * depending on the type of the file.
5319 * If @cancellable is not %NULL, then the operation can be cancelled by
5320 * triggering the cancellable object from another thread. If the operation
5321 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5323 * Returns: (transfer full): a #GFileMonitor for the given @file,
5324 * or %NULL on error.
5325 * Free the returned object with g_object_unref().
5330 g_file_monitor (GFile *file,
5331 GFileMonitorFlags flags,
5332 GCancellable *cancellable,
5335 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5336 return g_file_monitor_directory (file,
5337 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5338 cancellable, error);
5340 return g_file_monitor_file (file, flags, cancellable, error);
5343 /********************************************
5344 * Default implementation of async ops *
5345 ********************************************/
5349 GFileQueryInfoFlags flags;
5350 } QueryInfoAsyncData;
5353 query_info_data_free (QueryInfoAsyncData *data)
5355 g_free (data->attributes);
5360 query_info_async_thread (GTask *task,
5363 GCancellable *cancellable)
5365 QueryInfoAsyncData *data = task_data;
5367 GError *error = NULL;
5369 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5371 g_task_return_pointer (task, info, g_object_unref);
5373 g_task_return_error (task, error);
5377 g_file_real_query_info_async (GFile *file,
5378 const char *attributes,
5379 GFileQueryInfoFlags flags,
5381 GCancellable *cancellable,
5382 GAsyncReadyCallback callback,
5386 QueryInfoAsyncData *data;
5388 data = g_new0 (QueryInfoAsyncData, 1);
5389 data->attributes = g_strdup (attributes);
5390 data->flags = flags;
5392 task = g_task_new (file, cancellable, callback, user_data);
5393 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5394 g_task_set_priority (task, io_priority);
5395 g_task_run_in_thread (task, query_info_async_thread);
5396 g_object_unref (task);
5400 g_file_real_query_info_finish (GFile *file,
5404 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5406 return g_task_propagate_pointer (G_TASK (res), error);
5410 query_filesystem_info_async_thread (GTask *task,
5413 GCancellable *cancellable)
5415 const char *attributes = task_data;
5417 GError *error = NULL;
5419 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5421 g_task_return_pointer (task, info, g_object_unref);
5423 g_task_return_error (task, error);
5427 g_file_real_query_filesystem_info_async (GFile *file,
5428 const char *attributes,
5430 GCancellable *cancellable,
5431 GAsyncReadyCallback callback,
5436 task = g_task_new (file, cancellable, callback, user_data);
5437 g_task_set_task_data (task, g_strdup (attributes), g_free);
5438 g_task_set_priority (task, io_priority);
5439 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5440 g_object_unref (task);
5444 g_file_real_query_filesystem_info_finish (GFile *file,
5448 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5450 return g_task_propagate_pointer (G_TASK (res), error);
5454 enumerate_children_async_thread (GTask *task,
5457 GCancellable *cancellable)
5459 QueryInfoAsyncData *data = task_data;
5460 GFileEnumerator *enumerator;
5461 GError *error = NULL;
5463 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5465 g_task_return_error (task, error);
5467 g_task_return_pointer (task, enumerator, g_object_unref);
5471 g_file_real_enumerate_children_async (GFile *file,
5472 const char *attributes,
5473 GFileQueryInfoFlags flags,
5475 GCancellable *cancellable,
5476 GAsyncReadyCallback callback,
5480 QueryInfoAsyncData *data;
5482 data = g_new0 (QueryInfoAsyncData, 1);
5483 data->attributes = g_strdup (attributes);
5484 data->flags = flags;
5486 task = g_task_new (file, cancellable, callback, user_data);
5487 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5488 g_task_set_priority (task, io_priority);
5489 g_task_run_in_thread (task, enumerate_children_async_thread);
5490 g_object_unref (task);
5493 static GFileEnumerator *
5494 g_file_real_enumerate_children_finish (GFile *file,
5498 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5500 return g_task_propagate_pointer (G_TASK (res), error);
5504 open_read_async_thread (GTask *task,
5507 GCancellable *cancellable)
5509 GFileInputStream *stream;
5510 GError *error = NULL;
5512 stream = g_file_read (G_FILE (object), cancellable, &error);
5514 g_task_return_pointer (task, stream, g_object_unref);
5516 g_task_return_error (task, error);
5520 g_file_real_read_async (GFile *file,
5522 GCancellable *cancellable,
5523 GAsyncReadyCallback callback,
5528 task = g_task_new (file, cancellable, callback, user_data);
5529 g_task_set_priority (task, io_priority);
5530 g_task_run_in_thread (task, open_read_async_thread);
5531 g_object_unref (task);
5534 static GFileInputStream *
5535 g_file_real_read_finish (GFile *file,
5539 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5541 return g_task_propagate_pointer (G_TASK (res), error);
5545 append_to_async_thread (GTask *task,
5546 gpointer source_object,
5548 GCancellable *cancellable)
5550 GFileCreateFlags *data = task_data;
5551 GFileOutputStream *stream;
5552 GError *error = NULL;
5554 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5556 g_task_return_pointer (task, stream, g_object_unref);
5558 g_task_return_error (task, error);
5562 g_file_real_append_to_async (GFile *file,
5563 GFileCreateFlags flags,
5565 GCancellable *cancellable,
5566 GAsyncReadyCallback callback,
5569 GFileCreateFlags *data;
5572 data = g_new0 (GFileCreateFlags, 1);
5575 task = g_task_new (file, cancellable, callback, user_data);
5576 g_task_set_task_data (task, data, g_free);
5577 g_task_set_priority (task, io_priority);
5579 g_task_run_in_thread (task, append_to_async_thread);
5580 g_object_unref (task);
5583 static GFileOutputStream *
5584 g_file_real_append_to_finish (GFile *file,
5588 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5590 return g_task_propagate_pointer (G_TASK (res), error);
5594 create_async_thread (GTask *task,
5595 gpointer source_object,
5597 GCancellable *cancellable)
5599 GFileCreateFlags *data = task_data;
5600 GFileOutputStream *stream;
5601 GError *error = NULL;
5603 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5605 g_task_return_pointer (task, stream, g_object_unref);
5607 g_task_return_error (task, error);
5611 g_file_real_create_async (GFile *file,
5612 GFileCreateFlags flags,
5614 GCancellable *cancellable,
5615 GAsyncReadyCallback callback,
5618 GFileCreateFlags *data;
5621 data = g_new0 (GFileCreateFlags, 1);
5624 task = g_task_new (file, cancellable, callback, user_data);
5625 g_task_set_task_data (task, data, g_free);
5626 g_task_set_priority (task, io_priority);
5628 g_task_run_in_thread (task, create_async_thread);
5629 g_object_unref (task);
5632 static GFileOutputStream *
5633 g_file_real_create_finish (GFile *file,
5637 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5639 return g_task_propagate_pointer (G_TASK (res), error);
5643 GFileOutputStream *stream;
5645 gboolean make_backup;
5646 GFileCreateFlags flags;
5650 replace_async_data_free (ReplaceAsyncData *data)
5653 g_object_unref (data->stream);
5654 g_free (data->etag);
5659 replace_async_thread (GTask *task,
5660 gpointer source_object,
5662 GCancellable *cancellable)
5664 GFileOutputStream *stream;
5665 ReplaceAsyncData *data = task_data;
5666 GError *error = NULL;
5668 stream = g_file_replace (G_FILE (source_object),
5676 g_task_return_pointer (task, stream, g_object_unref);
5678 g_task_return_error (task, error);
5682 g_file_real_replace_async (GFile *file,
5684 gboolean make_backup,
5685 GFileCreateFlags flags,
5687 GCancellable *cancellable,
5688 GAsyncReadyCallback callback,
5692 ReplaceAsyncData *data;
5694 data = g_new0 (ReplaceAsyncData, 1);
5695 data->etag = g_strdup (etag);
5696 data->make_backup = make_backup;
5697 data->flags = flags;
5699 task = g_task_new (file, cancellable, callback, user_data);
5700 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5701 g_task_set_priority (task, io_priority);
5703 g_task_run_in_thread (task, replace_async_thread);
5704 g_object_unref (task);
5707 static GFileOutputStream *
5708 g_file_real_replace_finish (GFile *file,
5712 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5714 return g_task_propagate_pointer (G_TASK (res), error);
5718 delete_async_thread (GTask *task,
5721 GCancellable *cancellable)
5723 GError *error = NULL;
5725 if (g_file_delete (G_FILE (object), cancellable, &error))
5726 g_task_return_boolean (task, TRUE);
5728 g_task_return_error (task, error);
5732 g_file_real_delete_async (GFile *file,
5734 GCancellable *cancellable,
5735 GAsyncReadyCallback callback,
5740 task = g_task_new (file, cancellable, callback, user_data);
5741 g_task_set_priority (task, io_priority);
5742 g_task_run_in_thread (task, delete_async_thread);
5743 g_object_unref (task);
5747 g_file_real_delete_finish (GFile *file,
5751 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5753 return g_task_propagate_boolean (G_TASK (res), error);
5757 trash_async_thread (GTask *task,
5760 GCancellable *cancellable)
5762 GError *error = NULL;
5764 if (g_file_trash (G_FILE (object), cancellable, &error))
5765 g_task_return_boolean (task, TRUE);
5767 g_task_return_error (task, error);
5771 g_file_real_trash_async (GFile *file,
5773 GCancellable *cancellable,
5774 GAsyncReadyCallback callback,
5779 task = g_task_new (file, cancellable, callback, user_data);
5780 g_task_set_priority (task, io_priority);
5781 g_task_run_in_thread (task, trash_async_thread);
5782 g_object_unref (task);
5786 g_file_real_trash_finish (GFile *file,
5790 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5792 return g_task_propagate_boolean (G_TASK (res), error);
5796 make_directory_async_thread (GTask *task,
5799 GCancellable *cancellable)
5801 GError *error = NULL;
5803 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5804 g_task_return_boolean (task, TRUE);
5806 g_task_return_error (task, error);
5810 g_file_real_make_directory_async (GFile *file,
5812 GCancellable *cancellable,
5813 GAsyncReadyCallback callback,
5818 task = g_task_new (file, cancellable, callback, user_data);
5819 g_task_set_priority (task, io_priority);
5820 g_task_run_in_thread (task, make_directory_async_thread);
5821 g_object_unref (task);
5825 g_file_real_make_directory_finish (GFile *file,
5829 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5831 return g_task_propagate_boolean (G_TASK (res), error);
5835 open_readwrite_async_thread (GTask *task,
5838 GCancellable *cancellable)
5840 GFileIOStream *stream;
5841 GError *error = NULL;
5843 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5846 g_task_return_error (task, error);
5848 g_task_return_pointer (task, stream, g_object_unref);
5852 g_file_real_open_readwrite_async (GFile *file,
5854 GCancellable *cancellable,
5855 GAsyncReadyCallback callback,
5860 task = g_task_new (file, cancellable, callback, user_data);
5861 g_task_set_priority (task, io_priority);
5863 g_task_run_in_thread (task, open_readwrite_async_thread);
5864 g_object_unref (task);
5867 static GFileIOStream *
5868 g_file_real_open_readwrite_finish (GFile *file,
5872 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5874 return g_task_propagate_pointer (G_TASK (res), error);
5878 create_readwrite_async_thread (GTask *task,
5881 GCancellable *cancellable)
5883 GFileCreateFlags *data = task_data;
5884 GFileIOStream *stream;
5885 GError *error = NULL;
5887 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5890 g_task_return_error (task, error);
5892 g_task_return_pointer (task, stream, g_object_unref);
5896 g_file_real_create_readwrite_async (GFile *file,
5897 GFileCreateFlags flags,
5899 GCancellable *cancellable,
5900 GAsyncReadyCallback callback,
5903 GFileCreateFlags *data;
5906 data = g_new0 (GFileCreateFlags, 1);
5909 task = g_task_new (file, cancellable, callback, user_data);
5910 g_task_set_task_data (task, data, g_free);
5911 g_task_set_priority (task, io_priority);
5913 g_task_run_in_thread (task, create_readwrite_async_thread);
5914 g_object_unref (task);
5917 static GFileIOStream *
5918 g_file_real_create_readwrite_finish (GFile *file,
5922 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5924 return g_task_propagate_pointer (G_TASK (res), error);
5929 gboolean make_backup;
5930 GFileCreateFlags flags;
5931 } ReplaceRWAsyncData;
5934 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5936 g_free (data->etag);
5941 replace_readwrite_async_thread (GTask *task,
5944 GCancellable *cancellable)
5946 GFileIOStream *stream;
5947 GError *error = NULL;
5948 ReplaceRWAsyncData *data = task_data;
5950 stream = g_file_replace_readwrite (G_FILE (object),
5958 g_task_return_error (task, error);
5960 g_task_return_pointer (task, stream, g_object_unref);
5964 g_file_real_replace_readwrite_async (GFile *file,
5966 gboolean make_backup,
5967 GFileCreateFlags flags,
5969 GCancellable *cancellable,
5970 GAsyncReadyCallback callback,
5974 ReplaceRWAsyncData *data;
5976 data = g_new0 (ReplaceRWAsyncData, 1);
5977 data->etag = g_strdup (etag);
5978 data->make_backup = make_backup;
5979 data->flags = flags;
5981 task = g_task_new (file, cancellable, callback, user_data);
5982 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5983 g_task_set_priority (task, io_priority);
5985 g_task_run_in_thread (task, replace_readwrite_async_thread);
5986 g_object_unref (task);
5989 static GFileIOStream *
5990 g_file_real_replace_readwrite_finish (GFile *file,
5994 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5996 return g_task_propagate_pointer (G_TASK (res), error);
6000 set_display_name_async_thread (GTask *task,
6003 GCancellable *cancellable)
6005 GError *error = NULL;
6006 char *name = task_data;
6009 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6012 g_task_return_error (task, error);
6014 g_task_return_pointer (task, file, g_object_unref);
6018 g_file_real_set_display_name_async (GFile *file,
6019 const char *display_name,
6021 GCancellable *cancellable,
6022 GAsyncReadyCallback callback,
6027 task = g_task_new (file, cancellable, callback, user_data);
6028 g_task_set_task_data (task, g_strdup (display_name), g_free);
6029 g_task_set_priority (task, io_priority);
6031 g_task_run_in_thread (task, set_display_name_async_thread);
6032 g_object_unref (task);
6036 g_file_real_set_display_name_finish (GFile *file,
6040 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6042 return g_task_propagate_pointer (G_TASK (res), error);
6046 GFileQueryInfoFlags flags;
6053 set_info_data_free (SetInfoAsyncData *data)
6056 g_object_unref (data->info);
6058 g_error_free (data->error);
6063 set_info_async_thread (GTask *task,
6066 GCancellable *cancellable)
6068 SetInfoAsyncData *data = task_data;
6071 data->res = g_file_set_attributes_from_info (G_FILE (object),
6079 g_file_real_set_attributes_async (GFile *file,
6081 GFileQueryInfoFlags flags,
6083 GCancellable *cancellable,
6084 GAsyncReadyCallback callback,
6088 SetInfoAsyncData *data;
6090 data = g_new0 (SetInfoAsyncData, 1);
6091 data->info = g_file_info_dup (info);
6092 data->flags = flags;
6094 task = g_task_new (file, cancellable, callback, user_data);
6095 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6096 g_task_set_priority (task, io_priority);
6098 g_task_run_in_thread (task, set_info_async_thread);
6099 g_object_unref (task);
6103 g_file_real_set_attributes_finish (GFile *file,
6108 SetInfoAsyncData *data;
6110 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6112 data = g_task_get_task_data (G_TASK (res));
6115 *info = g_object_ref (data->info);
6117 if (error != NULL && data->error)
6118 *error = g_error_copy (data->error);
6124 find_enclosing_mount_async_thread (GTask *task,
6127 GCancellable *cancellable)
6129 GError *error = NULL;
6132 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6135 g_task_return_error (task, error);
6137 g_task_return_pointer (task, mount, g_object_unref);
6141 g_file_real_find_enclosing_mount_async (GFile *file,
6143 GCancellable *cancellable,
6144 GAsyncReadyCallback callback,
6149 task = g_task_new (file, cancellable, callback, user_data);
6150 g_task_set_priority (task, io_priority);
6152 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6153 g_object_unref (task);
6157 g_file_real_find_enclosing_mount_finish (GFile *file,
6161 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6163 return g_task_propagate_pointer (G_TASK (res), error);
6170 GFileCopyFlags flags;
6171 GFileProgressCallback progress_cb;
6172 gpointer progress_cb_data;
6176 copy_async_data_free (CopyAsyncData *data)
6178 g_object_unref (data->source);
6179 g_object_unref (data->destination);
6180 g_slice_free (CopyAsyncData, data);
6184 CopyAsyncData *data;
6185 goffset current_num_bytes;
6186 goffset total_num_bytes;
6190 copy_async_progress_in_main (gpointer user_data)
6192 ProgressData *progress = user_data;
6193 CopyAsyncData *data = progress->data;
6195 data->progress_cb (progress->current_num_bytes,
6196 progress->total_num_bytes,
6197 data->progress_cb_data);
6203 copy_async_progress_callback (goffset current_num_bytes,
6204 goffset total_num_bytes,
6207 GTask *task = user_data;
6208 CopyAsyncData *data = g_task_get_task_data (task);
6209 ProgressData *progress;
6211 progress = g_new (ProgressData, 1);
6212 progress->data = data;
6213 progress->current_num_bytes = current_num_bytes;
6214 progress->total_num_bytes = total_num_bytes;
6216 g_main_context_invoke_full (g_task_get_context (task),
6217 g_task_get_priority (task),
6218 copy_async_progress_in_main,
6224 copy_async_thread (GTask *task,
6227 GCancellable *cancellable)
6229 CopyAsyncData *data = task_data;
6231 GError *error = NULL;
6233 result = g_file_copy (data->source,
6237 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6241 g_task_return_boolean (task, TRUE);
6243 g_task_return_error (task, error);
6247 g_file_real_copy_async (GFile *source,
6249 GFileCopyFlags flags,
6251 GCancellable *cancellable,
6252 GFileProgressCallback progress_callback,
6253 gpointer progress_callback_data,
6254 GAsyncReadyCallback callback,
6258 CopyAsyncData *data;
6260 data = g_slice_new (CopyAsyncData);
6261 data->source = g_object_ref (source);
6262 data->destination = g_object_ref (destination);
6263 data->flags = flags;
6264 data->progress_cb = progress_callback;
6265 data->progress_cb_data = progress_callback_data;
6267 task = g_task_new (source, cancellable, callback, user_data);
6268 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6269 g_task_set_priority (task, io_priority);
6270 g_task_run_in_thread (task, copy_async_thread);
6271 g_object_unref (task);
6275 g_file_real_copy_finish (GFile *file,
6279 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6281 return g_task_propagate_boolean (G_TASK (res), error);
6285 /********************************************
6286 * Default VFS operations *
6287 ********************************************/
6290 * g_file_new_for_path:
6291 * @path: a string containing a relative or absolute path.
6292 * The string must be encoded in the glib filename encoding.
6294 * Constructs a #GFile for a given path. This operation never
6295 * fails, but the returned object might not support any I/O
6296 * operation if @path is malformed.
6298 * Returns: (transfer full): a new #GFile for the given @path.
6299 * Free the returned object with g_object_unref().
6302 g_file_new_for_path (const char *path)
6304 g_return_val_if_fail (path != NULL, NULL);
6306 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6310 * g_file_new_for_uri:
6311 * @uri: a UTF-8 string containing a URI
6313 * Constructs a #GFile for a given URI. This operation never
6314 * fails, but the returned object might not support any I/O
6315 * operation if @uri is malformed or if the uri type is
6318 * Returns: (transfer full): a new #GFile for the given @uri.
6319 * Free the returned object with g_object_unref().
6322 g_file_new_for_uri (const char *uri)
6324 g_return_val_if_fail (uri != NULL, NULL);
6326 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6331 * @tmpl: (type filename) (allow-none): Template for the file
6332 * name, as in g_file_open_tmp(), or %NULL for a default template
6333 * @iostream: (out): on return, a #GFileIOStream for the created file
6334 * @error: a #GError, or %NULL
6336 * Opens a file in the preferred directory for temporary files (as
6337 * returned by g_get_tmp_dir()) and returns a #GFile and
6338 * #GFileIOStream pointing to it.
6340 * @tmpl should be a string in the GLib file name encoding
6341 * containing a sequence of six 'X' characters, and containing no
6342 * directory components. If it is %NULL, a default template is used.
6344 * Unlike the other #GFile constructors, this will return %NULL if
6345 * a temporary file could not be created.
6347 * Returns: (transfer full): a new #GFile.
6348 * Free the returned object with g_object_unref().
6353 g_file_new_tmp (const char *tmpl,
6354 GFileIOStream **iostream,
6360 GFileOutputStream *output;
6362 g_return_val_if_fail (iostream != NULL, NULL);
6364 fd = g_file_open_tmp (tmpl, &path, error);
6368 file = g_file_new_for_path (path);
6370 output = _g_local_file_output_stream_new (fd);
6371 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6373 g_object_unref (output);
6380 * g_file_parse_name:
6381 * @parse_name: a file name or path to be parsed
6383 * Constructs a #GFile with the given @parse_name (i.e. something
6384 * given by g_file_get_parse_name()). This operation never fails,
6385 * but the returned object might not support any I/O operation if
6386 * the @parse_name cannot be parsed.
6388 * Returns: (transfer full): a new #GFile.
6391 g_file_parse_name (const char *parse_name)
6393 g_return_val_if_fail (parse_name != NULL, NULL);
6395 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6399 is_valid_scheme_character (char c)
6401 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6404 /* Following RFC 2396, valid schemes are built like:
6405 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6408 has_valid_scheme (const char *uri)
6414 if (!g_ascii_isalpha (*p))
6419 } while (is_valid_scheme_character (*p));
6425 new_for_cmdline_arg (const gchar *arg,
6431 if (g_path_is_absolute (arg))
6432 return g_file_new_for_path (arg);
6434 if (has_valid_scheme (arg))
6435 return g_file_new_for_uri (arg);
6441 current_dir = g_get_current_dir ();
6442 filename = g_build_filename (current_dir, arg, NULL);
6443 g_free (current_dir);
6446 filename = g_build_filename (cwd, arg, NULL);
6448 file = g_file_new_for_path (filename);
6455 * g_file_new_for_commandline_arg:
6456 * @arg: a command line string
6458 * Creates a #GFile with the given argument from the command line.
6459 * The value of @arg can be either a URI, an absolute path or a
6460 * relative path resolved relative to the current working directory.
6461 * This operation never fails, but the returned object might not
6462 * support any I/O operation if @arg points to a malformed path.
6464 * Note that on Windows, this function expects its argument to be in
6465 * UTF-8 -- not the system code page. This means that you
6466 * should not use this function with string from argv as it is passed
6467 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6468 * the commandline. #GApplication also uses UTF-8 but
6469 * g_application_command_line_create_file_for_arg() may be more useful
6470 * for you there. It is also always possible to use this function with
6471 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6473 * Returns: (transfer full): a new #GFile.
6474 * Free the returned object with g_object_unref().
6477 g_file_new_for_commandline_arg (const char *arg)
6479 g_return_val_if_fail (arg != NULL, NULL);
6481 return new_for_cmdline_arg (arg, NULL);
6485 * g_file_new_for_commandline_arg_and_cwd:
6486 * @arg: a command line string
6487 * @cwd: the current working directory of the commandline
6489 * Creates a #GFile with the given argument from the command line.
6491 * This function is similar to g_file_new_for_commandline_arg() except
6492 * that it allows for passing the current working directory as an
6493 * argument instead of using the current working directory of the
6496 * This is useful if the commandline argument was given in a context
6497 * other than the invocation of the current process.
6499 * See also g_application_command_line_create_file_for_arg().
6501 * Returns: (transfer full): a new #GFile
6506 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6509 g_return_val_if_fail (arg != NULL, NULL);
6510 g_return_val_if_fail (cwd != NULL, NULL);
6512 return new_for_cmdline_arg (arg, cwd);
6516 * g_file_mount_enclosing_volume:
6517 * @location: input #GFile
6518 * @flags: flags affecting the operation
6519 * @mount_operation: (allow-none): a #GMountOperation
6520 * or %NULL to avoid user interaction
6521 * @cancellable: (allow-none): optional #GCancellable object,
6523 * @callback: (allow-none): a #GAsyncReadyCallback to call
6524 * when the request is satisfied, or %NULL
6525 * @user_data: the data to pass to callback function
6527 * Starts a @mount_operation, mounting the volume that contains
6528 * the file @location.
6530 * When this operation has completed, @callback will be called with
6531 * @user_user data, and the operation can be finalized with
6532 * g_file_mount_enclosing_volume_finish().
6534 * If @cancellable is not %NULL, then the operation can be cancelled by
6535 * triggering the cancellable object from another thread. If the operation
6536 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6539 g_file_mount_enclosing_volume (GFile *location,
6540 GMountMountFlags flags,
6541 GMountOperation *mount_operation,
6542 GCancellable *cancellable,
6543 GAsyncReadyCallback callback,
6548 g_return_if_fail (G_IS_FILE (location));
6550 iface = G_FILE_GET_IFACE (location);
6552 if (iface->mount_enclosing_volume == NULL)
6554 g_task_report_new_error (location, callback, user_data,
6555 g_file_mount_enclosing_volume,
6556 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6557 _("volume doesn't implement mount"));
6561 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6566 * g_file_mount_enclosing_volume_finish:
6567 * @location: input #GFile
6568 * @result: a #GAsyncResult
6569 * @error: a #GError, or %NULL
6571 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6573 * Returns: %TRUE if successful. If an error has occurred,
6574 * this function will return %FALSE and set @error
6575 * appropriately if present.
6578 g_file_mount_enclosing_volume_finish (GFile *location,
6579 GAsyncResult *result,
6584 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6585 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6587 if (g_async_result_legacy_propagate_error (result, error))
6589 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6590 return g_task_propagate_boolean (G_TASK (result), error);
6592 iface = G_FILE_GET_IFACE (location);
6594 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6597 /********************************************
6598 * Utility functions *
6599 ********************************************/
6602 * g_file_query_default_handler:
6603 * @file: a #GFile to open
6604 * @cancellable: optional #GCancellable object, %NULL to ignore
6605 * @error: a #GError, or %NULL
6607 * Returns the #GAppInfo that is registered as the default
6608 * application to handle the file specified by @file.
6610 * If @cancellable is not %NULL, then the operation can be cancelled by
6611 * triggering the cancellable object from another thread. If the operation
6612 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6614 * Returns: (transfer full): a #GAppInfo if the handle was found,
6615 * %NULL if there were errors.
6616 * When you are done with it, release it with g_object_unref()
6619 g_file_query_default_handler (GFile *file,
6620 GCancellable *cancellable,
6624 const char *content_type;
6629 uri_scheme = g_file_get_uri_scheme (file);
6630 if (uri_scheme && uri_scheme[0] != '\0')
6632 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6633 g_free (uri_scheme);
6635 if (appinfo != NULL)
6639 info = g_file_query_info (file,
6640 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6649 content_type = g_file_info_get_content_type (info);
6652 /* Don't use is_native(), as we want to support fuse paths if available */
6653 path = g_file_get_path (file);
6654 appinfo = g_app_info_get_default_for_type (content_type,
6659 g_object_unref (info);
6661 if (appinfo != NULL)
6664 g_set_error_literal (error, G_IO_ERROR,
6665 G_IO_ERROR_NOT_SUPPORTED,
6666 _("No application is registered as handling this file"));
6670 #define GET_CONTENT_BLOCK_SIZE 8192
6673 * g_file_load_contents:
6674 * @file: input #GFile
6675 * @cancellable: optional #GCancellable object, %NULL to ignore
6676 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6677 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6678 * or %NULL if the length is not needed
6679 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6680 * or %NULL if the entity tag is not needed
6681 * @error: a #GError, or %NULL
6683 * Loads the content of the file into memory. The data is always
6684 * zero-terminated, but this is not included in the resultant @length.
6685 * The returned @content should be freed with g_free() when no longer
6688 * If @cancellable is not %NULL, then the operation can be cancelled by
6689 * triggering the cancellable object from another thread. If the operation
6690 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6692 * Returns: %TRUE if the @file's contents were successfully loaded.
6693 * %FALSE if there were errors.
6696 g_file_load_contents (GFile *file,
6697 GCancellable *cancellable,
6703 GFileInputStream *in;
6704 GByteArray *content;
6709 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6710 g_return_val_if_fail (contents != NULL, FALSE);
6712 in = g_file_read (file, cancellable, error);
6716 content = g_byte_array_new ();
6719 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6720 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6721 content->data + pos,
6722 GET_CONTENT_BLOCK_SIZE,
6723 cancellable, error)) > 0)
6726 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6733 info = g_file_input_stream_query_info (in,
6734 G_FILE_ATTRIBUTE_ETAG_VALUE,
6739 *etag_out = g_strdup (g_file_info_get_etag (info));
6740 g_object_unref (info);
6744 /* Ignore errors on close */
6745 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6746 g_object_unref (in);
6750 /* error is set already */
6751 g_byte_array_free (content, TRUE);
6758 /* Zero terminate (we got an extra byte allocated for this */
6759 content->data[pos] = 0;
6761 *contents = (char *)g_byte_array_free (content, FALSE);
6768 GFileReadMoreCallback read_more_callback;
6769 GByteArray *content;
6776 load_contents_data_free (LoadContentsData *data)
6779 g_byte_array_free (data->content, TRUE);
6780 g_free (data->etag);
6785 load_contents_close_callback (GObject *obj,
6786 GAsyncResult *close_res,
6789 GInputStream *stream = G_INPUT_STREAM (obj);
6790 LoadContentsData *data = user_data;
6792 /* Ignore errors here, we're only reading anyway */
6793 g_input_stream_close_finish (stream, close_res, NULL);
6794 g_object_unref (stream);
6796 g_task_return_boolean (data->task, TRUE);
6797 g_object_unref (data->task);
6801 load_contents_fstat_callback (GObject *obj,
6802 GAsyncResult *stat_res,
6805 GInputStream *stream = G_INPUT_STREAM (obj);
6806 LoadContentsData *data = user_data;
6809 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6813 data->etag = g_strdup (g_file_info_get_etag (info));
6814 g_object_unref (info);
6817 g_input_stream_close_async (stream, 0,
6818 g_task_get_cancellable (data->task),
6819 load_contents_close_callback, data);
6823 load_contents_read_callback (GObject *obj,
6824 GAsyncResult *read_res,
6827 GInputStream *stream = G_INPUT_STREAM (obj);
6828 LoadContentsData *data = user_data;
6829 GError *error = NULL;
6832 read_size = g_input_stream_read_finish (stream, read_res, &error);
6836 g_task_return_error (data->task, error);
6837 g_object_unref (data->task);
6839 /* Close the file ignoring any error */
6840 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6841 g_object_unref (stream);
6843 else if (read_size == 0)
6845 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6846 G_FILE_ATTRIBUTE_ETAG_VALUE,
6848 g_task_get_cancellable (data->task),
6849 load_contents_fstat_callback,
6852 else if (read_size > 0)
6854 data->pos += read_size;
6856 g_byte_array_set_size (data->content,
6857 data->pos + GET_CONTENT_BLOCK_SIZE);
6860 if (data->read_more_callback &&
6861 !data->read_more_callback ((char *)data->content->data, data->pos,
6862 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6863 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6864 G_FILE_ATTRIBUTE_ETAG_VALUE,
6866 g_task_get_cancellable (data->task),
6867 load_contents_fstat_callback,
6870 g_input_stream_read_async (stream,
6871 data->content->data + data->pos,
6872 GET_CONTENT_BLOCK_SIZE,
6874 g_task_get_cancellable (data->task),
6875 load_contents_read_callback,
6881 load_contents_open_callback (GObject *obj,
6882 GAsyncResult *open_res,
6885 GFile *file = G_FILE (obj);
6886 GFileInputStream *stream;
6887 LoadContentsData *data = user_data;
6888 GError *error = NULL;
6890 stream = g_file_read_finish (file, open_res, &error);
6894 g_byte_array_set_size (data->content,
6895 data->pos + GET_CONTENT_BLOCK_SIZE);
6896 g_input_stream_read_async (G_INPUT_STREAM (stream),
6897 data->content->data + data->pos,
6898 GET_CONTENT_BLOCK_SIZE,
6900 g_task_get_cancellable (data->task),
6901 load_contents_read_callback,
6906 g_task_return_error (data->task, error);
6907 g_object_unref (data->task);
6912 * g_file_load_partial_contents_async: (skip)
6913 * @file: input #GFile
6914 * @cancellable: optional #GCancellable object, %NULL to ignore
6915 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6916 * and to specify whether further data should be read
6917 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6918 * @user_data: the data to pass to the callback functions
6920 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6921 * be used to stop reading from the file when appropriate, else this
6922 * function will behave exactly as g_file_load_contents_async(). This
6923 * operation can be finished by g_file_load_partial_contents_finish().
6925 * Users of this function should be aware that @user_data is passed to
6926 * both the @read_more_callback and the @callback.
6928 * If @cancellable is not %NULL, then the operation can be cancelled by
6929 * triggering the cancellable object from another thread. If the operation
6930 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6933 g_file_load_partial_contents_async (GFile *file,
6934 GCancellable *cancellable,
6935 GFileReadMoreCallback read_more_callback,
6936 GAsyncReadyCallback callback,
6939 LoadContentsData *data;
6941 g_return_if_fail (G_IS_FILE (file));
6943 data = g_new0 (LoadContentsData, 1);
6944 data->read_more_callback = read_more_callback;
6945 data->content = g_byte_array_new ();
6947 data->task = g_task_new (file, cancellable, callback, user_data);
6948 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6950 g_file_read_async (file,
6952 g_task_get_cancellable (data->task),
6953 load_contents_open_callback,
6958 * g_file_load_partial_contents_finish:
6959 * @file: input #GFile
6960 * @res: a #GAsyncResult
6961 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6962 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6963 * or %NULL if the length is not needed
6964 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6965 * or %NULL if the entity tag is not needed
6966 * @error: a #GError, or %NULL
6968 * Finishes an asynchronous partial load operation that was started
6969 * with g_file_load_partial_contents_async(). The data is always
6970 * zero-terminated, but this is not included in the resultant @length.
6971 * The returned @content should be freed with g_free() when no longer
6974 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6975 * present, it will be set appropriately.
6978 g_file_load_partial_contents_finish (GFile *file,
6986 LoadContentsData *data;
6988 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6989 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6990 g_return_val_if_fail (contents != NULL, FALSE);
6992 task = G_TASK (res);
6994 if (!g_task_propagate_boolean (task, error))
7001 data = g_task_get_task_data (task);
7004 *length = data->pos;
7008 *etag_out = data->etag;
7012 /* Zero terminate */
7013 g_byte_array_set_size (data->content, data->pos + 1);
7014 data->content->data[data->pos] = 0;
7016 *contents = (char *)g_byte_array_free (data->content, FALSE);
7017 data->content = NULL;
7023 * g_file_load_contents_async:
7024 * @file: input #GFile
7025 * @cancellable: optional #GCancellable object, %NULL to ignore
7026 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7027 * @user_data: the data to pass to callback function
7029 * Starts an asynchronous load of the @file's contents.
7031 * For more details, see g_file_load_contents() which is
7032 * the synchronous version of this call.
7034 * When the load operation has completed, @callback will be called
7035 * with @user data. To finish the operation, call
7036 * g_file_load_contents_finish() with the #GAsyncResult returned by
7039 * If @cancellable is not %NULL, then the operation can be cancelled by
7040 * triggering the cancellable object from another thread. If the operation
7041 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7044 g_file_load_contents_async (GFile *file,
7045 GCancellable *cancellable,
7046 GAsyncReadyCallback callback,
7049 g_file_load_partial_contents_async (file,
7052 callback, user_data);
7056 * g_file_load_contents_finish:
7057 * @file: input #GFile
7058 * @res: a #GAsyncResult
7059 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7060 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7061 * or %NULL if the length is not needed
7062 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7063 * or %NULL if the entity tag is not needed
7064 * @error: a #GError, or %NULL
7066 * Finishes an asynchronous load of the @file's contents.
7067 * The contents are placed in @contents, and @length is set to the
7068 * size of the @contents string. The @content should be freed with
7069 * g_free() when no longer needed. If @etag_out is present, it will be
7070 * set to the new entity tag for the @file.
7072 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7073 * present, it will be set appropriately.
7076 g_file_load_contents_finish (GFile *file,
7083 return g_file_load_partial_contents_finish (file,
7092 * g_file_replace_contents:
7093 * @file: input #GFile
7094 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7095 * @length: the length of @contents in bytes
7096 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7097 * for the document, or %NULL
7098 * @make_backup: %TRUE if a backup should be created
7099 * @flags: a set of #GFileCreateFlags
7100 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7101 * for the document. This should be freed with g_free() when no longer
7103 * @cancellable: optional #GCancellable object, %NULL to ignore
7104 * @error: a #GError, or %NULL
7106 * Replaces the contents of @file with @contents of @length bytes.
7108 * If @etag is specified (not %NULL), any existing file must have that etag,
7109 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7111 * If @make_backup is %TRUE, this function will attempt to make a backup
7114 * If @cancellable is not %NULL, then the operation can be cancelled by
7115 * triggering the cancellable object from another thread. If the operation
7116 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7118 * The returned @new_etag can be used to verify that the file hasn't
7119 * changed the next time it is saved over.
7121 * Returns: %TRUE if successful. If an error has occurred, this function
7122 * will return %FALSE and set @error appropriately if present.
7125 g_file_replace_contents (GFile *file,
7126 const char *contents,
7129 gboolean make_backup,
7130 GFileCreateFlags flags,
7132 GCancellable *cancellable,
7135 GFileOutputStream *out;
7136 gsize pos, remainder;
7140 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7141 g_return_val_if_fail (contents != NULL, FALSE);
7143 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7149 while (remainder > 0 &&
7150 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7152 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7160 if (remainder > 0 && res < 0)
7162 /* Ignore errors on close */
7163 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7164 g_object_unref (out);
7166 /* error is set already */
7170 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7173 *new_etag = g_file_output_stream_get_etag (out);
7175 g_object_unref (out);
7186 } ReplaceContentsData;
7189 replace_contents_data_free (ReplaceContentsData *data)
7191 g_bytes_unref (data->content);
7192 g_free (data->etag);
7197 replace_contents_close_callback (GObject *obj,
7198 GAsyncResult *close_res,
7201 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7202 ReplaceContentsData *data = user_data;
7204 /* Ignore errors here, we're only reading anyway */
7205 g_output_stream_close_finish (stream, close_res, NULL);
7206 g_object_unref (stream);
7210 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7211 g_task_return_boolean (data->task, TRUE);
7213 g_object_unref (data->task);
7217 replace_contents_write_callback (GObject *obj,
7218 GAsyncResult *read_res,
7221 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7222 ReplaceContentsData *data = user_data;
7223 GError *error = NULL;
7226 write_size = g_output_stream_write_finish (stream, read_res, &error);
7228 if (write_size <= 0)
7230 /* Error or EOF, close the file */
7233 data->failed = TRUE;
7234 g_task_return_error (data->task, error);
7236 g_output_stream_close_async (stream, 0,
7237 g_task_get_cancellable (data->task),
7238 replace_contents_close_callback, data);
7240 else if (write_size > 0)
7242 const gchar *content;
7245 content = g_bytes_get_data (data->content, &length);
7246 data->pos += write_size;
7248 if (data->pos >= length)
7249 g_output_stream_close_async (stream, 0,
7250 g_task_get_cancellable (data->task),
7251 replace_contents_close_callback, data);
7253 g_output_stream_write_async (stream,
7254 content + data->pos,
7257 g_task_get_cancellable (data->task),
7258 replace_contents_write_callback,
7264 replace_contents_open_callback (GObject *obj,
7265 GAsyncResult *open_res,
7268 GFile *file = G_FILE (obj);
7269 GFileOutputStream *stream;
7270 ReplaceContentsData *data = user_data;
7271 GError *error = NULL;
7273 stream = g_file_replace_finish (file, open_res, &error);
7277 const gchar *content;
7280 content = g_bytes_get_data (data->content, &length);
7281 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7282 content + data->pos,
7285 g_task_get_cancellable (data->task),
7286 replace_contents_write_callback,
7291 g_task_return_error (data->task, error);
7292 g_object_unref (data->task);
7297 * g_file_replace_contents_async:
7298 * @file: input #GFile
7299 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7300 * @length: the length of @contents in bytes
7301 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7302 * @make_backup: %TRUE if a backup should be created
7303 * @flags: a set of #GFileCreateFlags
7304 * @cancellable: optional #GCancellable object, %NULL to ignore
7305 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7306 * @user_data: the data to pass to callback function
7308 * Starts an asynchronous replacement of @file with the given
7309 * @contents of @length bytes. @etag will replace the document's
7310 * current entity tag.
7312 * When this operation has completed, @callback will be called with
7313 * @user_user data, and the operation can be finalized with
7314 * g_file_replace_contents_finish().
7316 * If @cancellable is not %NULL, then the operation can be cancelled by
7317 * triggering the cancellable object from another thread. If the operation
7318 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7320 * If @make_backup is %TRUE, this function will attempt to
7321 * make a backup of @file.
7323 * Note that no copy of @content will be made, so it must stay valid
7324 * until @callback is called. See g_file_replace_contents_bytes_async()
7325 * for a #GBytes version that will automatically hold a reference to the
7326 * contents (without copying) for the duration of the call.
7329 g_file_replace_contents_async (GFile *file,
7330 const char *contents,
7333 gboolean make_backup,
7334 GFileCreateFlags flags,
7335 GCancellable *cancellable,
7336 GAsyncReadyCallback callback,
7341 bytes = g_bytes_new_static (contents, length);
7342 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7343 cancellable, callback, user_data);
7344 g_bytes_unref (bytes);
7348 * g_file_replace_contents_bytes_async:
7349 * @file: input #GFile
7350 * @contents: a #GBytes
7351 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7352 * @make_backup: %TRUE if a backup should be created
7353 * @flags: a set of #GFileCreateFlags
7354 * @cancellable: optional #GCancellable object, %NULL to ignore
7355 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7356 * @user_data: the data to pass to callback function
7358 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7359 * This function will keep a ref on @contents until the operation is done.
7360 * Unlike g_file_replace_contents_async() this allows forgetting about the
7361 * content without waiting for the callback.
7363 * When this operation has completed, @callback will be called with
7364 * @user_user data, and the operation can be finalized with
7365 * g_file_replace_contents_finish().
7370 g_file_replace_contents_bytes_async (GFile *file,
7373 gboolean make_backup,
7374 GFileCreateFlags flags,
7375 GCancellable *cancellable,
7376 GAsyncReadyCallback callback,
7379 ReplaceContentsData *data;
7381 g_return_if_fail (G_IS_FILE (file));
7382 g_return_if_fail (contents != NULL);
7384 data = g_new0 (ReplaceContentsData, 1);
7386 data->content = g_bytes_ref (contents);
7388 data->task = g_task_new (file, cancellable, callback, user_data);
7389 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7391 g_file_replace_async (file,
7396 g_task_get_cancellable (data->task),
7397 replace_contents_open_callback,
7402 * g_file_replace_contents_finish:
7403 * @file: input #GFile
7404 * @res: a #GAsyncResult
7405 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7406 * for the document. This should be freed with g_free() when it is no
7407 * longer needed, or %NULL
7408 * @error: a #GError, or %NULL
7410 * Finishes an asynchronous replace of the given @file. See
7411 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7412 * tag for the document, if present.
7414 * Returns: %TRUE on success, %FALSE on failure.
7417 g_file_replace_contents_finish (GFile *file,
7423 ReplaceContentsData *data;
7425 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7426 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7428 task = G_TASK (res);
7430 if (!g_task_propagate_boolean (task, error))
7433 data = g_task_get_task_data (task);
7437 *new_etag = data->etag;
7438 data->etag = NULL; /* Take ownership */
7445 g_file_real_measure_disk_usage (GFile *file,
7446 GFileMeasureFlags flags,
7447 GCancellable *cancellable,
7448 GFileMeasureProgressCallback progress_callback,
7449 gpointer progress_data,
7450 guint64 *disk_usage,
7455 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7456 "Operation not supported for the current backend.");
7462 GFileMeasureFlags flags;
7463 GFileMeasureProgressCallback progress_callback;
7464 gpointer progress_data;
7476 GFileMeasureProgressCallback callback;
7479 guint64 current_size;
7485 measure_disk_usage_invoke_progress (gpointer user_data)
7487 MeasureProgress *progress = user_data;
7489 (* progress->callback) (progress->reporting,
7490 progress->current_size, progress->num_dirs, progress->num_files,
7491 progress->user_data);
7497 measure_disk_usage_progress (gboolean reporting,
7498 guint64 current_size,
7503 MeasureProgress progress;
7504 GTask *task = user_data;
7505 MeasureTaskData *data;
7507 data = g_task_get_task_data (task);
7509 progress.callback = data->progress_callback;
7510 progress.user_data = data->progress_data;
7511 progress.reporting = reporting;
7512 progress.current_size = current_size;
7513 progress.num_dirs = num_dirs;
7514 progress.num_files = num_files;
7516 g_main_context_invoke_full (g_task_get_context (task),
7517 g_task_get_priority (task),
7518 measure_disk_usage_invoke_progress,
7519 g_memdup (&progress, sizeof progress),
7524 measure_disk_usage_thread (GTask *task,
7525 gpointer source_object,
7527 GCancellable *cancellable)
7529 MeasureTaskData *data = task_data;
7530 GError *error = NULL;
7531 MeasureResult result;
7533 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7534 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7535 &result.disk_usage, &result.num_dirs, &result.num_files,
7537 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7539 g_task_return_error (task, error);
7543 g_file_real_measure_disk_usage_async (GFile *file,
7544 GFileMeasureFlags flags,
7546 GCancellable *cancellable,
7547 GFileMeasureProgressCallback progress_callback,
7548 gpointer progress_data,
7549 GAsyncReadyCallback callback,
7552 MeasureTaskData data;
7556 data.progress_callback = progress_callback;
7557 data.progress_data = progress_data;
7559 task = g_task_new (file, cancellable, callback, user_data);
7560 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7561 g_task_set_priority (task, io_priority);
7563 g_task_run_in_thread (task, measure_disk_usage_thread);
7564 g_object_unref (task);
7568 g_file_real_measure_disk_usage_finish (GFile *file,
7569 GAsyncResult *result,
7570 guint64 *disk_usage,
7575 MeasureResult *measure_result;
7577 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7579 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7581 if (measure_result == NULL)
7585 *disk_usage = measure_result->disk_usage;
7588 *num_dirs = measure_result->num_dirs;
7591 *num_files = measure_result->num_files;
7593 g_free (measure_result);
7599 * g_file_measure_disk_usage:
7601 * @flags: #GFileMeasureFlags
7602 * @cancellable: (allow-none): optional #GCancellable
7603 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7604 * @progress_data: user_data for @progress_callback
7605 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7606 * @num_dirs: (allow-none) (out): the number of directories encountered
7607 * @num_files: (allow-none) (out): the number of non-directories encountered
7608 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7610 * Recursively measures the disk usage of @file.
7612 * This is essentially an analog of the '<literal>du</literal>' command,
7613 * but it also reports the number of directories and non-directory files
7614 * encountered (including things like symbolic links).
7616 * By default, errors are only reported against the toplevel file
7617 * itself. Errors found while recursing are silently ignored, unless
7618 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7620 * The returned size, @disk_usage, is in bytes and should be formatted
7621 * with g_format_size() in order to get something reasonable for showing
7622 * in a user interface.
7624 * @progress_callback and @progress_data can be given to request
7625 * periodic progress updates while scanning. See the documentation for
7626 * #GFileMeasureProgressCallback for information about when and how the
7627 * callback will be invoked.
7629 * Returns: %TRUE if successful, with the out parameters set.
7630 * %FALSE otherwise, with @error set.
7635 g_file_measure_disk_usage (GFile *file,
7636 GFileMeasureFlags flags,
7637 GCancellable *cancellable,
7638 GFileMeasureProgressCallback progress_callback,
7639 gpointer progress_data,
7640 guint64 *disk_usage,
7645 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7646 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7647 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7649 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7650 progress_callback, progress_data,
7651 disk_usage, num_dirs, num_files,
7656 * g_file_measure_disk_usage_async:
7658 * @flags: #GFileMeasureFlags
7659 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7661 * @cancellable: (allow-none): optional #GCancellable
7662 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7663 * @progress_data: user_data for @progress_callback
7664 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7665 * @user_data: the data to pass to callback function
7667 * Recursively measures the disk usage of @file.
7669 * This is the asynchronous version of g_file_measure_disk_usage(). See
7670 * there for more information.
7675 g_file_measure_disk_usage_async (GFile *file,
7676 GFileMeasureFlags flags,
7678 GCancellable *cancellable,
7679 GFileMeasureProgressCallback progress_callback,
7680 gpointer progress_data,
7681 GAsyncReadyCallback callback,
7684 g_return_if_fail (G_IS_FILE (file));
7685 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7687 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7688 progress_callback, progress_data,
7689 callback, user_data);
7693 * g_file_measure_disk_usage_finish:
7695 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7696 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7697 * @num_dirs: (allow-none) (out): the number of directories encountered
7698 * @num_files: (allow-none) (out): the number of non-directories encountered
7699 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7701 * Collects the results from an earlier call to
7702 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7705 * Returns: %TRUE if successful, with the out parameters set.
7706 * %FALSE otherwise, with @error set.
7711 g_file_measure_disk_usage_finish (GFile *file,
7712 GAsyncResult *result,
7713 guint64 *disk_usage,
7718 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7719 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7721 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7725 * g_file_start_mountable:
7726 * @file: input #GFile
7727 * @flags: flags affecting the operation
7728 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7729 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7730 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7731 * @user_data: the data to pass to callback function
7733 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7734 * Using @start_operation, you can request callbacks when, for instance,
7735 * passwords are needed during authentication.
7737 * If @cancellable is not %NULL, then the operation can be cancelled by
7738 * triggering the cancellable object from another thread. If the operation
7739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7741 * When the operation is finished, @callback will be called.
7742 * You can then call g_file_mount_mountable_finish() to get
7743 * the result of the operation.
7748 g_file_start_mountable (GFile *file,
7749 GDriveStartFlags flags,
7750 GMountOperation *start_operation,
7751 GCancellable *cancellable,
7752 GAsyncReadyCallback callback,
7757 g_return_if_fail (G_IS_FILE (file));
7759 iface = G_FILE_GET_IFACE (file);
7761 if (iface->start_mountable == NULL)
7763 g_task_report_new_error (file, callback, user_data,
7764 g_file_start_mountable,
7765 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7766 _("Operation not supported"));
7770 (* iface->start_mountable) (file,
7779 * g_file_start_mountable_finish:
7780 * @file: input #GFile
7781 * @result: a #GAsyncResult
7782 * @error: a #GError, or %NULL
7784 * Finishes a start operation. See g_file_start_mountable() for details.
7786 * Finish an asynchronous start operation that was started
7787 * with g_file_start_mountable().
7789 * Returns: %TRUE if the operation finished successfully. %FALSE
7795 g_file_start_mountable_finish (GFile *file,
7796 GAsyncResult *result,
7801 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7802 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7804 if (g_async_result_legacy_propagate_error (result, error))
7806 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7807 return g_task_propagate_boolean (G_TASK (result), error);
7809 iface = G_FILE_GET_IFACE (file);
7810 return (* iface->start_mountable_finish) (file, result, error);
7814 * g_file_stop_mountable:
7815 * @file: input #GFile
7816 * @flags: flags affecting the operation
7817 * @mount_operation: (allow-none): a #GMountOperation,
7818 * or %NULL to avoid user interaction.
7819 * @cancellable: (allow-none): optional #GCancellable object,
7821 * @callback: (allow-none): a #GAsyncReadyCallback to call
7822 * when the request is satisfied, or %NULL
7823 * @user_data: the data to pass to callback function
7825 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7827 * If @cancellable is not %NULL, then the operation can be cancelled by
7828 * triggering the cancellable object from another thread. If the operation
7829 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7831 * When the operation is finished, @callback will be called.
7832 * You can then call g_file_stop_mountable_finish() to get
7833 * the result of the operation.
7838 g_file_stop_mountable (GFile *file,
7839 GMountUnmountFlags flags,
7840 GMountOperation *mount_operation,
7841 GCancellable *cancellable,
7842 GAsyncReadyCallback callback,
7847 g_return_if_fail (G_IS_FILE (file));
7849 iface = G_FILE_GET_IFACE (file);
7851 if (iface->stop_mountable == NULL)
7853 g_task_report_new_error (file, callback, user_data,
7854 g_file_stop_mountable,
7855 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7856 _("Operation not supported"));
7860 (* iface->stop_mountable) (file,
7869 * g_file_stop_mountable_finish:
7870 * @file: input #GFile
7871 * @result: a #GAsyncResult
7872 * @error: a #GError, or %NULL
7874 * Finishes an stop operation, see g_file_stop_mountable() for details.
7876 * Finish an asynchronous stop operation that was started
7877 * with g_file_stop_mountable().
7879 * Returns: %TRUE if the operation finished successfully.
7885 g_file_stop_mountable_finish (GFile *file,
7886 GAsyncResult *result,
7891 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7892 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7894 if (g_async_result_legacy_propagate_error (result, error))
7896 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7897 return g_task_propagate_boolean (G_TASK (result), error);
7899 iface = G_FILE_GET_IFACE (file);
7900 return (* iface->stop_mountable_finish) (file, result, error);
7904 * g_file_poll_mountable:
7905 * @file: input #GFile
7906 * @cancellable: optional #GCancellable object, %NULL to ignore
7907 * @callback: (allow-none): a #GAsyncReadyCallback to call
7908 * when the request is satisfied, or %NULL
7909 * @user_data: the data to pass to callback function
7911 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7913 * If @cancellable is not %NULL, then the operation can be cancelled by
7914 * triggering the cancellable object from another thread. If the operation
7915 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7917 * When the operation is finished, @callback will be called.
7918 * You can then call g_file_mount_mountable_finish() to get
7919 * the result of the operation.
7924 g_file_poll_mountable (GFile *file,
7925 GCancellable *cancellable,
7926 GAsyncReadyCallback callback,
7931 g_return_if_fail (G_IS_FILE (file));
7933 iface = G_FILE_GET_IFACE (file);
7935 if (iface->poll_mountable == NULL)
7937 g_task_report_new_error (file, callback, user_data,
7938 g_file_poll_mountable,
7939 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7940 _("Operation not supported"));
7944 (* iface->poll_mountable) (file,
7951 * g_file_poll_mountable_finish:
7952 * @file: input #GFile
7953 * @result: a #GAsyncResult
7954 * @error: a #GError, or %NULL
7956 * Finishes a poll operation. See g_file_poll_mountable() for details.
7958 * Finish an asynchronous poll operation that was polled
7959 * with g_file_poll_mountable().
7961 * Returns: %TRUE if the operation finished successfully. %FALSE
7967 g_file_poll_mountable_finish (GFile *file,
7968 GAsyncResult *result,
7973 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7974 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7976 if (g_async_result_legacy_propagate_error (result, error))
7978 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7979 return g_task_propagate_boolean (G_TASK (result), error);
7981 iface = G_FILE_GET_IFACE (file);
7982 return (* iface->poll_mountable_finish) (file, result, error);
7986 * g_file_supports_thread_contexts:
7989 * Checks if @file supports <link
7990 * linkend="g-main-context-push-thread-default-context">thread-default
7991 * contexts</link>. If this returns %FALSE, you cannot perform
7992 * asynchronous operations on @file in a thread that has a
7993 * thread-default context.
7995 * Returns: Whether or not @file supports thread-default contexts.
8000 g_file_supports_thread_contexts (GFile *file)
8004 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8006 iface = G_FILE_GET_IFACE (file);
8007 return iface->supports_thread_contexts;