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_error_free (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 /* Ok, ditch the previous lightweight info (on Unix we just
3116 * called lstat()); at this point we gather all the information
3117 * we need about the source from the opened file descriptor.
3119 g_object_unref (info);
3121 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3122 cancellable, error);
3123 g_free (attrs_to_read);
3127 do_set_attributes = TRUE;
3130 /* In the local file path, we pass down the source info which
3131 * includes things like unix::mode, to ensure that the target file
3132 * is not created with different permissions from the source file.
3134 * If a future API like g_file_replace_with_info() is added, switch
3135 * this code to use that.
3137 if (G_IS_LOCAL_FILE (destination))
3139 if (flags & G_FILE_COPY_OVERWRITE)
3140 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3142 flags & G_FILE_COPY_BACKUP,
3143 G_FILE_CREATE_REPLACE_DESTINATION,
3145 cancellable, error);
3147 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3149 cancellable, error);
3151 else if (flags & G_FILE_COPY_OVERWRITE)
3153 out = (GOutputStream *)g_file_replace (destination,
3155 flags & G_FILE_COPY_BACKUP,
3156 G_FILE_CREATE_REPLACE_DESTINATION,
3157 cancellable, error);
3161 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3168 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3170 GError *reflink_err = NULL;
3172 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3173 progress_callback, progress_callback_data,
3176 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3178 g_clear_error (&reflink_err);
3182 g_propagate_error (error, reflink_err);
3195 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3197 GError *splice_err = NULL;
3199 if (!splice_stream_with_progress (in, out, cancellable,
3200 progress_callback, progress_callback_data,
3203 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3205 g_clear_error (&splice_err);
3209 g_propagate_error (error, splice_err);
3222 /* A plain read/write loop */
3223 if (!copy_stream_with_progress (in, out, source, cancellable,
3224 progress_callback, progress_callback_data,
3232 /* Don't care about errors in source here */
3233 (void) g_input_stream_close (in, cancellable, NULL);
3234 g_object_unref (in);
3239 /* But write errors on close are bad! */
3240 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3242 g_object_unref (out);
3245 /* Ignore errors here. Failure to copy metadata is not a hard error */
3246 /* TODO: set these attributes /before/ we do the rename() on Unix */
3247 if (ret && do_set_attributes)
3249 g_file_set_attributes_from_info (destination,
3251 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3256 g_clear_object (&info);
3263 * @source: input #GFile
3264 * @destination: destination #GFile
3265 * @flags: set of #GFileCopyFlags
3266 * @cancellable: (allow-none): optional #GCancellable object,
3268 * @progress_callback: (allow-none) (scope call): function to callback with
3269 * progress information, or %NULL if progress information is not needed
3270 * @progress_callback_data: (closure): user data to pass to @progress_callback
3271 * @error: #GError to set on error, or %NULL
3273 * Copies the file @source to the location specified by @destination.
3274 * Can not handle recursive copies of directories.
3276 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3277 * existing @destination file is overwritten.
3279 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3280 * will be copied as symlinks, otherwise the target of the
3281 * @source symlink will be copied.
3283 * If @cancellable is not %NULL, then the operation can be cancelled by
3284 * triggering the cancellable object from another thread. If the operation
3285 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3287 * If @progress_callback is not %NULL, then the operation can be monitored
3288 * by setting this to a #GFileProgressCallback function.
3289 * @progress_callback_data will be passed to this function. It is guaranteed
3290 * that this callback will be called after all data has been transferred with
3291 * the total number of bytes copied during the operation.
3293 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3294 * is returned, independent on the status of the @destination.
3296 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3297 * the error %G_IO_ERROR_EXISTS is returned.
3299 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3300 * error is returned. If trying to overwrite a directory with a directory the
3301 * %G_IO_ERROR_WOULD_MERGE error is returned.
3303 * If the source is a directory and the target does not exist, or
3304 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3305 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3307 * If you are interested in copying the #GFile object itself (not the on-disk
3308 * file), see g_file_dup().
3310 * Returns: %TRUE on success, %FALSE otherwise.
3313 g_file_copy (GFile *source,
3315 GFileCopyFlags flags,
3316 GCancellable *cancellable,
3317 GFileProgressCallback progress_callback,
3318 gpointer progress_callback_data,
3325 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3326 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3328 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3331 iface = G_FILE_GET_IFACE (destination);
3335 res = (* iface->copy) (source, destination,
3337 progress_callback, progress_callback_data,
3343 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3345 g_propagate_error (error, my_error);
3349 g_clear_error (&my_error);
3352 /* If the types are different, and the destination method failed
3353 * also try the source method
3355 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3357 iface = G_FILE_GET_IFACE (source);
3362 res = (* iface->copy) (source, destination,
3364 progress_callback, progress_callback_data,
3370 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3372 g_propagate_error (error, my_error);
3376 g_clear_error (&my_error);
3380 return file_copy_fallback (source, destination, flags, cancellable,
3381 progress_callback, progress_callback_data,
3386 * g_file_copy_async: (skip)
3387 * @source: input #GFile
3388 * @destination: destination #GFile
3389 * @flags: set of #GFileCopyFlags
3390 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3392 * @cancellable: (allow-none): optional #GCancellable object,
3394 * @progress_callback: (allow-none): function to callback with progress
3395 * information, or %NULL if progress information is not needed
3396 * @progress_callback_data: (closure): user data to pass to @progress_callback
3397 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3398 * @user_data: the data to pass to callback function
3400 * Copies the file @source to the location specified by @destination
3401 * asynchronously. For details of the behaviour, see g_file_copy().
3403 * If @progress_callback is not %NULL, then that function that will be called
3404 * just like in g_file_copy(), however the callback will run in the main loop,
3405 * not in the thread that is doing the I/O operation.
3407 * When the operation is finished, @callback will be called. You can then call
3408 * g_file_copy_finish() to get the result of the operation.
3411 g_file_copy_async (GFile *source,
3413 GFileCopyFlags flags,
3415 GCancellable *cancellable,
3416 GFileProgressCallback progress_callback,
3417 gpointer progress_callback_data,
3418 GAsyncReadyCallback callback,
3423 g_return_if_fail (G_IS_FILE (source));
3424 g_return_if_fail (G_IS_FILE (destination));
3426 iface = G_FILE_GET_IFACE (source);
3427 (* iface->copy_async) (source,
3433 progress_callback_data,
3439 * g_file_copy_finish:
3440 * @file: input #GFile
3441 * @res: a #GAsyncResult
3442 * @error: a #GError, or %NULL
3444 * Finishes copying the file started with g_file_copy_async().
3446 * Returns: a %TRUE on success, %FALSE on error.
3449 g_file_copy_finish (GFile *file,
3455 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3456 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3458 if (g_async_result_legacy_propagate_error (res, error))
3461 iface = G_FILE_GET_IFACE (file);
3462 return (* iface->copy_finish) (file, res, error);
3467 * @source: #GFile pointing to the source location
3468 * @destination: #GFile pointing to the destination location
3469 * @flags: set of #GFileCopyFlags
3470 * @cancellable: (allow-none): optional #GCancellable object,
3472 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3473 * function for updates
3474 * @progress_callback_data: (closure): gpointer to user data for
3475 * the callback function
3476 * @error: #GError for returning error conditions, or %NULL
3478 * Tries to move the file or directory @source to the location specified
3479 * by @destination. If native move operations are supported then this is
3480 * used, otherwise a copy + delete fallback is used. The native
3481 * implementation may support moving directories (for instance on moves
3482 * inside the same filesystem), but the fallback code does not.
3484 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3485 * existing @destination file is overwritten.
3487 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3488 * will be copied as symlinks, otherwise the target of the
3489 * @source symlink will be copied.
3491 * If @cancellable is not %NULL, then the operation can be cancelled by
3492 * triggering the cancellable object from another thread. If the operation
3493 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3495 * If @progress_callback is not %NULL, then the operation can be monitored
3496 * by setting this to a #GFileProgressCallback function.
3497 * @progress_callback_data will be passed to this function. It is
3498 * guaranteed that this callback will be called after all data has been
3499 * transferred with the total number of bytes copied during the operation.
3501 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3502 * error is returned, independent on the status of the @destination.
3504 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3505 * then the error %G_IO_ERROR_EXISTS is returned.
3507 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3508 * error is returned. If trying to overwrite a directory with a directory the
3509 * %G_IO_ERROR_WOULD_MERGE error is returned.
3511 * If the source is a directory and the target does not exist, or
3512 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3513 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3514 * move operation isn't available).
3516 * Returns: %TRUE on successful move, %FALSE otherwise.
3519 g_file_move (GFile *source,
3521 GFileCopyFlags flags,
3522 GCancellable *cancellable,
3523 GFileProgressCallback progress_callback,
3524 gpointer progress_callback_data,
3531 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3532 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3534 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3537 iface = G_FILE_GET_IFACE (destination);
3541 res = (* iface->move) (source, destination,
3543 progress_callback, progress_callback_data,
3549 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3551 g_propagate_error (error, my_error);
3556 /* If the types are different, and the destination method failed
3557 * also try the source method
3559 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3561 iface = G_FILE_GET_IFACE (source);
3566 res = (* iface->move) (source, destination,
3568 progress_callback, progress_callback_data,
3574 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3576 g_propagate_error (error, my_error);
3582 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3584 g_set_error_literal (error, G_IO_ERROR,
3585 G_IO_ERROR_NOT_SUPPORTED,
3586 _("Operation not supported"));
3590 flags |= G_FILE_COPY_ALL_METADATA;
3591 if (!g_file_copy (source, destination, flags, cancellable,
3592 progress_callback, progress_callback_data,
3596 return g_file_delete (source, cancellable, error);
3600 * g_file_make_directory:
3601 * @file: input #GFile
3602 * @cancellable: (allow-none): optional #GCancellable object,
3604 * @error: a #GError, or %NULL
3606 * Creates a directory. Note that this will only create a child directory
3607 * of the immediate parent directory of the path or URI given by the #GFile.
3608 * To recursively create directories, see g_file_make_directory_with_parents().
3609 * This function will fail if the parent directory does not exist, setting
3610 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3611 * creating directories, this function will fail, setting @error to
3612 * %G_IO_ERROR_NOT_SUPPORTED.
3614 * For a local #GFile the newly created directory will have the default
3615 * (current) ownership and permissions of the current process.
3617 * If @cancellable is not %NULL, then the operation can be cancelled by
3618 * triggering the cancellable object from another thread. If the operation
3619 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3621 * Returns: %TRUE on successful creation, %FALSE otherwise.
3624 g_file_make_directory (GFile *file,
3625 GCancellable *cancellable,
3630 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3632 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3635 iface = G_FILE_GET_IFACE (file);
3637 if (iface->make_directory == NULL)
3639 g_set_error_literal (error, G_IO_ERROR,
3640 G_IO_ERROR_NOT_SUPPORTED,
3641 _("Operation not supported"));
3645 return (* iface->make_directory) (file, cancellable, error);
3649 * g_file_make_directory_async:
3650 * @file: input #GFile
3651 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3653 * @cancellable: (allow-none): optional #GCancellable object,
3655 * @callback: a #GAsyncReadyCallback to call
3656 * when the request is satisfied
3657 * @user_data: the data to pass to callback function
3659 * Asynchronously creates a directory.
3661 * Virtual: make_directory_async
3665 g_file_make_directory_async (GFile *file,
3667 GCancellable *cancellable,
3668 GAsyncReadyCallback callback,
3673 g_return_if_fail (G_IS_FILE (file));
3675 iface = G_FILE_GET_IFACE (file);
3676 (* iface->make_directory_async) (file,
3684 * g_file_make_directory_finish:
3685 * @file: input #GFile
3686 * @result: a #GAsyncResult
3687 * @error: a #GError, or %NULL
3689 * Finishes an asynchronous directory creation, started with
3690 * g_file_make_directory_async().
3692 * Virtual: make_directory_finish
3693 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3697 g_file_make_directory_finish (GFile *file,
3698 GAsyncResult *result,
3703 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3704 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3706 iface = G_FILE_GET_IFACE (file);
3707 return (* iface->make_directory_finish) (file, result, error);
3711 * g_file_make_directory_with_parents:
3712 * @file: input #GFile
3713 * @cancellable: (allow-none): optional #GCancellable object,
3715 * @error: a #GError, or %NULL
3717 * Creates a directory and any parent directories that may not
3718 * exist similar to 'mkdir -p'. If the file system does not support
3719 * creating directories, this function will fail, setting @error to
3720 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3721 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3722 * the similar g_mkdir_with_parents().
3724 * For a local #GFile the newly created directories will have the default
3725 * (current) ownership and permissions of the current process.
3727 * If @cancellable is not %NULL, then the operation can be cancelled by
3728 * triggering the cancellable object from another thread. If the operation
3729 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3731 * Returns: %TRUE if all directories have been successfully created, %FALSE
3737 g_file_make_directory_with_parents (GFile *file,
3738 GCancellable *cancellable,
3741 GFile *work_file = NULL;
3742 GList *list = NULL, *l;
3743 GError *my_error = NULL;
3745 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3747 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3750 g_file_make_directory (file, cancellable, &my_error);
3751 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3754 g_propagate_error (error, my_error);
3755 return my_error == NULL;
3758 work_file = g_object_ref (file);
3760 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3764 parent_file = g_file_get_parent (work_file);
3765 if (parent_file == NULL)
3768 g_clear_error (&my_error);
3769 g_file_make_directory (parent_file, cancellable, &my_error);
3771 g_object_unref (work_file);
3772 work_file = g_object_ref (parent_file);
3774 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3775 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3777 g_object_unref (parent_file);
3780 for (l = list; my_error == NULL && l; l = l->next)
3782 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3786 g_object_unref (work_file);
3789 while (list != NULL)
3791 g_object_unref ((GFile *) list->data);
3792 list = g_list_remove (list, list->data);
3795 if (my_error != NULL)
3797 g_propagate_error (error, my_error);
3801 return g_file_make_directory (file, cancellable, error);
3805 * g_file_make_symbolic_link:
3806 * @file: a #GFile with the name of the symlink to create
3807 * @symlink_value: a string with the path for the target of the new symlink
3808 * @cancellable: (allow-none): optional #GCancellable object,
3812 * Creates a symbolic link named @file which contains the string
3815 * If @cancellable is not %NULL, then the operation can be cancelled by
3816 * triggering the cancellable object from another thread. If the operation
3817 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3819 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3822 g_file_make_symbolic_link (GFile *file,
3823 const char *symlink_value,
3824 GCancellable *cancellable,
3829 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3830 g_return_val_if_fail (symlink_value != NULL, FALSE);
3832 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3835 if (*symlink_value == '\0')
3837 g_set_error_literal (error, G_IO_ERROR,
3838 G_IO_ERROR_INVALID_ARGUMENT,
3839 _("Invalid symlink value given"));
3843 iface = G_FILE_GET_IFACE (file);
3845 if (iface->make_symbolic_link == NULL)
3847 g_set_error_literal (error, G_IO_ERROR,
3848 G_IO_ERROR_NOT_SUPPORTED,
3849 _("Operation not supported"));
3853 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3858 * @file: input #GFile
3859 * @cancellable: (allow-none): optional #GCancellable object,
3861 * @error: a #GError, or %NULL
3863 * Deletes a file. If the @file is a directory, it will only be
3864 * deleted if it is empty. This has the same semantics as g_unlink().
3866 * If @cancellable is not %NULL, then the operation can be cancelled by
3867 * triggering the cancellable object from another thread. If the operation
3868 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3870 * Virtual: delete_file
3871 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3874 g_file_delete (GFile *file,
3875 GCancellable *cancellable,
3880 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3882 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3885 iface = G_FILE_GET_IFACE (file);
3887 if (iface->delete_file == NULL)
3889 g_set_error_literal (error, G_IO_ERROR,
3890 G_IO_ERROR_NOT_SUPPORTED,
3891 _("Operation not supported"));
3895 return (* iface->delete_file) (file, cancellable, error);
3899 * g_file_delete_async:
3900 * @file: input #GFile
3901 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3903 * @cancellable: (allow-none): optional #GCancellable object,
3905 * @callback: a #GAsyncReadyCallback to call
3906 * when the request is satisfied
3907 * @user_data: the data to pass to callback function
3909 * Asynchronously delete a file. If the @file is a directory, it will
3910 * only be deleted if it is empty. This has the same semantics as
3913 * Virtual: delete_file_async
3917 g_file_delete_async (GFile *file,
3919 GCancellable *cancellable,
3920 GAsyncReadyCallback callback,
3925 g_return_if_fail (G_IS_FILE (file));
3927 iface = G_FILE_GET_IFACE (file);
3928 (* iface->delete_file_async) (file,
3936 * g_file_delete_finish:
3937 * @file: input #GFile
3938 * @result: a #GAsyncResult
3939 * @error: a #GError, or %NULL
3941 * Finishes deleting a file started with g_file_delete_async().
3943 * Virtual: delete_file_finish
3944 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3948 g_file_delete_finish (GFile *file,
3949 GAsyncResult *result,
3954 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3955 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3957 if (g_async_result_legacy_propagate_error (result, error))
3960 iface = G_FILE_GET_IFACE (file);
3961 return (* iface->delete_file_finish) (file, result, error);
3966 * @file: #GFile to send to trash
3967 * @cancellable: (allow-none): optional #GCancellable object,
3969 * @error: a #GError, or %NULL
3971 * Sends @file to the "Trashcan", if possible. This is similar to
3972 * deleting it, but the user can recover it before emptying the trashcan.
3973 * Not all file systems support trashing, so this call can return the
3974 * %G_IO_ERROR_NOT_SUPPORTED error.
3976 * If @cancellable is not %NULL, then the operation can be cancelled by
3977 * triggering the cancellable object from another thread. If the operation
3978 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3981 * Returns: %TRUE on successful trash, %FALSE otherwise.
3984 g_file_trash (GFile *file,
3985 GCancellable *cancellable,
3990 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3992 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3995 iface = G_FILE_GET_IFACE (file);
3997 if (iface->trash == NULL)
3999 g_set_error_literal (error,
4000 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4001 _("Trash not supported"));
4005 return (* iface->trash) (file, cancellable, error);
4009 * g_file_trash_async:
4010 * @file: input #GFile
4011 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4013 * @cancellable: (allow-none): optional #GCancellable object,
4015 * @callback: a #GAsyncReadyCallback to call
4016 * when the request is satisfied
4017 * @user_data: the data to pass to callback function
4019 * Asynchronously sends @file to the Trash location, if possible.
4021 * Virtual: trash_async
4025 g_file_trash_async (GFile *file,
4027 GCancellable *cancellable,
4028 GAsyncReadyCallback callback,
4033 g_return_if_fail (G_IS_FILE (file));
4035 iface = G_FILE_GET_IFACE (file);
4036 (* iface->trash_async) (file,
4044 * g_file_trash_finish:
4045 * @file: input #GFile
4046 * @result: a #GAsyncResult
4047 * @error: a #GError, or %NULL
4049 * Finishes an asynchronous file trashing operation, started with
4050 * g_file_trash_async().
4052 * Virtual: trash_finish
4053 * Returns: %TRUE on successful trash, %FALSE otherwise.
4057 g_file_trash_finish (GFile *file,
4058 GAsyncResult *result,
4063 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4064 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4066 iface = G_FILE_GET_IFACE (file);
4067 return (* iface->trash_finish) (file, result, error);
4071 * g_file_set_display_name:
4072 * @file: input #GFile
4073 * @display_name: a string
4074 * @cancellable: (allow-none): optional #GCancellable object,
4076 * @error: a #GError, or %NULL
4078 * Renames @file to the specified display name.
4080 * The display name is converted from UTF-8 to the correct encoding
4081 * for the target filesystem if possible and the @file is renamed to this.
4083 * If you want to implement a rename operation in the user interface the
4084 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4085 * initial value in the rename widget, and then the result after editing
4086 * should be passed to g_file_set_display_name().
4088 * On success the resulting converted filename is returned.
4090 * If @cancellable is not %NULL, then the operation can be cancelled by
4091 * triggering the cancellable object from another thread. If the operation
4092 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4094 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4095 * or %NULL if there was an error.
4096 * Free the returned object with g_object_unref().
4099 g_file_set_display_name (GFile *file,
4100 const gchar *display_name,
4101 GCancellable *cancellable,
4106 g_return_val_if_fail (G_IS_FILE (file), NULL);
4107 g_return_val_if_fail (display_name != NULL, NULL);
4109 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4113 G_IO_ERROR_INVALID_ARGUMENT,
4114 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4118 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4121 iface = G_FILE_GET_IFACE (file);
4123 return (* iface->set_display_name) (file, display_name, cancellable, error);
4127 * g_file_set_display_name_async:
4128 * @file: input #GFile
4129 * @display_name: a string
4130 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4132 * @cancellable: (allow-none): optional #GCancellable object,
4134 * @callback: (scope async): a #GAsyncReadyCallback to call
4135 * when the request is satisfied
4136 * @user_data: (closure): the data to pass to callback function
4138 * Asynchronously sets the display name for a given #GFile.
4140 * For more details, see g_file_set_display_name() which is
4141 * the synchronous version of this call.
4143 * When the operation is finished, @callback will be called.
4144 * You can then call g_file_set_display_name_finish() to get
4145 * the result of the operation.
4148 g_file_set_display_name_async (GFile *file,
4149 const gchar *display_name,
4151 GCancellable *cancellable,
4152 GAsyncReadyCallback callback,
4157 g_return_if_fail (G_IS_FILE (file));
4158 g_return_if_fail (display_name != NULL);
4160 iface = G_FILE_GET_IFACE (file);
4161 (* iface->set_display_name_async) (file,
4170 * g_file_set_display_name_finish:
4171 * @file: input #GFile
4172 * @res: a #GAsyncResult
4173 * @error: a #GError, or %NULL
4175 * Finishes setting a display name started with
4176 * g_file_set_display_name_async().
4178 * Returns: (transfer full): a #GFile or %NULL on error.
4179 * Free the returned object with g_object_unref().
4182 g_file_set_display_name_finish (GFile *file,
4188 g_return_val_if_fail (G_IS_FILE (file), NULL);
4189 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4191 if (g_async_result_legacy_propagate_error (res, error))
4194 iface = G_FILE_GET_IFACE (file);
4195 return (* iface->set_display_name_finish) (file, res, error);
4199 * g_file_query_settable_attributes:
4200 * @file: input #GFile
4201 * @cancellable: (allow-none): optional #GCancellable object,
4203 * @error: a #GError, or %NULL
4205 * Obtain the list of settable attributes for the file.
4207 * Returns the type and full attribute name of all the attributes
4208 * that can be set on this file. This doesn't mean setting it will
4209 * always succeed though, you might get an access failure, or some
4210 * specific file may not support a specific attribute.
4212 * If @cancellable is not %NULL, then the operation can be cancelled by
4213 * triggering the cancellable object from another thread. If the operation
4214 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4216 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4217 * When you are done with it, release it with
4218 * g_file_attribute_info_list_unref()
4220 GFileAttributeInfoList *
4221 g_file_query_settable_attributes (GFile *file,
4222 GCancellable *cancellable,
4227 GFileAttributeInfoList *list;
4229 g_return_val_if_fail (G_IS_FILE (file), NULL);
4231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4234 iface = G_FILE_GET_IFACE (file);
4236 if (iface->query_settable_attributes == NULL)
4237 return g_file_attribute_info_list_new ();
4240 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4244 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4246 list = g_file_attribute_info_list_new ();
4247 g_error_free (my_error);
4250 g_propagate_error (error, my_error);
4257 * g_file_query_writable_namespaces:
4258 * @file: input #GFile
4259 * @cancellable: (allow-none): optional #GCancellable object,
4261 * @error: a #GError, or %NULL
4263 * Obtain the list of attribute namespaces where new attributes
4264 * can be created by a user. An example of this is extended
4265 * attributes (in the "xattr" namespace).
4267 * If @cancellable is not %NULL, then the operation can be cancelled by
4268 * triggering the cancellable object from another thread. If the operation
4269 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4271 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4272 * When you are done with it, release it with
4273 * g_file_attribute_info_list_unref()
4275 GFileAttributeInfoList *
4276 g_file_query_writable_namespaces (GFile *file,
4277 GCancellable *cancellable,
4282 GFileAttributeInfoList *list;
4284 g_return_val_if_fail (G_IS_FILE (file), NULL);
4286 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4289 iface = G_FILE_GET_IFACE (file);
4291 if (iface->query_writable_namespaces == NULL)
4292 return g_file_attribute_info_list_new ();
4295 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4299 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4301 list = g_file_attribute_info_list_new ();
4302 g_error_free (my_error);
4305 g_propagate_error (error, my_error);
4312 * g_file_set_attribute:
4313 * @file: input #GFile
4314 * @attribute: a string containing the attribute's name
4315 * @type: The type of the attribute
4316 * @value_p: (allow-none): a pointer to the value (or the pointer
4317 * itself if the type is a pointer type)
4318 * @flags: a set of #GFileQueryInfoFlags
4319 * @cancellable: (allow-none): optional #GCancellable object,
4321 * @error: a #GError, or %NULL
4323 * Sets an attribute in the file with attribute name @attribute to @value.
4325 * Some attributes can be unset by setting @attribute to
4326 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4328 * If @cancellable is not %NULL, then the operation can be cancelled by
4329 * triggering the cancellable object from another thread. If the operation
4330 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4332 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4335 g_file_set_attribute (GFile *file,
4336 const gchar *attribute,
4337 GFileAttributeType type,
4339 GFileQueryInfoFlags flags,
4340 GCancellable *cancellable,
4345 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4346 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4348 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4351 iface = G_FILE_GET_IFACE (file);
4353 if (iface->set_attribute == NULL)
4355 g_set_error_literal (error, G_IO_ERROR,
4356 G_IO_ERROR_NOT_SUPPORTED,
4357 _("Operation not supported"));
4361 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4365 * g_file_set_attributes_from_info:
4366 * @file: input #GFile
4367 * @info: a #GFileInfo
4368 * @flags: #GFileQueryInfoFlags
4369 * @cancellable: (allow-none): optional #GCancellable object,
4371 * @error: a #GError, or %NULL
4373 * Tries to set all attributes in the #GFileInfo on the target
4374 * values, not stopping on the first error.
4376 * If there is any error during this operation then @error will
4377 * be set to the first error. Error on particular fields are flagged
4378 * by setting the "status" field in the attribute value to
4379 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4380 * also detect further errors.
4382 * If @cancellable is not %NULL, then the operation can be cancelled by
4383 * triggering the cancellable object from another thread. If the operation
4384 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4386 * Returns: %FALSE if there was any error, %TRUE otherwise.
4389 g_file_set_attributes_from_info (GFile *file,
4391 GFileQueryInfoFlags flags,
4392 GCancellable *cancellable,
4397 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4398 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4400 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4403 g_file_info_clear_status (info);
4405 iface = G_FILE_GET_IFACE (file);
4407 return (* iface->set_attributes_from_info) (file,
4415 g_file_real_set_attributes_from_info (GFile *file,
4417 GFileQueryInfoFlags flags,
4418 GCancellable *cancellable,
4424 GFileAttributeValue *value;
4428 attributes = g_file_info_list_attributes (info, NULL);
4430 for (i = 0; attributes[i] != NULL; i++)
4432 value = _g_file_info_get_attribute_value (info, attributes[i]);
4434 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4437 if (!g_file_set_attribute (file, attributes[i],
4438 value->type, _g_file_attribute_value_peek_as_pointer (value),
4439 flags, cancellable, error))
4441 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4443 /* Don't set error multiple times */
4447 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4450 g_strfreev (attributes);
4456 * g_file_set_attributes_async:
4457 * @file: input #GFile
4458 * @info: a #GFileInfo
4459 * @flags: a #GFileQueryInfoFlags
4460 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4462 * @cancellable: (allow-none): optional #GCancellable object,
4464 * @callback: (scope async): a #GAsyncReadyCallback
4465 * @user_data: (closure): a #gpointer
4467 * Asynchronously sets the attributes of @file with @info.
4469 * For more details, see g_file_set_attributes_from_info(),
4470 * which is the synchronous version of this call.
4472 * When the operation is finished, @callback will be called.
4473 * You can then call g_file_set_attributes_finish() to get
4474 * the result of the operation.
4477 g_file_set_attributes_async (GFile *file,
4479 GFileQueryInfoFlags flags,
4481 GCancellable *cancellable,
4482 GAsyncReadyCallback callback,
4487 g_return_if_fail (G_IS_FILE (file));
4488 g_return_if_fail (G_IS_FILE_INFO (info));
4490 iface = G_FILE_GET_IFACE (file);
4491 (* iface->set_attributes_async) (file,
4501 * g_file_set_attributes_finish:
4502 * @file: input #GFile
4503 * @result: a #GAsyncResult
4504 * @info: (out) (transfer full): a #GFileInfo
4505 * @error: a #GError, or %NULL
4507 * Finishes setting an attribute started in g_file_set_attributes_async().
4509 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4512 g_file_set_attributes_finish (GFile *file,
4513 GAsyncResult *result,
4519 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4520 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4522 /* No standard handling of errors here, as we must set info even
4525 iface = G_FILE_GET_IFACE (file);
4526 return (* iface->set_attributes_finish) (file, result, info, error);
4530 * g_file_set_attribute_string:
4531 * @file: input #GFile
4532 * @attribute: a string containing the attribute's name
4533 * @value: a string containing the attribute's value
4534 * @flags: #GFileQueryInfoFlags
4535 * @cancellable: (allow-none): optional #GCancellable object,
4537 * @error: a #GError, or %NULL
4539 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4540 * If @attribute is of a different type, this operation will fail.
4542 * If @cancellable is not %NULL, then the operation can be cancelled by
4543 * triggering the cancellable object from another thread. If the operation
4544 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4546 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4549 g_file_set_attribute_string (GFile *file,
4550 const char *attribute,
4552 GFileQueryInfoFlags flags,
4553 GCancellable *cancellable,
4556 return g_file_set_attribute (file, attribute,
4557 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4558 flags, cancellable, error);
4562 * g_file_set_attribute_byte_string:
4563 * @file: input #GFile
4564 * @attribute: a string containing the attribute's name
4565 * @value: a string containing the attribute's new value
4566 * @flags: a #GFileQueryInfoFlags
4567 * @cancellable: (allow-none): optional #GCancellable object,
4569 * @error: a #GError, or %NULL
4571 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4572 * If @attribute is of a different type, this operation will fail,
4575 * If @cancellable is not %NULL, then the operation can be cancelled by
4576 * triggering the cancellable object from another thread. If the operation
4577 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4579 * Returns: %TRUE if the @attribute was successfully set to @value
4580 * in the @file, %FALSE otherwise.
4583 g_file_set_attribute_byte_string (GFile *file,
4584 const gchar *attribute,
4586 GFileQueryInfoFlags flags,
4587 GCancellable *cancellable,
4590 return g_file_set_attribute (file, attribute,
4591 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4592 flags, cancellable, error);
4596 * g_file_set_attribute_uint32:
4597 * @file: input #GFile
4598 * @attribute: a string containing the attribute's name
4599 * @value: a #guint32 containing the attribute's new value
4600 * @flags: a #GFileQueryInfoFlags
4601 * @cancellable: (allow-none): optional #GCancellable object,
4603 * @error: a #GError, or %NULL
4605 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4606 * If @attribute is of a different type, this operation will fail.
4608 * If @cancellable is not %NULL, then the operation can be cancelled by
4609 * triggering the cancellable object from another thread. If the operation
4610 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4612 * Returns: %TRUE if the @attribute was successfully set to @value
4613 * in the @file, %FALSE otherwise.
4616 g_file_set_attribute_uint32 (GFile *file,
4617 const gchar *attribute,
4619 GFileQueryInfoFlags flags,
4620 GCancellable *cancellable,
4623 return g_file_set_attribute (file, attribute,
4624 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4625 flags, cancellable, error);
4629 * g_file_set_attribute_int32:
4630 * @file: input #GFile
4631 * @attribute: a string containing the attribute's name
4632 * @value: a #gint32 containing the attribute's new value
4633 * @flags: a #GFileQueryInfoFlags
4634 * @cancellable: (allow-none): optional #GCancellable object,
4636 * @error: a #GError, or %NULL
4638 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4639 * If @attribute is of a different type, this operation will fail.
4641 * If @cancellable is not %NULL, then the operation can be cancelled by
4642 * triggering the cancellable object from another thread. If the operation
4643 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4645 * Returns: %TRUE if the @attribute was successfully set to @value
4646 * in the @file, %FALSE otherwise.
4649 g_file_set_attribute_int32 (GFile *file,
4650 const gchar *attribute,
4652 GFileQueryInfoFlags flags,
4653 GCancellable *cancellable,
4656 return g_file_set_attribute (file, attribute,
4657 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4658 flags, cancellable, error);
4662 * g_file_set_attribute_uint64:
4663 * @file: input #GFile
4664 * @attribute: a string containing the attribute's name
4665 * @value: a #guint64 containing the attribute's new value
4666 * @flags: a #GFileQueryInfoFlags
4667 * @cancellable: (allow-none): optional #GCancellable object,
4669 * @error: a #GError, or %NULL
4671 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4672 * If @attribute is of a different type, this operation will fail.
4674 * If @cancellable is not %NULL, then the operation can be cancelled by
4675 * triggering the cancellable object from another thread. If the operation
4676 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4678 * Returns: %TRUE if the @attribute was successfully set to @value
4679 * in the @file, %FALSE otherwise.
4682 g_file_set_attribute_uint64 (GFile *file,
4683 const gchar *attribute,
4685 GFileQueryInfoFlags flags,
4686 GCancellable *cancellable,
4689 return g_file_set_attribute (file, attribute,
4690 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4691 flags, cancellable, error);
4695 * g_file_set_attribute_int64:
4696 * @file: input #GFile
4697 * @attribute: a string containing the attribute's name
4698 * @value: a #guint64 containing the attribute's new value
4699 * @flags: a #GFileQueryInfoFlags
4700 * @cancellable: (allow-none): optional #GCancellable object,
4702 * @error: a #GError, or %NULL
4704 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4705 * If @attribute is of a different type, this operation will fail.
4707 * If @cancellable is not %NULL, then the operation can be cancelled by
4708 * triggering the cancellable object from another thread. If the operation
4709 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4711 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4714 g_file_set_attribute_int64 (GFile *file,
4715 const gchar *attribute,
4717 GFileQueryInfoFlags flags,
4718 GCancellable *cancellable,
4721 return g_file_set_attribute (file, attribute,
4722 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4723 flags, cancellable, error);
4727 * g_file_mount_mountable:
4728 * @file: input #GFile
4729 * @flags: flags affecting the operation
4730 * @mount_operation: (allow-none): a #GMountOperation,
4731 * or %NULL to avoid user interaction
4732 * @cancellable: (allow-none): optional #GCancellable object,
4734 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4735 * when the request is satisfied, or %NULL
4736 * @user_data: (closure): the data to pass to callback function
4738 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4739 * Using @mount_operation, you can request callbacks when, for instance,
4740 * passwords are needed during authentication.
4742 * If @cancellable is not %NULL, then the operation can be cancelled by
4743 * triggering the cancellable object from another thread. If the operation
4744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4746 * When the operation is finished, @callback will be called.
4747 * You can then call g_file_mount_mountable_finish() to get
4748 * the result of the operation.
4751 g_file_mount_mountable (GFile *file,
4752 GMountMountFlags flags,
4753 GMountOperation *mount_operation,
4754 GCancellable *cancellable,
4755 GAsyncReadyCallback callback,
4760 g_return_if_fail (G_IS_FILE (file));
4762 iface = G_FILE_GET_IFACE (file);
4764 if (iface->mount_mountable == NULL)
4766 g_task_report_new_error (file, callback, user_data,
4767 g_file_mount_mountable,
4768 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4769 _("Operation not supported"));
4773 (* iface->mount_mountable) (file,
4782 * g_file_mount_mountable_finish:
4783 * @file: input #GFile
4784 * @result: a #GAsyncResult
4785 * @error: a #GError, or %NULL
4787 * Finishes a mount operation. See g_file_mount_mountable() for details.
4789 * Finish an asynchronous mount operation that was started
4790 * with g_file_mount_mountable().
4792 * Returns: (transfer full): a #GFile or %NULL on error.
4793 * Free the returned object with g_object_unref().
4796 g_file_mount_mountable_finish (GFile *file,
4797 GAsyncResult *result,
4802 g_return_val_if_fail (G_IS_FILE (file), NULL);
4803 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4805 if (g_async_result_legacy_propagate_error (result, error))
4807 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4808 return g_task_propagate_pointer (G_TASK (result), error);
4810 iface = G_FILE_GET_IFACE (file);
4811 return (* iface->mount_mountable_finish) (file, result, error);
4815 * g_file_unmount_mountable:
4816 * @file: input #GFile
4817 * @flags: flags affecting the operation
4818 * @cancellable: (allow-none): optional #GCancellable object,
4820 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4821 * when the request is satisfied, or %NULL
4822 * @user_data: (closure): the data to pass to callback function
4824 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4826 * If @cancellable is not %NULL, then the operation can be cancelled by
4827 * triggering the cancellable object from another thread. If the operation
4828 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4830 * When the operation is finished, @callback will be called.
4831 * You can then call g_file_unmount_mountable_finish() to get
4832 * the result of the operation.
4834 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4837 g_file_unmount_mountable (GFile *file,
4838 GMountUnmountFlags flags,
4839 GCancellable *cancellable,
4840 GAsyncReadyCallback callback,
4845 g_return_if_fail (G_IS_FILE (file));
4847 iface = G_FILE_GET_IFACE (file);
4849 if (iface->unmount_mountable == NULL)
4851 g_task_report_new_error (file, callback, user_data,
4852 g_file_unmount_mountable_with_operation,
4853 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4854 _("Operation not supported"));
4858 (* iface->unmount_mountable) (file,
4866 * g_file_unmount_mountable_finish:
4867 * @file: input #GFile
4868 * @result: a #GAsyncResult
4869 * @error: a #GError, or %NULL
4871 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4873 * Finish an asynchronous unmount operation that was started
4874 * with g_file_unmount_mountable().
4876 * Returns: %TRUE if the operation finished successfully.
4879 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4883 g_file_unmount_mountable_finish (GFile *file,
4884 GAsyncResult *result,
4889 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4890 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4892 if (g_async_result_legacy_propagate_error (result, error))
4894 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4895 return g_task_propagate_boolean (G_TASK (result), error);
4897 iface = G_FILE_GET_IFACE (file);
4898 return (* iface->unmount_mountable_finish) (file, result, error);
4902 * g_file_unmount_mountable_with_operation:
4903 * @file: input #GFile
4904 * @flags: flags affecting the operation
4905 * @mount_operation: (allow-none): a #GMountOperation,
4906 * or %NULL to avoid user interaction
4907 * @cancellable: (allow-none): optional #GCancellable object,
4909 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4910 * when the request is satisfied, or %NULL
4911 * @user_data: (closure): the data to pass to callback function
4913 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4915 * If @cancellable is not %NULL, then the operation can be cancelled by
4916 * triggering the cancellable object from another thread. If the operation
4917 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4919 * When the operation is finished, @callback will be called.
4920 * You can then call g_file_unmount_mountable_finish() to get
4921 * the result of the operation.
4926 g_file_unmount_mountable_with_operation (GFile *file,
4927 GMountUnmountFlags flags,
4928 GMountOperation *mount_operation,
4929 GCancellable *cancellable,
4930 GAsyncReadyCallback callback,
4935 g_return_if_fail (G_IS_FILE (file));
4937 iface = G_FILE_GET_IFACE (file);
4939 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4941 g_task_report_new_error (file, callback, user_data,
4942 g_file_unmount_mountable_with_operation,
4943 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4944 _("Operation not supported"));
4948 if (iface->unmount_mountable_with_operation != NULL)
4949 (* iface->unmount_mountable_with_operation) (file,
4956 (* iface->unmount_mountable) (file,
4964 * g_file_unmount_mountable_with_operation_finish:
4965 * @file: input #GFile
4966 * @result: a #GAsyncResult
4967 * @error: a #GError, or %NULL
4969 * Finishes an unmount operation,
4970 * see g_file_unmount_mountable_with_operation() for details.
4972 * Finish an asynchronous unmount operation that was started
4973 * with g_file_unmount_mountable_with_operation().
4975 * Returns: %TRUE if the operation finished successfully.
4981 g_file_unmount_mountable_with_operation_finish (GFile *file,
4982 GAsyncResult *result,
4987 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4988 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4990 if (g_async_result_legacy_propagate_error (result, error))
4992 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4993 return g_task_propagate_boolean (G_TASK (result), error);
4995 iface = G_FILE_GET_IFACE (file);
4996 if (iface->unmount_mountable_with_operation_finish != NULL)
4997 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4999 return (* iface->unmount_mountable_finish) (file, result, error);
5003 * g_file_eject_mountable:
5004 * @file: input #GFile
5005 * @flags: flags affecting the operation
5006 * @cancellable: (allow-none): optional #GCancellable object,
5008 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5009 * when the request is satisfied, or %NULL
5010 * @user_data: (closure): the data to pass to callback function
5012 * Starts an asynchronous eject on a mountable.
5013 * When this operation has completed, @callback will be called with
5014 * @user_user data, and the operation can be finalized with
5015 * g_file_eject_mountable_finish().
5017 * If @cancellable is not %NULL, then the operation can be cancelled by
5018 * triggering the cancellable object from another thread. If the operation
5019 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5021 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5024 g_file_eject_mountable (GFile *file,
5025 GMountUnmountFlags flags,
5026 GCancellable *cancellable,
5027 GAsyncReadyCallback callback,
5032 g_return_if_fail (G_IS_FILE (file));
5034 iface = G_FILE_GET_IFACE (file);
5036 if (iface->eject_mountable == NULL)
5038 g_task_report_new_error (file, callback, user_data,
5039 g_file_eject_mountable_with_operation,
5040 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5041 _("Operation not supported"));
5045 (* iface->eject_mountable) (file,
5053 * g_file_eject_mountable_finish:
5054 * @file: input #GFile
5055 * @result: a #GAsyncResult
5056 * @error: a #GError, or %NULL
5058 * Finishes an asynchronous eject operation started by
5059 * g_file_eject_mountable().
5061 * Returns: %TRUE if the @file was ejected successfully.
5064 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5068 g_file_eject_mountable_finish (GFile *file,
5069 GAsyncResult *result,
5074 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5075 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5077 if (g_async_result_legacy_propagate_error (result, error))
5079 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5080 return g_task_propagate_boolean (G_TASK (result), error);
5082 iface = G_FILE_GET_IFACE (file);
5083 return (* iface->eject_mountable_finish) (file, result, error);
5087 * g_file_eject_mountable_with_operation:
5088 * @file: input #GFile
5089 * @flags: flags affecting the operation
5090 * @mount_operation: (allow-none): a #GMountOperation,
5091 * or %NULL to avoid user interaction
5092 * @cancellable: (allow-none): optional #GCancellable object,
5094 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5095 * when the request is satisfied, or %NULL
5096 * @user_data: (closure): the data to pass to callback function
5098 * Starts an asynchronous eject on a mountable.
5099 * When this operation has completed, @callback will be called with
5100 * @user_user data, and the operation can be finalized with
5101 * g_file_eject_mountable_with_operation_finish().
5103 * If @cancellable is not %NULL, then the operation can be cancelled by
5104 * triggering the cancellable object from another thread. If the operation
5105 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5110 g_file_eject_mountable_with_operation (GFile *file,
5111 GMountUnmountFlags flags,
5112 GMountOperation *mount_operation,
5113 GCancellable *cancellable,
5114 GAsyncReadyCallback callback,
5119 g_return_if_fail (G_IS_FILE (file));
5121 iface = G_FILE_GET_IFACE (file);
5123 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5125 g_task_report_new_error (file, callback, user_data,
5126 g_file_eject_mountable_with_operation,
5127 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5128 _("Operation not supported"));
5132 if (iface->eject_mountable_with_operation != NULL)
5133 (* iface->eject_mountable_with_operation) (file,
5140 (* iface->eject_mountable) (file,
5148 * g_file_eject_mountable_with_operation_finish:
5149 * @file: input #GFile
5150 * @result: a #GAsyncResult
5151 * @error: a #GError, or %NULL
5153 * Finishes an asynchronous eject operation started by
5154 * g_file_eject_mountable_with_operation().
5156 * Returns: %TRUE if the @file was ejected successfully.
5162 g_file_eject_mountable_with_operation_finish (GFile *file,
5163 GAsyncResult *result,
5168 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5169 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5171 if (g_async_result_legacy_propagate_error (result, error))
5173 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5174 return g_task_propagate_boolean (G_TASK (result), error);
5176 iface = G_FILE_GET_IFACE (file);
5177 if (iface->eject_mountable_with_operation_finish != NULL)
5178 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5180 return (* iface->eject_mountable_finish) (file, result, error);
5184 * g_file_monitor_directory:
5185 * @file: input #GFile
5186 * @flags: a set of #GFileMonitorFlags
5187 * @cancellable: (allow-none): optional #GCancellable object,
5189 * @error: a #GError, or %NULL
5191 * Obtains a directory monitor for the given file.
5192 * This may fail if directory monitoring is not supported.
5194 * If @cancellable is not %NULL, then the operation can be cancelled by
5195 * triggering the cancellable object from another thread. If the operation
5196 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5198 * It does not make sense for @flags to contain
5199 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5200 * directories. It is not possible to monitor all the files in a
5201 * directory for changes made via hard links; if you want to do this then
5202 * you must register individual watches with g_file_monitor().
5204 * Virtual: monitor_dir
5205 * Returns: (transfer full): a #GFileMonitor for the given @file,
5206 * or %NULL on error.
5207 * Free the returned object with g_object_unref().
5210 g_file_monitor_directory (GFile *file,
5211 GFileMonitorFlags flags,
5212 GCancellable *cancellable,
5217 g_return_val_if_fail (G_IS_FILE (file), NULL);
5218 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5220 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5223 iface = G_FILE_GET_IFACE (file);
5225 if (iface->monitor_dir == NULL)
5227 g_set_error_literal (error, G_IO_ERROR,
5228 G_IO_ERROR_NOT_SUPPORTED,
5229 _("Operation not supported"));
5233 return (* iface->monitor_dir) (file, flags, cancellable, error);
5237 * g_file_monitor_file:
5238 * @file: input #GFile
5239 * @flags: a set of #GFileMonitorFlags
5240 * @cancellable: (allow-none): optional #GCancellable object,
5242 * @error: a #GError, or %NULL
5244 * Obtains a file monitor for the given file. If no file notification
5245 * mechanism exists, then regular polling of the file is used.
5247 * If @cancellable is not %NULL, then the operation can be cancelled by
5248 * triggering the cancellable object from another thread. If the operation
5249 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5251 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5252 * will also attempt to report changes made to the file via another
5253 * filename (ie, a hard link). Without this flag, you can only rely on
5254 * changes made through the filename contained in @file to be
5255 * reported. Using this flag may result in an increase in resource
5256 * usage, and may not have any effect depending on the #GFileMonitor
5257 * backend and/or filesystem type.
5259 * Returns: (transfer full): a #GFileMonitor for the given @file,
5260 * or %NULL on error.
5261 * Free the returned object with g_object_unref().
5264 g_file_monitor_file (GFile *file,
5265 GFileMonitorFlags flags,
5266 GCancellable *cancellable,
5270 GFileMonitor *monitor;
5272 g_return_val_if_fail (G_IS_FILE (file), NULL);
5274 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5277 iface = G_FILE_GET_IFACE (file);
5281 if (iface->monitor_file)
5282 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5284 /* Fallback to polling */
5285 if (monitor == NULL)
5286 monitor = _g_poll_file_monitor_new (file);
5293 * @file: input #GFile
5294 * @flags: a set of #GFileMonitorFlags
5295 * @cancellable: (allow-none): optional #GCancellable object,
5297 * @error: a #GError, or %NULL
5299 * Obtains a file or directory monitor for the given file,
5300 * depending on the type of the file.
5302 * If @cancellable is not %NULL, then the operation can be cancelled by
5303 * triggering the cancellable object from another thread. If the operation
5304 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5306 * Returns: (transfer full): a #GFileMonitor for the given @file,
5307 * or %NULL on error.
5308 * Free the returned object with g_object_unref().
5313 g_file_monitor (GFile *file,
5314 GFileMonitorFlags flags,
5315 GCancellable *cancellable,
5318 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5319 return g_file_monitor_directory (file,
5320 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5321 cancellable, error);
5323 return g_file_monitor_file (file, flags, cancellable, error);
5326 /********************************************
5327 * Default implementation of async ops *
5328 ********************************************/
5332 GFileQueryInfoFlags flags;
5333 } QueryInfoAsyncData;
5336 query_info_data_free (QueryInfoAsyncData *data)
5338 g_free (data->attributes);
5343 query_info_async_thread (GTask *task,
5346 GCancellable *cancellable)
5348 QueryInfoAsyncData *data = task_data;
5350 GError *error = NULL;
5352 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5354 g_task_return_pointer (task, info, g_object_unref);
5356 g_task_return_error (task, error);
5360 g_file_real_query_info_async (GFile *file,
5361 const char *attributes,
5362 GFileQueryInfoFlags flags,
5364 GCancellable *cancellable,
5365 GAsyncReadyCallback callback,
5369 QueryInfoAsyncData *data;
5371 data = g_new0 (QueryInfoAsyncData, 1);
5372 data->attributes = g_strdup (attributes);
5373 data->flags = flags;
5375 task = g_task_new (file, cancellable, callback, user_data);
5376 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5377 g_task_set_priority (task, io_priority);
5378 g_task_run_in_thread (task, query_info_async_thread);
5379 g_object_unref (task);
5383 g_file_real_query_info_finish (GFile *file,
5387 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5389 return g_task_propagate_pointer (G_TASK (res), error);
5393 query_filesystem_info_async_thread (GTask *task,
5396 GCancellable *cancellable)
5398 const char *attributes = task_data;
5400 GError *error = NULL;
5402 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5404 g_task_return_pointer (task, info, g_object_unref);
5406 g_task_return_error (task, error);
5410 g_file_real_query_filesystem_info_async (GFile *file,
5411 const char *attributes,
5413 GCancellable *cancellable,
5414 GAsyncReadyCallback callback,
5419 task = g_task_new (file, cancellable, callback, user_data);
5420 g_task_set_task_data (task, g_strdup (attributes), g_free);
5421 g_task_set_priority (task, io_priority);
5422 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5423 g_object_unref (task);
5427 g_file_real_query_filesystem_info_finish (GFile *file,
5431 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5433 return g_task_propagate_pointer (G_TASK (res), error);
5437 enumerate_children_async_thread (GTask *task,
5440 GCancellable *cancellable)
5442 QueryInfoAsyncData *data = task_data;
5443 GFileEnumerator *enumerator;
5444 GError *error = NULL;
5446 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5448 g_task_return_error (task, error);
5450 g_task_return_pointer (task, enumerator, g_object_unref);
5454 g_file_real_enumerate_children_async (GFile *file,
5455 const char *attributes,
5456 GFileQueryInfoFlags flags,
5458 GCancellable *cancellable,
5459 GAsyncReadyCallback callback,
5463 QueryInfoAsyncData *data;
5465 data = g_new0 (QueryInfoAsyncData, 1);
5466 data->attributes = g_strdup (attributes);
5467 data->flags = flags;
5469 task = g_task_new (file, cancellable, callback, user_data);
5470 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5471 g_task_set_priority (task, io_priority);
5472 g_task_run_in_thread (task, enumerate_children_async_thread);
5473 g_object_unref (task);
5476 static GFileEnumerator *
5477 g_file_real_enumerate_children_finish (GFile *file,
5481 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5483 return g_task_propagate_pointer (G_TASK (res), error);
5487 open_read_async_thread (GTask *task,
5490 GCancellable *cancellable)
5492 GFileInputStream *stream;
5493 GError *error = NULL;
5495 stream = g_file_read (G_FILE (object), cancellable, &error);
5497 g_task_return_pointer (task, stream, g_object_unref);
5499 g_task_return_error (task, error);
5503 g_file_real_read_async (GFile *file,
5505 GCancellable *cancellable,
5506 GAsyncReadyCallback callback,
5511 task = g_task_new (file, cancellable, callback, user_data);
5512 g_task_set_priority (task, io_priority);
5513 g_task_run_in_thread (task, open_read_async_thread);
5514 g_object_unref (task);
5517 static GFileInputStream *
5518 g_file_real_read_finish (GFile *file,
5522 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5524 return g_task_propagate_pointer (G_TASK (res), error);
5528 append_to_async_thread (GTask *task,
5529 gpointer source_object,
5531 GCancellable *cancellable)
5533 GFileCreateFlags *data = task_data;
5534 GFileOutputStream *stream;
5535 GError *error = NULL;
5537 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5539 g_task_return_pointer (task, stream, g_object_unref);
5541 g_task_return_error (task, error);
5545 g_file_real_append_to_async (GFile *file,
5546 GFileCreateFlags flags,
5548 GCancellable *cancellable,
5549 GAsyncReadyCallback callback,
5552 GFileCreateFlags *data;
5555 data = g_new0 (GFileCreateFlags, 1);
5558 task = g_task_new (file, cancellable, callback, user_data);
5559 g_task_set_task_data (task, data, g_free);
5560 g_task_set_priority (task, io_priority);
5562 g_task_run_in_thread (task, append_to_async_thread);
5563 g_object_unref (task);
5566 static GFileOutputStream *
5567 g_file_real_append_to_finish (GFile *file,
5571 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5573 return g_task_propagate_pointer (G_TASK (res), error);
5577 create_async_thread (GTask *task,
5578 gpointer source_object,
5580 GCancellable *cancellable)
5582 GFileCreateFlags *data = task_data;
5583 GFileOutputStream *stream;
5584 GError *error = NULL;
5586 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5588 g_task_return_pointer (task, stream, g_object_unref);
5590 g_task_return_error (task, error);
5594 g_file_real_create_async (GFile *file,
5595 GFileCreateFlags flags,
5597 GCancellable *cancellable,
5598 GAsyncReadyCallback callback,
5601 GFileCreateFlags *data;
5604 data = g_new0 (GFileCreateFlags, 1);
5607 task = g_task_new (file, cancellable, callback, user_data);
5608 g_task_set_task_data (task, data, g_free);
5609 g_task_set_priority (task, io_priority);
5611 g_task_run_in_thread (task, create_async_thread);
5612 g_object_unref (task);
5615 static GFileOutputStream *
5616 g_file_real_create_finish (GFile *file,
5620 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5622 return g_task_propagate_pointer (G_TASK (res), error);
5626 GFileOutputStream *stream;
5628 gboolean make_backup;
5629 GFileCreateFlags flags;
5633 replace_async_data_free (ReplaceAsyncData *data)
5636 g_object_unref (data->stream);
5637 g_free (data->etag);
5642 replace_async_thread (GTask *task,
5643 gpointer source_object,
5645 GCancellable *cancellable)
5647 GFileOutputStream *stream;
5648 ReplaceAsyncData *data = task_data;
5649 GError *error = NULL;
5651 stream = g_file_replace (G_FILE (source_object),
5659 g_task_return_pointer (task, stream, g_object_unref);
5661 g_task_return_error (task, error);
5665 g_file_real_replace_async (GFile *file,
5667 gboolean make_backup,
5668 GFileCreateFlags flags,
5670 GCancellable *cancellable,
5671 GAsyncReadyCallback callback,
5675 ReplaceAsyncData *data;
5677 data = g_new0 (ReplaceAsyncData, 1);
5678 data->etag = g_strdup (etag);
5679 data->make_backup = make_backup;
5680 data->flags = flags;
5682 task = g_task_new (file, cancellable, callback, user_data);
5683 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5684 g_task_set_priority (task, io_priority);
5686 g_task_run_in_thread (task, replace_async_thread);
5687 g_object_unref (task);
5690 static GFileOutputStream *
5691 g_file_real_replace_finish (GFile *file,
5695 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5697 return g_task_propagate_pointer (G_TASK (res), error);
5701 delete_async_thread (GTask *task,
5704 GCancellable *cancellable)
5706 GError *error = NULL;
5708 if (g_file_delete (G_FILE (object), cancellable, &error))
5709 g_task_return_boolean (task, TRUE);
5711 g_task_return_error (task, error);
5715 g_file_real_delete_async (GFile *file,
5717 GCancellable *cancellable,
5718 GAsyncReadyCallback callback,
5723 task = g_task_new (file, cancellable, callback, user_data);
5724 g_task_set_priority (task, io_priority);
5725 g_task_run_in_thread (task, delete_async_thread);
5726 g_object_unref (task);
5730 g_file_real_delete_finish (GFile *file,
5734 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5736 return g_task_propagate_boolean (G_TASK (res), error);
5740 trash_async_thread (GTask *task,
5743 GCancellable *cancellable)
5745 GError *error = NULL;
5747 if (g_file_trash (G_FILE (object), cancellable, &error))
5748 g_task_return_boolean (task, TRUE);
5750 g_task_return_error (task, error);
5754 g_file_real_trash_async (GFile *file,
5756 GCancellable *cancellable,
5757 GAsyncReadyCallback callback,
5762 task = g_task_new (file, cancellable, callback, user_data);
5763 g_task_set_priority (task, io_priority);
5764 g_task_run_in_thread (task, trash_async_thread);
5765 g_object_unref (task);
5769 g_file_real_trash_finish (GFile *file,
5773 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5775 return g_task_propagate_boolean (G_TASK (res), error);
5779 make_directory_async_thread (GTask *task,
5782 GCancellable *cancellable)
5784 GError *error = NULL;
5786 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5787 g_task_return_boolean (task, TRUE);
5789 g_task_return_error (task, error);
5793 g_file_real_make_directory_async (GFile *file,
5795 GCancellable *cancellable,
5796 GAsyncReadyCallback callback,
5801 task = g_task_new (file, cancellable, callback, user_data);
5802 g_task_set_priority (task, io_priority);
5803 g_task_run_in_thread (task, make_directory_async_thread);
5804 g_object_unref (task);
5808 g_file_real_make_directory_finish (GFile *file,
5812 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5814 return g_task_propagate_boolean (G_TASK (res), error);
5818 open_readwrite_async_thread (GTask *task,
5821 GCancellable *cancellable)
5823 GFileIOStream *stream;
5824 GError *error = NULL;
5826 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5829 g_task_return_error (task, error);
5831 g_task_return_pointer (task, stream, g_object_unref);
5835 g_file_real_open_readwrite_async (GFile *file,
5837 GCancellable *cancellable,
5838 GAsyncReadyCallback callback,
5843 task = g_task_new (file, cancellable, callback, user_data);
5844 g_task_set_priority (task, io_priority);
5846 g_task_run_in_thread (task, open_readwrite_async_thread);
5847 g_object_unref (task);
5850 static GFileIOStream *
5851 g_file_real_open_readwrite_finish (GFile *file,
5855 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5857 return g_task_propagate_pointer (G_TASK (res), error);
5861 create_readwrite_async_thread (GTask *task,
5864 GCancellable *cancellable)
5866 GFileCreateFlags *data = task_data;
5867 GFileIOStream *stream;
5868 GError *error = NULL;
5870 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5873 g_task_return_error (task, error);
5875 g_task_return_pointer (task, stream, g_object_unref);
5879 g_file_real_create_readwrite_async (GFile *file,
5880 GFileCreateFlags flags,
5882 GCancellable *cancellable,
5883 GAsyncReadyCallback callback,
5886 GFileCreateFlags *data;
5889 data = g_new0 (GFileCreateFlags, 1);
5892 task = g_task_new (file, cancellable, callback, user_data);
5893 g_task_set_task_data (task, data, g_free);
5894 g_task_set_priority (task, io_priority);
5896 g_task_run_in_thread (task, create_readwrite_async_thread);
5897 g_object_unref (task);
5900 static GFileIOStream *
5901 g_file_real_create_readwrite_finish (GFile *file,
5905 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5907 return g_task_propagate_pointer (G_TASK (res), error);
5912 gboolean make_backup;
5913 GFileCreateFlags flags;
5914 } ReplaceRWAsyncData;
5917 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5919 g_free (data->etag);
5924 replace_readwrite_async_thread (GTask *task,
5927 GCancellable *cancellable)
5929 GFileIOStream *stream;
5930 GError *error = NULL;
5931 ReplaceRWAsyncData *data = task_data;
5933 stream = g_file_replace_readwrite (G_FILE (object),
5941 g_task_return_error (task, error);
5943 g_task_return_pointer (task, stream, g_object_unref);
5947 g_file_real_replace_readwrite_async (GFile *file,
5949 gboolean make_backup,
5950 GFileCreateFlags flags,
5952 GCancellable *cancellable,
5953 GAsyncReadyCallback callback,
5957 ReplaceRWAsyncData *data;
5959 data = g_new0 (ReplaceRWAsyncData, 1);
5960 data->etag = g_strdup (etag);
5961 data->make_backup = make_backup;
5962 data->flags = flags;
5964 task = g_task_new (file, cancellable, callback, user_data);
5965 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5966 g_task_set_priority (task, io_priority);
5968 g_task_run_in_thread (task, replace_readwrite_async_thread);
5969 g_object_unref (task);
5972 static GFileIOStream *
5973 g_file_real_replace_readwrite_finish (GFile *file,
5977 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5979 return g_task_propagate_pointer (G_TASK (res), error);
5983 set_display_name_async_thread (GTask *task,
5986 GCancellable *cancellable)
5988 GError *error = NULL;
5989 char *name = task_data;
5992 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5995 g_task_return_error (task, error);
5997 g_task_return_pointer (task, file, g_object_unref);
6001 g_file_real_set_display_name_async (GFile *file,
6002 const char *display_name,
6004 GCancellable *cancellable,
6005 GAsyncReadyCallback callback,
6010 task = g_task_new (file, cancellable, callback, user_data);
6011 g_task_set_task_data (task, g_strdup (display_name), g_free);
6012 g_task_set_priority (task, io_priority);
6014 g_task_run_in_thread (task, set_display_name_async_thread);
6015 g_object_unref (task);
6019 g_file_real_set_display_name_finish (GFile *file,
6023 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6025 return g_task_propagate_pointer (G_TASK (res), error);
6029 GFileQueryInfoFlags flags;
6036 set_info_data_free (SetInfoAsyncData *data)
6039 g_object_unref (data->info);
6041 g_error_free (data->error);
6046 set_info_async_thread (GTask *task,
6049 GCancellable *cancellable)
6051 SetInfoAsyncData *data = task_data;
6054 data->res = g_file_set_attributes_from_info (G_FILE (object),
6062 g_file_real_set_attributes_async (GFile *file,
6064 GFileQueryInfoFlags flags,
6066 GCancellable *cancellable,
6067 GAsyncReadyCallback callback,
6071 SetInfoAsyncData *data;
6073 data = g_new0 (SetInfoAsyncData, 1);
6074 data->info = g_file_info_dup (info);
6075 data->flags = flags;
6077 task = g_task_new (file, cancellable, callback, user_data);
6078 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6079 g_task_set_priority (task, io_priority);
6081 g_task_run_in_thread (task, set_info_async_thread);
6082 g_object_unref (task);
6086 g_file_real_set_attributes_finish (GFile *file,
6091 SetInfoAsyncData *data;
6093 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6095 data = g_task_get_task_data (G_TASK (res));
6098 *info = g_object_ref (data->info);
6100 if (error != NULL && data->error)
6101 *error = g_error_copy (data->error);
6107 find_enclosing_mount_async_thread (GTask *task,
6110 GCancellable *cancellable)
6112 GError *error = NULL;
6115 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6118 g_task_return_error (task, error);
6120 g_task_return_pointer (task, mount, g_object_unref);
6124 g_file_real_find_enclosing_mount_async (GFile *file,
6126 GCancellable *cancellable,
6127 GAsyncReadyCallback callback,
6132 task = g_task_new (file, cancellable, callback, user_data);
6133 g_task_set_priority (task, io_priority);
6135 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6136 g_object_unref (task);
6140 g_file_real_find_enclosing_mount_finish (GFile *file,
6144 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6146 return g_task_propagate_pointer (G_TASK (res), error);
6153 GFileCopyFlags flags;
6154 GFileProgressCallback progress_cb;
6155 gpointer progress_cb_data;
6159 copy_async_data_free (CopyAsyncData *data)
6161 g_object_unref (data->source);
6162 g_object_unref (data->destination);
6163 g_slice_free (CopyAsyncData, data);
6167 CopyAsyncData *data;
6168 goffset current_num_bytes;
6169 goffset total_num_bytes;
6173 copy_async_progress_in_main (gpointer user_data)
6175 ProgressData *progress = user_data;
6176 CopyAsyncData *data = progress->data;
6178 data->progress_cb (progress->current_num_bytes,
6179 progress->total_num_bytes,
6180 data->progress_cb_data);
6186 copy_async_progress_callback (goffset current_num_bytes,
6187 goffset total_num_bytes,
6190 GTask *task = user_data;
6191 CopyAsyncData *data = g_task_get_task_data (task);
6192 ProgressData *progress;
6194 progress = g_new (ProgressData, 1);
6195 progress->data = data;
6196 progress->current_num_bytes = current_num_bytes;
6197 progress->total_num_bytes = total_num_bytes;
6199 g_main_context_invoke_full (g_task_get_context (task),
6200 g_task_get_priority (task),
6201 copy_async_progress_in_main,
6207 copy_async_thread (GTask *task,
6210 GCancellable *cancellable)
6212 CopyAsyncData *data = task_data;
6214 GError *error = NULL;
6216 result = g_file_copy (data->source,
6220 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6224 g_task_return_boolean (task, TRUE);
6226 g_task_return_error (task, error);
6230 g_file_real_copy_async (GFile *source,
6232 GFileCopyFlags flags,
6234 GCancellable *cancellable,
6235 GFileProgressCallback progress_callback,
6236 gpointer progress_callback_data,
6237 GAsyncReadyCallback callback,
6241 CopyAsyncData *data;
6243 data = g_slice_new (CopyAsyncData);
6244 data->source = g_object_ref (source);
6245 data->destination = g_object_ref (destination);
6246 data->flags = flags;
6247 data->progress_cb = progress_callback;
6248 data->progress_cb_data = progress_callback_data;
6250 task = g_task_new (source, cancellable, callback, user_data);
6251 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6252 g_task_set_priority (task, io_priority);
6253 g_task_run_in_thread (task, copy_async_thread);
6254 g_object_unref (task);
6258 g_file_real_copy_finish (GFile *file,
6262 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6264 return g_task_propagate_boolean (G_TASK (res), error);
6268 /********************************************
6269 * Default VFS operations *
6270 ********************************************/
6273 * g_file_new_for_path:
6274 * @path: a string containing a relative or absolute path.
6275 * The string must be encoded in the glib filename encoding.
6277 * Constructs a #GFile for a given path. This operation never
6278 * fails, but the returned object might not support any I/O
6279 * operation if @path is malformed.
6281 * Returns: (transfer full): a new #GFile for the given @path.
6282 * Free the returned object with g_object_unref().
6285 g_file_new_for_path (const char *path)
6287 g_return_val_if_fail (path != NULL, NULL);
6289 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6293 * g_file_new_for_uri:
6294 * @uri: a UTF-8 string containing a URI
6296 * Constructs a #GFile for a given URI. This operation never
6297 * fails, but the returned object might not support any I/O
6298 * operation if @uri is malformed or if the uri type is
6301 * Returns: (transfer full): a new #GFile for the given @uri.
6302 * Free the returned object with g_object_unref().
6305 g_file_new_for_uri (const char *uri)
6307 g_return_val_if_fail (uri != NULL, NULL);
6309 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6314 * @tmpl: (type filename) (allow-none): Template for the file
6315 * name, as in g_file_open_tmp(), or %NULL for a default template
6316 * @iostream: (out): on return, a #GFileIOStream for the created file
6317 * @error: a #GError, or %NULL
6319 * Opens a file in the preferred directory for temporary files (as
6320 * returned by g_get_tmp_dir()) and returns a #GFile and
6321 * #GFileIOStream pointing to it.
6323 * @tmpl should be a string in the GLib file name encoding
6324 * containing a sequence of six 'X' characters, and containing no
6325 * directory components. If it is %NULL, a default template is used.
6327 * Unlike the other #GFile constructors, this will return %NULL if
6328 * a temporary file could not be created.
6330 * Returns: (transfer full): a new #GFile.
6331 * Free the returned object with g_object_unref().
6336 g_file_new_tmp (const char *tmpl,
6337 GFileIOStream **iostream,
6343 GFileOutputStream *output;
6345 g_return_val_if_fail (iostream != NULL, NULL);
6347 fd = g_file_open_tmp (tmpl, &path, error);
6351 file = g_file_new_for_path (path);
6353 output = _g_local_file_output_stream_new (fd);
6354 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6356 g_object_unref (output);
6363 * g_file_parse_name:
6364 * @parse_name: a file name or path to be parsed
6366 * Constructs a #GFile with the given @parse_name (i.e. something
6367 * given by g_file_get_parse_name()). This operation never fails,
6368 * but the returned object might not support any I/O operation if
6369 * the @parse_name cannot be parsed.
6371 * Returns: (transfer full): a new #GFile.
6374 g_file_parse_name (const char *parse_name)
6376 g_return_val_if_fail (parse_name != NULL, NULL);
6378 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6382 is_valid_scheme_character (char c)
6384 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6387 /* Following RFC 2396, valid schemes are built like:
6388 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6391 has_valid_scheme (const char *uri)
6397 if (!g_ascii_isalpha (*p))
6402 } while (is_valid_scheme_character (*p));
6408 new_for_cmdline_arg (const gchar *arg,
6414 if (g_path_is_absolute (arg))
6415 return g_file_new_for_path (arg);
6417 if (has_valid_scheme (arg))
6418 return g_file_new_for_uri (arg);
6424 current_dir = g_get_current_dir ();
6425 filename = g_build_filename (current_dir, arg, NULL);
6426 g_free (current_dir);
6429 filename = g_build_filename (cwd, arg, NULL);
6431 file = g_file_new_for_path (filename);
6438 * g_file_new_for_commandline_arg:
6439 * @arg: a command line string
6441 * Creates a #GFile with the given argument from the command line.
6442 * The value of @arg can be either a URI, an absolute path or a
6443 * relative path resolved relative to the current working directory.
6444 * This operation never fails, but the returned object might not
6445 * support any I/O operation if @arg points to a malformed path.
6447 * Returns: (transfer full): a new #GFile.
6448 * Free the returned object with g_object_unref().
6451 g_file_new_for_commandline_arg (const char *arg)
6453 g_return_val_if_fail (arg != NULL, NULL);
6455 return new_for_cmdline_arg (arg, NULL);
6459 * g_file_new_for_commandline_arg_and_cwd:
6460 * @arg: a command line string
6461 * @cwd: the current working directory of the commandline
6463 * Creates a #GFile with the given argument from the command line.
6465 * This function is similar to g_file_new_for_commandline_arg() except
6466 * that it allows for passing the current working directory as an
6467 * argument instead of using the current working directory of the
6470 * This is useful if the commandline argument was given in a context
6471 * other than the invocation of the current process.
6473 * See also g_application_command_line_create_file_for_arg().
6475 * Returns: (transfer full): a new #GFile
6480 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6483 g_return_val_if_fail (arg != NULL, NULL);
6484 g_return_val_if_fail (cwd != NULL, NULL);
6486 return new_for_cmdline_arg (arg, cwd);
6490 * g_file_mount_enclosing_volume:
6491 * @location: input #GFile
6492 * @flags: flags affecting the operation
6493 * @mount_operation: (allow-none): a #GMountOperation
6494 * or %NULL to avoid user interaction
6495 * @cancellable: (allow-none): optional #GCancellable object,
6497 * @callback: (allow-none): a #GAsyncReadyCallback to call
6498 * when the request is satisfied, or %NULL
6499 * @user_data: the data to pass to callback function
6501 * Starts a @mount_operation, mounting the volume that contains
6502 * the file @location.
6504 * When this operation has completed, @callback will be called with
6505 * @user_user data, and the operation can be finalized with
6506 * g_file_mount_enclosing_volume_finish().
6508 * If @cancellable is not %NULL, then the operation can be cancelled by
6509 * triggering the cancellable object from another thread. If the operation
6510 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6513 g_file_mount_enclosing_volume (GFile *location,
6514 GMountMountFlags flags,
6515 GMountOperation *mount_operation,
6516 GCancellable *cancellable,
6517 GAsyncReadyCallback callback,
6522 g_return_if_fail (G_IS_FILE (location));
6524 iface = G_FILE_GET_IFACE (location);
6526 if (iface->mount_enclosing_volume == NULL)
6528 g_task_report_new_error (location, callback, user_data,
6529 g_file_mount_enclosing_volume,
6530 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6531 _("volume doesn't implement mount"));
6535 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6540 * g_file_mount_enclosing_volume_finish:
6541 * @location: input #GFile
6542 * @result: a #GAsyncResult
6543 * @error: a #GError, or %NULL
6545 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6547 * Returns: %TRUE if successful. If an error has occurred,
6548 * this function will return %FALSE and set @error
6549 * appropriately if present.
6552 g_file_mount_enclosing_volume_finish (GFile *location,
6553 GAsyncResult *result,
6558 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6559 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6561 if (g_async_result_legacy_propagate_error (result, error))
6563 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6564 return g_task_propagate_boolean (G_TASK (result), error);
6566 iface = G_FILE_GET_IFACE (location);
6568 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6571 /********************************************
6572 * Utility functions *
6573 ********************************************/
6576 * g_file_query_default_handler:
6577 * @file: a #GFile to open
6578 * @cancellable: optional #GCancellable object, %NULL to ignore
6579 * @error: a #GError, or %NULL
6581 * Returns the #GAppInfo that is registered as the default
6582 * application to handle the file specified by @file.
6584 * If @cancellable is not %NULL, then the operation can be cancelled by
6585 * triggering the cancellable object from another thread. If the operation
6586 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6588 * Returns: (transfer full): a #GAppInfo if the handle was found,
6589 * %NULL if there were errors.
6590 * When you are done with it, release it with g_object_unref()
6593 g_file_query_default_handler (GFile *file,
6594 GCancellable *cancellable,
6598 const char *content_type;
6603 uri_scheme = g_file_get_uri_scheme (file);
6604 if (uri_scheme && uri_scheme[0] != '\0')
6606 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6607 g_free (uri_scheme);
6609 if (appinfo != NULL)
6613 info = g_file_query_info (file,
6614 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6623 content_type = g_file_info_get_content_type (info);
6626 /* Don't use is_native(), as we want to support fuse paths if available */
6627 path = g_file_get_path (file);
6628 appinfo = g_app_info_get_default_for_type (content_type,
6633 g_object_unref (info);
6635 if (appinfo != NULL)
6638 g_set_error_literal (error, G_IO_ERROR,
6639 G_IO_ERROR_NOT_SUPPORTED,
6640 _("No application is registered as handling this file"));
6644 #define GET_CONTENT_BLOCK_SIZE 8192
6647 * g_file_load_contents:
6648 * @file: input #GFile
6649 * @cancellable: optional #GCancellable object, %NULL to ignore
6650 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6651 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6652 * or %NULL if the length is not needed
6653 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6654 * or %NULL if the entity tag is not needed
6655 * @error: a #GError, or %NULL
6657 * Loads the content of the file into memory. The data is always
6658 * zero-terminated, but this is not included in the resultant @length.
6659 * The returned @content should be freed with g_free() when no longer
6662 * If @cancellable is not %NULL, then the operation can be cancelled by
6663 * triggering the cancellable object from another thread. If the operation
6664 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6666 * Returns: %TRUE if the @file's contents were successfully loaded.
6667 * %FALSE if there were errors.
6670 g_file_load_contents (GFile *file,
6671 GCancellable *cancellable,
6677 GFileInputStream *in;
6678 GByteArray *content;
6683 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6684 g_return_val_if_fail (contents != NULL, FALSE);
6686 in = g_file_read (file, cancellable, error);
6690 content = g_byte_array_new ();
6693 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6694 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6695 content->data + pos,
6696 GET_CONTENT_BLOCK_SIZE,
6697 cancellable, error)) > 0)
6700 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6707 info = g_file_input_stream_query_info (in,
6708 G_FILE_ATTRIBUTE_ETAG_VALUE,
6713 *etag_out = g_strdup (g_file_info_get_etag (info));
6714 g_object_unref (info);
6718 /* Ignore errors on close */
6719 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6720 g_object_unref (in);
6724 /* error is set already */
6725 g_byte_array_free (content, TRUE);
6732 /* Zero terminate (we got an extra byte allocated for this */
6733 content->data[pos] = 0;
6735 *contents = (char *)g_byte_array_free (content, FALSE);
6742 GFileReadMoreCallback read_more_callback;
6743 GByteArray *content;
6750 load_contents_data_free (LoadContentsData *data)
6753 g_byte_array_free (data->content, TRUE);
6754 g_free (data->etag);
6759 load_contents_close_callback (GObject *obj,
6760 GAsyncResult *close_res,
6763 GInputStream *stream = G_INPUT_STREAM (obj);
6764 LoadContentsData *data = user_data;
6766 /* Ignore errors here, we're only reading anyway */
6767 g_input_stream_close_finish (stream, close_res, NULL);
6768 g_object_unref (stream);
6770 g_task_return_boolean (data->task, TRUE);
6771 g_object_unref (data->task);
6775 load_contents_fstat_callback (GObject *obj,
6776 GAsyncResult *stat_res,
6779 GInputStream *stream = G_INPUT_STREAM (obj);
6780 LoadContentsData *data = user_data;
6783 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6787 data->etag = g_strdup (g_file_info_get_etag (info));
6788 g_object_unref (info);
6791 g_input_stream_close_async (stream, 0,
6792 g_task_get_cancellable (data->task),
6793 load_contents_close_callback, data);
6797 load_contents_read_callback (GObject *obj,
6798 GAsyncResult *read_res,
6801 GInputStream *stream = G_INPUT_STREAM (obj);
6802 LoadContentsData *data = user_data;
6803 GError *error = NULL;
6806 read_size = g_input_stream_read_finish (stream, read_res, &error);
6810 g_task_return_error (data->task, error);
6811 g_object_unref (data->task);
6813 /* Close the file ignoring any error */
6814 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6815 g_object_unref (stream);
6817 else if (read_size == 0)
6819 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6820 G_FILE_ATTRIBUTE_ETAG_VALUE,
6822 g_task_get_cancellable (data->task),
6823 load_contents_fstat_callback,
6826 else if (read_size > 0)
6828 data->pos += read_size;
6830 g_byte_array_set_size (data->content,
6831 data->pos + GET_CONTENT_BLOCK_SIZE);
6834 if (data->read_more_callback &&
6835 !data->read_more_callback ((char *)data->content->data, data->pos,
6836 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6837 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6838 G_FILE_ATTRIBUTE_ETAG_VALUE,
6840 g_task_get_cancellable (data->task),
6841 load_contents_fstat_callback,
6844 g_input_stream_read_async (stream,
6845 data->content->data + data->pos,
6846 GET_CONTENT_BLOCK_SIZE,
6848 g_task_get_cancellable (data->task),
6849 load_contents_read_callback,
6855 load_contents_open_callback (GObject *obj,
6856 GAsyncResult *open_res,
6859 GFile *file = G_FILE (obj);
6860 GFileInputStream *stream;
6861 LoadContentsData *data = user_data;
6862 GError *error = NULL;
6864 stream = g_file_read_finish (file, open_res, &error);
6868 g_byte_array_set_size (data->content,
6869 data->pos + GET_CONTENT_BLOCK_SIZE);
6870 g_input_stream_read_async (G_INPUT_STREAM (stream),
6871 data->content->data + data->pos,
6872 GET_CONTENT_BLOCK_SIZE,
6874 g_task_get_cancellable (data->task),
6875 load_contents_read_callback,
6880 g_task_return_error (data->task, error);
6881 g_object_unref (data->task);
6886 * g_file_load_partial_contents_async: (skip)
6887 * @file: input #GFile
6888 * @cancellable: optional #GCancellable object, %NULL to ignore
6889 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6890 * and to specify whether further data should be read
6891 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6892 * @user_data: the data to pass to the callback functions
6894 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6895 * be used to stop reading from the file when appropriate, else this
6896 * function will behave exactly as g_file_load_contents_async(). This
6897 * operation can be finished by g_file_load_partial_contents_finish().
6899 * Users of this function should be aware that @user_data is passed to
6900 * both the @read_more_callback and the @callback.
6902 * If @cancellable is not %NULL, then the operation can be cancelled by
6903 * triggering the cancellable object from another thread. If the operation
6904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6907 g_file_load_partial_contents_async (GFile *file,
6908 GCancellable *cancellable,
6909 GFileReadMoreCallback read_more_callback,
6910 GAsyncReadyCallback callback,
6913 LoadContentsData *data;
6915 g_return_if_fail (G_IS_FILE (file));
6917 data = g_new0 (LoadContentsData, 1);
6918 data->read_more_callback = read_more_callback;
6919 data->content = g_byte_array_new ();
6921 data->task = g_task_new (file, cancellable, callback, user_data);
6922 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6924 g_file_read_async (file,
6926 g_task_get_cancellable (data->task),
6927 load_contents_open_callback,
6932 * g_file_load_partial_contents_finish:
6933 * @file: input #GFile
6934 * @res: a #GAsyncResult
6935 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6936 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6937 * or %NULL if the length is not needed
6938 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6939 * or %NULL if the entity tag is not needed
6940 * @error: a #GError, or %NULL
6942 * Finishes an asynchronous partial load operation that was started
6943 * with g_file_load_partial_contents_async(). The data is always
6944 * zero-terminated, but this is not included in the resultant @length.
6945 * The returned @content should be freed with g_free() when no longer
6948 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6949 * present, it will be set appropriately.
6952 g_file_load_partial_contents_finish (GFile *file,
6960 LoadContentsData *data;
6962 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6963 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6964 g_return_val_if_fail (contents != NULL, FALSE);
6966 task = G_TASK (res);
6968 if (!g_task_propagate_boolean (task, error))
6975 data = g_task_get_task_data (task);
6978 *length = data->pos;
6982 *etag_out = data->etag;
6986 /* Zero terminate */
6987 g_byte_array_set_size (data->content, data->pos + 1);
6988 data->content->data[data->pos] = 0;
6990 *contents = (char *)g_byte_array_free (data->content, FALSE);
6991 data->content = NULL;
6997 * g_file_load_contents_async:
6998 * @file: input #GFile
6999 * @cancellable: optional #GCancellable object, %NULL to ignore
7000 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7001 * @user_data: the data to pass to callback function
7003 * Starts an asynchronous load of the @file's contents.
7005 * For more details, see g_file_load_contents() which is
7006 * the synchronous version of this call.
7008 * When the load operation has completed, @callback will be called
7009 * with @user data. To finish the operation, call
7010 * g_file_load_contents_finish() with the #GAsyncResult returned by
7013 * If @cancellable is not %NULL, then the operation can be cancelled by
7014 * triggering the cancellable object from another thread. If the operation
7015 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7018 g_file_load_contents_async (GFile *file,
7019 GCancellable *cancellable,
7020 GAsyncReadyCallback callback,
7023 g_file_load_partial_contents_async (file,
7026 callback, user_data);
7030 * g_file_load_contents_finish:
7031 * @file: input #GFile
7032 * @res: a #GAsyncResult
7033 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7034 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7035 * or %NULL if the length is not needed
7036 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7037 * or %NULL if the entity tag is not needed
7038 * @error: a #GError, or %NULL
7040 * Finishes an asynchronous load of the @file's contents.
7041 * The contents are placed in @contents, and @length is set to the
7042 * size of the @contents string. The @content should be freed with
7043 * g_free() when no longer needed. If @etag_out is present, it will be
7044 * set to the new entity tag for the @file.
7046 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7047 * present, it will be set appropriately.
7050 g_file_load_contents_finish (GFile *file,
7057 return g_file_load_partial_contents_finish (file,
7066 * g_file_replace_contents:
7067 * @file: input #GFile
7068 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7069 * @length: the length of @contents in bytes
7070 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
7071 * for the document, or %NULL
7072 * @make_backup: %TRUE if a backup should be created
7073 * @flags: a set of #GFileCreateFlags
7074 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
7075 * for the document. This should be freed with g_free() when no longer
7077 * @cancellable: optional #GCancellable object, %NULL to ignore
7078 * @error: a #GError, or %NULL
7080 * Replaces the contents of @file with @contents of @length bytes.
7082 * If @etag is specified (not %NULL), any existing file must have that etag,
7083 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7085 * If @make_backup is %TRUE, this function will attempt to make a backup
7088 * If @cancellable is not %NULL, then the operation can be cancelled by
7089 * triggering the cancellable object from another thread. If the operation
7090 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7092 * The returned @new_etag can be used to verify that the file hasn't
7093 * changed the next time it is saved over.
7095 * Returns: %TRUE if successful. If an error has occurred, this function
7096 * will return %FALSE and set @error appropriately if present.
7099 g_file_replace_contents (GFile *file,
7100 const char *contents,
7103 gboolean make_backup,
7104 GFileCreateFlags flags,
7106 GCancellable *cancellable,
7109 GFileOutputStream *out;
7110 gsize pos, remainder;
7114 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7115 g_return_val_if_fail (contents != NULL, FALSE);
7117 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7123 while (remainder > 0 &&
7124 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7126 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7134 if (remainder > 0 && res < 0)
7136 /* Ignore errors on close */
7137 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7138 g_object_unref (out);
7140 /* error is set already */
7144 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7147 *new_etag = g_file_output_stream_get_etag (out);
7149 g_object_unref (out);
7156 const char *content;
7161 } ReplaceContentsData;
7164 replace_contents_data_free (ReplaceContentsData *data)
7166 g_free (data->etag);
7171 replace_contents_close_callback (GObject *obj,
7172 GAsyncResult *close_res,
7175 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7176 ReplaceContentsData *data = user_data;
7178 /* Ignore errors here, we're only reading anyway */
7179 g_output_stream_close_finish (stream, close_res, NULL);
7180 g_object_unref (stream);
7184 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7185 g_task_return_boolean (data->task, TRUE);
7187 g_object_unref (data->task);
7191 replace_contents_write_callback (GObject *obj,
7192 GAsyncResult *read_res,
7195 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7196 ReplaceContentsData *data = user_data;
7197 GError *error = NULL;
7200 write_size = g_output_stream_write_finish (stream, read_res, &error);
7202 if (write_size <= 0)
7204 /* Error or EOF, close the file */
7207 data->failed = TRUE;
7208 g_task_return_error (data->task, error);
7210 g_output_stream_close_async (stream, 0,
7211 g_task_get_cancellable (data->task),
7212 replace_contents_close_callback, data);
7214 else if (write_size > 0)
7216 data->pos += write_size;
7218 if (data->pos >= data->length)
7219 g_output_stream_close_async (stream, 0,
7220 g_task_get_cancellable (data->task),
7221 replace_contents_close_callback, data);
7223 g_output_stream_write_async (stream,
7224 data->content + data->pos,
7225 data->length - data->pos,
7227 g_task_get_cancellable (data->task),
7228 replace_contents_write_callback,
7234 replace_contents_open_callback (GObject *obj,
7235 GAsyncResult *open_res,
7238 GFile *file = G_FILE (obj);
7239 GFileOutputStream *stream;
7240 ReplaceContentsData *data = user_data;
7241 GError *error = NULL;
7243 stream = g_file_replace_finish (file, open_res, &error);
7247 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7248 data->content + data->pos,
7249 data->length - data->pos,
7251 g_task_get_cancellable (data->task),
7252 replace_contents_write_callback,
7257 g_task_return_error (data->task, error);
7258 g_object_unref (data->task);
7263 * g_file_replace_contents_async:
7264 * @file: input #GFile
7265 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7266 * @length: the length of @contents in bytes
7267 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
7268 * @make_backup: %TRUE if a backup should be created
7269 * @flags: a set of #GFileCreateFlags
7270 * @cancellable: optional #GCancellable object, %NULL to ignore
7271 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7272 * @user_data: the data to pass to callback function
7274 * Starts an asynchronous replacement of @file with the given
7275 * @contents of @length bytes. @etag will replace the document's
7276 * current entity tag.
7278 * When this operation has completed, @callback will be called with
7279 * @user_user data, and the operation can be finalized with
7280 * g_file_replace_contents_finish().
7282 * If @cancellable is not %NULL, then the operation can be cancelled by
7283 * triggering the cancellable object from another thread. If the operation
7284 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7286 * If @make_backup is %TRUE, this function will attempt to
7287 * make a backup of @file.
7290 g_file_replace_contents_async (GFile *file,
7291 const char *contents,
7294 gboolean make_backup,
7295 GFileCreateFlags flags,
7296 GCancellable *cancellable,
7297 GAsyncReadyCallback callback,
7300 ReplaceContentsData *data;
7302 g_return_if_fail (G_IS_FILE (file));
7303 g_return_if_fail (contents != NULL);
7305 data = g_new0 (ReplaceContentsData, 1);
7307 data->content = contents;
7308 data->length = length;
7310 data->task = g_task_new (file, cancellable, callback, user_data);
7311 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7313 g_file_replace_async (file,
7318 g_task_get_cancellable (data->task),
7319 replace_contents_open_callback,
7324 * g_file_replace_contents_finish:
7325 * @file: input #GFile
7326 * @res: a #GAsyncResult
7327 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
7328 * for the document. This should be freed with g_free() when it is no
7329 * longer needed, or %NULL
7330 * @error: a #GError, or %NULL
7332 * Finishes an asynchronous replace of the given @file. See
7333 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7334 * tag for the document, if present.
7336 * Returns: %TRUE on success, %FALSE on failure.
7339 g_file_replace_contents_finish (GFile *file,
7345 ReplaceContentsData *data;
7347 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7348 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7350 task = G_TASK (res);
7352 if (!g_task_propagate_boolean (task, error))
7355 data = g_task_get_task_data (task);
7359 *new_etag = data->etag;
7360 data->etag = NULL; /* Take ownership */
7367 g_file_real_measure_disk_usage (GFile *file,
7368 GFileMeasureFlags flags,
7369 GCancellable *cancellable,
7370 GFileMeasureProgressCallback progress_callback,
7371 gpointer progress_data,
7372 guint64 *disk_usage,
7377 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7378 "Operation not supported for the current backend.");
7384 GFileMeasureFlags flags;
7385 GFileMeasureProgressCallback progress_callback;
7386 gpointer progress_data;
7398 GFileMeasureProgressCallback callback;
7401 guint64 current_size;
7407 measure_disk_usage_invoke_progress (gpointer user_data)
7409 MeasureProgress *progress = user_data;
7411 (* progress->callback) (progress->reporting,
7412 progress->current_size, progress->num_dirs, progress->num_files,
7413 progress->user_data);
7419 measure_disk_usage_progress (gboolean reporting,
7420 guint64 current_size,
7425 MeasureProgress progress;
7426 GTask *task = user_data;
7427 MeasureTaskData *data;
7429 data = g_task_get_task_data (task);
7431 progress.callback = data->progress_callback;
7432 progress.user_data = data->progress_data;
7433 progress.reporting = reporting;
7434 progress.current_size = current_size;
7435 progress.num_dirs = num_dirs;
7436 progress.num_files = num_files;
7438 g_main_context_invoke_full (g_task_get_context (task),
7439 g_task_get_priority (task),
7440 measure_disk_usage_invoke_progress,
7441 g_memdup (&progress, sizeof progress),
7446 measure_disk_usage_thread (GTask *task,
7447 gpointer source_object,
7449 GCancellable *cancellable)
7451 MeasureTaskData *data = task_data;
7452 GError *error = NULL;
7453 MeasureResult result;
7455 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7456 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7457 &result.disk_usage, &result.num_dirs, &result.num_files,
7459 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7461 g_task_return_error (task, error);
7465 g_file_real_measure_disk_usage_async (GFile *file,
7466 GFileMeasureFlags flags,
7468 GCancellable *cancellable,
7469 GFileMeasureProgressCallback progress_callback,
7470 gpointer progress_data,
7471 GAsyncReadyCallback callback,
7474 MeasureTaskData data;
7478 data.progress_callback = progress_callback;
7479 data.progress_data = progress_data;
7481 task = g_task_new (file, cancellable, callback, user_data);
7482 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7483 g_task_set_priority (task, io_priority);
7485 g_task_run_in_thread (task, measure_disk_usage_thread);
7486 g_object_unref (task);
7490 g_file_real_measure_disk_usage_finish (GFile *file,
7491 GAsyncResult *result,
7492 guint64 *disk_usage,
7497 MeasureResult *measure_result;
7499 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7501 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7503 if (measure_result == NULL)
7507 *disk_usage = measure_result->disk_usage;
7510 *num_dirs = measure_result->num_dirs;
7513 *num_files = measure_result->num_files;
7515 g_free (measure_result);
7521 * g_file_measure_disk_usage:
7523 * @flags: #GFileMeasureFlags
7524 * @cancellable: (allow-none): optional #GCancellable
7525 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7526 * @progress_data: user_data for @progress_callback
7527 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7528 * @num_dirs: (allow-none) (out): the number of directories encountered
7529 * @num_files: (allow-none) (out): the number of non-directories encountered
7530 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7532 * Recursively measures the disk usage of @file.
7534 * This is essentially an analog of the '<literal>du</literal>' command,
7535 * but it also reports the number of directories and non-directory files
7536 * encountered (including things like symbolic links).
7538 * By default, errors are only reported against the toplevel file
7539 * itself. Errors found while recursing are silently ignored, unless
7540 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7542 * The returned size, @disk_usage, is in bytes and should be formatted
7543 * with g_format_size() in order to get something reasonable for showing
7544 * in a user interface.
7546 * @progress_callback and @progress_data can be given to request
7547 * periodic progress updates while scanning. See the documentation for
7548 * #GFileMeasureProgressCallback for information about when and how the
7549 * callback will be invoked.
7551 * Returns: %TRUE if successful, with the out parameters set.
7552 * %FALSE otherwise, with @error set.
7557 g_file_measure_disk_usage (GFile *file,
7558 GFileMeasureFlags flags,
7559 GCancellable *cancellable,
7560 GFileMeasureProgressCallback progress_callback,
7561 gpointer progress_data,
7562 guint64 *disk_usage,
7567 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7568 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7569 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7571 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7572 progress_callback, progress_data,
7573 disk_usage, num_dirs, num_files,
7578 * g_file_measure_disk_usage_async:
7580 * @flags: #GFileMeasureFlags
7581 * @io_priority: the <link linkend="io-priority">I/O priority</link>
7583 * @cancellable: (allow-none): optional #GCancellable
7584 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7585 * @progress_data: user_data for @progress_callback
7586 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7587 * @user_data: the data to pass to callback function
7589 * Recursively measures the disk usage of @file.
7591 * This is the asynchronous version of g_file_measure_disk_usage(). See
7592 * there for more information.
7597 g_file_measure_disk_usage_async (GFile *file,
7598 GFileMeasureFlags flags,
7600 GCancellable *cancellable,
7601 GFileMeasureProgressCallback progress_callback,
7602 gpointer progress_data,
7603 GAsyncReadyCallback callback,
7606 g_return_if_fail (G_IS_FILE (file));
7607 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7609 return G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7610 progress_callback, progress_data,
7611 callback, user_data);
7615 * g_file_measure_disk_usage_finish:
7617 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7618 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7619 * @num_dirs: (allow-none) (out): the number of directories encountered
7620 * @num_files: (allow-none) (out): the number of non-directories encountered
7621 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7623 * Collects the results from an earlier call to
7624 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7627 * Returns: %TRUE if successful, with the out parameters set.
7628 * %FALSE otherwise, with @error set.
7633 g_file_measure_disk_usage_finish (GFile *file,
7634 GAsyncResult *result,
7635 guint64 *disk_usage,
7640 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7641 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7643 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7647 * g_file_start_mountable:
7648 * @file: input #GFile
7649 * @flags: flags affecting the operation
7650 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7651 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7652 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7653 * @user_data: the data to pass to callback function
7655 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7656 * Using @start_operation, you can request callbacks when, for instance,
7657 * passwords are needed during authentication.
7659 * If @cancellable is not %NULL, then the operation can be cancelled by
7660 * triggering the cancellable object from another thread. If the operation
7661 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7663 * When the operation is finished, @callback will be called.
7664 * You can then call g_file_mount_mountable_finish() to get
7665 * the result of the operation.
7670 g_file_start_mountable (GFile *file,
7671 GDriveStartFlags flags,
7672 GMountOperation *start_operation,
7673 GCancellable *cancellable,
7674 GAsyncReadyCallback callback,
7679 g_return_if_fail (G_IS_FILE (file));
7681 iface = G_FILE_GET_IFACE (file);
7683 if (iface->start_mountable == NULL)
7685 g_task_report_new_error (file, callback, user_data,
7686 g_file_start_mountable,
7687 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7688 _("Operation not supported"));
7692 (* iface->start_mountable) (file,
7701 * g_file_start_mountable_finish:
7702 * @file: input #GFile
7703 * @result: a #GAsyncResult
7704 * @error: a #GError, or %NULL
7706 * Finishes a start operation. See g_file_start_mountable() for details.
7708 * Finish an asynchronous start operation that was started
7709 * with g_file_start_mountable().
7711 * Returns: %TRUE if the operation finished successfully. %FALSE
7717 g_file_start_mountable_finish (GFile *file,
7718 GAsyncResult *result,
7723 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7724 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7726 if (g_async_result_legacy_propagate_error (result, error))
7728 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7729 return g_task_propagate_boolean (G_TASK (result), error);
7731 iface = G_FILE_GET_IFACE (file);
7732 return (* iface->start_mountable_finish) (file, result, error);
7736 * g_file_stop_mountable:
7737 * @file: input #GFile
7738 * @flags: flags affecting the operation
7739 * @mount_operation: (allow-none): a #GMountOperation,
7740 * or %NULL to avoid user interaction.
7741 * @cancellable: (allow-none): optional #GCancellable object,
7743 * @callback: (allow-none): a #GAsyncReadyCallback to call
7744 * when the request is satisfied, or %NULL
7745 * @user_data: the data to pass to callback function
7747 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7749 * If @cancellable is not %NULL, then the operation can be cancelled by
7750 * triggering the cancellable object from another thread. If the operation
7751 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7753 * When the operation is finished, @callback will be called.
7754 * You can then call g_file_stop_mountable_finish() to get
7755 * the result of the operation.
7760 g_file_stop_mountable (GFile *file,
7761 GMountUnmountFlags flags,
7762 GMountOperation *mount_operation,
7763 GCancellable *cancellable,
7764 GAsyncReadyCallback callback,
7769 g_return_if_fail (G_IS_FILE (file));
7771 iface = G_FILE_GET_IFACE (file);
7773 if (iface->stop_mountable == NULL)
7775 g_task_report_new_error (file, callback, user_data,
7776 g_file_stop_mountable,
7777 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7778 _("Operation not supported"));
7782 (* iface->stop_mountable) (file,
7791 * g_file_stop_mountable_finish:
7792 * @file: input #GFile
7793 * @result: a #GAsyncResult
7794 * @error: a #GError, or %NULL
7796 * Finishes an stop operation, see g_file_stop_mountable() for details.
7798 * Finish an asynchronous stop operation that was started
7799 * with g_file_stop_mountable().
7801 * Returns: %TRUE if the operation finished successfully.
7807 g_file_stop_mountable_finish (GFile *file,
7808 GAsyncResult *result,
7813 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7814 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7816 if (g_async_result_legacy_propagate_error (result, error))
7818 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7819 return g_task_propagate_boolean (G_TASK (result), error);
7821 iface = G_FILE_GET_IFACE (file);
7822 return (* iface->stop_mountable_finish) (file, result, error);
7826 * g_file_poll_mountable:
7827 * @file: input #GFile
7828 * @cancellable: optional #GCancellable object, %NULL to ignore
7829 * @callback: (allow-none): a #GAsyncReadyCallback to call
7830 * when the request is satisfied, or %NULL
7831 * @user_data: the data to pass to callback function
7833 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7835 * If @cancellable is not %NULL, then the operation can be cancelled by
7836 * triggering the cancellable object from another thread. If the operation
7837 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7839 * When the operation is finished, @callback will be called.
7840 * You can then call g_file_mount_mountable_finish() to get
7841 * the result of the operation.
7846 g_file_poll_mountable (GFile *file,
7847 GCancellable *cancellable,
7848 GAsyncReadyCallback callback,
7853 g_return_if_fail (G_IS_FILE (file));
7855 iface = G_FILE_GET_IFACE (file);
7857 if (iface->poll_mountable == NULL)
7859 g_task_report_new_error (file, callback, user_data,
7860 g_file_poll_mountable,
7861 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7862 _("Operation not supported"));
7866 (* iface->poll_mountable) (file,
7873 * g_file_poll_mountable_finish:
7874 * @file: input #GFile
7875 * @result: a #GAsyncResult
7876 * @error: a #GError, or %NULL
7878 * Finishes a poll operation. See g_file_poll_mountable() for details.
7880 * Finish an asynchronous poll operation that was polled
7881 * with g_file_poll_mountable().
7883 * Returns: %TRUE if the operation finished successfully. %FALSE
7889 g_file_poll_mountable_finish (GFile *file,
7890 GAsyncResult *result,
7895 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7896 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7898 if (g_async_result_legacy_propagate_error (result, error))
7900 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7901 return g_task_propagate_boolean (G_TASK (result), error);
7903 iface = G_FILE_GET_IFACE (file);
7904 return (* iface->poll_mountable_finish) (file, result, error);
7908 * g_file_supports_thread_contexts:
7911 * Checks if @file supports <link
7912 * linkend="g-main-context-push-thread-default-context">thread-default
7913 * contexts</link>. If this returns %FALSE, you cannot perform
7914 * asynchronous operations on @file in a thread that has a
7915 * thread-default context.
7917 * Returns: Whether or not @file supports thread-default contexts.
7922 g_file_supports_thread_contexts (GFile *file)
7926 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7928 iface = G_FILE_GET_IFACE (file);
7929 return iface->supports_thread_contexts;