1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
80 * <member>g_file_new_for_path() if you have a path.</member>
81 * <member>g_file_new_for_uri() if you have a URI.</member>
82 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
83 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
84 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
87 * One way to think of a #GFile is as an abstraction of a pathname. For
88 * normal files the system pathname is what is stored internally, but as
89 * #GFiles are extensible it could also be something else that corresponds
90 * to a pathname in a userspace implementation of a filesystem.
92 * #GFiles make up hierarchies of directories and files that correspond to
93 * the files on a filesystem. You can move through the file system with
94 * #GFile using g_file_get_parent() to get an identifier for the parent
95 * directory, g_file_get_child() to get a child within a directory,
96 * g_file_resolve_relative_path() to resolve a relative path between two
97 * #GFiles. There can be multiple hierarchies, so you may not end up at
98 * the same root if you repeatedly call g_file_get_parent() on two different
101 * All #GFiles have a basename (get with g_file_get_basename()). These names
102 * are byte strings that are used to identify the file on the filesystem
103 * (relative to its parent directory) and there is no guarantees that they
104 * have any particular charset encoding or even make any sense at all. If
105 * you want to use filenames in a user interface you should use the display
106 * name that you can get by requesting the
107 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
108 * This is guaranteed to be in UTF-8 and can be used in a user interface.
109 * But always store the real basename or the #GFile to use to actually
110 * access the file, because there is no way to go from a display name to
113 * Using #GFile as an identifier has the same weaknesses as using a path
114 * in that there may be multiple aliases for the same file. For instance,
115 * hard or soft links may cause two different #GFiles to refer to the same
116 * file. Other possible causes for aliases are: case insensitive filesystems,
117 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
118 * check if two #GFiles point to the same file you can query for the
119 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
120 * canonicalization of pathnames passed in, so that trivial differences in
121 * the path string used at creation (duplicated slashes, slash at end of
122 * path, "." or ".." path segments, etc) does not create different #GFiles.
124 * Many #GFile operations have both synchronous and asynchronous versions
125 * to suit your application. Asynchronous versions of synchronous functions
126 * simply have _async() appended to their function names. The asynchronous
127 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
128 * the operation, producing a GAsyncResult which is then passed to the
129 * function's matching _finish() operation.
131 * Some #GFile operations do not have synchronous analogs, as they may
132 * take a very long time to finish, and blocking may leave an application
133 * unusable. Notable cases include:
135 * <member>g_file_mount_mountable() to mount a mountable file.</member>
136 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
137 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
140 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
141 * One notable feature of #GFiles are entity tags, or "etags" for
142 * short. Entity tags are somewhat like a more abstract version of the
143 * traditional mtime, and can be used to quickly determine if the file has
144 * been modified from the version on the file system. See the HTTP 1.1
145 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
146 * for HTTP Etag headers, which are a very similar concept.
150 static void g_file_real_query_info_async (GFile *file,
151 const char *attributes,
152 GFileQueryInfoFlags flags,
154 GCancellable *cancellable,
155 GAsyncReadyCallback callback,
157 static GFileInfo * g_file_real_query_info_finish (GFile *file,
160 static void g_file_real_query_filesystem_info_async (GFile *file,
161 const char *attributes,
163 GCancellable *cancellable,
164 GAsyncReadyCallback callback,
166 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
169 static void g_file_real_enumerate_children_async (GFile *file,
170 const char *attributes,
171 GFileQueryInfoFlags flags,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
176 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
179 static void g_file_real_read_async (GFile *file,
181 GCancellable *cancellable,
182 GAsyncReadyCallback callback,
184 static GFileInputStream * g_file_real_read_finish (GFile *file,
187 static void g_file_real_append_to_async (GFile *file,
188 GFileCreateFlags flags,
190 GCancellable *cancellable,
191 GAsyncReadyCallback callback,
193 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
196 static void g_file_real_create_async (GFile *file,
197 GFileCreateFlags flags,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
202 static GFileOutputStream *g_file_real_create_finish (GFile *file,
205 static void g_file_real_replace_async (GFile *file,
207 gboolean make_backup,
208 GFileCreateFlags flags,
210 GCancellable *cancellable,
211 GAsyncReadyCallback callback,
213 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
216 static void g_file_real_delete_async (GFile *file,
218 GCancellable *cancellable,
219 GAsyncReadyCallback callback,
221 static gboolean g_file_real_delete_finish (GFile *file,
224 static void g_file_real_trash_async (GFile *file,
226 GCancellable *cancellable,
227 GAsyncReadyCallback callback,
229 static gboolean g_file_real_trash_finish (GFile *file,
232 static void g_file_real_make_directory_async (GFile *file,
234 GCancellable *cancellable,
235 GAsyncReadyCallback callback,
237 static gboolean g_file_real_make_directory_finish (GFile *file,
240 static void g_file_real_open_readwrite_async (GFile *file,
242 GCancellable *cancellable,
243 GAsyncReadyCallback callback,
245 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
248 static void g_file_real_create_readwrite_async (GFile *file,
249 GFileCreateFlags flags,
251 GCancellable *cancellable,
252 GAsyncReadyCallback callback,
254 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
257 static void g_file_real_replace_readwrite_async (GFile *file,
259 gboolean make_backup,
260 GFileCreateFlags flags,
262 GCancellable *cancellable,
263 GAsyncReadyCallback callback,
265 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
268 static gboolean g_file_real_set_attributes_from_info (GFile *file,
270 GFileQueryInfoFlags flags,
271 GCancellable *cancellable,
273 static void g_file_real_set_display_name_async (GFile *file,
274 const char *display_name,
276 GCancellable *cancellable,
277 GAsyncReadyCallback callback,
279 static GFile * g_file_real_set_display_name_finish (GFile *file,
282 static void g_file_real_set_attributes_async (GFile *file,
284 GFileQueryInfoFlags flags,
286 GCancellable *cancellable,
287 GAsyncReadyCallback callback,
289 static gboolean g_file_real_set_attributes_finish (GFile *file,
293 static void g_file_real_find_enclosing_mount_async (GFile *file,
295 GCancellable *cancellable,
296 GAsyncReadyCallback callback,
298 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
301 static void g_file_real_copy_async (GFile *source,
303 GFileCopyFlags flags,
305 GCancellable *cancellable,
306 GFileProgressCallback progress_callback,
307 gpointer progress_callback_data,
308 GAsyncReadyCallback callback,
310 static gboolean g_file_real_copy_finish (GFile *file,
314 static gboolean g_file_real_measure_disk_usage (GFile *file,
315 GFileMeasureFlags flags,
316 GCancellable *cancellable,
317 GFileMeasureProgressCallback progress_callback,
318 gpointer progress_data,
323 static void g_file_real_measure_disk_usage_async (GFile *file,
324 GFileMeasureFlags flags,
326 GCancellable *cancellable,
327 GFileMeasureProgressCallback progress_callback,
328 gpointer progress_data,
329 GAsyncReadyCallback callback,
331 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
332 GAsyncResult *result,
338 typedef GFileIface GFileInterface;
339 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
342 g_file_default_init (GFileIface *iface)
344 iface->enumerate_children_async = g_file_real_enumerate_children_async;
345 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
346 iface->set_display_name_async = g_file_real_set_display_name_async;
347 iface->set_display_name_finish = g_file_real_set_display_name_finish;
348 iface->query_info_async = g_file_real_query_info_async;
349 iface->query_info_finish = g_file_real_query_info_finish;
350 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
351 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
352 iface->set_attributes_async = g_file_real_set_attributes_async;
353 iface->set_attributes_finish = g_file_real_set_attributes_finish;
354 iface->read_async = g_file_real_read_async;
355 iface->read_finish = g_file_real_read_finish;
356 iface->append_to_async = g_file_real_append_to_async;
357 iface->append_to_finish = g_file_real_append_to_finish;
358 iface->create_async = g_file_real_create_async;
359 iface->create_finish = g_file_real_create_finish;
360 iface->replace_async = g_file_real_replace_async;
361 iface->replace_finish = g_file_real_replace_finish;
362 iface->delete_file_async = g_file_real_delete_async;
363 iface->delete_file_finish = g_file_real_delete_finish;
364 iface->trash_async = g_file_real_trash_async;
365 iface->trash_finish = g_file_real_trash_finish;
366 iface->make_directory_async = g_file_real_make_directory_async;
367 iface->make_directory_finish = g_file_real_make_directory_finish;
368 iface->open_readwrite_async = g_file_real_open_readwrite_async;
369 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
370 iface->create_readwrite_async = g_file_real_create_readwrite_async;
371 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
372 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
373 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
374 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
375 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
376 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
377 iface->copy_async = g_file_real_copy_async;
378 iface->copy_finish = g_file_real_copy_finish;
379 iface->measure_disk_usage = g_file_real_measure_disk_usage;
380 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
381 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
387 * @file: input #GFile
389 * Checks to see if a file is native to the platform.
391 * A native file s one expressed in the platform-native filename format,
392 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
393 * as it might be on a locally mounted remote filesystem.
395 * On some systems non-native files may be available using the native
396 * filesystem via a userspace filesystem (FUSE), in these cases this call
397 * will return %FALSE, but g_file_get_path() will still return a native path.
399 * This call does no blocking I/O.
401 * Returns: %TRUE if @file is native
404 g_file_is_native (GFile *file)
408 g_return_val_if_fail (G_IS_FILE (file), FALSE);
410 iface = G_FILE_GET_IFACE (file);
412 return (* iface->is_native) (file);
417 * g_file_has_uri_scheme:
418 * @file: input #GFile
419 * @uri_scheme: a string containing a URI scheme
421 * Checks to see if a #GFile has a given URI scheme.
423 * This call does no blocking I/O.
425 * Returns: %TRUE if #GFile's backend supports the
426 * given URI scheme, %FALSE if URI scheme is %NULL,
427 * not supported, or #GFile is invalid.
430 g_file_has_uri_scheme (GFile *file,
431 const char *uri_scheme)
435 g_return_val_if_fail (G_IS_FILE (file), FALSE);
436 g_return_val_if_fail (uri_scheme != NULL, FALSE);
438 iface = G_FILE_GET_IFACE (file);
440 return (* iface->has_uri_scheme) (file, uri_scheme);
445 * g_file_get_uri_scheme:
446 * @file: input #GFile
448 * Gets the URI scheme for a #GFile.
449 * RFC 3986 decodes the scheme as:
451 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
453 * Common schemes include "file", "http", "ftp", etc.
455 * This call does no blocking I/O.
457 * Returns: a string containing the URI scheme for the given
458 * #GFile. The returned string should be freed with g_free()
459 * when no longer needed.
462 g_file_get_uri_scheme (GFile *file)
466 g_return_val_if_fail (G_IS_FILE (file), NULL);
468 iface = G_FILE_GET_IFACE (file);
470 return (* iface->get_uri_scheme) (file);
475 * g_file_get_basename:
476 * @file: input #GFile
478 * Gets the base name (the last component of the path) for a given #GFile.
480 * If called for the top level of a system (such as the filesystem root
481 * or a uri like sftp://host/) it will return a single directory separator
482 * (and on Windows, possibly a drive letter).
484 * The base name is a byte string (not UTF-8). It has no defined encoding
485 * or rules other than it may not contain zero bytes. If you want to use
486 * filenames in a user interface you should use the display name that you
487 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
488 * attribute with g_file_query_info().
490 * This call does no blocking I/O.
492 * Returns: string containing the #GFile's base name, or %NULL
493 * if given #GFile is invalid. The returned string should be
494 * freed with g_free() when no longer needed.
497 g_file_get_basename (GFile *file)
501 g_return_val_if_fail (G_IS_FILE (file), NULL);
503 iface = G_FILE_GET_IFACE (file);
505 return (* iface->get_basename) (file);
510 * @file: input #GFile
512 * Gets the local pathname for #GFile, if one exists.
514 * This call does no blocking I/O.
516 * Returns: string containing the #GFile's path, or %NULL if
517 * no such path exists. The returned string should be
518 * freed with g_free() when no longer needed.
521 g_file_get_path (GFile *file)
525 g_return_val_if_fail (G_IS_FILE (file), NULL);
527 iface = G_FILE_GET_IFACE (file);
529 return (* iface->get_path) (file);
534 * @file: input #GFile
536 * Gets the URI for the @file.
538 * This call does no blocking I/O.
540 * Returns: a string containing the #GFile's URI.
541 * The returned string should be freed with g_free()
542 * when no longer needed.
545 g_file_get_uri (GFile *file)
549 g_return_val_if_fail (G_IS_FILE (file), NULL);
551 iface = G_FILE_GET_IFACE (file);
553 return (* iface->get_uri) (file);
557 * g_file_get_parse_name:
558 * @file: input #GFile
560 * Gets the parse name of the @file.
561 * A parse name is a UTF-8 string that describes the
562 * file such that one can get the #GFile back using
563 * g_file_parse_name().
565 * This is generally used to show the #GFile as a nice
566 * full-pathname kind of string in a user interface,
567 * like in a location entry.
569 * For local files with names that can safely be converted
570 * to UTF-8 the pathname is used, otherwise the IRI is used
571 * (a form of URI that allows UTF-8 characters unescaped).
573 * This call does no blocking I/O.
575 * Returns: a string containing the #GFile's parse name.
576 * The returned string should be freed with g_free()
577 * when no longer needed.
580 g_file_get_parse_name (GFile *file)
584 g_return_val_if_fail (G_IS_FILE (file), NULL);
586 iface = G_FILE_GET_IFACE (file);
588 return (* iface->get_parse_name) (file);
593 * @file: input #GFile
595 * Duplicates a #GFile handle. This operation does not duplicate
596 * the actual file or directory represented by the #GFile; see
597 * g_file_copy() if attempting to copy a file.
599 * This call does no blocking I/O.
601 * Returns: (transfer full): a new #GFile that is a duplicate
602 * of the given #GFile.
605 g_file_dup (GFile *file)
609 g_return_val_if_fail (G_IS_FILE (file), NULL);
611 iface = G_FILE_GET_IFACE (file);
613 return (* iface->dup) (file);
618 * @file: (type GFile): #gconstpointer to a #GFile
620 * Creates a hash value for a #GFile.
622 * This call does no blocking I/O.
625 * Returns: 0 if @file is not a valid #GFile, otherwise an
626 * integer that can be used as hash value for the #GFile.
627 * This function is intended for easily hashing a #GFile to
628 * add to a #GHashTable or similar data structure.
631 g_file_hash (gconstpointer file)
635 g_return_val_if_fail (G_IS_FILE (file), 0);
637 iface = G_FILE_GET_IFACE (file);
639 return (* iface->hash) ((GFile *)file);
644 * @file1: the first #GFile
645 * @file2: the second #GFile
647 * Checks equality of two given #GFiles.
649 * Note that two #GFiles that differ can still refer to the same
650 * file on the filesystem due to various forms of filename
653 * This call does no blocking I/O.
655 * Returns: %TRUE if @file1 and @file2 are equal.
656 * %FALSE if either is not a #GFile.
659 g_file_equal (GFile *file1,
664 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
665 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
667 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
670 iface = G_FILE_GET_IFACE (file1);
672 return (* iface->equal) (file1, file2);
678 * @file: input #GFile
680 * Gets the parent directory for the @file.
681 * If the @file represents the root directory of the
682 * file system, then %NULL will be returned.
684 * This call does no blocking I/O.
686 * Returns: (transfer full): a #GFile structure to the
687 * parent of the given #GFile or %NULL if there is
688 * no parent. Free the returned object with g_object_unref().
691 g_file_get_parent (GFile *file)
695 g_return_val_if_fail (G_IS_FILE (file), NULL);
697 iface = G_FILE_GET_IFACE (file);
699 return (* iface->get_parent) (file);
704 * @file: input #GFile
705 * @parent: (allow-none): the parent to check for, or %NULL
707 * Checks if @file has a parent, and optionally, if it is @parent.
709 * If @parent is %NULL then this function returns %TRUE if @file has any
710 * parent at all. If @parent is non-%NULL then %TRUE is only returned
711 * if @file is a child of @parent.
713 * Returns: %TRUE if @file is a child of @parent (or any parent in the
714 * case that @parent is %NULL).
719 g_file_has_parent (GFile *file,
722 GFile *actual_parent;
725 g_return_val_if_fail (G_IS_FILE (file), FALSE);
726 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
728 actual_parent = g_file_get_parent (file);
730 if (actual_parent != NULL)
733 result = g_file_equal (parent, actual_parent);
737 g_object_unref (actual_parent);
747 * @file: input #GFile
748 * @name: string containing the child's basename
750 * Gets a child of @file with basename equal to @name.
752 * Note that the file with that specific name might not exist, but
753 * you can still have a #GFile that points to it. You can use this
754 * for instance to create that file.
756 * This call does no blocking I/O.
758 * Returns: (transfer full): a #GFile to a child specified by @name.
759 * Free the returned object with g_object_unref().
762 g_file_get_child (GFile *file,
765 g_return_val_if_fail (G_IS_FILE (file), NULL);
766 g_return_val_if_fail (name != NULL, NULL);
768 return g_file_resolve_relative_path (file, name);
772 * g_file_get_child_for_display_name:
773 * @file: input #GFile
774 * @display_name: string to a possible child
775 * @error: return location for an error
777 * Gets the child of @file for a given @display_name (i.e. a UTF-8
778 * version of the name). If this function fails, it returns %NULL
779 * and @error will be set. This is very useful when constructing a
780 * #GFile for a new file and the user entered the filename in the
781 * user interface, for instance when you select a directory and
782 * type a filename in the file selector.
784 * This call does no blocking I/O.
786 * Returns: (transfer full): a #GFile to the specified child, or
787 * %NULL if the display name couldn't be converted.
788 * Free the returned object with g_object_unref().
791 g_file_get_child_for_display_name (GFile *file,
792 const char *display_name,
797 g_return_val_if_fail (G_IS_FILE (file), NULL);
798 g_return_val_if_fail (display_name != NULL, NULL);
800 iface = G_FILE_GET_IFACE (file);
802 return (* iface->get_child_for_display_name) (file, display_name, error);
807 * @file: input #GFile
808 * @prefix: input #GFile
810 * Checks whether @file has the prefix specified by @prefix.
812 * In other words, if the names of initial elements of @file's
813 * pathname match @prefix. Only full pathname elements are matched,
814 * so a path like /foo is not considered a prefix of /foobar, only
817 * This call does no I/O, as it works purely on names. As such it can
818 * sometimes return %FALSE even if @file is inside a @prefix (from a
819 * filesystem point of view), because the prefix of @file is an alias
822 * Virtual: prefix_matches
823 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
827 g_file_has_prefix (GFile *file,
832 g_return_val_if_fail (G_IS_FILE (file), FALSE);
833 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
835 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
838 iface = G_FILE_GET_IFACE (file);
840 /* The vtable function differs in arg order since
841 * we're using the old contains_file call
843 return (* iface->prefix_matches) (prefix, file);
847 * g_file_get_relative_path:
848 * @parent: input #GFile
849 * @descendant: input #GFile
851 * Gets the path for @descendant relative to @parent.
853 * This call does no blocking I/O.
855 * Returns: string with the relative path from @descendant
856 * to @parent, or %NULL if @descendant doesn't have @parent
857 * as prefix. The returned string should be freed with g_free()
858 * when no longer needed.
861 g_file_get_relative_path (GFile *parent,
866 g_return_val_if_fail (G_IS_FILE (parent), NULL);
867 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
869 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
872 iface = G_FILE_GET_IFACE (parent);
874 return (* iface->get_relative_path) (parent, descendant);
878 * g_file_resolve_relative_path:
879 * @file: input #GFile
880 * @relative_path: a given relative path string
882 * Resolves a relative path for @file to an absolute path.
884 * This call does no blocking I/O.
886 * Returns: (transfer full): #GFile to the resolved path.
887 * %NULL if @relative_path is %NULL or if @file is invalid.
888 * Free the returned object with g_object_unref().
891 g_file_resolve_relative_path (GFile *file,
892 const char *relative_path)
896 g_return_val_if_fail (G_IS_FILE (file), NULL);
897 g_return_val_if_fail (relative_path != NULL, NULL);
899 iface = G_FILE_GET_IFACE (file);
901 return (* iface->resolve_relative_path) (file, relative_path);
905 * g_file_enumerate_children:
906 * @file: input #GFile
907 * @attributes: an attribute query string
908 * @flags: a set of #GFileQueryInfoFlags
909 * @cancellable: (allow-none): optional #GCancellable object,
911 * @error: #GError for error reporting
913 * Gets the requested information about the files in a directory.
914 * The result is a #GFileEnumerator object that will give out
915 * #GFileInfo objects for all the files in the directory.
917 * The @attributes value is a string that specifies the file
918 * attributes that should be gathered. It is not an error if
919 * it's not possible to read a particular requested attribute
920 * from a file - it just won't be set. @attributes should
921 * be a comma-separated list of attributes or attribute wildcards.
922 * The wildcard "*" means all attributes, and a wildcard like
923 * "standard::*" means all attributes in the standard namespace.
924 * An example attribute query be "standard::*,owner::user".
925 * The standard attributes are available as defines, like
926 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
928 * If @cancellable is not %NULL, then the operation can be cancelled
929 * by triggering the cancellable object from another thread. If the
930 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
933 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
934 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
935 * error will be returned. Other errors are possible too.
937 * Returns: (transfer full): A #GFileEnumerator if successful,
938 * %NULL on error. Free the returned object with g_object_unref().
941 g_file_enumerate_children (GFile *file,
942 const char *attributes,
943 GFileQueryInfoFlags flags,
944 GCancellable *cancellable,
949 g_return_val_if_fail (G_IS_FILE (file), NULL);
951 if (g_cancellable_set_error_if_cancelled (cancellable, error))
954 iface = G_FILE_GET_IFACE (file);
956 if (iface->enumerate_children == NULL)
958 g_set_error_literal (error, G_IO_ERROR,
959 G_IO_ERROR_NOT_SUPPORTED,
960 _("Operation not supported"));
964 return (* iface->enumerate_children) (file, attributes, flags,
969 * g_file_enumerate_children_async:
970 * @file: input #GFile
971 * @attributes: an attribute query string
972 * @flags: a set of #GFileQueryInfoFlags
973 * @io_priority: the <link linkend="io-priority">I/O priority</link>
975 * @cancellable: (allow-none): optional #GCancellable object,
977 * @callback: (scope async): a #GAsyncReadyCallback to call when the
978 * request is satisfied
979 * @user_data: (closure): the data to pass to callback function
981 * Asynchronously gets the requested information about the files
982 * in a directory. The result is a #GFileEnumerator object that will
983 * give out #GFileInfo objects for all the files in the directory.
985 * For more details, see g_file_enumerate_children() which is
986 * the synchronous version of this call.
988 * When the operation is finished, @callback will be called. You can
989 * then call g_file_enumerate_children_finish() to get the result of
993 g_file_enumerate_children_async (GFile *file,
994 const char *attributes,
995 GFileQueryInfoFlags flags,
997 GCancellable *cancellable,
998 GAsyncReadyCallback callback,
1003 g_return_if_fail (G_IS_FILE (file));
1005 iface = G_FILE_GET_IFACE (file);
1006 (* iface->enumerate_children_async) (file,
1016 * g_file_enumerate_children_finish:
1017 * @file: input #GFile
1018 * @res: a #GAsyncResult
1021 * Finishes an async enumerate children operation.
1022 * See g_file_enumerate_children_async().
1024 * Returns: (transfer full): a #GFileEnumerator or %NULL
1025 * if an error occurred.
1026 * Free the returned object with g_object_unref().
1029 g_file_enumerate_children_finish (GFile *file,
1035 g_return_val_if_fail (G_IS_FILE (file), NULL);
1036 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1038 if (g_async_result_legacy_propagate_error (res, error))
1041 iface = G_FILE_GET_IFACE (file);
1042 return (* iface->enumerate_children_finish) (file, res, error);
1046 * g_file_query_exists:
1047 * @file: input #GFile
1048 * @cancellable: (allow-none): optional #GCancellable object,
1051 * Utility function to check if a particular file exists. This is
1052 * implemented using g_file_query_info() and as such does blocking I/O.
1054 * Note that in many cases it is racy to first check for file existence
1055 * and then execute something based on the outcome of that, because the
1056 * file might have been created or removed in between the operations. The
1057 * general approach to handling that is to not check, but just do the
1058 * operation and handle the errors as they come.
1060 * As an example of race-free checking, take the case of reading a file,
1061 * and if it doesn't exist, creating it. There are two racy versions: read
1062 * it, and on error create it; and: check if it exists, if not create it.
1063 * These can both result in two processes creating the file (with perhaps
1064 * a partially written file as the result). The correct approach is to
1065 * always try to create the file with g_file_create() which will either
1066 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1068 * However, in many cases an existence check is useful in a user interface,
1069 * for instance to make a menu item sensitive/insensitive, so that you don't
1070 * have to fool users that something is possible and then just show an error
1071 * dialog. If you do this, you should make sure to also handle the errors
1072 * that can happen due to races when you execute the operation.
1074 * Returns: %TRUE if the file exists (and can be detected without error),
1075 * %FALSE otherwise (or if cancelled).
1078 g_file_query_exists (GFile *file,
1079 GCancellable *cancellable)
1083 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1085 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1086 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1089 g_object_unref (info);
1097 * g_file_query_file_type:
1098 * @file: input #GFile
1099 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1100 * @cancellable: (allow-none): optional #GCancellable object,
1103 * Utility function to inspect the #GFileType of a file. This is
1104 * implemented using g_file_query_info() and as such does blocking I/O.
1106 * The primary use case of this method is to check if a file is
1107 * a regular file, directory, or symlink.
1109 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1110 * if the file does not exist
1115 g_file_query_file_type (GFile *file,
1116 GFileQueryInfoFlags flags,
1117 GCancellable *cancellable)
1120 GFileType file_type;
1122 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1123 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1127 file_type = g_file_info_get_file_type (info);
1128 g_object_unref (info);
1131 file_type = G_FILE_TYPE_UNKNOWN;
1137 * g_file_query_info:
1138 * @file: input #GFile
1139 * @attributes: an attribute query string
1140 * @flags: a set of #GFileQueryInfoFlags
1141 * @cancellable: (allow-none): optional #GCancellable object,
1145 * Gets the requested information about specified @file.
1146 * The result is a #GFileInfo object that contains key-value
1147 * attributes (such as the type or size of the file).
1149 * The @attributes value is a string that specifies the file
1150 * attributes that should be gathered. It is not an error if
1151 * it's not possible to read a particular requested attribute
1152 * from a file - it just won't be set. @attributes should be a
1153 * comma-separated list of attributes or attribute wildcards.
1154 * The wildcard "*" means all attributes, and a wildcard like
1155 * "standard::*" means all attributes in the standard namespace.
1156 * An example attribute query be "standard::*,owner::user".
1157 * The standard attributes are available as defines, like
1158 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1160 * If @cancellable is not %NULL, then the operation can be cancelled
1161 * by triggering the cancellable object from another thread. If the
1162 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1165 * For symlinks, normally the information about the target of the
1166 * symlink is returned, rather than information about the symlink
1167 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1168 * in @flags the information about the symlink itself will be returned.
1169 * Also, for symlinks that point to non-existing files the information
1170 * about the symlink itself will be returned.
1172 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1173 * returned. Other errors are possible too, and depend on what kind of
1174 * filesystem the file is on.
1176 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1177 * on error. Free the returned object with g_object_unref().
1180 g_file_query_info (GFile *file,
1181 const char *attributes,
1182 GFileQueryInfoFlags flags,
1183 GCancellable *cancellable,
1188 g_return_val_if_fail (G_IS_FILE (file), NULL);
1190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1193 iface = G_FILE_GET_IFACE (file);
1195 if (iface->query_info == NULL)
1197 g_set_error_literal (error, G_IO_ERROR,
1198 G_IO_ERROR_NOT_SUPPORTED,
1199 _("Operation not supported"));
1203 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1207 * g_file_query_info_async:
1208 * @file: input #GFile
1209 * @attributes: an attribute query string
1210 * @flags: a set of #GFileQueryInfoFlags
1211 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1213 * @cancellable: (allow-none): optional #GCancellable object,
1215 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1216 * request is satisfied
1217 * @user_data: (closure): the data to pass to callback function
1219 * Asynchronously gets the requested information about specified @file.
1220 * The result is a #GFileInfo object that contains key-value attributes
1221 * (such as type or size for the file).
1223 * For more details, see g_file_query_info() which is the synchronous
1224 * version of this call.
1226 * When the operation is finished, @callback will be called. You can
1227 * then call g_file_query_info_finish() to get the result of the operation.
1230 g_file_query_info_async (GFile *file,
1231 const char *attributes,
1232 GFileQueryInfoFlags flags,
1234 GCancellable *cancellable,
1235 GAsyncReadyCallback callback,
1240 g_return_if_fail (G_IS_FILE (file));
1242 iface = G_FILE_GET_IFACE (file);
1243 (* iface->query_info_async) (file,
1253 * g_file_query_info_finish:
1254 * @file: input #GFile
1255 * @res: a #GAsyncResult
1258 * Finishes an asynchronous file info query.
1259 * See g_file_query_info_async().
1261 * Returns: (transfer full): #GFileInfo for given @file
1262 * or %NULL on error. Free the returned object with
1266 g_file_query_info_finish (GFile *file,
1272 g_return_val_if_fail (G_IS_FILE (file), NULL);
1273 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1275 if (g_async_result_legacy_propagate_error (res, error))
1278 iface = G_FILE_GET_IFACE (file);
1279 return (* iface->query_info_finish) (file, res, error);
1283 * g_file_query_filesystem_info:
1284 * @file: input #GFile
1285 * @attributes: an attribute query string
1286 * @cancellable: (allow-none): optional #GCancellable object,
1290 * Similar to g_file_query_info(), but obtains information
1291 * about the filesystem the @file is on, rather than the file itself.
1292 * For instance the amount of space available and the type of
1295 * The @attributes value is a string that specifies the attributes
1296 * that should be gathered. It is not an error if it's not possible
1297 * to read a particular requested attribute from a file - it just
1298 * won't be set. @attributes should be a comma-separated list of
1299 * attributes or attribute wildcards. The wildcard "*" means all
1300 * attributes, and a wildcard like "filesystem::*" means all attributes
1301 * in the filesystem namespace. The standard namespace for filesystem
1302 * attributes is "filesystem". Common attributes of interest are
1303 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1304 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1305 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1307 * If @cancellable is not %NULL, then the operation can be cancelled
1308 * by triggering the cancellable object from another thread. If the
1309 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1312 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1313 * be returned. Other errors are possible too, and depend on what
1314 * kind of filesystem the file is on.
1316 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1317 * Free the returned object with g_object_unref().
1320 g_file_query_filesystem_info (GFile *file,
1321 const char *attributes,
1322 GCancellable *cancellable,
1327 g_return_val_if_fail (G_IS_FILE (file), NULL);
1329 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1332 iface = G_FILE_GET_IFACE (file);
1334 if (iface->query_filesystem_info == NULL)
1336 g_set_error_literal (error, G_IO_ERROR,
1337 G_IO_ERROR_NOT_SUPPORTED,
1338 _("Operation not supported"));
1342 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1346 * g_file_query_filesystem_info_async:
1347 * @file: input #GFile
1348 * @attributes: an attribute query string
1349 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1351 * @cancellable: (allow-none): optional #GCancellable object,
1353 * @callback: (scope async): a #GAsyncReadyCallback to call
1354 * when the request is satisfied
1355 * @user_data: (closure): the data to pass to callback function
1357 * Asynchronously gets the requested information about the filesystem
1358 * that the specified @file is on. The result is a #GFileInfo object
1359 * that contains key-value attributes (such as type or size for the
1362 * For more details, see g_file_query_filesystem_info() which is the
1363 * synchronous version of this call.
1365 * When the operation is finished, @callback will be called. You can
1366 * then call g_file_query_info_finish() to get the result of the
1370 g_file_query_filesystem_info_async (GFile *file,
1371 const char *attributes,
1373 GCancellable *cancellable,
1374 GAsyncReadyCallback callback,
1379 g_return_if_fail (G_IS_FILE (file));
1381 iface = G_FILE_GET_IFACE (file);
1382 (* iface->query_filesystem_info_async) (file,
1391 * g_file_query_filesystem_info_finish:
1392 * @file: input #GFile
1393 * @res: a #GAsyncResult
1396 * Finishes an asynchronous filesystem info query.
1397 * See g_file_query_filesystem_info_async().
1399 * Returns: (transfer full): #GFileInfo for given @file
1400 * or %NULL on error.
1401 * Free the returned object with g_object_unref().
1404 g_file_query_filesystem_info_finish (GFile *file,
1410 g_return_val_if_fail (G_IS_FILE (file), NULL);
1411 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1413 if (g_async_result_legacy_propagate_error (res, error))
1416 iface = G_FILE_GET_IFACE (file);
1417 return (* iface->query_filesystem_info_finish) (file, res, error);
1421 * g_file_find_enclosing_mount:
1422 * @file: input #GFile
1423 * @cancellable: (allow-none): optional #GCancellable object,
1427 * Gets a #GMount for the #GFile.
1429 * If the #GFileIface for @file does not have a mount (e.g.
1430 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1431 * and %NULL will be returned.
1433 * If @cancellable is not %NULL, then the operation can be cancelled by
1434 * triggering the cancellable object from another thread. If the operation
1435 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1437 * Returns: (transfer full): a #GMount where the @file is located
1438 * or %NULL on error.
1439 * Free the returned object with g_object_unref().
1442 g_file_find_enclosing_mount (GFile *file,
1443 GCancellable *cancellable,
1448 g_return_val_if_fail (G_IS_FILE (file), NULL);
1450 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1453 iface = G_FILE_GET_IFACE (file);
1454 if (iface->find_enclosing_mount == NULL)
1457 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1458 /* Translators: This is an error message when
1459 * trying to find the enclosing (user visible)
1460 * mount of a file, but none exists.
1462 _("Containing mount does not exist"));
1466 return (* iface->find_enclosing_mount) (file, cancellable, error);
1470 * g_file_find_enclosing_mount_async:
1472 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1474 * @cancellable: (allow-none): optional #GCancellable object,
1476 * @callback: (scope async): a #GAsyncReadyCallback to call
1477 * when the request is satisfied
1478 * @user_data: (closure): the data to pass to callback function
1480 * Asynchronously gets the mount for the file.
1482 * For more details, see g_file_find_enclosing_mount() which is
1483 * the synchronous version of this call.
1485 * When the operation is finished, @callback will be called.
1486 * You can then call g_file_find_enclosing_mount_finish() to
1487 * get the result of the operation.
1490 g_file_find_enclosing_mount_async (GFile *file,
1492 GCancellable *cancellable,
1493 GAsyncReadyCallback callback,
1498 g_return_if_fail (G_IS_FILE (file));
1500 iface = G_FILE_GET_IFACE (file);
1501 (* iface->find_enclosing_mount_async) (file,
1509 * g_file_find_enclosing_mount_finish:
1511 * @res: a #GAsyncResult
1514 * Finishes an asynchronous find mount request.
1515 * See g_file_find_enclosing_mount_async().
1517 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1518 * Free the returned object with g_object_unref().
1521 g_file_find_enclosing_mount_finish (GFile *file,
1527 g_return_val_if_fail (G_IS_FILE (file), NULL);
1528 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1530 if (g_async_result_legacy_propagate_error (res, error))
1533 iface = G_FILE_GET_IFACE (file);
1534 return (* iface->find_enclosing_mount_finish) (file, res, error);
1540 * @file: #GFile to read
1541 * @cancellable: (allow-none): a #GCancellable
1542 * @error: a #GError, or %NULL
1544 * Opens a file for reading. The result is a #GFileInputStream that
1545 * can be used to read the contents of the file.
1547 * If @cancellable is not %NULL, then the operation can be cancelled by
1548 * triggering the cancellable object from another thread. If the operation
1549 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1551 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1552 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1553 * error will be returned. Other errors are possible too, and depend
1554 * on what kind of filesystem the file is on.
1557 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1558 * Free the returned object with g_object_unref().
1561 g_file_read (GFile *file,
1562 GCancellable *cancellable,
1567 g_return_val_if_fail (G_IS_FILE (file), NULL);
1569 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1572 iface = G_FILE_GET_IFACE (file);
1574 if (iface->read_fn == NULL)
1576 g_set_error_literal (error, G_IO_ERROR,
1577 G_IO_ERROR_NOT_SUPPORTED,
1578 _("Operation not supported"));
1582 return (* iface->read_fn) (file, cancellable, error);
1587 * @file: input #GFile
1588 * @flags: a set of #GFileCreateFlags
1589 * @cancellable: (allow-none): optional #GCancellable object,
1591 * @error: a #GError, or %NULL
1593 * Gets an output stream for appending data to the file.
1594 * If the file doesn't already exist it is created.
1596 * By default files created are generally readable by everyone,
1597 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1598 * will be made readable only to the current user, to the level that
1599 * is supported on the target filesystem.
1601 * If @cancellable is not %NULL, then the operation can be cancelled
1602 * by triggering the cancellable object from another thread. If the
1603 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1606 * Some file systems don't allow all file names, and may return an
1607 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1608 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1609 * possible too, and depend on what kind of filesystem the file is on.
1611 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1612 * Free the returned object with g_object_unref().
1615 g_file_append_to (GFile *file,
1616 GFileCreateFlags flags,
1617 GCancellable *cancellable,
1622 g_return_val_if_fail (G_IS_FILE (file), NULL);
1624 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1627 iface = G_FILE_GET_IFACE (file);
1629 if (iface->append_to == NULL)
1631 g_set_error_literal (error, G_IO_ERROR,
1632 G_IO_ERROR_NOT_SUPPORTED,
1633 _("Operation not supported"));
1637 return (* iface->append_to) (file, flags, cancellable, error);
1642 * @file: input #GFile
1643 * @flags: a set of #GFileCreateFlags
1644 * @cancellable: (allow-none): optional #GCancellable object,
1646 * @error: a #GError, or %NULL
1648 * Creates a new file and returns an output stream for writing to it.
1649 * The file must not already exist.
1651 * By default files created are generally readable by everyone,
1652 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1653 * will be made readable only to the current user, to the level
1654 * that is supported on the target filesystem.
1656 * If @cancellable is not %NULL, then the operation can be cancelled
1657 * by triggering the cancellable object from another thread. If the
1658 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1661 * If a file or directory with this name already exists the
1662 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1663 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1664 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1665 * be returned. Other errors are possible too, and depend on what kind
1666 * of filesystem the file is on.
1668 * Returns: (transfer full): a #GFileOutputStream for the newly created
1669 * file, or %NULL on error.
1670 * Free the returned object with g_object_unref().
1673 g_file_create (GFile *file,
1674 GFileCreateFlags flags,
1675 GCancellable *cancellable,
1680 g_return_val_if_fail (G_IS_FILE (file), NULL);
1682 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1685 iface = G_FILE_GET_IFACE (file);
1687 if (iface->create == NULL)
1689 g_set_error_literal (error, G_IO_ERROR,
1690 G_IO_ERROR_NOT_SUPPORTED,
1691 _("Operation not supported"));
1695 return (* iface->create) (file, flags, cancellable, error);
1700 * @file: input #GFile
1701 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1702 * for the current #GFile, or #NULL to ignore
1703 * @make_backup: %TRUE if a backup should be created
1704 * @flags: a set of #GFileCreateFlags
1705 * @cancellable: (allow-none): optional #GCancellable object,
1707 * @error: a #GError, or %NULL
1709 * Returns an output stream for overwriting the file, possibly
1710 * creating a backup copy of the file first. If the file doesn't exist,
1711 * it will be created.
1713 * This will try to replace the file in the safest way possible so
1714 * that any errors during the writing will not affect an already
1715 * existing copy of the file. For instance, for local files it
1716 * may write to a temporary file and then atomically rename over
1717 * the destination when the stream is closed.
1719 * By default files created are generally readable by everyone,
1720 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1721 * will be made readable only to the current user, to the level that
1722 * is supported on the target filesystem.
1724 * If @cancellable is not %NULL, then the operation can be cancelled
1725 * by triggering the cancellable object from another thread. If the
1726 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1729 * If you pass in a non-%NULL @etag value, then this value is
1730 * compared to the current entity tag of the file, and if they differ
1731 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1732 * that the file has been changed since you last read it. You can get
1733 * the new etag from g_file_output_stream_get_etag() after you've
1734 * finished writing and closed the #GFileOutputStream. When you load
1735 * a new file you can use g_file_input_stream_query_info() to get
1736 * the etag of the file.
1738 * If @make_backup is %TRUE, this function will attempt to make a
1739 * backup of the current file before overwriting it. If this fails
1740 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1741 * want to replace anyway, try again with @make_backup set to %FALSE.
1743 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1744 * be returned, and if the file is some other form of non-regular file
1745 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1746 * file systems don't allow all file names, and may return an
1747 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1748 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1749 * possible too, and depend on what kind of filesystem the file is on.
1751 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1752 * Free the returned object with g_object_unref().
1755 g_file_replace (GFile *file,
1757 gboolean make_backup,
1758 GFileCreateFlags flags,
1759 GCancellable *cancellable,
1764 g_return_val_if_fail (G_IS_FILE (file), NULL);
1766 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1769 iface = G_FILE_GET_IFACE (file);
1771 if (iface->replace == NULL)
1773 g_set_error_literal (error, G_IO_ERROR,
1774 G_IO_ERROR_NOT_SUPPORTED,
1775 _("Operation not supported"));
1779 /* Handle empty tag string as NULL in consistent way. */
1780 if (etag && *etag == 0)
1783 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1787 * g_file_open_readwrite:
1788 * @file: #GFile to open
1789 * @cancellable: (allow-none): a #GCancellable
1790 * @error: a #GError, or %NULL
1792 * Opens an existing file for reading and writing. The result is
1793 * a #GFileIOStream that can be used to read and write the contents
1796 * If @cancellable is not %NULL, then the operation can be cancelled
1797 * by triggering the cancellable object from another thread. If the
1798 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1801 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1802 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1803 * error will be returned. Other errors are possible too, and depend on
1804 * what kind of filesystem the file is on. Note that in many non-local
1805 * file cases read and write streams are not supported, so make sure you
1806 * really need to do read and write streaming, rather than just opening
1807 * for reading or writing.
1809 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1810 * Free the returned object with g_object_unref().
1815 g_file_open_readwrite (GFile *file,
1816 GCancellable *cancellable,
1821 g_return_val_if_fail (G_IS_FILE (file), NULL);
1823 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1826 iface = G_FILE_GET_IFACE (file);
1828 if (iface->open_readwrite == NULL)
1830 g_set_error_literal (error, G_IO_ERROR,
1831 G_IO_ERROR_NOT_SUPPORTED,
1832 _("Operation not supported"));
1836 return (* iface->open_readwrite) (file, cancellable, error);
1840 * g_file_create_readwrite:
1842 * @flags: a set of #GFileCreateFlags
1843 * @cancellable: (allow-none): optional #GCancellable object,
1845 * @error: return location for a #GError, or %NULL
1847 * Creates a new file and returns a stream for reading and
1848 * writing to it. The file must not already exist.
1850 * By default files created are generally readable by everyone,
1851 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1852 * will be made readable only to the current user, to the level
1853 * that is supported on the target filesystem.
1855 * If @cancellable is not %NULL, then the operation can be cancelled
1856 * by triggering the cancellable object from another thread. If the
1857 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1860 * If a file or directory with this name already exists, the
1861 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1862 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1863 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1864 * will be returned. Other errors are possible too, and depend on what
1865 * kind of filesystem the file is on.
1867 * Note that in many non-local file cases read and write streams are
1868 * not supported, so make sure you really need to do read and write
1869 * streaming, rather than just opening for reading or writing.
1871 * Returns: (transfer full): a #GFileIOStream for the newly created
1872 * file, or %NULL on error.
1873 * Free the returned object with g_object_unref().
1878 g_file_create_readwrite (GFile *file,
1879 GFileCreateFlags flags,
1880 GCancellable *cancellable,
1885 g_return_val_if_fail (G_IS_FILE (file), NULL);
1887 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1890 iface = G_FILE_GET_IFACE (file);
1892 if (iface->create_readwrite == NULL)
1894 g_set_error_literal (error, G_IO_ERROR,
1895 G_IO_ERROR_NOT_SUPPORTED,
1896 _("Operation not supported"));
1900 return (* iface->create_readwrite) (file, flags, cancellable, error);
1904 * g_file_replace_readwrite:
1906 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1907 * for the current #GFile, or #NULL to ignore
1908 * @make_backup: %TRUE if a backup should be created
1909 * @flags: a set of #GFileCreateFlags
1910 * @cancellable: (allow-none): optional #GCancellable object,
1912 * @error: return location for a #GError, or %NULL
1914 * Returns an output stream for overwriting the file in readwrite mode,
1915 * possibly creating a backup copy of the file first. If the file doesn't
1916 * exist, it will be created.
1918 * For details about the behaviour, see g_file_replace() which does the
1919 * same thing but returns an output stream only.
1921 * Note that in many non-local file cases read and write streams are not
1922 * supported, so make sure you really need to do read and write streaming,
1923 * rather than just opening for reading or writing.
1925 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1926 * Free the returned object with g_object_unref().
1931 g_file_replace_readwrite (GFile *file,
1933 gboolean make_backup,
1934 GFileCreateFlags flags,
1935 GCancellable *cancellable,
1940 g_return_val_if_fail (G_IS_FILE (file), NULL);
1942 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1945 iface = G_FILE_GET_IFACE (file);
1947 if (iface->replace_readwrite == NULL)
1949 g_set_error_literal (error, G_IO_ERROR,
1950 G_IO_ERROR_NOT_SUPPORTED,
1951 _("Operation not supported"));
1955 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1959 * g_file_read_async:
1960 * @file: input #GFile
1961 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1963 * @cancellable: (allow-none): optional #GCancellable object,
1965 * @callback: (scope async): a #GAsyncReadyCallback to call
1966 * when the request is satisfied
1967 * @user_data: (closure): the data to pass to callback function
1969 * Asynchronously opens @file for reading.
1971 * For more details, see g_file_read() which is
1972 * the synchronous version of this call.
1974 * When the operation is finished, @callback will be called.
1975 * You can then call g_file_read_finish() to get the result
1979 g_file_read_async (GFile *file,
1981 GCancellable *cancellable,
1982 GAsyncReadyCallback callback,
1987 g_return_if_fail (G_IS_FILE (file));
1989 iface = G_FILE_GET_IFACE (file);
1990 (* iface->read_async) (file,
1998 * g_file_read_finish:
1999 * @file: input #GFile
2000 * @res: a #GAsyncResult
2001 * @error: a #GError, or %NULL
2003 * Finishes an asynchronous file read operation started with
2004 * g_file_read_async().
2006 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2007 * Free the returned object with g_object_unref().
2010 g_file_read_finish (GFile *file,
2016 g_return_val_if_fail (G_IS_FILE (file), NULL);
2017 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2019 if (g_async_result_legacy_propagate_error (res, error))
2022 iface = G_FILE_GET_IFACE (file);
2023 return (* iface->read_finish) (file, res, error);
2027 * g_file_append_to_async:
2028 * @file: input #GFile
2029 * @flags: a set of #GFileCreateFlags
2030 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2032 * @cancellable: (allow-none): optional #GCancellable object,
2034 * @callback: (scope async): a #GAsyncReadyCallback to call
2035 * when the request is satisfied
2036 * @user_data: (closure): the data to pass to callback function
2038 * Asynchronously opens @file for appending.
2040 * For more details, see g_file_append_to() which is
2041 * the synchronous version of this call.
2043 * When the operation is finished, @callback will be called.
2044 * You can then call g_file_append_to_finish() to get the result
2048 g_file_append_to_async (GFile *file,
2049 GFileCreateFlags flags,
2051 GCancellable *cancellable,
2052 GAsyncReadyCallback callback,
2057 g_return_if_fail (G_IS_FILE (file));
2059 iface = G_FILE_GET_IFACE (file);
2060 (* iface->append_to_async) (file,
2069 * g_file_append_to_finish:
2070 * @file: input #GFile
2071 * @res: #GAsyncResult
2072 * @error: a #GError, or %NULL
2074 * Finishes an asynchronous file append operation started with
2075 * g_file_append_to_async().
2077 * Returns: (transfer full): a valid #GFileOutputStream
2078 * or %NULL on error.
2079 * Free the returned object with g_object_unref().
2082 g_file_append_to_finish (GFile *file,
2088 g_return_val_if_fail (G_IS_FILE (file), NULL);
2089 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2091 if (g_async_result_legacy_propagate_error (res, error))
2094 iface = G_FILE_GET_IFACE (file);
2095 return (* iface->append_to_finish) (file, res, error);
2099 * g_file_create_async:
2100 * @file: input #GFile
2101 * @flags: a set of #GFileCreateFlags
2102 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2104 * @cancellable: (allow-none): optional #GCancellable object,
2106 * @callback: (scope async): a #GAsyncReadyCallback to call
2107 * when the request is satisfied
2108 * @user_data: (closure): the data to pass to callback function
2110 * Asynchronously creates a new file and returns an output stream
2111 * for writing to it. The file must not already exist.
2113 * For more details, see g_file_create() which is
2114 * the synchronous version of this call.
2116 * When the operation is finished, @callback will be called.
2117 * You can then call g_file_create_finish() to get the result
2121 g_file_create_async (GFile *file,
2122 GFileCreateFlags flags,
2124 GCancellable *cancellable,
2125 GAsyncReadyCallback callback,
2130 g_return_if_fail (G_IS_FILE (file));
2132 iface = G_FILE_GET_IFACE (file);
2133 (* iface->create_async) (file,
2142 * g_file_create_finish:
2143 * @file: input #GFile
2144 * @res: a #GAsyncResult
2145 * @error: a #GError, or %NULL
2147 * Finishes an asynchronous file create operation started with
2148 * g_file_create_async().
2150 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2151 * Free the returned object with g_object_unref().
2154 g_file_create_finish (GFile *file,
2160 g_return_val_if_fail (G_IS_FILE (file), NULL);
2161 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2163 if (g_async_result_legacy_propagate_error (res, error))
2166 iface = G_FILE_GET_IFACE (file);
2167 return (* iface->create_finish) (file, res, error);
2171 * g_file_replace_async:
2172 * @file: input #GFile
2173 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2174 * for the current #GFile, or NULL to ignore
2175 * @make_backup: %TRUE if a backup should be created
2176 * @flags: a set of #GFileCreateFlags
2177 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2179 * @cancellable: (allow-none): optional #GCancellable object,
2181 * @callback: (scope async): a #GAsyncReadyCallback to call
2182 * when the request is satisfied
2183 * @user_data: (closure): the data to pass to callback function
2185 * Asynchronously overwrites the file, replacing the contents,
2186 * possibly creating a backup copy of the file first.
2188 * For more details, see g_file_replace() which is
2189 * the synchronous version of this call.
2191 * When the operation is finished, @callback will be called.
2192 * You can then call g_file_replace_finish() to get the result
2196 g_file_replace_async (GFile *file,
2198 gboolean make_backup,
2199 GFileCreateFlags flags,
2201 GCancellable *cancellable,
2202 GAsyncReadyCallback callback,
2207 g_return_if_fail (G_IS_FILE (file));
2209 iface = G_FILE_GET_IFACE (file);
2210 (* iface->replace_async) (file,
2221 * g_file_replace_finish:
2222 * @file: input #GFile
2223 * @res: a #GAsyncResult
2224 * @error: a #GError, or %NULL
2226 * Finishes an asynchronous file replace operation started with
2227 * g_file_replace_async().
2229 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2230 * Free the returned object with g_object_unref().
2233 g_file_replace_finish (GFile *file,
2239 g_return_val_if_fail (G_IS_FILE (file), NULL);
2240 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2242 if (g_async_result_legacy_propagate_error (res, error))
2245 iface = G_FILE_GET_IFACE (file);
2246 return (* iface->replace_finish) (file, res, error);
2250 * g_file_open_readwrite_async
2251 * @file: input #GFile
2252 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2254 * @cancellable: (allow-none): optional #GCancellable object,
2256 * @callback: (scope async): a #GAsyncReadyCallback to call
2257 * when the request is satisfied
2258 * @user_data: (closure): the data to pass to callback function
2260 * Asynchronously opens @file for reading and writing.
2262 * For more details, see g_file_open_readwrite() which is
2263 * the synchronous version of this call.
2265 * When the operation is finished, @callback will be called.
2266 * You can then call g_file_open_readwrite_finish() to get
2267 * the result of the operation.
2272 g_file_open_readwrite_async (GFile *file,
2274 GCancellable *cancellable,
2275 GAsyncReadyCallback callback,
2280 g_return_if_fail (G_IS_FILE (file));
2282 iface = G_FILE_GET_IFACE (file);
2283 (* iface->open_readwrite_async) (file,
2291 * g_file_open_readwrite_finish:
2292 * @file: input #GFile
2293 * @res: a #GAsyncResult
2294 * @error: a #GError, or %NULL
2296 * Finishes an asynchronous file read operation started with
2297 * g_file_open_readwrite_async().
2299 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2300 * Free the returned object with g_object_unref().
2305 g_file_open_readwrite_finish (GFile *file,
2311 g_return_val_if_fail (G_IS_FILE (file), NULL);
2312 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2314 if (g_async_result_legacy_propagate_error (res, error))
2317 iface = G_FILE_GET_IFACE (file);
2318 return (* iface->open_readwrite_finish) (file, res, error);
2322 * g_file_create_readwrite_async:
2323 * @file: input #GFile
2324 * @flags: a set of #GFileCreateFlags
2325 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2327 * @cancellable: (allow-none): optional #GCancellable object,
2329 * @callback: (scope async): a #GAsyncReadyCallback to call
2330 * when the request is satisfied
2331 * @user_data: (closure): the data to pass to callback function
2333 * Asynchronously creates a new file and returns a stream
2334 * for reading and writing to it. The file must not already exist.
2336 * For more details, see g_file_create_readwrite() which is
2337 * the synchronous version of this call.
2339 * When the operation is finished, @callback will be called.
2340 * You can then call g_file_create_readwrite_finish() to get
2341 * the result of the operation.
2346 g_file_create_readwrite_async (GFile *file,
2347 GFileCreateFlags flags,
2349 GCancellable *cancellable,
2350 GAsyncReadyCallback callback,
2355 g_return_if_fail (G_IS_FILE (file));
2357 iface = G_FILE_GET_IFACE (file);
2358 (* iface->create_readwrite_async) (file,
2367 * g_file_create_readwrite_finish:
2368 * @file: input #GFile
2369 * @res: a #GAsyncResult
2370 * @error: a #GError, or %NULL
2372 * Finishes an asynchronous file create operation started with
2373 * g_file_create_readwrite_async().
2375 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2376 * Free the returned object with g_object_unref().
2381 g_file_create_readwrite_finish (GFile *file,
2387 g_return_val_if_fail (G_IS_FILE (file), NULL);
2388 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2390 if (g_async_result_legacy_propagate_error (res, error))
2393 iface = G_FILE_GET_IFACE (file);
2394 return (* iface->create_readwrite_finish) (file, res, error);
2398 * g_file_replace_readwrite_async:
2399 * @file: input #GFile
2400 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2401 * for the current #GFile, or NULL to ignore
2402 * @make_backup: %TRUE if a backup should be created
2403 * @flags: a set of #GFileCreateFlags
2404 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2406 * @cancellable: (allow-none): optional #GCancellable object,
2408 * @callback: (scope async): a #GAsyncReadyCallback to call
2409 * when the request is satisfied
2410 * @user_data: (closure): the data to pass to callback function
2412 * Asynchronously overwrites the file in read-write mode,
2413 * replacing the contents, possibly creating a backup copy
2414 * of the file first.
2416 * For more details, see g_file_replace_readwrite() which is
2417 * the synchronous version of this call.
2419 * When the operation is finished, @callback will be called.
2420 * You can then call g_file_replace_readwrite_finish() to get
2421 * the result of the operation.
2426 g_file_replace_readwrite_async (GFile *file,
2428 gboolean make_backup,
2429 GFileCreateFlags flags,
2431 GCancellable *cancellable,
2432 GAsyncReadyCallback callback,
2437 g_return_if_fail (G_IS_FILE (file));
2439 iface = G_FILE_GET_IFACE (file);
2440 (* iface->replace_readwrite_async) (file,
2451 * g_file_replace_readwrite_finish:
2452 * @file: input #GFile
2453 * @res: a #GAsyncResult
2454 * @error: a #GError, or %NULL
2456 * Finishes an asynchronous file replace operation started with
2457 * g_file_replace_readwrite_async().
2459 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2460 * Free the returned object with g_object_unref().
2465 g_file_replace_readwrite_finish (GFile *file,
2471 g_return_val_if_fail (G_IS_FILE (file), NULL);
2472 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2474 if (g_async_result_legacy_propagate_error (res, error))
2477 iface = G_FILE_GET_IFACE (file);
2478 return (* iface->replace_readwrite_finish) (file, res, error);
2482 copy_symlink (GFile *destination,
2483 GFileCopyFlags flags,
2484 GCancellable *cancellable,
2489 gboolean tried_delete;
2491 GFileType file_type;
2493 tried_delete = FALSE;
2497 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2499 /* Maybe it already existed, and we want to overwrite? */
2500 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2501 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2503 g_clear_error (&my_error);
2505 /* Don't overwrite if the destination is a directory */
2506 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2507 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2508 cancellable, &my_error);
2511 file_type = g_file_info_get_file_type (info);
2512 g_object_unref (info);
2514 if (file_type == G_FILE_TYPE_DIRECTORY)
2516 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2517 _("Can't copy over directory"));
2522 if (!g_file_delete (destination, cancellable, error))
2525 tried_delete = TRUE;
2529 g_propagate_error (error, my_error);
2536 static GFileInputStream *
2537 open_source_for_copy (GFile *source,
2539 GFileCopyFlags flags,
2540 GCancellable *cancellable,
2544 GFileInputStream *ret;
2546 GFileType file_type;
2549 ret = g_file_read (source, cancellable, &my_error);
2553 /* There was an error opening the source, try to set a good error for it: */
2554 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2556 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2557 * as that is less useful to the app. Better check for errors on the
2560 g_error_free (my_error);
2563 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2564 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2565 cancellable, &my_error);
2567 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2569 file_type = g_file_info_get_file_type (info);
2570 g_object_unref (info);
2572 if (flags & G_FILE_COPY_OVERWRITE)
2574 if (file_type == G_FILE_TYPE_DIRECTORY)
2576 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2577 _("Can't copy directory over directory"));
2580 /* continue to would_recurse error */
2584 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2585 _("Target file exists"));
2591 /* Error getting info from target, return that error
2592 * (except for NOT_FOUND, which is no error here)
2594 g_clear_object (&info);
2595 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2597 g_propagate_error (error, my_error);
2600 g_clear_error (&my_error);
2603 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2604 _("Can't recursively copy directory"));
2608 g_propagate_error (error, my_error);
2613 should_copy (GFileAttributeInfo *info,
2614 gboolean copy_all_attributes,
2615 gboolean skip_perms)
2617 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2620 if (copy_all_attributes)
2621 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2622 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2626 build_attribute_list_for_copy (GFile *file,
2627 GFileCopyFlags flags,
2628 char **out_attributes,
2629 GCancellable *cancellable,
2632 gboolean ret = FALSE;
2633 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2637 gboolean copy_all_attributes;
2638 gboolean skip_perms;
2640 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2641 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2643 /* Ignore errors here, if the target supports no attributes there is
2644 * nothing to copy. We still honor the cancellable though.
2646 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2647 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2650 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2651 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2654 if (attributes == NULL && namespaces == NULL)
2658 s = g_string_new ("");
2662 for (i = 0; i < attributes->n_infos; i++)
2664 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2669 g_string_append_c (s, ',');
2671 g_string_append (s, attributes->infos[i].name);
2678 for (i = 0; i < namespaces->n_infos; i++)
2680 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2685 g_string_append_c (s, ',');
2687 g_string_append (s, namespaces->infos[i].name);
2688 g_string_append (s, "::*");
2694 *out_attributes = g_string_free (s, FALSE);
2698 g_string_free (s, TRUE);
2700 g_file_attribute_info_list_unref (attributes);
2702 g_file_attribute_info_list_unref (namespaces);
2708 * g_file_copy_attributes:
2709 * @source: a #GFile with attributes
2710 * @destination: a #GFile to copy attributes to
2711 * @flags: a set of #GFileCopyFlags
2712 * @cancellable: (allow-none): optional #GCancellable object,
2714 * @error: a #GError, %NULL to ignore
2716 * Copies the file attributes from @source to @destination.
2718 * Normally only a subset of the file attributes are copied,
2719 * those that are copies in a normal file copy operation
2720 * (which for instance does not include e.g. owner). However
2721 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2722 * all the metadata that is possible to copy is copied. This
2723 * is useful when implementing move by copy + delete source.
2725 * Returns: %TRUE if the attributes were copied successfully,
2729 g_file_copy_attributes (GFile *source,
2731 GFileCopyFlags flags,
2732 GCancellable *cancellable,
2735 char *attrs_to_read;
2738 gboolean source_nofollow_symlinks;
2740 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2741 cancellable, error))
2744 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2746 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2747 * we just don't copy it.
2749 info = g_file_query_info (source, attrs_to_read,
2750 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2754 g_free (attrs_to_read);
2759 res = g_file_set_attributes_from_info (destination,
2761 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2764 g_object_unref (info);
2771 copy_stream_with_progress (GInputStream *in,
2774 GCancellable *cancellable,
2775 GFileProgressCallback progress_callback,
2776 gpointer progress_callback_data,
2779 gssize n_read, n_written;
2780 goffset current_size;
2781 char buffer[1024*64], *p;
2787 /* avoid performance impact of querying total size when it's not needed */
2788 if (progress_callback)
2790 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2791 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2795 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2796 total_size = g_file_info_get_size (info);
2797 g_object_unref (info);
2800 if (total_size == -1)
2802 info = g_file_query_info (source,
2803 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2804 G_FILE_QUERY_INFO_NONE,
2808 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2809 total_size = g_file_info_get_size (info);
2810 g_object_unref (info);
2815 if (total_size == -1)
2822 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2832 current_size += n_read;
2837 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2838 if (n_written == -1)
2845 n_read -= n_written;
2851 if (progress_callback)
2852 progress_callback (current_size, total_size, progress_callback_data);
2855 /* Make sure we send full copied size */
2856 if (progress_callback)
2857 progress_callback (current_size, total_size, progress_callback_data);
2865 do_splice (int fd_in,
2870 long *bytes_transferd,
2876 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2884 else if (errsv == ENOSYS || errsv == EINVAL)
2885 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2886 _("Splice not supported"));
2888 g_set_error (error, G_IO_ERROR,
2889 g_io_error_from_errno (errsv),
2890 _("Error splicing file: %s"),
2891 g_strerror (errsv));
2896 *bytes_transferd = result;
2901 splice_stream_with_progress (GInputStream *in,
2903 GCancellable *cancellable,
2904 GFileProgressCallback progress_callback,
2905 gpointer progress_callback_data,
2908 int buffer[2] = { -1, -1 };
2915 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2916 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2918 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2922 /* avoid performance impact of querying total size when it's not needed */
2923 if (progress_callback)
2927 if (fstat (fd_in, &sbuf) == 0)
2928 total_size = sbuf.st_size;
2931 if (total_size == -1)
2934 offset_in = offset_out = 0;
2941 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2944 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2955 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2958 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2961 n_read -= n_written;
2964 if (progress_callback)
2965 progress_callback (offset_in, total_size, progress_callback_data);
2968 /* Make sure we send full copied size */
2969 if (progress_callback)
2970 progress_callback (offset_in, total_size, progress_callback_data);
2972 if (!g_close (buffer[0], error))
2975 if (!g_close (buffer[1], error))
2979 if (buffer[0] != -1)
2980 (void) g_close (buffer[0], NULL);
2981 if (buffer[1] != -1)
2982 (void) g_close (buffer[1], NULL);
2990 btrfs_reflink_with_progress (GInputStream *in,
2993 GCancellable *cancellable,
2994 GFileProgressCallback progress_callback,
2995 gpointer progress_callback_data,
2998 goffset source_size;
3002 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3003 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3005 if (progress_callback)
3006 source_size = g_file_info_get_size (info);
3008 /* Btrfs clone ioctl properties:
3009 * - Works at the inode level
3010 * - Doesn't work with directories
3011 * - Always follows symlinks (source and destination)
3013 * By the time we get here, *in and *out are both regular files */
3014 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3019 g_set_error_literal (error, G_IO_ERROR,
3020 G_IO_ERROR_NOT_SUPPORTED,
3021 _("Copy (reflink/clone) between mounts is not supported"));
3022 else if (errno == EINVAL)
3023 g_set_error_literal (error, G_IO_ERROR,
3024 G_IO_ERROR_NOT_SUPPORTED,
3025 _("Copy (reflink/clone) is not supported or invalid"));
3027 /* Most probably something odd happened; retry with fallback */
3028 g_set_error_literal (error, G_IO_ERROR,
3029 G_IO_ERROR_NOT_SUPPORTED,
3030 _("Copy (reflink/clone) is not supported or didn't work"));
3031 /* We retry with fallback for all error cases because Btrfs is currently
3032 * unstable, and so we can't trust it to do clone properly.
3033 * In addition, any hard errors here would cause the same failure in the
3034 * fallback manual copy as well. */
3038 /* Make sure we send full copied size */
3039 if (progress_callback)
3040 progress_callback (source_size, source_size, progress_callback_data);
3047 file_copy_fallback (GFile *source,
3049 GFileCopyFlags flags,
3050 GCancellable *cancellable,
3051 GFileProgressCallback progress_callback,
3052 gpointer progress_callback_data,
3055 gboolean ret = FALSE;
3056 GFileInputStream *file_in = NULL;
3057 GInputStream *in = NULL;
3058 GOutputStream *out = NULL;
3059 GFileInfo *info = NULL;
3061 char *attrs_to_read;
3062 gboolean do_set_attributes = FALSE;
3064 /* need to know the file type */
3065 info = g_file_query_info (source,
3066 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3067 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3073 /* Maybe copy the symlink? */
3074 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3075 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3077 target = g_file_info_get_symlink_target (info);
3080 if (!copy_symlink (destination, flags, cancellable, target, error))
3086 /* ... else fall back on a regular file copy */
3088 /* Handle "special" files (pipes, device nodes, ...)? */
3089 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3091 /* FIXME: could try to recreate device nodes and others? */
3092 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3093 _("Can't copy special file"));
3097 /* Everything else should just fall back on a regular copy. */
3099 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3102 in = G_INPUT_STREAM (file_in);
3104 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3105 cancellable, error))
3108 if (attrs_to_read != NULL)
3110 GError *tmp_error = NULL;
3112 /* Ok, ditch the previous lightweight info (on Unix we just
3113 * called lstat()); at this point we gather all the information
3114 * we need about the source from the opened file descriptor.
3116 g_object_unref (info);
3118 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3119 cancellable, &tmp_error);
3122 /* Not all gvfs backends implement query_info_on_read(), we
3123 * can just fall back to the pathname again.
3124 * https://bugzilla.gnome.org/706254
3126 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3128 g_clear_error (&tmp_error);
3129 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3130 cancellable, error);
3134 g_free (attrs_to_read);
3135 g_propagate_error (error, tmp_error);
3139 g_free (attrs_to_read);
3143 do_set_attributes = TRUE;
3146 /* In the local file path, we pass down the source info which
3147 * includes things like unix::mode, to ensure that the target file
3148 * is not created with different permissions from the source file.
3150 * If a future API like g_file_replace_with_info() is added, switch
3151 * this code to use that.
3153 if (G_IS_LOCAL_FILE (destination))
3155 if (flags & G_FILE_COPY_OVERWRITE)
3156 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3158 flags & G_FILE_COPY_BACKUP,
3159 G_FILE_CREATE_REPLACE_DESTINATION,
3161 cancellable, error);
3163 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3165 cancellable, error);
3167 else if (flags & G_FILE_COPY_OVERWRITE)
3169 out = (GOutputStream *)g_file_replace (destination,
3171 flags & G_FILE_COPY_BACKUP,
3172 G_FILE_CREATE_REPLACE_DESTINATION,
3173 cancellable, error);
3177 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3184 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3186 GError *reflink_err = NULL;
3188 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3189 progress_callback, progress_callback_data,
3192 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3194 g_clear_error (&reflink_err);
3198 g_propagate_error (error, reflink_err);
3211 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3213 GError *splice_err = NULL;
3215 if (!splice_stream_with_progress (in, out, cancellable,
3216 progress_callback, progress_callback_data,
3219 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3221 g_clear_error (&splice_err);
3225 g_propagate_error (error, splice_err);
3238 /* A plain read/write loop */
3239 if (!copy_stream_with_progress (in, out, source, cancellable,
3240 progress_callback, progress_callback_data,
3248 /* Don't care about errors in source here */
3249 (void) g_input_stream_close (in, cancellable, NULL);
3250 g_object_unref (in);
3255 /* But write errors on close are bad! */
3256 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3258 g_object_unref (out);
3261 /* Ignore errors here. Failure to copy metadata is not a hard error */
3262 /* TODO: set these attributes /before/ we do the rename() on Unix */
3263 if (ret && do_set_attributes)
3265 g_file_set_attributes_from_info (destination,
3267 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3272 g_clear_object (&info);
3279 * @source: input #GFile
3280 * @destination: destination #GFile
3281 * @flags: set of #GFileCopyFlags
3282 * @cancellable: (allow-none): optional #GCancellable object,
3284 * @progress_callback: (allow-none) (scope call): function to callback with
3285 * progress information, or %NULL if progress information is not needed
3286 * @progress_callback_data: (closure): user data to pass to @progress_callback
3287 * @error: #GError to set on error, or %NULL
3289 * Copies the file @source to the location specified by @destination.
3290 * Can not handle recursive copies of directories.
3292 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3293 * existing @destination file is overwritten.
3295 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3296 * will be copied as symlinks, otherwise the target of the
3297 * @source symlink will be copied.
3299 * If @cancellable is not %NULL, then the operation can be cancelled by
3300 * triggering the cancellable object from another thread. If the operation
3301 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3303 * If @progress_callback is not %NULL, then the operation can be monitored
3304 * by setting this to a #GFileProgressCallback function.
3305 * @progress_callback_data will be passed to this function. It is guaranteed
3306 * that this callback will be called after all data has been transferred with
3307 * the total number of bytes copied during the operation.
3309 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3310 * is returned, independent on the status of the @destination.
3312 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3313 * the error %G_IO_ERROR_EXISTS is returned.
3315 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3316 * error is returned. If trying to overwrite a directory with a directory the
3317 * %G_IO_ERROR_WOULD_MERGE error is returned.
3319 * If the source is a directory and the target does not exist, or
3320 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3321 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3323 * If you are interested in copying the #GFile object itself (not the on-disk
3324 * file), see g_file_dup().
3326 * Returns: %TRUE on success, %FALSE otherwise.
3329 g_file_copy (GFile *source,
3331 GFileCopyFlags flags,
3332 GCancellable *cancellable,
3333 GFileProgressCallback progress_callback,
3334 gpointer progress_callback_data,
3341 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3342 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3344 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3347 iface = G_FILE_GET_IFACE (destination);
3351 res = (* iface->copy) (source, destination,
3353 progress_callback, progress_callback_data,
3359 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3361 g_propagate_error (error, my_error);
3365 g_clear_error (&my_error);
3368 /* If the types are different, and the destination method failed
3369 * also try the source method
3371 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3373 iface = G_FILE_GET_IFACE (source);
3378 res = (* iface->copy) (source, destination,
3380 progress_callback, progress_callback_data,
3386 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3388 g_propagate_error (error, my_error);
3392 g_clear_error (&my_error);
3396 return file_copy_fallback (source, destination, flags, cancellable,
3397 progress_callback, progress_callback_data,
3402 * g_file_copy_async: (skip)
3403 * @source: input #GFile
3404 * @destination: destination #GFile
3405 * @flags: set of #GFileCopyFlags
3406 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3408 * @cancellable: (allow-none): optional #GCancellable object,
3410 * @progress_callback: (allow-none): function to callback with progress
3411 * information, or %NULL if progress information is not needed
3412 * @progress_callback_data: (closure): user data to pass to @progress_callback
3413 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3414 * @user_data: the data to pass to callback function
3416 * Copies the file @source to the location specified by @destination
3417 * asynchronously. For details of the behaviour, see g_file_copy().
3419 * If @progress_callback is not %NULL, then that function that will be called
3420 * just like in g_file_copy(), however the callback will run in the main loop,
3421 * not in the thread that is doing the I/O operation.
3423 * When the operation is finished, @callback will be called. You can then call
3424 * g_file_copy_finish() to get the result of the operation.
3427 g_file_copy_async (GFile *source,
3429 GFileCopyFlags flags,
3431 GCancellable *cancellable,
3432 GFileProgressCallback progress_callback,
3433 gpointer progress_callback_data,
3434 GAsyncReadyCallback callback,
3439 g_return_if_fail (G_IS_FILE (source));
3440 g_return_if_fail (G_IS_FILE (destination));
3442 iface = G_FILE_GET_IFACE (source);
3443 (* iface->copy_async) (source,
3449 progress_callback_data,
3455 * g_file_copy_finish:
3456 * @file: input #GFile
3457 * @res: a #GAsyncResult
3458 * @error: a #GError, or %NULL
3460 * Finishes copying the file started with g_file_copy_async().
3462 * Returns: a %TRUE on success, %FALSE on error.
3465 g_file_copy_finish (GFile *file,
3471 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3472 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3474 if (g_async_result_legacy_propagate_error (res, error))
3477 iface = G_FILE_GET_IFACE (file);
3478 return (* iface->copy_finish) (file, res, error);
3483 * @source: #GFile pointing to the source location
3484 * @destination: #GFile pointing to the destination location
3485 * @flags: set of #GFileCopyFlags
3486 * @cancellable: (allow-none): optional #GCancellable object,
3488 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3489 * function for updates
3490 * @progress_callback_data: (closure): gpointer to user data for
3491 * the callback function
3492 * @error: #GError for returning error conditions, or %NULL
3494 * Tries to move the file or directory @source to the location specified
3495 * by @destination. If native move operations are supported then this is
3496 * used, otherwise a copy + delete fallback is used. The native
3497 * implementation may support moving directories (for instance on moves
3498 * inside the same filesystem), but the fallback code does not.
3500 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3501 * existing @destination file is overwritten.
3503 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3504 * will be copied as symlinks, otherwise the target of the
3505 * @source symlink will be copied.
3507 * If @cancellable is not %NULL, then the operation can be cancelled by
3508 * triggering the cancellable object from another thread. If the operation
3509 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3511 * If @progress_callback is not %NULL, then the operation can be monitored
3512 * by setting this to a #GFileProgressCallback function.
3513 * @progress_callback_data will be passed to this function. It is
3514 * guaranteed that this callback will be called after all data has been
3515 * transferred with the total number of bytes copied during the operation.
3517 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3518 * error is returned, independent on the status of the @destination.
3520 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3521 * then the error %G_IO_ERROR_EXISTS is returned.
3523 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3524 * error is returned. If trying to overwrite a directory with a directory the
3525 * %G_IO_ERROR_WOULD_MERGE error is returned.
3527 * If the source is a directory and the target does not exist, or
3528 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3529 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3530 * move operation isn't available).
3532 * Returns: %TRUE on successful move, %FALSE otherwise.
3535 g_file_move (GFile *source,
3537 GFileCopyFlags flags,
3538 GCancellable *cancellable,
3539 GFileProgressCallback progress_callback,
3540 gpointer progress_callback_data,
3547 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3548 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3550 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3553 iface = G_FILE_GET_IFACE (destination);
3557 res = (* iface->move) (source, destination,
3559 progress_callback, progress_callback_data,
3565 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3567 g_propagate_error (error, my_error);
3572 /* If the types are different, and the destination method failed
3573 * also try the source method
3575 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3577 iface = G_FILE_GET_IFACE (source);
3582 res = (* iface->move) (source, destination,
3584 progress_callback, progress_callback_data,
3590 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3592 g_propagate_error (error, my_error);
3598 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3600 g_set_error_literal (error, G_IO_ERROR,
3601 G_IO_ERROR_NOT_SUPPORTED,
3602 _("Operation not supported"));
3606 flags |= G_FILE_COPY_ALL_METADATA;
3607 if (!g_file_copy (source, destination, flags, cancellable,
3608 progress_callback, progress_callback_data,
3612 return g_file_delete (source, cancellable, error);
3616 * g_file_make_directory:
3617 * @file: input #GFile
3618 * @cancellable: (allow-none): optional #GCancellable object,
3620 * @error: a #GError, or %NULL
3622 * Creates a directory. Note that this will only create a child directory
3623 * of the immediate parent directory of the path or URI given by the #GFile.
3624 * To recursively create directories, see g_file_make_directory_with_parents().
3625 * This function will fail if the parent directory does not exist, setting
3626 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3627 * creating directories, this function will fail, setting @error to
3628 * %G_IO_ERROR_NOT_SUPPORTED.
3630 * For a local #GFile the newly created directory will have the default
3631 * (current) ownership and permissions of the current process.
3633 * If @cancellable is not %NULL, then the operation can be cancelled by
3634 * triggering the cancellable object from another thread. If the operation
3635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3637 * Returns: %TRUE on successful creation, %FALSE otherwise.
3640 g_file_make_directory (GFile *file,
3641 GCancellable *cancellable,
3646 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3648 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3651 iface = G_FILE_GET_IFACE (file);
3653 if (iface->make_directory == NULL)
3655 g_set_error_literal (error, G_IO_ERROR,
3656 G_IO_ERROR_NOT_SUPPORTED,
3657 _("Operation not supported"));
3661 return (* iface->make_directory) (file, cancellable, error);
3665 * g_file_make_directory_async:
3666 * @file: input #GFile
3667 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3669 * @cancellable: (allow-none): optional #GCancellable object,
3671 * @callback: a #GAsyncReadyCallback to call
3672 * when the request is satisfied
3673 * @user_data: the data to pass to callback function
3675 * Asynchronously creates a directory.
3677 * Virtual: make_directory_async
3681 g_file_make_directory_async (GFile *file,
3683 GCancellable *cancellable,
3684 GAsyncReadyCallback callback,
3689 g_return_if_fail (G_IS_FILE (file));
3691 iface = G_FILE_GET_IFACE (file);
3692 (* iface->make_directory_async) (file,
3700 * g_file_make_directory_finish:
3701 * @file: input #GFile
3702 * @result: a #GAsyncResult
3703 * @error: a #GError, or %NULL
3705 * Finishes an asynchronous directory creation, started with
3706 * g_file_make_directory_async().
3708 * Virtual: make_directory_finish
3709 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3713 g_file_make_directory_finish (GFile *file,
3714 GAsyncResult *result,
3719 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3720 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3722 iface = G_FILE_GET_IFACE (file);
3723 return (* iface->make_directory_finish) (file, result, error);
3727 * g_file_make_directory_with_parents:
3728 * @file: input #GFile
3729 * @cancellable: (allow-none): optional #GCancellable object,
3731 * @error: a #GError, or %NULL
3733 * Creates a directory and any parent directories that may not
3734 * exist similar to 'mkdir -p'. If the file system does not support
3735 * creating directories, this function will fail, setting @error to
3736 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3737 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3738 * the similar g_mkdir_with_parents().
3740 * For a local #GFile the newly created directories will have the default
3741 * (current) ownership and permissions of the current process.
3743 * If @cancellable is not %NULL, then the operation can be cancelled by
3744 * triggering the cancellable object from another thread. If the operation
3745 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3747 * Returns: %TRUE if all directories have been successfully created, %FALSE
3753 g_file_make_directory_with_parents (GFile *file,
3754 GCancellable *cancellable,
3757 GFile *work_file = NULL;
3758 GList *list = NULL, *l;
3759 GError *my_error = NULL;
3761 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3763 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3766 g_file_make_directory (file, cancellable, &my_error);
3767 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3770 g_propagate_error (error, my_error);
3771 return my_error == NULL;
3774 work_file = g_object_ref (file);
3776 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3780 parent_file = g_file_get_parent (work_file);
3781 if (parent_file == NULL)
3784 g_clear_error (&my_error);
3785 g_file_make_directory (parent_file, cancellable, &my_error);
3787 g_object_unref (work_file);
3788 work_file = g_object_ref (parent_file);
3790 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3791 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3793 g_object_unref (parent_file);
3796 for (l = list; my_error == NULL && l; l = l->next)
3798 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3802 g_object_unref (work_file);
3805 while (list != NULL)
3807 g_object_unref ((GFile *) list->data);
3808 list = g_list_remove (list, list->data);
3811 if (my_error != NULL)
3813 g_propagate_error (error, my_error);
3817 return g_file_make_directory (file, cancellable, error);
3821 * g_file_make_symbolic_link:
3822 * @file: a #GFile with the name of the symlink to create
3823 * @symlink_value: a string with the path for the target of the new symlink
3824 * @cancellable: (allow-none): optional #GCancellable object,
3828 * Creates a symbolic link named @file which contains the string
3831 * If @cancellable is not %NULL, then the operation can be cancelled by
3832 * triggering the cancellable object from another thread. If the operation
3833 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3835 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3838 g_file_make_symbolic_link (GFile *file,
3839 const char *symlink_value,
3840 GCancellable *cancellable,
3845 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3846 g_return_val_if_fail (symlink_value != NULL, FALSE);
3848 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3851 if (*symlink_value == '\0')
3853 g_set_error_literal (error, G_IO_ERROR,
3854 G_IO_ERROR_INVALID_ARGUMENT,
3855 _("Invalid symlink value given"));
3859 iface = G_FILE_GET_IFACE (file);
3861 if (iface->make_symbolic_link == NULL)
3863 g_set_error_literal (error, G_IO_ERROR,
3864 G_IO_ERROR_NOT_SUPPORTED,
3865 _("Operation not supported"));
3869 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3874 * @file: input #GFile
3875 * @cancellable: (allow-none): optional #GCancellable object,
3877 * @error: a #GError, or %NULL
3879 * Deletes a file. If the @file is a directory, it will only be
3880 * deleted if it is empty. This has the same semantics as g_unlink().
3882 * If @cancellable is not %NULL, then the operation can be cancelled by
3883 * triggering the cancellable object from another thread. If the operation
3884 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3886 * Virtual: delete_file
3887 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3890 g_file_delete (GFile *file,
3891 GCancellable *cancellable,
3896 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3898 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3901 iface = G_FILE_GET_IFACE (file);
3903 if (iface->delete_file == NULL)
3905 g_set_error_literal (error, G_IO_ERROR,
3906 G_IO_ERROR_NOT_SUPPORTED,
3907 _("Operation not supported"));
3911 return (* iface->delete_file) (file, cancellable, error);
3915 * g_file_delete_async:
3916 * @file: input #GFile
3917 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3919 * @cancellable: (allow-none): optional #GCancellable object,
3921 * @callback: a #GAsyncReadyCallback to call
3922 * when the request is satisfied
3923 * @user_data: the data to pass to callback function
3925 * Asynchronously delete a file. If the @file is a directory, it will
3926 * only be deleted if it is empty. This has the same semantics as
3929 * Virtual: delete_file_async
3933 g_file_delete_async (GFile *file,
3935 GCancellable *cancellable,
3936 GAsyncReadyCallback callback,
3941 g_return_if_fail (G_IS_FILE (file));
3943 iface = G_FILE_GET_IFACE (file);
3944 (* iface->delete_file_async) (file,
3952 * g_file_delete_finish:
3953 * @file: input #GFile
3954 * @result: a #GAsyncResult
3955 * @error: a #GError, or %NULL
3957 * Finishes deleting a file started with g_file_delete_async().
3959 * Virtual: delete_file_finish
3960 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3964 g_file_delete_finish (GFile *file,
3965 GAsyncResult *result,
3970 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3971 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3973 if (g_async_result_legacy_propagate_error (result, error))
3976 iface = G_FILE_GET_IFACE (file);
3977 return (* iface->delete_file_finish) (file, result, error);
3982 * @file: #GFile to send to trash
3983 * @cancellable: (allow-none): optional #GCancellable object,
3985 * @error: a #GError, or %NULL
3987 * Sends @file to the "Trashcan", if possible. This is similar to
3988 * deleting it, but the user can recover it before emptying the trashcan.
3989 * Not all file systems support trashing, so this call can return the
3990 * %G_IO_ERROR_NOT_SUPPORTED error.
3992 * If @cancellable is not %NULL, then the operation can be cancelled by
3993 * triggering the cancellable object from another thread. If the operation
3994 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3997 * Returns: %TRUE on successful trash, %FALSE otherwise.
4000 g_file_trash (GFile *file,
4001 GCancellable *cancellable,
4006 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4008 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4011 iface = G_FILE_GET_IFACE (file);
4013 if (iface->trash == NULL)
4015 g_set_error_literal (error,
4016 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4017 _("Trash not supported"));
4021 return (* iface->trash) (file, cancellable, error);
4025 * g_file_trash_async:
4026 * @file: input #GFile
4027 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4029 * @cancellable: (allow-none): optional #GCancellable object,
4031 * @callback: a #GAsyncReadyCallback to call
4032 * when the request is satisfied
4033 * @user_data: the data to pass to callback function
4035 * Asynchronously sends @file to the Trash location, if possible.
4037 * Virtual: trash_async
4041 g_file_trash_async (GFile *file,
4043 GCancellable *cancellable,
4044 GAsyncReadyCallback callback,
4049 g_return_if_fail (G_IS_FILE (file));
4051 iface = G_FILE_GET_IFACE (file);
4052 (* iface->trash_async) (file,
4060 * g_file_trash_finish:
4061 * @file: input #GFile
4062 * @result: a #GAsyncResult
4063 * @error: a #GError, or %NULL
4065 * Finishes an asynchronous file trashing operation, started with
4066 * g_file_trash_async().
4068 * Virtual: trash_finish
4069 * Returns: %TRUE on successful trash, %FALSE otherwise.
4073 g_file_trash_finish (GFile *file,
4074 GAsyncResult *result,
4079 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4080 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4082 iface = G_FILE_GET_IFACE (file);
4083 return (* iface->trash_finish) (file, result, error);
4087 * g_file_set_display_name:
4088 * @file: input #GFile
4089 * @display_name: a string
4090 * @cancellable: (allow-none): optional #GCancellable object,
4092 * @error: a #GError, or %NULL
4094 * Renames @file to the specified display name.
4096 * The display name is converted from UTF-8 to the correct encoding
4097 * for the target filesystem if possible and the @file is renamed to this.
4099 * If you want to implement a rename operation in the user interface the
4100 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4101 * initial value in the rename widget, and then the result after editing
4102 * should be passed to g_file_set_display_name().
4104 * On success the resulting converted filename is returned.
4106 * If @cancellable is not %NULL, then the operation can be cancelled by
4107 * triggering the cancellable object from another thread. If the operation
4108 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4110 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4111 * or %NULL if there was an error.
4112 * Free the returned object with g_object_unref().
4115 g_file_set_display_name (GFile *file,
4116 const gchar *display_name,
4117 GCancellable *cancellable,
4122 g_return_val_if_fail (G_IS_FILE (file), NULL);
4123 g_return_val_if_fail (display_name != NULL, NULL);
4125 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4129 G_IO_ERROR_INVALID_ARGUMENT,
4130 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4134 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4137 iface = G_FILE_GET_IFACE (file);
4139 return (* iface->set_display_name) (file, display_name, cancellable, error);
4143 * g_file_set_display_name_async:
4144 * @file: input #GFile
4145 * @display_name: a string
4146 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4148 * @cancellable: (allow-none): optional #GCancellable object,
4150 * @callback: (scope async): a #GAsyncReadyCallback to call
4151 * when the request is satisfied
4152 * @user_data: (closure): the data to pass to callback function
4154 * Asynchronously sets the display name for a given #GFile.
4156 * For more details, see g_file_set_display_name() which is
4157 * the synchronous version of this call.
4159 * When the operation is finished, @callback will be called.
4160 * You can then call g_file_set_display_name_finish() to get
4161 * the result of the operation.
4164 g_file_set_display_name_async (GFile *file,
4165 const gchar *display_name,
4167 GCancellable *cancellable,
4168 GAsyncReadyCallback callback,
4173 g_return_if_fail (G_IS_FILE (file));
4174 g_return_if_fail (display_name != NULL);
4176 iface = G_FILE_GET_IFACE (file);
4177 (* iface->set_display_name_async) (file,
4186 * g_file_set_display_name_finish:
4187 * @file: input #GFile
4188 * @res: a #GAsyncResult
4189 * @error: a #GError, or %NULL
4191 * Finishes setting a display name started with
4192 * g_file_set_display_name_async().
4194 * Returns: (transfer full): a #GFile or %NULL on error.
4195 * Free the returned object with g_object_unref().
4198 g_file_set_display_name_finish (GFile *file,
4204 g_return_val_if_fail (G_IS_FILE (file), NULL);
4205 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4207 if (g_async_result_legacy_propagate_error (res, error))
4210 iface = G_FILE_GET_IFACE (file);
4211 return (* iface->set_display_name_finish) (file, res, error);
4215 * g_file_query_settable_attributes:
4216 * @file: input #GFile
4217 * @cancellable: (allow-none): optional #GCancellable object,
4219 * @error: a #GError, or %NULL
4221 * Obtain the list of settable attributes for the file.
4223 * Returns the type and full attribute name of all the attributes
4224 * that can be set on this file. This doesn't mean setting it will
4225 * always succeed though, you might get an access failure, or some
4226 * specific file may not support a specific attribute.
4228 * If @cancellable is not %NULL, then the operation can be cancelled by
4229 * triggering the cancellable object from another thread. If the operation
4230 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4232 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4233 * When you are done with it, release it with
4234 * g_file_attribute_info_list_unref()
4236 GFileAttributeInfoList *
4237 g_file_query_settable_attributes (GFile *file,
4238 GCancellable *cancellable,
4243 GFileAttributeInfoList *list;
4245 g_return_val_if_fail (G_IS_FILE (file), NULL);
4247 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4250 iface = G_FILE_GET_IFACE (file);
4252 if (iface->query_settable_attributes == NULL)
4253 return g_file_attribute_info_list_new ();
4256 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4260 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4262 list = g_file_attribute_info_list_new ();
4263 g_error_free (my_error);
4266 g_propagate_error (error, my_error);
4273 * g_file_query_writable_namespaces:
4274 * @file: input #GFile
4275 * @cancellable: (allow-none): optional #GCancellable object,
4277 * @error: a #GError, or %NULL
4279 * Obtain the list of attribute namespaces where new attributes
4280 * can be created by a user. An example of this is extended
4281 * attributes (in the "xattr" namespace).
4283 * If @cancellable is not %NULL, then the operation can be cancelled by
4284 * triggering the cancellable object from another thread. If the operation
4285 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4287 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4288 * When you are done with it, release it with
4289 * g_file_attribute_info_list_unref()
4291 GFileAttributeInfoList *
4292 g_file_query_writable_namespaces (GFile *file,
4293 GCancellable *cancellable,
4298 GFileAttributeInfoList *list;
4300 g_return_val_if_fail (G_IS_FILE (file), NULL);
4302 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4305 iface = G_FILE_GET_IFACE (file);
4307 if (iface->query_writable_namespaces == NULL)
4308 return g_file_attribute_info_list_new ();
4311 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4315 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4317 list = g_file_attribute_info_list_new ();
4318 g_error_free (my_error);
4321 g_propagate_error (error, my_error);
4328 * g_file_set_attribute:
4329 * @file: input #GFile
4330 * @attribute: a string containing the attribute's name
4331 * @type: The type of the attribute
4332 * @value_p: (allow-none): a pointer to the value (or the pointer
4333 * itself if the type is a pointer type)
4334 * @flags: a set of #GFileQueryInfoFlags
4335 * @cancellable: (allow-none): optional #GCancellable object,
4337 * @error: a #GError, or %NULL
4339 * Sets an attribute in the file with attribute name @attribute to @value.
4341 * Some attributes can be unset by setting @attribute to
4342 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4344 * If @cancellable is not %NULL, then the operation can be cancelled by
4345 * triggering the cancellable object from another thread. If the operation
4346 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4348 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4351 g_file_set_attribute (GFile *file,
4352 const gchar *attribute,
4353 GFileAttributeType type,
4355 GFileQueryInfoFlags flags,
4356 GCancellable *cancellable,
4361 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4362 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4364 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4367 iface = G_FILE_GET_IFACE (file);
4369 if (iface->set_attribute == NULL)
4371 g_set_error_literal (error, G_IO_ERROR,
4372 G_IO_ERROR_NOT_SUPPORTED,
4373 _("Operation not supported"));
4377 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4381 * g_file_set_attributes_from_info:
4382 * @file: input #GFile
4383 * @info: a #GFileInfo
4384 * @flags: #GFileQueryInfoFlags
4385 * @cancellable: (allow-none): optional #GCancellable object,
4387 * @error: a #GError, or %NULL
4389 * Tries to set all attributes in the #GFileInfo on the target
4390 * values, not stopping on the first error.
4392 * If there is any error during this operation then @error will
4393 * be set to the first error. Error on particular fields are flagged
4394 * by setting the "status" field in the attribute value to
4395 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4396 * also detect further errors.
4398 * If @cancellable is not %NULL, then the operation can be cancelled by
4399 * triggering the cancellable object from another thread. If the operation
4400 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4402 * Returns: %FALSE if there was any error, %TRUE otherwise.
4405 g_file_set_attributes_from_info (GFile *file,
4407 GFileQueryInfoFlags flags,
4408 GCancellable *cancellable,
4413 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4414 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4416 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4419 g_file_info_clear_status (info);
4421 iface = G_FILE_GET_IFACE (file);
4423 return (* iface->set_attributes_from_info) (file,
4431 g_file_real_set_attributes_from_info (GFile *file,
4433 GFileQueryInfoFlags flags,
4434 GCancellable *cancellable,
4440 GFileAttributeValue *value;
4444 attributes = g_file_info_list_attributes (info, NULL);
4446 for (i = 0; attributes[i] != NULL; i++)
4448 value = _g_file_info_get_attribute_value (info, attributes[i]);
4450 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4453 if (!g_file_set_attribute (file, attributes[i],
4454 value->type, _g_file_attribute_value_peek_as_pointer (value),
4455 flags, cancellable, error))
4457 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4459 /* Don't set error multiple times */
4463 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4466 g_strfreev (attributes);
4472 * g_file_set_attributes_async:
4473 * @file: input #GFile
4474 * @info: a #GFileInfo
4475 * @flags: a #GFileQueryInfoFlags
4476 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4478 * @cancellable: (allow-none): optional #GCancellable object,
4480 * @callback: (scope async): a #GAsyncReadyCallback
4481 * @user_data: (closure): a #gpointer
4483 * Asynchronously sets the attributes of @file with @info.
4485 * For more details, see g_file_set_attributes_from_info(),
4486 * which is the synchronous version of this call.
4488 * When the operation is finished, @callback will be called.
4489 * You can then call g_file_set_attributes_finish() to get
4490 * the result of the operation.
4493 g_file_set_attributes_async (GFile *file,
4495 GFileQueryInfoFlags flags,
4497 GCancellable *cancellable,
4498 GAsyncReadyCallback callback,
4503 g_return_if_fail (G_IS_FILE (file));
4504 g_return_if_fail (G_IS_FILE_INFO (info));
4506 iface = G_FILE_GET_IFACE (file);
4507 (* iface->set_attributes_async) (file,
4517 * g_file_set_attributes_finish:
4518 * @file: input #GFile
4519 * @result: a #GAsyncResult
4520 * @info: (out) (transfer full): a #GFileInfo
4521 * @error: a #GError, or %NULL
4523 * Finishes setting an attribute started in g_file_set_attributes_async().
4525 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4528 g_file_set_attributes_finish (GFile *file,
4529 GAsyncResult *result,
4535 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4536 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4538 /* No standard handling of errors here, as we must set info even
4541 iface = G_FILE_GET_IFACE (file);
4542 return (* iface->set_attributes_finish) (file, result, info, error);
4546 * g_file_set_attribute_string:
4547 * @file: input #GFile
4548 * @attribute: a string containing the attribute's name
4549 * @value: a string containing the attribute's value
4550 * @flags: #GFileQueryInfoFlags
4551 * @cancellable: (allow-none): optional #GCancellable object,
4553 * @error: a #GError, or %NULL
4555 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4556 * If @attribute is of a different type, this operation will fail.
4558 * If @cancellable is not %NULL, then the operation can be cancelled by
4559 * triggering the cancellable object from another thread. If the operation
4560 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4562 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4565 g_file_set_attribute_string (GFile *file,
4566 const char *attribute,
4568 GFileQueryInfoFlags flags,
4569 GCancellable *cancellable,
4572 return g_file_set_attribute (file, attribute,
4573 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4574 flags, cancellable, error);
4578 * g_file_set_attribute_byte_string:
4579 * @file: input #GFile
4580 * @attribute: a string containing the attribute's name
4581 * @value: a string containing the attribute's new value
4582 * @flags: a #GFileQueryInfoFlags
4583 * @cancellable: (allow-none): optional #GCancellable object,
4585 * @error: a #GError, or %NULL
4587 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4588 * If @attribute is of a different type, this operation will fail,
4591 * If @cancellable is not %NULL, then the operation can be cancelled by
4592 * triggering the cancellable object from another thread. If the operation
4593 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4595 * Returns: %TRUE if the @attribute was successfully set to @value
4596 * in the @file, %FALSE otherwise.
4599 g_file_set_attribute_byte_string (GFile *file,
4600 const gchar *attribute,
4602 GFileQueryInfoFlags flags,
4603 GCancellable *cancellable,
4606 return g_file_set_attribute (file, attribute,
4607 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4608 flags, cancellable, error);
4612 * g_file_set_attribute_uint32:
4613 * @file: input #GFile
4614 * @attribute: a string containing the attribute's name
4615 * @value: a #guint32 containing the attribute's new value
4616 * @flags: a #GFileQueryInfoFlags
4617 * @cancellable: (allow-none): optional #GCancellable object,
4619 * @error: a #GError, or %NULL
4621 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4622 * If @attribute is of a different type, this operation will fail.
4624 * If @cancellable is not %NULL, then the operation can be cancelled by
4625 * triggering the cancellable object from another thread. If the operation
4626 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4628 * Returns: %TRUE if the @attribute was successfully set to @value
4629 * in the @file, %FALSE otherwise.
4632 g_file_set_attribute_uint32 (GFile *file,
4633 const gchar *attribute,
4635 GFileQueryInfoFlags flags,
4636 GCancellable *cancellable,
4639 return g_file_set_attribute (file, attribute,
4640 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4641 flags, cancellable, error);
4645 * g_file_set_attribute_int32:
4646 * @file: input #GFile
4647 * @attribute: a string containing the attribute's name
4648 * @value: a #gint32 containing the attribute's new value
4649 * @flags: a #GFileQueryInfoFlags
4650 * @cancellable: (allow-none): optional #GCancellable object,
4652 * @error: a #GError, or %NULL
4654 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4655 * If @attribute is of a different type, this operation will fail.
4657 * If @cancellable is not %NULL, then the operation can be cancelled by
4658 * triggering the cancellable object from another thread. If the operation
4659 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4661 * Returns: %TRUE if the @attribute was successfully set to @value
4662 * in the @file, %FALSE otherwise.
4665 g_file_set_attribute_int32 (GFile *file,
4666 const gchar *attribute,
4668 GFileQueryInfoFlags flags,
4669 GCancellable *cancellable,
4672 return g_file_set_attribute (file, attribute,
4673 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4674 flags, cancellable, error);
4678 * g_file_set_attribute_uint64:
4679 * @file: input #GFile
4680 * @attribute: a string containing the attribute's name
4681 * @value: a #guint64 containing the attribute's new value
4682 * @flags: a #GFileQueryInfoFlags
4683 * @cancellable: (allow-none): optional #GCancellable object,
4685 * @error: a #GError, or %NULL
4687 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4688 * If @attribute is of a different type, this operation will fail.
4690 * If @cancellable is not %NULL, then the operation can be cancelled by
4691 * triggering the cancellable object from another thread. If the operation
4692 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4694 * Returns: %TRUE if the @attribute was successfully set to @value
4695 * in the @file, %FALSE otherwise.
4698 g_file_set_attribute_uint64 (GFile *file,
4699 const gchar *attribute,
4701 GFileQueryInfoFlags flags,
4702 GCancellable *cancellable,
4705 return g_file_set_attribute (file, attribute,
4706 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4707 flags, cancellable, error);
4711 * g_file_set_attribute_int64:
4712 * @file: input #GFile
4713 * @attribute: a string containing the attribute's name
4714 * @value: a #guint64 containing the attribute's new value
4715 * @flags: a #GFileQueryInfoFlags
4716 * @cancellable: (allow-none): optional #GCancellable object,
4718 * @error: a #GError, or %NULL
4720 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4721 * If @attribute is of a different type, this operation will fail.
4723 * If @cancellable is not %NULL, then the operation can be cancelled by
4724 * triggering the cancellable object from another thread. If the operation
4725 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4727 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4730 g_file_set_attribute_int64 (GFile *file,
4731 const gchar *attribute,
4733 GFileQueryInfoFlags flags,
4734 GCancellable *cancellable,
4737 return g_file_set_attribute (file, attribute,
4738 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4739 flags, cancellable, error);
4743 * g_file_mount_mountable:
4744 * @file: input #GFile
4745 * @flags: flags affecting the operation
4746 * @mount_operation: (allow-none): a #GMountOperation,
4747 * or %NULL to avoid user interaction
4748 * @cancellable: (allow-none): optional #GCancellable object,
4750 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4751 * when the request is satisfied, or %NULL
4752 * @user_data: (closure): the data to pass to callback function
4754 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4755 * Using @mount_operation, you can request callbacks when, for instance,
4756 * passwords are needed during authentication.
4758 * If @cancellable is not %NULL, then the operation can be cancelled by
4759 * triggering the cancellable object from another thread. If the operation
4760 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4762 * When the operation is finished, @callback will be called.
4763 * You can then call g_file_mount_mountable_finish() to get
4764 * the result of the operation.
4767 g_file_mount_mountable (GFile *file,
4768 GMountMountFlags flags,
4769 GMountOperation *mount_operation,
4770 GCancellable *cancellable,
4771 GAsyncReadyCallback callback,
4776 g_return_if_fail (G_IS_FILE (file));
4778 iface = G_FILE_GET_IFACE (file);
4780 if (iface->mount_mountable == NULL)
4782 g_task_report_new_error (file, callback, user_data,
4783 g_file_mount_mountable,
4784 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4785 _("Operation not supported"));
4789 (* iface->mount_mountable) (file,
4798 * g_file_mount_mountable_finish:
4799 * @file: input #GFile
4800 * @result: a #GAsyncResult
4801 * @error: a #GError, or %NULL
4803 * Finishes a mount operation. See g_file_mount_mountable() for details.
4805 * Finish an asynchronous mount operation that was started
4806 * with g_file_mount_mountable().
4808 * Returns: (transfer full): a #GFile or %NULL on error.
4809 * Free the returned object with g_object_unref().
4812 g_file_mount_mountable_finish (GFile *file,
4813 GAsyncResult *result,
4818 g_return_val_if_fail (G_IS_FILE (file), NULL);
4819 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4821 if (g_async_result_legacy_propagate_error (result, error))
4823 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4824 return g_task_propagate_pointer (G_TASK (result), error);
4826 iface = G_FILE_GET_IFACE (file);
4827 return (* iface->mount_mountable_finish) (file, result, error);
4831 * g_file_unmount_mountable:
4832 * @file: input #GFile
4833 * @flags: flags affecting the operation
4834 * @cancellable: (allow-none): optional #GCancellable object,
4836 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4837 * when the request is satisfied, or %NULL
4838 * @user_data: (closure): the data to pass to callback function
4840 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4842 * If @cancellable is not %NULL, then the operation can be cancelled by
4843 * triggering the cancellable object from another thread. If the operation
4844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4846 * When the operation is finished, @callback will be called.
4847 * You can then call g_file_unmount_mountable_finish() to get
4848 * the result of the operation.
4850 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4853 g_file_unmount_mountable (GFile *file,
4854 GMountUnmountFlags flags,
4855 GCancellable *cancellable,
4856 GAsyncReadyCallback callback,
4861 g_return_if_fail (G_IS_FILE (file));
4863 iface = G_FILE_GET_IFACE (file);
4865 if (iface->unmount_mountable == NULL)
4867 g_task_report_new_error (file, callback, user_data,
4868 g_file_unmount_mountable_with_operation,
4869 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4870 _("Operation not supported"));
4874 (* iface->unmount_mountable) (file,
4882 * g_file_unmount_mountable_finish:
4883 * @file: input #GFile
4884 * @result: a #GAsyncResult
4885 * @error: a #GError, or %NULL
4887 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4889 * Finish an asynchronous unmount operation that was started
4890 * with g_file_unmount_mountable().
4892 * Returns: %TRUE if the operation finished successfully.
4895 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4899 g_file_unmount_mountable_finish (GFile *file,
4900 GAsyncResult *result,
4905 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4906 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4908 if (g_async_result_legacy_propagate_error (result, error))
4910 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4911 return g_task_propagate_boolean (G_TASK (result), error);
4913 iface = G_FILE_GET_IFACE (file);
4914 return (* iface->unmount_mountable_finish) (file, result, error);
4918 * g_file_unmount_mountable_with_operation:
4919 * @file: input #GFile
4920 * @flags: flags affecting the operation
4921 * @mount_operation: (allow-none): a #GMountOperation,
4922 * or %NULL to avoid user interaction
4923 * @cancellable: (allow-none): optional #GCancellable object,
4925 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4926 * when the request is satisfied, or %NULL
4927 * @user_data: (closure): the data to pass to callback function
4929 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4931 * If @cancellable is not %NULL, then the operation can be cancelled by
4932 * triggering the cancellable object from another thread. If the operation
4933 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4935 * When the operation is finished, @callback will be called.
4936 * You can then call g_file_unmount_mountable_finish() to get
4937 * the result of the operation.
4942 g_file_unmount_mountable_with_operation (GFile *file,
4943 GMountUnmountFlags flags,
4944 GMountOperation *mount_operation,
4945 GCancellable *cancellable,
4946 GAsyncReadyCallback callback,
4951 g_return_if_fail (G_IS_FILE (file));
4953 iface = G_FILE_GET_IFACE (file);
4955 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4957 g_task_report_new_error (file, callback, user_data,
4958 g_file_unmount_mountable_with_operation,
4959 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4960 _("Operation not supported"));
4964 if (iface->unmount_mountable_with_operation != NULL)
4965 (* iface->unmount_mountable_with_operation) (file,
4972 (* iface->unmount_mountable) (file,
4980 * g_file_unmount_mountable_with_operation_finish:
4981 * @file: input #GFile
4982 * @result: a #GAsyncResult
4983 * @error: a #GError, or %NULL
4985 * Finishes an unmount operation,
4986 * see g_file_unmount_mountable_with_operation() for details.
4988 * Finish an asynchronous unmount operation that was started
4989 * with g_file_unmount_mountable_with_operation().
4991 * Returns: %TRUE if the operation finished successfully.
4997 g_file_unmount_mountable_with_operation_finish (GFile *file,
4998 GAsyncResult *result,
5003 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5004 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5006 if (g_async_result_legacy_propagate_error (result, error))
5008 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5009 return g_task_propagate_boolean (G_TASK (result), error);
5011 iface = G_FILE_GET_IFACE (file);
5012 if (iface->unmount_mountable_with_operation_finish != NULL)
5013 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5015 return (* iface->unmount_mountable_finish) (file, result, error);
5019 * g_file_eject_mountable:
5020 * @file: input #GFile
5021 * @flags: flags affecting the operation
5022 * @cancellable: (allow-none): optional #GCancellable object,
5024 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5025 * when the request is satisfied, or %NULL
5026 * @user_data: (closure): the data to pass to callback function
5028 * Starts an asynchronous eject on a mountable.
5029 * When this operation has completed, @callback will be called with
5030 * @user_user data, and the operation can be finalized with
5031 * g_file_eject_mountable_finish().
5033 * If @cancellable is not %NULL, then the operation can be cancelled by
5034 * triggering the cancellable object from another thread. If the operation
5035 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5037 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5040 g_file_eject_mountable (GFile *file,
5041 GMountUnmountFlags flags,
5042 GCancellable *cancellable,
5043 GAsyncReadyCallback callback,
5048 g_return_if_fail (G_IS_FILE (file));
5050 iface = G_FILE_GET_IFACE (file);
5052 if (iface->eject_mountable == NULL)
5054 g_task_report_new_error (file, callback, user_data,
5055 g_file_eject_mountable_with_operation,
5056 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5057 _("Operation not supported"));
5061 (* iface->eject_mountable) (file,
5069 * g_file_eject_mountable_finish:
5070 * @file: input #GFile
5071 * @result: a #GAsyncResult
5072 * @error: a #GError, or %NULL
5074 * Finishes an asynchronous eject operation started by
5075 * g_file_eject_mountable().
5077 * Returns: %TRUE if the @file was ejected successfully.
5080 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5084 g_file_eject_mountable_finish (GFile *file,
5085 GAsyncResult *result,
5090 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5091 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5093 if (g_async_result_legacy_propagate_error (result, error))
5095 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5096 return g_task_propagate_boolean (G_TASK (result), error);
5098 iface = G_FILE_GET_IFACE (file);
5099 return (* iface->eject_mountable_finish) (file, result, error);
5103 * g_file_eject_mountable_with_operation:
5104 * @file: input #GFile
5105 * @flags: flags affecting the operation
5106 * @mount_operation: (allow-none): a #GMountOperation,
5107 * or %NULL to avoid user interaction
5108 * @cancellable: (allow-none): optional #GCancellable object,
5110 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5111 * when the request is satisfied, or %NULL
5112 * @user_data: (closure): the data to pass to callback function
5114 * Starts an asynchronous eject on a mountable.
5115 * When this operation has completed, @callback will be called with
5116 * @user_user data, and the operation can be finalized with
5117 * g_file_eject_mountable_with_operation_finish().
5119 * If @cancellable is not %NULL, then the operation can be cancelled by
5120 * triggering the cancellable object from another thread. If the operation
5121 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5126 g_file_eject_mountable_with_operation (GFile *file,
5127 GMountUnmountFlags flags,
5128 GMountOperation *mount_operation,
5129 GCancellable *cancellable,
5130 GAsyncReadyCallback callback,
5135 g_return_if_fail (G_IS_FILE (file));
5137 iface = G_FILE_GET_IFACE (file);
5139 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5141 g_task_report_new_error (file, callback, user_data,
5142 g_file_eject_mountable_with_operation,
5143 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5144 _("Operation not supported"));
5148 if (iface->eject_mountable_with_operation != NULL)
5149 (* iface->eject_mountable_with_operation) (file,
5156 (* iface->eject_mountable) (file,
5164 * g_file_eject_mountable_with_operation_finish:
5165 * @file: input #GFile
5166 * @result: a #GAsyncResult
5167 * @error: a #GError, or %NULL
5169 * Finishes an asynchronous eject operation started by
5170 * g_file_eject_mountable_with_operation().
5172 * Returns: %TRUE if the @file was ejected successfully.
5178 g_file_eject_mountable_with_operation_finish (GFile *file,
5179 GAsyncResult *result,
5184 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5185 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5187 if (g_async_result_legacy_propagate_error (result, error))
5189 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5190 return g_task_propagate_boolean (G_TASK (result), error);
5192 iface = G_FILE_GET_IFACE (file);
5193 if (iface->eject_mountable_with_operation_finish != NULL)
5194 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5196 return (* iface->eject_mountable_finish) (file, result, error);
5200 * g_file_monitor_directory:
5201 * @file: input #GFile
5202 * @flags: a set of #GFileMonitorFlags
5203 * @cancellable: (allow-none): optional #GCancellable object,
5205 * @error: a #GError, or %NULL
5207 * Obtains a directory monitor for the given file.
5208 * This may fail if directory monitoring is not supported.
5210 * If @cancellable is not %NULL, then the operation can be cancelled by
5211 * triggering the cancellable object from another thread. If the operation
5212 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5214 * It does not make sense for @flags to contain
5215 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5216 * directories. It is not possible to monitor all the files in a
5217 * directory for changes made via hard links; if you want to do this then
5218 * you must register individual watches with g_file_monitor().
5220 * Virtual: monitor_dir
5221 * Returns: (transfer full): a #GFileMonitor for the given @file,
5222 * or %NULL on error.
5223 * Free the returned object with g_object_unref().
5226 g_file_monitor_directory (GFile *file,
5227 GFileMonitorFlags flags,
5228 GCancellable *cancellable,
5233 g_return_val_if_fail (G_IS_FILE (file), NULL);
5234 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5236 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5239 iface = G_FILE_GET_IFACE (file);
5241 if (iface->monitor_dir == NULL)
5243 g_set_error_literal (error, G_IO_ERROR,
5244 G_IO_ERROR_NOT_SUPPORTED,
5245 _("Operation not supported"));
5249 return (* iface->monitor_dir) (file, flags, cancellable, error);
5253 * g_file_monitor_file:
5254 * @file: input #GFile
5255 * @flags: a set of #GFileMonitorFlags
5256 * @cancellable: (allow-none): optional #GCancellable object,
5258 * @error: a #GError, or %NULL
5260 * Obtains a file monitor for the given file. If no file notification
5261 * mechanism exists, then regular polling of the file is used.
5263 * If @cancellable is not %NULL, then the operation can be cancelled by
5264 * triggering the cancellable object from another thread. If the operation
5265 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5267 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5268 * will also attempt to report changes made to the file via another
5269 * filename (ie, a hard link). Without this flag, you can only rely on
5270 * changes made through the filename contained in @file to be
5271 * reported. Using this flag may result in an increase in resource
5272 * usage, and may not have any effect depending on the #GFileMonitor
5273 * backend and/or filesystem type.
5275 * Returns: (transfer full): a #GFileMonitor for the given @file,
5276 * or %NULL on error.
5277 * Free the returned object with g_object_unref().
5280 g_file_monitor_file (GFile *file,
5281 GFileMonitorFlags flags,
5282 GCancellable *cancellable,
5286 GFileMonitor *monitor;
5288 g_return_val_if_fail (G_IS_FILE (file), NULL);
5290 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5293 iface = G_FILE_GET_IFACE (file);
5297 if (iface->monitor_file)
5298 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5300 /* Fallback to polling */
5301 if (monitor == NULL)
5302 monitor = _g_poll_file_monitor_new (file);
5309 * @file: input #GFile
5310 * @flags: a set of #GFileMonitorFlags
5311 * @cancellable: (allow-none): optional #GCancellable object,
5313 * @error: a #GError, or %NULL
5315 * Obtains a file or directory monitor for the given file,
5316 * depending on the type of the file.
5318 * If @cancellable is not %NULL, then the operation can be cancelled by
5319 * triggering the cancellable object from another thread. If the operation
5320 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5322 * Returns: (transfer full): a #GFileMonitor for the given @file,
5323 * or %NULL on error.
5324 * Free the returned object with g_object_unref().
5329 g_file_monitor (GFile *file,
5330 GFileMonitorFlags flags,
5331 GCancellable *cancellable,
5334 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5335 return g_file_monitor_directory (file,
5336 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5337 cancellable, error);
5339 return g_file_monitor_file (file, flags, cancellable, error);
5342 /********************************************
5343 * Default implementation of async ops *
5344 ********************************************/
5348 GFileQueryInfoFlags flags;
5349 } QueryInfoAsyncData;
5352 query_info_data_free (QueryInfoAsyncData *data)
5354 g_free (data->attributes);
5359 query_info_async_thread (GTask *task,
5362 GCancellable *cancellable)
5364 QueryInfoAsyncData *data = task_data;
5366 GError *error = NULL;
5368 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5370 g_task_return_pointer (task, info, g_object_unref);
5372 g_task_return_error (task, error);
5376 g_file_real_query_info_async (GFile *file,
5377 const char *attributes,
5378 GFileQueryInfoFlags flags,
5380 GCancellable *cancellable,
5381 GAsyncReadyCallback callback,
5385 QueryInfoAsyncData *data;
5387 data = g_new0 (QueryInfoAsyncData, 1);
5388 data->attributes = g_strdup (attributes);
5389 data->flags = flags;
5391 task = g_task_new (file, cancellable, callback, user_data);
5392 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5393 g_task_set_priority (task, io_priority);
5394 g_task_run_in_thread (task, query_info_async_thread);
5395 g_object_unref (task);
5399 g_file_real_query_info_finish (GFile *file,
5403 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5405 return g_task_propagate_pointer (G_TASK (res), error);
5409 query_filesystem_info_async_thread (GTask *task,
5412 GCancellable *cancellable)
5414 const char *attributes = task_data;
5416 GError *error = NULL;
5418 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5420 g_task_return_pointer (task, info, g_object_unref);
5422 g_task_return_error (task, error);
5426 g_file_real_query_filesystem_info_async (GFile *file,
5427 const char *attributes,
5429 GCancellable *cancellable,
5430 GAsyncReadyCallback callback,
5435 task = g_task_new (file, cancellable, callback, user_data);
5436 g_task_set_task_data (task, g_strdup (attributes), g_free);
5437 g_task_set_priority (task, io_priority);
5438 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5439 g_object_unref (task);
5443 g_file_real_query_filesystem_info_finish (GFile *file,
5447 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5449 return g_task_propagate_pointer (G_TASK (res), error);
5453 enumerate_children_async_thread (GTask *task,
5456 GCancellable *cancellable)
5458 QueryInfoAsyncData *data = task_data;
5459 GFileEnumerator *enumerator;
5460 GError *error = NULL;
5462 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5464 g_task_return_error (task, error);
5466 g_task_return_pointer (task, enumerator, g_object_unref);
5470 g_file_real_enumerate_children_async (GFile *file,
5471 const char *attributes,
5472 GFileQueryInfoFlags flags,
5474 GCancellable *cancellable,
5475 GAsyncReadyCallback callback,
5479 QueryInfoAsyncData *data;
5481 data = g_new0 (QueryInfoAsyncData, 1);
5482 data->attributes = g_strdup (attributes);
5483 data->flags = flags;
5485 task = g_task_new (file, cancellable, callback, user_data);
5486 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5487 g_task_set_priority (task, io_priority);
5488 g_task_run_in_thread (task, enumerate_children_async_thread);
5489 g_object_unref (task);
5492 static GFileEnumerator *
5493 g_file_real_enumerate_children_finish (GFile *file,
5497 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5499 return g_task_propagate_pointer (G_TASK (res), error);
5503 open_read_async_thread (GTask *task,
5506 GCancellable *cancellable)
5508 GFileInputStream *stream;
5509 GError *error = NULL;
5511 stream = g_file_read (G_FILE (object), cancellable, &error);
5513 g_task_return_pointer (task, stream, g_object_unref);
5515 g_task_return_error (task, error);
5519 g_file_real_read_async (GFile *file,
5521 GCancellable *cancellable,
5522 GAsyncReadyCallback callback,
5527 task = g_task_new (file, cancellable, callback, user_data);
5528 g_task_set_priority (task, io_priority);
5529 g_task_run_in_thread (task, open_read_async_thread);
5530 g_object_unref (task);
5533 static GFileInputStream *
5534 g_file_real_read_finish (GFile *file,
5538 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5540 return g_task_propagate_pointer (G_TASK (res), error);
5544 append_to_async_thread (GTask *task,
5545 gpointer source_object,
5547 GCancellable *cancellable)
5549 GFileCreateFlags *data = task_data;
5550 GFileOutputStream *stream;
5551 GError *error = NULL;
5553 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5555 g_task_return_pointer (task, stream, g_object_unref);
5557 g_task_return_error (task, error);
5561 g_file_real_append_to_async (GFile *file,
5562 GFileCreateFlags flags,
5564 GCancellable *cancellable,
5565 GAsyncReadyCallback callback,
5568 GFileCreateFlags *data;
5571 data = g_new0 (GFileCreateFlags, 1);
5574 task = g_task_new (file, cancellable, callback, user_data);
5575 g_task_set_task_data (task, data, g_free);
5576 g_task_set_priority (task, io_priority);
5578 g_task_run_in_thread (task, append_to_async_thread);
5579 g_object_unref (task);
5582 static GFileOutputStream *
5583 g_file_real_append_to_finish (GFile *file,
5587 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5589 return g_task_propagate_pointer (G_TASK (res), error);
5593 create_async_thread (GTask *task,
5594 gpointer source_object,
5596 GCancellable *cancellable)
5598 GFileCreateFlags *data = task_data;
5599 GFileOutputStream *stream;
5600 GError *error = NULL;
5602 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5604 g_task_return_pointer (task, stream, g_object_unref);
5606 g_task_return_error (task, error);
5610 g_file_real_create_async (GFile *file,
5611 GFileCreateFlags flags,
5613 GCancellable *cancellable,
5614 GAsyncReadyCallback callback,
5617 GFileCreateFlags *data;
5620 data = g_new0 (GFileCreateFlags, 1);
5623 task = g_task_new (file, cancellable, callback, user_data);
5624 g_task_set_task_data (task, data, g_free);
5625 g_task_set_priority (task, io_priority);
5627 g_task_run_in_thread (task, create_async_thread);
5628 g_object_unref (task);
5631 static GFileOutputStream *
5632 g_file_real_create_finish (GFile *file,
5636 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5638 return g_task_propagate_pointer (G_TASK (res), error);
5642 GFileOutputStream *stream;
5644 gboolean make_backup;
5645 GFileCreateFlags flags;
5649 replace_async_data_free (ReplaceAsyncData *data)
5652 g_object_unref (data->stream);
5653 g_free (data->etag);
5658 replace_async_thread (GTask *task,
5659 gpointer source_object,
5661 GCancellable *cancellable)
5663 GFileOutputStream *stream;
5664 ReplaceAsyncData *data = task_data;
5665 GError *error = NULL;
5667 stream = g_file_replace (G_FILE (source_object),
5675 g_task_return_pointer (task, stream, g_object_unref);
5677 g_task_return_error (task, error);
5681 g_file_real_replace_async (GFile *file,
5683 gboolean make_backup,
5684 GFileCreateFlags flags,
5686 GCancellable *cancellable,
5687 GAsyncReadyCallback callback,
5691 ReplaceAsyncData *data;
5693 data = g_new0 (ReplaceAsyncData, 1);
5694 data->etag = g_strdup (etag);
5695 data->make_backup = make_backup;
5696 data->flags = flags;
5698 task = g_task_new (file, cancellable, callback, user_data);
5699 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5700 g_task_set_priority (task, io_priority);
5702 g_task_run_in_thread (task, replace_async_thread);
5703 g_object_unref (task);
5706 static GFileOutputStream *
5707 g_file_real_replace_finish (GFile *file,
5711 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5713 return g_task_propagate_pointer (G_TASK (res), error);
5717 delete_async_thread (GTask *task,
5720 GCancellable *cancellable)
5722 GError *error = NULL;
5724 if (g_file_delete (G_FILE (object), cancellable, &error))
5725 g_task_return_boolean (task, TRUE);
5727 g_task_return_error (task, error);
5731 g_file_real_delete_async (GFile *file,
5733 GCancellable *cancellable,
5734 GAsyncReadyCallback callback,
5739 task = g_task_new (file, cancellable, callback, user_data);
5740 g_task_set_priority (task, io_priority);
5741 g_task_run_in_thread (task, delete_async_thread);
5742 g_object_unref (task);
5746 g_file_real_delete_finish (GFile *file,
5750 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5752 return g_task_propagate_boolean (G_TASK (res), error);
5756 trash_async_thread (GTask *task,
5759 GCancellable *cancellable)
5761 GError *error = NULL;
5763 if (g_file_trash (G_FILE (object), cancellable, &error))
5764 g_task_return_boolean (task, TRUE);
5766 g_task_return_error (task, error);
5770 g_file_real_trash_async (GFile *file,
5772 GCancellable *cancellable,
5773 GAsyncReadyCallback callback,
5778 task = g_task_new (file, cancellable, callback, user_data);
5779 g_task_set_priority (task, io_priority);
5780 g_task_run_in_thread (task, trash_async_thread);
5781 g_object_unref (task);
5785 g_file_real_trash_finish (GFile *file,
5789 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5791 return g_task_propagate_boolean (G_TASK (res), error);
5795 make_directory_async_thread (GTask *task,
5798 GCancellable *cancellable)
5800 GError *error = NULL;
5802 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5803 g_task_return_boolean (task, TRUE);
5805 g_task_return_error (task, error);
5809 g_file_real_make_directory_async (GFile *file,
5811 GCancellable *cancellable,
5812 GAsyncReadyCallback callback,
5817 task = g_task_new (file, cancellable, callback, user_data);
5818 g_task_set_priority (task, io_priority);
5819 g_task_run_in_thread (task, make_directory_async_thread);
5820 g_object_unref (task);
5824 g_file_real_make_directory_finish (GFile *file,
5828 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5830 return g_task_propagate_boolean (G_TASK (res), error);
5834 open_readwrite_async_thread (GTask *task,
5837 GCancellable *cancellable)
5839 GFileIOStream *stream;
5840 GError *error = NULL;
5842 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5845 g_task_return_error (task, error);
5847 g_task_return_pointer (task, stream, g_object_unref);
5851 g_file_real_open_readwrite_async (GFile *file,
5853 GCancellable *cancellable,
5854 GAsyncReadyCallback callback,
5859 task = g_task_new (file, cancellable, callback, user_data);
5860 g_task_set_priority (task, io_priority);
5862 g_task_run_in_thread (task, open_readwrite_async_thread);
5863 g_object_unref (task);
5866 static GFileIOStream *
5867 g_file_real_open_readwrite_finish (GFile *file,
5871 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5873 return g_task_propagate_pointer (G_TASK (res), error);
5877 create_readwrite_async_thread (GTask *task,
5880 GCancellable *cancellable)
5882 GFileCreateFlags *data = task_data;
5883 GFileIOStream *stream;
5884 GError *error = NULL;
5886 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5889 g_task_return_error (task, error);
5891 g_task_return_pointer (task, stream, g_object_unref);
5895 g_file_real_create_readwrite_async (GFile *file,
5896 GFileCreateFlags flags,
5898 GCancellable *cancellable,
5899 GAsyncReadyCallback callback,
5902 GFileCreateFlags *data;
5905 data = g_new0 (GFileCreateFlags, 1);
5908 task = g_task_new (file, cancellable, callback, user_data);
5909 g_task_set_task_data (task, data, g_free);
5910 g_task_set_priority (task, io_priority);
5912 g_task_run_in_thread (task, create_readwrite_async_thread);
5913 g_object_unref (task);
5916 static GFileIOStream *
5917 g_file_real_create_readwrite_finish (GFile *file,
5921 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5923 return g_task_propagate_pointer (G_TASK (res), error);
5928 gboolean make_backup;
5929 GFileCreateFlags flags;
5930 } ReplaceRWAsyncData;
5933 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5935 g_free (data->etag);
5940 replace_readwrite_async_thread (GTask *task,
5943 GCancellable *cancellable)
5945 GFileIOStream *stream;
5946 GError *error = NULL;
5947 ReplaceRWAsyncData *data = task_data;
5949 stream = g_file_replace_readwrite (G_FILE (object),
5957 g_task_return_error (task, error);
5959 g_task_return_pointer (task, stream, g_object_unref);
5963 g_file_real_replace_readwrite_async (GFile *file,
5965 gboolean make_backup,
5966 GFileCreateFlags flags,
5968 GCancellable *cancellable,
5969 GAsyncReadyCallback callback,
5973 ReplaceRWAsyncData *data;
5975 data = g_new0 (ReplaceRWAsyncData, 1);
5976 data->etag = g_strdup (etag);
5977 data->make_backup = make_backup;
5978 data->flags = flags;
5980 task = g_task_new (file, cancellable, callback, user_data);
5981 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5982 g_task_set_priority (task, io_priority);
5984 g_task_run_in_thread (task, replace_readwrite_async_thread);
5985 g_object_unref (task);
5988 static GFileIOStream *
5989 g_file_real_replace_readwrite_finish (GFile *file,
5993 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5995 return g_task_propagate_pointer (G_TASK (res), error);
5999 set_display_name_async_thread (GTask *task,
6002 GCancellable *cancellable)
6004 GError *error = NULL;
6005 char *name = task_data;
6008 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6011 g_task_return_error (task, error);
6013 g_task_return_pointer (task, file, g_object_unref);
6017 g_file_real_set_display_name_async (GFile *file,
6018 const char *display_name,
6020 GCancellable *cancellable,
6021 GAsyncReadyCallback callback,
6026 task = g_task_new (file, cancellable, callback, user_data);
6027 g_task_set_task_data (task, g_strdup (display_name), g_free);
6028 g_task_set_priority (task, io_priority);
6030 g_task_run_in_thread (task, set_display_name_async_thread);
6031 g_object_unref (task);
6035 g_file_real_set_display_name_finish (GFile *file,
6039 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6041 return g_task_propagate_pointer (G_TASK (res), error);
6045 GFileQueryInfoFlags flags;
6052 set_info_data_free (SetInfoAsyncData *data)
6055 g_object_unref (data->info);
6057 g_error_free (data->error);
6062 set_info_async_thread (GTask *task,
6065 GCancellable *cancellable)
6067 SetInfoAsyncData *data = task_data;
6070 data->res = g_file_set_attributes_from_info (G_FILE (object),
6078 g_file_real_set_attributes_async (GFile *file,
6080 GFileQueryInfoFlags flags,
6082 GCancellable *cancellable,
6083 GAsyncReadyCallback callback,
6087 SetInfoAsyncData *data;
6089 data = g_new0 (SetInfoAsyncData, 1);
6090 data->info = g_file_info_dup (info);
6091 data->flags = flags;
6093 task = g_task_new (file, cancellable, callback, user_data);
6094 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6095 g_task_set_priority (task, io_priority);
6097 g_task_run_in_thread (task, set_info_async_thread);
6098 g_object_unref (task);
6102 g_file_real_set_attributes_finish (GFile *file,
6107 SetInfoAsyncData *data;
6109 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6111 data = g_task_get_task_data (G_TASK (res));
6114 *info = g_object_ref (data->info);
6116 if (error != NULL && data->error)
6117 *error = g_error_copy (data->error);
6123 find_enclosing_mount_async_thread (GTask *task,
6126 GCancellable *cancellable)
6128 GError *error = NULL;
6131 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6134 g_task_return_error (task, error);
6136 g_task_return_pointer (task, mount, g_object_unref);
6140 g_file_real_find_enclosing_mount_async (GFile *file,
6142 GCancellable *cancellable,
6143 GAsyncReadyCallback callback,
6148 task = g_task_new (file, cancellable, callback, user_data);
6149 g_task_set_priority (task, io_priority);
6151 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6152 g_object_unref (task);
6156 g_file_real_find_enclosing_mount_finish (GFile *file,
6160 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6162 return g_task_propagate_pointer (G_TASK (res), error);
6169 GFileCopyFlags flags;
6170 GFileProgressCallback progress_cb;
6171 gpointer progress_cb_data;
6175 copy_async_data_free (CopyAsyncData *data)
6177 g_object_unref (data->source);
6178 g_object_unref (data->destination);
6179 g_slice_free (CopyAsyncData, data);
6183 CopyAsyncData *data;
6184 goffset current_num_bytes;
6185 goffset total_num_bytes;
6189 copy_async_progress_in_main (gpointer user_data)
6191 ProgressData *progress = user_data;
6192 CopyAsyncData *data = progress->data;
6194 data->progress_cb (progress->current_num_bytes,
6195 progress->total_num_bytes,
6196 data->progress_cb_data);
6202 copy_async_progress_callback (goffset current_num_bytes,
6203 goffset total_num_bytes,
6206 GTask *task = user_data;
6207 CopyAsyncData *data = g_task_get_task_data (task);
6208 ProgressData *progress;
6210 progress = g_new (ProgressData, 1);
6211 progress->data = data;
6212 progress->current_num_bytes = current_num_bytes;
6213 progress->total_num_bytes = total_num_bytes;
6215 g_main_context_invoke_full (g_task_get_context (task),
6216 g_task_get_priority (task),
6217 copy_async_progress_in_main,
6223 copy_async_thread (GTask *task,
6226 GCancellable *cancellable)
6228 CopyAsyncData *data = task_data;
6230 GError *error = NULL;
6232 result = g_file_copy (data->source,
6236 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6240 g_task_return_boolean (task, TRUE);
6242 g_task_return_error (task, error);
6246 g_file_real_copy_async (GFile *source,
6248 GFileCopyFlags flags,
6250 GCancellable *cancellable,
6251 GFileProgressCallback progress_callback,
6252 gpointer progress_callback_data,
6253 GAsyncReadyCallback callback,
6257 CopyAsyncData *data;
6259 data = g_slice_new (CopyAsyncData);
6260 data->source = g_object_ref (source);
6261 data->destination = g_object_ref (destination);
6262 data->flags = flags;
6263 data->progress_cb = progress_callback;
6264 data->progress_cb_data = progress_callback_data;
6266 task = g_task_new (source, cancellable, callback, user_data);
6267 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6268 g_task_set_priority (task, io_priority);
6269 g_task_run_in_thread (task, copy_async_thread);
6270 g_object_unref (task);
6274 g_file_real_copy_finish (GFile *file,
6278 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6280 return g_task_propagate_boolean (G_TASK (res), error);
6284 /********************************************
6285 * Default VFS operations *
6286 ********************************************/
6289 * g_file_new_for_path:
6290 * @path: a string containing a relative or absolute path.
6291 * The string must be encoded in the glib filename encoding.
6293 * Constructs a #GFile for a given path. This operation never
6294 * fails, but the returned object might not support any I/O
6295 * operation if @path is malformed.
6297 * Returns: (transfer full): a new #GFile for the given @path.
6298 * Free the returned object with g_object_unref().
6301 g_file_new_for_path (const char *path)
6303 g_return_val_if_fail (path != NULL, NULL);
6305 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6309 * g_file_new_for_uri:
6310 * @uri: a UTF-8 string containing a URI
6312 * Constructs a #GFile for a given URI. This operation never
6313 * fails, but the returned object might not support any I/O
6314 * operation if @uri is malformed or if the uri type is
6317 * Returns: (transfer full): a new #GFile for the given @uri.
6318 * Free the returned object with g_object_unref().
6321 g_file_new_for_uri (const char *uri)
6323 g_return_val_if_fail (uri != NULL, NULL);
6325 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6330 * @tmpl: (type filename) (allow-none): Template for the file
6331 * name, as in g_file_open_tmp(), or %NULL for a default template
6332 * @iostream: (out): on return, a #GFileIOStream for the created file
6333 * @error: a #GError, or %NULL
6335 * Opens a file in the preferred directory for temporary files (as
6336 * returned by g_get_tmp_dir()) and returns a #GFile and
6337 * #GFileIOStream pointing to it.
6339 * @tmpl should be a string in the GLib file name encoding
6340 * containing a sequence of six 'X' characters, and containing no
6341 * directory components. If it is %NULL, a default template is used.
6343 * Unlike the other #GFile constructors, this will return %NULL if
6344 * a temporary file could not be created.
6346 * Returns: (transfer full): a new #GFile.
6347 * Free the returned object with g_object_unref().
6352 g_file_new_tmp (const char *tmpl,
6353 GFileIOStream **iostream,
6359 GFileOutputStream *output;
6361 g_return_val_if_fail (iostream != NULL, NULL);
6363 fd = g_file_open_tmp (tmpl, &path, error);
6367 file = g_file_new_for_path (path);
6369 output = _g_local_file_output_stream_new (fd);
6370 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6372 g_object_unref (output);
6379 * g_file_parse_name:
6380 * @parse_name: a file name or path to be parsed
6382 * Constructs a #GFile with the given @parse_name (i.e. something
6383 * given by g_file_get_parse_name()). This operation never fails,
6384 * but the returned object might not support any I/O operation if
6385 * the @parse_name cannot be parsed.
6387 * Returns: (transfer full): a new #GFile.
6390 g_file_parse_name (const char *parse_name)
6392 g_return_val_if_fail (parse_name != NULL, NULL);
6394 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6398 is_valid_scheme_character (char c)
6400 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6403 /* Following RFC 2396, valid schemes are built like:
6404 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6407 has_valid_scheme (const char *uri)
6413 if (!g_ascii_isalpha (*p))
6418 } while (is_valid_scheme_character (*p));
6424 new_for_cmdline_arg (const gchar *arg,
6430 if (g_path_is_absolute (arg))
6431 return g_file_new_for_path (arg);
6433 if (has_valid_scheme (arg))
6434 return g_file_new_for_uri (arg);
6440 current_dir = g_get_current_dir ();
6441 filename = g_build_filename (current_dir, arg, NULL);
6442 g_free (current_dir);
6445 filename = g_build_filename (cwd, arg, NULL);
6447 file = g_file_new_for_path (filename);
6454 * g_file_new_for_commandline_arg:
6455 * @arg: a command line string
6457 * Creates a #GFile with the given argument from the command line.
6458 * The value of @arg can be either a URI, an absolute path or a
6459 * relative path resolved relative to the current working directory.
6460 * This operation never fails, but the returned object might not
6461 * support any I/O operation if @arg points to a malformed path.
6463 * Note that on Windows, this function expects its argument to be in
6464 * UTF-8 -- not the system code page. This means that you
6465 * should not use this function with string from argv as it is passed
6466 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6467 * the commandline. #GApplication also uses UTF-8 but
6468 * g_application_command_line_create_file_for_arg() may be more useful
6469 * for you there. It is also always possible to use this function with
6470 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6472 * Returns: (transfer full): a new #GFile.
6473 * Free the returned object with g_object_unref().
6476 g_file_new_for_commandline_arg (const char *arg)
6478 g_return_val_if_fail (arg != NULL, NULL);
6480 return new_for_cmdline_arg (arg, NULL);
6484 * g_file_new_for_commandline_arg_and_cwd:
6485 * @arg: a command line string
6486 * @cwd: the current working directory of the commandline
6488 * Creates a #GFile with the given argument from the command line.
6490 * This function is similar to g_file_new_for_commandline_arg() except
6491 * that it allows for passing the current working directory as an
6492 * argument instead of using the current working directory of the
6495 * This is useful if the commandline argument was given in a context
6496 * other than the invocation of the current process.
6498 * See also g_application_command_line_create_file_for_arg().
6500 * Returns: (transfer full): a new #GFile
6505 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6508 g_return_val_if_fail (arg != NULL, NULL);
6509 g_return_val_if_fail (cwd != NULL, NULL);
6511 return new_for_cmdline_arg (arg, cwd);
6515 * g_file_mount_enclosing_volume:
6516 * @location: input #GFile
6517 * @flags: flags affecting the operation
6518 * @mount_operation: (allow-none): a #GMountOperation
6519 * or %NULL to avoid user interaction
6520 * @cancellable: (allow-none): optional #GCancellable object,
6522 * @callback: (allow-none): a #GAsyncReadyCallback to call
6523 * when the request is satisfied, or %NULL
6524 * @user_data: the data to pass to callback function
6526 * Starts a @mount_operation, mounting the volume that contains
6527 * the file @location.
6529 * When this operation has completed, @callback will be called with
6530 * @user_user data, and the operation can be finalized with
6531 * g_file_mount_enclosing_volume_finish().
6533 * If @cancellable is not %NULL, then the operation can be cancelled by
6534 * triggering the cancellable object from another thread. If the operation
6535 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6538 g_file_mount_enclosing_volume (GFile *location,
6539 GMountMountFlags flags,
6540 GMountOperation *mount_operation,
6541 GCancellable *cancellable,
6542 GAsyncReadyCallback callback,
6547 g_return_if_fail (G_IS_FILE (location));
6549 iface = G_FILE_GET_IFACE (location);
6551 if (iface->mount_enclosing_volume == NULL)
6553 g_task_report_new_error (location, callback, user_data,
6554 g_file_mount_enclosing_volume,
6555 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6556 _("volume doesn't implement mount"));
6560 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6565 * g_file_mount_enclosing_volume_finish:
6566 * @location: input #GFile
6567 * @result: a #GAsyncResult
6568 * @error: a #GError, or %NULL
6570 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6572 * Returns: %TRUE if successful. If an error has occurred,
6573 * this function will return %FALSE and set @error
6574 * appropriately if present.
6577 g_file_mount_enclosing_volume_finish (GFile *location,
6578 GAsyncResult *result,
6583 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6584 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6586 if (g_async_result_legacy_propagate_error (result, error))
6588 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6589 return g_task_propagate_boolean (G_TASK (result), error);
6591 iface = G_FILE_GET_IFACE (location);
6593 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6596 /********************************************
6597 * Utility functions *
6598 ********************************************/
6601 * g_file_query_default_handler:
6602 * @file: a #GFile to open
6603 * @cancellable: optional #GCancellable object, %NULL to ignore
6604 * @error: a #GError, or %NULL
6606 * Returns the #GAppInfo that is registered as the default
6607 * application to handle the file specified by @file.
6609 * If @cancellable is not %NULL, then the operation can be cancelled by
6610 * triggering the cancellable object from another thread. If the operation
6611 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6613 * Returns: (transfer full): a #GAppInfo if the handle was found,
6614 * %NULL if there were errors.
6615 * When you are done with it, release it with g_object_unref()
6618 g_file_query_default_handler (GFile *file,
6619 GCancellable *cancellable,
6623 const char *content_type;
6628 uri_scheme = g_file_get_uri_scheme (file);
6629 if (uri_scheme && uri_scheme[0] != '\0')
6631 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6632 g_free (uri_scheme);
6634 if (appinfo != NULL)
6638 info = g_file_query_info (file,
6639 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6648 content_type = g_file_info_get_content_type (info);
6651 /* Don't use is_native(), as we want to support fuse paths if available */
6652 path = g_file_get_path (file);
6653 appinfo = g_app_info_get_default_for_type (content_type,
6658 g_object_unref (info);
6660 if (appinfo != NULL)
6663 g_set_error_literal (error, G_IO_ERROR,
6664 G_IO_ERROR_NOT_SUPPORTED,
6665 _("No application is registered as handling this file"));
6669 #define GET_CONTENT_BLOCK_SIZE 8192
6672 * g_file_load_contents:
6673 * @file: input #GFile
6674 * @cancellable: optional #GCancellable object, %NULL to ignore
6675 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6676 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6677 * or %NULL if the length is not needed
6678 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6679 * or %NULL if the entity tag is not needed
6680 * @error: a #GError, or %NULL
6682 * Loads the content of the file into memory. The data is always
6683 * zero-terminated, but this is not included in the resultant @length.
6684 * The returned @content should be freed with g_free() when no longer
6687 * If @cancellable is not %NULL, then the operation can be cancelled by
6688 * triggering the cancellable object from another thread. If the operation
6689 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6691 * Returns: %TRUE if the @file's contents were successfully loaded.
6692 * %FALSE if there were errors.
6695 g_file_load_contents (GFile *file,
6696 GCancellable *cancellable,
6702 GFileInputStream *in;
6703 GByteArray *content;
6708 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6709 g_return_val_if_fail (contents != NULL, FALSE);
6711 in = g_file_read (file, cancellable, error);
6715 content = g_byte_array_new ();
6718 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6719 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6720 content->data + pos,
6721 GET_CONTENT_BLOCK_SIZE,
6722 cancellable, error)) > 0)
6725 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6732 info = g_file_input_stream_query_info (in,
6733 G_FILE_ATTRIBUTE_ETAG_VALUE,
6738 *etag_out = g_strdup (g_file_info_get_etag (info));
6739 g_object_unref (info);
6743 /* Ignore errors on close */
6744 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6745 g_object_unref (in);
6749 /* error is set already */
6750 g_byte_array_free (content, TRUE);
6757 /* Zero terminate (we got an extra byte allocated for this */
6758 content->data[pos] = 0;
6760 *contents = (char *)g_byte_array_free (content, FALSE);
6767 GFileReadMoreCallback read_more_callback;
6768 GByteArray *content;
6775 load_contents_data_free (LoadContentsData *data)
6778 g_byte_array_free (data->content, TRUE);
6779 g_free (data->etag);
6784 load_contents_close_callback (GObject *obj,
6785 GAsyncResult *close_res,
6788 GInputStream *stream = G_INPUT_STREAM (obj);
6789 LoadContentsData *data = user_data;
6791 /* Ignore errors here, we're only reading anyway */
6792 g_input_stream_close_finish (stream, close_res, NULL);
6793 g_object_unref (stream);
6795 g_task_return_boolean (data->task, TRUE);
6796 g_object_unref (data->task);
6800 load_contents_fstat_callback (GObject *obj,
6801 GAsyncResult *stat_res,
6804 GInputStream *stream = G_INPUT_STREAM (obj);
6805 LoadContentsData *data = user_data;
6808 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6812 data->etag = g_strdup (g_file_info_get_etag (info));
6813 g_object_unref (info);
6816 g_input_stream_close_async (stream, 0,
6817 g_task_get_cancellable (data->task),
6818 load_contents_close_callback, data);
6822 load_contents_read_callback (GObject *obj,
6823 GAsyncResult *read_res,
6826 GInputStream *stream = G_INPUT_STREAM (obj);
6827 LoadContentsData *data = user_data;
6828 GError *error = NULL;
6831 read_size = g_input_stream_read_finish (stream, read_res, &error);
6835 g_task_return_error (data->task, error);
6836 g_object_unref (data->task);
6838 /* Close the file ignoring any error */
6839 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6840 g_object_unref (stream);
6842 else if (read_size == 0)
6844 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6845 G_FILE_ATTRIBUTE_ETAG_VALUE,
6847 g_task_get_cancellable (data->task),
6848 load_contents_fstat_callback,
6851 else if (read_size > 0)
6853 data->pos += read_size;
6855 g_byte_array_set_size (data->content,
6856 data->pos + GET_CONTENT_BLOCK_SIZE);
6859 if (data->read_more_callback &&
6860 !data->read_more_callback ((char *)data->content->data, data->pos,
6861 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6862 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6863 G_FILE_ATTRIBUTE_ETAG_VALUE,
6865 g_task_get_cancellable (data->task),
6866 load_contents_fstat_callback,
6869 g_input_stream_read_async (stream,
6870 data->content->data + data->pos,
6871 GET_CONTENT_BLOCK_SIZE,
6873 g_task_get_cancellable (data->task),
6874 load_contents_read_callback,
6880 load_contents_open_callback (GObject *obj,
6881 GAsyncResult *open_res,
6884 GFile *file = G_FILE (obj);
6885 GFileInputStream *stream;
6886 LoadContentsData *data = user_data;
6887 GError *error = NULL;
6889 stream = g_file_read_finish (file, open_res, &error);
6893 g_byte_array_set_size (data->content,
6894 data->pos + GET_CONTENT_BLOCK_SIZE);
6895 g_input_stream_read_async (G_INPUT_STREAM (stream),
6896 data->content->data + data->pos,
6897 GET_CONTENT_BLOCK_SIZE,
6899 g_task_get_cancellable (data->task),
6900 load_contents_read_callback,
6905 g_task_return_error (data->task, error);
6906 g_object_unref (data->task);
6911 * g_file_load_partial_contents_async: (skip)
6912 * @file: input #GFile
6913 * @cancellable: optional #GCancellable object, %NULL to ignore
6914 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6915 * and to specify whether further data should be read
6916 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6917 * @user_data: the data to pass to the callback functions
6919 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6920 * be used to stop reading from the file when appropriate, else this
6921 * function will behave exactly as g_file_load_contents_async(). This
6922 * operation can be finished by g_file_load_partial_contents_finish().
6924 * Users of this function should be aware that @user_data is passed to
6925 * both the @read_more_callback and the @callback.
6927 * If @cancellable is not %NULL, then the operation can be cancelled by
6928 * triggering the cancellable object from another thread. If the operation
6929 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6932 g_file_load_partial_contents_async (GFile *file,
6933 GCancellable *cancellable,
6934 GFileReadMoreCallback read_more_callback,
6935 GAsyncReadyCallback callback,
6938 LoadContentsData *data;
6940 g_return_if_fail (G_IS_FILE (file));
6942 data = g_new0 (LoadContentsData, 1);
6943 data->read_more_callback = read_more_callback;
6944 data->content = g_byte_array_new ();
6946 data->task = g_task_new (file, cancellable, callback, user_data);
6947 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6949 g_file_read_async (file,
6951 g_task_get_cancellable (data->task),
6952 load_contents_open_callback,
6957 * g_file_load_partial_contents_finish:
6958 * @file: input #GFile
6959 * @res: a #GAsyncResult
6960 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6961 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6962 * or %NULL if the length is not needed
6963 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6964 * or %NULL if the entity tag is not needed
6965 * @error: a #GError, or %NULL
6967 * Finishes an asynchronous partial load operation that was started
6968 * with g_file_load_partial_contents_async(). The data is always
6969 * zero-terminated, but this is not included in the resultant @length.
6970 * The returned @content should be freed with g_free() when no longer
6973 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6974 * present, it will be set appropriately.
6977 g_file_load_partial_contents_finish (GFile *file,
6985 LoadContentsData *data;
6987 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6988 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6989 g_return_val_if_fail (contents != NULL, FALSE);
6991 task = G_TASK (res);
6993 if (!g_task_propagate_boolean (task, error))
7000 data = g_task_get_task_data (task);
7003 *length = data->pos;
7007 *etag_out = data->etag;
7011 /* Zero terminate */
7012 g_byte_array_set_size (data->content, data->pos + 1);
7013 data->content->data[data->pos] = 0;
7015 *contents = (char *)g_byte_array_free (data->content, FALSE);
7016 data->content = NULL;
7022 * g_file_load_contents_async:
7023 * @file: input #GFile
7024 * @cancellable: optional #GCancellable object, %NULL to ignore
7025 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7026 * @user_data: the data to pass to callback function
7028 * Starts an asynchronous load of the @file's contents.
7030 * For more details, see g_file_load_contents() which is
7031 * the synchronous version of this call.
7033 * When the load operation has completed, @callback will be called
7034 * with @user data. To finish the operation, call
7035 * g_file_load_contents_finish() with the #GAsyncResult returned by
7038 * If @cancellable is not %NULL, then the operation can be cancelled by
7039 * triggering the cancellable object from another thread. If the operation
7040 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7043 g_file_load_contents_async (GFile *file,
7044 GCancellable *cancellable,
7045 GAsyncReadyCallback callback,
7048 g_file_load_partial_contents_async (file,
7051 callback, user_data);
7055 * g_file_load_contents_finish:
7056 * @file: input #GFile
7057 * @res: a #GAsyncResult
7058 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7059 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7060 * or %NULL if the length is not needed
7061 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7062 * or %NULL if the entity tag is not needed
7063 * @error: a #GError, or %NULL
7065 * Finishes an asynchronous load of the @file's contents.
7066 * The contents are placed in @contents, and @length is set to the
7067 * size of the @contents string. The @content should be freed with
7068 * g_free() when no longer needed. If @etag_out is present, it will be
7069 * set to the new entity tag for the @file.
7071 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7072 * present, it will be set appropriately.
7075 g_file_load_contents_finish (GFile *file,
7082 return g_file_load_partial_contents_finish (file,
7091 * g_file_replace_contents:
7092 * @file: input #GFile
7093 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7094 * @length: the length of @contents in bytes
7095 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7096 * for the document, or %NULL
7097 * @make_backup: %TRUE if a backup should be created
7098 * @flags: a set of #GFileCreateFlags
7099 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7100 * for the document. This should be freed with g_free() when no longer
7102 * @cancellable: optional #GCancellable object, %NULL to ignore
7103 * @error: a #GError, or %NULL
7105 * Replaces the contents of @file with @contents of @length bytes.
7107 * If @etag is specified (not %NULL), any existing file must have that etag,
7108 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7110 * If @make_backup is %TRUE, this function will attempt to make a backup
7113 * If @cancellable is not %NULL, then the operation can be cancelled by
7114 * triggering the cancellable object from another thread. If the operation
7115 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7117 * The returned @new_etag can be used to verify that the file hasn't
7118 * changed the next time it is saved over.
7120 * Returns: %TRUE if successful. If an error has occurred, this function
7121 * will return %FALSE and set @error appropriately if present.
7124 g_file_replace_contents (GFile *file,
7125 const char *contents,
7128 gboolean make_backup,
7129 GFileCreateFlags flags,
7131 GCancellable *cancellable,
7134 GFileOutputStream *out;
7135 gsize pos, remainder;
7139 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7140 g_return_val_if_fail (contents != NULL, FALSE);
7142 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7148 while (remainder > 0 &&
7149 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7151 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7159 if (remainder > 0 && res < 0)
7161 /* Ignore errors on close */
7162 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7163 g_object_unref (out);
7165 /* error is set already */
7169 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7172 *new_etag = g_file_output_stream_get_etag (out);
7174 g_object_unref (out);
7185 } ReplaceContentsData;
7188 replace_contents_data_free (ReplaceContentsData *data)
7190 g_bytes_unref (data->content);
7191 g_free (data->etag);
7196 replace_contents_close_callback (GObject *obj,
7197 GAsyncResult *close_res,
7200 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7201 ReplaceContentsData *data = user_data;
7203 /* Ignore errors here, we're only reading anyway */
7204 g_output_stream_close_finish (stream, close_res, NULL);
7205 g_object_unref (stream);
7209 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7210 g_task_return_boolean (data->task, TRUE);
7212 g_object_unref (data->task);
7216 replace_contents_write_callback (GObject *obj,
7217 GAsyncResult *read_res,
7220 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7221 ReplaceContentsData *data = user_data;
7222 GError *error = NULL;
7225 write_size = g_output_stream_write_finish (stream, read_res, &error);
7227 if (write_size <= 0)
7229 /* Error or EOF, close the file */
7232 data->failed = TRUE;
7233 g_task_return_error (data->task, error);
7235 g_output_stream_close_async (stream, 0,
7236 g_task_get_cancellable (data->task),
7237 replace_contents_close_callback, data);
7239 else if (write_size > 0)
7241 const gchar *content;
7244 content = g_bytes_get_data (data->content, &length);
7245 data->pos += write_size;
7247 if (data->pos >= length)
7248 g_output_stream_close_async (stream, 0,
7249 g_task_get_cancellable (data->task),
7250 replace_contents_close_callback, data);
7252 g_output_stream_write_async (stream,
7253 content + data->pos,
7256 g_task_get_cancellable (data->task),
7257 replace_contents_write_callback,
7263 replace_contents_open_callback (GObject *obj,
7264 GAsyncResult *open_res,
7267 GFile *file = G_FILE (obj);
7268 GFileOutputStream *stream;
7269 ReplaceContentsData *data = user_data;
7270 GError *error = NULL;
7272 stream = g_file_replace_finish (file, open_res, &error);
7276 const gchar *content;
7279 content = g_bytes_get_data (data->content, &length);
7280 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7281 content + data->pos,
7284 g_task_get_cancellable (data->task),
7285 replace_contents_write_callback,
7290 g_task_return_error (data->task, error);
7291 g_object_unref (data->task);
7296 * g_file_replace_contents_async:
7297 * @file: input #GFile
7298 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7299 * @length: the length of @contents in bytes
7300 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7301 * @make_backup: %TRUE if a backup should be created
7302 * @flags: a set of #GFileCreateFlags
7303 * @cancellable: optional #GCancellable object, %NULL to ignore
7304 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7305 * @user_data: the data to pass to callback function
7307 * Starts an asynchronous replacement of @file with the given
7308 * @contents of @length bytes. @etag will replace the document's
7309 * current entity tag.
7311 * When this operation has completed, @callback will be called with
7312 * @user_user data, and the operation can be finalized with
7313 * g_file_replace_contents_finish().
7315 * If @cancellable is not %NULL, then the operation can be cancelled by
7316 * triggering the cancellable object from another thread. If the operation
7317 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7319 * If @make_backup is %TRUE, this function will attempt to
7320 * make a backup of @file.
7322 * Note that no copy of @content will be made, so it must stay valid
7323 * until @callback is called. See g_file_replace_contents_bytes_async()
7324 * for a #GBytes version that will automatically hold a reference to the
7325 * contents (without copying) for the duration of the call.
7328 g_file_replace_contents_async (GFile *file,
7329 const char *contents,
7332 gboolean make_backup,
7333 GFileCreateFlags flags,
7334 GCancellable *cancellable,
7335 GAsyncReadyCallback callback,
7340 bytes = g_bytes_new_static (contents, length);
7341 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7342 cancellable, callback, user_data);
7343 g_bytes_unref (bytes);
7347 * g_file_replace_contents_bytes_async:
7348 * @file: input #GFile
7349 * @contents: a #GBytes
7350 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7351 * @make_backup: %TRUE if a backup should be created
7352 * @flags: a set of #GFileCreateFlags
7353 * @cancellable: optional #GCancellable object, %NULL to ignore
7354 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7355 * @user_data: the data to pass to callback function
7357 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7358 * This function will keep a ref on @contents until the operation is done.
7359 * Unlike g_file_replace_contents_async() this allows forgetting about the
7360 * content without waiting for the callback.
7362 * When this operation has completed, @callback will be called with
7363 * @user_user data, and the operation can be finalized with
7364 * g_file_replace_contents_finish().
7369 g_file_replace_contents_bytes_async (GFile *file,
7372 gboolean make_backup,
7373 GFileCreateFlags flags,
7374 GCancellable *cancellable,
7375 GAsyncReadyCallback callback,
7378 ReplaceContentsData *data;
7380 g_return_if_fail (G_IS_FILE (file));
7381 g_return_if_fail (contents != NULL);
7383 data = g_new0 (ReplaceContentsData, 1);
7385 data->content = g_bytes_ref (contents);
7387 data->task = g_task_new (file, cancellable, callback, user_data);
7388 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7390 g_file_replace_async (file,
7395 g_task_get_cancellable (data->task),
7396 replace_contents_open_callback,
7401 * g_file_replace_contents_finish:
7402 * @file: input #GFile
7403 * @res: a #GAsyncResult
7404 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7405 * for the document. This should be freed with g_free() when it is no
7406 * longer needed, or %NULL
7407 * @error: a #GError, or %NULL
7409 * Finishes an asynchronous replace of the given @file. See
7410 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7411 * tag for the document, if present.
7413 * Returns: %TRUE on success, %FALSE on failure.
7416 g_file_replace_contents_finish (GFile *file,
7422 ReplaceContentsData *data;
7424 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7425 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7427 task = G_TASK (res);
7429 if (!g_task_propagate_boolean (task, error))
7432 data = g_task_get_task_data (task);
7436 *new_etag = data->etag;
7437 data->etag = NULL; /* Take ownership */
7444 g_file_real_measure_disk_usage (GFile *file,
7445 GFileMeasureFlags flags,
7446 GCancellable *cancellable,
7447 GFileMeasureProgressCallback progress_callback,
7448 gpointer progress_data,
7449 guint64 *disk_usage,
7454 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7455 "Operation not supported for the current backend.");
7461 GFileMeasureFlags flags;
7462 GFileMeasureProgressCallback progress_callback;
7463 gpointer progress_data;
7475 GFileMeasureProgressCallback callback;
7478 guint64 current_size;
7484 measure_disk_usage_invoke_progress (gpointer user_data)
7486 MeasureProgress *progress = user_data;
7488 (* progress->callback) (progress->reporting,
7489 progress->current_size, progress->num_dirs, progress->num_files,
7490 progress->user_data);
7496 measure_disk_usage_progress (gboolean reporting,
7497 guint64 current_size,
7502 MeasureProgress progress;
7503 GTask *task = user_data;
7504 MeasureTaskData *data;
7506 data = g_task_get_task_data (task);
7508 progress.callback = data->progress_callback;
7509 progress.user_data = data->progress_data;
7510 progress.reporting = reporting;
7511 progress.current_size = current_size;
7512 progress.num_dirs = num_dirs;
7513 progress.num_files = num_files;
7515 g_main_context_invoke_full (g_task_get_context (task),
7516 g_task_get_priority (task),
7517 measure_disk_usage_invoke_progress,
7518 g_memdup (&progress, sizeof progress),
7523 measure_disk_usage_thread (GTask *task,
7524 gpointer source_object,
7526 GCancellable *cancellable)
7528 MeasureTaskData *data = task_data;
7529 GError *error = NULL;
7530 MeasureResult result;
7532 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7533 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7534 &result.disk_usage, &result.num_dirs, &result.num_files,
7536 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7538 g_task_return_error (task, error);
7542 g_file_real_measure_disk_usage_async (GFile *file,
7543 GFileMeasureFlags flags,
7545 GCancellable *cancellable,
7546 GFileMeasureProgressCallback progress_callback,
7547 gpointer progress_data,
7548 GAsyncReadyCallback callback,
7551 MeasureTaskData data;
7555 data.progress_callback = progress_callback;
7556 data.progress_data = progress_data;
7558 task = g_task_new (file, cancellable, callback, user_data);
7559 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7560 g_task_set_priority (task, io_priority);
7562 g_task_run_in_thread (task, measure_disk_usage_thread);
7563 g_object_unref (task);
7567 g_file_real_measure_disk_usage_finish (GFile *file,
7568 GAsyncResult *result,
7569 guint64 *disk_usage,
7574 MeasureResult *measure_result;
7576 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7578 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7580 if (measure_result == NULL)
7584 *disk_usage = measure_result->disk_usage;
7587 *num_dirs = measure_result->num_dirs;
7590 *num_files = measure_result->num_files;
7592 g_free (measure_result);
7598 * g_file_measure_disk_usage:
7600 * @flags: #GFileMeasureFlags
7601 * @cancellable: (allow-none): optional #GCancellable
7602 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7603 * @progress_data: user_data for @progress_callback
7604 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7605 * @num_dirs: (allow-none) (out): the number of directories encountered
7606 * @num_files: (allow-none) (out): the number of non-directories encountered
7607 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7609 * Recursively measures the disk usage of @file.
7611 * This is essentially an analog of the '<literal>du</literal>' command,
7612 * but it also reports the number of directories and non-directory files
7613 * encountered (including things like symbolic links).
7615 * By default, errors are only reported against the toplevel file
7616 * itself. Errors found while recursing are silently ignored, unless
7617 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7619 * The returned size, @disk_usage, is in bytes and should be formatted
7620 * with g_format_size() in order to get something reasonable for showing
7621 * in a user interface.
7623 * @progress_callback and @progress_data can be given to request
7624 * periodic progress updates while scanning. See the documentation for
7625 * #GFileMeasureProgressCallback for information about when and how the
7626 * callback will be invoked.
7628 * Returns: %TRUE if successful, with the out parameters set.
7629 * %FALSE otherwise, with @error set.
7634 g_file_measure_disk_usage (GFile *file,
7635 GFileMeasureFlags flags,
7636 GCancellable *cancellable,
7637 GFileMeasureProgressCallback progress_callback,
7638 gpointer progress_data,
7639 guint64 *disk_usage,
7644 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7645 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7646 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7648 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7649 progress_callback, progress_data,
7650 disk_usage, num_dirs, num_files,
7655 * g_file_measure_disk_usage_async:
7657 * @flags: #GFileMeasureFlags
7658 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7660 * @cancellable: (allow-none): optional #GCancellable
7661 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7662 * @progress_data: user_data for @progress_callback
7663 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7664 * @user_data: the data to pass to callback function
7666 * Recursively measures the disk usage of @file.
7668 * This is the asynchronous version of g_file_measure_disk_usage(). See
7669 * there for more information.
7674 g_file_measure_disk_usage_async (GFile *file,
7675 GFileMeasureFlags flags,
7677 GCancellable *cancellable,
7678 GFileMeasureProgressCallback progress_callback,
7679 gpointer progress_data,
7680 GAsyncReadyCallback callback,
7683 g_return_if_fail (G_IS_FILE (file));
7684 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7686 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7687 progress_callback, progress_data,
7688 callback, user_data);
7692 * g_file_measure_disk_usage_finish:
7694 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7695 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7696 * @num_dirs: (allow-none) (out): the number of directories encountered
7697 * @num_files: (allow-none) (out): the number of non-directories encountered
7698 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7700 * Collects the results from an earlier call to
7701 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7704 * Returns: %TRUE if successful, with the out parameters set.
7705 * %FALSE otherwise, with @error set.
7710 g_file_measure_disk_usage_finish (GFile *file,
7711 GAsyncResult *result,
7712 guint64 *disk_usage,
7717 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7718 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7720 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7724 * g_file_start_mountable:
7725 * @file: input #GFile
7726 * @flags: flags affecting the operation
7727 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7728 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7729 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7730 * @user_data: the data to pass to callback function
7732 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7733 * Using @start_operation, you can request callbacks when, for instance,
7734 * passwords are needed during authentication.
7736 * If @cancellable is not %NULL, then the operation can be cancelled by
7737 * triggering the cancellable object from another thread. If the operation
7738 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7740 * When the operation is finished, @callback will be called.
7741 * You can then call g_file_mount_mountable_finish() to get
7742 * the result of the operation.
7747 g_file_start_mountable (GFile *file,
7748 GDriveStartFlags flags,
7749 GMountOperation *start_operation,
7750 GCancellable *cancellable,
7751 GAsyncReadyCallback callback,
7756 g_return_if_fail (G_IS_FILE (file));
7758 iface = G_FILE_GET_IFACE (file);
7760 if (iface->start_mountable == NULL)
7762 g_task_report_new_error (file, callback, user_data,
7763 g_file_start_mountable,
7764 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7765 _("Operation not supported"));
7769 (* iface->start_mountable) (file,
7778 * g_file_start_mountable_finish:
7779 * @file: input #GFile
7780 * @result: a #GAsyncResult
7781 * @error: a #GError, or %NULL
7783 * Finishes a start operation. See g_file_start_mountable() for details.
7785 * Finish an asynchronous start operation that was started
7786 * with g_file_start_mountable().
7788 * Returns: %TRUE if the operation finished successfully. %FALSE
7794 g_file_start_mountable_finish (GFile *file,
7795 GAsyncResult *result,
7800 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7801 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7803 if (g_async_result_legacy_propagate_error (result, error))
7805 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7806 return g_task_propagate_boolean (G_TASK (result), error);
7808 iface = G_FILE_GET_IFACE (file);
7809 return (* iface->start_mountable_finish) (file, result, error);
7813 * g_file_stop_mountable:
7814 * @file: input #GFile
7815 * @flags: flags affecting the operation
7816 * @mount_operation: (allow-none): a #GMountOperation,
7817 * or %NULL to avoid user interaction.
7818 * @cancellable: (allow-none): optional #GCancellable object,
7820 * @callback: (allow-none): a #GAsyncReadyCallback to call
7821 * when the request is satisfied, or %NULL
7822 * @user_data: the data to pass to callback function
7824 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7826 * If @cancellable is not %NULL, then the operation can be cancelled by
7827 * triggering the cancellable object from another thread. If the operation
7828 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7830 * When the operation is finished, @callback will be called.
7831 * You can then call g_file_stop_mountable_finish() to get
7832 * the result of the operation.
7837 g_file_stop_mountable (GFile *file,
7838 GMountUnmountFlags flags,
7839 GMountOperation *mount_operation,
7840 GCancellable *cancellable,
7841 GAsyncReadyCallback callback,
7846 g_return_if_fail (G_IS_FILE (file));
7848 iface = G_FILE_GET_IFACE (file);
7850 if (iface->stop_mountable == NULL)
7852 g_task_report_new_error (file, callback, user_data,
7853 g_file_stop_mountable,
7854 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7855 _("Operation not supported"));
7859 (* iface->stop_mountable) (file,
7868 * g_file_stop_mountable_finish:
7869 * @file: input #GFile
7870 * @result: a #GAsyncResult
7871 * @error: a #GError, or %NULL
7873 * Finishes an stop operation, see g_file_stop_mountable() for details.
7875 * Finish an asynchronous stop operation that was started
7876 * with g_file_stop_mountable().
7878 * Returns: %TRUE if the operation finished successfully.
7884 g_file_stop_mountable_finish (GFile *file,
7885 GAsyncResult *result,
7890 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7891 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7893 if (g_async_result_legacy_propagate_error (result, error))
7895 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7896 return g_task_propagate_boolean (G_TASK (result), error);
7898 iface = G_FILE_GET_IFACE (file);
7899 return (* iface->stop_mountable_finish) (file, result, error);
7903 * g_file_poll_mountable:
7904 * @file: input #GFile
7905 * @cancellable: optional #GCancellable object, %NULL to ignore
7906 * @callback: (allow-none): a #GAsyncReadyCallback to call
7907 * when the request is satisfied, or %NULL
7908 * @user_data: the data to pass to callback function
7910 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7912 * If @cancellable is not %NULL, then the operation can be cancelled by
7913 * triggering the cancellable object from another thread. If the operation
7914 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7916 * When the operation is finished, @callback will be called.
7917 * You can then call g_file_mount_mountable_finish() to get
7918 * the result of the operation.
7923 g_file_poll_mountable (GFile *file,
7924 GCancellable *cancellable,
7925 GAsyncReadyCallback callback,
7930 g_return_if_fail (G_IS_FILE (file));
7932 iface = G_FILE_GET_IFACE (file);
7934 if (iface->poll_mountable == NULL)
7936 g_task_report_new_error (file, callback, user_data,
7937 g_file_poll_mountable,
7938 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7939 _("Operation not supported"));
7943 (* iface->poll_mountable) (file,
7950 * g_file_poll_mountable_finish:
7951 * @file: input #GFile
7952 * @result: a #GAsyncResult
7953 * @error: a #GError, or %NULL
7955 * Finishes a poll operation. See g_file_poll_mountable() for details.
7957 * Finish an asynchronous poll operation that was polled
7958 * with g_file_poll_mountable().
7960 * Returns: %TRUE if the operation finished successfully. %FALSE
7966 g_file_poll_mountable_finish (GFile *file,
7967 GAsyncResult *result,
7972 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7973 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7975 if (g_async_result_legacy_propagate_error (result, error))
7977 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7978 return g_task_propagate_boolean (G_TASK (result), error);
7980 iface = G_FILE_GET_IFACE (file);
7981 return (* iface->poll_mountable_finish) (file, result, error);
7985 * g_file_supports_thread_contexts:
7988 * Checks if @file supports <link
7989 * linkend="g-main-context-push-thread-default-context">thread-default
7990 * contexts</link>. If this returns %FALSE, you cannot perform
7991 * asynchronous operations on @file in a thread that has a
7992 * thread-default context.
7994 * Returns: Whether or not @file supports thread-default contexts.
7999 g_file_supports_thread_contexts (GFile *file)
8003 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8005 iface = G_FILE_GET_IFACE (file);
8006 return iface->supports_thread_contexts;