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 * Some #GFile operations do not have synchronous analogs, as they may
130 * take a very long time to finish, and blocking may leave an application
131 * unusable. Notable cases include:
132 * - g_file_mount_mountable() to mount a mountable file.
133 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
134 * - g_file_eject_mountable_with_operation() to eject a mountable file.
136 * ## Entity Tags # {#gfile-etag}
138 * One notable feature of #GFiles are entity tags, or "etags" for
139 * short. Entity tags are somewhat like a more abstract version of the
140 * traditional mtime, and can be used to quickly determine if the file
141 * has been modified from the version on the file system. See the
143 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
144 * for HTTP Etag headers, which are a very similar concept.
147 static void g_file_real_query_info_async (GFile *file,
148 const char *attributes,
149 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
154 static GFileInfo * g_file_real_query_info_finish (GFile *file,
157 static void g_file_real_query_filesystem_info_async (GFile *file,
158 const char *attributes,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
166 static void g_file_real_enumerate_children_async (GFile *file,
167 const char *attributes,
168 GFileQueryInfoFlags flags,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
173 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
176 static void g_file_real_read_async (GFile *file,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileInputStream * g_file_real_read_finish (GFile *file,
184 static void g_file_real_append_to_async (GFile *file,
185 GFileCreateFlags flags,
187 GCancellable *cancellable,
188 GAsyncReadyCallback callback,
190 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
193 static void g_file_real_create_async (GFile *file,
194 GFileCreateFlags flags,
196 GCancellable *cancellable,
197 GAsyncReadyCallback callback,
199 static GFileOutputStream *g_file_real_create_finish (GFile *file,
202 static void g_file_real_replace_async (GFile *file,
204 gboolean make_backup,
205 GFileCreateFlags flags,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
210 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
213 static void g_file_real_delete_async (GFile *file,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static gboolean g_file_real_delete_finish (GFile *file,
221 static void g_file_real_trash_async (GFile *file,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
226 static gboolean g_file_real_trash_finish (GFile *file,
229 static void g_file_real_make_directory_async (GFile *file,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static gboolean g_file_real_make_directory_finish (GFile *file,
237 static void g_file_real_open_readwrite_async (GFile *file,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
245 static void g_file_real_create_readwrite_async (GFile *file,
246 GFileCreateFlags flags,
248 GCancellable *cancellable,
249 GAsyncReadyCallback callback,
251 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
254 static void g_file_real_replace_readwrite_async (GFile *file,
256 gboolean make_backup,
257 GFileCreateFlags flags,
259 GCancellable *cancellable,
260 GAsyncReadyCallback callback,
262 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
265 static gboolean g_file_real_set_attributes_from_info (GFile *file,
267 GFileQueryInfoFlags flags,
268 GCancellable *cancellable,
270 static void g_file_real_set_display_name_async (GFile *file,
271 const char *display_name,
273 GCancellable *cancellable,
274 GAsyncReadyCallback callback,
276 static GFile * g_file_real_set_display_name_finish (GFile *file,
279 static void g_file_real_set_attributes_async (GFile *file,
281 GFileQueryInfoFlags flags,
283 GCancellable *cancellable,
284 GAsyncReadyCallback callback,
286 static gboolean g_file_real_set_attributes_finish (GFile *file,
290 static void g_file_real_find_enclosing_mount_async (GFile *file,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
295 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
298 static void g_file_real_copy_async (GFile *source,
300 GFileCopyFlags flags,
302 GCancellable *cancellable,
303 GFileProgressCallback progress_callback,
304 gpointer progress_callback_data,
305 GAsyncReadyCallback callback,
307 static gboolean g_file_real_copy_finish (GFile *file,
311 static gboolean g_file_real_measure_disk_usage (GFile *file,
312 GFileMeasureFlags flags,
313 GCancellable *cancellable,
314 GFileMeasureProgressCallback progress_callback,
315 gpointer progress_data,
320 static void g_file_real_measure_disk_usage_async (GFile *file,
321 GFileMeasureFlags flags,
323 GCancellable *cancellable,
324 GFileMeasureProgressCallback progress_callback,
325 gpointer progress_data,
326 GAsyncReadyCallback callback,
328 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
329 GAsyncResult *result,
335 typedef GFileIface GFileInterface;
336 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
339 g_file_default_init (GFileIface *iface)
341 iface->enumerate_children_async = g_file_real_enumerate_children_async;
342 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
343 iface->set_display_name_async = g_file_real_set_display_name_async;
344 iface->set_display_name_finish = g_file_real_set_display_name_finish;
345 iface->query_info_async = g_file_real_query_info_async;
346 iface->query_info_finish = g_file_real_query_info_finish;
347 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
348 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
349 iface->set_attributes_async = g_file_real_set_attributes_async;
350 iface->set_attributes_finish = g_file_real_set_attributes_finish;
351 iface->read_async = g_file_real_read_async;
352 iface->read_finish = g_file_real_read_finish;
353 iface->append_to_async = g_file_real_append_to_async;
354 iface->append_to_finish = g_file_real_append_to_finish;
355 iface->create_async = g_file_real_create_async;
356 iface->create_finish = g_file_real_create_finish;
357 iface->replace_async = g_file_real_replace_async;
358 iface->replace_finish = g_file_real_replace_finish;
359 iface->delete_file_async = g_file_real_delete_async;
360 iface->delete_file_finish = g_file_real_delete_finish;
361 iface->trash_async = g_file_real_trash_async;
362 iface->trash_finish = g_file_real_trash_finish;
363 iface->make_directory_async = g_file_real_make_directory_async;
364 iface->make_directory_finish = g_file_real_make_directory_finish;
365 iface->open_readwrite_async = g_file_real_open_readwrite_async;
366 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
367 iface->create_readwrite_async = g_file_real_create_readwrite_async;
368 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
369 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
370 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
371 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
372 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
373 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
374 iface->copy_async = g_file_real_copy_async;
375 iface->copy_finish = g_file_real_copy_finish;
376 iface->measure_disk_usage = g_file_real_measure_disk_usage;
377 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
378 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
384 * @file: input #GFile
386 * Checks to see if a file is native to the platform.
388 * A native file s one expressed in the platform-native filename format,
389 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
390 * as it might be on a locally mounted remote filesystem.
392 * On some systems non-native files may be available using the native
393 * filesystem via a userspace filesystem (FUSE), in these cases this call
394 * will return %FALSE, but g_file_get_path() will still return a native path.
396 * This call does no blocking I/O.
398 * Returns: %TRUE if @file is native
401 g_file_is_native (GFile *file)
405 g_return_val_if_fail (G_IS_FILE (file), FALSE);
407 iface = G_FILE_GET_IFACE (file);
409 return (* iface->is_native) (file);
414 * g_file_has_uri_scheme:
415 * @file: input #GFile
416 * @uri_scheme: a string containing a URI scheme
418 * Checks to see if a #GFile has a given URI scheme.
420 * This call does no blocking I/O.
422 * Returns: %TRUE if #GFile's backend supports the
423 * given URI scheme, %FALSE if URI scheme is %NULL,
424 * not supported, or #GFile is invalid.
427 g_file_has_uri_scheme (GFile *file,
428 const char *uri_scheme)
432 g_return_val_if_fail (G_IS_FILE (file), FALSE);
433 g_return_val_if_fail (uri_scheme != NULL, FALSE);
435 iface = G_FILE_GET_IFACE (file);
437 return (* iface->has_uri_scheme) (file, uri_scheme);
442 * g_file_get_uri_scheme:
443 * @file: input #GFile
445 * Gets the URI scheme for a #GFile.
446 * RFC 3986 decodes the scheme as:
448 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
450 * Common schemes include "file", "http", "ftp", etc.
452 * This call does no blocking I/O.
454 * Returns: a string containing the URI scheme for the given
455 * #GFile. The returned string should be freed with g_free()
456 * when no longer needed.
459 g_file_get_uri_scheme (GFile *file)
463 g_return_val_if_fail (G_IS_FILE (file), NULL);
465 iface = G_FILE_GET_IFACE (file);
467 return (* iface->get_uri_scheme) (file);
472 * g_file_get_basename:
473 * @file: input #GFile
475 * Gets the base name (the last component of the path) for a given #GFile.
477 * If called for the top level of a system (such as the filesystem root
478 * or a uri like sftp://host/) it will return a single directory separator
479 * (and on Windows, possibly a drive letter).
481 * The base name is a byte string (not UTF-8). It has no defined encoding
482 * or rules other than it may not contain zero bytes. If you want to use
483 * filenames in a user interface you should use the display name that you
484 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
485 * attribute with g_file_query_info().
487 * This call does no blocking I/O.
489 * Returns: (nullable): string containing the #GFile's base name, or
490 * %NULL if given #GFile is invalid. The returned string should be
491 * freed with g_free() when no longer needed.
494 g_file_get_basename (GFile *file)
498 g_return_val_if_fail (G_IS_FILE (file), NULL);
500 iface = G_FILE_GET_IFACE (file);
502 return (* iface->get_basename) (file);
507 * @file: input #GFile
509 * Gets the local pathname for #GFile, if one exists.
511 * This call does no blocking I/O.
513 * Returns: (nullable): string containing the #GFile's path, or %NULL
514 * if no such path exists. The returned string should be freed
515 * with g_free() when no longer needed.
518 g_file_get_path (GFile *file)
522 g_return_val_if_fail (G_IS_FILE (file), NULL);
524 iface = G_FILE_GET_IFACE (file);
526 return (* iface->get_path) (file);
531 * @file: input #GFile
533 * Gets the URI for the @file.
535 * This call does no blocking I/O.
537 * Returns: a string containing the #GFile's URI.
538 * The returned string should be freed with g_free()
539 * when no longer needed.
542 g_file_get_uri (GFile *file)
546 g_return_val_if_fail (G_IS_FILE (file), NULL);
548 iface = G_FILE_GET_IFACE (file);
550 return (* iface->get_uri) (file);
554 * g_file_get_parse_name:
555 * @file: input #GFile
557 * Gets the parse name of the @file.
558 * A parse name is a UTF-8 string that describes the
559 * file such that one can get the #GFile back using
560 * g_file_parse_name().
562 * This is generally used to show the #GFile as a nice
563 * full-pathname kind of string in a user interface,
564 * like in a location entry.
566 * For local files with names that can safely be converted
567 * to UTF-8 the pathname is used, otherwise the IRI is used
568 * (a form of URI that allows UTF-8 characters unescaped).
570 * This call does no blocking I/O.
572 * Returns: a string containing the #GFile's parse name.
573 * The returned string should be freed with g_free()
574 * when no longer needed.
577 g_file_get_parse_name (GFile *file)
581 g_return_val_if_fail (G_IS_FILE (file), NULL);
583 iface = G_FILE_GET_IFACE (file);
585 return (* iface->get_parse_name) (file);
590 * @file: input #GFile
592 * Duplicates a #GFile handle. This operation does not duplicate
593 * the actual file or directory represented by the #GFile; see
594 * g_file_copy() if attempting to copy a file.
596 * This call does no blocking I/O.
598 * Returns: (transfer full): a new #GFile that is a duplicate
599 * of the given #GFile.
602 g_file_dup (GFile *file)
606 g_return_val_if_fail (G_IS_FILE (file), NULL);
608 iface = G_FILE_GET_IFACE (file);
610 return (* iface->dup) (file);
615 * @file: (type GFile): #gconstpointer to a #GFile
617 * Creates a hash value for a #GFile.
619 * This call does no blocking I/O.
622 * Returns: 0 if @file is not a valid #GFile, otherwise an
623 * integer that can be used as hash value for the #GFile.
624 * This function is intended for easily hashing a #GFile to
625 * add to a #GHashTable or similar data structure.
628 g_file_hash (gconstpointer file)
632 g_return_val_if_fail (G_IS_FILE (file), 0);
634 iface = G_FILE_GET_IFACE (file);
636 return (* iface->hash) ((GFile *)file);
641 * @file1: the first #GFile
642 * @file2: the second #GFile
644 * Checks if the two given #GFiles refer to the same file.
646 * Note that two #GFiles that differ can still refer to the same
647 * file on the filesystem due to various forms of filename
650 * This call does no blocking I/O.
652 * Returns: %TRUE if @file1 and @file2 are equal.
655 g_file_equal (GFile *file1,
660 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
661 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
663 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
666 iface = G_FILE_GET_IFACE (file1);
668 return (* iface->equal) (file1, file2);
674 * @file: input #GFile
676 * Gets the parent directory for the @file.
677 * If the @file represents the root directory of the
678 * file system, then %NULL will be returned.
680 * This call does no blocking I/O.
682 * Returns: (nullable) (transfer full): a #GFile structure to the
683 * parent of the given #GFile or %NULL if there is no parent. Free
684 * the returned object with g_object_unref().
687 g_file_get_parent (GFile *file)
691 g_return_val_if_fail (G_IS_FILE (file), NULL);
693 iface = G_FILE_GET_IFACE (file);
695 return (* iface->get_parent) (file);
700 * @file: input #GFile
701 * @parent: (allow-none): the parent to check for, or %NULL
703 * Checks if @file has a parent, and optionally, if it is @parent.
705 * If @parent is %NULL then this function returns %TRUE if @file has any
706 * parent at all. If @parent is non-%NULL then %TRUE is only returned
707 * if @file is a child of @parent.
709 * Returns: %TRUE if @file is a child of @parent (or any parent in the
710 * case that @parent is %NULL).
715 g_file_has_parent (GFile *file,
718 GFile *actual_parent;
721 g_return_val_if_fail (G_IS_FILE (file), FALSE);
722 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
724 actual_parent = g_file_get_parent (file);
726 if (actual_parent != NULL)
729 result = g_file_equal (parent, actual_parent);
733 g_object_unref (actual_parent);
743 * @file: input #GFile
744 * @name: string containing the child's basename
746 * Gets a child of @file with basename equal to @name.
748 * Note that the file with that specific name might not exist, but
749 * you can still have a #GFile that points to it. You can use this
750 * for instance to create that file.
752 * This call does no blocking I/O.
754 * Returns: (transfer full): a #GFile to a child specified by @name.
755 * Free the returned object with g_object_unref().
758 g_file_get_child (GFile *file,
761 g_return_val_if_fail (G_IS_FILE (file), NULL);
762 g_return_val_if_fail (name != NULL, NULL);
764 return g_file_resolve_relative_path (file, name);
768 * g_file_get_child_for_display_name:
769 * @file: input #GFile
770 * @display_name: string to a possible child
771 * @error: return location for an error
773 * Gets the child of @file for a given @display_name (i.e. a UTF-8
774 * version of the name). If this function fails, it returns %NULL
775 * and @error will be set. This is very useful when constructing a
776 * #GFile for a new file and the user entered the filename in the
777 * user interface, for instance when you select a directory and
778 * type a filename in the file selector.
780 * This call does no blocking I/O.
782 * Returns: (transfer full): a #GFile to the specified child, or
783 * %NULL if the display name couldn't be converted.
784 * Free the returned object with g_object_unref().
787 g_file_get_child_for_display_name (GFile *file,
788 const char *display_name,
793 g_return_val_if_fail (G_IS_FILE (file), NULL);
794 g_return_val_if_fail (display_name != NULL, NULL);
796 iface = G_FILE_GET_IFACE (file);
798 return (* iface->get_child_for_display_name) (file, display_name, error);
803 * @file: input #GFile
804 * @prefix: input #GFile
806 * Checks whether @file has the prefix specified by @prefix.
808 * In other words, if the names of initial elements of @file's
809 * pathname match @prefix. Only full pathname elements are matched,
810 * so a path like /foo is not considered a prefix of /foobar, only
813 * A #GFile is not a prefix of itself. If you want to check for
814 * equality, use g_file_equal().
816 * This call does no I/O, as it works purely on names. As such it can
817 * sometimes return %FALSE even if @file is inside a @prefix (from a
818 * filesystem point of view), because the prefix of @file is an alias
821 * Virtual: prefix_matches
822 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
826 g_file_has_prefix (GFile *file,
831 g_return_val_if_fail (G_IS_FILE (file), FALSE);
832 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
834 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
837 iface = G_FILE_GET_IFACE (file);
839 /* The vtable function differs in arg order since
840 * we're using the old contains_file call
842 return (* iface->prefix_matches) (prefix, file);
846 * g_file_get_relative_path:
847 * @parent: input #GFile
848 * @descendant: input #GFile
850 * Gets the path for @descendant relative to @parent.
852 * This call does no blocking I/O.
854 * Returns: (nullable): string with the relative path from @descendant
855 * to @parent, or %NULL if @descendant doesn't have @parent as
856 * prefix. The returned string should be freed with g_free() when
860 g_file_get_relative_path (GFile *parent,
865 g_return_val_if_fail (G_IS_FILE (parent), NULL);
866 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
868 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
871 iface = G_FILE_GET_IFACE (parent);
873 return (* iface->get_relative_path) (parent, descendant);
877 * g_file_resolve_relative_path:
878 * @file: input #GFile
879 * @relative_path: a given relative path string
881 * Resolves a relative path for @file to an absolute path.
883 * This call does no blocking I/O.
885 * Returns: (transfer full): #GFile to the resolved path.
886 * %NULL if @relative_path is %NULL or if @file is invalid.
887 * Free the returned object with g_object_unref().
890 g_file_resolve_relative_path (GFile *file,
891 const char *relative_path)
895 g_return_val_if_fail (G_IS_FILE (file), NULL);
896 g_return_val_if_fail (relative_path != NULL, NULL);
898 iface = G_FILE_GET_IFACE (file);
900 return (* iface->resolve_relative_path) (file, relative_path);
904 * g_file_enumerate_children:
905 * @file: input #GFile
906 * @attributes: an attribute query string
907 * @flags: a set of #GFileQueryInfoFlags
908 * @cancellable: (allow-none): optional #GCancellable object,
910 * @error: #GError for error reporting
912 * Gets the requested information about the files in a directory.
913 * The result is a #GFileEnumerator object that will give out
914 * #GFileInfo objects for all the files in the directory.
916 * The @attributes value is a string that specifies the file
917 * attributes that should be gathered. It is not an error if
918 * it's not possible to read a particular requested attribute
919 * from a file - it just won't be set. @attributes should
920 * be a comma-separated list of attributes or attribute wildcards.
921 * The wildcard "*" means all attributes, and a wildcard like
922 * "standard::*" means all attributes in the standard namespace.
923 * An example attribute query be "standard::*,owner::user".
924 * The standard attributes are available as defines, like
925 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
927 * If @cancellable is not %NULL, then the operation can be cancelled
928 * by triggering the cancellable object from another thread. If the
929 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
932 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
933 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
934 * error will be returned. Other errors are possible too.
936 * Returns: (transfer full): A #GFileEnumerator if successful,
937 * %NULL on error. Free the returned object with g_object_unref().
940 g_file_enumerate_children (GFile *file,
941 const char *attributes,
942 GFileQueryInfoFlags flags,
943 GCancellable *cancellable,
948 g_return_val_if_fail (G_IS_FILE (file), NULL);
950 if (g_cancellable_set_error_if_cancelled (cancellable, error))
953 iface = G_FILE_GET_IFACE (file);
955 if (iface->enumerate_children == NULL)
957 g_set_error_literal (error, G_IO_ERROR,
958 G_IO_ERROR_NOT_SUPPORTED,
959 _("Operation not supported"));
963 return (* iface->enumerate_children) (file, attributes, flags,
968 * g_file_enumerate_children_async:
969 * @file: input #GFile
970 * @attributes: an attribute query string
971 * @flags: a set of #GFileQueryInfoFlags
972 * @io_priority: the [I/O priority][io-priority] of the request
973 * @cancellable: (allow-none): optional #GCancellable object,
975 * @callback: (scope async): a #GAsyncReadyCallback to call when the
976 * request is satisfied
977 * @user_data: (closure): the data to pass to callback function
979 * Asynchronously gets the requested information about the files
980 * in a directory. The result is a #GFileEnumerator object that will
981 * give out #GFileInfo objects for all the files in the directory.
983 * For more details, see g_file_enumerate_children() which is
984 * the synchronous version of this call.
986 * When the operation is finished, @callback will be called. You can
987 * then call g_file_enumerate_children_finish() to get the result of
991 g_file_enumerate_children_async (GFile *file,
992 const char *attributes,
993 GFileQueryInfoFlags flags,
995 GCancellable *cancellable,
996 GAsyncReadyCallback callback,
1001 g_return_if_fail (G_IS_FILE (file));
1003 iface = G_FILE_GET_IFACE (file);
1004 (* iface->enumerate_children_async) (file,
1014 * g_file_enumerate_children_finish:
1015 * @file: input #GFile
1016 * @res: a #GAsyncResult
1019 * Finishes an async enumerate children operation.
1020 * See g_file_enumerate_children_async().
1022 * Returns: (transfer full): a #GFileEnumerator or %NULL
1023 * if an error occurred.
1024 * Free the returned object with g_object_unref().
1027 g_file_enumerate_children_finish (GFile *file,
1033 g_return_val_if_fail (G_IS_FILE (file), NULL);
1034 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1036 if (g_async_result_legacy_propagate_error (res, error))
1039 iface = G_FILE_GET_IFACE (file);
1040 return (* iface->enumerate_children_finish) (file, res, error);
1044 * g_file_query_exists:
1045 * @file: input #GFile
1046 * @cancellable: (allow-none): optional #GCancellable object,
1049 * Utility function to check if a particular file exists. This is
1050 * implemented using g_file_query_info() and as such does blocking I/O.
1052 * Note that in many cases it is racy to first check for file existence
1053 * and then execute something based on the outcome of that, because the
1054 * file might have been created or removed in between the operations. The
1055 * general approach to handling that is to not check, but just do the
1056 * operation and handle the errors as they come.
1058 * As an example of race-free checking, take the case of reading a file,
1059 * and if it doesn't exist, creating it. There are two racy versions: read
1060 * it, and on error create it; and: check if it exists, if not create it.
1061 * These can both result in two processes creating the file (with perhaps
1062 * a partially written file as the result). The correct approach is to
1063 * always try to create the file with g_file_create() which will either
1064 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1066 * However, in many cases an existence check is useful in a user interface,
1067 * for instance to make a menu item sensitive/insensitive, so that you don't
1068 * have to fool users that something is possible and then just show an error
1069 * dialog. If you do this, you should make sure to also handle the errors
1070 * that can happen due to races when you execute the operation.
1072 * Returns: %TRUE if the file exists (and can be detected without error),
1073 * %FALSE otherwise (or if cancelled).
1076 g_file_query_exists (GFile *file,
1077 GCancellable *cancellable)
1081 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1083 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1084 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1087 g_object_unref (info);
1095 * g_file_query_file_type:
1096 * @file: input #GFile
1097 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1098 * @cancellable: (allow-none): optional #GCancellable object,
1101 * Utility function to inspect the #GFileType of a file. This is
1102 * implemented using g_file_query_info() and as such does blocking I/O.
1104 * The primary use case of this method is to check if a file is
1105 * a regular file, directory, or symlink.
1107 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1108 * if the file does not exist
1113 g_file_query_file_type (GFile *file,
1114 GFileQueryInfoFlags flags,
1115 GCancellable *cancellable)
1118 GFileType file_type;
1120 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1121 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1125 file_type = g_file_info_get_file_type (info);
1126 g_object_unref (info);
1129 file_type = G_FILE_TYPE_UNKNOWN;
1135 * g_file_query_info:
1136 * @file: input #GFile
1137 * @attributes: an attribute query string
1138 * @flags: a set of #GFileQueryInfoFlags
1139 * @cancellable: (allow-none): optional #GCancellable object,
1143 * Gets the requested information about specified @file.
1144 * The result is a #GFileInfo object that contains key-value
1145 * attributes (such as the type or size of the file).
1147 * The @attributes value is a string that specifies the file
1148 * attributes that should be gathered. It is not an error if
1149 * it's not possible to read a particular requested attribute
1150 * from a file - it just won't be set. @attributes should be a
1151 * comma-separated list of attributes or attribute wildcards.
1152 * The wildcard "*" means all attributes, and a wildcard like
1153 * "standard::*" means all attributes in the standard namespace.
1154 * An example attribute query be "standard::*,owner::user".
1155 * The standard attributes are available as defines, like
1156 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1158 * If @cancellable is not %NULL, then the operation can be cancelled
1159 * by triggering the cancellable object from another thread. If the
1160 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1163 * For symlinks, normally the information about the target of the
1164 * symlink is returned, rather than information about the symlink
1165 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1166 * in @flags the information about the symlink itself will be returned.
1167 * Also, for symlinks that point to non-existing files the information
1168 * about the symlink itself will be returned.
1170 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1171 * returned. Other errors are possible too, and depend on what kind of
1172 * filesystem the file is on.
1174 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1175 * on error. Free the returned object with g_object_unref().
1178 g_file_query_info (GFile *file,
1179 const char *attributes,
1180 GFileQueryInfoFlags flags,
1181 GCancellable *cancellable,
1186 g_return_val_if_fail (G_IS_FILE (file), NULL);
1188 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1191 iface = G_FILE_GET_IFACE (file);
1193 if (iface->query_info == NULL)
1195 g_set_error_literal (error, G_IO_ERROR,
1196 G_IO_ERROR_NOT_SUPPORTED,
1197 _("Operation not supported"));
1201 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1205 * g_file_query_info_async:
1206 * @file: input #GFile
1207 * @attributes: an attribute query string
1208 * @flags: a set of #GFileQueryInfoFlags
1209 * @io_priority: the [I/O priority][io-priority] of the request
1210 * @cancellable: (allow-none): optional #GCancellable object,
1212 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1213 * request is satisfied
1214 * @user_data: (closure): the data to pass to callback function
1216 * Asynchronously gets the requested information about specified @file.
1217 * The result is a #GFileInfo object that contains key-value attributes
1218 * (such as type or size for the file).
1220 * For more details, see g_file_query_info() which is the synchronous
1221 * version of this call.
1223 * When the operation is finished, @callback will be called. You can
1224 * then call g_file_query_info_finish() to get the result of the operation.
1227 g_file_query_info_async (GFile *file,
1228 const char *attributes,
1229 GFileQueryInfoFlags flags,
1231 GCancellable *cancellable,
1232 GAsyncReadyCallback callback,
1237 g_return_if_fail (G_IS_FILE (file));
1239 iface = G_FILE_GET_IFACE (file);
1240 (* iface->query_info_async) (file,
1250 * g_file_query_info_finish:
1251 * @file: input #GFile
1252 * @res: a #GAsyncResult
1255 * Finishes an asynchronous file info query.
1256 * See g_file_query_info_async().
1258 * Returns: (transfer full): #GFileInfo for given @file
1259 * or %NULL on error. Free the returned object with
1263 g_file_query_info_finish (GFile *file,
1269 g_return_val_if_fail (G_IS_FILE (file), NULL);
1270 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1272 if (g_async_result_legacy_propagate_error (res, error))
1275 iface = G_FILE_GET_IFACE (file);
1276 return (* iface->query_info_finish) (file, res, error);
1280 * g_file_query_filesystem_info:
1281 * @file: input #GFile
1282 * @attributes: an attribute query string
1283 * @cancellable: (allow-none): optional #GCancellable object,
1287 * Similar to g_file_query_info(), but obtains information
1288 * about the filesystem the @file is on, rather than the file itself.
1289 * For instance the amount of space available and the type of
1292 * The @attributes value is a string that specifies the attributes
1293 * that should be gathered. It is not an error if it's not possible
1294 * to read a particular requested attribute from a file - it just
1295 * won't be set. @attributes should be a comma-separated list of
1296 * attributes or attribute wildcards. The wildcard "*" means all
1297 * attributes, and a wildcard like "filesystem::*" means all attributes
1298 * in the filesystem namespace. The standard namespace for filesystem
1299 * attributes is "filesystem". Common attributes of interest are
1300 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1301 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1302 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1304 * If @cancellable is not %NULL, then the operation can be cancelled
1305 * by triggering the cancellable object from another thread. If the
1306 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1309 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1310 * be returned. Other errors are possible too, and depend on what
1311 * kind of filesystem the file is on.
1313 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1314 * Free the returned object with g_object_unref().
1317 g_file_query_filesystem_info (GFile *file,
1318 const char *attributes,
1319 GCancellable *cancellable,
1324 g_return_val_if_fail (G_IS_FILE (file), NULL);
1326 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1329 iface = G_FILE_GET_IFACE (file);
1331 if (iface->query_filesystem_info == NULL)
1333 g_set_error_literal (error, G_IO_ERROR,
1334 G_IO_ERROR_NOT_SUPPORTED,
1335 _("Operation not supported"));
1339 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1343 * g_file_query_filesystem_info_async:
1344 * @file: input #GFile
1345 * @attributes: an attribute query string
1346 * @io_priority: the [I/O priority][io-priority] of the request
1347 * @cancellable: (allow-none): optional #GCancellable object,
1349 * @callback: (scope async): a #GAsyncReadyCallback to call
1350 * when the request is satisfied
1351 * @user_data: (closure): the data to pass to callback function
1353 * Asynchronously gets the requested information about the filesystem
1354 * that the specified @file is on. The result is a #GFileInfo object
1355 * that contains key-value attributes (such as type or size for the
1358 * For more details, see g_file_query_filesystem_info() which is the
1359 * synchronous version of this call.
1361 * When the operation is finished, @callback will be called. You can
1362 * then call g_file_query_info_finish() to get the result of the
1366 g_file_query_filesystem_info_async (GFile *file,
1367 const char *attributes,
1369 GCancellable *cancellable,
1370 GAsyncReadyCallback callback,
1375 g_return_if_fail (G_IS_FILE (file));
1377 iface = G_FILE_GET_IFACE (file);
1378 (* iface->query_filesystem_info_async) (file,
1387 * g_file_query_filesystem_info_finish:
1388 * @file: input #GFile
1389 * @res: a #GAsyncResult
1392 * Finishes an asynchronous filesystem info query.
1393 * See g_file_query_filesystem_info_async().
1395 * Returns: (transfer full): #GFileInfo for given @file
1396 * or %NULL on error.
1397 * Free the returned object with g_object_unref().
1400 g_file_query_filesystem_info_finish (GFile *file,
1406 g_return_val_if_fail (G_IS_FILE (file), NULL);
1407 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1409 if (g_async_result_legacy_propagate_error (res, error))
1412 iface = G_FILE_GET_IFACE (file);
1413 return (* iface->query_filesystem_info_finish) (file, res, error);
1417 * g_file_find_enclosing_mount:
1418 * @file: input #GFile
1419 * @cancellable: (allow-none): optional #GCancellable object,
1423 * Gets a #GMount for the #GFile.
1425 * If the #GFileIface for @file does not have a mount (e.g.
1426 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1427 * and %NULL will be returned.
1429 * If @cancellable is not %NULL, then the operation can be cancelled by
1430 * triggering the cancellable object from another thread. If the operation
1431 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1433 * Returns: (transfer full): a #GMount where the @file is located
1434 * or %NULL on error.
1435 * Free the returned object with g_object_unref().
1438 g_file_find_enclosing_mount (GFile *file,
1439 GCancellable *cancellable,
1444 g_return_val_if_fail (G_IS_FILE (file), NULL);
1446 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1449 iface = G_FILE_GET_IFACE (file);
1450 if (iface->find_enclosing_mount == NULL)
1453 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1454 /* Translators: This is an error message when
1455 * trying to find the enclosing (user visible)
1456 * mount of a file, but none exists.
1458 _("Containing mount does not exist"));
1462 return (* iface->find_enclosing_mount) (file, cancellable, error);
1466 * g_file_find_enclosing_mount_async:
1468 * @io_priority: the [I/O priority][io-priority] of the request
1469 * @cancellable: (allow-none): optional #GCancellable object,
1471 * @callback: (scope async): a #GAsyncReadyCallback to call
1472 * when the request is satisfied
1473 * @user_data: (closure): the data to pass to callback function
1475 * Asynchronously gets the mount for the file.
1477 * For more details, see g_file_find_enclosing_mount() which is
1478 * the synchronous version of this call.
1480 * When the operation is finished, @callback will be called.
1481 * You can then call g_file_find_enclosing_mount_finish() to
1482 * get the result of the operation.
1485 g_file_find_enclosing_mount_async (GFile *file,
1487 GCancellable *cancellable,
1488 GAsyncReadyCallback callback,
1493 g_return_if_fail (G_IS_FILE (file));
1495 iface = G_FILE_GET_IFACE (file);
1496 (* iface->find_enclosing_mount_async) (file,
1504 * g_file_find_enclosing_mount_finish:
1506 * @res: a #GAsyncResult
1509 * Finishes an asynchronous find mount request.
1510 * See g_file_find_enclosing_mount_async().
1512 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1513 * Free the returned object with g_object_unref().
1516 g_file_find_enclosing_mount_finish (GFile *file,
1522 g_return_val_if_fail (G_IS_FILE (file), NULL);
1523 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1525 if (g_async_result_legacy_propagate_error (res, error))
1528 iface = G_FILE_GET_IFACE (file);
1529 return (* iface->find_enclosing_mount_finish) (file, res, error);
1535 * @file: #GFile to read
1536 * @cancellable: (allow-none): a #GCancellable
1537 * @error: a #GError, or %NULL
1539 * Opens a file for reading. The result is a #GFileInputStream that
1540 * can be used to read the contents of the file.
1542 * If @cancellable is not %NULL, then the operation can be cancelled by
1543 * triggering the cancellable object from another thread. If the operation
1544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1546 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1547 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1548 * error will be returned. Other errors are possible too, and depend
1549 * on what kind of filesystem the file is on.
1552 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1553 * Free the returned object with g_object_unref().
1556 g_file_read (GFile *file,
1557 GCancellable *cancellable,
1562 g_return_val_if_fail (G_IS_FILE (file), NULL);
1564 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1567 iface = G_FILE_GET_IFACE (file);
1569 if (iface->read_fn == NULL)
1571 g_set_error_literal (error, G_IO_ERROR,
1572 G_IO_ERROR_NOT_SUPPORTED,
1573 _("Operation not supported"));
1577 return (* iface->read_fn) (file, cancellable, error);
1582 * @file: input #GFile
1583 * @flags: a set of #GFileCreateFlags
1584 * @cancellable: (allow-none): optional #GCancellable object,
1586 * @error: a #GError, or %NULL
1588 * Gets an output stream for appending data to the file.
1589 * If the file doesn't already exist it is created.
1591 * By default files created are generally readable by everyone,
1592 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1593 * will be made readable only to the current user, to the level that
1594 * is supported on the target filesystem.
1596 * If @cancellable is not %NULL, then the operation can be cancelled
1597 * by triggering the cancellable object from another thread. If the
1598 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1601 * Some file systems don't allow all file names, and may return an
1602 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1603 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1604 * possible too, and depend on what kind of filesystem the file is on.
1606 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1607 * Free the returned object with g_object_unref().
1610 g_file_append_to (GFile *file,
1611 GFileCreateFlags flags,
1612 GCancellable *cancellable,
1617 g_return_val_if_fail (G_IS_FILE (file), NULL);
1619 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1622 iface = G_FILE_GET_IFACE (file);
1624 if (iface->append_to == NULL)
1626 g_set_error_literal (error, G_IO_ERROR,
1627 G_IO_ERROR_NOT_SUPPORTED,
1628 _("Operation not supported"));
1632 return (* iface->append_to) (file, flags, cancellable, error);
1637 * @file: input #GFile
1638 * @flags: a set of #GFileCreateFlags
1639 * @cancellable: (allow-none): optional #GCancellable object,
1641 * @error: a #GError, or %NULL
1643 * Creates a new file and returns an output stream for writing to it.
1644 * The file must not already exist.
1646 * By default files created are generally readable by everyone,
1647 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1648 * will be made readable only to the current user, to the level
1649 * that is supported on the target filesystem.
1651 * If @cancellable is not %NULL, then the operation can be cancelled
1652 * by triggering the cancellable object from another thread. If the
1653 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1656 * If a file or directory with this name already exists the
1657 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1658 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1659 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1660 * be returned. Other errors are possible too, and depend on what kind
1661 * of filesystem the file is on.
1663 * Returns: (transfer full): a #GFileOutputStream for the newly created
1664 * file, or %NULL on error.
1665 * Free the returned object with g_object_unref().
1668 g_file_create (GFile *file,
1669 GFileCreateFlags flags,
1670 GCancellable *cancellable,
1675 g_return_val_if_fail (G_IS_FILE (file), NULL);
1677 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1680 iface = G_FILE_GET_IFACE (file);
1682 if (iface->create == NULL)
1684 g_set_error_literal (error, G_IO_ERROR,
1685 G_IO_ERROR_NOT_SUPPORTED,
1686 _("Operation not supported"));
1690 return (* iface->create) (file, flags, cancellable, error);
1695 * @file: input #GFile
1696 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1697 * for the current #GFile, or #NULL to ignore
1698 * @make_backup: %TRUE if a backup should be created
1699 * @flags: a set of #GFileCreateFlags
1700 * @cancellable: (allow-none): optional #GCancellable object,
1702 * @error: a #GError, or %NULL
1704 * Returns an output stream for overwriting the file, possibly
1705 * creating a backup copy of the file first. If the file doesn't exist,
1706 * it will be created.
1708 * This will try to replace the file in the safest way possible so
1709 * that any errors during the writing will not affect an already
1710 * existing copy of the file. For instance, for local files it
1711 * may write to a temporary file and then atomically rename over
1712 * the destination when the stream is closed.
1714 * By default files created are generally readable by everyone,
1715 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1716 * will be made readable only to the current user, to the level that
1717 * is supported on the target filesystem.
1719 * If @cancellable is not %NULL, then the operation can be cancelled
1720 * by triggering the cancellable object from another thread. If the
1721 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1724 * If you pass in a non-%NULL @etag value, then this value is
1725 * compared to the current entity tag of the file, and if they differ
1726 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1727 * that the file has been changed since you last read it. You can get
1728 * the new etag from g_file_output_stream_get_etag() after you've
1729 * finished writing and closed the #GFileOutputStream. When you load
1730 * a new file you can use g_file_input_stream_query_info() to get
1731 * the etag of the file.
1733 * If @make_backup is %TRUE, this function will attempt to make a
1734 * backup of the current file before overwriting it. If this fails
1735 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1736 * want to replace anyway, try again with @make_backup set to %FALSE.
1738 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1739 * be returned, and if the file is some other form of non-regular file
1740 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1741 * file systems don't allow all file names, and may return an
1742 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1743 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1744 * possible too, and depend on what kind of filesystem the file is on.
1746 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1747 * Free the returned object with g_object_unref().
1750 g_file_replace (GFile *file,
1752 gboolean make_backup,
1753 GFileCreateFlags flags,
1754 GCancellable *cancellable,
1759 g_return_val_if_fail (G_IS_FILE (file), NULL);
1761 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1764 iface = G_FILE_GET_IFACE (file);
1766 if (iface->replace == NULL)
1768 g_set_error_literal (error, G_IO_ERROR,
1769 G_IO_ERROR_NOT_SUPPORTED,
1770 _("Operation not supported"));
1774 /* Handle empty tag string as NULL in consistent way. */
1775 if (etag && *etag == 0)
1778 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1782 * g_file_open_readwrite:
1783 * @file: #GFile to open
1784 * @cancellable: (allow-none): a #GCancellable
1785 * @error: a #GError, or %NULL
1787 * Opens an existing file for reading and writing. The result is
1788 * a #GFileIOStream that can be used to read and write the contents
1791 * If @cancellable is not %NULL, then the operation can be cancelled
1792 * by triggering the cancellable object from another thread. If the
1793 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1796 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1797 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1798 * error will be returned. Other errors are possible too, and depend on
1799 * what kind of filesystem the file is on. Note that in many non-local
1800 * file cases read and write streams are not supported, so make sure you
1801 * really need to do read and write streaming, rather than just opening
1802 * for reading or writing.
1804 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1805 * Free the returned object with g_object_unref().
1810 g_file_open_readwrite (GFile *file,
1811 GCancellable *cancellable,
1816 g_return_val_if_fail (G_IS_FILE (file), NULL);
1818 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1821 iface = G_FILE_GET_IFACE (file);
1823 if (iface->open_readwrite == NULL)
1825 g_set_error_literal (error, G_IO_ERROR,
1826 G_IO_ERROR_NOT_SUPPORTED,
1827 _("Operation not supported"));
1831 return (* iface->open_readwrite) (file, cancellable, error);
1835 * g_file_create_readwrite:
1837 * @flags: a set of #GFileCreateFlags
1838 * @cancellable: (allow-none): optional #GCancellable object,
1840 * @error: return location for a #GError, or %NULL
1842 * Creates a new file and returns a stream for reading and
1843 * writing to it. The file must not already exist.
1845 * By default files created are generally readable by everyone,
1846 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1847 * will be made readable only to the current user, to the level
1848 * that is supported on the target filesystem.
1850 * If @cancellable is not %NULL, then the operation can be cancelled
1851 * by triggering the cancellable object from another thread. If the
1852 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1855 * If a file or directory with this name already exists, the
1856 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1857 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1858 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1859 * will be returned. Other errors are possible too, and depend on what
1860 * kind of filesystem the file is on.
1862 * Note that in many non-local file cases read and write streams are
1863 * not supported, so make sure you really need to do read and write
1864 * streaming, rather than just opening for reading or writing.
1866 * Returns: (transfer full): a #GFileIOStream for the newly created
1867 * file, or %NULL on error.
1868 * Free the returned object with g_object_unref().
1873 g_file_create_readwrite (GFile *file,
1874 GFileCreateFlags flags,
1875 GCancellable *cancellable,
1880 g_return_val_if_fail (G_IS_FILE (file), NULL);
1882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1885 iface = G_FILE_GET_IFACE (file);
1887 if (iface->create_readwrite == NULL)
1889 g_set_error_literal (error, G_IO_ERROR,
1890 G_IO_ERROR_NOT_SUPPORTED,
1891 _("Operation not supported"));
1895 return (* iface->create_readwrite) (file, flags, cancellable, error);
1899 * g_file_replace_readwrite:
1901 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1902 * for the current #GFile, or #NULL to ignore
1903 * @make_backup: %TRUE if a backup should be created
1904 * @flags: a set of #GFileCreateFlags
1905 * @cancellable: (allow-none): optional #GCancellable object,
1907 * @error: return location for a #GError, or %NULL
1909 * Returns an output stream for overwriting the file in readwrite mode,
1910 * possibly creating a backup copy of the file first. If the file doesn't
1911 * exist, it will be created.
1913 * For details about the behaviour, see g_file_replace() which does the
1914 * same thing but returns an output stream only.
1916 * Note that in many non-local file cases read and write streams are not
1917 * supported, so make sure you really need to do read and write streaming,
1918 * rather than just opening for reading or writing.
1920 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1921 * Free the returned object with g_object_unref().
1926 g_file_replace_readwrite (GFile *file,
1928 gboolean make_backup,
1929 GFileCreateFlags flags,
1930 GCancellable *cancellable,
1935 g_return_val_if_fail (G_IS_FILE (file), NULL);
1937 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1940 iface = G_FILE_GET_IFACE (file);
1942 if (iface->replace_readwrite == NULL)
1944 g_set_error_literal (error, G_IO_ERROR,
1945 G_IO_ERROR_NOT_SUPPORTED,
1946 _("Operation not supported"));
1950 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1954 * g_file_read_async:
1955 * @file: input #GFile
1956 * @io_priority: the [I/O priority][io-priority] of the request
1957 * @cancellable: (allow-none): optional #GCancellable object,
1959 * @callback: (scope async): a #GAsyncReadyCallback to call
1960 * when the request is satisfied
1961 * @user_data: (closure): the data to pass to callback function
1963 * Asynchronously opens @file for reading.
1965 * For more details, see g_file_read() which is
1966 * the synchronous version of this call.
1968 * When the operation is finished, @callback will be called.
1969 * You can then call g_file_read_finish() to get the result
1973 g_file_read_async (GFile *file,
1975 GCancellable *cancellable,
1976 GAsyncReadyCallback callback,
1981 g_return_if_fail (G_IS_FILE (file));
1983 iface = G_FILE_GET_IFACE (file);
1984 (* iface->read_async) (file,
1992 * g_file_read_finish:
1993 * @file: input #GFile
1994 * @res: a #GAsyncResult
1995 * @error: a #GError, or %NULL
1997 * Finishes an asynchronous file read operation started with
1998 * g_file_read_async().
2000 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2001 * Free the returned object with g_object_unref().
2004 g_file_read_finish (GFile *file,
2010 g_return_val_if_fail (G_IS_FILE (file), NULL);
2011 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2013 if (g_async_result_legacy_propagate_error (res, error))
2016 iface = G_FILE_GET_IFACE (file);
2017 return (* iface->read_finish) (file, res, error);
2021 * g_file_append_to_async:
2022 * @file: input #GFile
2023 * @flags: a set of #GFileCreateFlags
2024 * @io_priority: the [I/O priority][io-priority] of the request
2025 * @cancellable: (allow-none): optional #GCancellable object,
2027 * @callback: (scope async): a #GAsyncReadyCallback to call
2028 * when the request is satisfied
2029 * @user_data: (closure): the data to pass to callback function
2031 * Asynchronously opens @file for appending.
2033 * For more details, see g_file_append_to() which is
2034 * the synchronous version of this call.
2036 * When the operation is finished, @callback will be called.
2037 * You can then call g_file_append_to_finish() to get the result
2041 g_file_append_to_async (GFile *file,
2042 GFileCreateFlags flags,
2044 GCancellable *cancellable,
2045 GAsyncReadyCallback callback,
2050 g_return_if_fail (G_IS_FILE (file));
2052 iface = G_FILE_GET_IFACE (file);
2053 (* iface->append_to_async) (file,
2062 * g_file_append_to_finish:
2063 * @file: input #GFile
2064 * @res: #GAsyncResult
2065 * @error: a #GError, or %NULL
2067 * Finishes an asynchronous file append operation started with
2068 * g_file_append_to_async().
2070 * Returns: (transfer full): a valid #GFileOutputStream
2071 * or %NULL on error.
2072 * Free the returned object with g_object_unref().
2075 g_file_append_to_finish (GFile *file,
2081 g_return_val_if_fail (G_IS_FILE (file), NULL);
2082 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2084 if (g_async_result_legacy_propagate_error (res, error))
2087 iface = G_FILE_GET_IFACE (file);
2088 return (* iface->append_to_finish) (file, res, error);
2092 * g_file_create_async:
2093 * @file: input #GFile
2094 * @flags: a set of #GFileCreateFlags
2095 * @io_priority: the [I/O priority][io-priority] of the request
2096 * @cancellable: (allow-none): optional #GCancellable object,
2098 * @callback: (scope async): a #GAsyncReadyCallback to call
2099 * when the request is satisfied
2100 * @user_data: (closure): the data to pass to callback function
2102 * Asynchronously creates a new file and returns an output stream
2103 * for writing to it. The file must not already exist.
2105 * For more details, see g_file_create() which is
2106 * the synchronous version of this call.
2108 * When the operation is finished, @callback will be called.
2109 * You can then call g_file_create_finish() to get the result
2113 g_file_create_async (GFile *file,
2114 GFileCreateFlags flags,
2116 GCancellable *cancellable,
2117 GAsyncReadyCallback callback,
2122 g_return_if_fail (G_IS_FILE (file));
2124 iface = G_FILE_GET_IFACE (file);
2125 (* iface->create_async) (file,
2134 * g_file_create_finish:
2135 * @file: input #GFile
2136 * @res: a #GAsyncResult
2137 * @error: a #GError, or %NULL
2139 * Finishes an asynchronous file create operation started with
2140 * g_file_create_async().
2142 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2143 * Free the returned object with g_object_unref().
2146 g_file_create_finish (GFile *file,
2152 g_return_val_if_fail (G_IS_FILE (file), NULL);
2153 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2155 if (g_async_result_legacy_propagate_error (res, error))
2158 iface = G_FILE_GET_IFACE (file);
2159 return (* iface->create_finish) (file, res, error);
2163 * g_file_replace_async:
2164 * @file: input #GFile
2165 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2166 * or %NULL to ignore
2167 * @make_backup: %TRUE if a backup should be created
2168 * @flags: a set of #GFileCreateFlags
2169 * @io_priority: the [I/O priority][io-priority] of the request
2170 * @cancellable: (allow-none): optional #GCancellable object,
2172 * @callback: (scope async): a #GAsyncReadyCallback to call
2173 * when the request is satisfied
2174 * @user_data: (closure): the data to pass to callback function
2176 * Asynchronously overwrites the file, replacing the contents,
2177 * possibly creating a backup copy of the file first.
2179 * For more details, see g_file_replace() which is
2180 * the synchronous version of this call.
2182 * When the operation is finished, @callback will be called.
2183 * You can then call g_file_replace_finish() to get the result
2187 g_file_replace_async (GFile *file,
2189 gboolean make_backup,
2190 GFileCreateFlags flags,
2192 GCancellable *cancellable,
2193 GAsyncReadyCallback callback,
2198 g_return_if_fail (G_IS_FILE (file));
2200 iface = G_FILE_GET_IFACE (file);
2201 (* iface->replace_async) (file,
2212 * g_file_replace_finish:
2213 * @file: input #GFile
2214 * @res: a #GAsyncResult
2215 * @error: a #GError, or %NULL
2217 * Finishes an asynchronous file replace operation started with
2218 * g_file_replace_async().
2220 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2221 * Free the returned object with g_object_unref().
2224 g_file_replace_finish (GFile *file,
2230 g_return_val_if_fail (G_IS_FILE (file), NULL);
2231 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2233 if (g_async_result_legacy_propagate_error (res, error))
2236 iface = G_FILE_GET_IFACE (file);
2237 return (* iface->replace_finish) (file, res, error);
2241 * g_file_open_readwrite_async
2242 * @file: input #GFile
2243 * @io_priority: the [I/O priority][io-priority] of the request
2244 * @cancellable: (allow-none): optional #GCancellable object,
2246 * @callback: (scope async): a #GAsyncReadyCallback to call
2247 * when the request is satisfied
2248 * @user_data: (closure): the data to pass to callback function
2250 * Asynchronously opens @file for reading and writing.
2252 * For more details, see g_file_open_readwrite() which is
2253 * the synchronous version of this call.
2255 * When the operation is finished, @callback will be called.
2256 * You can then call g_file_open_readwrite_finish() to get
2257 * the result of the operation.
2262 g_file_open_readwrite_async (GFile *file,
2264 GCancellable *cancellable,
2265 GAsyncReadyCallback callback,
2270 g_return_if_fail (G_IS_FILE (file));
2272 iface = G_FILE_GET_IFACE (file);
2273 (* iface->open_readwrite_async) (file,
2281 * g_file_open_readwrite_finish:
2282 * @file: input #GFile
2283 * @res: a #GAsyncResult
2284 * @error: a #GError, or %NULL
2286 * Finishes an asynchronous file read operation started with
2287 * g_file_open_readwrite_async().
2289 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2290 * Free the returned object with g_object_unref().
2295 g_file_open_readwrite_finish (GFile *file,
2301 g_return_val_if_fail (G_IS_FILE (file), NULL);
2302 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2304 if (g_async_result_legacy_propagate_error (res, error))
2307 iface = G_FILE_GET_IFACE (file);
2308 return (* iface->open_readwrite_finish) (file, res, error);
2312 * g_file_create_readwrite_async:
2313 * @file: input #GFile
2314 * @flags: a set of #GFileCreateFlags
2315 * @io_priority: the [I/O priority][io-priority] of the request
2316 * @cancellable: (allow-none): optional #GCancellable object,
2318 * @callback: (scope async): a #GAsyncReadyCallback to call
2319 * when the request is satisfied
2320 * @user_data: (closure): the data to pass to callback function
2322 * Asynchronously creates a new file and returns a stream
2323 * for reading and writing to it. The file must not already exist.
2325 * For more details, see g_file_create_readwrite() which is
2326 * the synchronous version of this call.
2328 * When the operation is finished, @callback will be called.
2329 * You can then call g_file_create_readwrite_finish() to get
2330 * the result of the operation.
2335 g_file_create_readwrite_async (GFile *file,
2336 GFileCreateFlags flags,
2338 GCancellable *cancellable,
2339 GAsyncReadyCallback callback,
2344 g_return_if_fail (G_IS_FILE (file));
2346 iface = G_FILE_GET_IFACE (file);
2347 (* iface->create_readwrite_async) (file,
2356 * g_file_create_readwrite_finish:
2357 * @file: input #GFile
2358 * @res: a #GAsyncResult
2359 * @error: a #GError, or %NULL
2361 * Finishes an asynchronous file create operation started with
2362 * g_file_create_readwrite_async().
2364 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2365 * Free the returned object with g_object_unref().
2370 g_file_create_readwrite_finish (GFile *file,
2376 g_return_val_if_fail (G_IS_FILE (file), NULL);
2377 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2379 if (g_async_result_legacy_propagate_error (res, error))
2382 iface = G_FILE_GET_IFACE (file);
2383 return (* iface->create_readwrite_finish) (file, res, error);
2387 * g_file_replace_readwrite_async:
2388 * @file: input #GFile
2389 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2390 * or %NULL to ignore
2391 * @make_backup: %TRUE if a backup should be created
2392 * @flags: a set of #GFileCreateFlags
2393 * @io_priority: the [I/O priority][io-priority] of the request
2394 * @cancellable: (allow-none): optional #GCancellable object,
2396 * @callback: (scope async): a #GAsyncReadyCallback to call
2397 * when the request is satisfied
2398 * @user_data: (closure): the data to pass to callback function
2400 * Asynchronously overwrites the file in read-write mode,
2401 * replacing the contents, possibly creating a backup copy
2402 * of the file first.
2404 * For more details, see g_file_replace_readwrite() which is
2405 * the synchronous version of this call.
2407 * When the operation is finished, @callback will be called.
2408 * You can then call g_file_replace_readwrite_finish() to get
2409 * the result of the operation.
2414 g_file_replace_readwrite_async (GFile *file,
2416 gboolean make_backup,
2417 GFileCreateFlags flags,
2419 GCancellable *cancellable,
2420 GAsyncReadyCallback callback,
2425 g_return_if_fail (G_IS_FILE (file));
2427 iface = G_FILE_GET_IFACE (file);
2428 (* iface->replace_readwrite_async) (file,
2439 * g_file_replace_readwrite_finish:
2440 * @file: input #GFile
2441 * @res: a #GAsyncResult
2442 * @error: a #GError, or %NULL
2444 * Finishes an asynchronous file replace operation started with
2445 * g_file_replace_readwrite_async().
2447 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2448 * Free the returned object with g_object_unref().
2453 g_file_replace_readwrite_finish (GFile *file,
2459 g_return_val_if_fail (G_IS_FILE (file), NULL);
2460 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2462 if (g_async_result_legacy_propagate_error (res, error))
2465 iface = G_FILE_GET_IFACE (file);
2466 return (* iface->replace_readwrite_finish) (file, res, error);
2470 copy_symlink (GFile *destination,
2471 GFileCopyFlags flags,
2472 GCancellable *cancellable,
2477 gboolean tried_delete;
2479 GFileType file_type;
2481 tried_delete = FALSE;
2485 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2487 /* Maybe it already existed, and we want to overwrite? */
2488 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2489 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2491 g_clear_error (&my_error);
2493 /* Don't overwrite if the destination is a directory */
2494 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2495 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2496 cancellable, &my_error);
2499 file_type = g_file_info_get_file_type (info);
2500 g_object_unref (info);
2502 if (file_type == G_FILE_TYPE_DIRECTORY)
2504 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2505 _("Can't copy over directory"));
2510 if (!g_file_delete (destination, cancellable, error))
2513 tried_delete = TRUE;
2517 g_propagate_error (error, my_error);
2524 static GFileInputStream *
2525 open_source_for_copy (GFile *source,
2527 GFileCopyFlags flags,
2528 GCancellable *cancellable,
2532 GFileInputStream *ret;
2534 GFileType file_type;
2537 ret = g_file_read (source, cancellable, &my_error);
2541 /* There was an error opening the source, try to set a good error for it: */
2542 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2544 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2545 * as that is less useful to the app. Better check for errors on the
2548 g_error_free (my_error);
2551 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2552 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2553 cancellable, &my_error);
2555 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2557 file_type = g_file_info_get_file_type (info);
2558 g_object_unref (info);
2560 if (flags & G_FILE_COPY_OVERWRITE)
2562 if (file_type == G_FILE_TYPE_DIRECTORY)
2564 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2565 _("Can't copy directory over directory"));
2568 /* continue to would_recurse error */
2572 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2573 _("Target file exists"));
2579 /* Error getting info from target, return that error
2580 * (except for NOT_FOUND, which is no error here)
2582 g_clear_object (&info);
2583 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2585 g_propagate_error (error, my_error);
2588 g_clear_error (&my_error);
2591 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2592 _("Can't recursively copy directory"));
2596 g_propagate_error (error, my_error);
2601 should_copy (GFileAttributeInfo *info,
2602 gboolean copy_all_attributes,
2603 gboolean skip_perms)
2605 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2608 if (copy_all_attributes)
2609 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2610 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2614 build_attribute_list_for_copy (GFile *file,
2615 GFileCopyFlags flags,
2616 char **out_attributes,
2617 GCancellable *cancellable,
2620 gboolean ret = FALSE;
2621 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2625 gboolean copy_all_attributes;
2626 gboolean skip_perms;
2628 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2629 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2631 /* Ignore errors here, if the target supports no attributes there is
2632 * nothing to copy. We still honor the cancellable though.
2634 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2635 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2638 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2639 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2642 if (attributes == NULL && namespaces == NULL)
2646 s = g_string_new ("");
2650 for (i = 0; i < attributes->n_infos; i++)
2652 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2657 g_string_append_c (s, ',');
2659 g_string_append (s, attributes->infos[i].name);
2666 for (i = 0; i < namespaces->n_infos; i++)
2668 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2673 g_string_append_c (s, ',');
2675 g_string_append (s, namespaces->infos[i].name);
2676 g_string_append (s, "::*");
2682 *out_attributes = g_string_free (s, FALSE);
2686 g_string_free (s, TRUE);
2688 g_file_attribute_info_list_unref (attributes);
2690 g_file_attribute_info_list_unref (namespaces);
2696 * g_file_copy_attributes:
2697 * @source: a #GFile with attributes
2698 * @destination: a #GFile to copy attributes to
2699 * @flags: a set of #GFileCopyFlags
2700 * @cancellable: (allow-none): optional #GCancellable object,
2702 * @error: a #GError, %NULL to ignore
2704 * Copies the file attributes from @source to @destination.
2706 * Normally only a subset of the file attributes are copied,
2707 * those that are copies in a normal file copy operation
2708 * (which for instance does not include e.g. owner). However
2709 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2710 * all the metadata that is possible to copy is copied. This
2711 * is useful when implementing move by copy + delete source.
2713 * Returns: %TRUE if the attributes were copied successfully,
2717 g_file_copy_attributes (GFile *source,
2719 GFileCopyFlags flags,
2720 GCancellable *cancellable,
2723 char *attrs_to_read;
2726 gboolean source_nofollow_symlinks;
2728 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2729 cancellable, error))
2732 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2734 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2735 * we just don't copy it.
2737 info = g_file_query_info (source, attrs_to_read,
2738 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2742 g_free (attrs_to_read);
2747 res = g_file_set_attributes_from_info (destination,
2749 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2752 g_object_unref (info);
2759 copy_stream_with_progress (GInputStream *in,
2762 GCancellable *cancellable,
2763 GFileProgressCallback progress_callback,
2764 gpointer progress_callback_data,
2767 gssize n_read, n_written;
2768 goffset current_size;
2769 char buffer[1024*64], *p;
2775 /* avoid performance impact of querying total size when it's not needed */
2776 if (progress_callback)
2778 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2779 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2783 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2784 total_size = g_file_info_get_size (info);
2785 g_object_unref (info);
2788 if (total_size == -1)
2790 info = g_file_query_info (source,
2791 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2792 G_FILE_QUERY_INFO_NONE,
2796 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2797 total_size = g_file_info_get_size (info);
2798 g_object_unref (info);
2803 if (total_size == -1)
2810 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2820 current_size += n_read;
2825 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2826 if (n_written == -1)
2833 n_read -= n_written;
2839 if (progress_callback)
2840 progress_callback (current_size, total_size, progress_callback_data);
2843 /* Make sure we send full copied size */
2844 if (progress_callback)
2845 progress_callback (current_size, total_size, progress_callback_data);
2853 do_splice (int fd_in,
2858 long *bytes_transferd,
2864 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2872 else if (errsv == ENOSYS || errsv == EINVAL)
2873 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2874 _("Splice not supported"));
2876 g_set_error (error, G_IO_ERROR,
2877 g_io_error_from_errno (errsv),
2878 _("Error splicing file: %s"),
2879 g_strerror (errsv));
2884 *bytes_transferd = result;
2889 splice_stream_with_progress (GInputStream *in,
2891 GCancellable *cancellable,
2892 GFileProgressCallback progress_callback,
2893 gpointer progress_callback_data,
2896 int buffer[2] = { -1, -1 };
2903 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2904 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2906 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2910 /* avoid performance impact of querying total size when it's not needed */
2911 if (progress_callback)
2915 if (fstat (fd_in, &sbuf) == 0)
2916 total_size = sbuf.st_size;
2919 if (total_size == -1)
2922 offset_in = offset_out = 0;
2929 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2932 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2943 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2946 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2949 n_read -= n_written;
2952 if (progress_callback)
2953 progress_callback (offset_in, total_size, progress_callback_data);
2956 /* Make sure we send full copied size */
2957 if (progress_callback)
2958 progress_callback (offset_in, total_size, progress_callback_data);
2960 if (!g_close (buffer[0], error))
2963 if (!g_close (buffer[1], error))
2967 if (buffer[0] != -1)
2968 (void) g_close (buffer[0], NULL);
2969 if (buffer[1] != -1)
2970 (void) g_close (buffer[1], NULL);
2978 btrfs_reflink_with_progress (GInputStream *in,
2981 GCancellable *cancellable,
2982 GFileProgressCallback progress_callback,
2983 gpointer progress_callback_data,
2986 goffset source_size;
2990 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2991 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2993 if (progress_callback)
2994 source_size = g_file_info_get_size (info);
2996 /* Btrfs clone ioctl properties:
2997 * - Works at the inode level
2998 * - Doesn't work with directories
2999 * - Always follows symlinks (source and destination)
3001 * By the time we get here, *in and *out are both regular files */
3002 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3007 g_set_error_literal (error, G_IO_ERROR,
3008 G_IO_ERROR_NOT_SUPPORTED,
3009 _("Copy (reflink/clone) between mounts is not supported"));
3010 else if (errno == EINVAL)
3011 g_set_error_literal (error, G_IO_ERROR,
3012 G_IO_ERROR_NOT_SUPPORTED,
3013 _("Copy (reflink/clone) is not supported or invalid"));
3015 /* Most probably something odd happened; retry with fallback */
3016 g_set_error_literal (error, G_IO_ERROR,
3017 G_IO_ERROR_NOT_SUPPORTED,
3018 _("Copy (reflink/clone) is not supported or didn't work"));
3019 /* We retry with fallback for all error cases because Btrfs is currently
3020 * unstable, and so we can't trust it to do clone properly.
3021 * In addition, any hard errors here would cause the same failure in the
3022 * fallback manual copy as well. */
3026 /* Make sure we send full copied size */
3027 if (progress_callback)
3028 progress_callback (source_size, source_size, progress_callback_data);
3035 file_copy_fallback (GFile *source,
3037 GFileCopyFlags flags,
3038 GCancellable *cancellable,
3039 GFileProgressCallback progress_callback,
3040 gpointer progress_callback_data,
3043 gboolean ret = FALSE;
3044 GFileInputStream *file_in = NULL;
3045 GInputStream *in = NULL;
3046 GOutputStream *out = NULL;
3047 GFileInfo *info = NULL;
3049 char *attrs_to_read;
3050 gboolean do_set_attributes = FALSE;
3052 /* need to know the file type */
3053 info = g_file_query_info (source,
3054 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3055 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3061 /* Maybe copy the symlink? */
3062 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3063 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3065 target = g_file_info_get_symlink_target (info);
3068 if (!copy_symlink (destination, flags, cancellable, target, error))
3074 /* ... else fall back on a regular file copy */
3076 /* Handle "special" files (pipes, device nodes, ...)? */
3077 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3079 /* FIXME: could try to recreate device nodes and others? */
3080 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3081 _("Can't copy special file"));
3085 /* Everything else should just fall back on a regular copy. */
3087 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3090 in = G_INPUT_STREAM (file_in);
3092 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3093 cancellable, error))
3096 if (attrs_to_read != NULL)
3098 GError *tmp_error = NULL;
3100 /* Ok, ditch the previous lightweight info (on Unix we just
3101 * called lstat()); at this point we gather all the information
3102 * we need about the source from the opened file descriptor.
3104 g_object_unref (info);
3106 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3107 cancellable, &tmp_error);
3110 /* Not all gvfs backends implement query_info_on_read(), we
3111 * can just fall back to the pathname again.
3112 * https://bugzilla.gnome.org/706254
3114 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3116 g_clear_error (&tmp_error);
3117 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3118 cancellable, error);
3122 g_free (attrs_to_read);
3123 g_propagate_error (error, tmp_error);
3127 g_free (attrs_to_read);
3131 do_set_attributes = TRUE;
3134 /* In the local file path, we pass down the source info which
3135 * includes things like unix::mode, to ensure that the target file
3136 * is not created with different permissions from the source file.
3138 * If a future API like g_file_replace_with_info() is added, switch
3139 * this code to use that.
3141 if (G_IS_LOCAL_FILE (destination))
3143 if (flags & G_FILE_COPY_OVERWRITE)
3144 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3146 flags & G_FILE_COPY_BACKUP,
3147 G_FILE_CREATE_REPLACE_DESTINATION,
3149 cancellable, error);
3151 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3153 cancellable, error);
3155 else if (flags & G_FILE_COPY_OVERWRITE)
3157 out = (GOutputStream *)g_file_replace (destination,
3159 flags & G_FILE_COPY_BACKUP,
3160 G_FILE_CREATE_REPLACE_DESTINATION,
3161 cancellable, error);
3165 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3172 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3174 GError *reflink_err = NULL;
3176 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3177 progress_callback, progress_callback_data,
3180 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3182 g_clear_error (&reflink_err);
3186 g_propagate_error (error, reflink_err);
3199 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3201 GError *splice_err = NULL;
3203 if (!splice_stream_with_progress (in, out, cancellable,
3204 progress_callback, progress_callback_data,
3207 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3209 g_clear_error (&splice_err);
3213 g_propagate_error (error, splice_err);
3226 /* A plain read/write loop */
3227 if (!copy_stream_with_progress (in, out, source, cancellable,
3228 progress_callback, progress_callback_data,
3236 /* Don't care about errors in source here */
3237 (void) g_input_stream_close (in, cancellable, NULL);
3238 g_object_unref (in);
3243 /* But write errors on close are bad! */
3244 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3246 g_object_unref (out);
3249 /* Ignore errors here. Failure to copy metadata is not a hard error */
3250 /* TODO: set these attributes /before/ we do the rename() on Unix */
3251 if (ret && do_set_attributes)
3253 g_file_set_attributes_from_info (destination,
3255 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3260 g_clear_object (&info);
3267 * @source: input #GFile
3268 * @destination: destination #GFile
3269 * @flags: set of #GFileCopyFlags
3270 * @cancellable: (allow-none): optional #GCancellable object,
3272 * @progress_callback: (allow-none) (scope call): function to callback with
3273 * progress information, or %NULL if progress information is not needed
3274 * @progress_callback_data: (closure): user data to pass to @progress_callback
3275 * @error: #GError to set on error, or %NULL
3277 * Copies the file @source to the location specified by @destination.
3278 * Can not handle recursive copies of directories.
3280 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3281 * existing @destination file is overwritten.
3283 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3284 * will be copied as symlinks, otherwise the target of the
3285 * @source symlink will be copied.
3287 * If @cancellable is not %NULL, then the operation can be cancelled by
3288 * triggering the cancellable object from another thread. If the operation
3289 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3291 * If @progress_callback is not %NULL, then the operation can be monitored
3292 * by setting this to a #GFileProgressCallback function.
3293 * @progress_callback_data will be passed to this function. It is guaranteed
3294 * that this callback will be called after all data has been transferred with
3295 * the total number of bytes copied during the operation.
3297 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3298 * is returned, independent on the status of the @destination.
3300 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3301 * the error %G_IO_ERROR_EXISTS is returned.
3303 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3304 * error is returned. If trying to overwrite a directory with a directory the
3305 * %G_IO_ERROR_WOULD_MERGE error is returned.
3307 * If the source is a directory and the target does not exist, or
3308 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3309 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3311 * If you are interested in copying the #GFile object itself (not the on-disk
3312 * file), see g_file_dup().
3314 * Returns: %TRUE on success, %FALSE otherwise.
3317 g_file_copy (GFile *source,
3319 GFileCopyFlags flags,
3320 GCancellable *cancellable,
3321 GFileProgressCallback progress_callback,
3322 gpointer progress_callback_data,
3329 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3330 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3332 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3335 iface = G_FILE_GET_IFACE (destination);
3339 res = (* iface->copy) (source, destination,
3341 progress_callback, progress_callback_data,
3347 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3349 g_propagate_error (error, my_error);
3353 g_clear_error (&my_error);
3356 /* If the types are different, and the destination method failed
3357 * also try the source method
3359 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3361 iface = G_FILE_GET_IFACE (source);
3366 res = (* iface->copy) (source, destination,
3368 progress_callback, progress_callback_data,
3374 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3376 g_propagate_error (error, my_error);
3380 g_clear_error (&my_error);
3384 return file_copy_fallback (source, destination, flags, cancellable,
3385 progress_callback, progress_callback_data,
3390 * g_file_copy_async: (skip)
3391 * @source: input #GFile
3392 * @destination: destination #GFile
3393 * @flags: set of #GFileCopyFlags
3394 * @io_priority: the [I/O priority][io-priority] of the request
3395 * @cancellable: (allow-none): optional #GCancellable object,
3397 * @progress_callback: (allow-none): function to callback with progress
3398 * information, or %NULL if progress information is not needed
3399 * @progress_callback_data: (closure): user data to pass to @progress_callback
3400 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3401 * @user_data: the data to pass to callback function
3403 * Copies the file @source to the location specified by @destination
3404 * asynchronously. For details of the behaviour, see g_file_copy().
3406 * If @progress_callback is not %NULL, then that function that will be called
3407 * just like in g_file_copy(). The callback will run in the default main context
3408 * of the thread calling g_file_copy_async() — the same context as @callback is
3411 * When the operation is finished, @callback will be called. You can then call
3412 * g_file_copy_finish() to get the result of the operation.
3415 g_file_copy_async (GFile *source,
3417 GFileCopyFlags flags,
3419 GCancellable *cancellable,
3420 GFileProgressCallback progress_callback,
3421 gpointer progress_callback_data,
3422 GAsyncReadyCallback callback,
3427 g_return_if_fail (G_IS_FILE (source));
3428 g_return_if_fail (G_IS_FILE (destination));
3430 iface = G_FILE_GET_IFACE (source);
3431 (* iface->copy_async) (source,
3437 progress_callback_data,
3443 * g_file_copy_finish:
3444 * @file: input #GFile
3445 * @res: a #GAsyncResult
3446 * @error: a #GError, or %NULL
3448 * Finishes copying the file started with g_file_copy_async().
3450 * Returns: a %TRUE on success, %FALSE on error.
3453 g_file_copy_finish (GFile *file,
3459 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3460 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3462 if (g_async_result_legacy_propagate_error (res, error))
3465 iface = G_FILE_GET_IFACE (file);
3466 return (* iface->copy_finish) (file, res, error);
3471 * @source: #GFile pointing to the source location
3472 * @destination: #GFile pointing to the destination location
3473 * @flags: set of #GFileCopyFlags
3474 * @cancellable: (allow-none): optional #GCancellable object,
3476 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3477 * function for updates
3478 * @progress_callback_data: (closure): gpointer to user data for
3479 * the callback function
3480 * @error: #GError for returning error conditions, or %NULL
3482 * Tries to move the file or directory @source to the location specified
3483 * by @destination. If native move operations are supported then this is
3484 * used, otherwise a copy + delete fallback is used. The native
3485 * implementation may support moving directories (for instance on moves
3486 * inside the same filesystem), but the fallback code does not.
3488 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3489 * existing @destination file is overwritten.
3491 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3492 * will be copied as symlinks, otherwise the target of the
3493 * @source symlink will be copied.
3495 * If @cancellable is not %NULL, then the operation can be cancelled by
3496 * triggering the cancellable object from another thread. If the operation
3497 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3499 * If @progress_callback is not %NULL, then the operation can be monitored
3500 * by setting this to a #GFileProgressCallback function.
3501 * @progress_callback_data will be passed to this function. It is
3502 * guaranteed that this callback will be called after all data has been
3503 * transferred with the total number of bytes copied during the operation.
3505 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3506 * error is returned, independent on the status of the @destination.
3508 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3509 * then the error %G_IO_ERROR_EXISTS is returned.
3511 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3512 * error is returned. If trying to overwrite a directory with a directory the
3513 * %G_IO_ERROR_WOULD_MERGE error is returned.
3515 * If the source is a directory and the target does not exist, or
3516 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3517 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3518 * move operation isn't available).
3520 * Returns: %TRUE on successful move, %FALSE otherwise.
3523 g_file_move (GFile *source,
3525 GFileCopyFlags flags,
3526 GCancellable *cancellable,
3527 GFileProgressCallback progress_callback,
3528 gpointer progress_callback_data,
3535 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3536 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3538 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3541 iface = G_FILE_GET_IFACE (destination);
3545 res = (* iface->move) (source, destination,
3547 progress_callback, progress_callback_data,
3553 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3555 g_propagate_error (error, my_error);
3559 g_clear_error (&my_error);
3562 /* If the types are different, and the destination method failed
3563 * also try the source method
3565 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3567 iface = G_FILE_GET_IFACE (source);
3572 res = (* iface->move) (source, destination,
3574 progress_callback, progress_callback_data,
3580 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3582 g_propagate_error (error, my_error);
3586 g_clear_error (&my_error);
3590 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3592 g_set_error_literal (error, G_IO_ERROR,
3593 G_IO_ERROR_NOT_SUPPORTED,
3594 _("Operation not supported"));
3598 flags |= G_FILE_COPY_ALL_METADATA;
3599 if (!g_file_copy (source, destination, flags, cancellable,
3600 progress_callback, progress_callback_data,
3604 return g_file_delete (source, cancellable, error);
3608 * g_file_make_directory:
3609 * @file: input #GFile
3610 * @cancellable: (allow-none): optional #GCancellable object,
3612 * @error: a #GError, or %NULL
3614 * Creates a directory. Note that this will only create a child directory
3615 * of the immediate parent directory of the path or URI given by the #GFile.
3616 * To recursively create directories, see g_file_make_directory_with_parents().
3617 * This function will fail if the parent directory does not exist, setting
3618 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3619 * creating directories, this function will fail, setting @error to
3620 * %G_IO_ERROR_NOT_SUPPORTED.
3622 * For a local #GFile the newly created directory will have the default
3623 * (current) ownership and permissions of the current process.
3625 * If @cancellable is not %NULL, then the operation can be cancelled by
3626 * triggering the cancellable object from another thread. If the operation
3627 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3629 * Returns: %TRUE on successful creation, %FALSE otherwise.
3632 g_file_make_directory (GFile *file,
3633 GCancellable *cancellable,
3638 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3640 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3643 iface = G_FILE_GET_IFACE (file);
3645 if (iface->make_directory == NULL)
3647 g_set_error_literal (error, G_IO_ERROR,
3648 G_IO_ERROR_NOT_SUPPORTED,
3649 _("Operation not supported"));
3653 return (* iface->make_directory) (file, cancellable, error);
3657 * g_file_make_directory_async:
3658 * @file: input #GFile
3659 * @io_priority: the [I/O priority][io-priority] of the request
3660 * @cancellable: (allow-none): optional #GCancellable object,
3662 * @callback: a #GAsyncReadyCallback to call
3663 * when the request is satisfied
3664 * @user_data: the data to pass to callback function
3666 * Asynchronously creates a directory.
3668 * Virtual: make_directory_async
3672 g_file_make_directory_async (GFile *file,
3674 GCancellable *cancellable,
3675 GAsyncReadyCallback callback,
3680 g_return_if_fail (G_IS_FILE (file));
3682 iface = G_FILE_GET_IFACE (file);
3683 (* iface->make_directory_async) (file,
3691 * g_file_make_directory_finish:
3692 * @file: input #GFile
3693 * @result: a #GAsyncResult
3694 * @error: a #GError, or %NULL
3696 * Finishes an asynchronous directory creation, started with
3697 * g_file_make_directory_async().
3699 * Virtual: make_directory_finish
3700 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3704 g_file_make_directory_finish (GFile *file,
3705 GAsyncResult *result,
3710 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3711 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3713 iface = G_FILE_GET_IFACE (file);
3714 return (* iface->make_directory_finish) (file, result, error);
3718 * g_file_make_directory_with_parents:
3719 * @file: input #GFile
3720 * @cancellable: (allow-none): optional #GCancellable object,
3722 * @error: a #GError, or %NULL
3724 * Creates a directory and any parent directories that may not
3725 * exist similar to 'mkdir -p'. If the file system does not support
3726 * creating directories, this function will fail, setting @error to
3727 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3728 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3729 * the similar g_mkdir_with_parents().
3731 * For a local #GFile the newly created directories will have the default
3732 * (current) ownership and permissions of the current process.
3734 * If @cancellable is not %NULL, then the operation can be cancelled by
3735 * triggering the cancellable object from another thread. If the operation
3736 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3738 * Returns: %TRUE if all directories have been successfully created, %FALSE
3744 g_file_make_directory_with_parents (GFile *file,
3745 GCancellable *cancellable,
3748 GFile *work_file = NULL;
3749 GList *list = NULL, *l;
3750 GError *my_error = NULL;
3752 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3754 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3757 g_file_make_directory (file, cancellable, &my_error);
3758 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3761 g_propagate_error (error, my_error);
3762 return my_error == NULL;
3765 work_file = g_object_ref (file);
3767 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3771 parent_file = g_file_get_parent (work_file);
3772 if (parent_file == NULL)
3775 g_clear_error (&my_error);
3776 g_file_make_directory (parent_file, cancellable, &my_error);
3778 g_object_unref (work_file);
3779 work_file = g_object_ref (parent_file);
3781 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3782 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3784 g_object_unref (parent_file);
3787 for (l = list; my_error == NULL && l; l = l->next)
3789 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3793 g_object_unref (work_file);
3796 while (list != NULL)
3798 g_object_unref ((GFile *) list->data);
3799 list = g_list_remove (list, list->data);
3802 if (my_error != NULL)
3804 g_propagate_error (error, my_error);
3808 return g_file_make_directory (file, cancellable, error);
3812 * g_file_make_symbolic_link:
3813 * @file: a #GFile with the name of the symlink to create
3814 * @symlink_value: a string with the path for the target of the new symlink
3815 * @cancellable: (allow-none): optional #GCancellable object,
3819 * Creates a symbolic link named @file which contains the string
3822 * If @cancellable is not %NULL, then the operation can be cancelled by
3823 * triggering the cancellable object from another thread. If the operation
3824 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3826 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3829 g_file_make_symbolic_link (GFile *file,
3830 const char *symlink_value,
3831 GCancellable *cancellable,
3836 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3837 g_return_val_if_fail (symlink_value != NULL, FALSE);
3839 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3842 if (*symlink_value == '\0')
3844 g_set_error_literal (error, G_IO_ERROR,
3845 G_IO_ERROR_INVALID_ARGUMENT,
3846 _("Invalid symlink value given"));
3850 iface = G_FILE_GET_IFACE (file);
3852 if (iface->make_symbolic_link == NULL)
3854 g_set_error_literal (error, G_IO_ERROR,
3855 G_IO_ERROR_NOT_SUPPORTED,
3856 _("Operation not supported"));
3860 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3865 * @file: input #GFile
3866 * @cancellable: (allow-none): optional #GCancellable object,
3868 * @error: a #GError, or %NULL
3870 * Deletes a file. If the @file is a directory, it will only be
3871 * deleted if it is empty. This has the same semantics as g_unlink().
3873 * If @cancellable is not %NULL, then the operation can be cancelled by
3874 * triggering the cancellable object from another thread. If the operation
3875 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3877 * Virtual: delete_file
3878 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3881 g_file_delete (GFile *file,
3882 GCancellable *cancellable,
3887 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3889 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3892 iface = G_FILE_GET_IFACE (file);
3894 if (iface->delete_file == NULL)
3896 g_set_error_literal (error, G_IO_ERROR,
3897 G_IO_ERROR_NOT_SUPPORTED,
3898 _("Operation not supported"));
3902 return (* iface->delete_file) (file, cancellable, error);
3906 * g_file_delete_async:
3907 * @file: input #GFile
3908 * @io_priority: the [I/O priority][io-priority] of the request
3909 * @cancellable: (allow-none): optional #GCancellable object,
3911 * @callback: a #GAsyncReadyCallback to call
3912 * when the request is satisfied
3913 * @user_data: the data to pass to callback function
3915 * Asynchronously delete a file. If the @file is a directory, it will
3916 * only be deleted if it is empty. This has the same semantics as
3919 * Virtual: delete_file_async
3923 g_file_delete_async (GFile *file,
3925 GCancellable *cancellable,
3926 GAsyncReadyCallback callback,
3931 g_return_if_fail (G_IS_FILE (file));
3933 iface = G_FILE_GET_IFACE (file);
3934 (* iface->delete_file_async) (file,
3942 * g_file_delete_finish:
3943 * @file: input #GFile
3944 * @result: a #GAsyncResult
3945 * @error: a #GError, or %NULL
3947 * Finishes deleting a file started with g_file_delete_async().
3949 * Virtual: delete_file_finish
3950 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3954 g_file_delete_finish (GFile *file,
3955 GAsyncResult *result,
3960 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3961 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3963 if (g_async_result_legacy_propagate_error (result, error))
3966 iface = G_FILE_GET_IFACE (file);
3967 return (* iface->delete_file_finish) (file, result, error);
3972 * @file: #GFile to send to trash
3973 * @cancellable: (allow-none): optional #GCancellable object,
3975 * @error: a #GError, or %NULL
3977 * Sends @file to the "Trashcan", if possible. This is similar to
3978 * deleting it, but the user can recover it before emptying the trashcan.
3979 * Not all file systems support trashing, so this call can return the
3980 * %G_IO_ERROR_NOT_SUPPORTED error.
3982 * If @cancellable is not %NULL, then the operation can be cancelled by
3983 * triggering the cancellable object from another thread. If the operation
3984 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3987 * Returns: %TRUE on successful trash, %FALSE otherwise.
3990 g_file_trash (GFile *file,
3991 GCancellable *cancellable,
3996 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3998 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4001 iface = G_FILE_GET_IFACE (file);
4003 if (iface->trash == NULL)
4005 g_set_error_literal (error,
4006 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4007 _("Trash not supported"));
4011 return (* iface->trash) (file, cancellable, error);
4015 * g_file_trash_async:
4016 * @file: input #GFile
4017 * @io_priority: the [I/O priority][io-priority] of the request
4018 * @cancellable: (allow-none): optional #GCancellable object,
4020 * @callback: a #GAsyncReadyCallback to call
4021 * when the request is satisfied
4022 * @user_data: the data to pass to callback function
4024 * Asynchronously sends @file to the Trash location, if possible.
4026 * Virtual: trash_async
4030 g_file_trash_async (GFile *file,
4032 GCancellable *cancellable,
4033 GAsyncReadyCallback callback,
4038 g_return_if_fail (G_IS_FILE (file));
4040 iface = G_FILE_GET_IFACE (file);
4041 (* iface->trash_async) (file,
4049 * g_file_trash_finish:
4050 * @file: input #GFile
4051 * @result: a #GAsyncResult
4052 * @error: a #GError, or %NULL
4054 * Finishes an asynchronous file trashing operation, started with
4055 * g_file_trash_async().
4057 * Virtual: trash_finish
4058 * Returns: %TRUE on successful trash, %FALSE otherwise.
4062 g_file_trash_finish (GFile *file,
4063 GAsyncResult *result,
4068 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4069 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4071 iface = G_FILE_GET_IFACE (file);
4072 return (* iface->trash_finish) (file, result, error);
4076 * g_file_set_display_name:
4077 * @file: input #GFile
4078 * @display_name: a string
4079 * @cancellable: (allow-none): optional #GCancellable object,
4081 * @error: a #GError, or %NULL
4083 * Renames @file to the specified display name.
4085 * The display name is converted from UTF-8 to the correct encoding
4086 * for the target filesystem if possible and the @file is renamed to this.
4088 * If you want to implement a rename operation in the user interface the
4089 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4090 * initial value in the rename widget, and then the result after editing
4091 * should be passed to g_file_set_display_name().
4093 * On success the resulting converted filename is returned.
4095 * If @cancellable is not %NULL, then the operation can be cancelled by
4096 * triggering the cancellable object from another thread. If the operation
4097 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4099 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4100 * or %NULL if there was an error.
4101 * Free the returned object with g_object_unref().
4104 g_file_set_display_name (GFile *file,
4105 const gchar *display_name,
4106 GCancellable *cancellable,
4111 g_return_val_if_fail (G_IS_FILE (file), NULL);
4112 g_return_val_if_fail (display_name != NULL, NULL);
4114 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4118 G_IO_ERROR_INVALID_ARGUMENT,
4119 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4123 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4126 iface = G_FILE_GET_IFACE (file);
4128 return (* iface->set_display_name) (file, display_name, cancellable, error);
4132 * g_file_set_display_name_async:
4133 * @file: input #GFile
4134 * @display_name: a string
4135 * @io_priority: the [I/O priority][io-priority] of the request
4136 * @cancellable: (allow-none): optional #GCancellable object,
4138 * @callback: (scope async): a #GAsyncReadyCallback to call
4139 * when the request is satisfied
4140 * @user_data: (closure): the data to pass to callback function
4142 * Asynchronously sets the display name for a given #GFile.
4144 * For more details, see g_file_set_display_name() which is
4145 * the synchronous version of this call.
4147 * When the operation is finished, @callback will be called.
4148 * You can then call g_file_set_display_name_finish() to get
4149 * the result of the operation.
4152 g_file_set_display_name_async (GFile *file,
4153 const gchar *display_name,
4155 GCancellable *cancellable,
4156 GAsyncReadyCallback callback,
4161 g_return_if_fail (G_IS_FILE (file));
4162 g_return_if_fail (display_name != NULL);
4164 iface = G_FILE_GET_IFACE (file);
4165 (* iface->set_display_name_async) (file,
4174 * g_file_set_display_name_finish:
4175 * @file: input #GFile
4176 * @res: a #GAsyncResult
4177 * @error: a #GError, or %NULL
4179 * Finishes setting a display name started with
4180 * g_file_set_display_name_async().
4182 * Returns: (transfer full): a #GFile or %NULL on error.
4183 * Free the returned object with g_object_unref().
4186 g_file_set_display_name_finish (GFile *file,
4192 g_return_val_if_fail (G_IS_FILE (file), NULL);
4193 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4195 if (g_async_result_legacy_propagate_error (res, error))
4198 iface = G_FILE_GET_IFACE (file);
4199 return (* iface->set_display_name_finish) (file, res, error);
4203 * g_file_query_settable_attributes:
4204 * @file: input #GFile
4205 * @cancellable: (allow-none): optional #GCancellable object,
4207 * @error: a #GError, or %NULL
4209 * Obtain the list of settable attributes for the file.
4211 * Returns the type and full attribute name of all the attributes
4212 * that can be set on this file. This doesn't mean setting it will
4213 * always succeed though, you might get an access failure, or some
4214 * specific file may not support a specific attribute.
4216 * If @cancellable is not %NULL, then the operation can be cancelled by
4217 * triggering the cancellable object from another thread. If the operation
4218 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4220 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4221 * When you are done with it, release it with
4222 * g_file_attribute_info_list_unref()
4224 GFileAttributeInfoList *
4225 g_file_query_settable_attributes (GFile *file,
4226 GCancellable *cancellable,
4231 GFileAttributeInfoList *list;
4233 g_return_val_if_fail (G_IS_FILE (file), NULL);
4235 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4238 iface = G_FILE_GET_IFACE (file);
4240 if (iface->query_settable_attributes == NULL)
4241 return g_file_attribute_info_list_new ();
4244 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4248 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4250 list = g_file_attribute_info_list_new ();
4251 g_error_free (my_error);
4254 g_propagate_error (error, my_error);
4261 * g_file_query_writable_namespaces:
4262 * @file: input #GFile
4263 * @cancellable: (allow-none): optional #GCancellable object,
4265 * @error: a #GError, or %NULL
4267 * Obtain the list of attribute namespaces where new attributes
4268 * can be created by a user. An example of this is extended
4269 * attributes (in the "xattr" namespace).
4271 * If @cancellable is not %NULL, then the operation can be cancelled by
4272 * triggering the cancellable object from another thread. If the operation
4273 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4275 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4276 * When you are done with it, release it with
4277 * g_file_attribute_info_list_unref()
4279 GFileAttributeInfoList *
4280 g_file_query_writable_namespaces (GFile *file,
4281 GCancellable *cancellable,
4286 GFileAttributeInfoList *list;
4288 g_return_val_if_fail (G_IS_FILE (file), NULL);
4290 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4293 iface = G_FILE_GET_IFACE (file);
4295 if (iface->query_writable_namespaces == NULL)
4296 return g_file_attribute_info_list_new ();
4299 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4303 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4305 list = g_file_attribute_info_list_new ();
4306 g_error_free (my_error);
4309 g_propagate_error (error, my_error);
4316 * g_file_set_attribute:
4317 * @file: input #GFile
4318 * @attribute: a string containing the attribute's name
4319 * @type: The type of the attribute
4320 * @value_p: (allow-none): a pointer to the value (or the pointer
4321 * itself if the type is a pointer type)
4322 * @flags: a set of #GFileQueryInfoFlags
4323 * @cancellable: (allow-none): optional #GCancellable object,
4325 * @error: a #GError, or %NULL
4327 * Sets an attribute in the file with attribute name @attribute to @value.
4329 * Some attributes can be unset by setting @attribute to
4330 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4332 * If @cancellable is not %NULL, then the operation can be cancelled by
4333 * triggering the cancellable object from another thread. If the operation
4334 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4336 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4339 g_file_set_attribute (GFile *file,
4340 const gchar *attribute,
4341 GFileAttributeType type,
4343 GFileQueryInfoFlags flags,
4344 GCancellable *cancellable,
4349 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4350 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4352 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4355 iface = G_FILE_GET_IFACE (file);
4357 if (iface->set_attribute == NULL)
4359 g_set_error_literal (error, G_IO_ERROR,
4360 G_IO_ERROR_NOT_SUPPORTED,
4361 _("Operation not supported"));
4365 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4369 * g_file_set_attributes_from_info:
4370 * @file: input #GFile
4371 * @info: a #GFileInfo
4372 * @flags: #GFileQueryInfoFlags
4373 * @cancellable: (allow-none): optional #GCancellable object,
4375 * @error: a #GError, or %NULL
4377 * Tries to set all attributes in the #GFileInfo on the target
4378 * values, not stopping on the first error.
4380 * If there is any error during this operation then @error will
4381 * be set to the first error. Error on particular fields are flagged
4382 * by setting the "status" field in the attribute value to
4383 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4384 * also detect further errors.
4386 * If @cancellable is not %NULL, then the operation can be cancelled by
4387 * triggering the cancellable object from another thread. If the operation
4388 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4390 * Returns: %FALSE if there was any error, %TRUE otherwise.
4393 g_file_set_attributes_from_info (GFile *file,
4395 GFileQueryInfoFlags flags,
4396 GCancellable *cancellable,
4401 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4402 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4404 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4407 g_file_info_clear_status (info);
4409 iface = G_FILE_GET_IFACE (file);
4411 return (* iface->set_attributes_from_info) (file,
4419 g_file_real_set_attributes_from_info (GFile *file,
4421 GFileQueryInfoFlags flags,
4422 GCancellable *cancellable,
4428 GFileAttributeValue *value;
4432 attributes = g_file_info_list_attributes (info, NULL);
4434 for (i = 0; attributes[i] != NULL; i++)
4436 value = _g_file_info_get_attribute_value (info, attributes[i]);
4438 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4441 if (!g_file_set_attribute (file, attributes[i],
4442 value->type, _g_file_attribute_value_peek_as_pointer (value),
4443 flags, cancellable, error))
4445 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4447 /* Don't set error multiple times */
4451 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4454 g_strfreev (attributes);
4460 * g_file_set_attributes_async:
4461 * @file: input #GFile
4462 * @info: a #GFileInfo
4463 * @flags: a #GFileQueryInfoFlags
4464 * @io_priority: the [I/O priority][io-priority] of the request
4465 * @cancellable: (allow-none): optional #GCancellable object,
4467 * @callback: (scope async): a #GAsyncReadyCallback
4468 * @user_data: (closure): a #gpointer
4470 * Asynchronously sets the attributes of @file with @info.
4472 * For more details, see g_file_set_attributes_from_info(),
4473 * which is the synchronous version of this call.
4475 * When the operation is finished, @callback will be called.
4476 * You can then call g_file_set_attributes_finish() to get
4477 * the result of the operation.
4480 g_file_set_attributes_async (GFile *file,
4482 GFileQueryInfoFlags flags,
4484 GCancellable *cancellable,
4485 GAsyncReadyCallback callback,
4490 g_return_if_fail (G_IS_FILE (file));
4491 g_return_if_fail (G_IS_FILE_INFO (info));
4493 iface = G_FILE_GET_IFACE (file);
4494 (* iface->set_attributes_async) (file,
4504 * g_file_set_attributes_finish:
4505 * @file: input #GFile
4506 * @result: a #GAsyncResult
4507 * @info: (out) (transfer full): a #GFileInfo
4508 * @error: a #GError, or %NULL
4510 * Finishes setting an attribute started in g_file_set_attributes_async().
4512 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4515 g_file_set_attributes_finish (GFile *file,
4516 GAsyncResult *result,
4522 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4523 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4525 /* No standard handling of errors here, as we must set info even
4528 iface = G_FILE_GET_IFACE (file);
4529 return (* iface->set_attributes_finish) (file, result, info, error);
4533 * g_file_set_attribute_string:
4534 * @file: input #GFile
4535 * @attribute: a string containing the attribute's name
4536 * @value: a string containing the attribute's value
4537 * @flags: #GFileQueryInfoFlags
4538 * @cancellable: (allow-none): optional #GCancellable object,
4540 * @error: a #GError, or %NULL
4542 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4543 * If @attribute is of a different type, this operation will fail.
4545 * If @cancellable is not %NULL, then the operation can be cancelled by
4546 * triggering the cancellable object from another thread. If the operation
4547 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4549 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4552 g_file_set_attribute_string (GFile *file,
4553 const char *attribute,
4555 GFileQueryInfoFlags flags,
4556 GCancellable *cancellable,
4559 return g_file_set_attribute (file, attribute,
4560 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4561 flags, cancellable, error);
4565 * g_file_set_attribute_byte_string:
4566 * @file: input #GFile
4567 * @attribute: a string containing the attribute's name
4568 * @value: a string containing the attribute's new value
4569 * @flags: a #GFileQueryInfoFlags
4570 * @cancellable: (allow-none): optional #GCancellable object,
4572 * @error: a #GError, or %NULL
4574 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4575 * If @attribute is of a different type, this operation will fail,
4578 * If @cancellable is not %NULL, then the operation can be cancelled by
4579 * triggering the cancellable object from another thread. If the operation
4580 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4582 * Returns: %TRUE if the @attribute was successfully set to @value
4583 * in the @file, %FALSE otherwise.
4586 g_file_set_attribute_byte_string (GFile *file,
4587 const gchar *attribute,
4589 GFileQueryInfoFlags flags,
4590 GCancellable *cancellable,
4593 return g_file_set_attribute (file, attribute,
4594 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4595 flags, cancellable, error);
4599 * g_file_set_attribute_uint32:
4600 * @file: input #GFile
4601 * @attribute: a string containing the attribute's name
4602 * @value: a #guint32 containing the attribute's new value
4603 * @flags: a #GFileQueryInfoFlags
4604 * @cancellable: (allow-none): optional #GCancellable object,
4606 * @error: a #GError, or %NULL
4608 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4609 * If @attribute is of a different type, this operation will fail.
4611 * If @cancellable is not %NULL, then the operation can be cancelled by
4612 * triggering the cancellable object from another thread. If the operation
4613 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4615 * Returns: %TRUE if the @attribute was successfully set to @value
4616 * in the @file, %FALSE otherwise.
4619 g_file_set_attribute_uint32 (GFile *file,
4620 const gchar *attribute,
4622 GFileQueryInfoFlags flags,
4623 GCancellable *cancellable,
4626 return g_file_set_attribute (file, attribute,
4627 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4628 flags, cancellable, error);
4632 * g_file_set_attribute_int32:
4633 * @file: input #GFile
4634 * @attribute: a string containing the attribute's name
4635 * @value: a #gint32 containing the attribute's new value
4636 * @flags: a #GFileQueryInfoFlags
4637 * @cancellable: (allow-none): optional #GCancellable object,
4639 * @error: a #GError, or %NULL
4641 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4642 * If @attribute is of a different type, this operation will fail.
4644 * If @cancellable is not %NULL, then the operation can be cancelled by
4645 * triggering the cancellable object from another thread. If the operation
4646 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4648 * Returns: %TRUE if the @attribute was successfully set to @value
4649 * in the @file, %FALSE otherwise.
4652 g_file_set_attribute_int32 (GFile *file,
4653 const gchar *attribute,
4655 GFileQueryInfoFlags flags,
4656 GCancellable *cancellable,
4659 return g_file_set_attribute (file, attribute,
4660 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4661 flags, cancellable, error);
4665 * g_file_set_attribute_uint64:
4666 * @file: input #GFile
4667 * @attribute: a string containing the attribute's name
4668 * @value: a #guint64 containing the attribute's new value
4669 * @flags: a #GFileQueryInfoFlags
4670 * @cancellable: (allow-none): optional #GCancellable object,
4672 * @error: a #GError, or %NULL
4674 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4675 * If @attribute is of a different type, this operation will fail.
4677 * If @cancellable is not %NULL, then the operation can be cancelled by
4678 * triggering the cancellable object from another thread. If the operation
4679 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4681 * Returns: %TRUE if the @attribute was successfully set to @value
4682 * in the @file, %FALSE otherwise.
4685 g_file_set_attribute_uint64 (GFile *file,
4686 const gchar *attribute,
4688 GFileQueryInfoFlags flags,
4689 GCancellable *cancellable,
4692 return g_file_set_attribute (file, attribute,
4693 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4694 flags, cancellable, error);
4698 * g_file_set_attribute_int64:
4699 * @file: input #GFile
4700 * @attribute: a string containing the attribute's name
4701 * @value: a #guint64 containing the attribute's new value
4702 * @flags: a #GFileQueryInfoFlags
4703 * @cancellable: (allow-none): optional #GCancellable object,
4705 * @error: a #GError, or %NULL
4707 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4708 * If @attribute is of a different type, this operation will fail.
4710 * If @cancellable is not %NULL, then the operation can be cancelled by
4711 * triggering the cancellable object from another thread. If the operation
4712 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4714 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4717 g_file_set_attribute_int64 (GFile *file,
4718 const gchar *attribute,
4720 GFileQueryInfoFlags flags,
4721 GCancellable *cancellable,
4724 return g_file_set_attribute (file, attribute,
4725 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4726 flags, cancellable, error);
4730 * g_file_mount_mountable:
4731 * @file: input #GFile
4732 * @flags: flags affecting the operation
4733 * @mount_operation: (allow-none): a #GMountOperation,
4734 * or %NULL to avoid user interaction
4735 * @cancellable: (allow-none): optional #GCancellable object,
4737 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4738 * when the request is satisfied, or %NULL
4739 * @user_data: (closure): the data to pass to callback function
4741 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4742 * Using @mount_operation, you can request callbacks when, for instance,
4743 * passwords are needed during authentication.
4745 * If @cancellable is not %NULL, then the operation can be cancelled by
4746 * triggering the cancellable object from another thread. If the operation
4747 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4749 * When the operation is finished, @callback will be called.
4750 * You can then call g_file_mount_mountable_finish() to get
4751 * the result of the operation.
4754 g_file_mount_mountable (GFile *file,
4755 GMountMountFlags flags,
4756 GMountOperation *mount_operation,
4757 GCancellable *cancellable,
4758 GAsyncReadyCallback callback,
4763 g_return_if_fail (G_IS_FILE (file));
4765 iface = G_FILE_GET_IFACE (file);
4767 if (iface->mount_mountable == NULL)
4769 g_task_report_new_error (file, callback, user_data,
4770 g_file_mount_mountable,
4771 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4772 _("Operation not supported"));
4776 (* iface->mount_mountable) (file,
4785 * g_file_mount_mountable_finish:
4786 * @file: input #GFile
4787 * @result: a #GAsyncResult
4788 * @error: a #GError, or %NULL
4790 * Finishes a mount operation. See g_file_mount_mountable() for details.
4792 * Finish an asynchronous mount operation that was started
4793 * with g_file_mount_mountable().
4795 * Returns: (transfer full): a #GFile or %NULL on error.
4796 * Free the returned object with g_object_unref().
4799 g_file_mount_mountable_finish (GFile *file,
4800 GAsyncResult *result,
4805 g_return_val_if_fail (G_IS_FILE (file), NULL);
4806 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4808 if (g_async_result_legacy_propagate_error (result, error))
4810 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4811 return g_task_propagate_pointer (G_TASK (result), error);
4813 iface = G_FILE_GET_IFACE (file);
4814 return (* iface->mount_mountable_finish) (file, result, error);
4818 * g_file_unmount_mountable:
4819 * @file: input #GFile
4820 * @flags: flags affecting the operation
4821 * @cancellable: (allow-none): optional #GCancellable object,
4823 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4824 * when the request is satisfied, or %NULL
4825 * @user_data: (closure): the data to pass to callback function
4827 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4829 * If @cancellable is not %NULL, then the operation can be cancelled by
4830 * triggering the cancellable object from another thread. If the operation
4831 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4833 * When the operation is finished, @callback will be called.
4834 * You can then call g_file_unmount_mountable_finish() to get
4835 * the result of the operation.
4837 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4840 g_file_unmount_mountable (GFile *file,
4841 GMountUnmountFlags flags,
4842 GCancellable *cancellable,
4843 GAsyncReadyCallback callback,
4848 g_return_if_fail (G_IS_FILE (file));
4850 iface = G_FILE_GET_IFACE (file);
4852 if (iface->unmount_mountable == NULL)
4854 g_task_report_new_error (file, callback, user_data,
4855 g_file_unmount_mountable_with_operation,
4856 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4857 _("Operation not supported"));
4861 (* iface->unmount_mountable) (file,
4869 * g_file_unmount_mountable_finish:
4870 * @file: input #GFile
4871 * @result: a #GAsyncResult
4872 * @error: a #GError, or %NULL
4874 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4876 * Finish an asynchronous unmount operation that was started
4877 * with g_file_unmount_mountable().
4879 * Returns: %TRUE if the operation finished successfully.
4882 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4886 g_file_unmount_mountable_finish (GFile *file,
4887 GAsyncResult *result,
4892 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4893 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4895 if (g_async_result_legacy_propagate_error (result, error))
4897 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4898 return g_task_propagate_boolean (G_TASK (result), error);
4900 iface = G_FILE_GET_IFACE (file);
4901 return (* iface->unmount_mountable_finish) (file, result, error);
4905 * g_file_unmount_mountable_with_operation:
4906 * @file: input #GFile
4907 * @flags: flags affecting the operation
4908 * @mount_operation: (allow-none): a #GMountOperation,
4909 * or %NULL to avoid user interaction
4910 * @cancellable: (allow-none): optional #GCancellable object,
4912 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4913 * when the request is satisfied, or %NULL
4914 * @user_data: (closure): the data to pass to callback function
4916 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4918 * If @cancellable is not %NULL, then the operation can be cancelled by
4919 * triggering the cancellable object from another thread. If the operation
4920 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4922 * When the operation is finished, @callback will be called.
4923 * You can then call g_file_unmount_mountable_finish() to get
4924 * the result of the operation.
4929 g_file_unmount_mountable_with_operation (GFile *file,
4930 GMountUnmountFlags flags,
4931 GMountOperation *mount_operation,
4932 GCancellable *cancellable,
4933 GAsyncReadyCallback callback,
4938 g_return_if_fail (G_IS_FILE (file));
4940 iface = G_FILE_GET_IFACE (file);
4942 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4944 g_task_report_new_error (file, callback, user_data,
4945 g_file_unmount_mountable_with_operation,
4946 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4947 _("Operation not supported"));
4951 if (iface->unmount_mountable_with_operation != NULL)
4952 (* iface->unmount_mountable_with_operation) (file,
4959 (* iface->unmount_mountable) (file,
4967 * g_file_unmount_mountable_with_operation_finish:
4968 * @file: input #GFile
4969 * @result: a #GAsyncResult
4970 * @error: a #GError, or %NULL
4972 * Finishes an unmount operation,
4973 * see g_file_unmount_mountable_with_operation() for details.
4975 * Finish an asynchronous unmount operation that was started
4976 * with g_file_unmount_mountable_with_operation().
4978 * Returns: %TRUE if the operation finished successfully.
4984 g_file_unmount_mountable_with_operation_finish (GFile *file,
4985 GAsyncResult *result,
4990 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4991 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4993 if (g_async_result_legacy_propagate_error (result, error))
4995 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4996 return g_task_propagate_boolean (G_TASK (result), error);
4998 iface = G_FILE_GET_IFACE (file);
4999 if (iface->unmount_mountable_with_operation_finish != NULL)
5000 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5002 return (* iface->unmount_mountable_finish) (file, result, error);
5006 * g_file_eject_mountable:
5007 * @file: input #GFile
5008 * @flags: flags affecting the operation
5009 * @cancellable: (allow-none): optional #GCancellable object,
5011 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5012 * when the request is satisfied, or %NULL
5013 * @user_data: (closure): the data to pass to callback function
5015 * Starts an asynchronous eject on a mountable.
5016 * When this operation has completed, @callback will be called with
5017 * @user_user data, and the operation can be finalized with
5018 * g_file_eject_mountable_finish().
5020 * If @cancellable is not %NULL, then the operation can be cancelled by
5021 * triggering the cancellable object from another thread. If the operation
5022 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5024 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5027 g_file_eject_mountable (GFile *file,
5028 GMountUnmountFlags flags,
5029 GCancellable *cancellable,
5030 GAsyncReadyCallback callback,
5035 g_return_if_fail (G_IS_FILE (file));
5037 iface = G_FILE_GET_IFACE (file);
5039 if (iface->eject_mountable == NULL)
5041 g_task_report_new_error (file, callback, user_data,
5042 g_file_eject_mountable_with_operation,
5043 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5044 _("Operation not supported"));
5048 (* iface->eject_mountable) (file,
5056 * g_file_eject_mountable_finish:
5057 * @file: input #GFile
5058 * @result: a #GAsyncResult
5059 * @error: a #GError, or %NULL
5061 * Finishes an asynchronous eject operation started by
5062 * g_file_eject_mountable().
5064 * Returns: %TRUE if the @file was ejected successfully.
5067 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5071 g_file_eject_mountable_finish (GFile *file,
5072 GAsyncResult *result,
5077 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5078 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5080 if (g_async_result_legacy_propagate_error (result, error))
5082 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5083 return g_task_propagate_boolean (G_TASK (result), error);
5085 iface = G_FILE_GET_IFACE (file);
5086 return (* iface->eject_mountable_finish) (file, result, error);
5090 * g_file_eject_mountable_with_operation:
5091 * @file: input #GFile
5092 * @flags: flags affecting the operation
5093 * @mount_operation: (allow-none): a #GMountOperation,
5094 * or %NULL to avoid user interaction
5095 * @cancellable: (allow-none): optional #GCancellable object,
5097 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5098 * when the request is satisfied, or %NULL
5099 * @user_data: (closure): the data to pass to callback function
5101 * Starts an asynchronous eject on a mountable.
5102 * When this operation has completed, @callback will be called with
5103 * @user_user data, and the operation can be finalized with
5104 * g_file_eject_mountable_with_operation_finish().
5106 * If @cancellable is not %NULL, then the operation can be cancelled by
5107 * triggering the cancellable object from another thread. If the operation
5108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5113 g_file_eject_mountable_with_operation (GFile *file,
5114 GMountUnmountFlags flags,
5115 GMountOperation *mount_operation,
5116 GCancellable *cancellable,
5117 GAsyncReadyCallback callback,
5122 g_return_if_fail (G_IS_FILE (file));
5124 iface = G_FILE_GET_IFACE (file);
5126 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5128 g_task_report_new_error (file, callback, user_data,
5129 g_file_eject_mountable_with_operation,
5130 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5131 _("Operation not supported"));
5135 if (iface->eject_mountable_with_operation != NULL)
5136 (* iface->eject_mountable_with_operation) (file,
5143 (* iface->eject_mountable) (file,
5151 * g_file_eject_mountable_with_operation_finish:
5152 * @file: input #GFile
5153 * @result: a #GAsyncResult
5154 * @error: a #GError, or %NULL
5156 * Finishes an asynchronous eject operation started by
5157 * g_file_eject_mountable_with_operation().
5159 * Returns: %TRUE if the @file was ejected successfully.
5165 g_file_eject_mountable_with_operation_finish (GFile *file,
5166 GAsyncResult *result,
5171 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5172 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5174 if (g_async_result_legacy_propagate_error (result, error))
5176 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5177 return g_task_propagate_boolean (G_TASK (result), error);
5179 iface = G_FILE_GET_IFACE (file);
5180 if (iface->eject_mountable_with_operation_finish != NULL)
5181 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5183 return (* iface->eject_mountable_finish) (file, result, error);
5187 * g_file_monitor_directory:
5188 * @file: input #GFile
5189 * @flags: a set of #GFileMonitorFlags
5190 * @cancellable: (allow-none): optional #GCancellable object,
5192 * @error: a #GError, or %NULL
5194 * Obtains a directory monitor for the given file.
5195 * This may fail if directory monitoring is not supported.
5197 * If @cancellable is not %NULL, then the operation can be cancelled by
5198 * triggering the cancellable object from another thread. If the operation
5199 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5201 * It does not make sense for @flags to contain
5202 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5203 * directories. It is not possible to monitor all the files in a
5204 * directory for changes made via hard links; if you want to do this then
5205 * you must register individual watches with g_file_monitor().
5207 * Virtual: monitor_dir
5208 * Returns: (transfer full): a #GFileMonitor for the given @file,
5209 * or %NULL on error.
5210 * Free the returned object with g_object_unref().
5213 g_file_monitor_directory (GFile *file,
5214 GFileMonitorFlags flags,
5215 GCancellable *cancellable,
5220 g_return_val_if_fail (G_IS_FILE (file), NULL);
5221 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5223 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5226 iface = G_FILE_GET_IFACE (file);
5228 if (iface->monitor_dir == NULL)
5230 g_set_error_literal (error, G_IO_ERROR,
5231 G_IO_ERROR_NOT_SUPPORTED,
5232 _("Operation not supported"));
5236 return (* iface->monitor_dir) (file, flags, cancellable, error);
5240 * g_file_monitor_file:
5241 * @file: input #GFile
5242 * @flags: a set of #GFileMonitorFlags
5243 * @cancellable: (allow-none): optional #GCancellable object,
5245 * @error: a #GError, or %NULL
5247 * Obtains a file monitor for the given file. If no file notification
5248 * mechanism exists, then regular polling of the file is used.
5250 * If @cancellable is not %NULL, then the operation can be cancelled by
5251 * triggering the cancellable object from another thread. If the operation
5252 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5254 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5255 * will also attempt to report changes made to the file via another
5256 * filename (ie, a hard link). Without this flag, you can only rely on
5257 * changes made through the filename contained in @file to be
5258 * reported. Using this flag may result in an increase in resource
5259 * usage, and may not have any effect depending on the #GFileMonitor
5260 * backend and/or filesystem type.
5262 * Returns: (transfer full): a #GFileMonitor for the given @file,
5263 * or %NULL on error.
5264 * Free the returned object with g_object_unref().
5267 g_file_monitor_file (GFile *file,
5268 GFileMonitorFlags flags,
5269 GCancellable *cancellable,
5273 GFileMonitor *monitor;
5275 g_return_val_if_fail (G_IS_FILE (file), NULL);
5277 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5280 iface = G_FILE_GET_IFACE (file);
5284 if (iface->monitor_file)
5285 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5287 /* Fallback to polling */
5288 if (monitor == NULL)
5289 monitor = _g_poll_file_monitor_new (file);
5296 * @file: input #GFile
5297 * @flags: a set of #GFileMonitorFlags
5298 * @cancellable: (allow-none): optional #GCancellable object,
5300 * @error: a #GError, or %NULL
5302 * Obtains a file or directory monitor for the given file,
5303 * depending on the type of the file.
5305 * If @cancellable is not %NULL, then the operation can be cancelled by
5306 * triggering the cancellable object from another thread. If the operation
5307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5309 * Returns: (transfer full): a #GFileMonitor for the given @file,
5310 * or %NULL on error.
5311 * Free the returned object with g_object_unref().
5316 g_file_monitor (GFile *file,
5317 GFileMonitorFlags flags,
5318 GCancellable *cancellable,
5321 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5322 return g_file_monitor_directory (file,
5323 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5324 cancellable, error);
5326 return g_file_monitor_file (file, flags, cancellable, error);
5329 /********************************************
5330 * Default implementation of async ops *
5331 ********************************************/
5335 GFileQueryInfoFlags flags;
5336 } QueryInfoAsyncData;
5339 query_info_data_free (QueryInfoAsyncData *data)
5341 g_free (data->attributes);
5346 query_info_async_thread (GTask *task,
5349 GCancellable *cancellable)
5351 QueryInfoAsyncData *data = task_data;
5353 GError *error = NULL;
5355 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5357 g_task_return_pointer (task, info, g_object_unref);
5359 g_task_return_error (task, error);
5363 g_file_real_query_info_async (GFile *file,
5364 const char *attributes,
5365 GFileQueryInfoFlags flags,
5367 GCancellable *cancellable,
5368 GAsyncReadyCallback callback,
5372 QueryInfoAsyncData *data;
5374 data = g_new0 (QueryInfoAsyncData, 1);
5375 data->attributes = g_strdup (attributes);
5376 data->flags = flags;
5378 task = g_task_new (file, cancellable, callback, user_data);
5379 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5380 g_task_set_priority (task, io_priority);
5381 g_task_run_in_thread (task, query_info_async_thread);
5382 g_object_unref (task);
5386 g_file_real_query_info_finish (GFile *file,
5390 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5392 return g_task_propagate_pointer (G_TASK (res), error);
5396 query_filesystem_info_async_thread (GTask *task,
5399 GCancellable *cancellable)
5401 const char *attributes = task_data;
5403 GError *error = NULL;
5405 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5407 g_task_return_pointer (task, info, g_object_unref);
5409 g_task_return_error (task, error);
5413 g_file_real_query_filesystem_info_async (GFile *file,
5414 const char *attributes,
5416 GCancellable *cancellable,
5417 GAsyncReadyCallback callback,
5422 task = g_task_new (file, cancellable, callback, user_data);
5423 g_task_set_task_data (task, g_strdup (attributes), g_free);
5424 g_task_set_priority (task, io_priority);
5425 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5426 g_object_unref (task);
5430 g_file_real_query_filesystem_info_finish (GFile *file,
5434 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5436 return g_task_propagate_pointer (G_TASK (res), error);
5440 enumerate_children_async_thread (GTask *task,
5443 GCancellable *cancellable)
5445 QueryInfoAsyncData *data = task_data;
5446 GFileEnumerator *enumerator;
5447 GError *error = NULL;
5449 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5451 g_task_return_error (task, error);
5453 g_task_return_pointer (task, enumerator, g_object_unref);
5457 g_file_real_enumerate_children_async (GFile *file,
5458 const char *attributes,
5459 GFileQueryInfoFlags flags,
5461 GCancellable *cancellable,
5462 GAsyncReadyCallback callback,
5466 QueryInfoAsyncData *data;
5468 data = g_new0 (QueryInfoAsyncData, 1);
5469 data->attributes = g_strdup (attributes);
5470 data->flags = flags;
5472 task = g_task_new (file, cancellable, callback, user_data);
5473 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5474 g_task_set_priority (task, io_priority);
5475 g_task_run_in_thread (task, enumerate_children_async_thread);
5476 g_object_unref (task);
5479 static GFileEnumerator *
5480 g_file_real_enumerate_children_finish (GFile *file,
5484 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5486 return g_task_propagate_pointer (G_TASK (res), error);
5490 open_read_async_thread (GTask *task,
5493 GCancellable *cancellable)
5495 GFileInputStream *stream;
5496 GError *error = NULL;
5498 stream = g_file_read (G_FILE (object), cancellable, &error);
5500 g_task_return_pointer (task, stream, g_object_unref);
5502 g_task_return_error (task, error);
5506 g_file_real_read_async (GFile *file,
5508 GCancellable *cancellable,
5509 GAsyncReadyCallback callback,
5514 task = g_task_new (file, cancellable, callback, user_data);
5515 g_task_set_priority (task, io_priority);
5516 g_task_run_in_thread (task, open_read_async_thread);
5517 g_object_unref (task);
5520 static GFileInputStream *
5521 g_file_real_read_finish (GFile *file,
5525 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5527 return g_task_propagate_pointer (G_TASK (res), error);
5531 append_to_async_thread (GTask *task,
5532 gpointer source_object,
5534 GCancellable *cancellable)
5536 GFileCreateFlags *data = task_data;
5537 GFileOutputStream *stream;
5538 GError *error = NULL;
5540 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5542 g_task_return_pointer (task, stream, g_object_unref);
5544 g_task_return_error (task, error);
5548 g_file_real_append_to_async (GFile *file,
5549 GFileCreateFlags flags,
5551 GCancellable *cancellable,
5552 GAsyncReadyCallback callback,
5555 GFileCreateFlags *data;
5558 data = g_new0 (GFileCreateFlags, 1);
5561 task = g_task_new (file, cancellable, callback, user_data);
5562 g_task_set_task_data (task, data, g_free);
5563 g_task_set_priority (task, io_priority);
5565 g_task_run_in_thread (task, append_to_async_thread);
5566 g_object_unref (task);
5569 static GFileOutputStream *
5570 g_file_real_append_to_finish (GFile *file,
5574 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5576 return g_task_propagate_pointer (G_TASK (res), error);
5580 create_async_thread (GTask *task,
5581 gpointer source_object,
5583 GCancellable *cancellable)
5585 GFileCreateFlags *data = task_data;
5586 GFileOutputStream *stream;
5587 GError *error = NULL;
5589 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5591 g_task_return_pointer (task, stream, g_object_unref);
5593 g_task_return_error (task, error);
5597 g_file_real_create_async (GFile *file,
5598 GFileCreateFlags flags,
5600 GCancellable *cancellable,
5601 GAsyncReadyCallback callback,
5604 GFileCreateFlags *data;
5607 data = g_new0 (GFileCreateFlags, 1);
5610 task = g_task_new (file, cancellable, callback, user_data);
5611 g_task_set_task_data (task, data, g_free);
5612 g_task_set_priority (task, io_priority);
5614 g_task_run_in_thread (task, create_async_thread);
5615 g_object_unref (task);
5618 static GFileOutputStream *
5619 g_file_real_create_finish (GFile *file,
5623 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5625 return g_task_propagate_pointer (G_TASK (res), error);
5629 GFileOutputStream *stream;
5631 gboolean make_backup;
5632 GFileCreateFlags flags;
5636 replace_async_data_free (ReplaceAsyncData *data)
5639 g_object_unref (data->stream);
5640 g_free (data->etag);
5645 replace_async_thread (GTask *task,
5646 gpointer source_object,
5648 GCancellable *cancellable)
5650 GFileOutputStream *stream;
5651 ReplaceAsyncData *data = task_data;
5652 GError *error = NULL;
5654 stream = g_file_replace (G_FILE (source_object),
5662 g_task_return_pointer (task, stream, g_object_unref);
5664 g_task_return_error (task, error);
5668 g_file_real_replace_async (GFile *file,
5670 gboolean make_backup,
5671 GFileCreateFlags flags,
5673 GCancellable *cancellable,
5674 GAsyncReadyCallback callback,
5678 ReplaceAsyncData *data;
5680 data = g_new0 (ReplaceAsyncData, 1);
5681 data->etag = g_strdup (etag);
5682 data->make_backup = make_backup;
5683 data->flags = flags;
5685 task = g_task_new (file, cancellable, callback, user_data);
5686 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5687 g_task_set_priority (task, io_priority);
5689 g_task_run_in_thread (task, replace_async_thread);
5690 g_object_unref (task);
5693 static GFileOutputStream *
5694 g_file_real_replace_finish (GFile *file,
5698 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5700 return g_task_propagate_pointer (G_TASK (res), error);
5704 delete_async_thread (GTask *task,
5707 GCancellable *cancellable)
5709 GError *error = NULL;
5711 if (g_file_delete (G_FILE (object), cancellable, &error))
5712 g_task_return_boolean (task, TRUE);
5714 g_task_return_error (task, error);
5718 g_file_real_delete_async (GFile *file,
5720 GCancellable *cancellable,
5721 GAsyncReadyCallback callback,
5726 task = g_task_new (file, cancellable, callback, user_data);
5727 g_task_set_priority (task, io_priority);
5728 g_task_run_in_thread (task, delete_async_thread);
5729 g_object_unref (task);
5733 g_file_real_delete_finish (GFile *file,
5737 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5739 return g_task_propagate_boolean (G_TASK (res), error);
5743 trash_async_thread (GTask *task,
5746 GCancellable *cancellable)
5748 GError *error = NULL;
5750 if (g_file_trash (G_FILE (object), cancellable, &error))
5751 g_task_return_boolean (task, TRUE);
5753 g_task_return_error (task, error);
5757 g_file_real_trash_async (GFile *file,
5759 GCancellable *cancellable,
5760 GAsyncReadyCallback callback,
5765 task = g_task_new (file, cancellable, callback, user_data);
5766 g_task_set_priority (task, io_priority);
5767 g_task_run_in_thread (task, trash_async_thread);
5768 g_object_unref (task);
5772 g_file_real_trash_finish (GFile *file,
5776 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5778 return g_task_propagate_boolean (G_TASK (res), error);
5782 make_directory_async_thread (GTask *task,
5785 GCancellable *cancellable)
5787 GError *error = NULL;
5789 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5790 g_task_return_boolean (task, TRUE);
5792 g_task_return_error (task, error);
5796 g_file_real_make_directory_async (GFile *file,
5798 GCancellable *cancellable,
5799 GAsyncReadyCallback callback,
5804 task = g_task_new (file, cancellable, callback, user_data);
5805 g_task_set_priority (task, io_priority);
5806 g_task_run_in_thread (task, make_directory_async_thread);
5807 g_object_unref (task);
5811 g_file_real_make_directory_finish (GFile *file,
5815 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5817 return g_task_propagate_boolean (G_TASK (res), error);
5821 open_readwrite_async_thread (GTask *task,
5824 GCancellable *cancellable)
5826 GFileIOStream *stream;
5827 GError *error = NULL;
5829 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5832 g_task_return_error (task, error);
5834 g_task_return_pointer (task, stream, g_object_unref);
5838 g_file_real_open_readwrite_async (GFile *file,
5840 GCancellable *cancellable,
5841 GAsyncReadyCallback callback,
5846 task = g_task_new (file, cancellable, callback, user_data);
5847 g_task_set_priority (task, io_priority);
5849 g_task_run_in_thread (task, open_readwrite_async_thread);
5850 g_object_unref (task);
5853 static GFileIOStream *
5854 g_file_real_open_readwrite_finish (GFile *file,
5858 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5860 return g_task_propagate_pointer (G_TASK (res), error);
5864 create_readwrite_async_thread (GTask *task,
5867 GCancellable *cancellable)
5869 GFileCreateFlags *data = task_data;
5870 GFileIOStream *stream;
5871 GError *error = NULL;
5873 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5876 g_task_return_error (task, error);
5878 g_task_return_pointer (task, stream, g_object_unref);
5882 g_file_real_create_readwrite_async (GFile *file,
5883 GFileCreateFlags flags,
5885 GCancellable *cancellable,
5886 GAsyncReadyCallback callback,
5889 GFileCreateFlags *data;
5892 data = g_new0 (GFileCreateFlags, 1);
5895 task = g_task_new (file, cancellable, callback, user_data);
5896 g_task_set_task_data (task, data, g_free);
5897 g_task_set_priority (task, io_priority);
5899 g_task_run_in_thread (task, create_readwrite_async_thread);
5900 g_object_unref (task);
5903 static GFileIOStream *
5904 g_file_real_create_readwrite_finish (GFile *file,
5908 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5910 return g_task_propagate_pointer (G_TASK (res), error);
5915 gboolean make_backup;
5916 GFileCreateFlags flags;
5917 } ReplaceRWAsyncData;
5920 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5922 g_free (data->etag);
5927 replace_readwrite_async_thread (GTask *task,
5930 GCancellable *cancellable)
5932 GFileIOStream *stream;
5933 GError *error = NULL;
5934 ReplaceRWAsyncData *data = task_data;
5936 stream = g_file_replace_readwrite (G_FILE (object),
5944 g_task_return_error (task, error);
5946 g_task_return_pointer (task, stream, g_object_unref);
5950 g_file_real_replace_readwrite_async (GFile *file,
5952 gboolean make_backup,
5953 GFileCreateFlags flags,
5955 GCancellable *cancellable,
5956 GAsyncReadyCallback callback,
5960 ReplaceRWAsyncData *data;
5962 data = g_new0 (ReplaceRWAsyncData, 1);
5963 data->etag = g_strdup (etag);
5964 data->make_backup = make_backup;
5965 data->flags = flags;
5967 task = g_task_new (file, cancellable, callback, user_data);
5968 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5969 g_task_set_priority (task, io_priority);
5971 g_task_run_in_thread (task, replace_readwrite_async_thread);
5972 g_object_unref (task);
5975 static GFileIOStream *
5976 g_file_real_replace_readwrite_finish (GFile *file,
5980 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5982 return g_task_propagate_pointer (G_TASK (res), error);
5986 set_display_name_async_thread (GTask *task,
5989 GCancellable *cancellable)
5991 GError *error = NULL;
5992 char *name = task_data;
5995 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5998 g_task_return_error (task, error);
6000 g_task_return_pointer (task, file, g_object_unref);
6004 g_file_real_set_display_name_async (GFile *file,
6005 const char *display_name,
6007 GCancellable *cancellable,
6008 GAsyncReadyCallback callback,
6013 task = g_task_new (file, cancellable, callback, user_data);
6014 g_task_set_task_data (task, g_strdup (display_name), g_free);
6015 g_task_set_priority (task, io_priority);
6017 g_task_run_in_thread (task, set_display_name_async_thread);
6018 g_object_unref (task);
6022 g_file_real_set_display_name_finish (GFile *file,
6026 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6028 return g_task_propagate_pointer (G_TASK (res), error);
6032 GFileQueryInfoFlags flags;
6039 set_info_data_free (SetInfoAsyncData *data)
6042 g_object_unref (data->info);
6044 g_error_free (data->error);
6049 set_info_async_thread (GTask *task,
6052 GCancellable *cancellable)
6054 SetInfoAsyncData *data = task_data;
6057 data->res = g_file_set_attributes_from_info (G_FILE (object),
6065 g_file_real_set_attributes_async (GFile *file,
6067 GFileQueryInfoFlags flags,
6069 GCancellable *cancellable,
6070 GAsyncReadyCallback callback,
6074 SetInfoAsyncData *data;
6076 data = g_new0 (SetInfoAsyncData, 1);
6077 data->info = g_file_info_dup (info);
6078 data->flags = flags;
6080 task = g_task_new (file, cancellable, callback, user_data);
6081 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6082 g_task_set_priority (task, io_priority);
6084 g_task_run_in_thread (task, set_info_async_thread);
6085 g_object_unref (task);
6089 g_file_real_set_attributes_finish (GFile *file,
6094 SetInfoAsyncData *data;
6096 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6098 data = g_task_get_task_data (G_TASK (res));
6101 *info = g_object_ref (data->info);
6103 if (error != NULL && data->error)
6104 *error = g_error_copy (data->error);
6110 find_enclosing_mount_async_thread (GTask *task,
6113 GCancellable *cancellable)
6115 GError *error = NULL;
6118 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6121 g_task_return_error (task, error);
6123 g_task_return_pointer (task, mount, g_object_unref);
6127 g_file_real_find_enclosing_mount_async (GFile *file,
6129 GCancellable *cancellable,
6130 GAsyncReadyCallback callback,
6135 task = g_task_new (file, cancellable, callback, user_data);
6136 g_task_set_priority (task, io_priority);
6138 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6139 g_object_unref (task);
6143 g_file_real_find_enclosing_mount_finish (GFile *file,
6147 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6149 return g_task_propagate_pointer (G_TASK (res), error);
6156 GFileCopyFlags flags;
6157 GFileProgressCallback progress_cb;
6158 gpointer progress_cb_data;
6162 copy_async_data_free (CopyAsyncData *data)
6164 g_object_unref (data->source);
6165 g_object_unref (data->destination);
6166 g_slice_free (CopyAsyncData, data);
6170 CopyAsyncData *data;
6171 goffset current_num_bytes;
6172 goffset total_num_bytes;
6176 copy_async_progress_in_main (gpointer user_data)
6178 ProgressData *progress = user_data;
6179 CopyAsyncData *data = progress->data;
6181 data->progress_cb (progress->current_num_bytes,
6182 progress->total_num_bytes,
6183 data->progress_cb_data);
6189 copy_async_progress_callback (goffset current_num_bytes,
6190 goffset total_num_bytes,
6193 GTask *task = user_data;
6194 CopyAsyncData *data = g_task_get_task_data (task);
6195 ProgressData *progress;
6197 progress = g_new (ProgressData, 1);
6198 progress->data = data;
6199 progress->current_num_bytes = current_num_bytes;
6200 progress->total_num_bytes = total_num_bytes;
6202 g_main_context_invoke_full (g_task_get_context (task),
6203 g_task_get_priority (task),
6204 copy_async_progress_in_main,
6210 copy_async_thread (GTask *task,
6213 GCancellable *cancellable)
6215 CopyAsyncData *data = task_data;
6217 GError *error = NULL;
6219 result = g_file_copy (data->source,
6223 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6227 g_task_return_boolean (task, TRUE);
6229 g_task_return_error (task, error);
6233 g_file_real_copy_async (GFile *source,
6235 GFileCopyFlags flags,
6237 GCancellable *cancellable,
6238 GFileProgressCallback progress_callback,
6239 gpointer progress_callback_data,
6240 GAsyncReadyCallback callback,
6244 CopyAsyncData *data;
6246 data = g_slice_new (CopyAsyncData);
6247 data->source = g_object_ref (source);
6248 data->destination = g_object_ref (destination);
6249 data->flags = flags;
6250 data->progress_cb = progress_callback;
6251 data->progress_cb_data = progress_callback_data;
6253 task = g_task_new (source, cancellable, callback, user_data);
6254 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6255 g_task_set_priority (task, io_priority);
6256 g_task_run_in_thread (task, copy_async_thread);
6257 g_object_unref (task);
6261 g_file_real_copy_finish (GFile *file,
6265 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6267 return g_task_propagate_boolean (G_TASK (res), error);
6271 /********************************************
6272 * Default VFS operations *
6273 ********************************************/
6276 * g_file_new_for_path:
6277 * @path: a string containing a relative or absolute path.
6278 * The string must be encoded in the glib filename encoding.
6280 * Constructs a #GFile for a given path. This operation never
6281 * fails, but the returned object might not support any I/O
6282 * operation if @path is malformed.
6284 * Returns: (transfer full): a new #GFile for the given @path.
6285 * Free the returned object with g_object_unref().
6288 g_file_new_for_path (const char *path)
6290 g_return_val_if_fail (path != NULL, NULL);
6292 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6296 * g_file_new_for_uri:
6297 * @uri: a UTF-8 string containing a URI
6299 * Constructs a #GFile for a given URI. This operation never
6300 * fails, but the returned object might not support any I/O
6301 * operation if @uri is malformed or if the uri type is
6304 * Returns: (transfer full): a new #GFile for the given @uri.
6305 * Free the returned object with g_object_unref().
6308 g_file_new_for_uri (const char *uri)
6310 g_return_val_if_fail (uri != NULL, NULL);
6312 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6317 * @tmpl: (type filename) (allow-none): Template for the file
6318 * name, as in g_file_open_tmp(), or %NULL for a default template
6319 * @iostream: (out): on return, a #GFileIOStream for the created file
6320 * @error: a #GError, or %NULL
6322 * Opens a file in the preferred directory for temporary files (as
6323 * returned by g_get_tmp_dir()) and returns a #GFile and
6324 * #GFileIOStream pointing to it.
6326 * @tmpl should be a string in the GLib file name encoding
6327 * containing a sequence of six 'X' characters, and containing no
6328 * directory components. If it is %NULL, a default template is used.
6330 * Unlike the other #GFile constructors, this will return %NULL if
6331 * a temporary file could not be created.
6333 * Returns: (transfer full): a new #GFile.
6334 * Free the returned object with g_object_unref().
6339 g_file_new_tmp (const char *tmpl,
6340 GFileIOStream **iostream,
6346 GFileOutputStream *output;
6348 g_return_val_if_fail (iostream != NULL, NULL);
6350 fd = g_file_open_tmp (tmpl, &path, error);
6354 file = g_file_new_for_path (path);
6356 output = _g_local_file_output_stream_new (fd);
6357 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6359 g_object_unref (output);
6366 * g_file_parse_name:
6367 * @parse_name: a file name or path to be parsed
6369 * Constructs a #GFile with the given @parse_name (i.e. something
6370 * given by g_file_get_parse_name()). This operation never fails,
6371 * but the returned object might not support any I/O operation if
6372 * the @parse_name cannot be parsed.
6374 * Returns: (transfer full): a new #GFile.
6377 g_file_parse_name (const char *parse_name)
6379 g_return_val_if_fail (parse_name != NULL, NULL);
6381 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6385 is_valid_scheme_character (char c)
6387 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6390 /* Following RFC 2396, valid schemes are built like:
6391 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6394 has_valid_scheme (const char *uri)
6400 if (!g_ascii_isalpha (*p))
6405 } while (is_valid_scheme_character (*p));
6411 new_for_cmdline_arg (const gchar *arg,
6417 if (g_path_is_absolute (arg))
6418 return g_file_new_for_path (arg);
6420 if (has_valid_scheme (arg))
6421 return g_file_new_for_uri (arg);
6427 current_dir = g_get_current_dir ();
6428 filename = g_build_filename (current_dir, arg, NULL);
6429 g_free (current_dir);
6432 filename = g_build_filename (cwd, arg, NULL);
6434 file = g_file_new_for_path (filename);
6441 * g_file_new_for_commandline_arg:
6442 * @arg: a command line string
6444 * Creates a #GFile with the given argument from the command line.
6445 * The value of @arg can be either a URI, an absolute path or a
6446 * relative path resolved relative to the current working directory.
6447 * This operation never fails, but the returned object might not
6448 * support any I/O operation if @arg points to a malformed path.
6450 * Note that on Windows, this function expects its argument to be in
6451 * UTF-8 -- not the system code page. This means that you
6452 * should not use this function with string from argv as it is passed
6453 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6454 * the commandline. #GApplication also uses UTF-8 but
6455 * g_application_command_line_create_file_for_arg() may be more useful
6456 * for you there. It is also always possible to use this function with
6457 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6459 * Returns: (transfer full): a new #GFile.
6460 * Free the returned object with g_object_unref().
6463 g_file_new_for_commandline_arg (const char *arg)
6465 g_return_val_if_fail (arg != NULL, NULL);
6467 return new_for_cmdline_arg (arg, NULL);
6471 * g_file_new_for_commandline_arg_and_cwd:
6472 * @arg: a command line string
6473 * @cwd: the current working directory of the commandline
6475 * Creates a #GFile with the given argument from the command line.
6477 * This function is similar to g_file_new_for_commandline_arg() except
6478 * that it allows for passing the current working directory as an
6479 * argument instead of using the current working directory of the
6482 * This is useful if the commandline argument was given in a context
6483 * other than the invocation of the current process.
6485 * See also g_application_command_line_create_file_for_arg().
6487 * Returns: (transfer full): a new #GFile
6492 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6495 g_return_val_if_fail (arg != NULL, NULL);
6496 g_return_val_if_fail (cwd != NULL, NULL);
6498 return new_for_cmdline_arg (arg, cwd);
6502 * g_file_mount_enclosing_volume:
6503 * @location: input #GFile
6504 * @flags: flags affecting the operation
6505 * @mount_operation: (allow-none): a #GMountOperation
6506 * or %NULL to avoid user interaction
6507 * @cancellable: (allow-none): optional #GCancellable object,
6509 * @callback: (allow-none): a #GAsyncReadyCallback to call
6510 * when the request is satisfied, or %NULL
6511 * @user_data: the data to pass to callback function
6513 * Starts a @mount_operation, mounting the volume that contains
6514 * the file @location.
6516 * When this operation has completed, @callback will be called with
6517 * @user_user data, and the operation can be finalized with
6518 * g_file_mount_enclosing_volume_finish().
6520 * If @cancellable is not %NULL, then the operation can be cancelled by
6521 * triggering the cancellable object from another thread. If the operation
6522 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6525 g_file_mount_enclosing_volume (GFile *location,
6526 GMountMountFlags flags,
6527 GMountOperation *mount_operation,
6528 GCancellable *cancellable,
6529 GAsyncReadyCallback callback,
6534 g_return_if_fail (G_IS_FILE (location));
6536 iface = G_FILE_GET_IFACE (location);
6538 if (iface->mount_enclosing_volume == NULL)
6540 g_task_report_new_error (location, callback, user_data,
6541 g_file_mount_enclosing_volume,
6542 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6543 _("volume doesn't implement mount"));
6547 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6552 * g_file_mount_enclosing_volume_finish:
6553 * @location: input #GFile
6554 * @result: a #GAsyncResult
6555 * @error: a #GError, or %NULL
6557 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6559 * Returns: %TRUE if successful. If an error has occurred,
6560 * this function will return %FALSE and set @error
6561 * appropriately if present.
6564 g_file_mount_enclosing_volume_finish (GFile *location,
6565 GAsyncResult *result,
6570 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6571 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6573 if (g_async_result_legacy_propagate_error (result, error))
6575 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6576 return g_task_propagate_boolean (G_TASK (result), error);
6578 iface = G_FILE_GET_IFACE (location);
6580 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6583 /********************************************
6584 * Utility functions *
6585 ********************************************/
6588 * g_file_query_default_handler:
6589 * @file: a #GFile to open
6590 * @cancellable: optional #GCancellable object, %NULL to ignore
6591 * @error: a #GError, or %NULL
6593 * Returns the #GAppInfo that is registered as the default
6594 * application to handle the file specified by @file.
6596 * If @cancellable is not %NULL, then the operation can be cancelled by
6597 * triggering the cancellable object from another thread. If the operation
6598 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6600 * Returns: (transfer full): a #GAppInfo if the handle was found,
6601 * %NULL if there were errors.
6602 * When you are done with it, release it with g_object_unref()
6605 g_file_query_default_handler (GFile *file,
6606 GCancellable *cancellable,
6610 const char *content_type;
6615 uri_scheme = g_file_get_uri_scheme (file);
6616 if (uri_scheme && uri_scheme[0] != '\0')
6618 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6619 g_free (uri_scheme);
6621 if (appinfo != NULL)
6625 info = g_file_query_info (file,
6626 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6635 content_type = g_file_info_get_content_type (info);
6638 /* Don't use is_native(), as we want to support fuse paths if available */
6639 path = g_file_get_path (file);
6640 appinfo = g_app_info_get_default_for_type (content_type,
6645 g_object_unref (info);
6647 if (appinfo != NULL)
6650 g_set_error_literal (error, G_IO_ERROR,
6651 G_IO_ERROR_NOT_SUPPORTED,
6652 _("No application is registered as handling this file"));
6656 #define GET_CONTENT_BLOCK_SIZE 8192
6659 * g_file_load_contents:
6660 * @file: input #GFile
6661 * @cancellable: optional #GCancellable object, %NULL to ignore
6662 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6663 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6664 * or %NULL if the length is not needed
6665 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6666 * or %NULL if the entity tag is not needed
6667 * @error: a #GError, or %NULL
6669 * Loads the content of the file into memory. The data is always
6670 * zero-terminated, but this is not included in the resultant @length.
6671 * The returned @content should be freed with g_free() when no longer
6674 * If @cancellable is not %NULL, then the operation can be cancelled by
6675 * triggering the cancellable object from another thread. If the operation
6676 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6678 * Returns: %TRUE if the @file's contents were successfully loaded.
6679 * %FALSE if there were errors.
6682 g_file_load_contents (GFile *file,
6683 GCancellable *cancellable,
6689 GFileInputStream *in;
6690 GByteArray *content;
6695 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6696 g_return_val_if_fail (contents != NULL, FALSE);
6698 in = g_file_read (file, cancellable, error);
6702 content = g_byte_array_new ();
6705 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6706 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6707 content->data + pos,
6708 GET_CONTENT_BLOCK_SIZE,
6709 cancellable, error)) > 0)
6712 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6719 info = g_file_input_stream_query_info (in,
6720 G_FILE_ATTRIBUTE_ETAG_VALUE,
6725 *etag_out = g_strdup (g_file_info_get_etag (info));
6726 g_object_unref (info);
6730 /* Ignore errors on close */
6731 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6732 g_object_unref (in);
6736 /* error is set already */
6737 g_byte_array_free (content, TRUE);
6744 /* Zero terminate (we got an extra byte allocated for this */
6745 content->data[pos] = 0;
6747 *contents = (char *)g_byte_array_free (content, FALSE);
6754 GFileReadMoreCallback read_more_callback;
6755 GByteArray *content;
6762 load_contents_data_free (LoadContentsData *data)
6765 g_byte_array_free (data->content, TRUE);
6766 g_free (data->etag);
6771 load_contents_close_callback (GObject *obj,
6772 GAsyncResult *close_res,
6775 GInputStream *stream = G_INPUT_STREAM (obj);
6776 LoadContentsData *data = user_data;
6778 /* Ignore errors here, we're only reading anyway */
6779 g_input_stream_close_finish (stream, close_res, NULL);
6780 g_object_unref (stream);
6782 g_task_return_boolean (data->task, TRUE);
6783 g_object_unref (data->task);
6787 load_contents_fstat_callback (GObject *obj,
6788 GAsyncResult *stat_res,
6791 GInputStream *stream = G_INPUT_STREAM (obj);
6792 LoadContentsData *data = user_data;
6795 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6799 data->etag = g_strdup (g_file_info_get_etag (info));
6800 g_object_unref (info);
6803 g_input_stream_close_async (stream, 0,
6804 g_task_get_cancellable (data->task),
6805 load_contents_close_callback, data);
6809 load_contents_read_callback (GObject *obj,
6810 GAsyncResult *read_res,
6813 GInputStream *stream = G_INPUT_STREAM (obj);
6814 LoadContentsData *data = user_data;
6815 GError *error = NULL;
6818 read_size = g_input_stream_read_finish (stream, read_res, &error);
6822 g_task_return_error (data->task, error);
6823 g_object_unref (data->task);
6825 /* Close the file ignoring any error */
6826 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6827 g_object_unref (stream);
6829 else if (read_size == 0)
6831 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6832 G_FILE_ATTRIBUTE_ETAG_VALUE,
6834 g_task_get_cancellable (data->task),
6835 load_contents_fstat_callback,
6838 else if (read_size > 0)
6840 data->pos += read_size;
6842 g_byte_array_set_size (data->content,
6843 data->pos + GET_CONTENT_BLOCK_SIZE);
6846 if (data->read_more_callback &&
6847 !data->read_more_callback ((char *)data->content->data, data->pos,
6848 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6849 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6850 G_FILE_ATTRIBUTE_ETAG_VALUE,
6852 g_task_get_cancellable (data->task),
6853 load_contents_fstat_callback,
6856 g_input_stream_read_async (stream,
6857 data->content->data + data->pos,
6858 GET_CONTENT_BLOCK_SIZE,
6860 g_task_get_cancellable (data->task),
6861 load_contents_read_callback,
6867 load_contents_open_callback (GObject *obj,
6868 GAsyncResult *open_res,
6871 GFile *file = G_FILE (obj);
6872 GFileInputStream *stream;
6873 LoadContentsData *data = user_data;
6874 GError *error = NULL;
6876 stream = g_file_read_finish (file, open_res, &error);
6880 g_byte_array_set_size (data->content,
6881 data->pos + GET_CONTENT_BLOCK_SIZE);
6882 g_input_stream_read_async (G_INPUT_STREAM (stream),
6883 data->content->data + data->pos,
6884 GET_CONTENT_BLOCK_SIZE,
6886 g_task_get_cancellable (data->task),
6887 load_contents_read_callback,
6892 g_task_return_error (data->task, error);
6893 g_object_unref (data->task);
6898 * g_file_load_partial_contents_async: (skip)
6899 * @file: input #GFile
6900 * @cancellable: optional #GCancellable object, %NULL to ignore
6901 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6902 * and to specify whether further data should be read
6903 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6904 * @user_data: the data to pass to the callback functions
6906 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6907 * be used to stop reading from the file when appropriate, else this
6908 * function will behave exactly as g_file_load_contents_async(). This
6909 * operation can be finished by g_file_load_partial_contents_finish().
6911 * Users of this function should be aware that @user_data is passed to
6912 * both the @read_more_callback and the @callback.
6914 * If @cancellable is not %NULL, then the operation can be cancelled by
6915 * triggering the cancellable object from another thread. If the operation
6916 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6919 g_file_load_partial_contents_async (GFile *file,
6920 GCancellable *cancellable,
6921 GFileReadMoreCallback read_more_callback,
6922 GAsyncReadyCallback callback,
6925 LoadContentsData *data;
6927 g_return_if_fail (G_IS_FILE (file));
6929 data = g_new0 (LoadContentsData, 1);
6930 data->read_more_callback = read_more_callback;
6931 data->content = g_byte_array_new ();
6933 data->task = g_task_new (file, cancellable, callback, user_data);
6934 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6936 g_file_read_async (file,
6938 g_task_get_cancellable (data->task),
6939 load_contents_open_callback,
6944 * g_file_load_partial_contents_finish:
6945 * @file: input #GFile
6946 * @res: a #GAsyncResult
6947 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6948 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6949 * or %NULL if the length is not needed
6950 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6951 * or %NULL if the entity tag is not needed
6952 * @error: a #GError, or %NULL
6954 * Finishes an asynchronous partial load operation that was started
6955 * with g_file_load_partial_contents_async(). The data is always
6956 * zero-terminated, but this is not included in the resultant @length.
6957 * The returned @content should be freed with g_free() when no longer
6960 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6961 * present, it will be set appropriately.
6964 g_file_load_partial_contents_finish (GFile *file,
6972 LoadContentsData *data;
6974 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6975 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6976 g_return_val_if_fail (contents != NULL, FALSE);
6978 task = G_TASK (res);
6980 if (!g_task_propagate_boolean (task, error))
6987 data = g_task_get_task_data (task);
6990 *length = data->pos;
6994 *etag_out = data->etag;
6998 /* Zero terminate */
6999 g_byte_array_set_size (data->content, data->pos + 1);
7000 data->content->data[data->pos] = 0;
7002 *contents = (char *)g_byte_array_free (data->content, FALSE);
7003 data->content = NULL;
7009 * g_file_load_contents_async:
7010 * @file: input #GFile
7011 * @cancellable: optional #GCancellable object, %NULL to ignore
7012 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7013 * @user_data: the data to pass to callback function
7015 * Starts an asynchronous load of the @file's contents.
7017 * For more details, see g_file_load_contents() which is
7018 * the synchronous version of this call.
7020 * When the load operation has completed, @callback will be called
7021 * with @user data. To finish the operation, call
7022 * g_file_load_contents_finish() with the #GAsyncResult returned by
7025 * If @cancellable is not %NULL, then the operation can be cancelled by
7026 * triggering the cancellable object from another thread. If the operation
7027 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7030 g_file_load_contents_async (GFile *file,
7031 GCancellable *cancellable,
7032 GAsyncReadyCallback callback,
7035 g_file_load_partial_contents_async (file,
7038 callback, user_data);
7042 * g_file_load_contents_finish:
7043 * @file: input #GFile
7044 * @res: a #GAsyncResult
7045 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7046 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7047 * or %NULL if the length is not needed
7048 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7049 * or %NULL if the entity tag is not needed
7050 * @error: a #GError, or %NULL
7052 * Finishes an asynchronous load of the @file's contents.
7053 * The contents are placed in @contents, and @length is set to the
7054 * size of the @contents string. The @content should be freed with
7055 * g_free() when no longer needed. If @etag_out is present, it will be
7056 * set to the new entity tag for the @file.
7058 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7059 * present, it will be set appropriately.
7062 g_file_load_contents_finish (GFile *file,
7069 return g_file_load_partial_contents_finish (file,
7078 * g_file_replace_contents:
7079 * @file: input #GFile
7080 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7081 * @length: the length of @contents in bytes
7082 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7084 * @make_backup: %TRUE if a backup should be created
7085 * @flags: a set of #GFileCreateFlags
7086 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7087 * for the document. This should be freed with g_free() when no longer
7089 * @cancellable: optional #GCancellable object, %NULL to ignore
7090 * @error: a #GError, or %NULL
7092 * Replaces the contents of @file with @contents of @length bytes.
7094 * If @etag is specified (not %NULL), any existing file must have that etag,
7095 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7097 * If @make_backup is %TRUE, this function will attempt to make a backup
7100 * If @cancellable is not %NULL, then the operation can be cancelled by
7101 * triggering the cancellable object from another thread. If the operation
7102 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7104 * The returned @new_etag can be used to verify that the file hasn't
7105 * changed the next time it is saved over.
7107 * Returns: %TRUE if successful. If an error has occurred, this function
7108 * will return %FALSE and set @error appropriately if present.
7111 g_file_replace_contents (GFile *file,
7112 const char *contents,
7115 gboolean make_backup,
7116 GFileCreateFlags flags,
7118 GCancellable *cancellable,
7121 GFileOutputStream *out;
7122 gsize pos, remainder;
7126 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7127 g_return_val_if_fail (contents != NULL, FALSE);
7129 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7135 while (remainder > 0 &&
7136 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7138 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7146 if (remainder > 0 && res < 0)
7148 /* Ignore errors on close */
7149 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7150 g_object_unref (out);
7152 /* error is set already */
7156 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7159 *new_etag = g_file_output_stream_get_etag (out);
7161 g_object_unref (out);
7172 } ReplaceContentsData;
7175 replace_contents_data_free (ReplaceContentsData *data)
7177 g_bytes_unref (data->content);
7178 g_free (data->etag);
7183 replace_contents_close_callback (GObject *obj,
7184 GAsyncResult *close_res,
7187 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7188 ReplaceContentsData *data = user_data;
7190 /* Ignore errors here, we're only reading anyway */
7191 g_output_stream_close_finish (stream, close_res, NULL);
7192 g_object_unref (stream);
7196 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7197 g_task_return_boolean (data->task, TRUE);
7199 g_object_unref (data->task);
7203 replace_contents_write_callback (GObject *obj,
7204 GAsyncResult *read_res,
7207 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7208 ReplaceContentsData *data = user_data;
7209 GError *error = NULL;
7212 write_size = g_output_stream_write_finish (stream, read_res, &error);
7214 if (write_size <= 0)
7216 /* Error or EOF, close the file */
7219 data->failed = TRUE;
7220 g_task_return_error (data->task, error);
7222 g_output_stream_close_async (stream, 0,
7223 g_task_get_cancellable (data->task),
7224 replace_contents_close_callback, data);
7226 else if (write_size > 0)
7228 const gchar *content;
7231 content = g_bytes_get_data (data->content, &length);
7232 data->pos += write_size;
7234 if (data->pos >= length)
7235 g_output_stream_close_async (stream, 0,
7236 g_task_get_cancellable (data->task),
7237 replace_contents_close_callback, data);
7239 g_output_stream_write_async (stream,
7240 content + data->pos,
7243 g_task_get_cancellable (data->task),
7244 replace_contents_write_callback,
7250 replace_contents_open_callback (GObject *obj,
7251 GAsyncResult *open_res,
7254 GFile *file = G_FILE (obj);
7255 GFileOutputStream *stream;
7256 ReplaceContentsData *data = user_data;
7257 GError *error = NULL;
7259 stream = g_file_replace_finish (file, open_res, &error);
7263 const gchar *content;
7266 content = g_bytes_get_data (data->content, &length);
7267 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7268 content + data->pos,
7271 g_task_get_cancellable (data->task),
7272 replace_contents_write_callback,
7277 g_task_return_error (data->task, error);
7278 g_object_unref (data->task);
7283 * g_file_replace_contents_async:
7284 * @file: input #GFile
7285 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7286 * @length: the length of @contents in bytes
7287 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7288 * @make_backup: %TRUE if a backup should be created
7289 * @flags: a set of #GFileCreateFlags
7290 * @cancellable: optional #GCancellable object, %NULL to ignore
7291 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7292 * @user_data: the data to pass to callback function
7294 * Starts an asynchronous replacement of @file with the given
7295 * @contents of @length bytes. @etag will replace the document's
7296 * current entity tag.
7298 * When this operation has completed, @callback will be called with
7299 * @user_user data, and the operation can be finalized with
7300 * g_file_replace_contents_finish().
7302 * If @cancellable is not %NULL, then the operation can be cancelled by
7303 * triggering the cancellable object from another thread. If the operation
7304 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7306 * If @make_backup is %TRUE, this function will attempt to
7307 * make a backup of @file.
7309 * Note that no copy of @content will be made, so it must stay valid
7310 * until @callback is called. See g_file_replace_contents_bytes_async()
7311 * for a #GBytes version that will automatically hold a reference to the
7312 * contents (without copying) for the duration of the call.
7315 g_file_replace_contents_async (GFile *file,
7316 const char *contents,
7319 gboolean make_backup,
7320 GFileCreateFlags flags,
7321 GCancellable *cancellable,
7322 GAsyncReadyCallback callback,
7327 bytes = g_bytes_new_static (contents, length);
7328 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7329 cancellable, callback, user_data);
7330 g_bytes_unref (bytes);
7334 * g_file_replace_contents_bytes_async:
7335 * @file: input #GFile
7336 * @contents: a #GBytes
7337 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7338 * @make_backup: %TRUE if a backup should be created
7339 * @flags: a set of #GFileCreateFlags
7340 * @cancellable: optional #GCancellable object, %NULL to ignore
7341 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7342 * @user_data: the data to pass to callback function
7344 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7345 * This function will keep a ref on @contents until the operation is done.
7346 * Unlike g_file_replace_contents_async() this allows forgetting about the
7347 * content without waiting for the callback.
7349 * When this operation has completed, @callback will be called with
7350 * @user_user data, and the operation can be finalized with
7351 * g_file_replace_contents_finish().
7356 g_file_replace_contents_bytes_async (GFile *file,
7359 gboolean make_backup,
7360 GFileCreateFlags flags,
7361 GCancellable *cancellable,
7362 GAsyncReadyCallback callback,
7365 ReplaceContentsData *data;
7367 g_return_if_fail (G_IS_FILE (file));
7368 g_return_if_fail (contents != NULL);
7370 data = g_new0 (ReplaceContentsData, 1);
7372 data->content = g_bytes_ref (contents);
7374 data->task = g_task_new (file, cancellable, callback, user_data);
7375 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7377 g_file_replace_async (file,
7382 g_task_get_cancellable (data->task),
7383 replace_contents_open_callback,
7388 * g_file_replace_contents_finish:
7389 * @file: input #GFile
7390 * @res: a #GAsyncResult
7391 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7392 * for the document. This should be freed with g_free() when it is no
7393 * longer needed, or %NULL
7394 * @error: a #GError, or %NULL
7396 * Finishes an asynchronous replace of the given @file. See
7397 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7398 * tag for the document, if present.
7400 * Returns: %TRUE on success, %FALSE on failure.
7403 g_file_replace_contents_finish (GFile *file,
7409 ReplaceContentsData *data;
7411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7412 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7414 task = G_TASK (res);
7416 if (!g_task_propagate_boolean (task, error))
7419 data = g_task_get_task_data (task);
7423 *new_etag = data->etag;
7424 data->etag = NULL; /* Take ownership */
7431 g_file_real_measure_disk_usage (GFile *file,
7432 GFileMeasureFlags flags,
7433 GCancellable *cancellable,
7434 GFileMeasureProgressCallback progress_callback,
7435 gpointer progress_data,
7436 guint64 *disk_usage,
7441 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7442 "Operation not supported for the current backend.");
7448 GFileMeasureFlags flags;
7449 GFileMeasureProgressCallback progress_callback;
7450 gpointer progress_data;
7462 GFileMeasureProgressCallback callback;
7465 guint64 current_size;
7471 measure_disk_usage_invoke_progress (gpointer user_data)
7473 MeasureProgress *progress = user_data;
7475 (* progress->callback) (progress->reporting,
7476 progress->current_size, progress->num_dirs, progress->num_files,
7477 progress->user_data);
7483 measure_disk_usage_progress (gboolean reporting,
7484 guint64 current_size,
7489 MeasureProgress progress;
7490 GTask *task = user_data;
7491 MeasureTaskData *data;
7493 data = g_task_get_task_data (task);
7495 progress.callback = data->progress_callback;
7496 progress.user_data = data->progress_data;
7497 progress.reporting = reporting;
7498 progress.current_size = current_size;
7499 progress.num_dirs = num_dirs;
7500 progress.num_files = num_files;
7502 g_main_context_invoke_full (g_task_get_context (task),
7503 g_task_get_priority (task),
7504 measure_disk_usage_invoke_progress,
7505 g_memdup (&progress, sizeof progress),
7510 measure_disk_usage_thread (GTask *task,
7511 gpointer source_object,
7513 GCancellable *cancellable)
7515 MeasureTaskData *data = task_data;
7516 GError *error = NULL;
7517 MeasureResult result = { 0, };
7519 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7520 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7521 &result.disk_usage, &result.num_dirs, &result.num_files,
7523 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7525 g_task_return_error (task, error);
7529 g_file_real_measure_disk_usage_async (GFile *file,
7530 GFileMeasureFlags flags,
7532 GCancellable *cancellable,
7533 GFileMeasureProgressCallback progress_callback,
7534 gpointer progress_data,
7535 GAsyncReadyCallback callback,
7538 MeasureTaskData data;
7542 data.progress_callback = progress_callback;
7543 data.progress_data = progress_data;
7545 task = g_task_new (file, cancellable, callback, user_data);
7546 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7547 g_task_set_priority (task, io_priority);
7549 g_task_run_in_thread (task, measure_disk_usage_thread);
7550 g_object_unref (task);
7554 g_file_real_measure_disk_usage_finish (GFile *file,
7555 GAsyncResult *result,
7556 guint64 *disk_usage,
7561 MeasureResult *measure_result;
7563 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7565 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7567 if (measure_result == NULL)
7571 *disk_usage = measure_result->disk_usage;
7574 *num_dirs = measure_result->num_dirs;
7577 *num_files = measure_result->num_files;
7579 g_free (measure_result);
7585 * g_file_measure_disk_usage:
7587 * @flags: #GFileMeasureFlags
7588 * @cancellable: (allow-none): optional #GCancellable
7589 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7590 * @progress_data: user_data for @progress_callback
7591 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7592 * @num_dirs: (allow-none) (out): the number of directories encountered
7593 * @num_files: (allow-none) (out): the number of non-directories encountered
7594 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7596 * Recursively measures the disk usage of @file.
7598 * This is essentially an analog of the 'du' command, but it also
7599 * reports the number of directories and non-directory files encountered
7600 * (including things like symbolic links).
7602 * By default, errors are only reported against the toplevel file
7603 * itself. Errors found while recursing are silently ignored, unless
7604 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7606 * The returned size, @disk_usage, is in bytes and should be formatted
7607 * with g_format_size() in order to get something reasonable for showing
7608 * in a user interface.
7610 * @progress_callback and @progress_data can be given to request
7611 * periodic progress updates while scanning. See the documentation for
7612 * #GFileMeasureProgressCallback for information about when and how the
7613 * callback will be invoked.
7615 * Returns: %TRUE if successful, with the out parameters set.
7616 * %FALSE otherwise, with @error set.
7621 g_file_measure_disk_usage (GFile *file,
7622 GFileMeasureFlags flags,
7623 GCancellable *cancellable,
7624 GFileMeasureProgressCallback progress_callback,
7625 gpointer progress_data,
7626 guint64 *disk_usage,
7631 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7632 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7633 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7635 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7636 progress_callback, progress_data,
7637 disk_usage, num_dirs, num_files,
7642 * g_file_measure_disk_usage_async:
7644 * @flags: #GFileMeasureFlags
7645 * @io_priority: the [I/O priority][io-priority] of the request
7646 * @cancellable: (allow-none): optional #GCancellable
7647 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7648 * @progress_data: user_data for @progress_callback
7649 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7650 * @user_data: the data to pass to callback function
7652 * Recursively measures the disk usage of @file.
7654 * This is the asynchronous version of g_file_measure_disk_usage(). See
7655 * there for more information.
7660 g_file_measure_disk_usage_async (GFile *file,
7661 GFileMeasureFlags flags,
7663 GCancellable *cancellable,
7664 GFileMeasureProgressCallback progress_callback,
7665 gpointer progress_data,
7666 GAsyncReadyCallback callback,
7669 g_return_if_fail (G_IS_FILE (file));
7670 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7672 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7673 progress_callback, progress_data,
7674 callback, user_data);
7678 * g_file_measure_disk_usage_finish:
7680 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7681 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7682 * @num_dirs: (allow-none) (out): the number of directories encountered
7683 * @num_files: (allow-none) (out): the number of non-directories encountered
7684 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7686 * Collects the results from an earlier call to
7687 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7690 * Returns: %TRUE if successful, with the out parameters set.
7691 * %FALSE otherwise, with @error set.
7696 g_file_measure_disk_usage_finish (GFile *file,
7697 GAsyncResult *result,
7698 guint64 *disk_usage,
7703 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7704 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7706 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7710 * g_file_start_mountable:
7711 * @file: input #GFile
7712 * @flags: flags affecting the operation
7713 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7714 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7715 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7716 * @user_data: the data to pass to callback function
7718 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7719 * Using @start_operation, you can request callbacks when, for instance,
7720 * passwords are needed during authentication.
7722 * If @cancellable is not %NULL, then the operation can be cancelled by
7723 * triggering the cancellable object from another thread. If the operation
7724 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7726 * When the operation is finished, @callback will be called.
7727 * You can then call g_file_mount_mountable_finish() to get
7728 * the result of the operation.
7733 g_file_start_mountable (GFile *file,
7734 GDriveStartFlags flags,
7735 GMountOperation *start_operation,
7736 GCancellable *cancellable,
7737 GAsyncReadyCallback callback,
7742 g_return_if_fail (G_IS_FILE (file));
7744 iface = G_FILE_GET_IFACE (file);
7746 if (iface->start_mountable == NULL)
7748 g_task_report_new_error (file, callback, user_data,
7749 g_file_start_mountable,
7750 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7751 _("Operation not supported"));
7755 (* iface->start_mountable) (file,
7764 * g_file_start_mountable_finish:
7765 * @file: input #GFile
7766 * @result: a #GAsyncResult
7767 * @error: a #GError, or %NULL
7769 * Finishes a start operation. See g_file_start_mountable() for details.
7771 * Finish an asynchronous start operation that was started
7772 * with g_file_start_mountable().
7774 * Returns: %TRUE if the operation finished successfully. %FALSE
7780 g_file_start_mountable_finish (GFile *file,
7781 GAsyncResult *result,
7786 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7787 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7789 if (g_async_result_legacy_propagate_error (result, error))
7791 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7792 return g_task_propagate_boolean (G_TASK (result), error);
7794 iface = G_FILE_GET_IFACE (file);
7795 return (* iface->start_mountable_finish) (file, result, error);
7799 * g_file_stop_mountable:
7800 * @file: input #GFile
7801 * @flags: flags affecting the operation
7802 * @mount_operation: (allow-none): a #GMountOperation,
7803 * or %NULL to avoid user interaction.
7804 * @cancellable: (allow-none): optional #GCancellable object,
7806 * @callback: (allow-none): a #GAsyncReadyCallback to call
7807 * when the request is satisfied, or %NULL
7808 * @user_data: the data to pass to callback function
7810 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7812 * If @cancellable is not %NULL, then the operation can be cancelled by
7813 * triggering the cancellable object from another thread. If the operation
7814 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7816 * When the operation is finished, @callback will be called.
7817 * You can then call g_file_stop_mountable_finish() to get
7818 * the result of the operation.
7823 g_file_stop_mountable (GFile *file,
7824 GMountUnmountFlags flags,
7825 GMountOperation *mount_operation,
7826 GCancellable *cancellable,
7827 GAsyncReadyCallback callback,
7832 g_return_if_fail (G_IS_FILE (file));
7834 iface = G_FILE_GET_IFACE (file);
7836 if (iface->stop_mountable == NULL)
7838 g_task_report_new_error (file, callback, user_data,
7839 g_file_stop_mountable,
7840 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7841 _("Operation not supported"));
7845 (* iface->stop_mountable) (file,
7854 * g_file_stop_mountable_finish:
7855 * @file: input #GFile
7856 * @result: a #GAsyncResult
7857 * @error: a #GError, or %NULL
7859 * Finishes an stop operation, see g_file_stop_mountable() for details.
7861 * Finish an asynchronous stop operation that was started
7862 * with g_file_stop_mountable().
7864 * Returns: %TRUE if the operation finished successfully.
7870 g_file_stop_mountable_finish (GFile *file,
7871 GAsyncResult *result,
7876 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7877 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7879 if (g_async_result_legacy_propagate_error (result, error))
7881 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7882 return g_task_propagate_boolean (G_TASK (result), error);
7884 iface = G_FILE_GET_IFACE (file);
7885 return (* iface->stop_mountable_finish) (file, result, error);
7889 * g_file_poll_mountable:
7890 * @file: input #GFile
7891 * @cancellable: optional #GCancellable object, %NULL to ignore
7892 * @callback: (allow-none): a #GAsyncReadyCallback to call
7893 * when the request is satisfied, or %NULL
7894 * @user_data: the data to pass to callback function
7896 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7898 * If @cancellable is not %NULL, then the operation can be cancelled by
7899 * triggering the cancellable object from another thread. If the operation
7900 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7902 * When the operation is finished, @callback will be called.
7903 * You can then call g_file_mount_mountable_finish() to get
7904 * the result of the operation.
7909 g_file_poll_mountable (GFile *file,
7910 GCancellable *cancellable,
7911 GAsyncReadyCallback callback,
7916 g_return_if_fail (G_IS_FILE (file));
7918 iface = G_FILE_GET_IFACE (file);
7920 if (iface->poll_mountable == NULL)
7922 g_task_report_new_error (file, callback, user_data,
7923 g_file_poll_mountable,
7924 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7925 _("Operation not supported"));
7929 (* iface->poll_mountable) (file,
7936 * g_file_poll_mountable_finish:
7937 * @file: input #GFile
7938 * @result: a #GAsyncResult
7939 * @error: a #GError, or %NULL
7941 * Finishes a poll operation. See g_file_poll_mountable() for details.
7943 * Finish an asynchronous poll operation that was polled
7944 * with g_file_poll_mountable().
7946 * Returns: %TRUE if the operation finished successfully. %FALSE
7952 g_file_poll_mountable_finish (GFile *file,
7953 GAsyncResult *result,
7958 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7959 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7961 if (g_async_result_legacy_propagate_error (result, error))
7963 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7964 return g_task_propagate_boolean (G_TASK (result), error);
7966 iface = G_FILE_GET_IFACE (file);
7967 return (* iface->poll_mountable_finish) (file, result, error);
7971 * g_file_supports_thread_contexts:
7974 * Checks if @file supports
7975 * [thread-default contexts][g-main-context-push-thread-default-context].
7976 * If this returns %FALSE, you cannot perform asynchronous operations on
7977 * @file in a thread that has a thread-default context.
7979 * Returns: Whether or not @file supports thread-default contexts.
7984 g_file_supports_thread_contexts (GFile *file)
7988 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7990 iface = G_FILE_GET_IFACE (file);
7991 return iface->supports_thread_contexts;