Improvde #include order consistency
[glib.git] / gio / gfile.c
blobcd02b66e682c0baae499e588a9f793198f76f83f
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "config.h"
26 #ifdef HAVE_SPLICE
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #endif
32 #include <string.h>
33 #include <sys/types.h>
34 #ifdef HAVE_PWD_H
35 #include <pwd.h>
36 #endif
37 #include "gfile.h"
38 #include "gvfs.h"
39 #include "gtask.h"
40 #include "gfileattribute-priv.h"
41 #include "gfiledescriptorbased.h"
42 #include "gpollfilemonitor.h"
43 #include "gappinfo.h"
44 #include "gfileinputstream.h"
45 #include "gfileoutputstream.h"
46 #include "glocalfileoutputstream.h"
47 #include "glocalfileiostream.h"
48 #include "gcancellable.h"
49 #include "gasyncresult.h"
50 #include "gioerror.h"
51 #include "glibintl.h"
54 /**
55 * SECTION:gfile
56 * @short_description: File and Directory Handling
57 * @include: gio/gio.h
58 * @see_also: #GFileInfo, #GFileEnumerator
60 * #GFile is a high level abstraction for manipulating files on a
61 * virtual file system. #GFiles are lightweight, immutable objects
62 * that do no I/O upon creation. It is necessary to understand that
63 * #GFile objects do not represent files, merely an identifier for a
64 * file. All file content I/O is implemented as streaming operations
65 * (see #GInputStream and #GOutputStream).
67 * To construct a #GFile, you can use:
68 * <simplelist>
69 * <member>g_file_new_for_path() if you have a path.</member>
70 * <member>g_file_new_for_uri() if you have a URI.</member>
71 * <member>g_file_new_for_commandline_arg() for a command line argument.</member>
72 * <member>g_file_new_tmp() to create a temporary file from a template.</member>
73 * <member>g_file_parse_name() from a UTF-8 string gotten from g_file_get_parse_name().</member>
74 * </simplelist>
76 * One way to think of a #GFile is as an abstraction of a pathname. For
77 * normal files the system pathname is what is stored internally, but as
78 * #GFiles are extensible it could also be something else that corresponds
79 * to a pathname in a userspace implementation of a filesystem.
81 * #GFiles make up hierarchies of directories and files that correspond to
82 * the files on a filesystem. You can move through the file system with
83 * #GFile using g_file_get_parent() to get an identifier for the parent
84 * directory, g_file_get_child() to get a child within a directory,
85 * g_file_resolve_relative_path() to resolve a relative path between two
86 * #GFiles. There can be multiple hierarchies, so you may not end up at
87 * the same root if you repeatedly call g_file_get_parent() on two different
88 * files.
90 * All #GFiles have a basename (get with g_file_get_basename()). These names
91 * are byte strings that are used to identify the file on the filesystem
92 * (relative to its parent directory) and there is no guarantees that they
93 * have any particular charset encoding or even make any sense at all. If
94 * you want to use filenames in a user interface you should use the display
95 * name that you can get by requesting the
96 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
97 * This is guaranteed to be in UTF-8 and can be used in a user interface.
98 * But always store the real basename or the #GFile to use to actually
99 * access the file, because there is no way to go from a display name to
100 * the actual name.
102 * Using #GFile as an identifier has the same weaknesses as using a path
103 * in that there may be multiple aliases for the same file. For instance,
104 * hard or soft links may cause two different #GFiles to refer to the same
105 * file. Other possible causes for aliases are: case insensitive filesystems,
106 * short and long names on FAT/NTFS, or bind mounts in Linux. If you want to
107 * check if two #GFiles point to the same file you can query for the
108 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
109 * canonicalization of pathnames passed in, so that trivial differences in
110 * the path string used at creation (duplicated slashes, slash at end of
111 * path, "." or ".." path segments, etc) does not create different #GFiles.
113 * Many #GFile operations have both synchronous and asynchronous versions
114 * to suit your application. Asynchronous versions of synchronous functions
115 * simply have _async() appended to their function names. The asynchronous
116 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
117 * the operation, producing a GAsyncResult which is then passed to the
118 * function's matching _finish() operation.
120 * Some #GFile operations do not have synchronous analogs, as they may
121 * take a very long time to finish, and blocking may leave an application
122 * unusable. Notable cases include:
123 * <simplelist>
124 * <member>g_file_mount_mountable() to mount a mountable file.</member>
125 * <member>g_file_unmount_mountable_with_operation() to unmount a mountable file.</member>
126 * <member>g_file_eject_mountable_with_operation() to eject a mountable file.</member>
127 * </simplelist>
129 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
130 * One notable feature of #GFiles are entity tags, or "etags" for
131 * short. Entity tags are somewhat like a more abstract version of the
132 * traditional mtime, and can be used to quickly determine if the file has
133 * been modified from the version on the file system. See the HTTP 1.1
134 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
135 * for HTTP Etag headers, which are a very similar concept.
136 * </para>
139 static void g_file_real_query_info_async (GFile *file,
140 const char *attributes,
141 GFileQueryInfoFlags flags,
142 int io_priority,
143 GCancellable *cancellable,
144 GAsyncReadyCallback callback,
145 gpointer user_data);
146 static GFileInfo * g_file_real_query_info_finish (GFile *file,
147 GAsyncResult *res,
148 GError **error);
149 static void g_file_real_query_filesystem_info_async (GFile *file,
150 const char *attributes,
151 int io_priority,
152 GCancellable *cancellable,
153 GAsyncReadyCallback callback,
154 gpointer user_data);
155 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
156 GAsyncResult *res,
157 GError **error);
158 static void g_file_real_enumerate_children_async (GFile *file,
159 const char *attributes,
160 GFileQueryInfoFlags flags,
161 int io_priority,
162 GCancellable *cancellable,
163 GAsyncReadyCallback callback,
164 gpointer user_data);
165 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
166 GAsyncResult *res,
167 GError **error);
168 static void g_file_real_read_async (GFile *file,
169 int io_priority,
170 GCancellable *cancellable,
171 GAsyncReadyCallback callback,
172 gpointer user_data);
173 static GFileInputStream * g_file_real_read_finish (GFile *file,
174 GAsyncResult *res,
175 GError **error);
176 static void g_file_real_append_to_async (GFile *file,
177 GFileCreateFlags flags,
178 int io_priority,
179 GCancellable *cancellable,
180 GAsyncReadyCallback callback,
181 gpointer user_data);
182 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
183 GAsyncResult *res,
184 GError **error);
185 static void g_file_real_create_async (GFile *file,
186 GFileCreateFlags flags,
187 int io_priority,
188 GCancellable *cancellable,
189 GAsyncReadyCallback callback,
190 gpointer user_data);
191 static GFileOutputStream *g_file_real_create_finish (GFile *file,
192 GAsyncResult *res,
193 GError **error);
194 static void g_file_real_replace_async (GFile *file,
195 const char *etag,
196 gboolean make_backup,
197 GFileCreateFlags flags,
198 int io_priority,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
201 gpointer user_data);
202 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
203 GAsyncResult *res,
204 GError **error);
205 static void g_file_real_delete_async (GFile *file,
206 int io_priority,
207 GCancellable *cancellable,
208 GAsyncReadyCallback callback,
209 gpointer user_data);
210 static gboolean g_file_real_delete_finish (GFile *file,
211 GAsyncResult *res,
212 GError **error);
213 static void g_file_real_open_readwrite_async (GFile *file,
214 int io_priority,
215 GCancellable *cancellable,
216 GAsyncReadyCallback callback,
217 gpointer user_data);
218 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
219 GAsyncResult *res,
220 GError **error);
221 static void g_file_real_create_readwrite_async (GFile *file,
222 GFileCreateFlags flags,
223 int io_priority,
224 GCancellable *cancellable,
225 GAsyncReadyCallback callback,
226 gpointer user_data);
227 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
228 GAsyncResult *res,
229 GError **error);
230 static void g_file_real_replace_readwrite_async (GFile *file,
231 const char *etag,
232 gboolean make_backup,
233 GFileCreateFlags flags,
234 int io_priority,
235 GCancellable *cancellable,
236 GAsyncReadyCallback callback,
237 gpointer user_data);
238 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
239 GAsyncResult *res,
240 GError **error);
241 static gboolean g_file_real_set_attributes_from_info (GFile *file,
242 GFileInfo *info,
243 GFileQueryInfoFlags flags,
244 GCancellable *cancellable,
245 GError **error);
246 static void g_file_real_set_display_name_async (GFile *file,
247 const char *display_name,
248 int io_priority,
249 GCancellable *cancellable,
250 GAsyncReadyCallback callback,
251 gpointer user_data);
252 static GFile * g_file_real_set_display_name_finish (GFile *file,
253 GAsyncResult *res,
254 GError **error);
255 static void g_file_real_set_attributes_async (GFile *file,
256 GFileInfo *info,
257 GFileQueryInfoFlags flags,
258 int io_priority,
259 GCancellable *cancellable,
260 GAsyncReadyCallback callback,
261 gpointer user_data);
262 static gboolean g_file_real_set_attributes_finish (GFile *file,
263 GAsyncResult *res,
264 GFileInfo **info,
265 GError **error);
266 static void g_file_real_find_enclosing_mount_async (GFile *file,
267 int io_priority,
268 GCancellable *cancellable,
269 GAsyncReadyCallback callback,
270 gpointer user_data);
271 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
272 GAsyncResult *res,
273 GError **error);
274 static void g_file_real_copy_async (GFile *source,
275 GFile *destination,
276 GFileCopyFlags flags,
277 int io_priority,
278 GCancellable *cancellable,
279 GFileProgressCallback progress_callback,
280 gpointer progress_callback_data,
281 GAsyncReadyCallback callback,
282 gpointer user_data);
283 static gboolean g_file_real_copy_finish (GFile *file,
284 GAsyncResult *res,
285 GError **error);
287 typedef GFileIface GFileInterface;
288 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
290 static void
291 g_file_default_init (GFileIface *iface)
293 iface->enumerate_children_async = g_file_real_enumerate_children_async;
294 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
295 iface->set_display_name_async = g_file_real_set_display_name_async;
296 iface->set_display_name_finish = g_file_real_set_display_name_finish;
297 iface->query_info_async = g_file_real_query_info_async;
298 iface->query_info_finish = g_file_real_query_info_finish;
299 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
300 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
301 iface->set_attributes_async = g_file_real_set_attributes_async;
302 iface->set_attributes_finish = g_file_real_set_attributes_finish;
303 iface->read_async = g_file_real_read_async;
304 iface->read_finish = g_file_real_read_finish;
305 iface->append_to_async = g_file_real_append_to_async;
306 iface->append_to_finish = g_file_real_append_to_finish;
307 iface->create_async = g_file_real_create_async;
308 iface->create_finish = g_file_real_create_finish;
309 iface->replace_async = g_file_real_replace_async;
310 iface->replace_finish = g_file_real_replace_finish;
311 iface->delete_file_async = g_file_real_delete_async;
312 iface->delete_file_finish = g_file_real_delete_finish;
313 iface->open_readwrite_async = g_file_real_open_readwrite_async;
314 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
315 iface->create_readwrite_async = g_file_real_create_readwrite_async;
316 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
317 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
318 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
319 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
320 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
321 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
322 iface->copy_async = g_file_real_copy_async;
323 iface->copy_finish = g_file_real_copy_finish;
328 * g_file_is_native:
329 * @file: input #GFile
331 * Checks to see if a file is native to the platform.
333 * A native file s one expressed in the platform-native filename format,
334 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
335 * as it might be on a locally mounted remote filesystem.
337 * On some systems non-native files may be available using the native
338 * filesystem via a userspace filesystem (FUSE), in these cases this call
339 * will return %FALSE, but g_file_get_path() will still return a native path.
341 * This call does no blocking I/O.
343 * Returns: %TRUE if @file is native
345 gboolean
346 g_file_is_native (GFile *file)
348 GFileIface *iface;
350 g_return_val_if_fail (G_IS_FILE (file), FALSE);
352 iface = G_FILE_GET_IFACE (file);
354 return (* iface->is_native) (file);
359 * g_file_has_uri_scheme:
360 * @file: input #GFile
361 * @uri_scheme: a string containing a URI scheme
363 * Checks to see if a #GFile has a given URI scheme.
365 * This call does no blocking I/O.
367 * Returns: %TRUE if #GFile's backend supports the
368 * given URI scheme, %FALSE if URI scheme is %NULL,
369 * not supported, or #GFile is invalid.
371 gboolean
372 g_file_has_uri_scheme (GFile *file,
373 const char *uri_scheme)
375 GFileIface *iface;
377 g_return_val_if_fail (G_IS_FILE (file), FALSE);
378 g_return_val_if_fail (uri_scheme != NULL, FALSE);
380 iface = G_FILE_GET_IFACE (file);
382 return (* iface->has_uri_scheme) (file, uri_scheme);
387 * g_file_get_uri_scheme:
388 * @file: input #GFile
390 * Gets the URI scheme for a #GFile.
391 * RFC 3986 decodes the scheme as:
392 * <programlisting>
393 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
394 * </programlisting>
395 * Common schemes include "file", "http", "ftp", etc.
397 * This call does no blocking I/O.
399 * Returns: a string containing the URI scheme for the given
400 * #GFile. The returned string should be freed with g_free()
401 * when no longer needed.
403 char *
404 g_file_get_uri_scheme (GFile *file)
406 GFileIface *iface;
408 g_return_val_if_fail (G_IS_FILE (file), NULL);
410 iface = G_FILE_GET_IFACE (file);
412 return (* iface->get_uri_scheme) (file);
417 * g_file_get_basename:
418 * @file: input #GFile
420 * Gets the base name (the last component of the path) for a given #GFile.
422 * If called for the top level of a system (such as the filesystem root
423 * or a uri like sftp://host/) it will return a single directory separator
424 * (and on Windows, possibly a drive letter).
426 * The base name is a byte string (not UTF-8). It has no defined encoding
427 * or rules other than it may not contain zero bytes. If you want to use
428 * filenames in a user interface you should use the display name that you
429 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
430 * attribute with g_file_query_info().
432 * This call does no blocking I/O.
434 * Returns: string containing the #GFile's base name, or %NULL
435 * if given #GFile is invalid. The returned string should be
436 * freed with g_free() when no longer needed.
438 char *
439 g_file_get_basename (GFile *file)
441 GFileIface *iface;
443 g_return_val_if_fail (G_IS_FILE (file), NULL);
445 iface = G_FILE_GET_IFACE (file);
447 return (* iface->get_basename) (file);
451 * g_file_get_path:
452 * @file: input #GFile
454 * Gets the local pathname for #GFile, if one exists.
456 * This call does no blocking I/O.
458 * Returns: string containing the #GFile's path, or %NULL if
459 * no such path exists. The returned string should be
460 * freed with g_free() when no longer needed.
462 char *
463 g_file_get_path (GFile *file)
465 GFileIface *iface;
467 g_return_val_if_fail (G_IS_FILE (file), NULL);
469 iface = G_FILE_GET_IFACE (file);
471 return (* iface->get_path) (file);
475 * g_file_get_uri:
476 * @file: input #GFile
478 * Gets the URI for the @file.
480 * This call does no blocking I/O.
482 * Returns: a string containing the #GFile's URI.
483 * The returned string should be freed with g_free()
484 * when no longer needed.
486 char *
487 g_file_get_uri (GFile *file)
489 GFileIface *iface;
491 g_return_val_if_fail (G_IS_FILE (file), NULL);
493 iface = G_FILE_GET_IFACE (file);
495 return (* iface->get_uri) (file);
499 * g_file_get_parse_name:
500 * @file: input #GFile
502 * Gets the parse name of the @file.
503 * A parse name is a UTF-8 string that describes the
504 * file such that one can get the #GFile back using
505 * g_file_parse_name().
507 * This is generally used to show the #GFile as a nice
508 * full-pathname kind of string in a user interface,
509 * like in a location entry.
511 * For local files with names that can safely be converted
512 * to UTF-8 the pathname is used, otherwise the IRI is used
513 * (a form of URI that allows UTF-8 characters unescaped).
515 * This call does no blocking I/O.
517 * Returns: a string containing the #GFile's parse name.
518 * The returned string should be freed with g_free()
519 * when no longer needed.
521 char *
522 g_file_get_parse_name (GFile *file)
524 GFileIface *iface;
526 g_return_val_if_fail (G_IS_FILE (file), NULL);
528 iface = G_FILE_GET_IFACE (file);
530 return (* iface->get_parse_name) (file);
534 * g_file_dup:
535 * @file: input #GFile
537 * Duplicates a #GFile handle. This operation does not duplicate
538 * the actual file or directory represented by the #GFile; see
539 * g_file_copy() if attempting to copy a file.
541 * This call does no blocking I/O.
543 * Returns: (transfer full): a new #GFile that is a duplicate
544 * of the given #GFile.
546 GFile *
547 g_file_dup (GFile *file)
549 GFileIface *iface;
551 g_return_val_if_fail (G_IS_FILE (file), NULL);
553 iface = G_FILE_GET_IFACE (file);
555 return (* iface->dup) (file);
559 * g_file_hash:
560 * @file: (type GFile): #gconstpointer to a #GFile
562 * Creates a hash value for a #GFile.
564 * This call does no blocking I/O.
566 * Virtual: hash
567 * Returns: 0 if @file is not a valid #GFile, otherwise an
568 * integer that can be used as hash value for the #GFile.
569 * This function is intended for easily hashing a #GFile to
570 * add to a #GHashTable or similar data structure.
572 guint
573 g_file_hash (gconstpointer file)
575 GFileIface *iface;
577 g_return_val_if_fail (G_IS_FILE (file), 0);
579 iface = G_FILE_GET_IFACE (file);
581 return (* iface->hash) ((GFile *)file);
585 * g_file_equal:
586 * @file1: the first #GFile
587 * @file2: the second #GFile
589 * Checks equality of two given #GFiles.
591 * Note that two #GFiles that differ can still refer to the same
592 * file on the filesystem due to various forms of filename
593 * aliasing.
595 * This call does no blocking I/O.
597 * Returns: %TRUE if @file1 and @file2 are equal.
598 * %FALSE if either is not a #GFile.
600 gboolean
601 g_file_equal (GFile *file1,
602 GFile *file2)
604 GFileIface *iface;
606 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
607 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
609 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
610 return FALSE;
612 iface = G_FILE_GET_IFACE (file1);
614 return (* iface->equal) (file1, file2);
619 * g_file_get_parent:
620 * @file: input #GFile
622 * Gets the parent directory for the @file.
623 * If the @file represents the root directory of the
624 * file system, then %NULL will be returned.
626 * This call does no blocking I/O.
628 * Returns: (transfer full): a #GFile structure to the
629 * parent of the given #GFile or %NULL if there is
630 * no parent. Free the returned object with g_object_unref().
632 GFile *
633 g_file_get_parent (GFile *file)
635 GFileIface *iface;
637 g_return_val_if_fail (G_IS_FILE (file), NULL);
639 iface = G_FILE_GET_IFACE (file);
641 return (* iface->get_parent) (file);
645 * g_file_has_parent:
646 * @file: input #GFile
647 * @parent: (allow-none): the parent to check for, or %NULL
649 * Checks if @file has a parent, and optionally, if it is @parent.
651 * If @parent is %NULL then this function returns %TRUE if @file has any
652 * parent at all. If @parent is non-%NULL then %TRUE is only returned
653 * if @file is a child of @parent.
655 * Returns: %TRUE if @file is a child of @parent (or any parent in the
656 * case that @parent is %NULL).
658 * Since: 2.24
660 gboolean
661 g_file_has_parent (GFile *file,
662 GFile *parent)
664 GFile *actual_parent;
665 gboolean result;
667 g_return_val_if_fail (G_IS_FILE (file), FALSE);
668 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
670 actual_parent = g_file_get_parent (file);
672 if (actual_parent != NULL)
674 if (parent != NULL)
675 result = g_file_equal (parent, actual_parent);
676 else
677 result = TRUE;
679 g_object_unref (actual_parent);
681 else
682 result = FALSE;
684 return result;
688 * g_file_get_child:
689 * @file: input #GFile
690 * @name: string containing the child's basename
692 * Gets a child of @file with basename equal to @name.
694 * Note that the file with that specific name might not exist, but
695 * you can still have a #GFile that points to it. You can use this
696 * for instance to create that file.
698 * This call does no blocking I/O.
700 * Returns: (transfer full): a #GFile to a child specified by @name.
701 * Free the returned object with g_object_unref().
703 GFile *
704 g_file_get_child (GFile *file,
705 const char *name)
707 g_return_val_if_fail (G_IS_FILE (file), NULL);
708 g_return_val_if_fail (name != NULL, NULL);
710 return g_file_resolve_relative_path (file, name);
714 * g_file_get_child_for_display_name:
715 * @file: input #GFile
716 * @display_name: string to a possible child
717 * @error: return location for an error
719 * Gets the child of @file for a given @display_name (i.e. a UTF-8
720 * version of the name). If this function fails, it returns %NULL
721 * and @error will be set. This is very useful when constructing a
722 * #GFile for a new file and the user entered the filename in the
723 * user interface, for instance when you select a directory and
724 * type a filename in the file selector.
726 * This call does no blocking I/O.
728 * Returns: (transfer full): a #GFile to the specified child, or
729 * %NULL if the display name couldn't be converted.
730 * Free the returned object with g_object_unref().
732 GFile *
733 g_file_get_child_for_display_name (GFile *file,
734 const char *display_name,
735 GError **error)
737 GFileIface *iface;
739 g_return_val_if_fail (G_IS_FILE (file), NULL);
740 g_return_val_if_fail (display_name != NULL, NULL);
742 iface = G_FILE_GET_IFACE (file);
744 return (* iface->get_child_for_display_name) (file, display_name, error);
748 * g_file_has_prefix:
749 * @file: input #GFile
750 * @prefix: input #GFile
752 * Checks whether @file has the prefix specified by @prefix.
754 * In other words, if the names of initial elements of @file's
755 * pathname match @prefix. Only full pathname elements are matched,
756 * so a path like /foo is not considered a prefix of /foobar, only
757 * of /foo/bar.
759 * This call does no I/O, as it works purely on names. As such it can
760 * sometimes return %FALSE even if @file is inside a @prefix (from a
761 * filesystem point of view), because the prefix of @file is an alias
762 * of @prefix.
764 * Virtual: prefix_matches
765 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix,
766 * %FALSE otherwise.
768 gboolean
769 g_file_has_prefix (GFile *file,
770 GFile *prefix)
772 GFileIface *iface;
774 g_return_val_if_fail (G_IS_FILE (file), FALSE);
775 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
777 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
778 return FALSE;
780 iface = G_FILE_GET_IFACE (file);
782 /* The vtable function differs in arg order since
783 * we're using the old contains_file call
785 return (* iface->prefix_matches) (prefix, file);
789 * g_file_get_relative_path:
790 * @parent: input #GFile
791 * @descendant: input #GFile
793 * Gets the path for @descendant relative to @parent.
795 * This call does no blocking I/O.
797 * Returns: string with the relative path from @descendant
798 * to @parent, or %NULL if @descendant doesn't have @parent
799 * as prefix. The returned string should be freed with g_free()
800 * when no longer needed.
802 char *
803 g_file_get_relative_path (GFile *parent,
804 GFile *descendant)
806 GFileIface *iface;
808 g_return_val_if_fail (G_IS_FILE (parent), NULL);
809 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
811 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
812 return NULL;
814 iface = G_FILE_GET_IFACE (parent);
816 return (* iface->get_relative_path) (parent, descendant);
820 * g_file_resolve_relative_path:
821 * @file: input #GFile
822 * @relative_path: a given relative path string
824 * Resolves a relative path for @file to an absolute path.
826 * This call does no blocking I/O.
828 * Returns: (transfer full): #GFile to the resolved path.
829 * %NULL if @relative_path is %NULL or if @file is invalid.
830 * Free the returned object with g_object_unref().
832 GFile *
833 g_file_resolve_relative_path (GFile *file,
834 const char *relative_path)
836 GFileIface *iface;
838 g_return_val_if_fail (G_IS_FILE (file), NULL);
839 g_return_val_if_fail (relative_path != NULL, NULL);
841 iface = G_FILE_GET_IFACE (file);
843 return (* iface->resolve_relative_path) (file, relative_path);
847 * g_file_enumerate_children:
848 * @file: input #GFile
849 * @attributes: an attribute query string
850 * @flags: a set of #GFileQueryInfoFlags
851 * @cancellable: (allow-none): optional #GCancellable object,
852 * %NULL to ignore
853 * @error: #GError for error reporting
855 * Gets the requested information about the files in a directory.
856 * The result is a #GFileEnumerator object that will give out
857 * #GFileInfo objects for all the files in the directory.
859 * The @attributes value is a string that specifies the file
860 * attributes that should be gathered. It is not an error if
861 * it's not possible to read a particular requested attribute
862 * from a file - it just won't be set. @attributes should
863 * be a comma-separated list of attributes or attribute wildcards.
864 * The wildcard "*" means all attributes, and a wildcard like
865 * "standard::*" means all attributes in the standard namespace.
866 * An example attribute query be "standard::*,owner::user".
867 * The standard attributes are available as defines, like
868 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
870 * If @cancellable is not %NULL, then the operation can be cancelled
871 * by triggering the cancellable object from another thread. If the
872 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
873 * returned.
875 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
876 * be returned. If the file is not a directory, the %G_IO_ERROR_NOT_DIRECTORY
877 * error will be returned. Other errors are possible too.
879 * Returns: (transfer full): A #GFileEnumerator if successful,
880 * %NULL on error. Free the returned object with g_object_unref().
882 GFileEnumerator *
883 g_file_enumerate_children (GFile *file,
884 const char *attributes,
885 GFileQueryInfoFlags flags,
886 GCancellable *cancellable,
887 GError **error)
889 GFileIface *iface;
891 g_return_val_if_fail (G_IS_FILE (file), NULL);
893 if (g_cancellable_set_error_if_cancelled (cancellable, error))
894 return NULL;
896 iface = G_FILE_GET_IFACE (file);
898 if (iface->enumerate_children == NULL)
900 g_set_error_literal (error, G_IO_ERROR,
901 G_IO_ERROR_NOT_SUPPORTED,
902 _("Operation not supported"));
903 return NULL;
906 return (* iface->enumerate_children) (file, attributes, flags,
907 cancellable, error);
911 * g_file_enumerate_children_async:
912 * @file: input #GFile
913 * @attributes: an attribute query string
914 * @flags: a set of #GFileQueryInfoFlags
915 * @io_priority: the <link linkend="io-priority">I/O priority</link>
916 * of the request
917 * @cancellable: (allow-none): optional #GCancellable object,
918 * %NULL to ignore
919 * @callback: (scope async): a #GAsyncReadyCallback to call when the
920 * request is satisfied
921 * @user_data: (closure): the data to pass to callback function
923 * Asynchronously gets the requested information about the files
924 * in a directory. The result is a #GFileEnumerator object that will
925 * give out #GFileInfo objects for all the files in the directory.
927 * For more details, see g_file_enumerate_children() which is
928 * the synchronous version of this call.
930 * When the operation is finished, @callback will be called. You can
931 * then call g_file_enumerate_children_finish() to get the result of
932 * the operation.
934 void
935 g_file_enumerate_children_async (GFile *file,
936 const char *attributes,
937 GFileQueryInfoFlags flags,
938 int io_priority,
939 GCancellable *cancellable,
940 GAsyncReadyCallback callback,
941 gpointer user_data)
943 GFileIface *iface;
945 g_return_if_fail (G_IS_FILE (file));
947 iface = G_FILE_GET_IFACE (file);
948 (* iface->enumerate_children_async) (file,
949 attributes,
950 flags,
951 io_priority,
952 cancellable,
953 callback,
954 user_data);
958 * g_file_enumerate_children_finish:
959 * @file: input #GFile
960 * @res: a #GAsyncResult
961 * @error: a #GError
963 * Finishes an async enumerate children operation.
964 * See g_file_enumerate_children_async().
966 * Returns: (transfer full): a #GFileEnumerator or %NULL
967 * if an error occurred.
968 * Free the returned object with g_object_unref().
970 GFileEnumerator *
971 g_file_enumerate_children_finish (GFile *file,
972 GAsyncResult *res,
973 GError **error)
975 GFileIface *iface;
977 g_return_val_if_fail (G_IS_FILE (file), NULL);
978 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
980 if (g_async_result_legacy_propagate_error (res, error))
981 return NULL;
983 iface = G_FILE_GET_IFACE (file);
984 return (* iface->enumerate_children_finish) (file, res, error);
988 * g_file_query_exists:
989 * @file: input #GFile
990 * @cancellable: (allow-none): optional #GCancellable object,
991 * %NULL to ignore
993 * Utility function to check if a particular file exists. This is
994 * implemented using g_file_query_info() and as such does blocking I/O.
996 * Note that in many cases it is racy to first check for file existence
997 * and then execute something based on the outcome of that, because the
998 * file might have been created or removed in between the operations. The
999 * general approach to handling that is to not check, but just do the
1000 * operation and handle the errors as they come.
1002 * As an example of race-free checking, take the case of reading a file,
1003 * and if it doesn't exist, creating it. There are two racy versions: read
1004 * it, and on error create it; and: check if it exists, if not create it.
1005 * These can both result in two processes creating the file (with perhaps
1006 * a partially written file as the result). The correct approach is to
1007 * always try to create the file with g_file_create() which will either
1008 * atomically create the file or fail with a %G_IO_ERROR_EXISTS error.
1010 * However, in many cases an existence check is useful in a user interface,
1011 * for instance to make a menu item sensitive/insensitive, so that you don't
1012 * have to fool users that something is possible and then just show an error
1013 * dialog. If you do this, you should make sure to also handle the errors
1014 * that can happen due to races when you execute the operation.
1016 * Returns: %TRUE if the file exists (and can be detected without error),
1017 * %FALSE otherwise (or if cancelled).
1019 gboolean
1020 g_file_query_exists (GFile *file,
1021 GCancellable *cancellable)
1023 GFileInfo *info;
1025 g_return_val_if_fail (G_IS_FILE(file), FALSE);
1027 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
1028 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
1029 if (info != NULL)
1031 g_object_unref (info);
1032 return TRUE;
1035 return FALSE;
1039 * g_file_query_file_type:
1040 * @file: input #GFile
1041 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info()
1042 * @cancellable: (allow-none): optional #GCancellable object,
1043 * %NULL to ignore
1045 * Utility function to inspect the #GFileType of a file. This is
1046 * implemented using g_file_query_info() and as such does blocking I/O.
1048 * The primary use case of this method is to check if a file is
1049 * a regular file, directory, or symlink.
1051 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN
1052 * if the file does not exist
1054 * Since: 2.18
1056 GFileType
1057 g_file_query_file_type (GFile *file,
1058 GFileQueryInfoFlags flags,
1059 GCancellable *cancellable)
1061 GFileInfo *info;
1062 GFileType file_type;
1064 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1065 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1066 cancellable, NULL);
1067 if (info != NULL)
1069 file_type = g_file_info_get_file_type (info);
1070 g_object_unref (info);
1072 else
1073 file_type = G_FILE_TYPE_UNKNOWN;
1075 return file_type;
1079 * g_file_query_info:
1080 * @file: input #GFile
1081 * @attributes: an attribute query string
1082 * @flags: a set of #GFileQueryInfoFlags
1083 * @cancellable: (allow-none): optional #GCancellable object,
1084 * %NULL to ignore
1085 * @error: a #GError
1087 * Gets the requested information about specified @file.
1088 * The result is a #GFileInfo object that contains key-value
1089 * attributes (such as the type or size of the file).
1091 * The @attributes value is a string that specifies the file
1092 * attributes that should be gathered. It is not an error if
1093 * it's not possible to read a particular requested attribute
1094 * from a file - it just won't be set. @attributes should be a
1095 * comma-separated list of attributes or attribute wildcards.
1096 * The wildcard "*" means all attributes, and a wildcard like
1097 * "standard::*" means all attributes in the standard namespace.
1098 * An example attribute query be "standard::*,owner::user".
1099 * The standard attributes are available as defines, like
1100 * #G_FILE_ATTRIBUTE_STANDARD_NAME.
1102 * If @cancellable is not %NULL, then the operation can be cancelled
1103 * by triggering the cancellable object from another thread. If the
1104 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1105 * returned.
1107 * For symlinks, normally the information about the target of the
1108 * symlink is returned, rather than information about the symlink
1109 * itself. However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
1110 * in @flags the information about the symlink itself will be returned.
1111 * Also, for symlinks that point to non-existing files the information
1112 * about the symlink itself will be returned.
1114 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1115 * returned. Other errors are possible too, and depend on what kind of
1116 * filesystem the file is on.
1118 * Returns: (transfer full): a #GFileInfo for the given @file, or %NULL
1119 * on error. Free the returned object with g_object_unref().
1121 GFileInfo *
1122 g_file_query_info (GFile *file,
1123 const char *attributes,
1124 GFileQueryInfoFlags flags,
1125 GCancellable *cancellable,
1126 GError **error)
1128 GFileIface *iface;
1130 g_return_val_if_fail (G_IS_FILE (file), NULL);
1132 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1133 return NULL;
1135 iface = G_FILE_GET_IFACE (file);
1137 if (iface->query_info == NULL)
1139 g_set_error_literal (error, G_IO_ERROR,
1140 G_IO_ERROR_NOT_SUPPORTED,
1141 _("Operation not supported"));
1142 return NULL;
1145 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1149 * g_file_query_info_async:
1150 * @file: input #GFile
1151 * @attributes: an attribute query string
1152 * @flags: a set of #GFileQueryInfoFlags
1153 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1154 * of the request
1155 * @cancellable: (allow-none): optional #GCancellable object,
1156 * %NULL to ignore
1157 * @callback: (scope async): a #GAsyncReadyCallback to call when the
1158 * request is satisfied
1159 * @user_data: (closure): the data to pass to callback function
1161 * Asynchronously gets the requested information about specified @file.
1162 * The result is a #GFileInfo object that contains key-value attributes
1163 * (such as type or size for the file).
1165 * For more details, see g_file_query_info() which is the synchronous
1166 * version of this call.
1168 * When the operation is finished, @callback will be called. You can
1169 * then call g_file_query_info_finish() to get the result of the operation.
1171 void
1172 g_file_query_info_async (GFile *file,
1173 const char *attributes,
1174 GFileQueryInfoFlags flags,
1175 int io_priority,
1176 GCancellable *cancellable,
1177 GAsyncReadyCallback callback,
1178 gpointer user_data)
1180 GFileIface *iface;
1182 g_return_if_fail (G_IS_FILE (file));
1184 iface = G_FILE_GET_IFACE (file);
1185 (* iface->query_info_async) (file,
1186 attributes,
1187 flags,
1188 io_priority,
1189 cancellable,
1190 callback,
1191 user_data);
1195 * g_file_query_info_finish:
1196 * @file: input #GFile
1197 * @res: a #GAsyncResult
1198 * @error: a #GError
1200 * Finishes an asynchronous file info query.
1201 * See g_file_query_info_async().
1203 * Returns: (transfer full): #GFileInfo for given @file
1204 * or %NULL on error. Free the returned object with
1205 * g_object_unref().
1207 GFileInfo *
1208 g_file_query_info_finish (GFile *file,
1209 GAsyncResult *res,
1210 GError **error)
1212 GFileIface *iface;
1214 g_return_val_if_fail (G_IS_FILE (file), NULL);
1215 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1217 if (g_async_result_legacy_propagate_error (res, error))
1218 return NULL;
1220 iface = G_FILE_GET_IFACE (file);
1221 return (* iface->query_info_finish) (file, res, error);
1225 * g_file_query_filesystem_info:
1226 * @file: input #GFile
1227 * @attributes: an attribute query string
1228 * @cancellable: (allow-none): optional #GCancellable object,
1229 * %NULL to ignore
1230 * @error: a #GError
1232 * Similar to g_file_query_info(), but obtains information
1233 * about the filesystem the @file is on, rather than the file itself.
1234 * For instance the amount of space available and the type of
1235 * the filesystem.
1237 * The @attributes value is a string that specifies the attributes
1238 * that should be gathered. It is not an error if it's not possible
1239 * to read a particular requested attribute from a file - it just
1240 * won't be set. @attributes should be a comma-separated list of
1241 * attributes or attribute wildcards. The wildcard "*" means all
1242 * attributes, and a wildcard like "filesystem::*" means all attributes
1243 * in the filesystem namespace. The standard namespace for filesystem
1244 * attributes is "filesystem". Common attributes of interest are
1245 * #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE (the total size of the filesystem
1246 * in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of bytes available),
1247 * and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1249 * If @cancellable is not %NULL, then the operation can be cancelled
1250 * by triggering the cancellable object from another thread. If the
1251 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1252 * returned.
1254 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1255 * be returned. Other errors are possible too, and depend on what
1256 * kind of filesystem the file is on.
1258 * Returns: (transfer full): a #GFileInfo or %NULL if there was an error.
1259 * Free the returned object with g_object_unref().
1261 GFileInfo *
1262 g_file_query_filesystem_info (GFile *file,
1263 const char *attributes,
1264 GCancellable *cancellable,
1265 GError **error)
1267 GFileIface *iface;
1269 g_return_val_if_fail (G_IS_FILE (file), NULL);
1271 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1272 return NULL;
1274 iface = G_FILE_GET_IFACE (file);
1276 if (iface->query_filesystem_info == NULL)
1278 g_set_error_literal (error, G_IO_ERROR,
1279 G_IO_ERROR_NOT_SUPPORTED,
1280 _("Operation not supported"));
1281 return NULL;
1284 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1288 * g_file_query_filesystem_info_async:
1289 * @file: input #GFile
1290 * @attributes: an attribute query string
1291 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1292 * of the request
1293 * @cancellable: (allow-none): optional #GCancellable object,
1294 * %NULL to ignore
1295 * @callback: (scope async): a #GAsyncReadyCallback to call
1296 * when the request is satisfied
1297 * @user_data: (closure): the data to pass to callback function
1299 * Asynchronously gets the requested information about the filesystem
1300 * that the specified @file is on. The result is a #GFileInfo object
1301 * that contains key-value attributes (such as type or size for the
1302 * file).
1304 * For more details, see g_file_query_filesystem_info() which is the
1305 * synchronous version of this call.
1307 * When the operation is finished, @callback will be called. You can
1308 * then call g_file_query_info_finish() to get the result of the
1309 * operation.
1311 void
1312 g_file_query_filesystem_info_async (GFile *file,
1313 const char *attributes,
1314 int io_priority,
1315 GCancellable *cancellable,
1316 GAsyncReadyCallback callback,
1317 gpointer user_data)
1319 GFileIface *iface;
1321 g_return_if_fail (G_IS_FILE (file));
1323 iface = G_FILE_GET_IFACE (file);
1324 (* iface->query_filesystem_info_async) (file,
1325 attributes,
1326 io_priority,
1327 cancellable,
1328 callback,
1329 user_data);
1333 * g_file_query_filesystem_info_finish:
1334 * @file: input #GFile
1335 * @res: a #GAsyncResult
1336 * @error: a #GError
1338 * Finishes an asynchronous filesystem info query.
1339 * See g_file_query_filesystem_info_async().
1341 * Returns: (transfer full): #GFileInfo for given @file
1342 * or %NULL on error.
1343 * Free the returned object with g_object_unref().
1345 GFileInfo *
1346 g_file_query_filesystem_info_finish (GFile *file,
1347 GAsyncResult *res,
1348 GError **error)
1350 GFileIface *iface;
1352 g_return_val_if_fail (G_IS_FILE (file), NULL);
1353 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1355 if (g_async_result_legacy_propagate_error (res, error))
1356 return NULL;
1358 iface = G_FILE_GET_IFACE (file);
1359 return (* iface->query_filesystem_info_finish) (file, res, error);
1363 * g_file_find_enclosing_mount:
1364 * @file: input #GFile
1365 * @cancellable: (allow-none): optional #GCancellable object,
1366 * %NULL to ignore
1367 * @error: a #GError
1369 * Gets a #GMount for the #GFile.
1371 * If the #GFileIface for @file does not have a mount (e.g.
1372 * possibly a remote share), @error will be set to %G_IO_ERROR_NOT_FOUND
1373 * and %NULL will be returned.
1375 * If @cancellable is not %NULL, then the operation can be cancelled by
1376 * triggering the cancellable object from another thread. If the operation
1377 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1379 * Returns: (transfer full): a #GMount where the @file is located
1380 * or %NULL on error.
1381 * Free the returned object with g_object_unref().
1383 GMount *
1384 g_file_find_enclosing_mount (GFile *file,
1385 GCancellable *cancellable,
1386 GError **error)
1388 GFileIface *iface;
1390 g_return_val_if_fail (G_IS_FILE (file), NULL);
1392 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1393 return NULL;
1395 iface = G_FILE_GET_IFACE (file);
1396 if (iface->find_enclosing_mount == NULL)
1399 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1400 /* Translators: This is an error message when
1401 * trying to find the enclosing (user visible)
1402 * mount of a file, but none exists.
1404 _("Containing mount does not exist"));
1405 return NULL;
1408 return (* iface->find_enclosing_mount) (file, cancellable, error);
1412 * g_file_find_enclosing_mount_async:
1413 * @file: a #GFile
1414 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1415 * of the request
1416 * @cancellable: (allow-none): optional #GCancellable object,
1417 * %NULL to ignore
1418 * @callback: (scope async): a #GAsyncReadyCallback to call
1419 * when the request is satisfied
1420 * @user_data: (closure): the data to pass to callback function
1422 * Asynchronously gets the mount for the file.
1424 * For more details, see g_file_find_enclosing_mount() which is
1425 * the synchronous version of this call.
1427 * When the operation is finished, @callback will be called.
1428 * You can then call g_file_find_enclosing_mount_finish() to
1429 * get the result of the operation.
1431 void
1432 g_file_find_enclosing_mount_async (GFile *file,
1433 int io_priority,
1434 GCancellable *cancellable,
1435 GAsyncReadyCallback callback,
1436 gpointer user_data)
1438 GFileIface *iface;
1440 g_return_if_fail (G_IS_FILE (file));
1442 iface = G_FILE_GET_IFACE (file);
1443 (* iface->find_enclosing_mount_async) (file,
1444 io_priority,
1445 cancellable,
1446 callback,
1447 user_data);
1451 * g_file_find_enclosing_mount_finish:
1452 * @file: a #GFile
1453 * @res: a #GAsyncResult
1454 * @error: a #GError
1456 * Finishes an asynchronous find mount request.
1457 * See g_file_find_enclosing_mount_async().
1459 * Returns: (transfer full): #GMount for given @file or %NULL on error.
1460 * Free the returned object with g_object_unref().
1462 GMount *
1463 g_file_find_enclosing_mount_finish (GFile *file,
1464 GAsyncResult *res,
1465 GError **error)
1467 GFileIface *iface;
1469 g_return_val_if_fail (G_IS_FILE (file), NULL);
1470 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1472 if (g_async_result_legacy_propagate_error (res, error))
1473 return NULL;
1475 iface = G_FILE_GET_IFACE (file);
1476 return (* iface->find_enclosing_mount_finish) (file, res, error);
1481 * g_file_read:
1482 * @file: #GFile to read
1483 * @cancellable: (allow-none): a #GCancellable
1484 * @error: a #GError, or %NULL
1486 * Opens a file for reading. The result is a #GFileInputStream that
1487 * can be used to read the contents of the file.
1489 * If @cancellable is not %NULL, then the operation can be cancelled by
1490 * triggering the cancellable object from another thread. If the operation
1491 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1493 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will be
1494 * returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1495 * error will be returned. Other errors are possible too, and depend
1496 * on what kind of filesystem the file is on.
1498 * Virtual: read_fn
1499 * Returns: (transfer full): #GFileInputStream or %NULL on error.
1500 * Free the returned object with g_object_unref().
1502 GFileInputStream *
1503 g_file_read (GFile *file,
1504 GCancellable *cancellable,
1505 GError **error)
1507 GFileIface *iface;
1509 g_return_val_if_fail (G_IS_FILE (file), NULL);
1511 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1512 return NULL;
1514 iface = G_FILE_GET_IFACE (file);
1516 if (iface->read_fn == NULL)
1518 g_set_error_literal (error, G_IO_ERROR,
1519 G_IO_ERROR_NOT_SUPPORTED,
1520 _("Operation not supported"));
1521 return NULL;
1524 return (* iface->read_fn) (file, cancellable, error);
1528 * g_file_append_to:
1529 * @file: input #GFile
1530 * @flags: a set of #GFileCreateFlags
1531 * @cancellable: (allow-none): optional #GCancellable object,
1532 * %NULL to ignore
1533 * @error: a #GError, or %NULL
1535 * Gets an output stream for appending data to the file.
1536 * If the file doesn't already exist it is created.
1538 * By default files created are generally readable by everyone,
1539 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1540 * will be made readable only to the current user, to the level that
1541 * is supported on the target filesystem.
1543 * If @cancellable is not %NULL, then the operation can be cancelled
1544 * by triggering the cancellable object from another thread. If the
1545 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1546 * returned.
1548 * Some file systems don't allow all file names, and may return an
1549 * %G_IO_ERROR_INVALID_FILENAME error. If the file is a directory the
1550 * %G_IO_ERROR_IS_DIRECTORY error will be returned. Other errors are
1551 * possible too, and depend on what kind of filesystem the file is on.
1553 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
1554 * Free the returned object with g_object_unref().
1556 GFileOutputStream *
1557 g_file_append_to (GFile *file,
1558 GFileCreateFlags flags,
1559 GCancellable *cancellable,
1560 GError **error)
1562 GFileIface *iface;
1564 g_return_val_if_fail (G_IS_FILE (file), NULL);
1566 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1567 return NULL;
1569 iface = G_FILE_GET_IFACE (file);
1571 if (iface->append_to == NULL)
1573 g_set_error_literal (error, G_IO_ERROR,
1574 G_IO_ERROR_NOT_SUPPORTED,
1575 _("Operation not supported"));
1576 return NULL;
1579 return (* iface->append_to) (file, flags, cancellable, error);
1583 * g_file_create:
1584 * @file: input #GFile
1585 * @flags: a set of #GFileCreateFlags
1586 * @cancellable: (allow-none): optional #GCancellable object,
1587 * %NULL to ignore
1588 * @error: a #GError, or %NULL
1590 * Creates a new file and returns an output stream for writing to it.
1591 * The file must not already exist.
1593 * By default files created are generally readable by everyone,
1594 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1595 * will be made readable only to the current user, to the level
1596 * that is supported on the target filesystem.
1598 * If @cancellable is not %NULL, then the operation can be cancelled
1599 * by triggering the cancellable object from another thread. If the
1600 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1601 * returned.
1603 * If a file or directory with this name already exists the
1604 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1605 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1606 * error, and if the name is to long %G_IO_ERROR_FILENAME_TOO_LONG will
1607 * be returned. Other errors are possible too, and depend on what kind
1608 * of filesystem the file is on.
1610 * Returns: (transfer full): a #GFileOutputStream for the newly created
1611 * file, or %NULL on error.
1612 * Free the returned object with g_object_unref().
1614 GFileOutputStream *
1615 g_file_create (GFile *file,
1616 GFileCreateFlags flags,
1617 GCancellable *cancellable,
1618 GError **error)
1620 GFileIface *iface;
1622 g_return_val_if_fail (G_IS_FILE (file), NULL);
1624 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1625 return NULL;
1627 iface = G_FILE_GET_IFACE (file);
1629 if (iface->create == NULL)
1631 g_set_error_literal (error, G_IO_ERROR,
1632 G_IO_ERROR_NOT_SUPPORTED,
1633 _("Operation not supported"));
1634 return NULL;
1637 return (* iface->create) (file, flags, cancellable, error);
1641 * g_file_replace:
1642 * @file: input #GFile
1643 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1644 * for the current #GFile, or #NULL to ignore
1645 * @make_backup: %TRUE if a backup should be created
1646 * @flags: a set of #GFileCreateFlags
1647 * @cancellable: (allow-none): optional #GCancellable object,
1648 * %NULL to ignore
1649 * @error: a #GError, or %NULL
1651 * Returns an output stream for overwriting the file, possibly
1652 * creating a backup copy of the file first. If the file doesn't exist,
1653 * it will be created.
1655 * This will try to replace the file in the safest way possible so
1656 * that any errors during the writing will not affect an already
1657 * existing copy of the file. For instance, for local files it
1658 * may write to a temporary file and then atomically rename over
1659 * the destination when the stream is closed.
1661 * By default files created are generally readable by everyone,
1662 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1663 * will be made readable only to the current user, to the level that
1664 * is supported on the target filesystem.
1666 * If @cancellable is not %NULL, then the operation can be cancelled
1667 * by triggering the cancellable object from another thread. If the
1668 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1669 * returned.
1671 * If you pass in a non-%NULL @etag value, then this value is
1672 * compared to the current entity tag of the file, and if they differ
1673 * an %G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1674 * that the file has been changed since you last read it. You can get
1675 * the new etag from g_file_output_stream_get_etag() after you've
1676 * finished writing and closed the #GFileOutputStream. When you load
1677 * a new file you can use g_file_input_stream_query_info() to get
1678 * the etag of the file.
1680 * If @make_backup is %TRUE, this function will attempt to make a
1681 * backup of the current file before overwriting it. If this fails
1682 * a %G_IO_ERROR_CANT_CREATE_BACKUP error will be returned. If you
1683 * want to replace anyway, try again with @make_backup set to %FALSE.
1685 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will
1686 * be returned, and if the file is some other form of non-regular file
1687 * then a %G_IO_ERROR_NOT_REGULAR_FILE error will be returned. Some
1688 * file systems don't allow all file names, and may return an
1689 * %G_IO_ERROR_INVALID_FILENAME error, and if the name is to long
1690 * %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors are
1691 * possible too, and depend on what kind of filesystem the file is on.
1693 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
1694 * Free the returned object with g_object_unref().
1696 GFileOutputStream *
1697 g_file_replace (GFile *file,
1698 const char *etag,
1699 gboolean make_backup,
1700 GFileCreateFlags flags,
1701 GCancellable *cancellable,
1702 GError **error)
1704 GFileIface *iface;
1706 g_return_val_if_fail (G_IS_FILE (file), NULL);
1708 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1709 return NULL;
1711 iface = G_FILE_GET_IFACE (file);
1713 if (iface->replace == NULL)
1715 g_set_error_literal (error, G_IO_ERROR,
1716 G_IO_ERROR_NOT_SUPPORTED,
1717 _("Operation not supported"));
1718 return NULL;
1721 /* Handle empty tag string as NULL in consistent way. */
1722 if (etag && *etag == 0)
1723 etag = NULL;
1725 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1729 * g_file_open_readwrite:
1730 * @file: #GFile to open
1731 * @cancellable: (allow-none): a #GCancellable
1732 * @error: a #GError, or %NULL
1734 * Opens an existing file for reading and writing. The result is
1735 * a #GFileIOStream that can be used to read and write the contents
1736 * of the file.
1738 * If @cancellable is not %NULL, then the operation can be cancelled
1739 * by triggering the cancellable object from another thread. If the
1740 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1741 * returned.
1743 * If the file does not exist, the %G_IO_ERROR_NOT_FOUND error will
1744 * be returned. If the file is a directory, the %G_IO_ERROR_IS_DIRECTORY
1745 * error will be returned. Other errors are possible too, and depend on
1746 * what kind of filesystem the file is on. Note that in many non-local
1747 * file cases read and write streams are not supported, so make sure you
1748 * really need to do read and write streaming, rather than just opening
1749 * for reading or writing.
1751 * Returns: (transfer full): #GFileIOStream or %NULL on error.
1752 * Free the returned object with g_object_unref().
1754 * Since: 2.22
1756 GFileIOStream *
1757 g_file_open_readwrite (GFile *file,
1758 GCancellable *cancellable,
1759 GError **error)
1761 GFileIface *iface;
1763 g_return_val_if_fail (G_IS_FILE (file), NULL);
1765 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1766 return NULL;
1768 iface = G_FILE_GET_IFACE (file);
1770 if (iface->open_readwrite == NULL)
1772 g_set_error_literal (error, G_IO_ERROR,
1773 G_IO_ERROR_NOT_SUPPORTED,
1774 _("Operation not supported"));
1775 return NULL;
1778 return (* iface->open_readwrite) (file, cancellable, error);
1782 * g_file_create_readwrite:
1783 * @file: a #GFile
1784 * @flags: a set of #GFileCreateFlags
1785 * @cancellable: (allow-none): optional #GCancellable object,
1786 * %NULL to ignore
1787 * @error: return location for a #GError, or %NULL
1789 * Creates a new file and returns a stream for reading and
1790 * writing to it. The file must not already exist.
1792 * By default files created are generally readable by everyone,
1793 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1794 * will be made readable only to the current user, to the level
1795 * that is supported on the target filesystem.
1797 * If @cancellable is not %NULL, then the operation can be cancelled
1798 * by triggering the cancellable object from another thread. If the
1799 * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be
1800 * returned.
1802 * If a file or directory with this name already exists, the
1803 * %G_IO_ERROR_EXISTS error will be returned. Some file systems don't
1804 * allow all file names, and may return an %G_IO_ERROR_INVALID_FILENAME
1805 * error, and if the name is too long, %G_IO_ERROR_FILENAME_TOO_LONG
1806 * will be returned. Other errors are possible too, and depend on what
1807 * kind of filesystem the file is on.
1809 * Note that in many non-local file cases read and write streams are
1810 * not supported, so make sure you really need to do read and write
1811 * streaming, rather than just opening for reading or writing.
1813 * Returns: (transfer full): a #GFileIOStream for the newly created
1814 * file, or %NULL on error.
1815 * Free the returned object with g_object_unref().
1817 * Since: 2.22
1819 GFileIOStream *
1820 g_file_create_readwrite (GFile *file,
1821 GFileCreateFlags flags,
1822 GCancellable *cancellable,
1823 GError **error)
1825 GFileIface *iface;
1827 g_return_val_if_fail (G_IS_FILE (file), NULL);
1829 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1830 return NULL;
1832 iface = G_FILE_GET_IFACE (file);
1834 if (iface->create_readwrite == NULL)
1836 g_set_error_literal (error, G_IO_ERROR,
1837 G_IO_ERROR_NOT_SUPPORTED,
1838 _("Operation not supported"));
1839 return NULL;
1842 return (* iface->create_readwrite) (file, flags, cancellable, error);
1846 * g_file_replace_readwrite:
1847 * @file: a #GFile
1848 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link>
1849 * for the current #GFile, or #NULL to ignore
1850 * @make_backup: %TRUE if a backup should be created
1851 * @flags: a set of #GFileCreateFlags
1852 * @cancellable: (allow-none): optional #GCancellable object,
1853 * %NULL to ignore
1854 * @error: return location for a #GError, or %NULL
1856 * Returns an output stream for overwriting the file in readwrite mode,
1857 * possibly creating a backup copy of the file first. If the file doesn't
1858 * exist, it will be created.
1860 * For details about the behaviour, see g_file_replace() which does the
1861 * same thing but returns an output stream only.
1863 * Note that in many non-local file cases read and write streams are not
1864 * supported, so make sure you really need to do read and write streaming,
1865 * rather than just opening for reading or writing.
1867 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
1868 * Free the returned object with g_object_unref().
1870 * Since: 2.22
1872 GFileIOStream *
1873 g_file_replace_readwrite (GFile *file,
1874 const char *etag,
1875 gboolean make_backup,
1876 GFileCreateFlags flags,
1877 GCancellable *cancellable,
1878 GError **error)
1880 GFileIface *iface;
1882 g_return_val_if_fail (G_IS_FILE (file), NULL);
1884 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1885 return NULL;
1887 iface = G_FILE_GET_IFACE (file);
1889 if (iface->replace_readwrite == NULL)
1891 g_set_error_literal (error, G_IO_ERROR,
1892 G_IO_ERROR_NOT_SUPPORTED,
1893 _("Operation not supported"));
1894 return NULL;
1897 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1901 * g_file_read_async:
1902 * @file: input #GFile
1903 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1904 * of the request
1905 * @cancellable: (allow-none): optional #GCancellable object,
1906 * %NULL to ignore
1907 * @callback: (scope async): a #GAsyncReadyCallback to call
1908 * when the request is satisfied
1909 * @user_data: (closure): the data to pass to callback function
1911 * Asynchronously opens @file for reading.
1913 * For more details, see g_file_read() which is
1914 * the synchronous version of this call.
1916 * When the operation is finished, @callback will be called.
1917 * You can then call g_file_read_finish() to get the result
1918 * of the operation.
1920 void
1921 g_file_read_async (GFile *file,
1922 int io_priority,
1923 GCancellable *cancellable,
1924 GAsyncReadyCallback callback,
1925 gpointer user_data)
1927 GFileIface *iface;
1929 g_return_if_fail (G_IS_FILE (file));
1931 iface = G_FILE_GET_IFACE (file);
1932 (* iface->read_async) (file,
1933 io_priority,
1934 cancellable,
1935 callback,
1936 user_data);
1940 * g_file_read_finish:
1941 * @file: input #GFile
1942 * @res: a #GAsyncResult
1943 * @error: a #GError, or %NULL
1945 * Finishes an asynchronous file read operation started with
1946 * g_file_read_async().
1948 * Returns: (transfer full): a #GFileInputStream or %NULL on error.
1949 * Free the returned object with g_object_unref().
1951 GFileInputStream *
1952 g_file_read_finish (GFile *file,
1953 GAsyncResult *res,
1954 GError **error)
1956 GFileIface *iface;
1958 g_return_val_if_fail (G_IS_FILE (file), NULL);
1959 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1961 if (g_async_result_legacy_propagate_error (res, error))
1962 return NULL;
1964 iface = G_FILE_GET_IFACE (file);
1965 return (* iface->read_finish) (file, res, error);
1969 * g_file_append_to_async:
1970 * @file: input #GFile
1971 * @flags: a set of #GFileCreateFlags
1972 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1973 * of the request
1974 * @cancellable: (allow-none): optional #GCancellable object,
1975 * %NULL to ignore
1976 * @callback: (scope async): a #GAsyncReadyCallback to call
1977 * when the request is satisfied
1978 * @user_data: (closure): the data to pass to callback function
1980 * Asynchronously opens @file for appending.
1982 * For more details, see g_file_append_to() which is
1983 * the synchronous version of this call.
1985 * When the operation is finished, @callback will be called.
1986 * You can then call g_file_append_to_finish() to get the result
1987 * of the operation.
1989 void
1990 g_file_append_to_async (GFile *file,
1991 GFileCreateFlags flags,
1992 int io_priority,
1993 GCancellable *cancellable,
1994 GAsyncReadyCallback callback,
1995 gpointer user_data)
1997 GFileIface *iface;
1999 g_return_if_fail (G_IS_FILE (file));
2001 iface = G_FILE_GET_IFACE (file);
2002 (* iface->append_to_async) (file,
2003 flags,
2004 io_priority,
2005 cancellable,
2006 callback,
2007 user_data);
2011 * g_file_append_to_finish:
2012 * @file: input #GFile
2013 * @res: #GAsyncResult
2014 * @error: a #GError, or %NULL
2016 * Finishes an asynchronous file append operation started with
2017 * g_file_append_to_async().
2019 * Returns: (transfer full): a valid #GFileOutputStream
2020 * or %NULL on error.
2021 * Free the returned object with g_object_unref().
2023 GFileOutputStream *
2024 g_file_append_to_finish (GFile *file,
2025 GAsyncResult *res,
2026 GError **error)
2028 GFileIface *iface;
2030 g_return_val_if_fail (G_IS_FILE (file), NULL);
2031 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2033 if (g_async_result_legacy_propagate_error (res, error))
2034 return NULL;
2036 iface = G_FILE_GET_IFACE (file);
2037 return (* iface->append_to_finish) (file, res, error);
2041 * g_file_create_async:
2042 * @file: input #GFile
2043 * @flags: a set of #GFileCreateFlags
2044 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2045 * of the request
2046 * @cancellable: (allow-none): optional #GCancellable object,
2047 * %NULL to ignore
2048 * @callback: (scope async): a #GAsyncReadyCallback to call
2049 * when the request is satisfied
2050 * @user_data: (closure): the data to pass to callback function
2052 * Asynchronously creates a new file and returns an output stream
2053 * for writing to it. The file must not already exist.
2055 * For more details, see g_file_create() which is
2056 * the synchronous version of this call.
2058 * When the operation is finished, @callback will be called.
2059 * You can then call g_file_create_finish() to get the result
2060 * of the operation.
2062 void
2063 g_file_create_async (GFile *file,
2064 GFileCreateFlags flags,
2065 int io_priority,
2066 GCancellable *cancellable,
2067 GAsyncReadyCallback callback,
2068 gpointer user_data)
2070 GFileIface *iface;
2072 g_return_if_fail (G_IS_FILE (file));
2074 iface = G_FILE_GET_IFACE (file);
2075 (* iface->create_async) (file,
2076 flags,
2077 io_priority,
2078 cancellable,
2079 callback,
2080 user_data);
2084 * g_file_create_finish:
2085 * @file: input #GFile
2086 * @res: a #GAsyncResult
2087 * @error: a #GError, or %NULL
2089 * Finishes an asynchronous file create operation started with
2090 * g_file_create_async().
2092 * Returns: (transfer full): a #GFileOutputStream or %NULL on error.
2093 * Free the returned object with g_object_unref().
2095 GFileOutputStream *
2096 g_file_create_finish (GFile *file,
2097 GAsyncResult *res,
2098 GError **error)
2100 GFileIface *iface;
2102 g_return_val_if_fail (G_IS_FILE (file), NULL);
2103 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2105 if (g_async_result_legacy_propagate_error (res, error))
2106 return NULL;
2108 iface = G_FILE_GET_IFACE (file);
2109 return (* iface->create_finish) (file, res, error);
2113 * g_file_replace_async:
2114 * @file: input #GFile
2115 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2116 * for the current #GFile, or NULL to ignore
2117 * @make_backup: %TRUE if a backup should be created
2118 * @flags: a set of #GFileCreateFlags
2119 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2120 * of the request
2121 * @cancellable: (allow-none): optional #GCancellable object,
2122 * %NULL to ignore
2123 * @callback: (scope async): a #GAsyncReadyCallback to call
2124 * when the request is satisfied
2125 * @user_data: (closure): the data to pass to callback function
2127 * Asynchronously overwrites the file, replacing the contents,
2128 * possibly creating a backup copy of the file first.
2130 * For more details, see g_file_replace() which is
2131 * the synchronous version of this call.
2133 * When the operation is finished, @callback will be called.
2134 * You can then call g_file_replace_finish() to get the result
2135 * of the operation.
2137 void
2138 g_file_replace_async (GFile *file,
2139 const char *etag,
2140 gboolean make_backup,
2141 GFileCreateFlags flags,
2142 int io_priority,
2143 GCancellable *cancellable,
2144 GAsyncReadyCallback callback,
2145 gpointer user_data)
2147 GFileIface *iface;
2149 g_return_if_fail (G_IS_FILE (file));
2151 iface = G_FILE_GET_IFACE (file);
2152 (* iface->replace_async) (file,
2153 etag,
2154 make_backup,
2155 flags,
2156 io_priority,
2157 cancellable,
2158 callback,
2159 user_data);
2163 * g_file_replace_finish:
2164 * @file: input #GFile
2165 * @res: a #GAsyncResult
2166 * @error: a #GError, or %NULL
2168 * Finishes an asynchronous file replace operation started with
2169 * g_file_replace_async().
2171 * Returns: (transfer full): a #GFileOutputStream, or %NULL on error.
2172 * Free the returned object with g_object_unref().
2174 GFileOutputStream *
2175 g_file_replace_finish (GFile *file,
2176 GAsyncResult *res,
2177 GError **error)
2179 GFileIface *iface;
2181 g_return_val_if_fail (G_IS_FILE (file), NULL);
2182 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2184 if (g_async_result_legacy_propagate_error (res, error))
2185 return NULL;
2187 iface = G_FILE_GET_IFACE (file);
2188 return (* iface->replace_finish) (file, res, error);
2192 * g_file_open_readwrite_async
2193 * @file: input #GFile
2194 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2195 * of the request
2196 * @cancellable: (allow-none): optional #GCancellable object,
2197 * %NULL to ignore
2198 * @callback: (scope async): a #GAsyncReadyCallback to call
2199 * when the request is satisfied
2200 * @user_data: (closure): the data to pass to callback function
2202 * Asynchronously opens @file for reading and writing.
2204 * For more details, see g_file_open_readwrite() which is
2205 * the synchronous version of this call.
2207 * When the operation is finished, @callback will be called.
2208 * You can then call g_file_open_readwrite_finish() to get
2209 * the result of the operation.
2211 * Since: 2.22
2213 void
2214 g_file_open_readwrite_async (GFile *file,
2215 int io_priority,
2216 GCancellable *cancellable,
2217 GAsyncReadyCallback callback,
2218 gpointer user_data)
2220 GFileIface *iface;
2222 g_return_if_fail (G_IS_FILE (file));
2224 iface = G_FILE_GET_IFACE (file);
2225 (* iface->open_readwrite_async) (file,
2226 io_priority,
2227 cancellable,
2228 callback,
2229 user_data);
2233 * g_file_open_readwrite_finish:
2234 * @file: input #GFile
2235 * @res: a #GAsyncResult
2236 * @error: a #GError, or %NULL
2238 * Finishes an asynchronous file read operation started with
2239 * g_file_open_readwrite_async().
2241 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2242 * Free the returned object with g_object_unref().
2244 * Since: 2.22
2246 GFileIOStream *
2247 g_file_open_readwrite_finish (GFile *file,
2248 GAsyncResult *res,
2249 GError **error)
2251 GFileIface *iface;
2253 g_return_val_if_fail (G_IS_FILE (file), NULL);
2254 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2256 if (g_async_result_legacy_propagate_error (res, error))
2257 return NULL;
2259 iface = G_FILE_GET_IFACE (file);
2260 return (* iface->open_readwrite_finish) (file, res, error);
2264 * g_file_create_readwrite_async:
2265 * @file: input #GFile
2266 * @flags: a set of #GFileCreateFlags
2267 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2268 * of the request
2269 * @cancellable: (allow-none): optional #GCancellable object,
2270 * %NULL to ignore
2271 * @callback: (scope async): a #GAsyncReadyCallback to call
2272 * when the request is satisfied
2273 * @user_data: (closure): the data to pass to callback function
2275 * Asynchronously creates a new file and returns a stream
2276 * for reading and writing to it. The file must not already exist.
2278 * For more details, see g_file_create_readwrite() which is
2279 * the synchronous version of this call.
2281 * When the operation is finished, @callback will be called.
2282 * You can then call g_file_create_readwrite_finish() to get
2283 * the result of the operation.
2285 * Since: 2.22
2287 void
2288 g_file_create_readwrite_async (GFile *file,
2289 GFileCreateFlags flags,
2290 int io_priority,
2291 GCancellable *cancellable,
2292 GAsyncReadyCallback callback,
2293 gpointer user_data)
2295 GFileIface *iface;
2297 g_return_if_fail (G_IS_FILE (file));
2299 iface = G_FILE_GET_IFACE (file);
2300 (* iface->create_readwrite_async) (file,
2301 flags,
2302 io_priority,
2303 cancellable,
2304 callback,
2305 user_data);
2309 * g_file_create_readwrite_finish:
2310 * @file: input #GFile
2311 * @res: a #GAsyncResult
2312 * @error: a #GError, or %NULL
2314 * Finishes an asynchronous file create operation started with
2315 * g_file_create_readwrite_async().
2317 * Returns: (transfer full): a #GFileIOStream or %NULL on error.
2318 * Free the returned object with g_object_unref().
2320 * Since: 2.22
2322 GFileIOStream *
2323 g_file_create_readwrite_finish (GFile *file,
2324 GAsyncResult *res,
2325 GError **error)
2327 GFileIface *iface;
2329 g_return_val_if_fail (G_IS_FILE (file), NULL);
2330 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2332 if (g_async_result_legacy_propagate_error (res, error))
2333 return NULL;
2335 iface = G_FILE_GET_IFACE (file);
2336 return (* iface->create_readwrite_finish) (file, res, error);
2340 * g_file_replace_readwrite_async:
2341 * @file: input #GFile
2342 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link>
2343 * for the current #GFile, or NULL to ignore
2344 * @make_backup: %TRUE if a backup should be created
2345 * @flags: a set of #GFileCreateFlags
2346 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2347 * of the request
2348 * @cancellable: (allow-none): optional #GCancellable object,
2349 * %NULL to ignore
2350 * @callback: (scope async): a #GAsyncReadyCallback to call
2351 * when the request is satisfied
2352 * @user_data: (closure): the data to pass to callback function
2354 * Asynchronously overwrites the file in read-write mode,
2355 * replacing the contents, possibly creating a backup copy
2356 * of the file first.
2358 * For more details, see g_file_replace_readwrite() which is
2359 * the synchronous version of this call.
2361 * When the operation is finished, @callback will be called.
2362 * You can then call g_file_replace_readwrite_finish() to get
2363 * the result of the operation.
2365 * Since: 2.22
2367 void
2368 g_file_replace_readwrite_async (GFile *file,
2369 const char *etag,
2370 gboolean make_backup,
2371 GFileCreateFlags flags,
2372 int io_priority,
2373 GCancellable *cancellable,
2374 GAsyncReadyCallback callback,
2375 gpointer user_data)
2377 GFileIface *iface;
2379 g_return_if_fail (G_IS_FILE (file));
2381 iface = G_FILE_GET_IFACE (file);
2382 (* iface->replace_readwrite_async) (file,
2383 etag,
2384 make_backup,
2385 flags,
2386 io_priority,
2387 cancellable,
2388 callback,
2389 user_data);
2393 * g_file_replace_readwrite_finish:
2394 * @file: input #GFile
2395 * @res: a #GAsyncResult
2396 * @error: a #GError, or %NULL
2398 * Finishes an asynchronous file replace operation started with
2399 * g_file_replace_readwrite_async().
2401 * Returns: (transfer full): a #GFileIOStream, or %NULL on error.
2402 * Free the returned object with g_object_unref().
2404 * Since: 2.22
2406 GFileIOStream *
2407 g_file_replace_readwrite_finish (GFile *file,
2408 GAsyncResult *res,
2409 GError **error)
2411 GFileIface *iface;
2413 g_return_val_if_fail (G_IS_FILE (file), NULL);
2414 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2416 if (g_async_result_legacy_propagate_error (res, error))
2417 return NULL;
2419 iface = G_FILE_GET_IFACE (file);
2420 return (* iface->replace_readwrite_finish) (file, res, error);
2423 static gboolean
2424 copy_symlink (GFile *destination,
2425 GFileCopyFlags flags,
2426 GCancellable *cancellable,
2427 const char *target,
2428 GError **error)
2430 GError *my_error;
2431 gboolean tried_delete;
2432 GFileInfo *info;
2433 GFileType file_type;
2435 tried_delete = FALSE;
2437 retry:
2438 my_error = NULL;
2439 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2441 /* Maybe it already existed, and we want to overwrite? */
2442 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2443 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2445 g_error_free (my_error);
2447 /* Don't overwrite if the destination is a directory */
2448 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2449 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2450 cancellable, &my_error);
2451 if (info != NULL)
2453 file_type = g_file_info_get_file_type (info);
2454 g_object_unref (info);
2456 if (file_type == G_FILE_TYPE_DIRECTORY)
2458 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2459 _("Can't copy over directory"));
2460 return FALSE;
2464 if (!g_file_delete (destination, cancellable, error))
2465 return FALSE;
2467 tried_delete = TRUE;
2468 goto retry;
2470 /* Nah, fail */
2471 g_propagate_error (error, my_error);
2472 return FALSE;
2475 return TRUE;
2478 static GInputStream *
2479 open_source_for_copy (GFile *source,
2480 GFile *destination,
2481 GFileCopyFlags flags,
2482 GCancellable *cancellable,
2483 GError **error)
2485 GError *my_error;
2486 GInputStream *in;
2487 GFileInfo *info;
2488 GFileType file_type;
2490 my_error = NULL;
2491 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2492 if (in != NULL)
2493 return in;
2495 /* There was an error opening the source, try to set a good error for it: */
2496 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2498 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2499 * as that is less useful to the app. Better check for errors on the
2500 * target instead.
2502 g_error_free (my_error);
2503 my_error = NULL;
2505 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2506 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2507 cancellable, &my_error);
2508 if (info != NULL &&
2509 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2511 file_type = g_file_info_get_file_type (info);
2512 g_object_unref (info);
2514 if (flags & G_FILE_COPY_OVERWRITE)
2516 if (file_type == G_FILE_TYPE_DIRECTORY)
2518 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2519 _("Can't copy directory over directory"));
2520 return NULL;
2522 /* continue to would_recurse error */
2524 else
2526 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2527 _("Target file exists"));
2528 return NULL;
2531 else
2533 /* Error getting info from target, return that error
2534 * (except for NOT_FOUND, which is no error here)
2536 g_clear_object (&info);
2537 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2539 g_propagate_error (error, my_error);
2540 return NULL;
2542 g_clear_error (&my_error);
2545 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2546 _("Can't recursively copy directory"));
2547 return NULL;
2550 g_propagate_error (error, my_error);
2551 return NULL;
2554 static gboolean
2555 should_copy (GFileAttributeInfo *info,
2556 gboolean as_move,
2557 gboolean skip_perms)
2559 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2560 return FALSE;
2562 if (as_move)
2563 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2564 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2567 static char *
2568 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2569 GFileAttributeInfoList *namespaces,
2570 gboolean as_move,
2571 gboolean skip_perms)
2573 GString *s;
2574 gboolean first;
2575 int i;
2577 first = TRUE;
2578 s = g_string_new ("");
2580 if (attributes)
2582 for (i = 0; i < attributes->n_infos; i++)
2584 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2586 if (first)
2587 first = FALSE;
2588 else
2589 g_string_append_c (s, ',');
2591 g_string_append (s, attributes->infos[i].name);
2596 if (namespaces)
2598 for (i = 0; i < namespaces->n_infos; i++)
2600 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2602 if (first)
2603 first = FALSE;
2604 else
2605 g_string_append_c (s, ',');
2607 g_string_append (s, namespaces->infos[i].name);
2608 g_string_append (s, "::*");
2613 return g_string_free (s, FALSE);
2617 * g_file_copy_attributes:
2618 * @source: a #GFile with attributes
2619 * @destination: a #GFile to copy attributes to
2620 * @flags: a set of #GFileCopyFlags
2621 * @cancellable: (allow-none): optional #GCancellable object,
2622 * %NULL to ignore
2623 * @error: a #GError, %NULL to ignore
2625 * Copies the file attributes from @source to @destination.
2627 * Normally only a subset of the file attributes are copied,
2628 * those that are copies in a normal file copy operation
2629 * (which for instance does not include e.g. owner). However
2630 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2631 * all the metadata that is possible to copy is copied. This
2632 * is useful when implementing move by copy + delete source.
2634 * Returns: %TRUE if the attributes were copied successfully,
2635 * %FALSE otherwise.
2637 gboolean
2638 g_file_copy_attributes (GFile *source,
2639 GFile *destination,
2640 GFileCopyFlags flags,
2641 GCancellable *cancellable,
2642 GError **error)
2644 GFileAttributeInfoList *attributes, *namespaces;
2645 char *attrs_to_read;
2646 gboolean res;
2647 GFileInfo *info;
2648 gboolean as_move;
2649 gboolean source_nofollow_symlinks;
2650 gboolean skip_perms;
2652 as_move = flags & G_FILE_COPY_ALL_METADATA;
2653 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2654 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2656 /* Ignore errors here, if the target supports no attributes there is
2657 * nothing to copy
2659 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2660 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2662 if (attributes == NULL && namespaces == NULL)
2663 return TRUE;
2665 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2667 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2668 * we just don't copy it.
2670 info = g_file_query_info (source, attrs_to_read,
2671 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2672 cancellable,
2673 NULL);
2675 g_free (attrs_to_read);
2677 res = TRUE;
2678 if (info)
2680 res = g_file_set_attributes_from_info (destination,
2681 info,
2682 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2683 cancellable,
2684 error);
2685 g_object_unref (info);
2688 g_file_attribute_info_list_unref (attributes);
2689 g_file_attribute_info_list_unref (namespaces);
2691 return res;
2694 static gboolean
2695 copy_stream_with_progress (GInputStream *in,
2696 GOutputStream *out,
2697 GFile *source,
2698 GCancellable *cancellable,
2699 GFileProgressCallback progress_callback,
2700 gpointer progress_callback_data,
2701 GError **error)
2703 gssize n_read, n_written;
2704 goffset current_size;
2705 char buffer[1024*64], *p;
2706 gboolean res;
2707 goffset total_size;
2708 GFileInfo *info;
2710 total_size = -1;
2711 /* avoid performance impact of querying total size when it's not needed */
2712 if (progress_callback)
2714 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2715 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2716 cancellable, NULL);
2717 if (info)
2719 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2720 total_size = g_file_info_get_size (info);
2721 g_object_unref (info);
2724 if (total_size == -1)
2726 info = g_file_query_info (source,
2727 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2728 G_FILE_QUERY_INFO_NONE,
2729 cancellable, NULL);
2730 if (info)
2732 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2733 total_size = g_file_info_get_size (info);
2734 g_object_unref (info);
2739 if (total_size == -1)
2740 total_size = 0;
2742 current_size = 0;
2743 res = TRUE;
2744 while (TRUE)
2746 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2747 if (n_read == -1)
2749 res = FALSE;
2750 break;
2753 if (n_read == 0)
2754 break;
2756 current_size += n_read;
2758 p = buffer;
2759 while (n_read > 0)
2761 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2762 if (n_written == -1)
2764 res = FALSE;
2765 break;
2768 p += n_written;
2769 n_read -= n_written;
2772 if (!res)
2773 break;
2775 if (progress_callback)
2776 progress_callback (current_size, total_size, progress_callback_data);
2779 /* Make sure we send full copied size */
2780 if (progress_callback)
2781 progress_callback (current_size, total_size, progress_callback_data);
2783 return res;
2786 #ifdef HAVE_SPLICE
2788 static gboolean
2789 do_splice (int fd_in,
2790 loff_t *off_in,
2791 int fd_out,
2792 loff_t *off_out,
2793 size_t len,
2794 long *bytes_transferd,
2795 GError **error)
2797 long result;
2799 retry:
2800 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2802 if (result == -1)
2804 int errsv = errno;
2806 if (errsv == EINTR)
2807 goto retry;
2808 else if (errsv == ENOSYS || errsv == EINVAL)
2809 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2810 _("Splice not supported"));
2811 else
2812 g_set_error (error, G_IO_ERROR,
2813 g_io_error_from_errno (errsv),
2814 _("Error splicing file: %s"),
2815 g_strerror (errsv));
2817 return FALSE;
2820 *bytes_transferd = result;
2821 return TRUE;
2824 static gboolean
2825 splice_stream_with_progress (GInputStream *in,
2826 GOutputStream *out,
2827 GCancellable *cancellable,
2828 GFileProgressCallback progress_callback,
2829 gpointer progress_callback_data,
2830 GError **error)
2832 int buffer[2];
2833 gboolean res;
2834 goffset total_size;
2835 loff_t offset_in;
2836 loff_t offset_out;
2837 int fd_in, fd_out;
2839 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2840 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2842 if (pipe (buffer) != 0)
2844 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2845 "Pipe creation failed");
2846 return FALSE;
2849 total_size = -1;
2850 /* avoid performance impact of querying total size when it's not needed */
2851 if (progress_callback)
2853 struct stat sbuf;
2855 if (fstat (fd_in, &sbuf) == 0)
2856 total_size = sbuf.st_size;
2859 if (total_size == -1)
2860 total_size = 0;
2862 offset_in = offset_out = 0;
2863 res = FALSE;
2864 while (TRUE)
2866 long n_read;
2867 long n_written;
2869 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2870 break;
2872 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2873 break;
2875 if (n_read == 0)
2877 res = TRUE;
2878 break;
2881 while (n_read > 0)
2883 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2884 goto out;
2886 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2887 goto out;
2889 n_read -= n_written;
2892 if (progress_callback)
2893 progress_callback (offset_in, total_size, progress_callback_data);
2896 /* Make sure we send full copied size */
2897 if (progress_callback)
2898 progress_callback (offset_in, total_size, progress_callback_data);
2900 out:
2901 close (buffer[0]);
2902 close (buffer[1]);
2904 return res;
2906 #endif
2908 static gboolean
2909 file_copy_fallback (GFile *source,
2910 GFile *destination,
2911 GFileCopyFlags flags,
2912 GCancellable *cancellable,
2913 GFileProgressCallback progress_callback,
2914 gpointer progress_callback_data,
2915 GError **error)
2917 GInputStream *in;
2918 GOutputStream *out;
2919 GFileInfo *info;
2920 const char *target;
2921 gboolean result;
2922 #ifdef HAVE_SPLICE
2923 gboolean fallback = TRUE;
2924 #endif
2926 /* need to know the file type */
2927 info = g_file_query_info (source,
2928 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2929 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2930 cancellable,
2931 error);
2933 if (info == NULL)
2934 return FALSE;
2936 /* Maybe copy the symlink? */
2937 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2938 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2940 target = g_file_info_get_symlink_target (info);
2941 if (target)
2943 if (!copy_symlink (destination, flags, cancellable, target, error))
2945 g_object_unref (info);
2946 return FALSE;
2949 g_object_unref (info);
2950 goto copied_file;
2952 /* ... else fall back on a regular file copy */
2953 g_object_unref (info);
2955 /* Handle "special" files (pipes, device nodes, ...)? */
2956 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2958 /* FIXME: could try to recreate device nodes and others? */
2959 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2960 _("Can't copy special file"));
2961 g_object_unref (info);
2962 return FALSE;
2964 /* Everything else should just fall back on a regular copy. */
2965 else
2966 g_object_unref (info);
2968 in = open_source_for_copy (source, destination, flags, cancellable, error);
2969 if (in == NULL)
2970 return FALSE;
2972 if (flags & G_FILE_COPY_OVERWRITE)
2974 out = (GOutputStream *)g_file_replace (destination,
2975 NULL,
2976 flags & G_FILE_COPY_BACKUP,
2977 G_FILE_CREATE_REPLACE_DESTINATION,
2978 cancellable, error);
2980 else
2982 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2985 if (out == NULL)
2987 g_object_unref (in);
2988 return FALSE;
2991 #ifdef HAVE_SPLICE
2992 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2994 GError *splice_err = NULL;
2996 result = splice_stream_with_progress (in, out, cancellable,
2997 progress_callback, progress_callback_data,
2998 &splice_err);
3000 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
3002 fallback = FALSE;
3003 if (!result)
3004 g_propagate_error (error, splice_err);
3006 else
3007 g_clear_error (&splice_err);
3010 if (fallback)
3011 #endif
3012 result = copy_stream_with_progress (in, out, source, cancellable,
3013 progress_callback, progress_callback_data,
3014 error);
3016 /* Don't care about errors in source here */
3017 g_input_stream_close (in, cancellable, NULL);
3019 /* But write errors on close are bad! */
3020 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
3021 result = FALSE;
3023 g_object_unref (in);
3024 g_object_unref (out);
3026 if (result == FALSE)
3027 return FALSE;
3029 copied_file:
3030 /* Ignore errors here. Failure to copy metadata is not a hard error */
3031 g_file_copy_attributes (source, destination,
3032 flags, cancellable, NULL);
3034 return TRUE;
3038 * g_file_copy:
3039 * @source: input #GFile
3040 * @destination: destination #GFile
3041 * @flags: set of #GFileCopyFlags
3042 * @cancellable: (allow-none): optional #GCancellable object,
3043 * %NULL to ignore
3044 * @progress_callback: (allow-none) (scope call): function to callback with
3045 * progress information, or %NULL if progress information is not needed
3046 * @progress_callback_data: (closure): user data to pass to @progress_callback
3047 * @error: #GError to set on error, or %NULL
3049 * Copies the file @source to the location specified by @destination.
3050 * Can not handle recursive copies of directories.
3052 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3053 * existing @destination file is overwritten.
3055 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3056 * will be copied as symlinks, otherwise the target of the
3057 * @source symlink will be copied.
3059 * If @cancellable is not %NULL, then the operation can be cancelled by
3060 * triggering the cancellable object from another thread. If the operation
3061 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3063 * If @progress_callback is not %NULL, then the operation can be monitored
3064 * by setting this to a #GFileProgressCallback function.
3065 * @progress_callback_data will be passed to this function. It is guaranteed
3066 * that this callback will be called after all data has been transferred with
3067 * the total number of bytes copied during the operation.
3069 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND error
3070 * is returned, independent on the status of the @destination.
3072 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then
3073 * the error %G_IO_ERROR_EXISTS is returned.
3075 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3076 * error is returned. If trying to overwrite a directory with a directory the
3077 * %G_IO_ERROR_WOULD_MERGE error is returned.
3079 * If the source is a directory and the target does not exist, or
3080 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then the
3081 * %G_IO_ERROR_WOULD_RECURSE error is returned.
3083 * If you are interested in copying the #GFile object itself (not the on-disk
3084 * file), see g_file_dup().
3086 * Returns: %TRUE on success, %FALSE otherwise.
3088 gboolean
3089 g_file_copy (GFile *source,
3090 GFile *destination,
3091 GFileCopyFlags flags,
3092 GCancellable *cancellable,
3093 GFileProgressCallback progress_callback,
3094 gpointer progress_callback_data,
3095 GError **error)
3097 GFileIface *iface;
3098 GError *my_error;
3099 gboolean res;
3101 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3102 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3104 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3105 return FALSE;
3107 iface = G_FILE_GET_IFACE (destination);
3108 if (iface->copy)
3110 my_error = NULL;
3111 res = (* iface->copy) (source, destination,
3112 flags, cancellable,
3113 progress_callback, progress_callback_data,
3114 &my_error);
3116 if (res)
3117 return TRUE;
3119 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3121 g_propagate_error (error, my_error);
3122 return FALSE;
3124 else
3125 g_clear_error (&my_error);
3128 /* If the types are different, and the destination method failed
3129 * also try the source method
3131 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3133 iface = G_FILE_GET_IFACE (source);
3135 if (iface->copy)
3137 my_error = NULL;
3138 res = (* iface->copy) (source, destination,
3139 flags, cancellable,
3140 progress_callback, progress_callback_data,
3141 &my_error);
3143 if (res)
3144 return TRUE;
3146 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3148 g_propagate_error (error, my_error);
3149 return FALSE;
3151 else
3152 g_clear_error (&my_error);
3156 return file_copy_fallback (source, destination, flags, cancellable,
3157 progress_callback, progress_callback_data,
3158 error);
3162 * g_file_copy_async: (skip)
3163 * @source: input #GFile
3164 * @destination: destination #GFile
3165 * @flags: set of #GFileCopyFlags
3166 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3167 * of the request
3168 * @cancellable: (allow-none): optional #GCancellable object,
3169 * %NULL to ignore
3170 * @progress_callback: (allow-none): function to callback with progress
3171 * information, or %NULL if progress information is not needed
3172 * @progress_callback_data: (closure): user data to pass to @progress_callback
3173 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3174 * @user_data: the data to pass to callback function
3176 * Copies the file @source to the location specified by @destination
3177 * asynchronously. For details of the behaviour, see g_file_copy().
3179 * If @progress_callback is not %NULL, then that function that will be called
3180 * just like in g_file_copy(), however the callback will run in the main loop,
3181 * not in the thread that is doing the I/O operation.
3183 * When the operation is finished, @callback will be called. You can then call
3184 * g_file_copy_finish() to get the result of the operation.
3186 void
3187 g_file_copy_async (GFile *source,
3188 GFile *destination,
3189 GFileCopyFlags flags,
3190 int io_priority,
3191 GCancellable *cancellable,
3192 GFileProgressCallback progress_callback,
3193 gpointer progress_callback_data,
3194 GAsyncReadyCallback callback,
3195 gpointer user_data)
3197 GFileIface *iface;
3199 g_return_if_fail (G_IS_FILE (source));
3200 g_return_if_fail (G_IS_FILE (destination));
3202 iface = G_FILE_GET_IFACE (source);
3203 (* iface->copy_async) (source,
3204 destination,
3205 flags,
3206 io_priority,
3207 cancellable,
3208 progress_callback,
3209 progress_callback_data,
3210 callback,
3211 user_data);
3215 * g_file_copy_finish:
3216 * @file: input #GFile
3217 * @res: a #GAsyncResult
3218 * @error: a #GError, or %NULL
3220 * Finishes copying the file started with g_file_copy_async().
3222 * Returns: a %TRUE on success, %FALSE on error.
3224 gboolean
3225 g_file_copy_finish (GFile *file,
3226 GAsyncResult *res,
3227 GError **error)
3229 GFileIface *iface;
3231 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3232 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3234 if (g_async_result_legacy_propagate_error (res, error))
3235 return FALSE;
3237 iface = G_FILE_GET_IFACE (file);
3238 return (* iface->copy_finish) (file, res, error);
3242 * g_file_move:
3243 * @source: #GFile pointing to the source location
3244 * @destination: #GFile pointing to the destination location
3245 * @flags: set of #GFileCopyFlags
3246 * @cancellable: (allow-none): optional #GCancellable object,
3247 * %NULL to ignore
3248 * @progress_callback: (allow-none) (scope call): #GFileProgressCallback
3249 * function for updates
3250 * @progress_callback_data: (closure): gpointer to user data for
3251 * the callback function
3252 * @error: #GError for returning error conditions, or %NULL
3254 * Tries to move the file or directory @source to the location specified
3255 * by @destination. If native move operations are supported then this is
3256 * used, otherwise a copy + delete fallback is used. The native
3257 * implementation may support moving directories (for instance on moves
3258 * inside the same filesystem), but the fallback code does not.
3260 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3261 * existing @destination file is overwritten.
3263 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3264 * will be copied as symlinks, otherwise the target of the
3265 * @source symlink will be copied.
3267 * If @cancellable is not %NULL, then the operation can be cancelled by
3268 * triggering the cancellable object from another thread. If the operation
3269 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3271 * If @progress_callback is not %NULL, then the operation can be monitored
3272 * by setting this to a #GFileProgressCallback function.
3273 * @progress_callback_data will be passed to this function. It is
3274 * guaranteed that this callback will be called after all data has been
3275 * transferred with the total number of bytes copied during the operation.
3277 * If the @source file does not exist, then the %G_IO_ERROR_NOT_FOUND
3278 * error is returned, independent on the status of the @destination.
3280 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists,
3281 * then the error %G_IO_ERROR_EXISTS is returned.
3283 * If trying to overwrite a file over a directory, the %G_IO_ERROR_IS_DIRECTORY
3284 * error is returned. If trying to overwrite a directory with a directory the
3285 * %G_IO_ERROR_WOULD_MERGE error is returned.
3287 * If the source is a directory and the target does not exist, or
3288 * #G_FILE_COPY_OVERWRITE is specified and the target is a file, then
3289 * the %G_IO_ERROR_WOULD_RECURSE error may be returned (if the native
3290 * move operation isn't available).
3292 * Returns: %TRUE on successful move, %FALSE otherwise.
3294 gboolean
3295 g_file_move (GFile *source,
3296 GFile *destination,
3297 GFileCopyFlags flags,
3298 GCancellable *cancellable,
3299 GFileProgressCallback progress_callback,
3300 gpointer progress_callback_data,
3301 GError **error)
3303 GFileIface *iface;
3304 GError *my_error;
3305 gboolean res;
3307 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3308 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3310 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3311 return FALSE;
3313 iface = G_FILE_GET_IFACE (destination);
3314 if (iface->move)
3316 my_error = NULL;
3317 res = (* iface->move) (source, destination,
3318 flags, cancellable,
3319 progress_callback, progress_callback_data,
3320 &my_error);
3322 if (res)
3323 return TRUE;
3325 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3327 g_propagate_error (error, my_error);
3328 return FALSE;
3332 /* If the types are different, and the destination method failed
3333 * also try the source method
3335 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3337 iface = G_FILE_GET_IFACE (source);
3339 if (iface->move)
3341 my_error = NULL;
3342 res = (* iface->move) (source, destination,
3343 flags, cancellable,
3344 progress_callback, progress_callback_data,
3345 &my_error);
3347 if (res)
3348 return TRUE;
3350 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3352 g_propagate_error (error, my_error);
3353 return FALSE;
3358 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3360 g_set_error_literal (error, G_IO_ERROR,
3361 G_IO_ERROR_NOT_SUPPORTED,
3362 _("Operation not supported"));
3363 return FALSE;
3366 flags |= G_FILE_COPY_ALL_METADATA;
3367 if (!g_file_copy (source, destination, flags, cancellable,
3368 progress_callback, progress_callback_data,
3369 error))
3370 return FALSE;
3372 return g_file_delete (source, cancellable, error);
3376 * g_file_make_directory:
3377 * @file: input #GFile
3378 * @cancellable: (allow-none): optional #GCancellable object,
3379 * %NULL to ignore
3380 * @error: a #GError, or %NULL
3382 * Creates a directory. Note that this will only create a child directory
3383 * of the immediate parent directory of the path or URI given by the #GFile.
3384 * To recursively create directories, see g_file_make_directory_with_parents().
3385 * This function will fail if the parent directory does not exist, setting
3386 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support
3387 * creating directories, this function will fail, setting @error to
3388 * %G_IO_ERROR_NOT_SUPPORTED.
3390 * For a local #GFile the newly created directory will have the default
3391 * (current) ownership and permissions of the current process.
3393 * If @cancellable is not %NULL, then the operation can be cancelled by
3394 * triggering the cancellable object from another thread. If the operation
3395 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3397 * Returns: %TRUE on successful creation, %FALSE otherwise.
3399 gboolean
3400 g_file_make_directory (GFile *file,
3401 GCancellable *cancellable,
3402 GError **error)
3404 GFileIface *iface;
3406 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3408 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3409 return FALSE;
3411 iface = G_FILE_GET_IFACE (file);
3413 if (iface->make_directory == NULL)
3415 g_set_error_literal (error, G_IO_ERROR,
3416 G_IO_ERROR_NOT_SUPPORTED,
3417 _("Operation not supported"));
3418 return FALSE;
3421 return (* iface->make_directory) (file, cancellable, error);
3425 * g_file_make_directory_with_parents:
3426 * @file: input #GFile
3427 * @cancellable: (allow-none): optional #GCancellable object,
3428 * %NULL to ignore
3429 * @error: a #GError, or %NULL
3431 * Creates a directory and any parent directories that may not
3432 * exist similar to 'mkdir -p'. If the file system does not support
3433 * creating directories, this function will fail, setting @error to
3434 * %G_IO_ERROR_NOT_SUPPORTED. If the directory itself already exists,
3435 * this function will fail setting @error to %G_IO_ERROR_EXISTS, unlike
3436 * the similar g_mkdir_with_parents().
3438 * For a local #GFile the newly created directories will have the default
3439 * (current) ownership and permissions of the current process.
3441 * If @cancellable is not %NULL, then the operation can be cancelled by
3442 * triggering the cancellable object from another thread. If the operation
3443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3445 * Returns: %TRUE if all directories have been successfully created, %FALSE
3446 * otherwise.
3448 * Since: 2.18
3450 gboolean
3451 g_file_make_directory_with_parents (GFile *file,
3452 GCancellable *cancellable,
3453 GError **error)
3455 GFile *work_file = NULL;
3456 GList *list = NULL, *l;
3457 GError *my_error = NULL;
3459 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3461 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3462 return FALSE;
3464 g_file_make_directory (file, cancellable, &my_error);
3465 if (my_error == NULL || my_error->code != G_IO_ERROR_NOT_FOUND)
3467 if (my_error)
3468 g_propagate_error (error, my_error);
3469 return my_error == NULL;
3472 work_file = g_object_ref (file);
3474 while (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3476 GFile *parent_file;
3478 parent_file = g_file_get_parent (work_file);
3479 if (parent_file == NULL)
3480 break;
3482 g_clear_error (&my_error);
3483 g_file_make_directory (parent_file, cancellable, &my_error);
3485 g_object_unref (work_file);
3486 work_file = g_object_ref (parent_file);
3488 if (my_error != NULL && my_error->code == G_IO_ERROR_NOT_FOUND)
3489 list = g_list_prepend (list, parent_file); /* Transfer ownership of ref */
3490 else
3491 g_object_unref (parent_file);
3494 for (l = list; my_error == NULL && l; l = l->next)
3496 g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3499 if (work_file)
3500 g_object_unref (work_file);
3502 /* Clean up */
3503 while (list != NULL)
3505 g_object_unref ((GFile *) list->data);
3506 list = g_list_remove (list, list->data);
3509 if (my_error != NULL)
3511 g_propagate_error (error, my_error);
3512 return FALSE;
3515 return g_file_make_directory (file, cancellable, error);
3519 * g_file_make_symbolic_link:
3520 * @file: a #GFile with the name of the symlink to create
3521 * @symlink_value: a string with the path for the target of the new symlink
3522 * @cancellable: (allow-none): optional #GCancellable object,
3523 * %NULL to ignore
3524 * @error: a #GError
3526 * Creates a symbolic link named @file which contains the string
3527 * @symlink_value.
3529 * If @cancellable is not %NULL, then the operation can be cancelled by
3530 * triggering the cancellable object from another thread. If the operation
3531 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3533 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3535 gboolean
3536 g_file_make_symbolic_link (GFile *file,
3537 const char *symlink_value,
3538 GCancellable *cancellable,
3539 GError **error)
3541 GFileIface *iface;
3543 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3544 g_return_val_if_fail (symlink_value != NULL, FALSE);
3546 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3547 return FALSE;
3549 if (*symlink_value == '\0')
3551 g_set_error_literal (error, G_IO_ERROR,
3552 G_IO_ERROR_INVALID_ARGUMENT,
3553 _("Invalid symlink value given"));
3554 return FALSE;
3557 iface = G_FILE_GET_IFACE (file);
3559 if (iface->make_symbolic_link == NULL)
3561 g_set_error_literal (error, G_IO_ERROR,
3562 G_IO_ERROR_NOT_SUPPORTED,
3563 _("Operation not supported"));
3564 return FALSE;
3567 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3571 * g_file_delete:
3572 * @file: input #GFile
3573 * @cancellable: (allow-none): optional #GCancellable object,
3574 * %NULL to ignore
3575 * @error: a #GError, or %NULL
3577 * Deletes a file. If the @file is a directory, it will only be
3578 * deleted if it is empty. This has the same semantics as g_unlink().
3580 * If @cancellable is not %NULL, then the operation can be cancelled by
3581 * triggering the cancellable object from another thread. If the operation
3582 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3584 * Virtual: delete_file
3585 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3587 gboolean
3588 g_file_delete (GFile *file,
3589 GCancellable *cancellable,
3590 GError **error)
3592 GFileIface *iface;
3594 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3596 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3597 return FALSE;
3599 iface = G_FILE_GET_IFACE (file);
3601 if (iface->delete_file == NULL)
3603 g_set_error_literal (error, G_IO_ERROR,
3604 G_IO_ERROR_NOT_SUPPORTED,
3605 _("Operation not supported"));
3606 return FALSE;
3609 return (* iface->delete_file) (file, cancellable, error);
3613 * g_file_delete_async:
3614 * @file: input #GFile
3615 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3616 * of the request
3617 * @cancellable: (allow-none): optional #GCancellable object,
3618 * %NULL to ignore
3619 * @callback: a #GAsyncReadyCallback to call
3620 * when the request is satisfied
3621 * @user_data: the data to pass to callback function
3623 * Asynchronously delete a file. If the @file is a directory, it will
3624 * only be deleted if it is empty. This has the same semantics as
3625 * g_unlink().
3627 * Virtual: delete_file_async
3628 * Since: 2.34
3630 void
3631 g_file_delete_async (GFile *file,
3632 int io_priority,
3633 GCancellable *cancellable,
3634 GAsyncReadyCallback callback,
3635 gpointer user_data)
3637 GFileIface *iface;
3639 g_return_if_fail (G_IS_FILE (file));
3641 iface = G_FILE_GET_IFACE (file);
3642 (* iface->delete_file_async) (file,
3643 io_priority,
3644 cancellable,
3645 callback,
3646 user_data);
3650 * g_file_delete_finish:
3651 * @file: input #GFile
3652 * @result: a #GAsyncResult
3653 * @error: a #GError, or %NULL
3655 * Finishes deleting a file started with g_file_delete_async().
3657 * Virtual: delete_file_finish
3658 * Since: 2.34
3660 gboolean
3661 g_file_delete_finish (GFile *file,
3662 GAsyncResult *result,
3663 GError **error)
3665 GFileIface *iface;
3667 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3668 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
3670 if (g_async_result_legacy_propagate_error (result, error))
3671 return FALSE;
3673 iface = G_FILE_GET_IFACE (file);
3674 return (* iface->delete_file_finish) (file, result, error);
3678 * g_file_trash:
3679 * @file: #GFile to send to trash
3680 * @cancellable: (allow-none): optional #GCancellable object,
3681 * %NULL to ignore
3682 * @error: a #GError, or %NULL
3684 * Sends @file to the "Trashcan", if possible. This is similar to
3685 * deleting it, but the user can recover it before emptying the trashcan.
3686 * Not all file systems support trashing, so this call can return the
3687 * %G_IO_ERROR_NOT_SUPPORTED error.
3689 * If @cancellable is not %NULL, then the operation can be cancelled by
3690 * triggering the cancellable object from another thread. If the operation
3691 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3693 * Returns: %TRUE on successful trash, %FALSE otherwise.
3695 gboolean
3696 g_file_trash (GFile *file,
3697 GCancellable *cancellable,
3698 GError **error)
3700 GFileIface *iface;
3702 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3704 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3705 return FALSE;
3707 iface = G_FILE_GET_IFACE (file);
3709 if (iface->trash == NULL)
3711 g_set_error_literal (error,
3712 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3713 _("Trash not supported"));
3714 return FALSE;
3717 return (* iface->trash) (file, cancellable, error);
3721 * g_file_set_display_name:
3722 * @file: input #GFile
3723 * @display_name: a string
3724 * @cancellable: (allow-none): optional #GCancellable object,
3725 * %NULL to ignore
3726 * @error: a #GError, or %NULL
3728 * Renames @file to the specified display name.
3730 * The display name is converted from UTF-8 to the correct encoding
3731 * for the target filesystem if possible and the @file is renamed to this.
3733 * If you want to implement a rename operation in the user interface the
3734 * edit name (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the
3735 * initial value in the rename widget, and then the result after editing
3736 * should be passed to g_file_set_display_name().
3738 * On success the resulting converted filename is returned.
3740 * If @cancellable is not %NULL, then the operation can be cancelled by
3741 * triggering the cancellable object from another thread. If the operation
3742 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3744 * Returns: (transfer full): a #GFile specifying what @file was renamed to,
3745 * or %NULL if there was an error.
3746 * Free the returned object with g_object_unref().
3748 GFile *
3749 g_file_set_display_name (GFile *file,
3750 const gchar *display_name,
3751 GCancellable *cancellable,
3752 GError **error)
3754 GFileIface *iface;
3756 g_return_val_if_fail (G_IS_FILE (file), NULL);
3757 g_return_val_if_fail (display_name != NULL, NULL);
3759 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3761 g_set_error (error,
3762 G_IO_ERROR,
3763 G_IO_ERROR_INVALID_ARGUMENT,
3764 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3765 return NULL;
3768 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3769 return NULL;
3771 iface = G_FILE_GET_IFACE (file);
3773 return (* iface->set_display_name) (file, display_name, cancellable, error);
3777 * g_file_set_display_name_async:
3778 * @file: input #GFile
3779 * @display_name: a string
3780 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3781 * of the request
3782 * @cancellable: (allow-none): optional #GCancellable object,
3783 * %NULL to ignore
3784 * @callback: (scope async): a #GAsyncReadyCallback to call
3785 * when the request is satisfied
3786 * @user_data: (closure): the data to pass to callback function
3788 * Asynchronously sets the display name for a given #GFile.
3790 * For more details, see g_file_set_display_name() which is
3791 * the synchronous version of this call.
3793 * When the operation is finished, @callback will be called.
3794 * You can then call g_file_set_display_name_finish() to get
3795 * the result of the operation.
3797 void
3798 g_file_set_display_name_async (GFile *file,
3799 const gchar *display_name,
3800 gint io_priority,
3801 GCancellable *cancellable,
3802 GAsyncReadyCallback callback,
3803 gpointer user_data)
3805 GFileIface *iface;
3807 g_return_if_fail (G_IS_FILE (file));
3808 g_return_if_fail (display_name != NULL);
3810 iface = G_FILE_GET_IFACE (file);
3811 (* iface->set_display_name_async) (file,
3812 display_name,
3813 io_priority,
3814 cancellable,
3815 callback,
3816 user_data);
3820 * g_file_set_display_name_finish:
3821 * @file: input #GFile
3822 * @res: a #GAsyncResult
3823 * @error: a #GError, or %NULL
3825 * Finishes setting a display name started with
3826 * g_file_set_display_name_async().
3828 * Returns: (transfer full): a #GFile or %NULL on error.
3829 * Free the returned object with g_object_unref().
3831 GFile *
3832 g_file_set_display_name_finish (GFile *file,
3833 GAsyncResult *res,
3834 GError **error)
3836 GFileIface *iface;
3838 g_return_val_if_fail (G_IS_FILE (file), NULL);
3839 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3841 if (g_async_result_legacy_propagate_error (res, error))
3842 return NULL;
3844 iface = G_FILE_GET_IFACE (file);
3845 return (* iface->set_display_name_finish) (file, res, error);
3849 * g_file_query_settable_attributes:
3850 * @file: input #GFile
3851 * @cancellable: (allow-none): optional #GCancellable object,
3852 * %NULL to ignore
3853 * @error: a #GError, or %NULL
3855 * Obtain the list of settable attributes for the file.
3857 * Returns the type and full attribute name of all the attributes
3858 * that can be set on this file. This doesn't mean setting it will
3859 * always succeed though, you might get an access failure, or some
3860 * specific file may not support a specific attribute.
3862 * If @cancellable is not %NULL, then the operation can be cancelled by
3863 * triggering the cancellable object from another thread. If the operation
3864 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3866 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3867 * When you are done with it, release it with
3868 * g_file_attribute_info_list_unref()
3870 GFileAttributeInfoList *
3871 g_file_query_settable_attributes (GFile *file,
3872 GCancellable *cancellable,
3873 GError **error)
3875 GFileIface *iface;
3876 GError *my_error;
3877 GFileAttributeInfoList *list;
3879 g_return_val_if_fail (G_IS_FILE (file), NULL);
3881 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3882 return NULL;
3884 iface = G_FILE_GET_IFACE (file);
3886 if (iface->query_settable_attributes == NULL)
3887 return g_file_attribute_info_list_new ();
3889 my_error = NULL;
3890 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3892 if (list == NULL)
3894 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3896 list = g_file_attribute_info_list_new ();
3897 g_error_free (my_error);
3899 else
3900 g_propagate_error (error, my_error);
3903 return list;
3907 * g_file_query_writable_namespaces:
3908 * @file: input #GFile
3909 * @cancellable: (allow-none): optional #GCancellable object,
3910 * %NULL to ignore
3911 * @error: a #GError, or %NULL
3913 * Obtain the list of attribute namespaces where new attributes
3914 * can be created by a user. An example of this is extended
3915 * attributes (in the "xattr" namespace).
3917 * If @cancellable is not %NULL, then the operation can be cancelled by
3918 * triggering the cancellable object from another thread. If the operation
3919 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3921 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3922 * When you are done with it, release it with
3923 * g_file_attribute_info_list_unref()
3925 GFileAttributeInfoList *
3926 g_file_query_writable_namespaces (GFile *file,
3927 GCancellable *cancellable,
3928 GError **error)
3930 GFileIface *iface;
3931 GError *my_error;
3932 GFileAttributeInfoList *list;
3934 g_return_val_if_fail (G_IS_FILE (file), NULL);
3936 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3937 return NULL;
3939 iface = G_FILE_GET_IFACE (file);
3941 if (iface->query_writable_namespaces == NULL)
3942 return g_file_attribute_info_list_new ();
3944 my_error = NULL;
3945 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3947 if (list == NULL)
3949 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3951 list = g_file_attribute_info_list_new ();
3952 g_error_free (my_error);
3954 else
3955 g_propagate_error (error, my_error);
3958 return list;
3962 * g_file_set_attribute:
3963 * @file: input #GFile
3964 * @attribute: a string containing the attribute's name
3965 * @type: The type of the attribute
3966 * @value_p: (allow-none): a pointer to the value (or the pointer
3967 * itself if the type is a pointer type)
3968 * @flags: a set of #GFileQueryInfoFlags
3969 * @cancellable: (allow-none): optional #GCancellable object,
3970 * %NULL to ignore
3971 * @error: a #GError, or %NULL
3973 * Sets an attribute in the file with attribute name @attribute to @value.
3975 * Some attributes can be unset by setting @attribute to
3976 * %G_FILE_ATTRIBUTE_TYPE_INVALID and @value_p to %NULL.
3978 * If @cancellable is not %NULL, then the operation can be cancelled by
3979 * triggering the cancellable object from another thread. If the operation
3980 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3982 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3984 gboolean
3985 g_file_set_attribute (GFile *file,
3986 const gchar *attribute,
3987 GFileAttributeType type,
3988 gpointer value_p,
3989 GFileQueryInfoFlags flags,
3990 GCancellable *cancellable,
3991 GError **error)
3993 GFileIface *iface;
3995 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3996 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3998 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3999 return FALSE;
4001 iface = G_FILE_GET_IFACE (file);
4003 if (iface->set_attribute == NULL)
4005 g_set_error_literal (error, G_IO_ERROR,
4006 G_IO_ERROR_NOT_SUPPORTED,
4007 _("Operation not supported"));
4008 return FALSE;
4011 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
4015 * g_file_set_attributes_from_info:
4016 * @file: input #GFile
4017 * @info: a #GFileInfo
4018 * @flags: #GFileQueryInfoFlags
4019 * @cancellable: (allow-none): optional #GCancellable object,
4020 * %NULL to ignore
4021 * @error: a #GError, or %NULL
4023 * Tries to set all attributes in the #GFileInfo on the target
4024 * values, not stopping on the first error.
4026 * If there is any error during this operation then @error will
4027 * be set to the first error. Error on particular fields are flagged
4028 * by setting the "status" field in the attribute value to
4029 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can
4030 * also detect further errors.
4032 * If @cancellable is not %NULL, then the operation can be cancelled by
4033 * triggering the cancellable object from another thread. If the operation
4034 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4036 * Returns: %FALSE if there was any error, %TRUE otherwise.
4038 gboolean
4039 g_file_set_attributes_from_info (GFile *file,
4040 GFileInfo *info,
4041 GFileQueryInfoFlags flags,
4042 GCancellable *cancellable,
4043 GError **error)
4045 GFileIface *iface;
4047 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4048 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
4050 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4051 return FALSE;
4053 g_file_info_clear_status (info);
4055 iface = G_FILE_GET_IFACE (file);
4057 return (* iface->set_attributes_from_info) (file,
4058 info,
4059 flags,
4060 cancellable,
4061 error);
4064 static gboolean
4065 g_file_real_set_attributes_from_info (GFile *file,
4066 GFileInfo *info,
4067 GFileQueryInfoFlags flags,
4068 GCancellable *cancellable,
4069 GError **error)
4071 char **attributes;
4072 int i;
4073 gboolean res;
4074 GFileAttributeValue *value;
4076 res = TRUE;
4078 attributes = g_file_info_list_attributes (info, NULL);
4080 for (i = 0; attributes[i] != NULL; i++)
4082 value = _g_file_info_get_attribute_value (info, attributes[i]);
4084 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
4085 continue;
4087 if (!g_file_set_attribute (file, attributes[i],
4088 value->type, _g_file_attribute_value_peek_as_pointer (value),
4089 flags, cancellable, error))
4091 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
4092 res = FALSE;
4093 /* Don't set error multiple times */
4094 error = NULL;
4096 else
4097 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
4100 g_strfreev (attributes);
4102 return res;
4106 * g_file_set_attributes_async:
4107 * @file: input #GFile
4108 * @info: a #GFileInfo
4109 * @flags: a #GFileQueryInfoFlags
4110 * @io_priority: the <link linkend="io-priority">I/O priority</link>
4111 * of the request
4112 * @cancellable: (allow-none): optional #GCancellable object,
4113 * %NULL to ignore
4114 * @callback: (scope async): a #GAsyncReadyCallback
4115 * @user_data: (closure): a #gpointer
4117 * Asynchronously sets the attributes of @file with @info.
4119 * For more details, see g_file_set_attributes_from_info(),
4120 * which is the synchronous version of this call.
4122 * When the operation is finished, @callback will be called.
4123 * You can then call g_file_set_attributes_finish() to get
4124 * the result of the operation.
4126 void
4127 g_file_set_attributes_async (GFile *file,
4128 GFileInfo *info,
4129 GFileQueryInfoFlags flags,
4130 int io_priority,
4131 GCancellable *cancellable,
4132 GAsyncReadyCallback callback,
4133 gpointer user_data)
4135 GFileIface *iface;
4137 g_return_if_fail (G_IS_FILE (file));
4138 g_return_if_fail (G_IS_FILE_INFO (info));
4140 iface = G_FILE_GET_IFACE (file);
4141 (* iface->set_attributes_async) (file,
4142 info,
4143 flags,
4144 io_priority,
4145 cancellable,
4146 callback,
4147 user_data);
4151 * g_file_set_attributes_finish:
4152 * @file: input #GFile
4153 * @result: a #GAsyncResult
4154 * @info: (out) (transfer full): a #GFileInfo
4155 * @error: a #GError, or %NULL
4157 * Finishes setting an attribute started in g_file_set_attributes_async().
4159 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4161 gboolean
4162 g_file_set_attributes_finish (GFile *file,
4163 GAsyncResult *result,
4164 GFileInfo **info,
4165 GError **error)
4167 GFileIface *iface;
4169 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4170 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4172 /* No standard handling of errors here, as we must set info even
4173 * on errors
4175 iface = G_FILE_GET_IFACE (file);
4176 return (* iface->set_attributes_finish) (file, result, info, error);
4180 * g_file_set_attribute_string:
4181 * @file: input #GFile
4182 * @attribute: a string containing the attribute's name
4183 * @value: a string containing the attribute's value
4184 * @flags: #GFileQueryInfoFlags
4185 * @cancellable: (allow-none): optional #GCancellable object,
4186 * %NULL to ignore
4187 * @error: a #GError, or %NULL
4189 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4190 * If @attribute is of a different type, this operation will fail.
4192 * If @cancellable is not %NULL, then the operation can be cancelled by
4193 * triggering the cancellable object from another thread. If the operation
4194 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4196 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4198 gboolean
4199 g_file_set_attribute_string (GFile *file,
4200 const char *attribute,
4201 const char *value,
4202 GFileQueryInfoFlags flags,
4203 GCancellable *cancellable,
4204 GError **error)
4206 return g_file_set_attribute (file, attribute,
4207 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4208 flags, cancellable, error);
4212 * g_file_set_attribute_byte_string:
4213 * @file: input #GFile
4214 * @attribute: a string containing the attribute's name
4215 * @value: a string containing the attribute's new value
4216 * @flags: a #GFileQueryInfoFlags
4217 * @cancellable: (allow-none): optional #GCancellable object,
4218 * %NULL to ignore
4219 * @error: a #GError, or %NULL
4221 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4222 * If @attribute is of a different type, this operation will fail,
4223 * returning %FALSE.
4225 * If @cancellable is not %NULL, then the operation can be cancelled by
4226 * triggering the cancellable object from another thread. If the operation
4227 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4229 * Returns: %TRUE if the @attribute was successfully set to @value
4230 * in the @file, %FALSE otherwise.
4232 gboolean
4233 g_file_set_attribute_byte_string (GFile *file,
4234 const gchar *attribute,
4235 const gchar *value,
4236 GFileQueryInfoFlags flags,
4237 GCancellable *cancellable,
4238 GError **error)
4240 return g_file_set_attribute (file, attribute,
4241 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4242 flags, cancellable, error);
4246 * g_file_set_attribute_uint32:
4247 * @file: input #GFile
4248 * @attribute: a string containing the attribute's name
4249 * @value: a #guint32 containing the attribute's new value
4250 * @flags: a #GFileQueryInfoFlags
4251 * @cancellable: (allow-none): optional #GCancellable object,
4252 * %NULL to ignore
4253 * @error: a #GError, or %NULL
4255 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4256 * If @attribute is of a different type, this operation will fail.
4258 * If @cancellable is not %NULL, then the operation can be cancelled by
4259 * triggering the cancellable object from another thread. If the operation
4260 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4262 * Returns: %TRUE if the @attribute was successfully set to @value
4263 * in the @file, %FALSE otherwise.
4265 gboolean
4266 g_file_set_attribute_uint32 (GFile *file,
4267 const gchar *attribute,
4268 guint32 value,
4269 GFileQueryInfoFlags flags,
4270 GCancellable *cancellable,
4271 GError **error)
4273 return g_file_set_attribute (file, attribute,
4274 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4275 flags, cancellable, error);
4279 * g_file_set_attribute_int32:
4280 * @file: input #GFile
4281 * @attribute: a string containing the attribute's name
4282 * @value: a #gint32 containing the attribute's new value
4283 * @flags: a #GFileQueryInfoFlags
4284 * @cancellable: (allow-none): optional #GCancellable object,
4285 * %NULL to ignore
4286 * @error: a #GError, or %NULL
4288 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4289 * If @attribute is of a different type, this operation will fail.
4291 * If @cancellable is not %NULL, then the operation can be cancelled by
4292 * triggering the cancellable object from another thread. If the operation
4293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4295 * Returns: %TRUE if the @attribute was successfully set to @value
4296 * in the @file, %FALSE otherwise.
4298 gboolean
4299 g_file_set_attribute_int32 (GFile *file,
4300 const gchar *attribute,
4301 gint32 value,
4302 GFileQueryInfoFlags flags,
4303 GCancellable *cancellable,
4304 GError **error)
4306 return g_file_set_attribute (file, attribute,
4307 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4308 flags, cancellable, error);
4312 * g_file_set_attribute_uint64:
4313 * @file: input #GFile
4314 * @attribute: a string containing the attribute's name
4315 * @value: a #guint64 containing the attribute's new value
4316 * @flags: a #GFileQueryInfoFlags
4317 * @cancellable: (allow-none): optional #GCancellable object,
4318 * %NULL to ignore
4319 * @error: a #GError, or %NULL
4321 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4322 * If @attribute is of a different type, this operation will fail.
4324 * If @cancellable is not %NULL, then the operation can be cancelled by
4325 * triggering the cancellable object from another thread. If the operation
4326 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4328 * Returns: %TRUE if the @attribute was successfully set to @value
4329 * in the @file, %FALSE otherwise.
4331 gboolean
4332 g_file_set_attribute_uint64 (GFile *file,
4333 const gchar *attribute,
4334 guint64 value,
4335 GFileQueryInfoFlags flags,
4336 GCancellable *cancellable,
4337 GError **error)
4339 return g_file_set_attribute (file, attribute,
4340 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4341 flags, cancellable, error);
4345 * g_file_set_attribute_int64:
4346 * @file: input #GFile
4347 * @attribute: a string containing the attribute's name
4348 * @value: a #guint64 containing the attribute's new value
4349 * @flags: a #GFileQueryInfoFlags
4350 * @cancellable: (allow-none): optional #GCancellable object,
4351 * %NULL to ignore
4352 * @error: a #GError, or %NULL
4354 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4355 * If @attribute is of a different type, this operation will fail.
4357 * If @cancellable is not %NULL, then the operation can be cancelled by
4358 * triggering the cancellable object from another thread. If the operation
4359 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4361 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4363 gboolean
4364 g_file_set_attribute_int64 (GFile *file,
4365 const gchar *attribute,
4366 gint64 value,
4367 GFileQueryInfoFlags flags,
4368 GCancellable *cancellable,
4369 GError **error)
4371 return g_file_set_attribute (file, attribute,
4372 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4373 flags, cancellable, error);
4377 * g_file_mount_mountable:
4378 * @file: input #GFile
4379 * @flags: flags affecting the operation
4380 * @mount_operation: (allow-none): a #GMountOperation,
4381 * or %NULL to avoid user interaction
4382 * @cancellable: (allow-none): optional #GCancellable object,
4383 * %NULL to ignore
4384 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4385 * when the request is satisfied, or %NULL
4386 * @user_data: (closure): the data to pass to callback function
4388 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4389 * Using @mount_operation, you can request callbacks when, for instance,
4390 * passwords are needed during authentication.
4392 * If @cancellable is not %NULL, then the operation can be cancelled by
4393 * triggering the cancellable object from another thread. If the operation
4394 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4396 * When the operation is finished, @callback will be called.
4397 * You can then call g_file_mount_mountable_finish() to get
4398 * the result of the operation.
4400 void
4401 g_file_mount_mountable (GFile *file,
4402 GMountMountFlags flags,
4403 GMountOperation *mount_operation,
4404 GCancellable *cancellable,
4405 GAsyncReadyCallback callback,
4406 gpointer user_data)
4408 GFileIface *iface;
4410 g_return_if_fail (G_IS_FILE (file));
4412 iface = G_FILE_GET_IFACE (file);
4414 if (iface->mount_mountable == NULL)
4416 g_task_report_new_error (file, callback, user_data,
4417 g_file_mount_mountable,
4418 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4419 _("Operation not supported"));
4420 return;
4423 (* iface->mount_mountable) (file,
4424 flags,
4425 mount_operation,
4426 cancellable,
4427 callback,
4428 user_data);
4432 * g_file_mount_mountable_finish:
4433 * @file: input #GFile
4434 * @result: a #GAsyncResult
4435 * @error: a #GError, or %NULL
4437 * Finishes a mount operation. See g_file_mount_mountable() for details.
4439 * Finish an asynchronous mount operation that was started
4440 * with g_file_mount_mountable().
4442 * Returns: (transfer full): a #GFile or %NULL on error.
4443 * Free the returned object with g_object_unref().
4445 GFile *
4446 g_file_mount_mountable_finish (GFile *file,
4447 GAsyncResult *result,
4448 GError **error)
4450 GFileIface *iface;
4452 g_return_val_if_fail (G_IS_FILE (file), NULL);
4453 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4455 if (g_async_result_legacy_propagate_error (result, error))
4456 return NULL;
4457 else if (g_async_result_is_tagged (result, g_file_mount_mountable))
4458 return g_task_propagate_pointer (G_TASK (result), error);
4460 iface = G_FILE_GET_IFACE (file);
4461 return (* iface->mount_mountable_finish) (file, result, error);
4465 * g_file_unmount_mountable:
4466 * @file: input #GFile
4467 * @flags: flags affecting the operation
4468 * @cancellable: (allow-none): optional #GCancellable object,
4469 * %NULL to ignore
4470 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4471 * when the request is satisfied, or %NULL
4472 * @user_data: (closure): the data to pass to callback function
4474 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4476 * If @cancellable is not %NULL, then the operation can be cancelled by
4477 * triggering the cancellable object from another thread. If the operation
4478 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4480 * When the operation is finished, @callback will be called.
4481 * You can then call g_file_unmount_mountable_finish() to get
4482 * the result of the operation.
4484 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4486 void
4487 g_file_unmount_mountable (GFile *file,
4488 GMountUnmountFlags flags,
4489 GCancellable *cancellable,
4490 GAsyncReadyCallback callback,
4491 gpointer user_data)
4493 GFileIface *iface;
4495 g_return_if_fail (G_IS_FILE (file));
4497 iface = G_FILE_GET_IFACE (file);
4499 if (iface->unmount_mountable == NULL)
4501 g_task_report_new_error (file, callback, user_data,
4502 g_file_unmount_mountable_with_operation,
4503 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4504 _("Operation not supported"));
4505 return;
4508 (* iface->unmount_mountable) (file,
4509 flags,
4510 cancellable,
4511 callback,
4512 user_data);
4516 * g_file_unmount_mountable_finish:
4517 * @file: input #GFile
4518 * @result: a #GAsyncResult
4519 * @error: a #GError, or %NULL
4521 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4523 * Finish an asynchronous unmount operation that was started
4524 * with g_file_unmount_mountable().
4526 * Returns: %TRUE if the operation finished successfully.
4527 * %FALSE otherwise.
4529 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish()
4530 * instead.
4532 gboolean
4533 g_file_unmount_mountable_finish (GFile *file,
4534 GAsyncResult *result,
4535 GError **error)
4537 GFileIface *iface;
4539 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4540 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4542 if (g_async_result_legacy_propagate_error (result, error))
4543 return FALSE;
4544 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4545 return g_task_propagate_boolean (G_TASK (result), error);
4547 iface = G_FILE_GET_IFACE (file);
4548 return (* iface->unmount_mountable_finish) (file, result, error);
4552 * g_file_unmount_mountable_with_operation:
4553 * @file: input #GFile
4554 * @flags: flags affecting the operation
4555 * @mount_operation: (allow-none): a #GMountOperation,
4556 * or %NULL to avoid user interaction
4557 * @cancellable: (allow-none): optional #GCancellable object,
4558 * %NULL to ignore
4559 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4560 * when the request is satisfied, or %NULL
4561 * @user_data: (closure): the data to pass to callback function
4563 * Unmounts a file of type #G_FILE_TYPE_MOUNTABLE.
4565 * If @cancellable is not %NULL, then the operation can be cancelled by
4566 * triggering the cancellable object from another thread. If the operation
4567 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4569 * When the operation is finished, @callback will be called.
4570 * You can then call g_file_unmount_mountable_finish() to get
4571 * the result of the operation.
4573 * Since: 2.22
4575 void
4576 g_file_unmount_mountable_with_operation (GFile *file,
4577 GMountUnmountFlags flags,
4578 GMountOperation *mount_operation,
4579 GCancellable *cancellable,
4580 GAsyncReadyCallback callback,
4581 gpointer user_data)
4583 GFileIface *iface;
4585 g_return_if_fail (G_IS_FILE (file));
4587 iface = G_FILE_GET_IFACE (file);
4589 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4591 g_task_report_new_error (file, callback, user_data,
4592 g_file_unmount_mountable_with_operation,
4593 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4594 _("Operation not supported"));
4595 return;
4598 if (iface->unmount_mountable_with_operation != NULL)
4599 (* iface->unmount_mountable_with_operation) (file,
4600 flags,
4601 mount_operation,
4602 cancellable,
4603 callback,
4604 user_data);
4605 else
4606 (* iface->unmount_mountable) (file,
4607 flags,
4608 cancellable,
4609 callback,
4610 user_data);
4614 * g_file_unmount_mountable_with_operation_finish:
4615 * @file: input #GFile
4616 * @result: a #GAsyncResult
4617 * @error: a #GError, or %NULL
4619 * Finishes an unmount operation,
4620 * see g_file_unmount_mountable_with_operation() for details.
4622 * Finish an asynchronous unmount operation that was started
4623 * with g_file_unmount_mountable_with_operation().
4625 * Returns: %TRUE if the operation finished successfully.
4626 * %FALSE otherwise.
4628 * Since: 2.22
4630 gboolean
4631 g_file_unmount_mountable_with_operation_finish (GFile *file,
4632 GAsyncResult *result,
4633 GError **error)
4635 GFileIface *iface;
4637 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4638 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4640 if (g_async_result_legacy_propagate_error (result, error))
4641 return FALSE;
4642 else if (g_async_result_is_tagged (result, g_file_unmount_mountable_with_operation))
4643 return g_task_propagate_boolean (G_TASK (result), error);
4645 iface = G_FILE_GET_IFACE (file);
4646 if (iface->unmount_mountable_with_operation_finish != NULL)
4647 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4648 else
4649 return (* iface->unmount_mountable_finish) (file, result, error);
4653 * g_file_eject_mountable:
4654 * @file: input #GFile
4655 * @flags: flags affecting the operation
4656 * @cancellable: (allow-none): optional #GCancellable object,
4657 * %NULL to ignore
4658 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4659 * when the request is satisfied, or %NULL
4660 * @user_data: (closure): the data to pass to callback function
4662 * Starts an asynchronous eject on a mountable.
4663 * When this operation has completed, @callback will be called with
4664 * @user_user data, and the operation can be finalized with
4665 * g_file_eject_mountable_finish().
4667 * If @cancellable is not %NULL, then the operation can be cancelled by
4668 * triggering the cancellable object from another thread. If the operation
4669 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4671 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4673 void
4674 g_file_eject_mountable (GFile *file,
4675 GMountUnmountFlags flags,
4676 GCancellable *cancellable,
4677 GAsyncReadyCallback callback,
4678 gpointer user_data)
4680 GFileIface *iface;
4682 g_return_if_fail (G_IS_FILE (file));
4684 iface = G_FILE_GET_IFACE (file);
4686 if (iface->eject_mountable == NULL)
4688 g_task_report_new_error (file, callback, user_data,
4689 g_file_eject_mountable_with_operation,
4690 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4691 _("Operation not supported"));
4692 return;
4695 (* iface->eject_mountable) (file,
4696 flags,
4697 cancellable,
4698 callback,
4699 user_data);
4703 * g_file_eject_mountable_finish:
4704 * @file: input #GFile
4705 * @result: a #GAsyncResult
4706 * @error: a #GError, or %NULL
4708 * Finishes an asynchronous eject operation started by
4709 * g_file_eject_mountable().
4711 * Returns: %TRUE if the @file was ejected successfully.
4712 * %FALSE otherwise.
4714 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish()
4715 * instead.
4717 gboolean
4718 g_file_eject_mountable_finish (GFile *file,
4719 GAsyncResult *result,
4720 GError **error)
4722 GFileIface *iface;
4724 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4725 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4727 if (g_async_result_legacy_propagate_error (result, error))
4728 return FALSE;
4729 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4730 return g_task_propagate_boolean (G_TASK (result), error);
4732 iface = G_FILE_GET_IFACE (file);
4733 return (* iface->eject_mountable_finish) (file, result, error);
4737 * g_file_eject_mountable_with_operation:
4738 * @file: input #GFile
4739 * @flags: flags affecting the operation
4740 * @mount_operation: (allow-none): a #GMountOperation,
4741 * or %NULL to avoid user interaction
4742 * @cancellable: (allow-none): optional #GCancellable object,
4743 * %NULL to ignore
4744 * @callback: (scope async) (allow-none): a #GAsyncReadyCallback to call
4745 * when the request is satisfied, or %NULL
4746 * @user_data: (closure): the data to pass to callback function
4748 * Starts an asynchronous eject on a mountable.
4749 * When this operation has completed, @callback will be called with
4750 * @user_user data, and the operation can be finalized with
4751 * g_file_eject_mountable_with_operation_finish().
4753 * If @cancellable is not %NULL, then the operation can be cancelled by
4754 * triggering the cancellable object from another thread. If the operation
4755 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4757 * Since: 2.22
4759 void
4760 g_file_eject_mountable_with_operation (GFile *file,
4761 GMountUnmountFlags flags,
4762 GMountOperation *mount_operation,
4763 GCancellable *cancellable,
4764 GAsyncReadyCallback callback,
4765 gpointer user_data)
4767 GFileIface *iface;
4769 g_return_if_fail (G_IS_FILE (file));
4771 iface = G_FILE_GET_IFACE (file);
4773 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4775 g_task_report_new_error (file, callback, user_data,
4776 g_file_eject_mountable_with_operation,
4777 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
4778 _("Operation not supported"));
4779 return;
4782 if (iface->eject_mountable_with_operation != NULL)
4783 (* iface->eject_mountable_with_operation) (file,
4784 flags,
4785 mount_operation,
4786 cancellable,
4787 callback,
4788 user_data);
4789 else
4790 (* iface->eject_mountable) (file,
4791 flags,
4792 cancellable,
4793 callback,
4794 user_data);
4798 * g_file_eject_mountable_with_operation_finish:
4799 * @file: input #GFile
4800 * @result: a #GAsyncResult
4801 * @error: a #GError, or %NULL
4803 * Finishes an asynchronous eject operation started by
4804 * g_file_eject_mountable_with_operation().
4806 * Returns: %TRUE if the @file was ejected successfully.
4807 * %FALSE otherwise.
4809 * Since: 2.22
4811 gboolean
4812 g_file_eject_mountable_with_operation_finish (GFile *file,
4813 GAsyncResult *result,
4814 GError **error)
4816 GFileIface *iface;
4818 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4819 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4821 if (g_async_result_legacy_propagate_error (result, error))
4822 return FALSE;
4823 else if (g_async_result_is_tagged (result, g_file_eject_mountable_with_operation))
4824 return g_task_propagate_boolean (G_TASK (result), error);
4826 iface = G_FILE_GET_IFACE (file);
4827 if (iface->eject_mountable_with_operation_finish != NULL)
4828 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4829 else
4830 return (* iface->eject_mountable_finish) (file, result, error);
4834 * g_file_monitor_directory:
4835 * @file: input #GFile
4836 * @flags: a set of #GFileMonitorFlags
4837 * @cancellable: (allow-none): optional #GCancellable object,
4838 * %NULL to ignore
4839 * @error: a #GError, or %NULL
4841 * Obtains a directory monitor for the given file.
4842 * This may fail if directory monitoring is not supported.
4844 * If @cancellable is not %NULL, then the operation can be cancelled by
4845 * triggering the cancellable object from another thread. If the operation
4846 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4848 * It does not make sense for @flags to contain
4849 * %G_FILE_MONITOR_WATCH_HARD_LINKS, since hard links can not be made to
4850 * directories. It is not possible to monitor all the files in a
4851 * directory for changes made via hard links; if you want to do this then
4852 * you must register individual watches with g_file_monitor().
4854 * Virtual: monitor_dir
4855 * Returns: (transfer full): a #GFileMonitor for the given @file,
4856 * or %NULL on error.
4857 * Free the returned object with g_object_unref().
4859 GFileMonitor *
4860 g_file_monitor_directory (GFile *file,
4861 GFileMonitorFlags flags,
4862 GCancellable *cancellable,
4863 GError **error)
4865 GFileIface *iface;
4867 g_return_val_if_fail (G_IS_FILE (file), NULL);
4868 g_return_val_if_fail (~flags & G_FILE_MONITOR_WATCH_HARD_LINKS, NULL);
4870 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4871 return NULL;
4873 iface = G_FILE_GET_IFACE (file);
4875 if (iface->monitor_dir == NULL)
4877 g_set_error_literal (error, G_IO_ERROR,
4878 G_IO_ERROR_NOT_SUPPORTED,
4879 _("Operation not supported"));
4880 return NULL;
4883 return (* iface->monitor_dir) (file, flags, cancellable, error);
4887 * g_file_monitor_file:
4888 * @file: input #GFile
4889 * @flags: a set of #GFileMonitorFlags
4890 * @cancellable: (allow-none): optional #GCancellable object,
4891 * %NULL to ignore
4892 * @error: a #GError, or %NULL
4894 * Obtains a file monitor for the given file. If no file notification
4895 * mechanism exists, then regular polling of the file is used.
4897 * If @cancellable is not %NULL, then the operation can be cancelled by
4898 * triggering the cancellable object from another thread. If the operation
4899 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4901 * If @flags contains %G_FILE_MONITOR_WATCH_HARD_LINKS then the monitor
4902 * will also attempt to report changes made to the file via another
4903 * filename (ie, a hard link). Without this flag, you can only rely on
4904 * changes made through the filename contained in @file to be
4905 * reported. Using this flag may result in an increase in resource
4906 * usage, and may not have any effect depending on the #GFileMonitor
4907 * backend and/or filesystem type.
4909 * Returns: (transfer full): a #GFileMonitor for the given @file,
4910 * or %NULL on error.
4911 * Free the returned object with g_object_unref().
4913 GFileMonitor *
4914 g_file_monitor_file (GFile *file,
4915 GFileMonitorFlags flags,
4916 GCancellable *cancellable,
4917 GError **error)
4919 GFileIface *iface;
4920 GFileMonitor *monitor;
4922 g_return_val_if_fail (G_IS_FILE (file), NULL);
4924 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4925 return NULL;
4927 iface = G_FILE_GET_IFACE (file);
4929 monitor = NULL;
4931 if (iface->monitor_file)
4932 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4934 /* Fallback to polling */
4935 if (monitor == NULL)
4936 monitor = _g_poll_file_monitor_new (file);
4938 return monitor;
4942 * g_file_monitor:
4943 * @file: input #GFile
4944 * @flags: a set of #GFileMonitorFlags
4945 * @cancellable: (allow-none): optional #GCancellable object,
4946 * %NULL to ignore
4947 * @error: a #GError, or %NULL
4949 * Obtains a file or directory monitor for the given file,
4950 * depending on the type of the file.
4952 * If @cancellable is not %NULL, then the operation can be cancelled by
4953 * triggering the cancellable object from another thread. If the operation
4954 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4956 * Returns: (transfer full): a #GFileMonitor for the given @file,
4957 * or %NULL on error.
4958 * Free the returned object with g_object_unref().
4960 * Since: 2.18
4962 GFileMonitor *
4963 g_file_monitor (GFile *file,
4964 GFileMonitorFlags flags,
4965 GCancellable *cancellable,
4966 GError **error)
4968 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4969 return g_file_monitor_directory (file,
4970 flags & ~G_FILE_MONITOR_WATCH_HARD_LINKS,
4971 cancellable, error);
4972 else
4973 return g_file_monitor_file (file, flags, cancellable, error);
4976 /********************************************
4977 * Default implementation of async ops *
4978 ********************************************/
4980 typedef struct {
4981 char *attributes;
4982 GFileQueryInfoFlags flags;
4983 } QueryInfoAsyncData;
4985 static void
4986 query_info_data_free (QueryInfoAsyncData *data)
4988 g_free (data->attributes);
4989 g_free (data);
4992 static void
4993 query_info_async_thread (GTask *task,
4994 gpointer object,
4995 gpointer task_data,
4996 GCancellable *cancellable)
4998 QueryInfoAsyncData *data = task_data;
4999 GFileInfo *info;
5000 GError *error = NULL;
5002 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5003 if (info)
5004 g_task_return_pointer (task, info, g_object_unref);
5005 else
5006 g_task_return_error (task, error);
5009 static void
5010 g_file_real_query_info_async (GFile *file,
5011 const char *attributes,
5012 GFileQueryInfoFlags flags,
5013 int io_priority,
5014 GCancellable *cancellable,
5015 GAsyncReadyCallback callback,
5016 gpointer user_data)
5018 GTask *task;
5019 QueryInfoAsyncData *data;
5021 data = g_new0 (QueryInfoAsyncData, 1);
5022 data->attributes = g_strdup (attributes);
5023 data->flags = flags;
5025 task = g_task_new (file, cancellable, callback, user_data);
5026 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5027 g_task_set_priority (task, io_priority);
5028 g_task_run_in_thread (task, query_info_async_thread);
5029 g_object_unref (task);
5032 static GFileInfo *
5033 g_file_real_query_info_finish (GFile *file,
5034 GAsyncResult *res,
5035 GError **error)
5037 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5039 return g_task_propagate_pointer (G_TASK (res), error);
5042 static void
5043 query_filesystem_info_async_thread (GTask *task,
5044 gpointer object,
5045 gpointer task_data,
5046 GCancellable *cancellable)
5048 const char *attributes = task_data;
5049 GFileInfo *info;
5050 GError *error = NULL;
5052 info = g_file_query_filesystem_info (G_FILE (object), attributes, cancellable, &error);
5053 if (info)
5054 g_task_return_pointer (task, info, g_object_unref);
5055 else
5056 g_task_return_error (task, error);
5059 static void
5060 g_file_real_query_filesystem_info_async (GFile *file,
5061 const char *attributes,
5062 int io_priority,
5063 GCancellable *cancellable,
5064 GAsyncReadyCallback callback,
5065 gpointer user_data)
5067 GTask *task;
5069 task = g_task_new (file, cancellable, callback, user_data);
5070 g_task_set_task_data (task, g_strdup (attributes), g_free);
5071 g_task_set_priority (task, io_priority);
5072 g_task_run_in_thread (task, query_filesystem_info_async_thread);
5073 g_object_unref (task);
5076 static GFileInfo *
5077 g_file_real_query_filesystem_info_finish (GFile *file,
5078 GAsyncResult *res,
5079 GError **error)
5081 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5083 return g_task_propagate_pointer (G_TASK (res), error);
5086 static void
5087 enumerate_children_async_thread (GTask *task,
5088 gpointer object,
5089 gpointer task_data,
5090 GCancellable *cancellable)
5092 QueryInfoAsyncData *data = task_data;
5093 GFileEnumerator *enumerator;
5094 GError *error = NULL;
5096 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
5097 if (error)
5098 g_task_return_error (task, error);
5099 else
5100 g_task_return_pointer (task, enumerator, g_object_unref);
5103 static void
5104 g_file_real_enumerate_children_async (GFile *file,
5105 const char *attributes,
5106 GFileQueryInfoFlags flags,
5107 int io_priority,
5108 GCancellable *cancellable,
5109 GAsyncReadyCallback callback,
5110 gpointer user_data)
5112 GTask *task;
5113 QueryInfoAsyncData *data;
5115 data = g_new0 (QueryInfoAsyncData, 1);
5116 data->attributes = g_strdup (attributes);
5117 data->flags = flags;
5119 task = g_task_new (file, cancellable, callback, user_data);
5120 g_task_set_task_data (task, data, (GDestroyNotify)query_info_data_free);
5121 g_task_set_priority (task, io_priority);
5122 g_task_run_in_thread (task, enumerate_children_async_thread);
5123 g_object_unref (task);
5126 static GFileEnumerator *
5127 g_file_real_enumerate_children_finish (GFile *file,
5128 GAsyncResult *res,
5129 GError **error)
5131 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5133 return g_task_propagate_pointer (G_TASK (res), error);
5136 static void
5137 open_read_async_thread (GTask *task,
5138 gpointer object,
5139 gpointer task_data,
5140 GCancellable *cancellable)
5142 GFileIface *iface;
5143 GFileInputStream *stream;
5144 GError *error = NULL;
5146 iface = G_FILE_GET_IFACE (object);
5148 if (iface->read_fn == NULL)
5150 g_task_return_new_error (task, G_IO_ERROR,
5151 G_IO_ERROR_NOT_SUPPORTED,
5152 _("Operation not supported"));
5153 return;
5156 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5157 if (stream)
5158 g_task_return_pointer (task, stream, g_object_unref);
5159 else
5160 g_task_return_error (task, error);
5163 static void
5164 g_file_real_read_async (GFile *file,
5165 int io_priority,
5166 GCancellable *cancellable,
5167 GAsyncReadyCallback callback,
5168 gpointer user_data)
5170 GTask *task;
5172 task = g_task_new (file, cancellable, callback, user_data);
5173 g_task_set_priority (task, io_priority);
5174 g_task_run_in_thread (task, open_read_async_thread);
5175 g_object_unref (task);
5178 static GFileInputStream *
5179 g_file_real_read_finish (GFile *file,
5180 GAsyncResult *res,
5181 GError **error)
5183 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5185 return g_task_propagate_pointer (G_TASK (res), error);
5188 static void
5189 append_to_async_thread (GTask *task,
5190 gpointer source_object,
5191 gpointer task_data,
5192 GCancellable *cancellable)
5194 GFileIface *iface;
5195 GFileCreateFlags *data = task_data;
5196 GFileOutputStream *stream;
5197 GError *error = NULL;
5199 iface = G_FILE_GET_IFACE (source_object);
5201 stream = iface->append_to (G_FILE (source_object), *data, cancellable, &error);
5202 if (stream)
5203 g_task_return_pointer (task, stream, g_object_unref);
5204 else
5205 g_task_return_error (task, error);
5208 static void
5209 g_file_real_append_to_async (GFile *file,
5210 GFileCreateFlags flags,
5211 int io_priority,
5212 GCancellable *cancellable,
5213 GAsyncReadyCallback callback,
5214 gpointer user_data)
5216 GFileCreateFlags *data;
5217 GTask *task;
5219 data = g_new0 (GFileCreateFlags, 1);
5220 *data = flags;
5222 task = g_task_new (file, cancellable, callback, user_data);
5223 g_task_set_task_data (task, data, g_free);
5224 g_task_set_priority (task, io_priority);
5226 g_task_run_in_thread (task, append_to_async_thread);
5227 g_object_unref (task);
5230 static GFileOutputStream *
5231 g_file_real_append_to_finish (GFile *file,
5232 GAsyncResult *res,
5233 GError **error)
5235 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5237 return g_task_propagate_pointer (G_TASK (res), error);
5240 static void
5241 create_async_thread (GTask *task,
5242 gpointer source_object,
5243 gpointer task_data,
5244 GCancellable *cancellable)
5246 GFileIface *iface;
5247 GFileCreateFlags *data = task_data;
5248 GFileOutputStream *stream;
5249 GError *error = NULL;
5251 iface = G_FILE_GET_IFACE (source_object);
5253 stream = iface->create (G_FILE (source_object), *data, cancellable, &error);
5254 if (stream)
5255 g_task_return_pointer (task, stream, g_object_unref);
5256 else
5257 g_task_return_error (task, error);
5260 static void
5261 g_file_real_create_async (GFile *file,
5262 GFileCreateFlags flags,
5263 int io_priority,
5264 GCancellable *cancellable,
5265 GAsyncReadyCallback callback,
5266 gpointer user_data)
5268 GFileCreateFlags *data;
5269 GTask *task;
5271 data = g_new0 (GFileCreateFlags, 1);
5272 *data = flags;
5274 task = g_task_new (file, cancellable, callback, user_data);
5275 g_task_set_task_data (task, data, g_free);
5276 g_task_set_priority (task, io_priority);
5278 g_task_run_in_thread (task, create_async_thread);
5279 g_object_unref (task);
5282 static GFileOutputStream *
5283 g_file_real_create_finish (GFile *file,
5284 GAsyncResult *res,
5285 GError **error)
5287 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5289 return g_task_propagate_pointer (G_TASK (res), error);
5292 typedef struct {
5293 GFileOutputStream *stream;
5294 char *etag;
5295 gboolean make_backup;
5296 GFileCreateFlags flags;
5297 } ReplaceAsyncData;
5299 static void
5300 replace_async_data_free (ReplaceAsyncData *data)
5302 if (data->stream)
5303 g_object_unref (data->stream);
5304 g_free (data->etag);
5305 g_free (data);
5308 static void
5309 replace_async_thread (GTask *task,
5310 gpointer source_object,
5311 gpointer task_data,
5312 GCancellable *cancellable)
5314 GFileIface *iface;
5315 GFileOutputStream *stream;
5316 ReplaceAsyncData *data = task_data;
5317 GError *error = NULL;
5319 iface = G_FILE_GET_IFACE (source_object);
5321 stream = iface->replace (G_FILE (source_object),
5322 data->etag,
5323 data->make_backup,
5324 data->flags,
5325 cancellable,
5326 &error);
5328 if (stream)
5329 g_task_return_pointer (task, stream, g_object_unref);
5330 else
5331 g_task_return_error (task, error);
5334 static void
5335 g_file_real_replace_async (GFile *file,
5336 const char *etag,
5337 gboolean make_backup,
5338 GFileCreateFlags flags,
5339 int io_priority,
5340 GCancellable *cancellable,
5341 GAsyncReadyCallback callback,
5342 gpointer user_data)
5344 GTask *task;
5345 ReplaceAsyncData *data;
5347 data = g_new0 (ReplaceAsyncData, 1);
5348 data->etag = g_strdup (etag);
5349 data->make_backup = make_backup;
5350 data->flags = flags;
5352 task = g_task_new (file, cancellable, callback, user_data);
5353 g_task_set_task_data (task, data, (GDestroyNotify)replace_async_data_free);
5354 g_task_set_priority (task, io_priority);
5356 g_task_run_in_thread (task, replace_async_thread);
5357 g_object_unref (task);
5360 static GFileOutputStream *
5361 g_file_real_replace_finish (GFile *file,
5362 GAsyncResult *res,
5363 GError **error)
5365 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5367 return g_task_propagate_pointer (G_TASK (res), error);
5370 static void
5371 delete_async_thread (GTask *task,
5372 gpointer object,
5373 gpointer task_data,
5374 GCancellable *cancellable)
5376 GFile *file = object;
5377 GFileIface *iface;
5378 GError *error = NULL;
5380 iface = G_FILE_GET_IFACE (object);
5382 if (iface->delete_file (file,
5383 cancellable,
5384 &error))
5385 g_task_return_boolean (task, TRUE);
5386 else
5387 g_task_return_error (task, error);
5390 static void
5391 g_file_real_delete_async (GFile *file,
5392 int io_priority,
5393 GCancellable *cancellable,
5394 GAsyncReadyCallback callback,
5395 gpointer user_data)
5397 GTask *task;
5399 task = g_task_new (file, cancellable, callback, user_data);
5400 g_task_set_priority (task, io_priority);
5401 g_task_run_in_thread (task, delete_async_thread);
5402 g_object_unref (task);
5405 static gboolean
5406 g_file_real_delete_finish (GFile *file,
5407 GAsyncResult *res,
5408 GError **error)
5410 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5412 return g_task_propagate_boolean (G_TASK (res), error);
5415 static void
5416 open_readwrite_async_thread (GTask *task,
5417 gpointer object,
5418 gpointer task_data,
5419 GCancellable *cancellable)
5421 GFileIface *iface;
5422 GFileIOStream *stream;
5423 GError *error = NULL;
5425 iface = G_FILE_GET_IFACE (object);
5427 if (iface->open_readwrite == NULL)
5429 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5430 _("Operation not supported"));
5431 return;
5434 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5436 if (stream == NULL)
5437 g_task_return_error (task, error);
5438 else
5439 g_task_return_pointer (task, stream, g_object_unref);
5442 static void
5443 g_file_real_open_readwrite_async (GFile *file,
5444 int io_priority,
5445 GCancellable *cancellable,
5446 GAsyncReadyCallback callback,
5447 gpointer user_data)
5449 GTask *task;
5451 task = g_task_new (file, cancellable, callback, user_data);
5452 g_task_set_priority (task, io_priority);
5454 g_task_run_in_thread (task, open_readwrite_async_thread);
5455 g_object_unref (task);
5458 static GFileIOStream *
5459 g_file_real_open_readwrite_finish (GFile *file,
5460 GAsyncResult *res,
5461 GError **error)
5463 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5465 return g_task_propagate_pointer (G_TASK (res), error);
5468 static void
5469 create_readwrite_async_thread (GTask *task,
5470 gpointer object,
5471 gpointer task_data,
5472 GCancellable *cancellable)
5474 GFileIface *iface;
5475 GFileCreateFlags *data = task_data;
5476 GFileIOStream *stream;
5477 GError *error = NULL;
5479 iface = G_FILE_GET_IFACE (object);
5481 if (iface->create_readwrite == NULL)
5483 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
5484 _("Operation not supported"));
5485 return;
5488 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5490 if (stream == NULL)
5491 g_task_return_error (task, error);
5492 else
5493 g_task_return_pointer (task, stream, g_object_unref);
5496 static void
5497 g_file_real_create_readwrite_async (GFile *file,
5498 GFileCreateFlags flags,
5499 int io_priority,
5500 GCancellable *cancellable,
5501 GAsyncReadyCallback callback,
5502 gpointer user_data)
5504 GFileCreateFlags *data;
5505 GTask *task;
5507 data = g_new0 (GFileCreateFlags, 1);
5508 *data = flags;
5510 task = g_task_new (file, cancellable, callback, user_data);
5511 g_task_set_task_data (task, data, g_free);
5512 g_task_set_priority (task, io_priority);
5514 g_task_run_in_thread (task, create_readwrite_async_thread);
5515 g_object_unref (task);
5518 static GFileIOStream *
5519 g_file_real_create_readwrite_finish (GFile *file,
5520 GAsyncResult *res,
5521 GError **error)
5523 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5525 return g_task_propagate_pointer (G_TASK (res), error);
5528 typedef struct {
5529 char *etag;
5530 gboolean make_backup;
5531 GFileCreateFlags flags;
5532 } ReplaceRWAsyncData;
5534 static void
5535 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5537 g_free (data->etag);
5538 g_free (data);
5541 static void
5542 replace_readwrite_async_thread (GTask *task,
5543 gpointer object,
5544 gpointer task_data,
5545 GCancellable *cancellable)
5547 GFileIface *iface;
5548 GFileIOStream *stream;
5549 GError *error = NULL;
5550 ReplaceRWAsyncData *data = task_data;
5552 iface = G_FILE_GET_IFACE (object);
5554 stream = iface->replace_readwrite (G_FILE (object),
5555 data->etag,
5556 data->make_backup,
5557 data->flags,
5558 cancellable,
5559 &error);
5561 if (stream == NULL)
5562 g_task_return_error (task, error);
5563 else
5564 g_task_return_pointer (task, stream, g_object_unref);
5567 static void
5568 g_file_real_replace_readwrite_async (GFile *file,
5569 const char *etag,
5570 gboolean make_backup,
5571 GFileCreateFlags flags,
5572 int io_priority,
5573 GCancellable *cancellable,
5574 GAsyncReadyCallback callback,
5575 gpointer user_data)
5577 GTask *task;
5578 ReplaceRWAsyncData *data;
5580 data = g_new0 (ReplaceRWAsyncData, 1);
5581 data->etag = g_strdup (etag);
5582 data->make_backup = make_backup;
5583 data->flags = flags;
5585 task = g_task_new (file, cancellable, callback, user_data);
5586 g_task_set_task_data (task, data, (GDestroyNotify)replace_rw_async_data_free);
5587 g_task_set_priority (task, io_priority);
5589 g_task_run_in_thread (task, replace_readwrite_async_thread);
5590 g_object_unref (task);
5593 static GFileIOStream *
5594 g_file_real_replace_readwrite_finish (GFile *file,
5595 GAsyncResult *res,
5596 GError **error)
5598 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5600 return g_task_propagate_pointer (G_TASK (res), error);
5603 static void
5604 set_display_name_async_thread (GTask *task,
5605 gpointer object,
5606 gpointer task_data,
5607 GCancellable *cancellable)
5609 GError *error = NULL;
5610 char *name = task_data;
5611 GFile *file;
5613 file = g_file_set_display_name (G_FILE (object), name, cancellable, &error);
5615 if (file == NULL)
5616 g_task_return_error (task, error);
5617 else
5618 g_task_return_pointer (task, file, g_object_unref);
5621 static void
5622 g_file_real_set_display_name_async (GFile *file,
5623 const char *display_name,
5624 int io_priority,
5625 GCancellable *cancellable,
5626 GAsyncReadyCallback callback,
5627 gpointer user_data)
5629 GTask *task;
5631 task = g_task_new (file, cancellable, callback, user_data);
5632 g_task_set_task_data (task, g_strdup (display_name), g_free);
5633 g_task_set_priority (task, io_priority);
5635 g_task_run_in_thread (task, set_display_name_async_thread);
5636 g_object_unref (task);
5639 static GFile *
5640 g_file_real_set_display_name_finish (GFile *file,
5641 GAsyncResult *res,
5642 GError **error)
5644 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5646 return g_task_propagate_pointer (G_TASK (res), error);
5649 typedef struct {
5650 GFileQueryInfoFlags flags;
5651 GFileInfo *info;
5652 gboolean res;
5653 GError *error;
5654 } SetInfoAsyncData;
5656 static void
5657 set_info_data_free (SetInfoAsyncData *data)
5659 if (data->info)
5660 g_object_unref (data->info);
5661 if (data->error)
5662 g_error_free (data->error);
5663 g_free (data);
5666 static void
5667 set_info_async_thread (GTask *task,
5668 gpointer object,
5669 gpointer task_data,
5670 GCancellable *cancellable)
5672 SetInfoAsyncData *data = task_data;
5674 data->error = NULL;
5675 data->res = g_file_set_attributes_from_info (G_FILE (object),
5676 data->info,
5677 data->flags,
5678 cancellable,
5679 &data->error);
5682 static void
5683 g_file_real_set_attributes_async (GFile *file,
5684 GFileInfo *info,
5685 GFileQueryInfoFlags flags,
5686 int io_priority,
5687 GCancellable *cancellable,
5688 GAsyncReadyCallback callback,
5689 gpointer user_data)
5691 GTask *task;
5692 SetInfoAsyncData *data;
5694 data = g_new0 (SetInfoAsyncData, 1);
5695 data->info = g_file_info_dup (info);
5696 data->flags = flags;
5698 task = g_task_new (file, cancellable, callback, user_data);
5699 g_task_set_task_data (task, data, (GDestroyNotify)set_info_data_free);
5700 g_task_set_priority (task, io_priority);
5702 g_task_run_in_thread (task, set_info_async_thread);
5703 g_object_unref (task);
5706 static gboolean
5707 g_file_real_set_attributes_finish (GFile *file,
5708 GAsyncResult *res,
5709 GFileInfo **info,
5710 GError **error)
5712 SetInfoAsyncData *data;
5714 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5716 data = g_task_get_task_data (G_TASK (res));
5718 if (info)
5719 *info = g_object_ref (data->info);
5721 if (error != NULL && data->error)
5722 *error = g_error_copy (data->error);
5724 return data->res;
5727 static void
5728 find_enclosing_mount_async_thread (GTask *task,
5729 gpointer object,
5730 gpointer task_data,
5731 GCancellable *cancellable)
5733 GError *error = NULL;
5734 GMount *mount;
5736 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5738 if (mount == NULL)
5739 g_task_return_error (task, error);
5740 else
5741 g_task_return_pointer (task, mount, g_object_unref);
5744 static void
5745 g_file_real_find_enclosing_mount_async (GFile *file,
5746 int io_priority,
5747 GCancellable *cancellable,
5748 GAsyncReadyCallback callback,
5749 gpointer user_data)
5751 GTask *task;
5753 task = g_task_new (file, cancellable, callback, user_data);
5754 g_task_set_priority (task, io_priority);
5756 g_task_run_in_thread (task, find_enclosing_mount_async_thread);
5757 g_object_unref (task);
5760 static GMount *
5761 g_file_real_find_enclosing_mount_finish (GFile *file,
5762 GAsyncResult *res,
5763 GError **error)
5765 g_return_val_if_fail (g_task_is_valid (res, file), NULL);
5767 return g_task_propagate_pointer (G_TASK (res), error);
5771 typedef struct {
5772 GFile *source;
5773 GFile *destination;
5774 GFileCopyFlags flags;
5775 GFileProgressCallback progress_cb;
5776 gpointer progress_cb_data;
5777 } CopyAsyncData;
5779 static void
5780 copy_async_data_free (CopyAsyncData *data)
5782 g_object_unref (data->source);
5783 g_object_unref (data->destination);
5784 g_slice_free (CopyAsyncData, data);
5787 typedef struct {
5788 CopyAsyncData *data;
5789 goffset current_num_bytes;
5790 goffset total_num_bytes;
5791 } ProgressData;
5793 static gboolean
5794 copy_async_progress_in_main (gpointer user_data)
5796 ProgressData *progress = user_data;
5797 CopyAsyncData *data = progress->data;
5799 data->progress_cb (progress->current_num_bytes,
5800 progress->total_num_bytes,
5801 data->progress_cb_data);
5803 return FALSE;
5806 static void
5807 copy_async_progress_callback (goffset current_num_bytes,
5808 goffset total_num_bytes,
5809 gpointer user_data)
5811 GTask *task = user_data;
5812 CopyAsyncData *data = g_task_get_task_data (task);
5813 ProgressData *progress;
5815 progress = g_new (ProgressData, 1);
5816 progress->data = data;
5817 progress->current_num_bytes = current_num_bytes;
5818 progress->total_num_bytes = total_num_bytes;
5820 g_main_context_invoke_full (g_task_get_context (task),
5821 g_task_get_priority (task),
5822 copy_async_progress_in_main,
5823 progress,
5824 g_free);
5827 static void
5828 copy_async_thread (GTask *task,
5829 gpointer source,
5830 gpointer task_data,
5831 GCancellable *cancellable)
5833 CopyAsyncData *data = task_data;
5834 gboolean result;
5835 GError *error = NULL;
5837 result = g_file_copy (data->source,
5838 data->destination,
5839 data->flags,
5840 cancellable,
5841 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5842 task,
5843 &error);
5844 if (result)
5845 g_task_return_boolean (task, TRUE);
5846 else
5847 g_task_return_error (task, error);
5850 static void
5851 g_file_real_copy_async (GFile *source,
5852 GFile *destination,
5853 GFileCopyFlags flags,
5854 int io_priority,
5855 GCancellable *cancellable,
5856 GFileProgressCallback progress_callback,
5857 gpointer progress_callback_data,
5858 GAsyncReadyCallback callback,
5859 gpointer user_data)
5861 GTask *task;
5862 CopyAsyncData *data;
5864 data = g_slice_new (CopyAsyncData);
5865 data->source = g_object_ref (source);
5866 data->destination = g_object_ref (destination);
5867 data->flags = flags;
5868 data->progress_cb = progress_callback;
5869 data->progress_cb_data = progress_callback_data;
5871 task = g_task_new (source, cancellable, callback, user_data);
5872 g_task_set_task_data (task, data, (GDestroyNotify)copy_async_data_free);
5873 g_task_set_priority (task, io_priority);
5874 g_task_run_in_thread (task, copy_async_thread);
5875 g_object_unref (task);
5878 static gboolean
5879 g_file_real_copy_finish (GFile *file,
5880 GAsyncResult *res,
5881 GError **error)
5883 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
5885 return g_task_propagate_boolean (G_TASK (res), error);
5889 /********************************************
5890 * Default VFS operations *
5891 ********************************************/
5894 * g_file_new_for_path:
5895 * @path: a string containing a relative or absolute path.
5896 * The string must be encoded in the glib filename encoding.
5898 * Constructs a #GFile for a given path. This operation never
5899 * fails, but the returned object might not support any I/O
5900 * operation if @path is malformed.
5902 * Returns: (transfer full): a new #GFile for the given @path.
5903 * Free the returned object with g_object_unref().
5905 GFile *
5906 g_file_new_for_path (const char *path)
5908 g_return_val_if_fail (path != NULL, NULL);
5910 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5914 * g_file_new_for_uri:
5915 * @uri: a UTF-8 string containing a URI
5917 * Constructs a #GFile for a given URI. This operation never
5918 * fails, but the returned object might not support any I/O
5919 * operation if @uri is malformed or if the uri type is
5920 * not supported.
5922 * Returns: (transfer full): a new #GFile for the given @uri.
5923 * Free the returned object with g_object_unref().
5925 GFile *
5926 g_file_new_for_uri (const char *uri)
5928 g_return_val_if_fail (uri != NULL, NULL);
5930 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5934 * g_file_new_tmp:
5935 * @tmpl: (type filename) (allow-none): Template for the file
5936 * name, as in g_file_open_tmp(), or %NULL for a default template
5937 * @iostream: (out): on return, a #GFileIOStream for the created file
5938 * @error: a #GError, or %NULL
5940 * Opens a file in the preferred directory for temporary files (as
5941 * returned by g_get_tmp_dir()) and returns a #GFile and
5942 * #GFileIOStream pointing to it.
5944 * @tmpl should be a string in the GLib file name encoding
5945 * containing a sequence of six 'X' characters, and containing no
5946 * directory components. If it is %NULL, a default template is used.
5948 * Unlike the other #GFile constructors, this will return %NULL if
5949 * a temporary file could not be created.
5951 * Returns: (transfer full): a new #GFile.
5952 * Free the returned object with g_object_unref().
5954 * Since: 2.32
5956 GFile *
5957 g_file_new_tmp (const char *tmpl,
5958 GFileIOStream **iostream,
5959 GError **error)
5961 gint fd;
5962 gchar *path;
5963 GFile *file;
5964 GFileOutputStream *output;
5966 g_return_val_if_fail (iostream != NULL, NULL);
5968 fd = g_file_open_tmp (tmpl, &path, error);
5969 if (fd == -1)
5970 return NULL;
5972 file = g_file_new_for_path (path);
5974 output = _g_local_file_output_stream_new (fd);
5975 *iostream = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
5977 g_object_unref (output);
5978 g_free (path);
5980 return file;
5984 * g_file_parse_name:
5985 * @parse_name: a file name or path to be parsed
5987 * Constructs a #GFile with the given @parse_name (i.e. something
5988 * given by g_file_get_parse_name()). This operation never fails,
5989 * but the returned object might not support any I/O operation if
5990 * the @parse_name cannot be parsed.
5992 * Returns: (transfer full): a new #GFile.
5994 GFile *
5995 g_file_parse_name (const char *parse_name)
5997 g_return_val_if_fail (parse_name != NULL, NULL);
5999 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
6002 static gboolean
6003 is_valid_scheme_character (char c)
6005 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
6008 /* Following RFC 2396, valid schemes are built like:
6009 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
6011 static gboolean
6012 has_valid_scheme (const char *uri)
6014 const char *p;
6016 p = uri;
6018 if (!g_ascii_isalpha (*p))
6019 return FALSE;
6021 do {
6022 p++;
6023 } while (is_valid_scheme_character (*p));
6025 return *p == ':';
6028 static GFile *
6029 new_for_cmdline_arg (const gchar *arg,
6030 const gchar *cwd)
6032 GFile *file;
6033 char *filename;
6035 if (g_path_is_absolute (arg))
6036 return g_file_new_for_path (arg);
6038 if (has_valid_scheme (arg))
6039 return g_file_new_for_uri (arg);
6041 if (cwd == NULL)
6043 char *current_dir;
6045 current_dir = g_get_current_dir ();
6046 filename = g_build_filename (current_dir, arg, NULL);
6047 g_free (current_dir);
6049 else
6050 filename = g_build_filename (cwd, arg, NULL);
6052 file = g_file_new_for_path (filename);
6053 g_free (filename);
6055 return file;
6059 * g_file_new_for_commandline_arg:
6060 * @arg: a command line string
6062 * Creates a #GFile with the given argument from the command line.
6063 * The value of @arg can be either a URI, an absolute path or a
6064 * relative path resolved relative to the current working directory.
6065 * This operation never fails, but the returned object might not
6066 * support any I/O operation if @arg points to a malformed path.
6068 * Returns: (transfer full): a new #GFile.
6069 * Free the returned object with g_object_unref().
6071 GFile *
6072 g_file_new_for_commandline_arg (const char *arg)
6074 g_return_val_if_fail (arg != NULL, NULL);
6076 return new_for_cmdline_arg (arg, NULL);
6080 * g_file_new_for_commandline_arg_and_cwd:
6081 * @arg: a command line string
6082 * @cwd: the current working directory of the commandline
6084 * Creates a #GFile with the given argument from the command line.
6086 * This function is similar to g_file_new_for_commandline_arg() except
6087 * that it allows for passing the current working directory as an
6088 * argument instead of using the current working directory of the
6089 * process.
6091 * This is useful if the commandline argument was given in a context
6092 * other than the invocation of the current process.
6094 * See also g_application_command_line_create_file_for_arg().
6096 * Returns: (transfer full): a new #GFile
6098 * Since: 2.36
6100 GFile *
6101 g_file_new_for_commandline_arg_and_cwd (const gchar *arg,
6102 const gchar *cwd)
6104 g_return_val_if_fail (arg != NULL, NULL);
6105 g_return_val_if_fail (cwd != NULL, NULL);
6107 return new_for_cmdline_arg (arg, cwd);
6111 * g_file_mount_enclosing_volume:
6112 * @location: input #GFile
6113 * @flags: flags affecting the operation
6114 * @mount_operation: (allow-none): a #GMountOperation
6115 * or %NULL to avoid user interaction
6116 * @cancellable: (allow-none): optional #GCancellable object,
6117 * %NULL to ignore
6118 * @callback: (allow-none): a #GAsyncReadyCallback to call
6119 * when the request is satisfied, or %NULL
6120 * @user_data: the data to pass to callback function
6122 * Starts a @mount_operation, mounting the volume that contains
6123 * the file @location.
6125 * When this operation has completed, @callback will be called with
6126 * @user_user data, and the operation can be finalized with
6127 * g_file_mount_enclosing_volume_finish().
6129 * If @cancellable is not %NULL, then the operation can be cancelled by
6130 * triggering the cancellable object from another thread. If the operation
6131 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6133 void
6134 g_file_mount_enclosing_volume (GFile *location,
6135 GMountMountFlags flags,
6136 GMountOperation *mount_operation,
6137 GCancellable *cancellable,
6138 GAsyncReadyCallback callback,
6139 gpointer user_data)
6141 GFileIface *iface;
6143 g_return_if_fail (G_IS_FILE (location));
6145 iface = G_FILE_GET_IFACE (location);
6147 if (iface->mount_enclosing_volume == NULL)
6149 g_task_report_new_error (location, callback, user_data,
6150 g_file_mount_enclosing_volume,
6151 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6152 _("volume doesn't implement mount"));
6153 return;
6156 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6161 * g_file_mount_enclosing_volume_finish:
6162 * @location: input #GFile
6163 * @result: a #GAsyncResult
6164 * @error: a #GError, or %NULL
6166 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6168 * Returns: %TRUE if successful. If an error has occurred,
6169 * this function will return %FALSE and set @error
6170 * appropriately if present.
6172 gboolean
6173 g_file_mount_enclosing_volume_finish (GFile *location,
6174 GAsyncResult *result,
6175 GError **error)
6177 GFileIface *iface;
6179 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6180 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6182 if (g_async_result_legacy_propagate_error (result, error))
6183 return FALSE;
6184 else if (g_async_result_is_tagged (result, g_file_mount_enclosing_volume))
6185 return g_task_propagate_boolean (G_TASK (result), error);
6187 iface = G_FILE_GET_IFACE (location);
6189 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6192 /********************************************
6193 * Utility functions *
6194 ********************************************/
6197 * g_file_query_default_handler:
6198 * @file: a #GFile to open
6199 * @cancellable: optional #GCancellable object, %NULL to ignore
6200 * @error: a #GError, or %NULL
6202 * Returns the #GAppInfo that is registered as the default
6203 * application to handle the file specified by @file.
6205 * If @cancellable is not %NULL, then the operation can be cancelled by
6206 * triggering the cancellable object from another thread. If the operation
6207 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6209 * Returns: (transfer full): a #GAppInfo if the handle was found,
6210 * %NULL if there were errors.
6211 * When you are done with it, release it with g_object_unref()
6213 GAppInfo *
6214 g_file_query_default_handler (GFile *file,
6215 GCancellable *cancellable,
6216 GError **error)
6218 char *uri_scheme;
6219 const char *content_type;
6220 GAppInfo *appinfo;
6221 GFileInfo *info;
6222 char *path;
6224 uri_scheme = g_file_get_uri_scheme (file);
6225 if (uri_scheme && uri_scheme[0] != '\0')
6227 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6228 g_free (uri_scheme);
6230 if (appinfo != NULL)
6231 return appinfo;
6234 info = g_file_query_info (file,
6235 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6237 cancellable,
6238 error);
6239 if (info == NULL)
6240 return NULL;
6242 appinfo = NULL;
6244 content_type = g_file_info_get_content_type (info);
6245 if (content_type)
6247 /* Don't use is_native(), as we want to support fuse paths if available */
6248 path = g_file_get_path (file);
6249 appinfo = g_app_info_get_default_for_type (content_type,
6250 path == NULL);
6251 g_free (path);
6254 g_object_unref (info);
6256 if (appinfo != NULL)
6257 return appinfo;
6259 g_set_error_literal (error, G_IO_ERROR,
6260 G_IO_ERROR_NOT_SUPPORTED,
6261 _("No application is registered as handling this file"));
6262 return NULL;
6265 #define GET_CONTENT_BLOCK_SIZE 8192
6268 * g_file_load_contents:
6269 * @file: input #GFile
6270 * @cancellable: optional #GCancellable object, %NULL to ignore
6271 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6272 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6273 * or %NULL if the length is not needed
6274 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6275 * or %NULL if the entity tag is not needed
6276 * @error: a #GError, or %NULL
6278 * Loads the content of the file into memory. The data is always
6279 * zero-terminated, but this is not included in the resultant @length.
6280 * The returned @content should be freed with g_free() when no longer
6281 * needed.
6283 * If @cancellable is not %NULL, then the operation can be cancelled by
6284 * triggering the cancellable object from another thread. If the operation
6285 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6287 * Returns: %TRUE if the @file's contents were successfully loaded.
6288 * %FALSE if there were errors.
6290 gboolean
6291 g_file_load_contents (GFile *file,
6292 GCancellable *cancellable,
6293 char **contents,
6294 gsize *length,
6295 char **etag_out,
6296 GError **error)
6298 GFileInputStream *in;
6299 GByteArray *content;
6300 gsize pos;
6301 gssize res;
6302 GFileInfo *info;
6304 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6305 g_return_val_if_fail (contents != NULL, FALSE);
6307 in = g_file_read (file, cancellable, error);
6308 if (in == NULL)
6309 return FALSE;
6311 content = g_byte_array_new ();
6312 pos = 0;
6314 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6315 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6316 content->data + pos,
6317 GET_CONTENT_BLOCK_SIZE,
6318 cancellable, error)) > 0)
6320 pos += res;
6321 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6324 if (etag_out)
6326 *etag_out = NULL;
6328 info = g_file_input_stream_query_info (in,
6329 G_FILE_ATTRIBUTE_ETAG_VALUE,
6330 cancellable,
6331 NULL);
6332 if (info)
6334 *etag_out = g_strdup (g_file_info_get_etag (info));
6335 g_object_unref (info);
6339 /* Ignore errors on close */
6340 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6341 g_object_unref (in);
6343 if (res < 0)
6345 /* error is set already */
6346 g_byte_array_free (content, TRUE);
6347 return FALSE;
6350 if (length)
6351 *length = pos;
6353 /* Zero terminate (we got an extra byte allocated for this */
6354 content->data[pos] = 0;
6356 *contents = (char *)g_byte_array_free (content, FALSE);
6358 return TRUE;
6361 typedef struct {
6362 GTask *task;
6363 GFileReadMoreCallback read_more_callback;
6364 GByteArray *content;
6365 gsize pos;
6366 char *etag;
6367 } LoadContentsData;
6370 static void
6371 load_contents_data_free (LoadContentsData *data)
6373 if (data->content)
6374 g_byte_array_free (data->content, TRUE);
6375 g_free (data->etag);
6376 g_free (data);
6379 static void
6380 load_contents_close_callback (GObject *obj,
6381 GAsyncResult *close_res,
6382 gpointer user_data)
6384 GInputStream *stream = G_INPUT_STREAM (obj);
6385 LoadContentsData *data = user_data;
6387 /* Ignore errors here, we're only reading anyway */
6388 g_input_stream_close_finish (stream, close_res, NULL);
6389 g_object_unref (stream);
6391 g_task_return_boolean (data->task, TRUE);
6392 g_object_unref (data->task);
6395 static void
6396 load_contents_fstat_callback (GObject *obj,
6397 GAsyncResult *stat_res,
6398 gpointer user_data)
6400 GInputStream *stream = G_INPUT_STREAM (obj);
6401 LoadContentsData *data = user_data;
6402 GFileInfo *info;
6404 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6405 stat_res, NULL);
6406 if (info)
6408 data->etag = g_strdup (g_file_info_get_etag (info));
6409 g_object_unref (info);
6412 g_input_stream_close_async (stream, 0,
6413 g_task_get_cancellable (data->task),
6414 load_contents_close_callback, data);
6417 static void
6418 load_contents_read_callback (GObject *obj,
6419 GAsyncResult *read_res,
6420 gpointer user_data)
6422 GInputStream *stream = G_INPUT_STREAM (obj);
6423 LoadContentsData *data = user_data;
6424 GError *error = NULL;
6425 gssize read_size;
6427 read_size = g_input_stream_read_finish (stream, read_res, &error);
6429 if (read_size < 0)
6431 /* EOF, close the file */
6432 g_task_return_error (data->task, error);
6433 g_input_stream_close_async (stream, 0,
6434 g_task_get_cancellable (data->task),
6435 load_contents_close_callback, data);
6437 else if (read_size == 0)
6439 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6440 G_FILE_ATTRIBUTE_ETAG_VALUE,
6442 g_task_get_cancellable (data->task),
6443 load_contents_fstat_callback,
6444 data);
6446 else if (read_size > 0)
6448 data->pos += read_size;
6450 g_byte_array_set_size (data->content,
6451 data->pos + GET_CONTENT_BLOCK_SIZE);
6454 if (data->read_more_callback &&
6455 !data->read_more_callback ((char *)data->content->data, data->pos,
6456 g_async_result_get_user_data (G_ASYNC_RESULT (data->task))))
6457 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6458 G_FILE_ATTRIBUTE_ETAG_VALUE,
6460 g_task_get_cancellable (data->task),
6461 load_contents_fstat_callback,
6462 data);
6463 else
6464 g_input_stream_read_async (stream,
6465 data->content->data + data->pos,
6466 GET_CONTENT_BLOCK_SIZE,
6468 g_task_get_cancellable (data->task),
6469 load_contents_read_callback,
6470 data);
6474 static void
6475 load_contents_open_callback (GObject *obj,
6476 GAsyncResult *open_res,
6477 gpointer user_data)
6479 GFile *file = G_FILE (obj);
6480 GFileInputStream *stream;
6481 LoadContentsData *data = user_data;
6482 GError *error = NULL;
6484 stream = g_file_read_finish (file, open_res, &error);
6486 if (stream)
6488 g_byte_array_set_size (data->content,
6489 data->pos + GET_CONTENT_BLOCK_SIZE);
6490 g_input_stream_read_async (G_INPUT_STREAM (stream),
6491 data->content->data + data->pos,
6492 GET_CONTENT_BLOCK_SIZE,
6494 g_task_get_cancellable (data->task),
6495 load_contents_read_callback,
6496 data);
6498 else
6500 g_task_return_error (data->task, error);
6501 g_object_unref (data->task);
6506 * g_file_load_partial_contents_async: (skip)
6507 * @file: input #GFile
6508 * @cancellable: optional #GCancellable object, %NULL to ignore
6509 * @read_more_callback: a #GFileReadMoreCallback to receive partial data
6510 * and to specify whether further data should be read
6511 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6512 * @user_data: the data to pass to the callback functions
6514 * Reads the partial contents of a file. A #GFileReadMoreCallback should
6515 * be used to stop reading from the file when appropriate, else this
6516 * function will behave exactly as g_file_load_contents_async(). This
6517 * operation can be finished by g_file_load_partial_contents_finish().
6519 * Users of this function should be aware that @user_data is passed to
6520 * both the @read_more_callback and the @callback.
6522 * If @cancellable is not %NULL, then the operation can be cancelled by
6523 * triggering the cancellable object from another thread. If the operation
6524 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6526 void
6527 g_file_load_partial_contents_async (GFile *file,
6528 GCancellable *cancellable,
6529 GFileReadMoreCallback read_more_callback,
6530 GAsyncReadyCallback callback,
6531 gpointer user_data)
6533 LoadContentsData *data;
6535 g_return_if_fail (G_IS_FILE (file));
6537 data = g_new0 (LoadContentsData, 1);
6538 data->read_more_callback = read_more_callback;
6539 data->content = g_byte_array_new ();
6541 data->task = g_task_new (file, cancellable, callback, user_data);
6542 g_task_set_task_data (data->task, data, (GDestroyNotify)load_contents_data_free);
6544 g_file_read_async (file,
6546 g_task_get_cancellable (data->task),
6547 load_contents_open_callback,
6548 data);
6552 * g_file_load_partial_contents_finish:
6553 * @file: input #GFile
6554 * @res: a #GAsyncResult
6555 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6556 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6557 * or %NULL if the length is not needed
6558 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6559 * or %NULL if the entity tag is not needed
6560 * @error: a #GError, or %NULL
6562 * Finishes an asynchronous partial load operation that was started
6563 * with g_file_load_partial_contents_async(). The data is always
6564 * zero-terminated, but this is not included in the resultant @length.
6565 * The returned @content should be freed with g_free() when no longer
6566 * needed.
6568 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6569 * present, it will be set appropriately.
6571 gboolean
6572 g_file_load_partial_contents_finish (GFile *file,
6573 GAsyncResult *res,
6574 char **contents,
6575 gsize *length,
6576 char **etag_out,
6577 GError **error)
6579 GTask *task;
6580 LoadContentsData *data;
6582 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6583 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6584 g_return_val_if_fail (contents != NULL, FALSE);
6586 task = G_TASK (res);
6588 if (!g_task_propagate_boolean (task, error))
6590 if (length)
6591 *length = 0;
6592 return FALSE;
6595 data = g_task_get_task_data (task);
6597 if (length)
6598 *length = data->pos;
6600 if (etag_out)
6602 *etag_out = data->etag;
6603 data->etag = NULL;
6606 /* Zero terminate */
6607 g_byte_array_set_size (data->content, data->pos + 1);
6608 data->content->data[data->pos] = 0;
6610 *contents = (char *)g_byte_array_free (data->content, FALSE);
6611 data->content = NULL;
6613 return TRUE;
6617 * g_file_load_contents_async:
6618 * @file: input #GFile
6619 * @cancellable: optional #GCancellable object, %NULL to ignore
6620 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6621 * @user_data: the data to pass to callback function
6623 * Starts an asynchronous load of the @file's contents.
6625 * For more details, see g_file_load_contents() which is
6626 * the synchronous version of this call.
6628 * When the load operation has completed, @callback will be called
6629 * with @user data. To finish the operation, call
6630 * g_file_load_contents_finish() with the #GAsyncResult returned by
6631 * the @callback.
6633 * If @cancellable is not %NULL, then the operation can be cancelled by
6634 * triggering the cancellable object from another thread. If the operation
6635 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6637 void
6638 g_file_load_contents_async (GFile *file,
6639 GCancellable *cancellable,
6640 GAsyncReadyCallback callback,
6641 gpointer user_data)
6643 g_file_load_partial_contents_async (file,
6644 cancellable,
6645 NULL,
6646 callback, user_data);
6650 * g_file_load_contents_finish:
6651 * @file: input #GFile
6652 * @res: a #GAsyncResult
6653 * @contents: (out) (transfer full) (element-type guint8) (array length=length): a location to place the contents of the file
6654 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6655 * or %NULL if the length is not needed
6656 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6657 * or %NULL if the entity tag is not needed
6658 * @error: a #GError, or %NULL
6660 * Finishes an asynchronous load of the @file's contents.
6661 * The contents are placed in @contents, and @length is set to the
6662 * size of the @contents string. The @content should be freed with
6663 * g_free() when no longer needed. If @etag_out is present, it will be
6664 * set to the new entity tag for the @file.
6666 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6667 * present, it will be set appropriately.
6669 gboolean
6670 g_file_load_contents_finish (GFile *file,
6671 GAsyncResult *res,
6672 char **contents,
6673 gsize *length,
6674 char **etag_out,
6675 GError **error)
6677 return g_file_load_partial_contents_finish (file,
6678 res,
6679 contents,
6680 length,
6681 etag_out,
6682 error);
6686 * g_file_replace_contents:
6687 * @file: input #GFile
6688 * @contents: (element-type guint8) (array length=length): a string containing the new contents for @file
6689 * @length: the length of @contents in bytes
6690 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6691 * for the document, or %NULL
6692 * @make_backup: %TRUE if a backup should be created
6693 * @flags: a set of #GFileCreateFlags
6694 * @new_etag: (allow-none) (out): a location to a new <link linkend="gfile-etag">entity tag</link>
6695 * for the document. This should be freed with g_free() when no longer
6696 * needed, or %NULL
6697 * @cancellable: optional #GCancellable object, %NULL to ignore
6698 * @error: a #GError, or %NULL
6700 * Replaces the contents of @file with @contents of @length bytes.
6702 * If @etag is specified (not %NULL), any existing file must have that etag,
6703 * or the error %G_IO_ERROR_WRONG_ETAG will be returned.
6705 * If @make_backup is %TRUE, this function will attempt to make a backup
6706 * of @file.
6708 * If @cancellable is not %NULL, then the operation can be cancelled by
6709 * triggering the cancellable object from another thread. If the operation
6710 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6712 * The returned @new_etag can be used to verify that the file hasn't
6713 * changed the next time it is saved over.
6715 * Returns: %TRUE if successful. If an error has occurred, this function
6716 * will return %FALSE and set @error appropriately if present.
6718 gboolean
6719 g_file_replace_contents (GFile *file,
6720 const char *contents,
6721 gsize length,
6722 const char *etag,
6723 gboolean make_backup,
6724 GFileCreateFlags flags,
6725 char **new_etag,
6726 GCancellable *cancellable,
6727 GError **error)
6729 GFileOutputStream *out;
6730 gsize pos, remainder;
6731 gssize res;
6732 gboolean ret;
6734 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6735 g_return_val_if_fail (contents != NULL, FALSE);
6737 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6738 if (out == NULL)
6739 return FALSE;
6741 pos = 0;
6742 remainder = length;
6743 while (remainder > 0 &&
6744 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6745 contents + pos,
6746 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6747 cancellable,
6748 error)) > 0)
6750 pos += res;
6751 remainder -= res;
6754 if (remainder > 0 && res < 0)
6756 /* Ignore errors on close */
6757 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6758 g_object_unref (out);
6760 /* error is set already */
6761 return FALSE;
6764 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6766 if (new_etag)
6767 *new_etag = g_file_output_stream_get_etag (out);
6769 g_object_unref (out);
6771 return ret;
6774 typedef struct {
6775 GTask *task;
6776 const char *content;
6777 gsize length;
6778 gsize pos;
6779 char *etag;
6780 gboolean failed;
6781 } ReplaceContentsData;
6783 static void
6784 replace_contents_data_free (ReplaceContentsData *data)
6786 g_free (data->etag);
6787 g_free (data);
6790 static void
6791 replace_contents_close_callback (GObject *obj,
6792 GAsyncResult *close_res,
6793 gpointer user_data)
6795 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6796 ReplaceContentsData *data = user_data;
6798 /* Ignore errors here, we're only reading anyway */
6799 g_output_stream_close_finish (stream, close_res, NULL);
6800 g_object_unref (stream);
6802 if (!data->failed)
6804 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6805 g_task_return_boolean (data->task, TRUE);
6807 g_object_unref (data->task);
6810 static void
6811 replace_contents_write_callback (GObject *obj,
6812 GAsyncResult *read_res,
6813 gpointer user_data)
6815 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6816 ReplaceContentsData *data = user_data;
6817 GError *error = NULL;
6818 gssize write_size;
6820 write_size = g_output_stream_write_finish (stream, read_res, &error);
6822 if (write_size <= 0)
6824 /* Error or EOF, close the file */
6825 if (write_size < 0)
6827 data->failed = TRUE;
6828 g_task_return_error (data->task, error);
6830 g_output_stream_close_async (stream, 0,
6831 g_task_get_cancellable (data->task),
6832 replace_contents_close_callback, data);
6834 else if (write_size > 0)
6836 data->pos += write_size;
6838 if (data->pos >= data->length)
6839 g_output_stream_close_async (stream, 0,
6840 g_task_get_cancellable (data->task),
6841 replace_contents_close_callback, data);
6842 else
6843 g_output_stream_write_async (stream,
6844 data->content + data->pos,
6845 data->length - data->pos,
6847 g_task_get_cancellable (data->task),
6848 replace_contents_write_callback,
6849 data);
6853 static void
6854 replace_contents_open_callback (GObject *obj,
6855 GAsyncResult *open_res,
6856 gpointer user_data)
6858 GFile *file = G_FILE (obj);
6859 GFileOutputStream *stream;
6860 ReplaceContentsData *data = user_data;
6861 GError *error = NULL;
6863 stream = g_file_replace_finish (file, open_res, &error);
6865 if (stream)
6867 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6868 data->content + data->pos,
6869 data->length - data->pos,
6871 g_task_get_cancellable (data->task),
6872 replace_contents_write_callback,
6873 data);
6875 else
6877 g_task_return_error (data->task, error);
6878 g_object_unref (data->task);
6883 * g_file_replace_contents_async:
6884 * @file: input #GFile
6885 * @contents: (element-type guint8) (array length=length): string of contents to replace the file with
6886 * @length: the length of @contents in bytes
6887 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6888 * @make_backup: %TRUE if a backup should be created
6889 * @flags: a set of #GFileCreateFlags
6890 * @cancellable: optional #GCancellable object, %NULL to ignore
6891 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6892 * @user_data: the data to pass to callback function
6894 * Starts an asynchronous replacement of @file with the given
6895 * @contents of @length bytes. @etag will replace the document's
6896 * current entity tag.
6898 * When this operation has completed, @callback will be called with
6899 * @user_user data, and the operation can be finalized with
6900 * g_file_replace_contents_finish().
6902 * If @cancellable is not %NULL, then the operation can be cancelled by
6903 * triggering the cancellable object from another thread. If the operation
6904 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6906 * If @make_backup is %TRUE, this function will attempt to
6907 * make a backup of @file.
6909 void
6910 g_file_replace_contents_async (GFile *file,
6911 const char *contents,
6912 gsize length,
6913 const char *etag,
6914 gboolean make_backup,
6915 GFileCreateFlags flags,
6916 GCancellable *cancellable,
6917 GAsyncReadyCallback callback,
6918 gpointer user_data)
6920 ReplaceContentsData *data;
6922 g_return_if_fail (G_IS_FILE (file));
6923 g_return_if_fail (contents != NULL);
6925 data = g_new0 (ReplaceContentsData, 1);
6927 data->content = contents;
6928 data->length = length;
6930 data->task = g_task_new (file, cancellable, callback, user_data);
6931 g_task_set_task_data (data->task, data, (GDestroyNotify)replace_contents_data_free);
6933 g_file_replace_async (file,
6934 etag,
6935 make_backup,
6936 flags,
6938 g_task_get_cancellable (data->task),
6939 replace_contents_open_callback,
6940 data);
6944 * g_file_replace_contents_finish:
6945 * @file: input #GFile
6946 * @res: a #GAsyncResult
6947 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6948 * for the document. This should be freed with g_free() when it is no
6949 * longer needed, or %NULL
6950 * @error: a #GError, or %NULL
6952 * Finishes an asynchronous replace of the given @file. See
6953 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6954 * tag for the document, if present.
6956 * Returns: %TRUE on success, %FALSE on failure.
6958 gboolean
6959 g_file_replace_contents_finish (GFile *file,
6960 GAsyncResult *res,
6961 char **new_etag,
6962 GError **error)
6964 GTask *task;
6965 ReplaceContentsData *data;
6967 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6968 g_return_val_if_fail (g_task_is_valid (res, file), FALSE);
6970 task = G_TASK (res);
6972 if (!g_task_propagate_boolean (task, error))
6973 return FALSE;
6975 data = g_task_get_task_data (task);
6977 if (new_etag)
6979 *new_etag = data->etag;
6980 data->etag = NULL; /* Take ownership */
6983 return TRUE;
6987 * g_file_start_mountable:
6988 * @file: input #GFile
6989 * @flags: flags affecting the operation
6990 * @start_operation: (allow-none): a #GMountOperation, or %NULL to avoid user interaction
6991 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
6992 * @callback: (allow-none): a #GAsyncReadyCallback to call when the request is satisfied, or %NULL
6993 * @user_data: the data to pass to callback function
6995 * Starts a file of type #G_FILE_TYPE_MOUNTABLE.
6996 * Using @start_operation, you can request callbacks when, for instance,
6997 * passwords are needed during authentication.
6999 * If @cancellable is not %NULL, then the operation can be cancelled by
7000 * triggering the cancellable object from another thread. If the operation
7001 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7003 * When the operation is finished, @callback will be called.
7004 * You can then call g_file_mount_mountable_finish() to get
7005 * the result of the operation.
7007 * Since: 2.22
7009 void
7010 g_file_start_mountable (GFile *file,
7011 GDriveStartFlags flags,
7012 GMountOperation *start_operation,
7013 GCancellable *cancellable,
7014 GAsyncReadyCallback callback,
7015 gpointer user_data)
7017 GFileIface *iface;
7019 g_return_if_fail (G_IS_FILE (file));
7021 iface = G_FILE_GET_IFACE (file);
7023 if (iface->start_mountable == NULL)
7025 g_task_report_new_error (file, callback, user_data,
7026 g_file_start_mountable,
7027 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7028 _("Operation not supported"));
7029 return;
7032 (* iface->start_mountable) (file,
7033 flags,
7034 start_operation,
7035 cancellable,
7036 callback,
7037 user_data);
7041 * g_file_start_mountable_finish:
7042 * @file: input #GFile
7043 * @result: a #GAsyncResult
7044 * @error: a #GError, or %NULL
7046 * Finishes a start operation. See g_file_start_mountable() for details.
7048 * Finish an asynchronous start operation that was started
7049 * with g_file_start_mountable().
7051 * Returns: %TRUE if the operation finished successfully. %FALSE
7052 * otherwise.
7054 * Since: 2.22
7056 gboolean
7057 g_file_start_mountable_finish (GFile *file,
7058 GAsyncResult *result,
7059 GError **error)
7061 GFileIface *iface;
7063 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7064 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7066 if (g_async_result_legacy_propagate_error (result, error))
7067 return FALSE;
7068 else if (g_async_result_is_tagged (result, g_file_start_mountable))
7069 return g_task_propagate_boolean (G_TASK (result), error);
7071 iface = G_FILE_GET_IFACE (file);
7072 return (* iface->start_mountable_finish) (file, result, error);
7076 * g_file_stop_mountable:
7077 * @file: input #GFile
7078 * @flags: flags affecting the operation
7079 * @mount_operation: (allow-none): a #GMountOperation,
7080 * or %NULL to avoid user interaction.
7081 * @cancellable: (allow-none): optional #GCancellable object,
7082 * %NULL to ignore
7083 * @callback: (allow-none): a #GAsyncReadyCallback to call
7084 * when the request is satisfied, or %NULL
7085 * @user_data: the data to pass to callback function
7087 * Stops a file of type #G_FILE_TYPE_MOUNTABLE.
7089 * If @cancellable is not %NULL, then the operation can be cancelled by
7090 * triggering the cancellable object from another thread. If the operation
7091 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7093 * When the operation is finished, @callback will be called.
7094 * You can then call g_file_stop_mountable_finish() to get
7095 * the result of the operation.
7097 * Since: 2.22
7099 void
7100 g_file_stop_mountable (GFile *file,
7101 GMountUnmountFlags flags,
7102 GMountOperation *mount_operation,
7103 GCancellable *cancellable,
7104 GAsyncReadyCallback callback,
7105 gpointer user_data)
7107 GFileIface *iface;
7109 g_return_if_fail (G_IS_FILE (file));
7111 iface = G_FILE_GET_IFACE (file);
7113 if (iface->stop_mountable == NULL)
7115 g_task_report_new_error (file, callback, user_data,
7116 g_file_stop_mountable,
7117 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7118 _("Operation not supported"));
7119 return;
7122 (* iface->stop_mountable) (file,
7123 flags,
7124 mount_operation,
7125 cancellable,
7126 callback,
7127 user_data);
7131 * g_file_stop_mountable_finish:
7132 * @file: input #GFile
7133 * @result: a #GAsyncResult
7134 * @error: a #GError, or %NULL
7136 * Finishes an stop operation, see g_file_stop_mountable() for details.
7138 * Finish an asynchronous stop operation that was started
7139 * with g_file_stop_mountable().
7141 * Returns: %TRUE if the operation finished successfully.
7142 * %FALSE otherwise.
7144 * Since: 2.22
7146 gboolean
7147 g_file_stop_mountable_finish (GFile *file,
7148 GAsyncResult *result,
7149 GError **error)
7151 GFileIface *iface;
7153 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7154 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7156 if (g_async_result_legacy_propagate_error (result, error))
7157 return FALSE;
7158 else if (g_async_result_is_tagged (result, g_file_stop_mountable))
7159 return g_task_propagate_boolean (G_TASK (result), error);
7161 iface = G_FILE_GET_IFACE (file);
7162 return (* iface->stop_mountable_finish) (file, result, error);
7166 * g_file_poll_mountable:
7167 * @file: input #GFile
7168 * @cancellable: optional #GCancellable object, %NULL to ignore
7169 * @callback: (allow-none): a #GAsyncReadyCallback to call
7170 * when the request is satisfied, or %NULL
7171 * @user_data: the data to pass to callback function
7173 * Polls a file of type #G_FILE_TYPE_MOUNTABLE.
7175 * If @cancellable is not %NULL, then the operation can be cancelled by
7176 * triggering the cancellable object from another thread. If the operation
7177 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7179 * When the operation is finished, @callback will be called.
7180 * You can then call g_file_mount_mountable_finish() to get
7181 * the result of the operation.
7183 * Since: 2.22
7185 void
7186 g_file_poll_mountable (GFile *file,
7187 GCancellable *cancellable,
7188 GAsyncReadyCallback callback,
7189 gpointer user_data)
7191 GFileIface *iface;
7193 g_return_if_fail (G_IS_FILE (file));
7195 iface = G_FILE_GET_IFACE (file);
7197 if (iface->poll_mountable == NULL)
7199 g_task_report_new_error (file, callback, user_data,
7200 g_file_poll_mountable,
7201 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
7202 _("Operation not supported"));
7203 return;
7206 (* iface->poll_mountable) (file,
7207 cancellable,
7208 callback,
7209 user_data);
7213 * g_file_poll_mountable_finish:
7214 * @file: input #GFile
7215 * @result: a #GAsyncResult
7216 * @error: a #GError, or %NULL
7218 * Finishes a poll operation. See g_file_poll_mountable() for details.
7220 * Finish an asynchronous poll operation that was polled
7221 * with g_file_poll_mountable().
7223 * Returns: %TRUE if the operation finished successfully. %FALSE
7224 * otherwise.
7226 * Since: 2.22
7228 gboolean
7229 g_file_poll_mountable_finish (GFile *file,
7230 GAsyncResult *result,
7231 GError **error)
7233 GFileIface *iface;
7235 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7236 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7238 if (g_async_result_legacy_propagate_error (result, error))
7239 return FALSE;
7240 else if (g_async_result_is_tagged (result, g_file_poll_mountable))
7241 return g_task_propagate_boolean (G_TASK (result), error);
7243 iface = G_FILE_GET_IFACE (file);
7244 return (* iface->poll_mountable_finish) (file, result, error);
7248 * g_file_supports_thread_contexts:
7249 * @file: a #GFile
7251 * Checks if @file supports <link
7252 * linkend="g-main-context-push-thread-default-context">thread-default
7253 * contexts</link>. If this returns %FALSE, you cannot perform
7254 * asynchronous operations on @file in a thread that has a
7255 * thread-default context.
7257 * Returns: Whether or not @file supports thread-default contexts.
7259 * Since: 2.22
7261 gboolean
7262 g_file_supports_thread_contexts (GFile *file)
7264 GFileIface *iface;
7266 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7268 iface = G_FILE_GET_IFACE (file);
7269 return iface->supports_thread_contexts;