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:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
85 * One way to think of a #GFile is as an abstraction of a pathname. For
86 * normal files the system pathname is what is stored internally, but as
87 * #GFiles are extensible it could also be something else that corresponds
88 * to a pathname in a userspace implementation of a filesystem.
90 * #GFiles make up hierarchies of directories and files that correspond to
91 * the files on a filesystem. You can move through the file system with
92 * #GFile using g_file_get_parent() to get an identifier for the parent
93 * directory, g_file_get_child() to get a child within a directory,
94 * g_file_resolve_relative_path() to resolve a relative path between two
95 * #GFiles. There can be multiple hierarchies, so you may not end up at
96 * the same root if you repeatedly call g_file_get_parent() on two different
99 * All #GFiles have a basename (get with g_file_get_basename()). These names
100 * are byte strings that are used to identify the file on the filesystem
101 * (relative to its parent directory) and there is no guarantees that they
102 * have any particular charset encoding or even make any sense at all. If
103 * you want to use filenames in a user interface you should use the display
104 * name that you can get by requesting the
105 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
106 * This is guaranteed to be in UTF-8 and can be used in a user interface.
107 * But always store the real basename or the #GFile to use to actually
108 * access the file, because there is no way to go from a display name to
111 * Using #GFile as an identifier has the same weaknesses as using a path
112 * in that there may be multiple aliases for the same file. For instance,
113 * hard or soft links may cause two different #GFiles to refer to the same
114 * file. Other possible causes for aliases are: case insensitive filesystems,
115 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
116 * check if two #GFiles point to the same file you can query for the
117 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
118 * canonicalization of pathnames passed in, so that trivial differences in
119 * the path string used at creation (duplicated slashes, slash at end of
120 * path, "." or ".." path segments, etc) does not create different #GFiles.
122 * Many #GFile operations have both synchronous and asynchronous versions
123 * to suit your application. Asynchronous versions of synchronous functions
124 * simply have _async() appended to their function names. The asynchronous
125 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
126 * the operation, producing a GAsyncResult which is then passed to the
127 * function's matching _finish() operation.
129 * It is highly recommended to use asynchronous calls when running within a
130 * shared main loop, such as in the main thread of an application. This avoids
131 * I/O operations blocking other sources on the main loop from being dispatched.
132 * Synchronous I/O operations should be performed from worker threads. See the
133 * [introduction to asynchronous programming section][async-programming] for
136 * Some #GFile operations almost always take a noticeable amount of time, and
137 * so do not have synchronous analogs. Notable cases include:
138 * - g_file_mount_mountable() to mount a mountable file.
139 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
140 * - g_file_eject_mountable_with_operation() to eject a mountable file.
142 * ## Entity Tags # {#gfile-etag}
144 * One notable feature of #GFiles are entity tags, or "etags" for
145 * short. Entity tags are somewhat like a more abstract version of the
146 * traditional mtime, and can be used to quickly determine if the file
147 * has been modified from the version on the file system. See the
149 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
150 * for HTTP Etag headers, which are a very similar concept.
153 static void g_file_real_query_info_async (GFile *file,
154 const char *attributes,
155 GFileQueryInfoFlags flags,
157 GCancellable *cancellable,
158 GAsyncReadyCallback callback,
160 static GFileInfo * g_file_real_query_info_finish (GFile *file,
163 static void g_file_real_query_filesystem_info_async (GFile *file,
164 const char *attributes,
166 GCancellable *cancellable,
167 GAsyncReadyCallback callback,
169 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
172 static void g_file_real_enumerate_children_async (GFile *file,
173 const char *attributes,
174 GFileQueryInfoFlags flags,
176 GCancellable *cancellable,
177 GAsyncReadyCallback callback,
179 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
182 static void g_file_real_read_async (GFile *file,
184 GCancellable *cancellable,
185 GAsyncReadyCallback callback,
187 static GFileInputStream * g_file_real_read_finish (GFile *file,
190 static void g_file_real_append_to_async (GFile *file,
191 GFileCreateFlags flags,
193 GCancellable *cancellable,
194 GAsyncReadyCallback callback,
196 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
199 static void g_file_real_create_async (GFile *file,
200 GFileCreateFlags flags,
202 GCancellable *cancellable,
203 GAsyncReadyCallback callback,
205 static GFileOutputStream *g_file_real_create_finish (GFile *file,
208 static void g_file_real_replace_async (GFile *file,
210 gboolean make_backup,
211 GFileCreateFlags flags,
213 GCancellable *cancellable,
214 GAsyncReadyCallback callback,
216 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
219 static void g_file_real_delete_async (GFile *file,
221 GCancellable *cancellable,
222 GAsyncReadyCallback callback,
224 static gboolean g_file_real_delete_finish (GFile *file,
227 static void g_file_real_trash_async (GFile *file,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
232 static gboolean g_file_real_trash_finish (GFile *file,
235 static void g_file_real_make_directory_async (GFile *file,
237 GCancellable *cancellable,
238 GAsyncReadyCallback callback,
240 static gboolean g_file_real_make_directory_finish (GFile *file,
243 static void g_file_real_open_readwrite_async (GFile *file,
245 GCancellable *cancellable,
246 GAsyncReadyCallback callback,
248 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
251 static void g_file_real_create_readwrite_async (GFile *file,
252 GFileCreateFlags flags,
254 GCancellable *cancellable,
255 GAsyncReadyCallback callback,
257 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
260 static void g_file_real_replace_readwrite_async (GFile *file,
262 gboolean make_backup,
263 GFileCreateFlags flags,
265 GCancellable *cancellable,
266 GAsyncReadyCallback callback,
268 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
271 static gboolean g_file_real_set_attributes_from_info (GFile *file,
273 GFileQueryInfoFlags flags,
274 GCancellable *cancellable,
276 static void g_file_real_set_display_name_async (GFile *file,
277 const char *display_name,
279 GCancellable *cancellable,
280 GAsyncReadyCallback callback,
282 static GFile * g_file_real_set_display_name_finish (GFile *file,
285 static void g_file_real_set_attributes_async (GFile *file,
287 GFileQueryInfoFlags flags,
289 GCancellable *cancellable,
290 GAsyncReadyCallback callback,
292 static gboolean g_file_real_set_attributes_finish (GFile *file,
296 static void g_file_real_find_enclosing_mount_async (GFile *file,
298 GCancellable *cancellable,
299 GAsyncReadyCallback callback,
301 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
304 static void g_file_real_copy_async (GFile *source,
306 GFileCopyFlags flags,
308 GCancellable *cancellable,
309 GFileProgressCallback progress_callback,
310 gpointer progress_callback_data,
311 GAsyncReadyCallback callback,
313 static gboolean g_file_real_copy_finish (GFile *file,
317 static gboolean g_file_real_measure_disk_usage (GFile *file,
318 GFileMeasureFlags flags,
319 GCancellable *cancellable,
320 GFileMeasureProgressCallback progress_callback,
321 gpointer progress_data,
326 static void g_file_real_measure_disk_usage_async (GFile *file,
327 GFileMeasureFlags flags,
329 GCancellable *cancellable,
330 GFileMeasureProgressCallback progress_callback,
331 gpointer progress_data,
332 GAsyncReadyCallback callback,
334 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
335 GAsyncResult *result,
341 typedef GFileIface GFileInterface;
342 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
345 g_file_default_init (GFileIface *iface)
347 iface->enumerate_children_async = g_file_real_enumerate_children_async;
348 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
349 iface->set_display_name_async = g_file_real_set_display_name_async;
350 iface->set_display_name_finish = g_file_real_set_display_name_finish;
351 iface->query_info_async = g_file_real_query_info_async;
352 iface->query_info_finish = g_file_real_query_info_finish;
353 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
354 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
355 iface->set_attributes_async = g_file_real_set_attributes_async;
356 iface->set_attributes_finish = g_file_real_set_attributes_finish;
357 iface->read_async = g_file_real_read_async;
358 iface->read_finish = g_file_real_read_finish;
359 iface->append_to_async = g_file_real_append_to_async;
360 iface->append_to_finish = g_file_real_append_to_finish;
361 iface->create_async = g_file_real_create_async;
362 iface->create_finish = g_file_real_create_finish;
363 iface->replace_async = g_file_real_replace_async;
364 iface->replace_finish = g_file_real_replace_finish;
365 iface->delete_file_async = g_file_real_delete_async;
366 iface->delete_file_finish = g_file_real_delete_finish;
367 iface->trash_async = g_file_real_trash_async;
368 iface->trash_finish = g_file_real_trash_finish;
369 iface->make_directory_async = g_file_real_make_directory_async;
370 iface->make_directory_finish = g_file_real_make_directory_finish;
371 iface->open_readwrite_async = g_file_real_open_readwrite_async;
372 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
373 iface->create_readwrite_async = g_file_real_create_readwrite_async;
374 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
375 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
376 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
377 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
378 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
379 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
380 iface->copy_async = g_file_real_copy_async;
381 iface->copy_finish = g_file_real_copy_finish;
382 iface->measure_disk_usage = g_file_real_measure_disk_usage;
383 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
384 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
390 * @file: input #GFile
392 * Checks to see if a file is native to the platform.
394 * A native file s one expressed in the platform-native filename format,
395 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
396 * as it might be on a locally mounted remote filesystem.
398 * On some systems non-native files may be available using the native
399 * filesystem via a userspace filesystem (FUSE), in these cases this call
400 * will return %FALSE, but g_file_get_path() will still return a native path.
402 * This call does no blocking I/O.
404 * Returns: %TRUE if @file is native
407 g_file_is_native (GFile *file)
411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
413 iface = G_FILE_GET_IFACE (file);
415 return (* iface->is_native) (file);
420 * g_file_has_uri_scheme:
421 * @file: input #GFile
422 * @uri_scheme: a string containing a URI scheme
424 * Checks to see if a #GFile has a given URI scheme.
426 * This call does no blocking I/O.
428 * Returns: %TRUE if #GFile's backend supports the
429 * given URI scheme, %FALSE if URI scheme is %NULL,
430 * not supported, or #GFile is invalid.
433 g_file_has_uri_scheme (GFile *file,
434 const char *uri_scheme)
438 g_return_val_if_fail (G_IS_FILE (file), FALSE);
439 g_return_val_if_fail (uri_scheme != NULL, FALSE);
441 iface = G_FILE_GET_IFACE (file);
443 return (* iface->has_uri_scheme) (file, uri_scheme);
448 * g_file_get_uri_scheme:
449 * @file: input #GFile
451 * Gets the URI scheme for a #GFile.
452 * RFC 3986 decodes the scheme as:
454 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
456 * Common schemes include "file", "http", "ftp", etc.
458 * This call does no blocking I/O.
460 * Returns: a string containing the URI scheme for the given
461 * #GFile. The returned string should be freed with g_free()
462 * when no longer needed.
465 g_file_get_uri_scheme (GFile *file)
469 g_return_val_if_fail (G_IS_FILE (file), NULL);
471 iface = G_FILE_GET_IFACE (file);
473 return (* iface->get_uri_scheme) (file);
478 * g_file_get_basename:
479 * @file: input #GFile
481 * Gets the base name (the last component of the path) for a given #GFile.
483 * If called for the top level of a system (such as the filesystem root
484 * or a uri like sftp://host/) it will return a single directory separator
485 * (and on Windows, possibly a drive letter).
487 * The base name is a byte string (not UTF-8). It has no defined encoding
488 * or rules other than it may not contain zero bytes. If you want to use
489 * filenames in a user interface you should use the display name that you
490 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
491 * attribute with g_file_query_info().
493 * This call does no blocking I/O.
495 * Returns: (nullable): string containing the #GFile's base name, or
496 * %NULL if given #GFile is invalid. The returned string should be
497 * freed with g_free() when no longer needed.
500 g_file_get_basename (GFile *file)
504 g_return_val_if_fail (G_IS_FILE (file), NULL);
506 iface = G_FILE_GET_IFACE (file);
508 return (* iface->get_basename) (file);
513 * @file: input #GFile
515 * Gets the local pathname for #GFile, if one exists.
517 * This call does no blocking I/O.
519 * Returns: (nullable): string containing the #GFile's path, or %NULL
520 * if no such path exists. The returned string should be freed
521 * with g_free() when no longer needed.
524 g_file_get_path (GFile *file)
528 g_return_val_if_fail (G_IS_FILE (file), NULL);
530 iface = G_FILE_GET_IFACE (file);
532 return (* iface->get_path) (file);
537 * @file: input #GFile
539 * Gets the URI for the @file.
541 * This call does no blocking I/O.
543 * Returns: a string containing the #GFile's URI.
544 * The returned string should be freed with g_free()
545 * when no longer needed.
548 g_file_get_uri (GFile *file)
552 g_return_val_if_fail (G_IS_FILE (file), NULL);
554 iface = G_FILE_GET_IFACE (file);
556 return (* iface->get_uri) (file);
560 * g_file_get_parse_name:
561 * @file: input #GFile
563 * Gets the parse name of the @file.
564 * A parse name is a UTF-8 string that describes the
565 * file such that one can get the #GFile back using
566 * g_file_parse_name().
568 * This is generally used to show the #GFile as a nice
569 * full-pathname kind of string in a user interface,
570 * like in a location entry.
572 * For local files with names that can safely be converted
573 * to UTF-8 the pathname is used, otherwise the IRI is used
574 * (a form of URI that allows UTF-8 characters unescaped).
576 * This call does no blocking I/O.
578 * Returns: a string containing the #GFile's parse name.
579 * The returned string should be freed with g_free()
580 * when no longer needed.
583 g_file_get_parse_name (GFile *file)
587 g_return_val_if_fail (G_IS_FILE (file), NULL);
589 iface = G_FILE_GET_IFACE (file);
591 return (* iface->get_parse_name) (file);
596 * @file: input #GFile
598 * Duplicates a #GFile handle. This operation does not duplicate
599 * the actual file or directory represented by the #GFile; see
600 * g_file_copy() if attempting to copy a file.
602 * This call does no blocking I/O.
604 * Returns: (transfer full): a new #GFile that is a duplicate
605 * of the given #GFile.
608 g_file_dup (GFile *file)
612 g_return_val_if_fail (G_IS_FILE (file), NULL);
614 iface = G_FILE_GET_IFACE (file);
616 return (* iface->dup) (file);
621 * @file: (type GFile): #gconstpointer to a #GFile
623 * Creates a hash value for a #GFile.
625 * This call does no blocking I/O.
628 * Returns: 0 if @file is not a valid #GFile, otherwise an
629 * integer that can be used as hash value for the #GFile.
630 * This function is intended for easily hashing a #GFile to
631 * add to a #GHashTable or similar data structure.
634 g_file_hash (gconstpointer file)
638 g_return_val_if_fail (G_IS_FILE (file), 0);
640 iface = G_FILE_GET_IFACE (file);
642 return (* iface->hash) ((GFile *)file);
647 * @file1: the first #GFile
648 * @file2: the second #GFile
650 * Checks if the two given #GFiles refer to the same file.
652 * Note that two #GFiles that differ can still refer to the same
653 * file on the filesystem due to various forms of filename
656 * This call does no blocking I/O.
658 * Returns: %TRUE if @file1 and @file2 are equal.
661 g_file_equal (GFile *file1,
666 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
667 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
672 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
675 iface = G_FILE_GET_IFACE (file1);
677 return (* iface->equal) (file1, file2);
683 * @file: input #GFile
685 * Gets the parent directory for the @file.
686 * If the @file represents the root directory of the
687 * file system, then %NULL will be returned.
689 * This call does no blocking I/O.
691 * Returns: (nullable) (transfer full): a #GFile structure to the
692 * parent of the given #GFile or %NULL if there is no parent. Free
693 * the returned object with g_object_unref().
696 g_file_get_parent (GFile *file)
700 g_return_val_if_fail (G_IS_FILE (file), NULL);
702 iface = G_FILE_GET_IFACE (file);
704 return (* iface->get_parent) (file);
709 * @file: input #GFile
710 * @parent: (allow-none): the parent to check for, or %NULL
712 * Checks if @file has a parent, and optionally, if it is @parent.
714 * If @parent is %NULL then this function returns %TRUE if @file has any
715 * parent at all. If @parent is non-%NULL then %TRUE is only returned
716 * if @file is a child of @parent.
718 * Returns: %TRUE if @file is a child of @parent (or any parent in the
719 * case that @parent is %NULL).
724 g_file_has_parent (GFile *file,
727 GFile *actual_parent;
730 g_return_val_if_fail (G_IS_FILE (file), FALSE);
731 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
733 actual_parent = g_file_get_parent (file);
735 if (actual_parent != NULL)
738 result = g_file_equal (parent, actual_parent);
742 g_object_unref (actual_parent);
752 * @file: input #GFile
753 * @name: string containing the child's basename
755 * Gets a child of @file with basename equal to @name.
757 * Note that the file with that specific name might not exist, but
758 * you can still have a #GFile that points to it. You can use this
759 * for instance to create that file.
761 * This call does no blocking I/O.
763 * Returns: (transfer full): a #GFile to a child specified by @name.
764 * Free the returned object with g_object_unref().
767 g_file_get_child (GFile *file,
770 g_return_val_if_fail (G_IS_FILE (file), NULL);
771 g_return_val_if_fail (name != NULL, NULL);
773 return g_file_resolve_relative_path (file, name);
777 * g_file_get_child_for_display_name:
778 * @file: input #GFile
779 * @display_name: string to a possible child
780 * @error: return location for an error
782 * Gets the child of @file for a given @display_name (i.e. a UTF-8
783 * version of the name). If this function fails, it returns %NULL
784 * and @error will be set. This is very useful when constructing a
785 * #GFile for a new file and the user entered the filename in the
786 * user interface, for instance when you select a directory and
787 * type a filename in the file selector.
789 * This call does no blocking I/O.
791 * Returns: (transfer full): a #GFile to the specified child, or
792 * %NULL if the display name couldn't be converted.
793 * Free the returned object with g_object_unref().
796 g_file_get_child_for_display_name (GFile *file,
797 const char *display_name,
802 g_return_val_if_fail (G_IS_FILE (file), NULL);
803 g_return_val_if_fail (display_name != NULL, NULL);
805 iface = G_FILE_GET_IFACE (file);
807 return (* iface->get_child_for_display_name) (file, display_name, error);
812 * @file: input #GFile
813 * @prefix: input #GFile
815 * Checks whether @file has the prefix specified by @prefix.
817 * In other words, if the names of initial elements of @file's
818 * pathname match @prefix. Only full pathname elements are matched,
819 * so a path like /foo is not considered a prefix of /foobar, only
822 * A #GFile is not a prefix of itself. If you want to check for
823 * equality, use g_file_equal().
825 * This call does no I/O, as it works purely on names. As such it can
826 * sometimes return %FALSE even if @file is inside a @prefix (from a
827 * filesystem point of view), because the prefix of @file is an alias
830 * Virtual: prefix_matches
831 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
835 g_file_has_prefix (GFile *file,
840 g_return_val_if_fail (G_IS_FILE (file), FALSE);
841 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
843 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
846 iface = G_FILE_GET_IFACE (file);
848 /* The vtable function differs in arg order since
849 * we're using the old contains_file call
851 return (* iface->prefix_matches) (prefix, file);
855 * g_file_get_relative_path:
856 * @parent: input #GFile
857 * @descendant: input #GFile
859 * Gets the path for @descendant relative to @parent.
861 * This call does no blocking I/O.
863 * Returns: (nullable): string with the relative path from @descendant
864 * to @parent, or %NULL if @descendant doesn't have @parent as
865 * prefix. The returned string should be freed with g_free() when
869 g_file_get_relative_path (GFile *parent,
874 g_return_val_if_fail (G_IS_FILE (parent), NULL);
875 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
877 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
880 iface = G_FILE_GET_IFACE (parent);
882 return (* iface->get_relative_path) (parent, descendant);
886 * g_file_resolve_relative_path:
887 * @file: input #GFile
888 * @relative_path: a given relative path string
890 * Resolves a relative path for @file to an absolute path.
892 * This call does no blocking I/O.
894 * Returns: (transfer full): #GFile to the resolved path.
895 * %NULL if @relative_path is %NULL or if @file is invalid.
896 * Free the returned object with g_object_unref().
899 g_file_resolve_relative_path (GFile *file,
900 const char *relative_path)
904 g_return_val_if_fail (G_IS_FILE (file), NULL);
905 g_return_val_if_fail (relative_path != NULL, NULL);
907 iface = G_FILE_GET_IFACE (file);
909 return (* iface->resolve_relative_path) (file, relative_path);
913 * g_file_enumerate_children:
914 * @file: input #GFile
915 * @attributes: an attribute query string
916 * @flags: a set of #GFileQueryInfoFlags
917 * @cancellable: (allow-none): optional #GCancellable object,
919 * @error: #GError for error reporting
921 * Gets the requested information about the files in a directory.
922 * The result is a #GFileEnumerator object that will give out
923 * #GFileInfo objects for all the files in the directory.
925 * The @attributes value is a string that specifies the file
926 * attributes that should be gathered. It is not an error if
927 * it's not possible to read a particular requested attribute
928 * from a file - it just won't be set. @attributes should
929 * be a comma-separated list of attributes or attribute wildcards.
930 * The wildcard "*" means all attributes, and a wildcard like
931 * "standard::*" means all attributes in the standard namespace.
932 * An example attribute query be "standard::*,owner::user".
933 * The standard attributes are available as defines, like
934 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
936 * If @cancellable is not %NULL, then the operation can be cancelled
937 * by triggering the cancellable object from another thread. If the
938 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
941 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
942 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
943 * error will be returned. Other errors are possible too.
945 * Returns: (transfer full): A #GFileEnumerator if successful,
946 * %NULL on error. Free the returned object with g_object_unref().
949 g_file_enumerate_children (GFile *file,
950 const char *attributes,
951 GFileQueryInfoFlags flags,
952 GCancellable *cancellable,
957 g_return_val_if_fail (G_IS_FILE (file), NULL);
959 if (g_cancellable_set_error_if_cancelled (cancellable, error))
962 iface = G_FILE_GET_IFACE (file);
964 if (iface->enumerate_children == NULL)
966 g_set_error_literal (error, G_IO_ERROR,
967 G_IO_ERROR_NOT_SUPPORTED,
968 _("Operation not supported"));
972 return (* iface->enumerate_children) (file, attributes, flags,
977 * g_file_enumerate_children_async:
978 * @file: input #GFile
979 * @attributes: an attribute query string
980 * @flags: a set of #GFileQueryInfoFlags
981 * @io_priority: the [I/O priority][io-priority] of the request
982 * @cancellable: (allow-none): optional #GCancellable object,
984 * @callback: (scope async): a #GAsyncReadyCallback to call when the
985 * request is satisfied
986 * @user_data: (closure): the data to pass to callback function
988 * Asynchronously gets the requested information about the files
989 * in a directory. The result is a #GFileEnumerator object that will
990 * give out #GFileInfo objects for all the files in the directory.
992 * For more details, see g_file_enumerate_children() which is
993 * the synchronous version of this call.
995 * When the operation is finished, @callback will be called. You can
996 * then call g_file_enumerate_children_finish() to get the result of
1000 g_file_enumerate_children_async (GFile *file,
1001 const char *attributes,
1002 GFileQueryInfoFlags flags,
1004 GCancellable *cancellable,
1005 GAsyncReadyCallback callback,
1010 g_return_if_fail (G_IS_FILE (file));
1012 iface = G_FILE_GET_IFACE (file);
1013 (* iface->enumerate_children_async) (file,
1023 * g_file_enumerate_children_finish:
1024 * @file: input #GFile
1025 * @res: a #GAsyncResult
1028 * Finishes an async enumerate children operation.
1029 * See g_file_enumerate_children_async().
1031 * Returns: (transfer full): a #GFileEnumerator or %NULL
1032 * if an error occurred.
1033 * Free the returned object with g_object_unref().
1036 g_file_enumerate_children_finish (GFile *file,
1042 g_return_val_if_fail (G_IS_FILE (file), NULL);
1043 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1045 if (g_async_result_legacy_propagate_error (res, error))
1048 iface = G_FILE_GET_IFACE (file);
1049 return (* iface->enumerate_children_finish) (file, res, error);
1053 * g_file_query_exists:
1054 * @file: input #GFile
1055 * @cancellable: (allow-none): optional #GCancellable object,
1058 * Utility function to check if a particular file exists. This is
1059 * implemented using g_file_query_info() and as such does blocking I/O.
1061 * Note that in many cases it is racy to first check for file existence
1062 * and then execute something based on the outcome of that, because the
1063 * file might have been created or removed in between the operations. The
1064 * general approach to handling that is to not check, but just do the
1065 * operation and handle the errors as they come.
1067 * As an example of race-free checking, take the case of reading a file,
1068 * and if it doesn't exist, creating it. There are two racy versions: read
1069 * it, and on error create it; and: check if it exists, if not create it.
1070 * These can both result in two processes creating the file (with perhaps
1071 * a partially written file as the result). The correct approach is to
1072 * always try to create the file with g_file_create() which will either
1073 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1075 * However, in many cases an existence check is useful in a user interface,
1076 * for instance to make a menu item sensitive/insensitive, so that you don't
1077 * have to fool users that something is possible and then just show an error
1078 * dialog. If you do this, you should make sure to also handle the errors
1079 * that can happen due to races when you execute the operation.
1081 * Returns: %TRUE if the file exists (and can be detected without error),
1082 * %FALSE otherwise (or if cancelled).
1085 g_file_query_exists (GFile *file,
1086 GCancellable *cancellable)
1090 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1092 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1093 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1096 g_object_unref (info);
1104 * g_file_query_file_type:
1105 * @file: input #GFile
1106 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1107 * @cancellable: (allow-none): optional #GCancellable object,
1110 * Utility function to inspect the #GFileType of a file. This is
1111 * implemented using g_file_query_info() and as such does blocking I/O.
1113 * The primary use case of this method is to check if a file is
1114 * a regular file, directory, or symlink.
1116 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1117 * if the file does not exist
1122 g_file_query_file_type (GFile *file,
1123 GFileQueryInfoFlags flags,
1124 GCancellable *cancellable)
1127 GFileType file_type;
1129 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1130 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1134 file_type = g_file_info_get_file_type (info);
1135 g_object_unref (info);
1138 file_type = G_FILE_TYPE_UNKNOWN;
1144 * g_file_query_info:
1145 * @file: input #GFile
1146 * @attributes: an attribute query string
1147 * @flags: a set of #GFileQueryInfoFlags
1148 * @cancellable: (allow-none): optional #GCancellable object,
1152 * Gets the requested information about specified @file.
1153 * The result is a #GFileInfo object that contains key-value
1154 * attributes (such as the type or size of the file).
1156 * The @attributes value is a string that specifies the file
1157 * attributes that should be gathered. It is not an error if
1158 * it's not possible to read a particular requested attribute
1159 * from a file - it just won't be set. @attributes should be a
1160 * comma-separated list of attributes or attribute wildcards.
1161 * The wildcard "*" means all attributes, and a wildcard like
1162 * "standard::*" means all attributes in the standard namespace.
1163 * An example attribute query be "standard::*,owner::user".
1164 * The standard attributes are available as defines, like
1165 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1167 * If @cancellable is not %NULL, then the operation can be cancelled
1168 * by triggering the cancellable object from another thread. If the
1169 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1172 * For symlinks, normally the information about the target of the
1173 * symlink is returned, rather than information about the symlink
1174 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1175 * in @flags the information about the symlink itself will be returned.
1176 * Also, for symlinks that point to non-existing files the information
1177 * about the symlink itself will be returned.
1179 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1180 * returned. Other errors are possible too, and depend on what kind of
1181 * filesystem the file is on.
1183 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1184 * on error. Free the returned object with g_object_unref().
1187 g_file_query_info (GFile *file,
1188 const char *attributes,
1189 GFileQueryInfoFlags flags,
1190 GCancellable *cancellable,
1195 g_return_val_if_fail (G_IS_FILE (file), NULL);
1197 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1200 iface = G_FILE_GET_IFACE (file);
1202 if (iface->query_info == NULL)
1204 g_set_error_literal (error, G_IO_ERROR,
1205 G_IO_ERROR_NOT_SUPPORTED,
1206 _("Operation not supported"));
1210 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1214 * g_file_query_info_async:
1215 * @file: input #GFile
1216 * @attributes: an attribute query string
1217 * @flags: a set of #GFileQueryInfoFlags
1218 * @io_priority: the [I/O priority][io-priority] of the request
1219 * @cancellable: (allow-none): optional #GCancellable object,
1221 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1222 * request is satisfied
1223 * @user_data: (closure): the data to pass to callback function
1225 * Asynchronously gets the requested information about specified @file.
1226 * The result is a #GFileInfo object that contains key-value attributes
1227 * (such as type or size for the file).
1229 * For more details, see g_file_query_info() which is the synchronous
1230 * version of this call.
1232 * When the operation is finished, @callback will be called. You can
1233 * then call g_file_query_info_finish() to get the result of the operation.
1236 g_file_query_info_async (GFile *file,
1237 const char *attributes,
1238 GFileQueryInfoFlags flags,
1240 GCancellable *cancellable,
1241 GAsyncReadyCallback callback,
1246 g_return_if_fail (G_IS_FILE (file));
1248 iface = G_FILE_GET_IFACE (file);
1249 (* iface->query_info_async) (file,
1259 * g_file_query_info_finish:
1260 * @file: input #GFile
1261 * @res: a #GAsyncResult
1264 * Finishes an asynchronous file info query.
1265 * See g_file_query_info_async().
1267 * Returns: (transfer full): #GFileInfo for given @file
1268 * or %NULL on error. Free the returned object with
1272 g_file_query_info_finish (GFile *file,
1278 g_return_val_if_fail (G_IS_FILE (file), NULL);
1279 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1281 if (g_async_result_legacy_propagate_error (res, error))
1284 iface = G_FILE_GET_IFACE (file);
1285 return (* iface->query_info_finish) (file, res, error);
1289 * g_file_query_filesystem_info:
1290 * @file: input #GFile
1291 * @attributes: an attribute query string
1292 * @cancellable: (allow-none): optional #GCancellable object,
1296 * Similar to g_file_query_info(), but obtains information
1297 * about the filesystem the @file is on, rather than the file itself.
1298 * For instance the amount of space available and the type of
1301 * The @attributes value is a string that specifies the attributes
1302 * that should be gathered. It is not an error if it's not possible
1303 * to read a particular requested attribute from a file - it just
1304 * won't be set. @attributes should be a comma-separated list of
1305 * attributes or attribute wildcards. The wildcard "*" means all
1306 * attributes, and a wildcard like "filesystem::*" means all attributes
1307 * in the filesystem namespace. The standard namespace for filesystem
1308 * attributes is "filesystem". Common attributes of interest are
1309 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1310 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1311 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1313 * If @cancellable is not %NULL, then the operation can be cancelled
1314 * by triggering the cancellable object from another thread. If the
1315 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1318 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1319 * be returned. Other errors are possible too, and depend on what
1320 * kind of filesystem the file is on.
1322 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1323 * Free the returned object with g_object_unref().
1326 g_file_query_filesystem_info (GFile *file,
1327 const char *attributes,
1328 GCancellable *cancellable,
1333 g_return_val_if_fail (G_IS_FILE (file), NULL);
1335 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1338 iface = G_FILE_GET_IFACE (file);
1340 if (iface->query_filesystem_info == NULL)
1342 g_set_error_literal (error, G_IO_ERROR,
1343 G_IO_ERROR_NOT_SUPPORTED,
1344 _("Operation not supported"));
1348 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1352 * g_file_query_filesystem_info_async:
1353 * @file: input #GFile
1354 * @attributes: an attribute query string
1355 * @io_priority: the [I/O priority][io-priority] of the request
1356 * @cancellable: (allow-none): optional #GCancellable object,
1358 * @callback: (scope async): a #GAsyncReadyCallback to call
1359 * when the request is satisfied
1360 * @user_data: (closure): the data to pass to callback function
1362 * Asynchronously gets the requested information about the filesystem
1363 * that the specified @file is on. The result is a #GFileInfo object
1364 * that contains key-value attributes (such as type or size for the
1367 * For more details, see g_file_query_filesystem_info() which is the
1368 * synchronous version of this call.
1370 * When the operation is finished, @callback will be called. You can
1371 * then call g_file_query_info_finish() to get the result of the
1375 g_file_query_filesystem_info_async (GFile *file,
1376 const char *attributes,
1378 GCancellable *cancellable,
1379 GAsyncReadyCallback callback,
1384 g_return_if_fail (G_IS_FILE (file));
1386 iface = G_FILE_GET_IFACE (file);
1387 (* iface->query_filesystem_info_async) (file,
1396 * g_file_query_filesystem_info_finish:
1397 * @file: input #GFile
1398 * @res: a #GAsyncResult
1401 * Finishes an asynchronous filesystem info query.
1402 * See g_file_query_filesystem_info_async().
1404 * Returns: (transfer full): #GFileInfo for given @file
1405 * or %NULL on error.
1406 * Free the returned object with g_object_unref().
1409 g_file_query_filesystem_info_finish (GFile *file,
1415 g_return_val_if_fail (G_IS_FILE (file), NULL);
1416 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1418 if (g_async_result_legacy_propagate_error (res, error))
1421 iface = G_FILE_GET_IFACE (file);
1422 return (* iface->query_filesystem_info_finish) (file, res, error);
1426 * g_file_find_enclosing_mount:
1427 * @file: input #GFile
1428 * @cancellable: (allow-none): optional #GCancellable object,
1432 * Gets a #GMount for the #GFile.
1434 * If the #GFileIface for @file does not have a mount (e.g.
1435 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1436 * and %NULL will be returned.
1438 * If @cancellable is not %NULL, then the operation can be cancelled by
1439 * triggering the cancellable object from another thread. If the operation
1440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1442 * Returns: (transfer full): a #GMount where the @file is located
1443 * or %NULL on error.
1444 * Free the returned object with g_object_unref().
1447 g_file_find_enclosing_mount (GFile *file,
1448 GCancellable *cancellable,
1453 g_return_val_if_fail (G_IS_FILE (file), NULL);
1455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1458 iface = G_FILE_GET_IFACE (file);
1459 if (iface->find_enclosing_mount == NULL)
1462 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1463 /* Translators: This is an error message when
1464 * trying to find the enclosing (user visible)
1465 * mount of a file, but none exists.
1467 _("Containing mount does not exist"));
1471 return (* iface->find_enclosing_mount) (file, cancellable, error);
1475 * g_file_find_enclosing_mount_async:
1477 * @io_priority: the [I/O priority][io-priority] of the request
1478 * @cancellable: (allow-none): optional #GCancellable object,
1480 * @callback: (scope async): a #GAsyncReadyCallback to call
1481 * when the request is satisfied
1482 * @user_data: (closure): the data to pass to callback function
1484 * Asynchronously gets the mount for the file.
1486 * For more details, see g_file_find_enclosing_mount() which is
1487 * the synchronous version of this call.
1489 * When the operation is finished, @callback will be called.
1490 * You can then call g_file_find_enclosing_mount_finish() to
1491 * get the result of the operation.
1494 g_file_find_enclosing_mount_async (GFile *file,
1496 GCancellable *cancellable,
1497 GAsyncReadyCallback callback,
1502 g_return_if_fail (G_IS_FILE (file));
1504 iface = G_FILE_GET_IFACE (file);
1505 (* iface->find_enclosing_mount_async) (file,
1513 * g_file_find_enclosing_mount_finish:
1515 * @res: a #GAsyncResult
1518 * Finishes an asynchronous find mount request.
1519 * See g_file_find_enclosing_mount_async().
1521 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1522 * Free the returned object with g_object_unref().
1525 g_file_find_enclosing_mount_finish (GFile *file,
1531 g_return_val_if_fail (G_IS_FILE (file), NULL);
1532 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1534 if (g_async_result_legacy_propagate_error (res, error))
1537 iface = G_FILE_GET_IFACE (file);
1538 return (* iface->find_enclosing_mount_finish) (file, res, error);
1544 * @file: #GFile to read
1545 * @cancellable: (allow-none): a #GCancellable
1546 * @error: a #GError, or %NULL
1548 * Opens a file for reading. The result is a #GFileInputStream that
1549 * can be used to read the contents of the file.
1551 * If @cancellable is not %NULL, then the operation can be cancelled by
1552 * triggering the cancellable object from another thread. If the operation
1553 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1555 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1556 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1557 * error will be returned. Other errors are possible too, and depend
1558 * on what kind of filesystem the file is on.
1561 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1562 * Free the returned object with g_object_unref().
1565 g_file_read (GFile *file,
1566 GCancellable *cancellable,
1571 g_return_val_if_fail (G_IS_FILE (file), NULL);
1573 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1576 iface = G_FILE_GET_IFACE (file);
1578 if (iface->read_fn == NULL)
1580 g_set_error_literal (error, G_IO_ERROR,
1581 G_IO_ERROR_NOT_SUPPORTED,
1582 _("Operation not supported"));
1586 return (* iface->read_fn) (file, cancellable, error);
1591 * @file: input #GFile
1592 * @flags: a set of #GFileCreateFlags
1593 * @cancellable: (allow-none): optional #GCancellable object,
1595 * @error: a #GError, or %NULL
1597 * Gets an output stream for appending data to the file.
1598 * If the file doesn't already exist it is created.
1600 * By default files created are generally readable by everyone,
1601 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1602 * will be made readable only to the current user, to the level that
1603 * is supported on the target filesystem.
1605 * If @cancellable is not %NULL, then the operation can be cancelled
1606 * by triggering the cancellable object from another thread. If the
1607 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1610 * Some file systems don't allow all file names, and may return an
1611 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1612 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1613 * possible too, and depend on what kind of filesystem the file is on.
1615 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1616 * Free the returned object with g_object_unref().
1619 g_file_append_to (GFile *file,
1620 GFileCreateFlags flags,
1621 GCancellable *cancellable,
1626 g_return_val_if_fail (G_IS_FILE (file), NULL);
1628 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1631 iface = G_FILE_GET_IFACE (file);
1633 if (iface->append_to == NULL)
1635 g_set_error_literal (error, G_IO_ERROR,
1636 G_IO_ERROR_NOT_SUPPORTED,
1637 _("Operation not supported"));
1641 return (* iface->append_to) (file, flags, cancellable, error);
1646 * @file: input #GFile
1647 * @flags: a set of #GFileCreateFlags
1648 * @cancellable: (allow-none): optional #GCancellable object,
1650 * @error: a #GError, or %NULL
1652 * Creates a new file and returns an output stream for writing to it.
1653 * The file must not already exist.
1655 * By default files created are generally readable by everyone,
1656 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1657 * will be made readable only to the current user, to the level
1658 * that is supported on the target filesystem.
1660 * If @cancellable is not %NULL, then the operation can be cancelled
1661 * by triggering the cancellable object from another thread. If the
1662 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1665 * If a file or directory with this name already exists the
1666 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1667 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1668 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1669 * be returned. Other errors are possible too, and depend on what kind
1670 * of filesystem the file is on.
1672 * Returns: (transfer full): a #GFileOutputStream for the newly created
1673 * file, or %NULL on error.
1674 * Free the returned object with g_object_unref().
1677 g_file_create (GFile *file,
1678 GFileCreateFlags flags,
1679 GCancellable *cancellable,
1684 g_return_val_if_fail (G_IS_FILE (file), NULL);
1686 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1689 iface = G_FILE_GET_IFACE (file);
1691 if (iface->create == NULL)
1693 g_set_error_literal (error, G_IO_ERROR,
1694 G_IO_ERROR_NOT_SUPPORTED,
1695 _("Operation not supported"));
1699 return (* iface->create) (file, flags, cancellable, error);
1704 * @file: input #GFile
1705 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1706 * for the current #GFile, or #NULL to ignore
1707 * @make_backup: %TRUE if a backup should be created
1708 * @flags: a set of #GFileCreateFlags
1709 * @cancellable: (allow-none): optional #GCancellable object,
1711 * @error: a #GError, or %NULL
1713 * Returns an output stream for overwriting the file, possibly
1714 * creating a backup copy of the file first. If the file doesn't exist,
1715 * it will be created.
1717 * This will try to replace the file in the safest way possible so
1718 * that any errors during the writing will not affect an already
1719 * existing copy of the file. For instance, for local files it
1720 * may write to a temporary file and then atomically rename over
1721 * the destination when the stream is closed.
1723 * By default files created are generally readable by everyone,
1724 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1725 * will be made readable only to the current user, to the level that
1726 * is supported on the target filesystem.
1728 * If @cancellable is not %NULL, then the operation can be cancelled
1729 * by triggering the cancellable object from another thread. If the
1730 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1733 * If you pass in a non-%NULL @etag value and @file already exists, then
1734 * this value is compared to the current entity tag of the file, and if
1735 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1736 * generally means that the file has been changed since you last read
1737 * it. You can get the new etag from g_file_output_stream_get_etag()
1738 * after you've finished writing and closed the #GFileOutputStream. When
1739 * you load a new file you can use g_file_input_stream_query_info() to
1740 * get the etag of the file.
1742 * If @make_backup is %TRUE, this function will attempt to make a
1743 * backup of the current file before overwriting it. If this fails
1744 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1745 * want to replace anyway, try again with @make_backup set to %FALSE.
1747 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1748 * be returned, and if the file is some other form of non-regular file
1749 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1750 * file systems don't allow all file names, and may return an
1751 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1752 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1753 * possible too, and depend on what kind of filesystem the file is on.
1755 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1756 * Free the returned object with g_object_unref().
1759 g_file_replace (GFile *file,
1761 gboolean make_backup,
1762 GFileCreateFlags flags,
1763 GCancellable *cancellable,
1768 g_return_val_if_fail (G_IS_FILE (file), NULL);
1770 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1773 iface = G_FILE_GET_IFACE (file);
1775 if (iface->replace == NULL)
1777 g_set_error_literal (error, G_IO_ERROR,
1778 G_IO_ERROR_NOT_SUPPORTED,
1779 _("Operation not supported"));
1783 /* Handle empty tag string as NULL in consistent way. */
1784 if (etag && *etag == 0)
1787 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1791 * g_file_open_readwrite:
1792 * @file: #GFile to open
1793 * @cancellable: (allow-none): a #GCancellable
1794 * @error: a #GError, or %NULL
1796 * Opens an existing file for reading and writing. The result is
1797 * a #GFileIOStream that can be used to read and write the contents
1800 * If @cancellable is not %NULL, then the operation can be cancelled
1801 * by triggering the cancellable object from another thread. If the
1802 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1805 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1806 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1807 * error will be returned. Other errors are possible too, and depend on
1808 * what kind of filesystem the file is on. Note that in many non-local
1809 * file cases read and write streams are not supported, so make sure you
1810 * really need to do read and write streaming, rather than just opening
1811 * for reading or writing.
1813 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1814 * Free the returned object with g_object_unref().
1819 g_file_open_readwrite (GFile *file,
1820 GCancellable *cancellable,
1825 g_return_val_if_fail (G_IS_FILE (file), NULL);
1827 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1830 iface = G_FILE_GET_IFACE (file);
1832 if (iface->open_readwrite == NULL)
1834 g_set_error_literal (error, G_IO_ERROR,
1835 G_IO_ERROR_NOT_SUPPORTED,
1836 _("Operation not supported"));
1840 return (* iface->open_readwrite) (file, cancellable, error);
1844 * g_file_create_readwrite:
1846 * @flags: a set of #GFileCreateFlags
1847 * @cancellable: (allow-none): optional #GCancellable object,
1849 * @error: return location for a #GError, or %NULL
1851 * Creates a new file and returns a stream for reading and
1852 * writing to it. The file must not already exist.
1854 * By default files created are generally readable by everyone,
1855 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1856 * will be made readable only to the current user, to the level
1857 * that is supported on the target filesystem.
1859 * If @cancellable is not %NULL, then the operation can be cancelled
1860 * by triggering the cancellable object from another thread. If the
1861 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1864 * If a file or directory with this name already exists, the
1865 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1866 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1867 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1868 * will be returned. Other errors are possible too, and depend on what
1869 * kind of filesystem the file is on.
1871 * Note that in many non-local file cases read and write streams are
1872 * not supported, so make sure you really need to do read and write
1873 * streaming, rather than just opening for reading or writing.
1875 * Returns: (transfer full): a #GFileIOStream for the newly created
1876 * file, or %NULL on error.
1877 * Free the returned object with g_object_unref().
1882 g_file_create_readwrite (GFile *file,
1883 GFileCreateFlags flags,
1884 GCancellable *cancellable,
1889 g_return_val_if_fail (G_IS_FILE (file), NULL);
1891 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1894 iface = G_FILE_GET_IFACE (file);
1896 if (iface->create_readwrite == NULL)
1898 g_set_error_literal (error, G_IO_ERROR,
1899 G_IO_ERROR_NOT_SUPPORTED,
1900 _("Operation not supported"));
1904 return (* iface->create_readwrite) (file, flags, cancellable, error);
1908 * g_file_replace_readwrite:
1910 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1911 * for the current #GFile, or #NULL to ignore
1912 * @make_backup: %TRUE if a backup should be created
1913 * @flags: a set of #GFileCreateFlags
1914 * @cancellable: (allow-none): optional #GCancellable object,
1916 * @error: return location for a #GError, or %NULL
1918 * Returns an output stream for overwriting the file in readwrite mode,
1919 * possibly creating a backup copy of the file first. If the file doesn't
1920 * exist, it will be created.
1922 * For details about the behaviour, see g_file_replace() which does the
1923 * same thing but returns an output stream only.
1925 * Note that in many non-local file cases read and write streams are not
1926 * supported, so make sure you really need to do read and write streaming,
1927 * rather than just opening for reading or writing.
1929 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1930 * Free the returned object with g_object_unref().
1935 g_file_replace_readwrite (GFile *file,
1937 gboolean make_backup,
1938 GFileCreateFlags flags,
1939 GCancellable *cancellable,
1944 g_return_val_if_fail (G_IS_FILE (file), NULL);
1946 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1949 iface = G_FILE_GET_IFACE (file);
1951 if (iface->replace_readwrite == NULL)
1953 g_set_error_literal (error, G_IO_ERROR,
1954 G_IO_ERROR_NOT_SUPPORTED,
1955 _("Operation not supported"));
1959 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1963 * g_file_read_async:
1964 * @file: input #GFile
1965 * @io_priority: the [I/O priority][io-priority] of the request
1966 * @cancellable: (allow-none): optional #GCancellable object,
1968 * @callback: (scope async): a #GAsyncReadyCallback to call
1969 * when the request is satisfied
1970 * @user_data: (closure): the data to pass to callback function
1972 * Asynchronously opens @file for reading.
1974 * For more details, see g_file_read() which is
1975 * the synchronous version of this call.
1977 * When the operation is finished, @callback will be called.
1978 * You can then call g_file_read_finish() to get the result
1982 g_file_read_async (GFile *file,
1984 GCancellable *cancellable,
1985 GAsyncReadyCallback callback,
1990 g_return_if_fail (G_IS_FILE (file));
1992 iface = G_FILE_GET_IFACE (file);
1993 (* iface->read_async) (file,
2001 * g_file_read_finish:
2002 * @file: input #GFile
2003 * @res: a #GAsyncResult
2004 * @error: a #GError, or %NULL
2006 * Finishes an asynchronous file read operation started with
2007 * g_file_read_async().
2009 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2010 * Free the returned object with g_object_unref().
2013 g_file_read_finish (GFile *file,
2019 g_return_val_if_fail (G_IS_FILE (file), NULL);
2020 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2022 if (g_async_result_legacy_propagate_error (res, error))
2025 iface = G_FILE_GET_IFACE (file);
2026 return (* iface->read_finish) (file, res, error);
2030 * g_file_append_to_async:
2031 * @file: input #GFile
2032 * @flags: a set of #GFileCreateFlags
2033 * @io_priority: the [I/O priority][io-priority] of the request
2034 * @cancellable: (allow-none): optional #GCancellable object,
2036 * @callback: (scope async): a #GAsyncReadyCallback to call
2037 * when the request is satisfied
2038 * @user_data: (closure): the data to pass to callback function
2040 * Asynchronously opens @file for appending.
2042 * For more details, see g_file_append_to() which is
2043 * the synchronous version of this call.
2045 * When the operation is finished, @callback will be called.
2046 * You can then call g_file_append_to_finish() to get the result
2050 g_file_append_to_async (GFile *file,
2051 GFileCreateFlags flags,
2053 GCancellable *cancellable,
2054 GAsyncReadyCallback callback,
2059 g_return_if_fail (G_IS_FILE (file));
2061 iface = G_FILE_GET_IFACE (file);
2062 (* iface->append_to_async) (file,
2071 * g_file_append_to_finish:
2072 * @file: input #GFile
2073 * @res: #GAsyncResult
2074 * @error: a #GError, or %NULL
2076 * Finishes an asynchronous file append operation started with
2077 * g_file_append_to_async().
2079 * Returns: (transfer full): a valid #GFileOutputStream
2080 * or %NULL on error.
2081 * Free the returned object with g_object_unref().
2084 g_file_append_to_finish (GFile *file,
2090 g_return_val_if_fail (G_IS_FILE (file), NULL);
2091 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2093 if (g_async_result_legacy_propagate_error (res, error))
2096 iface = G_FILE_GET_IFACE (file);
2097 return (* iface->append_to_finish) (file, res, error);
2101 * g_file_create_async:
2102 * @file: input #GFile
2103 * @flags: a set of #GFileCreateFlags
2104 * @io_priority: the [I/O priority][io-priority] of the request
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 [entity tag][gfile-etag] for the current #GFile,
2175 * or %NULL to ignore
2176 * @make_backup: %TRUE if a backup should be created
2177 * @flags: a set of #GFileCreateFlags
2178 * @io_priority: the [I/O priority][io-priority] of the request
2179 * @cancellable: (allow-none): optional #GCancellable object,
2181 * @callback: (scope async): a #GAsyncReadyCallback to call
2182 * when the request is satisfied
2183 * @user_data: (closure): the data to pass to callback function
2185 * Asynchronously overwrites the file, replacing the contents,
2186 * possibly creating a backup copy of the file first.
2188 * For more details, see g_file_replace() which is
2189 * the synchronous version of this call.
2191 * When the operation is finished, @callback will be called.
2192 * You can then call g_file_replace_finish() to get the result
2196 g_file_replace_async (GFile *file,
2198 gboolean make_backup,
2199 GFileCreateFlags flags,
2201 GCancellable *cancellable,
2202 GAsyncReadyCallback callback,
2207 g_return_if_fail (G_IS_FILE (file));
2209 iface = G_FILE_GET_IFACE (file);
2210 (* iface->replace_async) (file,
2221 * g_file_replace_finish:
2222 * @file: input #GFile
2223 * @res: a #GAsyncResult
2224 * @error: a #GError, or %NULL
2226 * Finishes an asynchronous file replace operation started with
2227 * g_file_replace_async().
2229 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2230 * Free the returned object with g_object_unref().
2233 g_file_replace_finish (GFile *file,
2239 g_return_val_if_fail (G_IS_FILE (file), NULL);
2240 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2242 if (g_async_result_legacy_propagate_error (res, error))
2245 iface = G_FILE_GET_IFACE (file);
2246 return (* iface->replace_finish) (file, res, error);
2250 * g_file_open_readwrite_async
2251 * @file: input #GFile
2252 * @io_priority: the [I/O priority][io-priority] of the request
2253 * @cancellable: (allow-none): optional #GCancellable object,
2255 * @callback: (scope async): a #GAsyncReadyCallback to call
2256 * when the request is satisfied
2257 * @user_data: (closure): the data to pass to callback function
2259 * Asynchronously opens @file for reading and writing.
2261 * For more details, see g_file_open_readwrite() which is
2262 * the synchronous version of this call.
2264 * When the operation is finished, @callback will be called.
2265 * You can then call g_file_open_readwrite_finish() to get
2266 * the result of the operation.
2271 g_file_open_readwrite_async (GFile *file,
2273 GCancellable *cancellable,
2274 GAsyncReadyCallback callback,
2279 g_return_if_fail (G_IS_FILE (file));
2281 iface = G_FILE_GET_IFACE (file);
2282 (* iface->open_readwrite_async) (file,
2290 * g_file_open_readwrite_finish:
2291 * @file: input #GFile
2292 * @res: a #GAsyncResult
2293 * @error: a #GError, or %NULL
2295 * Finishes an asynchronous file read operation started with
2296 * g_file_open_readwrite_async().
2298 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2299 * Free the returned object with g_object_unref().
2304 g_file_open_readwrite_finish (GFile *file,
2310 g_return_val_if_fail (G_IS_FILE (file), NULL);
2311 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2313 if (g_async_result_legacy_propagate_error (res, error))
2316 iface = G_FILE_GET_IFACE (file);
2317 return (* iface->open_readwrite_finish) (file, res, error);
2321 * g_file_create_readwrite_async:
2322 * @file: input #GFile
2323 * @flags: a set of #GFileCreateFlags
2324 * @io_priority: the [I/O priority][io-priority] of the request
2325 * @cancellable: (allow-none): optional #GCancellable object,
2327 * @callback: (scope async): a #GAsyncReadyCallback to call
2328 * when the request is satisfied
2329 * @user_data: (closure): the data to pass to callback function
2331 * Asynchronously creates a new file and returns a stream
2332 * for reading and writing to it. The file must not already exist.
2334 * For more details, see g_file_create_readwrite() which is
2335 * the synchronous version of this call.
2337 * When the operation is finished, @callback will be called.
2338 * You can then call g_file_create_readwrite_finish() to get
2339 * the result of the operation.
2344 g_file_create_readwrite_async (GFile *file,
2345 GFileCreateFlags flags,
2347 GCancellable *cancellable,
2348 GAsyncReadyCallback callback,
2353 g_return_if_fail (G_IS_FILE (file));
2355 iface = G_FILE_GET_IFACE (file);
2356 (* iface->create_readwrite_async) (file,
2365 * g_file_create_readwrite_finish:
2366 * @file: input #GFile
2367 * @res: a #GAsyncResult
2368 * @error: a #GError, or %NULL
2370 * Finishes an asynchronous file create operation started with
2371 * g_file_create_readwrite_async().
2373 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2374 * Free the returned object with g_object_unref().
2379 g_file_create_readwrite_finish (GFile *file,
2385 g_return_val_if_fail (G_IS_FILE (file), NULL);
2386 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2388 if (g_async_result_legacy_propagate_error (res, error))
2391 iface = G_FILE_GET_IFACE (file);
2392 return (* iface->create_readwrite_finish) (file, res, error);
2396 * g_file_replace_readwrite_async:
2397 * @file: input #GFile
2398 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2399 * or %NULL to ignore
2400 * @make_backup: %TRUE if a backup should be created
2401 * @flags: a set of #GFileCreateFlags
2402 * @io_priority: the [I/O priority][io-priority] of the request
2403 * @cancellable: (allow-none): optional #GCancellable object,
2405 * @callback: (scope async): a #GAsyncReadyCallback to call
2406 * when the request is satisfied
2407 * @user_data: (closure): the data to pass to callback function
2409 * Asynchronously overwrites the file in read-write mode,
2410 * replacing the contents, possibly creating a backup copy
2411 * of the file first.
2413 * For more details, see g_file_replace_readwrite() which is
2414 * the synchronous version of this call.
2416 * When the operation is finished, @callback will be called.
2417 * You can then call g_file_replace_readwrite_finish() to get
2418 * the result of the operation.
2423 g_file_replace_readwrite_async (GFile *file,
2425 gboolean make_backup,
2426 GFileCreateFlags flags,
2428 GCancellable *cancellable,
2429 GAsyncReadyCallback callback,
2434 g_return_if_fail (G_IS_FILE (file));
2436 iface = G_FILE_GET_IFACE (file);
2437 (* iface->replace_readwrite_async) (file,
2448 * g_file_replace_readwrite_finish:
2449 * @file: input #GFile
2450 * @res: a #GAsyncResult
2451 * @error: a #GError, or %NULL
2453 * Finishes an asynchronous file replace operation started with
2454 * g_file_replace_readwrite_async().
2456 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2457 * Free the returned object with g_object_unref().
2462 g_file_replace_readwrite_finish (GFile *file,
2468 g_return_val_if_fail (G_IS_FILE (file), NULL);
2469 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2471 if (g_async_result_legacy_propagate_error (res, error))
2474 iface = G_FILE_GET_IFACE (file);
2475 return (* iface->replace_readwrite_finish) (file, res, error);
2479 copy_symlink (GFile *destination,
2480 GFileCopyFlags flags,
2481 GCancellable *cancellable,
2486 gboolean tried_delete;
2488 GFileType file_type;
2490 tried_delete = FALSE;
2494 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2496 /* Maybe it already existed, and we want to overwrite? */
2497 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2498 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2500 g_clear_error (&my_error);
2502 /* Don't overwrite if the destination is a directory */
2503 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2504 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2505 cancellable, &my_error);
2508 file_type = g_file_info_get_file_type (info);
2509 g_object_unref (info);
2511 if (file_type == G_FILE_TYPE_DIRECTORY)
2513 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2514 _("Can't copy over directory"));
2519 if (!g_file_delete (destination, cancellable, error))
2522 tried_delete = TRUE;
2526 g_propagate_error (error, my_error);
2533 static GFileInputStream *
2534 open_source_for_copy (GFile *source,
2536 GFileCopyFlags flags,
2537 GCancellable *cancellable,
2541 GFileInputStream *ret;
2543 GFileType file_type;
2546 ret = g_file_read (source, cancellable, &my_error);
2550 /* There was an error opening the source, try to set a good error for it: */
2551 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2553 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2554 * as that is less useful to the app. Better check for errors on the
2557 g_error_free (my_error);
2560 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2561 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2562 cancellable, &my_error);
2564 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2566 file_type = g_file_info_get_file_type (info);
2567 g_object_unref (info);
2569 if (flags & G_FILE_COPY_OVERWRITE)
2571 if (file_type == G_FILE_TYPE_DIRECTORY)
2573 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2574 _("Can't copy directory over directory"));
2577 /* continue to would_recurse error */
2581 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2582 _("Target file exists"));
2588 /* Error getting info from target, return that error
2589 * (except for NOT_FOUND, which is no error here)
2591 g_clear_object (&info);
2592 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2594 g_propagate_error (error, my_error);
2597 g_clear_error (&my_error);
2600 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2601 _("Can't recursively copy directory"));
2605 g_propagate_error (error, my_error);
2610 should_copy (GFileAttributeInfo *info,
2611 gboolean copy_all_attributes,
2612 gboolean skip_perms)
2614 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2617 if (copy_all_attributes)
2618 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2619 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2623 build_attribute_list_for_copy (GFile *file,
2624 GFileCopyFlags flags,
2625 char **out_attributes,
2626 GCancellable *cancellable,
2629 gboolean ret = FALSE;
2630 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2634 gboolean copy_all_attributes;
2635 gboolean skip_perms;
2637 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2638 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2640 /* Ignore errors here, if the target supports no attributes there is
2641 * nothing to copy. We still honor the cancellable though.
2643 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2644 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2647 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2651 if (attributes == NULL && namespaces == NULL)
2655 s = g_string_new ("");
2659 for (i = 0; i < attributes->n_infos; i++)
2661 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2666 g_string_append_c (s, ',');
2668 g_string_append (s, attributes->infos[i].name);
2675 for (i = 0; i < namespaces->n_infos; i++)
2677 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2682 g_string_append_c (s, ',');
2684 g_string_append (s, namespaces->infos[i].name);
2685 g_string_append (s, "::*");
2691 *out_attributes = g_string_free (s, FALSE);
2695 g_string_free (s, TRUE);
2697 g_file_attribute_info_list_unref (attributes);
2699 g_file_attribute_info_list_unref (namespaces);
2705 * g_file_copy_attributes:
2706 * @source: a #GFile with attributes
2707 * @destination: a #GFile to copy attributes to
2708 * @flags: a set of #GFileCopyFlags
2709 * @cancellable: (allow-none): optional #GCancellable object,
2711 * @error: a #GError, %NULL to ignore
2713 * Copies the file attributes from @source to @destination.
2715 * Normally only a subset of the file attributes are copied,
2716 * those that are copies in a normal file copy operation
2717 * (which for instance does not include e.g. owner). However
2718 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2719 * all the metadata that is possible to copy is copied. This
2720 * is useful when implementing move by copy + delete source.
2722 * Returns: %TRUE if the attributes were copied successfully,
2726 g_file_copy_attributes (GFile *source,
2728 GFileCopyFlags flags,
2729 GCancellable *cancellable,
2732 char *attrs_to_read;
2735 gboolean source_nofollow_symlinks;
2737 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2738 cancellable, error))
2741 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2743 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2744 * we just don't copy it.
2746 info = g_file_query_info (source, attrs_to_read,
2747 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2751 g_free (attrs_to_read);
2756 res = g_file_set_attributes_from_info (destination,
2758 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2761 g_object_unref (info);
2768 copy_stream_with_progress (GInputStream *in,
2771 GCancellable *cancellable,
2772 GFileProgressCallback progress_callback,
2773 gpointer progress_callback_data,
2776 gssize n_read, n_written;
2777 goffset current_size;
2778 char buffer[1024*64], *p;
2784 /* avoid performance impact of querying total size when it's not needed */
2785 if (progress_callback)
2787 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2788 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2792 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2793 total_size = g_file_info_get_size (info);
2794 g_object_unref (info);
2797 if (total_size == -1)
2799 info = g_file_query_info (source,
2800 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2801 G_FILE_QUERY_INFO_NONE,
2805 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2806 total_size = g_file_info_get_size (info);
2807 g_object_unref (info);
2812 if (total_size == -1)
2819 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2829 current_size += n_read;
2834 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2835 if (n_written == -1)
2842 n_read -= n_written;
2848 if (progress_callback)
2849 progress_callback (current_size, total_size, progress_callback_data);
2852 /* Make sure we send full copied size */
2853 if (progress_callback)
2854 progress_callback (current_size, total_size, progress_callback_data);
2862 do_splice (int fd_in,
2867 long *bytes_transferd,
2873 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2881 else if (errsv == ENOSYS || errsv == EINVAL)
2882 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2883 _("Splice not supported"));
2885 g_set_error (error, G_IO_ERROR,
2886 g_io_error_from_errno (errsv),
2887 _("Error splicing file: %s"),
2888 g_strerror (errsv));
2893 *bytes_transferd = result;
2898 splice_stream_with_progress (GInputStream *in,
2900 GCancellable *cancellable,
2901 GFileProgressCallback progress_callback,
2902 gpointer progress_callback_data,
2905 int buffer[2] = { -1, -1 };
2912 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2913 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2915 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2919 /* avoid performance impact of querying total size when it's not needed */
2920 if (progress_callback)
2924 if (fstat (fd_in, &sbuf) == 0)
2925 total_size = sbuf.st_size;
2928 if (total_size == -1)
2931 offset_in = offset_out = 0;
2938 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2941 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2952 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2955 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2958 n_read -= n_written;
2961 if (progress_callback)
2962 progress_callback (offset_in, total_size, progress_callback_data);
2965 /* Make sure we send full copied size */
2966 if (progress_callback)
2967 progress_callback (offset_in, total_size, progress_callback_data);
2969 if (!g_close (buffer[0], error))
2972 if (!g_close (buffer[1], error))
2976 if (buffer[0] != -1)
2977 (void) g_close (buffer[0], NULL);
2978 if (buffer[1] != -1)
2979 (void) g_close (buffer[1], NULL);
2987 btrfs_reflink_with_progress (GInputStream *in,
2990 GCancellable *cancellable,
2991 GFileProgressCallback progress_callback,
2992 gpointer progress_callback_data,
2995 goffset source_size;
2999 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3000 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3002 if (progress_callback)
3003 source_size = g_file_info_get_size (info);
3005 /* Btrfs clone ioctl properties:
3006 * - Works at the inode level
3007 * - Doesn't work with directories
3008 * - Always follows symlinks (source and destination)
3010 * By the time we get here, *in and *out are both regular files */
3011 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3016 g_set_error_literal (error, G_IO_ERROR,
3017 G_IO_ERROR_NOT_SUPPORTED,
3018 _("Copy (reflink/clone) between mounts is not supported"));
3019 else if (errno == EINVAL)
3020 g_set_error_literal (error, G_IO_ERROR,
3021 G_IO_ERROR_NOT_SUPPORTED,
3022 _("Copy (reflink/clone) is not supported or invalid"));
3024 /* Most probably something odd happened; retry with fallback */
3025 g_set_error_literal (error, G_IO_ERROR,
3026 G_IO_ERROR_NOT_SUPPORTED,
3027 _("Copy (reflink/clone) is not supported or didn't work"));
3028 /* We retry with fallback for all error cases because Btrfs is currently
3029 * unstable, and so we can't trust it to do clone properly.
3030 * In addition, any hard errors here would cause the same failure in the
3031 * fallback manual copy as well. */
3035 /* Make sure we send full copied size */
3036 if (progress_callback)
3037 progress_callback (source_size, source_size, progress_callback_data);
3044 file_copy_fallback (GFile *source,
3046 GFileCopyFlags flags,
3047 GCancellable *cancellable,
3048 GFileProgressCallback progress_callback,
3049 gpointer progress_callback_data,
3052 gboolean ret = FALSE;
3053 GFileInputStream *file_in = NULL;
3054 GInputStream *in = NULL;
3055 GOutputStream *out = NULL;
3056 GFileInfo *info = NULL;
3058 char *attrs_to_read;
3059 gboolean do_set_attributes = FALSE;
3061 /* need to know the file type */
3062 info = g_file_query_info (source,
3063 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3064 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3070 /* Maybe copy the symlink? */
3071 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3072 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3074 target = g_file_info_get_symlink_target (info);
3077 if (!copy_symlink (destination, flags, cancellable, target, error))
3083 /* ... else fall back on a regular file copy */
3085 /* Handle "special" files (pipes, device nodes, ...)? */
3086 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3088 /* FIXME: could try to recreate device nodes and others? */
3089 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3090 _("Can't copy special file"));
3094 /* Everything else should just fall back on a regular copy. */
3096 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3099 in = G_INPUT_STREAM (file_in);
3101 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3102 cancellable, error))
3105 if (attrs_to_read != NULL)
3107 GError *tmp_error = NULL;
3109 /* Ok, ditch the previous lightweight info (on Unix we just
3110 * called lstat()); at this point we gather all the information
3111 * we need about the source from the opened file descriptor.
3113 g_object_unref (info);
3115 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3116 cancellable, &tmp_error);
3119 /* Not all gvfs backends implement query_info_on_read(), we
3120 * can just fall back to the pathname again.
3121 * https://bugzilla.gnome.org/706254
3123 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3125 g_clear_error (&tmp_error);
3126 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3127 cancellable, error);
3131 g_free (attrs_to_read);
3132 g_propagate_error (error, tmp_error);
3136 g_free (attrs_to_read);
3140 do_set_attributes = TRUE;
3143 /* In the local file path, we pass down the source info which
3144 * includes things like unix::mode, to ensure that the target file
3145 * is not created with different permissions from the source file.
3147 * If a future API like g_file_replace_with_info() is added, switch
3148 * this code to use that.
3150 if (G_IS_LOCAL_FILE (destination))
3152 if (flags & G_FILE_COPY_OVERWRITE)
3153 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3155 flags & G_FILE_COPY_BACKUP,
3156 G_FILE_CREATE_REPLACE_DESTINATION,
3158 cancellable, error);
3160 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3162 cancellable, error);
3164 else if (flags & G_FILE_COPY_OVERWRITE)
3166 out = (GOutputStream *)g_file_replace (destination,
3168 flags & G_FILE_COPY_BACKUP,
3169 G_FILE_CREATE_REPLACE_DESTINATION,
3170 cancellable, error);
3174 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3181 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3183 GError *reflink_err = NULL;
3185 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3186 progress_callback, progress_callback_data,
3189 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3191 g_clear_error (&reflink_err);
3195 g_propagate_error (error, reflink_err);
3208 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3210 GError *splice_err = NULL;
3212 if (!splice_stream_with_progress (in, out, cancellable,
3213 progress_callback, progress_callback_data,
3216 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3218 g_clear_error (&splice_err);
3222 g_propagate_error (error, splice_err);
3235 /* A plain read/write loop */
3236 if (!copy_stream_with_progress (in, out, source, cancellable,
3237 progress_callback, progress_callback_data,
3245 /* Don't care about errors in source here */
3246 (void) g_input_stream_close (in, cancellable, NULL);
3247 g_object_unref (in);
3252 /* But write errors on close are bad! */
3253 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3255 g_object_unref (out);
3258 /* Ignore errors here. Failure to copy metadata is not a hard error */
3259 /* TODO: set these attributes /before/ we do the rename() on Unix */
3260 if (ret && do_set_attributes)
3262 g_file_set_attributes_from_info (destination,
3264 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3269 g_clear_object (&info);
3276 * @source: input #GFile
3277 * @destination: destination #GFile
3278 * @flags: set of #GFileCopyFlags
3279 * @cancellable: (allow-none): optional #GCancellable object,
3281 * @progress_callback: (allow-none) (scope call): function to callback with
3282 * progress information, or %NULL if progress information is not needed
3283 * @progress_callback_data: (closure): user data to pass to @progress_callback
3284 * @error: #GError to set on error, or %NULL
3286 * Copies the file @source to the location specified by @destination.
3287 * Can not handle recursive copies of directories.
3289 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3290 * existing @destination file is overwritten.
3292 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3293 * will be copied as symlinks, otherwise the target of the
3294 * @source symlink will be copied.
3296 * If @cancellable is not %NULL, then the operation can be cancelled by
3297 * triggering the cancellable object from another thread. If the operation
3298 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3300 * If @progress_callback is not %NULL, then the operation can be monitored
3301 * by setting this to a #GFileProgressCallback function.
3302 * @progress_callback_data will be passed to this function. It is guaranteed
3303 * that this callback will be called after all data has been transferred with
3304 * the total number of bytes copied during the operation.
3306 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3307 * is returned, independent on the status of the @destination.
3309 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3310 * the error %G_IO_ERROR_EXISTS is returned.
3312 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3313 * error is returned. If trying to overwrite a directory with a directory the
3314 * %G_IO_ERROR_WOULD_MERGE error is returned.
3316 * If the source is a directory and the target does not exist, or
3317 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3318 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3320 * If you are interested in copying the #GFile object itself (not the on-disk
3321 * file), see g_file_dup().
3323 * Returns: %TRUE on success, %FALSE otherwise.
3326 g_file_copy (GFile *source,
3328 GFileCopyFlags flags,
3329 GCancellable *cancellable,
3330 GFileProgressCallback progress_callback,
3331 gpointer progress_callback_data,
3338 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3339 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3341 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3344 iface = G_FILE_GET_IFACE (destination);
3348 res = (* iface->copy) (source, destination,
3350 progress_callback, progress_callback_data,
3356 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3358 g_propagate_error (error, my_error);
3362 g_clear_error (&my_error);
3365 /* If the types are different, and the destination method failed
3366 * also try the source method
3368 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3370 iface = G_FILE_GET_IFACE (source);
3375 res = (* iface->copy) (source, destination,
3377 progress_callback, progress_callback_data,
3383 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3385 g_propagate_error (error, my_error);
3389 g_clear_error (&my_error);
3393 return file_copy_fallback (source, destination, flags, cancellable,
3394 progress_callback, progress_callback_data,
3399 * g_file_copy_async: (skip)
3400 * @source: input #GFile
3401 * @destination: destination #GFile
3402 * @flags: set of #GFileCopyFlags
3403 * @io_priority: the [I/O priority][io-priority] of the request
3404 * @cancellable: (allow-none): optional #GCancellable object,
3406 * @progress_callback: (allow-none): function to callback with progress
3407 * information, or %NULL if progress information is not needed
3408 * @progress_callback_data: (closure): user data to pass to @progress_callback
3409 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3410 * @user_data: the data to pass to callback function
3412 * Copies the file @source to the location specified by @destination
3413 * asynchronously. For details of the behaviour, see g_file_copy().
3415 * If @progress_callback is not %NULL, then that function that will be called
3416 * just like in g_file_copy(). The callback will run in the default main context
3417 * of the thread calling g_file_copy_async() — the same context as @callback is
3420 * When the operation is finished, @callback will be called. You can then call
3421 * g_file_copy_finish() to get the result of the operation.
3424 g_file_copy_async (GFile *source,
3426 GFileCopyFlags flags,
3428 GCancellable *cancellable,
3429 GFileProgressCallback progress_callback,
3430 gpointer progress_callback_data,
3431 GAsyncReadyCallback callback,
3436 g_return_if_fail (G_IS_FILE (source));
3437 g_return_if_fail (G_IS_FILE (destination));
3439 iface = G_FILE_GET_IFACE (source);
3440 (* iface->copy_async) (source,
3446 progress_callback_data,
3452 * g_file_copy_finish:
3453 * @file: input #GFile
3454 * @res: a #GAsyncResult
3455 * @error: a #GError, or %NULL
3457 * Finishes copying the file started with g_file_copy_async().
3459 * Returns: a %TRUE on success, %FALSE on error.
3462 g_file_copy_finish (GFile *file,
3468 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3469 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3471 if (g_async_result_legacy_propagate_error (res, error))
3474 iface = G_FILE_GET_IFACE (file);
3475 return (* iface->copy_finish) (file, res, error);
3480 * @source: #GFile pointing to the source location
3481 * @destination: #GFile pointing to the destination location
3482 * @flags: set of #GFileCopyFlags
3483 * @cancellable: (allow-none): optional #GCancellable object,
3485 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3486 * function for updates
3487 * @progress_callback_data: (closure): gpointer to user data for
3488 * the callback function
3489 * @error: #GError for returning error conditions, or %NULL
3491 * Tries to move the file or directory @source to the location specified
3492 * by @destination. If native move operations are supported then this is
3493 * used, otherwise a copy + delete fallback is used. The native
3494 * implementation may support moving directories (for instance on moves
3495 * inside the same filesystem), but the fallback code does not.
3497 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3498 * existing @destination file is overwritten.
3500 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3501 * will be copied as symlinks, otherwise the target of the
3502 * @source symlink will be copied.
3504 * If @cancellable is not %NULL, then the operation can be cancelled by
3505 * triggering the cancellable object from another thread. If the operation
3506 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3508 * If @progress_callback is not %NULL, then the operation can be monitored
3509 * by setting this to a #GFileProgressCallback function.
3510 * @progress_callback_data will be passed to this function. It is
3511 * guaranteed that this callback will be called after all data has been
3512 * transferred with the total number of bytes copied during the operation.
3514 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3515 * error is returned, independent on the status of the @destination.
3517 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3518 * then the error %G_IO_ERROR_EXISTS is returned.
3520 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3521 * error is returned. If trying to overwrite a directory with a directory the
3522 * %G_IO_ERROR_WOULD_MERGE error is returned.
3524 * If the source is a directory and the target does not exist, or
3525 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3526 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3527 * move operation isn't available).
3529 * Returns: %TRUE on successful move, %FALSE otherwise.
3532 g_file_move (GFile *source,
3534 GFileCopyFlags flags,
3535 GCancellable *cancellable,
3536 GFileProgressCallback progress_callback,
3537 gpointer progress_callback_data,
3544 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3545 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3547 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3550 iface = G_FILE_GET_IFACE (destination);
3554 res = (* iface->move) (source, destination,
3556 progress_callback, progress_callback_data,
3562 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3564 g_propagate_error (error, my_error);
3568 g_clear_error (&my_error);
3571 /* If the types are different, and the destination method failed
3572 * also try the source method
3574 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3576 iface = G_FILE_GET_IFACE (source);
3581 res = (* iface->move) (source, destination,
3583 progress_callback, progress_callback_data,
3589 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3591 g_propagate_error (error, my_error);
3595 g_clear_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 [I/O priority][io-priority] of the request
3669 * @cancellable: (allow-none): optional #GCancellable object,
3671 * @callback: a #GAsyncReadyCallback to call
3672 * when the request is satisfied
3673 * @user_data: the data to pass to callback function
3675 * Asynchronously creates a directory.
3677 * Virtual: make_directory_async
3681 g_file_make_directory_async (GFile *file,
3683 GCancellable *cancellable,
3684 GAsyncReadyCallback callback,
3689 g_return_if_fail (G_IS_FILE (file));
3691 iface = G_FILE_GET_IFACE (file);
3692 (* iface->make_directory_async) (file,
3700 * g_file_make_directory_finish:
3701 * @file: input #GFile
3702 * @result: a #GAsyncResult
3703 * @error: a #GError, or %NULL
3705 * Finishes an asynchronous directory creation, started with
3706 * g_file_make_directory_async().
3708 * Virtual: make_directory_finish
3709 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3713 g_file_make_directory_finish (GFile *file,
3714 GAsyncResult *result,
3719 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3720 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3722 iface = G_FILE_GET_IFACE (file);
3723 return (* iface->make_directory_finish) (file, result, error);
3727 * g_file_make_directory_with_parents:
3728 * @file: input #GFile
3729 * @cancellable: (allow-none): optional #GCancellable object,
3731 * @error: a #GError, or %NULL
3733 * Creates a directory and any parent directories that may not
3734 * exist similar to 'mkdir -p'. If the file system does not support
3735 * creating directories, this function will fail, setting @error to
3736 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3737 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3738 * the similar g_mkdir_with_parents().
3740 * For a local #GFile the newly created directories will have the default
3741 * (current) ownership and permissions of the current process.
3743 * If @cancellable is not %NULL, then the operation can be cancelled by
3744 * triggering the cancellable object from another thread. If the operation
3745 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3747 * Returns: %TRUE if all directories have been successfully created, %FALSE
3753 g_file_make_directory_with_parents (GFile *file,
3754 GCancellable *cancellable,
3757 GFile *work_file = NULL;
3758 GList *list = NULL, *l;
3759 GError *my_error = NULL;
3761 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3766 /* Try for the simple case of not having to create any parent
3767 * directories. If any parent directory needs to be created, this
3768 * call will fail with NOT_FOUND. If that happens, then that value of
3769 * my_error persists into the while loop below.
3771 g_file_make_directory (file, cancellable, &my_error);
3772 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3775 g_propagate_error (error, my_error);
3776 return my_error == NULL;
3779 work_file = g_object_ref (file);
3781 /* Creates the parent directories as needed. In case any particular
3782 * creation operation fails for lack of other parent directories
3783 * (NOT_FOUND), the directory is added to a list of directories to
3784 * create later, and the value of my_error is retained until the next
3785 * iteration of the loop. After the loop my_error should either be
3786 * empty or contain a real failure condition.
3788 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3792 parent_file = g_file_get_parent (work_file);
3793 if (parent_file == NULL)
3796 g_clear_error (&my_error);
3797 g_file_make_directory (parent_file, cancellable, &my_error);
3798 /* Another process may have created the directory in between the
3799 * G_IO_ERROR_NOT_FOUND and now
3801 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3802 g_clear_error (&my_error);
3804 g_object_unref (work_file);
3805 work_file = g_object_ref (parent_file);
3807 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3808 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3810 g_object_unref (parent_file);
3813 /* All directories should be able to be created now, so an error at
3814 * this point means the whole operation must fail -- except an EXISTS
3815 * error, which means that another process already created the
3816 * directory in between the previous failure and now.
3818 for (l = list; my_error == NULL && l; l = l->next)
3820 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3821 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3822 g_clear_error (&my_error);
3826 g_object_unref (work_file);
3829 while (list != NULL)
3831 g_object_unref ((GFile *) list->data);
3832 list = g_list_remove (list, list->data);
3835 /* At this point an error in my_error means a that something
3836 * unexpected failed in either of the loops above, so the whole
3837 * operation must fail.
3839 if (my_error != NULL)
3841 g_propagate_error (error, my_error);
3845 return g_file_make_directory (file, cancellable, error);
3849 * g_file_make_symbolic_link:
3850 * @file: a #GFile with the name of the symlink to create
3851 * @symlink_value: a string with the path for the target of the new symlink
3852 * @cancellable: (allow-none): optional #GCancellable object,
3856 * Creates a symbolic link named @file which contains the string
3859 * If @cancellable is not %NULL, then the operation can be cancelled by
3860 * triggering the cancellable object from another thread. If the operation
3861 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3863 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3866 g_file_make_symbolic_link (GFile *file,
3867 const char *symlink_value,
3868 GCancellable *cancellable,
3873 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3874 g_return_val_if_fail (symlink_value != NULL, FALSE);
3876 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3879 if (*symlink_value == '\0')
3881 g_set_error_literal (error, G_IO_ERROR,
3882 G_IO_ERROR_INVALID_ARGUMENT,
3883 _("Invalid symlink value given"));
3887 iface = G_FILE_GET_IFACE (file);
3889 if (iface->make_symbolic_link == NULL)
3891 g_set_error_literal (error, G_IO_ERROR,
3892 G_IO_ERROR_NOT_SUPPORTED,
3893 _("Operation not supported"));
3897 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3902 * @file: input #GFile
3903 * @cancellable: (allow-none): optional #GCancellable object,
3905 * @error: a #GError, or %NULL
3907 * Deletes a file. If the @file is a directory, it will only be
3908 * deleted if it is empty. This has the same semantics as g_unlink().
3910 * If @cancellable is not %NULL, then the operation can be cancelled by
3911 * triggering the cancellable object from another thread. If the operation
3912 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3914 * Virtual: delete_file
3915 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3918 g_file_delete (GFile *file,
3919 GCancellable *cancellable,
3924 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3926 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3929 iface = G_FILE_GET_IFACE (file);
3931 if (iface->delete_file == NULL)
3933 g_set_error_literal (error, G_IO_ERROR,
3934 G_IO_ERROR_NOT_SUPPORTED,
3935 _("Operation not supported"));
3939 return (* iface->delete_file) (file, cancellable, error);
3943 * g_file_delete_async:
3944 * @file: input #GFile
3945 * @io_priority: the [I/O priority][io-priority] of the request
3946 * @cancellable: (allow-none): optional #GCancellable object,
3948 * @callback: a #GAsyncReadyCallback to call
3949 * when the request is satisfied
3950 * @user_data: the data to pass to callback function
3952 * Asynchronously delete a file. If the @file is a directory, it will
3953 * only be deleted if it is empty. This has the same semantics as
3956 * Virtual: delete_file_async
3960 g_file_delete_async (GFile *file,
3962 GCancellable *cancellable,
3963 GAsyncReadyCallback callback,
3968 g_return_if_fail (G_IS_FILE (file));
3970 iface = G_FILE_GET_IFACE (file);
3971 (* iface->delete_file_async) (file,
3979 * g_file_delete_finish:
3980 * @file: input #GFile
3981 * @result: a #GAsyncResult
3982 * @error: a #GError, or %NULL
3984 * Finishes deleting a file started with g_file_delete_async().
3986 * Virtual: delete_file_finish
3987 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3991 g_file_delete_finish (GFile *file,
3992 GAsyncResult *result,
3997 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3998 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4000 if (g_async_result_legacy_propagate_error (result, error))
4003 iface = G_FILE_GET_IFACE (file);
4004 return (* iface->delete_file_finish) (file, result, error);
4009 * @file: #GFile to send to trash
4010 * @cancellable: (allow-none): optional #GCancellable object,
4012 * @error: a #GError, or %NULL
4014 * Sends @file to the "Trashcan", if possible. This is similar to
4015 * deleting it, but the user can recover it before emptying the trashcan.
4016 * Not all file systems support trashing, so this call can return the
4017 * %G_IO_ERROR_NOT_SUPPORTED error.
4019 * If @cancellable is not %NULL, then the operation can be cancelled by
4020 * triggering the cancellable object from another thread. If the operation
4021 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4024 * Returns: %TRUE on successful trash, %FALSE otherwise.
4027 g_file_trash (GFile *file,
4028 GCancellable *cancellable,
4033 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4035 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4038 iface = G_FILE_GET_IFACE (file);
4040 if (iface->trash == NULL)
4042 g_set_error_literal (error,
4043 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4044 _("Trash not supported"));
4048 return (* iface->trash) (file, cancellable, error);
4052 * g_file_trash_async:
4053 * @file: input #GFile
4054 * @io_priority: the [I/O priority][io-priority] of the request
4055 * @cancellable: (allow-none): optional #GCancellable object,
4057 * @callback: a #GAsyncReadyCallback to call
4058 * when the request is satisfied
4059 * @user_data: the data to pass to callback function
4061 * Asynchronously sends @file to the Trash location, if possible.
4063 * Virtual: trash_async
4067 g_file_trash_async (GFile *file,
4069 GCancellable *cancellable,
4070 GAsyncReadyCallback callback,
4075 g_return_if_fail (G_IS_FILE (file));
4077 iface = G_FILE_GET_IFACE (file);
4078 (* iface->trash_async) (file,
4086 * g_file_trash_finish:
4087 * @file: input #GFile
4088 * @result: a #GAsyncResult
4089 * @error: a #GError, or %NULL
4091 * Finishes an asynchronous file trashing operation, started with
4092 * g_file_trash_async().
4094 * Virtual: trash_finish
4095 * Returns: %TRUE on successful trash, %FALSE otherwise.
4099 g_file_trash_finish (GFile *file,
4100 GAsyncResult *result,
4105 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4106 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4108 iface = G_FILE_GET_IFACE (file);
4109 return (* iface->trash_finish) (file, result, error);
4113 * g_file_set_display_name:
4114 * @file: input #GFile
4115 * @display_name: a string
4116 * @cancellable: (allow-none): optional #GCancellable object,
4118 * @error: a #GError, or %NULL
4120 * Renames @file to the specified display name.
4122 * The display name is converted from UTF-8 to the correct encoding
4123 * for the target filesystem if possible and the @file is renamed to this.
4125 * If you want to implement a rename operation in the user interface the
4126 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4127 * initial value in the rename widget, and then the result after editing
4128 * should be passed to g_file_set_display_name().
4130 * On success the resulting converted filename is returned.
4132 * If @cancellable is not %NULL, then the operation can be cancelled by
4133 * triggering the cancellable object from another thread. If the operation
4134 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4136 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4137 * or %NULL if there was an error.
4138 * Free the returned object with g_object_unref().
4141 g_file_set_display_name (GFile *file,
4142 const gchar *display_name,
4143 GCancellable *cancellable,
4148 g_return_val_if_fail (G_IS_FILE (file), NULL);
4149 g_return_val_if_fail (display_name != NULL, NULL);
4151 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4155 G_IO_ERROR_INVALID_ARGUMENT,
4156 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4160 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4163 iface = G_FILE_GET_IFACE (file);
4165 return (* iface->set_display_name) (file, display_name, cancellable, error);
4169 * g_file_set_display_name_async:
4170 * @file: input #GFile
4171 * @display_name: a string
4172 * @io_priority: the [I/O priority][io-priority] of the request
4173 * @cancellable: (allow-none): optional #GCancellable object,
4175 * @callback: (scope async): a #GAsyncReadyCallback to call
4176 * when the request is satisfied
4177 * @user_data: (closure): the data to pass to callback function
4179 * Asynchronously sets the display name for a given #GFile.
4181 * For more details, see g_file_set_display_name() which is
4182 * the synchronous version of this call.
4184 * When the operation is finished, @callback will be called.
4185 * You can then call g_file_set_display_name_finish() to get
4186 * the result of the operation.
4189 g_file_set_display_name_async (GFile *file,
4190 const gchar *display_name,
4192 GCancellable *cancellable,
4193 GAsyncReadyCallback callback,
4198 g_return_if_fail (G_IS_FILE (file));
4199 g_return_if_fail (display_name != NULL);
4201 iface = G_FILE_GET_IFACE (file);
4202 (* iface->set_display_name_async) (file,
4211 * g_file_set_display_name_finish:
4212 * @file: input #GFile
4213 * @res: a #GAsyncResult
4214 * @error: a #GError, or %NULL
4216 * Finishes setting a display name started with
4217 * g_file_set_display_name_async().
4219 * Returns: (transfer full): a #GFile or %NULL on error.
4220 * Free the returned object with g_object_unref().
4223 g_file_set_display_name_finish (GFile *file,
4229 g_return_val_if_fail (G_IS_FILE (file), NULL);
4230 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4232 if (g_async_result_legacy_propagate_error (res, error))
4235 iface = G_FILE_GET_IFACE (file);
4236 return (* iface->set_display_name_finish) (file, res, error);
4240 * g_file_query_settable_attributes:
4241 * @file: input #GFile
4242 * @cancellable: (allow-none): optional #GCancellable object,
4244 * @error: a #GError, or %NULL
4246 * Obtain the list of settable attributes for the file.
4248 * Returns the type and full attribute name of all the attributes
4249 * that can be set on this file. This doesn't mean setting it will
4250 * always succeed though, you might get an access failure, or some
4251 * specific file may not support a specific attribute.
4253 * If @cancellable is not %NULL, then the operation can be cancelled by
4254 * triggering the cancellable object from another thread. If the operation
4255 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4257 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4258 * When you are done with it, release it with
4259 * g_file_attribute_info_list_unref()
4261 GFileAttributeInfoList *
4262 g_file_query_settable_attributes (GFile *file,
4263 GCancellable *cancellable,
4268 GFileAttributeInfoList *list;
4270 g_return_val_if_fail (G_IS_FILE (file), NULL);
4272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4275 iface = G_FILE_GET_IFACE (file);
4277 if (iface->query_settable_attributes == NULL)
4278 return g_file_attribute_info_list_new ();
4281 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4285 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4287 list = g_file_attribute_info_list_new ();
4288 g_error_free (my_error);
4291 g_propagate_error (error, my_error);
4298 * g_file_query_writable_namespaces:
4299 * @file: input #GFile
4300 * @cancellable: (allow-none): optional #GCancellable object,
4302 * @error: a #GError, or %NULL
4304 * Obtain the list of attribute namespaces where new attributes
4305 * can be created by a user. An example of this is extended
4306 * attributes (in the "xattr" namespace).
4308 * If @cancellable is not %NULL, then the operation can be cancelled by
4309 * triggering the cancellable object from another thread. If the operation
4310 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4312 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4313 * When you are done with it, release it with
4314 * g_file_attribute_info_list_unref()
4316 GFileAttributeInfoList *
4317 g_file_query_writable_namespaces (GFile *file,
4318 GCancellable *cancellable,
4323 GFileAttributeInfoList *list;
4325 g_return_val_if_fail (G_IS_FILE (file), NULL);
4327 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4330 iface = G_FILE_GET_IFACE (file);
4332 if (iface->query_writable_namespaces == NULL)
4333 return g_file_attribute_info_list_new ();
4336 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4340 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4342 list = g_file_attribute_info_list_new ();
4343 g_error_free (my_error);
4346 g_propagate_error (error, my_error);
4353 * g_file_set_attribute:
4354 * @file: input #GFile
4355 * @attribute: a string containing the attribute's name
4356 * @type: The type of the attribute
4357 * @value_p: (allow-none): a pointer to the value (or the pointer
4358 * itself if the type is a pointer type)
4359 * @flags: a set of #GFileQueryInfoFlags
4360 * @cancellable: (allow-none): optional #GCancellable object,
4362 * @error: a #GError, or %NULL
4364 * Sets an attribute in the file with attribute name @attribute to @value.
4366 * Some attributes can be unset by setting @attribute to
4367 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4369 * If @cancellable is not %NULL, then the operation can be cancelled by
4370 * triggering the cancellable object from another thread. If the operation
4371 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4373 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4376 g_file_set_attribute (GFile *file,
4377 const gchar *attribute,
4378 GFileAttributeType type,
4380 GFileQueryInfoFlags flags,
4381 GCancellable *cancellable,
4386 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4387 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4389 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4392 iface = G_FILE_GET_IFACE (file);
4394 if (iface->set_attribute == NULL)
4396 g_set_error_literal (error, G_IO_ERROR,
4397 G_IO_ERROR_NOT_SUPPORTED,
4398 _("Operation not supported"));
4402 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4406 * g_file_set_attributes_from_info:
4407 * @file: input #GFile
4408 * @info: a #GFileInfo
4409 * @flags: #GFileQueryInfoFlags
4410 * @cancellable: (allow-none): optional #GCancellable object,
4412 * @error: a #GError, or %NULL
4414 * Tries to set all attributes in the #GFileInfo on the target
4415 * values, not stopping on the first error.
4417 * If there is any error during this operation then @error will
4418 * be set to the first error. Error on particular fields are flagged
4419 * by setting the "status" field in the attribute value to
4420 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4421 * also detect further errors.
4423 * If @cancellable is not %NULL, then the operation can be cancelled by
4424 * triggering the cancellable object from another thread. If the operation
4425 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4427 * Returns: %FALSE if there was any error, %TRUE otherwise.
4430 g_file_set_attributes_from_info (GFile *file,
4432 GFileQueryInfoFlags flags,
4433 GCancellable *cancellable,
4438 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4439 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4441 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4444 g_file_info_clear_status (info);
4446 iface = G_FILE_GET_IFACE (file);
4448 return (* iface->set_attributes_from_info) (file,
4456 g_file_real_set_attributes_from_info (GFile *file,
4458 GFileQueryInfoFlags flags,
4459 GCancellable *cancellable,
4465 GFileAttributeValue *value;
4469 attributes = g_file_info_list_attributes (info, NULL);
4471 for (i = 0; attributes[i] != NULL; i++)
4473 value = _g_file_info_get_attribute_value (info, attributes[i]);
4475 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4478 if (!g_file_set_attribute (file, attributes[i],
4479 value->type, _g_file_attribute_value_peek_as_pointer (value),
4480 flags, cancellable, error))
4482 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4484 /* Don't set error multiple times */
4488 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4491 g_strfreev (attributes);
4497 * g_file_set_attributes_async:
4498 * @file: input #GFile
4499 * @info: a #GFileInfo
4500 * @flags: a #GFileQueryInfoFlags
4501 * @io_priority: the [I/O priority][io-priority] of the request
4502 * @cancellable: (allow-none): optional #GCancellable object,
4504 * @callback: (scope async): a #GAsyncReadyCallback
4505 * @user_data: (closure): a #gpointer
4507 * Asynchronously sets the attributes of @file with @info.
4509 * For more details, see g_file_set_attributes_from_info(),
4510 * which is the synchronous version of this call.
4512 * When the operation is finished, @callback will be called.
4513 * You can then call g_file_set_attributes_finish() to get
4514 * the result of the operation.
4517 g_file_set_attributes_async (GFile *file,
4519 GFileQueryInfoFlags flags,
4521 GCancellable *cancellable,
4522 GAsyncReadyCallback callback,
4527 g_return_if_fail (G_IS_FILE (file));
4528 g_return_if_fail (G_IS_FILE_INFO (info));
4530 iface = G_FILE_GET_IFACE (file);
4531 (* iface->set_attributes_async) (file,
4541 * g_file_set_attributes_finish:
4542 * @file: input #GFile
4543 * @result: a #GAsyncResult
4544 * @info: (out) (transfer full): a #GFileInfo
4545 * @error: a #GError, or %NULL
4547 * Finishes setting an attribute started in g_file_set_attributes_async().
4549 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4552 g_file_set_attributes_finish (GFile *file,
4553 GAsyncResult *result,
4559 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4560 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4562 /* No standard handling of errors here, as we must set info even
4565 iface = G_FILE_GET_IFACE (file);
4566 return (* iface->set_attributes_finish) (file, result, info, error);
4570 * g_file_set_attribute_string:
4571 * @file: input #GFile
4572 * @attribute: a string containing the attribute's name
4573 * @value: a string containing the attribute's value
4574 * @flags: #GFileQueryInfoFlags
4575 * @cancellable: (allow-none): optional #GCancellable object,
4577 * @error: a #GError, or %NULL
4579 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4580 * If @attribute is of a different type, this operation will fail.
4582 * If @cancellable is not %NULL, then the operation can be cancelled by
4583 * triggering the cancellable object from another thread. If the operation
4584 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4586 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4589 g_file_set_attribute_string (GFile *file,
4590 const char *attribute,
4592 GFileQueryInfoFlags flags,
4593 GCancellable *cancellable,
4596 return g_file_set_attribute (file, attribute,
4597 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4598 flags, cancellable, error);
4602 * g_file_set_attribute_byte_string:
4603 * @file: input #GFile
4604 * @attribute: a string containing the attribute's name
4605 * @value: a string containing the attribute's new value
4606 * @flags: a #GFileQueryInfoFlags
4607 * @cancellable: (allow-none): optional #GCancellable object,
4609 * @error: a #GError, or %NULL
4611 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4612 * If @attribute is of a different type, this operation will fail,
4615 * If @cancellable is not %NULL, then the operation can be cancelled by
4616 * triggering the cancellable object from another thread. If the operation
4617 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4619 * Returns: %TRUE if the @attribute was successfully set to @value
4620 * in the @file, %FALSE otherwise.
4623 g_file_set_attribute_byte_string (GFile *file,
4624 const gchar *attribute,
4626 GFileQueryInfoFlags flags,
4627 GCancellable *cancellable,
4630 return g_file_set_attribute (file, attribute,
4631 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4632 flags, cancellable, error);
4636 * g_file_set_attribute_uint32:
4637 * @file: input #GFile
4638 * @attribute: a string containing the attribute's name
4639 * @value: a #guint32 containing the attribute's new value
4640 * @flags: a #GFileQueryInfoFlags
4641 * @cancellable: (allow-none): optional #GCancellable object,
4643 * @error: a #GError, or %NULL
4645 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4646 * If @attribute is of a different type, this operation will fail.
4648 * If @cancellable is not %NULL, then the operation can be cancelled by
4649 * triggering the cancellable object from another thread. If the operation
4650 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4652 * Returns: %TRUE if the @attribute was successfully set to @value
4653 * in the @file, %FALSE otherwise.
4656 g_file_set_attribute_uint32 (GFile *file,
4657 const gchar *attribute,
4659 GFileQueryInfoFlags flags,
4660 GCancellable *cancellable,
4663 return g_file_set_attribute (file, attribute,
4664 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4665 flags, cancellable, error);
4669 * g_file_set_attribute_int32:
4670 * @file: input #GFile
4671 * @attribute: a string containing the attribute's name
4672 * @value: a #gint32 containing the attribute's new value
4673 * @flags: a #GFileQueryInfoFlags
4674 * @cancellable: (allow-none): optional #GCancellable object,
4676 * @error: a #GError, or %NULL
4678 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4679 * If @attribute is of a different type, this operation will fail.
4681 * If @cancellable is not %NULL, then the operation can be cancelled by
4682 * triggering the cancellable object from another thread. If the operation
4683 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4685 * Returns: %TRUE if the @attribute was successfully set to @value
4686 * in the @file, %FALSE otherwise.
4689 g_file_set_attribute_int32 (GFile *file,
4690 const gchar *attribute,
4692 GFileQueryInfoFlags flags,
4693 GCancellable *cancellable,
4696 return g_file_set_attribute (file, attribute,
4697 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4698 flags, cancellable, error);
4702 * g_file_set_attribute_uint64:
4703 * @file: input #GFile
4704 * @attribute: a string containing the attribute's name
4705 * @value: a #guint64 containing the attribute's new value
4706 * @flags: a #GFileQueryInfoFlags
4707 * @cancellable: (allow-none): optional #GCancellable object,
4709 * @error: a #GError, or %NULL
4711 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4712 * If @attribute is of a different type, this operation will fail.
4714 * If @cancellable is not %NULL, then the operation can be cancelled by
4715 * triggering the cancellable object from another thread. If the operation
4716 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4718 * Returns: %TRUE if the @attribute was successfully set to @value
4719 * in the @file, %FALSE otherwise.
4722 g_file_set_attribute_uint64 (GFile *file,
4723 const gchar *attribute,
4725 GFileQueryInfoFlags flags,
4726 GCancellable *cancellable,
4729 return g_file_set_attribute (file, attribute,
4730 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4731 flags, cancellable, error);
4735 * g_file_set_attribute_int64:
4736 * @file: input #GFile
4737 * @attribute: a string containing the attribute's name
4738 * @value: a #guint64 containing the attribute's new value
4739 * @flags: a #GFileQueryInfoFlags
4740 * @cancellable: (allow-none): optional #GCancellable object,
4742 * @error: a #GError, or %NULL
4744 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4745 * If @attribute is of a different type, this operation will fail.
4747 * If @cancellable is not %NULL, then the operation can be cancelled by
4748 * triggering the cancellable object from another thread. If the operation
4749 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4751 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4754 g_file_set_attribute_int64 (GFile *file,
4755 const gchar *attribute,
4757 GFileQueryInfoFlags flags,
4758 GCancellable *cancellable,
4761 return g_file_set_attribute (file, attribute,
4762 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4763 flags, cancellable, error);
4767 * g_file_mount_mountable:
4768 * @file: input #GFile
4769 * @flags: flags affecting the operation
4770 * @mount_operation: (allow-none): a #GMountOperation,
4771 * or %NULL to avoid user interaction
4772 * @cancellable: (allow-none): optional #GCancellable object,
4774 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4775 * when the request is satisfied, or %NULL
4776 * @user_data: (closure): the data to pass to callback function
4778 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4779 * Using @mount_operation, you can request callbacks when, for instance,
4780 * passwords are needed during authentication.
4782 * If @cancellable is not %NULL, then the operation can be cancelled by
4783 * triggering the cancellable object from another thread. If the operation
4784 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4786 * When the operation is finished, @callback will be called.
4787 * You can then call g_file_mount_mountable_finish() to get
4788 * the result of the operation.
4791 g_file_mount_mountable (GFile *file,
4792 GMountMountFlags flags,
4793 GMountOperation *mount_operation,
4794 GCancellable *cancellable,
4795 GAsyncReadyCallback callback,
4800 g_return_if_fail (G_IS_FILE (file));
4802 iface = G_FILE_GET_IFACE (file);
4804 if (iface->mount_mountable == NULL)
4806 g_task_report_new_error (file, callback, user_data,
4807 g_file_mount_mountable,
4808 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4809 _("Operation not supported"));
4813 (* iface->mount_mountable) (file,
4822 * g_file_mount_mountable_finish:
4823 * @file: input #GFile
4824 * @result: a #GAsyncResult
4825 * @error: a #GError, or %NULL
4827 * Finishes a mount operation. See g_file_mount_mountable() for details.
4829 * Finish an asynchronous mount operation that was started
4830 * with g_file_mount_mountable().
4832 * Returns: (transfer full): a #GFile or %NULL on error.
4833 * Free the returned object with g_object_unref().
4836 g_file_mount_mountable_finish (GFile *file,
4837 GAsyncResult *result,
4842 g_return_val_if_fail (G_IS_FILE (file), NULL);
4843 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4845 if (g_async_result_legacy_propagate_error (result, error))
4847 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4848 return g_task_propagate_pointer (G_TASK (result), error);
4850 iface = G_FILE_GET_IFACE (file);
4851 return (* iface->mount_mountable_finish) (file, result, error);
4855 * g_file_unmount_mountable:
4856 * @file: input #GFile
4857 * @flags: flags affecting the operation
4858 * @cancellable: (allow-none): optional #GCancellable object,
4860 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4861 * when the request is satisfied, or %NULL
4862 * @user_data: (closure): the data to pass to callback function
4864 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4866 * If @cancellable is not %NULL, then the operation can be cancelled by
4867 * triggering the cancellable object from another thread. If the operation
4868 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4870 * When the operation is finished, @callback will be called.
4871 * You can then call g_file_unmount_mountable_finish() to get
4872 * the result of the operation.
4874 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4877 g_file_unmount_mountable (GFile *file,
4878 GMountUnmountFlags flags,
4879 GCancellable *cancellable,
4880 GAsyncReadyCallback callback,
4885 g_return_if_fail (G_IS_FILE (file));
4887 iface = G_FILE_GET_IFACE (file);
4889 if (iface->unmount_mountable == NULL)
4891 g_task_report_new_error (file, callback, user_data,
4892 g_file_unmount_mountable_with_operation,
4893 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4894 _("Operation not supported"));
4898 (* iface->unmount_mountable) (file,
4906 * g_file_unmount_mountable_finish:
4907 * @file: input #GFile
4908 * @result: a #GAsyncResult
4909 * @error: a #GError, or %NULL
4911 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4913 * Finish an asynchronous unmount operation that was started
4914 * with g_file_unmount_mountable().
4916 * Returns: %TRUE if the operation finished successfully.
4919 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4923 g_file_unmount_mountable_finish (GFile *file,
4924 GAsyncResult *result,
4929 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4930 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4932 if (g_async_result_legacy_propagate_error (result, error))
4934 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4935 return g_task_propagate_boolean (G_TASK (result), error);
4937 iface = G_FILE_GET_IFACE (file);
4938 return (* iface->unmount_mountable_finish) (file, result, error);
4942 * g_file_unmount_mountable_with_operation:
4943 * @file: input #GFile
4944 * @flags: flags affecting the operation
4945 * @mount_operation: (allow-none): a #GMountOperation,
4946 * or %NULL to avoid user interaction
4947 * @cancellable: (allow-none): optional #GCancellable object,
4949 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4950 * when the request is satisfied, or %NULL
4951 * @user_data: (closure): the data to pass to callback function
4953 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4955 * If @cancellable is not %NULL, then the operation can be cancelled by
4956 * triggering the cancellable object from another thread. If the operation
4957 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4959 * When the operation is finished, @callback will be called.
4960 * You can then call g_file_unmount_mountable_finish() to get
4961 * the result of the operation.
4966 g_file_unmount_mountable_with_operation (GFile *file,
4967 GMountUnmountFlags flags,
4968 GMountOperation *mount_operation,
4969 GCancellable *cancellable,
4970 GAsyncReadyCallback callback,
4975 g_return_if_fail (G_IS_FILE (file));
4977 iface = G_FILE_GET_IFACE (file);
4979 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4981 g_task_report_new_error (file, callback, user_data,
4982 g_file_unmount_mountable_with_operation,
4983 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4984 _("Operation not supported"));
4988 if (iface->unmount_mountable_with_operation != NULL)
4989 (* iface->unmount_mountable_with_operation) (file,
4996 (* iface->unmount_mountable) (file,
5004 * g_file_unmount_mountable_with_operation_finish:
5005 * @file: input #GFile
5006 * @result: a #GAsyncResult
5007 * @error: a #GError, or %NULL
5009 * Finishes an unmount operation,
5010 * see g_file_unmount_mountable_with_operation() for details.
5012 * Finish an asynchronous unmount operation that was started
5013 * with g_file_unmount_mountable_with_operation().
5015 * Returns: %TRUE if the operation finished successfully.
5021 g_file_unmount_mountable_with_operation_finish (GFile *file,
5022 GAsyncResult *result,
5027 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5028 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5030 if (g_async_result_legacy_propagate_error (result, error))
5032 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5033 return g_task_propagate_boolean (G_TASK (result), error);
5035 iface = G_FILE_GET_IFACE (file);
5036 if (iface->unmount_mountable_with_operation_finish != NULL)
5037 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5039 return (* iface->unmount_mountable_finish) (file, result, error);
5043 * g_file_eject_mountable:
5044 * @file: input #GFile
5045 * @flags: flags affecting the operation
5046 * @cancellable: (allow-none): optional #GCancellable object,
5048 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5049 * when the request is satisfied, or %NULL
5050 * @user_data: (closure): the data to pass to callback function
5052 * Starts an asynchronous eject on a mountable.
5053 * When this operation has completed, @callback will be called with
5054 * @user_user data, and the operation can be finalized with
5055 * g_file_eject_mountable_finish().
5057 * If @cancellable is not %NULL, then the operation can be cancelled by
5058 * triggering the cancellable object from another thread. If the operation
5059 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5061 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5064 g_file_eject_mountable (GFile *file,
5065 GMountUnmountFlags flags,
5066 GCancellable *cancellable,
5067 GAsyncReadyCallback callback,
5072 g_return_if_fail (G_IS_FILE (file));
5074 iface = G_FILE_GET_IFACE (file);
5076 if (iface->eject_mountable == NULL)
5078 g_task_report_new_error (file, callback, user_data,
5079 g_file_eject_mountable_with_operation,
5080 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5081 _("Operation not supported"));
5085 (* iface->eject_mountable) (file,
5093 * g_file_eject_mountable_finish:
5094 * @file: input #GFile
5095 * @result: a #GAsyncResult
5096 * @error: a #GError, or %NULL
5098 * Finishes an asynchronous eject operation started by
5099 * g_file_eject_mountable().
5101 * Returns: %TRUE if the @file was ejected successfully.
5104 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5108 g_file_eject_mountable_finish (GFile *file,
5109 GAsyncResult *result,
5114 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5115 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5117 if (g_async_result_legacy_propagate_error (result, error))
5119 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5120 return g_task_propagate_boolean (G_TASK (result), error);
5122 iface = G_FILE_GET_IFACE (file);
5123 return (* iface->eject_mountable_finish) (file, result, error);
5127 * g_file_eject_mountable_with_operation:
5128 * @file: input #GFile
5129 * @flags: flags affecting the operation
5130 * @mount_operation: (allow-none): a #GMountOperation,
5131 * or %NULL to avoid user interaction
5132 * @cancellable: (allow-none): optional #GCancellable object,
5134 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5135 * when the request is satisfied, or %NULL
5136 * @user_data: (closure): the data to pass to callback function
5138 * Starts an asynchronous eject on a mountable.
5139 * When this operation has completed, @callback will be called with
5140 * @user_user data, and the operation can be finalized with
5141 * g_file_eject_mountable_with_operation_finish().
5143 * If @cancellable is not %NULL, then the operation can be cancelled by
5144 * triggering the cancellable object from another thread. If the operation
5145 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5150 g_file_eject_mountable_with_operation (GFile *file,
5151 GMountUnmountFlags flags,
5152 GMountOperation *mount_operation,
5153 GCancellable *cancellable,
5154 GAsyncReadyCallback callback,
5159 g_return_if_fail (G_IS_FILE (file));
5161 iface = G_FILE_GET_IFACE (file);
5163 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5165 g_task_report_new_error (file, callback, user_data,
5166 g_file_eject_mountable_with_operation,
5167 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5168 _("Operation not supported"));
5172 if (iface->eject_mountable_with_operation != NULL)
5173 (* iface->eject_mountable_with_operation) (file,
5180 (* iface->eject_mountable) (file,
5188 * g_file_eject_mountable_with_operation_finish:
5189 * @file: input #GFile
5190 * @result: a #GAsyncResult
5191 * @error: a #GError, or %NULL
5193 * Finishes an asynchronous eject operation started by
5194 * g_file_eject_mountable_with_operation().
5196 * Returns: %TRUE if the @file was ejected successfully.
5202 g_file_eject_mountable_with_operation_finish (GFile *file,
5203 GAsyncResult *result,
5208 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5209 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5211 if (g_async_result_legacy_propagate_error (result, error))
5213 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5214 return g_task_propagate_boolean (G_TASK (result), error);
5216 iface = G_FILE_GET_IFACE (file);
5217 if (iface->eject_mountable_with_operation_finish != NULL)
5218 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5220 return (* iface->eject_mountable_finish) (file, result, error);
5224 * g_file_monitor_directory:
5225 * @file: input #GFile
5226 * @flags: a set of #GFileMonitorFlags
5227 * @cancellable: (allow-none): optional #GCancellable object,
5229 * @error: a #GError, or %NULL
5231 * Obtains a directory monitor for the given file.
5232 * This may fail if directory monitoring is not supported.
5234 * If @cancellable is not %NULL, then the operation can be cancelled by
5235 * triggering the cancellable object from another thread. If the operation
5236 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5238 * It does not make sense for @flags to contain
5239 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5240 * directories. It is not possible to monitor all the files in a
5241 * directory for changes made via hard links; if you want to do this then
5242 * you must register individual watches with g_file_monitor().
5244 * Virtual: monitor_dir
5245 * Returns: (transfer full): a #GFileMonitor for the given @file,
5246 * or %NULL on error.
5247 * Free the returned object with g_object_unref().
5250 g_file_monitor_directory (GFile *file,
5251 GFileMonitorFlags flags,
5252 GCancellable *cancellable,
5257 g_return_val_if_fail (G_IS_FILE (file), NULL);
5258 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5260 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5263 iface = G_FILE_GET_IFACE (file);
5265 if (iface->monitor_dir == NULL)
5267 g_set_error_literal (error, G_IO_ERROR,
5268 G_IO_ERROR_NOT_SUPPORTED,
5269 _("Operation not supported"));
5273 return (* iface->monitor_dir) (file, flags, cancellable, error);
5277 * g_file_monitor_file:
5278 * @file: input #GFile
5279 * @flags: a set of #GFileMonitorFlags
5280 * @cancellable: (allow-none): optional #GCancellable object,
5282 * @error: a #GError, or %NULL
5284 * Obtains a file monitor for the given file. If no file notification
5285 * mechanism exists, then regular polling of the file is used.
5287 * If @cancellable is not %NULL, then the operation can be cancelled by
5288 * triggering the cancellable object from another thread. If the operation
5289 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5291 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5292 * will also attempt to report changes made to the file via another
5293 * filename (ie, a hard link). Without this flag, you can only rely on
5294 * changes made through the filename contained in @file to be
5295 * reported. Using this flag may result in an increase in resource
5296 * usage, and may not have any effect depending on the #GFileMonitor
5297 * backend and/or filesystem type.
5299 * Returns: (transfer full): a #GFileMonitor for the given @file,
5300 * or %NULL on error.
5301 * Free the returned object with g_object_unref().
5304 g_file_monitor_file (GFile *file,
5305 GFileMonitorFlags flags,
5306 GCancellable *cancellable,
5310 GFileMonitor *monitor;
5312 g_return_val_if_fail (G_IS_FILE (file), NULL);
5314 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5317 iface = G_FILE_GET_IFACE (file);
5321 if (iface->monitor_file)
5322 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5324 /* Fallback to polling */
5325 if (monitor == NULL)
5326 monitor = _g_poll_file_monitor_new (file);
5333 * @file: input #GFile
5334 * @flags: a set of #GFileMonitorFlags
5335 * @cancellable: (allow-none): optional #GCancellable object,
5337 * @error: a #GError, or %NULL
5339 * Obtains a file or directory monitor for the given file,
5340 * depending on the type of the file.
5342 * If @cancellable is not %NULL, then the operation can be cancelled by
5343 * triggering the cancellable object from another thread. If the operation
5344 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5346 * Returns: (transfer full): a #GFileMonitor for the given @file,
5347 * or %NULL on error.
5348 * Free the returned object with g_object_unref().
5353 g_file_monitor (GFile *file,
5354 GFileMonitorFlags flags,
5355 GCancellable *cancellable,
5358 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5359 return g_file_monitor_directory (file,
5360 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5361 cancellable, error);
5363 return g_file_monitor_file (file, flags, cancellable, error);
5366 /********************************************
5367 * Default implementation of async ops *
5368 ********************************************/
5372 GFileQueryInfoFlags flags;
5373 } QueryInfoAsyncData;
5376 query_info_data_free (QueryInfoAsyncData *data)
5378 g_free (data->attributes);
5383 query_info_async_thread (GTask *task,
5386 GCancellable *cancellable)
5388 QueryInfoAsyncData *data = task_data;
5390 GError *error = NULL;
5392 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5394 g_task_return_pointer (task, info, g_object_unref);
5396 g_task_return_error (task, error);
5400 g_file_real_query_info_async (GFile *file,
5401 const char *attributes,
5402 GFileQueryInfoFlags flags,
5404 GCancellable *cancellable,
5405 GAsyncReadyCallback callback,
5409 QueryInfoAsyncData *data;
5411 data = g_new0 (QueryInfoAsyncData, 1);
5412 data->attributes = g_strdup (attributes);
5413 data->flags = flags;
5415 task = g_task_new (file, cancellable, callback, user_data);
5416 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5417 g_task_set_priority (task, io_priority);
5418 g_task_run_in_thread (task, query_info_async_thread);
5419 g_object_unref (task);
5423 g_file_real_query_info_finish (GFile *file,
5427 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5429 return g_task_propagate_pointer (G_TASK (res), error);
5433 query_filesystem_info_async_thread (GTask *task,
5436 GCancellable *cancellable)
5438 const char *attributes = task_data;
5440 GError *error = NULL;
5442 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5444 g_task_return_pointer (task, info, g_object_unref);
5446 g_task_return_error (task, error);
5450 g_file_real_query_filesystem_info_async (GFile *file,
5451 const char *attributes,
5453 GCancellable *cancellable,
5454 GAsyncReadyCallback callback,
5459 task = g_task_new (file, cancellable, callback, user_data);
5460 g_task_set_task_data (task, g_strdup (attributes), g_free);
5461 g_task_set_priority (task, io_priority);
5462 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5463 g_object_unref (task);
5467 g_file_real_query_filesystem_info_finish (GFile *file,
5471 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5473 return g_task_propagate_pointer (G_TASK (res), error);
5477 enumerate_children_async_thread (GTask *task,
5480 GCancellable *cancellable)
5482 QueryInfoAsyncData *data = task_data;
5483 GFileEnumerator *enumerator;
5484 GError *error = NULL;
5486 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5488 g_task_return_error (task, error);
5490 g_task_return_pointer (task, enumerator, g_object_unref);
5494 g_file_real_enumerate_children_async (GFile *file,
5495 const char *attributes,
5496 GFileQueryInfoFlags flags,
5498 GCancellable *cancellable,
5499 GAsyncReadyCallback callback,
5503 QueryInfoAsyncData *data;
5505 data = g_new0 (QueryInfoAsyncData, 1);
5506 data->attributes = g_strdup (attributes);
5507 data->flags = flags;
5509 task = g_task_new (file, cancellable, callback, user_data);
5510 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5511 g_task_set_priority (task, io_priority);
5512 g_task_run_in_thread (task, enumerate_children_async_thread);
5513 g_object_unref (task);
5516 static GFileEnumerator *
5517 g_file_real_enumerate_children_finish (GFile *file,
5521 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5523 return g_task_propagate_pointer (G_TASK (res), error);
5527 open_read_async_thread (GTask *task,
5530 GCancellable *cancellable)
5532 GFileInputStream *stream;
5533 GError *error = NULL;
5535 stream = g_file_read (G_FILE (object), cancellable, &error);
5537 g_task_return_pointer (task, stream, g_object_unref);
5539 g_task_return_error (task, error);
5543 g_file_real_read_async (GFile *file,
5545 GCancellable *cancellable,
5546 GAsyncReadyCallback callback,
5551 task = g_task_new (file, cancellable, callback, user_data);
5552 g_task_set_priority (task, io_priority);
5553 g_task_run_in_thread (task, open_read_async_thread);
5554 g_object_unref (task);
5557 static GFileInputStream *
5558 g_file_real_read_finish (GFile *file,
5562 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5564 return g_task_propagate_pointer (G_TASK (res), error);
5568 append_to_async_thread (GTask *task,
5569 gpointer source_object,
5571 GCancellable *cancellable)
5573 GFileCreateFlags *data = task_data;
5574 GFileOutputStream *stream;
5575 GError *error = NULL;
5577 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5579 g_task_return_pointer (task, stream, g_object_unref);
5581 g_task_return_error (task, error);
5585 g_file_real_append_to_async (GFile *file,
5586 GFileCreateFlags flags,
5588 GCancellable *cancellable,
5589 GAsyncReadyCallback callback,
5592 GFileCreateFlags *data;
5595 data = g_new0 (GFileCreateFlags, 1);
5598 task = g_task_new (file, cancellable, callback, user_data);
5599 g_task_set_task_data (task, data, g_free);
5600 g_task_set_priority (task, io_priority);
5602 g_task_run_in_thread (task, append_to_async_thread);
5603 g_object_unref (task);
5606 static GFileOutputStream *
5607 g_file_real_append_to_finish (GFile *file,
5611 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5613 return g_task_propagate_pointer (G_TASK (res), error);
5617 create_async_thread (GTask *task,
5618 gpointer source_object,
5620 GCancellable *cancellable)
5622 GFileCreateFlags *data = task_data;
5623 GFileOutputStream *stream;
5624 GError *error = NULL;
5626 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5628 g_task_return_pointer (task, stream, g_object_unref);
5630 g_task_return_error (task, error);
5634 g_file_real_create_async (GFile *file,
5635 GFileCreateFlags flags,
5637 GCancellable *cancellable,
5638 GAsyncReadyCallback callback,
5641 GFileCreateFlags *data;
5644 data = g_new0 (GFileCreateFlags, 1);
5647 task = g_task_new (file, cancellable, callback, user_data);
5648 g_task_set_task_data (task, data, g_free);
5649 g_task_set_priority (task, io_priority);
5651 g_task_run_in_thread (task, create_async_thread);
5652 g_object_unref (task);
5655 static GFileOutputStream *
5656 g_file_real_create_finish (GFile *file,
5660 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5662 return g_task_propagate_pointer (G_TASK (res), error);
5666 GFileOutputStream *stream;
5668 gboolean make_backup;
5669 GFileCreateFlags flags;
5673 replace_async_data_free (ReplaceAsyncData *data)
5676 g_object_unref (data->stream);
5677 g_free (data->etag);
5682 replace_async_thread (GTask *task,
5683 gpointer source_object,
5685 GCancellable *cancellable)
5687 GFileOutputStream *stream;
5688 ReplaceAsyncData *data = task_data;
5689 GError *error = NULL;
5691 stream = g_file_replace (G_FILE (source_object),
5699 g_task_return_pointer (task, stream, g_object_unref);
5701 g_task_return_error (task, error);
5705 g_file_real_replace_async (GFile *file,
5707 gboolean make_backup,
5708 GFileCreateFlags flags,
5710 GCancellable *cancellable,
5711 GAsyncReadyCallback callback,
5715 ReplaceAsyncData *data;
5717 data = g_new0 (ReplaceAsyncData, 1);
5718 data->etag = g_strdup (etag);
5719 data->make_backup = make_backup;
5720 data->flags = flags;
5722 task = g_task_new (file, cancellable, callback, user_data);
5723 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5724 g_task_set_priority (task, io_priority);
5726 g_task_run_in_thread (task, replace_async_thread);
5727 g_object_unref (task);
5730 static GFileOutputStream *
5731 g_file_real_replace_finish (GFile *file,
5735 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5737 return g_task_propagate_pointer (G_TASK (res), error);
5741 delete_async_thread (GTask *task,
5744 GCancellable *cancellable)
5746 GError *error = NULL;
5748 if (g_file_delete (G_FILE (object), cancellable, &error))
5749 g_task_return_boolean (task, TRUE);
5751 g_task_return_error (task, error);
5755 g_file_real_delete_async (GFile *file,
5757 GCancellable *cancellable,
5758 GAsyncReadyCallback callback,
5763 task = g_task_new (file, cancellable, callback, user_data);
5764 g_task_set_priority (task, io_priority);
5765 g_task_run_in_thread (task, delete_async_thread);
5766 g_object_unref (task);
5770 g_file_real_delete_finish (GFile *file,
5774 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5776 return g_task_propagate_boolean (G_TASK (res), error);
5780 trash_async_thread (GTask *task,
5783 GCancellable *cancellable)
5785 GError *error = NULL;
5787 if (g_file_trash (G_FILE (object), cancellable, &error))
5788 g_task_return_boolean (task, TRUE);
5790 g_task_return_error (task, error);
5794 g_file_real_trash_async (GFile *file,
5796 GCancellable *cancellable,
5797 GAsyncReadyCallback callback,
5802 task = g_task_new (file, cancellable, callback, user_data);
5803 g_task_set_priority (task, io_priority);
5804 g_task_run_in_thread (task, trash_async_thread);
5805 g_object_unref (task);
5809 g_file_real_trash_finish (GFile *file,
5813 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5815 return g_task_propagate_boolean (G_TASK (res), error);
5819 make_directory_async_thread (GTask *task,
5822 GCancellable *cancellable)
5824 GError *error = NULL;
5826 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5827 g_task_return_boolean (task, TRUE);
5829 g_task_return_error (task, error);
5833 g_file_real_make_directory_async (GFile *file,
5835 GCancellable *cancellable,
5836 GAsyncReadyCallback callback,
5841 task = g_task_new (file, cancellable, callback, user_data);
5842 g_task_set_priority (task, io_priority);
5843 g_task_run_in_thread (task, make_directory_async_thread);
5844 g_object_unref (task);
5848 g_file_real_make_directory_finish (GFile *file,
5852 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5854 return g_task_propagate_boolean (G_TASK (res), error);
5858 open_readwrite_async_thread (GTask *task,
5861 GCancellable *cancellable)
5863 GFileIOStream *stream;
5864 GError *error = NULL;
5866 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5869 g_task_return_error (task, error);
5871 g_task_return_pointer (task, stream, g_object_unref);
5875 g_file_real_open_readwrite_async (GFile *file,
5877 GCancellable *cancellable,
5878 GAsyncReadyCallback callback,
5883 task = g_task_new (file, cancellable, callback, user_data);
5884 g_task_set_priority (task, io_priority);
5886 g_task_run_in_thread (task, open_readwrite_async_thread);
5887 g_object_unref (task);
5890 static GFileIOStream *
5891 g_file_real_open_readwrite_finish (GFile *file,
5895 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5897 return g_task_propagate_pointer (G_TASK (res), error);
5901 create_readwrite_async_thread (GTask *task,
5904 GCancellable *cancellable)
5906 GFileCreateFlags *data = task_data;
5907 GFileIOStream *stream;
5908 GError *error = NULL;
5910 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5913 g_task_return_error (task, error);
5915 g_task_return_pointer (task, stream, g_object_unref);
5919 g_file_real_create_readwrite_async (GFile *file,
5920 GFileCreateFlags flags,
5922 GCancellable *cancellable,
5923 GAsyncReadyCallback callback,
5926 GFileCreateFlags *data;
5929 data = g_new0 (GFileCreateFlags, 1);
5932 task = g_task_new (file, cancellable, callback, user_data);
5933 g_task_set_task_data (task, data, g_free);
5934 g_task_set_priority (task, io_priority);
5936 g_task_run_in_thread (task, create_readwrite_async_thread);
5937 g_object_unref (task);
5940 static GFileIOStream *
5941 g_file_real_create_readwrite_finish (GFile *file,
5945 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5947 return g_task_propagate_pointer (G_TASK (res), error);
5952 gboolean make_backup;
5953 GFileCreateFlags flags;
5954 } ReplaceRWAsyncData;
5957 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5959 g_free (data->etag);
5964 replace_readwrite_async_thread (GTask *task,
5967 GCancellable *cancellable)
5969 GFileIOStream *stream;
5970 GError *error = NULL;
5971 ReplaceRWAsyncData *data = task_data;
5973 stream = g_file_replace_readwrite (G_FILE (object),
5981 g_task_return_error (task, error);
5983 g_task_return_pointer (task, stream, g_object_unref);
5987 g_file_real_replace_readwrite_async (GFile *file,
5989 gboolean make_backup,
5990 GFileCreateFlags flags,
5992 GCancellable *cancellable,
5993 GAsyncReadyCallback callback,
5997 ReplaceRWAsyncData *data;
5999 data = g_new0 (ReplaceRWAsyncData, 1);
6000 data->etag = g_strdup (etag);
6001 data->make_backup = make_backup;
6002 data->flags = flags;
6004 task = g_task_new (file, cancellable, callback, user_data);
6005 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
6006 g_task_set_priority (task, io_priority);
6008 g_task_run_in_thread (task, replace_readwrite_async_thread);
6009 g_object_unref (task);
6012 static GFileIOStream *
6013 g_file_real_replace_readwrite_finish (GFile *file,
6017 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6019 return g_task_propagate_pointer (G_TASK (res), error);
6023 set_display_name_async_thread (GTask *task,
6026 GCancellable *cancellable)
6028 GError *error = NULL;
6029 char *name = task_data;
6032 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6035 g_task_return_error (task, error);
6037 g_task_return_pointer (task, file, g_object_unref);
6041 g_file_real_set_display_name_async (GFile *file,
6042 const char *display_name,
6044 GCancellable *cancellable,
6045 GAsyncReadyCallback callback,
6050 task = g_task_new (file, cancellable, callback, user_data);
6051 g_task_set_task_data (task, g_strdup (display_name), g_free);
6052 g_task_set_priority (task, io_priority);
6054 g_task_run_in_thread (task, set_display_name_async_thread);
6055 g_object_unref (task);
6059 g_file_real_set_display_name_finish (GFile *file,
6063 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6065 return g_task_propagate_pointer (G_TASK (res), error);
6069 GFileQueryInfoFlags flags;
6076 set_info_data_free (SetInfoAsyncData *data)
6079 g_object_unref (data->info);
6081 g_error_free (data->error);
6086 set_info_async_thread (GTask *task,
6089 GCancellable *cancellable)
6091 SetInfoAsyncData *data = task_data;
6094 data->res = g_file_set_attributes_from_info (G_FILE (object),
6102 g_file_real_set_attributes_async (GFile *file,
6104 GFileQueryInfoFlags flags,
6106 GCancellable *cancellable,
6107 GAsyncReadyCallback callback,
6111 SetInfoAsyncData *data;
6113 data = g_new0 (SetInfoAsyncData, 1);
6114 data->info = g_file_info_dup (info);
6115 data->flags = flags;
6117 task = g_task_new (file, cancellable, callback, user_data);
6118 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6119 g_task_set_priority (task, io_priority);
6121 g_task_run_in_thread (task, set_info_async_thread);
6122 g_object_unref (task);
6126 g_file_real_set_attributes_finish (GFile *file,
6131 SetInfoAsyncData *data;
6133 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6135 data = g_task_get_task_data (G_TASK (res));
6138 *info = g_object_ref (data->info);
6140 if (error != NULL && data->error)
6141 *error = g_error_copy (data->error);
6147 find_enclosing_mount_async_thread (GTask *task,
6150 GCancellable *cancellable)
6152 GError *error = NULL;
6155 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6158 g_task_return_error (task, error);
6160 g_task_return_pointer (task, mount, g_object_unref);
6164 g_file_real_find_enclosing_mount_async (GFile *file,
6166 GCancellable *cancellable,
6167 GAsyncReadyCallback callback,
6172 task = g_task_new (file, cancellable, callback, user_data);
6173 g_task_set_priority (task, io_priority);
6175 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6176 g_object_unref (task);
6180 g_file_real_find_enclosing_mount_finish (GFile *file,
6184 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6186 return g_task_propagate_pointer (G_TASK (res), error);
6193 GFileCopyFlags flags;
6194 GFileProgressCallback progress_cb;
6195 gpointer progress_cb_data;
6199 copy_async_data_free (CopyAsyncData *data)
6201 g_object_unref (data->source);
6202 g_object_unref (data->destination);
6203 g_slice_free (CopyAsyncData, data);
6207 CopyAsyncData *data;
6208 goffset current_num_bytes;
6209 goffset total_num_bytes;
6213 copy_async_progress_in_main (gpointer user_data)
6215 ProgressData *progress = user_data;
6216 CopyAsyncData *data = progress->data;
6218 data->progress_cb (progress->current_num_bytes,
6219 progress->total_num_bytes,
6220 data->progress_cb_data);
6226 copy_async_progress_callback (goffset current_num_bytes,
6227 goffset total_num_bytes,
6230 GTask *task = user_data;
6231 CopyAsyncData *data = g_task_get_task_data (task);
6232 ProgressData *progress;
6234 progress = g_new (ProgressData, 1);
6235 progress->data = data;
6236 progress->current_num_bytes = current_num_bytes;
6237 progress->total_num_bytes = total_num_bytes;
6239 g_main_context_invoke_full (g_task_get_context (task),
6240 g_task_get_priority (task),
6241 copy_async_progress_in_main,
6247 copy_async_thread (GTask *task,
6250 GCancellable *cancellable)
6252 CopyAsyncData *data = task_data;
6254 GError *error = NULL;
6256 result = g_file_copy (data->source,
6260 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6264 g_task_return_boolean (task, TRUE);
6266 g_task_return_error (task, error);
6270 g_file_real_copy_async (GFile *source,
6272 GFileCopyFlags flags,
6274 GCancellable *cancellable,
6275 GFileProgressCallback progress_callback,
6276 gpointer progress_callback_data,
6277 GAsyncReadyCallback callback,
6281 CopyAsyncData *data;
6283 data = g_slice_new (CopyAsyncData);
6284 data->source = g_object_ref (source);
6285 data->destination = g_object_ref (destination);
6286 data->flags = flags;
6287 data->progress_cb = progress_callback;
6288 data->progress_cb_data = progress_callback_data;
6290 task = g_task_new (source, cancellable, callback, user_data);
6291 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6292 g_task_set_priority (task, io_priority);
6293 g_task_run_in_thread (task, copy_async_thread);
6294 g_object_unref (task);
6298 g_file_real_copy_finish (GFile *file,
6302 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6304 return g_task_propagate_boolean (G_TASK (res), error);
6308 /********************************************
6309 * Default VFS operations *
6310 ********************************************/
6313 * g_file_new_for_path:
6314 * @path: a string containing a relative or absolute path.
6315 * The string must be encoded in the glib filename encoding.
6317 * Constructs a #GFile for a given path. This operation never
6318 * fails, but the returned object might not support any I/O
6319 * operation if @path is malformed.
6321 * Returns: (transfer full): a new #GFile for the given @path.
6322 * Free the returned object with g_object_unref().
6325 g_file_new_for_path (const char *path)
6327 g_return_val_if_fail (path != NULL, NULL);
6329 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6333 * g_file_new_for_uri:
6334 * @uri: a UTF-8 string containing a URI
6336 * Constructs a #GFile for a given URI. This operation never
6337 * fails, but the returned object might not support any I/O
6338 * operation if @uri is malformed or if the uri type is
6341 * Returns: (transfer full): a new #GFile for the given @uri.
6342 * Free the returned object with g_object_unref().
6345 g_file_new_for_uri (const char *uri)
6347 g_return_val_if_fail (uri != NULL, NULL);
6349 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6354 * @tmpl: (type filename) (allow-none): Template for the file
6355 * name, as in g_file_open_tmp(), or %NULL for a default template
6356 * @iostream: (out): on return, a #GFileIOStream for the created file
6357 * @error: a #GError, or %NULL
6359 * Opens a file in the preferred directory for temporary files (as
6360 * returned by g_get_tmp_dir()) and returns a #GFile and
6361 * #GFileIOStream pointing to it.
6363 * @tmpl should be a string in the GLib file name encoding
6364 * containing a sequence of six 'X' characters, and containing no
6365 * directory components. If it is %NULL, a default template is used.
6367 * Unlike the other #GFile constructors, this will return %NULL if
6368 * a temporary file could not be created.
6370 * Returns: (transfer full): a new #GFile.
6371 * Free the returned object with g_object_unref().
6376 g_file_new_tmp (const char *tmpl,
6377 GFileIOStream **iostream,
6383 GFileOutputStream *output;
6385 g_return_val_if_fail (iostream != NULL, NULL);
6387 fd = g_file_open_tmp (tmpl, &path, error);
6391 file = g_file_new_for_path (path);
6393 output = _g_local_file_output_stream_new (fd);
6394 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6396 g_object_unref (output);
6403 * g_file_parse_name:
6404 * @parse_name: a file name or path to be parsed
6406 * Constructs a #GFile with the given @parse_name (i.e. something
6407 * given by g_file_get_parse_name()). This operation never fails,
6408 * but the returned object might not support any I/O operation if
6409 * the @parse_name cannot be parsed.
6411 * Returns: (transfer full): a new #GFile.
6414 g_file_parse_name (const char *parse_name)
6416 g_return_val_if_fail (parse_name != NULL, NULL);
6418 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6422 is_valid_scheme_character (char c)
6424 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6427 /* Following RFC 2396, valid schemes are built like:
6428 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6431 has_valid_scheme (const char *uri)
6437 if (!g_ascii_isalpha (*p))
6442 } while (is_valid_scheme_character (*p));
6448 new_for_cmdline_arg (const gchar *arg,
6454 if (g_path_is_absolute (arg))
6455 return g_file_new_for_path (arg);
6457 if (has_valid_scheme (arg))
6458 return g_file_new_for_uri (arg);
6464 current_dir = g_get_current_dir ();
6465 filename = g_build_filename (current_dir, arg, NULL);
6466 g_free (current_dir);
6469 filename = g_build_filename (cwd, arg, NULL);
6471 file = g_file_new_for_path (filename);
6478 * g_file_new_for_commandline_arg:
6479 * @arg: a command line string
6481 * Creates a #GFile with the given argument from the command line.
6482 * The value of @arg can be either a URI, an absolute path or a
6483 * relative path resolved relative to the current working directory.
6484 * This operation never fails, but the returned object might not
6485 * support any I/O operation if @arg points to a malformed path.
6487 * Note that on Windows, this function expects its argument to be in
6488 * UTF-8 -- not the system code page. This means that you
6489 * should not use this function with string from argv as it is passed
6490 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6491 * the commandline. #GApplication also uses UTF-8 but
6492 * g_application_command_line_create_file_for_arg() may be more useful
6493 * for you there. It is also always possible to use this function with
6494 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6496 * Returns: (transfer full): a new #GFile.
6497 * Free the returned object with g_object_unref().
6500 g_file_new_for_commandline_arg (const char *arg)
6502 g_return_val_if_fail (arg != NULL, NULL);
6504 return new_for_cmdline_arg (arg, NULL);
6508 * g_file_new_for_commandline_arg_and_cwd:
6509 * @arg: a command line string
6510 * @cwd: the current working directory of the commandline
6512 * Creates a #GFile with the given argument from the command line.
6514 * This function is similar to g_file_new_for_commandline_arg() except
6515 * that it allows for passing the current working directory as an
6516 * argument instead of using the current working directory of the
6519 * This is useful if the commandline argument was given in a context
6520 * other than the invocation of the current process.
6522 * See also g_application_command_line_create_file_for_arg().
6524 * Returns: (transfer full): a new #GFile
6529 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6532 g_return_val_if_fail (arg != NULL, NULL);
6533 g_return_val_if_fail (cwd != NULL, NULL);
6535 return new_for_cmdline_arg (arg, cwd);
6539 * g_file_mount_enclosing_volume:
6540 * @location: input #GFile
6541 * @flags: flags affecting the operation
6542 * @mount_operation: (allow-none): a #GMountOperation
6543 * or %NULL to avoid user interaction
6544 * @cancellable: (allow-none): optional #GCancellable object,
6546 * @callback: (allow-none): a #GAsyncReadyCallback to call
6547 * when the request is satisfied, or %NULL
6548 * @user_data: the data to pass to callback function
6550 * Starts a @mount_operation, mounting the volume that contains
6551 * the file @location.
6553 * When this operation has completed, @callback will be called with
6554 * @user_user data, and the operation can be finalized with
6555 * g_file_mount_enclosing_volume_finish().
6557 * If @cancellable is not %NULL, then the operation can be cancelled by
6558 * triggering the cancellable object from another thread. If the operation
6559 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6562 g_file_mount_enclosing_volume (GFile *location,
6563 GMountMountFlags flags,
6564 GMountOperation *mount_operation,
6565 GCancellable *cancellable,
6566 GAsyncReadyCallback callback,
6571 g_return_if_fail (G_IS_FILE (location));
6573 iface = G_FILE_GET_IFACE (location);
6575 if (iface->mount_enclosing_volume == NULL)
6577 g_task_report_new_error (location, callback, user_data,
6578 g_file_mount_enclosing_volume,
6579 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6580 _("volume doesn't implement mount"));
6584 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6589 * g_file_mount_enclosing_volume_finish:
6590 * @location: input #GFile
6591 * @result: a #GAsyncResult
6592 * @error: a #GError, or %NULL
6594 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6596 * Returns: %TRUE if successful. If an error has occurred,
6597 * this function will return %FALSE and set @error
6598 * appropriately if present.
6601 g_file_mount_enclosing_volume_finish (GFile *location,
6602 GAsyncResult *result,
6607 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6608 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6610 if (g_async_result_legacy_propagate_error (result, error))
6612 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6613 return g_task_propagate_boolean (G_TASK (result), error);
6615 iface = G_FILE_GET_IFACE (location);
6617 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6620 /********************************************
6621 * Utility functions *
6622 ********************************************/
6625 * g_file_query_default_handler:
6626 * @file: a #GFile to open
6627 * @cancellable: optional #GCancellable object, %NULL to ignore
6628 * @error: a #GError, or %NULL
6630 * Returns the #GAppInfo that is registered as the default
6631 * application to handle the file specified by @file.
6633 * If @cancellable is not %NULL, then the operation can be cancelled by
6634 * triggering the cancellable object from another thread. If the operation
6635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6637 * Returns: (transfer full): a #GAppInfo if the handle was found,
6638 * %NULL if there were errors.
6639 * When you are done with it, release it with g_object_unref()
6642 g_file_query_default_handler (GFile *file,
6643 GCancellable *cancellable,
6647 const char *content_type;
6652 uri_scheme = g_file_get_uri_scheme (file);
6653 if (uri_scheme && uri_scheme[0] != '\0')
6655 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6656 g_free (uri_scheme);
6658 if (appinfo != NULL)
6662 info = g_file_query_info (file,
6663 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6672 content_type = g_file_info_get_content_type (info);
6675 /* Don't use is_native(), as we want to support fuse paths if available */
6676 path = g_file_get_path (file);
6677 appinfo = g_app_info_get_default_for_type (content_type,
6682 g_object_unref (info);
6684 if (appinfo != NULL)
6687 g_set_error_literal (error, G_IO_ERROR,
6688 G_IO_ERROR_NOT_SUPPORTED,
6689 _("No application is registered as handling this file"));
6693 #define GET_CONTENT_BLOCK_SIZE 8192
6696 * g_file_load_contents:
6697 * @file: input #GFile
6698 * @cancellable: optional #GCancellable object, %NULL to ignore
6699 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6700 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6701 * or %NULL if the length is not needed
6702 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6703 * or %NULL if the entity tag is not needed
6704 * @error: a #GError, or %NULL
6706 * Loads the content of the file into memory. The data is always
6707 * zero-terminated, but this is not included in the resultant @length.
6708 * The returned @content should be freed with g_free() when no longer
6711 * If @cancellable is not %NULL, then the operation can be cancelled by
6712 * triggering the cancellable object from another thread. If the operation
6713 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6715 * Returns: %TRUE if the @file's contents were successfully loaded.
6716 * %FALSE if there were errors.
6719 g_file_load_contents (GFile *file,
6720 GCancellable *cancellable,
6726 GFileInputStream *in;
6727 GByteArray *content;
6732 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6733 g_return_val_if_fail (contents != NULL, FALSE);
6735 in = g_file_read (file, cancellable, error);
6739 content = g_byte_array_new ();
6742 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6743 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6744 content->data + pos,
6745 GET_CONTENT_BLOCK_SIZE,
6746 cancellable, error)) > 0)
6749 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6756 info = g_file_input_stream_query_info (in,
6757 G_FILE_ATTRIBUTE_ETAG_VALUE,
6762 *etag_out = g_strdup (g_file_info_get_etag (info));
6763 g_object_unref (info);
6767 /* Ignore errors on close */
6768 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6769 g_object_unref (in);
6773 /* error is set already */
6774 g_byte_array_free (content, TRUE);
6781 /* Zero terminate (we got an extra byte allocated for this */
6782 content->data[pos] = 0;
6784 *contents = (char *)g_byte_array_free (content, FALSE);
6791 GFileReadMoreCallback read_more_callback;
6792 GByteArray *content;
6799 load_contents_data_free (LoadContentsData *data)
6802 g_byte_array_free (data->content, TRUE);
6803 g_free (data->etag);
6808 load_contents_close_callback (GObject *obj,
6809 GAsyncResult *close_res,
6812 GInputStream *stream = G_INPUT_STREAM (obj);
6813 LoadContentsData *data = user_data;
6815 /* Ignore errors here, we're only reading anyway */
6816 g_input_stream_close_finish (stream, close_res, NULL);
6817 g_object_unref (stream);
6819 g_task_return_boolean (data->task, TRUE);
6820 g_object_unref (data->task);
6824 load_contents_fstat_callback (GObject *obj,
6825 GAsyncResult *stat_res,
6828 GInputStream *stream = G_INPUT_STREAM (obj);
6829 LoadContentsData *data = user_data;
6832 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6836 data->etag = g_strdup (g_file_info_get_etag (info));
6837 g_object_unref (info);
6840 g_input_stream_close_async (stream, 0,
6841 g_task_get_cancellable (data->task),
6842 load_contents_close_callback, data);
6846 load_contents_read_callback (GObject *obj,
6847 GAsyncResult *read_res,
6850 GInputStream *stream = G_INPUT_STREAM (obj);
6851 LoadContentsData *data = user_data;
6852 GError *error = NULL;
6855 read_size = g_input_stream_read_finish (stream, read_res, &error);
6859 g_task_return_error (data->task, error);
6860 g_object_unref (data->task);
6862 /* Close the file ignoring any error */
6863 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6864 g_object_unref (stream);
6866 else if (read_size == 0)
6868 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6869 G_FILE_ATTRIBUTE_ETAG_VALUE,
6871 g_task_get_cancellable (data->task),
6872 load_contents_fstat_callback,
6875 else if (read_size > 0)
6877 data->pos += read_size;
6879 g_byte_array_set_size (data->content,
6880 data->pos + GET_CONTENT_BLOCK_SIZE);
6883 if (data->read_more_callback &&
6884 !data->read_more_callback ((char *)data->content->data, data->pos,
6885 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6886 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6887 G_FILE_ATTRIBUTE_ETAG_VALUE,
6889 g_task_get_cancellable (data->task),
6890 load_contents_fstat_callback,
6893 g_input_stream_read_async (stream,
6894 data->content->data + data->pos,
6895 GET_CONTENT_BLOCK_SIZE,
6897 g_task_get_cancellable (data->task),
6898 load_contents_read_callback,
6904 load_contents_open_callback (GObject *obj,
6905 GAsyncResult *open_res,
6908 GFile *file = G_FILE (obj);
6909 GFileInputStream *stream;
6910 LoadContentsData *data = user_data;
6911 GError *error = NULL;
6913 stream = g_file_read_finish (file, open_res, &error);
6917 g_byte_array_set_size (data->content,
6918 data->pos + GET_CONTENT_BLOCK_SIZE);
6919 g_input_stream_read_async (G_INPUT_STREAM (stream),
6920 data->content->data + data->pos,
6921 GET_CONTENT_BLOCK_SIZE,
6923 g_task_get_cancellable (data->task),
6924 load_contents_read_callback,
6929 g_task_return_error (data->task, error);
6930 g_object_unref (data->task);
6935 * g_file_load_partial_contents_async: (skip)
6936 * @file: input #GFile
6937 * @cancellable: optional #GCancellable object, %NULL to ignore
6938 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6939 * and to specify whether further data should be read
6940 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6941 * @user_data: the data to pass to the callback functions
6943 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6944 * be used to stop reading from the file when appropriate, else this
6945 * function will behave exactly as g_file_load_contents_async(). This
6946 * operation can be finished by g_file_load_partial_contents_finish().
6948 * Users of this function should be aware that @user_data is passed to
6949 * both the @read_more_callback and the @callback.
6951 * If @cancellable is not %NULL, then the operation can be cancelled by
6952 * triggering the cancellable object from another thread. If the operation
6953 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6956 g_file_load_partial_contents_async (GFile *file,
6957 GCancellable *cancellable,
6958 GFileReadMoreCallback read_more_callback,
6959 GAsyncReadyCallback callback,
6962 LoadContentsData *data;
6964 g_return_if_fail (G_IS_FILE (file));
6966 data = g_new0 (LoadContentsData, 1);
6967 data->read_more_callback = read_more_callback;
6968 data->content = g_byte_array_new ();
6970 data->task = g_task_new (file, cancellable, callback, user_data);
6971 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6973 g_file_read_async (file,
6975 g_task_get_cancellable (data->task),
6976 load_contents_open_callback,
6981 * g_file_load_partial_contents_finish:
6982 * @file: input #GFile
6983 * @res: a #GAsyncResult
6984 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6985 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6986 * or %NULL if the length is not needed
6987 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6988 * or %NULL if the entity tag is not needed
6989 * @error: a #GError, or %NULL
6991 * Finishes an asynchronous partial load operation that was started
6992 * with g_file_load_partial_contents_async(). The data is always
6993 * zero-terminated, but this is not included in the resultant @length.
6994 * The returned @content should be freed with g_free() when no longer
6997 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6998 * present, it will be set appropriately.
7001 g_file_load_partial_contents_finish (GFile *file,
7009 LoadContentsData *data;
7011 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7012 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7013 g_return_val_if_fail (contents != NULL, FALSE);
7015 task = G_TASK (res);
7017 if (!g_task_propagate_boolean (task, error))
7024 data = g_task_get_task_data (task);
7027 *length = data->pos;
7031 *etag_out = data->etag;
7035 /* Zero terminate */
7036 g_byte_array_set_size (data->content, data->pos + 1);
7037 data->content->data[data->pos] = 0;
7039 *contents = (char *)g_byte_array_free (data->content, FALSE);
7040 data->content = NULL;
7046 * g_file_load_contents_async:
7047 * @file: input #GFile
7048 * @cancellable: optional #GCancellable object, %NULL to ignore
7049 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7050 * @user_data: the data to pass to callback function
7052 * Starts an asynchronous load of the @file's contents.
7054 * For more details, see g_file_load_contents() which is
7055 * the synchronous version of this call.
7057 * When the load operation has completed, @callback will be called
7058 * with @user data. To finish the operation, call
7059 * g_file_load_contents_finish() with the #GAsyncResult returned by
7062 * If @cancellable is not %NULL, then the operation can be cancelled by
7063 * triggering the cancellable object from another thread. If the operation
7064 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7067 g_file_load_contents_async (GFile *file,
7068 GCancellable *cancellable,
7069 GAsyncReadyCallback callback,
7072 g_file_load_partial_contents_async (file,
7075 callback, user_data);
7079 * g_file_load_contents_finish:
7080 * @file: input #GFile
7081 * @res: a #GAsyncResult
7082 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7083 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7084 * or %NULL if the length is not needed
7085 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7086 * or %NULL if the entity tag is not needed
7087 * @error: a #GError, or %NULL
7089 * Finishes an asynchronous load of the @file's contents.
7090 * The contents are placed in @contents, and @length is set to the
7091 * size of the @contents string. The @content should be freed with
7092 * g_free() when no longer needed. If @etag_out is present, it will be
7093 * set to the new entity tag for the @file.
7095 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7096 * present, it will be set appropriately.
7099 g_file_load_contents_finish (GFile *file,
7106 return g_file_load_partial_contents_finish (file,
7115 * g_file_replace_contents:
7116 * @file: input #GFile
7117 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7118 * @length: the length of @contents in bytes
7119 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7121 * @make_backup: %TRUE if a backup should be created
7122 * @flags: a set of #GFileCreateFlags
7123 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7124 * for the document. This should be freed with g_free() when no longer
7126 * @cancellable: optional #GCancellable object, %NULL to ignore
7127 * @error: a #GError, or %NULL
7129 * Replaces the contents of @file with @contents of @length bytes.
7131 * If @etag is specified (not %NULL), any existing file must have that etag,
7132 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7134 * If @make_backup is %TRUE, this function will attempt to make a backup
7135 * of @file. Internally, it uses g_file_replace(), so will try to replace the
7136 * file contents in the safest way possible. For example, atomic renames are
7137 * used when replacing local files’ contents.
7139 * If @cancellable is not %NULL, then the operation can be cancelled by
7140 * triggering the cancellable object from another thread. If the operation
7141 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7143 * The returned @new_etag can be used to verify that the file hasn't
7144 * changed the next time it is saved over.
7146 * Returns: %TRUE if successful. If an error has occurred, this function
7147 * will return %FALSE and set @error appropriately if present.
7150 g_file_replace_contents (GFile *file,
7151 const char *contents,
7154 gboolean make_backup,
7155 GFileCreateFlags flags,
7157 GCancellable *cancellable,
7160 GFileOutputStream *out;
7161 gsize pos, remainder;
7165 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7166 g_return_val_if_fail (contents != NULL, FALSE);
7168 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7174 while (remainder > 0 &&
7175 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7177 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7185 if (remainder > 0 && res < 0)
7187 /* Ignore errors on close */
7188 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7189 g_object_unref (out);
7191 /* error is set already */
7195 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7198 *new_etag = g_file_output_stream_get_etag (out);
7200 g_object_unref (out);
7211 } ReplaceContentsData;
7214 replace_contents_data_free (ReplaceContentsData *data)
7216 g_bytes_unref (data->content);
7217 g_free (data->etag);
7222 replace_contents_close_callback (GObject *obj,
7223 GAsyncResult *close_res,
7226 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7227 ReplaceContentsData *data = user_data;
7229 /* Ignore errors here, we're only reading anyway */
7230 g_output_stream_close_finish (stream, close_res, NULL);
7231 g_object_unref (stream);
7235 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7236 g_task_return_boolean (data->task, TRUE);
7238 g_object_unref (data->task);
7242 replace_contents_write_callback (GObject *obj,
7243 GAsyncResult *read_res,
7246 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7247 ReplaceContentsData *data = user_data;
7248 GError *error = NULL;
7251 write_size = g_output_stream_write_finish (stream, read_res, &error);
7253 if (write_size <= 0)
7255 /* Error or EOF, close the file */
7258 data->failed = TRUE;
7259 g_task_return_error (data->task, error);
7261 g_output_stream_close_async (stream, 0,
7262 g_task_get_cancellable (data->task),
7263 replace_contents_close_callback, data);
7265 else if (write_size > 0)
7267 const gchar *content;
7270 content = g_bytes_get_data (data->content, &length);
7271 data->pos += write_size;
7273 if (data->pos >= length)
7274 g_output_stream_close_async (stream, 0,
7275 g_task_get_cancellable (data->task),
7276 replace_contents_close_callback, data);
7278 g_output_stream_write_async (stream,
7279 content + data->pos,
7282 g_task_get_cancellable (data->task),
7283 replace_contents_write_callback,
7289 replace_contents_open_callback (GObject *obj,
7290 GAsyncResult *open_res,
7293 GFile *file = G_FILE (obj);
7294 GFileOutputStream *stream;
7295 ReplaceContentsData *data = user_data;
7296 GError *error = NULL;
7298 stream = g_file_replace_finish (file, open_res, &error);
7302 const gchar *content;
7305 content = g_bytes_get_data (data->content, &length);
7306 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7307 content + data->pos,
7310 g_task_get_cancellable (data->task),
7311 replace_contents_write_callback,
7316 g_task_return_error (data->task, error);
7317 g_object_unref (data->task);
7322 * g_file_replace_contents_async:
7323 * @file: input #GFile
7324 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7325 * @length: the length of @contents in bytes
7326 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7327 * @make_backup: %TRUE if a backup should be created
7328 * @flags: a set of #GFileCreateFlags
7329 * @cancellable: optional #GCancellable object, %NULL to ignore
7330 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7331 * @user_data: the data to pass to callback function
7333 * Starts an asynchronous replacement of @file with the given
7334 * @contents of @length bytes. @etag will replace the document's
7335 * current entity tag.
7337 * When this operation has completed, @callback will be called with
7338 * @user_user data, and the operation can be finalized with
7339 * g_file_replace_contents_finish().
7341 * If @cancellable is not %NULL, then the operation can be cancelled by
7342 * triggering the cancellable object from another thread. If the operation
7343 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7345 * If @make_backup is %TRUE, this function will attempt to
7346 * make a backup of @file.
7348 * Note that no copy of @content will be made, so it must stay valid
7349 * until @callback is called. See g_file_replace_contents_bytes_async()
7350 * for a #GBytes version that will automatically hold a reference to the
7351 * contents (without copying) for the duration of the call.
7354 g_file_replace_contents_async (GFile *file,
7355 const char *contents,
7358 gboolean make_backup,
7359 GFileCreateFlags flags,
7360 GCancellable *cancellable,
7361 GAsyncReadyCallback callback,
7366 bytes = g_bytes_new_static (contents, length);
7367 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7368 cancellable, callback, user_data);
7369 g_bytes_unref (bytes);
7373 * g_file_replace_contents_bytes_async:
7374 * @file: input #GFile
7375 * @contents: a #GBytes
7376 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7377 * @make_backup: %TRUE if a backup should be created
7378 * @flags: a set of #GFileCreateFlags
7379 * @cancellable: optional #GCancellable object, %NULL to ignore
7380 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7381 * @user_data: the data to pass to callback function
7383 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7384 * This function will keep a ref on @contents until the operation is done.
7385 * Unlike g_file_replace_contents_async() this allows forgetting about the
7386 * content without waiting for the callback.
7388 * When this operation has completed, @callback will be called with
7389 * @user_user data, and the operation can be finalized with
7390 * g_file_replace_contents_finish().
7395 g_file_replace_contents_bytes_async (GFile *file,
7398 gboolean make_backup,
7399 GFileCreateFlags flags,
7400 GCancellable *cancellable,
7401 GAsyncReadyCallback callback,
7404 ReplaceContentsData *data;
7406 g_return_if_fail (G_IS_FILE (file));
7407 g_return_if_fail (contents != NULL);
7409 data = g_new0 (ReplaceContentsData, 1);
7411 data->content = g_bytes_ref (contents);
7413 data->task = g_task_new (file, cancellable, callback, user_data);
7414 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7416 g_file_replace_async (file,
7421 g_task_get_cancellable (data->task),
7422 replace_contents_open_callback,
7427 * g_file_replace_contents_finish:
7428 * @file: input #GFile
7429 * @res: a #GAsyncResult
7430 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7431 * for the document. This should be freed with g_free() when it is no
7432 * longer needed, or %NULL
7433 * @error: a #GError, or %NULL
7435 * Finishes an asynchronous replace of the given @file. See
7436 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7437 * tag for the document, if present.
7439 * Returns: %TRUE on success, %FALSE on failure.
7442 g_file_replace_contents_finish (GFile *file,
7448 ReplaceContentsData *data;
7450 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7451 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7453 task = G_TASK (res);
7455 if (!g_task_propagate_boolean (task, error))
7458 data = g_task_get_task_data (task);
7462 *new_etag = data->etag;
7463 data->etag = NULL; /* Take ownership */
7470 g_file_real_measure_disk_usage (GFile *file,
7471 GFileMeasureFlags flags,
7472 GCancellable *cancellable,
7473 GFileMeasureProgressCallback progress_callback,
7474 gpointer progress_data,
7475 guint64 *disk_usage,
7480 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7481 "Operation not supported for the current backend.");
7487 GFileMeasureFlags flags;
7488 GFileMeasureProgressCallback progress_callback;
7489 gpointer progress_data;
7501 GFileMeasureProgressCallback callback;
7504 guint64 current_size;
7510 measure_disk_usage_invoke_progress (gpointer user_data)
7512 MeasureProgress *progress = user_data;
7514 (* progress->callback) (progress->reporting,
7515 progress->current_size, progress->num_dirs, progress->num_files,
7516 progress->user_data);
7522 measure_disk_usage_progress (gboolean reporting,
7523 guint64 current_size,
7528 MeasureProgress progress;
7529 GTask *task = user_data;
7530 MeasureTaskData *data;
7532 data = g_task_get_task_data (task);
7534 progress.callback = data->progress_callback;
7535 progress.user_data = data->progress_data;
7536 progress.reporting = reporting;
7537 progress.current_size = current_size;
7538 progress.num_dirs = num_dirs;
7539 progress.num_files = num_files;
7541 g_main_context_invoke_full (g_task_get_context (task),
7542 g_task_get_priority (task),
7543 measure_disk_usage_invoke_progress,
7544 g_memdup (&progress, sizeof progress),
7549 measure_disk_usage_thread (GTask *task,
7550 gpointer source_object,
7552 GCancellable *cancellable)
7554 MeasureTaskData *data = task_data;
7555 GError *error = NULL;
7556 MeasureResult result = { 0, };
7558 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7559 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7560 &result.disk_usage, &result.num_dirs, &result.num_files,
7562 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7564 g_task_return_error (task, error);
7568 g_file_real_measure_disk_usage_async (GFile *file,
7569 GFileMeasureFlags flags,
7571 GCancellable *cancellable,
7572 GFileMeasureProgressCallback progress_callback,
7573 gpointer progress_data,
7574 GAsyncReadyCallback callback,
7577 MeasureTaskData data;
7581 data.progress_callback = progress_callback;
7582 data.progress_data = progress_data;
7584 task = g_task_new (file, cancellable, callback, user_data);
7585 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7586 g_task_set_priority (task, io_priority);
7588 g_task_run_in_thread (task, measure_disk_usage_thread);
7589 g_object_unref (task);
7593 g_file_real_measure_disk_usage_finish (GFile *file,
7594 GAsyncResult *result,
7595 guint64 *disk_usage,
7600 MeasureResult *measure_result;
7602 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7604 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7606 if (measure_result == NULL)
7610 *disk_usage = measure_result->disk_usage;
7613 *num_dirs = measure_result->num_dirs;
7616 *num_files = measure_result->num_files;
7618 g_free (measure_result);
7624 * g_file_measure_disk_usage:
7626 * @flags: #GFileMeasureFlags
7627 * @cancellable: (allow-none): optional #GCancellable
7628 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7629 * @progress_data: user_data for @progress_callback
7630 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7631 * @num_dirs: (allow-none) (out): the number of directories encountered
7632 * @num_files: (allow-none) (out): the number of non-directories encountered
7633 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7635 * Recursively measures the disk usage of @file.
7637 * This is essentially an analog of the 'du' command, but it also
7638 * reports the number of directories and non-directory files encountered
7639 * (including things like symbolic links).
7641 * By default, errors are only reported against the toplevel file
7642 * itself. Errors found while recursing are silently ignored, unless
7643 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7645 * The returned size, @disk_usage, is in bytes and should be formatted
7646 * with g_format_size() in order to get something reasonable for showing
7647 * in a user interface.
7649 * @progress_callback and @progress_data can be given to request
7650 * periodic progress updates while scanning. See the documentation for
7651 * #GFileMeasureProgressCallback for information about when and how the
7652 * callback will be invoked.
7654 * Returns: %TRUE if successful, with the out parameters set.
7655 * %FALSE otherwise, with @error set.
7660 g_file_measure_disk_usage (GFile *file,
7661 GFileMeasureFlags flags,
7662 GCancellable *cancellable,
7663 GFileMeasureProgressCallback progress_callback,
7664 gpointer progress_data,
7665 guint64 *disk_usage,
7670 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7671 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7672 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7674 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7675 progress_callback, progress_data,
7676 disk_usage, num_dirs, num_files,
7681 * g_file_measure_disk_usage_async:
7683 * @flags: #GFileMeasureFlags
7684 * @io_priority: the [I/O priority][io-priority] of the request
7685 * @cancellable: (allow-none): optional #GCancellable
7686 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7687 * @progress_data: user_data for @progress_callback
7688 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7689 * @user_data: the data to pass to callback function
7691 * Recursively measures the disk usage of @file.
7693 * This is the asynchronous version of g_file_measure_disk_usage(). See
7694 * there for more information.
7699 g_file_measure_disk_usage_async (GFile *file,
7700 GFileMeasureFlags flags,
7702 GCancellable *cancellable,
7703 GFileMeasureProgressCallback progress_callback,
7704 gpointer progress_data,
7705 GAsyncReadyCallback callback,
7708 g_return_if_fail (G_IS_FILE (file));
7709 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7711 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7712 progress_callback, progress_data,
7713 callback, user_data);
7717 * g_file_measure_disk_usage_finish:
7719 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7720 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7721 * @num_dirs: (allow-none) (out): the number of directories encountered
7722 * @num_files: (allow-none) (out): the number of non-directories encountered
7723 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7725 * Collects the results from an earlier call to
7726 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7729 * Returns: %TRUE if successful, with the out parameters set.
7730 * %FALSE otherwise, with @error set.
7735 g_file_measure_disk_usage_finish (GFile *file,
7736 GAsyncResult *result,
7737 guint64 *disk_usage,
7742 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7743 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7745 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7749 * g_file_start_mountable:
7750 * @file: input #GFile
7751 * @flags: flags affecting the operation
7752 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7753 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7754 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7755 * @user_data: the data to pass to callback function
7757 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7758 * Using @start_operation, you can request callbacks when, for instance,
7759 * passwords are needed during authentication.
7761 * If @cancellable is not %NULL, then the operation can be cancelled by
7762 * triggering the cancellable object from another thread. If the operation
7763 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7765 * When the operation is finished, @callback will be called.
7766 * You can then call g_file_mount_mountable_finish() to get
7767 * the result of the operation.
7772 g_file_start_mountable (GFile *file,
7773 GDriveStartFlags flags,
7774 GMountOperation *start_operation,
7775 GCancellable *cancellable,
7776 GAsyncReadyCallback callback,
7781 g_return_if_fail (G_IS_FILE (file));
7783 iface = G_FILE_GET_IFACE (file);
7785 if (iface->start_mountable == NULL)
7787 g_task_report_new_error (file, callback, user_data,
7788 g_file_start_mountable,
7789 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7790 _("Operation not supported"));
7794 (* iface->start_mountable) (file,
7803 * g_file_start_mountable_finish:
7804 * @file: input #GFile
7805 * @result: a #GAsyncResult
7806 * @error: a #GError, or %NULL
7808 * Finishes a start operation. See g_file_start_mountable() for details.
7810 * Finish an asynchronous start operation that was started
7811 * with g_file_start_mountable().
7813 * Returns: %TRUE if the operation finished successfully. %FALSE
7819 g_file_start_mountable_finish (GFile *file,
7820 GAsyncResult *result,
7825 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7826 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7828 if (g_async_result_legacy_propagate_error (result, error))
7830 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7831 return g_task_propagate_boolean (G_TASK (result), error);
7833 iface = G_FILE_GET_IFACE (file);
7834 return (* iface->start_mountable_finish) (file, result, error);
7838 * g_file_stop_mountable:
7839 * @file: input #GFile
7840 * @flags: flags affecting the operation
7841 * @mount_operation: (allow-none): a #GMountOperation,
7842 * or %NULL to avoid user interaction.
7843 * @cancellable: (allow-none): optional #GCancellable object,
7845 * @callback: (allow-none): a #GAsyncReadyCallback to call
7846 * when the request is satisfied, or %NULL
7847 * @user_data: the data to pass to callback function
7849 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7851 * If @cancellable is not %NULL, then the operation can be cancelled by
7852 * triggering the cancellable object from another thread. If the operation
7853 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7855 * When the operation is finished, @callback will be called.
7856 * You can then call g_file_stop_mountable_finish() to get
7857 * the result of the operation.
7862 g_file_stop_mountable (GFile *file,
7863 GMountUnmountFlags flags,
7864 GMountOperation *mount_operation,
7865 GCancellable *cancellable,
7866 GAsyncReadyCallback callback,
7871 g_return_if_fail (G_IS_FILE (file));
7873 iface = G_FILE_GET_IFACE (file);
7875 if (iface->stop_mountable == NULL)
7877 g_task_report_new_error (file, callback, user_data,
7878 g_file_stop_mountable,
7879 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7880 _("Operation not supported"));
7884 (* iface->stop_mountable) (file,
7893 * g_file_stop_mountable_finish:
7894 * @file: input #GFile
7895 * @result: a #GAsyncResult
7896 * @error: a #GError, or %NULL
7898 * Finishes an stop operation, see g_file_stop_mountable() for details.
7900 * Finish an asynchronous stop operation that was started
7901 * with g_file_stop_mountable().
7903 * Returns: %TRUE if the operation finished successfully.
7909 g_file_stop_mountable_finish (GFile *file,
7910 GAsyncResult *result,
7915 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7916 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7918 if (g_async_result_legacy_propagate_error (result, error))
7920 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7921 return g_task_propagate_boolean (G_TASK (result), error);
7923 iface = G_FILE_GET_IFACE (file);
7924 return (* iface->stop_mountable_finish) (file, result, error);
7928 * g_file_poll_mountable:
7929 * @file: input #GFile
7930 * @cancellable: optional #GCancellable object, %NULL to ignore
7931 * @callback: (allow-none): a #GAsyncReadyCallback to call
7932 * when the request is satisfied, or %NULL
7933 * @user_data: the data to pass to callback function
7935 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7937 * If @cancellable is not %NULL, then the operation can be cancelled by
7938 * triggering the cancellable object from another thread. If the operation
7939 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7941 * When the operation is finished, @callback will be called.
7942 * You can then call g_file_mount_mountable_finish() to get
7943 * the result of the operation.
7948 g_file_poll_mountable (GFile *file,
7949 GCancellable *cancellable,
7950 GAsyncReadyCallback callback,
7955 g_return_if_fail (G_IS_FILE (file));
7957 iface = G_FILE_GET_IFACE (file);
7959 if (iface->poll_mountable == NULL)
7961 g_task_report_new_error (file, callback, user_data,
7962 g_file_poll_mountable,
7963 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7964 _("Operation not supported"));
7968 (* iface->poll_mountable) (file,
7975 * g_file_poll_mountable_finish:
7976 * @file: input #GFile
7977 * @result: a #GAsyncResult
7978 * @error: a #GError, or %NULL
7980 * Finishes a poll operation. See g_file_poll_mountable() for details.
7982 * Finish an asynchronous poll operation that was polled
7983 * with g_file_poll_mountable().
7985 * Returns: %TRUE if the operation finished successfully. %FALSE
7991 g_file_poll_mountable_finish (GFile *file,
7992 GAsyncResult *result,
7997 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7998 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
8000 if (g_async_result_legacy_propagate_error (result, error))
8002 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
8003 return g_task_propagate_boolean (G_TASK (result), error);
8005 iface = G_FILE_GET_IFACE (file);
8006 return (* iface->poll_mountable_finish) (file, result, error);
8010 * g_file_supports_thread_contexts:
8013 * Checks if @file supports
8014 * [thread-default contexts][g-main-context-push-thread-default-context].
8015 * If this returns %FALSE, you cannot perform asynchronous operations on
8016 * @file in a thread that has a thread-default context.
8018 * Returns: Whether or not @file supports thread-default contexts.
8023 g_file_supports_thread_contexts (GFile *file)
8027 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8029 iface = G_FILE_GET_IFACE (file);
8030 return iface->supports_thread_contexts;