1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
85 * One way to think of a #GFile is as an abstraction of a pathname. For
86 * normal files the system pathname is what is stored internally, but as
87 * #GFiles are extensible it could also be something else that corresponds
88 * to a pathname in a userspace implementation of a filesystem.
90 * #GFiles make up hierarchies of directories and files that correspond to
91 * the files on a filesystem. You can move through the file system with
92 * #GFile using g_file_get_parent() to get an identifier for the parent
93 * directory, g_file_get_child() to get a child within a directory,
94 * g_file_resolve_relative_path() to resolve a relative path between two
95 * #GFiles. There can be multiple hierarchies, so you may not end up at
96 * the same root if you repeatedly call g_file_get_parent() on two different
99 * All #GFiles have a basename (get with g_file_get_basename()). These names
100 * are byte strings that are used to identify the file on the filesystem
101 * (relative to its parent directory) and there is no guarantees that they
102 * have any particular charset encoding or even make any sense at all. If
103 * you want to use filenames in a user interface you should use the display
104 * name that you can get by requesting the
105 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
106 * This is guaranteed to be in UTF-8 and can be used in a user interface.
107 * But always store the real basename or the #GFile to use to actually
108 * access the file, because there is no way to go from a display name to
111 * Using #GFile as an identifier has the same weaknesses as using a path
112 * in that there may be multiple aliases for the same file. For instance,
113 * hard or soft links may cause two different #GFiles to refer to the same
114 * file. Other possible causes for aliases are: case insensitive filesystems,
115 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
116 * check if two #GFiles point to the same file you can query for the
117 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
118 * canonicalization of pathnames passed in, so that trivial differences in
119 * the path string used at creation (duplicated slashes, slash at end of
120 * path, "." or ".." path segments, etc) does not create different #GFiles.
122 * Many #GFile operations have both synchronous and asynchronous versions
123 * to suit your application. Asynchronous versions of synchronous functions
124 * simply have _async() appended to their function names. The asynchronous
125 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
126 * the operation, producing a GAsyncResult which is then passed to the
127 * function's matching _finish() operation.
129 * Some #GFile operations do not have synchronous analogs, as they may
130 * take a very long time to finish, and blocking may leave an application
131 * unusable. Notable cases include:
132 * - g_file_mount_mountable() to mount a mountable file.
133 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
134 * - g_file_eject_mountable_with_operation() to eject a mountable file.
136 * ## Entity Tags # {#gfile-etag}
138 * One notable feature of #GFiles are entity tags, or "etags" for
139 * short. Entity tags are somewhat like a more abstract version of the
140 * traditional mtime, and can be used to quickly determine if the file
141 * has been modified from the version on the file system. See the
143 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
144 * for HTTP Etag headers, which are a very similar concept.
147 static void g_file_real_query_info_async (GFile *file,
148 const char *attributes,
149 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
154 static GFileInfo * g_file_real_query_info_finish (GFile *file,
157 static void g_file_real_query_filesystem_info_async (GFile *file,
158 const char *attributes,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
166 static void g_file_real_enumerate_children_async (GFile *file,
167 const char *attributes,
168 GFileQueryInfoFlags flags,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
173 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
176 static void g_file_real_read_async (GFile *file,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileInputStream * g_file_real_read_finish (GFile *file,
184 static void g_file_real_append_to_async (GFile *file,
185 GFileCreateFlags flags,
187 GCancellable *cancellable,
188 GAsyncReadyCallback callback,
190 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
193 static void g_file_real_create_async (GFile *file,
194 GFileCreateFlags flags,
196 GCancellable *cancellable,
197 GAsyncReadyCallback callback,
199 static GFileOutputStream *g_file_real_create_finish (GFile *file,
202 static void g_file_real_replace_async (GFile *file,
204 gboolean make_backup,
205 GFileCreateFlags flags,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
210 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
213 static void g_file_real_delete_async (GFile *file,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static gboolean g_file_real_delete_finish (GFile *file,
221 static void g_file_real_trash_async (GFile *file,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
226 static gboolean g_file_real_trash_finish (GFile *file,
229 static void g_file_real_make_directory_async (GFile *file,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static gboolean g_file_real_make_directory_finish (GFile *file,
237 static void g_file_real_open_readwrite_async (GFile *file,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
245 static void g_file_real_create_readwrite_async (GFile *file,
246 GFileCreateFlags flags,
248 GCancellable *cancellable,
249 GAsyncReadyCallback callback,
251 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
254 static void g_file_real_replace_readwrite_async (GFile *file,
256 gboolean make_backup,
257 GFileCreateFlags flags,
259 GCancellable *cancellable,
260 GAsyncReadyCallback callback,
262 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
265 static gboolean g_file_real_set_attributes_from_info (GFile *file,
267 GFileQueryInfoFlags flags,
268 GCancellable *cancellable,
270 static void g_file_real_set_display_name_async (GFile *file,
271 const char *display_name,
273 GCancellable *cancellable,
274 GAsyncReadyCallback callback,
276 static GFile * g_file_real_set_display_name_finish (GFile *file,
279 static void g_file_real_set_attributes_async (GFile *file,
281 GFileQueryInfoFlags flags,
283 GCancellable *cancellable,
284 GAsyncReadyCallback callback,
286 static gboolean g_file_real_set_attributes_finish (GFile *file,
290 static void g_file_real_find_enclosing_mount_async (GFile *file,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
295 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
298 static void g_file_real_copy_async (GFile *source,
300 GFileCopyFlags flags,
302 GCancellable *cancellable,
303 GFileProgressCallback progress_callback,
304 gpointer progress_callback_data,
305 GAsyncReadyCallback callback,
307 static gboolean g_file_real_copy_finish (GFile *file,
311 static gboolean g_file_real_measure_disk_usage (GFile *file,
312 GFileMeasureFlags flags,
313 GCancellable *cancellable,
314 GFileMeasureProgressCallback progress_callback,
315 gpointer progress_data,
320 static void g_file_real_measure_disk_usage_async (GFile *file,
321 GFileMeasureFlags flags,
323 GCancellable *cancellable,
324 GFileMeasureProgressCallback progress_callback,
325 gpointer progress_data,
326 GAsyncReadyCallback callback,
328 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
329 GAsyncResult *result,
335 typedef GFileIface GFileInterface;
336 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
339 g_file_default_init (GFileIface *iface)
341 iface->enumerate_children_async = g_file_real_enumerate_children_async;
342 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
343 iface->set_display_name_async = g_file_real_set_display_name_async;
344 iface->set_display_name_finish = g_file_real_set_display_name_finish;
345 iface->query_info_async = g_file_real_query_info_async;
346 iface->query_info_finish = g_file_real_query_info_finish;
347 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
348 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
349 iface->set_attributes_async = g_file_real_set_attributes_async;
350 iface->set_attributes_finish = g_file_real_set_attributes_finish;
351 iface->read_async = g_file_real_read_async;
352 iface->read_finish = g_file_real_read_finish;
353 iface->append_to_async = g_file_real_append_to_async;
354 iface->append_to_finish = g_file_real_append_to_finish;
355 iface->create_async = g_file_real_create_async;
356 iface->create_finish = g_file_real_create_finish;
357 iface->replace_async = g_file_real_replace_async;
358 iface->replace_finish = g_file_real_replace_finish;
359 iface->delete_file_async = g_file_real_delete_async;
360 iface->delete_file_finish = g_file_real_delete_finish;
361 iface->trash_async = g_file_real_trash_async;
362 iface->trash_finish = g_file_real_trash_finish;
363 iface->make_directory_async = g_file_real_make_directory_async;
364 iface->make_directory_finish = g_file_real_make_directory_finish;
365 iface->open_readwrite_async = g_file_real_open_readwrite_async;
366 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
367 iface->create_readwrite_async = g_file_real_create_readwrite_async;
368 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
369 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
370 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
371 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
372 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
373 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
374 iface->copy_async = g_file_real_copy_async;
375 iface->copy_finish = g_file_real_copy_finish;
376 iface->measure_disk_usage = g_file_real_measure_disk_usage;
377 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
378 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
384 * @file: input #GFile
386 * Checks to see if a file is native to the platform.
388 * A native file s one expressed in the platform-native filename format,
389 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
390 * as it might be on a locally mounted remote filesystem.
392 * On some systems non-native files may be available using the native
393 * filesystem via a userspace filesystem (FUSE), in these cases this call
394 * will return %FALSE, but g_file_get_path() will still return a native path.
396 * This call does no blocking I/O.
398 * Returns: %TRUE if @file is native
401 g_file_is_native (GFile *file)
405 g_return_val_if_fail (G_IS_FILE (file), FALSE);
407 iface = G_FILE_GET_IFACE (file);
409 return (* iface->is_native) (file);
414 * g_file_has_uri_scheme:
415 * @file: input #GFile
416 * @uri_scheme: a string containing a URI scheme
418 * Checks to see if a #GFile has a given URI scheme.
420 * This call does no blocking I/O.
422 * Returns: %TRUE if #GFile's backend supports the
423 * given URI scheme, %FALSE if URI scheme is %NULL,
424 * not supported, or #GFile is invalid.
427 g_file_has_uri_scheme (GFile *file,
428 const char *uri_scheme)
432 g_return_val_if_fail (G_IS_FILE (file), FALSE);
433 g_return_val_if_fail (uri_scheme != NULL, FALSE);
435 iface = G_FILE_GET_IFACE (file);
437 return (* iface->has_uri_scheme) (file, uri_scheme);
442 * g_file_get_uri_scheme:
443 * @file: input #GFile
445 * Gets the URI scheme for a #GFile.
446 * RFC 3986 decodes the scheme as:
448 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
450 * Common schemes include "file", "http", "ftp", etc.
452 * This call does no blocking I/O.
454 * Returns: a string containing the URI scheme for the given
455 * #GFile. The returned string should be freed with g_free()
456 * when no longer needed.
459 g_file_get_uri_scheme (GFile *file)
463 g_return_val_if_fail (G_IS_FILE (file), NULL);
465 iface = G_FILE_GET_IFACE (file);
467 return (* iface->get_uri_scheme) (file);
472 * g_file_get_basename:
473 * @file: input #GFile
475 * Gets the base name (the last component of the path) for a given #GFile.
477 * If called for the top level of a system (such as the filesystem root
478 * or a uri like sftp://host/) it will return a single directory separator
479 * (and on Windows, possibly a drive letter).
481 * The base name is a byte string (not UTF-8). It has no defined encoding
482 * or rules other than it may not contain zero bytes. If you want to use
483 * filenames in a user interface you should use the display name that you
484 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
485 * attribute with g_file_query_info().
487 * This call does no blocking I/O.
489 * Returns: string containing the #GFile's base name, or %NULL
490 * if given #GFile is invalid. The returned string should be
491 * freed with g_free() when no longer needed.
494 g_file_get_basename (GFile *file)
498 g_return_val_if_fail (G_IS_FILE (file), NULL);
500 iface = G_FILE_GET_IFACE (file);
502 return (* iface->get_basename) (file);
507 * @file: input #GFile
509 * Gets the local pathname for #GFile, if one exists.
511 * This call does no blocking I/O.
513 * Returns: string containing the #GFile's path, or %NULL if
514 * no such path exists. The returned string should be
515 * freed with g_free() when no longer needed.
518 g_file_get_path (GFile *file)
522 g_return_val_if_fail (G_IS_FILE (file), NULL);
524 iface = G_FILE_GET_IFACE (file);
526 return (* iface->get_path) (file);
531 * @file: input #GFile
533 * Gets the URI for the @file.
535 * This call does no blocking I/O.
537 * Returns: a string containing the #GFile's URI.
538 * The returned string should be freed with g_free()
539 * when no longer needed.
542 g_file_get_uri (GFile *file)
546 g_return_val_if_fail (G_IS_FILE (file), NULL);
548 iface = G_FILE_GET_IFACE (file);
550 return (* iface->get_uri) (file);
554 * g_file_get_parse_name:
555 * @file: input #GFile
557 * Gets the parse name of the @file.
558 * A parse name is a UTF-8 string that describes the
559 * file such that one can get the #GFile back using
560 * g_file_parse_name().
562 * This is generally used to show the #GFile as a nice
563 * full-pathname kind of string in a user interface,
564 * like in a location entry.
566 * For local files with names that can safely be converted
567 * to UTF-8 the pathname is used, otherwise the IRI is used
568 * (a form of URI that allows UTF-8 characters unescaped).
570 * This call does no blocking I/O.
572 * Returns: a string containing the #GFile's parse name.
573 * The returned string should be freed with g_free()
574 * when no longer needed.
577 g_file_get_parse_name (GFile *file)
581 g_return_val_if_fail (G_IS_FILE (file), NULL);
583 iface = G_FILE_GET_IFACE (file);
585 return (* iface->get_parse_name) (file);
590 * @file: input #GFile
592 * Duplicates a #GFile handle. This operation does not duplicate
593 * the actual file or directory represented by the #GFile; see
594 * g_file_copy() if attempting to copy a file.
596 * This call does no blocking I/O.
598 * Returns: (transfer full): a new #GFile that is a duplicate
599 * of the given #GFile.
602 g_file_dup (GFile *file)
606 g_return_val_if_fail (G_IS_FILE (file), NULL);
608 iface = G_FILE_GET_IFACE (file);
610 return (* iface->dup) (file);
615 * @file: (type GFile): #gconstpointer to a #GFile
617 * Creates a hash value for a #GFile.
619 * This call does no blocking I/O.
622 * Returns: 0 if @file is not a valid #GFile, otherwise an
623 * integer that can be used as hash value for the #GFile.
624 * This function is intended for easily hashing a #GFile to
625 * add to a #GHashTable or similar data structure.
628 g_file_hash (gconstpointer file)
632 g_return_val_if_fail (G_IS_FILE (file), 0);
634 iface = G_FILE_GET_IFACE (file);
636 return (* iface->hash) ((GFile *)file);
641 * @file1: the first #GFile
642 * @file2: the second #GFile
644 * Checks equality of two given #GFiles.
646 * Note that two #GFiles that differ can still refer to the same
647 * file on the filesystem due to various forms of filename
650 * This call does no blocking I/O.
652 * Returns: %TRUE if @file1 and @file2 are equal.
653 * %FALSE if either is not a #GFile.
656 g_file_equal (GFile *file1,
661 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
662 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
664 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
667 iface = G_FILE_GET_IFACE (file1);
669 return (* iface->equal) (file1, file2);
675 * @file: input #GFile
677 * Gets the parent directory for the @file.
678 * If the @file represents the root directory of the
679 * file system, then %NULL will be returned.
681 * This call does no blocking I/O.
683 * Returns: (transfer full): a #GFile structure to the
684 * parent of the given #GFile or %NULL if there is
685 * no parent. Free the returned object with g_object_unref().
688 g_file_get_parent (GFile *file)
692 g_return_val_if_fail (G_IS_FILE (file), NULL);
694 iface = G_FILE_GET_IFACE (file);
696 return (* iface->get_parent) (file);
701 * @file: input #GFile
702 * @parent: (allow-none): the parent to check for, or %NULL
704 * Checks if @file has a parent, and optionally, if it is @parent.
706 * If @parent is %NULL then this function returns %TRUE if @file has any
707 * parent at all. If @parent is non-%NULL then %TRUE is only returned
708 * if @file is a child of @parent.
710 * Returns: %TRUE if @file is a child of @parent (or any parent in the
711 * case that @parent is %NULL).
716 g_file_has_parent (GFile *file,
719 GFile *actual_parent;
722 g_return_val_if_fail (G_IS_FILE (file), FALSE);
723 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
725 actual_parent = g_file_get_parent (file);
727 if (actual_parent != NULL)
730 result = g_file_equal (parent, actual_parent);
734 g_object_unref (actual_parent);
744 * @file: input #GFile
745 * @name: string containing the child's basename
747 * Gets a child of @file with basename equal to @name.
749 * Note that the file with that specific name might not exist, but
750 * you can still have a #GFile that points to it. You can use this
751 * for instance to create that file.
753 * This call does no blocking I/O.
755 * Returns: (transfer full): a #GFile to a child specified by @name.
756 * Free the returned object with g_object_unref().
759 g_file_get_child (GFile *file,
762 g_return_val_if_fail (G_IS_FILE (file), NULL);
763 g_return_val_if_fail (name != NULL, NULL);
765 return g_file_resolve_relative_path (file, name);
769 * g_file_get_child_for_display_name:
770 * @file: input #GFile
771 * @display_name: string to a possible child
772 * @error: return location for an error
774 * Gets the child of @file for a given @display_name (i.e. a UTF-8
775 * version of the name). If this function fails, it returns %NULL
776 * and @error will be set. This is very useful when constructing a
777 * #GFile for a new file and the user entered the filename in the
778 * user interface, for instance when you select a directory and
779 * type a filename in the file selector.
781 * This call does no blocking I/O.
783 * Returns: (transfer full): a #GFile to the specified child, or
784 * %NULL if the display name couldn't be converted.
785 * Free the returned object with g_object_unref().
788 g_file_get_child_for_display_name (GFile *file,
789 const char *display_name,
794 g_return_val_if_fail (G_IS_FILE (file), NULL);
795 g_return_val_if_fail (display_name != NULL, NULL);
797 iface = G_FILE_GET_IFACE (file);
799 return (* iface->get_child_for_display_name) (file, display_name, error);
804 * @file: input #GFile
805 * @prefix: input #GFile
807 * Checks whether @file has the prefix specified by @prefix.
809 * In other words, if the names of initial elements of @file's
810 * pathname match @prefix. Only full pathname elements are matched,
811 * so a path like /foo is not considered a prefix of /foobar, only
814 * This call does no I/O, as it works purely on names. As such it can
815 * sometimes return %FALSE even if @file is inside a @prefix (from a
816 * filesystem point of view), because the prefix of @file is an alias
819 * Virtual: prefix_matches
820 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
824 g_file_has_prefix (GFile *file,
829 g_return_val_if_fail (G_IS_FILE (file), FALSE);
830 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
832 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
835 iface = G_FILE_GET_IFACE (file);
837 /* The vtable function differs in arg order since
838 * we're using the old contains_file call
840 return (* iface->prefix_matches) (prefix, file);
844 * g_file_get_relative_path:
845 * @parent: input #GFile
846 * @descendant: input #GFile
848 * Gets the path for @descendant relative to @parent.
850 * This call does no blocking I/O.
852 * Returns: string with the relative path from @descendant
853 * to @parent, or %NULL if @descendant doesn't have @parent
854 * as prefix. The returned string should be freed with g_free()
855 * when no longer needed.
858 g_file_get_relative_path (GFile *parent,
863 g_return_val_if_fail (G_IS_FILE (parent), NULL);
864 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
866 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
869 iface = G_FILE_GET_IFACE (parent);
871 return (* iface->get_relative_path) (parent, descendant);
875 * g_file_resolve_relative_path:
876 * @file: input #GFile
877 * @relative_path: a given relative path string
879 * Resolves a relative path for @file to an absolute path.
881 * This call does no blocking I/O.
883 * Returns: (transfer full): #GFile to the resolved path.
884 * %NULL if @relative_path is %NULL or if @file is invalid.
885 * Free the returned object with g_object_unref().
888 g_file_resolve_relative_path (GFile *file,
889 const char *relative_path)
893 g_return_val_if_fail (G_IS_FILE (file), NULL);
894 g_return_val_if_fail (relative_path != NULL, NULL);
896 iface = G_FILE_GET_IFACE (file);
898 return (* iface->resolve_relative_path) (file, relative_path);
902 * g_file_enumerate_children:
903 * @file: input #GFile
904 * @attributes: an attribute query string
905 * @flags: a set of #GFileQueryInfoFlags
906 * @cancellable: (allow-none): optional #GCancellable object,
908 * @error: #GError for error reporting
910 * Gets the requested information about the files in a directory.
911 * The result is a #GFileEnumerator object that will give out
912 * #GFileInfo objects for all the files in the directory.
914 * The @attributes value is a string that specifies the file
915 * attributes that should be gathered. It is not an error if
916 * it's not possible to read a particular requested attribute
917 * from a file - it just won't be set. @attributes should
918 * be a comma-separated list of attributes or attribute wildcards.
919 * The wildcard "*" means all attributes, and a wildcard like
920 * "standard::*" means all attributes in the standard namespace.
921 * An example attribute query be "standard::*,owner::user".
922 * The standard attributes are available as defines, like
923 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
925 * If @cancellable is not %NULL, then the operation can be cancelled
926 * by triggering the cancellable object from another thread. If the
927 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
930 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
931 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
932 * error will be returned. Other errors are possible too.
934 * Returns: (transfer full): A #GFileEnumerator if successful,
935 * %NULL on error. Free the returned object with g_object_unref().
938 g_file_enumerate_children (GFile *file,
939 const char *attributes,
940 GFileQueryInfoFlags flags,
941 GCancellable *cancellable,
946 g_return_val_if_fail (G_IS_FILE (file), NULL);
948 if (g_cancellable_set_error_if_cancelled (cancellable, error))
951 iface = G_FILE_GET_IFACE (file);
953 if (iface->enumerate_children == NULL)
955 g_set_error_literal (error, G_IO_ERROR,
956 G_IO_ERROR_NOT_SUPPORTED,
957 _("Operation not supported"));
961 return (* iface->enumerate_children) (file, attributes, flags,
966 * g_file_enumerate_children_async:
967 * @file: input #GFile
968 * @attributes: an attribute query string
969 * @flags: a set of #GFileQueryInfoFlags
970 * @io_priority: the [I/O priority][io-priority] of the request
971 * @cancellable: (allow-none): optional #GCancellable object,
973 * @callback: (scope async): a #GAsyncReadyCallback to call when the
974 * request is satisfied
975 * @user_data: (closure): the data to pass to callback function
977 * Asynchronously gets the requested information about the files
978 * in a directory. The result is a #GFileEnumerator object that will
979 * give out #GFileInfo objects for all the files in the directory.
981 * For more details, see g_file_enumerate_children() which is
982 * the synchronous version of this call.
984 * When the operation is finished, @callback will be called. You can
985 * then call g_file_enumerate_children_finish() to get the result of
989 g_file_enumerate_children_async (GFile *file,
990 const char *attributes,
991 GFileQueryInfoFlags flags,
993 GCancellable *cancellable,
994 GAsyncReadyCallback callback,
999 g_return_if_fail (G_IS_FILE (file));
1001 iface = G_FILE_GET_IFACE (file);
1002 (* iface->enumerate_children_async) (file,
1012 * g_file_enumerate_children_finish:
1013 * @file: input #GFile
1014 * @res: a #GAsyncResult
1017 * Finishes an async enumerate children operation.
1018 * See g_file_enumerate_children_async().
1020 * Returns: (transfer full): a #GFileEnumerator or %NULL
1021 * if an error occurred.
1022 * Free the returned object with g_object_unref().
1025 g_file_enumerate_children_finish (GFile *file,
1031 g_return_val_if_fail (G_IS_FILE (file), NULL);
1032 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1034 if (g_async_result_legacy_propagate_error (res, error))
1037 iface = G_FILE_GET_IFACE (file);
1038 return (* iface->enumerate_children_finish) (file, res, error);
1042 * g_file_query_exists:
1043 * @file: input #GFile
1044 * @cancellable: (allow-none): optional #GCancellable object,
1047 * Utility function to check if a particular file exists. This is
1048 * implemented using g_file_query_info() and as such does blocking I/O.
1050 * Note that in many cases it is racy to first check for file existence
1051 * and then execute something based on the outcome of that, because the
1052 * file might have been created or removed in between the operations. The
1053 * general approach to handling that is to not check, but just do the
1054 * operation and handle the errors as they come.
1056 * As an example of race-free checking, take the case of reading a file,
1057 * and if it doesn't exist, creating it. There are two racy versions: read
1058 * it, and on error create it; and: check if it exists, if not create it.
1059 * These can both result in two processes creating the file (with perhaps
1060 * a partially written file as the result). The correct approach is to
1061 * always try to create the file with g_file_create() which will either
1062 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1064 * However, in many cases an existence check is useful in a user interface,
1065 * for instance to make a menu item sensitive/insensitive, so that you don't
1066 * have to fool users that something is possible and then just show an error
1067 * dialog. If you do this, you should make sure to also handle the errors
1068 * that can happen due to races when you execute the operation.
1070 * Returns: %TRUE if the file exists (and can be detected without error),
1071 * %FALSE otherwise (or if cancelled).
1074 g_file_query_exists (GFile *file,
1075 GCancellable *cancellable)
1079 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1081 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1082 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1085 g_object_unref (info);
1093 * g_file_query_file_type:
1094 * @file: input #GFile
1095 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1096 * @cancellable: (allow-none): optional #GCancellable object,
1099 * Utility function to inspect the #GFileType of a file. This is
1100 * implemented using g_file_query_info() and as such does blocking I/O.
1102 * The primary use case of this method is to check if a file is
1103 * a regular file, directory, or symlink.
1105 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1106 * if the file does not exist
1111 g_file_query_file_type (GFile *file,
1112 GFileQueryInfoFlags flags,
1113 GCancellable *cancellable)
1116 GFileType file_type;
1118 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1119 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1123 file_type = g_file_info_get_file_type (info);
1124 g_object_unref (info);
1127 file_type = G_FILE_TYPE_UNKNOWN;
1133 * g_file_query_info:
1134 * @file: input #GFile
1135 * @attributes: an attribute query string
1136 * @flags: a set of #GFileQueryInfoFlags
1137 * @cancellable: (allow-none): optional #GCancellable object,
1141 * Gets the requested information about specified @file.
1142 * The result is a #GFileInfo object that contains key-value
1143 * attributes (such as the type or size of the file).
1145 * The @attributes value is a string that specifies the file
1146 * attributes that should be gathered. It is not an error if
1147 * it's not possible to read a particular requested attribute
1148 * from a file - it just won't be set. @attributes should be a
1149 * comma-separated list of attributes or attribute wildcards.
1150 * The wildcard "*" means all attributes, and a wildcard like
1151 * "standard::*" means all attributes in the standard namespace.
1152 * An example attribute query be "standard::*,owner::user".
1153 * The standard attributes are available as defines, like
1154 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1156 * If @cancellable is not %NULL, then the operation can be cancelled
1157 * by triggering the cancellable object from another thread. If the
1158 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1161 * For symlinks, normally the information about the target of the
1162 * symlink is returned, rather than information about the symlink
1163 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1164 * in @flags the information about the symlink itself will be returned.
1165 * Also, for symlinks that point to non-existing files the information
1166 * about the symlink itself will be returned.
1168 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1169 * returned. Other errors are possible too, and depend on what kind of
1170 * filesystem the file is on.
1172 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1173 * on error. Free the returned object with g_object_unref().
1176 g_file_query_info (GFile *file,
1177 const char *attributes,
1178 GFileQueryInfoFlags flags,
1179 GCancellable *cancellable,
1184 g_return_val_if_fail (G_IS_FILE (file), NULL);
1186 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1189 iface = G_FILE_GET_IFACE (file);
1191 if (iface->query_info == NULL)
1193 g_set_error_literal (error, G_IO_ERROR,
1194 G_IO_ERROR_NOT_SUPPORTED,
1195 _("Operation not supported"));
1199 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1203 * g_file_query_info_async:
1204 * @file: input #GFile
1205 * @attributes: an attribute query string
1206 * @flags: a set of #GFileQueryInfoFlags
1207 * @io_priority: the [I/O priority][io-priority] of the request
1208 * @cancellable: (allow-none): optional #GCancellable object,
1210 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1211 * request is satisfied
1212 * @user_data: (closure): the data to pass to callback function
1214 * Asynchronously gets the requested information about specified @file.
1215 * The result is a #GFileInfo object that contains key-value attributes
1216 * (such as type or size for the file).
1218 * For more details, see g_file_query_info() which is the synchronous
1219 * version of this call.
1221 * When the operation is finished, @callback will be called. You can
1222 * then call g_file_query_info_finish() to get the result of the operation.
1225 g_file_query_info_async (GFile *file,
1226 const char *attributes,
1227 GFileQueryInfoFlags flags,
1229 GCancellable *cancellable,
1230 GAsyncReadyCallback callback,
1235 g_return_if_fail (G_IS_FILE (file));
1237 iface = G_FILE_GET_IFACE (file);
1238 (* iface->query_info_async) (file,
1248 * g_file_query_info_finish:
1249 * @file: input #GFile
1250 * @res: a #GAsyncResult
1253 * Finishes an asynchronous file info query.
1254 * See g_file_query_info_async().
1256 * Returns: (transfer full): #GFileInfo for given @file
1257 * or %NULL on error. Free the returned object with
1261 g_file_query_info_finish (GFile *file,
1267 g_return_val_if_fail (G_IS_FILE (file), NULL);
1268 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1270 if (g_async_result_legacy_propagate_error (res, error))
1273 iface = G_FILE_GET_IFACE (file);
1274 return (* iface->query_info_finish) (file, res, error);
1278 * g_file_query_filesystem_info:
1279 * @file: input #GFile
1280 * @attributes: an attribute query string
1281 * @cancellable: (allow-none): optional #GCancellable object,
1285 * Similar to g_file_query_info(), but obtains information
1286 * about the filesystem the @file is on, rather than the file itself.
1287 * For instance the amount of space available and the type of
1290 * The @attributes value is a string that specifies the attributes
1291 * that should be gathered. It is not an error if it's not possible
1292 * to read a particular requested attribute from a file - it just
1293 * won't be set. @attributes should be a comma-separated list of
1294 * attributes or attribute wildcards. The wildcard "*" means all
1295 * attributes, and a wildcard like "filesystem::*" means all attributes
1296 * in the filesystem namespace. The standard namespace for filesystem
1297 * attributes is "filesystem". Common attributes of interest are
1298 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1299 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1300 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1302 * If @cancellable is not %NULL, then the operation can be cancelled
1303 * by triggering the cancellable object from another thread. If the
1304 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1307 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1308 * be returned. Other errors are possible too, and depend on what
1309 * kind of filesystem the file is on.
1311 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1312 * Free the returned object with g_object_unref().
1315 g_file_query_filesystem_info (GFile *file,
1316 const char *attributes,
1317 GCancellable *cancellable,
1322 g_return_val_if_fail (G_IS_FILE (file), NULL);
1324 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1327 iface = G_FILE_GET_IFACE (file);
1329 if (iface->query_filesystem_info == NULL)
1331 g_set_error_literal (error, G_IO_ERROR,
1332 G_IO_ERROR_NOT_SUPPORTED,
1333 _("Operation not supported"));
1337 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1341 * g_file_query_filesystem_info_async:
1342 * @file: input #GFile
1343 * @attributes: an attribute query string
1344 * @io_priority: the [I/O priority][io-priority] of the request
1345 * @cancellable: (allow-none): optional #GCancellable object,
1347 * @callback: (scope async): a #GAsyncReadyCallback to call
1348 * when the request is satisfied
1349 * @user_data: (closure): the data to pass to callback function
1351 * Asynchronously gets the requested information about the filesystem
1352 * that the specified @file is on. The result is a #GFileInfo object
1353 * that contains key-value attributes (such as type or size for the
1356 * For more details, see g_file_query_filesystem_info() which is the
1357 * synchronous version of this call.
1359 * When the operation is finished, @callback will be called. You can
1360 * then call g_file_query_info_finish() to get the result of the
1364 g_file_query_filesystem_info_async (GFile *file,
1365 const char *attributes,
1367 GCancellable *cancellable,
1368 GAsyncReadyCallback callback,
1373 g_return_if_fail (G_IS_FILE (file));
1375 iface = G_FILE_GET_IFACE (file);
1376 (* iface->query_filesystem_info_async) (file,
1385 * g_file_query_filesystem_info_finish:
1386 * @file: input #GFile
1387 * @res: a #GAsyncResult
1390 * Finishes an asynchronous filesystem info query.
1391 * See g_file_query_filesystem_info_async().
1393 * Returns: (transfer full): #GFileInfo for given @file
1394 * or %NULL on error.
1395 * Free the returned object with g_object_unref().
1398 g_file_query_filesystem_info_finish (GFile *file,
1404 g_return_val_if_fail (G_IS_FILE (file), NULL);
1405 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1407 if (g_async_result_legacy_propagate_error (res, error))
1410 iface = G_FILE_GET_IFACE (file);
1411 return (* iface->query_filesystem_info_finish) (file, res, error);
1415 * g_file_find_enclosing_mount:
1416 * @file: input #GFile
1417 * @cancellable: (allow-none): optional #GCancellable object,
1421 * Gets a #GMount for the #GFile.
1423 * If the #GFileIface for @file does not have a mount (e.g.
1424 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1425 * and %NULL will be returned.
1427 * If @cancellable is not %NULL, then the operation can be cancelled by
1428 * triggering the cancellable object from another thread. If the operation
1429 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1431 * Returns: (transfer full): a #GMount where the @file is located
1432 * or %NULL on error.
1433 * Free the returned object with g_object_unref().
1436 g_file_find_enclosing_mount (GFile *file,
1437 GCancellable *cancellable,
1442 g_return_val_if_fail (G_IS_FILE (file), NULL);
1444 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1447 iface = G_FILE_GET_IFACE (file);
1448 if (iface->find_enclosing_mount == NULL)
1451 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1452 /* Translators: This is an error message when
1453 * trying to find the enclosing (user visible)
1454 * mount of a file, but none exists.
1456 _("Containing mount does not exist"));
1460 return (* iface->find_enclosing_mount) (file, cancellable, error);
1464 * g_file_find_enclosing_mount_async:
1466 * @io_priority: the [I/O priority][io-priority] of the request
1467 * @cancellable: (allow-none): optional #GCancellable object,
1469 * @callback: (scope async): a #GAsyncReadyCallback to call
1470 * when the request is satisfied
1471 * @user_data: (closure): the data to pass to callback function
1473 * Asynchronously gets the mount for the file.
1475 * For more details, see g_file_find_enclosing_mount() which is
1476 * the synchronous version of this call.
1478 * When the operation is finished, @callback will be called.
1479 * You can then call g_file_find_enclosing_mount_finish() to
1480 * get the result of the operation.
1483 g_file_find_enclosing_mount_async (GFile *file,
1485 GCancellable *cancellable,
1486 GAsyncReadyCallback callback,
1491 g_return_if_fail (G_IS_FILE (file));
1493 iface = G_FILE_GET_IFACE (file);
1494 (* iface->find_enclosing_mount_async) (file,
1502 * g_file_find_enclosing_mount_finish:
1504 * @res: a #GAsyncResult
1507 * Finishes an asynchronous find mount request.
1508 * See g_file_find_enclosing_mount_async().
1510 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1511 * Free the returned object with g_object_unref().
1514 g_file_find_enclosing_mount_finish (GFile *file,
1520 g_return_val_if_fail (G_IS_FILE (file), NULL);
1521 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1523 if (g_async_result_legacy_propagate_error (res, error))
1526 iface = G_FILE_GET_IFACE (file);
1527 return (* iface->find_enclosing_mount_finish) (file, res, error);
1533 * @file: #GFile to read
1534 * @cancellable: (allow-none): a #GCancellable
1535 * @error: a #GError, or %NULL
1537 * Opens a file for reading. The result is a #GFileInputStream that
1538 * can be used to read the contents of the file.
1540 * If @cancellable is not %NULL, then the operation can be cancelled by
1541 * triggering the cancellable object from another thread. If the operation
1542 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1544 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1545 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1546 * error will be returned. Other errors are possible too, and depend
1547 * on what kind of filesystem the file is on.
1550 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1551 * Free the returned object with g_object_unref().
1554 g_file_read (GFile *file,
1555 GCancellable *cancellable,
1560 g_return_val_if_fail (G_IS_FILE (file), NULL);
1562 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1565 iface = G_FILE_GET_IFACE (file);
1567 if (iface->read_fn == NULL)
1569 g_set_error_literal (error, G_IO_ERROR,
1570 G_IO_ERROR_NOT_SUPPORTED,
1571 _("Operation not supported"));
1575 return (* iface->read_fn) (file, cancellable, error);
1580 * @file: input #GFile
1581 * @flags: a set of #GFileCreateFlags
1582 * @cancellable: (allow-none): optional #GCancellable object,
1584 * @error: a #GError, or %NULL
1586 * Gets an output stream for appending data to the file.
1587 * If the file doesn't already exist it is created.
1589 * By default files created are generally readable by everyone,
1590 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1591 * will be made readable only to the current user, to the level that
1592 * is supported on the target filesystem.
1594 * If @cancellable is not %NULL, then the operation can be cancelled
1595 * by triggering the cancellable object from another thread. If the
1596 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1599 * Some file systems don't allow all file names, and may return an
1600 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1601 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1602 * possible too, and depend on what kind of filesystem the file is on.
1604 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1605 * Free the returned object with g_object_unref().
1608 g_file_append_to (GFile *file,
1609 GFileCreateFlags flags,
1610 GCancellable *cancellable,
1615 g_return_val_if_fail (G_IS_FILE (file), NULL);
1617 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1620 iface = G_FILE_GET_IFACE (file);
1622 if (iface->append_to == NULL)
1624 g_set_error_literal (error, G_IO_ERROR,
1625 G_IO_ERROR_NOT_SUPPORTED,
1626 _("Operation not supported"));
1630 return (* iface->append_to) (file, flags, cancellable, error);
1635 * @file: input #GFile
1636 * @flags: a set of #GFileCreateFlags
1637 * @cancellable: (allow-none): optional #GCancellable object,
1639 * @error: a #GError, or %NULL
1641 * Creates a new file and returns an output stream for writing to it.
1642 * The file must not already exist.
1644 * By default files created are generally readable by everyone,
1645 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1646 * will be made readable only to the current user, to the level
1647 * that is supported on the target filesystem.
1649 * If @cancellable is not %NULL, then the operation can be cancelled
1650 * by triggering the cancellable object from another thread. If the
1651 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1654 * If a file or directory with this name already exists the
1655 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1656 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1657 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1658 * be returned. Other errors are possible too, and depend on what kind
1659 * of filesystem the file is on.
1661 * Returns: (transfer full): a #GFileOutputStream for the newly created
1662 * file, or %NULL on error.
1663 * Free the returned object with g_object_unref().
1666 g_file_create (GFile *file,
1667 GFileCreateFlags flags,
1668 GCancellable *cancellable,
1673 g_return_val_if_fail (G_IS_FILE (file), NULL);
1675 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1678 iface = G_FILE_GET_IFACE (file);
1680 if (iface->create == NULL)
1682 g_set_error_literal (error, G_IO_ERROR,
1683 G_IO_ERROR_NOT_SUPPORTED,
1684 _("Operation not supported"));
1688 return (* iface->create) (file, flags, cancellable, error);
1693 * @file: input #GFile
1694 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1695 * for the current #GFile, or #NULL to ignore
1696 * @make_backup: %TRUE if a backup should be created
1697 * @flags: a set of #GFileCreateFlags
1698 * @cancellable: (allow-none): optional #GCancellable object,
1700 * @error: a #GError, or %NULL
1702 * Returns an output stream for overwriting the file, possibly
1703 * creating a backup copy of the file first. If the file doesn't exist,
1704 * it will be created.
1706 * This will try to replace the file in the safest way possible so
1707 * that any errors during the writing will not affect an already
1708 * existing copy of the file. For instance, for local files it
1709 * may write to a temporary file and then atomically rename over
1710 * the destination when the stream is closed.
1712 * By default files created are generally readable by everyone,
1713 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1714 * will be made readable only to the current user, to the level that
1715 * is supported on the target filesystem.
1717 * If @cancellable is not %NULL, then the operation can be cancelled
1718 * by triggering the cancellable object from another thread. If the
1719 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1722 * If you pass in a non-%NULL @etag value, then this value is
1723 * compared to the current entity tag of the file, and if they differ
1724 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1725 * that the file has been changed since you last read it. You can get
1726 * the new etag from g_file_output_stream_get_etag() after you've
1727 * finished writing and closed the #GFileOutputStream. When you load
1728 * a new file you can use g_file_input_stream_query_info() to get
1729 * the etag of the file.
1731 * If @make_backup is %TRUE, this function will attempt to make a
1732 * backup of the current file before overwriting it. If this fails
1733 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1734 * want to replace anyway, try again with @make_backup set to %FALSE.
1736 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1737 * be returned, and if the file is some other form of non-regular file
1738 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1739 * file systems don't allow all file names, and may return an
1740 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1741 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1742 * possible too, and depend on what kind of filesystem the file is on.
1744 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1745 * Free the returned object with g_object_unref().
1748 g_file_replace (GFile *file,
1750 gboolean make_backup,
1751 GFileCreateFlags flags,
1752 GCancellable *cancellable,
1757 g_return_val_if_fail (G_IS_FILE (file), NULL);
1759 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1762 iface = G_FILE_GET_IFACE (file);
1764 if (iface->replace == NULL)
1766 g_set_error_literal (error, G_IO_ERROR,
1767 G_IO_ERROR_NOT_SUPPORTED,
1768 _("Operation not supported"));
1772 /* Handle empty tag string as NULL in consistent way. */
1773 if (etag && *etag == 0)
1776 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1780 * g_file_open_readwrite:
1781 * @file: #GFile to open
1782 * @cancellable: (allow-none): a #GCancellable
1783 * @error: a #GError, or %NULL
1785 * Opens an existing file for reading and writing. The result is
1786 * a #GFileIOStream that can be used to read and write the contents
1789 * If @cancellable is not %NULL, then the operation can be cancelled
1790 * by triggering the cancellable object from another thread. If the
1791 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1794 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1795 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1796 * error will be returned. Other errors are possible too, and depend on
1797 * what kind of filesystem the file is on. Note that in many non-local
1798 * file cases read and write streams are not supported, so make sure you
1799 * really need to do read and write streaming, rather than just opening
1800 * for reading or writing.
1802 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1803 * Free the returned object with g_object_unref().
1808 g_file_open_readwrite (GFile *file,
1809 GCancellable *cancellable,
1814 g_return_val_if_fail (G_IS_FILE (file), NULL);
1816 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1819 iface = G_FILE_GET_IFACE (file);
1821 if (iface->open_readwrite == NULL)
1823 g_set_error_literal (error, G_IO_ERROR,
1824 G_IO_ERROR_NOT_SUPPORTED,
1825 _("Operation not supported"));
1829 return (* iface->open_readwrite) (file, cancellable, error);
1833 * g_file_create_readwrite:
1835 * @flags: a set of #GFileCreateFlags
1836 * @cancellable: (allow-none): optional #GCancellable object,
1838 * @error: return location for a #GError, or %NULL
1840 * Creates a new file and returns a stream for reading and
1841 * writing to it. The file must not already exist.
1843 * By default files created are generally readable by everyone,
1844 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1845 * will be made readable only to the current user, to the level
1846 * that is supported on the target filesystem.
1848 * If @cancellable is not %NULL, then the operation can be cancelled
1849 * by triggering the cancellable object from another thread. If the
1850 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1853 * If a file or directory with this name already exists, the
1854 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1855 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1856 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1857 * will be returned. Other errors are possible too, and depend on what
1858 * kind of filesystem the file is on.
1860 * Note that in many non-local file cases read and write streams are
1861 * not supported, so make sure you really need to do read and write
1862 * streaming, rather than just opening for reading or writing.
1864 * Returns: (transfer full): a #GFileIOStream for the newly created
1865 * file, or %NULL on error.
1866 * Free the returned object with g_object_unref().
1871 g_file_create_readwrite (GFile *file,
1872 GFileCreateFlags flags,
1873 GCancellable *cancellable,
1878 g_return_val_if_fail (G_IS_FILE (file), NULL);
1880 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1883 iface = G_FILE_GET_IFACE (file);
1885 if (iface->create_readwrite == NULL)
1887 g_set_error_literal (error, G_IO_ERROR,
1888 G_IO_ERROR_NOT_SUPPORTED,
1889 _("Operation not supported"));
1893 return (* iface->create_readwrite) (file, flags, cancellable, error);
1897 * g_file_replace_readwrite:
1899 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1900 * for the current #GFile, or #NULL to ignore
1901 * @make_backup: %TRUE if a backup should be created
1902 * @flags: a set of #GFileCreateFlags
1903 * @cancellable: (allow-none): optional #GCancellable object,
1905 * @error: return location for a #GError, or %NULL
1907 * Returns an output stream for overwriting the file in readwrite mode,
1908 * possibly creating a backup copy of the file first. If the file doesn't
1909 * exist, it will be created.
1911 * For details about the behaviour, see g_file_replace() which does the
1912 * same thing but returns an output stream only.
1914 * Note that in many non-local file cases read and write streams are not
1915 * supported, so make sure you really need to do read and write streaming,
1916 * rather than just opening for reading or writing.
1918 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1919 * Free the returned object with g_object_unref().
1924 g_file_replace_readwrite (GFile *file,
1926 gboolean make_backup,
1927 GFileCreateFlags flags,
1928 GCancellable *cancellable,
1933 g_return_val_if_fail (G_IS_FILE (file), NULL);
1935 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1938 iface = G_FILE_GET_IFACE (file);
1940 if (iface->replace_readwrite == NULL)
1942 g_set_error_literal (error, G_IO_ERROR,
1943 G_IO_ERROR_NOT_SUPPORTED,
1944 _("Operation not supported"));
1948 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1952 * g_file_read_async:
1953 * @file: input #GFile
1954 * @io_priority: the [I/O priority][io-priority] of the request
1955 * @cancellable: (allow-none): optional #GCancellable object,
1957 * @callback: (scope async): a #GAsyncReadyCallback to call
1958 * when the request is satisfied
1959 * @user_data: (closure): the data to pass to callback function
1961 * Asynchronously opens @file for reading.
1963 * For more details, see g_file_read() which is
1964 * the synchronous version of this call.
1966 * When the operation is finished, @callback will be called.
1967 * You can then call g_file_read_finish() to get the result
1971 g_file_read_async (GFile *file,
1973 GCancellable *cancellable,
1974 GAsyncReadyCallback callback,
1979 g_return_if_fail (G_IS_FILE (file));
1981 iface = G_FILE_GET_IFACE (file);
1982 (* iface->read_async) (file,
1990 * g_file_read_finish:
1991 * @file: input #GFile
1992 * @res: a #GAsyncResult
1993 * @error: a #GError, or %NULL
1995 * Finishes an asynchronous file read operation started with
1996 * g_file_read_async().
1998 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1999 * Free the returned object with g_object_unref().
2002 g_file_read_finish (GFile *file,
2008 g_return_val_if_fail (G_IS_FILE (file), NULL);
2009 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2011 if (g_async_result_legacy_propagate_error (res, error))
2014 iface = G_FILE_GET_IFACE (file);
2015 return (* iface->read_finish) (file, res, error);
2019 * g_file_append_to_async:
2020 * @file: input #GFile
2021 * @flags: a set of #GFileCreateFlags
2022 * @io_priority: the [I/O priority][io-priority] of the request
2023 * @cancellable: (allow-none): optional #GCancellable object,
2025 * @callback: (scope async): a #GAsyncReadyCallback to call
2026 * when the request is satisfied
2027 * @user_data: (closure): the data to pass to callback function
2029 * Asynchronously opens @file for appending.
2031 * For more details, see g_file_append_to() which is
2032 * the synchronous version of this call.
2034 * When the operation is finished, @callback will be called.
2035 * You can then call g_file_append_to_finish() to get the result
2039 g_file_append_to_async (GFile *file,
2040 GFileCreateFlags flags,
2042 GCancellable *cancellable,
2043 GAsyncReadyCallback callback,
2048 g_return_if_fail (G_IS_FILE (file));
2050 iface = G_FILE_GET_IFACE (file);
2051 (* iface->append_to_async) (file,
2060 * g_file_append_to_finish:
2061 * @file: input #GFile
2062 * @res: #GAsyncResult
2063 * @error: a #GError, or %NULL
2065 * Finishes an asynchronous file append operation started with
2066 * g_file_append_to_async().
2068 * Returns: (transfer full): a valid #GFileOutputStream
2069 * or %NULL on error.
2070 * Free the returned object with g_object_unref().
2073 g_file_append_to_finish (GFile *file,
2079 g_return_val_if_fail (G_IS_FILE (file), NULL);
2080 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2082 if (g_async_result_legacy_propagate_error (res, error))
2085 iface = G_FILE_GET_IFACE (file);
2086 return (* iface->append_to_finish) (file, res, error);
2090 * g_file_create_async:
2091 * @file: input #GFile
2092 * @flags: a set of #GFileCreateFlags
2093 * @io_priority: the [I/O priority][io-priority] of the request
2094 * @cancellable: (allow-none): optional #GCancellable object,
2096 * @callback: (scope async): a #GAsyncReadyCallback to call
2097 * when the request is satisfied
2098 * @user_data: (closure): the data to pass to callback function
2100 * Asynchronously creates a new file and returns an output stream
2101 * for writing to it. The file must not already exist.
2103 * For more details, see g_file_create() which is
2104 * the synchronous version of this call.
2106 * When the operation is finished, @callback will be called.
2107 * You can then call g_file_create_finish() to get the result
2111 g_file_create_async (GFile *file,
2112 GFileCreateFlags flags,
2114 GCancellable *cancellable,
2115 GAsyncReadyCallback callback,
2120 g_return_if_fail (G_IS_FILE (file));
2122 iface = G_FILE_GET_IFACE (file);
2123 (* iface->create_async) (file,
2132 * g_file_create_finish:
2133 * @file: input #GFile
2134 * @res: a #GAsyncResult
2135 * @error: a #GError, or %NULL
2137 * Finishes an asynchronous file create operation started with
2138 * g_file_create_async().
2140 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2141 * Free the returned object with g_object_unref().
2144 g_file_create_finish (GFile *file,
2150 g_return_val_if_fail (G_IS_FILE (file), NULL);
2151 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2153 if (g_async_result_legacy_propagate_error (res, error))
2156 iface = G_FILE_GET_IFACE (file);
2157 return (* iface->create_finish) (file, res, error);
2161 * g_file_replace_async:
2162 * @file: input #GFile
2163 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2164 * or %NULL to ignore
2165 * @make_backup: %TRUE if a backup should be created
2166 * @flags: a set of #GFileCreateFlags
2167 * @io_priority: the [I/O priority][io-priority] of the request
2168 * @cancellable: (allow-none): optional #GCancellable object,
2170 * @callback: (scope async): a #GAsyncReadyCallback to call
2171 * when the request is satisfied
2172 * @user_data: (closure): the data to pass to callback function
2174 * Asynchronously overwrites the file, replacing the contents,
2175 * possibly creating a backup copy of the file first.
2177 * For more details, see g_file_replace() which is
2178 * the synchronous version of this call.
2180 * When the operation is finished, @callback will be called.
2181 * You can then call g_file_replace_finish() to get the result
2185 g_file_replace_async (GFile *file,
2187 gboolean make_backup,
2188 GFileCreateFlags flags,
2190 GCancellable *cancellable,
2191 GAsyncReadyCallback callback,
2196 g_return_if_fail (G_IS_FILE (file));
2198 iface = G_FILE_GET_IFACE (file);
2199 (* iface->replace_async) (file,
2210 * g_file_replace_finish:
2211 * @file: input #GFile
2212 * @res: a #GAsyncResult
2213 * @error: a #GError, or %NULL
2215 * Finishes an asynchronous file replace operation started with
2216 * g_file_replace_async().
2218 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2219 * Free the returned object with g_object_unref().
2222 g_file_replace_finish (GFile *file,
2228 g_return_val_if_fail (G_IS_FILE (file), NULL);
2229 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2231 if (g_async_result_legacy_propagate_error (res, error))
2234 iface = G_FILE_GET_IFACE (file);
2235 return (* iface->replace_finish) (file, res, error);
2239 * g_file_open_readwrite_async
2240 * @file: input #GFile
2241 * @io_priority: the [I/O priority][io-priority] of the request
2242 * @cancellable: (allow-none): optional #GCancellable object,
2244 * @callback: (scope async): a #GAsyncReadyCallback to call
2245 * when the request is satisfied
2246 * @user_data: (closure): the data to pass to callback function
2248 * Asynchronously opens @file for reading and writing.
2250 * For more details, see g_file_open_readwrite() which is
2251 * the synchronous version of this call.
2253 * When the operation is finished, @callback will be called.
2254 * You can then call g_file_open_readwrite_finish() to get
2255 * the result of the operation.
2260 g_file_open_readwrite_async (GFile *file,
2262 GCancellable *cancellable,
2263 GAsyncReadyCallback callback,
2268 g_return_if_fail (G_IS_FILE (file));
2270 iface = G_FILE_GET_IFACE (file);
2271 (* iface->open_readwrite_async) (file,
2279 * g_file_open_readwrite_finish:
2280 * @file: input #GFile
2281 * @res: a #GAsyncResult
2282 * @error: a #GError, or %NULL
2284 * Finishes an asynchronous file read operation started with
2285 * g_file_open_readwrite_async().
2287 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2288 * Free the returned object with g_object_unref().
2293 g_file_open_readwrite_finish (GFile *file,
2299 g_return_val_if_fail (G_IS_FILE (file), NULL);
2300 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2302 if (g_async_result_legacy_propagate_error (res, error))
2305 iface = G_FILE_GET_IFACE (file);
2306 return (* iface->open_readwrite_finish) (file, res, error);
2310 * g_file_create_readwrite_async:
2311 * @file: input #GFile
2312 * @flags: a set of #GFileCreateFlags
2313 * @io_priority: the [I/O priority][io-priority] of the request
2314 * @cancellable: (allow-none): optional #GCancellable object,
2316 * @callback: (scope async): a #GAsyncReadyCallback to call
2317 * when the request is satisfied
2318 * @user_data: (closure): the data to pass to callback function
2320 * Asynchronously creates a new file and returns a stream
2321 * for reading and writing to it. The file must not already exist.
2323 * For more details, see g_file_create_readwrite() which is
2324 * the synchronous version of this call.
2326 * When the operation is finished, @callback will be called.
2327 * You can then call g_file_create_readwrite_finish() to get
2328 * the result of the operation.
2333 g_file_create_readwrite_async (GFile *file,
2334 GFileCreateFlags flags,
2336 GCancellable *cancellable,
2337 GAsyncReadyCallback callback,
2342 g_return_if_fail (G_IS_FILE (file));
2344 iface = G_FILE_GET_IFACE (file);
2345 (* iface->create_readwrite_async) (file,
2354 * g_file_create_readwrite_finish:
2355 * @file: input #GFile
2356 * @res: a #GAsyncResult
2357 * @error: a #GError, or %NULL
2359 * Finishes an asynchronous file create operation started with
2360 * g_file_create_readwrite_async().
2362 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2363 * Free the returned object with g_object_unref().
2368 g_file_create_readwrite_finish (GFile *file,
2374 g_return_val_if_fail (G_IS_FILE (file), NULL);
2375 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2377 if (g_async_result_legacy_propagate_error (res, error))
2380 iface = G_FILE_GET_IFACE (file);
2381 return (* iface->create_readwrite_finish) (file, res, error);
2385 * g_file_replace_readwrite_async:
2386 * @file: input #GFile
2387 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2388 * or %NULL to ignore
2389 * @make_backup: %TRUE if a backup should be created
2390 * @flags: a set of #GFileCreateFlags
2391 * @io_priority: the [I/O priority][io-priority] of the request
2392 * @cancellable: (allow-none): optional #GCancellable object,
2394 * @callback: (scope async): a #GAsyncReadyCallback to call
2395 * when the request is satisfied
2396 * @user_data: (closure): the data to pass to callback function
2398 * Asynchronously overwrites the file in read-write mode,
2399 * replacing the contents, possibly creating a backup copy
2400 * of the file first.
2402 * For more details, see g_file_replace_readwrite() which is
2403 * the synchronous version of this call.
2405 * When the operation is finished, @callback will be called.
2406 * You can then call g_file_replace_readwrite_finish() to get
2407 * the result of the operation.
2412 g_file_replace_readwrite_async (GFile *file,
2414 gboolean make_backup,
2415 GFileCreateFlags flags,
2417 GCancellable *cancellable,
2418 GAsyncReadyCallback callback,
2423 g_return_if_fail (G_IS_FILE (file));
2425 iface = G_FILE_GET_IFACE (file);
2426 (* iface->replace_readwrite_async) (file,
2437 * g_file_replace_readwrite_finish:
2438 * @file: input #GFile
2439 * @res: a #GAsyncResult
2440 * @error: a #GError, or %NULL
2442 * Finishes an asynchronous file replace operation started with
2443 * g_file_replace_readwrite_async().
2445 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2446 * Free the returned object with g_object_unref().
2451 g_file_replace_readwrite_finish (GFile *file,
2457 g_return_val_if_fail (G_IS_FILE (file), NULL);
2458 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2460 if (g_async_result_legacy_propagate_error (res, error))
2463 iface = G_FILE_GET_IFACE (file);
2464 return (* iface->replace_readwrite_finish) (file, res, error);
2468 copy_symlink (GFile *destination,
2469 GFileCopyFlags flags,
2470 GCancellable *cancellable,
2475 gboolean tried_delete;
2477 GFileType file_type;
2479 tried_delete = FALSE;
2483 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2485 /* Maybe it already existed, and we want to overwrite? */
2486 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2487 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2489 g_clear_error (&my_error);
2491 /* Don't overwrite if the destination is a directory */
2492 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2493 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2494 cancellable, &my_error);
2497 file_type = g_file_info_get_file_type (info);
2498 g_object_unref (info);
2500 if (file_type == G_FILE_TYPE_DIRECTORY)
2502 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2503 _("Can't copy over directory"));
2508 if (!g_file_delete (destination, cancellable, error))
2511 tried_delete = TRUE;
2515 g_propagate_error (error, my_error);
2522 static GFileInputStream *
2523 open_source_for_copy (GFile *source,
2525 GFileCopyFlags flags,
2526 GCancellable *cancellable,
2530 GFileInputStream *ret;
2532 GFileType file_type;
2535 ret = g_file_read (source, cancellable, &my_error);
2539 /* There was an error opening the source, try to set a good error for it: */
2540 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2542 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2543 * as that is less useful to the app. Better check for errors on the
2546 g_error_free (my_error);
2549 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2550 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2551 cancellable, &my_error);
2553 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2555 file_type = g_file_info_get_file_type (info);
2556 g_object_unref (info);
2558 if (flags & G_FILE_COPY_OVERWRITE)
2560 if (file_type == G_FILE_TYPE_DIRECTORY)
2562 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2563 _("Can't copy directory over directory"));
2566 /* continue to would_recurse error */
2570 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2571 _("Target file exists"));
2577 /* Error getting info from target, return that error
2578 * (except for NOT_FOUND, which is no error here)
2580 g_clear_object (&info);
2581 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2583 g_propagate_error (error, my_error);
2586 g_clear_error (&my_error);
2589 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2590 _("Can't recursively copy directory"));
2594 g_propagate_error (error, my_error);
2599 should_copy (GFileAttributeInfo *info,
2600 gboolean copy_all_attributes,
2601 gboolean skip_perms)
2603 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2606 if (copy_all_attributes)
2607 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2608 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2612 build_attribute_list_for_copy (GFile *file,
2613 GFileCopyFlags flags,
2614 char **out_attributes,
2615 GCancellable *cancellable,
2618 gboolean ret = FALSE;
2619 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2623 gboolean copy_all_attributes;
2624 gboolean skip_perms;
2626 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2627 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2629 /* Ignore errors here, if the target supports no attributes there is
2630 * nothing to copy. We still honor the cancellable though.
2632 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2633 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2636 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2637 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2640 if (attributes == NULL && namespaces == NULL)
2644 s = g_string_new ("");
2648 for (i = 0; i < attributes->n_infos; i++)
2650 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2655 g_string_append_c (s, ',');
2657 g_string_append (s, attributes->infos[i].name);
2664 for (i = 0; i < namespaces->n_infos; i++)
2666 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2671 g_string_append_c (s, ',');
2673 g_string_append (s, namespaces->infos[i].name);
2674 g_string_append (s, "::*");
2680 *out_attributes = g_string_free (s, FALSE);
2684 g_string_free (s, TRUE);
2686 g_file_attribute_info_list_unref (attributes);
2688 g_file_attribute_info_list_unref (namespaces);
2694 * g_file_copy_attributes:
2695 * @source: a #GFile with attributes
2696 * @destination: a #GFile to copy attributes to
2697 * @flags: a set of #GFileCopyFlags
2698 * @cancellable: (allow-none): optional #GCancellable object,
2700 * @error: a #GError, %NULL to ignore
2702 * Copies the file attributes from @source to @destination.
2704 * Normally only a subset of the file attributes are copied,
2705 * those that are copies in a normal file copy operation
2706 * (which for instance does not include e.g. owner). However
2707 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2708 * all the metadata that is possible to copy is copied. This
2709 * is useful when implementing move by copy + delete source.
2711 * Returns: %TRUE if the attributes were copied successfully,
2715 g_file_copy_attributes (GFile *source,
2717 GFileCopyFlags flags,
2718 GCancellable *cancellable,
2721 char *attrs_to_read;
2724 gboolean source_nofollow_symlinks;
2726 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2727 cancellable, error))
2730 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2732 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2733 * we just don't copy it.
2735 info = g_file_query_info (source, attrs_to_read,
2736 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2740 g_free (attrs_to_read);
2745 res = g_file_set_attributes_from_info (destination,
2747 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2750 g_object_unref (info);
2757 copy_stream_with_progress (GInputStream *in,
2760 GCancellable *cancellable,
2761 GFileProgressCallback progress_callback,
2762 gpointer progress_callback_data,
2765 gssize n_read, n_written;
2766 goffset current_size;
2767 char buffer[1024*64], *p;
2773 /* avoid performance impact of querying total size when it's not needed */
2774 if (progress_callback)
2776 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2777 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2781 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2782 total_size = g_file_info_get_size (info);
2783 g_object_unref (info);
2786 if (total_size == -1)
2788 info = g_file_query_info (source,
2789 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2790 G_FILE_QUERY_INFO_NONE,
2794 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2795 total_size = g_file_info_get_size (info);
2796 g_object_unref (info);
2801 if (total_size == -1)
2808 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2818 current_size += n_read;
2823 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2824 if (n_written == -1)
2831 n_read -= n_written;
2837 if (progress_callback)
2838 progress_callback (current_size, total_size, progress_callback_data);
2841 /* Make sure we send full copied size */
2842 if (progress_callback)
2843 progress_callback (current_size, total_size, progress_callback_data);
2851 do_splice (int fd_in,
2856 long *bytes_transferd,
2862 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2870 else if (errsv == ENOSYS || errsv == EINVAL)
2871 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2872 _("Splice not supported"));
2874 g_set_error (error, G_IO_ERROR,
2875 g_io_error_from_errno (errsv),
2876 _("Error splicing file: %s"),
2877 g_strerror (errsv));
2882 *bytes_transferd = result;
2887 splice_stream_with_progress (GInputStream *in,
2889 GCancellable *cancellable,
2890 GFileProgressCallback progress_callback,
2891 gpointer progress_callback_data,
2894 int buffer[2] = { -1, -1 };
2901 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2902 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2904 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2908 /* avoid performance impact of querying total size when it's not needed */
2909 if (progress_callback)
2913 if (fstat (fd_in, &sbuf) == 0)
2914 total_size = sbuf.st_size;
2917 if (total_size == -1)
2920 offset_in = offset_out = 0;
2927 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2930 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2941 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2944 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2947 n_read -= n_written;
2950 if (progress_callback)
2951 progress_callback (offset_in, total_size, progress_callback_data);
2954 /* Make sure we send full copied size */
2955 if (progress_callback)
2956 progress_callback (offset_in, total_size, progress_callback_data);
2958 if (!g_close (buffer[0], error))
2961 if (!g_close (buffer[1], error))
2965 if (buffer[0] != -1)
2966 (void) g_close (buffer[0], NULL);
2967 if (buffer[1] != -1)
2968 (void) g_close (buffer[1], NULL);
2976 btrfs_reflink_with_progress (GInputStream *in,
2979 GCancellable *cancellable,
2980 GFileProgressCallback progress_callback,
2981 gpointer progress_callback_data,
2984 goffset source_size;
2988 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2989 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2991 if (progress_callback)
2992 source_size = g_file_info_get_size (info);
2994 /* Btrfs clone ioctl properties:
2995 * - Works at the inode level
2996 * - Doesn't work with directories
2997 * - Always follows symlinks (source and destination)
2999 * By the time we get here, *in and *out are both regular files */
3000 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3005 g_set_error_literal (error, G_IO_ERROR,
3006 G_IO_ERROR_NOT_SUPPORTED,
3007 _("Copy (reflink/clone) between mounts is not supported"));
3008 else if (errno == EINVAL)
3009 g_set_error_literal (error, G_IO_ERROR,
3010 G_IO_ERROR_NOT_SUPPORTED,
3011 _("Copy (reflink/clone) is not supported or invalid"));
3013 /* Most probably something odd happened; retry with fallback */
3014 g_set_error_literal (error, G_IO_ERROR,
3015 G_IO_ERROR_NOT_SUPPORTED,
3016 _("Copy (reflink/clone) is not supported or didn't work"));
3017 /* We retry with fallback for all error cases because Btrfs is currently
3018 * unstable, and so we can't trust it to do clone properly.
3019 * In addition, any hard errors here would cause the same failure in the
3020 * fallback manual copy as well. */
3024 /* Make sure we send full copied size */
3025 if (progress_callback)
3026 progress_callback (source_size, source_size, progress_callback_data);
3033 file_copy_fallback (GFile *source,
3035 GFileCopyFlags flags,
3036 GCancellable *cancellable,
3037 GFileProgressCallback progress_callback,
3038 gpointer progress_callback_data,
3041 gboolean ret = FALSE;
3042 GFileInputStream *file_in = NULL;
3043 GInputStream *in = NULL;
3044 GOutputStream *out = NULL;
3045 GFileInfo *info = NULL;
3047 char *attrs_to_read;
3048 gboolean do_set_attributes = FALSE;
3050 /* need to know the file type */
3051 info = g_file_query_info (source,
3052 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3053 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3059 /* Maybe copy the symlink? */
3060 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3061 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3063 target = g_file_info_get_symlink_target (info);
3066 if (!copy_symlink (destination, flags, cancellable, target, error))
3072 /* ... else fall back on a regular file copy */
3074 /* Handle "special" files (pipes, device nodes, ...)? */
3075 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3077 /* FIXME: could try to recreate device nodes and others? */
3078 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3079 _("Can't copy special file"));
3083 /* Everything else should just fall back on a regular copy. */
3085 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3088 in = G_INPUT_STREAM (file_in);
3090 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3091 cancellable, error))
3094 if (attrs_to_read != NULL)
3096 GError *tmp_error = NULL;
3098 /* Ok, ditch the previous lightweight info (on Unix we just
3099 * called lstat()); at this point we gather all the information
3100 * we need about the source from the opened file descriptor.
3102 g_object_unref (info);
3104 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3105 cancellable, &tmp_error);
3108 /* Not all gvfs backends implement query_info_on_read(), we
3109 * can just fall back to the pathname again.
3110 * https://bugzilla.gnome.org/706254
3112 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3114 g_clear_error (&tmp_error);
3115 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3116 cancellable, error);
3120 g_free (attrs_to_read);
3121 g_propagate_error (error, tmp_error);
3125 g_free (attrs_to_read);
3129 do_set_attributes = TRUE;
3132 /* In the local file path, we pass down the source info which
3133 * includes things like unix::mode, to ensure that the target file
3134 * is not created with different permissions from the source file.
3136 * If a future API like g_file_replace_with_info() is added, switch
3137 * this code to use that.
3139 if (G_IS_LOCAL_FILE (destination))
3141 if (flags & G_FILE_COPY_OVERWRITE)
3142 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3144 flags & G_FILE_COPY_BACKUP,
3145 G_FILE_CREATE_REPLACE_DESTINATION,
3147 cancellable, error);
3149 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3151 cancellable, error);
3153 else if (flags & G_FILE_COPY_OVERWRITE)
3155 out = (GOutputStream *)g_file_replace (destination,
3157 flags & G_FILE_COPY_BACKUP,
3158 G_FILE_CREATE_REPLACE_DESTINATION,
3159 cancellable, error);
3163 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3170 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3172 GError *reflink_err = NULL;
3174 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3175 progress_callback, progress_callback_data,
3178 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3180 g_clear_error (&reflink_err);
3184 g_propagate_error (error, reflink_err);
3197 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3199 GError *splice_err = NULL;
3201 if (!splice_stream_with_progress (in, out, cancellable,
3202 progress_callback, progress_callback_data,
3205 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3207 g_clear_error (&splice_err);
3211 g_propagate_error (error, splice_err);
3224 /* A plain read/write loop */
3225 if (!copy_stream_with_progress (in, out, source, cancellable,
3226 progress_callback, progress_callback_data,
3234 /* Don't care about errors in source here */
3235 (void) g_input_stream_close (in, cancellable, NULL);
3236 g_object_unref (in);
3241 /* But write errors on close are bad! */
3242 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3244 g_object_unref (out);
3247 /* Ignore errors here. Failure to copy metadata is not a hard error */
3248 /* TODO: set these attributes /before/ we do the rename() on Unix */
3249 if (ret && do_set_attributes)
3251 g_file_set_attributes_from_info (destination,
3253 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3258 g_clear_object (&info);
3265 * @source: input #GFile
3266 * @destination: destination #GFile
3267 * @flags: set of #GFileCopyFlags
3268 * @cancellable: (allow-none): optional #GCancellable object,
3270 * @progress_callback: (allow-none) (scope call): function to callback with
3271 * progress information, or %NULL if progress information is not needed
3272 * @progress_callback_data: (closure): user data to pass to @progress_callback
3273 * @error: #GError to set on error, or %NULL
3275 * Copies the file @source to the location specified by @destination.
3276 * Can not handle recursive copies of directories.
3278 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3279 * existing @destination file is overwritten.
3281 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3282 * will be copied as symlinks, otherwise the target of the
3283 * @source symlink will be copied.
3285 * If @cancellable is not %NULL, then the operation can be cancelled by
3286 * triggering the cancellable object from another thread. If the operation
3287 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3289 * If @progress_callback is not %NULL, then the operation can be monitored
3290 * by setting this to a #GFileProgressCallback function.
3291 * @progress_callback_data will be passed to this function. It is guaranteed
3292 * that this callback will be called after all data has been transferred with
3293 * the total number of bytes copied during the operation.
3295 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3296 * is returned, independent on the status of the @destination.
3298 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3299 * the error %G_IO_ERROR_EXISTS is returned.
3301 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3302 * error is returned. If trying to overwrite a directory with a directory the
3303 * %G_IO_ERROR_WOULD_MERGE error is returned.
3305 * If the source is a directory and the target does not exist, or
3306 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3307 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3309 * If you are interested in copying the #GFile object itself (not the on-disk
3310 * file), see g_file_dup().
3312 * Returns: %TRUE on success, %FALSE otherwise.
3315 g_file_copy (GFile *source,
3317 GFileCopyFlags flags,
3318 GCancellable *cancellable,
3319 GFileProgressCallback progress_callback,
3320 gpointer progress_callback_data,
3327 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3328 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3333 iface = G_FILE_GET_IFACE (destination);
3337 res = (* iface->copy) (source, destination,
3339 progress_callback, progress_callback_data,
3345 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3347 g_propagate_error (error, my_error);
3351 g_clear_error (&my_error);
3354 /* If the types are different, and the destination method failed
3355 * also try the source method
3357 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3359 iface = G_FILE_GET_IFACE (source);
3364 res = (* iface->copy) (source, destination,
3366 progress_callback, progress_callback_data,
3372 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3374 g_propagate_error (error, my_error);
3378 g_clear_error (&my_error);
3382 return file_copy_fallback (source, destination, flags, cancellable,
3383 progress_callback, progress_callback_data,
3388 * g_file_copy_async: (skip)
3389 * @source: input #GFile
3390 * @destination: destination #GFile
3391 * @flags: set of #GFileCopyFlags
3392 * @io_priority: the [I/O priority][io-priority] of the request
3393 * @cancellable: (allow-none): optional #GCancellable object,
3395 * @progress_callback: (allow-none): function to callback with progress
3396 * information, or %NULL if progress information is not needed
3397 * @progress_callback_data: (closure): user data to pass to @progress_callback
3398 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3399 * @user_data: the data to pass to callback function
3401 * Copies the file @source to the location specified by @destination
3402 * asynchronously. For details of the behaviour, see g_file_copy().
3404 * If @progress_callback is not %NULL, then that function that will be called
3405 * just like in g_file_copy(), however the callback will run in the main loop,
3406 * not in the thread that is doing the I/O operation.
3408 * When the operation is finished, @callback will be called. You can then call
3409 * g_file_copy_finish() to get the result of the operation.
3412 g_file_copy_async (GFile *source,
3414 GFileCopyFlags flags,
3416 GCancellable *cancellable,
3417 GFileProgressCallback progress_callback,
3418 gpointer progress_callback_data,
3419 GAsyncReadyCallback callback,
3424 g_return_if_fail (G_IS_FILE (source));
3425 g_return_if_fail (G_IS_FILE (destination));
3427 iface = G_FILE_GET_IFACE (source);
3428 (* iface->copy_async) (source,
3434 progress_callback_data,
3440 * g_file_copy_finish:
3441 * @file: input #GFile
3442 * @res: a #GAsyncResult
3443 * @error: a #GError, or %NULL
3445 * Finishes copying the file started with g_file_copy_async().
3447 * Returns: a %TRUE on success, %FALSE on error.
3450 g_file_copy_finish (GFile *file,
3456 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3457 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3459 if (g_async_result_legacy_propagate_error (res, error))
3462 iface = G_FILE_GET_IFACE (file);
3463 return (* iface->copy_finish) (file, res, error);
3468 * @source: #GFile pointing to the source location
3469 * @destination: #GFile pointing to the destination location
3470 * @flags: set of #GFileCopyFlags
3471 * @cancellable: (allow-none): optional #GCancellable object,
3473 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3474 * function for updates
3475 * @progress_callback_data: (closure): gpointer to user data for
3476 * the callback function
3477 * @error: #GError for returning error conditions, or %NULL
3479 * Tries to move the file or directory @source to the location specified
3480 * by @destination. If native move operations are supported then this is
3481 * used, otherwise a copy + delete fallback is used. The native
3482 * implementation may support moving directories (for instance on moves
3483 * inside the same filesystem), but the fallback code does not.
3485 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3486 * existing @destination file is overwritten.
3488 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3489 * will be copied as symlinks, otherwise the target of the
3490 * @source symlink will be copied.
3492 * If @cancellable is not %NULL, then the operation can be cancelled by
3493 * triggering the cancellable object from another thread. If the operation
3494 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3496 * If @progress_callback is not %NULL, then the operation can be monitored
3497 * by setting this to a #GFileProgressCallback function.
3498 * @progress_callback_data will be passed to this function. It is
3499 * guaranteed that this callback will be called after all data has been
3500 * transferred with the total number of bytes copied during the operation.
3502 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3503 * error is returned, independent on the status of the @destination.
3505 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3506 * then the error %G_IO_ERROR_EXISTS is returned.
3508 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3509 * error is returned. If trying to overwrite a directory with a directory the
3510 * %G_IO_ERROR_WOULD_MERGE error is returned.
3512 * If the source is a directory and the target does not exist, or
3513 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3514 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3515 * move operation isn't available).
3517 * Returns: %TRUE on successful move, %FALSE otherwise.
3520 g_file_move (GFile *source,
3522 GFileCopyFlags flags,
3523 GCancellable *cancellable,
3524 GFileProgressCallback progress_callback,
3525 gpointer progress_callback_data,
3532 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3533 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3535 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3538 iface = G_FILE_GET_IFACE (destination);
3542 res = (* iface->move) (source, destination,
3544 progress_callback, progress_callback_data,
3550 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3552 g_propagate_error (error, my_error);
3557 /* If the types are different, and the destination method failed
3558 * also try the source method
3560 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3562 iface = G_FILE_GET_IFACE (source);
3567 res = (* iface->move) (source, destination,
3569 progress_callback, progress_callback_data,
3575 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3577 g_propagate_error (error, my_error);
3583 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3585 g_set_error_literal (error, G_IO_ERROR,
3586 G_IO_ERROR_NOT_SUPPORTED,
3587 _("Operation not supported"));
3591 flags |= G_FILE_COPY_ALL_METADATA;
3592 if (!g_file_copy (source, destination, flags, cancellable,
3593 progress_callback, progress_callback_data,
3597 return g_file_delete (source, cancellable, error);
3601 * g_file_make_directory:
3602 * @file: input #GFile
3603 * @cancellable: (allow-none): optional #GCancellable object,
3605 * @error: a #GError, or %NULL
3607 * Creates a directory. Note that this will only create a child directory
3608 * of the immediate parent directory of the path or URI given by the #GFile.
3609 * To recursively create directories, see g_file_make_directory_with_parents().
3610 * This function will fail if the parent directory does not exist, setting
3611 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3612 * creating directories, this function will fail, setting @error to
3613 * %G_IO_ERROR_NOT_SUPPORTED.
3615 * For a local #GFile the newly created directory will have the default
3616 * (current) ownership and permissions of the current process.
3618 * If @cancellable is not %NULL, then the operation can be cancelled by
3619 * triggering the cancellable object from another thread. If the operation
3620 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3622 * Returns: %TRUE on successful creation, %FALSE otherwise.
3625 g_file_make_directory (GFile *file,
3626 GCancellable *cancellable,
3631 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3633 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3636 iface = G_FILE_GET_IFACE (file);
3638 if (iface->make_directory == NULL)
3640 g_set_error_literal (error, G_IO_ERROR,
3641 G_IO_ERROR_NOT_SUPPORTED,
3642 _("Operation not supported"));
3646 return (* iface->make_directory) (file, cancellable, error);
3650 * g_file_make_directory_async:
3651 * @file: input #GFile
3652 * @io_priority: the [I/O priority][io-priority] of the request
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 [I/O priority][io-priority] of the request
3902 * @cancellable: (allow-none): optional #GCancellable object,
3904 * @callback: a #GAsyncReadyCallback to call
3905 * when the request is satisfied
3906 * @user_data: the data to pass to callback function
3908 * Asynchronously delete a file. If the @file is a directory, it will
3909 * only be deleted if it is empty. This has the same semantics as
3912 * Virtual: delete_file_async
3916 g_file_delete_async (GFile *file,
3918 GCancellable *cancellable,
3919 GAsyncReadyCallback callback,
3924 g_return_if_fail (G_IS_FILE (file));
3926 iface = G_FILE_GET_IFACE (file);
3927 (* iface->delete_file_async) (file,
3935 * g_file_delete_finish:
3936 * @file: input #GFile
3937 * @result: a #GAsyncResult
3938 * @error: a #GError, or %NULL
3940 * Finishes deleting a file started with g_file_delete_async().
3942 * Virtual: delete_file_finish
3943 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3947 g_file_delete_finish (GFile *file,
3948 GAsyncResult *result,
3953 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3954 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3956 if (g_async_result_legacy_propagate_error (result, error))
3959 iface = G_FILE_GET_IFACE (file);
3960 return (* iface->delete_file_finish) (file, result, error);
3965 * @file: #GFile to send to trash
3966 * @cancellable: (allow-none): optional #GCancellable object,
3968 * @error: a #GError, or %NULL
3970 * Sends @file to the "Trashcan", if possible. This is similar to
3971 * deleting it, but the user can recover it before emptying the trashcan.
3972 * Not all file systems support trashing, so this call can return the
3973 * %G_IO_ERROR_NOT_SUPPORTED error.
3975 * If @cancellable is not %NULL, then the operation can be cancelled by
3976 * triggering the cancellable object from another thread. If the operation
3977 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3980 * Returns: %TRUE on successful trash, %FALSE otherwise.
3983 g_file_trash (GFile *file,
3984 GCancellable *cancellable,
3989 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3991 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3994 iface = G_FILE_GET_IFACE (file);
3996 if (iface->trash == NULL)
3998 g_set_error_literal (error,
3999 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4000 _("Trash not supported"));
4004 return (* iface->trash) (file, cancellable, error);
4008 * g_file_trash_async:
4009 * @file: input #GFile
4010 * @io_priority: the [I/O priority][io-priority] of the request
4011 * @cancellable: (allow-none): optional #GCancellable object,
4013 * @callback: a #GAsyncReadyCallback to call
4014 * when the request is satisfied
4015 * @user_data: the data to pass to callback function
4017 * Asynchronously sends @file to the Trash location, if possible.
4019 * Virtual: trash_async
4023 g_file_trash_async (GFile *file,
4025 GCancellable *cancellable,
4026 GAsyncReadyCallback callback,
4031 g_return_if_fail (G_IS_FILE (file));
4033 iface = G_FILE_GET_IFACE (file);
4034 (* iface->trash_async) (file,
4042 * g_file_trash_finish:
4043 * @file: input #GFile
4044 * @result: a #GAsyncResult
4045 * @error: a #GError, or %NULL
4047 * Finishes an asynchronous file trashing operation, started with
4048 * g_file_trash_async().
4050 * Virtual: trash_finish
4051 * Returns: %TRUE on successful trash, %FALSE otherwise.
4055 g_file_trash_finish (GFile *file,
4056 GAsyncResult *result,
4061 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4062 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4064 iface = G_FILE_GET_IFACE (file);
4065 return (* iface->trash_finish) (file, result, error);
4069 * g_file_set_display_name:
4070 * @file: input #GFile
4071 * @display_name: a string
4072 * @cancellable: (allow-none): optional #GCancellable object,
4074 * @error: a #GError, or %NULL
4076 * Renames @file to the specified display name.
4078 * The display name is converted from UTF-8 to the correct encoding
4079 * for the target filesystem if possible and the @file is renamed to this.
4081 * If you want to implement a rename operation in the user interface the
4082 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4083 * initial value in the rename widget, and then the result after editing
4084 * should be passed to g_file_set_display_name().
4086 * On success the resulting converted filename is returned.
4088 * If @cancellable is not %NULL, then the operation can be cancelled by
4089 * triggering the cancellable object from another thread. If the operation
4090 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4092 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4093 * or %NULL if there was an error.
4094 * Free the returned object with g_object_unref().
4097 g_file_set_display_name (GFile *file,
4098 const gchar *display_name,
4099 GCancellable *cancellable,
4104 g_return_val_if_fail (G_IS_FILE (file), NULL);
4105 g_return_val_if_fail (display_name != NULL, NULL);
4107 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4111 G_IO_ERROR_INVALID_ARGUMENT,
4112 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4116 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4119 iface = G_FILE_GET_IFACE (file);
4121 return (* iface->set_display_name) (file, display_name, cancellable, error);
4125 * g_file_set_display_name_async:
4126 * @file: input #GFile
4127 * @display_name: a string
4128 * @io_priority: the [I/O priority][io-priority] of the request
4129 * @cancellable: (allow-none): optional #GCancellable object,
4131 * @callback: (scope async): a #GAsyncReadyCallback to call
4132 * when the request is satisfied
4133 * @user_data: (closure): the data to pass to callback function
4135 * Asynchronously sets the display name for a given #GFile.
4137 * For more details, see g_file_set_display_name() which is
4138 * the synchronous version of this call.
4140 * When the operation is finished, @callback will be called.
4141 * You can then call g_file_set_display_name_finish() to get
4142 * the result of the operation.
4145 g_file_set_display_name_async (GFile *file,
4146 const gchar *display_name,
4148 GCancellable *cancellable,
4149 GAsyncReadyCallback callback,
4154 g_return_if_fail (G_IS_FILE (file));
4155 g_return_if_fail (display_name != NULL);
4157 iface = G_FILE_GET_IFACE (file);
4158 (* iface->set_display_name_async) (file,
4167 * g_file_set_display_name_finish:
4168 * @file: input #GFile
4169 * @res: a #GAsyncResult
4170 * @error: a #GError, or %NULL
4172 * Finishes setting a display name started with
4173 * g_file_set_display_name_async().
4175 * Returns: (transfer full): a #GFile or %NULL on error.
4176 * Free the returned object with g_object_unref().
4179 g_file_set_display_name_finish (GFile *file,
4185 g_return_val_if_fail (G_IS_FILE (file), NULL);
4186 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4188 if (g_async_result_legacy_propagate_error (res, error))
4191 iface = G_FILE_GET_IFACE (file);
4192 return (* iface->set_display_name_finish) (file, res, error);
4196 * g_file_query_settable_attributes:
4197 * @file: input #GFile
4198 * @cancellable: (allow-none): optional #GCancellable object,
4200 * @error: a #GError, or %NULL
4202 * Obtain the list of settable attributes for the file.
4204 * Returns the type and full attribute name of all the attributes
4205 * that can be set on this file. This doesn't mean setting it will
4206 * always succeed though, you might get an access failure, or some
4207 * specific file may not support a specific attribute.
4209 * If @cancellable is not %NULL, then the operation can be cancelled by
4210 * triggering the cancellable object from another thread. If the operation
4211 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4213 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4214 * When you are done with it, release it with
4215 * g_file_attribute_info_list_unref()
4217 GFileAttributeInfoList *
4218 g_file_query_settable_attributes (GFile *file,
4219 GCancellable *cancellable,
4224 GFileAttributeInfoList *list;
4226 g_return_val_if_fail (G_IS_FILE (file), NULL);
4228 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4231 iface = G_FILE_GET_IFACE (file);
4233 if (iface->query_settable_attributes == NULL)
4234 return g_file_attribute_info_list_new ();
4237 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4241 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4243 list = g_file_attribute_info_list_new ();
4244 g_error_free (my_error);
4247 g_propagate_error (error, my_error);
4254 * g_file_query_writable_namespaces:
4255 * @file: input #GFile
4256 * @cancellable: (allow-none): optional #GCancellable object,
4258 * @error: a #GError, or %NULL
4260 * Obtain the list of attribute namespaces where new attributes
4261 * can be created by a user. An example of this is extended
4262 * attributes (in the "xattr" namespace).
4264 * If @cancellable is not %NULL, then the operation can be cancelled by
4265 * triggering the cancellable object from another thread. If the operation
4266 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4268 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4269 * When you are done with it, release it with
4270 * g_file_attribute_info_list_unref()
4272 GFileAttributeInfoList *
4273 g_file_query_writable_namespaces (GFile *file,
4274 GCancellable *cancellable,
4279 GFileAttributeInfoList *list;
4281 g_return_val_if_fail (G_IS_FILE (file), NULL);
4283 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4286 iface = G_FILE_GET_IFACE (file);
4288 if (iface->query_writable_namespaces == NULL)
4289 return g_file_attribute_info_list_new ();
4292 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4296 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4298 list = g_file_attribute_info_list_new ();
4299 g_error_free (my_error);
4302 g_propagate_error (error, my_error);
4309 * g_file_set_attribute:
4310 * @file: input #GFile
4311 * @attribute: a string containing the attribute's name
4312 * @type: The type of the attribute
4313 * @value_p: (allow-none): a pointer to the value (or the pointer
4314 * itself if the type is a pointer type)
4315 * @flags: a set of #GFileQueryInfoFlags
4316 * @cancellable: (allow-none): optional #GCancellable object,
4318 * @error: a #GError, or %NULL
4320 * Sets an attribute in the file with attribute name @attribute to @value.
4322 * Some attributes can be unset by setting @attribute to
4323 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4325 * If @cancellable is not %NULL, then the operation can be cancelled by
4326 * triggering the cancellable object from another thread. If the operation
4327 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4329 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4332 g_file_set_attribute (GFile *file,
4333 const gchar *attribute,
4334 GFileAttributeType type,
4336 GFileQueryInfoFlags flags,
4337 GCancellable *cancellable,
4342 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4343 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4345 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4348 iface = G_FILE_GET_IFACE (file);
4350 if (iface->set_attribute == NULL)
4352 g_set_error_literal (error, G_IO_ERROR,
4353 G_IO_ERROR_NOT_SUPPORTED,
4354 _("Operation not supported"));
4358 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4362 * g_file_set_attributes_from_info:
4363 * @file: input #GFile
4364 * @info: a #GFileInfo
4365 * @flags: #GFileQueryInfoFlags
4366 * @cancellable: (allow-none): optional #GCancellable object,
4368 * @error: a #GError, or %NULL
4370 * Tries to set all attributes in the #GFileInfo on the target
4371 * values, not stopping on the first error.
4373 * If there is any error during this operation then @error will
4374 * be set to the first error. Error on particular fields are flagged
4375 * by setting the "status" field in the attribute value to
4376 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4377 * also detect further errors.
4379 * If @cancellable is not %NULL, then the operation can be cancelled by
4380 * triggering the cancellable object from another thread. If the operation
4381 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4383 * Returns: %FALSE if there was any error, %TRUE otherwise.
4386 g_file_set_attributes_from_info (GFile *file,
4388 GFileQueryInfoFlags flags,
4389 GCancellable *cancellable,
4394 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4395 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4397 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4400 g_file_info_clear_status (info);
4402 iface = G_FILE_GET_IFACE (file);
4404 return (* iface->set_attributes_from_info) (file,
4412 g_file_real_set_attributes_from_info (GFile *file,
4414 GFileQueryInfoFlags flags,
4415 GCancellable *cancellable,
4421 GFileAttributeValue *value;
4425 attributes = g_file_info_list_attributes (info, NULL);
4427 for (i = 0; attributes[i] != NULL; i++)
4429 value = _g_file_info_get_attribute_value (info, attributes[i]);
4431 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4434 if (!g_file_set_attribute (file, attributes[i],
4435 value->type, _g_file_attribute_value_peek_as_pointer (value),
4436 flags, cancellable, error))
4438 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4440 /* Don't set error multiple times */
4444 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4447 g_strfreev (attributes);
4453 * g_file_set_attributes_async:
4454 * @file: input #GFile
4455 * @info: a #GFileInfo
4456 * @flags: a #GFileQueryInfoFlags
4457 * @io_priority: the [I/O priority][io-priority] of the request
4458 * @cancellable: (allow-none): optional #GCancellable object,
4460 * @callback: (scope async): a #GAsyncReadyCallback
4461 * @user_data: (closure): a #gpointer
4463 * Asynchronously sets the attributes of @file with @info.
4465 * For more details, see g_file_set_attributes_from_info(),
4466 * which is the synchronous version of this call.
4468 * When the operation is finished, @callback will be called.
4469 * You can then call g_file_set_attributes_finish() to get
4470 * the result of the operation.
4473 g_file_set_attributes_async (GFile *file,
4475 GFileQueryInfoFlags flags,
4477 GCancellable *cancellable,
4478 GAsyncReadyCallback callback,
4483 g_return_if_fail (G_IS_FILE (file));
4484 g_return_if_fail (G_IS_FILE_INFO (info));
4486 iface = G_FILE_GET_IFACE (file);
4487 (* iface->set_attributes_async) (file,
4497 * g_file_set_attributes_finish:
4498 * @file: input #GFile
4499 * @result: a #GAsyncResult
4500 * @info: (out) (transfer full): a #GFileInfo
4501 * @error: a #GError, or %NULL
4503 * Finishes setting an attribute started in g_file_set_attributes_async().
4505 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4508 g_file_set_attributes_finish (GFile *file,
4509 GAsyncResult *result,
4515 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4516 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4518 /* No standard handling of errors here, as we must set info even
4521 iface = G_FILE_GET_IFACE (file);
4522 return (* iface->set_attributes_finish) (file, result, info, error);
4526 * g_file_set_attribute_string:
4527 * @file: input #GFile
4528 * @attribute: a string containing the attribute's name
4529 * @value: a string containing the attribute's value
4530 * @flags: #GFileQueryInfoFlags
4531 * @cancellable: (allow-none): optional #GCancellable object,
4533 * @error: a #GError, or %NULL
4535 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4536 * If @attribute is of a different type, this operation will fail.
4538 * If @cancellable is not %NULL, then the operation can be cancelled by
4539 * triggering the cancellable object from another thread. If the operation
4540 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4542 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4545 g_file_set_attribute_string (GFile *file,
4546 const char *attribute,
4548 GFileQueryInfoFlags flags,
4549 GCancellable *cancellable,
4552 return g_file_set_attribute (file, attribute,
4553 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4554 flags, cancellable, error);
4558 * g_file_set_attribute_byte_string:
4559 * @file: input #GFile
4560 * @attribute: a string containing the attribute's name
4561 * @value: a string containing the attribute's new value
4562 * @flags: a #GFileQueryInfoFlags
4563 * @cancellable: (allow-none): optional #GCancellable object,
4565 * @error: a #GError, or %NULL
4567 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4568 * If @attribute is of a different type, this operation will fail,
4571 * If @cancellable is not %NULL, then the operation can be cancelled by
4572 * triggering the cancellable object from another thread. If the operation
4573 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4575 * Returns: %TRUE if the @attribute was successfully set to @value
4576 * in the @file, %FALSE otherwise.
4579 g_file_set_attribute_byte_string (GFile *file,
4580 const gchar *attribute,
4582 GFileQueryInfoFlags flags,
4583 GCancellable *cancellable,
4586 return g_file_set_attribute (file, attribute,
4587 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4588 flags, cancellable, error);
4592 * g_file_set_attribute_uint32:
4593 * @file: input #GFile
4594 * @attribute: a string containing the attribute's name
4595 * @value: a #guint32 containing the attribute's new value
4596 * @flags: a #GFileQueryInfoFlags
4597 * @cancellable: (allow-none): optional #GCancellable object,
4599 * @error: a #GError, or %NULL
4601 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4602 * If @attribute is of a different type, this operation will fail.
4604 * If @cancellable is not %NULL, then the operation can be cancelled by
4605 * triggering the cancellable object from another thread. If the operation
4606 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4608 * Returns: %TRUE if the @attribute was successfully set to @value
4609 * in the @file, %FALSE otherwise.
4612 g_file_set_attribute_uint32 (GFile *file,
4613 const gchar *attribute,
4615 GFileQueryInfoFlags flags,
4616 GCancellable *cancellable,
4619 return g_file_set_attribute (file, attribute,
4620 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4621 flags, cancellable, error);
4625 * g_file_set_attribute_int32:
4626 * @file: input #GFile
4627 * @attribute: a string containing the attribute's name
4628 * @value: a #gint32 containing the attribute's new value
4629 * @flags: a #GFileQueryInfoFlags
4630 * @cancellable: (allow-none): optional #GCancellable object,
4632 * @error: a #GError, or %NULL
4634 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4635 * If @attribute is of a different type, this operation will fail.
4637 * If @cancellable is not %NULL, then the operation can be cancelled by
4638 * triggering the cancellable object from another thread. If the operation
4639 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4641 * Returns: %TRUE if the @attribute was successfully set to @value
4642 * in the @file, %FALSE otherwise.
4645 g_file_set_attribute_int32 (GFile *file,
4646 const gchar *attribute,
4648 GFileQueryInfoFlags flags,
4649 GCancellable *cancellable,
4652 return g_file_set_attribute (file, attribute,
4653 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4654 flags, cancellable, error);
4658 * g_file_set_attribute_uint64:
4659 * @file: input #GFile
4660 * @attribute: a string containing the attribute's name
4661 * @value: a #guint64 containing the attribute's new value
4662 * @flags: a #GFileQueryInfoFlags
4663 * @cancellable: (allow-none): optional #GCancellable object,
4665 * @error: a #GError, or %NULL
4667 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4668 * If @attribute is of a different type, this operation will fail.
4670 * If @cancellable is not %NULL, then the operation can be cancelled by
4671 * triggering the cancellable object from another thread. If the operation
4672 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4674 * Returns: %TRUE if the @attribute was successfully set to @value
4675 * in the @file, %FALSE otherwise.
4678 g_file_set_attribute_uint64 (GFile *file,
4679 const gchar *attribute,
4681 GFileQueryInfoFlags flags,
4682 GCancellable *cancellable,
4685 return g_file_set_attribute (file, attribute,
4686 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4687 flags, cancellable, error);
4691 * g_file_set_attribute_int64:
4692 * @file: input #GFile
4693 * @attribute: a string containing the attribute's name
4694 * @value: a #guint64 containing the attribute's new value
4695 * @flags: a #GFileQueryInfoFlags
4696 * @cancellable: (allow-none): optional #GCancellable object,
4698 * @error: a #GError, or %NULL
4700 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4701 * If @attribute is of a different type, this operation will fail.
4703 * If @cancellable is not %NULL, then the operation can be cancelled by
4704 * triggering the cancellable object from another thread. If the operation
4705 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4707 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4710 g_file_set_attribute_int64 (GFile *file,
4711 const gchar *attribute,
4713 GFileQueryInfoFlags flags,
4714 GCancellable *cancellable,
4717 return g_file_set_attribute (file, attribute,
4718 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4719 flags, cancellable, error);
4723 * g_file_mount_mountable:
4724 * @file: input #GFile
4725 * @flags: flags affecting the operation
4726 * @mount_operation: (allow-none): a #GMountOperation,
4727 * or %NULL to avoid user interaction
4728 * @cancellable: (allow-none): optional #GCancellable object,
4730 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4731 * when the request is satisfied, or %NULL
4732 * @user_data: (closure): the data to pass to callback function
4734 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4735 * Using @mount_operation, you can request callbacks when, for instance,
4736 * passwords are needed during authentication.
4738 * If @cancellable is not %NULL, then the operation can be cancelled by
4739 * triggering the cancellable object from another thread. If the operation
4740 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4742 * When the operation is finished, @callback will be called.
4743 * You can then call g_file_mount_mountable_finish() to get
4744 * the result of the operation.
4747 g_file_mount_mountable (GFile *file,
4748 GMountMountFlags flags,
4749 GMountOperation *mount_operation,
4750 GCancellable *cancellable,
4751 GAsyncReadyCallback callback,
4756 g_return_if_fail (G_IS_FILE (file));
4758 iface = G_FILE_GET_IFACE (file);
4760 if (iface->mount_mountable == NULL)
4762 g_task_report_new_error (file, callback, user_data,
4763 g_file_mount_mountable,
4764 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4765 _("Operation not supported"));
4769 (* iface->mount_mountable) (file,
4778 * g_file_mount_mountable_finish:
4779 * @file: input #GFile
4780 * @result: a #GAsyncResult
4781 * @error: a #GError, or %NULL
4783 * Finishes a mount operation. See g_file_mount_mountable() for details.
4785 * Finish an asynchronous mount operation that was started
4786 * with g_file_mount_mountable().
4788 * Returns: (transfer full): a #GFile or %NULL on error.
4789 * Free the returned object with g_object_unref().
4792 g_file_mount_mountable_finish (GFile *file,
4793 GAsyncResult *result,
4798 g_return_val_if_fail (G_IS_FILE (file), NULL);
4799 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4801 if (g_async_result_legacy_propagate_error (result, error))
4803 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4804 return g_task_propagate_pointer (G_TASK (result), error);
4806 iface = G_FILE_GET_IFACE (file);
4807 return (* iface->mount_mountable_finish) (file, result, error);
4811 * g_file_unmount_mountable:
4812 * @file: input #GFile
4813 * @flags: flags affecting the operation
4814 * @cancellable: (allow-none): optional #GCancellable object,
4816 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4817 * when the request is satisfied, or %NULL
4818 * @user_data: (closure): the data to pass to callback function
4820 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4822 * If @cancellable is not %NULL, then the operation can be cancelled by
4823 * triggering the cancellable object from another thread. If the operation
4824 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4826 * When the operation is finished, @callback will be called.
4827 * You can then call g_file_unmount_mountable_finish() to get
4828 * the result of the operation.
4830 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4833 g_file_unmount_mountable (GFile *file,
4834 GMountUnmountFlags flags,
4835 GCancellable *cancellable,
4836 GAsyncReadyCallback callback,
4841 g_return_if_fail (G_IS_FILE (file));
4843 iface = G_FILE_GET_IFACE (file);
4845 if (iface->unmount_mountable == NULL)
4847 g_task_report_new_error (file, callback, user_data,
4848 g_file_unmount_mountable_with_operation,
4849 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4850 _("Operation not supported"));
4854 (* iface->unmount_mountable) (file,
4862 * g_file_unmount_mountable_finish:
4863 * @file: input #GFile
4864 * @result: a #GAsyncResult
4865 * @error: a #GError, or %NULL
4867 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4869 * Finish an asynchronous unmount operation that was started
4870 * with g_file_unmount_mountable().
4872 * Returns: %TRUE if the operation finished successfully.
4875 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4879 g_file_unmount_mountable_finish (GFile *file,
4880 GAsyncResult *result,
4885 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4886 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4888 if (g_async_result_legacy_propagate_error (result, error))
4890 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4891 return g_task_propagate_boolean (G_TASK (result), error);
4893 iface = G_FILE_GET_IFACE (file);
4894 return (* iface->unmount_mountable_finish) (file, result, error);
4898 * g_file_unmount_mountable_with_operation:
4899 * @file: input #GFile
4900 * @flags: flags affecting the operation
4901 * @mount_operation: (allow-none): a #GMountOperation,
4902 * or %NULL to avoid user interaction
4903 * @cancellable: (allow-none): optional #GCancellable object,
4905 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4906 * when the request is satisfied, or %NULL
4907 * @user_data: (closure): the data to pass to callback function
4909 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4911 * If @cancellable is not %NULL, then the operation can be cancelled by
4912 * triggering the cancellable object from another thread. If the operation
4913 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4915 * When the operation is finished, @callback will be called.
4916 * You can then call g_file_unmount_mountable_finish() to get
4917 * the result of the operation.
4922 g_file_unmount_mountable_with_operation (GFile *file,
4923 GMountUnmountFlags flags,
4924 GMountOperation *mount_operation,
4925 GCancellable *cancellable,
4926 GAsyncReadyCallback callback,
4931 g_return_if_fail (G_IS_FILE (file));
4933 iface = G_FILE_GET_IFACE (file);
4935 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4937 g_task_report_new_error (file, callback, user_data,
4938 g_file_unmount_mountable_with_operation,
4939 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4940 _("Operation not supported"));
4944 if (iface->unmount_mountable_with_operation != NULL)
4945 (* iface->unmount_mountable_with_operation) (file,
4952 (* iface->unmount_mountable) (file,
4960 * g_file_unmount_mountable_with_operation_finish:
4961 * @file: input #GFile
4962 * @result: a #GAsyncResult
4963 * @error: a #GError, or %NULL
4965 * Finishes an unmount operation,
4966 * see g_file_unmount_mountable_with_operation() for details.
4968 * Finish an asynchronous unmount operation that was started
4969 * with g_file_unmount_mountable_with_operation().
4971 * Returns: %TRUE if the operation finished successfully.
4977 g_file_unmount_mountable_with_operation_finish (GFile *file,
4978 GAsyncResult *result,
4983 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4984 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4986 if (g_async_result_legacy_propagate_error (result, error))
4988 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4989 return g_task_propagate_boolean (G_TASK (result), error);
4991 iface = G_FILE_GET_IFACE (file);
4992 if (iface->unmount_mountable_with_operation_finish != NULL)
4993 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4995 return (* iface->unmount_mountable_finish) (file, result, error);
4999 * g_file_eject_mountable:
5000 * @file: input #GFile
5001 * @flags: flags affecting the operation
5002 * @cancellable: (allow-none): optional #GCancellable object,
5004 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5005 * when the request is satisfied, or %NULL
5006 * @user_data: (closure): the data to pass to callback function
5008 * Starts an asynchronous eject on a mountable.
5009 * When this operation has completed, @callback will be called with
5010 * @user_user data, and the operation can be finalized with
5011 * g_file_eject_mountable_finish().
5013 * If @cancellable is not %NULL, then the operation can be cancelled by
5014 * triggering the cancellable object from another thread. If the operation
5015 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5017 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5020 g_file_eject_mountable (GFile *file,
5021 GMountUnmountFlags flags,
5022 GCancellable *cancellable,
5023 GAsyncReadyCallback callback,
5028 g_return_if_fail (G_IS_FILE (file));
5030 iface = G_FILE_GET_IFACE (file);
5032 if (iface->eject_mountable == NULL)
5034 g_task_report_new_error (file, callback, user_data,
5035 g_file_eject_mountable_with_operation,
5036 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5037 _("Operation not supported"));
5041 (* iface->eject_mountable) (file,
5049 * g_file_eject_mountable_finish:
5050 * @file: input #GFile
5051 * @result: a #GAsyncResult
5052 * @error: a #GError, or %NULL
5054 * Finishes an asynchronous eject operation started by
5055 * g_file_eject_mountable().
5057 * Returns: %TRUE if the @file was ejected successfully.
5060 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5064 g_file_eject_mountable_finish (GFile *file,
5065 GAsyncResult *result,
5070 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5071 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5073 if (g_async_result_legacy_propagate_error (result, error))
5075 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5076 return g_task_propagate_boolean (G_TASK (result), error);
5078 iface = G_FILE_GET_IFACE (file);
5079 return (* iface->eject_mountable_finish) (file, result, error);
5083 * g_file_eject_mountable_with_operation:
5084 * @file: input #GFile
5085 * @flags: flags affecting the operation
5086 * @mount_operation: (allow-none): a #GMountOperation,
5087 * or %NULL to avoid user interaction
5088 * @cancellable: (allow-none): optional #GCancellable object,
5090 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5091 * when the request is satisfied, or %NULL
5092 * @user_data: (closure): the data to pass to callback function
5094 * Starts an asynchronous eject on a mountable.
5095 * When this operation has completed, @callback will be called with
5096 * @user_user data, and the operation can be finalized with
5097 * g_file_eject_mountable_with_operation_finish().
5099 * If @cancellable is not %NULL, then the operation can be cancelled by
5100 * triggering the cancellable object from another thread. If the operation
5101 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5106 g_file_eject_mountable_with_operation (GFile *file,
5107 GMountUnmountFlags flags,
5108 GMountOperation *mount_operation,
5109 GCancellable *cancellable,
5110 GAsyncReadyCallback callback,
5115 g_return_if_fail (G_IS_FILE (file));
5117 iface = G_FILE_GET_IFACE (file);
5119 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5121 g_task_report_new_error (file, callback, user_data,
5122 g_file_eject_mountable_with_operation,
5123 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5124 _("Operation not supported"));
5128 if (iface->eject_mountable_with_operation != NULL)
5129 (* iface->eject_mountable_with_operation) (file,
5136 (* iface->eject_mountable) (file,
5144 * g_file_eject_mountable_with_operation_finish:
5145 * @file: input #GFile
5146 * @result: a #GAsyncResult
5147 * @error: a #GError, or %NULL
5149 * Finishes an asynchronous eject operation started by
5150 * g_file_eject_mountable_with_operation().
5152 * Returns: %TRUE if the @file was ejected successfully.
5158 g_file_eject_mountable_with_operation_finish (GFile *file,
5159 GAsyncResult *result,
5164 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5165 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5167 if (g_async_result_legacy_propagate_error (result, error))
5169 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5170 return g_task_propagate_boolean (G_TASK (result), error);
5172 iface = G_FILE_GET_IFACE (file);
5173 if (iface->eject_mountable_with_operation_finish != NULL)
5174 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5176 return (* iface->eject_mountable_finish) (file, result, error);
5180 * g_file_monitor_directory:
5181 * @file: input #GFile
5182 * @flags: a set of #GFileMonitorFlags
5183 * @cancellable: (allow-none): optional #GCancellable object,
5185 * @error: a #GError, or %NULL
5187 * Obtains a directory monitor for the given file.
5188 * This may fail if directory monitoring is not supported.
5190 * If @cancellable is not %NULL, then the operation can be cancelled by
5191 * triggering the cancellable object from another thread. If the operation
5192 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5194 * It does not make sense for @flags to contain
5195 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5196 * directories. It is not possible to monitor all the files in a
5197 * directory for changes made via hard links; if you want to do this then
5198 * you must register individual watches with g_file_monitor().
5200 * Virtual: monitor_dir
5201 * Returns: (transfer full): a #GFileMonitor for the given @file,
5202 * or %NULL on error.
5203 * Free the returned object with g_object_unref().
5206 g_file_monitor_directory (GFile *file,
5207 GFileMonitorFlags flags,
5208 GCancellable *cancellable,
5213 g_return_val_if_fail (G_IS_FILE (file), NULL);
5214 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5216 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5219 iface = G_FILE_GET_IFACE (file);
5221 if (iface->monitor_dir == NULL)
5223 g_set_error_literal (error, G_IO_ERROR,
5224 G_IO_ERROR_NOT_SUPPORTED,
5225 _("Operation not supported"));
5229 return (* iface->monitor_dir) (file, flags, cancellable, error);
5233 * g_file_monitor_file:
5234 * @file: input #GFile
5235 * @flags: a set of #GFileMonitorFlags
5236 * @cancellable: (allow-none): optional #GCancellable object,
5238 * @error: a #GError, or %NULL
5240 * Obtains a file monitor for the given file. If no file notification
5241 * mechanism exists, then regular polling of the file is used.
5243 * If @cancellable is not %NULL, then the operation can be cancelled by
5244 * triggering the cancellable object from another thread. If the operation
5245 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5247 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5248 * will also attempt to report changes made to the file via another
5249 * filename (ie, a hard link). Without this flag, you can only rely on
5250 * changes made through the filename contained in @file to be
5251 * reported. Using this flag may result in an increase in resource
5252 * usage, and may not have any effect depending on the #GFileMonitor
5253 * backend and/or filesystem type.
5255 * Returns: (transfer full): a #GFileMonitor for the given @file,
5256 * or %NULL on error.
5257 * Free the returned object with g_object_unref().
5260 g_file_monitor_file (GFile *file,
5261 GFileMonitorFlags flags,
5262 GCancellable *cancellable,
5266 GFileMonitor *monitor;
5268 g_return_val_if_fail (G_IS_FILE (file), NULL);
5270 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5273 iface = G_FILE_GET_IFACE (file);
5277 if (iface->monitor_file)
5278 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5280 /* Fallback to polling */
5281 if (monitor == NULL)
5282 monitor = _g_poll_file_monitor_new (file);
5289 * @file: input #GFile
5290 * @flags: a set of #GFileMonitorFlags
5291 * @cancellable: (allow-none): optional #GCancellable object,
5293 * @error: a #GError, or %NULL
5295 * Obtains a file or directory monitor for the given file,
5296 * depending on the type of the file.
5298 * If @cancellable is not %NULL, then the operation can be cancelled by
5299 * triggering the cancellable object from another thread. If the operation
5300 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5302 * Returns: (transfer full): a #GFileMonitor for the given @file,
5303 * or %NULL on error.
5304 * Free the returned object with g_object_unref().
5309 g_file_monitor (GFile *file,
5310 GFileMonitorFlags flags,
5311 GCancellable *cancellable,
5314 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5315 return g_file_monitor_directory (file,
5316 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5317 cancellable, error);
5319 return g_file_monitor_file (file, flags, cancellable, error);
5322 /********************************************
5323 * Default implementation of async ops *
5324 ********************************************/
5328 GFileQueryInfoFlags flags;
5329 } QueryInfoAsyncData;
5332 query_info_data_free (QueryInfoAsyncData *data)
5334 g_free (data->attributes);
5339 query_info_async_thread (GTask *task,
5342 GCancellable *cancellable)
5344 QueryInfoAsyncData *data = task_data;
5346 GError *error = NULL;
5348 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5350 g_task_return_pointer (task, info, g_object_unref);
5352 g_task_return_error (task, error);
5356 g_file_real_query_info_async (GFile *file,
5357 const char *attributes,
5358 GFileQueryInfoFlags flags,
5360 GCancellable *cancellable,
5361 GAsyncReadyCallback callback,
5365 QueryInfoAsyncData *data;
5367 data = g_new0 (QueryInfoAsyncData, 1);
5368 data->attributes = g_strdup (attributes);
5369 data->flags = flags;
5371 task = g_task_new (file, cancellable, callback, user_data);
5372 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5373 g_task_set_priority (task, io_priority);
5374 g_task_run_in_thread (task, query_info_async_thread);
5375 g_object_unref (task);
5379 g_file_real_query_info_finish (GFile *file,
5383 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5385 return g_task_propagate_pointer (G_TASK (res), error);
5389 query_filesystem_info_async_thread (GTask *task,
5392 GCancellable *cancellable)
5394 const char *attributes = task_data;
5396 GError *error = NULL;
5398 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5400 g_task_return_pointer (task, info, g_object_unref);
5402 g_task_return_error (task, error);
5406 g_file_real_query_filesystem_info_async (GFile *file,
5407 const char *attributes,
5409 GCancellable *cancellable,
5410 GAsyncReadyCallback callback,
5415 task = g_task_new (file, cancellable, callback, user_data);
5416 g_task_set_task_data (task, g_strdup (attributes), g_free);
5417 g_task_set_priority (task, io_priority);
5418 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5419 g_object_unref (task);
5423 g_file_real_query_filesystem_info_finish (GFile *file,
5427 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5429 return g_task_propagate_pointer (G_TASK (res), error);
5433 enumerate_children_async_thread (GTask *task,
5436 GCancellable *cancellable)
5438 QueryInfoAsyncData *data = task_data;
5439 GFileEnumerator *enumerator;
5440 GError *error = NULL;
5442 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5444 g_task_return_error (task, error);
5446 g_task_return_pointer (task, enumerator, g_object_unref);
5450 g_file_real_enumerate_children_async (GFile *file,
5451 const char *attributes,
5452 GFileQueryInfoFlags flags,
5454 GCancellable *cancellable,
5455 GAsyncReadyCallback callback,
5459 QueryInfoAsyncData *data;
5461 data = g_new0 (QueryInfoAsyncData, 1);
5462 data->attributes = g_strdup (attributes);
5463 data->flags = flags;
5465 task = g_task_new (file, cancellable, callback, user_data);
5466 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5467 g_task_set_priority (task, io_priority);
5468 g_task_run_in_thread (task, enumerate_children_async_thread);
5469 g_object_unref (task);
5472 static GFileEnumerator *
5473 g_file_real_enumerate_children_finish (GFile *file,
5477 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5479 return g_task_propagate_pointer (G_TASK (res), error);
5483 open_read_async_thread (GTask *task,
5486 GCancellable *cancellable)
5488 GFileInputStream *stream;
5489 GError *error = NULL;
5491 stream = g_file_read (G_FILE (object), cancellable, &error);
5493 g_task_return_pointer (task, stream, g_object_unref);
5495 g_task_return_error (task, error);
5499 g_file_real_read_async (GFile *file,
5501 GCancellable *cancellable,
5502 GAsyncReadyCallback callback,
5507 task = g_task_new (file, cancellable, callback, user_data);
5508 g_task_set_priority (task, io_priority);
5509 g_task_run_in_thread (task, open_read_async_thread);
5510 g_object_unref (task);
5513 static GFileInputStream *
5514 g_file_real_read_finish (GFile *file,
5518 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5520 return g_task_propagate_pointer (G_TASK (res), error);
5524 append_to_async_thread (GTask *task,
5525 gpointer source_object,
5527 GCancellable *cancellable)
5529 GFileCreateFlags *data = task_data;
5530 GFileOutputStream *stream;
5531 GError *error = NULL;
5533 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5535 g_task_return_pointer (task, stream, g_object_unref);
5537 g_task_return_error (task, error);
5541 g_file_real_append_to_async (GFile *file,
5542 GFileCreateFlags flags,
5544 GCancellable *cancellable,
5545 GAsyncReadyCallback callback,
5548 GFileCreateFlags *data;
5551 data = g_new0 (GFileCreateFlags, 1);
5554 task = g_task_new (file, cancellable, callback, user_data);
5555 g_task_set_task_data (task, data, g_free);
5556 g_task_set_priority (task, io_priority);
5558 g_task_run_in_thread (task, append_to_async_thread);
5559 g_object_unref (task);
5562 static GFileOutputStream *
5563 g_file_real_append_to_finish (GFile *file,
5567 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5569 return g_task_propagate_pointer (G_TASK (res), error);
5573 create_async_thread (GTask *task,
5574 gpointer source_object,
5576 GCancellable *cancellable)
5578 GFileCreateFlags *data = task_data;
5579 GFileOutputStream *stream;
5580 GError *error = NULL;
5582 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5584 g_task_return_pointer (task, stream, g_object_unref);
5586 g_task_return_error (task, error);
5590 g_file_real_create_async (GFile *file,
5591 GFileCreateFlags flags,
5593 GCancellable *cancellable,
5594 GAsyncReadyCallback callback,
5597 GFileCreateFlags *data;
5600 data = g_new0 (GFileCreateFlags, 1);
5603 task = g_task_new (file, cancellable, callback, user_data);
5604 g_task_set_task_data (task, data, g_free);
5605 g_task_set_priority (task, io_priority);
5607 g_task_run_in_thread (task, create_async_thread);
5608 g_object_unref (task);
5611 static GFileOutputStream *
5612 g_file_real_create_finish (GFile *file,
5616 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5618 return g_task_propagate_pointer (G_TASK (res), error);
5622 GFileOutputStream *stream;
5624 gboolean make_backup;
5625 GFileCreateFlags flags;
5629 replace_async_data_free (ReplaceAsyncData *data)
5632 g_object_unref (data->stream);
5633 g_free (data->etag);
5638 replace_async_thread (GTask *task,
5639 gpointer source_object,
5641 GCancellable *cancellable)
5643 GFileOutputStream *stream;
5644 ReplaceAsyncData *data = task_data;
5645 GError *error = NULL;
5647 stream = g_file_replace (G_FILE (source_object),
5655 g_task_return_pointer (task, stream, g_object_unref);
5657 g_task_return_error (task, error);
5661 g_file_real_replace_async (GFile *file,
5663 gboolean make_backup,
5664 GFileCreateFlags flags,
5666 GCancellable *cancellable,
5667 GAsyncReadyCallback callback,
5671 ReplaceAsyncData *data;
5673 data = g_new0 (ReplaceAsyncData, 1);
5674 data->etag = g_strdup (etag);
5675 data->make_backup = make_backup;
5676 data->flags = flags;
5678 task = g_task_new (file, cancellable, callback, user_data);
5679 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5680 g_task_set_priority (task, io_priority);
5682 g_task_run_in_thread (task, replace_async_thread);
5683 g_object_unref (task);
5686 static GFileOutputStream *
5687 g_file_real_replace_finish (GFile *file,
5691 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5693 return g_task_propagate_pointer (G_TASK (res), error);
5697 delete_async_thread (GTask *task,
5700 GCancellable *cancellable)
5702 GError *error = NULL;
5704 if (g_file_delete (G_FILE (object), cancellable, &error))
5705 g_task_return_boolean (task, TRUE);
5707 g_task_return_error (task, error);
5711 g_file_real_delete_async (GFile *file,
5713 GCancellable *cancellable,
5714 GAsyncReadyCallback callback,
5719 task = g_task_new (file, cancellable, callback, user_data);
5720 g_task_set_priority (task, io_priority);
5721 g_task_run_in_thread (task, delete_async_thread);
5722 g_object_unref (task);
5726 g_file_real_delete_finish (GFile *file,
5730 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5732 return g_task_propagate_boolean (G_TASK (res), error);
5736 trash_async_thread (GTask *task,
5739 GCancellable *cancellable)
5741 GError *error = NULL;
5743 if (g_file_trash (G_FILE (object), cancellable, &error))
5744 g_task_return_boolean (task, TRUE);
5746 g_task_return_error (task, error);
5750 g_file_real_trash_async (GFile *file,
5752 GCancellable *cancellable,
5753 GAsyncReadyCallback callback,
5758 task = g_task_new (file, cancellable, callback, user_data);
5759 g_task_set_priority (task, io_priority);
5760 g_task_run_in_thread (task, trash_async_thread);
5761 g_object_unref (task);
5765 g_file_real_trash_finish (GFile *file,
5769 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5771 return g_task_propagate_boolean (G_TASK (res), error);
5775 make_directory_async_thread (GTask *task,
5778 GCancellable *cancellable)
5780 GError *error = NULL;
5782 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5783 g_task_return_boolean (task, TRUE);
5785 g_task_return_error (task, error);
5789 g_file_real_make_directory_async (GFile *file,
5791 GCancellable *cancellable,
5792 GAsyncReadyCallback callback,
5797 task = g_task_new (file, cancellable, callback, user_data);
5798 g_task_set_priority (task, io_priority);
5799 g_task_run_in_thread (task, make_directory_async_thread);
5800 g_object_unref (task);
5804 g_file_real_make_directory_finish (GFile *file,
5808 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5810 return g_task_propagate_boolean (G_TASK (res), error);
5814 open_readwrite_async_thread (GTask *task,
5817 GCancellable *cancellable)
5819 GFileIOStream *stream;
5820 GError *error = NULL;
5822 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5825 g_task_return_error (task, error);
5827 g_task_return_pointer (task, stream, g_object_unref);
5831 g_file_real_open_readwrite_async (GFile *file,
5833 GCancellable *cancellable,
5834 GAsyncReadyCallback callback,
5839 task = g_task_new (file, cancellable, callback, user_data);
5840 g_task_set_priority (task, io_priority);
5842 g_task_run_in_thread (task, open_readwrite_async_thread);
5843 g_object_unref (task);
5846 static GFileIOStream *
5847 g_file_real_open_readwrite_finish (GFile *file,
5851 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5853 return g_task_propagate_pointer (G_TASK (res), error);
5857 create_readwrite_async_thread (GTask *task,
5860 GCancellable *cancellable)
5862 GFileCreateFlags *data = task_data;
5863 GFileIOStream *stream;
5864 GError *error = NULL;
5866 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5869 g_task_return_error (task, error);
5871 g_task_return_pointer (task, stream, g_object_unref);
5875 g_file_real_create_readwrite_async (GFile *file,
5876 GFileCreateFlags flags,
5878 GCancellable *cancellable,
5879 GAsyncReadyCallback callback,
5882 GFileCreateFlags *data;
5885 data = g_new0 (GFileCreateFlags, 1);
5888 task = g_task_new (file, cancellable, callback, user_data);
5889 g_task_set_task_data (task, data, g_free);
5890 g_task_set_priority (task, io_priority);
5892 g_task_run_in_thread (task, create_readwrite_async_thread);
5893 g_object_unref (task);
5896 static GFileIOStream *
5897 g_file_real_create_readwrite_finish (GFile *file,
5901 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5903 return g_task_propagate_pointer (G_TASK (res), error);
5908 gboolean make_backup;
5909 GFileCreateFlags flags;
5910 } ReplaceRWAsyncData;
5913 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5915 g_free (data->etag);
5920 replace_readwrite_async_thread (GTask *task,
5923 GCancellable *cancellable)
5925 GFileIOStream *stream;
5926 GError *error = NULL;
5927 ReplaceRWAsyncData *data = task_data;
5929 stream = g_file_replace_readwrite (G_FILE (object),
5937 g_task_return_error (task, error);
5939 g_task_return_pointer (task, stream, g_object_unref);
5943 g_file_real_replace_readwrite_async (GFile *file,
5945 gboolean make_backup,
5946 GFileCreateFlags flags,
5948 GCancellable *cancellable,
5949 GAsyncReadyCallback callback,
5953 ReplaceRWAsyncData *data;
5955 data = g_new0 (ReplaceRWAsyncData, 1);
5956 data->etag = g_strdup (etag);
5957 data->make_backup = make_backup;
5958 data->flags = flags;
5960 task = g_task_new (file, cancellable, callback, user_data);
5961 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5962 g_task_set_priority (task, io_priority);
5964 g_task_run_in_thread (task, replace_readwrite_async_thread);
5965 g_object_unref (task);
5968 static GFileIOStream *
5969 g_file_real_replace_readwrite_finish (GFile *file,
5973 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5975 return g_task_propagate_pointer (G_TASK (res), error);
5979 set_display_name_async_thread (GTask *task,
5982 GCancellable *cancellable)
5984 GError *error = NULL;
5985 char *name = task_data;
5988 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5991 g_task_return_error (task, error);
5993 g_task_return_pointer (task, file, g_object_unref);
5997 g_file_real_set_display_name_async (GFile *file,
5998 const char *display_name,
6000 GCancellable *cancellable,
6001 GAsyncReadyCallback callback,
6006 task = g_task_new (file, cancellable, callback, user_data);
6007 g_task_set_task_data (task, g_strdup (display_name), g_free);
6008 g_task_set_priority (task, io_priority);
6010 g_task_run_in_thread (task, set_display_name_async_thread);
6011 g_object_unref (task);
6015 g_file_real_set_display_name_finish (GFile *file,
6019 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6021 return g_task_propagate_pointer (G_TASK (res), error);
6025 GFileQueryInfoFlags flags;
6032 set_info_data_free (SetInfoAsyncData *data)
6035 g_object_unref (data->info);
6037 g_error_free (data->error);
6042 set_info_async_thread (GTask *task,
6045 GCancellable *cancellable)
6047 SetInfoAsyncData *data = task_data;
6050 data->res = g_file_set_attributes_from_info (G_FILE (object),
6058 g_file_real_set_attributes_async (GFile *file,
6060 GFileQueryInfoFlags flags,
6062 GCancellable *cancellable,
6063 GAsyncReadyCallback callback,
6067 SetInfoAsyncData *data;
6069 data = g_new0 (SetInfoAsyncData, 1);
6070 data->info = g_file_info_dup (info);
6071 data->flags = flags;
6073 task = g_task_new (file, cancellable, callback, user_data);
6074 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6075 g_task_set_priority (task, io_priority);
6077 g_task_run_in_thread (task, set_info_async_thread);
6078 g_object_unref (task);
6082 g_file_real_set_attributes_finish (GFile *file,
6087 SetInfoAsyncData *data;
6089 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6091 data = g_task_get_task_data (G_TASK (res));
6094 *info = g_object_ref (data->info);
6096 if (error != NULL && data->error)
6097 *error = g_error_copy (data->error);
6103 find_enclosing_mount_async_thread (GTask *task,
6106 GCancellable *cancellable)
6108 GError *error = NULL;
6111 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6114 g_task_return_error (task, error);
6116 g_task_return_pointer (task, mount, g_object_unref);
6120 g_file_real_find_enclosing_mount_async (GFile *file,
6122 GCancellable *cancellable,
6123 GAsyncReadyCallback callback,
6128 task = g_task_new (file, cancellable, callback, user_data);
6129 g_task_set_priority (task, io_priority);
6131 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6132 g_object_unref (task);
6136 g_file_real_find_enclosing_mount_finish (GFile *file,
6140 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6142 return g_task_propagate_pointer (G_TASK (res), error);
6149 GFileCopyFlags flags;
6150 GFileProgressCallback progress_cb;
6151 gpointer progress_cb_data;
6155 copy_async_data_free (CopyAsyncData *data)
6157 g_object_unref (data->source);
6158 g_object_unref (data->destination);
6159 g_slice_free (CopyAsyncData, data);
6163 CopyAsyncData *data;
6164 goffset current_num_bytes;
6165 goffset total_num_bytes;
6169 copy_async_progress_in_main (gpointer user_data)
6171 ProgressData *progress = user_data;
6172 CopyAsyncData *data = progress->data;
6174 data->progress_cb (progress->current_num_bytes,
6175 progress->total_num_bytes,
6176 data->progress_cb_data);
6182 copy_async_progress_callback (goffset current_num_bytes,
6183 goffset total_num_bytes,
6186 GTask *task = user_data;
6187 CopyAsyncData *data = g_task_get_task_data (task);
6188 ProgressData *progress;
6190 progress = g_new (ProgressData, 1);
6191 progress->data = data;
6192 progress->current_num_bytes = current_num_bytes;
6193 progress->total_num_bytes = total_num_bytes;
6195 g_main_context_invoke_full (g_task_get_context (task),
6196 g_task_get_priority (task),
6197 copy_async_progress_in_main,
6203 copy_async_thread (GTask *task,
6206 GCancellable *cancellable)
6208 CopyAsyncData *data = task_data;
6210 GError *error = NULL;
6212 result = g_file_copy (data->source,
6216 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6220 g_task_return_boolean (task, TRUE);
6222 g_task_return_error (task, error);
6226 g_file_real_copy_async (GFile *source,
6228 GFileCopyFlags flags,
6230 GCancellable *cancellable,
6231 GFileProgressCallback progress_callback,
6232 gpointer progress_callback_data,
6233 GAsyncReadyCallback callback,
6237 CopyAsyncData *data;
6239 data = g_slice_new (CopyAsyncData);
6240 data->source = g_object_ref (source);
6241 data->destination = g_object_ref (destination);
6242 data->flags = flags;
6243 data->progress_cb = progress_callback;
6244 data->progress_cb_data = progress_callback_data;
6246 task = g_task_new (source, cancellable, callback, user_data);
6247 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6248 g_task_set_priority (task, io_priority);
6249 g_task_run_in_thread (task, copy_async_thread);
6250 g_object_unref (task);
6254 g_file_real_copy_finish (GFile *file,
6258 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6260 return g_task_propagate_boolean (G_TASK (res), error);
6264 /********************************************
6265 * Default VFS operations *
6266 ********************************************/
6269 * g_file_new_for_path:
6270 * @path: a string containing a relative or absolute path.
6271 * The string must be encoded in the glib filename encoding.
6273 * Constructs a #GFile for a given path. This operation never
6274 * fails, but the returned object might not support any I/O
6275 * operation if @path is malformed.
6277 * Returns: (transfer full): a new #GFile for the given @path.
6278 * Free the returned object with g_object_unref().
6281 g_file_new_for_path (const char *path)
6283 g_return_val_if_fail (path != NULL, NULL);
6285 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6289 * g_file_new_for_uri:
6290 * @uri: a UTF-8 string containing a URI
6292 * Constructs a #GFile for a given URI. This operation never
6293 * fails, but the returned object might not support any I/O
6294 * operation if @uri is malformed or if the uri type is
6297 * Returns: (transfer full): a new #GFile for the given @uri.
6298 * Free the returned object with g_object_unref().
6301 g_file_new_for_uri (const char *uri)
6303 g_return_val_if_fail (uri != NULL, NULL);
6305 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6310 * @tmpl: (type filename) (allow-none): Template for the file
6311 * name, as in g_file_open_tmp(), or %NULL for a default template
6312 * @iostream: (out): on return, a #GFileIOStream for the created file
6313 * @error: a #GError, or %NULL
6315 * Opens a file in the preferred directory for temporary files (as
6316 * returned by g_get_tmp_dir()) and returns a #GFile and
6317 * #GFileIOStream pointing to it.
6319 * @tmpl should be a string in the GLib file name encoding
6320 * containing a sequence of six 'X' characters, and containing no
6321 * directory components. If it is %NULL, a default template is used.
6323 * Unlike the other #GFile constructors, this will return %NULL if
6324 * a temporary file could not be created.
6326 * Returns: (transfer full): a new #GFile.
6327 * Free the returned object with g_object_unref().
6332 g_file_new_tmp (const char *tmpl,
6333 GFileIOStream **iostream,
6339 GFileOutputStream *output;
6341 g_return_val_if_fail (iostream != NULL, NULL);
6343 fd = g_file_open_tmp (tmpl, &path, error);
6347 file = g_file_new_for_path (path);
6349 output = _g_local_file_output_stream_new (fd);
6350 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6352 g_object_unref (output);
6359 * g_file_parse_name:
6360 * @parse_name: a file name or path to be parsed
6362 * Constructs a #GFile with the given @parse_name (i.e. something
6363 * given by g_file_get_parse_name()). This operation never fails,
6364 * but the returned object might not support any I/O operation if
6365 * the @parse_name cannot be parsed.
6367 * Returns: (transfer full): a new #GFile.
6370 g_file_parse_name (const char *parse_name)
6372 g_return_val_if_fail (parse_name != NULL, NULL);
6374 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6378 is_valid_scheme_character (char c)
6380 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6383 /* Following RFC 2396, valid schemes are built like:
6384 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6387 has_valid_scheme (const char *uri)
6393 if (!g_ascii_isalpha (*p))
6398 } while (is_valid_scheme_character (*p));
6404 new_for_cmdline_arg (const gchar *arg,
6410 if (g_path_is_absolute (arg))
6411 return g_file_new_for_path (arg);
6413 if (has_valid_scheme (arg))
6414 return g_file_new_for_uri (arg);
6420 current_dir = g_get_current_dir ();
6421 filename = g_build_filename (current_dir, arg, NULL);
6422 g_free (current_dir);
6425 filename = g_build_filename (cwd, arg, NULL);
6427 file = g_file_new_for_path (filename);
6434 * g_file_new_for_commandline_arg:
6435 * @arg: a command line string
6437 * Creates a #GFile with the given argument from the command line.
6438 * The value of @arg can be either a URI, an absolute path or a
6439 * relative path resolved relative to the current working directory.
6440 * This operation never fails, but the returned object might not
6441 * support any I/O operation if @arg points to a malformed path.
6443 * Note that on Windows, this function expects its argument to be in
6444 * UTF-8 -- not the system code page. This means that you
6445 * should not use this function with string from argv as it is passed
6446 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6447 * the commandline. #GApplication also uses UTF-8 but
6448 * g_application_command_line_create_file_for_arg() may be more useful
6449 * for you there. It is also always possible to use this function with
6450 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6452 * Returns: (transfer full): a new #GFile.
6453 * Free the returned object with g_object_unref().
6456 g_file_new_for_commandline_arg (const char *arg)
6458 g_return_val_if_fail (arg != NULL, NULL);
6460 return new_for_cmdline_arg (arg, NULL);
6464 * g_file_new_for_commandline_arg_and_cwd:
6465 * @arg: a command line string
6466 * @cwd: the current working directory of the commandline
6468 * Creates a #GFile with the given argument from the command line.
6470 * This function is similar to g_file_new_for_commandline_arg() except
6471 * that it allows for passing the current working directory as an
6472 * argument instead of using the current working directory of the
6475 * This is useful if the commandline argument was given in a context
6476 * other than the invocation of the current process.
6478 * See also g_application_command_line_create_file_for_arg().
6480 * Returns: (transfer full): a new #GFile
6485 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6488 g_return_val_if_fail (arg != NULL, NULL);
6489 g_return_val_if_fail (cwd != NULL, NULL);
6491 return new_for_cmdline_arg (arg, cwd);
6495 * g_file_mount_enclosing_volume:
6496 * @location: input #GFile
6497 * @flags: flags affecting the operation
6498 * @mount_operation: (allow-none): a #GMountOperation
6499 * or %NULL to avoid user interaction
6500 * @cancellable: (allow-none): optional #GCancellable object,
6502 * @callback: (allow-none): a #GAsyncReadyCallback to call
6503 * when the request is satisfied, or %NULL
6504 * @user_data: the data to pass to callback function
6506 * Starts a @mount_operation, mounting the volume that contains
6507 * the file @location.
6509 * When this operation has completed, @callback will be called with
6510 * @user_user data, and the operation can be finalized with
6511 * g_file_mount_enclosing_volume_finish().
6513 * If @cancellable is not %NULL, then the operation can be cancelled by
6514 * triggering the cancellable object from another thread. If the operation
6515 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6518 g_file_mount_enclosing_volume (GFile *location,
6519 GMountMountFlags flags,
6520 GMountOperation *mount_operation,
6521 GCancellable *cancellable,
6522 GAsyncReadyCallback callback,
6527 g_return_if_fail (G_IS_FILE (location));
6529 iface = G_FILE_GET_IFACE (location);
6531 if (iface->mount_enclosing_volume == NULL)
6533 g_task_report_new_error (location, callback, user_data,
6534 g_file_mount_enclosing_volume,
6535 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6536 _("volume doesn't implement mount"));
6540 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6545 * g_file_mount_enclosing_volume_finish:
6546 * @location: input #GFile
6547 * @result: a #GAsyncResult
6548 * @error: a #GError, or %NULL
6550 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6552 * Returns: %TRUE if successful. If an error has occurred,
6553 * this function will return %FALSE and set @error
6554 * appropriately if present.
6557 g_file_mount_enclosing_volume_finish (GFile *location,
6558 GAsyncResult *result,
6563 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6564 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6566 if (g_async_result_legacy_propagate_error (result, error))
6568 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6569 return g_task_propagate_boolean (G_TASK (result), error);
6571 iface = G_FILE_GET_IFACE (location);
6573 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6576 /********************************************
6577 * Utility functions *
6578 ********************************************/
6581 * g_file_query_default_handler:
6582 * @file: a #GFile to open
6583 * @cancellable: optional #GCancellable object, %NULL to ignore
6584 * @error: a #GError, or %NULL
6586 * Returns the #GAppInfo that is registered as the default
6587 * application to handle the file specified by @file.
6589 * If @cancellable is not %NULL, then the operation can be cancelled by
6590 * triggering the cancellable object from another thread. If the operation
6591 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6593 * Returns: (transfer full): a #GAppInfo if the handle was found,
6594 * %NULL if there were errors.
6595 * When you are done with it, release it with g_object_unref()
6598 g_file_query_default_handler (GFile *file,
6599 GCancellable *cancellable,
6603 const char *content_type;
6608 uri_scheme = g_file_get_uri_scheme (file);
6609 if (uri_scheme && uri_scheme[0] != '\0')
6611 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6612 g_free (uri_scheme);
6614 if (appinfo != NULL)
6618 info = g_file_query_info (file,
6619 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6628 content_type = g_file_info_get_content_type (info);
6631 /* Don't use is_native(), as we want to support fuse paths if available */
6632 path = g_file_get_path (file);
6633 appinfo = g_app_info_get_default_for_type (content_type,
6638 g_object_unref (info);
6640 if (appinfo != NULL)
6643 g_set_error_literal (error, G_IO_ERROR,
6644 G_IO_ERROR_NOT_SUPPORTED,
6645 _("No application is registered as handling this file"));
6649 #define GET_CONTENT_BLOCK_SIZE 8192
6652 * g_file_load_contents:
6653 * @file: input #GFile
6654 * @cancellable: optional #GCancellable object, %NULL to ignore
6655 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6656 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6657 * or %NULL if the length is not needed
6658 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6659 * or %NULL if the entity tag is not needed
6660 * @error: a #GError, or %NULL
6662 * Loads the content of the file into memory. The data is always
6663 * zero-terminated, but this is not included in the resultant @length.
6664 * The returned @content should be freed with g_free() when no longer
6667 * If @cancellable is not %NULL, then the operation can be cancelled by
6668 * triggering the cancellable object from another thread. If the operation
6669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6671 * Returns: %TRUE if the @file's contents were successfully loaded.
6672 * %FALSE if there were errors.
6675 g_file_load_contents (GFile *file,
6676 GCancellable *cancellable,
6682 GFileInputStream *in;
6683 GByteArray *content;
6688 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6689 g_return_val_if_fail (contents != NULL, FALSE);
6691 in = g_file_read (file, cancellable, error);
6695 content = g_byte_array_new ();
6698 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6699 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6700 content->data + pos,
6701 GET_CONTENT_BLOCK_SIZE,
6702 cancellable, error)) > 0)
6705 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6712 info = g_file_input_stream_query_info (in,
6713 G_FILE_ATTRIBUTE_ETAG_VALUE,
6718 *etag_out = g_strdup (g_file_info_get_etag (info));
6719 g_object_unref (info);
6723 /* Ignore errors on close */
6724 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6725 g_object_unref (in);
6729 /* error is set already */
6730 g_byte_array_free (content, TRUE);
6737 /* Zero terminate (we got an extra byte allocated for this */
6738 content->data[pos] = 0;
6740 *contents = (char *)g_byte_array_free (content, FALSE);
6747 GFileReadMoreCallback read_more_callback;
6748 GByteArray *content;
6755 load_contents_data_free (LoadContentsData *data)
6758 g_byte_array_free (data->content, TRUE);
6759 g_free (data->etag);
6764 load_contents_close_callback (GObject *obj,
6765 GAsyncResult *close_res,
6768 GInputStream *stream = G_INPUT_STREAM (obj);
6769 LoadContentsData *data = user_data;
6771 /* Ignore errors here, we're only reading anyway */
6772 g_input_stream_close_finish (stream, close_res, NULL);
6773 g_object_unref (stream);
6775 g_task_return_boolean (data->task, TRUE);
6776 g_object_unref (data->task);
6780 load_contents_fstat_callback (GObject *obj,
6781 GAsyncResult *stat_res,
6784 GInputStream *stream = G_INPUT_STREAM (obj);
6785 LoadContentsData *data = user_data;
6788 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6792 data->etag = g_strdup (g_file_info_get_etag (info));
6793 g_object_unref (info);
6796 g_input_stream_close_async (stream, 0,
6797 g_task_get_cancellable (data->task),
6798 load_contents_close_callback, data);
6802 load_contents_read_callback (GObject *obj,
6803 GAsyncResult *read_res,
6806 GInputStream *stream = G_INPUT_STREAM (obj);
6807 LoadContentsData *data = user_data;
6808 GError *error = NULL;
6811 read_size = g_input_stream_read_finish (stream, read_res, &error);
6815 g_task_return_error (data->task, error);
6816 g_object_unref (data->task);
6818 /* Close the file ignoring any error */
6819 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6820 g_object_unref (stream);
6822 else if (read_size == 0)
6824 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6825 G_FILE_ATTRIBUTE_ETAG_VALUE,
6827 g_task_get_cancellable (data->task),
6828 load_contents_fstat_callback,
6831 else if (read_size > 0)
6833 data->pos += read_size;
6835 g_byte_array_set_size (data->content,
6836 data->pos + GET_CONTENT_BLOCK_SIZE);
6839 if (data->read_more_callback &&
6840 !data->read_more_callback ((char *)data->content->data, data->pos,
6841 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6842 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6843 G_FILE_ATTRIBUTE_ETAG_VALUE,
6845 g_task_get_cancellable (data->task),
6846 load_contents_fstat_callback,
6849 g_input_stream_read_async (stream,
6850 data->content->data + data->pos,
6851 GET_CONTENT_BLOCK_SIZE,
6853 g_task_get_cancellable (data->task),
6854 load_contents_read_callback,
6860 load_contents_open_callback (GObject *obj,
6861 GAsyncResult *open_res,
6864 GFile *file = G_FILE (obj);
6865 GFileInputStream *stream;
6866 LoadContentsData *data = user_data;
6867 GError *error = NULL;
6869 stream = g_file_read_finish (file, open_res, &error);
6873 g_byte_array_set_size (data->content,
6874 data->pos + GET_CONTENT_BLOCK_SIZE);
6875 g_input_stream_read_async (G_INPUT_STREAM (stream),
6876 data->content->data + data->pos,
6877 GET_CONTENT_BLOCK_SIZE,
6879 g_task_get_cancellable (data->task),
6880 load_contents_read_callback,
6885 g_task_return_error (data->task, error);
6886 g_object_unref (data->task);
6891 * g_file_load_partial_contents_async: (skip)
6892 * @file: input #GFile
6893 * @cancellable: optional #GCancellable object, %NULL to ignore
6894 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6895 * and to specify whether further data should be read
6896 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6897 * @user_data: the data to pass to the callback functions
6899 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6900 * be used to stop reading from the file when appropriate, else this
6901 * function will behave exactly as g_file_load_contents_async(). This
6902 * operation can be finished by g_file_load_partial_contents_finish().
6904 * Users of this function should be aware that @user_data is passed to
6905 * both the @read_more_callback and the @callback.
6907 * If @cancellable is not %NULL, then the operation can be cancelled by
6908 * triggering the cancellable object from another thread. If the operation
6909 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6912 g_file_load_partial_contents_async (GFile *file,
6913 GCancellable *cancellable,
6914 GFileReadMoreCallback read_more_callback,
6915 GAsyncReadyCallback callback,
6918 LoadContentsData *data;
6920 g_return_if_fail (G_IS_FILE (file));
6922 data = g_new0 (LoadContentsData, 1);
6923 data->read_more_callback = read_more_callback;
6924 data->content = g_byte_array_new ();
6926 data->task = g_task_new (file, cancellable, callback, user_data);
6927 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6929 g_file_read_async (file,
6931 g_task_get_cancellable (data->task),
6932 load_contents_open_callback,
6937 * g_file_load_partial_contents_finish:
6938 * @file: input #GFile
6939 * @res: a #GAsyncResult
6940 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6941 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6942 * or %NULL if the length is not needed
6943 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6944 * or %NULL if the entity tag is not needed
6945 * @error: a #GError, or %NULL
6947 * Finishes an asynchronous partial load operation that was started
6948 * with g_file_load_partial_contents_async(). The data is always
6949 * zero-terminated, but this is not included in the resultant @length.
6950 * The returned @content should be freed with g_free() when no longer
6953 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6954 * present, it will be set appropriately.
6957 g_file_load_partial_contents_finish (GFile *file,
6965 LoadContentsData *data;
6967 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6968 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6969 g_return_val_if_fail (contents != NULL, FALSE);
6971 task = G_TASK (res);
6973 if (!g_task_propagate_boolean (task, error))
6980 data = g_task_get_task_data (task);
6983 *length = data->pos;
6987 *etag_out = data->etag;
6991 /* Zero terminate */
6992 g_byte_array_set_size (data->content, data->pos + 1);
6993 data->content->data[data->pos] = 0;
6995 *contents = (char *)g_byte_array_free (data->content, FALSE);
6996 data->content = NULL;
7002 * g_file_load_contents_async:
7003 * @file: input #GFile
7004 * @cancellable: optional #GCancellable object, %NULL to ignore
7005 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7006 * @user_data: the data to pass to callback function
7008 * Starts an asynchronous load of the @file's contents.
7010 * For more details, see g_file_load_contents() which is
7011 * the synchronous version of this call.
7013 * When the load operation has completed, @callback will be called
7014 * with @user data. To finish the operation, call
7015 * g_file_load_contents_finish() with the #GAsyncResult returned by
7018 * If @cancellable is not %NULL, then the operation can be cancelled by
7019 * triggering the cancellable object from another thread. If the operation
7020 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7023 g_file_load_contents_async (GFile *file,
7024 GCancellable *cancellable,
7025 GAsyncReadyCallback callback,
7028 g_file_load_partial_contents_async (file,
7031 callback, user_data);
7035 * g_file_load_contents_finish:
7036 * @file: input #GFile
7037 * @res: a #GAsyncResult
7038 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7039 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7040 * or %NULL if the length is not needed
7041 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7042 * or %NULL if the entity tag is not needed
7043 * @error: a #GError, or %NULL
7045 * Finishes an asynchronous load of the @file's contents.
7046 * The contents are placed in @contents, and @length is set to the
7047 * size of the @contents string. The @content should be freed with
7048 * g_free() when no longer needed. If @etag_out is present, it will be
7049 * set to the new entity tag for the @file.
7051 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7052 * present, it will be set appropriately.
7055 g_file_load_contents_finish (GFile *file,
7062 return g_file_load_partial_contents_finish (file,
7071 * g_file_replace_contents:
7072 * @file: input #GFile
7073 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7074 * @length: the length of @contents in bytes
7075 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7077 * @make_backup: %TRUE if a backup should be created
7078 * @flags: a set of #GFileCreateFlags
7079 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7080 * for the document. This should be freed with g_free() when no longer
7082 * @cancellable: optional #GCancellable object, %NULL to ignore
7083 * @error: a #GError, or %NULL
7085 * Replaces the contents of @file with @contents of @length bytes.
7087 * If @etag is specified (not %NULL), any existing file must have that etag,
7088 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7090 * If @make_backup is %TRUE, this function will attempt to make a backup
7093 * If @cancellable is not %NULL, then the operation can be cancelled by
7094 * triggering the cancellable object from another thread. If the operation
7095 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7097 * The returned @new_etag can be used to verify that the file hasn't
7098 * changed the next time it is saved over.
7100 * Returns: %TRUE if successful. If an error has occurred, this function
7101 * will return %FALSE and set @error appropriately if present.
7104 g_file_replace_contents (GFile *file,
7105 const char *contents,
7108 gboolean make_backup,
7109 GFileCreateFlags flags,
7111 GCancellable *cancellable,
7114 GFileOutputStream *out;
7115 gsize pos, remainder;
7119 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7120 g_return_val_if_fail (contents != NULL, FALSE);
7122 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7128 while (remainder > 0 &&
7129 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7131 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7139 if (remainder > 0 && res < 0)
7141 /* Ignore errors on close */
7142 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7143 g_object_unref (out);
7145 /* error is set already */
7149 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7152 *new_etag = g_file_output_stream_get_etag (out);
7154 g_object_unref (out);
7165 } ReplaceContentsData;
7168 replace_contents_data_free (ReplaceContentsData *data)
7170 g_bytes_unref (data->content);
7171 g_free (data->etag);
7176 replace_contents_close_callback (GObject *obj,
7177 GAsyncResult *close_res,
7180 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7181 ReplaceContentsData *data = user_data;
7183 /* Ignore errors here, we're only reading anyway */
7184 g_output_stream_close_finish (stream, close_res, NULL);
7185 g_object_unref (stream);
7189 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7190 g_task_return_boolean (data->task, TRUE);
7192 g_object_unref (data->task);
7196 replace_contents_write_callback (GObject *obj,
7197 GAsyncResult *read_res,
7200 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7201 ReplaceContentsData *data = user_data;
7202 GError *error = NULL;
7205 write_size = g_output_stream_write_finish (stream, read_res, &error);
7207 if (write_size <= 0)
7209 /* Error or EOF, close the file */
7212 data->failed = TRUE;
7213 g_task_return_error (data->task, error);
7215 g_output_stream_close_async (stream, 0,
7216 g_task_get_cancellable (data->task),
7217 replace_contents_close_callback, data);
7219 else if (write_size > 0)
7221 const gchar *content;
7224 content = g_bytes_get_data (data->content, &length);
7225 data->pos += write_size;
7227 if (data->pos >= length)
7228 g_output_stream_close_async (stream, 0,
7229 g_task_get_cancellable (data->task),
7230 replace_contents_close_callback, data);
7232 g_output_stream_write_async (stream,
7233 content + data->pos,
7236 g_task_get_cancellable (data->task),
7237 replace_contents_write_callback,
7243 replace_contents_open_callback (GObject *obj,
7244 GAsyncResult *open_res,
7247 GFile *file = G_FILE (obj);
7248 GFileOutputStream *stream;
7249 ReplaceContentsData *data = user_data;
7250 GError *error = NULL;
7252 stream = g_file_replace_finish (file, open_res, &error);
7256 const gchar *content;
7259 content = g_bytes_get_data (data->content, &length);
7260 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7261 content + data->pos,
7264 g_task_get_cancellable (data->task),
7265 replace_contents_write_callback,
7270 g_task_return_error (data->task, error);
7271 g_object_unref (data->task);
7276 * g_file_replace_contents_async:
7277 * @file: input #GFile
7278 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7279 * @length: the length of @contents in bytes
7280 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7281 * @make_backup: %TRUE if a backup should be created
7282 * @flags: a set of #GFileCreateFlags
7283 * @cancellable: optional #GCancellable object, %NULL to ignore
7284 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7285 * @user_data: the data to pass to callback function
7287 * Starts an asynchronous replacement of @file with the given
7288 * @contents of @length bytes. @etag will replace the document's
7289 * current entity tag.
7291 * When this operation has completed, @callback will be called with
7292 * @user_user data, and the operation can be finalized with
7293 * g_file_replace_contents_finish().
7295 * If @cancellable is not %NULL, then the operation can be cancelled by
7296 * triggering the cancellable object from another thread. If the operation
7297 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7299 * If @make_backup is %TRUE, this function will attempt to
7300 * make a backup of @file.
7302 * Note that no copy of @content will be made, so it must stay valid
7303 * until @callback is called. See g_file_replace_contents_bytes_async()
7304 * for a #GBytes version that will automatically hold a reference to the
7305 * contents (without copying) for the duration of the call.
7308 g_file_replace_contents_async (GFile *file,
7309 const char *contents,
7312 gboolean make_backup,
7313 GFileCreateFlags flags,
7314 GCancellable *cancellable,
7315 GAsyncReadyCallback callback,
7320 bytes = g_bytes_new_static (contents, length);
7321 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7322 cancellable, callback, user_data);
7323 g_bytes_unref (bytes);
7327 * g_file_replace_contents_bytes_async:
7328 * @file: input #GFile
7329 * @contents: a #GBytes
7330 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7331 * @make_backup: %TRUE if a backup should be created
7332 * @flags: a set of #GFileCreateFlags
7333 * @cancellable: optional #GCancellable object, %NULL to ignore
7334 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7335 * @user_data: the data to pass to callback function
7337 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7338 * This function will keep a ref on @contents until the operation is done.
7339 * Unlike g_file_replace_contents_async() this allows forgetting about the
7340 * content without waiting for the callback.
7342 * When this operation has completed, @callback will be called with
7343 * @user_user data, and the operation can be finalized with
7344 * g_file_replace_contents_finish().
7349 g_file_replace_contents_bytes_async (GFile *file,
7352 gboolean make_backup,
7353 GFileCreateFlags flags,
7354 GCancellable *cancellable,
7355 GAsyncReadyCallback callback,
7358 ReplaceContentsData *data;
7360 g_return_if_fail (G_IS_FILE (file));
7361 g_return_if_fail (contents != NULL);
7363 data = g_new0 (ReplaceContentsData, 1);
7365 data->content = g_bytes_ref (contents);
7367 data->task = g_task_new (file, cancellable, callback, user_data);
7368 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7370 g_file_replace_async (file,
7375 g_task_get_cancellable (data->task),
7376 replace_contents_open_callback,
7381 * g_file_replace_contents_finish:
7382 * @file: input #GFile
7383 * @res: a #GAsyncResult
7384 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7385 * for the document. This should be freed with g_free() when it is no
7386 * longer needed, or %NULL
7387 * @error: a #GError, or %NULL
7389 * Finishes an asynchronous replace of the given @file. See
7390 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7391 * tag for the document, if present.
7393 * Returns: %TRUE on success, %FALSE on failure.
7396 g_file_replace_contents_finish (GFile *file,
7402 ReplaceContentsData *data;
7404 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7405 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7407 task = G_TASK (res);
7409 if (!g_task_propagate_boolean (task, error))
7412 data = g_task_get_task_data (task);
7416 *new_etag = data->etag;
7417 data->etag = NULL; /* Take ownership */
7424 g_file_real_measure_disk_usage (GFile *file,
7425 GFileMeasureFlags flags,
7426 GCancellable *cancellable,
7427 GFileMeasureProgressCallback progress_callback,
7428 gpointer progress_data,
7429 guint64 *disk_usage,
7434 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7435 "Operation not supported for the current backend.");
7441 GFileMeasureFlags flags;
7442 GFileMeasureProgressCallback progress_callback;
7443 gpointer progress_data;
7455 GFileMeasureProgressCallback callback;
7458 guint64 current_size;
7464 measure_disk_usage_invoke_progress (gpointer user_data)
7466 MeasureProgress *progress = user_data;
7468 (* progress->callback) (progress->reporting,
7469 progress->current_size, progress->num_dirs, progress->num_files,
7470 progress->user_data);
7476 measure_disk_usage_progress (gboolean reporting,
7477 guint64 current_size,
7482 MeasureProgress progress;
7483 GTask *task = user_data;
7484 MeasureTaskData *data;
7486 data = g_task_get_task_data (task);
7488 progress.callback = data->progress_callback;
7489 progress.user_data = data->progress_data;
7490 progress.reporting = reporting;
7491 progress.current_size = current_size;
7492 progress.num_dirs = num_dirs;
7493 progress.num_files = num_files;
7495 g_main_context_invoke_full (g_task_get_context (task),
7496 g_task_get_priority (task),
7497 measure_disk_usage_invoke_progress,
7498 g_memdup (&progress, sizeof progress),
7503 measure_disk_usage_thread (GTask *task,
7504 gpointer source_object,
7506 GCancellable *cancellable)
7508 MeasureTaskData *data = task_data;
7509 GError *error = NULL;
7510 MeasureResult result;
7512 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7513 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7514 &result.disk_usage, &result.num_dirs, &result.num_files,
7516 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7518 g_task_return_error (task, error);
7522 g_file_real_measure_disk_usage_async (GFile *file,
7523 GFileMeasureFlags flags,
7525 GCancellable *cancellable,
7526 GFileMeasureProgressCallback progress_callback,
7527 gpointer progress_data,
7528 GAsyncReadyCallback callback,
7531 MeasureTaskData data;
7535 data.progress_callback = progress_callback;
7536 data.progress_data = progress_data;
7538 task = g_task_new (file, cancellable, callback, user_data);
7539 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7540 g_task_set_priority (task, io_priority);
7542 g_task_run_in_thread (task, measure_disk_usage_thread);
7543 g_object_unref (task);
7547 g_file_real_measure_disk_usage_finish (GFile *file,
7548 GAsyncResult *result,
7549 guint64 *disk_usage,
7554 MeasureResult *measure_result;
7556 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7558 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7560 if (measure_result == NULL)
7564 *disk_usage = measure_result->disk_usage;
7567 *num_dirs = measure_result->num_dirs;
7570 *num_files = measure_result->num_files;
7572 g_free (measure_result);
7578 * g_file_measure_disk_usage:
7580 * @flags: #GFileMeasureFlags
7581 * @cancellable: (allow-none): optional #GCancellable
7582 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7583 * @progress_data: user_data for @progress_callback
7584 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7585 * @num_dirs: (allow-none) (out): the number of directories encountered
7586 * @num_files: (allow-none) (out): the number of non-directories encountered
7587 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7589 * Recursively measures the disk usage of @file.
7591 * This is essentially an analog of the 'du' command, but it also
7592 * reports the number of directories and non-directory files encountered
7593 * (including things like symbolic links).
7595 * By default, errors are only reported against the toplevel file
7596 * itself. Errors found while recursing are silently ignored, unless
7597 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7599 * The returned size, @disk_usage, is in bytes and should be formatted
7600 * with g_format_size() in order to get something reasonable for showing
7601 * in a user interface.
7603 * @progress_callback and @progress_data can be given to request
7604 * periodic progress updates while scanning. See the documentation for
7605 * #GFileMeasureProgressCallback for information about when and how the
7606 * callback will be invoked.
7608 * Returns: %TRUE if successful, with the out parameters set.
7609 * %FALSE otherwise, with @error set.
7614 g_file_measure_disk_usage (GFile *file,
7615 GFileMeasureFlags flags,
7616 GCancellable *cancellable,
7617 GFileMeasureProgressCallback progress_callback,
7618 gpointer progress_data,
7619 guint64 *disk_usage,
7624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7625 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7626 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7628 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7629 progress_callback, progress_data,
7630 disk_usage, num_dirs, num_files,
7635 * g_file_measure_disk_usage_async:
7637 * @flags: #GFileMeasureFlags
7638 * @io_priority: the [I/O priority][io-priority] of the request
7639 * @cancellable: (allow-none): optional #GCancellable
7640 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7641 * @progress_data: user_data for @progress_callback
7642 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7643 * @user_data: the data to pass to callback function
7645 * Recursively measures the disk usage of @file.
7647 * This is the asynchronous version of g_file_measure_disk_usage(). See
7648 * there for more information.
7653 g_file_measure_disk_usage_async (GFile *file,
7654 GFileMeasureFlags flags,
7656 GCancellable *cancellable,
7657 GFileMeasureProgressCallback progress_callback,
7658 gpointer progress_data,
7659 GAsyncReadyCallback callback,
7662 g_return_if_fail (G_IS_FILE (file));
7663 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7665 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7666 progress_callback, progress_data,
7667 callback, user_data);
7671 * g_file_measure_disk_usage_finish:
7673 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7674 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7675 * @num_dirs: (allow-none) (out): the number of directories encountered
7676 * @num_files: (allow-none) (out): the number of non-directories encountered
7677 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7679 * Collects the results from an earlier call to
7680 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7683 * Returns: %TRUE if successful, with the out parameters set.
7684 * %FALSE otherwise, with @error set.
7689 g_file_measure_disk_usage_finish (GFile *file,
7690 GAsyncResult *result,
7691 guint64 *disk_usage,
7696 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7697 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7699 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7703 * g_file_start_mountable:
7704 * @file: input #GFile
7705 * @flags: flags affecting the operation
7706 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7707 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7708 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7709 * @user_data: the data to pass to callback function
7711 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7712 * Using @start_operation, you can request callbacks when, for instance,
7713 * passwords are needed during authentication.
7715 * If @cancellable is not %NULL, then the operation can be cancelled by
7716 * triggering the cancellable object from another thread. If the operation
7717 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7719 * When the operation is finished, @callback will be called.
7720 * You can then call g_file_mount_mountable_finish() to get
7721 * the result of the operation.
7726 g_file_start_mountable (GFile *file,
7727 GDriveStartFlags flags,
7728 GMountOperation *start_operation,
7729 GCancellable *cancellable,
7730 GAsyncReadyCallback callback,
7735 g_return_if_fail (G_IS_FILE (file));
7737 iface = G_FILE_GET_IFACE (file);
7739 if (iface->start_mountable == NULL)
7741 g_task_report_new_error (file, callback, user_data,
7742 g_file_start_mountable,
7743 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7744 _("Operation not supported"));
7748 (* iface->start_mountable) (file,
7757 * g_file_start_mountable_finish:
7758 * @file: input #GFile
7759 * @result: a #GAsyncResult
7760 * @error: a #GError, or %NULL
7762 * Finishes a start operation. See g_file_start_mountable() for details.
7764 * Finish an asynchronous start operation that was started
7765 * with g_file_start_mountable().
7767 * Returns: %TRUE if the operation finished successfully. %FALSE
7773 g_file_start_mountable_finish (GFile *file,
7774 GAsyncResult *result,
7779 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7780 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7782 if (g_async_result_legacy_propagate_error (result, error))
7784 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7785 return g_task_propagate_boolean (G_TASK (result), error);
7787 iface = G_FILE_GET_IFACE (file);
7788 return (* iface->start_mountable_finish) (file, result, error);
7792 * g_file_stop_mountable:
7793 * @file: input #GFile
7794 * @flags: flags affecting the operation
7795 * @mount_operation: (allow-none): a #GMountOperation,
7796 * or %NULL to avoid user interaction.
7797 * @cancellable: (allow-none): optional #GCancellable object,
7799 * @callback: (allow-none): a #GAsyncReadyCallback to call
7800 * when the request is satisfied, or %NULL
7801 * @user_data: the data to pass to callback function
7803 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7805 * If @cancellable is not %NULL, then the operation can be cancelled by
7806 * triggering the cancellable object from another thread. If the operation
7807 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7809 * When the operation is finished, @callback will be called.
7810 * You can then call g_file_stop_mountable_finish() to get
7811 * the result of the operation.
7816 g_file_stop_mountable (GFile *file,
7817 GMountUnmountFlags flags,
7818 GMountOperation *mount_operation,
7819 GCancellable *cancellable,
7820 GAsyncReadyCallback callback,
7825 g_return_if_fail (G_IS_FILE (file));
7827 iface = G_FILE_GET_IFACE (file);
7829 if (iface->stop_mountable == NULL)
7831 g_task_report_new_error (file, callback, user_data,
7832 g_file_stop_mountable,
7833 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7834 _("Operation not supported"));
7838 (* iface->stop_mountable) (file,
7847 * g_file_stop_mountable_finish:
7848 * @file: input #GFile
7849 * @result: a #GAsyncResult
7850 * @error: a #GError, or %NULL
7852 * Finishes an stop operation, see g_file_stop_mountable() for details.
7854 * Finish an asynchronous stop operation that was started
7855 * with g_file_stop_mountable().
7857 * Returns: %TRUE if the operation finished successfully.
7863 g_file_stop_mountable_finish (GFile *file,
7864 GAsyncResult *result,
7869 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7870 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7872 if (g_async_result_legacy_propagate_error (result, error))
7874 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7875 return g_task_propagate_boolean (G_TASK (result), error);
7877 iface = G_FILE_GET_IFACE (file);
7878 return (* iface->stop_mountable_finish) (file, result, error);
7882 * g_file_poll_mountable:
7883 * @file: input #GFile
7884 * @cancellable: optional #GCancellable object, %NULL to ignore
7885 * @callback: (allow-none): a #GAsyncReadyCallback to call
7886 * when the request is satisfied, or %NULL
7887 * @user_data: the data to pass to callback function
7889 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7891 * If @cancellable is not %NULL, then the operation can be cancelled by
7892 * triggering the cancellable object from another thread. If the operation
7893 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7895 * When the operation is finished, @callback will be called.
7896 * You can then call g_file_mount_mountable_finish() to get
7897 * the result of the operation.
7902 g_file_poll_mountable (GFile *file,
7903 GCancellable *cancellable,
7904 GAsyncReadyCallback callback,
7909 g_return_if_fail (G_IS_FILE (file));
7911 iface = G_FILE_GET_IFACE (file);
7913 if (iface->poll_mountable == NULL)
7915 g_task_report_new_error (file, callback, user_data,
7916 g_file_poll_mountable,
7917 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7918 _("Operation not supported"));
7922 (* iface->poll_mountable) (file,
7929 * g_file_poll_mountable_finish:
7930 * @file: input #GFile
7931 * @result: a #GAsyncResult
7932 * @error: a #GError, or %NULL
7934 * Finishes a poll operation. See g_file_poll_mountable() for details.
7936 * Finish an asynchronous poll operation that was polled
7937 * with g_file_poll_mountable().
7939 * Returns: %TRUE if the operation finished successfully. %FALSE
7945 g_file_poll_mountable_finish (GFile *file,
7946 GAsyncResult *result,
7951 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7952 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7954 if (g_async_result_legacy_propagate_error (result, error))
7956 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7957 return g_task_propagate_boolean (G_TASK (result), error);
7959 iface = G_FILE_GET_IFACE (file);
7960 return (* iface->poll_mountable_finish) (file, result, error);
7964 * g_file_supports_thread_contexts:
7967 * Checks if @file supports
7968 * [thread-default contexts][g-main-context-push-thread-default-context].
7969 * If this returns %FALSE, you cannot perform asynchronous operations on
7970 * @file in a thread that has a thread-default context.
7972 * Returns: Whether or not @file supports thread-default contexts.
7977 g_file_supports_thread_contexts (GFile *file)
7981 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7983 iface = G_FILE_GET_IFACE (file);
7984 return iface->supports_thread_contexts;