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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <sys/ioctl.h>
30 /* See linux.git/fs/btrfs/ioctl.h */
31 #define BTRFS_IOCTL_MAGIC 0x94
32 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
43 #include <sys/types.h>
49 #include "glib/gstdio.h"
51 #include "glib-unix.h"
55 #include "gfileattribute-priv.h"
56 #include "gfiledescriptorbased.h"
57 #include "gpollfilemonitor.h"
59 #include "gfileinputstream.h"
60 #include "gfileoutputstream.h"
61 #include "glocalfileoutputstream.h"
62 #include "glocalfileiostream.h"
63 #include "glocalfile.h"
64 #include "gcancellable.h"
65 #include "gasyncresult.h"
72 * @short_description: File and Directory Handling
74 * @see_also: #GFileInfo, #GFileEnumerator
76 * #GFile is a high level abstraction for manipulating files on a
77 * virtual file system. #GFiles are lightweight, immutable objects
78 * that do no I/O upon creation. It is necessary to understand that
79 * #GFile objects do not represent files, merely an identifier for a
80 * file. All file content I/O is implemented as streaming operations
81 * (see #GInputStream and #GOutputStream).
83 * To construct a #GFile, you can use:
85 * <member>g_file_new_for_path() if you have a path.</member>
86 * <member>g_file_new_for_uri() if you have a URI.</member>
87 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
88 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
89 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
92 * One way to think of a #GFile is as an abstraction of a pathname. For
93 * normal files the system pathname is what is stored internally, but as
94 * #GFiles are extensible it could also be something else that corresponds
95 * to a pathname in a userspace implementation of a filesystem.
97 * #GFiles make up hierarchies of directories and files that correspond to
98 * the files on a filesystem. You can move through the file system with
99 * #GFile using g_file_get_parent() to get an identifier for the parent
100 * directory, g_file_get_child() to get a child within a directory,
101 * g_file_resolve_relative_path() to resolve a relative path between two
102 * #GFiles. There can be multiple hierarchies, so you may not end up at
103 * the same root if you repeatedly call g_file_get_parent() on two different
106 * All #GFiles have a basename (get with g_file_get_basename()). These names
107 * are byte strings that are used to identify the file on the filesystem
108 * (relative to its parent directory) and there is no guarantees that they
109 * have any particular charset encoding or even make any sense at all. If
110 * you want to use filenames in a user interface you should use the display
111 * name that you can get by requesting the
112 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
113 * This is guaranteed to be in UTF-8 and can be used in a user interface.
114 * But always store the real basename or the #GFile to use to actually
115 * access the file, because there is no way to go from a display name to
118 * Using #GFile as an identifier has the same weaknesses as using a path
119 * in that there may be multiple aliases for the same file. For instance,
120 * hard or soft links may cause two different #GFiles to refer to the same
121 * file. Other possible causes for aliases are: case insensitive filesystems,
122 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
123 * check if two #GFiles point to the same file you can query for the
124 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
125 * canonicalization of pathnames passed in, so that trivial differences in
126 * the path string used at creation (duplicated slashes, slash at end of
127 * path, "." or ".." path segments, etc) does not create different #GFiles.
129 * Many #GFile operations have both synchronous and asynchronous versions
130 * to suit your application. Asynchronous versions of synchronous functions
131 * simply have _async() appended to their function names. The asynchronous
132 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
133 * the operation, producing a GAsyncResult which is then passed to the
134 * function's matching _finish() operation.
136 * Some #GFile operations do not have synchronous analogs, as they may
137 * take a very long time to finish, and blocking may leave an application
138 * unusable. Notable cases include:
140 * <member>g_file_mount_mountable() to mount a mountable file.</member>
141 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
142 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
145 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
146 * One notable feature of #GFiles are entity tags, or "etags" for
147 * short. Entity tags are somewhat like a more abstract version of the
148 * traditional mtime, and can be used to quickly determine if the file has
149 * been modified from the version on the file system. See the HTTP 1.1
150 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
151 * for HTTP Etag headers, which are a very similar concept.
155 static void g_file_real_query_info_async (GFile *file,
156 const char *attributes,
157 GFileQueryInfoFlags flags,
159 GCancellable *cancellable,
160 GAsyncReadyCallback callback,
162 static GFileInfo * g_file_real_query_info_finish (GFile *file,
165 static void g_file_real_query_filesystem_info_async (GFile *file,
166 const char *attributes,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
171 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
174 static void g_file_real_enumerate_children_async (GFile *file,
175 const char *attributes,
176 GFileQueryInfoFlags flags,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
184 static void g_file_real_read_async (GFile *file,
186 GCancellable *cancellable,
187 GAsyncReadyCallback callback,
189 static GFileInputStream * g_file_real_read_finish (GFile *file,
192 static void g_file_real_append_to_async (GFile *file,
193 GFileCreateFlags flags,
195 GCancellable *cancellable,
196 GAsyncReadyCallback callback,
198 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
201 static void g_file_real_create_async (GFile *file,
202 GFileCreateFlags flags,
204 GCancellable *cancellable,
205 GAsyncReadyCallback callback,
207 static GFileOutputStream *g_file_real_create_finish (GFile *file,
210 static void g_file_real_replace_async (GFile *file,
212 gboolean make_backup,
213 GFileCreateFlags flags,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
221 static void g_file_real_delete_async (GFile *file,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
226 static gboolean g_file_real_delete_finish (GFile *file,
229 static void g_file_real_trash_async (GFile *file,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static gboolean g_file_real_trash_finish (GFile *file,
237 static void g_file_real_make_directory_async (GFile *file,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static gboolean g_file_real_make_directory_finish (GFile *file,
245 static void g_file_real_open_readwrite_async (GFile *file,
247 GCancellable *cancellable,
248 GAsyncReadyCallback callback,
250 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
253 static void g_file_real_create_readwrite_async (GFile *file,
254 GFileCreateFlags flags,
256 GCancellable *cancellable,
257 GAsyncReadyCallback callback,
259 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
262 static void g_file_real_replace_readwrite_async (GFile *file,
264 gboolean make_backup,
265 GFileCreateFlags flags,
267 GCancellable *cancellable,
268 GAsyncReadyCallback callback,
270 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
273 static gboolean g_file_real_set_attributes_from_info (GFile *file,
275 GFileQueryInfoFlags flags,
276 GCancellable *cancellable,
278 static void g_file_real_set_display_name_async (GFile *file,
279 const char *display_name,
281 GCancellable *cancellable,
282 GAsyncReadyCallback callback,
284 static GFile * g_file_real_set_display_name_finish (GFile *file,
287 static void g_file_real_set_attributes_async (GFile *file,
289 GFileQueryInfoFlags flags,
291 GCancellable *cancellable,
292 GAsyncReadyCallback callback,
294 static gboolean g_file_real_set_attributes_finish (GFile *file,
298 static void g_file_real_find_enclosing_mount_async (GFile *file,
300 GCancellable *cancellable,
301 GAsyncReadyCallback callback,
303 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
306 static void g_file_real_copy_async (GFile *source,
308 GFileCopyFlags flags,
310 GCancellable *cancellable,
311 GFileProgressCallback progress_callback,
312 gpointer progress_callback_data,
313 GAsyncReadyCallback callback,
315 static gboolean g_file_real_copy_finish (GFile *file,
319 static gboolean g_file_real_measure_disk_usage (GFile *file,
320 GFileMeasureFlags flags,
321 GCancellable *cancellable,
322 GFileMeasureProgressCallback progress_callback,
323 gpointer progress_data,
328 static void g_file_real_measure_disk_usage_async (GFile *file,
329 GFileMeasureFlags flags,
331 GCancellable *cancellable,
332 GFileMeasureProgressCallback progress_callback,
333 gpointer progress_data,
334 GAsyncReadyCallback callback,
336 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
337 GAsyncResult *result,
343 typedef GFileIface GFileInterface;
344 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
347 g_file_default_init (GFileIface *iface)
349 iface->enumerate_children_async = g_file_real_enumerate_children_async;
350 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
351 iface->set_display_name_async = g_file_real_set_display_name_async;
352 iface->set_display_name_finish = g_file_real_set_display_name_finish;
353 iface->query_info_async = g_file_real_query_info_async;
354 iface->query_info_finish = g_file_real_query_info_finish;
355 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
356 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
357 iface->set_attributes_async = g_file_real_set_attributes_async;
358 iface->set_attributes_finish = g_file_real_set_attributes_finish;
359 iface->read_async = g_file_real_read_async;
360 iface->read_finish = g_file_real_read_finish;
361 iface->append_to_async = g_file_real_append_to_async;
362 iface->append_to_finish = g_file_real_append_to_finish;
363 iface->create_async = g_file_real_create_async;
364 iface->create_finish = g_file_real_create_finish;
365 iface->replace_async = g_file_real_replace_async;
366 iface->replace_finish = g_file_real_replace_finish;
367 iface->delete_file_async = g_file_real_delete_async;
368 iface->delete_file_finish = g_file_real_delete_finish;
369 iface->trash_async = g_file_real_trash_async;
370 iface->trash_finish = g_file_real_trash_finish;
371 iface->make_directory_async = g_file_real_make_directory_async;
372 iface->make_directory_finish = g_file_real_make_directory_finish;
373 iface->open_readwrite_async = g_file_real_open_readwrite_async;
374 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
375 iface->create_readwrite_async = g_file_real_create_readwrite_async;
376 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
377 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
378 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
379 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
380 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
381 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
382 iface->copy_async = g_file_real_copy_async;
383 iface->copy_finish = g_file_real_copy_finish;
384 iface->measure_disk_usage = g_file_real_measure_disk_usage;
385 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
386 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
392 * @file: input #GFile
394 * Checks to see if a file is native to the platform.
396 * A native file s one expressed in the platform-native filename format,
397 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
398 * as it might be on a locally mounted remote filesystem.
400 * On some systems non-native files may be available using the native
401 * filesystem via a userspace filesystem (FUSE), in these cases this call
402 * will return %FALSE, but g_file_get_path() will still return a native path.
404 * This call does no blocking I/O.
406 * Returns: %TRUE if @file is native
409 g_file_is_native (GFile *file)
413 g_return_val_if_fail (G_IS_FILE (file), FALSE);
415 iface = G_FILE_GET_IFACE (file);
417 return (* iface->is_native) (file);
422 * g_file_has_uri_scheme:
423 * @file: input #GFile
424 * @uri_scheme: a string containing a URI scheme
426 * Checks to see if a #GFile has a given URI scheme.
428 * This call does no blocking I/O.
430 * Returns: %TRUE if #GFile's backend supports the
431 * given URI scheme, %FALSE if URI scheme is %NULL,
432 * not supported, or #GFile is invalid.
435 g_file_has_uri_scheme (GFile *file,
436 const char *uri_scheme)
440 g_return_val_if_fail (G_IS_FILE (file), FALSE);
441 g_return_val_if_fail (uri_scheme != NULL, FALSE);
443 iface = G_FILE_GET_IFACE (file);
445 return (* iface->has_uri_scheme) (file, uri_scheme);
450 * g_file_get_uri_scheme:
451 * @file: input #GFile
453 * Gets the URI scheme for a #GFile.
454 * RFC 3986 decodes the scheme as:
456 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
458 * Common schemes include "file", "http", "ftp", etc.
460 * This call does no blocking I/O.
462 * Returns: a string containing the URI scheme for the given
463 * #GFile. The returned string should be freed with g_free()
464 * when no longer needed.
467 g_file_get_uri_scheme (GFile *file)
471 g_return_val_if_fail (G_IS_FILE (file), NULL);
473 iface = G_FILE_GET_IFACE (file);
475 return (* iface->get_uri_scheme) (file);
480 * g_file_get_basename:
481 * @file: input #GFile
483 * Gets the base name (the last component of the path) for a given #GFile.
485 * If called for the top level of a system (such as the filesystem root
486 * or a uri like sftp://host/) it will return a single directory separator
487 * (and on Windows, possibly a drive letter).
489 * The base name is a byte string (not UTF-8). It has no defined encoding
490 * or rules other than it may not contain zero bytes. If you want to use
491 * filenames in a user interface you should use the display name that you
492 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
493 * attribute with g_file_query_info().
495 * This call does no blocking I/O.
497 * Returns: string containing the #GFile's base name, or %NULL
498 * if given #GFile is invalid. The returned string should be
499 * freed with g_free() when no longer needed.
502 g_file_get_basename (GFile *file)
506 g_return_val_if_fail (G_IS_FILE (file), NULL);
508 iface = G_FILE_GET_IFACE (file);
510 return (* iface->get_basename) (file);
515 * @file: input #GFile
517 * Gets the local pathname for #GFile, if one exists.
519 * This call does no blocking I/O.
521 * Returns: string containing the #GFile's path, or %NULL if
522 * no such path exists. The returned string should be
523 * freed with g_free() when no longer needed.
526 g_file_get_path (GFile *file)
530 g_return_val_if_fail (G_IS_FILE (file), NULL);
532 iface = G_FILE_GET_IFACE (file);
534 return (* iface->get_path) (file);
539 * @file: input #GFile
541 * Gets the URI for the @file.
543 * This call does no blocking I/O.
545 * Returns: a string containing the #GFile's URI.
546 * The returned string should be freed with g_free()
547 * when no longer needed.
550 g_file_get_uri (GFile *file)
554 g_return_val_if_fail (G_IS_FILE (file), NULL);
556 iface = G_FILE_GET_IFACE (file);
558 return (* iface->get_uri) (file);
562 * g_file_get_parse_name:
563 * @file: input #GFile
565 * Gets the parse name of the @file.
566 * A parse name is a UTF-8 string that describes the
567 * file such that one can get the #GFile back using
568 * g_file_parse_name().
570 * This is generally used to show the #GFile as a nice
571 * full-pathname kind of string in a user interface,
572 * like in a location entry.
574 * For local files with names that can safely be converted
575 * to UTF-8 the pathname is used, otherwise the IRI is used
576 * (a form of URI that allows UTF-8 characters unescaped).
578 * This call does no blocking I/O.
580 * Returns: a string containing the #GFile's parse name.
581 * The returned string should be freed with g_free()
582 * when no longer needed.
585 g_file_get_parse_name (GFile *file)
589 g_return_val_if_fail (G_IS_FILE (file), NULL);
591 iface = G_FILE_GET_IFACE (file);
593 return (* iface->get_parse_name) (file);
598 * @file: input #GFile
600 * Duplicates a #GFile handle. This operation does not duplicate
601 * the actual file or directory represented by the #GFile; see
602 * g_file_copy() if attempting to copy a file.
604 * This call does no blocking I/O.
606 * Returns: (transfer full): a new #GFile that is a duplicate
607 * of the given #GFile.
610 g_file_dup (GFile *file)
614 g_return_val_if_fail (G_IS_FILE (file), NULL);
616 iface = G_FILE_GET_IFACE (file);
618 return (* iface->dup) (file);
623 * @file: (type GFile): #gconstpointer to a #GFile
625 * Creates a hash value for a #GFile.
627 * This call does no blocking I/O.
630 * Returns: 0 if @file is not a valid #GFile, otherwise an
631 * integer that can be used as hash value for the #GFile.
632 * This function is intended for easily hashing a #GFile to
633 * add to a #GHashTable or similar data structure.
636 g_file_hash (gconstpointer file)
640 g_return_val_if_fail (G_IS_FILE (file), 0);
642 iface = G_FILE_GET_IFACE (file);
644 return (* iface->hash) ((GFile *)file);
649 * @file1: the first #GFile
650 * @file2: the second #GFile
652 * Checks equality of two given #GFiles.
654 * Note that two #GFiles that differ can still refer to the same
655 * file on the filesystem due to various forms of filename
658 * This call does no blocking I/O.
660 * Returns: %TRUE if @file1 and @file2 are equal.
661 * %FALSE if either is not a #GFile.
664 g_file_equal (GFile *file1,
669 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
670 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
672 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
675 iface = G_FILE_GET_IFACE (file1);
677 return (* iface->equal) (file1, file2);
683 * @file: input #GFile
685 * Gets the parent directory for the @file.
686 * If the @file represents the root directory of the
687 * file system, then %NULL will be returned.
689 * This call does no blocking I/O.
691 * Returns: (transfer full): a #GFile structure to the
692 * parent of the given #GFile or %NULL if there is
693 * no parent. Free the returned object with g_object_unref().
696 g_file_get_parent (GFile *file)
700 g_return_val_if_fail (G_IS_FILE (file), NULL);
702 iface = G_FILE_GET_IFACE (file);
704 return (* iface->get_parent) (file);
709 * @file: input #GFile
710 * @parent: (allow-none): the parent to check for, or %NULL
712 * Checks if @file has a parent, and optionally, if it is @parent.
714 * If @parent is %NULL then this function returns %TRUE if @file has any
715 * parent at all. If @parent is non-%NULL then %TRUE is only returned
716 * if @file is a child of @parent.
718 * Returns: %TRUE if @file is a child of @parent (or any parent in the
719 * case that @parent is %NULL).
724 g_file_has_parent (GFile *file,
727 GFile *actual_parent;
730 g_return_val_if_fail (G_IS_FILE (file), FALSE);
731 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
733 actual_parent = g_file_get_parent (file);
735 if (actual_parent != NULL)
738 result = g_file_equal (parent, actual_parent);
742 g_object_unref (actual_parent);
752 * @file: input #GFile
753 * @name: string containing the child's basename
755 * Gets a child of @file with basename equal to @name.
757 * Note that the file with that specific name might not exist, but
758 * you can still have a #GFile that points to it. You can use this
759 * for instance to create that file.
761 * This call does no blocking I/O.
763 * Returns: (transfer full): a #GFile to a child specified by @name.
764 * Free the returned object with g_object_unref().
767 g_file_get_child (GFile *file,
770 g_return_val_if_fail (G_IS_FILE (file), NULL);
771 g_return_val_if_fail (name != NULL, NULL);
773 return g_file_resolve_relative_path (file, name);
777 * g_file_get_child_for_display_name:
778 * @file: input #GFile
779 * @display_name: string to a possible child
780 * @error: return location for an error
782 * Gets the child of @file for a given @display_name (i.e. a UTF-8
783 * version of the name). If this function fails, it returns %NULL
784 * and @error will be set. This is very useful when constructing a
785 * #GFile for a new file and the user entered the filename in the
786 * user interface, for instance when you select a directory and
787 * type a filename in the file selector.
789 * This call does no blocking I/O.
791 * Returns: (transfer full): a #GFile to the specified child, or
792 * %NULL if the display name couldn't be converted.
793 * Free the returned object with g_object_unref().
796 g_file_get_child_for_display_name (GFile *file,
797 const char *display_name,
802 g_return_val_if_fail (G_IS_FILE (file), NULL);
803 g_return_val_if_fail (display_name != NULL, NULL);
805 iface = G_FILE_GET_IFACE (file);
807 return (* iface->get_child_for_display_name) (file, display_name, error);
812 * @file: input #GFile
813 * @prefix: input #GFile
815 * Checks whether @file has the prefix specified by @prefix.
817 * In other words, if the names of initial elements of @file's
818 * pathname match @prefix. Only full pathname elements are matched,
819 * so a path like /foo is not considered a prefix of /foobar, only
822 * This call does no I/O, as it works purely on names. As such it can
823 * sometimes return %FALSE even if @file is inside a @prefix (from a
824 * filesystem point of view), because the prefix of @file is an alias
827 * Virtual: prefix_matches
828 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
832 g_file_has_prefix (GFile *file,
837 g_return_val_if_fail (G_IS_FILE (file), FALSE);
838 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
840 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
843 iface = G_FILE_GET_IFACE (file);
845 /* The vtable function differs in arg order since
846 * we're using the old contains_file call
848 return (* iface->prefix_matches) (prefix, file);
852 * g_file_get_relative_path:
853 * @parent: input #GFile
854 * @descendant: input #GFile
856 * Gets the path for @descendant relative to @parent.
858 * This call does no blocking I/O.
860 * Returns: string with the relative path from @descendant
861 * to @parent, or %NULL if @descendant doesn't have @parent
862 * as prefix. The returned string should be freed with g_free()
863 * when no longer needed.
866 g_file_get_relative_path (GFile *parent,
871 g_return_val_if_fail (G_IS_FILE (parent), NULL);
872 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
874 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
877 iface = G_FILE_GET_IFACE (parent);
879 return (* iface->get_relative_path) (parent, descendant);
883 * g_file_resolve_relative_path:
884 * @file: input #GFile
885 * @relative_path: a given relative path string
887 * Resolves a relative path for @file to an absolute path.
889 * This call does no blocking I/O.
891 * Returns: (transfer full): #GFile to the resolved path.
892 * %NULL if @relative_path is %NULL or if @file is invalid.
893 * Free the returned object with g_object_unref().
896 g_file_resolve_relative_path (GFile *file,
897 const char *relative_path)
901 g_return_val_if_fail (G_IS_FILE (file), NULL);
902 g_return_val_if_fail (relative_path != NULL, NULL);
904 iface = G_FILE_GET_IFACE (file);
906 return (* iface->resolve_relative_path) (file, relative_path);
910 * g_file_enumerate_children:
911 * @file: input #GFile
912 * @attributes: an attribute query string
913 * @flags: a set of #GFileQueryInfoFlags
914 * @cancellable: (allow-none): optional #GCancellable object,
916 * @error: #GError for error reporting
918 * Gets the requested information about the files in a directory.
919 * The result is a #GFileEnumerator object that will give out
920 * #GFileInfo objects for all the files in the directory.
922 * The @attributes value is a string that specifies the file
923 * attributes that should be gathered. It is not an error if
924 * it's not possible to read a particular requested attribute
925 * from a file - it just won't be set. @attributes should
926 * be a comma-separated list of attributes or attribute wildcards.
927 * The wildcard "*" means all attributes, and a wildcard like
928 * "standard::*" means all attributes in the standard namespace.
929 * An example attribute query be "standard::*,owner::user".
930 * The standard attributes are available as defines, like
931 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
933 * If @cancellable is not %NULL, then the operation can be cancelled
934 * by triggering the cancellable object from another thread. If the
935 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
938 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
939 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
940 * error will be returned. Other errors are possible too.
942 * Returns: (transfer full): A #GFileEnumerator if successful,
943 * %NULL on error. Free the returned object with g_object_unref().
946 g_file_enumerate_children (GFile *file,
947 const char *attributes,
948 GFileQueryInfoFlags flags,
949 GCancellable *cancellable,
954 g_return_val_if_fail (G_IS_FILE (file), NULL);
956 if (g_cancellable_set_error_if_cancelled (cancellable, error))
959 iface = G_FILE_GET_IFACE (file);
961 if (iface->enumerate_children == NULL)
963 g_set_error_literal (error, G_IO_ERROR,
964 G_IO_ERROR_NOT_SUPPORTED,
965 _("Operation not supported"));
969 return (* iface->enumerate_children) (file, attributes, flags,
974 * g_file_enumerate_children_async:
975 * @file: input #GFile
976 * @attributes: an attribute query string
977 * @flags: a set of #GFileQueryInfoFlags
978 * @io_priority: the <link linkend="io-priority">I/O priority</link>
980 * @cancellable: (allow-none): optional #GCancellable object,
982 * @callback: (scope async): a #GAsyncReadyCallback to call when the
983 * request is satisfied
984 * @user_data: (closure): the data to pass to callback function
986 * Asynchronously gets the requested information about the files
987 * in a directory. The result is a #GFileEnumerator object that will
988 * give out #GFileInfo objects for all the files in the directory.
990 * For more details, see g_file_enumerate_children() which is
991 * the synchronous version of this call.
993 * When the operation is finished, @callback will be called. You can
994 * then call g_file_enumerate_children_finish() to get the result of
998 g_file_enumerate_children_async (GFile *file,
999 const char *attributes,
1000 GFileQueryInfoFlags flags,
1002 GCancellable *cancellable,
1003 GAsyncReadyCallback callback,
1008 g_return_if_fail (G_IS_FILE (file));
1010 iface = G_FILE_GET_IFACE (file);
1011 (* iface->enumerate_children_async) (file,
1021 * g_file_enumerate_children_finish:
1022 * @file: input #GFile
1023 * @res: a #GAsyncResult
1026 * Finishes an async enumerate children operation.
1027 * See g_file_enumerate_children_async().
1029 * Returns: (transfer full): a #GFileEnumerator or %NULL
1030 * if an error occurred.
1031 * Free the returned object with g_object_unref().
1034 g_file_enumerate_children_finish (GFile *file,
1040 g_return_val_if_fail (G_IS_FILE (file), NULL);
1041 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1043 if (g_async_result_legacy_propagate_error (res, error))
1046 iface = G_FILE_GET_IFACE (file);
1047 return (* iface->enumerate_children_finish) (file, res, error);
1051 * g_file_query_exists:
1052 * @file: input #GFile
1053 * @cancellable: (allow-none): optional #GCancellable object,
1056 * Utility function to check if a particular file exists. This is
1057 * implemented using g_file_query_info() and as such does blocking I/O.
1059 * Note that in many cases it is racy to first check for file existence
1060 * and then execute something based on the outcome of that, because the
1061 * file might have been created or removed in between the operations. The
1062 * general approach to handling that is to not check, but just do the
1063 * operation and handle the errors as they come.
1065 * As an example of race-free checking, take the case of reading a file,
1066 * and if it doesn't exist, creating it. There are two racy versions: read
1067 * it, and on error create it; and: check if it exists, if not create it.
1068 * These can both result in two processes creating the file (with perhaps
1069 * a partially written file as the result). The correct approach is to
1070 * always try to create the file with g_file_create() which will either
1071 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1073 * However, in many cases an existence check is useful in a user interface,
1074 * for instance to make a menu item sensitive/insensitive, so that you don't
1075 * have to fool users that something is possible and then just show an error
1076 * dialog. If you do this, you should make sure to also handle the errors
1077 * that can happen due to races when you execute the operation.
1079 * Returns: %TRUE if the file exists (and can be detected without error),
1080 * %FALSE otherwise (or if cancelled).
1083 g_file_query_exists (GFile *file,
1084 GCancellable *cancellable)
1088 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1090 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1091 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1094 g_object_unref (info);
1102 * g_file_query_file_type:
1103 * @file: input #GFile
1104 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1105 * @cancellable: (allow-none): optional #GCancellable object,
1108 * Utility function to inspect the #GFileType of a file. This is
1109 * implemented using g_file_query_info() and as such does blocking I/O.
1111 * The primary use case of this method is to check if a file is
1112 * a regular file, directory, or symlink.
1114 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1115 * if the file does not exist
1120 g_file_query_file_type (GFile *file,
1121 GFileQueryInfoFlags flags,
1122 GCancellable *cancellable)
1125 GFileType file_type;
1127 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1128 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1132 file_type = g_file_info_get_file_type (info);
1133 g_object_unref (info);
1136 file_type = G_FILE_TYPE_UNKNOWN;
1142 * g_file_query_info:
1143 * @file: input #GFile
1144 * @attributes: an attribute query string
1145 * @flags: a set of #GFileQueryInfoFlags
1146 * @cancellable: (allow-none): optional #GCancellable object,
1150 * Gets the requested information about specified @file.
1151 * The result is a #GFileInfo object that contains key-value
1152 * attributes (such as the type or size of the file).
1154 * The @attributes value is a string that specifies the file
1155 * attributes that should be gathered. It is not an error if
1156 * it's not possible to read a particular requested attribute
1157 * from a file - it just won't be set. @attributes should be a
1158 * comma-separated list of attributes or attribute wildcards.
1159 * The wildcard "*" means all attributes, and a wildcard like
1160 * "standard::*" means all attributes in the standard namespace.
1161 * An example attribute query be "standard::*,owner::user".
1162 * The standard attributes are available as defines, like
1163 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1165 * If @cancellable is not %NULL, then the operation can be cancelled
1166 * by triggering the cancellable object from another thread. If the
1167 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1170 * For symlinks, normally the information about the target of the
1171 * symlink is returned, rather than information about the symlink
1172 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1173 * in @flags the information about the symlink itself will be returned.
1174 * Also, for symlinks that point to non-existing files the information
1175 * about the symlink itself will be returned.
1177 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1178 * returned. Other errors are possible too, and depend on what kind of
1179 * filesystem the file is on.
1181 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1182 * on error. Free the returned object with g_object_unref().
1185 g_file_query_info (GFile *file,
1186 const char *attributes,
1187 GFileQueryInfoFlags flags,
1188 GCancellable *cancellable,
1193 g_return_val_if_fail (G_IS_FILE (file), NULL);
1195 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1198 iface = G_FILE_GET_IFACE (file);
1200 if (iface->query_info == NULL)
1202 g_set_error_literal (error, G_IO_ERROR,
1203 G_IO_ERROR_NOT_SUPPORTED,
1204 _("Operation not supported"));
1208 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1212 * g_file_query_info_async:
1213 * @file: input #GFile
1214 * @attributes: an attribute query string
1215 * @flags: a set of #GFileQueryInfoFlags
1216 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1218 * @cancellable: (allow-none): optional #GCancellable object,
1220 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1221 * request is satisfied
1222 * @user_data: (closure): the data to pass to callback function
1224 * Asynchronously gets the requested information about specified @file.
1225 * The result is a #GFileInfo object that contains key-value attributes
1226 * (such as type or size for the file).
1228 * For more details, see g_file_query_info() which is the synchronous
1229 * version of this call.
1231 * When the operation is finished, @callback will be called. You can
1232 * then call g_file_query_info_finish() to get the result of the operation.
1235 g_file_query_info_async (GFile *file,
1236 const char *attributes,
1237 GFileQueryInfoFlags flags,
1239 GCancellable *cancellable,
1240 GAsyncReadyCallback callback,
1245 g_return_if_fail (G_IS_FILE (file));
1247 iface = G_FILE_GET_IFACE (file);
1248 (* iface->query_info_async) (file,
1258 * g_file_query_info_finish:
1259 * @file: input #GFile
1260 * @res: a #GAsyncResult
1263 * Finishes an asynchronous file info query.
1264 * See g_file_query_info_async().
1266 * Returns: (transfer full): #GFileInfo for given @file
1267 * or %NULL on error. Free the returned object with
1271 g_file_query_info_finish (GFile *file,
1277 g_return_val_if_fail (G_IS_FILE (file), NULL);
1278 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1280 if (g_async_result_legacy_propagate_error (res, error))
1283 iface = G_FILE_GET_IFACE (file);
1284 return (* iface->query_info_finish) (file, res, error);
1288 * g_file_query_filesystem_info:
1289 * @file: input #GFile
1290 * @attributes: an attribute query string
1291 * @cancellable: (allow-none): optional #GCancellable object,
1295 * Similar to g_file_query_info(), but obtains information
1296 * about the filesystem the @file is on, rather than the file itself.
1297 * For instance the amount of space available and the type of
1300 * The @attributes value is a string that specifies the attributes
1301 * that should be gathered. It is not an error if it's not possible
1302 * to read a particular requested attribute from a file - it just
1303 * won't be set. @attributes should be a comma-separated list of
1304 * attributes or attribute wildcards. The wildcard "*" means all
1305 * attributes, and a wildcard like "filesystem::*" means all attributes
1306 * in the filesystem namespace. The standard namespace for filesystem
1307 * attributes is "filesystem". Common attributes of interest are
1308 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1309 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1310 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1312 * If @cancellable is not %NULL, then the operation can be cancelled
1313 * by triggering the cancellable object from another thread. If the
1314 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1317 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1318 * be returned. Other errors are possible too, and depend on what
1319 * kind of filesystem the file is on.
1321 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1322 * Free the returned object with g_object_unref().
1325 g_file_query_filesystem_info (GFile *file,
1326 const char *attributes,
1327 GCancellable *cancellable,
1332 g_return_val_if_fail (G_IS_FILE (file), NULL);
1334 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1337 iface = G_FILE_GET_IFACE (file);
1339 if (iface->query_filesystem_info == NULL)
1341 g_set_error_literal (error, G_IO_ERROR,
1342 G_IO_ERROR_NOT_SUPPORTED,
1343 _("Operation not supported"));
1347 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1351 * g_file_query_filesystem_info_async:
1352 * @file: input #GFile
1353 * @attributes: an attribute query string
1354 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1356 * @cancellable: (allow-none): optional #GCancellable object,
1358 * @callback: (scope async): a #GAsyncReadyCallback to call
1359 * when the request is satisfied
1360 * @user_data: (closure): the data to pass to callback function
1362 * Asynchronously gets the requested information about the filesystem
1363 * that the specified @file is on. The result is a #GFileInfo object
1364 * that contains key-value attributes (such as type or size for the
1367 * For more details, see g_file_query_filesystem_info() which is the
1368 * synchronous version of this call.
1370 * When the operation is finished, @callback will be called. You can
1371 * then call g_file_query_info_finish() to get the result of the
1375 g_file_query_filesystem_info_async (GFile *file,
1376 const char *attributes,
1378 GCancellable *cancellable,
1379 GAsyncReadyCallback callback,
1384 g_return_if_fail (G_IS_FILE (file));
1386 iface = G_FILE_GET_IFACE (file);
1387 (* iface->query_filesystem_info_async) (file,
1396 * g_file_query_filesystem_info_finish:
1397 * @file: input #GFile
1398 * @res: a #GAsyncResult
1401 * Finishes an asynchronous filesystem info query.
1402 * See g_file_query_filesystem_info_async().
1404 * Returns: (transfer full): #GFileInfo for given @file
1405 * or %NULL on error.
1406 * Free the returned object with g_object_unref().
1409 g_file_query_filesystem_info_finish (GFile *file,
1415 g_return_val_if_fail (G_IS_FILE (file), NULL);
1416 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1418 if (g_async_result_legacy_propagate_error (res, error))
1421 iface = G_FILE_GET_IFACE (file);
1422 return (* iface->query_filesystem_info_finish) (file, res, error);
1426 * g_file_find_enclosing_mount:
1427 * @file: input #GFile
1428 * @cancellable: (allow-none): optional #GCancellable object,
1432 * Gets a #GMount for the #GFile.
1434 * If the #GFileIface for @file does not have a mount (e.g.
1435 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1436 * and %NULL will be returned.
1438 * If @cancellable is not %NULL, then the operation can be cancelled by
1439 * triggering the cancellable object from another thread. If the operation
1440 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1442 * Returns: (transfer full): a #GMount where the @file is located
1443 * or %NULL on error.
1444 * Free the returned object with g_object_unref().
1447 g_file_find_enclosing_mount (GFile *file,
1448 GCancellable *cancellable,
1453 g_return_val_if_fail (G_IS_FILE (file), NULL);
1455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1458 iface = G_FILE_GET_IFACE (file);
1459 if (iface->find_enclosing_mount == NULL)
1462 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1463 /* Translators: This is an error message when
1464 * trying to find the enclosing (user visible)
1465 * mount of a file, but none exists.
1467 _("Containing mount does not exist"));
1471 return (* iface->find_enclosing_mount) (file, cancellable, error);
1475 * g_file_find_enclosing_mount_async:
1477 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1479 * @cancellable: (allow-none): optional #GCancellable object,
1481 * @callback: (scope async): a #GAsyncReadyCallback to call
1482 * when the request is satisfied
1483 * @user_data: (closure): the data to pass to callback function
1485 * Asynchronously gets the mount for the file.
1487 * For more details, see g_file_find_enclosing_mount() which is
1488 * the synchronous version of this call.
1490 * When the operation is finished, @callback will be called.
1491 * You can then call g_file_find_enclosing_mount_finish() to
1492 * get the result of the operation.
1495 g_file_find_enclosing_mount_async (GFile *file,
1497 GCancellable *cancellable,
1498 GAsyncReadyCallback callback,
1503 g_return_if_fail (G_IS_FILE (file));
1505 iface = G_FILE_GET_IFACE (file);
1506 (* iface->find_enclosing_mount_async) (file,
1514 * g_file_find_enclosing_mount_finish:
1516 * @res: a #GAsyncResult
1519 * Finishes an asynchronous find mount request.
1520 * See g_file_find_enclosing_mount_async().
1522 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1523 * Free the returned object with g_object_unref().
1526 g_file_find_enclosing_mount_finish (GFile *file,
1532 g_return_val_if_fail (G_IS_FILE (file), NULL);
1533 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1535 if (g_async_result_legacy_propagate_error (res, error))
1538 iface = G_FILE_GET_IFACE (file);
1539 return (* iface->find_enclosing_mount_finish) (file, res, error);
1545 * @file: #GFile to read
1546 * @cancellable: (allow-none): a #GCancellable
1547 * @error: a #GError, or %NULL
1549 * Opens a file for reading. The result is a #GFileInputStream that
1550 * can be used to read the contents of the file.
1552 * If @cancellable is not %NULL, then the operation can be cancelled by
1553 * triggering the cancellable object from another thread. If the operation
1554 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1556 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1557 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1558 * error will be returned. Other errors are possible too, and depend
1559 * on what kind of filesystem the file is on.
1562 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1563 * Free the returned object with g_object_unref().
1566 g_file_read (GFile *file,
1567 GCancellable *cancellable,
1572 g_return_val_if_fail (G_IS_FILE (file), NULL);
1574 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1577 iface = G_FILE_GET_IFACE (file);
1579 if (iface->read_fn == NULL)
1581 g_set_error_literal (error, G_IO_ERROR,
1582 G_IO_ERROR_NOT_SUPPORTED,
1583 _("Operation not supported"));
1587 return (* iface->read_fn) (file, cancellable, error);
1592 * @file: input #GFile
1593 * @flags: a set of #GFileCreateFlags
1594 * @cancellable: (allow-none): optional #GCancellable object,
1596 * @error: a #GError, or %NULL
1598 * Gets an output stream for appending data to the file.
1599 * If the file doesn't already exist it is created.
1601 * By default files created are generally readable by everyone,
1602 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1603 * will be made readable only to the current user, to the level that
1604 * is supported on the target filesystem.
1606 * If @cancellable is not %NULL, then the operation can be cancelled
1607 * by triggering the cancellable object from another thread. If the
1608 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1611 * Some file systems don't allow all file names, and may return an
1612 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1613 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1614 * possible too, and depend on what kind of filesystem the file is on.
1616 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1617 * Free the returned object with g_object_unref().
1620 g_file_append_to (GFile *file,
1621 GFileCreateFlags flags,
1622 GCancellable *cancellable,
1627 g_return_val_if_fail (G_IS_FILE (file), NULL);
1629 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1632 iface = G_FILE_GET_IFACE (file);
1634 if (iface->append_to == NULL)
1636 g_set_error_literal (error, G_IO_ERROR,
1637 G_IO_ERROR_NOT_SUPPORTED,
1638 _("Operation not supported"));
1642 return (* iface->append_to) (file, flags, cancellable, error);
1647 * @file: input #GFile
1648 * @flags: a set of #GFileCreateFlags
1649 * @cancellable: (allow-none): optional #GCancellable object,
1651 * @error: a #GError, or %NULL
1653 * Creates a new file and returns an output stream for writing to it.
1654 * The file must not already exist.
1656 * By default files created are generally readable by everyone,
1657 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1658 * will be made readable only to the current user, to the level
1659 * that is supported on the target filesystem.
1661 * If @cancellable is not %NULL, then the operation can be cancelled
1662 * by triggering the cancellable object from another thread. If the
1663 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1666 * If a file or directory with this name already exists the
1667 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1668 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1669 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1670 * be returned. Other errors are possible too, and depend on what kind
1671 * of filesystem the file is on.
1673 * Returns: (transfer full): a #GFileOutputStream for the newly created
1674 * file, or %NULL on error.
1675 * Free the returned object with g_object_unref().
1678 g_file_create (GFile *file,
1679 GFileCreateFlags flags,
1680 GCancellable *cancellable,
1685 g_return_val_if_fail (G_IS_FILE (file), NULL);
1687 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1690 iface = G_FILE_GET_IFACE (file);
1692 if (iface->create == NULL)
1694 g_set_error_literal (error, G_IO_ERROR,
1695 G_IO_ERROR_NOT_SUPPORTED,
1696 _("Operation not supported"));
1700 return (* iface->create) (file, flags, cancellable, error);
1705 * @file: input #GFile
1706 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1707 * for the current #GFile, or #NULL to ignore
1708 * @make_backup: %TRUE if a backup should be created
1709 * @flags: a set of #GFileCreateFlags
1710 * @cancellable: (allow-none): optional #GCancellable object,
1712 * @error: a #GError, or %NULL
1714 * Returns an output stream for overwriting the file, possibly
1715 * creating a backup copy of the file first. If the file doesn't exist,
1716 * it will be created.
1718 * This will try to replace the file in the safest way possible so
1719 * that any errors during the writing will not affect an already
1720 * existing copy of the file. For instance, for local files it
1721 * may write to a temporary file and then atomically rename over
1722 * the destination when the stream is closed.
1724 * By default files created are generally readable by everyone,
1725 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1726 * will be made readable only to the current user, to the level that
1727 * is supported on the target filesystem.
1729 * If @cancellable is not %NULL, then the operation can be cancelled
1730 * by triggering the cancellable object from another thread. If the
1731 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1734 * If you pass in a non-%NULL @etag value, then this value is
1735 * compared to the current entity tag of the file, and if they differ
1736 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1737 * that the file has been changed since you last read it. You can get
1738 * the new etag from g_file_output_stream_get_etag() after you've
1739 * finished writing and closed the #GFileOutputStream. When you load
1740 * a new file you can use g_file_input_stream_query_info() to get
1741 * the etag of the file.
1743 * If @make_backup is %TRUE, this function will attempt to make a
1744 * backup of the current file before overwriting it. If this fails
1745 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1746 * want to replace anyway, try again with @make_backup set to %FALSE.
1748 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1749 * be returned, and if the file is some other form of non-regular file
1750 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1751 * file systems don't allow all file names, and may return an
1752 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1753 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1754 * possible too, and depend on what kind of filesystem the file is on.
1756 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1757 * Free the returned object with g_object_unref().
1760 g_file_replace (GFile *file,
1762 gboolean make_backup,
1763 GFileCreateFlags flags,
1764 GCancellable *cancellable,
1769 g_return_val_if_fail (G_IS_FILE (file), NULL);
1771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1774 iface = G_FILE_GET_IFACE (file);
1776 if (iface->replace == NULL)
1778 g_set_error_literal (error, G_IO_ERROR,
1779 G_IO_ERROR_NOT_SUPPORTED,
1780 _("Operation not supported"));
1784 /* Handle empty tag string as NULL in consistent way. */
1785 if (etag && *etag == 0)
1788 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1792 * g_file_open_readwrite:
1793 * @file: #GFile to open
1794 * @cancellable: (allow-none): a #GCancellable
1795 * @error: a #GError, or %NULL
1797 * Opens an existing file for reading and writing. The result is
1798 * a #GFileIOStream that can be used to read and write the contents
1801 * If @cancellable is not %NULL, then the operation can be cancelled
1802 * by triggering the cancellable object from another thread. If the
1803 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1806 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1807 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1808 * error will be returned. Other errors are possible too, and depend on
1809 * what kind of filesystem the file is on. Note that in many non-local
1810 * file cases read and write streams are not supported, so make sure you
1811 * really need to do read and write streaming, rather than just opening
1812 * for reading or writing.
1814 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1815 * Free the returned object with g_object_unref().
1820 g_file_open_readwrite (GFile *file,
1821 GCancellable *cancellable,
1826 g_return_val_if_fail (G_IS_FILE (file), NULL);
1828 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1831 iface = G_FILE_GET_IFACE (file);
1833 if (iface->open_readwrite == NULL)
1835 g_set_error_literal (error, G_IO_ERROR,
1836 G_IO_ERROR_NOT_SUPPORTED,
1837 _("Operation not supported"));
1841 return (* iface->open_readwrite) (file, cancellable, error);
1845 * g_file_create_readwrite:
1847 * @flags: a set of #GFileCreateFlags
1848 * @cancellable: (allow-none): optional #GCancellable object,
1850 * @error: return location for a #GError, or %NULL
1852 * Creates a new file and returns a stream for reading and
1853 * writing to it. The file must not already exist.
1855 * By default files created are generally readable by everyone,
1856 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1857 * will be made readable only to the current user, to the level
1858 * that is supported on the target filesystem.
1860 * If @cancellable is not %NULL, then the operation can be cancelled
1861 * by triggering the cancellable object from another thread. If the
1862 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1865 * If a file or directory with this name already exists, the
1866 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1867 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1868 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1869 * will be returned. Other errors are possible too, and depend on what
1870 * kind of filesystem the file is on.
1872 * Note that in many non-local file cases read and write streams are
1873 * not supported, so make sure you really need to do read and write
1874 * streaming, rather than just opening for reading or writing.
1876 * Returns: (transfer full): a #GFileIOStream for the newly created
1877 * file, or %NULL on error.
1878 * Free the returned object with g_object_unref().
1883 g_file_create_readwrite (GFile *file,
1884 GFileCreateFlags flags,
1885 GCancellable *cancellable,
1890 g_return_val_if_fail (G_IS_FILE (file), NULL);
1892 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1895 iface = G_FILE_GET_IFACE (file);
1897 if (iface->create_readwrite == NULL)
1899 g_set_error_literal (error, G_IO_ERROR,
1900 G_IO_ERROR_NOT_SUPPORTED,
1901 _("Operation not supported"));
1905 return (* iface->create_readwrite) (file, flags, cancellable, error);
1909 * g_file_replace_readwrite:
1911 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1912 * for the current #GFile, or #NULL to ignore
1913 * @make_backup: %TRUE if a backup should be created
1914 * @flags: a set of #GFileCreateFlags
1915 * @cancellable: (allow-none): optional #GCancellable object,
1917 * @error: return location for a #GError, or %NULL
1919 * Returns an output stream for overwriting the file in readwrite mode,
1920 * possibly creating a backup copy of the file first. If the file doesn't
1921 * exist, it will be created.
1923 * For details about the behaviour, see g_file_replace() which does the
1924 * same thing but returns an output stream only.
1926 * Note that in many non-local file cases read and write streams are not
1927 * supported, so make sure you really need to do read and write streaming,
1928 * rather than just opening for reading or writing.
1930 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1931 * Free the returned object with g_object_unref().
1936 g_file_replace_readwrite (GFile *file,
1938 gboolean make_backup,
1939 GFileCreateFlags flags,
1940 GCancellable *cancellable,
1945 g_return_val_if_fail (G_IS_FILE (file), NULL);
1947 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1950 iface = G_FILE_GET_IFACE (file);
1952 if (iface->replace_readwrite == NULL)
1954 g_set_error_literal (error, G_IO_ERROR,
1955 G_IO_ERROR_NOT_SUPPORTED,
1956 _("Operation not supported"));
1960 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1964 * g_file_read_async:
1965 * @file: input #GFile
1966 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1968 * @cancellable: (allow-none): optional #GCancellable object,
1970 * @callback: (scope async): a #GAsyncReadyCallback to call
1971 * when the request is satisfied
1972 * @user_data: (closure): the data to pass to callback function
1974 * Asynchronously opens @file for reading.
1976 * For more details, see g_file_read() which is
1977 * the synchronous version of this call.
1979 * When the operation is finished, @callback will be called.
1980 * You can then call g_file_read_finish() to get the result
1984 g_file_read_async (GFile *file,
1986 GCancellable *cancellable,
1987 GAsyncReadyCallback callback,
1992 g_return_if_fail (G_IS_FILE (file));
1994 iface = G_FILE_GET_IFACE (file);
1995 (* iface->read_async) (file,
2003 * g_file_read_finish:
2004 * @file: input #GFile
2005 * @res: a #GAsyncResult
2006 * @error: a #GError, or %NULL
2008 * Finishes an asynchronous file read operation started with
2009 * g_file_read_async().
2011 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2012 * Free the returned object with g_object_unref().
2015 g_file_read_finish (GFile *file,
2021 g_return_val_if_fail (G_IS_FILE (file), NULL);
2022 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2024 if (g_async_result_legacy_propagate_error (res, error))
2027 iface = G_FILE_GET_IFACE (file);
2028 return (* iface->read_finish) (file, res, error);
2032 * g_file_append_to_async:
2033 * @file: input #GFile
2034 * @flags: a set of #GFileCreateFlags
2035 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2037 * @cancellable: (allow-none): optional #GCancellable object,
2039 * @callback: (scope async): a #GAsyncReadyCallback to call
2040 * when the request is satisfied
2041 * @user_data: (closure): the data to pass to callback function
2043 * Asynchronously opens @file for appending.
2045 * For more details, see g_file_append_to() which is
2046 * the synchronous version of this call.
2048 * When the operation is finished, @callback will be called.
2049 * You can then call g_file_append_to_finish() to get the result
2053 g_file_append_to_async (GFile *file,
2054 GFileCreateFlags flags,
2056 GCancellable *cancellable,
2057 GAsyncReadyCallback callback,
2062 g_return_if_fail (G_IS_FILE (file));
2064 iface = G_FILE_GET_IFACE (file);
2065 (* iface->append_to_async) (file,
2074 * g_file_append_to_finish:
2075 * @file: input #GFile
2076 * @res: #GAsyncResult
2077 * @error: a #GError, or %NULL
2079 * Finishes an asynchronous file append operation started with
2080 * g_file_append_to_async().
2082 * Returns: (transfer full): a valid #GFileOutputStream
2083 * or %NULL on error.
2084 * Free the returned object with g_object_unref().
2087 g_file_append_to_finish (GFile *file,
2093 g_return_val_if_fail (G_IS_FILE (file), NULL);
2094 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2096 if (g_async_result_legacy_propagate_error (res, error))
2099 iface = G_FILE_GET_IFACE (file);
2100 return (* iface->append_to_finish) (file, res, error);
2104 * g_file_create_async:
2105 * @file: input #GFile
2106 * @flags: a set of #GFileCreateFlags
2107 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2109 * @cancellable: (allow-none): optional #GCancellable object,
2111 * @callback: (scope async): a #GAsyncReadyCallback to call
2112 * when the request is satisfied
2113 * @user_data: (closure): the data to pass to callback function
2115 * Asynchronously creates a new file and returns an output stream
2116 * for writing to it. The file must not already exist.
2118 * For more details, see g_file_create() which is
2119 * the synchronous version of this call.
2121 * When the operation is finished, @callback will be called.
2122 * You can then call g_file_create_finish() to get the result
2126 g_file_create_async (GFile *file,
2127 GFileCreateFlags flags,
2129 GCancellable *cancellable,
2130 GAsyncReadyCallback callback,
2135 g_return_if_fail (G_IS_FILE (file));
2137 iface = G_FILE_GET_IFACE (file);
2138 (* iface->create_async) (file,
2147 * g_file_create_finish:
2148 * @file: input #GFile
2149 * @res: a #GAsyncResult
2150 * @error: a #GError, or %NULL
2152 * Finishes an asynchronous file create operation started with
2153 * g_file_create_async().
2155 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2156 * Free the returned object with g_object_unref().
2159 g_file_create_finish (GFile *file,
2165 g_return_val_if_fail (G_IS_FILE (file), NULL);
2166 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2168 if (g_async_result_legacy_propagate_error (res, error))
2171 iface = G_FILE_GET_IFACE (file);
2172 return (* iface->create_finish) (file, res, error);
2176 * g_file_replace_async:
2177 * @file: input #GFile
2178 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2179 * for the current #GFile, or NULL to ignore
2180 * @make_backup: %TRUE if a backup should be created
2181 * @flags: a set of #GFileCreateFlags
2182 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2184 * @cancellable: (allow-none): optional #GCancellable object,
2186 * @callback: (scope async): a #GAsyncReadyCallback to call
2187 * when the request is satisfied
2188 * @user_data: (closure): the data to pass to callback function
2190 * Asynchronously overwrites the file, replacing the contents,
2191 * possibly creating a backup copy of the file first.
2193 * For more details, see g_file_replace() which is
2194 * the synchronous version of this call.
2196 * When the operation is finished, @callback will be called.
2197 * You can then call g_file_replace_finish() to get the result
2201 g_file_replace_async (GFile *file,
2203 gboolean make_backup,
2204 GFileCreateFlags flags,
2206 GCancellable *cancellable,
2207 GAsyncReadyCallback callback,
2212 g_return_if_fail (G_IS_FILE (file));
2214 iface = G_FILE_GET_IFACE (file);
2215 (* iface->replace_async) (file,
2226 * g_file_replace_finish:
2227 * @file: input #GFile
2228 * @res: a #GAsyncResult
2229 * @error: a #GError, or %NULL
2231 * Finishes an asynchronous file replace operation started with
2232 * g_file_replace_async().
2234 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2235 * Free the returned object with g_object_unref().
2238 g_file_replace_finish (GFile *file,
2244 g_return_val_if_fail (G_IS_FILE (file), NULL);
2245 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2247 if (g_async_result_legacy_propagate_error (res, error))
2250 iface = G_FILE_GET_IFACE (file);
2251 return (* iface->replace_finish) (file, res, error);
2255 * g_file_open_readwrite_async
2256 * @file: input #GFile
2257 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2259 * @cancellable: (allow-none): optional #GCancellable object,
2261 * @callback: (scope async): a #GAsyncReadyCallback to call
2262 * when the request is satisfied
2263 * @user_data: (closure): the data to pass to callback function
2265 * Asynchronously opens @file for reading and writing.
2267 * For more details, see g_file_open_readwrite() which is
2268 * the synchronous version of this call.
2270 * When the operation is finished, @callback will be called.
2271 * You can then call g_file_open_readwrite_finish() to get
2272 * the result of the operation.
2277 g_file_open_readwrite_async (GFile *file,
2279 GCancellable *cancellable,
2280 GAsyncReadyCallback callback,
2285 g_return_if_fail (G_IS_FILE (file));
2287 iface = G_FILE_GET_IFACE (file);
2288 (* iface->open_readwrite_async) (file,
2296 * g_file_open_readwrite_finish:
2297 * @file: input #GFile
2298 * @res: a #GAsyncResult
2299 * @error: a #GError, or %NULL
2301 * Finishes an asynchronous file read operation started with
2302 * g_file_open_readwrite_async().
2304 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2305 * Free the returned object with g_object_unref().
2310 g_file_open_readwrite_finish (GFile *file,
2316 g_return_val_if_fail (G_IS_FILE (file), NULL);
2317 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2319 if (g_async_result_legacy_propagate_error (res, error))
2322 iface = G_FILE_GET_IFACE (file);
2323 return (* iface->open_readwrite_finish) (file, res, error);
2327 * g_file_create_readwrite_async:
2328 * @file: input #GFile
2329 * @flags: a set of #GFileCreateFlags
2330 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2332 * @cancellable: (allow-none): optional #GCancellable object,
2334 * @callback: (scope async): a #GAsyncReadyCallback to call
2335 * when the request is satisfied
2336 * @user_data: (closure): the data to pass to callback function
2338 * Asynchronously creates a new file and returns a stream
2339 * for reading and writing to it. The file must not already exist.
2341 * For more details, see g_file_create_readwrite() which is
2342 * the synchronous version of this call.
2344 * When the operation is finished, @callback will be called.
2345 * You can then call g_file_create_readwrite_finish() to get
2346 * the result of the operation.
2351 g_file_create_readwrite_async (GFile *file,
2352 GFileCreateFlags flags,
2354 GCancellable *cancellable,
2355 GAsyncReadyCallback callback,
2360 g_return_if_fail (G_IS_FILE (file));
2362 iface = G_FILE_GET_IFACE (file);
2363 (* iface->create_readwrite_async) (file,
2372 * g_file_create_readwrite_finish:
2373 * @file: input #GFile
2374 * @res: a #GAsyncResult
2375 * @error: a #GError, or %NULL
2377 * Finishes an asynchronous file create operation started with
2378 * g_file_create_readwrite_async().
2380 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2381 * Free the returned object with g_object_unref().
2386 g_file_create_readwrite_finish (GFile *file,
2392 g_return_val_if_fail (G_IS_FILE (file), NULL);
2393 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2395 if (g_async_result_legacy_propagate_error (res, error))
2398 iface = G_FILE_GET_IFACE (file);
2399 return (* iface->create_readwrite_finish) (file, res, error);
2403 * g_file_replace_readwrite_async:
2404 * @file: input #GFile
2405 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2406 * for the current #GFile, or NULL to ignore
2407 * @make_backup: %TRUE if a backup should be created
2408 * @flags: a set of #GFileCreateFlags
2409 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2411 * @cancellable: (allow-none): optional #GCancellable object,
2413 * @callback: (scope async): a #GAsyncReadyCallback to call
2414 * when the request is satisfied
2415 * @user_data: (closure): the data to pass to callback function
2417 * Asynchronously overwrites the file in read-write mode,
2418 * replacing the contents, possibly creating a backup copy
2419 * of the file first.
2421 * For more details, see g_file_replace_readwrite() which is
2422 * the synchronous version of this call.
2424 * When the operation is finished, @callback will be called.
2425 * You can then call g_file_replace_readwrite_finish() to get
2426 * the result of the operation.
2431 g_file_replace_readwrite_async (GFile *file,
2433 gboolean make_backup,
2434 GFileCreateFlags flags,
2436 GCancellable *cancellable,
2437 GAsyncReadyCallback callback,
2442 g_return_if_fail (G_IS_FILE (file));
2444 iface = G_FILE_GET_IFACE (file);
2445 (* iface->replace_readwrite_async) (file,
2456 * g_file_replace_readwrite_finish:
2457 * @file: input #GFile
2458 * @res: a #GAsyncResult
2459 * @error: a #GError, or %NULL
2461 * Finishes an asynchronous file replace operation started with
2462 * g_file_replace_readwrite_async().
2464 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2465 * Free the returned object with g_object_unref().
2470 g_file_replace_readwrite_finish (GFile *file,
2476 g_return_val_if_fail (G_IS_FILE (file), NULL);
2477 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2479 if (g_async_result_legacy_propagate_error (res, error))
2482 iface = G_FILE_GET_IFACE (file);
2483 return (* iface->replace_readwrite_finish) (file, res, error);
2487 copy_symlink (GFile *destination,
2488 GFileCopyFlags flags,
2489 GCancellable *cancellable,
2494 gboolean tried_delete;
2496 GFileType file_type;
2498 tried_delete = FALSE;
2502 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2504 /* Maybe it already existed, and we want to overwrite? */
2505 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2506 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2508 g_clear_error (&my_error);
2510 /* Don't overwrite if the destination is a directory */
2511 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2512 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2513 cancellable, &my_error);
2516 file_type = g_file_info_get_file_type (info);
2517 g_object_unref (info);
2519 if (file_type == G_FILE_TYPE_DIRECTORY)
2521 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2522 _("Can't copy over directory"));
2527 if (!g_file_delete (destination, cancellable, error))
2530 tried_delete = TRUE;
2534 g_propagate_error (error, my_error);
2541 static GFileInputStream *
2542 open_source_for_copy (GFile *source,
2544 GFileCopyFlags flags,
2545 GCancellable *cancellable,
2549 GFileInputStream *ret;
2551 GFileType file_type;
2554 ret = g_file_read (source, cancellable, &my_error);
2558 /* There was an error opening the source, try to set a good error for it: */
2559 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2561 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2562 * as that is less useful to the app. Better check for errors on the
2565 g_error_free (my_error);
2568 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2569 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2570 cancellable, &my_error);
2572 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2574 file_type = g_file_info_get_file_type (info);
2575 g_object_unref (info);
2577 if (flags & G_FILE_COPY_OVERWRITE)
2579 if (file_type == G_FILE_TYPE_DIRECTORY)
2581 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2582 _("Can't copy directory over directory"));
2585 /* continue to would_recurse error */
2589 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2590 _("Target file exists"));
2596 /* Error getting info from target, return that error
2597 * (except for NOT_FOUND, which is no error here)
2599 g_clear_object (&info);
2600 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2602 g_propagate_error (error, my_error);
2605 g_clear_error (&my_error);
2608 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2609 _("Can't recursively copy directory"));
2613 g_propagate_error (error, my_error);
2618 should_copy (GFileAttributeInfo *info,
2619 gboolean copy_all_attributes,
2620 gboolean skip_perms)
2622 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2625 if (copy_all_attributes)
2626 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2627 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2631 build_attribute_list_for_copy (GFile *file,
2632 GFileCopyFlags flags,
2633 char **out_attributes,
2634 GCancellable *cancellable,
2637 gboolean ret = FALSE;
2638 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2642 gboolean copy_all_attributes;
2643 gboolean skip_perms;
2645 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2646 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2648 /* Ignore errors here, if the target supports no attributes there is
2649 * nothing to copy. We still honor the cancellable though.
2651 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2655 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2656 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2659 if (attributes == NULL && namespaces == NULL)
2663 s = g_string_new ("");
2667 for (i = 0; i < attributes->n_infos; i++)
2669 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2674 g_string_append_c (s, ',');
2676 g_string_append (s, attributes->infos[i].name);
2683 for (i = 0; i < namespaces->n_infos; i++)
2685 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2690 g_string_append_c (s, ',');
2692 g_string_append (s, namespaces->infos[i].name);
2693 g_string_append (s, "::*");
2699 *out_attributes = g_string_free (s, FALSE);
2703 g_string_free (s, TRUE);
2705 g_file_attribute_info_list_unref (attributes);
2707 g_file_attribute_info_list_unref (namespaces);
2713 * g_file_copy_attributes:
2714 * @source: a #GFile with attributes
2715 * @destination: a #GFile to copy attributes to
2716 * @flags: a set of #GFileCopyFlags
2717 * @cancellable: (allow-none): optional #GCancellable object,
2719 * @error: a #GError, %NULL to ignore
2721 * Copies the file attributes from @source to @destination.
2723 * Normally only a subset of the file attributes are copied,
2724 * those that are copies in a normal file copy operation
2725 * (which for instance does not include e.g. owner). However
2726 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2727 * all the metadata that is possible to copy is copied. This
2728 * is useful when implementing move by copy + delete source.
2730 * Returns: %TRUE if the attributes were copied successfully,
2734 g_file_copy_attributes (GFile *source,
2736 GFileCopyFlags flags,
2737 GCancellable *cancellable,
2740 char *attrs_to_read;
2743 gboolean source_nofollow_symlinks;
2745 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2746 cancellable, error))
2749 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2751 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2752 * we just don't copy it.
2754 info = g_file_query_info (source, attrs_to_read,
2755 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2759 g_free (attrs_to_read);
2764 res = g_file_set_attributes_from_info (destination,
2766 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2769 g_object_unref (info);
2776 copy_stream_with_progress (GInputStream *in,
2779 GCancellable *cancellable,
2780 GFileProgressCallback progress_callback,
2781 gpointer progress_callback_data,
2784 gssize n_read, n_written;
2785 goffset current_size;
2786 char buffer[1024*64], *p;
2792 /* avoid performance impact of querying total size when it's not needed */
2793 if (progress_callback)
2795 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2796 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2800 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2801 total_size = g_file_info_get_size (info);
2802 g_object_unref (info);
2805 if (total_size == -1)
2807 info = g_file_query_info (source,
2808 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2809 G_FILE_QUERY_INFO_NONE,
2813 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2814 total_size = g_file_info_get_size (info);
2815 g_object_unref (info);
2820 if (total_size == -1)
2827 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2837 current_size += n_read;
2842 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2843 if (n_written == -1)
2850 n_read -= n_written;
2856 if (progress_callback)
2857 progress_callback (current_size, total_size, progress_callback_data);
2860 /* Make sure we send full copied size */
2861 if (progress_callback)
2862 progress_callback (current_size, total_size, progress_callback_data);
2870 do_splice (int fd_in,
2875 long *bytes_transferd,
2881 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2889 else if (errsv == ENOSYS || errsv == EINVAL)
2890 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2891 _("Splice not supported"));
2893 g_set_error (error, G_IO_ERROR,
2894 g_io_error_from_errno (errsv),
2895 _("Error splicing file: %s"),
2896 g_strerror (errsv));
2901 *bytes_transferd = result;
2906 splice_stream_with_progress (GInputStream *in,
2908 GCancellable *cancellable,
2909 GFileProgressCallback progress_callback,
2910 gpointer progress_callback_data,
2913 int buffer[2] = { -1, -1 };
2920 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2921 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2923 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2927 /* avoid performance impact of querying total size when it's not needed */
2928 if (progress_callback)
2932 if (fstat (fd_in, &sbuf) == 0)
2933 total_size = sbuf.st_size;
2936 if (total_size == -1)
2939 offset_in = offset_out = 0;
2946 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2949 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2960 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2963 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2966 n_read -= n_written;
2969 if (progress_callback)
2970 progress_callback (offset_in, total_size, progress_callback_data);
2973 /* Make sure we send full copied size */
2974 if (progress_callback)
2975 progress_callback (offset_in, total_size, progress_callback_data);
2977 if (!g_close (buffer[0], error))
2980 if (!g_close (buffer[1], error))
2984 if (buffer[0] != -1)
2985 (void) g_close (buffer[0], NULL);
2986 if (buffer[1] != -1)
2987 (void) g_close (buffer[1], NULL);
2995 btrfs_reflink_with_progress (GInputStream *in,
2998 GCancellable *cancellable,
2999 GFileProgressCallback progress_callback,
3000 gpointer progress_callback_data,
3003 goffset source_size;
3007 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
3008 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
3010 if (progress_callback)
3011 source_size = g_file_info_get_size (info);
3013 /* Btrfs clone ioctl properties:
3014 * - Works at the inode level
3015 * - Doesn't work with directories
3016 * - Always follows symlinks (source and destination)
3018 * By the time we get here, *in and *out are both regular files */
3019 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3024 g_set_error_literal (error, G_IO_ERROR,
3025 G_IO_ERROR_NOT_SUPPORTED,
3026 _("Copy (reflink/clone) between mounts is not supported"));
3027 else if (errno == EINVAL)
3028 g_set_error_literal (error, G_IO_ERROR,
3029 G_IO_ERROR_NOT_SUPPORTED,
3030 _("Copy (reflink/clone) is not supported or invalid"));
3032 /* Most probably something odd happened; retry with fallback */
3033 g_set_error_literal (error, G_IO_ERROR,
3034 G_IO_ERROR_NOT_SUPPORTED,
3035 _("Copy (reflink/clone) is not supported or didn't work"));
3036 /* We retry with fallback for all error cases because Btrfs is currently
3037 * unstable, and so we can't trust it to do clone properly.
3038 * In addition, any hard errors here would cause the same failure in the
3039 * fallback manual copy as well. */
3043 /* Make sure we send full copied size */
3044 if (progress_callback)
3045 progress_callback (source_size, source_size, progress_callback_data);
3052 file_copy_fallback (GFile *source,
3054 GFileCopyFlags flags,
3055 GCancellable *cancellable,
3056 GFileProgressCallback progress_callback,
3057 gpointer progress_callback_data,
3060 gboolean ret = FALSE;
3061 GFileInputStream *file_in = NULL;
3062 GInputStream *in = NULL;
3063 GOutputStream *out = NULL;
3064 GFileInfo *info = NULL;
3066 char *attrs_to_read;
3067 gboolean do_set_attributes = FALSE;
3069 /* need to know the file type */
3070 info = g_file_query_info (source,
3071 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3072 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3078 /* Maybe copy the symlink? */
3079 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3080 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3082 target = g_file_info_get_symlink_target (info);
3085 if (!copy_symlink (destination, flags, cancellable, target, error))
3091 /* ... else fall back on a regular file copy */
3093 /* Handle "special" files (pipes, device nodes, ...)? */
3094 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3096 /* FIXME: could try to recreate device nodes and others? */
3097 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3098 _("Can't copy special file"));
3102 /* Everything else should just fall back on a regular copy. */
3104 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3107 in = G_INPUT_STREAM (file_in);
3109 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3110 cancellable, error))
3113 if (attrs_to_read != NULL)
3115 GError *tmp_error = NULL;
3117 /* Ok, ditch the previous lightweight info (on Unix we just
3118 * called lstat()); at this point we gather all the information
3119 * we need about the source from the opened file descriptor.
3121 g_object_unref (info);
3123 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3124 cancellable, &tmp_error);
3127 /* Not all gvfs backends implement query_info_on_read(), we
3128 * can just fall back to the pathname again.
3129 * https://bugzilla.gnome.org/706254
3131 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3133 g_clear_error (&tmp_error);
3134 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3135 cancellable, error);
3139 g_free (attrs_to_read);
3140 g_propagate_error (error, tmp_error);
3144 g_free (attrs_to_read);
3148 do_set_attributes = TRUE;
3151 /* In the local file path, we pass down the source info which
3152 * includes things like unix::mode, to ensure that the target file
3153 * is not created with different permissions from the source file.
3155 * If a future API like g_file_replace_with_info() is added, switch
3156 * this code to use that.
3158 if (G_IS_LOCAL_FILE (destination))
3160 if (flags & G_FILE_COPY_OVERWRITE)
3161 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3163 flags & G_FILE_COPY_BACKUP,
3164 G_FILE_CREATE_REPLACE_DESTINATION,
3166 cancellable, error);
3168 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3170 cancellable, error);
3172 else if (flags & G_FILE_COPY_OVERWRITE)
3174 out = (GOutputStream *)g_file_replace (destination,
3176 flags & G_FILE_COPY_BACKUP,
3177 G_FILE_CREATE_REPLACE_DESTINATION,
3178 cancellable, error);
3182 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3189 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3191 GError *reflink_err = NULL;
3193 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3194 progress_callback, progress_callback_data,
3197 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3199 g_clear_error (&reflink_err);
3203 g_propagate_error (error, reflink_err);
3216 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3218 GError *splice_err = NULL;
3220 if (!splice_stream_with_progress (in, out, cancellable,
3221 progress_callback, progress_callback_data,
3224 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3226 g_clear_error (&splice_err);
3230 g_propagate_error (error, splice_err);
3243 /* A plain read/write loop */
3244 if (!copy_stream_with_progress (in, out, source, cancellable,
3245 progress_callback, progress_callback_data,
3253 /* Don't care about errors in source here */
3254 (void) g_input_stream_close (in, cancellable, NULL);
3255 g_object_unref (in);
3260 /* But write errors on close are bad! */
3261 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3263 g_object_unref (out);
3266 /* Ignore errors here. Failure to copy metadata is not a hard error */
3267 /* TODO: set these attributes /before/ we do the rename() on Unix */
3268 if (ret && do_set_attributes)
3270 g_file_set_attributes_from_info (destination,
3272 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3277 g_clear_object (&info);
3284 * @source: input #GFile
3285 * @destination: destination #GFile
3286 * @flags: set of #GFileCopyFlags
3287 * @cancellable: (allow-none): optional #GCancellable object,
3289 * @progress_callback: (allow-none) (scope call): function to callback with
3290 * progress information, or %NULL if progress information is not needed
3291 * @progress_callback_data: (closure): user data to pass to @progress_callback
3292 * @error: #GError to set on error, or %NULL
3294 * Copies the file @source to the location specified by @destination.
3295 * Can not handle recursive copies of directories.
3297 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3298 * existing @destination file is overwritten.
3300 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3301 * will be copied as symlinks, otherwise the target of the
3302 * @source symlink will be copied.
3304 * If @cancellable is not %NULL, then the operation can be cancelled by
3305 * triggering the cancellable object from another thread. If the operation
3306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3308 * If @progress_callback is not %NULL, then the operation can be monitored
3309 * by setting this to a #GFileProgressCallback function.
3310 * @progress_callback_data will be passed to this function. It is guaranteed
3311 * that this callback will be called after all data has been transferred with
3312 * the total number of bytes copied during the operation.
3314 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3315 * is returned, independent on the status of the @destination.
3317 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3318 * the error %G_IO_ERROR_EXISTS is returned.
3320 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3321 * error is returned. If trying to overwrite a directory with a directory the
3322 * %G_IO_ERROR_WOULD_MERGE error is returned.
3324 * If the source is a directory and the target does not exist, or
3325 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3326 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3328 * If you are interested in copying the #GFile object itself (not the on-disk
3329 * file), see g_file_dup().
3331 * Returns: %TRUE on success, %FALSE otherwise.
3334 g_file_copy (GFile *source,
3336 GFileCopyFlags flags,
3337 GCancellable *cancellable,
3338 GFileProgressCallback progress_callback,
3339 gpointer progress_callback_data,
3346 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3347 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3352 iface = G_FILE_GET_IFACE (destination);
3356 res = (* iface->copy) (source, destination,
3358 progress_callback, progress_callback_data,
3364 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3366 g_propagate_error (error, my_error);
3370 g_clear_error (&my_error);
3373 /* If the types are different, and the destination method failed
3374 * also try the source method
3376 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3378 iface = G_FILE_GET_IFACE (source);
3383 res = (* iface->copy) (source, destination,
3385 progress_callback, progress_callback_data,
3391 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3393 g_propagate_error (error, my_error);
3397 g_clear_error (&my_error);
3401 return file_copy_fallback (source, destination, flags, cancellable,
3402 progress_callback, progress_callback_data,
3407 * g_file_copy_async: (skip)
3408 * @source: input #GFile
3409 * @destination: destination #GFile
3410 * @flags: set of #GFileCopyFlags
3411 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3413 * @cancellable: (allow-none): optional #GCancellable object,
3415 * @progress_callback: (allow-none): function to callback with progress
3416 * information, or %NULL if progress information is not needed
3417 * @progress_callback_data: (closure): user data to pass to @progress_callback
3418 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3419 * @user_data: the data to pass to callback function
3421 * Copies the file @source to the location specified by @destination
3422 * asynchronously. For details of the behaviour, see g_file_copy().
3424 * If @progress_callback is not %NULL, then that function that will be called
3425 * just like in g_file_copy(), however the callback will run in the main loop,
3426 * not in the thread that is doing the I/O operation.
3428 * When the operation is finished, @callback will be called. You can then call
3429 * g_file_copy_finish() to get the result of the operation.
3432 g_file_copy_async (GFile *source,
3434 GFileCopyFlags flags,
3436 GCancellable *cancellable,
3437 GFileProgressCallback progress_callback,
3438 gpointer progress_callback_data,
3439 GAsyncReadyCallback callback,
3444 g_return_if_fail (G_IS_FILE (source));
3445 g_return_if_fail (G_IS_FILE (destination));
3447 iface = G_FILE_GET_IFACE (source);
3448 (* iface->copy_async) (source,
3454 progress_callback_data,
3460 * g_file_copy_finish:
3461 * @file: input #GFile
3462 * @res: a #GAsyncResult
3463 * @error: a #GError, or %NULL
3465 * Finishes copying the file started with g_file_copy_async().
3467 * Returns: a %TRUE on success, %FALSE on error.
3470 g_file_copy_finish (GFile *file,
3476 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3477 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3479 if (g_async_result_legacy_propagate_error (res, error))
3482 iface = G_FILE_GET_IFACE (file);
3483 return (* iface->copy_finish) (file, res, error);
3488 * @source: #GFile pointing to the source location
3489 * @destination: #GFile pointing to the destination location
3490 * @flags: set of #GFileCopyFlags
3491 * @cancellable: (allow-none): optional #GCancellable object,
3493 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3494 * function for updates
3495 * @progress_callback_data: (closure): gpointer to user data for
3496 * the callback function
3497 * @error: #GError for returning error conditions, or %NULL
3499 * Tries to move the file or directory @source to the location specified
3500 * by @destination. If native move operations are supported then this is
3501 * used, otherwise a copy + delete fallback is used. The native
3502 * implementation may support moving directories (for instance on moves
3503 * inside the same filesystem), but the fallback code does not.
3505 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3506 * existing @destination file is overwritten.
3508 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3509 * will be copied as symlinks, otherwise the target of the
3510 * @source symlink will be copied.
3512 * If @cancellable is not %NULL, then the operation can be cancelled by
3513 * triggering the cancellable object from another thread. If the operation
3514 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3516 * If @progress_callback is not %NULL, then the operation can be monitored
3517 * by setting this to a #GFileProgressCallback function.
3518 * @progress_callback_data will be passed to this function. It is
3519 * guaranteed that this callback will be called after all data has been
3520 * transferred with the total number of bytes copied during the operation.
3522 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3523 * error is returned, independent on the status of the @destination.
3525 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3526 * then the error %G_IO_ERROR_EXISTS is returned.
3528 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3529 * error is returned. If trying to overwrite a directory with a directory the
3530 * %G_IO_ERROR_WOULD_MERGE error is returned.
3532 * If the source is a directory and the target does not exist, or
3533 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3534 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3535 * move operation isn't available).
3537 * Returns: %TRUE on successful move, %FALSE otherwise.
3540 g_file_move (GFile *source,
3542 GFileCopyFlags flags,
3543 GCancellable *cancellable,
3544 GFileProgressCallback progress_callback,
3545 gpointer progress_callback_data,
3552 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3553 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3555 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3558 iface = G_FILE_GET_IFACE (destination);
3562 res = (* iface->move) (source, destination,
3564 progress_callback, progress_callback_data,
3570 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3572 g_propagate_error (error, my_error);
3577 /* If the types are different, and the destination method failed
3578 * also try the source method
3580 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3582 iface = G_FILE_GET_IFACE (source);
3587 res = (* iface->move) (source, destination,
3589 progress_callback, progress_callback_data,
3595 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3597 g_propagate_error (error, my_error);
3603 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3605 g_set_error_literal (error, G_IO_ERROR,
3606 G_IO_ERROR_NOT_SUPPORTED,
3607 _("Operation not supported"));
3611 flags |= G_FILE_COPY_ALL_METADATA;
3612 if (!g_file_copy (source, destination, flags, cancellable,
3613 progress_callback, progress_callback_data,
3617 return g_file_delete (source, cancellable, error);
3621 * g_file_make_directory:
3622 * @file: input #GFile
3623 * @cancellable: (allow-none): optional #GCancellable object,
3625 * @error: a #GError, or %NULL
3627 * Creates a directory. Note that this will only create a child directory
3628 * of the immediate parent directory of the path or URI given by the #GFile.
3629 * To recursively create directories, see g_file_make_directory_with_parents().
3630 * This function will fail if the parent directory does not exist, setting
3631 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3632 * creating directories, this function will fail, setting @error to
3633 * %G_IO_ERROR_NOT_SUPPORTED.
3635 * For a local #GFile the newly created directory will have the default
3636 * (current) ownership and permissions of the current process.
3638 * If @cancellable is not %NULL, then the operation can be cancelled by
3639 * triggering the cancellable object from another thread. If the operation
3640 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3642 * Returns: %TRUE on successful creation, %FALSE otherwise.
3645 g_file_make_directory (GFile *file,
3646 GCancellable *cancellable,
3651 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3653 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3656 iface = G_FILE_GET_IFACE (file);
3658 if (iface->make_directory == NULL)
3660 g_set_error_literal (error, G_IO_ERROR,
3661 G_IO_ERROR_NOT_SUPPORTED,
3662 _("Operation not supported"));
3666 return (* iface->make_directory) (file, cancellable, error);
3670 * g_file_make_directory_async:
3671 * @file: input #GFile
3672 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3674 * @cancellable: (allow-none): optional #GCancellable object,
3676 * @callback: a #GAsyncReadyCallback to call
3677 * when the request is satisfied
3678 * @user_data: the data to pass to callback function
3680 * Asynchronously creates a directory.
3682 * Virtual: make_directory_async
3686 g_file_make_directory_async (GFile *file,
3688 GCancellable *cancellable,
3689 GAsyncReadyCallback callback,
3694 g_return_if_fail (G_IS_FILE (file));
3696 iface = G_FILE_GET_IFACE (file);
3697 (* iface->make_directory_async) (file,
3705 * g_file_make_directory_finish:
3706 * @file: input #GFile
3707 * @result: a #GAsyncResult
3708 * @error: a #GError, or %NULL
3710 * Finishes an asynchronous directory creation, started with
3711 * g_file_make_directory_async().
3713 * Virtual: make_directory_finish
3714 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3718 g_file_make_directory_finish (GFile *file,
3719 GAsyncResult *result,
3724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3725 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3727 iface = G_FILE_GET_IFACE (file);
3728 return (* iface->make_directory_finish) (file, result, error);
3732 * g_file_make_directory_with_parents:
3733 * @file: input #GFile
3734 * @cancellable: (allow-none): optional #GCancellable object,
3736 * @error: a #GError, or %NULL
3738 * Creates a directory and any parent directories that may not
3739 * exist similar to 'mkdir -p'. If the file system does not support
3740 * creating directories, this function will fail, setting @error to
3741 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3742 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3743 * the similar g_mkdir_with_parents().
3745 * For a local #GFile the newly created directories will have the default
3746 * (current) ownership and permissions of the current process.
3748 * If @cancellable is not %NULL, then the operation can be cancelled by
3749 * triggering the cancellable object from another thread. If the operation
3750 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3752 * Returns: %TRUE if all directories have been successfully created, %FALSE
3758 g_file_make_directory_with_parents (GFile *file,
3759 GCancellable *cancellable,
3762 GFile *work_file = NULL;
3763 GList *list = NULL, *l;
3764 GError *my_error = NULL;
3766 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3771 g_file_make_directory (file, cancellable, &my_error);
3772 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3775 g_propagate_error (error, my_error);
3776 return my_error == NULL;
3779 work_file = g_object_ref (file);
3781 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3785 parent_file = g_file_get_parent (work_file);
3786 if (parent_file == NULL)
3789 g_clear_error (&my_error);
3790 g_file_make_directory (parent_file, cancellable, &my_error);
3792 g_object_unref (work_file);
3793 work_file = g_object_ref (parent_file);
3795 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3796 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3798 g_object_unref (parent_file);
3801 for (l = list; my_error == NULL && l; l = l->next)
3803 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3807 g_object_unref (work_file);
3810 while (list != NULL)
3812 g_object_unref ((GFile *) list->data);
3813 list = g_list_remove (list, list->data);
3816 if (my_error != NULL)
3818 g_propagate_error (error, my_error);
3822 return g_file_make_directory (file, cancellable, error);
3826 * g_file_make_symbolic_link:
3827 * @file: a #GFile with the name of the symlink to create
3828 * @symlink_value: a string with the path for the target of the new symlink
3829 * @cancellable: (allow-none): optional #GCancellable object,
3833 * Creates a symbolic link named @file which contains the string
3836 * If @cancellable is not %NULL, then the operation can be cancelled by
3837 * triggering the cancellable object from another thread. If the operation
3838 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3840 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3843 g_file_make_symbolic_link (GFile *file,
3844 const char *symlink_value,
3845 GCancellable *cancellable,
3850 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3851 g_return_val_if_fail (symlink_value != NULL, FALSE);
3853 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3856 if (*symlink_value == '\0')
3858 g_set_error_literal (error, G_IO_ERROR,
3859 G_IO_ERROR_INVALID_ARGUMENT,
3860 _("Invalid symlink value given"));
3864 iface = G_FILE_GET_IFACE (file);
3866 if (iface->make_symbolic_link == NULL)
3868 g_set_error_literal (error, G_IO_ERROR,
3869 G_IO_ERROR_NOT_SUPPORTED,
3870 _("Operation not supported"));
3874 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3879 * @file: input #GFile
3880 * @cancellable: (allow-none): optional #GCancellable object,
3882 * @error: a #GError, or %NULL
3884 * Deletes a file. If the @file is a directory, it will only be
3885 * deleted if it is empty. This has the same semantics as g_unlink().
3887 * If @cancellable is not %NULL, then the operation can be cancelled by
3888 * triggering the cancellable object from another thread. If the operation
3889 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3891 * Virtual: delete_file
3892 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3895 g_file_delete (GFile *file,
3896 GCancellable *cancellable,
3901 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3903 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3906 iface = G_FILE_GET_IFACE (file);
3908 if (iface->delete_file == NULL)
3910 g_set_error_literal (error, G_IO_ERROR,
3911 G_IO_ERROR_NOT_SUPPORTED,
3912 _("Operation not supported"));
3916 return (* iface->delete_file) (file, cancellable, error);
3920 * g_file_delete_async:
3921 * @file: input #GFile
3922 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3924 * @cancellable: (allow-none): optional #GCancellable object,
3926 * @callback: a #GAsyncReadyCallback to call
3927 * when the request is satisfied
3928 * @user_data: the data to pass to callback function
3930 * Asynchronously delete a file. If the @file is a directory, it will
3931 * only be deleted if it is empty. This has the same semantics as
3934 * Virtual: delete_file_async
3938 g_file_delete_async (GFile *file,
3940 GCancellable *cancellable,
3941 GAsyncReadyCallback callback,
3946 g_return_if_fail (G_IS_FILE (file));
3948 iface = G_FILE_GET_IFACE (file);
3949 (* iface->delete_file_async) (file,
3957 * g_file_delete_finish:
3958 * @file: input #GFile
3959 * @result: a #GAsyncResult
3960 * @error: a #GError, or %NULL
3962 * Finishes deleting a file started with g_file_delete_async().
3964 * Virtual: delete_file_finish
3965 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3969 g_file_delete_finish (GFile *file,
3970 GAsyncResult *result,
3975 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3976 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3978 if (g_async_result_legacy_propagate_error (result, error))
3981 iface = G_FILE_GET_IFACE (file);
3982 return (* iface->delete_file_finish) (file, result, error);
3987 * @file: #GFile to send to trash
3988 * @cancellable: (allow-none): optional #GCancellable object,
3990 * @error: a #GError, or %NULL
3992 * Sends @file to the "Trashcan", if possible. This is similar to
3993 * deleting it, but the user can recover it before emptying the trashcan.
3994 * Not all file systems support trashing, so this call can return the
3995 * %G_IO_ERROR_NOT_SUPPORTED error.
3997 * If @cancellable is not %NULL, then the operation can be cancelled by
3998 * triggering the cancellable object from another thread. If the operation
3999 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4002 * Returns: %TRUE on successful trash, %FALSE otherwise.
4005 g_file_trash (GFile *file,
4006 GCancellable *cancellable,
4011 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4013 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4016 iface = G_FILE_GET_IFACE (file);
4018 if (iface->trash == NULL)
4020 g_set_error_literal (error,
4021 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4022 _("Trash not supported"));
4026 return (* iface->trash) (file, cancellable, error);
4030 * g_file_trash_async:
4031 * @file: input #GFile
4032 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4034 * @cancellable: (allow-none): optional #GCancellable object,
4036 * @callback: a #GAsyncReadyCallback to call
4037 * when the request is satisfied
4038 * @user_data: the data to pass to callback function
4040 * Asynchronously sends @file to the Trash location, if possible.
4042 * Virtual: trash_async
4046 g_file_trash_async (GFile *file,
4048 GCancellable *cancellable,
4049 GAsyncReadyCallback callback,
4054 g_return_if_fail (G_IS_FILE (file));
4056 iface = G_FILE_GET_IFACE (file);
4057 (* iface->trash_async) (file,
4065 * g_file_trash_finish:
4066 * @file: input #GFile
4067 * @result: a #GAsyncResult
4068 * @error: a #GError, or %NULL
4070 * Finishes an asynchronous file trashing operation, started with
4071 * g_file_trash_async().
4073 * Virtual: trash_finish
4074 * Returns: %TRUE on successful trash, %FALSE otherwise.
4078 g_file_trash_finish (GFile *file,
4079 GAsyncResult *result,
4084 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4085 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4087 iface = G_FILE_GET_IFACE (file);
4088 return (* iface->trash_finish) (file, result, error);
4092 * g_file_set_display_name:
4093 * @file: input #GFile
4094 * @display_name: a string
4095 * @cancellable: (allow-none): optional #GCancellable object,
4097 * @error: a #GError, or %NULL
4099 * Renames @file to the specified display name.
4101 * The display name is converted from UTF-8 to the correct encoding
4102 * for the target filesystem if possible and the @file is renamed to this.
4104 * If you want to implement a rename operation in the user interface the
4105 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4106 * initial value in the rename widget, and then the result after editing
4107 * should be passed to g_file_set_display_name().
4109 * On success the resulting converted filename is returned.
4111 * If @cancellable is not %NULL, then the operation can be cancelled by
4112 * triggering the cancellable object from another thread. If the operation
4113 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4115 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4116 * or %NULL if there was an error.
4117 * Free the returned object with g_object_unref().
4120 g_file_set_display_name (GFile *file,
4121 const gchar *display_name,
4122 GCancellable *cancellable,
4127 g_return_val_if_fail (G_IS_FILE (file), NULL);
4128 g_return_val_if_fail (display_name != NULL, NULL);
4130 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4134 G_IO_ERROR_INVALID_ARGUMENT,
4135 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4139 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4142 iface = G_FILE_GET_IFACE (file);
4144 return (* iface->set_display_name) (file, display_name, cancellable, error);
4148 * g_file_set_display_name_async:
4149 * @file: input #GFile
4150 * @display_name: a string
4151 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4153 * @cancellable: (allow-none): optional #GCancellable object,
4155 * @callback: (scope async): a #GAsyncReadyCallback to call
4156 * when the request is satisfied
4157 * @user_data: (closure): the data to pass to callback function
4159 * Asynchronously sets the display name for a given #GFile.
4161 * For more details, see g_file_set_display_name() which is
4162 * the synchronous version of this call.
4164 * When the operation is finished, @callback will be called.
4165 * You can then call g_file_set_display_name_finish() to get
4166 * the result of the operation.
4169 g_file_set_display_name_async (GFile *file,
4170 const gchar *display_name,
4172 GCancellable *cancellable,
4173 GAsyncReadyCallback callback,
4178 g_return_if_fail (G_IS_FILE (file));
4179 g_return_if_fail (display_name != NULL);
4181 iface = G_FILE_GET_IFACE (file);
4182 (* iface->set_display_name_async) (file,
4191 * g_file_set_display_name_finish:
4192 * @file: input #GFile
4193 * @res: a #GAsyncResult
4194 * @error: a #GError, or %NULL
4196 * Finishes setting a display name started with
4197 * g_file_set_display_name_async().
4199 * Returns: (transfer full): a #GFile or %NULL on error.
4200 * Free the returned object with g_object_unref().
4203 g_file_set_display_name_finish (GFile *file,
4209 g_return_val_if_fail (G_IS_FILE (file), NULL);
4210 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4212 if (g_async_result_legacy_propagate_error (res, error))
4215 iface = G_FILE_GET_IFACE (file);
4216 return (* iface->set_display_name_finish) (file, res, error);
4220 * g_file_query_settable_attributes:
4221 * @file: input #GFile
4222 * @cancellable: (allow-none): optional #GCancellable object,
4224 * @error: a #GError, or %NULL
4226 * Obtain the list of settable attributes for the file.
4228 * Returns the type and full attribute name of all the attributes
4229 * that can be set on this file. This doesn't mean setting it will
4230 * always succeed though, you might get an access failure, or some
4231 * specific file may not support a specific attribute.
4233 * If @cancellable is not %NULL, then the operation can be cancelled by
4234 * triggering the cancellable object from another thread. If the operation
4235 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4237 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4238 * When you are done with it, release it with
4239 * g_file_attribute_info_list_unref()
4241 GFileAttributeInfoList *
4242 g_file_query_settable_attributes (GFile *file,
4243 GCancellable *cancellable,
4248 GFileAttributeInfoList *list;
4250 g_return_val_if_fail (G_IS_FILE (file), NULL);
4252 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4255 iface = G_FILE_GET_IFACE (file);
4257 if (iface->query_settable_attributes == NULL)
4258 return g_file_attribute_info_list_new ();
4261 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4265 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4267 list = g_file_attribute_info_list_new ();
4268 g_error_free (my_error);
4271 g_propagate_error (error, my_error);
4278 * g_file_query_writable_namespaces:
4279 * @file: input #GFile
4280 * @cancellable: (allow-none): optional #GCancellable object,
4282 * @error: a #GError, or %NULL
4284 * Obtain the list of attribute namespaces where new attributes
4285 * can be created by a user. An example of this is extended
4286 * attributes (in the "xattr" namespace).
4288 * If @cancellable is not %NULL, then the operation can be cancelled by
4289 * triggering the cancellable object from another thread. If the operation
4290 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4292 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4293 * When you are done with it, release it with
4294 * g_file_attribute_info_list_unref()
4296 GFileAttributeInfoList *
4297 g_file_query_writable_namespaces (GFile *file,
4298 GCancellable *cancellable,
4303 GFileAttributeInfoList *list;
4305 g_return_val_if_fail (G_IS_FILE (file), NULL);
4307 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4310 iface = G_FILE_GET_IFACE (file);
4312 if (iface->query_writable_namespaces == NULL)
4313 return g_file_attribute_info_list_new ();
4316 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4320 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4322 list = g_file_attribute_info_list_new ();
4323 g_error_free (my_error);
4326 g_propagate_error (error, my_error);
4333 * g_file_set_attribute:
4334 * @file: input #GFile
4335 * @attribute: a string containing the attribute's name
4336 * @type: The type of the attribute
4337 * @value_p: (allow-none): a pointer to the value (or the pointer
4338 * itself if the type is a pointer type)
4339 * @flags: a set of #GFileQueryInfoFlags
4340 * @cancellable: (allow-none): optional #GCancellable object,
4342 * @error: a #GError, or %NULL
4344 * Sets an attribute in the file with attribute name @attribute to @value.
4346 * Some attributes can be unset by setting @attribute to
4347 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4349 * If @cancellable is not %NULL, then the operation can be cancelled by
4350 * triggering the cancellable object from another thread. If the operation
4351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4353 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4356 g_file_set_attribute (GFile *file,
4357 const gchar *attribute,
4358 GFileAttributeType type,
4360 GFileQueryInfoFlags flags,
4361 GCancellable *cancellable,
4366 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4367 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4369 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4372 iface = G_FILE_GET_IFACE (file);
4374 if (iface->set_attribute == NULL)
4376 g_set_error_literal (error, G_IO_ERROR,
4377 G_IO_ERROR_NOT_SUPPORTED,
4378 _("Operation not supported"));
4382 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4386 * g_file_set_attributes_from_info:
4387 * @file: input #GFile
4388 * @info: a #GFileInfo
4389 * @flags: #GFileQueryInfoFlags
4390 * @cancellable: (allow-none): optional #GCancellable object,
4392 * @error: a #GError, or %NULL
4394 * Tries to set all attributes in the #GFileInfo on the target
4395 * values, not stopping on the first error.
4397 * If there is any error during this operation then @error will
4398 * be set to the first error. Error on particular fields are flagged
4399 * by setting the "status" field in the attribute value to
4400 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4401 * also detect further errors.
4403 * If @cancellable is not %NULL, then the operation can be cancelled by
4404 * triggering the cancellable object from another thread. If the operation
4405 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4407 * Returns: %FALSE if there was any error, %TRUE otherwise.
4410 g_file_set_attributes_from_info (GFile *file,
4412 GFileQueryInfoFlags flags,
4413 GCancellable *cancellable,
4418 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4419 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4421 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4424 g_file_info_clear_status (info);
4426 iface = G_FILE_GET_IFACE (file);
4428 return (* iface->set_attributes_from_info) (file,
4436 g_file_real_set_attributes_from_info (GFile *file,
4438 GFileQueryInfoFlags flags,
4439 GCancellable *cancellable,
4445 GFileAttributeValue *value;
4449 attributes = g_file_info_list_attributes (info, NULL);
4451 for (i = 0; attributes[i] != NULL; i++)
4453 value = _g_file_info_get_attribute_value (info, attributes[i]);
4455 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4458 if (!g_file_set_attribute (file, attributes[i],
4459 value->type, _g_file_attribute_value_peek_as_pointer (value),
4460 flags, cancellable, error))
4462 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4464 /* Don't set error multiple times */
4468 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4471 g_strfreev (attributes);
4477 * g_file_set_attributes_async:
4478 * @file: input #GFile
4479 * @info: a #GFileInfo
4480 * @flags: a #GFileQueryInfoFlags
4481 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4483 * @cancellable: (allow-none): optional #GCancellable object,
4485 * @callback: (scope async): a #GAsyncReadyCallback
4486 * @user_data: (closure): a #gpointer
4488 * Asynchronously sets the attributes of @file with @info.
4490 * For more details, see g_file_set_attributes_from_info(),
4491 * which is the synchronous version of this call.
4493 * When the operation is finished, @callback will be called.
4494 * You can then call g_file_set_attributes_finish() to get
4495 * the result of the operation.
4498 g_file_set_attributes_async (GFile *file,
4500 GFileQueryInfoFlags flags,
4502 GCancellable *cancellable,
4503 GAsyncReadyCallback callback,
4508 g_return_if_fail (G_IS_FILE (file));
4509 g_return_if_fail (G_IS_FILE_INFO (info));
4511 iface = G_FILE_GET_IFACE (file);
4512 (* iface->set_attributes_async) (file,
4522 * g_file_set_attributes_finish:
4523 * @file: input #GFile
4524 * @result: a #GAsyncResult
4525 * @info: (out) (transfer full): a #GFileInfo
4526 * @error: a #GError, or %NULL
4528 * Finishes setting an attribute started in g_file_set_attributes_async().
4530 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4533 g_file_set_attributes_finish (GFile *file,
4534 GAsyncResult *result,
4540 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4541 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4543 /* No standard handling of errors here, as we must set info even
4546 iface = G_FILE_GET_IFACE (file);
4547 return (* iface->set_attributes_finish) (file, result, info, error);
4551 * g_file_set_attribute_string:
4552 * @file: input #GFile
4553 * @attribute: a string containing the attribute's name
4554 * @value: a string containing the attribute's value
4555 * @flags: #GFileQueryInfoFlags
4556 * @cancellable: (allow-none): optional #GCancellable object,
4558 * @error: a #GError, or %NULL
4560 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4561 * If @attribute is of a different type, this operation will fail.
4563 * If @cancellable is not %NULL, then the operation can be cancelled by
4564 * triggering the cancellable object from another thread. If the operation
4565 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4567 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4570 g_file_set_attribute_string (GFile *file,
4571 const char *attribute,
4573 GFileQueryInfoFlags flags,
4574 GCancellable *cancellable,
4577 return g_file_set_attribute (file, attribute,
4578 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4579 flags, cancellable, error);
4583 * g_file_set_attribute_byte_string:
4584 * @file: input #GFile
4585 * @attribute: a string containing the attribute's name
4586 * @value: a string containing the attribute's new value
4587 * @flags: a #GFileQueryInfoFlags
4588 * @cancellable: (allow-none): optional #GCancellable object,
4590 * @error: a #GError, or %NULL
4592 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4593 * If @attribute is of a different type, this operation will fail,
4596 * If @cancellable is not %NULL, then the operation can be cancelled by
4597 * triggering the cancellable object from another thread. If the operation
4598 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4600 * Returns: %TRUE if the @attribute was successfully set to @value
4601 * in the @file, %FALSE otherwise.
4604 g_file_set_attribute_byte_string (GFile *file,
4605 const gchar *attribute,
4607 GFileQueryInfoFlags flags,
4608 GCancellable *cancellable,
4611 return g_file_set_attribute (file, attribute,
4612 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4613 flags, cancellable, error);
4617 * g_file_set_attribute_uint32:
4618 * @file: input #GFile
4619 * @attribute: a string containing the attribute's name
4620 * @value: a #guint32 containing the attribute's new value
4621 * @flags: a #GFileQueryInfoFlags
4622 * @cancellable: (allow-none): optional #GCancellable object,
4624 * @error: a #GError, or %NULL
4626 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4627 * If @attribute is of a different type, this operation will fail.
4629 * If @cancellable is not %NULL, then the operation can be cancelled by
4630 * triggering the cancellable object from another thread. If the operation
4631 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4633 * Returns: %TRUE if the @attribute was successfully set to @value
4634 * in the @file, %FALSE otherwise.
4637 g_file_set_attribute_uint32 (GFile *file,
4638 const gchar *attribute,
4640 GFileQueryInfoFlags flags,
4641 GCancellable *cancellable,
4644 return g_file_set_attribute (file, attribute,
4645 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4646 flags, cancellable, error);
4650 * g_file_set_attribute_int32:
4651 * @file: input #GFile
4652 * @attribute: a string containing the attribute's name
4653 * @value: a #gint32 containing the attribute's new value
4654 * @flags: a #GFileQueryInfoFlags
4655 * @cancellable: (allow-none): optional #GCancellable object,
4657 * @error: a #GError, or %NULL
4659 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4660 * If @attribute is of a different type, this operation will fail.
4662 * If @cancellable is not %NULL, then the operation can be cancelled by
4663 * triggering the cancellable object from another thread. If the operation
4664 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4666 * Returns: %TRUE if the @attribute was successfully set to @value
4667 * in the @file, %FALSE otherwise.
4670 g_file_set_attribute_int32 (GFile *file,
4671 const gchar *attribute,
4673 GFileQueryInfoFlags flags,
4674 GCancellable *cancellable,
4677 return g_file_set_attribute (file, attribute,
4678 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4679 flags, cancellable, error);
4683 * g_file_set_attribute_uint64:
4684 * @file: input #GFile
4685 * @attribute: a string containing the attribute's name
4686 * @value: a #guint64 containing the attribute's new value
4687 * @flags: a #GFileQueryInfoFlags
4688 * @cancellable: (allow-none): optional #GCancellable object,
4690 * @error: a #GError, or %NULL
4692 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4693 * If @attribute is of a different type, this operation will fail.
4695 * If @cancellable is not %NULL, then the operation can be cancelled by
4696 * triggering the cancellable object from another thread. If the operation
4697 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4699 * Returns: %TRUE if the @attribute was successfully set to @value
4700 * in the @file, %FALSE otherwise.
4703 g_file_set_attribute_uint64 (GFile *file,
4704 const gchar *attribute,
4706 GFileQueryInfoFlags flags,
4707 GCancellable *cancellable,
4710 return g_file_set_attribute (file, attribute,
4711 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4712 flags, cancellable, error);
4716 * g_file_set_attribute_int64:
4717 * @file: input #GFile
4718 * @attribute: a string containing the attribute's name
4719 * @value: a #guint64 containing the attribute's new value
4720 * @flags: a #GFileQueryInfoFlags
4721 * @cancellable: (allow-none): optional #GCancellable object,
4723 * @error: a #GError, or %NULL
4725 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4726 * If @attribute is of a different type, this operation will fail.
4728 * If @cancellable is not %NULL, then the operation can be cancelled by
4729 * triggering the cancellable object from another thread. If the operation
4730 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4732 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4735 g_file_set_attribute_int64 (GFile *file,
4736 const gchar *attribute,
4738 GFileQueryInfoFlags flags,
4739 GCancellable *cancellable,
4742 return g_file_set_attribute (file, attribute,
4743 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4744 flags, cancellable, error);
4748 * g_file_mount_mountable:
4749 * @file: input #GFile
4750 * @flags: flags affecting the operation
4751 * @mount_operation: (allow-none): a #GMountOperation,
4752 * or %NULL to avoid user interaction
4753 * @cancellable: (allow-none): optional #GCancellable object,
4755 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4756 * when the request is satisfied, or %NULL
4757 * @user_data: (closure): the data to pass to callback function
4759 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4760 * Using @mount_operation, you can request callbacks when, for instance,
4761 * passwords are needed during authentication.
4763 * If @cancellable is not %NULL, then the operation can be cancelled by
4764 * triggering the cancellable object from another thread. If the operation
4765 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4767 * When the operation is finished, @callback will be called.
4768 * You can then call g_file_mount_mountable_finish() to get
4769 * the result of the operation.
4772 g_file_mount_mountable (GFile *file,
4773 GMountMountFlags flags,
4774 GMountOperation *mount_operation,
4775 GCancellable *cancellable,
4776 GAsyncReadyCallback callback,
4781 g_return_if_fail (G_IS_FILE (file));
4783 iface = G_FILE_GET_IFACE (file);
4785 if (iface->mount_mountable == NULL)
4787 g_task_report_new_error (file, callback, user_data,
4788 g_file_mount_mountable,
4789 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4790 _("Operation not supported"));
4794 (* iface->mount_mountable) (file,
4803 * g_file_mount_mountable_finish:
4804 * @file: input #GFile
4805 * @result: a #GAsyncResult
4806 * @error: a #GError, or %NULL
4808 * Finishes a mount operation. See g_file_mount_mountable() for details.
4810 * Finish an asynchronous mount operation that was started
4811 * with g_file_mount_mountable().
4813 * Returns: (transfer full): a #GFile or %NULL on error.
4814 * Free the returned object with g_object_unref().
4817 g_file_mount_mountable_finish (GFile *file,
4818 GAsyncResult *result,
4823 g_return_val_if_fail (G_IS_FILE (file), NULL);
4824 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4826 if (g_async_result_legacy_propagate_error (result, error))
4828 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4829 return g_task_propagate_pointer (G_TASK (result), error);
4831 iface = G_FILE_GET_IFACE (file);
4832 return (* iface->mount_mountable_finish) (file, result, error);
4836 * g_file_unmount_mountable:
4837 * @file: input #GFile
4838 * @flags: flags affecting the operation
4839 * @cancellable: (allow-none): optional #GCancellable object,
4841 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4842 * when the request is satisfied, or %NULL
4843 * @user_data: (closure): the data to pass to callback function
4845 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4847 * If @cancellable is not %NULL, then the operation can be cancelled by
4848 * triggering the cancellable object from another thread. If the operation
4849 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4851 * When the operation is finished, @callback will be called.
4852 * You can then call g_file_unmount_mountable_finish() to get
4853 * the result of the operation.
4855 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4858 g_file_unmount_mountable (GFile *file,
4859 GMountUnmountFlags flags,
4860 GCancellable *cancellable,
4861 GAsyncReadyCallback callback,
4866 g_return_if_fail (G_IS_FILE (file));
4868 iface = G_FILE_GET_IFACE (file);
4870 if (iface->unmount_mountable == NULL)
4872 g_task_report_new_error (file, callback, user_data,
4873 g_file_unmount_mountable_with_operation,
4874 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4875 _("Operation not supported"));
4879 (* iface->unmount_mountable) (file,
4887 * g_file_unmount_mountable_finish:
4888 * @file: input #GFile
4889 * @result: a #GAsyncResult
4890 * @error: a #GError, or %NULL
4892 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4894 * Finish an asynchronous unmount operation that was started
4895 * with g_file_unmount_mountable().
4897 * Returns: %TRUE if the operation finished successfully.
4900 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4904 g_file_unmount_mountable_finish (GFile *file,
4905 GAsyncResult *result,
4910 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4911 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4913 if (g_async_result_legacy_propagate_error (result, error))
4915 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4916 return g_task_propagate_boolean (G_TASK (result), error);
4918 iface = G_FILE_GET_IFACE (file);
4919 return (* iface->unmount_mountable_finish) (file, result, error);
4923 * g_file_unmount_mountable_with_operation:
4924 * @file: input #GFile
4925 * @flags: flags affecting the operation
4926 * @mount_operation: (allow-none): a #GMountOperation,
4927 * or %NULL to avoid user interaction
4928 * @cancellable: (allow-none): optional #GCancellable object,
4930 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4931 * when the request is satisfied, or %NULL
4932 * @user_data: (closure): the data to pass to callback function
4934 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4936 * If @cancellable is not %NULL, then the operation can be cancelled by
4937 * triggering the cancellable object from another thread. If the operation
4938 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4940 * When the operation is finished, @callback will be called.
4941 * You can then call g_file_unmount_mountable_finish() to get
4942 * the result of the operation.
4947 g_file_unmount_mountable_with_operation (GFile *file,
4948 GMountUnmountFlags flags,
4949 GMountOperation *mount_operation,
4950 GCancellable *cancellable,
4951 GAsyncReadyCallback callback,
4956 g_return_if_fail (G_IS_FILE (file));
4958 iface = G_FILE_GET_IFACE (file);
4960 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4962 g_task_report_new_error (file, callback, user_data,
4963 g_file_unmount_mountable_with_operation,
4964 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4965 _("Operation not supported"));
4969 if (iface->unmount_mountable_with_operation != NULL)
4970 (* iface->unmount_mountable_with_operation) (file,
4977 (* iface->unmount_mountable) (file,
4985 * g_file_unmount_mountable_with_operation_finish:
4986 * @file: input #GFile
4987 * @result: a #GAsyncResult
4988 * @error: a #GError, or %NULL
4990 * Finishes an unmount operation,
4991 * see g_file_unmount_mountable_with_operation() for details.
4993 * Finish an asynchronous unmount operation that was started
4994 * with g_file_unmount_mountable_with_operation().
4996 * Returns: %TRUE if the operation finished successfully.
5002 g_file_unmount_mountable_with_operation_finish (GFile *file,
5003 GAsyncResult *result,
5008 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5009 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5011 if (g_async_result_legacy_propagate_error (result, error))
5013 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5014 return g_task_propagate_boolean (G_TASK (result), error);
5016 iface = G_FILE_GET_IFACE (file);
5017 if (iface->unmount_mountable_with_operation_finish != NULL)
5018 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5020 return (* iface->unmount_mountable_finish) (file, result, error);
5024 * g_file_eject_mountable:
5025 * @file: input #GFile
5026 * @flags: flags affecting the operation
5027 * @cancellable: (allow-none): optional #GCancellable object,
5029 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5030 * when the request is satisfied, or %NULL
5031 * @user_data: (closure): the data to pass to callback function
5033 * Starts an asynchronous eject on a mountable.
5034 * When this operation has completed, @callback will be called with
5035 * @user_user data, and the operation can be finalized with
5036 * g_file_eject_mountable_finish().
5038 * If @cancellable is not %NULL, then the operation can be cancelled by
5039 * triggering the cancellable object from another thread. If the operation
5040 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5042 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5045 g_file_eject_mountable (GFile *file,
5046 GMountUnmountFlags flags,
5047 GCancellable *cancellable,
5048 GAsyncReadyCallback callback,
5053 g_return_if_fail (G_IS_FILE (file));
5055 iface = G_FILE_GET_IFACE (file);
5057 if (iface->eject_mountable == NULL)
5059 g_task_report_new_error (file, callback, user_data,
5060 g_file_eject_mountable_with_operation,
5061 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5062 _("Operation not supported"));
5066 (* iface->eject_mountable) (file,
5074 * g_file_eject_mountable_finish:
5075 * @file: input #GFile
5076 * @result: a #GAsyncResult
5077 * @error: a #GError, or %NULL
5079 * Finishes an asynchronous eject operation started by
5080 * g_file_eject_mountable().
5082 * Returns: %TRUE if the @file was ejected successfully.
5085 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5089 g_file_eject_mountable_finish (GFile *file,
5090 GAsyncResult *result,
5095 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5096 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5098 if (g_async_result_legacy_propagate_error (result, error))
5100 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5101 return g_task_propagate_boolean (G_TASK (result), error);
5103 iface = G_FILE_GET_IFACE (file);
5104 return (* iface->eject_mountable_finish) (file, result, error);
5108 * g_file_eject_mountable_with_operation:
5109 * @file: input #GFile
5110 * @flags: flags affecting the operation
5111 * @mount_operation: (allow-none): a #GMountOperation,
5112 * or %NULL to avoid user interaction
5113 * @cancellable: (allow-none): optional #GCancellable object,
5115 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5116 * when the request is satisfied, or %NULL
5117 * @user_data: (closure): the data to pass to callback function
5119 * Starts an asynchronous eject on a mountable.
5120 * When this operation has completed, @callback will be called with
5121 * @user_user data, and the operation can be finalized with
5122 * g_file_eject_mountable_with_operation_finish().
5124 * If @cancellable is not %NULL, then the operation can be cancelled by
5125 * triggering the cancellable object from another thread. If the operation
5126 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5131 g_file_eject_mountable_with_operation (GFile *file,
5132 GMountUnmountFlags flags,
5133 GMountOperation *mount_operation,
5134 GCancellable *cancellable,
5135 GAsyncReadyCallback callback,
5140 g_return_if_fail (G_IS_FILE (file));
5142 iface = G_FILE_GET_IFACE (file);
5144 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5146 g_task_report_new_error (file, callback, user_data,
5147 g_file_eject_mountable_with_operation,
5148 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5149 _("Operation not supported"));
5153 if (iface->eject_mountable_with_operation != NULL)
5154 (* iface->eject_mountable_with_operation) (file,
5161 (* iface->eject_mountable) (file,
5169 * g_file_eject_mountable_with_operation_finish:
5170 * @file: input #GFile
5171 * @result: a #GAsyncResult
5172 * @error: a #GError, or %NULL
5174 * Finishes an asynchronous eject operation started by
5175 * g_file_eject_mountable_with_operation().
5177 * Returns: %TRUE if the @file was ejected successfully.
5183 g_file_eject_mountable_with_operation_finish (GFile *file,
5184 GAsyncResult *result,
5189 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5190 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5192 if (g_async_result_legacy_propagate_error (result, error))
5194 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5195 return g_task_propagate_boolean (G_TASK (result), error);
5197 iface = G_FILE_GET_IFACE (file);
5198 if (iface->eject_mountable_with_operation_finish != NULL)
5199 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5201 return (* iface->eject_mountable_finish) (file, result, error);
5205 * g_file_monitor_directory:
5206 * @file: input #GFile
5207 * @flags: a set of #GFileMonitorFlags
5208 * @cancellable: (allow-none): optional #GCancellable object,
5210 * @error: a #GError, or %NULL
5212 * Obtains a directory monitor for the given file.
5213 * This may fail if directory monitoring is not supported.
5215 * If @cancellable is not %NULL, then the operation can be cancelled by
5216 * triggering the cancellable object from another thread. If the operation
5217 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5219 * It does not make sense for @flags to contain
5220 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5221 * directories. It is not possible to monitor all the files in a
5222 * directory for changes made via hard links; if you want to do this then
5223 * you must register individual watches with g_file_monitor().
5225 * Virtual: monitor_dir
5226 * Returns: (transfer full): a #GFileMonitor for the given @file,
5227 * or %NULL on error.
5228 * Free the returned object with g_object_unref().
5231 g_file_monitor_directory (GFile *file,
5232 GFileMonitorFlags flags,
5233 GCancellable *cancellable,
5238 g_return_val_if_fail (G_IS_FILE (file), NULL);
5239 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5241 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5244 iface = G_FILE_GET_IFACE (file);
5246 if (iface->monitor_dir == NULL)
5248 g_set_error_literal (error, G_IO_ERROR,
5249 G_IO_ERROR_NOT_SUPPORTED,
5250 _("Operation not supported"));
5254 return (* iface->monitor_dir) (file, flags, cancellable, error);
5258 * g_file_monitor_file:
5259 * @file: input #GFile
5260 * @flags: a set of #GFileMonitorFlags
5261 * @cancellable: (allow-none): optional #GCancellable object,
5263 * @error: a #GError, or %NULL
5265 * Obtains a file monitor for the given file. If no file notification
5266 * mechanism exists, then regular polling of the file is used.
5268 * If @cancellable is not %NULL, then the operation can be cancelled by
5269 * triggering the cancellable object from another thread. If the operation
5270 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5272 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5273 * will also attempt to report changes made to the file via another
5274 * filename (ie, a hard link). Without this flag, you can only rely on
5275 * changes made through the filename contained in @file to be
5276 * reported. Using this flag may result in an increase in resource
5277 * usage, and may not have any effect depending on the #GFileMonitor
5278 * backend and/or filesystem type.
5280 * Returns: (transfer full): a #GFileMonitor for the given @file,
5281 * or %NULL on error.
5282 * Free the returned object with g_object_unref().
5285 g_file_monitor_file (GFile *file,
5286 GFileMonitorFlags flags,
5287 GCancellable *cancellable,
5291 GFileMonitor *monitor;
5293 g_return_val_if_fail (G_IS_FILE (file), NULL);
5295 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5298 iface = G_FILE_GET_IFACE (file);
5302 if (iface->monitor_file)
5303 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5305 /* Fallback to polling */
5306 if (monitor == NULL)
5307 monitor = _g_poll_file_monitor_new (file);
5314 * @file: input #GFile
5315 * @flags: a set of #GFileMonitorFlags
5316 * @cancellable: (allow-none): optional #GCancellable object,
5318 * @error: a #GError, or %NULL
5320 * Obtains a file or directory monitor for the given file,
5321 * depending on the type of the file.
5323 * If @cancellable is not %NULL, then the operation can be cancelled by
5324 * triggering the cancellable object from another thread. If the operation
5325 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5327 * Returns: (transfer full): a #GFileMonitor for the given @file,
5328 * or %NULL on error.
5329 * Free the returned object with g_object_unref().
5334 g_file_monitor (GFile *file,
5335 GFileMonitorFlags flags,
5336 GCancellable *cancellable,
5339 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5340 return g_file_monitor_directory (file,
5341 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5342 cancellable, error);
5344 return g_file_monitor_file (file, flags, cancellable, error);
5347 /********************************************
5348 * Default implementation of async ops *
5349 ********************************************/
5353 GFileQueryInfoFlags flags;
5354 } QueryInfoAsyncData;
5357 query_info_data_free (QueryInfoAsyncData *data)
5359 g_free (data->attributes);
5364 query_info_async_thread (GTask *task,
5367 GCancellable *cancellable)
5369 QueryInfoAsyncData *data = task_data;
5371 GError *error = NULL;
5373 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5375 g_task_return_pointer (task, info, g_object_unref);
5377 g_task_return_error (task, error);
5381 g_file_real_query_info_async (GFile *file,
5382 const char *attributes,
5383 GFileQueryInfoFlags flags,
5385 GCancellable *cancellable,
5386 GAsyncReadyCallback callback,
5390 QueryInfoAsyncData *data;
5392 data = g_new0 (QueryInfoAsyncData, 1);
5393 data->attributes = g_strdup (attributes);
5394 data->flags = flags;
5396 task = g_task_new (file, cancellable, callback, user_data);
5397 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5398 g_task_set_priority (task, io_priority);
5399 g_task_run_in_thread (task, query_info_async_thread);
5400 g_object_unref (task);
5404 g_file_real_query_info_finish (GFile *file,
5408 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5410 return g_task_propagate_pointer (G_TASK (res), error);
5414 query_filesystem_info_async_thread (GTask *task,
5417 GCancellable *cancellable)
5419 const char *attributes = task_data;
5421 GError *error = NULL;
5423 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5425 g_task_return_pointer (task, info, g_object_unref);
5427 g_task_return_error (task, error);
5431 g_file_real_query_filesystem_info_async (GFile *file,
5432 const char *attributes,
5434 GCancellable *cancellable,
5435 GAsyncReadyCallback callback,
5440 task = g_task_new (file, cancellable, callback, user_data);
5441 g_task_set_task_data (task, g_strdup (attributes), g_free);
5442 g_task_set_priority (task, io_priority);
5443 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5444 g_object_unref (task);
5448 g_file_real_query_filesystem_info_finish (GFile *file,
5452 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5454 return g_task_propagate_pointer (G_TASK (res), error);
5458 enumerate_children_async_thread (GTask *task,
5461 GCancellable *cancellable)
5463 QueryInfoAsyncData *data = task_data;
5464 GFileEnumerator *enumerator;
5465 GError *error = NULL;
5467 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5469 g_task_return_error (task, error);
5471 g_task_return_pointer (task, enumerator, g_object_unref);
5475 g_file_real_enumerate_children_async (GFile *file,
5476 const char *attributes,
5477 GFileQueryInfoFlags flags,
5479 GCancellable *cancellable,
5480 GAsyncReadyCallback callback,
5484 QueryInfoAsyncData *data;
5486 data = g_new0 (QueryInfoAsyncData, 1);
5487 data->attributes = g_strdup (attributes);
5488 data->flags = flags;
5490 task = g_task_new (file, cancellable, callback, user_data);
5491 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5492 g_task_set_priority (task, io_priority);
5493 g_task_run_in_thread (task, enumerate_children_async_thread);
5494 g_object_unref (task);
5497 static GFileEnumerator *
5498 g_file_real_enumerate_children_finish (GFile *file,
5502 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5504 return g_task_propagate_pointer (G_TASK (res), error);
5508 open_read_async_thread (GTask *task,
5511 GCancellable *cancellable)
5513 GFileInputStream *stream;
5514 GError *error = NULL;
5516 stream = g_file_read (G_FILE (object), cancellable, &error);
5518 g_task_return_pointer (task, stream, g_object_unref);
5520 g_task_return_error (task, error);
5524 g_file_real_read_async (GFile *file,
5526 GCancellable *cancellable,
5527 GAsyncReadyCallback callback,
5532 task = g_task_new (file, cancellable, callback, user_data);
5533 g_task_set_priority (task, io_priority);
5534 g_task_run_in_thread (task, open_read_async_thread);
5535 g_object_unref (task);
5538 static GFileInputStream *
5539 g_file_real_read_finish (GFile *file,
5543 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5545 return g_task_propagate_pointer (G_TASK (res), error);
5549 append_to_async_thread (GTask *task,
5550 gpointer source_object,
5552 GCancellable *cancellable)
5554 GFileCreateFlags *data = task_data;
5555 GFileOutputStream *stream;
5556 GError *error = NULL;
5558 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5560 g_task_return_pointer (task, stream, g_object_unref);
5562 g_task_return_error (task, error);
5566 g_file_real_append_to_async (GFile *file,
5567 GFileCreateFlags flags,
5569 GCancellable *cancellable,
5570 GAsyncReadyCallback callback,
5573 GFileCreateFlags *data;
5576 data = g_new0 (GFileCreateFlags, 1);
5579 task = g_task_new (file, cancellable, callback, user_data);
5580 g_task_set_task_data (task, data, g_free);
5581 g_task_set_priority (task, io_priority);
5583 g_task_run_in_thread (task, append_to_async_thread);
5584 g_object_unref (task);
5587 static GFileOutputStream *
5588 g_file_real_append_to_finish (GFile *file,
5592 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5594 return g_task_propagate_pointer (G_TASK (res), error);
5598 create_async_thread (GTask *task,
5599 gpointer source_object,
5601 GCancellable *cancellable)
5603 GFileCreateFlags *data = task_data;
5604 GFileOutputStream *stream;
5605 GError *error = NULL;
5607 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5609 g_task_return_pointer (task, stream, g_object_unref);
5611 g_task_return_error (task, error);
5615 g_file_real_create_async (GFile *file,
5616 GFileCreateFlags flags,
5618 GCancellable *cancellable,
5619 GAsyncReadyCallback callback,
5622 GFileCreateFlags *data;
5625 data = g_new0 (GFileCreateFlags, 1);
5628 task = g_task_new (file, cancellable, callback, user_data);
5629 g_task_set_task_data (task, data, g_free);
5630 g_task_set_priority (task, io_priority);
5632 g_task_run_in_thread (task, create_async_thread);
5633 g_object_unref (task);
5636 static GFileOutputStream *
5637 g_file_real_create_finish (GFile *file,
5641 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5643 return g_task_propagate_pointer (G_TASK (res), error);
5647 GFileOutputStream *stream;
5649 gboolean make_backup;
5650 GFileCreateFlags flags;
5654 replace_async_data_free (ReplaceAsyncData *data)
5657 g_object_unref (data->stream);
5658 g_free (data->etag);
5663 replace_async_thread (GTask *task,
5664 gpointer source_object,
5666 GCancellable *cancellable)
5668 GFileOutputStream *stream;
5669 ReplaceAsyncData *data = task_data;
5670 GError *error = NULL;
5672 stream = g_file_replace (G_FILE (source_object),
5680 g_task_return_pointer (task, stream, g_object_unref);
5682 g_task_return_error (task, error);
5686 g_file_real_replace_async (GFile *file,
5688 gboolean make_backup,
5689 GFileCreateFlags flags,
5691 GCancellable *cancellable,
5692 GAsyncReadyCallback callback,
5696 ReplaceAsyncData *data;
5698 data = g_new0 (ReplaceAsyncData, 1);
5699 data->etag = g_strdup (etag);
5700 data->make_backup = make_backup;
5701 data->flags = flags;
5703 task = g_task_new (file, cancellable, callback, user_data);
5704 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5705 g_task_set_priority (task, io_priority);
5707 g_task_run_in_thread (task, replace_async_thread);
5708 g_object_unref (task);
5711 static GFileOutputStream *
5712 g_file_real_replace_finish (GFile *file,
5716 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5718 return g_task_propagate_pointer (G_TASK (res), error);
5722 delete_async_thread (GTask *task,
5725 GCancellable *cancellable)
5727 GError *error = NULL;
5729 if (g_file_delete (G_FILE (object), cancellable, &error))
5730 g_task_return_boolean (task, TRUE);
5732 g_task_return_error (task, error);
5736 g_file_real_delete_async (GFile *file,
5738 GCancellable *cancellable,
5739 GAsyncReadyCallback callback,
5744 task = g_task_new (file, cancellable, callback, user_data);
5745 g_task_set_priority (task, io_priority);
5746 g_task_run_in_thread (task, delete_async_thread);
5747 g_object_unref (task);
5751 g_file_real_delete_finish (GFile *file,
5755 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5757 return g_task_propagate_boolean (G_TASK (res), error);
5761 trash_async_thread (GTask *task,
5764 GCancellable *cancellable)
5766 GError *error = NULL;
5768 if (g_file_trash (G_FILE (object), cancellable, &error))
5769 g_task_return_boolean (task, TRUE);
5771 g_task_return_error (task, error);
5775 g_file_real_trash_async (GFile *file,
5777 GCancellable *cancellable,
5778 GAsyncReadyCallback callback,
5783 task = g_task_new (file, cancellable, callback, user_data);
5784 g_task_set_priority (task, io_priority);
5785 g_task_run_in_thread (task, trash_async_thread);
5786 g_object_unref (task);
5790 g_file_real_trash_finish (GFile *file,
5794 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5796 return g_task_propagate_boolean (G_TASK (res), error);
5800 make_directory_async_thread (GTask *task,
5803 GCancellable *cancellable)
5805 GError *error = NULL;
5807 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5808 g_task_return_boolean (task, TRUE);
5810 g_task_return_error (task, error);
5814 g_file_real_make_directory_async (GFile *file,
5816 GCancellable *cancellable,
5817 GAsyncReadyCallback callback,
5822 task = g_task_new (file, cancellable, callback, user_data);
5823 g_task_set_priority (task, io_priority);
5824 g_task_run_in_thread (task, make_directory_async_thread);
5825 g_object_unref (task);
5829 g_file_real_make_directory_finish (GFile *file,
5833 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5835 return g_task_propagate_boolean (G_TASK (res), error);
5839 open_readwrite_async_thread (GTask *task,
5842 GCancellable *cancellable)
5844 GFileIOStream *stream;
5845 GError *error = NULL;
5847 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5850 g_task_return_error (task, error);
5852 g_task_return_pointer (task, stream, g_object_unref);
5856 g_file_real_open_readwrite_async (GFile *file,
5858 GCancellable *cancellable,
5859 GAsyncReadyCallback callback,
5864 task = g_task_new (file, cancellable, callback, user_data);
5865 g_task_set_priority (task, io_priority);
5867 g_task_run_in_thread (task, open_readwrite_async_thread);
5868 g_object_unref (task);
5871 static GFileIOStream *
5872 g_file_real_open_readwrite_finish (GFile *file,
5876 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5878 return g_task_propagate_pointer (G_TASK (res), error);
5882 create_readwrite_async_thread (GTask *task,
5885 GCancellable *cancellable)
5887 GFileCreateFlags *data = task_data;
5888 GFileIOStream *stream;
5889 GError *error = NULL;
5891 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5894 g_task_return_error (task, error);
5896 g_task_return_pointer (task, stream, g_object_unref);
5900 g_file_real_create_readwrite_async (GFile *file,
5901 GFileCreateFlags flags,
5903 GCancellable *cancellable,
5904 GAsyncReadyCallback callback,
5907 GFileCreateFlags *data;
5910 data = g_new0 (GFileCreateFlags, 1);
5913 task = g_task_new (file, cancellable, callback, user_data);
5914 g_task_set_task_data (task, data, g_free);
5915 g_task_set_priority (task, io_priority);
5917 g_task_run_in_thread (task, create_readwrite_async_thread);
5918 g_object_unref (task);
5921 static GFileIOStream *
5922 g_file_real_create_readwrite_finish (GFile *file,
5926 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5928 return g_task_propagate_pointer (G_TASK (res), error);
5933 gboolean make_backup;
5934 GFileCreateFlags flags;
5935 } ReplaceRWAsyncData;
5938 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5940 g_free (data->etag);
5945 replace_readwrite_async_thread (GTask *task,
5948 GCancellable *cancellable)
5950 GFileIOStream *stream;
5951 GError *error = NULL;
5952 ReplaceRWAsyncData *data = task_data;
5954 stream = g_file_replace_readwrite (G_FILE (object),
5962 g_task_return_error (task, error);
5964 g_task_return_pointer (task, stream, g_object_unref);
5968 g_file_real_replace_readwrite_async (GFile *file,
5970 gboolean make_backup,
5971 GFileCreateFlags flags,
5973 GCancellable *cancellable,
5974 GAsyncReadyCallback callback,
5978 ReplaceRWAsyncData *data;
5980 data = g_new0 (ReplaceRWAsyncData, 1);
5981 data->etag = g_strdup (etag);
5982 data->make_backup = make_backup;
5983 data->flags = flags;
5985 task = g_task_new (file, cancellable, callback, user_data);
5986 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5987 g_task_set_priority (task, io_priority);
5989 g_task_run_in_thread (task, replace_readwrite_async_thread);
5990 g_object_unref (task);
5993 static GFileIOStream *
5994 g_file_real_replace_readwrite_finish (GFile *file,
5998 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6000 return g_task_propagate_pointer (G_TASK (res), error);
6004 set_display_name_async_thread (GTask *task,
6007 GCancellable *cancellable)
6009 GError *error = NULL;
6010 char *name = task_data;
6013 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6016 g_task_return_error (task, error);
6018 g_task_return_pointer (task, file, g_object_unref);
6022 g_file_real_set_display_name_async (GFile *file,
6023 const char *display_name,
6025 GCancellable *cancellable,
6026 GAsyncReadyCallback callback,
6031 task = g_task_new (file, cancellable, callback, user_data);
6032 g_task_set_task_data (task, g_strdup (display_name), g_free);
6033 g_task_set_priority (task, io_priority);
6035 g_task_run_in_thread (task, set_display_name_async_thread);
6036 g_object_unref (task);
6040 g_file_real_set_display_name_finish (GFile *file,
6044 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6046 return g_task_propagate_pointer (G_TASK (res), error);
6050 GFileQueryInfoFlags flags;
6057 set_info_data_free (SetInfoAsyncData *data)
6060 g_object_unref (data->info);
6062 g_error_free (data->error);
6067 set_info_async_thread (GTask *task,
6070 GCancellable *cancellable)
6072 SetInfoAsyncData *data = task_data;
6075 data->res = g_file_set_attributes_from_info (G_FILE (object),
6083 g_file_real_set_attributes_async (GFile *file,
6085 GFileQueryInfoFlags flags,
6087 GCancellable *cancellable,
6088 GAsyncReadyCallback callback,
6092 SetInfoAsyncData *data;
6094 data = g_new0 (SetInfoAsyncData, 1);
6095 data->info = g_file_info_dup (info);
6096 data->flags = flags;
6098 task = g_task_new (file, cancellable, callback, user_data);
6099 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6100 g_task_set_priority (task, io_priority);
6102 g_task_run_in_thread (task, set_info_async_thread);
6103 g_object_unref (task);
6107 g_file_real_set_attributes_finish (GFile *file,
6112 SetInfoAsyncData *data;
6114 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6116 data = g_task_get_task_data (G_TASK (res));
6119 *info = g_object_ref (data->info);
6121 if (error != NULL && data->error)
6122 *error = g_error_copy (data->error);
6128 find_enclosing_mount_async_thread (GTask *task,
6131 GCancellable *cancellable)
6133 GError *error = NULL;
6136 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6139 g_task_return_error (task, error);
6141 g_task_return_pointer (task, mount, g_object_unref);
6145 g_file_real_find_enclosing_mount_async (GFile *file,
6147 GCancellable *cancellable,
6148 GAsyncReadyCallback callback,
6153 task = g_task_new (file, cancellable, callback, user_data);
6154 g_task_set_priority (task, io_priority);
6156 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6157 g_object_unref (task);
6161 g_file_real_find_enclosing_mount_finish (GFile *file,
6165 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6167 return g_task_propagate_pointer (G_TASK (res), error);
6174 GFileCopyFlags flags;
6175 GFileProgressCallback progress_cb;
6176 gpointer progress_cb_data;
6180 copy_async_data_free (CopyAsyncData *data)
6182 g_object_unref (data->source);
6183 g_object_unref (data->destination);
6184 g_slice_free (CopyAsyncData, data);
6188 CopyAsyncData *data;
6189 goffset current_num_bytes;
6190 goffset total_num_bytes;
6194 copy_async_progress_in_main (gpointer user_data)
6196 ProgressData *progress = user_data;
6197 CopyAsyncData *data = progress->data;
6199 data->progress_cb (progress->current_num_bytes,
6200 progress->total_num_bytes,
6201 data->progress_cb_data);
6207 copy_async_progress_callback (goffset current_num_bytes,
6208 goffset total_num_bytes,
6211 GTask *task = user_data;
6212 CopyAsyncData *data = g_task_get_task_data (task);
6213 ProgressData *progress;
6215 progress = g_new (ProgressData, 1);
6216 progress->data = data;
6217 progress->current_num_bytes = current_num_bytes;
6218 progress->total_num_bytes = total_num_bytes;
6220 g_main_context_invoke_full (g_task_get_context (task),
6221 g_task_get_priority (task),
6222 copy_async_progress_in_main,
6228 copy_async_thread (GTask *task,
6231 GCancellable *cancellable)
6233 CopyAsyncData *data = task_data;
6235 GError *error = NULL;
6237 result = g_file_copy (data->source,
6241 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6245 g_task_return_boolean (task, TRUE);
6247 g_task_return_error (task, error);
6251 g_file_real_copy_async (GFile *source,
6253 GFileCopyFlags flags,
6255 GCancellable *cancellable,
6256 GFileProgressCallback progress_callback,
6257 gpointer progress_callback_data,
6258 GAsyncReadyCallback callback,
6262 CopyAsyncData *data;
6264 data = g_slice_new (CopyAsyncData);
6265 data->source = g_object_ref (source);
6266 data->destination = g_object_ref (destination);
6267 data->flags = flags;
6268 data->progress_cb = progress_callback;
6269 data->progress_cb_data = progress_callback_data;
6271 task = g_task_new (source, cancellable, callback, user_data);
6272 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6273 g_task_set_priority (task, io_priority);
6274 g_task_run_in_thread (task, copy_async_thread);
6275 g_object_unref (task);
6279 g_file_real_copy_finish (GFile *file,
6283 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6285 return g_task_propagate_boolean (G_TASK (res), error);
6289 /********************************************
6290 * Default VFS operations *
6291 ********************************************/
6294 * g_file_new_for_path:
6295 * @path: a string containing a relative or absolute path.
6296 * The string must be encoded in the glib filename encoding.
6298 * Constructs a #GFile for a given path. This operation never
6299 * fails, but the returned object might not support any I/O
6300 * operation if @path is malformed.
6302 * Returns: (transfer full): a new #GFile for the given @path.
6303 * Free the returned object with g_object_unref().
6306 g_file_new_for_path (const char *path)
6308 g_return_val_if_fail (path != NULL, NULL);
6310 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6314 * g_file_new_for_uri:
6315 * @uri: a UTF-8 string containing a URI
6317 * Constructs a #GFile for a given URI. This operation never
6318 * fails, but the returned object might not support any I/O
6319 * operation if @uri is malformed or if the uri type is
6322 * Returns: (transfer full): a new #GFile for the given @uri.
6323 * Free the returned object with g_object_unref().
6326 g_file_new_for_uri (const char *uri)
6328 g_return_val_if_fail (uri != NULL, NULL);
6330 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6335 * @tmpl: (type filename) (allow-none): Template for the file
6336 * name, as in g_file_open_tmp(), or %NULL for a default template
6337 * @iostream: (out): on return, a #GFileIOStream for the created file
6338 * @error: a #GError, or %NULL
6340 * Opens a file in the preferred directory for temporary files (as
6341 * returned by g_get_tmp_dir()) and returns a #GFile and
6342 * #GFileIOStream pointing to it.
6344 * @tmpl should be a string in the GLib file name encoding
6345 * containing a sequence of six 'X' characters, and containing no
6346 * directory components. If it is %NULL, a default template is used.
6348 * Unlike the other #GFile constructors, this will return %NULL if
6349 * a temporary file could not be created.
6351 * Returns: (transfer full): a new #GFile.
6352 * Free the returned object with g_object_unref().
6357 g_file_new_tmp (const char *tmpl,
6358 GFileIOStream **iostream,
6364 GFileOutputStream *output;
6366 g_return_val_if_fail (iostream != NULL, NULL);
6368 fd = g_file_open_tmp (tmpl, &path, error);
6372 file = g_file_new_for_path (path);
6374 output = _g_local_file_output_stream_new (fd);
6375 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6377 g_object_unref (output);
6384 * g_file_parse_name:
6385 * @parse_name: a file name or path to be parsed
6387 * Constructs a #GFile with the given @parse_name (i.e. something
6388 * given by g_file_get_parse_name()). This operation never fails,
6389 * but the returned object might not support any I/O operation if
6390 * the @parse_name cannot be parsed.
6392 * Returns: (transfer full): a new #GFile.
6395 g_file_parse_name (const char *parse_name)
6397 g_return_val_if_fail (parse_name != NULL, NULL);
6399 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6403 is_valid_scheme_character (char c)
6405 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6408 /* Following RFC 2396, valid schemes are built like:
6409 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6412 has_valid_scheme (const char *uri)
6418 if (!g_ascii_isalpha (*p))
6423 } while (is_valid_scheme_character (*p));
6429 new_for_cmdline_arg (const gchar *arg,
6435 if (g_path_is_absolute (arg))
6436 return g_file_new_for_path (arg);
6438 if (has_valid_scheme (arg))
6439 return g_file_new_for_uri (arg);
6445 current_dir = g_get_current_dir ();
6446 filename = g_build_filename (current_dir, arg, NULL);
6447 g_free (current_dir);
6450 filename = g_build_filename (cwd, arg, NULL);
6452 file = g_file_new_for_path (filename);
6459 * g_file_new_for_commandline_arg:
6460 * @arg: a command line string
6462 * Creates a #GFile with the given argument from the command line.
6463 * The value of @arg can be either a URI, an absolute path or a
6464 * relative path resolved relative to the current working directory.
6465 * This operation never fails, but the returned object might not
6466 * support any I/O operation if @arg points to a malformed path.
6468 * Returns: (transfer full): a new #GFile.
6469 * Free the returned object with g_object_unref().
6472 g_file_new_for_commandline_arg (const char *arg)
6474 g_return_val_if_fail (arg != NULL, NULL);
6476 return new_for_cmdline_arg (arg, NULL);
6480 * g_file_new_for_commandline_arg_and_cwd:
6481 * @arg: a command line string
6482 * @cwd: the current working directory of the commandline
6484 * Creates a #GFile with the given argument from the command line.
6486 * This function is similar to g_file_new_for_commandline_arg() except
6487 * that it allows for passing the current working directory as an
6488 * argument instead of using the current working directory of the
6491 * This is useful if the commandline argument was given in a context
6492 * other than the invocation of the current process.
6494 * See also g_application_command_line_create_file_for_arg().
6496 * Returns: (transfer full): a new #GFile
6501 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6504 g_return_val_if_fail (arg != NULL, NULL);
6505 g_return_val_if_fail (cwd != NULL, NULL);
6507 return new_for_cmdline_arg (arg, cwd);
6511 * g_file_mount_enclosing_volume:
6512 * @location: input #GFile
6513 * @flags: flags affecting the operation
6514 * @mount_operation: (allow-none): a #GMountOperation
6515 * or %NULL to avoid user interaction
6516 * @cancellable: (allow-none): optional #GCancellable object,
6518 * @callback: (allow-none): a #GAsyncReadyCallback to call
6519 * when the request is satisfied, or %NULL
6520 * @user_data: the data to pass to callback function
6522 * Starts a @mount_operation, mounting the volume that contains
6523 * the file @location.
6525 * When this operation has completed, @callback will be called with
6526 * @user_user data, and the operation can be finalized with
6527 * g_file_mount_enclosing_volume_finish().
6529 * If @cancellable is not %NULL, then the operation can be cancelled by
6530 * triggering the cancellable object from another thread. If the operation
6531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6534 g_file_mount_enclosing_volume (GFile *location,
6535 GMountMountFlags flags,
6536 GMountOperation *mount_operation,
6537 GCancellable *cancellable,
6538 GAsyncReadyCallback callback,
6543 g_return_if_fail (G_IS_FILE (location));
6545 iface = G_FILE_GET_IFACE (location);
6547 if (iface->mount_enclosing_volume == NULL)
6549 g_task_report_new_error (location, callback, user_data,
6550 g_file_mount_enclosing_volume,
6551 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6552 _("volume doesn't implement mount"));
6556 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6561 * g_file_mount_enclosing_volume_finish:
6562 * @location: input #GFile
6563 * @result: a #GAsyncResult
6564 * @error: a #GError, or %NULL
6566 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6568 * Returns: %TRUE if successful. If an error has occurred,
6569 * this function will return %FALSE and set @error
6570 * appropriately if present.
6573 g_file_mount_enclosing_volume_finish (GFile *location,
6574 GAsyncResult *result,
6579 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6580 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6582 if (g_async_result_legacy_propagate_error (result, error))
6584 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6585 return g_task_propagate_boolean (G_TASK (result), error);
6587 iface = G_FILE_GET_IFACE (location);
6589 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6592 /********************************************
6593 * Utility functions *
6594 ********************************************/
6597 * g_file_query_default_handler:
6598 * @file: a #GFile to open
6599 * @cancellable: optional #GCancellable object, %NULL to ignore
6600 * @error: a #GError, or %NULL
6602 * Returns the #GAppInfo that is registered as the default
6603 * application to handle the file specified by @file.
6605 * If @cancellable is not %NULL, then the operation can be cancelled by
6606 * triggering the cancellable object from another thread. If the operation
6607 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6609 * Returns: (transfer full): a #GAppInfo if the handle was found,
6610 * %NULL if there were errors.
6611 * When you are done with it, release it with g_object_unref()
6614 g_file_query_default_handler (GFile *file,
6615 GCancellable *cancellable,
6619 const char *content_type;
6624 uri_scheme = g_file_get_uri_scheme (file);
6625 if (uri_scheme && uri_scheme[0] != '\0')
6627 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6628 g_free (uri_scheme);
6630 if (appinfo != NULL)
6634 info = g_file_query_info (file,
6635 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6644 content_type = g_file_info_get_content_type (info);
6647 /* Don't use is_native(), as we want to support fuse paths if available */
6648 path = g_file_get_path (file);
6649 appinfo = g_app_info_get_default_for_type (content_type,
6654 g_object_unref (info);
6656 if (appinfo != NULL)
6659 g_set_error_literal (error, G_IO_ERROR,
6660 G_IO_ERROR_NOT_SUPPORTED,
6661 _("No application is registered as handling this file"));
6665 #define GET_CONTENT_BLOCK_SIZE 8192
6668 * g_file_load_contents:
6669 * @file: input #GFile
6670 * @cancellable: optional #GCancellable object, %NULL to ignore
6671 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6672 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6673 * or %NULL if the length is not needed
6674 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6675 * or %NULL if the entity tag is not needed
6676 * @error: a #GError, or %NULL
6678 * Loads the content of the file into memory. The data is always
6679 * zero-terminated, but this is not included in the resultant @length.
6680 * The returned @content should be freed with g_free() when no longer
6683 * If @cancellable is not %NULL, then the operation can be cancelled by
6684 * triggering the cancellable object from another thread. If the operation
6685 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6687 * Returns: %TRUE if the @file's contents were successfully loaded.
6688 * %FALSE if there were errors.
6691 g_file_load_contents (GFile *file,
6692 GCancellable *cancellable,
6698 GFileInputStream *in;
6699 GByteArray *content;
6704 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6705 g_return_val_if_fail (contents != NULL, FALSE);
6707 in = g_file_read (file, cancellable, error);
6711 content = g_byte_array_new ();
6714 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6715 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6716 content->data + pos,
6717 GET_CONTENT_BLOCK_SIZE,
6718 cancellable, error)) > 0)
6721 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6728 info = g_file_input_stream_query_info (in,
6729 G_FILE_ATTRIBUTE_ETAG_VALUE,
6734 *etag_out = g_strdup (g_file_info_get_etag (info));
6735 g_object_unref (info);
6739 /* Ignore errors on close */
6740 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6741 g_object_unref (in);
6745 /* error is set already */
6746 g_byte_array_free (content, TRUE);
6753 /* Zero terminate (we got an extra byte allocated for this */
6754 content->data[pos] = 0;
6756 *contents = (char *)g_byte_array_free (content, FALSE);
6763 GFileReadMoreCallback read_more_callback;
6764 GByteArray *content;
6771 load_contents_data_free (LoadContentsData *data)
6774 g_byte_array_free (data->content, TRUE);
6775 g_free (data->etag);
6780 load_contents_close_callback (GObject *obj,
6781 GAsyncResult *close_res,
6784 GInputStream *stream = G_INPUT_STREAM (obj);
6785 LoadContentsData *data = user_data;
6787 /* Ignore errors here, we're only reading anyway */
6788 g_input_stream_close_finish (stream, close_res, NULL);
6789 g_object_unref (stream);
6791 g_task_return_boolean (data->task, TRUE);
6792 g_object_unref (data->task);
6796 load_contents_fstat_callback (GObject *obj,
6797 GAsyncResult *stat_res,
6800 GInputStream *stream = G_INPUT_STREAM (obj);
6801 LoadContentsData *data = user_data;
6804 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6808 data->etag = g_strdup (g_file_info_get_etag (info));
6809 g_object_unref (info);
6812 g_input_stream_close_async (stream, 0,
6813 g_task_get_cancellable (data->task),
6814 load_contents_close_callback, data);
6818 load_contents_read_callback (GObject *obj,
6819 GAsyncResult *read_res,
6822 GInputStream *stream = G_INPUT_STREAM (obj);
6823 LoadContentsData *data = user_data;
6824 GError *error = NULL;
6827 read_size = g_input_stream_read_finish (stream, read_res, &error);
6831 g_task_return_error (data->task, error);
6832 g_object_unref (data->task);
6834 /* Close the file ignoring any error */
6835 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6836 g_object_unref (stream);
6838 else if (read_size == 0)
6840 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6841 G_FILE_ATTRIBUTE_ETAG_VALUE,
6843 g_task_get_cancellable (data->task),
6844 load_contents_fstat_callback,
6847 else if (read_size > 0)
6849 data->pos += read_size;
6851 g_byte_array_set_size (data->content,
6852 data->pos + GET_CONTENT_BLOCK_SIZE);
6855 if (data->read_more_callback &&
6856 !data->read_more_callback ((char *)data->content->data, data->pos,
6857 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6858 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6859 G_FILE_ATTRIBUTE_ETAG_VALUE,
6861 g_task_get_cancellable (data->task),
6862 load_contents_fstat_callback,
6865 g_input_stream_read_async (stream,
6866 data->content->data + data->pos,
6867 GET_CONTENT_BLOCK_SIZE,
6869 g_task_get_cancellable (data->task),
6870 load_contents_read_callback,
6876 load_contents_open_callback (GObject *obj,
6877 GAsyncResult *open_res,
6880 GFile *file = G_FILE (obj);
6881 GFileInputStream *stream;
6882 LoadContentsData *data = user_data;
6883 GError *error = NULL;
6885 stream = g_file_read_finish (file, open_res, &error);
6889 g_byte_array_set_size (data->content,
6890 data->pos + GET_CONTENT_BLOCK_SIZE);
6891 g_input_stream_read_async (G_INPUT_STREAM (stream),
6892 data->content->data + data->pos,
6893 GET_CONTENT_BLOCK_SIZE,
6895 g_task_get_cancellable (data->task),
6896 load_contents_read_callback,
6901 g_task_return_error (data->task, error);
6902 g_object_unref (data->task);
6907 * g_file_load_partial_contents_async: (skip)
6908 * @file: input #GFile
6909 * @cancellable: optional #GCancellable object, %NULL to ignore
6910 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6911 * and to specify whether further data should be read
6912 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6913 * @user_data: the data to pass to the callback functions
6915 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6916 * be used to stop reading from the file when appropriate, else this
6917 * function will behave exactly as g_file_load_contents_async(). This
6918 * operation can be finished by g_file_load_partial_contents_finish().
6920 * Users of this function should be aware that @user_data is passed to
6921 * both the @read_more_callback and the @callback.
6923 * If @cancellable is not %NULL, then the operation can be cancelled by
6924 * triggering the cancellable object from another thread. If the operation
6925 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6928 g_file_load_partial_contents_async (GFile *file,
6929 GCancellable *cancellable,
6930 GFileReadMoreCallback read_more_callback,
6931 GAsyncReadyCallback callback,
6934 LoadContentsData *data;
6936 g_return_if_fail (G_IS_FILE (file));
6938 data = g_new0 (LoadContentsData, 1);
6939 data->read_more_callback = read_more_callback;
6940 data->content = g_byte_array_new ();
6942 data->task = g_task_new (file, cancellable, callback, user_data);
6943 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6945 g_file_read_async (file,
6947 g_task_get_cancellable (data->task),
6948 load_contents_open_callback,
6953 * g_file_load_partial_contents_finish:
6954 * @file: input #GFile
6955 * @res: a #GAsyncResult
6956 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6957 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6958 * or %NULL if the length is not needed
6959 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6960 * or %NULL if the entity tag is not needed
6961 * @error: a #GError, or %NULL
6963 * Finishes an asynchronous partial load operation that was started
6964 * with g_file_load_partial_contents_async(). The data is always
6965 * zero-terminated, but this is not included in the resultant @length.
6966 * The returned @content should be freed with g_free() when no longer
6969 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6970 * present, it will be set appropriately.
6973 g_file_load_partial_contents_finish (GFile *file,
6981 LoadContentsData *data;
6983 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6984 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6985 g_return_val_if_fail (contents != NULL, FALSE);
6987 task = G_TASK (res);
6989 if (!g_task_propagate_boolean (task, error))
6996 data = g_task_get_task_data (task);
6999 *length = data->pos;
7003 *etag_out = data->etag;
7007 /* Zero terminate */
7008 g_byte_array_set_size (data->content, data->pos + 1);
7009 data->content->data[data->pos] = 0;
7011 *contents = (char *)g_byte_array_free (data->content, FALSE);
7012 data->content = NULL;
7018 * g_file_load_contents_async:
7019 * @file: input #GFile
7020 * @cancellable: optional #GCancellable object, %NULL to ignore
7021 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7022 * @user_data: the data to pass to callback function
7024 * Starts an asynchronous load of the @file's contents.
7026 * For more details, see g_file_load_contents() which is
7027 * the synchronous version of this call.
7029 * When the load operation has completed, @callback will be called
7030 * with @user data. To finish the operation, call
7031 * g_file_load_contents_finish() with the #GAsyncResult returned by
7034 * If @cancellable is not %NULL, then the operation can be cancelled by
7035 * triggering the cancellable object from another thread. If the operation
7036 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7039 g_file_load_contents_async (GFile *file,
7040 GCancellable *cancellable,
7041 GAsyncReadyCallback callback,
7044 g_file_load_partial_contents_async (file,
7047 callback, user_data);
7051 * g_file_load_contents_finish:
7052 * @file: input #GFile
7053 * @res: a #GAsyncResult
7054 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7055 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7056 * or %NULL if the length is not needed
7057 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7058 * or %NULL if the entity tag is not needed
7059 * @error: a #GError, or %NULL
7061 * Finishes an asynchronous load of the @file's contents.
7062 * The contents are placed in @contents, and @length is set to the
7063 * size of the @contents string. The @content should be freed with
7064 * g_free() when no longer needed. If @etag_out is present, it will be
7065 * set to the new entity tag for the @file.
7067 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7068 * present, it will be set appropriately.
7071 g_file_load_contents_finish (GFile *file,
7078 return g_file_load_partial_contents_finish (file,
7087 * g_file_replace_contents:
7088 * @file: input #GFile
7089 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7090 * @length: the length of @contents in bytes
7091 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7092 * for the document, or %NULL
7093 * @make_backup: %TRUE if a backup should be created
7094 * @flags: a set of #GFileCreateFlags
7095 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7096 * for the document. This should be freed with g_free() when no longer
7098 * @cancellable: optional #GCancellable object, %NULL to ignore
7099 * @error: a #GError, or %NULL
7101 * Replaces the contents of @file with @contents of @length bytes.
7103 * If @etag is specified (not %NULL), any existing file must have that etag,
7104 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7106 * If @make_backup is %TRUE, this function will attempt to make a backup
7109 * If @cancellable is not %NULL, then the operation can be cancelled by
7110 * triggering the cancellable object from another thread. If the operation
7111 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7113 * The returned @new_etag can be used to verify that the file hasn't
7114 * changed the next time it is saved over.
7116 * Returns: %TRUE if successful. If an error has occurred, this function
7117 * will return %FALSE and set @error appropriately if present.
7120 g_file_replace_contents (GFile *file,
7121 const char *contents,
7124 gboolean make_backup,
7125 GFileCreateFlags flags,
7127 GCancellable *cancellable,
7130 GFileOutputStream *out;
7131 gsize pos, remainder;
7135 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7136 g_return_val_if_fail (contents != NULL, FALSE);
7138 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7144 while (remainder > 0 &&
7145 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7147 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7155 if (remainder > 0 && res < 0)
7157 /* Ignore errors on close */
7158 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7159 g_object_unref (out);
7161 /* error is set already */
7165 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7168 *new_etag = g_file_output_stream_get_etag (out);
7170 g_object_unref (out);
7177 const char *content;
7182 } ReplaceContentsData;
7185 replace_contents_data_free (ReplaceContentsData *data)
7187 g_free (data->etag);
7192 replace_contents_close_callback (GObject *obj,
7193 GAsyncResult *close_res,
7196 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7197 ReplaceContentsData *data = user_data;
7199 /* Ignore errors here, we're only reading anyway */
7200 g_output_stream_close_finish (stream, close_res, NULL);
7201 g_object_unref (stream);
7205 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7206 g_task_return_boolean (data->task, TRUE);
7208 g_object_unref (data->task);
7212 replace_contents_write_callback (GObject *obj,
7213 GAsyncResult *read_res,
7216 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7217 ReplaceContentsData *data = user_data;
7218 GError *error = NULL;
7221 write_size = g_output_stream_write_finish (stream, read_res, &error);
7223 if (write_size <= 0)
7225 /* Error or EOF, close the file */
7228 data->failed = TRUE;
7229 g_task_return_error (data->task, error);
7231 g_output_stream_close_async (stream, 0,
7232 g_task_get_cancellable (data->task),
7233 replace_contents_close_callback, data);
7235 else if (write_size > 0)
7237 data->pos += write_size;
7239 if (data->pos >= data->length)
7240 g_output_stream_close_async (stream, 0,
7241 g_task_get_cancellable (data->task),
7242 replace_contents_close_callback, data);
7244 g_output_stream_write_async (stream,
7245 data->content + data->pos,
7246 data->length - data->pos,
7248 g_task_get_cancellable (data->task),
7249 replace_contents_write_callback,
7255 replace_contents_open_callback (GObject *obj,
7256 GAsyncResult *open_res,
7259 GFile *file = G_FILE (obj);
7260 GFileOutputStream *stream;
7261 ReplaceContentsData *data = user_data;
7262 GError *error = NULL;
7264 stream = g_file_replace_finish (file, open_res, &error);
7268 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7269 data->content + data->pos,
7270 data->length - data->pos,
7272 g_task_get_cancellable (data->task),
7273 replace_contents_write_callback,
7278 g_task_return_error (data->task, error);
7279 g_object_unref (data->task);
7284 * g_file_replace_contents_async:
7285 * @file: input #GFile
7286 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7287 * @length: the length of @contents in bytes
7288 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7289 * @make_backup: %TRUE if a backup should be created
7290 * @flags: a set of #GFileCreateFlags
7291 * @cancellable: optional #GCancellable object, %NULL to ignore
7292 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7293 * @user_data: the data to pass to callback function
7295 * Starts an asynchronous replacement of @file with the given
7296 * @contents of @length bytes. @etag will replace the document's
7297 * current entity tag.
7299 * When this operation has completed, @callback will be called with
7300 * @user_user data, and the operation can be finalized with
7301 * g_file_replace_contents_finish().
7303 * If @cancellable is not %NULL, then the operation can be cancelled by
7304 * triggering the cancellable object from another thread. If the operation
7305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7307 * If @make_backup is %TRUE, this function will attempt to
7308 * make a backup of @file.
7311 g_file_replace_contents_async (GFile *file,
7312 const char *contents,
7315 gboolean make_backup,
7316 GFileCreateFlags flags,
7317 GCancellable *cancellable,
7318 GAsyncReadyCallback callback,
7321 ReplaceContentsData *data;
7323 g_return_if_fail (G_IS_FILE (file));
7324 g_return_if_fail (contents != NULL);
7326 data = g_new0 (ReplaceContentsData, 1);
7328 data->content = contents;
7329 data->length = length;
7331 data->task = g_task_new (file, cancellable, callback, user_data);
7332 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7334 g_file_replace_async (file,
7339 g_task_get_cancellable (data->task),
7340 replace_contents_open_callback,
7345 * g_file_replace_contents_finish:
7346 * @file: input #GFile
7347 * @res: a #GAsyncResult
7348 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7349 * for the document. This should be freed with g_free() when it is no
7350 * longer needed, or %NULL
7351 * @error: a #GError, or %NULL
7353 * Finishes an asynchronous replace of the given @file. See
7354 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7355 * tag for the document, if present.
7357 * Returns: %TRUE on success, %FALSE on failure.
7360 g_file_replace_contents_finish (GFile *file,
7366 ReplaceContentsData *data;
7368 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7369 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7371 task = G_TASK (res);
7373 if (!g_task_propagate_boolean (task, error))
7376 data = g_task_get_task_data (task);
7380 *new_etag = data->etag;
7381 data->etag = NULL; /* Take ownership */
7388 g_file_real_measure_disk_usage (GFile *file,
7389 GFileMeasureFlags flags,
7390 GCancellable *cancellable,
7391 GFileMeasureProgressCallback progress_callback,
7392 gpointer progress_data,
7393 guint64 *disk_usage,
7398 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7399 "Operation not supported for the current backend.");
7405 GFileMeasureFlags flags;
7406 GFileMeasureProgressCallback progress_callback;
7407 gpointer progress_data;
7419 GFileMeasureProgressCallback callback;
7422 guint64 current_size;
7428 measure_disk_usage_invoke_progress (gpointer user_data)
7430 MeasureProgress *progress = user_data;
7432 (* progress->callback) (progress->reporting,
7433 progress->current_size, progress->num_dirs, progress->num_files,
7434 progress->user_data);
7440 measure_disk_usage_progress (gboolean reporting,
7441 guint64 current_size,
7446 MeasureProgress progress;
7447 GTask *task = user_data;
7448 MeasureTaskData *data;
7450 data = g_task_get_task_data (task);
7452 progress.callback = data->progress_callback;
7453 progress.user_data = data->progress_data;
7454 progress.reporting = reporting;
7455 progress.current_size = current_size;
7456 progress.num_dirs = num_dirs;
7457 progress.num_files = num_files;
7459 g_main_context_invoke_full (g_task_get_context (task),
7460 g_task_get_priority (task),
7461 measure_disk_usage_invoke_progress,
7462 g_memdup (&progress, sizeof progress),
7467 measure_disk_usage_thread (GTask *task,
7468 gpointer source_object,
7470 GCancellable *cancellable)
7472 MeasureTaskData *data = task_data;
7473 GError *error = NULL;
7474 MeasureResult result;
7476 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7477 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7478 &result.disk_usage, &result.num_dirs, &result.num_files,
7480 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7482 g_task_return_error (task, error);
7486 g_file_real_measure_disk_usage_async (GFile *file,
7487 GFileMeasureFlags flags,
7489 GCancellable *cancellable,
7490 GFileMeasureProgressCallback progress_callback,
7491 gpointer progress_data,
7492 GAsyncReadyCallback callback,
7495 MeasureTaskData data;
7499 data.progress_callback = progress_callback;
7500 data.progress_data = progress_data;
7502 task = g_task_new (file, cancellable, callback, user_data);
7503 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7504 g_task_set_priority (task, io_priority);
7506 g_task_run_in_thread (task, measure_disk_usage_thread);
7507 g_object_unref (task);
7511 g_file_real_measure_disk_usage_finish (GFile *file,
7512 GAsyncResult *result,
7513 guint64 *disk_usage,
7518 MeasureResult *measure_result;
7520 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7522 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7524 if (measure_result == NULL)
7528 *disk_usage = measure_result->disk_usage;
7531 *num_dirs = measure_result->num_dirs;
7534 *num_files = measure_result->num_files;
7536 g_free (measure_result);
7542 * g_file_measure_disk_usage:
7544 * @flags: #GFileMeasureFlags
7545 * @cancellable: (allow-none): optional #GCancellable
7546 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7547 * @progress_data: user_data for @progress_callback
7548 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7549 * @num_dirs: (allow-none) (out): the number of directories encountered
7550 * @num_files: (allow-none) (out): the number of non-directories encountered
7551 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7553 * Recursively measures the disk usage of @file.
7555 * This is essentially an analog of the '<literal>du</literal>' command,
7556 * but it also reports the number of directories and non-directory files
7557 * encountered (including things like symbolic links).
7559 * By default, errors are only reported against the toplevel file
7560 * itself. Errors found while recursing are silently ignored, unless
7561 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7563 * The returned size, @disk_usage, is in bytes and should be formatted
7564 * with g_format_size() in order to get something reasonable for showing
7565 * in a user interface.
7567 * @progress_callback and @progress_data can be given to request
7568 * periodic progress updates while scanning. See the documentation for
7569 * #GFileMeasureProgressCallback for information about when and how the
7570 * callback will be invoked.
7572 * Returns: %TRUE if successful, with the out parameters set.
7573 * %FALSE otherwise, with @error set.
7578 g_file_measure_disk_usage (GFile *file,
7579 GFileMeasureFlags flags,
7580 GCancellable *cancellable,
7581 GFileMeasureProgressCallback progress_callback,
7582 gpointer progress_data,
7583 guint64 *disk_usage,
7588 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7589 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7590 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7592 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7593 progress_callback, progress_data,
7594 disk_usage, num_dirs, num_files,
7599 * g_file_measure_disk_usage_async:
7601 * @flags: #GFileMeasureFlags
7602 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7604 * @cancellable: (allow-none): optional #GCancellable
7605 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7606 * @progress_data: user_data for @progress_callback
7607 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7608 * @user_data: the data to pass to callback function
7610 * Recursively measures the disk usage of @file.
7612 * This is the asynchronous version of g_file_measure_disk_usage(). See
7613 * there for more information.
7618 g_file_measure_disk_usage_async (GFile *file,
7619 GFileMeasureFlags flags,
7621 GCancellable *cancellable,
7622 GFileMeasureProgressCallback progress_callback,
7623 gpointer progress_data,
7624 GAsyncReadyCallback callback,
7627 g_return_if_fail (G_IS_FILE (file));
7628 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7630 return G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7631 progress_callback, progress_data,
7632 callback, user_data);
7636 * g_file_measure_disk_usage_finish:
7638 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7639 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7640 * @num_dirs: (allow-none) (out): the number of directories encountered
7641 * @num_files: (allow-none) (out): the number of non-directories encountered
7642 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7644 * Collects the results from an earlier call to
7645 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7648 * Returns: %TRUE if successful, with the out parameters set.
7649 * %FALSE otherwise, with @error set.
7654 g_file_measure_disk_usage_finish (GFile *file,
7655 GAsyncResult *result,
7656 guint64 *disk_usage,
7661 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7662 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7664 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7668 * g_file_start_mountable:
7669 * @file: input #GFile
7670 * @flags: flags affecting the operation
7671 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7672 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7673 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7674 * @user_data: the data to pass to callback function
7676 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7677 * Using @start_operation, you can request callbacks when, for instance,
7678 * passwords are needed during authentication.
7680 * If @cancellable is not %NULL, then the operation can be cancelled by
7681 * triggering the cancellable object from another thread. If the operation
7682 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7684 * When the operation is finished, @callback will be called.
7685 * You can then call g_file_mount_mountable_finish() to get
7686 * the result of the operation.
7691 g_file_start_mountable (GFile *file,
7692 GDriveStartFlags flags,
7693 GMountOperation *start_operation,
7694 GCancellable *cancellable,
7695 GAsyncReadyCallback callback,
7700 g_return_if_fail (G_IS_FILE (file));
7702 iface = G_FILE_GET_IFACE (file);
7704 if (iface->start_mountable == NULL)
7706 g_task_report_new_error (file, callback, user_data,
7707 g_file_start_mountable,
7708 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7709 _("Operation not supported"));
7713 (* iface->start_mountable) (file,
7722 * g_file_start_mountable_finish:
7723 * @file: input #GFile
7724 * @result: a #GAsyncResult
7725 * @error: a #GError, or %NULL
7727 * Finishes a start operation. See g_file_start_mountable() for details.
7729 * Finish an asynchronous start operation that was started
7730 * with g_file_start_mountable().
7732 * Returns: %TRUE if the operation finished successfully. %FALSE
7738 g_file_start_mountable_finish (GFile *file,
7739 GAsyncResult *result,
7744 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7745 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7747 if (g_async_result_legacy_propagate_error (result, error))
7749 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7750 return g_task_propagate_boolean (G_TASK (result), error);
7752 iface = G_FILE_GET_IFACE (file);
7753 return (* iface->start_mountable_finish) (file, result, error);
7757 * g_file_stop_mountable:
7758 * @file: input #GFile
7759 * @flags: flags affecting the operation
7760 * @mount_operation: (allow-none): a #GMountOperation,
7761 * or %NULL to avoid user interaction.
7762 * @cancellable: (allow-none): optional #GCancellable object,
7764 * @callback: (allow-none): a #GAsyncReadyCallback to call
7765 * when the request is satisfied, or %NULL
7766 * @user_data: the data to pass to callback function
7768 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7770 * If @cancellable is not %NULL, then the operation can be cancelled by
7771 * triggering the cancellable object from another thread. If the operation
7772 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7774 * When the operation is finished, @callback will be called.
7775 * You can then call g_file_stop_mountable_finish() to get
7776 * the result of the operation.
7781 g_file_stop_mountable (GFile *file,
7782 GMountUnmountFlags flags,
7783 GMountOperation *mount_operation,
7784 GCancellable *cancellable,
7785 GAsyncReadyCallback callback,
7790 g_return_if_fail (G_IS_FILE (file));
7792 iface = G_FILE_GET_IFACE (file);
7794 if (iface->stop_mountable == NULL)
7796 g_task_report_new_error (file, callback, user_data,
7797 g_file_stop_mountable,
7798 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7799 _("Operation not supported"));
7803 (* iface->stop_mountable) (file,
7812 * g_file_stop_mountable_finish:
7813 * @file: input #GFile
7814 * @result: a #GAsyncResult
7815 * @error: a #GError, or %NULL
7817 * Finishes an stop operation, see g_file_stop_mountable() for details.
7819 * Finish an asynchronous stop operation that was started
7820 * with g_file_stop_mountable().
7822 * Returns: %TRUE if the operation finished successfully.
7828 g_file_stop_mountable_finish (GFile *file,
7829 GAsyncResult *result,
7834 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7835 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7837 if (g_async_result_legacy_propagate_error (result, error))
7839 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7840 return g_task_propagate_boolean (G_TASK (result), error);
7842 iface = G_FILE_GET_IFACE (file);
7843 return (* iface->stop_mountable_finish) (file, result, error);
7847 * g_file_poll_mountable:
7848 * @file: input #GFile
7849 * @cancellable: optional #GCancellable object, %NULL to ignore
7850 * @callback: (allow-none): a #GAsyncReadyCallback to call
7851 * when the request is satisfied, or %NULL
7852 * @user_data: the data to pass to callback function
7854 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7856 * If @cancellable is not %NULL, then the operation can be cancelled by
7857 * triggering the cancellable object from another thread. If the operation
7858 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7860 * When the operation is finished, @callback will be called.
7861 * You can then call g_file_mount_mountable_finish() to get
7862 * the result of the operation.
7867 g_file_poll_mountable (GFile *file,
7868 GCancellable *cancellable,
7869 GAsyncReadyCallback callback,
7874 g_return_if_fail (G_IS_FILE (file));
7876 iface = G_FILE_GET_IFACE (file);
7878 if (iface->poll_mountable == NULL)
7880 g_task_report_new_error (file, callback, user_data,
7881 g_file_poll_mountable,
7882 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7883 _("Operation not supported"));
7887 (* iface->poll_mountable) (file,
7894 * g_file_poll_mountable_finish:
7895 * @file: input #GFile
7896 * @result: a #GAsyncResult
7897 * @error: a #GError, or %NULL
7899 * Finishes a poll operation. See g_file_poll_mountable() for details.
7901 * Finish an asynchronous poll operation that was polled
7902 * with g_file_poll_mountable().
7904 * Returns: %TRUE if the operation finished successfully. %FALSE
7910 g_file_poll_mountable_finish (GFile *file,
7911 GAsyncResult *result,
7916 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7917 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7919 if (g_async_result_legacy_propagate_error (result, error))
7921 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7922 return g_task_propagate_boolean (G_TASK (result), error);
7924 iface = G_FILE_GET_IFACE (file);
7925 return (* iface->poll_mountable_finish) (file, result, error);
7929 * g_file_supports_thread_contexts:
7932 * Checks if @file supports <link
7933 * linkend="g-main-context-push-thread-default-context">thread-default
7934 * contexts</link>. If this returns %FALSE, you cannot perform
7935 * asynchronous operations on @file in a thread that has a
7936 * thread-default context.
7938 * Returns: Whether or not @file supports thread-default contexts.
7943 g_file_supports_thread_contexts (GFile *file)
7947 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7949 iface = G_FILE_GET_IFACE (file);
7950 return iface->supports_thread_contexts;