1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
26 #include <sys/ioctl.h>
28 /* See linux.git/fs/btrfs/ioctl.h */
29 #define BTRFS_IOCTL_MAGIC 0x94
30 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
41 #include <sys/types.h>
44 #include "glib/gstdio.h"
46 #include "glib-unix.h"
50 #include "gfileattribute-priv.h"
51 #include "gfiledescriptorbased.h"
52 #include "gpollfilemonitor.h"
54 #include "gfileinputstream.h"
55 #include "gfileoutputstream.h"
56 #include "glocalfileoutputstream.h"
57 #include "glocalfileiostream.h"
58 #include "glocalfile.h"
59 #include "gcancellable.h"
60 #include "gasyncresult.h"
67 * @short_description: File and Directory Handling
69 * @see_also: #GFileInfo, #GFileEnumerator
71 * #GFile is a high level abstraction for manipulating files on a
72 * virtual file system. #GFiles are lightweight, immutable objects
73 * that do no I/O upon creation. It is necessary to understand that
74 * #GFile objects do not represent files, merely an identifier for a
75 * file. All file content I/O is implemented as streaming operations
76 * (see #GInputStream and #GOutputStream).
78 * To construct a #GFile, you can use:
79 * - g_file_new_for_path() if you have a path.
80 * - g_file_new_for_uri() if you have a URI.
81 * - g_file_new_for_commandline_arg() for a command line argument.
82 * - g_file_new_tmp() to create a temporary file from a template.
83 * - g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().
85 * One way to think of a #GFile is as an abstraction of a pathname. For
86 * normal files the system pathname is what is stored internally, but as
87 * #GFiles are extensible it could also be something else that corresponds
88 * to a pathname in a userspace implementation of a filesystem.
90 * #GFiles make up hierarchies of directories and files that correspond to
91 * the files on a filesystem. You can move through the file system with
92 * #GFile using g_file_get_parent() to get an identifier for the parent
93 * directory, g_file_get_child() to get a child within a directory,
94 * g_file_resolve_relative_path() to resolve a relative path between two
95 * #GFiles. There can be multiple hierarchies, so you may not end up at
96 * the same root if you repeatedly call g_file_get_parent() on two different
99 * All #GFiles have a basename (get with g_file_get_basename()). These names
100 * are byte strings that are used to identify the file on the filesystem
101 * (relative to its parent directory) and there is no guarantees that they
102 * have any particular charset encoding or even make any sense at all. If
103 * you want to use filenames in a user interface you should use the display
104 * name that you can get by requesting the
105 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
106 * This is guaranteed to be in UTF-8 and can be used in a user interface.
107 * But always store the real basename or the #GFile to use to actually
108 * access the file, because there is no way to go from a display name to
111 * Using #GFile as an identifier has the same weaknesses as using a path
112 * in that there may be multiple aliases for the same file. For instance,
113 * hard or soft links may cause two different #GFiles to refer to the same
114 * file. Other possible causes for aliases are: case insensitive filesystems,
115 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
116 * check if two #GFiles point to the same file you can query for the
117 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
118 * canonicalization of pathnames passed in, so that trivial differences in
119 * the path string used at creation (duplicated slashes, slash at end of
120 * path, "." or ".." path segments, etc) does not create different #GFiles.
122 * Many #GFile operations have both synchronous and asynchronous versions
123 * to suit your application. Asynchronous versions of synchronous functions
124 * simply have _async() appended to their function names. The asynchronous
125 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
126 * the operation, producing a GAsyncResult which is then passed to the
127 * function's matching _finish() operation.
129 * Some #GFile operations do not have synchronous analogs, as they may
130 * take a very long time to finish, and blocking may leave an application
131 * unusable. Notable cases include:
132 * - g_file_mount_mountable() to mount a mountable file.
133 * - g_file_unmount_mountable_with_operation() to unmount a mountable file.
134 * - g_file_eject_mountable_with_operation() to eject a mountable file.
136 * ## Entity Tags # {#gfile-etag}
138 * One notable feature of #GFiles are entity tags, or "etags" for
139 * short. Entity tags are somewhat like a more abstract version of the
140 * traditional mtime, and can be used to quickly determine if the file
141 * has been modified from the version on the file system. See the
143 * [specification](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html)
144 * for HTTP Etag headers, which are a very similar concept.
147 static void g_file_real_query_info_async (GFile *file,
148 const char *attributes,
149 GFileQueryInfoFlags flags,
151 GCancellable *cancellable,
152 GAsyncReadyCallback callback,
154 static GFileInfo * g_file_real_query_info_finish (GFile *file,
157 static void g_file_real_query_filesystem_info_async (GFile *file,
158 const char *attributes,
160 GCancellable *cancellable,
161 GAsyncReadyCallback callback,
163 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
166 static void g_file_real_enumerate_children_async (GFile *file,
167 const char *attributes,
168 GFileQueryInfoFlags flags,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
173 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
176 static void g_file_real_read_async (GFile *file,
178 GCancellable *cancellable,
179 GAsyncReadyCallback callback,
181 static GFileInputStream * g_file_real_read_finish (GFile *file,
184 static void g_file_real_append_to_async (GFile *file,
185 GFileCreateFlags flags,
187 GCancellable *cancellable,
188 GAsyncReadyCallback callback,
190 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
193 static void g_file_real_create_async (GFile *file,
194 GFileCreateFlags flags,
196 GCancellable *cancellable,
197 GAsyncReadyCallback callback,
199 static GFileOutputStream *g_file_real_create_finish (GFile *file,
202 static void g_file_real_replace_async (GFile *file,
204 gboolean make_backup,
205 GFileCreateFlags flags,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
210 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
213 static void g_file_real_delete_async (GFile *file,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
218 static gboolean g_file_real_delete_finish (GFile *file,
221 static void g_file_real_trash_async (GFile *file,
223 GCancellable *cancellable,
224 GAsyncReadyCallback callback,
226 static gboolean g_file_real_trash_finish (GFile *file,
229 static void g_file_real_make_directory_async (GFile *file,
231 GCancellable *cancellable,
232 GAsyncReadyCallback callback,
234 static gboolean g_file_real_make_directory_finish (GFile *file,
237 static void g_file_real_open_readwrite_async (GFile *file,
239 GCancellable *cancellable,
240 GAsyncReadyCallback callback,
242 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
245 static void g_file_real_create_readwrite_async (GFile *file,
246 GFileCreateFlags flags,
248 GCancellable *cancellable,
249 GAsyncReadyCallback callback,
251 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
254 static void g_file_real_replace_readwrite_async (GFile *file,
256 gboolean make_backup,
257 GFileCreateFlags flags,
259 GCancellable *cancellable,
260 GAsyncReadyCallback callback,
262 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
265 static gboolean g_file_real_set_attributes_from_info (GFile *file,
267 GFileQueryInfoFlags flags,
268 GCancellable *cancellable,
270 static void g_file_real_set_display_name_async (GFile *file,
271 const char *display_name,
273 GCancellable *cancellable,
274 GAsyncReadyCallback callback,
276 static GFile * g_file_real_set_display_name_finish (GFile *file,
279 static void g_file_real_set_attributes_async (GFile *file,
281 GFileQueryInfoFlags flags,
283 GCancellable *cancellable,
284 GAsyncReadyCallback callback,
286 static gboolean g_file_real_set_attributes_finish (GFile *file,
290 static void g_file_real_find_enclosing_mount_async (GFile *file,
292 GCancellable *cancellable,
293 GAsyncReadyCallback callback,
295 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
298 static void g_file_real_copy_async (GFile *source,
300 GFileCopyFlags flags,
302 GCancellable *cancellable,
303 GFileProgressCallback progress_callback,
304 gpointer progress_callback_data,
305 GAsyncReadyCallback callback,
307 static gboolean g_file_real_copy_finish (GFile *file,
311 static gboolean g_file_real_measure_disk_usage (GFile *file,
312 GFileMeasureFlags flags,
313 GCancellable *cancellable,
314 GFileMeasureProgressCallback progress_callback,
315 gpointer progress_data,
320 static void g_file_real_measure_disk_usage_async (GFile *file,
321 GFileMeasureFlags flags,
323 GCancellable *cancellable,
324 GFileMeasureProgressCallback progress_callback,
325 gpointer progress_data,
326 GAsyncReadyCallback callback,
328 static gboolean g_file_real_measure_disk_usage_finish (GFile *file,
329 GAsyncResult *result,
335 typedef GFileIface GFileInterface;
336 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
339 g_file_default_init (GFileIface *iface)
341 iface->enumerate_children_async = g_file_real_enumerate_children_async;
342 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
343 iface->set_display_name_async = g_file_real_set_display_name_async;
344 iface->set_display_name_finish = g_file_real_set_display_name_finish;
345 iface->query_info_async = g_file_real_query_info_async;
346 iface->query_info_finish = g_file_real_query_info_finish;
347 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
348 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
349 iface->set_attributes_async = g_file_real_set_attributes_async;
350 iface->set_attributes_finish = g_file_real_set_attributes_finish;
351 iface->read_async = g_file_real_read_async;
352 iface->read_finish = g_file_real_read_finish;
353 iface->append_to_async = g_file_real_append_to_async;
354 iface->append_to_finish = g_file_real_append_to_finish;
355 iface->create_async = g_file_real_create_async;
356 iface->create_finish = g_file_real_create_finish;
357 iface->replace_async = g_file_real_replace_async;
358 iface->replace_finish = g_file_real_replace_finish;
359 iface->delete_file_async = g_file_real_delete_async;
360 iface->delete_file_finish = g_file_real_delete_finish;
361 iface->trash_async = g_file_real_trash_async;
362 iface->trash_finish = g_file_real_trash_finish;
363 iface->make_directory_async = g_file_real_make_directory_async;
364 iface->make_directory_finish = g_file_real_make_directory_finish;
365 iface->open_readwrite_async = g_file_real_open_readwrite_async;
366 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
367 iface->create_readwrite_async = g_file_real_create_readwrite_async;
368 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
369 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
370 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
371 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
372 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
373 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
374 iface->copy_async = g_file_real_copy_async;
375 iface->copy_finish = g_file_real_copy_finish;
376 iface->measure_disk_usage = g_file_real_measure_disk_usage;
377 iface->measure_disk_usage_async = g_file_real_measure_disk_usage_async;
378 iface->measure_disk_usage_finish = g_file_real_measure_disk_usage_finish;
384 * @file: input #GFile
386 * Checks to see if a file is native to the platform.
388 * A native file s one expressed in the platform-native filename format,
389 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
390 * as it might be on a locally mounted remote filesystem.
392 * On some systems non-native files may be available using the native
393 * filesystem via a userspace filesystem (FUSE), in these cases this call
394 * will return %FALSE, but g_file_get_path() will still return a native path.
396 * This call does no blocking I/O.
398 * Returns: %TRUE if @file is native
401 g_file_is_native (GFile *file)
405 g_return_val_if_fail (G_IS_FILE (file), FALSE);
407 iface = G_FILE_GET_IFACE (file);
409 return (* iface->is_native) (file);
414 * g_file_has_uri_scheme:
415 * @file: input #GFile
416 * @uri_scheme: a string containing a URI scheme
418 * Checks to see if a #GFile has a given URI scheme.
420 * This call does no blocking I/O.
422 * Returns: %TRUE if #GFile's backend supports the
423 * given URI scheme, %FALSE if URI scheme is %NULL,
424 * not supported, or #GFile is invalid.
427 g_file_has_uri_scheme (GFile *file,
428 const char *uri_scheme)
432 g_return_val_if_fail (G_IS_FILE (file), FALSE);
433 g_return_val_if_fail (uri_scheme != NULL, FALSE);
435 iface = G_FILE_GET_IFACE (file);
437 return (* iface->has_uri_scheme) (file, uri_scheme);
442 * g_file_get_uri_scheme:
443 * @file: input #GFile
445 * Gets the URI scheme for a #GFile.
446 * RFC 3986 decodes the scheme as:
448 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
450 * Common schemes include "file", "http", "ftp", etc.
452 * This call does no blocking I/O.
454 * Returns: a string containing the URI scheme for the given
455 * #GFile. The returned string should be freed with g_free()
456 * when no longer needed.
459 g_file_get_uri_scheme (GFile *file)
463 g_return_val_if_fail (G_IS_FILE (file), NULL);
465 iface = G_FILE_GET_IFACE (file);
467 return (* iface->get_uri_scheme) (file);
472 * g_file_get_basename:
473 * @file: input #GFile
475 * Gets the base name (the last component of the path) for a given #GFile.
477 * If called for the top level of a system (such as the filesystem root
478 * or a uri like sftp://host/) it will return a single directory separator
479 * (and on Windows, possibly a drive letter).
481 * The base name is a byte string (not UTF-8). It has no defined encoding
482 * or rules other than it may not contain zero bytes. If you want to use
483 * filenames in a user interface you should use the display name that you
484 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
485 * attribute with g_file_query_info().
487 * This call does no blocking I/O.
489 * Returns: (nullable): string containing the #GFile's base name, or
490 * %NULL if given #GFile is invalid. The returned string should be
491 * freed with g_free() when no longer needed.
494 g_file_get_basename (GFile *file)
498 g_return_val_if_fail (G_IS_FILE (file), NULL);
500 iface = G_FILE_GET_IFACE (file);
502 return (* iface->get_basename) (file);
507 * @file: input #GFile
509 * Gets the local pathname for #GFile, if one exists.
511 * This call does no blocking I/O.
513 * Returns: (nullable): string containing the #GFile's path, or %NULL
514 * if no such path exists. The returned string should be freed
515 * with g_free() when no longer needed.
518 g_file_get_path (GFile *file)
522 g_return_val_if_fail (G_IS_FILE (file), NULL);
524 iface = G_FILE_GET_IFACE (file);
526 return (* iface->get_path) (file);
531 * @file: input #GFile
533 * Gets the URI for the @file.
535 * This call does no blocking I/O.
537 * Returns: a string containing the #GFile's URI.
538 * The returned string should be freed with g_free()
539 * when no longer needed.
542 g_file_get_uri (GFile *file)
546 g_return_val_if_fail (G_IS_FILE (file), NULL);
548 iface = G_FILE_GET_IFACE (file);
550 return (* iface->get_uri) (file);
554 * g_file_get_parse_name:
555 * @file: input #GFile
557 * Gets the parse name of the @file.
558 * A parse name is a UTF-8 string that describes the
559 * file such that one can get the #GFile back using
560 * g_file_parse_name().
562 * This is generally used to show the #GFile as a nice
563 * full-pathname kind of string in a user interface,
564 * like in a location entry.
566 * For local files with names that can safely be converted
567 * to UTF-8 the pathname is used, otherwise the IRI is used
568 * (a form of URI that allows UTF-8 characters unescaped).
570 * This call does no blocking I/O.
572 * Returns: a string containing the #GFile's parse name.
573 * The returned string should be freed with g_free()
574 * when no longer needed.
577 g_file_get_parse_name (GFile *file)
581 g_return_val_if_fail (G_IS_FILE (file), NULL);
583 iface = G_FILE_GET_IFACE (file);
585 return (* iface->get_parse_name) (file);
590 * @file: input #GFile
592 * Duplicates a #GFile handle. This operation does not duplicate
593 * the actual file or directory represented by the #GFile; see
594 * g_file_copy() if attempting to copy a file.
596 * This call does no blocking I/O.
598 * Returns: (transfer full): a new #GFile that is a duplicate
599 * of the given #GFile.
602 g_file_dup (GFile *file)
606 g_return_val_if_fail (G_IS_FILE (file), NULL);
608 iface = G_FILE_GET_IFACE (file);
610 return (* iface->dup) (file);
615 * @file: (type GFile): #gconstpointer to a #GFile
617 * Creates a hash value for a #GFile.
619 * This call does no blocking I/O.
622 * Returns: 0 if @file is not a valid #GFile, otherwise an
623 * integer that can be used as hash value for the #GFile.
624 * This function is intended for easily hashing a #GFile to
625 * add to a #GHashTable or similar data structure.
628 g_file_hash (gconstpointer file)
632 g_return_val_if_fail (G_IS_FILE (file), 0);
634 iface = G_FILE_GET_IFACE (file);
636 return (* iface->hash) ((GFile *)file);
641 * @file1: the first #GFile
642 * @file2: the second #GFile
644 * Checks if the two given #GFiles refer to the same file.
646 * Note that two #GFiles that differ can still refer to the same
647 * file on the filesystem due to various forms of filename
650 * This call does no blocking I/O.
652 * Returns: %TRUE if @file1 and @file2 are equal.
655 g_file_equal (GFile *file1,
660 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
661 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
666 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
669 iface = G_FILE_GET_IFACE (file1);
671 return (* iface->equal) (file1, file2);
677 * @file: input #GFile
679 * Gets the parent directory for the @file.
680 * If the @file represents the root directory of the
681 * file system, then %NULL will be returned.
683 * This call does no blocking I/O.
685 * Returns: (nullable) (transfer full): a #GFile structure to the
686 * parent of the given #GFile or %NULL if there is no parent. Free
687 * the returned object with g_object_unref().
690 g_file_get_parent (GFile *file)
694 g_return_val_if_fail (G_IS_FILE (file), NULL);
696 iface = G_FILE_GET_IFACE (file);
698 return (* iface->get_parent) (file);
703 * @file: input #GFile
704 * @parent: (allow-none): the parent to check for, or %NULL
706 * Checks if @file has a parent, and optionally, if it is @parent.
708 * If @parent is %NULL then this function returns %TRUE if @file has any
709 * parent at all. If @parent is non-%NULL then %TRUE is only returned
710 * if @file is a child of @parent.
712 * Returns: %TRUE if @file is a child of @parent (or any parent in the
713 * case that @parent is %NULL).
718 g_file_has_parent (GFile *file,
721 GFile *actual_parent;
724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
725 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
727 actual_parent = g_file_get_parent (file);
729 if (actual_parent != NULL)
732 result = g_file_equal (parent, actual_parent);
736 g_object_unref (actual_parent);
746 * @file: input #GFile
747 * @name: string containing the child's basename
749 * Gets a child of @file with basename equal to @name.
751 * Note that the file with that specific name might not exist, but
752 * you can still have a #GFile that points to it. You can use this
753 * for instance to create that file.
755 * This call does no blocking I/O.
757 * Returns: (transfer full): a #GFile to a child specified by @name.
758 * Free the returned object with g_object_unref().
761 g_file_get_child (GFile *file,
764 g_return_val_if_fail (G_IS_FILE (file), NULL);
765 g_return_val_if_fail (name != NULL, NULL);
767 return g_file_resolve_relative_path (file, name);
771 * g_file_get_child_for_display_name:
772 * @file: input #GFile
773 * @display_name: string to a possible child
774 * @error: return location for an error
776 * Gets the child of @file for a given @display_name (i.e. a UTF-8
777 * version of the name). If this function fails, it returns %NULL
778 * and @error will be set. This is very useful when constructing a
779 * #GFile for a new file and the user entered the filename in the
780 * user interface, for instance when you select a directory and
781 * type a filename in the file selector.
783 * This call does no blocking I/O.
785 * Returns: (transfer full): a #GFile to the specified child, or
786 * %NULL if the display name couldn't be converted.
787 * Free the returned object with g_object_unref().
790 g_file_get_child_for_display_name (GFile *file,
791 const char *display_name,
796 g_return_val_if_fail (G_IS_FILE (file), NULL);
797 g_return_val_if_fail (display_name != NULL, NULL);
799 iface = G_FILE_GET_IFACE (file);
801 return (* iface->get_child_for_display_name) (file, display_name, error);
806 * @file: input #GFile
807 * @prefix: input #GFile
809 * Checks whether @file has the prefix specified by @prefix.
811 * In other words, if the names of initial elements of @file's
812 * pathname match @prefix. Only full pathname elements are matched,
813 * so a path like /foo is not considered a prefix of /foobar, only
816 * A #GFile is not a prefix of itself. If you want to check for
817 * equality, use g_file_equal().
819 * This call does no I/O, as it works purely on names. As such it can
820 * sometimes return %FALSE even if @file is inside a @prefix (from a
821 * filesystem point of view), because the prefix of @file is an alias
824 * Virtual: prefix_matches
825 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
829 g_file_has_prefix (GFile *file,
834 g_return_val_if_fail (G_IS_FILE (file), FALSE);
835 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
837 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
840 iface = G_FILE_GET_IFACE (file);
842 /* The vtable function differs in arg order since
843 * we're using the old contains_file call
845 return (* iface->prefix_matches) (prefix, file);
849 * g_file_get_relative_path:
850 * @parent: input #GFile
851 * @descendant: input #GFile
853 * Gets the path for @descendant relative to @parent.
855 * This call does no blocking I/O.
857 * Returns: (nullable): string with the relative path from @descendant
858 * to @parent, or %NULL if @descendant doesn't have @parent as
859 * prefix. The returned string should be freed with g_free() when
863 g_file_get_relative_path (GFile *parent,
868 g_return_val_if_fail (G_IS_FILE (parent), NULL);
869 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
871 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
874 iface = G_FILE_GET_IFACE (parent);
876 return (* iface->get_relative_path) (parent, descendant);
880 * g_file_resolve_relative_path:
881 * @file: input #GFile
882 * @relative_path: a given relative path string
884 * Resolves a relative path for @file to an absolute path.
886 * This call does no blocking I/O.
888 * Returns: (transfer full): #GFile to the resolved path.
889 * %NULL if @relative_path is %NULL or if @file is invalid.
890 * Free the returned object with g_object_unref().
893 g_file_resolve_relative_path (GFile *file,
894 const char *relative_path)
898 g_return_val_if_fail (G_IS_FILE (file), NULL);
899 g_return_val_if_fail (relative_path != NULL, NULL);
901 iface = G_FILE_GET_IFACE (file);
903 return (* iface->resolve_relative_path) (file, relative_path);
907 * g_file_enumerate_children:
908 * @file: input #GFile
909 * @attributes: an attribute query string
910 * @flags: a set of #GFileQueryInfoFlags
911 * @cancellable: (allow-none): optional #GCancellable object,
913 * @error: #GError for error reporting
915 * Gets the requested information about the files in a directory.
916 * The result is a #GFileEnumerator object that will give out
917 * #GFileInfo objects for all the files in the directory.
919 * The @attributes value is a string that specifies the file
920 * attributes that should be gathered. It is not an error if
921 * it's not possible to read a particular requested attribute
922 * from a file - it just won't be set. @attributes should
923 * be a comma-separated list of attributes or attribute wildcards.
924 * The wildcard "*" means all attributes, and a wildcard like
925 * "standard::*" means all attributes in the standard namespace.
926 * An example attribute query be "standard::*,owner::user".
927 * The standard attributes are available as defines, like
928 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
930 * If @cancellable is not %NULL, then the operation can be cancelled
931 * by triggering the cancellable object from another thread. If the
932 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
935 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
936 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
937 * error will be returned. Other errors are possible too.
939 * Returns: (transfer full): A #GFileEnumerator if successful,
940 * %NULL on error. Free the returned object with g_object_unref().
943 g_file_enumerate_children (GFile *file,
944 const char *attributes,
945 GFileQueryInfoFlags flags,
946 GCancellable *cancellable,
951 g_return_val_if_fail (G_IS_FILE (file), NULL);
953 if (g_cancellable_set_error_if_cancelled (cancellable, error))
956 iface = G_FILE_GET_IFACE (file);
958 if (iface->enumerate_children == NULL)
960 g_set_error_literal (error, G_IO_ERROR,
961 G_IO_ERROR_NOT_SUPPORTED,
962 _("Operation not supported"));
966 return (* iface->enumerate_children) (file, attributes, flags,
971 * g_file_enumerate_children_async:
972 * @file: input #GFile
973 * @attributes: an attribute query string
974 * @flags: a set of #GFileQueryInfoFlags
975 * @io_priority: the [I/O priority][io-priority] of the request
976 * @cancellable: (allow-none): optional #GCancellable object,
978 * @callback: (scope async): a #GAsyncReadyCallback to call when the
979 * request is satisfied
980 * @user_data: (closure): the data to pass to callback function
982 * Asynchronously gets the requested information about the files
983 * in a directory. The result is a #GFileEnumerator object that will
984 * give out #GFileInfo objects for all the files in the directory.
986 * For more details, see g_file_enumerate_children() which is
987 * the synchronous version of this call.
989 * When the operation is finished, @callback will be called. You can
990 * then call g_file_enumerate_children_finish() to get the result of
994 g_file_enumerate_children_async (GFile *file,
995 const char *attributes,
996 GFileQueryInfoFlags flags,
998 GCancellable *cancellable,
999 GAsyncReadyCallback callback,
1004 g_return_if_fail (G_IS_FILE (file));
1006 iface = G_FILE_GET_IFACE (file);
1007 (* iface->enumerate_children_async) (file,
1017 * g_file_enumerate_children_finish:
1018 * @file: input #GFile
1019 * @res: a #GAsyncResult
1022 * Finishes an async enumerate children operation.
1023 * See g_file_enumerate_children_async().
1025 * Returns: (transfer full): a #GFileEnumerator or %NULL
1026 * if an error occurred.
1027 * Free the returned object with g_object_unref().
1030 g_file_enumerate_children_finish (GFile *file,
1036 g_return_val_if_fail (G_IS_FILE (file), NULL);
1037 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1039 if (g_async_result_legacy_propagate_error (res, error))
1042 iface = G_FILE_GET_IFACE (file);
1043 return (* iface->enumerate_children_finish) (file, res, error);
1047 * g_file_query_exists:
1048 * @file: input #GFile
1049 * @cancellable: (allow-none): optional #GCancellable object,
1052 * Utility function to check if a particular file exists. This is
1053 * implemented using g_file_query_info() and as such does blocking I/O.
1055 * Note that in many cases it is racy to first check for file existence
1056 * and then execute something based on the outcome of that, because the
1057 * file might have been created or removed in between the operations. The
1058 * general approach to handling that is to not check, but just do the
1059 * operation and handle the errors as they come.
1061 * As an example of race-free checking, take the case of reading a file,
1062 * and if it doesn't exist, creating it. There are two racy versions: read
1063 * it, and on error create it; and: check if it exists, if not create it.
1064 * These can both result in two processes creating the file (with perhaps
1065 * a partially written file as the result). The correct approach is to
1066 * always try to create the file with g_file_create() which will either
1067 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1069 * However, in many cases an existence check is useful in a user interface,
1070 * for instance to make a menu item sensitive/insensitive, so that you don't
1071 * have to fool users that something is possible and then just show an error
1072 * dialog. If you do this, you should make sure to also handle the errors
1073 * that can happen due to races when you execute the operation.
1075 * Returns: %TRUE if the file exists (and can be detected without error),
1076 * %FALSE otherwise (or if cancelled).
1079 g_file_query_exists (GFile *file,
1080 GCancellable *cancellable)
1084 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1086 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1087 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1090 g_object_unref (info);
1098 * g_file_query_file_type:
1099 * @file: input #GFile
1100 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1101 * @cancellable: (allow-none): optional #GCancellable object,
1104 * Utility function to inspect the #GFileType of a file. This is
1105 * implemented using g_file_query_info() and as such does blocking I/O.
1107 * The primary use case of this method is to check if a file is
1108 * a regular file, directory, or symlink.
1110 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1111 * if the file does not exist
1116 g_file_query_file_type (GFile *file,
1117 GFileQueryInfoFlags flags,
1118 GCancellable *cancellable)
1121 GFileType file_type;
1123 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1124 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1128 file_type = g_file_info_get_file_type (info);
1129 g_object_unref (info);
1132 file_type = G_FILE_TYPE_UNKNOWN;
1138 * g_file_query_info:
1139 * @file: input #GFile
1140 * @attributes: an attribute query string
1141 * @flags: a set of #GFileQueryInfoFlags
1142 * @cancellable: (allow-none): optional #GCancellable object,
1146 * Gets the requested information about specified @file.
1147 * The result is a #GFileInfo object that contains key-value
1148 * attributes (such as the type or size of the file).
1150 * The @attributes value is a string that specifies the file
1151 * attributes that should be gathered. It is not an error if
1152 * it's not possible to read a particular requested attribute
1153 * from a file - it just won't be set. @attributes should be a
1154 * comma-separated list of attributes or attribute wildcards.
1155 * The wildcard "*" means all attributes, and a wildcard like
1156 * "standard::*" means all attributes in the standard namespace.
1157 * An example attribute query be "standard::*,owner::user".
1158 * The standard attributes are available as defines, like
1159 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1161 * If @cancellable is not %NULL, then the operation can be cancelled
1162 * by triggering the cancellable object from another thread. If the
1163 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1166 * For symlinks, normally the information about the target of the
1167 * symlink is returned, rather than information about the symlink
1168 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1169 * in @flags the information about the symlink itself will be returned.
1170 * Also, for symlinks that point to non-existing files the information
1171 * about the symlink itself will be returned.
1173 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1174 * returned. Other errors are possible too, and depend on what kind of
1175 * filesystem the file is on.
1177 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1178 * on error. Free the returned object with g_object_unref().
1181 g_file_query_info (GFile *file,
1182 const char *attributes,
1183 GFileQueryInfoFlags flags,
1184 GCancellable *cancellable,
1189 g_return_val_if_fail (G_IS_FILE (file), NULL);
1191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1194 iface = G_FILE_GET_IFACE (file);
1196 if (iface->query_info == NULL)
1198 g_set_error_literal (error, G_IO_ERROR,
1199 G_IO_ERROR_NOT_SUPPORTED,
1200 _("Operation not supported"));
1204 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1208 * g_file_query_info_async:
1209 * @file: input #GFile
1210 * @attributes: an attribute query string
1211 * @flags: a set of #GFileQueryInfoFlags
1212 * @io_priority: the [I/O priority][io-priority] of the request
1213 * @cancellable: (allow-none): optional #GCancellable object,
1215 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1216 * request is satisfied
1217 * @user_data: (closure): the data to pass to callback function
1219 * Asynchronously gets the requested information about specified @file.
1220 * The result is a #GFileInfo object that contains key-value attributes
1221 * (such as type or size for the file).
1223 * For more details, see g_file_query_info() which is the synchronous
1224 * version of this call.
1226 * When the operation is finished, @callback will be called. You can
1227 * then call g_file_query_info_finish() to get the result of the operation.
1230 g_file_query_info_async (GFile *file,
1231 const char *attributes,
1232 GFileQueryInfoFlags flags,
1234 GCancellable *cancellable,
1235 GAsyncReadyCallback callback,
1240 g_return_if_fail (G_IS_FILE (file));
1242 iface = G_FILE_GET_IFACE (file);
1243 (* iface->query_info_async) (file,
1253 * g_file_query_info_finish:
1254 * @file: input #GFile
1255 * @res: a #GAsyncResult
1258 * Finishes an asynchronous file info query.
1259 * See g_file_query_info_async().
1261 * Returns: (transfer full): #GFileInfo for given @file
1262 * or %NULL on error. Free the returned object with
1266 g_file_query_info_finish (GFile *file,
1272 g_return_val_if_fail (G_IS_FILE (file), NULL);
1273 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1275 if (g_async_result_legacy_propagate_error (res, error))
1278 iface = G_FILE_GET_IFACE (file);
1279 return (* iface->query_info_finish) (file, res, error);
1283 * g_file_query_filesystem_info:
1284 * @file: input #GFile
1285 * @attributes: an attribute query string
1286 * @cancellable: (allow-none): optional #GCancellable object,
1290 * Similar to g_file_query_info(), but obtains information
1291 * about the filesystem the @file is on, rather than the file itself.
1292 * For instance the amount of space available and the type of
1295 * The @attributes value is a string that specifies the attributes
1296 * that should be gathered. It is not an error if it's not possible
1297 * to read a particular requested attribute from a file - it just
1298 * won't be set. @attributes should be a comma-separated list of
1299 * attributes or attribute wildcards. The wildcard "*" means all
1300 * attributes, and a wildcard like "filesystem::*" means all attributes
1301 * in the filesystem namespace. The standard namespace for filesystem
1302 * attributes is "filesystem". Common attributes of interest are
1303 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1304 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1305 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1307 * If @cancellable is not %NULL, then the operation can be cancelled
1308 * by triggering the cancellable object from another thread. If the
1309 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1312 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1313 * be returned. Other errors are possible too, and depend on what
1314 * kind of filesystem the file is on.
1316 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1317 * Free the returned object with g_object_unref().
1320 g_file_query_filesystem_info (GFile *file,
1321 const char *attributes,
1322 GCancellable *cancellable,
1327 g_return_val_if_fail (G_IS_FILE (file), NULL);
1329 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1332 iface = G_FILE_GET_IFACE (file);
1334 if (iface->query_filesystem_info == NULL)
1336 g_set_error_literal (error, G_IO_ERROR,
1337 G_IO_ERROR_NOT_SUPPORTED,
1338 _("Operation not supported"));
1342 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1346 * g_file_query_filesystem_info_async:
1347 * @file: input #GFile
1348 * @attributes: an attribute query string
1349 * @io_priority: the [I/O priority][io-priority] of the request
1350 * @cancellable: (allow-none): optional #GCancellable object,
1352 * @callback: (scope async): a #GAsyncReadyCallback to call
1353 * when the request is satisfied
1354 * @user_data: (closure): the data to pass to callback function
1356 * Asynchronously gets the requested information about the filesystem
1357 * that the specified @file is on. The result is a #GFileInfo object
1358 * that contains key-value attributes (such as type or size for the
1361 * For more details, see g_file_query_filesystem_info() which is the
1362 * synchronous version of this call.
1364 * When the operation is finished, @callback will be called. You can
1365 * then call g_file_query_info_finish() to get the result of the
1369 g_file_query_filesystem_info_async (GFile *file,
1370 const char *attributes,
1372 GCancellable *cancellable,
1373 GAsyncReadyCallback callback,
1378 g_return_if_fail (G_IS_FILE (file));
1380 iface = G_FILE_GET_IFACE (file);
1381 (* iface->query_filesystem_info_async) (file,
1390 * g_file_query_filesystem_info_finish:
1391 * @file: input #GFile
1392 * @res: a #GAsyncResult
1395 * Finishes an asynchronous filesystem info query.
1396 * See g_file_query_filesystem_info_async().
1398 * Returns: (transfer full): #GFileInfo for given @file
1399 * or %NULL on error.
1400 * Free the returned object with g_object_unref().
1403 g_file_query_filesystem_info_finish (GFile *file,
1409 g_return_val_if_fail (G_IS_FILE (file), NULL);
1410 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1412 if (g_async_result_legacy_propagate_error (res, error))
1415 iface = G_FILE_GET_IFACE (file);
1416 return (* iface->query_filesystem_info_finish) (file, res, error);
1420 * g_file_find_enclosing_mount:
1421 * @file: input #GFile
1422 * @cancellable: (allow-none): optional #GCancellable object,
1426 * Gets a #GMount for the #GFile.
1428 * If the #GFileIface for @file does not have a mount (e.g.
1429 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1430 * and %NULL will be returned.
1432 * If @cancellable is not %NULL, then the operation can be cancelled by
1433 * triggering the cancellable object from another thread. If the operation
1434 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1436 * Returns: (transfer full): a #GMount where the @file is located
1437 * or %NULL on error.
1438 * Free the returned object with g_object_unref().
1441 g_file_find_enclosing_mount (GFile *file,
1442 GCancellable *cancellable,
1447 g_return_val_if_fail (G_IS_FILE (file), NULL);
1449 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1452 iface = G_FILE_GET_IFACE (file);
1453 if (iface->find_enclosing_mount == NULL)
1456 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1457 /* Translators: This is an error message when
1458 * trying to find the enclosing (user visible)
1459 * mount of a file, but none exists.
1461 _("Containing mount does not exist"));
1465 return (* iface->find_enclosing_mount) (file, cancellable, error);
1469 * g_file_find_enclosing_mount_async:
1471 * @io_priority: the [I/O priority][io-priority] of the request
1472 * @cancellable: (allow-none): optional #GCancellable object,
1474 * @callback: (scope async): a #GAsyncReadyCallback to call
1475 * when the request is satisfied
1476 * @user_data: (closure): the data to pass to callback function
1478 * Asynchronously gets the mount for the file.
1480 * For more details, see g_file_find_enclosing_mount() which is
1481 * the synchronous version of this call.
1483 * When the operation is finished, @callback will be called.
1484 * You can then call g_file_find_enclosing_mount_finish() to
1485 * get the result of the operation.
1488 g_file_find_enclosing_mount_async (GFile *file,
1490 GCancellable *cancellable,
1491 GAsyncReadyCallback callback,
1496 g_return_if_fail (G_IS_FILE (file));
1498 iface = G_FILE_GET_IFACE (file);
1499 (* iface->find_enclosing_mount_async) (file,
1507 * g_file_find_enclosing_mount_finish:
1509 * @res: a #GAsyncResult
1512 * Finishes an asynchronous find mount request.
1513 * See g_file_find_enclosing_mount_async().
1515 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1516 * Free the returned object with g_object_unref().
1519 g_file_find_enclosing_mount_finish (GFile *file,
1525 g_return_val_if_fail (G_IS_FILE (file), NULL);
1526 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1528 if (g_async_result_legacy_propagate_error (res, error))
1531 iface = G_FILE_GET_IFACE (file);
1532 return (* iface->find_enclosing_mount_finish) (file, res, error);
1538 * @file: #GFile to read
1539 * @cancellable: (allow-none): a #GCancellable
1540 * @error: a #GError, or %NULL
1542 * Opens a file for reading. The result is a #GFileInputStream that
1543 * can be used to read the contents of the file.
1545 * If @cancellable is not %NULL, then the operation can be cancelled by
1546 * triggering the cancellable object from another thread. If the operation
1547 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1549 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1550 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1551 * error will be returned. Other errors are possible too, and depend
1552 * on what kind of filesystem the file is on.
1555 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1556 * Free the returned object with g_object_unref().
1559 g_file_read (GFile *file,
1560 GCancellable *cancellable,
1565 g_return_val_if_fail (G_IS_FILE (file), NULL);
1567 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1570 iface = G_FILE_GET_IFACE (file);
1572 if (iface->read_fn == NULL)
1574 g_set_error_literal (error, G_IO_ERROR,
1575 G_IO_ERROR_NOT_SUPPORTED,
1576 _("Operation not supported"));
1580 return (* iface->read_fn) (file, cancellable, error);
1585 * @file: input #GFile
1586 * @flags: a set of #GFileCreateFlags
1587 * @cancellable: (allow-none): optional #GCancellable object,
1589 * @error: a #GError, or %NULL
1591 * Gets an output stream for appending data to the file.
1592 * If the file doesn't already exist it is created.
1594 * By default files created are generally readable by everyone,
1595 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1596 * will be made readable only to the current user, to the level that
1597 * is supported on the target filesystem.
1599 * If @cancellable is not %NULL, then the operation can be cancelled
1600 * by triggering the cancellable object from another thread. If the
1601 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1604 * Some file systems don't allow all file names, and may return an
1605 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1606 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1607 * possible too, and depend on what kind of filesystem the file is on.
1609 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1610 * Free the returned object with g_object_unref().
1613 g_file_append_to (GFile *file,
1614 GFileCreateFlags flags,
1615 GCancellable *cancellable,
1620 g_return_val_if_fail (G_IS_FILE (file), NULL);
1622 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1625 iface = G_FILE_GET_IFACE (file);
1627 if (iface->append_to == NULL)
1629 g_set_error_literal (error, G_IO_ERROR,
1630 G_IO_ERROR_NOT_SUPPORTED,
1631 _("Operation not supported"));
1635 return (* iface->append_to) (file, flags, cancellable, error);
1640 * @file: input #GFile
1641 * @flags: a set of #GFileCreateFlags
1642 * @cancellable: (allow-none): optional #GCancellable object,
1644 * @error: a #GError, or %NULL
1646 * Creates a new file and returns an output stream for writing to it.
1647 * The file must not already exist.
1649 * By default files created are generally readable by everyone,
1650 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1651 * will be made readable only to the current user, to the level
1652 * that is supported on the target filesystem.
1654 * If @cancellable is not %NULL, then the operation can be cancelled
1655 * by triggering the cancellable object from another thread. If the
1656 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1659 * If a file or directory with this name already exists the
1660 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1661 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1662 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1663 * be returned. Other errors are possible too, and depend on what kind
1664 * of filesystem the file is on.
1666 * Returns: (transfer full): a #GFileOutputStream for the newly created
1667 * file, or %NULL on error.
1668 * Free the returned object with g_object_unref().
1671 g_file_create (GFile *file,
1672 GFileCreateFlags flags,
1673 GCancellable *cancellable,
1678 g_return_val_if_fail (G_IS_FILE (file), NULL);
1680 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1683 iface = G_FILE_GET_IFACE (file);
1685 if (iface->create == NULL)
1687 g_set_error_literal (error, G_IO_ERROR,
1688 G_IO_ERROR_NOT_SUPPORTED,
1689 _("Operation not supported"));
1693 return (* iface->create) (file, flags, cancellable, error);
1698 * @file: input #GFile
1699 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1700 * for the current #GFile, or #NULL to ignore
1701 * @make_backup: %TRUE if a backup should be created
1702 * @flags: a set of #GFileCreateFlags
1703 * @cancellable: (allow-none): optional #GCancellable object,
1705 * @error: a #GError, or %NULL
1707 * Returns an output stream for overwriting the file, possibly
1708 * creating a backup copy of the file first. If the file doesn't exist,
1709 * it will be created.
1711 * This will try to replace the file in the safest way possible so
1712 * that any errors during the writing will not affect an already
1713 * existing copy of the file. For instance, for local files it
1714 * may write to a temporary file and then atomically rename over
1715 * the destination when the stream is closed.
1717 * By default files created are generally readable by everyone,
1718 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1719 * will be made readable only to the current user, to the level that
1720 * is supported on the target filesystem.
1722 * If @cancellable is not %NULL, then the operation can be cancelled
1723 * by triggering the cancellable object from another thread. If the
1724 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1727 * If you pass in a non-%NULL @etag value and @file already exists, then
1728 * this value is compared to the current entity tag of the file, and if
1729 * they differ an %G_IO_ERROR_WRONG_ETAG error is returned. This
1730 * generally means that the file has been changed since you last read
1731 * it. You can get the new etag from g_file_output_stream_get_etag()
1732 * after you've finished writing and closed the #GFileOutputStream. When
1733 * you load a new file you can use g_file_input_stream_query_info() to
1734 * get the etag of the file.
1736 * If @make_backup is %TRUE, this function will attempt to make a
1737 * backup of the current file before overwriting it. If this fails
1738 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1739 * want to replace anyway, try again with @make_backup set to %FALSE.
1741 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1742 * be returned, and if the file is some other form of non-regular file
1743 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1744 * file systems don't allow all file names, and may return an
1745 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1746 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1747 * possible too, and depend on what kind of filesystem the file is on.
1749 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1750 * Free the returned object with g_object_unref().
1753 g_file_replace (GFile *file,
1755 gboolean make_backup,
1756 GFileCreateFlags flags,
1757 GCancellable *cancellable,
1762 g_return_val_if_fail (G_IS_FILE (file), NULL);
1764 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1767 iface = G_FILE_GET_IFACE (file);
1769 if (iface->replace == NULL)
1771 g_set_error_literal (error, G_IO_ERROR,
1772 G_IO_ERROR_NOT_SUPPORTED,
1773 _("Operation not supported"));
1777 /* Handle empty tag string as NULL in consistent way. */
1778 if (etag && *etag == 0)
1781 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1785 * g_file_open_readwrite:
1786 * @file: #GFile to open
1787 * @cancellable: (allow-none): a #GCancellable
1788 * @error: a #GError, or %NULL
1790 * Opens an existing file for reading and writing. The result is
1791 * a #GFileIOStream that can be used to read and write the contents
1794 * If @cancellable is not %NULL, then the operation can be cancelled
1795 * by triggering the cancellable object from another thread. If the
1796 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1799 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1800 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1801 * error will be returned. Other errors are possible too, and depend on
1802 * what kind of filesystem the file is on. Note that in many non-local
1803 * file cases read and write streams are not supported, so make sure you
1804 * really need to do read and write streaming, rather than just opening
1805 * for reading or writing.
1807 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1808 * Free the returned object with g_object_unref().
1813 g_file_open_readwrite (GFile *file,
1814 GCancellable *cancellable,
1819 g_return_val_if_fail (G_IS_FILE (file), NULL);
1821 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1824 iface = G_FILE_GET_IFACE (file);
1826 if (iface->open_readwrite == NULL)
1828 g_set_error_literal (error, G_IO_ERROR,
1829 G_IO_ERROR_NOT_SUPPORTED,
1830 _("Operation not supported"));
1834 return (* iface->open_readwrite) (file, cancellable, error);
1838 * g_file_create_readwrite:
1840 * @flags: a set of #GFileCreateFlags
1841 * @cancellable: (allow-none): optional #GCancellable object,
1843 * @error: return location for a #GError, or %NULL
1845 * Creates a new file and returns a stream for reading and
1846 * writing to it. The file must not already exist.
1848 * By default files created are generally readable by everyone,
1849 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1850 * will be made readable only to the current user, to the level
1851 * that is supported on the target filesystem.
1853 * If @cancellable is not %NULL, then the operation can be cancelled
1854 * by triggering the cancellable object from another thread. If the
1855 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1858 * If a file or directory with this name already exists, the
1859 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1860 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1861 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1862 * will be returned. Other errors are possible too, and depend on what
1863 * kind of filesystem the file is on.
1865 * Note that in many non-local file cases read and write streams are
1866 * not supported, so make sure you really need to do read and write
1867 * streaming, rather than just opening for reading or writing.
1869 * Returns: (transfer full): a #GFileIOStream for the newly created
1870 * file, or %NULL on error.
1871 * Free the returned object with g_object_unref().
1876 g_file_create_readwrite (GFile *file,
1877 GFileCreateFlags flags,
1878 GCancellable *cancellable,
1883 g_return_val_if_fail (G_IS_FILE (file), NULL);
1885 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1888 iface = G_FILE_GET_IFACE (file);
1890 if (iface->create_readwrite == NULL)
1892 g_set_error_literal (error, G_IO_ERROR,
1893 G_IO_ERROR_NOT_SUPPORTED,
1894 _("Operation not supported"));
1898 return (* iface->create_readwrite) (file, flags, cancellable, error);
1902 * g_file_replace_readwrite:
1904 * @etag: (allow-none): an optional [entity tag][gfile-etag]
1905 * for the current #GFile, or #NULL to ignore
1906 * @make_backup: %TRUE if a backup should be created
1907 * @flags: a set of #GFileCreateFlags
1908 * @cancellable: (allow-none): optional #GCancellable object,
1910 * @error: return location for a #GError, or %NULL
1912 * Returns an output stream for overwriting the file in readwrite mode,
1913 * possibly creating a backup copy of the file first. If the file doesn't
1914 * exist, it will be created.
1916 * For details about the behaviour, see g_file_replace() which does the
1917 * same thing but returns an output stream only.
1919 * Note that in many non-local file cases read and write streams are not
1920 * supported, so make sure you really need to do read and write streaming,
1921 * rather than just opening for reading or writing.
1923 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1924 * Free the returned object with g_object_unref().
1929 g_file_replace_readwrite (GFile *file,
1931 gboolean make_backup,
1932 GFileCreateFlags flags,
1933 GCancellable *cancellable,
1938 g_return_val_if_fail (G_IS_FILE (file), NULL);
1940 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1943 iface = G_FILE_GET_IFACE (file);
1945 if (iface->replace_readwrite == NULL)
1947 g_set_error_literal (error, G_IO_ERROR,
1948 G_IO_ERROR_NOT_SUPPORTED,
1949 _("Operation not supported"));
1953 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1957 * g_file_read_async:
1958 * @file: input #GFile
1959 * @io_priority: the [I/O priority][io-priority] of the request
1960 * @cancellable: (allow-none): optional #GCancellable object,
1962 * @callback: (scope async): a #GAsyncReadyCallback to call
1963 * when the request is satisfied
1964 * @user_data: (closure): the data to pass to callback function
1966 * Asynchronously opens @file for reading.
1968 * For more details, see g_file_read() which is
1969 * the synchronous version of this call.
1971 * When the operation is finished, @callback will be called.
1972 * You can then call g_file_read_finish() to get the result
1976 g_file_read_async (GFile *file,
1978 GCancellable *cancellable,
1979 GAsyncReadyCallback callback,
1984 g_return_if_fail (G_IS_FILE (file));
1986 iface = G_FILE_GET_IFACE (file);
1987 (* iface->read_async) (file,
1995 * g_file_read_finish:
1996 * @file: input #GFile
1997 * @res: a #GAsyncResult
1998 * @error: a #GError, or %NULL
2000 * Finishes an asynchronous file read operation started with
2001 * g_file_read_async().
2003 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
2004 * Free the returned object with g_object_unref().
2007 g_file_read_finish (GFile *file,
2013 g_return_val_if_fail (G_IS_FILE (file), NULL);
2014 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2016 if (g_async_result_legacy_propagate_error (res, error))
2019 iface = G_FILE_GET_IFACE (file);
2020 return (* iface->read_finish) (file, res, error);
2024 * g_file_append_to_async:
2025 * @file: input #GFile
2026 * @flags: a set of #GFileCreateFlags
2027 * @io_priority: the [I/O priority][io-priority] of the request
2028 * @cancellable: (allow-none): optional #GCancellable object,
2030 * @callback: (scope async): a #GAsyncReadyCallback to call
2031 * when the request is satisfied
2032 * @user_data: (closure): the data to pass to callback function
2034 * Asynchronously opens @file for appending.
2036 * For more details, see g_file_append_to() which is
2037 * the synchronous version of this call.
2039 * When the operation is finished, @callback will be called.
2040 * You can then call g_file_append_to_finish() to get the result
2044 g_file_append_to_async (GFile *file,
2045 GFileCreateFlags flags,
2047 GCancellable *cancellable,
2048 GAsyncReadyCallback callback,
2053 g_return_if_fail (G_IS_FILE (file));
2055 iface = G_FILE_GET_IFACE (file);
2056 (* iface->append_to_async) (file,
2065 * g_file_append_to_finish:
2066 * @file: input #GFile
2067 * @res: #GAsyncResult
2068 * @error: a #GError, or %NULL
2070 * Finishes an asynchronous file append operation started with
2071 * g_file_append_to_async().
2073 * Returns: (transfer full): a valid #GFileOutputStream
2074 * or %NULL on error.
2075 * Free the returned object with g_object_unref().
2078 g_file_append_to_finish (GFile *file,
2084 g_return_val_if_fail (G_IS_FILE (file), NULL);
2085 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2087 if (g_async_result_legacy_propagate_error (res, error))
2090 iface = G_FILE_GET_IFACE (file);
2091 return (* iface->append_to_finish) (file, res, error);
2095 * g_file_create_async:
2096 * @file: input #GFile
2097 * @flags: a set of #GFileCreateFlags
2098 * @io_priority: the [I/O priority][io-priority] of the request
2099 * @cancellable: (allow-none): optional #GCancellable object,
2101 * @callback: (scope async): a #GAsyncReadyCallback to call
2102 * when the request is satisfied
2103 * @user_data: (closure): the data to pass to callback function
2105 * Asynchronously creates a new file and returns an output stream
2106 * for writing to it. The file must not already exist.
2108 * For more details, see g_file_create() which is
2109 * the synchronous version of this call.
2111 * When the operation is finished, @callback will be called.
2112 * You can then call g_file_create_finish() to get the result
2116 g_file_create_async (GFile *file,
2117 GFileCreateFlags flags,
2119 GCancellable *cancellable,
2120 GAsyncReadyCallback callback,
2125 g_return_if_fail (G_IS_FILE (file));
2127 iface = G_FILE_GET_IFACE (file);
2128 (* iface->create_async) (file,
2137 * g_file_create_finish:
2138 * @file: input #GFile
2139 * @res: a #GAsyncResult
2140 * @error: a #GError, or %NULL
2142 * Finishes an asynchronous file create operation started with
2143 * g_file_create_async().
2145 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2146 * Free the returned object with g_object_unref().
2149 g_file_create_finish (GFile *file,
2155 g_return_val_if_fail (G_IS_FILE (file), NULL);
2156 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2158 if (g_async_result_legacy_propagate_error (res, error))
2161 iface = G_FILE_GET_IFACE (file);
2162 return (* iface->create_finish) (file, res, error);
2166 * g_file_replace_async:
2167 * @file: input #GFile
2168 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2169 * or %NULL to ignore
2170 * @make_backup: %TRUE if a backup should be created
2171 * @flags: a set of #GFileCreateFlags
2172 * @io_priority: the [I/O priority][io-priority] of the request
2173 * @cancellable: (allow-none): optional #GCancellable object,
2175 * @callback: (scope async): a #GAsyncReadyCallback to call
2176 * when the request is satisfied
2177 * @user_data: (closure): the data to pass to callback function
2179 * Asynchronously overwrites the file, replacing the contents,
2180 * possibly creating a backup copy of the file first.
2182 * For more details, see g_file_replace() which is
2183 * the synchronous version of this call.
2185 * When the operation is finished, @callback will be called.
2186 * You can then call g_file_replace_finish() to get the result
2190 g_file_replace_async (GFile *file,
2192 gboolean make_backup,
2193 GFileCreateFlags flags,
2195 GCancellable *cancellable,
2196 GAsyncReadyCallback callback,
2201 g_return_if_fail (G_IS_FILE (file));
2203 iface = G_FILE_GET_IFACE (file);
2204 (* iface->replace_async) (file,
2215 * g_file_replace_finish:
2216 * @file: input #GFile
2217 * @res: a #GAsyncResult
2218 * @error: a #GError, or %NULL
2220 * Finishes an asynchronous file replace operation started with
2221 * g_file_replace_async().
2223 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2224 * Free the returned object with g_object_unref().
2227 g_file_replace_finish (GFile *file,
2233 g_return_val_if_fail (G_IS_FILE (file), NULL);
2234 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2236 if (g_async_result_legacy_propagate_error (res, error))
2239 iface = G_FILE_GET_IFACE (file);
2240 return (* iface->replace_finish) (file, res, error);
2244 * g_file_open_readwrite_async
2245 * @file: input #GFile
2246 * @io_priority: the [I/O priority][io-priority] of the request
2247 * @cancellable: (allow-none): optional #GCancellable object,
2249 * @callback: (scope async): a #GAsyncReadyCallback to call
2250 * when the request is satisfied
2251 * @user_data: (closure): the data to pass to callback function
2253 * Asynchronously opens @file for reading and writing.
2255 * For more details, see g_file_open_readwrite() which is
2256 * the synchronous version of this call.
2258 * When the operation is finished, @callback will be called.
2259 * You can then call g_file_open_readwrite_finish() to get
2260 * the result of the operation.
2265 g_file_open_readwrite_async (GFile *file,
2267 GCancellable *cancellable,
2268 GAsyncReadyCallback callback,
2273 g_return_if_fail (G_IS_FILE (file));
2275 iface = G_FILE_GET_IFACE (file);
2276 (* iface->open_readwrite_async) (file,
2284 * g_file_open_readwrite_finish:
2285 * @file: input #GFile
2286 * @res: a #GAsyncResult
2287 * @error: a #GError, or %NULL
2289 * Finishes an asynchronous file read operation started with
2290 * g_file_open_readwrite_async().
2292 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2293 * Free the returned object with g_object_unref().
2298 g_file_open_readwrite_finish (GFile *file,
2304 g_return_val_if_fail (G_IS_FILE (file), NULL);
2305 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2307 if (g_async_result_legacy_propagate_error (res, error))
2310 iface = G_FILE_GET_IFACE (file);
2311 return (* iface->open_readwrite_finish) (file, res, error);
2315 * g_file_create_readwrite_async:
2316 * @file: input #GFile
2317 * @flags: a set of #GFileCreateFlags
2318 * @io_priority: the [I/O priority][io-priority] of the request
2319 * @cancellable: (allow-none): optional #GCancellable object,
2321 * @callback: (scope async): a #GAsyncReadyCallback to call
2322 * when the request is satisfied
2323 * @user_data: (closure): the data to pass to callback function
2325 * Asynchronously creates a new file and returns a stream
2326 * for reading and writing to it. The file must not already exist.
2328 * For more details, see g_file_create_readwrite() which is
2329 * the synchronous version of this call.
2331 * When the operation is finished, @callback will be called.
2332 * You can then call g_file_create_readwrite_finish() to get
2333 * the result of the operation.
2338 g_file_create_readwrite_async (GFile *file,
2339 GFileCreateFlags flags,
2341 GCancellable *cancellable,
2342 GAsyncReadyCallback callback,
2347 g_return_if_fail (G_IS_FILE (file));
2349 iface = G_FILE_GET_IFACE (file);
2350 (* iface->create_readwrite_async) (file,
2359 * g_file_create_readwrite_finish:
2360 * @file: input #GFile
2361 * @res: a #GAsyncResult
2362 * @error: a #GError, or %NULL
2364 * Finishes an asynchronous file create operation started with
2365 * g_file_create_readwrite_async().
2367 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2368 * Free the returned object with g_object_unref().
2373 g_file_create_readwrite_finish (GFile *file,
2379 g_return_val_if_fail (G_IS_FILE (file), NULL);
2380 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2382 if (g_async_result_legacy_propagate_error (res, error))
2385 iface = G_FILE_GET_IFACE (file);
2386 return (* iface->create_readwrite_finish) (file, res, error);
2390 * g_file_replace_readwrite_async:
2391 * @file: input #GFile
2392 * @etag: (allow-none): an [entity tag][gfile-etag] for the current #GFile,
2393 * or %NULL to ignore
2394 * @make_backup: %TRUE if a backup should be created
2395 * @flags: a set of #GFileCreateFlags
2396 * @io_priority: the [I/O priority][io-priority] of the request
2397 * @cancellable: (allow-none): optional #GCancellable object,
2399 * @callback: (scope async): a #GAsyncReadyCallback to call
2400 * when the request is satisfied
2401 * @user_data: (closure): the data to pass to callback function
2403 * Asynchronously overwrites the file in read-write mode,
2404 * replacing the contents, possibly creating a backup copy
2405 * of the file first.
2407 * For more details, see g_file_replace_readwrite() which is
2408 * the synchronous version of this call.
2410 * When the operation is finished, @callback will be called.
2411 * You can then call g_file_replace_readwrite_finish() to get
2412 * the result of the operation.
2417 g_file_replace_readwrite_async (GFile *file,
2419 gboolean make_backup,
2420 GFileCreateFlags flags,
2422 GCancellable *cancellable,
2423 GAsyncReadyCallback callback,
2428 g_return_if_fail (G_IS_FILE (file));
2430 iface = G_FILE_GET_IFACE (file);
2431 (* iface->replace_readwrite_async) (file,
2442 * g_file_replace_readwrite_finish:
2443 * @file: input #GFile
2444 * @res: a #GAsyncResult
2445 * @error: a #GError, or %NULL
2447 * Finishes an asynchronous file replace operation started with
2448 * g_file_replace_readwrite_async().
2450 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2451 * Free the returned object with g_object_unref().
2456 g_file_replace_readwrite_finish (GFile *file,
2462 g_return_val_if_fail (G_IS_FILE (file), NULL);
2463 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2465 if (g_async_result_legacy_propagate_error (res, error))
2468 iface = G_FILE_GET_IFACE (file);
2469 return (* iface->replace_readwrite_finish) (file, res, error);
2473 copy_symlink (GFile *destination,
2474 GFileCopyFlags flags,
2475 GCancellable *cancellable,
2480 gboolean tried_delete;
2482 GFileType file_type;
2484 tried_delete = FALSE;
2488 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2490 /* Maybe it already existed, and we want to overwrite? */
2491 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2492 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2494 g_clear_error (&my_error);
2496 /* Don't overwrite if the destination is a directory */
2497 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2498 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2499 cancellable, &my_error);
2502 file_type = g_file_info_get_file_type (info);
2503 g_object_unref (info);
2505 if (file_type == G_FILE_TYPE_DIRECTORY)
2507 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2508 _("Can't copy over directory"));
2513 if (!g_file_delete (destination, cancellable, error))
2516 tried_delete = TRUE;
2520 g_propagate_error (error, my_error);
2527 static GFileInputStream *
2528 open_source_for_copy (GFile *source,
2530 GFileCopyFlags flags,
2531 GCancellable *cancellable,
2535 GFileInputStream *ret;
2537 GFileType file_type;
2540 ret = g_file_read (source, cancellable, &my_error);
2544 /* There was an error opening the source, try to set a good error for it: */
2545 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2547 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2548 * as that is less useful to the app. Better check for errors on the
2551 g_error_free (my_error);
2554 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2555 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2556 cancellable, &my_error);
2558 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2560 file_type = g_file_info_get_file_type (info);
2561 g_object_unref (info);
2563 if (flags & G_FILE_COPY_OVERWRITE)
2565 if (file_type == G_FILE_TYPE_DIRECTORY)
2567 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2568 _("Can't copy directory over directory"));
2571 /* continue to would_recurse error */
2575 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2576 _("Target file exists"));
2582 /* Error getting info from target, return that error
2583 * (except for NOT_FOUND, which is no error here)
2585 g_clear_object (&info);
2586 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2588 g_propagate_error (error, my_error);
2591 g_clear_error (&my_error);
2594 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2595 _("Can't recursively copy directory"));
2599 g_propagate_error (error, my_error);
2604 should_copy (GFileAttributeInfo *info,
2605 gboolean copy_all_attributes,
2606 gboolean skip_perms)
2608 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2611 if (copy_all_attributes)
2612 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2613 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2617 build_attribute_list_for_copy (GFile *file,
2618 GFileCopyFlags flags,
2619 char **out_attributes,
2620 GCancellable *cancellable,
2623 gboolean ret = FALSE;
2624 GFileAttributeInfoList *attributes = NULL, *namespaces = NULL;
2628 gboolean copy_all_attributes;
2629 gboolean skip_perms;
2631 copy_all_attributes = flags & G_FILE_COPY_ALL_METADATA;
2632 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2634 /* Ignore errors here, if the target supports no attributes there is
2635 * nothing to copy. We still honor the cancellable though.
2637 attributes = g_file_query_settable_attributes (file, cancellable, NULL);
2638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2641 namespaces = g_file_query_writable_namespaces (file, cancellable, NULL);
2642 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2645 if (attributes == NULL && namespaces == NULL)
2649 s = g_string_new ("");
2653 for (i = 0; i < attributes->n_infos; i++)
2655 if (should_copy (&attributes->infos[i], copy_all_attributes, skip_perms))
2660 g_string_append_c (s, ',');
2662 g_string_append (s, attributes->infos[i].name);
2669 for (i = 0; i < namespaces->n_infos; i++)
2671 if (should_copy (&namespaces->infos[i], copy_all_attributes, FALSE))
2676 g_string_append_c (s, ',');
2678 g_string_append (s, namespaces->infos[i].name);
2679 g_string_append (s, "::*");
2685 *out_attributes = g_string_free (s, FALSE);
2689 g_string_free (s, TRUE);
2691 g_file_attribute_info_list_unref (attributes);
2693 g_file_attribute_info_list_unref (namespaces);
2699 * g_file_copy_attributes:
2700 * @source: a #GFile with attributes
2701 * @destination: a #GFile to copy attributes to
2702 * @flags: a set of #GFileCopyFlags
2703 * @cancellable: (allow-none): optional #GCancellable object,
2705 * @error: a #GError, %NULL to ignore
2707 * Copies the file attributes from @source to @destination.
2709 * Normally only a subset of the file attributes are copied,
2710 * those that are copies in a normal file copy operation
2711 * (which for instance does not include e.g. owner). However
2712 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2713 * all the metadata that is possible to copy is copied. This
2714 * is useful when implementing move by copy + delete source.
2716 * Returns: %TRUE if the attributes were copied successfully,
2720 g_file_copy_attributes (GFile *source,
2722 GFileCopyFlags flags,
2723 GCancellable *cancellable,
2726 char *attrs_to_read;
2729 gboolean source_nofollow_symlinks;
2731 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
2732 cancellable, error))
2735 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2737 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2738 * we just don't copy it.
2740 info = g_file_query_info (source, attrs_to_read,
2741 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2745 g_free (attrs_to_read);
2750 res = g_file_set_attributes_from_info (destination,
2752 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2755 g_object_unref (info);
2762 copy_stream_with_progress (GInputStream *in,
2765 GCancellable *cancellable,
2766 GFileProgressCallback progress_callback,
2767 gpointer progress_callback_data,
2770 gssize n_read, n_written;
2771 goffset current_size;
2772 char buffer[1024*64], *p;
2778 /* avoid performance impact of querying total size when it's not needed */
2779 if (progress_callback)
2781 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2782 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2786 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2787 total_size = g_file_info_get_size (info);
2788 g_object_unref (info);
2791 if (total_size == -1)
2793 info = g_file_query_info (source,
2794 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2795 G_FILE_QUERY_INFO_NONE,
2799 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2800 total_size = g_file_info_get_size (info);
2801 g_object_unref (info);
2806 if (total_size == -1)
2813 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2823 current_size += n_read;
2828 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2829 if (n_written == -1)
2836 n_read -= n_written;
2842 if (progress_callback)
2843 progress_callback (current_size, total_size, progress_callback_data);
2846 /* Make sure we send full copied size */
2847 if (progress_callback)
2848 progress_callback (current_size, total_size, progress_callback_data);
2856 do_splice (int fd_in,
2861 long *bytes_transferd,
2867 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2875 else if (errsv == ENOSYS || errsv == EINVAL)
2876 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2877 _("Splice not supported"));
2879 g_set_error (error, G_IO_ERROR,
2880 g_io_error_from_errno (errsv),
2881 _("Error splicing file: %s"),
2882 g_strerror (errsv));
2887 *bytes_transferd = result;
2892 splice_stream_with_progress (GInputStream *in,
2894 GCancellable *cancellable,
2895 GFileProgressCallback progress_callback,
2896 gpointer progress_callback_data,
2899 int buffer[2] = { -1, -1 };
2906 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2907 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2909 if (!g_unix_open_pipe (buffer, FD_CLOEXEC, error))
2913 /* avoid performance impact of querying total size when it's not needed */
2914 if (progress_callback)
2918 if (fstat (fd_in, &sbuf) == 0)
2919 total_size = sbuf.st_size;
2922 if (total_size == -1)
2925 offset_in = offset_out = 0;
2932 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2935 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2946 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2949 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2952 n_read -= n_written;
2955 if (progress_callback)
2956 progress_callback (offset_in, total_size, progress_callback_data);
2959 /* Make sure we send full copied size */
2960 if (progress_callback)
2961 progress_callback (offset_in, total_size, progress_callback_data);
2963 if (!g_close (buffer[0], error))
2966 if (!g_close (buffer[1], error))
2970 if (buffer[0] != -1)
2971 (void) g_close (buffer[0], NULL);
2972 if (buffer[1] != -1)
2973 (void) g_close (buffer[1], NULL);
2981 btrfs_reflink_with_progress (GInputStream *in,
2984 GCancellable *cancellable,
2985 GFileProgressCallback progress_callback,
2986 gpointer progress_callback_data,
2989 goffset source_size;
2993 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2994 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2996 if (progress_callback)
2997 source_size = g_file_info_get_size (info);
2999 /* Btrfs clone ioctl properties:
3000 * - Works at the inode level
3001 * - Doesn't work with directories
3002 * - Always follows symlinks (source and destination)
3004 * By the time we get here, *in and *out are both regular files */
3005 ret = ioctl (fd_out, BTRFS_IOC_CLONE, fd_in);
3010 g_set_error_literal (error, G_IO_ERROR,
3011 G_IO_ERROR_NOT_SUPPORTED,
3012 _("Copy (reflink/clone) between mounts is not supported"));
3013 else if (errno == EINVAL)
3014 g_set_error_literal (error, G_IO_ERROR,
3015 G_IO_ERROR_NOT_SUPPORTED,
3016 _("Copy (reflink/clone) is not supported or invalid"));
3018 /* Most probably something odd happened; retry with fallback */
3019 g_set_error_literal (error, G_IO_ERROR,
3020 G_IO_ERROR_NOT_SUPPORTED,
3021 _("Copy (reflink/clone) is not supported or didn't work"));
3022 /* We retry with fallback for all error cases because Btrfs is currently
3023 * unstable, and so we can't trust it to do clone properly.
3024 * In addition, any hard errors here would cause the same failure in the
3025 * fallback manual copy as well. */
3029 /* Make sure we send full copied size */
3030 if (progress_callback)
3031 progress_callback (source_size, source_size, progress_callback_data);
3038 file_copy_fallback (GFile *source,
3040 GFileCopyFlags flags,
3041 GCancellable *cancellable,
3042 GFileProgressCallback progress_callback,
3043 gpointer progress_callback_data,
3046 gboolean ret = FALSE;
3047 GFileInputStream *file_in = NULL;
3048 GInputStream *in = NULL;
3049 GOutputStream *out = NULL;
3050 GFileInfo *info = NULL;
3052 char *attrs_to_read;
3053 gboolean do_set_attributes = FALSE;
3055 /* need to know the file type */
3056 info = g_file_query_info (source,
3057 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
3058 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3064 /* Maybe copy the symlink? */
3065 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
3066 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
3068 target = g_file_info_get_symlink_target (info);
3071 if (!copy_symlink (destination, flags, cancellable, target, error))
3077 /* ... else fall back on a regular file copy */
3079 /* Handle "special" files (pipes, device nodes, ...)? */
3080 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
3082 /* FIXME: could try to recreate device nodes and others? */
3083 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3084 _("Can't copy special file"));
3088 /* Everything else should just fall back on a regular copy. */
3090 file_in = open_source_for_copy (source, destination, flags, cancellable, error);
3093 in = G_INPUT_STREAM (file_in);
3095 if (!build_attribute_list_for_copy (destination, flags, &attrs_to_read,
3096 cancellable, error))
3099 if (attrs_to_read != NULL)
3101 GError *tmp_error = NULL;
3103 /* Ok, ditch the previous lightweight info (on Unix we just
3104 * called lstat()); at this point we gather all the information
3105 * we need about the source from the opened file descriptor.
3107 g_object_unref (info);
3109 info = g_file_input_stream_query_info (file_in, attrs_to_read,
3110 cancellable, &tmp_error);
3113 /* Not all gvfs backends implement query_info_on_read(), we
3114 * can just fall back to the pathname again.
3115 * https://bugzilla.gnome.org/706254
3117 if (g_error_matches (tmp_error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3119 g_clear_error (&tmp_error);
3120 info = g_file_query_info (source, attrs_to_read, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3121 cancellable, error);
3125 g_free (attrs_to_read);
3126 g_propagate_error (error, tmp_error);
3130 g_free (attrs_to_read);
3134 do_set_attributes = TRUE;
3137 /* In the local file path, we pass down the source info which
3138 * includes things like unix::mode, to ensure that the target file
3139 * is not created with different permissions from the source file.
3141 * If a future API like g_file_replace_with_info() is added, switch
3142 * this code to use that.
3144 if (G_IS_LOCAL_FILE (destination))
3146 if (flags & G_FILE_COPY_OVERWRITE)
3147 out = (GOutputStream*)_g_local_file_output_stream_replace (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3149 flags & G_FILE_COPY_BACKUP,
3150 G_FILE_CREATE_REPLACE_DESTINATION,
3152 cancellable, error);
3154 out = (GOutputStream*)_g_local_file_output_stream_create (_g_local_file_get_filename (G_LOCAL_FILE (destination)),
3156 cancellable, error);
3158 else if (flags & G_FILE_COPY_OVERWRITE)
3160 out = (GOutputStream *)g_file_replace (destination,
3162 flags & G_FILE_COPY_BACKUP,
3163 G_FILE_CREATE_REPLACE_DESTINATION,
3164 cancellable, error);
3168 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
3175 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3177 GError *reflink_err = NULL;
3179 if (!btrfs_reflink_with_progress (in, out, info, cancellable,
3180 progress_callback, progress_callback_data,
3183 if (g_error_matches (reflink_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3185 g_clear_error (&reflink_err);
3189 g_propagate_error (error, reflink_err);
3202 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
3204 GError *splice_err = NULL;
3206 if (!splice_stream_with_progress (in, out, cancellable,
3207 progress_callback, progress_callback_data,
3210 if (g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3212 g_clear_error (&splice_err);
3216 g_propagate_error (error, splice_err);
3229 /* A plain read/write loop */
3230 if (!copy_stream_with_progress (in, out, source, cancellable,
3231 progress_callback, progress_callback_data,
3239 /* Don't care about errors in source here */
3240 (void) g_input_stream_close (in, cancellable, NULL);
3241 g_object_unref (in);
3246 /* But write errors on close are bad! */
3247 if (!g_output_stream_close (out, cancellable, ret ? error : NULL))
3249 g_object_unref (out);
3252 /* Ignore errors here. Failure to copy metadata is not a hard error */
3253 /* TODO: set these attributes /before/ we do the rename() on Unix */
3254 if (ret && do_set_attributes)
3256 g_file_set_attributes_from_info (destination,
3258 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
3263 g_clear_object (&info);
3270 * @source: input #GFile
3271 * @destination: destination #GFile
3272 * @flags: set of #GFileCopyFlags
3273 * @cancellable: (allow-none): optional #GCancellable object,
3275 * @progress_callback: (allow-none) (scope call): function to callback with
3276 * progress information, or %NULL if progress information is not needed
3277 * @progress_callback_data: (closure): user data to pass to @progress_callback
3278 * @error: #GError to set on error, or %NULL
3280 * Copies the file @source to the location specified by @destination.
3281 * Can not handle recursive copies of directories.
3283 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3284 * existing @destination file is overwritten.
3286 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3287 * will be copied as symlinks, otherwise the target of the
3288 * @source symlink will be copied.
3290 * If @cancellable is not %NULL, then the operation can be cancelled by
3291 * triggering the cancellable object from another thread. If the operation
3292 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3294 * If @progress_callback is not %NULL, then the operation can be monitored
3295 * by setting this to a #GFileProgressCallback function.
3296 * @progress_callback_data will be passed to this function. It is guaranteed
3297 * that this callback will be called after all data has been transferred with
3298 * the total number of bytes copied during the operation.
3300 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3301 * is returned, independent on the status of the @destination.
3303 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3304 * the error %G_IO_ERROR_EXISTS is returned.
3306 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3307 * error is returned. If trying to overwrite a directory with a directory the
3308 * %G_IO_ERROR_WOULD_MERGE error is returned.
3310 * If the source is a directory and the target does not exist, or
3311 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3312 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3314 * If you are interested in copying the #GFile object itself (not the on-disk
3315 * file), see g_file_dup().
3317 * Returns: %TRUE on success, %FALSE otherwise.
3320 g_file_copy (GFile *source,
3322 GFileCopyFlags flags,
3323 GCancellable *cancellable,
3324 GFileProgressCallback progress_callback,
3325 gpointer progress_callback_data,
3332 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3333 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3335 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3338 iface = G_FILE_GET_IFACE (destination);
3342 res = (* iface->copy) (source, destination,
3344 progress_callback, progress_callback_data,
3350 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3352 g_propagate_error (error, my_error);
3356 g_clear_error (&my_error);
3359 /* If the types are different, and the destination method failed
3360 * also try the source method
3362 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3364 iface = G_FILE_GET_IFACE (source);
3369 res = (* iface->copy) (source, destination,
3371 progress_callback, progress_callback_data,
3377 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3379 g_propagate_error (error, my_error);
3383 g_clear_error (&my_error);
3387 return file_copy_fallback (source, destination, flags, cancellable,
3388 progress_callback, progress_callback_data,
3393 * g_file_copy_async: (skip)
3394 * @source: input #GFile
3395 * @destination: destination #GFile
3396 * @flags: set of #GFileCopyFlags
3397 * @io_priority: the [I/O priority][io-priority] of the request
3398 * @cancellable: (allow-none): optional #GCancellable object,
3400 * @progress_callback: (allow-none): function to callback with progress
3401 * information, or %NULL if progress information is not needed
3402 * @progress_callback_data: (closure): user data to pass to @progress_callback
3403 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3404 * @user_data: the data to pass to callback function
3406 * Copies the file @source to the location specified by @destination
3407 * asynchronously. For details of the behaviour, see g_file_copy().
3409 * If @progress_callback is not %NULL, then that function that will be called
3410 * just like in g_file_copy(). The callback will run in the default main context
3411 * of the thread calling g_file_copy_async() — the same context as @callback is
3414 * When the operation is finished, @callback will be called. You can then call
3415 * g_file_copy_finish() to get the result of the operation.
3418 g_file_copy_async (GFile *source,
3420 GFileCopyFlags flags,
3422 GCancellable *cancellable,
3423 GFileProgressCallback progress_callback,
3424 gpointer progress_callback_data,
3425 GAsyncReadyCallback callback,
3430 g_return_if_fail (G_IS_FILE (source));
3431 g_return_if_fail (G_IS_FILE (destination));
3433 iface = G_FILE_GET_IFACE (source);
3434 (* iface->copy_async) (source,
3440 progress_callback_data,
3446 * g_file_copy_finish:
3447 * @file: input #GFile
3448 * @res: a #GAsyncResult
3449 * @error: a #GError, or %NULL
3451 * Finishes copying the file started with g_file_copy_async().
3453 * Returns: a %TRUE on success, %FALSE on error.
3456 g_file_copy_finish (GFile *file,
3462 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3463 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3465 if (g_async_result_legacy_propagate_error (res, error))
3468 iface = G_FILE_GET_IFACE (file);
3469 return (* iface->copy_finish) (file, res, error);
3474 * @source: #GFile pointing to the source location
3475 * @destination: #GFile pointing to the destination location
3476 * @flags: set of #GFileCopyFlags
3477 * @cancellable: (allow-none): optional #GCancellable object,
3479 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3480 * function for updates
3481 * @progress_callback_data: (closure): gpointer to user data for
3482 * the callback function
3483 * @error: #GError for returning error conditions, or %NULL
3485 * Tries to move the file or directory @source to the location specified
3486 * by @destination. If native move operations are supported then this is
3487 * used, otherwise a copy + delete fallback is used. The native
3488 * implementation may support moving directories (for instance on moves
3489 * inside the same filesystem), but the fallback code does not.
3491 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3492 * existing @destination file is overwritten.
3494 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3495 * will be copied as symlinks, otherwise the target of the
3496 * @source symlink will be copied.
3498 * If @cancellable is not %NULL, then the operation can be cancelled by
3499 * triggering the cancellable object from another thread. If the operation
3500 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3502 * If @progress_callback is not %NULL, then the operation can be monitored
3503 * by setting this to a #GFileProgressCallback function.
3504 * @progress_callback_data will be passed to this function. It is
3505 * guaranteed that this callback will be called after all data has been
3506 * transferred with the total number of bytes copied during the operation.
3508 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3509 * error is returned, independent on the status of the @destination.
3511 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3512 * then the error %G_IO_ERROR_EXISTS is returned.
3514 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3515 * error is returned. If trying to overwrite a directory with a directory the
3516 * %G_IO_ERROR_WOULD_MERGE error is returned.
3518 * If the source is a directory and the target does not exist, or
3519 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3520 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3521 * move operation isn't available).
3523 * Returns: %TRUE on successful move, %FALSE otherwise.
3526 g_file_move (GFile *source,
3528 GFileCopyFlags flags,
3529 GCancellable *cancellable,
3530 GFileProgressCallback progress_callback,
3531 gpointer progress_callback_data,
3538 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3539 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3541 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3544 iface = G_FILE_GET_IFACE (destination);
3548 res = (* iface->move) (source, destination,
3550 progress_callback, progress_callback_data,
3556 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3558 g_propagate_error (error, my_error);
3562 g_clear_error (&my_error);
3565 /* If the types are different, and the destination method failed
3566 * also try the source method
3568 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3570 iface = G_FILE_GET_IFACE (source);
3575 res = (* iface->move) (source, destination,
3577 progress_callback, progress_callback_data,
3583 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3585 g_propagate_error (error, my_error);
3589 g_clear_error (&my_error);
3593 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3595 g_set_error_literal (error, G_IO_ERROR,
3596 G_IO_ERROR_NOT_SUPPORTED,
3597 _("Operation not supported"));
3601 flags |= G_FILE_COPY_ALL_METADATA;
3602 if (!g_file_copy (source, destination, flags, cancellable,
3603 progress_callback, progress_callback_data,
3607 return g_file_delete (source, cancellable, error);
3611 * g_file_make_directory:
3612 * @file: input #GFile
3613 * @cancellable: (allow-none): optional #GCancellable object,
3615 * @error: a #GError, or %NULL
3617 * Creates a directory. Note that this will only create a child directory
3618 * of the immediate parent directory of the path or URI given by the #GFile.
3619 * To recursively create directories, see g_file_make_directory_with_parents().
3620 * This function will fail if the parent directory does not exist, setting
3621 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3622 * creating directories, this function will fail, setting @error to
3623 * %G_IO_ERROR_NOT_SUPPORTED.
3625 * For a local #GFile the newly created directory will have the default
3626 * (current) ownership and permissions of the current process.
3628 * If @cancellable is not %NULL, then the operation can be cancelled by
3629 * triggering the cancellable object from another thread. If the operation
3630 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3632 * Returns: %TRUE on successful creation, %FALSE otherwise.
3635 g_file_make_directory (GFile *file,
3636 GCancellable *cancellable,
3641 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3643 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3646 iface = G_FILE_GET_IFACE (file);
3648 if (iface->make_directory == NULL)
3650 g_set_error_literal (error, G_IO_ERROR,
3651 G_IO_ERROR_NOT_SUPPORTED,
3652 _("Operation not supported"));
3656 return (* iface->make_directory) (file, cancellable, error);
3660 * g_file_make_directory_async:
3661 * @file: input #GFile
3662 * @io_priority: the [I/O priority][io-priority] of the request
3663 * @cancellable: (allow-none): optional #GCancellable object,
3665 * @callback: a #GAsyncReadyCallback to call
3666 * when the request is satisfied
3667 * @user_data: the data to pass to callback function
3669 * Asynchronously creates a directory.
3671 * Virtual: make_directory_async
3675 g_file_make_directory_async (GFile *file,
3677 GCancellable *cancellable,
3678 GAsyncReadyCallback callback,
3683 g_return_if_fail (G_IS_FILE (file));
3685 iface = G_FILE_GET_IFACE (file);
3686 (* iface->make_directory_async) (file,
3694 * g_file_make_directory_finish:
3695 * @file: input #GFile
3696 * @result: a #GAsyncResult
3697 * @error: a #GError, or %NULL
3699 * Finishes an asynchronous directory creation, started with
3700 * g_file_make_directory_async().
3702 * Virtual: make_directory_finish
3703 * Returns: %TRUE on successful directory creation, %FALSE otherwise.
3707 g_file_make_directory_finish (GFile *file,
3708 GAsyncResult *result,
3713 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3714 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3716 iface = G_FILE_GET_IFACE (file);
3717 return (* iface->make_directory_finish) (file, result, error);
3721 * g_file_make_directory_with_parents:
3722 * @file: input #GFile
3723 * @cancellable: (allow-none): optional #GCancellable object,
3725 * @error: a #GError, or %NULL
3727 * Creates a directory and any parent directories that may not
3728 * exist similar to 'mkdir -p'. If the file system does not support
3729 * creating directories, this function will fail, setting @error to
3730 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3731 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3732 * the similar g_mkdir_with_parents().
3734 * For a local #GFile the newly created directories will have the default
3735 * (current) ownership and permissions of the current process.
3737 * If @cancellable is not %NULL, then the operation can be cancelled by
3738 * triggering the cancellable object from another thread. If the operation
3739 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3741 * Returns: %TRUE if all directories have been successfully created, %FALSE
3747 g_file_make_directory_with_parents (GFile *file,
3748 GCancellable *cancellable,
3751 GFile *work_file = NULL;
3752 GList *list = NULL, *l;
3753 GError *my_error = NULL;
3755 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3757 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3760 g_file_make_directory (file, cancellable, &my_error);
3761 if (!g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3764 g_propagate_error (error, my_error);
3765 return my_error == NULL;
3768 work_file = g_object_ref (file);
3770 while (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3774 parent_file = g_file_get_parent (work_file);
3775 if (parent_file == NULL)
3778 g_clear_error (&my_error);
3779 g_file_make_directory (parent_file, cancellable, &my_error);
3780 /* Another process may have created the directory in between the
3781 * G_IO_ERROR_NOT_FOUND and now
3783 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3784 g_clear_error (&my_error);
3786 g_object_unref (work_file);
3787 work_file = g_object_ref (parent_file);
3789 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
3790 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3792 g_object_unref (parent_file);
3795 for (l = list; my_error == NULL && l; l = l->next)
3797 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3798 if (g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_EXISTS))
3799 g_clear_error (&my_error);
3803 g_object_unref (work_file);
3806 while (list != NULL)
3808 g_object_unref ((GFile *) list->data);
3809 list = g_list_remove (list, list->data);
3812 if (my_error != NULL)
3814 g_propagate_error (error, my_error);
3818 return g_file_make_directory (file, cancellable, error);
3822 * g_file_make_symbolic_link:
3823 * @file: a #GFile with the name of the symlink to create
3824 * @symlink_value: a string with the path for the target of the new symlink
3825 * @cancellable: (allow-none): optional #GCancellable object,
3829 * Creates a symbolic link named @file which contains the string
3832 * If @cancellable is not %NULL, then the operation can be cancelled by
3833 * triggering the cancellable object from another thread. If the operation
3834 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3836 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3839 g_file_make_symbolic_link (GFile *file,
3840 const char *symlink_value,
3841 GCancellable *cancellable,
3846 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3847 g_return_val_if_fail (symlink_value != NULL, FALSE);
3849 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3852 if (*symlink_value == '\0')
3854 g_set_error_literal (error, G_IO_ERROR,
3855 G_IO_ERROR_INVALID_ARGUMENT,
3856 _("Invalid symlink value given"));
3860 iface = G_FILE_GET_IFACE (file);
3862 if (iface->make_symbolic_link == NULL)
3864 g_set_error_literal (error, G_IO_ERROR,
3865 G_IO_ERROR_NOT_SUPPORTED,
3866 _("Operation not supported"));
3870 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3875 * @file: input #GFile
3876 * @cancellable: (allow-none): optional #GCancellable object,
3878 * @error: a #GError, or %NULL
3880 * Deletes a file. If the @file is a directory, it will only be
3881 * deleted if it is empty. This has the same semantics as g_unlink().
3883 * If @cancellable is not %NULL, then the operation can be cancelled by
3884 * triggering the cancellable object from another thread. If the operation
3885 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3887 * Virtual: delete_file
3888 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3891 g_file_delete (GFile *file,
3892 GCancellable *cancellable,
3897 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3899 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3902 iface = G_FILE_GET_IFACE (file);
3904 if (iface->delete_file == NULL)
3906 g_set_error_literal (error, G_IO_ERROR,
3907 G_IO_ERROR_NOT_SUPPORTED,
3908 _("Operation not supported"));
3912 return (* iface->delete_file) (file, cancellable, error);
3916 * g_file_delete_async:
3917 * @file: input #GFile
3918 * @io_priority: the [I/O priority][io-priority] of the request
3919 * @cancellable: (allow-none): optional #GCancellable object,
3921 * @callback: a #GAsyncReadyCallback to call
3922 * when the request is satisfied
3923 * @user_data: the data to pass to callback function
3925 * Asynchronously delete a file. If the @file is a directory, it will
3926 * only be deleted if it is empty. This has the same semantics as
3929 * Virtual: delete_file_async
3933 g_file_delete_async (GFile *file,
3935 GCancellable *cancellable,
3936 GAsyncReadyCallback callback,
3941 g_return_if_fail (G_IS_FILE (file));
3943 iface = G_FILE_GET_IFACE (file);
3944 (* iface->delete_file_async) (file,
3952 * g_file_delete_finish:
3953 * @file: input #GFile
3954 * @result: a #GAsyncResult
3955 * @error: a #GError, or %NULL
3957 * Finishes deleting a file started with g_file_delete_async().
3959 * Virtual: delete_file_finish
3960 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3964 g_file_delete_finish (GFile *file,
3965 GAsyncResult *result,
3970 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3971 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3973 if (g_async_result_legacy_propagate_error (result, error))
3976 iface = G_FILE_GET_IFACE (file);
3977 return (* iface->delete_file_finish) (file, result, error);
3982 * @file: #GFile to send to trash
3983 * @cancellable: (allow-none): optional #GCancellable object,
3985 * @error: a #GError, or %NULL
3987 * Sends @file to the "Trashcan", if possible. This is similar to
3988 * deleting it, but the user can recover it before emptying the trashcan.
3989 * Not all file systems support trashing, so this call can return the
3990 * %G_IO_ERROR_NOT_SUPPORTED error.
3992 * If @cancellable is not %NULL, then the operation can be cancelled by
3993 * triggering the cancellable object from another thread. If the operation
3994 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3997 * Returns: %TRUE on successful trash, %FALSE otherwise.
4000 g_file_trash (GFile *file,
4001 GCancellable *cancellable,
4006 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4008 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4011 iface = G_FILE_GET_IFACE (file);
4013 if (iface->trash == NULL)
4015 g_set_error_literal (error,
4016 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4017 _("Trash not supported"));
4021 return (* iface->trash) (file, cancellable, error);
4025 * g_file_trash_async:
4026 * @file: input #GFile
4027 * @io_priority: the [I/O priority][io-priority] of the request
4028 * @cancellable: (allow-none): optional #GCancellable object,
4030 * @callback: a #GAsyncReadyCallback to call
4031 * when the request is satisfied
4032 * @user_data: the data to pass to callback function
4034 * Asynchronously sends @file to the Trash location, if possible.
4036 * Virtual: trash_async
4040 g_file_trash_async (GFile *file,
4042 GCancellable *cancellable,
4043 GAsyncReadyCallback callback,
4048 g_return_if_fail (G_IS_FILE (file));
4050 iface = G_FILE_GET_IFACE (file);
4051 (* iface->trash_async) (file,
4059 * g_file_trash_finish:
4060 * @file: input #GFile
4061 * @result: a #GAsyncResult
4062 * @error: a #GError, or %NULL
4064 * Finishes an asynchronous file trashing operation, started with
4065 * g_file_trash_async().
4067 * Virtual: trash_finish
4068 * Returns: %TRUE on successful trash, %FALSE otherwise.
4072 g_file_trash_finish (GFile *file,
4073 GAsyncResult *result,
4078 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4079 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4081 iface = G_FILE_GET_IFACE (file);
4082 return (* iface->trash_finish) (file, result, error);
4086 * g_file_set_display_name:
4087 * @file: input #GFile
4088 * @display_name: a string
4089 * @cancellable: (allow-none): optional #GCancellable object,
4091 * @error: a #GError, or %NULL
4093 * Renames @file to the specified display name.
4095 * The display name is converted from UTF-8 to the correct encoding
4096 * for the target filesystem if possible and the @file is renamed to this.
4098 * If you want to implement a rename operation in the user interface the
4099 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
4100 * initial value in the rename widget, and then the result after editing
4101 * should be passed to g_file_set_display_name().
4103 * On success the resulting converted filename is returned.
4105 * If @cancellable is not %NULL, then the operation can be cancelled by
4106 * triggering the cancellable object from another thread. If the operation
4107 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4109 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
4110 * or %NULL if there was an error.
4111 * Free the returned object with g_object_unref().
4114 g_file_set_display_name (GFile *file,
4115 const gchar *display_name,
4116 GCancellable *cancellable,
4121 g_return_val_if_fail (G_IS_FILE (file), NULL);
4122 g_return_val_if_fail (display_name != NULL, NULL);
4124 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
4128 G_IO_ERROR_INVALID_ARGUMENT,
4129 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
4133 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4136 iface = G_FILE_GET_IFACE (file);
4138 return (* iface->set_display_name) (file, display_name, cancellable, error);
4142 * g_file_set_display_name_async:
4143 * @file: input #GFile
4144 * @display_name: a string
4145 * @io_priority: the [I/O priority][io-priority] of the request
4146 * @cancellable: (allow-none): optional #GCancellable object,
4148 * @callback: (scope async): a #GAsyncReadyCallback to call
4149 * when the request is satisfied
4150 * @user_data: (closure): the data to pass to callback function
4152 * Asynchronously sets the display name for a given #GFile.
4154 * For more details, see g_file_set_display_name() which is
4155 * the synchronous version of this call.
4157 * When the operation is finished, @callback will be called.
4158 * You can then call g_file_set_display_name_finish() to get
4159 * the result of the operation.
4162 g_file_set_display_name_async (GFile *file,
4163 const gchar *display_name,
4165 GCancellable *cancellable,
4166 GAsyncReadyCallback callback,
4171 g_return_if_fail (G_IS_FILE (file));
4172 g_return_if_fail (display_name != NULL);
4174 iface = G_FILE_GET_IFACE (file);
4175 (* iface->set_display_name_async) (file,
4184 * g_file_set_display_name_finish:
4185 * @file: input #GFile
4186 * @res: a #GAsyncResult
4187 * @error: a #GError, or %NULL
4189 * Finishes setting a display name started with
4190 * g_file_set_display_name_async().
4192 * Returns: (transfer full): a #GFile or %NULL on error.
4193 * Free the returned object with g_object_unref().
4196 g_file_set_display_name_finish (GFile *file,
4202 g_return_val_if_fail (G_IS_FILE (file), NULL);
4203 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
4205 if (g_async_result_legacy_propagate_error (res, error))
4208 iface = G_FILE_GET_IFACE (file);
4209 return (* iface->set_display_name_finish) (file, res, error);
4213 * g_file_query_settable_attributes:
4214 * @file: input #GFile
4215 * @cancellable: (allow-none): optional #GCancellable object,
4217 * @error: a #GError, or %NULL
4219 * Obtain the list of settable attributes for the file.
4221 * Returns the type and full attribute name of all the attributes
4222 * that can be set on this file. This doesn't mean setting it will
4223 * always succeed though, you might get an access failure, or some
4224 * specific file may not support a specific attribute.
4226 * If @cancellable is not %NULL, then the operation can be cancelled by
4227 * triggering the cancellable object from another thread. If the operation
4228 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4230 * Returns: a #GFileAttributeInfoList describing the settable attributes.
4231 * When you are done with it, release it with
4232 * g_file_attribute_info_list_unref()
4234 GFileAttributeInfoList *
4235 g_file_query_settable_attributes (GFile *file,
4236 GCancellable *cancellable,
4241 GFileAttributeInfoList *list;
4243 g_return_val_if_fail (G_IS_FILE (file), NULL);
4245 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4248 iface = G_FILE_GET_IFACE (file);
4250 if (iface->query_settable_attributes == NULL)
4251 return g_file_attribute_info_list_new ();
4254 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
4258 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4260 list = g_file_attribute_info_list_new ();
4261 g_error_free (my_error);
4264 g_propagate_error (error, my_error);
4271 * g_file_query_writable_namespaces:
4272 * @file: input #GFile
4273 * @cancellable: (allow-none): optional #GCancellable object,
4275 * @error: a #GError, or %NULL
4277 * Obtain the list of attribute namespaces where new attributes
4278 * can be created by a user. An example of this is extended
4279 * attributes (in the "xattr" namespace).
4281 * If @cancellable is not %NULL, then the operation can be cancelled by
4282 * triggering the cancellable object from another thread. If the operation
4283 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4285 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
4286 * When you are done with it, release it with
4287 * g_file_attribute_info_list_unref()
4289 GFileAttributeInfoList *
4290 g_file_query_writable_namespaces (GFile *file,
4291 GCancellable *cancellable,
4296 GFileAttributeInfoList *list;
4298 g_return_val_if_fail (G_IS_FILE (file), NULL);
4300 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4303 iface = G_FILE_GET_IFACE (file);
4305 if (iface->query_writable_namespaces == NULL)
4306 return g_file_attribute_info_list_new ();
4309 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
4313 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
4315 list = g_file_attribute_info_list_new ();
4316 g_error_free (my_error);
4319 g_propagate_error (error, my_error);
4326 * g_file_set_attribute:
4327 * @file: input #GFile
4328 * @attribute: a string containing the attribute's name
4329 * @type: The type of the attribute
4330 * @value_p: (allow-none): a pointer to the value (or the pointer
4331 * itself if the type is a pointer type)
4332 * @flags: a set of #GFileQueryInfoFlags
4333 * @cancellable: (allow-none): optional #GCancellable object,
4335 * @error: a #GError, or %NULL
4337 * Sets an attribute in the file with attribute name @attribute to @value.
4339 * Some attributes can be unset by setting @attribute to
4340 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
4342 * If @cancellable is not %NULL, then the operation can be cancelled by
4343 * triggering the cancellable object from another thread. If the operation
4344 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4346 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
4349 g_file_set_attribute (GFile *file,
4350 const gchar *attribute,
4351 GFileAttributeType type,
4353 GFileQueryInfoFlags flags,
4354 GCancellable *cancellable,
4359 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4360 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
4362 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4365 iface = G_FILE_GET_IFACE (file);
4367 if (iface->set_attribute == NULL)
4369 g_set_error_literal (error, G_IO_ERROR,
4370 G_IO_ERROR_NOT_SUPPORTED,
4371 _("Operation not supported"));
4375 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4379 * g_file_set_attributes_from_info:
4380 * @file: input #GFile
4381 * @info: a #GFileInfo
4382 * @flags: #GFileQueryInfoFlags
4383 * @cancellable: (allow-none): optional #GCancellable object,
4385 * @error: a #GError, or %NULL
4387 * Tries to set all attributes in the #GFileInfo on the target
4388 * values, not stopping on the first error.
4390 * If there is any error during this operation then @error will
4391 * be set to the first error. Error on particular fields are flagged
4392 * by setting the "status" field in the attribute value to
4393 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4394 * also detect further errors.
4396 * If @cancellable is not %NULL, then the operation can be cancelled by
4397 * triggering the cancellable object from another thread. If the operation
4398 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4400 * Returns: %FALSE if there was any error, %TRUE otherwise.
4403 g_file_set_attributes_from_info (GFile *file,
4405 GFileQueryInfoFlags flags,
4406 GCancellable *cancellable,
4411 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4412 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4414 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4417 g_file_info_clear_status (info);
4419 iface = G_FILE_GET_IFACE (file);
4421 return (* iface->set_attributes_from_info) (file,
4429 g_file_real_set_attributes_from_info (GFile *file,
4431 GFileQueryInfoFlags flags,
4432 GCancellable *cancellable,
4438 GFileAttributeValue *value;
4442 attributes = g_file_info_list_attributes (info, NULL);
4444 for (i = 0; attributes[i] != NULL; i++)
4446 value = _g_file_info_get_attribute_value (info, attributes[i]);
4448 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4451 if (!g_file_set_attribute (file, attributes[i],
4452 value->type, _g_file_attribute_value_peek_as_pointer (value),
4453 flags, cancellable, error))
4455 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4457 /* Don't set error multiple times */
4461 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4464 g_strfreev (attributes);
4470 * g_file_set_attributes_async:
4471 * @file: input #GFile
4472 * @info: a #GFileInfo
4473 * @flags: a #GFileQueryInfoFlags
4474 * @io_priority: the [I/O priority][io-priority] of the request
4475 * @cancellable: (allow-none): optional #GCancellable object,
4477 * @callback: (scope async): a #GAsyncReadyCallback
4478 * @user_data: (closure): a #gpointer
4480 * Asynchronously sets the attributes of @file with @info.
4482 * For more details, see g_file_set_attributes_from_info(),
4483 * which is the synchronous version of this call.
4485 * When the operation is finished, @callback will be called.
4486 * You can then call g_file_set_attributes_finish() to get
4487 * the result of the operation.
4490 g_file_set_attributes_async (GFile *file,
4492 GFileQueryInfoFlags flags,
4494 GCancellable *cancellable,
4495 GAsyncReadyCallback callback,
4500 g_return_if_fail (G_IS_FILE (file));
4501 g_return_if_fail (G_IS_FILE_INFO (info));
4503 iface = G_FILE_GET_IFACE (file);
4504 (* iface->set_attributes_async) (file,
4514 * g_file_set_attributes_finish:
4515 * @file: input #GFile
4516 * @result: a #GAsyncResult
4517 * @info: (out) (transfer full): a #GFileInfo
4518 * @error: a #GError, or %NULL
4520 * Finishes setting an attribute started in g_file_set_attributes_async().
4522 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4525 g_file_set_attributes_finish (GFile *file,
4526 GAsyncResult *result,
4532 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4533 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4535 /* No standard handling of errors here, as we must set info even
4538 iface = G_FILE_GET_IFACE (file);
4539 return (* iface->set_attributes_finish) (file, result, info, error);
4543 * g_file_set_attribute_string:
4544 * @file: input #GFile
4545 * @attribute: a string containing the attribute's name
4546 * @value: a string containing the attribute's value
4547 * @flags: #GFileQueryInfoFlags
4548 * @cancellable: (allow-none): optional #GCancellable object,
4550 * @error: a #GError, or %NULL
4552 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4553 * If @attribute is of a different type, this operation will fail.
4555 * If @cancellable is not %NULL, then the operation can be cancelled by
4556 * triggering the cancellable object from another thread. If the operation
4557 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4559 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4562 g_file_set_attribute_string (GFile *file,
4563 const char *attribute,
4565 GFileQueryInfoFlags flags,
4566 GCancellable *cancellable,
4569 return g_file_set_attribute (file, attribute,
4570 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4571 flags, cancellable, error);
4575 * g_file_set_attribute_byte_string:
4576 * @file: input #GFile
4577 * @attribute: a string containing the attribute's name
4578 * @value: a string containing the attribute's new value
4579 * @flags: a #GFileQueryInfoFlags
4580 * @cancellable: (allow-none): optional #GCancellable object,
4582 * @error: a #GError, or %NULL
4584 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4585 * If @attribute is of a different type, this operation will fail,
4588 * If @cancellable is not %NULL, then the operation can be cancelled by
4589 * triggering the cancellable object from another thread. If the operation
4590 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4592 * Returns: %TRUE if the @attribute was successfully set to @value
4593 * in the @file, %FALSE otherwise.
4596 g_file_set_attribute_byte_string (GFile *file,
4597 const gchar *attribute,
4599 GFileQueryInfoFlags flags,
4600 GCancellable *cancellable,
4603 return g_file_set_attribute (file, attribute,
4604 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4605 flags, cancellable, error);
4609 * g_file_set_attribute_uint32:
4610 * @file: input #GFile
4611 * @attribute: a string containing the attribute's name
4612 * @value: a #guint32 containing the attribute's new value
4613 * @flags: a #GFileQueryInfoFlags
4614 * @cancellable: (allow-none): optional #GCancellable object,
4616 * @error: a #GError, or %NULL
4618 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4619 * If @attribute is of a different type, this operation will fail.
4621 * If @cancellable is not %NULL, then the operation can be cancelled by
4622 * triggering the cancellable object from another thread. If the operation
4623 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4625 * Returns: %TRUE if the @attribute was successfully set to @value
4626 * in the @file, %FALSE otherwise.
4629 g_file_set_attribute_uint32 (GFile *file,
4630 const gchar *attribute,
4632 GFileQueryInfoFlags flags,
4633 GCancellable *cancellable,
4636 return g_file_set_attribute (file, attribute,
4637 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4638 flags, cancellable, error);
4642 * g_file_set_attribute_int32:
4643 * @file: input #GFile
4644 * @attribute: a string containing the attribute's name
4645 * @value: a #gint32 containing the attribute's new value
4646 * @flags: a #GFileQueryInfoFlags
4647 * @cancellable: (allow-none): optional #GCancellable object,
4649 * @error: a #GError, or %NULL
4651 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4652 * If @attribute is of a different type, this operation will fail.
4654 * If @cancellable is not %NULL, then the operation can be cancelled by
4655 * triggering the cancellable object from another thread. If the operation
4656 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4658 * Returns: %TRUE if the @attribute was successfully set to @value
4659 * in the @file, %FALSE otherwise.
4662 g_file_set_attribute_int32 (GFile *file,
4663 const gchar *attribute,
4665 GFileQueryInfoFlags flags,
4666 GCancellable *cancellable,
4669 return g_file_set_attribute (file, attribute,
4670 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4671 flags, cancellable, error);
4675 * g_file_set_attribute_uint64:
4676 * @file: input #GFile
4677 * @attribute: a string containing the attribute's name
4678 * @value: a #guint64 containing the attribute's new value
4679 * @flags: a #GFileQueryInfoFlags
4680 * @cancellable: (allow-none): optional #GCancellable object,
4682 * @error: a #GError, or %NULL
4684 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4685 * If @attribute is of a different type, this operation will fail.
4687 * If @cancellable is not %NULL, then the operation can be cancelled by
4688 * triggering the cancellable object from another thread. If the operation
4689 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4691 * Returns: %TRUE if the @attribute was successfully set to @value
4692 * in the @file, %FALSE otherwise.
4695 g_file_set_attribute_uint64 (GFile *file,
4696 const gchar *attribute,
4698 GFileQueryInfoFlags flags,
4699 GCancellable *cancellable,
4702 return g_file_set_attribute (file, attribute,
4703 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4704 flags, cancellable, error);
4708 * g_file_set_attribute_int64:
4709 * @file: input #GFile
4710 * @attribute: a string containing the attribute's name
4711 * @value: a #guint64 containing the attribute's new value
4712 * @flags: a #GFileQueryInfoFlags
4713 * @cancellable: (allow-none): optional #GCancellable object,
4715 * @error: a #GError, or %NULL
4717 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4718 * If @attribute is of a different type, this operation will fail.
4720 * If @cancellable is not %NULL, then the operation can be cancelled by
4721 * triggering the cancellable object from another thread. If the operation
4722 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4724 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4727 g_file_set_attribute_int64 (GFile *file,
4728 const gchar *attribute,
4730 GFileQueryInfoFlags flags,
4731 GCancellable *cancellable,
4734 return g_file_set_attribute (file, attribute,
4735 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4736 flags, cancellable, error);
4740 * g_file_mount_mountable:
4741 * @file: input #GFile
4742 * @flags: flags affecting the operation
4743 * @mount_operation: (allow-none): a #GMountOperation,
4744 * or %NULL to avoid user interaction
4745 * @cancellable: (allow-none): optional #GCancellable object,
4747 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4748 * when the request is satisfied, or %NULL
4749 * @user_data: (closure): the data to pass to callback function
4751 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4752 * Using @mount_operation, you can request callbacks when, for instance,
4753 * passwords are needed during authentication.
4755 * If @cancellable is not %NULL, then the operation can be cancelled by
4756 * triggering the cancellable object from another thread. If the operation
4757 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4759 * When the operation is finished, @callback will be called.
4760 * You can then call g_file_mount_mountable_finish() to get
4761 * the result of the operation.
4764 g_file_mount_mountable (GFile *file,
4765 GMountMountFlags flags,
4766 GMountOperation *mount_operation,
4767 GCancellable *cancellable,
4768 GAsyncReadyCallback callback,
4773 g_return_if_fail (G_IS_FILE (file));
4775 iface = G_FILE_GET_IFACE (file);
4777 if (iface->mount_mountable == NULL)
4779 g_task_report_new_error (file, callback, user_data,
4780 g_file_mount_mountable,
4781 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4782 _("Operation not supported"));
4786 (* iface->mount_mountable) (file,
4795 * g_file_mount_mountable_finish:
4796 * @file: input #GFile
4797 * @result: a #GAsyncResult
4798 * @error: a #GError, or %NULL
4800 * Finishes a mount operation. See g_file_mount_mountable() for details.
4802 * Finish an asynchronous mount operation that was started
4803 * with g_file_mount_mountable().
4805 * Returns: (transfer full): a #GFile or %NULL on error.
4806 * Free the returned object with g_object_unref().
4809 g_file_mount_mountable_finish (GFile *file,
4810 GAsyncResult *result,
4815 g_return_val_if_fail (G_IS_FILE (file), NULL);
4816 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4818 if (g_async_result_legacy_propagate_error (result, error))
4820 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4821 return g_task_propagate_pointer (G_TASK (result), error);
4823 iface = G_FILE_GET_IFACE (file);
4824 return (* iface->mount_mountable_finish) (file, result, error);
4828 * g_file_unmount_mountable:
4829 * @file: input #GFile
4830 * @flags: flags affecting the operation
4831 * @cancellable: (allow-none): optional #GCancellable object,
4833 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4834 * when the request is satisfied, or %NULL
4835 * @user_data: (closure): the data to pass to callback function
4837 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4839 * If @cancellable is not %NULL, then the operation can be cancelled by
4840 * triggering the cancellable object from another thread. If the operation
4841 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4843 * When the operation is finished, @callback will be called.
4844 * You can then call g_file_unmount_mountable_finish() to get
4845 * the result of the operation.
4847 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4850 g_file_unmount_mountable (GFile *file,
4851 GMountUnmountFlags flags,
4852 GCancellable *cancellable,
4853 GAsyncReadyCallback callback,
4858 g_return_if_fail (G_IS_FILE (file));
4860 iface = G_FILE_GET_IFACE (file);
4862 if (iface->unmount_mountable == NULL)
4864 g_task_report_new_error (file, callback, user_data,
4865 g_file_unmount_mountable_with_operation,
4866 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4867 _("Operation not supported"));
4871 (* iface->unmount_mountable) (file,
4879 * g_file_unmount_mountable_finish:
4880 * @file: input #GFile
4881 * @result: a #GAsyncResult
4882 * @error: a #GError, or %NULL
4884 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4886 * Finish an asynchronous unmount operation that was started
4887 * with g_file_unmount_mountable().
4889 * Returns: %TRUE if the operation finished successfully.
4892 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4896 g_file_unmount_mountable_finish (GFile *file,
4897 GAsyncResult *result,
4902 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4903 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4905 if (g_async_result_legacy_propagate_error (result, error))
4907 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4908 return g_task_propagate_boolean (G_TASK (result), error);
4910 iface = G_FILE_GET_IFACE (file);
4911 return (* iface->unmount_mountable_finish) (file, result, error);
4915 * g_file_unmount_mountable_with_operation:
4916 * @file: input #GFile
4917 * @flags: flags affecting the operation
4918 * @mount_operation: (allow-none): a #GMountOperation,
4919 * or %NULL to avoid user interaction
4920 * @cancellable: (allow-none): optional #GCancellable object,
4922 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4923 * when the request is satisfied, or %NULL
4924 * @user_data: (closure): the data to pass to callback function
4926 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4928 * If @cancellable is not %NULL, then the operation can be cancelled by
4929 * triggering the cancellable object from another thread. If the operation
4930 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4932 * When the operation is finished, @callback will be called.
4933 * You can then call g_file_unmount_mountable_finish() to get
4934 * the result of the operation.
4939 g_file_unmount_mountable_with_operation (GFile *file,
4940 GMountUnmountFlags flags,
4941 GMountOperation *mount_operation,
4942 GCancellable *cancellable,
4943 GAsyncReadyCallback callback,
4948 g_return_if_fail (G_IS_FILE (file));
4950 iface = G_FILE_GET_IFACE (file);
4952 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4954 g_task_report_new_error (file, callback, user_data,
4955 g_file_unmount_mountable_with_operation,
4956 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4957 _("Operation not supported"));
4961 if (iface->unmount_mountable_with_operation != NULL)
4962 (* iface->unmount_mountable_with_operation) (file,
4969 (* iface->unmount_mountable) (file,
4977 * g_file_unmount_mountable_with_operation_finish:
4978 * @file: input #GFile
4979 * @result: a #GAsyncResult
4980 * @error: a #GError, or %NULL
4982 * Finishes an unmount operation,
4983 * see g_file_unmount_mountable_with_operation() for details.
4985 * Finish an asynchronous unmount operation that was started
4986 * with g_file_unmount_mountable_with_operation().
4988 * Returns: %TRUE if the operation finished successfully.
4994 g_file_unmount_mountable_with_operation_finish (GFile *file,
4995 GAsyncResult *result,
5000 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5001 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5003 if (g_async_result_legacy_propagate_error (result, error))
5005 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
5006 return g_task_propagate_boolean (G_TASK (result), error);
5008 iface = G_FILE_GET_IFACE (file);
5009 if (iface->unmount_mountable_with_operation_finish != NULL)
5010 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
5012 return (* iface->unmount_mountable_finish) (file, result, error);
5016 * g_file_eject_mountable:
5017 * @file: input #GFile
5018 * @flags: flags affecting the operation
5019 * @cancellable: (allow-none): optional #GCancellable object,
5021 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5022 * when the request is satisfied, or %NULL
5023 * @user_data: (closure): the data to pass to callback function
5025 * Starts an asynchronous eject on a mountable.
5026 * When this operation has completed, @callback will be called with
5027 * @user_user data, and the operation can be finalized with
5028 * g_file_eject_mountable_finish().
5030 * If @cancellable is not %NULL, then the operation can be cancelled by
5031 * triggering the cancellable object from another thread. If the operation
5032 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5034 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
5037 g_file_eject_mountable (GFile *file,
5038 GMountUnmountFlags flags,
5039 GCancellable *cancellable,
5040 GAsyncReadyCallback callback,
5045 g_return_if_fail (G_IS_FILE (file));
5047 iface = G_FILE_GET_IFACE (file);
5049 if (iface->eject_mountable == NULL)
5051 g_task_report_new_error (file, callback, user_data,
5052 g_file_eject_mountable_with_operation,
5053 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5054 _("Operation not supported"));
5058 (* iface->eject_mountable) (file,
5066 * g_file_eject_mountable_finish:
5067 * @file: input #GFile
5068 * @result: a #GAsyncResult
5069 * @error: a #GError, or %NULL
5071 * Finishes an asynchronous eject operation started by
5072 * g_file_eject_mountable().
5074 * Returns: %TRUE if the @file was ejected successfully.
5077 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
5081 g_file_eject_mountable_finish (GFile *file,
5082 GAsyncResult *result,
5087 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5088 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5090 if (g_async_result_legacy_propagate_error (result, error))
5092 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5093 return g_task_propagate_boolean (G_TASK (result), error);
5095 iface = G_FILE_GET_IFACE (file);
5096 return (* iface->eject_mountable_finish) (file, result, error);
5100 * g_file_eject_mountable_with_operation:
5101 * @file: input #GFile
5102 * @flags: flags affecting the operation
5103 * @mount_operation: (allow-none): a #GMountOperation,
5104 * or %NULL to avoid user interaction
5105 * @cancellable: (allow-none): optional #GCancellable object,
5107 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
5108 * when the request is satisfied, or %NULL
5109 * @user_data: (closure): the data to pass to callback function
5111 * Starts an asynchronous eject on a mountable.
5112 * When this operation has completed, @callback will be called with
5113 * @user_user data, and the operation can be finalized with
5114 * g_file_eject_mountable_with_operation_finish().
5116 * If @cancellable is not %NULL, then the operation can be cancelled by
5117 * triggering the cancellable object from another thread. If the operation
5118 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5123 g_file_eject_mountable_with_operation (GFile *file,
5124 GMountUnmountFlags flags,
5125 GMountOperation *mount_operation,
5126 GCancellable *cancellable,
5127 GAsyncReadyCallback callback,
5132 g_return_if_fail (G_IS_FILE (file));
5134 iface = G_FILE_GET_IFACE (file);
5136 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
5138 g_task_report_new_error (file, callback, user_data,
5139 g_file_eject_mountable_with_operation,
5140 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5141 _("Operation not supported"));
5145 if (iface->eject_mountable_with_operation != NULL)
5146 (* iface->eject_mountable_with_operation) (file,
5153 (* iface->eject_mountable) (file,
5161 * g_file_eject_mountable_with_operation_finish:
5162 * @file: input #GFile
5163 * @result: a #GAsyncResult
5164 * @error: a #GError, or %NULL
5166 * Finishes an asynchronous eject operation started by
5167 * g_file_eject_mountable_with_operation().
5169 * Returns: %TRUE if the @file was ejected successfully.
5175 g_file_eject_mountable_with_operation_finish (GFile *file,
5176 GAsyncResult *result,
5181 g_return_val_if_fail (G_IS_FILE (file), FALSE);
5182 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
5184 if (g_async_result_legacy_propagate_error (result, error))
5186 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
5187 return g_task_propagate_boolean (G_TASK (result), error);
5189 iface = G_FILE_GET_IFACE (file);
5190 if (iface->eject_mountable_with_operation_finish != NULL)
5191 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
5193 return (* iface->eject_mountable_finish) (file, result, error);
5197 * g_file_monitor_directory:
5198 * @file: input #GFile
5199 * @flags: a set of #GFileMonitorFlags
5200 * @cancellable: (allow-none): optional #GCancellable object,
5202 * @error: a #GError, or %NULL
5204 * Obtains a directory monitor for the given file.
5205 * This may fail if directory monitoring is not supported.
5207 * If @cancellable is not %NULL, then the operation can be cancelled by
5208 * triggering the cancellable object from another thread. If the operation
5209 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5211 * It does not make sense for @flags to contain
5212 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
5213 * directories. It is not possible to monitor all the files in a
5214 * directory for changes made via hard links; if you want to do this then
5215 * you must register individual watches with g_file_monitor().
5217 * Virtual: monitor_dir
5218 * Returns: (transfer full): a #GFileMonitor for the given @file,
5219 * or %NULL on error.
5220 * Free the returned object with g_object_unref().
5223 g_file_monitor_directory (GFile *file,
5224 GFileMonitorFlags flags,
5225 GCancellable *cancellable,
5230 g_return_val_if_fail (G_IS_FILE (file), NULL);
5231 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
5233 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5236 iface = G_FILE_GET_IFACE (file);
5238 if (iface->monitor_dir == NULL)
5240 g_set_error_literal (error, G_IO_ERROR,
5241 G_IO_ERROR_NOT_SUPPORTED,
5242 _("Operation not supported"));
5246 return (* iface->monitor_dir) (file, flags, cancellable, error);
5250 * g_file_monitor_file:
5251 * @file: input #GFile
5252 * @flags: a set of #GFileMonitorFlags
5253 * @cancellable: (allow-none): optional #GCancellable object,
5255 * @error: a #GError, or %NULL
5257 * Obtains a file monitor for the given file. If no file notification
5258 * mechanism exists, then regular polling of the file is used.
5260 * If @cancellable is not %NULL, then the operation can be cancelled by
5261 * triggering the cancellable object from another thread. If the operation
5262 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5264 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
5265 * will also attempt to report changes made to the file via another
5266 * filename (ie, a hard link). Without this flag, you can only rely on
5267 * changes made through the filename contained in @file to be
5268 * reported. Using this flag may result in an increase in resource
5269 * usage, and may not have any effect depending on the #GFileMonitor
5270 * backend and/or filesystem type.
5272 * Returns: (transfer full): a #GFileMonitor for the given @file,
5273 * or %NULL on error.
5274 * Free the returned object with g_object_unref().
5277 g_file_monitor_file (GFile *file,
5278 GFileMonitorFlags flags,
5279 GCancellable *cancellable,
5283 GFileMonitor *monitor;
5285 g_return_val_if_fail (G_IS_FILE (file), NULL);
5287 if (g_cancellable_set_error_if_cancelled (cancellable, error))
5290 iface = G_FILE_GET_IFACE (file);
5294 if (iface->monitor_file)
5295 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
5297 /* Fallback to polling */
5298 if (monitor == NULL)
5299 monitor = _g_poll_file_monitor_new (file);
5306 * @file: input #GFile
5307 * @flags: a set of #GFileMonitorFlags
5308 * @cancellable: (allow-none): optional #GCancellable object,
5310 * @error: a #GError, or %NULL
5312 * Obtains a file or directory monitor for the given file,
5313 * depending on the type of the file.
5315 * If @cancellable is not %NULL, then the operation can be cancelled by
5316 * triggering the cancellable object from another thread. If the operation
5317 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
5319 * Returns: (transfer full): a #GFileMonitor for the given @file,
5320 * or %NULL on error.
5321 * Free the returned object with g_object_unref().
5326 g_file_monitor (GFile *file,
5327 GFileMonitorFlags flags,
5328 GCancellable *cancellable,
5331 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
5332 return g_file_monitor_directory (file,
5333 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
5334 cancellable, error);
5336 return g_file_monitor_file (file, flags, cancellable, error);
5339 /********************************************
5340 * Default implementation of async ops *
5341 ********************************************/
5345 GFileQueryInfoFlags flags;
5346 } QueryInfoAsyncData;
5349 query_info_data_free (QueryInfoAsyncData *data)
5351 g_free (data->attributes);
5356 query_info_async_thread (GTask *task,
5359 GCancellable *cancellable)
5361 QueryInfoAsyncData *data = task_data;
5363 GError *error = NULL;
5365 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5367 g_task_return_pointer (task, info, g_object_unref);
5369 g_task_return_error (task, error);
5373 g_file_real_query_info_async (GFile *file,
5374 const char *attributes,
5375 GFileQueryInfoFlags flags,
5377 GCancellable *cancellable,
5378 GAsyncReadyCallback callback,
5382 QueryInfoAsyncData *data;
5384 data = g_new0 (QueryInfoAsyncData, 1);
5385 data->attributes = g_strdup (attributes);
5386 data->flags = flags;
5388 task = g_task_new (file, cancellable, callback, user_data);
5389 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5390 g_task_set_priority (task, io_priority);
5391 g_task_run_in_thread (task, query_info_async_thread);
5392 g_object_unref (task);
5396 g_file_real_query_info_finish (GFile *file,
5400 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5402 return g_task_propagate_pointer (G_TASK (res), error);
5406 query_filesystem_info_async_thread (GTask *task,
5409 GCancellable *cancellable)
5411 const char *attributes = task_data;
5413 GError *error = NULL;
5415 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5417 g_task_return_pointer (task, info, g_object_unref);
5419 g_task_return_error (task, error);
5423 g_file_real_query_filesystem_info_async (GFile *file,
5424 const char *attributes,
5426 GCancellable *cancellable,
5427 GAsyncReadyCallback callback,
5432 task = g_task_new (file, cancellable, callback, user_data);
5433 g_task_set_task_data (task, g_strdup (attributes), g_free);
5434 g_task_set_priority (task, io_priority);
5435 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5436 g_object_unref (task);
5440 g_file_real_query_filesystem_info_finish (GFile *file,
5444 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5446 return g_task_propagate_pointer (G_TASK (res), error);
5450 enumerate_children_async_thread (GTask *task,
5453 GCancellable *cancellable)
5455 QueryInfoAsyncData *data = task_data;
5456 GFileEnumerator *enumerator;
5457 GError *error = NULL;
5459 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5461 g_task_return_error (task, error);
5463 g_task_return_pointer (task, enumerator, g_object_unref);
5467 g_file_real_enumerate_children_async (GFile *file,
5468 const char *attributes,
5469 GFileQueryInfoFlags flags,
5471 GCancellable *cancellable,
5472 GAsyncReadyCallback callback,
5476 QueryInfoAsyncData *data;
5478 data = g_new0 (QueryInfoAsyncData, 1);
5479 data->attributes = g_strdup (attributes);
5480 data->flags = flags;
5482 task = g_task_new (file, cancellable, callback, user_data);
5483 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5484 g_task_set_priority (task, io_priority);
5485 g_task_run_in_thread (task, enumerate_children_async_thread);
5486 g_object_unref (task);
5489 static GFileEnumerator *
5490 g_file_real_enumerate_children_finish (GFile *file,
5494 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5496 return g_task_propagate_pointer (G_TASK (res), error);
5500 open_read_async_thread (GTask *task,
5503 GCancellable *cancellable)
5505 GFileInputStream *stream;
5506 GError *error = NULL;
5508 stream = g_file_read (G_FILE (object), cancellable, &error);
5510 g_task_return_pointer (task, stream, g_object_unref);
5512 g_task_return_error (task, error);
5516 g_file_real_read_async (GFile *file,
5518 GCancellable *cancellable,
5519 GAsyncReadyCallback callback,
5524 task = g_task_new (file, cancellable, callback, user_data);
5525 g_task_set_priority (task, io_priority);
5526 g_task_run_in_thread (task, open_read_async_thread);
5527 g_object_unref (task);
5530 static GFileInputStream *
5531 g_file_real_read_finish (GFile *file,
5535 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5537 return g_task_propagate_pointer (G_TASK (res), error);
5541 append_to_async_thread (GTask *task,
5542 gpointer source_object,
5544 GCancellable *cancellable)
5546 GFileCreateFlags *data = task_data;
5547 GFileOutputStream *stream;
5548 GError *error = NULL;
5550 stream = g_file_append_to (G_FILE (source_object), *data, cancellable, &error);
5552 g_task_return_pointer (task, stream, g_object_unref);
5554 g_task_return_error (task, error);
5558 g_file_real_append_to_async (GFile *file,
5559 GFileCreateFlags flags,
5561 GCancellable *cancellable,
5562 GAsyncReadyCallback callback,
5565 GFileCreateFlags *data;
5568 data = g_new0 (GFileCreateFlags, 1);
5571 task = g_task_new (file, cancellable, callback, user_data);
5572 g_task_set_task_data (task, data, g_free);
5573 g_task_set_priority (task, io_priority);
5575 g_task_run_in_thread (task, append_to_async_thread);
5576 g_object_unref (task);
5579 static GFileOutputStream *
5580 g_file_real_append_to_finish (GFile *file,
5584 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5586 return g_task_propagate_pointer (G_TASK (res), error);
5590 create_async_thread (GTask *task,
5591 gpointer source_object,
5593 GCancellable *cancellable)
5595 GFileCreateFlags *data = task_data;
5596 GFileOutputStream *stream;
5597 GError *error = NULL;
5599 stream = g_file_create (G_FILE (source_object), *data, cancellable, &error);
5601 g_task_return_pointer (task, stream, g_object_unref);
5603 g_task_return_error (task, error);
5607 g_file_real_create_async (GFile *file,
5608 GFileCreateFlags flags,
5610 GCancellable *cancellable,
5611 GAsyncReadyCallback callback,
5614 GFileCreateFlags *data;
5617 data = g_new0 (GFileCreateFlags, 1);
5620 task = g_task_new (file, cancellable, callback, user_data);
5621 g_task_set_task_data (task, data, g_free);
5622 g_task_set_priority (task, io_priority);
5624 g_task_run_in_thread (task, create_async_thread);
5625 g_object_unref (task);
5628 static GFileOutputStream *
5629 g_file_real_create_finish (GFile *file,
5633 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5635 return g_task_propagate_pointer (G_TASK (res), error);
5639 GFileOutputStream *stream;
5641 gboolean make_backup;
5642 GFileCreateFlags flags;
5646 replace_async_data_free (ReplaceAsyncData *data)
5649 g_object_unref (data->stream);
5650 g_free (data->etag);
5655 replace_async_thread (GTask *task,
5656 gpointer source_object,
5658 GCancellable *cancellable)
5660 GFileOutputStream *stream;
5661 ReplaceAsyncData *data = task_data;
5662 GError *error = NULL;
5664 stream = g_file_replace (G_FILE (source_object),
5672 g_task_return_pointer (task, stream, g_object_unref);
5674 g_task_return_error (task, error);
5678 g_file_real_replace_async (GFile *file,
5680 gboolean make_backup,
5681 GFileCreateFlags flags,
5683 GCancellable *cancellable,
5684 GAsyncReadyCallback callback,
5688 ReplaceAsyncData *data;
5690 data = g_new0 (ReplaceAsyncData, 1);
5691 data->etag = g_strdup (etag);
5692 data->make_backup = make_backup;
5693 data->flags = flags;
5695 task = g_task_new (file, cancellable, callback, user_data);
5696 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5697 g_task_set_priority (task, io_priority);
5699 g_task_run_in_thread (task, replace_async_thread);
5700 g_object_unref (task);
5703 static GFileOutputStream *
5704 g_file_real_replace_finish (GFile *file,
5708 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5710 return g_task_propagate_pointer (G_TASK (res), error);
5714 delete_async_thread (GTask *task,
5717 GCancellable *cancellable)
5719 GError *error = NULL;
5721 if (g_file_delete (G_FILE (object), cancellable, &error))
5722 g_task_return_boolean (task, TRUE);
5724 g_task_return_error (task, error);
5728 g_file_real_delete_async (GFile *file,
5730 GCancellable *cancellable,
5731 GAsyncReadyCallback callback,
5736 task = g_task_new (file, cancellable, callback, user_data);
5737 g_task_set_priority (task, io_priority);
5738 g_task_run_in_thread (task, delete_async_thread);
5739 g_object_unref (task);
5743 g_file_real_delete_finish (GFile *file,
5747 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5749 return g_task_propagate_boolean (G_TASK (res), error);
5753 trash_async_thread (GTask *task,
5756 GCancellable *cancellable)
5758 GError *error = NULL;
5760 if (g_file_trash (G_FILE (object), cancellable, &error))
5761 g_task_return_boolean (task, TRUE);
5763 g_task_return_error (task, error);
5767 g_file_real_trash_async (GFile *file,
5769 GCancellable *cancellable,
5770 GAsyncReadyCallback callback,
5775 task = g_task_new (file, cancellable, callback, user_data);
5776 g_task_set_priority (task, io_priority);
5777 g_task_run_in_thread (task, trash_async_thread);
5778 g_object_unref (task);
5782 g_file_real_trash_finish (GFile *file,
5786 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5788 return g_task_propagate_boolean (G_TASK (res), error);
5792 make_directory_async_thread (GTask *task,
5795 GCancellable *cancellable)
5797 GError *error = NULL;
5799 if (g_file_make_directory (G_FILE (object), cancellable, &error))
5800 g_task_return_boolean (task, TRUE);
5802 g_task_return_error (task, error);
5806 g_file_real_make_directory_async (GFile *file,
5808 GCancellable *cancellable,
5809 GAsyncReadyCallback callback,
5814 task = g_task_new (file, cancellable, callback, user_data);
5815 g_task_set_priority (task, io_priority);
5816 g_task_run_in_thread (task, make_directory_async_thread);
5817 g_object_unref (task);
5821 g_file_real_make_directory_finish (GFile *file,
5825 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5827 return g_task_propagate_boolean (G_TASK (res), error);
5831 open_readwrite_async_thread (GTask *task,
5834 GCancellable *cancellable)
5836 GFileIOStream *stream;
5837 GError *error = NULL;
5839 stream = g_file_open_readwrite (G_FILE (object), cancellable, &error);
5842 g_task_return_error (task, error);
5844 g_task_return_pointer (task, stream, g_object_unref);
5848 g_file_real_open_readwrite_async (GFile *file,
5850 GCancellable *cancellable,
5851 GAsyncReadyCallback callback,
5856 task = g_task_new (file, cancellable, callback, user_data);
5857 g_task_set_priority (task, io_priority);
5859 g_task_run_in_thread (task, open_readwrite_async_thread);
5860 g_object_unref (task);
5863 static GFileIOStream *
5864 g_file_real_open_readwrite_finish (GFile *file,
5868 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5870 return g_task_propagate_pointer (G_TASK (res), error);
5874 create_readwrite_async_thread (GTask *task,
5877 GCancellable *cancellable)
5879 GFileCreateFlags *data = task_data;
5880 GFileIOStream *stream;
5881 GError *error = NULL;
5883 stream = g_file_create_readwrite (G_FILE (object), *data, cancellable, &error);
5886 g_task_return_error (task, error);
5888 g_task_return_pointer (task, stream, g_object_unref);
5892 g_file_real_create_readwrite_async (GFile *file,
5893 GFileCreateFlags flags,
5895 GCancellable *cancellable,
5896 GAsyncReadyCallback callback,
5899 GFileCreateFlags *data;
5902 data = g_new0 (GFileCreateFlags, 1);
5905 task = g_task_new (file, cancellable, callback, user_data);
5906 g_task_set_task_data (task, data, g_free);
5907 g_task_set_priority (task, io_priority);
5909 g_task_run_in_thread (task, create_readwrite_async_thread);
5910 g_object_unref (task);
5913 static GFileIOStream *
5914 g_file_real_create_readwrite_finish (GFile *file,
5918 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5920 return g_task_propagate_pointer (G_TASK (res), error);
5925 gboolean make_backup;
5926 GFileCreateFlags flags;
5927 } ReplaceRWAsyncData;
5930 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5932 g_free (data->etag);
5937 replace_readwrite_async_thread (GTask *task,
5940 GCancellable *cancellable)
5942 GFileIOStream *stream;
5943 GError *error = NULL;
5944 ReplaceRWAsyncData *data = task_data;
5946 stream = g_file_replace_readwrite (G_FILE (object),
5954 g_task_return_error (task, error);
5956 g_task_return_pointer (task, stream, g_object_unref);
5960 g_file_real_replace_readwrite_async (GFile *file,
5962 gboolean make_backup,
5963 GFileCreateFlags flags,
5965 GCancellable *cancellable,
5966 GAsyncReadyCallback callback,
5970 ReplaceRWAsyncData *data;
5972 data = g_new0 (ReplaceRWAsyncData, 1);
5973 data->etag = g_strdup (etag);
5974 data->make_backup = make_backup;
5975 data->flags = flags;
5977 task = g_task_new (file, cancellable, callback, user_data);
5978 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5979 g_task_set_priority (task, io_priority);
5981 g_task_run_in_thread (task, replace_readwrite_async_thread);
5982 g_object_unref (task);
5985 static GFileIOStream *
5986 g_file_real_replace_readwrite_finish (GFile *file,
5990 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5992 return g_task_propagate_pointer (G_TASK (res), error);
5996 set_display_name_async_thread (GTask *task,
5999 GCancellable *cancellable)
6001 GError *error = NULL;
6002 char *name = task_data;
6005 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
6008 g_task_return_error (task, error);
6010 g_task_return_pointer (task, file, g_object_unref);
6014 g_file_real_set_display_name_async (GFile *file,
6015 const char *display_name,
6017 GCancellable *cancellable,
6018 GAsyncReadyCallback callback,
6023 task = g_task_new (file, cancellable, callback, user_data);
6024 g_task_set_task_data (task, g_strdup (display_name), g_free);
6025 g_task_set_priority (task, io_priority);
6027 g_task_run_in_thread (task, set_display_name_async_thread);
6028 g_object_unref (task);
6032 g_file_real_set_display_name_finish (GFile *file,
6036 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6038 return g_task_propagate_pointer (G_TASK (res), error);
6042 GFileQueryInfoFlags flags;
6049 set_info_data_free (SetInfoAsyncData *data)
6052 g_object_unref (data->info);
6054 g_error_free (data->error);
6059 set_info_async_thread (GTask *task,
6062 GCancellable *cancellable)
6064 SetInfoAsyncData *data = task_data;
6067 data->res = g_file_set_attributes_from_info (G_FILE (object),
6075 g_file_real_set_attributes_async (GFile *file,
6077 GFileQueryInfoFlags flags,
6079 GCancellable *cancellable,
6080 GAsyncReadyCallback callback,
6084 SetInfoAsyncData *data;
6086 data = g_new0 (SetInfoAsyncData, 1);
6087 data->info = g_file_info_dup (info);
6088 data->flags = flags;
6090 task = g_task_new (file, cancellable, callback, user_data);
6091 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
6092 g_task_set_priority (task, io_priority);
6094 g_task_run_in_thread (task, set_info_async_thread);
6095 g_object_unref (task);
6099 g_file_real_set_attributes_finish (GFile *file,
6104 SetInfoAsyncData *data;
6106 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6108 data = g_task_get_task_data (G_TASK (res));
6111 *info = g_object_ref (data->info);
6113 if (error != NULL && data->error)
6114 *error = g_error_copy (data->error);
6120 find_enclosing_mount_async_thread (GTask *task,
6123 GCancellable *cancellable)
6125 GError *error = NULL;
6128 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
6131 g_task_return_error (task, error);
6133 g_task_return_pointer (task, mount, g_object_unref);
6137 g_file_real_find_enclosing_mount_async (GFile *file,
6139 GCancellable *cancellable,
6140 GAsyncReadyCallback callback,
6145 task = g_task_new (file, cancellable, callback, user_data);
6146 g_task_set_priority (task, io_priority);
6148 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
6149 g_object_unref (task);
6153 g_file_real_find_enclosing_mount_finish (GFile *file,
6157 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
6159 return g_task_propagate_pointer (G_TASK (res), error);
6166 GFileCopyFlags flags;
6167 GFileProgressCallback progress_cb;
6168 gpointer progress_cb_data;
6172 copy_async_data_free (CopyAsyncData *data)
6174 g_object_unref (data->source);
6175 g_object_unref (data->destination);
6176 g_slice_free (CopyAsyncData, data);
6180 CopyAsyncData *data;
6181 goffset current_num_bytes;
6182 goffset total_num_bytes;
6186 copy_async_progress_in_main (gpointer user_data)
6188 ProgressData *progress = user_data;
6189 CopyAsyncData *data = progress->data;
6191 data->progress_cb (progress->current_num_bytes,
6192 progress->total_num_bytes,
6193 data->progress_cb_data);
6199 copy_async_progress_callback (goffset current_num_bytes,
6200 goffset total_num_bytes,
6203 GTask *task = user_data;
6204 CopyAsyncData *data = g_task_get_task_data (task);
6205 ProgressData *progress;
6207 progress = g_new (ProgressData, 1);
6208 progress->data = data;
6209 progress->current_num_bytes = current_num_bytes;
6210 progress->total_num_bytes = total_num_bytes;
6212 g_main_context_invoke_full (g_task_get_context (task),
6213 g_task_get_priority (task),
6214 copy_async_progress_in_main,
6220 copy_async_thread (GTask *task,
6223 GCancellable *cancellable)
6225 CopyAsyncData *data = task_data;
6227 GError *error = NULL;
6229 result = g_file_copy (data->source,
6233 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
6237 g_task_return_boolean (task, TRUE);
6239 g_task_return_error (task, error);
6243 g_file_real_copy_async (GFile *source,
6245 GFileCopyFlags flags,
6247 GCancellable *cancellable,
6248 GFileProgressCallback progress_callback,
6249 gpointer progress_callback_data,
6250 GAsyncReadyCallback callback,
6254 CopyAsyncData *data;
6256 data = g_slice_new (CopyAsyncData);
6257 data->source = g_object_ref (source);
6258 data->destination = g_object_ref (destination);
6259 data->flags = flags;
6260 data->progress_cb = progress_callback;
6261 data->progress_cb_data = progress_callback_data;
6263 task = g_task_new (source, cancellable, callback, user_data);
6264 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
6265 g_task_set_priority (task, io_priority);
6266 g_task_run_in_thread (task, copy_async_thread);
6267 g_object_unref (task);
6271 g_file_real_copy_finish (GFile *file,
6275 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6277 return g_task_propagate_boolean (G_TASK (res), error);
6281 /********************************************
6282 * Default VFS operations *
6283 ********************************************/
6286 * g_file_new_for_path:
6287 * @path: a string containing a relative or absolute path.
6288 * The string must be encoded in the glib filename encoding.
6290 * Constructs a #GFile for a given path. This operation never
6291 * fails, but the returned object might not support any I/O
6292 * operation if @path is malformed.
6294 * Returns: (transfer full): a new #GFile for the given @path.
6295 * Free the returned object with g_object_unref().
6298 g_file_new_for_path (const char *path)
6300 g_return_val_if_fail (path != NULL, NULL);
6302 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
6306 * g_file_new_for_uri:
6307 * @uri: a UTF-8 string containing a URI
6309 * Constructs a #GFile for a given URI. This operation never
6310 * fails, but the returned object might not support any I/O
6311 * operation if @uri is malformed or if the uri type is
6314 * Returns: (transfer full): a new #GFile for the given @uri.
6315 * Free the returned object with g_object_unref().
6318 g_file_new_for_uri (const char *uri)
6320 g_return_val_if_fail (uri != NULL, NULL);
6322 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
6327 * @tmpl: (type filename) (allow-none): Template for the file
6328 * name, as in g_file_open_tmp(), or %NULL for a default template
6329 * @iostream: (out): on return, a #GFileIOStream for the created file
6330 * @error: a #GError, or %NULL
6332 * Opens a file in the preferred directory for temporary files (as
6333 * returned by g_get_tmp_dir()) and returns a #GFile and
6334 * #GFileIOStream pointing to it.
6336 * @tmpl should be a string in the GLib file name encoding
6337 * containing a sequence of six 'X' characters, and containing no
6338 * directory components. If it is %NULL, a default template is used.
6340 * Unlike the other #GFile constructors, this will return %NULL if
6341 * a temporary file could not be created.
6343 * Returns: (transfer full): a new #GFile.
6344 * Free the returned object with g_object_unref().
6349 g_file_new_tmp (const char *tmpl,
6350 GFileIOStream **iostream,
6356 GFileOutputStream *output;
6358 g_return_val_if_fail (iostream != NULL, NULL);
6360 fd = g_file_open_tmp (tmpl, &path, error);
6364 file = g_file_new_for_path (path);
6366 output = _g_local_file_output_stream_new (fd);
6367 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
6369 g_object_unref (output);
6376 * g_file_parse_name:
6377 * @parse_name: a file name or path to be parsed
6379 * Constructs a #GFile with the given @parse_name (i.e. something
6380 * given by g_file_get_parse_name()). This operation never fails,
6381 * but the returned object might not support any I/O operation if
6382 * the @parse_name cannot be parsed.
6384 * Returns: (transfer full): a new #GFile.
6387 g_file_parse_name (const char *parse_name)
6389 g_return_val_if_fail (parse_name != NULL, NULL);
6391 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6395 is_valid_scheme_character (char c)
6397 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6400 /* Following RFC 2396, valid schemes are built like:
6401 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6404 has_valid_scheme (const char *uri)
6410 if (!g_ascii_isalpha (*p))
6415 } while (is_valid_scheme_character (*p));
6421 new_for_cmdline_arg (const gchar *arg,
6427 if (g_path_is_absolute (arg))
6428 return g_file_new_for_path (arg);
6430 if (has_valid_scheme (arg))
6431 return g_file_new_for_uri (arg);
6437 current_dir = g_get_current_dir ();
6438 filename = g_build_filename (current_dir, arg, NULL);
6439 g_free (current_dir);
6442 filename = g_build_filename (cwd, arg, NULL);
6444 file = g_file_new_for_path (filename);
6451 * g_file_new_for_commandline_arg:
6452 * @arg: a command line string
6454 * Creates a #GFile with the given argument from the command line.
6455 * The value of @arg can be either a URI, an absolute path or a
6456 * relative path resolved relative to the current working directory.
6457 * This operation never fails, but the returned object might not
6458 * support any I/O operation if @arg points to a malformed path.
6460 * Note that on Windows, this function expects its argument to be in
6461 * UTF-8 -- not the system code page. This means that you
6462 * should not use this function with string from argv as it is passed
6463 * to main(). g_win32_get_command_line() will return a UTF-8 version of
6464 * the commandline. #GApplication also uses UTF-8 but
6465 * g_application_command_line_create_file_for_arg() may be more useful
6466 * for you there. It is also always possible to use this function with
6467 * #GOptionContext arguments of type %G_OPTION_ARG_FILENAME.
6469 * Returns: (transfer full): a new #GFile.
6470 * Free the returned object with g_object_unref().
6473 g_file_new_for_commandline_arg (const char *arg)
6475 g_return_val_if_fail (arg != NULL, NULL);
6477 return new_for_cmdline_arg (arg, NULL);
6481 * g_file_new_for_commandline_arg_and_cwd:
6482 * @arg: a command line string
6483 * @cwd: the current working directory of the commandline
6485 * Creates a #GFile with the given argument from the command line.
6487 * This function is similar to g_file_new_for_commandline_arg() except
6488 * that it allows for passing the current working directory as an
6489 * argument instead of using the current working directory of the
6492 * This is useful if the commandline argument was given in a context
6493 * other than the invocation of the current process.
6495 * See also g_application_command_line_create_file_for_arg().
6497 * Returns: (transfer full): a new #GFile
6502 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6505 g_return_val_if_fail (arg != NULL, NULL);
6506 g_return_val_if_fail (cwd != NULL, NULL);
6508 return new_for_cmdline_arg (arg, cwd);
6512 * g_file_mount_enclosing_volume:
6513 * @location: input #GFile
6514 * @flags: flags affecting the operation
6515 * @mount_operation: (allow-none): a #GMountOperation
6516 * or %NULL to avoid user interaction
6517 * @cancellable: (allow-none): optional #GCancellable object,
6519 * @callback: (allow-none): a #GAsyncReadyCallback to call
6520 * when the request is satisfied, or %NULL
6521 * @user_data: the data to pass to callback function
6523 * Starts a @mount_operation, mounting the volume that contains
6524 * the file @location.
6526 * When this operation has completed, @callback will be called with
6527 * @user_user data, and the operation can be finalized with
6528 * g_file_mount_enclosing_volume_finish().
6530 * If @cancellable is not %NULL, then the operation can be cancelled by
6531 * triggering the cancellable object from another thread. If the operation
6532 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6535 g_file_mount_enclosing_volume (GFile *location,
6536 GMountMountFlags flags,
6537 GMountOperation *mount_operation,
6538 GCancellable *cancellable,
6539 GAsyncReadyCallback callback,
6544 g_return_if_fail (G_IS_FILE (location));
6546 iface = G_FILE_GET_IFACE (location);
6548 if (iface->mount_enclosing_volume == NULL)
6550 g_task_report_new_error (location, callback, user_data,
6551 g_file_mount_enclosing_volume,
6552 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6553 _("volume doesn't implement mount"));
6557 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6562 * g_file_mount_enclosing_volume_finish:
6563 * @location: input #GFile
6564 * @result: a #GAsyncResult
6565 * @error: a #GError, or %NULL
6567 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6569 * Returns: %TRUE if successful. If an error has occurred,
6570 * this function will return %FALSE and set @error
6571 * appropriately if present.
6574 g_file_mount_enclosing_volume_finish (GFile *location,
6575 GAsyncResult *result,
6580 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6581 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6583 if (g_async_result_legacy_propagate_error (result, error))
6585 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6586 return g_task_propagate_boolean (G_TASK (result), error);
6588 iface = G_FILE_GET_IFACE (location);
6590 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6593 /********************************************
6594 * Utility functions *
6595 ********************************************/
6598 * g_file_query_default_handler:
6599 * @file: a #GFile to open
6600 * @cancellable: optional #GCancellable object, %NULL to ignore
6601 * @error: a #GError, or %NULL
6603 * Returns the #GAppInfo that is registered as the default
6604 * application to handle the file specified by @file.
6606 * If @cancellable is not %NULL, then the operation can be cancelled by
6607 * triggering the cancellable object from another thread. If the operation
6608 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6610 * Returns: (transfer full): a #GAppInfo if the handle was found,
6611 * %NULL if there were errors.
6612 * When you are done with it, release it with g_object_unref()
6615 g_file_query_default_handler (GFile *file,
6616 GCancellable *cancellable,
6620 const char *content_type;
6625 uri_scheme = g_file_get_uri_scheme (file);
6626 if (uri_scheme && uri_scheme[0] != '\0')
6628 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6629 g_free (uri_scheme);
6631 if (appinfo != NULL)
6635 info = g_file_query_info (file,
6636 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6645 content_type = g_file_info_get_content_type (info);
6648 /* Don't use is_native(), as we want to support fuse paths if available */
6649 path = g_file_get_path (file);
6650 appinfo = g_app_info_get_default_for_type (content_type,
6655 g_object_unref (info);
6657 if (appinfo != NULL)
6660 g_set_error_literal (error, G_IO_ERROR,
6661 G_IO_ERROR_NOT_SUPPORTED,
6662 _("No application is registered as handling this file"));
6666 #define GET_CONTENT_BLOCK_SIZE 8192
6669 * g_file_load_contents:
6670 * @file: input #GFile
6671 * @cancellable: optional #GCancellable object, %NULL to ignore
6672 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6673 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6674 * or %NULL if the length is not needed
6675 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6676 * or %NULL if the entity tag is not needed
6677 * @error: a #GError, or %NULL
6679 * Loads the content of the file into memory. The data is always
6680 * zero-terminated, but this is not included in the resultant @length.
6681 * The returned @content should be freed with g_free() when no longer
6684 * If @cancellable is not %NULL, then the operation can be cancelled by
6685 * triggering the cancellable object from another thread. If the operation
6686 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6688 * Returns: %TRUE if the @file's contents were successfully loaded.
6689 * %FALSE if there were errors.
6692 g_file_load_contents (GFile *file,
6693 GCancellable *cancellable,
6699 GFileInputStream *in;
6700 GByteArray *content;
6705 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6706 g_return_val_if_fail (contents != NULL, FALSE);
6708 in = g_file_read (file, cancellable, error);
6712 content = g_byte_array_new ();
6715 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6716 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6717 content->data + pos,
6718 GET_CONTENT_BLOCK_SIZE,
6719 cancellable, error)) > 0)
6722 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6729 info = g_file_input_stream_query_info (in,
6730 G_FILE_ATTRIBUTE_ETAG_VALUE,
6735 *etag_out = g_strdup (g_file_info_get_etag (info));
6736 g_object_unref (info);
6740 /* Ignore errors on close */
6741 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6742 g_object_unref (in);
6746 /* error is set already */
6747 g_byte_array_free (content, TRUE);
6754 /* Zero terminate (we got an extra byte allocated for this */
6755 content->data[pos] = 0;
6757 *contents = (char *)g_byte_array_free (content, FALSE);
6764 GFileReadMoreCallback read_more_callback;
6765 GByteArray *content;
6772 load_contents_data_free (LoadContentsData *data)
6775 g_byte_array_free (data->content, TRUE);
6776 g_free (data->etag);
6781 load_contents_close_callback (GObject *obj,
6782 GAsyncResult *close_res,
6785 GInputStream *stream = G_INPUT_STREAM (obj);
6786 LoadContentsData *data = user_data;
6788 /* Ignore errors here, we're only reading anyway */
6789 g_input_stream_close_finish (stream, close_res, NULL);
6790 g_object_unref (stream);
6792 g_task_return_boolean (data->task, TRUE);
6793 g_object_unref (data->task);
6797 load_contents_fstat_callback (GObject *obj,
6798 GAsyncResult *stat_res,
6801 GInputStream *stream = G_INPUT_STREAM (obj);
6802 LoadContentsData *data = user_data;
6805 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6809 data->etag = g_strdup (g_file_info_get_etag (info));
6810 g_object_unref (info);
6813 g_input_stream_close_async (stream, 0,
6814 g_task_get_cancellable (data->task),
6815 load_contents_close_callback, data);
6819 load_contents_read_callback (GObject *obj,
6820 GAsyncResult *read_res,
6823 GInputStream *stream = G_INPUT_STREAM (obj);
6824 LoadContentsData *data = user_data;
6825 GError *error = NULL;
6828 read_size = g_input_stream_read_finish (stream, read_res, &error);
6832 g_task_return_error (data->task, error);
6833 g_object_unref (data->task);
6835 /* Close the file ignoring any error */
6836 g_input_stream_close_async (stream, 0, NULL, NULL, NULL);
6837 g_object_unref (stream);
6839 else if (read_size == 0)
6841 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6842 G_FILE_ATTRIBUTE_ETAG_VALUE,
6844 g_task_get_cancellable (data->task),
6845 load_contents_fstat_callback,
6848 else if (read_size > 0)
6850 data->pos += read_size;
6852 g_byte_array_set_size (data->content,
6853 data->pos + GET_CONTENT_BLOCK_SIZE);
6856 if (data->read_more_callback &&
6857 !data->read_more_callback ((char *)data->content->data, data->pos,
6858 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6859 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6860 G_FILE_ATTRIBUTE_ETAG_VALUE,
6862 g_task_get_cancellable (data->task),
6863 load_contents_fstat_callback,
6866 g_input_stream_read_async (stream,
6867 data->content->data + data->pos,
6868 GET_CONTENT_BLOCK_SIZE,
6870 g_task_get_cancellable (data->task),
6871 load_contents_read_callback,
6877 load_contents_open_callback (GObject *obj,
6878 GAsyncResult *open_res,
6881 GFile *file = G_FILE (obj);
6882 GFileInputStream *stream;
6883 LoadContentsData *data = user_data;
6884 GError *error = NULL;
6886 stream = g_file_read_finish (file, open_res, &error);
6890 g_byte_array_set_size (data->content,
6891 data->pos + GET_CONTENT_BLOCK_SIZE);
6892 g_input_stream_read_async (G_INPUT_STREAM (stream),
6893 data->content->data + data->pos,
6894 GET_CONTENT_BLOCK_SIZE,
6896 g_task_get_cancellable (data->task),
6897 load_contents_read_callback,
6902 g_task_return_error (data->task, error);
6903 g_object_unref (data->task);
6908 * g_file_load_partial_contents_async: (skip)
6909 * @file: input #GFile
6910 * @cancellable: optional #GCancellable object, %NULL to ignore
6911 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6912 * and to specify whether further data should be read
6913 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6914 * @user_data: the data to pass to the callback functions
6916 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6917 * be used to stop reading from the file when appropriate, else this
6918 * function will behave exactly as g_file_load_contents_async(). This
6919 * operation can be finished by g_file_load_partial_contents_finish().
6921 * Users of this function should be aware that @user_data is passed to
6922 * both the @read_more_callback and the @callback.
6924 * If @cancellable is not %NULL, then the operation can be cancelled by
6925 * triggering the cancellable object from another thread. If the operation
6926 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6929 g_file_load_partial_contents_async (GFile *file,
6930 GCancellable *cancellable,
6931 GFileReadMoreCallback read_more_callback,
6932 GAsyncReadyCallback callback,
6935 LoadContentsData *data;
6937 g_return_if_fail (G_IS_FILE (file));
6939 data = g_new0 (LoadContentsData, 1);
6940 data->read_more_callback = read_more_callback;
6941 data->content = g_byte_array_new ();
6943 data->task = g_task_new (file, cancellable, callback, user_data);
6944 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6946 g_file_read_async (file,
6948 g_task_get_cancellable (data->task),
6949 load_contents_open_callback,
6954 * g_file_load_partial_contents_finish:
6955 * @file: input #GFile
6956 * @res: a #GAsyncResult
6957 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6958 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6959 * or %NULL if the length is not needed
6960 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6961 * or %NULL if the entity tag is not needed
6962 * @error: a #GError, or %NULL
6964 * Finishes an asynchronous partial load operation that was started
6965 * with g_file_load_partial_contents_async(). The data is always
6966 * zero-terminated, but this is not included in the resultant @length.
6967 * The returned @content should be freed with g_free() when no longer
6970 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6971 * present, it will be set appropriately.
6974 g_file_load_partial_contents_finish (GFile *file,
6982 LoadContentsData *data;
6984 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6985 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6986 g_return_val_if_fail (contents != NULL, FALSE);
6988 task = G_TASK (res);
6990 if (!g_task_propagate_boolean (task, error))
6997 data = g_task_get_task_data (task);
7000 *length = data->pos;
7004 *etag_out = data->etag;
7008 /* Zero terminate */
7009 g_byte_array_set_size (data->content, data->pos + 1);
7010 data->content->data[data->pos] = 0;
7012 *contents = (char *)g_byte_array_free (data->content, FALSE);
7013 data->content = NULL;
7019 * g_file_load_contents_async:
7020 * @file: input #GFile
7021 * @cancellable: optional #GCancellable object, %NULL to ignore
7022 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7023 * @user_data: the data to pass to callback function
7025 * Starts an asynchronous load of the @file's contents.
7027 * For more details, see g_file_load_contents() which is
7028 * the synchronous version of this call.
7030 * When the load operation has completed, @callback will be called
7031 * with @user data. To finish the operation, call
7032 * g_file_load_contents_finish() with the #GAsyncResult returned by
7035 * If @cancellable is not %NULL, then the operation can be cancelled by
7036 * triggering the cancellable object from another thread. If the operation
7037 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7040 g_file_load_contents_async (GFile *file,
7041 GCancellable *cancellable,
7042 GAsyncReadyCallback callback,
7045 g_file_load_partial_contents_async (file,
7048 callback, user_data);
7052 * g_file_load_contents_finish:
7053 * @file: input #GFile
7054 * @res: a #GAsyncResult
7055 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
7056 * @length: (out) (allow-none): a location to place the length of the contents of the file,
7057 * or %NULL if the length is not needed
7058 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
7059 * or %NULL if the entity tag is not needed
7060 * @error: a #GError, or %NULL
7062 * Finishes an asynchronous load of the @file's contents.
7063 * The contents are placed in @contents, and @length is set to the
7064 * size of the @contents string. The @content should be freed with
7065 * g_free() when no longer needed. If @etag_out is present, it will be
7066 * set to the new entity tag for the @file.
7068 * Returns: %TRUE if the load was successful. If %FALSE and @error is
7069 * present, it will be set appropriately.
7072 g_file_load_contents_finish (GFile *file,
7079 return g_file_load_partial_contents_finish (file,
7088 * g_file_replace_contents:
7089 * @file: input #GFile
7090 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
7091 * @length: the length of @contents in bytes
7092 * @etag: (allow-none): the old [entity-tag][gfile-etag] for the document,
7094 * @make_backup: %TRUE if a backup should be created
7095 * @flags: a set of #GFileCreateFlags
7096 * @new_etag: (allow-none) (out): a location to a new [entity tag][gfile-etag]
7097 * for the document. This should be freed with g_free() when no longer
7099 * @cancellable: optional #GCancellable object, %NULL to ignore
7100 * @error: a #GError, or %NULL
7102 * Replaces the contents of @file with @contents of @length bytes.
7104 * If @etag is specified (not %NULL), any existing file must have that etag,
7105 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
7107 * If @make_backup is %TRUE, this function will attempt to make a backup
7110 * If @cancellable is not %NULL, then the operation can be cancelled by
7111 * triggering the cancellable object from another thread. If the operation
7112 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7114 * The returned @new_etag can be used to verify that the file hasn't
7115 * changed the next time it is saved over.
7117 * Returns: %TRUE if successful. If an error has occurred, this function
7118 * will return %FALSE and set @error appropriately if present.
7121 g_file_replace_contents (GFile *file,
7122 const char *contents,
7125 gboolean make_backup,
7126 GFileCreateFlags flags,
7128 GCancellable *cancellable,
7131 GFileOutputStream *out;
7132 gsize pos, remainder;
7136 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7137 g_return_val_if_fail (contents != NULL, FALSE);
7139 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
7145 while (remainder > 0 &&
7146 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
7148 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
7156 if (remainder > 0 && res < 0)
7158 /* Ignore errors on close */
7159 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
7160 g_object_unref (out);
7162 /* error is set already */
7166 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
7169 *new_etag = g_file_output_stream_get_etag (out);
7171 g_object_unref (out);
7182 } ReplaceContentsData;
7185 replace_contents_data_free (ReplaceContentsData *data)
7187 g_bytes_unref (data->content);
7188 g_free (data->etag);
7193 replace_contents_close_callback (GObject *obj,
7194 GAsyncResult *close_res,
7197 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7198 ReplaceContentsData *data = user_data;
7200 /* Ignore errors here, we're only reading anyway */
7201 g_output_stream_close_finish (stream, close_res, NULL);
7202 g_object_unref (stream);
7206 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
7207 g_task_return_boolean (data->task, TRUE);
7209 g_object_unref (data->task);
7213 replace_contents_write_callback (GObject *obj,
7214 GAsyncResult *read_res,
7217 GOutputStream *stream = G_OUTPUT_STREAM (obj);
7218 ReplaceContentsData *data = user_data;
7219 GError *error = NULL;
7222 write_size = g_output_stream_write_finish (stream, read_res, &error);
7224 if (write_size <= 0)
7226 /* Error or EOF, close the file */
7229 data->failed = TRUE;
7230 g_task_return_error (data->task, error);
7232 g_output_stream_close_async (stream, 0,
7233 g_task_get_cancellable (data->task),
7234 replace_contents_close_callback, data);
7236 else if (write_size > 0)
7238 const gchar *content;
7241 content = g_bytes_get_data (data->content, &length);
7242 data->pos += write_size;
7244 if (data->pos >= length)
7245 g_output_stream_close_async (stream, 0,
7246 g_task_get_cancellable (data->task),
7247 replace_contents_close_callback, data);
7249 g_output_stream_write_async (stream,
7250 content + data->pos,
7253 g_task_get_cancellable (data->task),
7254 replace_contents_write_callback,
7260 replace_contents_open_callback (GObject *obj,
7261 GAsyncResult *open_res,
7264 GFile *file = G_FILE (obj);
7265 GFileOutputStream *stream;
7266 ReplaceContentsData *data = user_data;
7267 GError *error = NULL;
7269 stream = g_file_replace_finish (file, open_res, &error);
7273 const gchar *content;
7276 content = g_bytes_get_data (data->content, &length);
7277 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
7278 content + data->pos,
7281 g_task_get_cancellable (data->task),
7282 replace_contents_write_callback,
7287 g_task_return_error (data->task, error);
7288 g_object_unref (data->task);
7293 * g_file_replace_contents_async:
7294 * @file: input #GFile
7295 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
7296 * @length: the length of @contents in bytes
7297 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7298 * @make_backup: %TRUE if a backup should be created
7299 * @flags: a set of #GFileCreateFlags
7300 * @cancellable: optional #GCancellable object, %NULL to ignore
7301 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7302 * @user_data: the data to pass to callback function
7304 * Starts an asynchronous replacement of @file with the given
7305 * @contents of @length bytes. @etag will replace the document's
7306 * current entity tag.
7308 * When this operation has completed, @callback will be called with
7309 * @user_user data, and the operation can be finalized with
7310 * g_file_replace_contents_finish().
7312 * If @cancellable is not %NULL, then the operation can be cancelled by
7313 * triggering the cancellable object from another thread. If the operation
7314 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7316 * If @make_backup is %TRUE, this function will attempt to
7317 * make a backup of @file.
7319 * Note that no copy of @content will be made, so it must stay valid
7320 * until @callback is called. See g_file_replace_contents_bytes_async()
7321 * for a #GBytes version that will automatically hold a reference to the
7322 * contents (without copying) for the duration of the call.
7325 g_file_replace_contents_async (GFile *file,
7326 const char *contents,
7329 gboolean make_backup,
7330 GFileCreateFlags flags,
7331 GCancellable *cancellable,
7332 GAsyncReadyCallback callback,
7337 bytes = g_bytes_new_static (contents, length);
7338 g_file_replace_contents_bytes_async (file, bytes, etag, make_backup, flags,
7339 cancellable, callback, user_data);
7340 g_bytes_unref (bytes);
7344 * g_file_replace_contents_bytes_async:
7345 * @file: input #GFile
7346 * @contents: a #GBytes
7347 * @etag: (allow-none): a new [entity tag][gfile-etag] for the @file, or %NULL
7348 * @make_backup: %TRUE if a backup should be created
7349 * @flags: a set of #GFileCreateFlags
7350 * @cancellable: optional #GCancellable object, %NULL to ignore
7351 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
7352 * @user_data: the data to pass to callback function
7354 * Same as g_file_replace_contents_async() but takes a #GBytes input instead.
7355 * This function will keep a ref on @contents until the operation is done.
7356 * Unlike g_file_replace_contents_async() this allows forgetting about the
7357 * content without waiting for the callback.
7359 * When this operation has completed, @callback will be called with
7360 * @user_user data, and the operation can be finalized with
7361 * g_file_replace_contents_finish().
7366 g_file_replace_contents_bytes_async (GFile *file,
7369 gboolean make_backup,
7370 GFileCreateFlags flags,
7371 GCancellable *cancellable,
7372 GAsyncReadyCallback callback,
7375 ReplaceContentsData *data;
7377 g_return_if_fail (G_IS_FILE (file));
7378 g_return_if_fail (contents != NULL);
7380 data = g_new0 (ReplaceContentsData, 1);
7382 data->content = g_bytes_ref (contents);
7384 data->task = g_task_new (file, cancellable, callback, user_data);
7385 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
7387 g_file_replace_async (file,
7392 g_task_get_cancellable (data->task),
7393 replace_contents_open_callback,
7398 * g_file_replace_contents_finish:
7399 * @file: input #GFile
7400 * @res: a #GAsyncResult
7401 * @new_etag: (out) (allow-none): a location of a new [entity tag][gfile-etag]
7402 * for the document. This should be freed with g_free() when it is no
7403 * longer needed, or %NULL
7404 * @error: a #GError, or %NULL
7406 * Finishes an asynchronous replace of the given @file. See
7407 * g_file_replace_contents_async(). Sets @new_etag to the new entity
7408 * tag for the document, if present.
7410 * Returns: %TRUE on success, %FALSE on failure.
7413 g_file_replace_contents_finish (GFile *file,
7419 ReplaceContentsData *data;
7421 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7422 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
7424 task = G_TASK (res);
7426 if (!g_task_propagate_boolean (task, error))
7429 data = g_task_get_task_data (task);
7433 *new_etag = data->etag;
7434 data->etag = NULL; /* Take ownership */
7441 g_file_real_measure_disk_usage (GFile *file,
7442 GFileMeasureFlags flags,
7443 GCancellable *cancellable,
7444 GFileMeasureProgressCallback progress_callback,
7445 gpointer progress_data,
7446 guint64 *disk_usage,
7451 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7452 "Operation not supported for the current backend.");
7458 GFileMeasureFlags flags;
7459 GFileMeasureProgressCallback progress_callback;
7460 gpointer progress_data;
7472 GFileMeasureProgressCallback callback;
7475 guint64 current_size;
7481 measure_disk_usage_invoke_progress (gpointer user_data)
7483 MeasureProgress *progress = user_data;
7485 (* progress->callback) (progress->reporting,
7486 progress->current_size, progress->num_dirs, progress->num_files,
7487 progress->user_data);
7493 measure_disk_usage_progress (gboolean reporting,
7494 guint64 current_size,
7499 MeasureProgress progress;
7500 GTask *task = user_data;
7501 MeasureTaskData *data;
7503 data = g_task_get_task_data (task);
7505 progress.callback = data->progress_callback;
7506 progress.user_data = data->progress_data;
7507 progress.reporting = reporting;
7508 progress.current_size = current_size;
7509 progress.num_dirs = num_dirs;
7510 progress.num_files = num_files;
7512 g_main_context_invoke_full (g_task_get_context (task),
7513 g_task_get_priority (task),
7514 measure_disk_usage_invoke_progress,
7515 g_memdup (&progress, sizeof progress),
7520 measure_disk_usage_thread (GTask *task,
7521 gpointer source_object,
7523 GCancellable *cancellable)
7525 MeasureTaskData *data = task_data;
7526 GError *error = NULL;
7527 MeasureResult result = { 0, };
7529 if (g_file_measure_disk_usage (source_object, data->flags, cancellable,
7530 data->progress_callback ? measure_disk_usage_progress : NULL, task,
7531 &result.disk_usage, &result.num_dirs, &result.num_files,
7533 g_task_return_pointer (task, g_memdup (&result, sizeof result), g_free);
7535 g_task_return_error (task, error);
7539 g_file_real_measure_disk_usage_async (GFile *file,
7540 GFileMeasureFlags flags,
7542 GCancellable *cancellable,
7543 GFileMeasureProgressCallback progress_callback,
7544 gpointer progress_data,
7545 GAsyncReadyCallback callback,
7548 MeasureTaskData data;
7552 data.progress_callback = progress_callback;
7553 data.progress_data = progress_data;
7555 task = g_task_new (file, cancellable, callback, user_data);
7556 g_task_set_task_data (task, g_memdup (&data, sizeof data), g_free);
7557 g_task_set_priority (task, io_priority);
7559 g_task_run_in_thread (task, measure_disk_usage_thread);
7560 g_object_unref (task);
7564 g_file_real_measure_disk_usage_finish (GFile *file,
7565 GAsyncResult *result,
7566 guint64 *disk_usage,
7571 MeasureResult *measure_result;
7573 g_return_val_if_fail (g_task_is_valid (result, file), FALSE);
7575 measure_result = g_task_propagate_pointer (G_TASK (result), error);
7577 if (measure_result == NULL)
7581 *disk_usage = measure_result->disk_usage;
7584 *num_dirs = measure_result->num_dirs;
7587 *num_files = measure_result->num_files;
7589 g_free (measure_result);
7595 * g_file_measure_disk_usage:
7597 * @flags: #GFileMeasureFlags
7598 * @cancellable: (allow-none): optional #GCancellable
7599 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7600 * @progress_data: user_data for @progress_callback
7601 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7602 * @num_dirs: (allow-none) (out): the number of directories encountered
7603 * @num_files: (allow-none) (out): the number of non-directories encountered
7604 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7606 * Recursively measures the disk usage of @file.
7608 * This is essentially an analog of the 'du' command, but it also
7609 * reports the number of directories and non-directory files encountered
7610 * (including things like symbolic links).
7612 * By default, errors are only reported against the toplevel file
7613 * itself. Errors found while recursing are silently ignored, unless
7614 * %G_FILE_DISK_USAGE_REPORT_ALL_ERRORS is given in @flags.
7616 * The returned size, @disk_usage, is in bytes and should be formatted
7617 * with g_format_size() in order to get something reasonable for showing
7618 * in a user interface.
7620 * @progress_callback and @progress_data can be given to request
7621 * periodic progress updates while scanning. See the documentation for
7622 * #GFileMeasureProgressCallback for information about when and how the
7623 * callback will be invoked.
7625 * Returns: %TRUE if successful, with the out parameters set.
7626 * %FALSE otherwise, with @error set.
7631 g_file_measure_disk_usage (GFile *file,
7632 GFileMeasureFlags flags,
7633 GCancellable *cancellable,
7634 GFileMeasureProgressCallback progress_callback,
7635 gpointer progress_data,
7636 guint64 *disk_usage,
7641 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7642 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
7643 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7645 return G_FILE_GET_IFACE (file)->measure_disk_usage (file, flags, cancellable,
7646 progress_callback, progress_data,
7647 disk_usage, num_dirs, num_files,
7652 * g_file_measure_disk_usage_async:
7654 * @flags: #GFileMeasureFlags
7655 * @io_priority: the [I/O priority][io-priority] of the request
7656 * @cancellable: (allow-none): optional #GCancellable
7657 * @progress_callback: (allow-none): a #GFileMeasureProgressCallback
7658 * @progress_data: user_data for @progress_callback
7659 * @callback: (allow-none): a #GAsyncReadyCallback to call when complete
7660 * @user_data: the data to pass to callback function
7662 * Recursively measures the disk usage of @file.
7664 * This is the asynchronous version of g_file_measure_disk_usage(). See
7665 * there for more information.
7670 g_file_measure_disk_usage_async (GFile *file,
7671 GFileMeasureFlags flags,
7673 GCancellable *cancellable,
7674 GFileMeasureProgressCallback progress_callback,
7675 gpointer progress_data,
7676 GAsyncReadyCallback callback,
7679 g_return_if_fail (G_IS_FILE (file));
7680 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
7682 G_FILE_GET_IFACE (file)->measure_disk_usage_async (file, flags, io_priority, cancellable,
7683 progress_callback, progress_data,
7684 callback, user_data);
7688 * g_file_measure_disk_usage_finish:
7690 * @result: the #GAsyncResult passed to your #GAsyncReadyCallback
7691 * @disk_usage: (allow-none) (out): the number of bytes of disk space used
7692 * @num_dirs: (allow-none) (out): the number of directories encountered
7693 * @num_files: (allow-none) (out): the number of non-directories encountered
7694 * @error: (allow-none): %NULL, or a pointer to a %NULL #GError pointer
7696 * Collects the results from an earlier call to
7697 * g_file_measure_disk_usage_async(). See g_file_measure_disk_usage() for
7700 * Returns: %TRUE if successful, with the out parameters set.
7701 * %FALSE otherwise, with @error set.
7706 g_file_measure_disk_usage_finish (GFile *file,
7707 GAsyncResult *result,
7708 guint64 *disk_usage,
7713 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7714 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
7716 return G_FILE_GET_IFACE (file)->measure_disk_usage_finish (file, result, disk_usage, num_dirs, num_files, error);
7720 * g_file_start_mountable:
7721 * @file: input #GFile
7722 * @flags: flags affecting the operation
7723 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
7724 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
7725 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
7726 * @user_data: the data to pass to callback function
7728 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
7729 * Using @start_operation, you can request callbacks when, for instance,
7730 * passwords are needed during authentication.
7732 * If @cancellable is not %NULL, then the operation can be cancelled by
7733 * triggering the cancellable object from another thread. If the operation
7734 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7736 * When the operation is finished, @callback will be called.
7737 * You can then call g_file_mount_mountable_finish() to get
7738 * the result of the operation.
7743 g_file_start_mountable (GFile *file,
7744 GDriveStartFlags flags,
7745 GMountOperation *start_operation,
7746 GCancellable *cancellable,
7747 GAsyncReadyCallback callback,
7752 g_return_if_fail (G_IS_FILE (file));
7754 iface = G_FILE_GET_IFACE (file);
7756 if (iface->start_mountable == NULL)
7758 g_task_report_new_error (file, callback, user_data,
7759 g_file_start_mountable,
7760 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7761 _("Operation not supported"));
7765 (* iface->start_mountable) (file,
7774 * g_file_start_mountable_finish:
7775 * @file: input #GFile
7776 * @result: a #GAsyncResult
7777 * @error: a #GError, or %NULL
7779 * Finishes a start operation. See g_file_start_mountable() for details.
7781 * Finish an asynchronous start operation that was started
7782 * with g_file_start_mountable().
7784 * Returns: %TRUE if the operation finished successfully. %FALSE
7790 g_file_start_mountable_finish (GFile *file,
7791 GAsyncResult *result,
7796 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7797 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7799 if (g_async_result_legacy_propagate_error (result, error))
7801 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7802 return g_task_propagate_boolean (G_TASK (result), error);
7804 iface = G_FILE_GET_IFACE (file);
7805 return (* iface->start_mountable_finish) (file, result, error);
7809 * g_file_stop_mountable:
7810 * @file: input #GFile
7811 * @flags: flags affecting the operation
7812 * @mount_operation: (allow-none): a #GMountOperation,
7813 * or %NULL to avoid user interaction.
7814 * @cancellable: (allow-none): optional #GCancellable object,
7816 * @callback: (allow-none): a #GAsyncReadyCallback to call
7817 * when the request is satisfied, or %NULL
7818 * @user_data: the data to pass to callback function
7820 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7822 * If @cancellable is not %NULL, then the operation can be cancelled by
7823 * triggering the cancellable object from another thread. If the operation
7824 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7826 * When the operation is finished, @callback will be called.
7827 * You can then call g_file_stop_mountable_finish() to get
7828 * the result of the operation.
7833 g_file_stop_mountable (GFile *file,
7834 GMountUnmountFlags flags,
7835 GMountOperation *mount_operation,
7836 GCancellable *cancellable,
7837 GAsyncReadyCallback callback,
7842 g_return_if_fail (G_IS_FILE (file));
7844 iface = G_FILE_GET_IFACE (file);
7846 if (iface->stop_mountable == NULL)
7848 g_task_report_new_error (file, callback, user_data,
7849 g_file_stop_mountable,
7850 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7851 _("Operation not supported"));
7855 (* iface->stop_mountable) (file,
7864 * g_file_stop_mountable_finish:
7865 * @file: input #GFile
7866 * @result: a #GAsyncResult
7867 * @error: a #GError, or %NULL
7869 * Finishes an stop operation, see g_file_stop_mountable() for details.
7871 * Finish an asynchronous stop operation that was started
7872 * with g_file_stop_mountable().
7874 * Returns: %TRUE if the operation finished successfully.
7880 g_file_stop_mountable_finish (GFile *file,
7881 GAsyncResult *result,
7886 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7887 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7889 if (g_async_result_legacy_propagate_error (result, error))
7891 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7892 return g_task_propagate_boolean (G_TASK (result), error);
7894 iface = G_FILE_GET_IFACE (file);
7895 return (* iface->stop_mountable_finish) (file, result, error);
7899 * g_file_poll_mountable:
7900 * @file: input #GFile
7901 * @cancellable: optional #GCancellable object, %NULL to ignore
7902 * @callback: (allow-none): a #GAsyncReadyCallback to call
7903 * when the request is satisfied, or %NULL
7904 * @user_data: the data to pass to callback function
7906 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7908 * If @cancellable is not %NULL, then the operation can be cancelled by
7909 * triggering the cancellable object from another thread. If the operation
7910 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7912 * When the operation is finished, @callback will be called.
7913 * You can then call g_file_mount_mountable_finish() to get
7914 * the result of the operation.
7919 g_file_poll_mountable (GFile *file,
7920 GCancellable *cancellable,
7921 GAsyncReadyCallback callback,
7926 g_return_if_fail (G_IS_FILE (file));
7928 iface = G_FILE_GET_IFACE (file);
7930 if (iface->poll_mountable == NULL)
7932 g_task_report_new_error (file, callback, user_data,
7933 g_file_poll_mountable,
7934 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7935 _("Operation not supported"));
7939 (* iface->poll_mountable) (file,
7946 * g_file_poll_mountable_finish:
7947 * @file: input #GFile
7948 * @result: a #GAsyncResult
7949 * @error: a #GError, or %NULL
7951 * Finishes a poll operation. See g_file_poll_mountable() for details.
7953 * Finish an asynchronous poll operation that was polled
7954 * with g_file_poll_mountable().
7956 * Returns: %TRUE if the operation finished successfully. %FALSE
7962 g_file_poll_mountable_finish (GFile *file,
7963 GAsyncResult *result,
7968 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7969 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7971 if (g_async_result_legacy_propagate_error (result, error))
7973 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7974 return g_task_propagate_boolean (G_TASK (result), error);
7976 iface = G_FILE_GET_IFACE (file);
7977 return (* iface->poll_mountable_finish) (file, result, error);
7981 * g_file_supports_thread_contexts:
7984 * Checks if @file supports
7985 * [thread-default contexts][g-main-context-push-thread-default-context].
7986 * If this returns %FALSE, you cannot perform asynchronous operations on
7987 * @file in a thread that has a thread-default context.
7989 * Returns: Whether or not @file supports thread-default contexts.
7994 g_file_supports_thread_contexts (GFile *file)
7998 g_return_val_if_fail (G_IS_FILE (file), FALSE);
8000 iface = G_FILE_GET_IFACE (file);
8001 return iface->supports_thread_contexts;