Work around deadlock in unix-streams test
[glib.git] / gio / gfile.c
blob9fd10ca1d4bd9209841ae40dfd3019c052cac299
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 #define _GNU_SOURCE
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #endif
33 #include <string.h>
34 #include <sys/types.h>
35 #ifdef HAVE_PWD_H
36 #include <pwd.h>
37 #endif
38 #include "gfile.h"
39 #include "gvfs.h"
40 #include "gioscheduler.h"
41 #include "gsimpleasyncresult.h"
42 #include "gfileattribute-priv.h"
43 #include "gfiledescriptorbased.h"
44 #include "gpollfilemonitor.h"
45 #include "gappinfo.h"
46 #include "gfileinputstream.h"
47 #include "gfileoutputstream.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. #GFile<!-- -->s are lightweight, immutable
62 * objects 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 file. All
64 * file content I/O is implemented as streaming operations (see #GInputStream and
65 * #GOutputStream).
67 * To construct a #GFile, you can use:
68 * g_file_new_for_path() if you have a path.
69 * g_file_new_for_uri() if you have a URI.
70 * g_file_new_for_commandline_arg() for a command line argument.
71 * g_file_parse_name() from a utf8 string gotten from g_file_get_parse_name().
73 * One way to think of a #GFile is as an abstraction of a pathname. For normal
74 * files the system pathname is what is stored internally, but as #GFile<!-- -->s
75 * are extensible it could also be something else that corresponds to a pathname
76 * in a userspace implementation of a filesystem.
78 * #GFile<!-- -->s make up hierarchies of directories and files that correspond to the
79 * files on a filesystem. You can move through the file system with #GFile using
80 * g_file_get_parent() to get an identifier for the parent directory, g_file_get_child()
81 * to get a child within a directory, g_file_resolve_relative_path() to resolve a relative
82 * path between two #GFile<!-- -->s. There can be multiple hierarchies, so you may not
83 * end up at the same root if you repeatedly call g_file_get_parent() on two different
84 * files.
86 * All #GFile<!-- -->s have a basename (get with g_file_get_basename()). These names
87 * are byte strings that are used to identify the file on the filesystem (relative to
88 * its parent directory) and there is no guarantees that they have any particular charset
89 * encoding or even make any sense at all. If you want to use filenames in a user
90 * interface you should use the display name that you can get by requesting the
91 * %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME attribute with g_file_query_info().
92 * This is guaranteed to be in utf8 and can be used in a user interface. But always
93 * store the real basename or the #GFile to use to actually access the file, because
94 * there is no way to go from a display name to the actual name.
96 * Using #GFile as an identifier has the same weaknesses as using a path in that
97 * there may be multiple aliases for the same file. For instance, hard or
98 * soft links may cause two different #GFile<!-- -->s to refer to the same file.
99 * Other possible causes for aliases are: case insensitive filesystems, short
100 * and long names on Fat/NTFS, or bind mounts in Linux. If you want to check if
101 * two #GFile<!-- -->s point to the same file you can query for the
102 * %G_FILE_ATTRIBUTE_ID_FILE attribute. Note that #GFile does some trivial
103 * canonicalization of pathnames passed in, so that trivial differences in the
104 * path string used at creation (duplicated slashes, slash at end of path, "."
105 * or ".." path segments, etc) does not create different #GFile<!-- -->s.
107 * Many #GFile operations have both synchronous and asynchronous versions
108 * to suit your application. Asynchronous versions of synchronous functions
109 * simply have _async() appended to their function names. The asynchronous
110 * I/O functions call a #GAsyncReadyCallback which is then used to finalize
111 * the operation, producing a GAsyncResult which is then passed to the
112 * function's matching _finish() operation.
114 * Some #GFile operations do not have synchronous analogs, as they may
115 * take a very long time to finish, and blocking may leave an application
116 * unusable. Notable cases include:
117 * g_file_mount_mountable() to mount a mountable file.
118 * g_file_unmount_mountable_with_operation() to unmount a mountable file.
119 * g_file_eject_mountable_with_operation() to eject a mountable file.
121 * <para id="gfile-etag"><indexterm><primary>entity tag</primary></indexterm>
122 * One notable feature of #GFile<!-- -->s are entity tags, or "etags" for
123 * short. Entity tags are somewhat like a more abstract version of the
124 * traditional mtime, and can be used to quickly determine if the file has
125 * been modified from the version on the file system. See the HTTP 1.1
126 * <ulink url="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">specification</ulink>
127 * for HTTP Etag headers, which are a very similar concept.
128 * </para>
131 static void g_file_real_query_info_async (GFile *file,
132 const char *attributes,
133 GFileQueryInfoFlags flags,
134 int io_priority,
135 GCancellable *cancellable,
136 GAsyncReadyCallback callback,
137 gpointer user_data);
138 static GFileInfo * g_file_real_query_info_finish (GFile *file,
139 GAsyncResult *res,
140 GError **error);
141 static void g_file_real_query_filesystem_info_async (GFile *file,
142 const char *attributes,
143 int io_priority,
144 GCancellable *cancellable,
145 GAsyncReadyCallback callback,
146 gpointer user_data);
147 static GFileInfo * g_file_real_query_filesystem_info_finish (GFile *file,
148 GAsyncResult *res,
149 GError **error);
150 static void g_file_real_enumerate_children_async (GFile *file,
151 const char *attributes,
152 GFileQueryInfoFlags flags,
153 int io_priority,
154 GCancellable *cancellable,
155 GAsyncReadyCallback callback,
156 gpointer user_data);
157 static GFileEnumerator * g_file_real_enumerate_children_finish (GFile *file,
158 GAsyncResult *res,
159 GError **error);
160 static void g_file_real_read_async (GFile *file,
161 int io_priority,
162 GCancellable *cancellable,
163 GAsyncReadyCallback callback,
164 gpointer user_data);
165 static GFileInputStream * g_file_real_read_finish (GFile *file,
166 GAsyncResult *res,
167 GError **error);
168 static void g_file_real_append_to_async (GFile *file,
169 GFileCreateFlags flags,
170 int io_priority,
171 GCancellable *cancellable,
172 GAsyncReadyCallback callback,
173 gpointer user_data);
174 static GFileOutputStream *g_file_real_append_to_finish (GFile *file,
175 GAsyncResult *res,
176 GError **error);
177 static void g_file_real_create_async (GFile *file,
178 GFileCreateFlags flags,
179 int io_priority,
180 GCancellable *cancellable,
181 GAsyncReadyCallback callback,
182 gpointer user_data);
183 static GFileOutputStream *g_file_real_create_finish (GFile *file,
184 GAsyncResult *res,
185 GError **error);
186 static void g_file_real_replace_async (GFile *file,
187 const char *etag,
188 gboolean make_backup,
189 GFileCreateFlags flags,
190 int io_priority,
191 GCancellable *cancellable,
192 GAsyncReadyCallback callback,
193 gpointer user_data);
194 static GFileOutputStream *g_file_real_replace_finish (GFile *file,
195 GAsyncResult *res,
196 GError **error);
197 static void g_file_real_open_readwrite_async (GFile *file,
198 int io_priority,
199 GCancellable *cancellable,
200 GAsyncReadyCallback callback,
201 gpointer user_data);
202 static GFileIOStream * g_file_real_open_readwrite_finish (GFile *file,
203 GAsyncResult *res,
204 GError **error);
205 static void g_file_real_create_readwrite_async (GFile *file,
206 GFileCreateFlags flags,
207 int io_priority,
208 GCancellable *cancellable,
209 GAsyncReadyCallback callback,
210 gpointer user_data);
211 static GFileIOStream * g_file_real_create_readwrite_finish (GFile *file,
212 GAsyncResult *res,
213 GError **error);
214 static void g_file_real_replace_readwrite_async (GFile *file,
215 const char *etag,
216 gboolean make_backup,
217 GFileCreateFlags flags,
218 int io_priority,
219 GCancellable *cancellable,
220 GAsyncReadyCallback callback,
221 gpointer user_data);
222 static GFileIOStream * g_file_real_replace_readwrite_finish (GFile *file,
223 GAsyncResult *res,
224 GError **error);
225 static gboolean g_file_real_set_attributes_from_info (GFile *file,
226 GFileInfo *info,
227 GFileQueryInfoFlags flags,
228 GCancellable *cancellable,
229 GError **error);
230 static void g_file_real_set_display_name_async (GFile *file,
231 const char *display_name,
232 int io_priority,
233 GCancellable *cancellable,
234 GAsyncReadyCallback callback,
235 gpointer user_data);
236 static GFile * g_file_real_set_display_name_finish (GFile *file,
237 GAsyncResult *res,
238 GError **error);
239 static void g_file_real_set_attributes_async (GFile *file,
240 GFileInfo *info,
241 GFileQueryInfoFlags flags,
242 int io_priority,
243 GCancellable *cancellable,
244 GAsyncReadyCallback callback,
245 gpointer user_data);
246 static gboolean g_file_real_set_attributes_finish (GFile *file,
247 GAsyncResult *res,
248 GFileInfo **info,
249 GError **error);
250 static void g_file_real_find_enclosing_mount_async (GFile *file,
251 int io_priority,
252 GCancellable *cancellable,
253 GAsyncReadyCallback callback,
254 gpointer user_data);
255 static GMount * g_file_real_find_enclosing_mount_finish (GFile *file,
256 GAsyncResult *res,
257 GError **error);
258 static void g_file_real_copy_async (GFile *source,
259 GFile *destination,
260 GFileCopyFlags flags,
261 int io_priority,
262 GCancellable *cancellable,
263 GFileProgressCallback progress_callback,
264 gpointer progress_callback_data,
265 GAsyncReadyCallback callback,
266 gpointer user_data);
267 static gboolean g_file_real_copy_finish (GFile *file,
268 GAsyncResult *res,
269 GError **error);
271 typedef GFileIface GFileInterface;
272 G_DEFINE_INTERFACE (GFile, g_file, G_TYPE_OBJECT)
274 static void
275 g_file_default_init (GFileIface *iface)
277 iface->enumerate_children_async = g_file_real_enumerate_children_async;
278 iface->enumerate_children_finish = g_file_real_enumerate_children_finish;
279 iface->set_display_name_async = g_file_real_set_display_name_async;
280 iface->set_display_name_finish = g_file_real_set_display_name_finish;
281 iface->query_info_async = g_file_real_query_info_async;
282 iface->query_info_finish = g_file_real_query_info_finish;
283 iface->query_filesystem_info_async = g_file_real_query_filesystem_info_async;
284 iface->query_filesystem_info_finish = g_file_real_query_filesystem_info_finish;
285 iface->set_attributes_async = g_file_real_set_attributes_async;
286 iface->set_attributes_finish = g_file_real_set_attributes_finish;
287 iface->read_async = g_file_real_read_async;
288 iface->read_finish = g_file_real_read_finish;
289 iface->append_to_async = g_file_real_append_to_async;
290 iface->append_to_finish = g_file_real_append_to_finish;
291 iface->create_async = g_file_real_create_async;
292 iface->create_finish = g_file_real_create_finish;
293 iface->replace_async = g_file_real_replace_async;
294 iface->replace_finish = g_file_real_replace_finish;
295 iface->open_readwrite_async = g_file_real_open_readwrite_async;
296 iface->open_readwrite_finish = g_file_real_open_readwrite_finish;
297 iface->create_readwrite_async = g_file_real_create_readwrite_async;
298 iface->create_readwrite_finish = g_file_real_create_readwrite_finish;
299 iface->replace_readwrite_async = g_file_real_replace_readwrite_async;
300 iface->replace_readwrite_finish = g_file_real_replace_readwrite_finish;
301 iface->find_enclosing_mount_async = g_file_real_find_enclosing_mount_async;
302 iface->find_enclosing_mount_finish = g_file_real_find_enclosing_mount_finish;
303 iface->set_attributes_from_info = g_file_real_set_attributes_from_info;
304 iface->copy_async = g_file_real_copy_async;
305 iface->copy_finish = g_file_real_copy_finish;
310 * g_file_is_native:
311 * @file: input #GFile.
313 * Checks to see if a file is native to the platform.
315 * A native file s one expressed in the platform-native filename format,
316 * e.g. "C:\Windows" or "/usr/bin/". This does not mean the file is local,
317 * as it might be on a locally mounted remote filesystem.
319 * On some systems non-native files may be available using
320 * the native filesystem via a userspace filesystem (FUSE), in
321 * these cases this call will return %FALSE, but g_file_get_path()
322 * will still return a native path.
324 * This call does no blocking i/o.
326 * Returns: %TRUE if file is native.
328 gboolean
329 g_file_is_native (GFile *file)
331 GFileIface *iface;
333 g_return_val_if_fail (G_IS_FILE (file), FALSE);
335 iface = G_FILE_GET_IFACE (file);
337 return (* iface->is_native) (file);
342 * g_file_has_uri_scheme:
343 * @file: input #GFile.
344 * @uri_scheme: a string containing a URI scheme.
346 * Checks to see if a #GFile has a given URI scheme.
348 * This call does no blocking i/o.
350 * Returns: %TRUE if #GFile's backend supports the
351 * given URI scheme, %FALSE if URI scheme is %NULL,
352 * not supported, or #GFile is invalid.
354 gboolean
355 g_file_has_uri_scheme (GFile *file,
356 const char *uri_scheme)
358 GFileIface *iface;
360 g_return_val_if_fail (G_IS_FILE (file), FALSE);
361 g_return_val_if_fail (uri_scheme != NULL, FALSE);
363 iface = G_FILE_GET_IFACE (file);
365 return (* iface->has_uri_scheme) (file, uri_scheme);
370 * g_file_get_uri_scheme:
371 * @file: input #GFile.
373 * Gets the URI scheme for a #GFile.
374 * RFC 3986 decodes the scheme as:
375 * <programlisting>
376 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
377 * </programlisting>
378 * Common schemes include "file", "http", "ftp", etc.
380 * This call does no blocking i/o.
382 * Returns: a string containing the URI scheme for the given
383 * #GFile. The returned string should be freed with g_free()
384 * when no longer needed.
386 char *
387 g_file_get_uri_scheme (GFile *file)
389 GFileIface *iface;
391 g_return_val_if_fail (G_IS_FILE (file), NULL);
393 iface = G_FILE_GET_IFACE (file);
395 return (* iface->get_uri_scheme) (file);
400 * g_file_get_basename:
401 * @file: input #GFile.
403 * Gets the base name (the last component of the path) for a given #GFile.
405 * If called for the top level of a system (such as the filesystem root
406 * or a uri like sftp://host/) it will return a single directory separator
407 * (and on Windows, possibly a drive letter).
409 * The base name is a byte string (*not* UTF-8). It has no defined encoding
410 * or rules other than it may not contain zero bytes. If you want to use
411 * filenames in a user interface you should use the display name that you
412 * can get by requesting the %G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME
413 * attribute with g_file_query_info().
415 * This call does no blocking i/o.
417 * Returns: string containing the #GFile's base name, or %NULL
418 * if given #GFile is invalid. The returned string should be
419 * freed with g_free() when no longer needed.
421 char *
422 g_file_get_basename (GFile *file)
424 GFileIface *iface;
426 g_return_val_if_fail (G_IS_FILE (file), NULL);
428 iface = G_FILE_GET_IFACE (file);
430 return (* iface->get_basename) (file);
434 * g_file_get_path:
435 * @file: input #GFile.
437 * Gets the local pathname for #GFile, if one exists.
439 * This call does no blocking i/o.
441 * Returns: string containing the #GFile's path, or %NULL if
442 * no such path exists. The returned string should be
443 * freed with g_free() when no longer needed.
445 char *
446 g_file_get_path (GFile *file)
448 GFileIface *iface;
450 g_return_val_if_fail (G_IS_FILE (file), NULL);
452 iface = G_FILE_GET_IFACE (file);
454 return (* iface->get_path) (file);
458 * g_file_get_uri:
459 * @file: input #GFile.
461 * Gets the URI for the @file.
463 * This call does no blocking i/o.
465 * Returns: a string containing the #GFile's URI.
466 * The returned string should be freed with g_free() when no longer needed.
468 char *
469 g_file_get_uri (GFile *file)
471 GFileIface *iface;
473 g_return_val_if_fail (G_IS_FILE (file), NULL);
475 iface = G_FILE_GET_IFACE (file);
477 return (* iface->get_uri) (file);
481 * g_file_get_parse_name:
482 * @file: input #GFile.
484 * Gets the parse name of the @file.
485 * A parse name is a UTF-8 string that describes the
486 * file such that one can get the #GFile back using
487 * g_file_parse_name().
489 * This is generally used to show the #GFile as a nice
490 * full-pathname kind of string in a user interface,
491 * like in a location entry.
493 * For local files with names that can safely be converted
494 * to UTF8 the pathname is used, otherwise the IRI is used
495 * (a form of URI that allows UTF8 characters unescaped).
497 * This call does no blocking i/o.
499 * Returns: a string containing the #GFile's parse name. The returned
500 * string should be freed with g_free() when no longer needed.
502 char *
503 g_file_get_parse_name (GFile *file)
505 GFileIface *iface;
507 g_return_val_if_fail (G_IS_FILE (file), NULL);
509 iface = G_FILE_GET_IFACE (file);
511 return (* iface->get_parse_name) (file);
515 * g_file_dup:
516 * @file: input #GFile.
518 * Duplicates a #GFile handle. This operation does not duplicate
519 * the actual file or directory represented by the #GFile; see
520 * g_file_copy() if attempting to copy a file.
522 * This call does no blocking i/o.
524 * Returns: a new #GFile that is a duplicate of the given #GFile.
526 GFile *
527 g_file_dup (GFile *file)
529 GFileIface *iface;
531 g_return_val_if_fail (G_IS_FILE (file), NULL);
533 iface = G_FILE_GET_IFACE (file);
535 return (* iface->dup) (file);
539 * g_file_hash:
540 * @file: #gconstpointer to a #GFile.
542 * Creates a hash value for a #GFile.
544 * This call does no blocking i/o.
546 * Returns: 0 if @file is not a valid #GFile, otherwise an
547 * integer that can be used as hash value for the #GFile.
548 * This function is intended for easily hashing a #GFile to
549 * add to a #GHashTable or similar data structure.
551 guint
552 g_file_hash (gconstpointer file)
554 GFileIface *iface;
556 g_return_val_if_fail (G_IS_FILE (file), 0);
558 iface = G_FILE_GET_IFACE (file);
560 return (* iface->hash) ((GFile *)file);
564 * g_file_equal:
565 * @file1: the first #GFile.
566 * @file2: the second #GFile.
568 * Checks equality of two given #GFile<!-- -->s. Note that two
569 * #GFile<!-- -->s that differ can still refer to the same
570 * file on the filesystem due to various forms of filename
571 * aliasing.
573 * This call does no blocking i/o.
575 * Returns: %TRUE if @file1 and @file2 are equal.
576 * %FALSE if either is not a #GFile.
578 gboolean
579 g_file_equal (GFile *file1,
580 GFile *file2)
582 GFileIface *iface;
584 g_return_val_if_fail (G_IS_FILE (file1), FALSE);
585 g_return_val_if_fail (G_IS_FILE (file2), FALSE);
587 if (G_TYPE_FROM_INSTANCE (file1) != G_TYPE_FROM_INSTANCE (file2))
588 return FALSE;
590 iface = G_FILE_GET_IFACE (file1);
592 return (* iface->equal) (file1, file2);
597 * g_file_get_parent:
598 * @file: input #GFile.
600 * Gets the parent directory for the @file.
601 * If the @file represents the root directory of the
602 * file system, then %NULL will be returned.
604 * This call does no blocking i/o.
606 * Returns: a #GFile structure to the parent of the given
607 * #GFile or %NULL if there is no parent.
608 * Free the returned object with g_object_unref().
610 GFile *
611 g_file_get_parent (GFile *file)
613 GFileIface *iface;
615 g_return_val_if_fail (G_IS_FILE (file), NULL);
617 iface = G_FILE_GET_IFACE (file);
619 return (* iface->get_parent) (file);
623 * g_file_has_parent:
624 * @file: input #GFile
625 * @parent: the parent to check for, or %NULL
627 * Checks if @file has a parent, and optionally, if it is @parent.
629 * If @parent is %NULL then this function returns %TRUE if @file has any
630 * parent at all. If @parent is non-%NULL then %TRUE is only returned
631 * if @file is a child of @parent.
633 * Returns: %TRUE if @file is a child of @parent (or any parent in the
634 * case that @parent is %NULL).
636 * Since: 2.24
638 gboolean
639 g_file_has_parent (GFile *file,
640 GFile *parent)
642 GFile *actual_parent;
643 gboolean result;
645 g_return_val_if_fail (G_IS_FILE (file), FALSE);
646 g_return_val_if_fail (parent == NULL || G_IS_FILE (parent), FALSE);
648 actual_parent = g_file_get_parent (file);
650 if (actual_parent != NULL)
652 if (parent != NULL)
653 result = g_file_equal (parent, actual_parent);
654 else
655 result = TRUE;
657 g_object_unref (actual_parent);
659 else
660 result = FALSE;
662 return result;
666 * g_file_get_child:
667 * @file: input #GFile.
668 * @name: string containing the child's basename.
670 * Gets a child of @file with basename equal to @name.
672 * Note that the file with that specific name might not exist, but
673 * you can still have a #GFile that points to it. You can use this
674 * for instance to create that file.
676 * This call does no blocking i/o.
678 * Returns: a #GFile to a child specified by @name.
679 * Free the returned object with g_object_unref().
681 GFile *
682 g_file_get_child (GFile *file,
683 const char *name)
685 g_return_val_if_fail (G_IS_FILE (file), NULL);
686 g_return_val_if_fail (name != NULL, NULL);
688 return g_file_resolve_relative_path (file, name);
692 * g_file_get_child_for_display_name:
693 * @file: input #GFile.
694 * @display_name: string to a possible child.
695 * @error: #GError.
697 * Gets the child of @file for a given @display_name (i.e. a UTF8
698 * version of the name). If this function fails, it returns %NULL and @error will be
699 * set. This is very useful when constructing a GFile for a new file
700 * and the user entered the filename in the user interface, for instance
701 * when you select a directory and type a filename in the file selector.
703 * This call does no blocking i/o.
705 * Returns: a #GFile to the specified child, or
706 * %NULL if the display name couldn't be converted.
707 * Free the returned object with g_object_unref().
709 GFile *
710 g_file_get_child_for_display_name (GFile *file,
711 const char *display_name,
712 GError **error)
714 GFileIface *iface;
716 g_return_val_if_fail (G_IS_FILE (file), NULL);
717 g_return_val_if_fail (display_name != NULL, NULL);
719 iface = G_FILE_GET_IFACE (file);
721 return (* iface->get_child_for_display_name) (file, display_name, error);
725 * g_file_has_prefix:
726 * @file: input #GFile.
727 * @prefix: input #GFile.
729 * Checks whether @file has the prefix specified by @prefix. In other word,
730 * if the names of inital elements of @file<!-- -->s pathname match @prefix.
731 * Only full pathname elements are matched, so a path like /foo is not
732 * considered a prefix of /foobar, only of /foo/bar.
734 * This call does no i/o, as it works purely on names. As such it can
735 * sometimes return %FALSE even if @file is inside a @prefix (from a
736 * filesystem point of view), because the prefix of @file is an alias
737 * of @prefix.
739 * Returns: %TRUE if the @files's parent, grandparent, etc is @prefix.
740 * %FALSE otherwise.
742 gboolean
743 g_file_has_prefix (GFile *file,
744 GFile *prefix)
746 GFileIface *iface;
748 g_return_val_if_fail (G_IS_FILE (file), FALSE);
749 g_return_val_if_fail (G_IS_FILE (prefix), FALSE);
751 if (G_TYPE_FROM_INSTANCE (file) != G_TYPE_FROM_INSTANCE (prefix))
752 return FALSE;
754 iface = G_FILE_GET_IFACE (file);
756 /* The vtable function differs in arg order since we're
757 using the old contains_file call */
758 return (* iface->prefix_matches) (prefix, file);
762 * g_file_get_relative_path:
763 * @parent: input #GFile.
764 * @descendant: input #GFile.
766 * Gets the path for @descendant relative to @parent.
768 * This call does no blocking i/o.
770 * Returns: string with the relative path from @descendant
771 * to @parent, or %NULL if @descendant doesn't have @parent as prefix.
772 * The returned string should be freed with g_free() when no longer needed.
774 char *
775 g_file_get_relative_path (GFile *parent,
776 GFile *descendant)
778 GFileIface *iface;
780 g_return_val_if_fail (G_IS_FILE (parent), NULL);
781 g_return_val_if_fail (G_IS_FILE (descendant), NULL);
783 if (G_TYPE_FROM_INSTANCE (parent) != G_TYPE_FROM_INSTANCE (descendant))
784 return NULL;
786 iface = G_FILE_GET_IFACE (parent);
788 return (* iface->get_relative_path) (parent, descendant);
792 * g_file_resolve_relative_path:
793 * @file: input #GFile.
794 * @relative_path: a given relative path string.
796 * Resolves a relative path for @file to an absolute path.
798 * This call does no blocking i/o.
800 * Returns: #GFile to the resolved path. %NULL if @relative_path
801 * is %NULL or if @file is invalid.
802 * Free the returned object with g_object_unref().
804 GFile *
805 g_file_resolve_relative_path (GFile *file,
806 const char *relative_path)
808 GFileIface *iface;
810 g_return_val_if_fail (G_IS_FILE (file), NULL);
811 g_return_val_if_fail (relative_path != NULL, NULL);
813 iface = G_FILE_GET_IFACE (file);
815 return (* iface->resolve_relative_path) (file, relative_path);
819 * g_file_enumerate_children:
820 * @file: input #GFile.
821 * @attributes: an attribute query string.
822 * @flags: a set of #GFileQueryInfoFlags.
823 * @cancellable: optional #GCancellable object, %NULL to ignore.
824 * @error: #GError for error reporting.
826 * Gets the requested information about the files in a directory. The result
827 * is a #GFileEnumerator object that will give out #GFileInfo objects for
828 * all the files in the directory.
830 * The @attributes value is a string that specifies the file attributes that
831 * should be gathered. It is not an error if it's not possible to read a particular
832 * requested attribute from a file - it just won't be set. @attributes should
833 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
834 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
835 * namespace. An example attribute query be "standard::*,owner::user".
836 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
838 * If @cancellable is not %NULL, then the operation can be cancelled by
839 * triggering the cancellable object from another thread. If the operation
840 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
842 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
843 * If the file is not a directory, the G_FILE_ERROR_NOTDIR error will be returned.
844 * Other errors are possible too.
846 * Returns: A #GFileEnumerator if successful, %NULL on error.
847 * Free the returned object with g_object_unref().
849 GFileEnumerator *
850 g_file_enumerate_children (GFile *file,
851 const char *attributes,
852 GFileQueryInfoFlags flags,
853 GCancellable *cancellable,
854 GError **error)
857 GFileIface *iface;
859 g_return_val_if_fail (G_IS_FILE (file), NULL);
861 if (g_cancellable_set_error_if_cancelled (cancellable, error))
862 return NULL;
864 iface = G_FILE_GET_IFACE (file);
866 if (iface->enumerate_children == NULL)
868 g_set_error_literal (error, G_IO_ERROR,
869 G_IO_ERROR_NOT_SUPPORTED,
870 _("Operation not supported"));
871 return NULL;
874 return (* iface->enumerate_children) (file, attributes, flags,
875 cancellable, error);
879 * g_file_enumerate_children_async:
880 * @file: input #GFile.
881 * @attributes: an attribute query string.
882 * @flags: a set of #GFileQueryInfoFlags.
883 * @io_priority: the <link linkend="io-priority">I/O priority</link>
884 * of the request.
885 * @cancellable: optional #GCancellable object, %NULL to ignore.
886 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
887 * @user_data: the data to pass to callback function
889 * Asynchronously gets the requested information about the files in a directory. The result
890 * is a #GFileEnumerator object that will give out #GFileInfo objects for
891 * all the files in the directory.
893 * For more details, see g_file_enumerate_children() which is
894 * the synchronous version of this call.
896 * When the operation is finished, @callback will be called. You can then call
897 * g_file_enumerate_children_finish() to get the result of the operation.
899 void
900 g_file_enumerate_children_async (GFile *file,
901 const char *attributes,
902 GFileQueryInfoFlags flags,
903 int io_priority,
904 GCancellable *cancellable,
905 GAsyncReadyCallback callback,
906 gpointer user_data)
908 GFileIface *iface;
910 g_return_if_fail (G_IS_FILE (file));
912 iface = G_FILE_GET_IFACE (file);
913 (* iface->enumerate_children_async) (file,
914 attributes,
915 flags,
916 io_priority,
917 cancellable,
918 callback,
919 user_data);
923 * g_file_enumerate_children_finish:
924 * @file: input #GFile.
925 * @res: a #GAsyncResult.
926 * @error: a #GError.
928 * Finishes an async enumerate children operation.
929 * See g_file_enumerate_children_async().
931 * Returns: a #GFileEnumerator or %NULL if an error occurred.
932 * Free the returned object with g_object_unref().
934 GFileEnumerator *
935 g_file_enumerate_children_finish (GFile *file,
936 GAsyncResult *res,
937 GError **error)
939 GFileIface *iface;
941 g_return_val_if_fail (G_IS_FILE (file), NULL);
942 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
944 if (G_IS_SIMPLE_ASYNC_RESULT (res))
946 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
947 if (g_simple_async_result_propagate_error (simple, error))
948 return NULL;
951 iface = G_FILE_GET_IFACE (file);
952 return (* iface->enumerate_children_finish) (file, res, error);
956 * g_file_query_exists:
957 * @file: input #GFile.
958 * @cancellable: optional #GCancellable object, %NULL to ignore.
960 * Utility function to check if a particular file exists. This is
961 * implemented using g_file_query_info() and as such does blocking I/O.
963 * Note that in many cases it is racy to first check for file existence
964 * and then execute something based on the outcome of that, because the
965 * file might have been created or removed in between the operations. The
966 * general approach to handling that is to not check, but just do the
967 * operation and handle the errors as they come.
969 * As an example of race-free checking, take the case of reading a file, and
970 * if it doesn't exist, creating it. There are two racy versions: read it, and
971 * on error create it; and: check if it exists, if not create it. These
972 * can both result in two processes creating the file (with perhaps a partially
973 * written file as the result). The correct approach is to always try to create
974 * the file with g_file_create() which will either atomically create the file
975 * or fail with a G_IO_ERROR_EXISTS error.
977 * However, in many cases an existence check is useful in a user
978 * interface, for instance to make a menu item sensitive/insensitive, so that
979 * you don't have to fool users that something is possible and then just show
980 * and error dialog. If you do this, you should make sure to also handle the
981 * errors that can happen due to races when you execute the operation.
983 * Returns: %TRUE if the file exists (and can be detected without error), %FALSE otherwise (or if cancelled).
985 gboolean
986 g_file_query_exists (GFile *file,
987 GCancellable *cancellable)
989 GFileInfo *info;
991 g_return_val_if_fail (G_IS_FILE(file), FALSE);
993 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE,
994 G_FILE_QUERY_INFO_NONE, cancellable, NULL);
995 if (info != NULL)
997 g_object_unref (info);
998 return TRUE;
1001 return FALSE;
1005 * g_file_query_file_type:
1006 * @file: input #GFile.
1007 * @flags: a set of #GFileQueryInfoFlags passed to g_file_query_info().
1008 * @cancellable: optional #GCancellable object, %NULL to ignore.
1010 * Utility function to inspect the #GFileType of a file. This is
1011 * implemented using g_file_query_info() and as such does blocking I/O.
1013 * The primary use case of this method is to check if a file is a regular file,
1014 * directory, or symlink.
1016 * Returns: The #GFileType of the file and #G_FILE_TYPE_UNKNOWN if the file
1017 * does not exist
1019 * Since: 2.18
1021 GFileType
1022 g_file_query_file_type (GFile *file,
1023 GFileQueryInfoFlags flags,
1024 GCancellable *cancellable)
1026 GFileInfo *info;
1027 GFileType file_type;
1029 g_return_val_if_fail (G_IS_FILE(file), G_FILE_TYPE_UNKNOWN);
1030 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, flags,
1031 cancellable, NULL);
1032 if (info != NULL)
1034 file_type = g_file_info_get_file_type (info);
1035 g_object_unref (info);
1037 else
1038 file_type = G_FILE_TYPE_UNKNOWN;
1040 return file_type;
1044 * g_file_query_info:
1045 * @file: input #GFile.
1046 * @attributes: an attribute query string.
1047 * @flags: a set of #GFileQueryInfoFlags.
1048 * @cancellable: optional #GCancellable object, %NULL to ignore.
1049 * @error: a #GError.
1051 * Gets the requested information about specified @file. The result
1052 * is a #GFileInfo object that contains key-value attributes (such as
1053 * the type or size of the file).
1055 * The @attributes value is a string that specifies the file attributes that
1056 * should be gathered. It is not an error if it's not possible to read a particular
1057 * requested attribute from a file - it just won't be set. @attributes should
1058 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1059 * means all attributes, and a wildcard like "standard::*" means all attributes in the standard
1060 * namespace. An example attribute query be "standard::*,owner::user".
1061 * The standard attributes are available as defines, like #G_FILE_ATTRIBUTE_STANDARD_NAME.
1063 * If @cancellable is not %NULL, then the operation can be cancelled by
1064 * triggering the cancellable object from another thread. If the operation
1065 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1067 * For symlinks, normally the information about the target of the
1068 * symlink is returned, rather than information about the symlink itself.
1069 * However if you pass #G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS in @flags the
1070 * information about the symlink itself will be returned. Also, for symlinks
1071 * that point to non-existing files the information about the symlink itself
1072 * will be returned.
1074 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1075 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1077 * Returns: a #GFileInfo for the given @file, or %NULL on error.
1078 * Free the returned object with g_object_unref().
1080 GFileInfo *
1081 g_file_query_info (GFile *file,
1082 const char *attributes,
1083 GFileQueryInfoFlags flags,
1084 GCancellable *cancellable,
1085 GError **error)
1087 GFileIface *iface;
1089 g_return_val_if_fail (G_IS_FILE (file), NULL);
1091 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1092 return NULL;
1094 iface = G_FILE_GET_IFACE (file);
1096 if (iface->query_info == NULL)
1098 g_set_error_literal (error, G_IO_ERROR,
1099 G_IO_ERROR_NOT_SUPPORTED,
1100 _("Operation not supported"));
1101 return NULL;
1104 return (* iface->query_info) (file, attributes, flags, cancellable, error);
1108 * g_file_query_info_async:
1109 * @file: input #GFile.
1110 * @attributes: an attribute query string.
1111 * @flags: a set of #GFileQueryInfoFlags.
1112 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1113 * of the request.
1114 * @cancellable: optional #GCancellable object, %NULL to ignore.
1115 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1116 * @user_data: the data to pass to callback function
1118 * Asynchronously gets the requested information about specified @file. The result
1119 * is a #GFileInfo object that contains key-value attributes (such as type or size
1120 * for the file).
1122 * For more details, see g_file_query_info() which is
1123 * the synchronous version of this call.
1125 * When the operation is finished, @callback will be called. You can then call
1126 * g_file_query_info_finish() to get the result of the operation.
1128 void
1129 g_file_query_info_async (GFile *file,
1130 const char *attributes,
1131 GFileQueryInfoFlags flags,
1132 int io_priority,
1133 GCancellable *cancellable,
1134 GAsyncReadyCallback callback,
1135 gpointer user_data)
1137 GFileIface *iface;
1139 g_return_if_fail (G_IS_FILE (file));
1141 iface = G_FILE_GET_IFACE (file);
1142 (* iface->query_info_async) (file,
1143 attributes,
1144 flags,
1145 io_priority,
1146 cancellable,
1147 callback,
1148 user_data);
1152 * g_file_query_info_finish:
1153 * @file: input #GFile.
1154 * @res: a #GAsyncResult.
1155 * @error: a #GError.
1157 * Finishes an asynchronous file info query.
1158 * See g_file_query_info_async().
1160 * Returns: #GFileInfo for given @file or %NULL on error.
1161 * Free the returned object with g_object_unref().
1163 GFileInfo *
1164 g_file_query_info_finish (GFile *file,
1165 GAsyncResult *res,
1166 GError **error)
1168 GFileIface *iface;
1170 g_return_val_if_fail (G_IS_FILE (file), NULL);
1171 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1173 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1175 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1176 if (g_simple_async_result_propagate_error (simple, error))
1177 return NULL;
1180 iface = G_FILE_GET_IFACE (file);
1181 return (* iface->query_info_finish) (file, res, error);
1185 * g_file_query_filesystem_info:
1186 * @file: input #GFile.
1187 * @attributes: an attribute query string.
1188 * @cancellable: optional #GCancellable object, %NULL to ignore.
1189 * @error: a #GError.
1191 * Similar to g_file_query_info(), but obtains information
1192 * about the filesystem the @file is on, rather than the file itself.
1193 * For instance the amount of space available and the type of
1194 * the filesystem.
1196 * The @attributes value is a string that specifies the file attributes that
1197 * should be gathered. It is not an error if it's not possible to read a particular
1198 * requested attribute from a file - it just won't be set. @attributes should
1199 * be a comma-separated list of attributes or attribute wildcards. The wildcard "*"
1200 * means all attributes, and a wildcard like "fs:*" means all attributes in the fs
1201 * namespace. The standard namespace for filesystem attributes is "fs".
1202 * Common attributes of interest are #G_FILE_ATTRIBUTE_FILESYSTEM_SIZE
1203 * (the total size of the filesystem in bytes), #G_FILE_ATTRIBUTE_FILESYSTEM_FREE (number of
1204 * bytes available), and #G_FILE_ATTRIBUTE_FILESYSTEM_TYPE (type of the filesystem).
1206 * If @cancellable is not %NULL, then the operation can be cancelled by
1207 * triggering the cancellable object from another thread. If the operation
1208 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1210 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1211 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1213 * Returns: a #GFileInfo or %NULL if there was an error.
1214 * Free the returned object with g_object_unref().
1216 GFileInfo *
1217 g_file_query_filesystem_info (GFile *file,
1218 const char *attributes,
1219 GCancellable *cancellable,
1220 GError **error)
1222 GFileIface *iface;
1224 g_return_val_if_fail (G_IS_FILE (file), NULL);
1226 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1227 return NULL;
1229 iface = G_FILE_GET_IFACE (file);
1231 if (iface->query_filesystem_info == NULL)
1233 g_set_error_literal (error, G_IO_ERROR,
1234 G_IO_ERROR_NOT_SUPPORTED,
1235 _("Operation not supported"));
1236 return NULL;
1239 return (* iface->query_filesystem_info) (file, attributes, cancellable, error);
1243 * g_file_query_filesystem_info_async:
1244 * @file: input #GFile.
1245 * @attributes: an attribute query string.
1246 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1247 * of the request.
1248 * @cancellable: optional #GCancellable object, %NULL to ignore.
1249 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1250 * @user_data: the data to pass to callback function
1252 * Asynchronously gets the requested information about the filesystem
1253 * that the specified @file is on. The result is a #GFileInfo object
1254 * that contains key-value attributes (such as type or size for the
1255 * file).
1257 * For more details, see g_file_query_filesystem_info() which is the
1258 * synchronous version of this call.
1260 * When the operation is finished, @callback will be called. You can
1261 * then call g_file_query_info_finish() to get the result of the
1262 * operation.
1264 void
1265 g_file_query_filesystem_info_async (GFile *file,
1266 const char *attributes,
1267 int io_priority,
1268 GCancellable *cancellable,
1269 GAsyncReadyCallback callback,
1270 gpointer user_data)
1272 GFileIface *iface;
1274 g_return_if_fail (G_IS_FILE (file));
1276 iface = G_FILE_GET_IFACE (file);
1277 (* iface->query_filesystem_info_async) (file,
1278 attributes,
1279 io_priority,
1280 cancellable,
1281 callback,
1282 user_data);
1286 * g_file_query_filesystem_info_finish:
1287 * @file: input #GFile.
1288 * @res: a #GAsyncResult.
1289 * @error: a #GError.
1291 * Finishes an asynchronous filesystem info query. See
1292 * g_file_query_filesystem_info_async().
1294 * Returns: #GFileInfo for given @file or %NULL on error.
1295 * Free the returned object with g_object_unref().
1297 GFileInfo *
1298 g_file_query_filesystem_info_finish (GFile *file,
1299 GAsyncResult *res,
1300 GError **error)
1302 GFileIface *iface;
1304 g_return_val_if_fail (G_IS_FILE (file), NULL);
1305 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1307 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1309 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1310 if (g_simple_async_result_propagate_error (simple, error))
1311 return NULL;
1314 iface = G_FILE_GET_IFACE (file);
1315 return (* iface->query_filesystem_info_finish) (file, res, error);
1319 * g_file_find_enclosing_mount:
1320 * @file: input #GFile.
1321 * @cancellable: optional #GCancellable object, %NULL to ignore.
1322 * @error: a #GError.
1324 * Gets a #GMount for the #GFile.
1326 * If the #GFileIface for @file does not have a mount (e.g. possibly a
1327 * remote share), @error will be set to %G_IO_ERROR_NOT_FOUND and %NULL
1328 * will be returned.
1330 * If @cancellable is not %NULL, then the operation can be cancelled by
1331 * triggering the cancellable object from another thread. If the operation
1332 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1334 * Returns: a #GMount where the @file is located or %NULL on error.
1335 * Free the returned object with g_object_unref().
1337 GMount *
1338 g_file_find_enclosing_mount (GFile *file,
1339 GCancellable *cancellable,
1340 GError **error)
1342 GFileIface *iface;
1344 g_return_val_if_fail (G_IS_FILE (file), NULL);
1346 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1347 return NULL;
1349 iface = G_FILE_GET_IFACE (file);
1350 if (iface->find_enclosing_mount == NULL)
1353 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
1354 /* Translators: This is an error message when trying to find the
1355 * enclosing (user visible) mount of a file, but none exists. */
1356 _("Containing mount does not exist"));
1357 return NULL;
1360 return (* iface->find_enclosing_mount) (file, cancellable, error);
1364 * g_file_find_enclosing_mount_async:
1365 * @file: a #GFile
1366 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1367 * of the request.
1368 * @cancellable: optional #GCancellable object, %NULL to ignore.
1369 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1370 * @user_data: the data to pass to callback function
1372 * Asynchronously gets the mount for the file.
1374 * For more details, see g_file_find_enclosing_mount() which is
1375 * the synchronous version of this call.
1377 * When the operation is finished, @callback will be called. You can then call
1378 * g_file_find_enclosing_mount_finish() to get the result of the operation.
1380 void
1381 g_file_find_enclosing_mount_async (GFile *file,
1382 int io_priority,
1383 GCancellable *cancellable,
1384 GAsyncReadyCallback callback,
1385 gpointer user_data)
1387 GFileIface *iface;
1389 g_return_if_fail (G_IS_FILE (file));
1391 iface = G_FILE_GET_IFACE (file);
1392 (* iface->find_enclosing_mount_async) (file,
1393 io_priority,
1394 cancellable,
1395 callback,
1396 user_data);
1400 * g_file_find_enclosing_mount_finish:
1401 * @file: a #GFile
1402 * @res: a #GAsyncResult
1403 * @error: a #GError
1405 * Finishes an asynchronous find mount request.
1406 * See g_file_find_enclosing_mount_async().
1408 * Returns: #GMount for given @file or %NULL on error.
1409 * Free the returned object with g_object_unref().
1411 GMount *
1412 g_file_find_enclosing_mount_finish (GFile *file,
1413 GAsyncResult *res,
1414 GError **error)
1416 GFileIface *iface;
1418 g_return_val_if_fail (G_IS_FILE (file), NULL);
1419 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1421 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1423 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1424 if (g_simple_async_result_propagate_error (simple, error))
1425 return NULL;
1428 iface = G_FILE_GET_IFACE (file);
1429 return (* iface->find_enclosing_mount_finish) (file, res, error);
1434 * g_file_read:
1435 * @file: #GFile to read.
1436 * @cancellable: a #GCancellable
1437 * @error: a #GError, or %NULL
1439 * Opens a file for reading. The result is a #GFileInputStream that
1440 * can be used to read the contents of the file.
1442 * If @cancellable is not %NULL, then the operation can be cancelled by
1443 * triggering the cancellable object from another thread. If the operation
1444 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1446 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1447 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1448 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1450 * Returns: #GFileInputStream or %NULL on error.
1451 * Free the returned object with g_object_unref().
1453 GFileInputStream *
1454 g_file_read (GFile *file,
1455 GCancellable *cancellable,
1456 GError **error)
1458 GFileIface *iface;
1460 g_return_val_if_fail (G_IS_FILE (file), NULL);
1462 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1463 return NULL;
1465 iface = G_FILE_GET_IFACE (file);
1467 if (iface->read_fn == NULL)
1469 g_set_error_literal (error, G_IO_ERROR,
1470 G_IO_ERROR_NOT_SUPPORTED,
1471 _("Operation not supported"));
1472 return NULL;
1475 return (* iface->read_fn) (file, cancellable, error);
1479 * g_file_append_to:
1480 * @file: input #GFile.
1481 * @flags: a set of #GFileCreateFlags.
1482 * @cancellable: optional #GCancellable object, %NULL to ignore.
1483 * @error: a #GError, or %NULL
1485 * Gets an output stream for appending data to the file. If
1486 * the file doesn't already exist it is created.
1488 * By default files created are generally readable by everyone,
1489 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1490 * will be made readable only to the current user, to the level that
1491 * is supported on the target filesystem.
1493 * If @cancellable is not %NULL, then the operation can be cancelled by
1494 * triggering the cancellable object from another thread. If the operation
1495 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1497 * Some file systems don't allow all file names, and may
1498 * return an %G_IO_ERROR_INVALID_FILENAME error.
1499 * If the file is a directory the %G_IO_ERROR_IS_DIRECTORY error will be
1500 * returned. Other errors are possible too, and depend on what kind of
1501 * filesystem the file is on.
1503 * Returns: a #GFileOutputStream, or %NULL on error.
1504 * Free the returned object with g_object_unref().
1506 GFileOutputStream *
1507 g_file_append_to (GFile *file,
1508 GFileCreateFlags flags,
1509 GCancellable *cancellable,
1510 GError **error)
1512 GFileIface *iface;
1514 g_return_val_if_fail (G_IS_FILE (file), NULL);
1516 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1517 return NULL;
1519 iface = G_FILE_GET_IFACE (file);
1521 if (iface->append_to == NULL)
1523 g_set_error_literal (error, G_IO_ERROR,
1524 G_IO_ERROR_NOT_SUPPORTED,
1525 _("Operation not supported"));
1526 return NULL;
1529 return (* iface->append_to) (file, flags, cancellable, error);
1533 * g_file_create:
1534 * @file: input #GFile.
1535 * @flags: a set of #GFileCreateFlags.
1536 * @cancellable: optional #GCancellable object, %NULL to ignore.
1537 * @error: a #GError, or %NULL
1539 * Creates a new file and returns an output stream for writing to it.
1540 * The file must not already exist.
1542 * By default files created are generally readable by everyone,
1543 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1544 * will be made readable only to the current user, to the level that
1545 * is supported on the target filesystem.
1547 * If @cancellable is not %NULL, then the operation can be cancelled by
1548 * triggering the cancellable object from another thread. If the operation
1549 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1551 * If a file or directory with this name already exists the G_IO_ERROR_EXISTS
1552 * error will be returned.
1553 * Some file systems don't allow all file names, and may
1554 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1555 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1556 * Other errors are possible too, and depend on what kind of
1557 * filesystem the file is on.
1559 * Returns: a #GFileOutputStream for the newly created file, or
1560 * %NULL on error.
1561 * Free the returned object with g_object_unref().
1563 GFileOutputStream *
1564 g_file_create (GFile *file,
1565 GFileCreateFlags flags,
1566 GCancellable *cancellable,
1567 GError **error)
1569 GFileIface *iface;
1571 g_return_val_if_fail (G_IS_FILE (file), NULL);
1573 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1574 return NULL;
1576 iface = G_FILE_GET_IFACE (file);
1578 if (iface->create == NULL)
1580 g_set_error_literal (error, G_IO_ERROR,
1581 G_IO_ERROR_NOT_SUPPORTED,
1582 _("Operation not supported"));
1583 return NULL;
1586 return (* iface->create) (file, flags, cancellable, error);
1590 * g_file_replace:
1591 * @file: input #GFile.
1592 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1593 * current #GFile, or #NULL to ignore.
1594 * @make_backup: %TRUE if a backup should be created.
1595 * @flags: a set of #GFileCreateFlags.
1596 * @cancellable: optional #GCancellable object, %NULL to ignore.
1597 * @error: a #GError, or %NULL
1599 * Returns an output stream for overwriting the file, possibly
1600 * creating a backup copy of the file first. If the file doesn't exist,
1601 * it will be created.
1603 * This will try to replace the file in the safest way possible so
1604 * that any errors during the writing will not affect an already
1605 * existing copy of the file. For instance, for local files it
1606 * may write to a temporary file and then atomically rename over
1607 * the destination when the stream is closed.
1609 * By default files created are generally readable by everyone,
1610 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1611 * will be made readable only to the current user, to the level that
1612 * is supported on the target filesystem.
1614 * If @cancellable is not %NULL, then the operation can be cancelled by
1615 * triggering the cancellable object from another thread. If the operation
1616 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1618 * If you pass in a non-#NULL @etag value, then this value is
1619 * compared to the current entity tag of the file, and if they differ
1620 * an G_IO_ERROR_WRONG_ETAG error is returned. This generally means
1621 * that the file has been changed since you last read it. You can get
1622 * the new etag from g_file_output_stream_get_etag() after you've
1623 * finished writing and closed the #GFileOutputStream. When you load
1624 * a new file you can use g_file_input_stream_query_info() to get
1625 * the etag of the file.
1627 * If @make_backup is %TRUE, this function will attempt to make a backup
1628 * of the current file before overwriting it. If this fails a G_IO_ERROR_CANT_CREATE_BACKUP
1629 * error will be returned. If you want to replace anyway, try again with
1630 * @make_backup set to %FALSE.
1632 * If the file is a directory the G_IO_ERROR_IS_DIRECTORY error will be returned,
1633 * and if the file is some other form of non-regular file then a
1634 * G_IO_ERROR_NOT_REGULAR_FILE error will be returned.
1635 * Some file systems don't allow all file names, and may
1636 * return an G_IO_ERROR_INVALID_FILENAME error, and if the name
1637 * is to long G_IO_ERROR_FILENAME_TOO_LONG will be returned.
1638 * Other errors are possible too, and depend on what kind of
1639 * filesystem the file is on.
1641 * Returns: a #GFileOutputStream or %NULL on error.
1642 * Free the returned object with g_object_unref().
1644 GFileOutputStream *
1645 g_file_replace (GFile *file,
1646 const char *etag,
1647 gboolean make_backup,
1648 GFileCreateFlags flags,
1649 GCancellable *cancellable,
1650 GError **error)
1652 GFileIface *iface;
1654 g_return_val_if_fail (G_IS_FILE (file), NULL);
1656 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1657 return NULL;
1659 iface = G_FILE_GET_IFACE (file);
1661 if (iface->replace == NULL)
1663 g_set_error_literal (error, G_IO_ERROR,
1664 G_IO_ERROR_NOT_SUPPORTED,
1665 _("Operation not supported"));
1666 return NULL;
1670 /* Handle empty tag string as NULL in consistent way. */
1671 if (etag && *etag == 0)
1672 etag = NULL;
1674 return (* iface->replace) (file, etag, make_backup, flags, cancellable, error);
1678 * g_file_open_readwrite:
1679 * @file: #GFile to open
1680 * @cancellable: a #GCancellable
1681 * @error: a #GError, or %NULL
1683 * Opens an existing file for reading and writing. The result is
1684 * a #GFileIOStream that can be used to read and write the contents of the file.
1686 * If @cancellable is not %NULL, then the operation can be cancelled by
1687 * triggering the cancellable object from another thread. If the operation
1688 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1690 * If the file does not exist, the G_IO_ERROR_NOT_FOUND error will be returned.
1691 * If the file is a directory, the G_IO_ERROR_IS_DIRECTORY error will be returned.
1692 * Other errors are possible too, and depend on what kind of filesystem the file is on.
1693 * Note that in many non-local file cases read and write streams are not supported,
1694 * so make sure you really need to do read and write streaming, rather than
1695 * just opening for reading or writing.
1697 * Returns: #GFileIOStream or %NULL on error.
1698 * Free the returned object with g_object_unref().
1700 * Since: 2.22
1702 GFileIOStream *
1703 g_file_open_readwrite (GFile *file,
1704 GCancellable *cancellable,
1705 GError **error)
1707 GFileIface *iface;
1709 g_return_val_if_fail (G_IS_FILE (file), NULL);
1711 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1712 return NULL;
1714 iface = G_FILE_GET_IFACE (file);
1716 if (iface->open_readwrite == NULL)
1718 g_set_error_literal (error, G_IO_ERROR,
1719 G_IO_ERROR_NOT_SUPPORTED,
1720 _("Operation not supported"));
1721 return NULL;
1724 return (* iface->open_readwrite) (file, cancellable, error);
1728 * g_file_create_readwrite:
1729 * @file: a #GFile
1730 * @flags: a set of #GFileCreateFlags
1731 * @cancellable: optional #GCancellable object, %NULL to ignore
1732 * @error: return location for a #GError, or %NULL
1734 * Creates a new file and returns a stream for reading and writing to it.
1735 * The file must not already exist.
1737 * By default files created are generally readable by everyone,
1738 * but if you pass #G_FILE_CREATE_PRIVATE in @flags the file
1739 * will be made readable only to the current user, to the level that
1740 * is supported on the target filesystem.
1742 * If @cancellable is not %NULL, then the operation can be cancelled by
1743 * triggering the cancellable object from another thread. If the operation
1744 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
1746 * If a file or directory with this name already exists the %G_IO_ERROR_EXISTS
1747 * error will be returned. Some file systems don't allow all file names,
1748 * and may return an %G_IO_ERROR_INVALID_FILENAME error, and if the name
1749 * is too long, %G_IO_ERROR_FILENAME_TOO_LONG will be returned. Other errors
1750 * are possible too, and depend on what kind of filesystem the file is on.
1752 * Note that in many non-local file cases read and write streams are not
1753 * supported, so make sure you really need to do read and write streaming,
1754 * rather than just opening for reading or writing.
1756 * Returns: a #GFileIOStream for the newly created file, or %NULL on error.
1757 * Free the returned object with g_object_unref().
1759 * Since: 2.22
1761 GFileIOStream *
1762 g_file_create_readwrite (GFile *file,
1763 GFileCreateFlags flags,
1764 GCancellable *cancellable,
1765 GError **error)
1767 GFileIface *iface;
1769 g_return_val_if_fail (G_IS_FILE (file), NULL);
1771 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1772 return NULL;
1774 iface = G_FILE_GET_IFACE (file);
1776 if (iface->create_readwrite == NULL)
1778 g_set_error_literal (error, G_IO_ERROR,
1779 G_IO_ERROR_NOT_SUPPORTED,
1780 _("Operation not supported"));
1781 return NULL;
1784 return (* iface->create_readwrite) (file, flags, cancellable, error);
1788 * g_file_replace_readwrite:
1789 * @file: a #GFile
1790 * @etag: (allow-none): an optional <link linkend="gfile-etag">entity tag</link> for the
1791 * current #GFile, or #NULL to ignore
1792 * @make_backup: %TRUE if a backup should be created
1793 * @flags: a set of #GFileCreateFlags
1794 * @cancellable: optional #GCancellable object, %NULL to ignore
1795 * @error: return location for a #GError, or %NULL
1797 * Returns an output stream for overwriting the file in readwrite mode,
1798 * possibly creating a backup copy of the file first. If the file doesn't
1799 * exist, it will be created.
1801 * For details about the behaviour, see g_file_replace() which does the same
1802 * thing but returns an output stream only.
1804 * Note that in many non-local file cases read and write streams are not
1805 * supported, so make sure you really need to do read and write streaming,
1806 * rather than just opening for reading or writing.
1808 * Returns: a #GFileIOStream or %NULL on error.
1809 * Free the returned object with g_object_unref().
1811 * Since: 2.22
1813 GFileIOStream *
1814 g_file_replace_readwrite (GFile *file,
1815 const char *etag,
1816 gboolean make_backup,
1817 GFileCreateFlags flags,
1818 GCancellable *cancellable,
1819 GError **error)
1821 GFileIface *iface;
1823 g_return_val_if_fail (G_IS_FILE (file), NULL);
1825 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1826 return NULL;
1828 iface = G_FILE_GET_IFACE (file);
1830 if (iface->replace_readwrite == NULL)
1832 g_set_error_literal (error, G_IO_ERROR,
1833 G_IO_ERROR_NOT_SUPPORTED,
1834 _("Operation not supported"));
1835 return NULL;
1838 return (* iface->replace_readwrite) (file, etag, make_backup, flags, cancellable, error);
1842 * g_file_read_async:
1843 * @file: input #GFile
1844 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1845 * of the request.
1846 * @cancellable: optional #GCancellable object, %NULL to ignore.
1847 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1848 * @user_data: the data to pass to callback function
1850 * Asynchronously opens @file for reading.
1852 * For more details, see g_file_read() which is
1853 * the synchronous version of this call.
1855 * When the operation is finished, @callback will be called. You can then call
1856 * g_file_read_finish() to get the result of the operation.
1858 void
1859 g_file_read_async (GFile *file,
1860 int io_priority,
1861 GCancellable *cancellable,
1862 GAsyncReadyCallback callback,
1863 gpointer user_data)
1865 GFileIface *iface;
1867 g_return_if_fail (G_IS_FILE (file));
1869 iface = G_FILE_GET_IFACE (file);
1870 (* iface->read_async) (file,
1871 io_priority,
1872 cancellable,
1873 callback,
1874 user_data);
1878 * g_file_read_finish:
1879 * @file: input #GFile.
1880 * @res: a #GAsyncResult.
1881 * @error: a #GError, or %NULL
1883 * Finishes an asynchronous file read operation started with
1884 * g_file_read_async().
1886 * Returns: a #GFileInputStream or %NULL on error.
1887 * Free the returned object with g_object_unref().
1889 GFileInputStream *
1890 g_file_read_finish (GFile *file,
1891 GAsyncResult *res,
1892 GError **error)
1894 GFileIface *iface;
1896 g_return_val_if_fail (G_IS_FILE (file), NULL);
1897 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1899 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1901 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1902 if (g_simple_async_result_propagate_error (simple, error))
1903 return NULL;
1906 iface = G_FILE_GET_IFACE (file);
1907 return (* iface->read_finish) (file, res, error);
1911 * g_file_append_to_async:
1912 * @file: input #GFile.
1913 * @flags: a set of #GFileCreateFlags.
1914 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1915 * of the request.
1916 * @cancellable: optional #GCancellable object, %NULL to ignore.
1917 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1918 * @user_data: the data to pass to callback function
1920 * Asynchronously opens @file for appending.
1922 * For more details, see g_file_append_to() which is
1923 * the synchronous version of this call.
1925 * When the operation is finished, @callback will be called. You can then call
1926 * g_file_append_to_finish() to get the result of the operation.
1928 void
1929 g_file_append_to_async (GFile *file,
1930 GFileCreateFlags flags,
1931 int io_priority,
1932 GCancellable *cancellable,
1933 GAsyncReadyCallback callback,
1934 gpointer user_data)
1936 GFileIface *iface;
1938 g_return_if_fail (G_IS_FILE (file));
1940 iface = G_FILE_GET_IFACE (file);
1941 (* iface->append_to_async) (file,
1942 flags,
1943 io_priority,
1944 cancellable,
1945 callback,
1946 user_data);
1950 * g_file_append_to_finish:
1951 * @file: input #GFile.
1952 * @res: #GAsyncResult
1953 * @error: a #GError, or %NULL
1955 * Finishes an asynchronous file append operation started with
1956 * g_file_append_to_async().
1958 * Returns: a valid #GFileOutputStream or %NULL on error.
1959 * Free the returned object with g_object_unref().
1961 GFileOutputStream *
1962 g_file_append_to_finish (GFile *file,
1963 GAsyncResult *res,
1964 GError **error)
1966 GFileIface *iface;
1968 g_return_val_if_fail (G_IS_FILE (file), NULL);
1969 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
1971 if (G_IS_SIMPLE_ASYNC_RESULT (res))
1973 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
1974 if (g_simple_async_result_propagate_error (simple, error))
1975 return NULL;
1978 iface = G_FILE_GET_IFACE (file);
1979 return (* iface->append_to_finish) (file, res, error);
1983 * g_file_create_async:
1984 * @file: input #GFile.
1985 * @flags: a set of #GFileCreateFlags.
1986 * @io_priority: the <link linkend="io-priority">I/O priority</link>
1987 * of the request.
1988 * @cancellable: optional #GCancellable object, %NULL to ignore.
1989 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
1990 * @user_data: the data to pass to callback function
1992 * Asynchronously creates a new file and returns an output stream for writing to it.
1993 * The file must not already exist.
1995 * For more details, see g_file_create() which is
1996 * the synchronous version of this call.
1998 * When the operation is finished, @callback will be called. You can then call
1999 * g_file_create_finish() to get the result of the operation.
2001 void
2002 g_file_create_async (GFile *file,
2003 GFileCreateFlags flags,
2004 int io_priority,
2005 GCancellable *cancellable,
2006 GAsyncReadyCallback callback,
2007 gpointer user_data)
2009 GFileIface *iface;
2011 g_return_if_fail (G_IS_FILE (file));
2013 iface = G_FILE_GET_IFACE (file);
2014 (* iface->create_async) (file,
2015 flags,
2016 io_priority,
2017 cancellable,
2018 callback,
2019 user_data);
2023 * g_file_create_finish:
2024 * @file: input #GFile.
2025 * @res: a #GAsyncResult.
2026 * @error: a #GError, or %NULL
2028 * Finishes an asynchronous file create operation started with
2029 * g_file_create_async().
2031 * Returns: a #GFileOutputStream or %NULL on error.
2032 * Free the returned object with g_object_unref().
2034 GFileOutputStream *
2035 g_file_create_finish (GFile *file,
2036 GAsyncResult *res,
2037 GError **error)
2039 GFileIface *iface;
2041 g_return_val_if_fail (G_IS_FILE (file), NULL);
2042 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2044 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2046 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2047 if (g_simple_async_result_propagate_error (simple, error))
2048 return NULL;
2051 iface = G_FILE_GET_IFACE (file);
2052 return (* iface->create_finish) (file, res, error);
2056 * g_file_replace_async:
2057 * @file: input #GFile.
2058 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2059 * current #GFile, or NULL to ignore.
2060 * @make_backup: %TRUE if a backup should be created.
2061 * @flags: a set of #GFileCreateFlags.
2062 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2063 * of the request.
2064 * @cancellable: optional #GCancellable object, %NULL to ignore.
2065 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2066 * @user_data: the data to pass to callback function
2068 * Asynchronously overwrites the file, replacing the contents, possibly
2069 * creating a backup copy of the file first.
2071 * For more details, see g_file_replace() which is
2072 * the synchronous version of this call.
2074 * When the operation is finished, @callback will be called. You can then call
2075 * g_file_replace_finish() to get the result of the operation.
2077 void
2078 g_file_replace_async (GFile *file,
2079 const char *etag,
2080 gboolean make_backup,
2081 GFileCreateFlags flags,
2082 int io_priority,
2083 GCancellable *cancellable,
2084 GAsyncReadyCallback callback,
2085 gpointer user_data)
2087 GFileIface *iface;
2089 g_return_if_fail (G_IS_FILE (file));
2091 iface = G_FILE_GET_IFACE (file);
2092 (* iface->replace_async) (file,
2093 etag,
2094 make_backup,
2095 flags,
2096 io_priority,
2097 cancellable,
2098 callback,
2099 user_data);
2103 * g_file_replace_finish:
2104 * @file: input #GFile.
2105 * @res: a #GAsyncResult.
2106 * @error: a #GError, or %NULL
2108 * Finishes an asynchronous file replace operation started with
2109 * g_file_replace_async().
2111 * Returns: a #GFileOutputStream, or %NULL on error.
2112 * Free the returned object with g_object_unref().
2114 GFileOutputStream *
2115 g_file_replace_finish (GFile *file,
2116 GAsyncResult *res,
2117 GError **error)
2119 GFileIface *iface;
2121 g_return_val_if_fail (G_IS_FILE (file), NULL);
2122 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2124 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2126 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2127 if (g_simple_async_result_propagate_error (simple, error))
2128 return NULL;
2131 iface = G_FILE_GET_IFACE (file);
2132 return (* iface->replace_finish) (file, res, error);
2137 * g_file_open_readwrite_async:
2138 * @file: input #GFile.
2139 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2140 * of the request.
2141 * @cancellable: optional #GCancellable object, %NULL to ignore.
2142 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2143 * @user_data: the data to pass to callback function
2145 * Asynchronously opens @file for reading and writing.
2147 * For more details, see g_file_open_readwrite() which is
2148 * the synchronous version of this call.
2150 * When the operation is finished, @callback will be called. You can then call
2151 * g_file_open_readwrite_finish() to get the result of the operation.
2153 * Since: 2.22
2155 void
2156 g_file_open_readwrite_async (GFile *file,
2157 int io_priority,
2158 GCancellable *cancellable,
2159 GAsyncReadyCallback callback,
2160 gpointer user_data)
2162 GFileIface *iface;
2164 g_return_if_fail (G_IS_FILE (file));
2166 iface = G_FILE_GET_IFACE (file);
2167 (* iface->open_readwrite_async) (file,
2168 io_priority,
2169 cancellable,
2170 callback,
2171 user_data);
2175 * g_file_open_readwrite_finish:
2176 * @file: input #GFile.
2177 * @res: a #GAsyncResult.
2178 * @error: a #GError, or %NULL
2180 * Finishes an asynchronous file read operation started with
2181 * g_file_open_readwrite_async().
2183 * Returns: a #GFileIOStream or %NULL on error.
2184 * Free the returned object with g_object_unref().
2186 * Since: 2.22
2188 GFileIOStream *
2189 g_file_open_readwrite_finish (GFile *file,
2190 GAsyncResult *res,
2191 GError **error)
2193 GFileIface *iface;
2195 g_return_val_if_fail (G_IS_FILE (file), NULL);
2196 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2198 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2200 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2201 if (g_simple_async_result_propagate_error (simple, error))
2202 return NULL;
2205 iface = G_FILE_GET_IFACE (file);
2206 return (* iface->open_readwrite_finish) (file, res, error);
2211 * g_file_create_readwrite_async:
2212 * @file: input #GFile
2213 * @flags: a set of #GFileCreateFlags
2214 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2215 * of the request
2216 * @cancellable: optional #GCancellable object, %NULL to ignore
2217 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2218 * @user_data: the data to pass to callback function
2220 * Asynchronously creates a new file and returns a stream for reading and
2221 * writing to it. The file must not already exist.
2223 * For more details, see g_file_create_readwrite() which is
2224 * the synchronous version of this call.
2226 * When the operation is finished, @callback will be called. You can then
2227 * call g_file_create_readwrite_finish() to get the result of the operation.
2229 * Since: 2.22
2231 void
2232 g_file_create_readwrite_async (GFile *file,
2233 GFileCreateFlags flags,
2234 int io_priority,
2235 GCancellable *cancellable,
2236 GAsyncReadyCallback callback,
2237 gpointer user_data)
2239 GFileIface *iface;
2241 g_return_if_fail (G_IS_FILE (file));
2243 iface = G_FILE_GET_IFACE (file);
2244 (* iface->create_readwrite_async) (file,
2245 flags,
2246 io_priority,
2247 cancellable,
2248 callback,
2249 user_data);
2253 * g_file_create_readwrite_finish:
2254 * @file: input #GFile
2255 * @res: a #GAsyncResult
2256 * @error: a #GError, or %NULL
2258 * Finishes an asynchronous file create operation started with
2259 * g_file_create_readwrite_async().
2261 * Returns: a #GFileIOStream or %NULL on error.
2262 * Free the returned object with g_object_unref().
2264 * Since: 2.22
2266 GFileIOStream *
2267 g_file_create_readwrite_finish (GFile *file,
2268 GAsyncResult *res,
2269 GError **error)
2271 GFileIface *iface;
2273 g_return_val_if_fail (G_IS_FILE (file), NULL);
2274 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2276 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2278 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2279 if (g_simple_async_result_propagate_error (simple, error))
2280 return NULL;
2283 iface = G_FILE_GET_IFACE (file);
2284 return (* iface->create_readwrite_finish) (file, res, error);
2288 * g_file_replace_readwrite_async:
2289 * @file: input #GFile.
2290 * @etag: (allow-none): an <link linkend="gfile-etag">entity tag</link> for the
2291 * current #GFile, or NULL to ignore.
2292 * @make_backup: %TRUE if a backup should be created.
2293 * @flags: a set of #GFileCreateFlags.
2294 * @io_priority: the <link linkend="io-priority">I/O priority</link>
2295 * of the request.
2296 * @cancellable: optional #GCancellable object, %NULL to ignore.
2297 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
2298 * @user_data: the data to pass to callback function
2300 * Asynchronously overwrites the file in read-write mode, replacing the
2301 * contents, possibly creating a backup copy of the file first.
2303 * For more details, see g_file_replace_readwrite() which is
2304 * the synchronous version of this call.
2306 * When the operation is finished, @callback will be called. You can then
2307 * call g_file_replace_readwrite_finish() to get the result of the operation.
2309 * Since: 2.22
2311 void
2312 g_file_replace_readwrite_async (GFile *file,
2313 const char *etag,
2314 gboolean make_backup,
2315 GFileCreateFlags flags,
2316 int io_priority,
2317 GCancellable *cancellable,
2318 GAsyncReadyCallback callback,
2319 gpointer user_data)
2321 GFileIface *iface;
2323 g_return_if_fail (G_IS_FILE (file));
2325 iface = G_FILE_GET_IFACE (file);
2326 (* iface->replace_readwrite_async) (file,
2327 etag,
2328 make_backup,
2329 flags,
2330 io_priority,
2331 cancellable,
2332 callback,
2333 user_data);
2337 * g_file_replace_readwrite_finish:
2338 * @file: input #GFile.
2339 * @res: a #GAsyncResult.
2340 * @error: a #GError, or %NULL
2342 * Finishes an asynchronous file replace operation started with
2343 * g_file_replace_readwrite_async().
2345 * Returns: a #GFileIOStream, or %NULL on error.
2346 * Free the returned object with g_object_unref().
2348 * Since: 2.22
2350 GFileIOStream *
2351 g_file_replace_readwrite_finish (GFile *file,
2352 GAsyncResult *res,
2353 GError **error)
2355 GFileIface *iface;
2357 g_return_val_if_fail (G_IS_FILE (file), NULL);
2358 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
2360 if (G_IS_SIMPLE_ASYNC_RESULT (res))
2362 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
2363 if (g_simple_async_result_propagate_error (simple, error))
2364 return NULL;
2367 iface = G_FILE_GET_IFACE (file);
2368 return (* iface->replace_readwrite_finish) (file, res, error);
2371 static gboolean
2372 copy_symlink (GFile *destination,
2373 GFileCopyFlags flags,
2374 GCancellable *cancellable,
2375 const char *target,
2376 GError **error)
2378 GError *my_error;
2379 gboolean tried_delete;
2380 GFileInfo *info;
2381 GFileType file_type;
2383 tried_delete = FALSE;
2385 retry:
2386 my_error = NULL;
2387 if (!g_file_make_symbolic_link (destination, target, cancellable, &my_error))
2389 /* Maybe it already existed, and we want to overwrite? */
2390 if (!tried_delete && (flags & G_FILE_COPY_OVERWRITE) &&
2391 my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_EXISTS)
2393 g_error_free (my_error);
2396 /* Don't overwrite if the destination is a directory */
2397 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2398 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2399 cancellable, &my_error);
2400 if (info != NULL)
2402 file_type = g_file_info_get_file_type (info);
2403 g_object_unref (info);
2405 if (file_type == G_FILE_TYPE_DIRECTORY)
2407 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY,
2408 _("Can't copy over directory"));
2409 return FALSE;
2413 if (!g_file_delete (destination, cancellable, error))
2414 return FALSE;
2416 tried_delete = TRUE;
2417 goto retry;
2419 /* Nah, fail */
2420 g_propagate_error (error, my_error);
2421 return FALSE;
2424 return TRUE;
2427 static GInputStream *
2428 open_source_for_copy (GFile *source,
2429 GFile *destination,
2430 GFileCopyFlags flags,
2431 GCancellable *cancellable,
2432 GError **error)
2434 GError *my_error;
2435 GInputStream *in;
2436 GFileInfo *info;
2437 GFileType file_type;
2439 my_error = NULL;
2440 in = (GInputStream *)g_file_read (source, cancellable, &my_error);
2441 if (in != NULL)
2442 return in;
2444 /* There was an error opening the source, try to set a good error for it: */
2446 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_IS_DIRECTORY)
2448 /* The source is a directory, don't fail with WOULD_RECURSE immediately,
2449 * as that is less useful to the app. Better check for errors on the
2450 * target instead.
2452 g_error_free (my_error);
2453 my_error = NULL;
2455 info = g_file_query_info (destination, G_FILE_ATTRIBUTE_STANDARD_TYPE,
2456 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2457 cancellable, &my_error);
2458 if (info != NULL &&
2459 g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
2461 file_type = g_file_info_get_file_type (info);
2462 g_object_unref (info);
2464 if (flags & G_FILE_COPY_OVERWRITE)
2466 if (file_type == G_FILE_TYPE_DIRECTORY)
2468 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_MERGE,
2469 _("Can't copy directory over directory"));
2470 return NULL;
2472 /* continue to would_recurse error */
2474 else
2476 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
2477 _("Target file exists"));
2478 return NULL;
2481 else
2483 /* Error getting info from target, return that error
2484 * (except for NOT_FOUND, which is no error here)
2486 if (my_error != NULL && !g_error_matches (my_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
2488 g_propagate_error (error, my_error);
2489 return NULL;
2491 g_clear_error (&my_error);
2494 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE,
2495 _("Can't recursively copy directory"));
2496 return NULL;
2499 g_propagate_error (error, my_error);
2500 return NULL;
2503 static gboolean
2504 should_copy (GFileAttributeInfo *info,
2505 gboolean as_move,
2506 gboolean skip_perms)
2508 if (skip_perms && strcmp(info->name, "unix::mode") == 0)
2509 return FALSE;
2511 if (as_move)
2512 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED;
2513 return info->flags & G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE;
2516 static char *
2517 build_attribute_list_for_copy (GFileAttributeInfoList *attributes,
2518 GFileAttributeInfoList *namespaces,
2519 gboolean as_move,
2520 gboolean skip_perms)
2522 GString *s;
2523 gboolean first;
2524 int i;
2526 first = TRUE;
2527 s = g_string_new ("");
2529 if (attributes)
2531 for (i = 0; i < attributes->n_infos; i++)
2533 if (should_copy (&attributes->infos[i], as_move, skip_perms))
2535 if (first)
2536 first = FALSE;
2537 else
2538 g_string_append_c (s, ',');
2540 g_string_append (s, attributes->infos[i].name);
2545 if (namespaces)
2547 for (i = 0; i < namespaces->n_infos; i++)
2549 if (should_copy (&namespaces->infos[i], as_move, FALSE))
2551 if (first)
2552 first = FALSE;
2553 else
2554 g_string_append_c (s, ',');
2556 g_string_append (s, namespaces->infos[i].name);
2557 g_string_append (s, "::*");
2562 return g_string_free (s, FALSE);
2566 * g_file_copy_attributes:
2567 * @source: a #GFile with attributes.
2568 * @destination: a #GFile to copy attributes to.
2569 * @flags: a set of #GFileCopyFlags.
2570 * @cancellable: optional #GCancellable object, %NULL to ignore.
2571 * @error: a #GError, %NULL to ignore.
2573 * Copies the file attributes from @source to @destination.
2575 * Normally only a subset of the file attributes are copied,
2576 * those that are copies in a normal file copy operation
2577 * (which for instance does not include e.g. owner). However
2578 * if #G_FILE_COPY_ALL_METADATA is specified in @flags, then
2579 * all the metadata that is possible to copy is copied. This
2580 * is useful when implementing move by copy + delete source.
2582 * Returns: %TRUE if the attributes were copied successfully, %FALSE otherwise.
2584 gboolean
2585 g_file_copy_attributes (GFile *source,
2586 GFile *destination,
2587 GFileCopyFlags flags,
2588 GCancellable *cancellable,
2589 GError **error)
2591 GFileAttributeInfoList *attributes, *namespaces;
2592 char *attrs_to_read;
2593 gboolean res;
2594 GFileInfo *info;
2595 gboolean as_move;
2596 gboolean source_nofollow_symlinks;
2597 gboolean skip_perms;
2599 as_move = flags & G_FILE_COPY_ALL_METADATA;
2600 source_nofollow_symlinks = flags & G_FILE_COPY_NOFOLLOW_SYMLINKS;
2601 skip_perms = (flags & G_FILE_COPY_TARGET_DEFAULT_PERMS) != 0;
2603 /* Ignore errors here, if the target supports no attributes there is nothing to copy */
2604 attributes = g_file_query_settable_attributes (destination, cancellable, NULL);
2605 namespaces = g_file_query_writable_namespaces (destination, cancellable, NULL);
2607 if (attributes == NULL && namespaces == NULL)
2608 return TRUE;
2610 attrs_to_read = build_attribute_list_for_copy (attributes, namespaces, as_move, skip_perms);
2612 /* Ignore errors here, if we can't read some info (e.g. if it doesn't exist)
2613 * we just don't copy it.
2615 info = g_file_query_info (source, attrs_to_read,
2616 source_nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS:0,
2617 cancellable,
2618 NULL);
2620 g_free (attrs_to_read);
2622 res = TRUE;
2623 if (info)
2625 res = g_file_set_attributes_from_info (destination,
2626 info,
2627 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2628 cancellable,
2629 error);
2630 g_object_unref (info);
2633 g_file_attribute_info_list_unref (attributes);
2634 g_file_attribute_info_list_unref (namespaces);
2636 return res;
2639 static gboolean
2640 copy_stream_with_progress (GInputStream *in,
2641 GOutputStream *out,
2642 GFile *source,
2643 GCancellable *cancellable,
2644 GFileProgressCallback progress_callback,
2645 gpointer progress_callback_data,
2646 GError **error)
2648 gssize n_read, n_written;
2649 goffset current_size;
2650 char buffer[1024*64], *p;
2651 gboolean res;
2652 goffset total_size;
2653 GFileInfo *info;
2655 total_size = -1;
2656 /* avoid performance impact of querying total size when it's not needed */
2657 if (progress_callback)
2659 info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (in),
2660 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2661 cancellable, NULL);
2662 if (info)
2664 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2665 total_size = g_file_info_get_size (info);
2666 g_object_unref (info);
2669 if (total_size == -1)
2671 info = g_file_query_info (source,
2672 G_FILE_ATTRIBUTE_STANDARD_SIZE,
2673 G_FILE_QUERY_INFO_NONE,
2674 cancellable, NULL);
2675 if (info)
2677 if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
2678 total_size = g_file_info_get_size (info);
2679 g_object_unref (info);
2684 if (total_size == -1)
2685 total_size = 0;
2687 current_size = 0;
2688 res = TRUE;
2689 while (TRUE)
2691 n_read = g_input_stream_read (in, buffer, sizeof (buffer), cancellable, error);
2692 if (n_read == -1)
2694 res = FALSE;
2695 break;
2698 if (n_read == 0)
2699 break;
2701 current_size += n_read;
2703 p = buffer;
2704 while (n_read > 0)
2706 n_written = g_output_stream_write (out, p, n_read, cancellable, error);
2707 if (n_written == -1)
2709 res = FALSE;
2710 break;
2713 p += n_written;
2714 n_read -= n_written;
2717 if (!res)
2718 break;
2720 if (progress_callback)
2721 progress_callback (current_size, total_size, progress_callback_data);
2724 /* Make sure we send full copied size */
2725 if (progress_callback)
2726 progress_callback (current_size, total_size, progress_callback_data);
2728 return res;
2731 #ifdef HAVE_SPLICE
2733 static gboolean
2734 do_splice (int fd_in,
2735 loff_t *off_in,
2736 int fd_out,
2737 loff_t *off_out,
2738 size_t len,
2739 long *bytes_transferd,
2740 GError **error)
2742 long result;
2744 retry:
2745 result = splice (fd_in, off_in, fd_out, off_out, len, SPLICE_F_MORE);
2747 if (result == -1)
2749 int errsv = errno;
2751 if (errsv == EINTR)
2752 goto retry;
2753 else if (errsv == ENOSYS || errsv == EINVAL)
2754 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2755 _("Splice not supported"));
2756 else
2757 g_set_error (error, G_IO_ERROR,
2758 g_io_error_from_errno (errsv),
2759 _("Error splicing file: %s"),
2760 g_strerror (errsv));
2762 return FALSE;
2765 *bytes_transferd = result;
2766 return TRUE;
2769 static gboolean
2770 splice_stream_with_progress (GInputStream *in,
2771 GOutputStream *out,
2772 GCancellable *cancellable,
2773 GFileProgressCallback progress_callback,
2774 gpointer progress_callback_data,
2775 GError **error)
2777 int buffer[2];
2778 gboolean res;
2779 goffset total_size;
2780 loff_t offset_in;
2781 loff_t offset_out;
2782 int fd_in, fd_out;
2784 fd_in = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (in));
2785 fd_out = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (out));
2787 if (pipe (buffer) != 0)
2789 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2790 "Pipe creation failed");
2791 return FALSE;
2794 total_size = -1;
2795 /* avoid performance impact of querying total size when it's not needed */
2796 if (progress_callback)
2798 struct stat sbuf;
2800 if (fstat (fd_in, &sbuf) == 0)
2801 total_size = sbuf.st_size;
2804 if (total_size == -1)
2805 total_size = 0;
2807 offset_in = offset_out = 0;
2808 res = FALSE;
2809 while (TRUE)
2811 long n_read;
2812 long n_written;
2814 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2815 break;
2817 if (!do_splice (fd_in, &offset_in, buffer[1], NULL, 1024*64, &n_read, error))
2818 break;
2820 if (n_read == 0)
2822 res = TRUE;
2823 break;
2826 while (n_read > 0)
2828 if (g_cancellable_set_error_if_cancelled (cancellable, error))
2829 goto out;
2831 if (!do_splice (buffer[0], NULL, fd_out, &offset_out, n_read, &n_written, error))
2832 goto out;
2834 n_read -= n_written;
2837 if (progress_callback)
2838 progress_callback (offset_in, total_size, progress_callback_data);
2841 /* Make sure we send full copied size */
2842 if (progress_callback)
2843 progress_callback (offset_in, total_size, progress_callback_data);
2845 out:
2846 close (buffer[0]);
2847 close (buffer[1]);
2849 return res;
2851 #endif
2853 static gboolean
2854 file_copy_fallback (GFile *source,
2855 GFile *destination,
2856 GFileCopyFlags flags,
2857 GCancellable *cancellable,
2858 GFileProgressCallback progress_callback,
2859 gpointer progress_callback_data,
2860 GError **error)
2862 GInputStream *in;
2863 GOutputStream *out;
2864 GFileInfo *info;
2865 const char *target;
2866 gboolean result;
2867 #ifdef HAVE_SPLICE
2868 gboolean fallback = TRUE;
2869 #endif
2871 /* need to know the file type */
2872 info = g_file_query_info (source,
2873 G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
2874 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
2875 cancellable,
2876 error);
2878 if (info == NULL)
2879 return FALSE;
2881 /* Maybe copy the symlink? */
2882 if ((flags & G_FILE_COPY_NOFOLLOW_SYMLINKS) &&
2883 g_file_info_get_file_type (info) == G_FILE_TYPE_SYMBOLIC_LINK)
2885 target = g_file_info_get_symlink_target (info);
2886 if (target)
2888 if (!copy_symlink (destination, flags, cancellable, target, error))
2890 g_object_unref (info);
2891 return FALSE;
2894 g_object_unref (info);
2895 goto copied_file;
2897 /* ... else fall back on a regular file copy */
2898 g_object_unref (info);
2900 /* Handle "special" files (pipes, device nodes, ...)? */
2901 else if (g_file_info_get_file_type (info) == G_FILE_TYPE_SPECIAL)
2903 /* FIXME: could try to recreate device nodes and others? */
2905 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
2906 _("Can't copy special file"));
2907 g_object_unref (info);
2908 return FALSE;
2910 /* Everything else should just fall back on a regular copy. */
2911 else
2912 g_object_unref (info);
2914 in = open_source_for_copy (source, destination, flags, cancellable, error);
2915 if (in == NULL)
2916 return FALSE;
2918 if (flags & G_FILE_COPY_OVERWRITE)
2920 out = (GOutputStream *)g_file_replace (destination,
2921 NULL,
2922 flags & G_FILE_COPY_BACKUP,
2923 G_FILE_CREATE_REPLACE_DESTINATION,
2924 cancellable, error);
2926 else
2928 out = (GOutputStream *)g_file_create (destination, 0, cancellable, error);
2931 if (out == NULL)
2933 g_object_unref (in);
2934 return FALSE;
2937 #ifdef HAVE_SPLICE
2938 if (G_IS_FILE_DESCRIPTOR_BASED (in) && G_IS_FILE_DESCRIPTOR_BASED (out))
2940 GError *splice_err = NULL;
2942 result = splice_stream_with_progress (in, out, cancellable,
2943 progress_callback, progress_callback_data,
2944 &splice_err);
2946 if (result || !g_error_matches (splice_err, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
2948 fallback = FALSE;
2949 if (!result)
2950 g_propagate_error (error, splice_err);
2952 else
2953 g_clear_error (&splice_err);
2956 if (fallback)
2957 #endif
2958 result = copy_stream_with_progress (in, out, source, cancellable,
2959 progress_callback, progress_callback_data,
2960 error);
2962 /* Don't care about errors in source here */
2963 g_input_stream_close (in, cancellable, NULL);
2965 /* But write errors on close are bad! */
2966 if (!g_output_stream_close (out, cancellable, result ? error : NULL))
2967 result = FALSE;
2969 g_object_unref (in);
2970 g_object_unref (out);
2972 if (result == FALSE)
2973 return FALSE;
2975 copied_file:
2976 /* Ignore errors here. Failure to copy metadata is not a hard error */
2977 g_file_copy_attributes (source, destination,
2978 flags, cancellable, NULL);
2980 return TRUE;
2984 * g_file_copy:
2985 * @source: input #GFile.
2986 * @destination: destination #GFile
2987 * @flags: set of #GFileCopyFlags
2988 * @cancellable: optional #GCancellable object, %NULL to ignore.
2989 * @progress_callback: function to callback with progress information
2990 * @progress_callback_data: user data to pass to @progress_callback
2991 * @error: #GError to set on error, or %NULL
2993 * Copies the file @source to the location specified by @destination.
2994 * Can not handle recursive copies of directories.
2996 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
2997 * existing @destination file is overwritten.
2999 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3000 * will be copied as symlinks, otherwise the target of the
3001 * @source symlink will be copied.
3003 * If @cancellable is not %NULL, then the operation can be cancelled by
3004 * triggering the cancellable object from another thread. If the operation
3005 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3007 * If @progress_callback is not %NULL, then the operation can be monitored by
3008 * setting this to a #GFileProgressCallback function. @progress_callback_data
3009 * will be passed to this function. It is guaranteed that this callback will
3010 * be called after all data has been transferred with the total number of bytes
3011 * copied during the operation.
3013 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3014 * error is returned, independent on the status of the @destination.
3016 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3017 * error G_IO_ERROR_EXISTS is returned.
3019 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3020 * error is returned. If trying to overwrite a directory with a directory the
3021 * G_IO_ERROR_WOULD_MERGE error is returned.
3023 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3024 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3025 * is returned.
3027 * If you are interested in copying the #GFile object itself (not the on-disk
3028 * file), see g_file_dup().
3030 * Returns: %TRUE on success, %FALSE otherwise.
3032 gboolean
3033 g_file_copy (GFile *source,
3034 GFile *destination,
3035 GFileCopyFlags flags,
3036 GCancellable *cancellable,
3037 GFileProgressCallback progress_callback,
3038 gpointer progress_callback_data,
3039 GError **error)
3041 GFileIface *iface;
3042 GError *my_error;
3043 gboolean res;
3045 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3046 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3048 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3049 return FALSE;
3051 iface = G_FILE_GET_IFACE (destination);
3052 if (iface->copy)
3054 my_error = NULL;
3055 res = (* iface->copy) (source, destination,
3056 flags, cancellable,
3057 progress_callback, progress_callback_data,
3058 &my_error);
3060 if (res)
3061 return TRUE;
3063 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3065 g_propagate_error (error, my_error);
3066 return FALSE;
3068 else
3069 g_clear_error (&my_error);
3072 /* If the types are different, and the destination method failed
3073 also try the source method */
3074 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3076 iface = G_FILE_GET_IFACE (source);
3078 if (iface->copy)
3080 my_error = NULL;
3081 res = (* iface->copy) (source, destination,
3082 flags, cancellable,
3083 progress_callback, progress_callback_data,
3084 &my_error);
3086 if (res)
3087 return TRUE;
3089 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3091 g_propagate_error (error, my_error);
3092 return FALSE;
3094 else
3095 g_clear_error (&my_error);
3099 return file_copy_fallback (source, destination, flags, cancellable,
3100 progress_callback, progress_callback_data,
3101 error);
3105 * g_file_copy_async:
3106 * @source: input #GFile.
3107 * @destination: destination #GFile
3108 * @flags: set of #GFileCopyFlags
3109 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3110 * of the request.
3111 * @cancellable: optional #GCancellable object, %NULL to ignore.
3112 * @progress_callback: function to callback with progress information
3113 * @progress_callback_data: user data to pass to @progress_callback
3114 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3115 * @user_data: the data to pass to callback function
3117 * Copies the file @source to the location specified by @destination
3118 * asynchronously. For details of the behaviour, see g_file_copy().
3120 * If @progress_callback is not %NULL, then that function that will be called
3121 * just like in g_file_copy(), however the callback will run in the main loop,
3122 * not in the thread that is doing the I/O operation.
3124 * When the operation is finished, @callback will be called. You can then call
3125 * g_file_copy_finish() to get the result of the operation.
3127 void
3128 g_file_copy_async (GFile *source,
3129 GFile *destination,
3130 GFileCopyFlags flags,
3131 int io_priority,
3132 GCancellable *cancellable,
3133 GFileProgressCallback progress_callback,
3134 gpointer progress_callback_data,
3135 GAsyncReadyCallback callback,
3136 gpointer user_data)
3138 GFileIface *iface;
3140 g_return_if_fail (G_IS_FILE (source));
3141 g_return_if_fail (G_IS_FILE (destination));
3143 iface = G_FILE_GET_IFACE (source);
3144 (* iface->copy_async) (source,
3145 destination,
3146 flags,
3147 io_priority,
3148 cancellable,
3149 progress_callback,
3150 progress_callback_data,
3151 callback,
3152 user_data);
3156 * g_file_copy_finish:
3157 * @file: input #GFile.
3158 * @res: a #GAsyncResult.
3159 * @error: a #GError, or %NULL
3161 * Finishes copying the file started with
3162 * g_file_copy_async().
3164 * Returns: a %TRUE on success, %FALSE on error.
3166 gboolean
3167 g_file_copy_finish (GFile *file,
3168 GAsyncResult *res,
3169 GError **error)
3171 GFileIface *iface;
3173 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3174 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), FALSE);
3176 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3178 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3180 if (g_simple_async_result_propagate_error (simple, error))
3181 return FALSE;
3184 iface = G_FILE_GET_IFACE (file);
3185 return (* iface->copy_finish) (file, res, error);
3189 * g_file_move:
3190 * @source: #GFile pointing to the source location.
3191 * @destination: #GFile pointing to the destination location.
3192 * @flags: set of #GFileCopyFlags.
3193 * @cancellable: optional #GCancellable object, %NULL to ignore.
3194 * @progress_callback: #GFileProgressCallback function for updates.
3195 * @progress_callback_data: gpointer to user data for the callback function.
3196 * @error: #GError for returning error conditions, or %NULL
3199 * Tries to move the file or directory @source to the location specified by @destination.
3200 * If native move operations are supported then this is used, otherwise a copy + delete
3201 * fallback is used. The native implementation may support moving directories (for instance
3202 * on moves inside the same filesystem), but the fallback code does not.
3204 * If the flag #G_FILE_COPY_OVERWRITE is specified an already
3205 * existing @destination file is overwritten.
3207 * If the flag #G_FILE_COPY_NOFOLLOW_SYMLINKS is specified then symlinks
3208 * will be copied as symlinks, otherwise the target of the
3209 * @source symlink will be copied.
3211 * If @cancellable is not %NULL, then the operation can be cancelled by
3212 * triggering the cancellable object from another thread. If the operation
3213 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3215 * If @progress_callback is not %NULL, then the operation can be monitored by
3216 * setting this to a #GFileProgressCallback function. @progress_callback_data
3217 * will be passed to this function. It is guaranteed that this callback will
3218 * be called after all data has been transferred with the total number of bytes
3219 * copied during the operation.
3221 * If the @source file does not exist then the G_IO_ERROR_NOT_FOUND
3222 * error is returned, independent on the status of the @destination.
3224 * If #G_FILE_COPY_OVERWRITE is not specified and the target exists, then the
3225 * error G_IO_ERROR_EXISTS is returned.
3227 * If trying to overwrite a file over a directory the G_IO_ERROR_IS_DIRECTORY
3228 * error is returned. If trying to overwrite a directory with a directory the
3229 * G_IO_ERROR_WOULD_MERGE error is returned.
3231 * If the source is a directory and the target does not exist, or #G_FILE_COPY_OVERWRITE is
3232 * specified and the target is a file, then the G_IO_ERROR_WOULD_RECURSE error
3233 * may be returned (if the native move operation isn't available).
3235 * Returns: %TRUE on successful move, %FALSE otherwise.
3237 gboolean
3238 g_file_move (GFile *source,
3239 GFile *destination,
3240 GFileCopyFlags flags,
3241 GCancellable *cancellable,
3242 GFileProgressCallback progress_callback,
3243 gpointer progress_callback_data,
3244 GError **error)
3246 GFileIface *iface;
3247 GError *my_error;
3248 gboolean res;
3250 g_return_val_if_fail (G_IS_FILE (source), FALSE);
3251 g_return_val_if_fail (G_IS_FILE (destination), FALSE);
3253 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3254 return FALSE;
3256 iface = G_FILE_GET_IFACE (destination);
3257 if (iface->move)
3259 my_error = NULL;
3260 res = (* iface->move) (source, destination,
3261 flags, cancellable,
3262 progress_callback, progress_callback_data,
3263 &my_error);
3265 if (res)
3266 return TRUE;
3268 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3270 g_propagate_error (error, my_error);
3271 return FALSE;
3275 /* If the types are different, and the destination method failed
3276 also try the source method */
3277 if (G_OBJECT_TYPE (source) != G_OBJECT_TYPE (destination))
3279 iface = G_FILE_GET_IFACE (source);
3281 if (iface->move)
3283 my_error = NULL;
3284 res = (* iface->move) (source, destination,
3285 flags, cancellable,
3286 progress_callback, progress_callback_data,
3287 &my_error);
3289 if (res)
3290 return TRUE;
3292 if (my_error->domain != G_IO_ERROR || my_error->code != G_IO_ERROR_NOT_SUPPORTED)
3294 g_propagate_error (error, my_error);
3295 return FALSE;
3300 if (flags & G_FILE_COPY_NO_FALLBACK_FOR_MOVE)
3302 g_set_error_literal (error, G_IO_ERROR,
3303 G_IO_ERROR_NOT_SUPPORTED,
3304 _("Operation not supported"));
3305 return FALSE;
3308 flags |= G_FILE_COPY_ALL_METADATA;
3309 if (!g_file_copy (source, destination, flags, cancellable,
3310 progress_callback, progress_callback_data,
3311 error))
3312 return FALSE;
3314 return g_file_delete (source, cancellable, error);
3318 * g_file_make_directory
3319 * @file: input #GFile.
3320 * @cancellable: optional #GCancellable object, %NULL to ignore.
3321 * @error: a #GError, or %NULL
3323 * Creates a directory. Note that this will only create a child directory of
3324 * the immediate parent directory of the path or URI given by the #GFile. To
3325 * recursively create directories, see g_file_make_directory_with_parents().
3326 * This function will fail if the parent directory does not exist, setting
3327 * @error to %G_IO_ERROR_NOT_FOUND. If the file system doesn't support creating
3328 * directories, this function will fail, setting @error to
3329 * %G_IO_ERROR_NOT_SUPPORTED.
3331 * For a local #GFile the newly created directory will have the default
3332 * (current) ownership and permissions of the current process.
3334 * If @cancellable is not %NULL, then the operation can be cancelled by
3335 * triggering the cancellable object from another thread. If the operation
3336 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3338 * Returns: %TRUE on successful creation, %FALSE otherwise.
3340 gboolean
3341 g_file_make_directory (GFile *file,
3342 GCancellable *cancellable,
3343 GError **error)
3345 GFileIface *iface;
3347 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3350 return FALSE;
3352 iface = G_FILE_GET_IFACE (file);
3354 if (iface->make_directory == NULL)
3356 g_set_error_literal (error, G_IO_ERROR,
3357 G_IO_ERROR_NOT_SUPPORTED,
3358 _("Operation not supported"));
3359 return FALSE;
3362 return (* iface->make_directory) (file, cancellable, error);
3366 * g_file_make_directory_with_parents:
3367 * @file: input #GFile.
3368 * @cancellable: optional #GCancellable object, %NULL to ignore.
3369 * @error: a #GError, or %NULL
3371 * Creates a directory and any parent directories that may not exist similar to
3372 * 'mkdir -p'. If the file system does not support creating directories, this
3373 * function will fail, setting @error to %G_IO_ERROR_NOT_SUPPORTED.
3375 * For a local #GFile the newly created directories will have the default
3376 * (current) ownership and permissions of the current process.
3378 * If @cancellable is not %NULL, then the operation can be cancelled by
3379 * triggering the cancellable object from another thread. If the operation
3380 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3382 * Returns: %TRUE if all directories have been successfully created, %FALSE
3383 * otherwise.
3385 * Since: 2.18
3387 gboolean
3388 g_file_make_directory_with_parents (GFile *file,
3389 GCancellable *cancellable,
3390 GError **error)
3392 gboolean result;
3393 GFile *parent_file, *work_file;
3394 GList *list = NULL, *l;
3395 GError *my_error = NULL;
3397 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3398 return FALSE;
3400 result = g_file_make_directory (file, cancellable, &my_error);
3401 if (result || my_error->code != G_IO_ERROR_NOT_FOUND)
3403 if (my_error)
3404 g_propagate_error (error, my_error);
3405 return result;
3408 work_file = file;
3410 while (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3412 g_clear_error (&my_error);
3414 parent_file = g_file_get_parent (work_file);
3415 if (parent_file == NULL)
3416 break;
3417 result = g_file_make_directory (parent_file, cancellable, &my_error);
3419 if (!result && my_error->code == G_IO_ERROR_NOT_FOUND)
3420 list = g_list_prepend (list, parent_file);
3422 work_file = parent_file;
3425 for (l = list; result && l; l = l->next)
3427 result = g_file_make_directory ((GFile *) l->data, cancellable, &my_error);
3430 /* Clean up */
3431 while (list != NULL)
3433 g_object_unref ((GFile *) list->data);
3434 list = g_list_remove (list, list->data);
3437 if (!result)
3439 g_propagate_error (error, my_error);
3440 return result;
3443 return g_file_make_directory (file, cancellable, error);
3447 * g_file_make_symbolic_link:
3448 * @file: a #GFile with the name of the symlink to create
3449 * @symlink_value: a string with the path for the target of the new symlink
3450 * @cancellable: optional #GCancellable object, %NULL to ignore.
3451 * @error: a #GError.
3453 * Creates a symbolic link named @file which contains the string
3454 * @symlink_value.
3456 * If @cancellable is not %NULL, then the operation can be cancelled by
3457 * triggering the cancellable object from another thread. If the operation
3458 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3460 * Returns: %TRUE on the creation of a new symlink, %FALSE otherwise.
3462 gboolean
3463 g_file_make_symbolic_link (GFile *file,
3464 const char *symlink_value,
3465 GCancellable *cancellable,
3466 GError **error)
3468 GFileIface *iface;
3470 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3471 g_return_val_if_fail (symlink_value != NULL, FALSE);
3473 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3474 return FALSE;
3476 if (*symlink_value == '\0')
3478 g_set_error_literal (error, G_IO_ERROR,
3479 G_IO_ERROR_INVALID_ARGUMENT,
3480 _("Invalid symlink value given"));
3481 return FALSE;
3484 iface = G_FILE_GET_IFACE (file);
3486 if (iface->make_symbolic_link == NULL)
3488 g_set_error_literal (error, G_IO_ERROR,
3489 G_IO_ERROR_NOT_SUPPORTED,
3490 _("Operation not supported"));
3491 return FALSE;
3494 return (* iface->make_symbolic_link) (file, symlink_value, cancellable, error);
3498 * g_file_delete:
3499 * @file: input #GFile.
3500 * @cancellable: optional #GCancellable object, %NULL to ignore.
3501 * @error: a #GError, or %NULL
3503 * Deletes a file. If the @file is a directory, it will only be deleted if it
3504 * is empty.
3506 * If @cancellable is not %NULL, then the operation can be cancelled by
3507 * triggering the cancellable object from another thread. If the operation
3508 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3510 * Returns: %TRUE if the file was deleted. %FALSE otherwise.
3512 gboolean
3513 g_file_delete (GFile *file,
3514 GCancellable *cancellable,
3515 GError **error)
3517 GFileIface *iface;
3519 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3521 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3522 return FALSE;
3524 iface = G_FILE_GET_IFACE (file);
3526 if (iface->delete_file == NULL)
3528 g_set_error_literal (error, G_IO_ERROR,
3529 G_IO_ERROR_NOT_SUPPORTED,
3530 _("Operation not supported"));
3531 return FALSE;
3534 return (* iface->delete_file) (file, cancellable, error);
3538 * g_file_trash:
3539 * @file: #GFile to send to trash.
3540 * @cancellable: optional #GCancellable object, %NULL to ignore.
3541 * @error: a #GError, or %NULL
3543 * Sends @file to the "Trashcan", if possible. This is similar to
3544 * deleting it, but the user can recover it before emptying the trashcan.
3545 * Not all file systems support trashing, so this call can return the
3546 * %G_IO_ERROR_NOT_SUPPORTED error.
3549 * If @cancellable is not %NULL, then the operation can be cancelled by
3550 * triggering the cancellable object from another thread. If the operation
3551 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3553 * Returns: %TRUE on successful trash, %FALSE otherwise.
3555 gboolean
3556 g_file_trash (GFile *file,
3557 GCancellable *cancellable,
3558 GError **error)
3560 GFileIface *iface;
3562 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3564 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3565 return FALSE;
3567 iface = G_FILE_GET_IFACE (file);
3569 if (iface->trash == NULL)
3571 g_set_error_literal (error,
3572 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
3573 _("Trash not supported"));
3574 return FALSE;
3577 return (* iface->trash) (file, cancellable, error);
3581 * g_file_set_display_name:
3582 * @file: input #GFile.
3583 * @display_name: a string.
3584 * @cancellable: optional #GCancellable object, %NULL to ignore.
3585 * @error: a #GError, or %NULL
3587 * Renames @file to the specified display name.
3589 * The display name is converted from UTF8 to the correct encoding for the target
3590 * filesystem if possible and the @file is renamed to this.
3592 * If you want to implement a rename operation in the user interface the edit name
3593 * (#G_FILE_ATTRIBUTE_STANDARD_EDIT_NAME) should be used as the initial value in the rename
3594 * widget, and then the result after editing should be passed to g_file_set_display_name().
3596 * On success the resulting converted filename is returned.
3598 * If @cancellable is not %NULL, then the operation can be cancelled by
3599 * triggering the cancellable object from another thread. If the operation
3600 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3602 * Returns: a #GFile specifying what @file was renamed to, or %NULL
3603 * if there was an error.
3604 * Free the returned object with g_object_unref().
3606 GFile *
3607 g_file_set_display_name (GFile *file,
3608 const char *display_name,
3609 GCancellable *cancellable,
3610 GError **error)
3612 GFileIface *iface;
3614 g_return_val_if_fail (G_IS_FILE (file), NULL);
3615 g_return_val_if_fail (display_name != NULL, NULL);
3617 if (strchr (display_name, G_DIR_SEPARATOR) != NULL)
3619 g_set_error (error,
3620 G_IO_ERROR,
3621 G_IO_ERROR_INVALID_ARGUMENT,
3622 _("File names cannot contain '%c'"), G_DIR_SEPARATOR);
3623 return NULL;
3626 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3627 return NULL;
3629 iface = G_FILE_GET_IFACE (file);
3631 return (* iface->set_display_name) (file, display_name, cancellable, error);
3635 * g_file_set_display_name_async:
3636 * @file: input #GFile.
3637 * @display_name: a string.
3638 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3639 * of the request.
3640 * @cancellable: optional #GCancellable object, %NULL to ignore.
3641 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
3642 * @user_data: the data to pass to callback function
3644 * Asynchronously sets the display name for a given #GFile.
3646 * For more details, see g_file_set_display_name() which is
3647 * the synchronous version of this call.
3649 * When the operation is finished, @callback will be called. You can then call
3650 * g_file_set_display_name_finish() to get the result of the operation.
3652 void
3653 g_file_set_display_name_async (GFile *file,
3654 const char *display_name,
3655 int io_priority,
3656 GCancellable *cancellable,
3657 GAsyncReadyCallback callback,
3658 gpointer user_data)
3660 GFileIface *iface;
3662 g_return_if_fail (G_IS_FILE (file));
3663 g_return_if_fail (display_name != NULL);
3665 iface = G_FILE_GET_IFACE (file);
3666 (* iface->set_display_name_async) (file,
3667 display_name,
3668 io_priority,
3669 cancellable,
3670 callback,
3671 user_data);
3675 * g_file_set_display_name_finish:
3676 * @file: input #GFile.
3677 * @res: a #GAsyncResult.
3678 * @error: a #GError, or %NULL
3680 * Finishes setting a display name started with
3681 * g_file_set_display_name_async().
3683 * Returns: a #GFile or %NULL on error.
3684 * Free the returned object with g_object_unref().
3686 GFile *
3687 g_file_set_display_name_finish (GFile *file,
3688 GAsyncResult *res,
3689 GError **error)
3691 GFileIface *iface;
3693 g_return_val_if_fail (G_IS_FILE (file), NULL);
3694 g_return_val_if_fail (G_IS_ASYNC_RESULT (res), NULL);
3696 if (G_IS_SIMPLE_ASYNC_RESULT (res))
3698 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
3699 if (g_simple_async_result_propagate_error (simple, error))
3700 return NULL;
3703 iface = G_FILE_GET_IFACE (file);
3704 return (* iface->set_display_name_finish) (file, res, error);
3708 * g_file_query_settable_attributes:
3709 * @file: input #GFile.
3710 * @cancellable: optional #GCancellable object, %NULL to ignore.
3711 * @error: a #GError, or %NULL
3713 * Obtain the list of settable attributes for the file.
3715 * Returns the type and full attribute name of all the attributes
3716 * that can be set on this file. This doesn't mean setting it will always
3717 * succeed though, you might get an access failure, or some specific
3718 * file may not support a specific attribute.
3720 * If @cancellable is not %NULL, then the operation can be cancelled by
3721 * triggering the cancellable object from another thread. If the operation
3722 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3724 * Returns: a #GFileAttributeInfoList describing the settable attributes.
3725 * When you are done with it, release it with g_file_attribute_info_list_unref()
3727 GFileAttributeInfoList *
3728 g_file_query_settable_attributes (GFile *file,
3729 GCancellable *cancellable,
3730 GError **error)
3732 GFileIface *iface;
3733 GError *my_error;
3734 GFileAttributeInfoList *list;
3736 g_return_val_if_fail (G_IS_FILE (file), NULL);
3738 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3739 return NULL;
3741 iface = G_FILE_GET_IFACE (file);
3743 if (iface->query_settable_attributes == NULL)
3744 return g_file_attribute_info_list_new ();
3746 my_error = NULL;
3747 list = (* iface->query_settable_attributes) (file, cancellable, &my_error);
3749 if (list == NULL)
3751 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3753 list = g_file_attribute_info_list_new ();
3754 g_error_free (my_error);
3756 else
3757 g_propagate_error (error, my_error);
3760 return list;
3764 * g_file_query_writable_namespaces:
3765 * @file: input #GFile.
3766 * @cancellable: optional #GCancellable object, %NULL to ignore.
3767 * @error: a #GError, or %NULL
3769 * Obtain the list of attribute namespaces where new attributes
3770 * can be created by a user. An example of this is extended
3771 * attributes (in the "xattr" namespace).
3773 * If @cancellable is not %NULL, then the operation can be cancelled by
3774 * triggering the cancellable object from another thread. If the operation
3775 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3777 * Returns: a #GFileAttributeInfoList describing the writable namespaces.
3778 * When you are done with it, release it with g_file_attribute_info_list_unref()
3780 GFileAttributeInfoList *
3781 g_file_query_writable_namespaces (GFile *file,
3782 GCancellable *cancellable,
3783 GError **error)
3785 GFileIface *iface;
3786 GError *my_error;
3787 GFileAttributeInfoList *list;
3789 g_return_val_if_fail (G_IS_FILE (file), NULL);
3791 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3792 return NULL;
3794 iface = G_FILE_GET_IFACE (file);
3796 if (iface->query_writable_namespaces == NULL)
3797 return g_file_attribute_info_list_new ();
3799 my_error = NULL;
3800 list = (* iface->query_writable_namespaces) (file, cancellable, &my_error);
3802 if (list == NULL)
3804 if (my_error->domain == G_IO_ERROR && my_error->code == G_IO_ERROR_NOT_SUPPORTED)
3806 list = g_file_attribute_info_list_new ();
3807 g_error_free (my_error);
3809 else
3810 g_propagate_error (error, my_error);
3813 return list;
3817 * g_file_set_attribute:
3818 * @file: input #GFile.
3819 * @attribute: a string containing the attribute's name.
3820 * @type: The type of the attribute
3821 * @value_p: a pointer to the value (or the pointer itself if the type is a pointer type)
3822 * @flags: a set of #GFileQueryInfoFlags.
3823 * @cancellable: optional #GCancellable object, %NULL to ignore.
3824 * @error: a #GError, or %NULL
3826 * Sets an attribute in the file with attribute name @attribute to @value.
3828 * If @cancellable is not %NULL, then the operation can be cancelled by
3829 * triggering the cancellable object from another thread. If the operation
3830 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3832 * Returns: %TRUE if the attribute was set, %FALSE otherwise.
3834 gboolean
3835 g_file_set_attribute (GFile *file,
3836 const char *attribute,
3837 GFileAttributeType type,
3838 gpointer value_p,
3839 GFileQueryInfoFlags flags,
3840 GCancellable *cancellable,
3841 GError **error)
3843 GFileIface *iface;
3845 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3846 g_return_val_if_fail (attribute != NULL && *attribute != '\0', FALSE);
3848 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3849 return FALSE;
3851 iface = G_FILE_GET_IFACE (file);
3853 if (iface->set_attribute == NULL)
3855 g_set_error_literal (error, G_IO_ERROR,
3856 G_IO_ERROR_NOT_SUPPORTED,
3857 _("Operation not supported"));
3858 return FALSE;
3861 return (* iface->set_attribute) (file, attribute, type, value_p, flags, cancellable, error);
3865 * g_file_set_attributes_from_info:
3866 * @file: input #GFile.
3867 * @info: a #GFileInfo.
3868 * @flags: #GFileQueryInfoFlags
3869 * @cancellable: optional #GCancellable object, %NULL to ignore.
3870 * @error: a #GError, or %NULL
3872 * Tries to set all attributes in the #GFileInfo on the target values,
3873 * not stopping on the first error.
3875 * If there is any error during this operation then @error will be set to
3876 * the first error. Error on particular fields are flagged by setting
3877 * the "status" field in the attribute value to
3878 * %G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING, which means you can also detect
3879 * further errors.
3881 * If @cancellable is not %NULL, then the operation can be cancelled by
3882 * triggering the cancellable object from another thread. If the operation
3883 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
3885 * Returns: %TRUE if there was any error, %FALSE otherwise.
3887 gboolean
3888 g_file_set_attributes_from_info (GFile *file,
3889 GFileInfo *info,
3890 GFileQueryInfoFlags flags,
3891 GCancellable *cancellable,
3892 GError **error)
3894 GFileIface *iface;
3896 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3897 g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
3899 if (g_cancellable_set_error_if_cancelled (cancellable, error))
3900 return FALSE;
3902 g_file_info_clear_status (info);
3904 iface = G_FILE_GET_IFACE (file);
3906 return (* iface->set_attributes_from_info) (file,
3907 info,
3908 flags,
3909 cancellable,
3910 error);
3914 static gboolean
3915 g_file_real_set_attributes_from_info (GFile *file,
3916 GFileInfo *info,
3917 GFileQueryInfoFlags flags,
3918 GCancellable *cancellable,
3919 GError **error)
3921 char **attributes;
3922 int i;
3923 gboolean res;
3924 GFileAttributeValue *value;
3926 res = TRUE;
3928 attributes = g_file_info_list_attributes (info, NULL);
3930 for (i = 0; attributes[i] != NULL; i++)
3932 value = _g_file_info_get_attribute_value (info, attributes[i]);
3934 if (value->status != G_FILE_ATTRIBUTE_STATUS_UNSET)
3935 continue;
3937 if (!g_file_set_attribute (file, attributes[i],
3938 value->type, _g_file_attribute_value_peek_as_pointer (value),
3939 flags, cancellable, error))
3941 value->status = G_FILE_ATTRIBUTE_STATUS_ERROR_SETTING;
3942 res = FALSE;
3943 /* Don't set error multiple times */
3944 error = NULL;
3946 else
3947 value->status = G_FILE_ATTRIBUTE_STATUS_SET;
3950 g_strfreev (attributes);
3952 return res;
3956 * g_file_set_attributes_async:
3957 * @file: input #GFile.
3958 * @info: a #GFileInfo.
3959 * @flags: a #GFileQueryInfoFlags.
3960 * @io_priority: the <link linkend="io-priority">I/O priority</link>
3961 * of the request.
3962 * @cancellable: optional #GCancellable object, %NULL to ignore.
3963 * @callback: a #GAsyncReadyCallback.
3964 * @user_data: a #gpointer.
3966 * Asynchronously sets the attributes of @file with @info.
3968 * For more details, see g_file_set_attributes_from_info() which is
3969 * the synchronous version of this call.
3971 * When the operation is finished, @callback will be called. You can then call
3972 * g_file_set_attributes_finish() to get the result of the operation.
3974 void
3975 g_file_set_attributes_async (GFile *file,
3976 GFileInfo *info,
3977 GFileQueryInfoFlags flags,
3978 int io_priority,
3979 GCancellable *cancellable,
3980 GAsyncReadyCallback callback,
3981 gpointer user_data)
3983 GFileIface *iface;
3985 g_return_if_fail (G_IS_FILE (file));
3986 g_return_if_fail (G_IS_FILE_INFO (info));
3988 iface = G_FILE_GET_IFACE (file);
3989 (* iface->set_attributes_async) (file,
3990 info,
3991 flags,
3992 io_priority,
3993 cancellable,
3994 callback,
3995 user_data);
3999 * g_file_set_attributes_finish:
4000 * @file: input #GFile.
4001 * @result: a #GAsyncResult.
4002 * @info: (out) (transfer full): a #GFileInfo.
4003 * @error: a #GError, or %NULL
4005 * Finishes setting an attribute started in g_file_set_attributes_async().
4007 * Returns: %TRUE if the attributes were set correctly, %FALSE otherwise.
4009 gboolean
4010 g_file_set_attributes_finish (GFile *file,
4011 GAsyncResult *result,
4012 GFileInfo **info,
4013 GError **error)
4015 GFileIface *iface;
4017 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4018 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4020 /* No standard handling of errors here, as we must set info even
4021 * on errors
4023 iface = G_FILE_GET_IFACE (file);
4024 return (* iface->set_attributes_finish) (file, result, info, error);
4028 * g_file_set_attribute_string:
4029 * @file: input #GFile.
4030 * @attribute: a string containing the attribute's name.
4031 * @value: a string containing the attribute's value.
4032 * @flags: #GFileQueryInfoFlags.
4033 * @cancellable: optional #GCancellable object, %NULL to ignore.
4034 * @error: a #GError, or %NULL
4036 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_STRING to @value.
4037 * If @attribute is of a different type, this operation will fail.
4039 * If @cancellable is not %NULL, then the operation can be cancelled by
4040 * triggering the cancellable object from another thread. If the operation
4041 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4043 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4045 gboolean
4046 g_file_set_attribute_string (GFile *file,
4047 const char *attribute,
4048 const char *value,
4049 GFileQueryInfoFlags flags,
4050 GCancellable *cancellable,
4051 GError **error)
4053 return g_file_set_attribute (file, attribute,
4054 G_FILE_ATTRIBUTE_TYPE_STRING, (gpointer)value,
4055 flags, cancellable, error);
4059 * g_file_set_attribute_byte_string:
4060 * @file: input #GFile.
4061 * @attribute: a string containing the attribute's name.
4062 * @value: a string containing the attribute's new value.
4063 * @flags: a #GFileQueryInfoFlags.
4064 * @cancellable: optional #GCancellable object, %NULL to ignore.
4065 * @error: a #GError, or %NULL
4067 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_BYTE_STRING to @value.
4068 * If @attribute is of a different type, this operation will fail,
4069 * returning %FALSE.
4071 * If @cancellable is not %NULL, then the operation can be cancelled by
4072 * triggering the cancellable object from another thread. If the operation
4073 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4075 * Returns: %TRUE if the @attribute was successfully set to @value
4076 * in the @file, %FALSE otherwise.
4078 gboolean
4079 g_file_set_attribute_byte_string (GFile *file,
4080 const char *attribute,
4081 const char *value,
4082 GFileQueryInfoFlags flags,
4083 GCancellable *cancellable,
4084 GError **error)
4086 return g_file_set_attribute (file, attribute,
4087 G_FILE_ATTRIBUTE_TYPE_BYTE_STRING, (gpointer)value,
4088 flags, cancellable, error);
4092 * g_file_set_attribute_uint32:
4093 * @file: input #GFile.
4094 * @attribute: a string containing the attribute's name.
4095 * @value: a #guint32 containing the attribute's new value.
4096 * @flags: a #GFileQueryInfoFlags.
4097 * @cancellable: optional #GCancellable object, %NULL to ignore.
4098 * @error: a #GError, or %NULL
4100 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT32 to @value.
4101 * If @attribute is of a different type, this operation will fail.
4103 * If @cancellable is not %NULL, then the operation can be cancelled by
4104 * triggering the cancellable object from another thread. If the operation
4105 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4107 * Returns: %TRUE if the @attribute was successfully set to @value
4108 * in the @file, %FALSE otherwise.
4110 gboolean
4111 g_file_set_attribute_uint32 (GFile *file,
4112 const char *attribute,
4113 guint32 value,
4114 GFileQueryInfoFlags flags,
4115 GCancellable *cancellable,
4116 GError **error)
4118 return g_file_set_attribute (file, attribute,
4119 G_FILE_ATTRIBUTE_TYPE_UINT32, &value,
4120 flags, cancellable, error);
4124 * g_file_set_attribute_int32:
4125 * @file: input #GFile.
4126 * @attribute: a string containing the attribute's name.
4127 * @value: a #gint32 containing the attribute's new value.
4128 * @flags: a #GFileQueryInfoFlags.
4129 * @cancellable: optional #GCancellable object, %NULL to ignore.
4130 * @error: a #GError, or %NULL
4132 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT32 to @value.
4133 * If @attribute is of a different type, this operation will fail.
4135 * If @cancellable is not %NULL, then the operation can be cancelled by
4136 * triggering the cancellable object from another thread. If the operation
4137 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4139 * Returns: %TRUE if the @attribute was successfully set to @value
4140 * in the @file, %FALSE otherwise.
4142 gboolean
4143 g_file_set_attribute_int32 (GFile *file,
4144 const char *attribute,
4145 gint32 value,
4146 GFileQueryInfoFlags flags,
4147 GCancellable *cancellable,
4148 GError **error)
4150 return g_file_set_attribute (file, attribute,
4151 G_FILE_ATTRIBUTE_TYPE_INT32, &value,
4152 flags, cancellable, error);
4156 * g_file_set_attribute_uint64:
4157 * @file: input #GFile.
4158 * @attribute: a string containing the attribute's name.
4159 * @value: a #guint64 containing the attribute's new value.
4160 * @flags: a #GFileQueryInfoFlags.
4161 * @cancellable: optional #GCancellable object, %NULL to ignore.
4162 * @error: a #GError, or %NULL
4164 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_UINT64 to @value.
4165 * If @attribute is of a different type, this operation will fail.
4167 * If @cancellable is not %NULL, then the operation can be cancelled by
4168 * triggering the cancellable object from another thread. If the operation
4169 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4171 * Returns: %TRUE if the @attribute was successfully set to @value
4172 * in the @file, %FALSE otherwise.
4174 gboolean
4175 g_file_set_attribute_uint64 (GFile *file,
4176 const char *attribute,
4177 guint64 value,
4178 GFileQueryInfoFlags flags,
4179 GCancellable *cancellable,
4180 GError **error)
4182 return g_file_set_attribute (file, attribute,
4183 G_FILE_ATTRIBUTE_TYPE_UINT64, &value,
4184 flags, cancellable, error);
4188 * g_file_set_attribute_int64:
4189 * @file: input #GFile.
4190 * @attribute: a string containing the attribute's name.
4191 * @value: a #guint64 containing the attribute's new value.
4192 * @flags: a #GFileQueryInfoFlags.
4193 * @cancellable: optional #GCancellable object, %NULL to ignore.
4194 * @error: a #GError, or %NULL
4196 * Sets @attribute of type %G_FILE_ATTRIBUTE_TYPE_INT64 to @value.
4197 * If @attribute is of a different type, this operation will fail.
4199 * If @cancellable is not %NULL, then the operation can be cancelled by
4200 * triggering the cancellable object from another thread. If the operation
4201 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4203 * Returns: %TRUE if the @attribute was successfully set, %FALSE otherwise.
4205 gboolean
4206 g_file_set_attribute_int64 (GFile *file,
4207 const char *attribute,
4208 gint64 value,
4209 GFileQueryInfoFlags flags,
4210 GCancellable *cancellable,
4211 GError **error)
4213 return g_file_set_attribute (file, attribute,
4214 G_FILE_ATTRIBUTE_TYPE_INT64, &value,
4215 flags, cancellable, error);
4219 * g_file_mount_mountable:
4220 * @file: input #GFile.
4221 * @flags: flags affecting the operation
4222 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4223 * @cancellable: optional #GCancellable object, %NULL to ignore.
4224 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4225 * @user_data: the data to pass to callback function
4227 * Mounts a file of type G_FILE_TYPE_MOUNTABLE.
4228 * Using @mount_operation, you can request callbacks when, for instance,
4229 * passwords are needed during authentication.
4231 * If @cancellable is not %NULL, then the operation can be cancelled by
4232 * triggering the cancellable object from another thread. If the operation
4233 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4235 * When the operation is finished, @callback will be called. You can then call
4236 * g_file_mount_mountable_finish() to get the result of the operation.
4238 void
4239 g_file_mount_mountable (GFile *file,
4240 GMountMountFlags flags,
4241 GMountOperation *mount_operation,
4242 GCancellable *cancellable,
4243 GAsyncReadyCallback callback,
4244 gpointer user_data)
4246 GFileIface *iface;
4248 g_return_if_fail (G_IS_FILE (file));
4250 iface = G_FILE_GET_IFACE (file);
4252 if (iface->mount_mountable == NULL)
4254 g_simple_async_report_error_in_idle (G_OBJECT (file),
4255 callback,
4256 user_data,
4257 G_IO_ERROR,
4258 G_IO_ERROR_NOT_SUPPORTED,
4259 _("Operation not supported"));
4260 return;
4263 (* iface->mount_mountable) (file,
4264 flags,
4265 mount_operation,
4266 cancellable,
4267 callback,
4268 user_data);
4272 * g_file_mount_mountable_finish:
4273 * @file: input #GFile.
4274 * @result: a #GAsyncResult.
4275 * @error: a #GError, or %NULL
4277 * Finishes a mount operation. See g_file_mount_mountable() for details.
4279 * Finish an asynchronous mount operation that was started
4280 * with g_file_mount_mountable().
4282 * Returns: a #GFile or %NULL on error.
4283 * Free the returned object with g_object_unref().
4285 GFile *
4286 g_file_mount_mountable_finish (GFile *file,
4287 GAsyncResult *result,
4288 GError **error)
4290 GFileIface *iface;
4292 g_return_val_if_fail (G_IS_FILE (file), NULL);
4293 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
4295 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4297 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4298 if (g_simple_async_result_propagate_error (simple, error))
4299 return NULL;
4302 iface = G_FILE_GET_IFACE (file);
4303 return (* iface->mount_mountable_finish) (file, result, error);
4307 * g_file_unmount_mountable:
4308 * @file: input #GFile.
4309 * @flags: flags affecting the operation
4310 * @cancellable: optional #GCancellable object, %NULL to ignore.
4311 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4312 * @user_data: the data to pass to callback function
4314 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4316 * If @cancellable is not %NULL, then the operation can be cancelled by
4317 * triggering the cancellable object from another thread. If the operation
4318 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4320 * When the operation is finished, @callback will be called. You can then call
4321 * g_file_unmount_mountable_finish() to get the result of the operation.
4323 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation() instead.
4325 void
4326 g_file_unmount_mountable (GFile *file,
4327 GMountUnmountFlags flags,
4328 GCancellable *cancellable,
4329 GAsyncReadyCallback callback,
4330 gpointer user_data)
4332 GFileIface *iface;
4334 g_return_if_fail (G_IS_FILE (file));
4336 iface = G_FILE_GET_IFACE (file);
4338 if (iface->unmount_mountable == NULL)
4340 g_simple_async_report_error_in_idle (G_OBJECT (file),
4341 callback,
4342 user_data,
4343 G_IO_ERROR,
4344 G_IO_ERROR_NOT_SUPPORTED,
4345 _("Operation not supported"));
4346 return;
4349 (* iface->unmount_mountable) (file,
4350 flags,
4351 cancellable,
4352 callback,
4353 user_data);
4357 * g_file_unmount_mountable_finish:
4358 * @file: input #GFile.
4359 * @result: a #GAsyncResult.
4360 * @error: a #GError, or %NULL
4362 * Finishes an unmount operation, see g_file_unmount_mountable() for details.
4364 * Finish an asynchronous unmount operation that was started
4365 * with g_file_unmount_mountable().
4367 * Returns: %TRUE if the operation finished successfully. %FALSE
4368 * otherwise.
4370 * Deprecated: 2.22: Use g_file_unmount_mountable_with_operation_finish() instead.
4372 gboolean
4373 g_file_unmount_mountable_finish (GFile *file,
4374 GAsyncResult *result,
4375 GError **error)
4377 GFileIface *iface;
4379 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4380 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4382 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4384 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4385 if (g_simple_async_result_propagate_error (simple, error))
4386 return FALSE;
4389 iface = G_FILE_GET_IFACE (file);
4390 return (* iface->unmount_mountable_finish) (file, result, error);
4394 * g_file_unmount_mountable_with_operation:
4395 * @file: input #GFile.
4396 * @flags: flags affecting the operation
4397 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4398 * @cancellable: optional #GCancellable object, %NULL to ignore.
4399 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4400 * @user_data: the data to pass to callback function
4402 * Unmounts a file of type G_FILE_TYPE_MOUNTABLE.
4404 * If @cancellable is not %NULL, then the operation can be cancelled by
4405 * triggering the cancellable object from another thread. If the operation
4406 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4408 * When the operation is finished, @callback will be called. You can then call
4409 * g_file_unmount_mountable_finish() to get the result of the operation.
4411 * Since: 2.22
4413 void
4414 g_file_unmount_mountable_with_operation (GFile *file,
4415 GMountUnmountFlags flags,
4416 GMountOperation *mount_operation,
4417 GCancellable *cancellable,
4418 GAsyncReadyCallback callback,
4419 gpointer user_data)
4421 GFileIface *iface;
4423 g_return_if_fail (G_IS_FILE (file));
4425 iface = G_FILE_GET_IFACE (file);
4427 if (iface->unmount_mountable == NULL && iface->unmount_mountable_with_operation == NULL)
4429 g_simple_async_report_error_in_idle (G_OBJECT (file),
4430 callback,
4431 user_data,
4432 G_IO_ERROR,
4433 G_IO_ERROR_NOT_SUPPORTED,
4434 _("Operation not supported"));
4435 return;
4438 if (iface->unmount_mountable_with_operation != NULL)
4439 (* iface->unmount_mountable_with_operation) (file,
4440 flags,
4441 mount_operation,
4442 cancellable,
4443 callback,
4444 user_data);
4445 else
4446 (* iface->unmount_mountable) (file,
4447 flags,
4448 cancellable,
4449 callback,
4450 user_data);
4454 * g_file_unmount_mountable_with_operation_finish:
4455 * @file: input #GFile.
4456 * @result: a #GAsyncResult.
4457 * @error: a #GError, or %NULL
4459 * Finishes an unmount operation, see g_file_unmount_mountable_with_operation() for details.
4461 * Finish an asynchronous unmount operation that was started
4462 * with g_file_unmount_mountable_with_operation().
4464 * Returns: %TRUE if the operation finished successfully. %FALSE
4465 * otherwise.
4467 * Since: 2.22
4469 gboolean
4470 g_file_unmount_mountable_with_operation_finish (GFile *file,
4471 GAsyncResult *result,
4472 GError **error)
4474 GFileIface *iface;
4476 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4477 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4479 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4481 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4482 if (g_simple_async_result_propagate_error (simple, error))
4483 return FALSE;
4486 iface = G_FILE_GET_IFACE (file);
4487 if (iface->unmount_mountable_with_operation_finish != NULL)
4488 return (* iface->unmount_mountable_with_operation_finish) (file, result, error);
4489 else
4490 return (* iface->unmount_mountable_finish) (file, result, error);
4494 * g_file_eject_mountable:
4495 * @file: input #GFile.
4496 * @flags: flags affecting the operation
4497 * @cancellable: optional #GCancellable object, %NULL to ignore.
4498 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4499 * @user_data: the data to pass to callback function
4501 * Starts an asynchronous eject on a mountable.
4502 * When this operation has completed, @callback will be called with
4503 * @user_user data, and the operation can be finalized with
4504 * g_file_eject_mountable_finish().
4506 * If @cancellable is not %NULL, then the operation can be cancelled by
4507 * triggering the cancellable object from another thread. If the operation
4508 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4510 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation() instead.
4512 void
4513 g_file_eject_mountable (GFile *file,
4514 GMountUnmountFlags flags,
4515 GCancellable *cancellable,
4516 GAsyncReadyCallback callback,
4517 gpointer user_data)
4519 GFileIface *iface;
4521 g_return_if_fail (G_IS_FILE (file));
4523 iface = G_FILE_GET_IFACE (file);
4525 if (iface->eject_mountable == NULL)
4527 g_simple_async_report_error_in_idle (G_OBJECT (file),
4528 callback,
4529 user_data,
4530 G_IO_ERROR,
4531 G_IO_ERROR_NOT_SUPPORTED,
4532 _("Operation not supported"));
4533 return;
4536 (* iface->eject_mountable) (file,
4537 flags,
4538 cancellable,
4539 callback,
4540 user_data);
4544 * g_file_eject_mountable_finish:
4545 * @file: input #GFile.
4546 * @result: a #GAsyncResult.
4547 * @error: a #GError, or %NULL
4549 * Finishes an asynchronous eject operation started by
4550 * g_file_eject_mountable().
4552 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4553 * otherwise.
4555 * Deprecated: 2.22: Use g_file_eject_mountable_with_operation_finish() instead.
4557 gboolean
4558 g_file_eject_mountable_finish (GFile *file,
4559 GAsyncResult *result,
4560 GError **error)
4562 GFileIface *iface;
4564 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4565 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4567 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4569 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4570 if (g_simple_async_result_propagate_error (simple, error))
4571 return FALSE;
4574 iface = G_FILE_GET_IFACE (file);
4575 return (* iface->eject_mountable_finish) (file, result, error);
4579 * g_file_eject_mountable_with_operation:
4580 * @file: input #GFile.
4581 * @flags: flags affecting the operation
4582 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
4583 * @cancellable: optional #GCancellable object, %NULL to ignore.
4584 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
4585 * @user_data: the data to pass to callback function
4587 * Starts an asynchronous eject on a mountable.
4588 * When this operation has completed, @callback will be called with
4589 * @user_user data, and the operation can be finalized with
4590 * g_file_eject_mountable_with_operation_finish().
4592 * If @cancellable is not %NULL, then the operation can be cancelled by
4593 * triggering the cancellable object from another thread. If the operation
4594 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4596 * Since: 2.22
4598 void
4599 g_file_eject_mountable_with_operation (GFile *file,
4600 GMountUnmountFlags flags,
4601 GMountOperation *mount_operation,
4602 GCancellable *cancellable,
4603 GAsyncReadyCallback callback,
4604 gpointer user_data)
4606 GFileIface *iface;
4608 g_return_if_fail (G_IS_FILE (file));
4610 iface = G_FILE_GET_IFACE (file);
4612 if (iface->eject_mountable == NULL && iface->eject_mountable_with_operation == NULL)
4614 g_simple_async_report_error_in_idle (G_OBJECT (file),
4615 callback,
4616 user_data,
4617 G_IO_ERROR,
4618 G_IO_ERROR_NOT_SUPPORTED,
4619 _("Operation not supported"));
4620 return;
4623 if (iface->eject_mountable_with_operation != NULL)
4624 (* iface->eject_mountable_with_operation) (file,
4625 flags,
4626 mount_operation,
4627 cancellable,
4628 callback,
4629 user_data);
4630 else
4631 (* iface->eject_mountable) (file,
4632 flags,
4633 cancellable,
4634 callback,
4635 user_data);
4639 * g_file_eject_mountable_with_operation_finish:
4640 * @file: input #GFile.
4641 * @result: a #GAsyncResult.
4642 * @error: a #GError, or %NULL
4644 * Finishes an asynchronous eject operation started by
4645 * g_file_eject_mountable_with_operation().
4647 * Returns: %TRUE if the @file was ejected successfully. %FALSE
4648 * otherwise.
4650 * Since: 2.22
4652 gboolean
4653 g_file_eject_mountable_with_operation_finish (GFile *file,
4654 GAsyncResult *result,
4655 GError **error)
4657 GFileIface *iface;
4659 g_return_val_if_fail (G_IS_FILE (file), FALSE);
4660 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
4662 if (G_IS_SIMPLE_ASYNC_RESULT (result))
4664 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
4665 if (g_simple_async_result_propagate_error (simple, error))
4666 return FALSE;
4669 iface = G_FILE_GET_IFACE (file);
4670 if (iface->eject_mountable_with_operation_finish != NULL)
4671 return (* iface->eject_mountable_with_operation_finish) (file, result, error);
4672 else
4673 return (* iface->eject_mountable_finish) (file, result, error);
4677 * g_file_monitor_directory:
4678 * @file: input #GFile.
4679 * @flags: a set of #GFileMonitorFlags.
4680 * @cancellable: optional #GCancellable object, %NULL to ignore.
4681 * @error: a #GError, or %NULL.
4683 * Obtains a directory monitor for the given file.
4684 * This may fail if directory monitoring is not supported.
4686 * If @cancellable is not %NULL, then the operation can be cancelled by
4687 * triggering the cancellable object from another thread. If the operation
4688 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4690 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4691 * Free the returned object with g_object_unref().
4693 GFileMonitor*
4694 g_file_monitor_directory (GFile *file,
4695 GFileMonitorFlags flags,
4696 GCancellable *cancellable,
4697 GError **error)
4699 GFileIface *iface;
4701 g_return_val_if_fail (G_IS_FILE (file), NULL);
4703 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4704 return NULL;
4706 iface = G_FILE_GET_IFACE (file);
4708 if (iface->monitor_dir == NULL)
4710 g_set_error_literal (error, G_IO_ERROR,
4711 G_IO_ERROR_NOT_SUPPORTED,
4712 _("Operation not supported"));
4713 return NULL;
4716 return (* iface->monitor_dir) (file, flags, cancellable, error);
4720 * g_file_monitor_file:
4721 * @file: input #GFile.
4722 * @flags: a set of #GFileMonitorFlags.
4723 * @cancellable: optional #GCancellable object, %NULL to ignore.
4724 * @error: a #GError, or %NULL.
4726 * Obtains a file monitor for the given file. If no file notification
4727 * mechanism exists, then regular polling of the file is used.
4729 * If @cancellable is not %NULL, then the operation can be cancelled by
4730 * triggering the cancellable object from another thread. If the operation
4731 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4733 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4734 * Free the returned object with g_object_unref().
4736 GFileMonitor*
4737 g_file_monitor_file (GFile *file,
4738 GFileMonitorFlags flags,
4739 GCancellable *cancellable,
4740 GError **error)
4742 GFileIface *iface;
4743 GFileMonitor *monitor;
4745 g_return_val_if_fail (G_IS_FILE (file), NULL);
4747 if (g_cancellable_set_error_if_cancelled (cancellable, error))
4748 return NULL;
4750 iface = G_FILE_GET_IFACE (file);
4752 monitor = NULL;
4754 if (iface->monitor_file)
4755 monitor = (* iface->monitor_file) (file, flags, cancellable, NULL);
4757 /* Fallback to polling */
4758 if (monitor == NULL)
4759 monitor = _g_poll_file_monitor_new (file);
4761 return monitor;
4765 * g_file_monitor:
4766 * @file: input #GFile
4767 * @flags: a set of #GFileMonitorFlags
4768 * @cancellable: optional #GCancellable object, %NULL to ignore
4769 * @error: a #GError, or %NULL
4771 * Obtains a file or directory monitor for the given file, depending
4772 * on the type of the file.
4774 * If @cancellable is not %NULL, then the operation can be cancelled by
4775 * triggering the cancellable object from another thread. If the operation
4776 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
4778 * Returns: a #GFileMonitor for the given @file, or %NULL on error.
4779 * Free the returned object with g_object_unref().
4781 * Since: 2.18
4783 GFileMonitor*
4784 g_file_monitor (GFile *file,
4785 GFileMonitorFlags flags,
4786 GCancellable *cancellable,
4787 GError **error)
4789 if (g_file_query_file_type (file, 0, cancellable) == G_FILE_TYPE_DIRECTORY)
4790 return g_file_monitor_directory (file, flags, cancellable, error);
4791 else
4792 return g_file_monitor_file (file, flags, cancellable, error);
4795 /********************************************
4796 * Default implementation of async ops *
4797 ********************************************/
4799 typedef struct {
4800 char *attributes;
4801 GFileQueryInfoFlags flags;
4802 GFileInfo *info;
4803 } QueryInfoAsyncData;
4805 static void
4806 query_info_data_free (QueryInfoAsyncData *data)
4808 if (data->info)
4809 g_object_unref (data->info);
4810 g_free (data->attributes);
4811 g_free (data);
4814 static void
4815 query_info_async_thread (GSimpleAsyncResult *res,
4816 GObject *object,
4817 GCancellable *cancellable)
4819 GError *error = NULL;
4820 QueryInfoAsyncData *data;
4821 GFileInfo *info;
4823 data = g_simple_async_result_get_op_res_gpointer (res);
4825 info = g_file_query_info (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4827 if (info == NULL)
4829 g_simple_async_result_set_from_error (res, error);
4830 g_error_free (error);
4832 else
4833 data->info = info;
4836 static void
4837 g_file_real_query_info_async (GFile *file,
4838 const char *attributes,
4839 GFileQueryInfoFlags flags,
4840 int io_priority,
4841 GCancellable *cancellable,
4842 GAsyncReadyCallback callback,
4843 gpointer user_data)
4845 GSimpleAsyncResult *res;
4846 QueryInfoAsyncData *data;
4848 data = g_new0 (QueryInfoAsyncData, 1);
4849 data->attributes = g_strdup (attributes);
4850 data->flags = flags;
4852 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_info_async);
4853 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
4855 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
4856 g_object_unref (res);
4859 static GFileInfo *
4860 g_file_real_query_info_finish (GFile *file,
4861 GAsyncResult *res,
4862 GError **error)
4864 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4865 QueryInfoAsyncData *data;
4867 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_info_async);
4869 data = g_simple_async_result_get_op_res_gpointer (simple);
4870 if (data->info)
4871 return g_object_ref (data->info);
4873 return NULL;
4876 typedef struct {
4877 char *attributes;
4878 GFileInfo *info;
4879 } QueryFilesystemInfoAsyncData;
4881 static void
4882 query_filesystem_info_data_free (QueryFilesystemInfoAsyncData *data)
4884 if (data->info)
4885 g_object_unref (data->info);
4886 g_free (data->attributes);
4887 g_free (data);
4890 static void
4891 query_filesystem_info_async_thread (GSimpleAsyncResult *res,
4892 GObject *object,
4893 GCancellable *cancellable)
4895 GError *error = NULL;
4896 QueryFilesystemInfoAsyncData *data;
4897 GFileInfo *info;
4899 data = g_simple_async_result_get_op_res_gpointer (res);
4901 info = g_file_query_filesystem_info (G_FILE (object), data->attributes, cancellable, &error);
4903 if (info == NULL)
4905 g_simple_async_result_set_from_error (res, error);
4906 g_error_free (error);
4908 else
4909 data->info = info;
4912 static void
4913 g_file_real_query_filesystem_info_async (GFile *file,
4914 const char *attributes,
4915 int io_priority,
4916 GCancellable *cancellable,
4917 GAsyncReadyCallback callback,
4918 gpointer user_data)
4920 GSimpleAsyncResult *res;
4921 QueryFilesystemInfoAsyncData *data;
4923 data = g_new0 (QueryFilesystemInfoAsyncData, 1);
4924 data->attributes = g_strdup (attributes);
4926 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_query_filesystem_info_async);
4927 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_filesystem_info_data_free);
4929 g_simple_async_result_run_in_thread (res, query_filesystem_info_async_thread, io_priority, cancellable);
4930 g_object_unref (res);
4933 static GFileInfo *
4934 g_file_real_query_filesystem_info_finish (GFile *file,
4935 GAsyncResult *res,
4936 GError **error)
4938 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
4939 QueryFilesystemInfoAsyncData *data;
4941 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_query_filesystem_info_async);
4943 data = g_simple_async_result_get_op_res_gpointer (simple);
4944 if (data->info)
4945 return g_object_ref (data->info);
4947 return NULL;
4950 typedef struct {
4951 char *attributes;
4952 GFileQueryInfoFlags flags;
4953 GFileEnumerator *enumerator;
4954 } EnumerateChildrenAsyncData;
4956 static void
4957 enumerate_children_data_free (EnumerateChildrenAsyncData *data)
4959 if (data->enumerator)
4960 g_object_unref (data->enumerator);
4961 g_free (data->attributes);
4962 g_free (data);
4965 static void
4966 enumerate_children_async_thread (GSimpleAsyncResult *res,
4967 GObject *object,
4968 GCancellable *cancellable)
4970 GError *error = NULL;
4971 EnumerateChildrenAsyncData *data;
4972 GFileEnumerator *enumerator;
4974 data = g_simple_async_result_get_op_res_gpointer (res);
4976 enumerator = g_file_enumerate_children (G_FILE (object), data->attributes, data->flags, cancellable, &error);
4978 if (enumerator == NULL)
4980 g_simple_async_result_set_from_error (res, error);
4981 g_error_free (error);
4983 else
4984 data->enumerator = enumerator;
4987 static void
4988 g_file_real_enumerate_children_async (GFile *file,
4989 const char *attributes,
4990 GFileQueryInfoFlags flags,
4991 int io_priority,
4992 GCancellable *cancellable,
4993 GAsyncReadyCallback callback,
4994 gpointer user_data)
4996 GSimpleAsyncResult *res;
4997 EnumerateChildrenAsyncData *data;
4999 data = g_new0 (EnumerateChildrenAsyncData, 1);
5000 data->attributes = g_strdup (attributes);
5001 data->flags = flags;
5003 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_enumerate_children_async);
5004 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)enumerate_children_data_free);
5006 g_simple_async_result_run_in_thread (res, enumerate_children_async_thread, io_priority, cancellable);
5007 g_object_unref (res);
5010 static GFileEnumerator *
5011 g_file_real_enumerate_children_finish (GFile *file,
5012 GAsyncResult *res,
5013 GError **error)
5015 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5016 EnumerateChildrenAsyncData *data;
5018 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_enumerate_children_async);
5020 data = g_simple_async_result_get_op_res_gpointer (simple);
5021 if (data->enumerator)
5022 return g_object_ref (data->enumerator);
5024 return NULL;
5027 static void
5028 open_read_async_thread (GSimpleAsyncResult *res,
5029 GObject *object,
5030 GCancellable *cancellable)
5032 GFileIface *iface;
5033 GFileInputStream *stream;
5034 GError *error = NULL;
5036 iface = G_FILE_GET_IFACE (object);
5038 if (iface->read_fn == NULL)
5040 g_set_error_literal (&error, G_IO_ERROR,
5041 G_IO_ERROR_NOT_SUPPORTED,
5042 _("Operation not supported"));
5044 g_simple_async_result_set_from_error (res, error);
5045 g_error_free (error);
5047 return;
5050 stream = iface->read_fn (G_FILE (object), cancellable, &error);
5052 if (stream == NULL)
5054 g_simple_async_result_set_from_error (res, error);
5055 g_error_free (error);
5057 else
5058 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5061 static void
5062 g_file_real_read_async (GFile *file,
5063 int io_priority,
5064 GCancellable *cancellable,
5065 GAsyncReadyCallback callback,
5066 gpointer user_data)
5068 GSimpleAsyncResult *res;
5070 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_read_async);
5072 g_simple_async_result_run_in_thread (res, open_read_async_thread, io_priority, cancellable);
5073 g_object_unref (res);
5076 static GFileInputStream *
5077 g_file_real_read_finish (GFile *file,
5078 GAsyncResult *res,
5079 GError **error)
5081 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5082 gpointer op;
5084 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_read_async);
5086 op = g_simple_async_result_get_op_res_gpointer (simple);
5087 if (op)
5088 return g_object_ref (op);
5090 return NULL;
5093 static void
5094 append_to_async_thread (GSimpleAsyncResult *res,
5095 GObject *object,
5096 GCancellable *cancellable)
5098 GFileIface *iface;
5099 GFileCreateFlags *data;
5100 GFileOutputStream *stream;
5101 GError *error = NULL;
5103 iface = G_FILE_GET_IFACE (object);
5105 data = g_simple_async_result_get_op_res_gpointer (res);
5107 stream = iface->append_to (G_FILE (object), *data, cancellable, &error);
5109 if (stream == NULL)
5111 g_simple_async_result_set_from_error (res, error);
5112 g_error_free (error);
5114 else
5115 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5118 static void
5119 g_file_real_append_to_async (GFile *file,
5120 GFileCreateFlags flags,
5121 int io_priority,
5122 GCancellable *cancellable,
5123 GAsyncReadyCallback callback,
5124 gpointer user_data)
5126 GFileCreateFlags *data;
5127 GSimpleAsyncResult *res;
5129 data = g_new0 (GFileCreateFlags, 1);
5130 *data = flags;
5132 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_append_to_async);
5133 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5135 g_simple_async_result_run_in_thread (res, append_to_async_thread, io_priority, cancellable);
5136 g_object_unref (res);
5139 static GFileOutputStream *
5140 g_file_real_append_to_finish (GFile *file,
5141 GAsyncResult *res,
5142 GError **error)
5144 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5145 gpointer op;
5147 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_append_to_async);
5149 op = g_simple_async_result_get_op_res_gpointer (simple);
5150 if (op)
5151 return g_object_ref (op);
5153 return NULL;
5156 static void
5157 create_async_thread (GSimpleAsyncResult *res,
5158 GObject *object,
5159 GCancellable *cancellable)
5161 GFileIface *iface;
5162 GFileCreateFlags *data;
5163 GFileOutputStream *stream;
5164 GError *error = NULL;
5166 iface = G_FILE_GET_IFACE (object);
5168 data = g_simple_async_result_get_op_res_gpointer (res);
5170 stream = iface->create (G_FILE (object), *data, cancellable, &error);
5172 if (stream == NULL)
5174 g_simple_async_result_set_from_error (res, error);
5175 g_error_free (error);
5177 else
5178 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5181 static void
5182 g_file_real_create_async (GFile *file,
5183 GFileCreateFlags flags,
5184 int io_priority,
5185 GCancellable *cancellable,
5186 GAsyncReadyCallback callback,
5187 gpointer user_data)
5189 GFileCreateFlags *data;
5190 GSimpleAsyncResult *res;
5192 data = g_new0 (GFileCreateFlags, 1);
5193 *data = flags;
5195 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_async);
5196 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5198 g_simple_async_result_run_in_thread (res, create_async_thread, io_priority, cancellable);
5199 g_object_unref (res);
5202 static GFileOutputStream *
5203 g_file_real_create_finish (GFile *file,
5204 GAsyncResult *res,
5205 GError **error)
5207 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5208 gpointer op;
5210 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_async);
5212 op = g_simple_async_result_get_op_res_gpointer (simple);
5213 if (op)
5214 return g_object_ref (op);
5216 return NULL;
5219 typedef struct {
5220 GFileOutputStream *stream;
5221 char *etag;
5222 gboolean make_backup;
5223 GFileCreateFlags flags;
5224 } ReplaceAsyncData;
5226 static void
5227 replace_async_data_free (ReplaceAsyncData *data)
5229 if (data->stream)
5230 g_object_unref (data->stream);
5231 g_free (data->etag);
5232 g_free (data);
5235 static void
5236 replace_async_thread (GSimpleAsyncResult *res,
5237 GObject *object,
5238 GCancellable *cancellable)
5240 GFileIface *iface;
5241 GFileOutputStream *stream;
5242 GError *error = NULL;
5243 ReplaceAsyncData *data;
5245 iface = G_FILE_GET_IFACE (object);
5247 data = g_simple_async_result_get_op_res_gpointer (res);
5249 stream = iface->replace (G_FILE (object),
5250 data->etag,
5251 data->make_backup,
5252 data->flags,
5253 cancellable,
5254 &error);
5256 if (stream == NULL)
5258 g_simple_async_result_set_from_error (res, error);
5259 g_error_free (error);
5261 else
5262 data->stream = stream;
5265 static void
5266 g_file_real_replace_async (GFile *file,
5267 const char *etag,
5268 gboolean make_backup,
5269 GFileCreateFlags flags,
5270 int io_priority,
5271 GCancellable *cancellable,
5272 GAsyncReadyCallback callback,
5273 gpointer user_data)
5275 GSimpleAsyncResult *res;
5276 ReplaceAsyncData *data;
5278 data = g_new0 (ReplaceAsyncData, 1);
5279 data->etag = g_strdup (etag);
5280 data->make_backup = make_backup;
5281 data->flags = flags;
5283 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_async);
5284 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_async_data_free);
5286 g_simple_async_result_run_in_thread (res, replace_async_thread, io_priority, cancellable);
5287 g_object_unref (res);
5290 static GFileOutputStream *
5291 g_file_real_replace_finish (GFile *file,
5292 GAsyncResult *res,
5293 GError **error)
5295 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5296 ReplaceAsyncData *data;
5298 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_async);
5300 data = g_simple_async_result_get_op_res_gpointer (simple);
5301 if (data->stream)
5302 return g_object_ref (data->stream);
5304 return NULL;
5307 static void
5308 open_readwrite_async_thread (GSimpleAsyncResult *res,
5309 GObject *object,
5310 GCancellable *cancellable)
5312 GFileIface *iface;
5313 GFileIOStream *stream;
5314 GError *error = NULL;
5316 iface = G_FILE_GET_IFACE (object);
5318 if (iface->open_readwrite == NULL)
5320 g_set_error_literal (&error, G_IO_ERROR,
5321 G_IO_ERROR_NOT_SUPPORTED,
5322 _("Operation not supported"));
5324 g_simple_async_result_set_from_error (res, error);
5325 g_error_free (error);
5327 return;
5330 stream = iface->open_readwrite (G_FILE (object), cancellable, &error);
5332 if (stream == NULL)
5334 g_simple_async_result_set_from_error (res, error);
5335 g_error_free (error);
5337 else
5338 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5341 static void
5342 g_file_real_open_readwrite_async (GFile *file,
5343 int io_priority,
5344 GCancellable *cancellable,
5345 GAsyncReadyCallback callback,
5346 gpointer user_data)
5348 GSimpleAsyncResult *res;
5350 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_open_readwrite_async);
5352 g_simple_async_result_run_in_thread (res, open_readwrite_async_thread, io_priority, cancellable);
5353 g_object_unref (res);
5356 static GFileIOStream *
5357 g_file_real_open_readwrite_finish (GFile *file,
5358 GAsyncResult *res,
5359 GError **error)
5361 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5362 gpointer op;
5364 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_open_readwrite_async);
5366 op = g_simple_async_result_get_op_res_gpointer (simple);
5367 if (op)
5368 return g_object_ref (op);
5370 return NULL;
5373 static void
5374 create_readwrite_async_thread (GSimpleAsyncResult *res,
5375 GObject *object,
5376 GCancellable *cancellable)
5378 GFileIface *iface;
5379 GFileCreateFlags *data;
5380 GFileIOStream *stream;
5381 GError *error = NULL;
5383 iface = G_FILE_GET_IFACE (object);
5385 data = g_simple_async_result_get_op_res_gpointer (res);
5387 if (iface->create_readwrite == NULL)
5389 g_set_error_literal (&error, G_IO_ERROR,
5390 G_IO_ERROR_NOT_SUPPORTED,
5391 _("Operation not supported"));
5393 g_simple_async_result_set_from_error (res, error);
5394 g_error_free (error);
5396 return;
5399 stream = iface->create_readwrite (G_FILE (object), *data, cancellable, &error);
5401 if (stream == NULL)
5403 g_simple_async_result_set_from_error (res, error);
5404 g_error_free (error);
5406 else
5407 g_simple_async_result_set_op_res_gpointer (res, stream, g_object_unref);
5410 static void
5411 g_file_real_create_readwrite_async (GFile *file,
5412 GFileCreateFlags flags,
5413 int io_priority,
5414 GCancellable *cancellable,
5415 GAsyncReadyCallback callback,
5416 gpointer user_data)
5418 GFileCreateFlags *data;
5419 GSimpleAsyncResult *res;
5421 data = g_new0 (GFileCreateFlags, 1);
5422 *data = flags;
5424 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_create_readwrite_async);
5425 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)g_free);
5427 g_simple_async_result_run_in_thread (res, create_readwrite_async_thread, io_priority, cancellable);
5428 g_object_unref (res);
5431 static GFileIOStream *
5432 g_file_real_create_readwrite_finish (GFile *file,
5433 GAsyncResult *res,
5434 GError **error)
5436 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5437 gpointer op;
5439 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_create_readwrite_async);
5441 op = g_simple_async_result_get_op_res_gpointer (simple);
5442 if (op)
5443 return g_object_ref (op);
5445 return NULL;
5448 typedef struct {
5449 GFileIOStream *stream;
5450 char *etag;
5451 gboolean make_backup;
5452 GFileCreateFlags flags;
5453 } ReplaceRWAsyncData;
5455 static void
5456 replace_rw_async_data_free (ReplaceRWAsyncData *data)
5458 if (data->stream)
5459 g_object_unref (data->stream);
5460 g_free (data->etag);
5461 g_free (data);
5464 static void
5465 replace_readwrite_async_thread (GSimpleAsyncResult *res,
5466 GObject *object,
5467 GCancellable *cancellable)
5469 GFileIface *iface;
5470 GFileIOStream *stream;
5471 GError *error = NULL;
5472 ReplaceRWAsyncData *data;
5474 iface = G_FILE_GET_IFACE (object);
5476 data = g_simple_async_result_get_op_res_gpointer (res);
5478 stream = iface->replace_readwrite (G_FILE (object),
5479 data->etag,
5480 data->make_backup,
5481 data->flags,
5482 cancellable,
5483 &error);
5485 if (stream == NULL)
5487 g_simple_async_result_set_from_error (res, error);
5488 g_error_free (error);
5490 else
5491 data->stream = stream;
5494 static void
5495 g_file_real_replace_readwrite_async (GFile *file,
5496 const char *etag,
5497 gboolean make_backup,
5498 GFileCreateFlags flags,
5499 int io_priority,
5500 GCancellable *cancellable,
5501 GAsyncReadyCallback callback,
5502 gpointer user_data)
5504 GSimpleAsyncResult *res;
5505 ReplaceRWAsyncData *data;
5507 data = g_new0 (ReplaceRWAsyncData, 1);
5508 data->etag = g_strdup (etag);
5509 data->make_backup = make_backup;
5510 data->flags = flags;
5512 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_replace_readwrite_async);
5513 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_rw_async_data_free);
5515 g_simple_async_result_run_in_thread (res, replace_readwrite_async_thread, io_priority, cancellable);
5516 g_object_unref (res);
5519 static GFileIOStream *
5520 g_file_real_replace_readwrite_finish (GFile *file,
5521 GAsyncResult *res,
5522 GError **error)
5524 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5525 ReplaceRWAsyncData *data;
5527 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_replace_readwrite_async);
5529 data = g_simple_async_result_get_op_res_gpointer (simple);
5530 if (data->stream)
5531 return g_object_ref (data->stream);
5533 return NULL;
5536 typedef struct {
5537 char *name;
5538 GFile *file;
5539 } SetDisplayNameAsyncData;
5541 static void
5542 set_display_name_data_free (SetDisplayNameAsyncData *data)
5544 g_free (data->name);
5545 if (data->file)
5546 g_object_unref (data->file);
5547 g_free (data);
5550 static void
5551 set_display_name_async_thread (GSimpleAsyncResult *res,
5552 GObject *object,
5553 GCancellable *cancellable)
5555 GError *error = NULL;
5556 SetDisplayNameAsyncData *data;
5557 GFile *file;
5559 data = g_simple_async_result_get_op_res_gpointer (res);
5561 file = g_file_set_display_name (G_FILE (object), data->name, cancellable, &error);
5563 if (file == NULL)
5565 g_simple_async_result_set_from_error (res, error);
5566 g_error_free (error);
5568 else
5569 data->file = file;
5572 static void
5573 g_file_real_set_display_name_async (GFile *file,
5574 const char *display_name,
5575 int io_priority,
5576 GCancellable *cancellable,
5577 GAsyncReadyCallback callback,
5578 gpointer user_data)
5580 GSimpleAsyncResult *res;
5581 SetDisplayNameAsyncData *data;
5583 data = g_new0 (SetDisplayNameAsyncData, 1);
5584 data->name = g_strdup (display_name);
5586 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_display_name_async);
5587 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_display_name_data_free);
5589 g_simple_async_result_run_in_thread (res, set_display_name_async_thread, io_priority, cancellable);
5590 g_object_unref (res);
5593 static GFile *
5594 g_file_real_set_display_name_finish (GFile *file,
5595 GAsyncResult *res,
5596 GError **error)
5598 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5599 SetDisplayNameAsyncData *data;
5601 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_display_name_async);
5603 data = g_simple_async_result_get_op_res_gpointer (simple);
5604 if (data->file)
5605 return g_object_ref (data->file);
5607 return NULL;
5610 typedef struct {
5611 GFileQueryInfoFlags flags;
5612 GFileInfo *info;
5613 gboolean res;
5614 GError *error;
5615 } SetInfoAsyncData;
5617 static void
5618 set_info_data_free (SetInfoAsyncData *data)
5620 if (data->info)
5621 g_object_unref (data->info);
5622 if (data->error)
5623 g_error_free (data->error);
5624 g_free (data);
5627 static void
5628 set_info_async_thread (GSimpleAsyncResult *res,
5629 GObject *object,
5630 GCancellable *cancellable)
5632 SetInfoAsyncData *data;
5634 data = g_simple_async_result_get_op_res_gpointer (res);
5636 data->error = NULL;
5637 data->res = g_file_set_attributes_from_info (G_FILE (object),
5638 data->info,
5639 data->flags,
5640 cancellable,
5641 &data->error);
5644 static void
5645 g_file_real_set_attributes_async (GFile *file,
5646 GFileInfo *info,
5647 GFileQueryInfoFlags flags,
5648 int io_priority,
5649 GCancellable *cancellable,
5650 GAsyncReadyCallback callback,
5651 gpointer user_data)
5653 GSimpleAsyncResult *res;
5654 SetInfoAsyncData *data;
5656 data = g_new0 (SetInfoAsyncData, 1);
5657 data->info = g_file_info_dup (info);
5658 data->flags = flags;
5660 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_set_attributes_async);
5661 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)set_info_data_free);
5663 g_simple_async_result_run_in_thread (res, set_info_async_thread, io_priority, cancellable);
5664 g_object_unref (res);
5667 static gboolean
5668 g_file_real_set_attributes_finish (GFile *file,
5669 GAsyncResult *res,
5670 GFileInfo **info,
5671 GError **error)
5673 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5674 SetInfoAsyncData *data;
5676 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_set_attributes_async);
5678 data = g_simple_async_result_get_op_res_gpointer (simple);
5680 if (info)
5681 *info = g_object_ref (data->info);
5683 if (error != NULL && data->error)
5684 *error = g_error_copy (data->error);
5686 return data->res;
5689 static void
5690 find_enclosing_mount_async_thread (GSimpleAsyncResult *res,
5691 GObject *object,
5692 GCancellable *cancellable)
5694 GError *error = NULL;
5695 GMount *mount;
5697 mount = g_file_find_enclosing_mount (G_FILE (object), cancellable, &error);
5699 if (mount == NULL)
5701 g_simple_async_result_set_from_error (res, error);
5702 g_error_free (error);
5704 else
5705 g_simple_async_result_set_op_res_gpointer (res, mount, (GDestroyNotify)g_object_unref);
5708 static void
5709 g_file_real_find_enclosing_mount_async (GFile *file,
5710 int io_priority,
5711 GCancellable *cancellable,
5712 GAsyncReadyCallback callback,
5713 gpointer user_data)
5715 GSimpleAsyncResult *res;
5717 res = g_simple_async_result_new (G_OBJECT (file), callback, user_data, g_file_real_find_enclosing_mount_async);
5719 g_simple_async_result_run_in_thread (res, find_enclosing_mount_async_thread, io_priority, cancellable);
5720 g_object_unref (res);
5723 static GMount *
5724 g_file_real_find_enclosing_mount_finish (GFile *file,
5725 GAsyncResult *res,
5726 GError **error)
5728 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
5729 GMount *mount;
5731 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_real_find_enclosing_mount_async);
5733 mount = g_simple_async_result_get_op_res_gpointer (simple);
5734 return g_object_ref (mount);
5738 typedef struct {
5739 GFile *source;
5740 GFile *destination;
5741 GFileCopyFlags flags;
5742 GFileProgressCallback progress_cb;
5743 gpointer progress_cb_data;
5744 GIOSchedulerJob *job;
5745 } CopyAsyncData;
5747 static void
5748 copy_async_data_free (CopyAsyncData *data)
5750 g_object_unref (data->source);
5751 g_object_unref (data->destination);
5752 g_free (data);
5755 typedef struct {
5756 CopyAsyncData *data;
5757 goffset current_num_bytes;
5758 goffset total_num_bytes;
5759 } ProgressData;
5761 static gboolean
5762 copy_async_progress_in_main (gpointer user_data)
5764 ProgressData *progress = user_data;
5765 CopyAsyncData *data = progress->data;
5767 data->progress_cb (progress->current_num_bytes,
5768 progress->total_num_bytes,
5769 data->progress_cb_data);
5771 return FALSE;
5774 static gboolean
5775 mainloop_barrier (gpointer user_data)
5777 /* Does nothing, but ensures all queued idles before
5778 this are run */
5779 return FALSE;
5783 static void
5784 copy_async_progress_callback (goffset current_num_bytes,
5785 goffset total_num_bytes,
5786 gpointer user_data)
5788 CopyAsyncData *data = user_data;
5789 ProgressData *progress;
5791 progress = g_new (ProgressData, 1);
5792 progress->data = data;
5793 progress->current_num_bytes = current_num_bytes;
5794 progress->total_num_bytes = total_num_bytes;
5796 g_io_scheduler_job_send_to_mainloop_async (data->job,
5797 copy_async_progress_in_main,
5798 progress,
5799 g_free);
5802 static gboolean
5803 copy_async_thread (GIOSchedulerJob *job,
5804 GCancellable *cancellable,
5805 gpointer user_data)
5807 GSimpleAsyncResult *res;
5808 CopyAsyncData *data;
5809 gboolean result;
5810 GError *error;
5812 res = user_data;
5813 data = g_simple_async_result_get_op_res_gpointer (res);
5815 error = NULL;
5816 data->job = job;
5817 result = g_file_copy (data->source,
5818 data->destination,
5819 data->flags,
5820 cancellable,
5821 (data->progress_cb != NULL) ? copy_async_progress_callback : NULL,
5822 data,
5823 &error);
5825 /* Ensure all progress callbacks are done running in main thread */
5826 if (data->progress_cb != NULL)
5827 g_io_scheduler_job_send_to_mainloop (job,
5828 mainloop_barrier,
5829 NULL, NULL);
5831 if (!result)
5833 g_simple_async_result_set_from_error (res, error);
5834 g_error_free (error);
5837 g_simple_async_result_complete_in_idle (res);
5839 return FALSE;
5842 static void
5843 g_file_real_copy_async (GFile *source,
5844 GFile *destination,
5845 GFileCopyFlags flags,
5846 int io_priority,
5847 GCancellable *cancellable,
5848 GFileProgressCallback progress_callback,
5849 gpointer progress_callback_data,
5850 GAsyncReadyCallback callback,
5851 gpointer user_data)
5853 GSimpleAsyncResult *res;
5854 CopyAsyncData *data;
5856 data = g_new0 (CopyAsyncData, 1);
5857 data->source = g_object_ref (source);
5858 data->destination = g_object_ref (destination);
5859 data->flags = flags;
5860 data->progress_cb = progress_callback;
5861 data->progress_cb_data = progress_callback_data;
5863 res = g_simple_async_result_new (G_OBJECT (source), callback, user_data, g_file_real_copy_async);
5864 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)copy_async_data_free);
5866 g_io_scheduler_push_job (copy_async_thread, res, g_object_unref, io_priority, cancellable);
5869 static gboolean
5870 g_file_real_copy_finish (GFile *file,
5871 GAsyncResult *res,
5872 GError **error)
5874 /* Error handled in g_file_copy_finish() */
5875 return TRUE;
5879 /********************************************
5880 * Default VFS operations *
5881 ********************************************/
5884 * g_file_new_for_path:
5885 * @path: a string containing a relative or absolute path.
5887 * Constructs a #GFile for a given path. This operation never
5888 * fails, but the returned object might not support any I/O
5889 * operation if @path is malformed.
5891 * Returns: a new #GFile for the given @path.
5893 GFile *
5894 g_file_new_for_path (const char *path)
5896 g_return_val_if_fail (path != NULL, NULL);
5898 return g_vfs_get_file_for_path (g_vfs_get_default (), path);
5902 * g_file_new_for_uri:
5903 * @uri: a string containing a URI.
5905 * Constructs a #GFile for a given URI. This operation never
5906 * fails, but the returned object might not support any I/O
5907 * operation if @uri is malformed or if the uri type is
5908 * not supported.
5910 * Returns: a #GFile for the given @uri.
5911 **/
5912 GFile *
5913 g_file_new_for_uri (const char *uri)
5915 g_return_val_if_fail (uri != NULL, NULL);
5917 return g_vfs_get_file_for_uri (g_vfs_get_default (), uri);
5921 * g_file_parse_name:
5922 * @parse_name: a file name or path to be parsed.
5924 * Constructs a #GFile with the given @parse_name (i.e. something given by g_file_get_parse_name()).
5925 * This operation never fails, but the returned object might not support any I/O
5926 * operation if the @parse_name cannot be parsed.
5928 * Returns: a new #GFile.
5930 GFile *
5931 g_file_parse_name (const char *parse_name)
5933 g_return_val_if_fail (parse_name != NULL, NULL);
5935 return g_vfs_parse_name (g_vfs_get_default (), parse_name);
5938 static gboolean
5939 is_valid_scheme_character (char c)
5941 return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.';
5944 /* Following RFC 2396, valid schemes are built like:
5945 * scheme = alpha *( alpha | digit | "+" | "-" | "." )
5947 static gboolean
5948 has_valid_scheme (const char *uri)
5950 const char *p;
5952 p = uri;
5954 if (!g_ascii_isalpha (*p))
5955 return FALSE;
5957 do {
5958 p++;
5959 } while (is_valid_scheme_character (*p));
5961 return *p == ':';
5965 * g_file_new_for_commandline_arg:
5966 * @arg: a command line string.
5968 * Creates a #GFile with the given argument from the command line. The value of
5969 * @arg can be either a URI, an absolute path or a relative path resolved
5970 * relative to the current working directory.
5971 * This operation never fails, but the returned object might not support any
5972 * I/O operation if @arg points to a malformed path.
5974 * Returns: a new #GFile.
5976 GFile *
5977 g_file_new_for_commandline_arg (const char *arg)
5979 GFile *file;
5980 char *filename;
5981 char *current_dir;
5983 g_return_val_if_fail (arg != NULL, NULL);
5985 if (g_path_is_absolute (arg))
5986 return g_file_new_for_path (arg);
5988 if (has_valid_scheme (arg))
5989 return g_file_new_for_uri (arg);
5991 current_dir = g_get_current_dir ();
5992 filename = g_build_filename (current_dir, arg, NULL);
5993 g_free (current_dir);
5995 file = g_file_new_for_path (filename);
5996 g_free (filename);
5998 return file;
6002 * g_file_mount_enclosing_volume:
6003 * @location: input #GFile.
6004 * @flags: flags affecting the operation
6005 * @mount_operation: a #GMountOperation or %NULL to avoid user interaction.
6006 * @cancellable: optional #GCancellable object, %NULL to ignore.
6007 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6008 * @user_data: the data to pass to callback function
6010 * Starts a @mount_operation, mounting the volume that contains the file @location.
6012 * When this operation has completed, @callback will be called with
6013 * @user_user data, and the operation can be finalized with
6014 * g_file_mount_enclosing_volume_finish().
6016 * If @cancellable is not %NULL, then the operation can be cancelled by
6017 * triggering the cancellable object from another thread. If the operation
6018 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6020 void
6021 g_file_mount_enclosing_volume (GFile *location,
6022 GMountMountFlags flags,
6023 GMountOperation *mount_operation,
6024 GCancellable *cancellable,
6025 GAsyncReadyCallback callback,
6026 gpointer user_data)
6028 GFileIface *iface;
6030 g_return_if_fail (G_IS_FILE (location));
6032 iface = G_FILE_GET_IFACE (location);
6034 if (iface->mount_enclosing_volume == NULL)
6036 g_simple_async_report_error_in_idle (G_OBJECT (location),
6037 callback, user_data,
6038 G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
6039 _("volume doesn't implement mount"));
6041 return;
6044 (* iface->mount_enclosing_volume) (location, flags, mount_operation, cancellable, callback, user_data);
6049 * g_file_mount_enclosing_volume_finish:
6050 * @location: input #GFile.
6051 * @result: a #GAsyncResult.
6052 * @error: a #GError, or %NULL
6054 * Finishes a mount operation started by g_file_mount_enclosing_volume().
6056 * Returns: %TRUE if successful. If an error
6057 * has occurred, this function will return %FALSE and set @error
6058 * appropriately if present.
6060 gboolean
6061 g_file_mount_enclosing_volume_finish (GFile *location,
6062 GAsyncResult *result,
6063 GError **error)
6065 GFileIface *iface;
6067 g_return_val_if_fail (G_IS_FILE (location), FALSE);
6068 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
6070 if (G_IS_SIMPLE_ASYNC_RESULT (result))
6072 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
6073 if (g_simple_async_result_propagate_error (simple, error))
6074 return FALSE;
6077 iface = G_FILE_GET_IFACE (location);
6079 return (* iface->mount_enclosing_volume_finish) (location, result, error);
6082 /********************************************
6083 * Utility functions *
6084 ********************************************/
6087 * g_file_query_default_handler:
6088 * @file: a #GFile to open.
6089 * @cancellable: optional #GCancellable object, %NULL to ignore.
6090 * @error: a #GError, or %NULL
6092 * Returns the #GAppInfo that is registered as the default
6093 * application to handle the file specified by @file.
6095 * If @cancellable is not %NULL, then the operation can be cancelled by
6096 * triggering the cancellable object from another thread. If the operation
6097 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6099 * Returns: a #GAppInfo if the handle was found, %NULL if there were errors.
6100 * When you are done with it, release it with g_object_unref()
6102 GAppInfo *
6103 g_file_query_default_handler (GFile *file,
6104 GCancellable *cancellable,
6105 GError **error)
6107 char *uri_scheme;
6108 const char *content_type;
6109 GAppInfo *appinfo;
6110 GFileInfo *info;
6111 char *path;
6113 uri_scheme = g_file_get_uri_scheme (file);
6114 if (uri_scheme && uri_scheme[0] != '\0')
6116 appinfo = g_app_info_get_default_for_uri_scheme (uri_scheme);
6117 g_free (uri_scheme);
6119 if (appinfo != NULL)
6120 return appinfo;
6123 info = g_file_query_info (file,
6124 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
6126 cancellable,
6127 error);
6128 if (info == NULL)
6129 return NULL;
6131 appinfo = NULL;
6133 content_type = g_file_info_get_content_type (info);
6134 if (content_type)
6136 /* Don't use is_native(), as we want to support fuse paths if availible */
6137 path = g_file_get_path (file);
6138 appinfo = g_app_info_get_default_for_type (content_type,
6139 path == NULL);
6140 g_free (path);
6143 g_object_unref (info);
6145 if (appinfo != NULL)
6146 return appinfo;
6148 g_set_error_literal (error, G_IO_ERROR,
6149 G_IO_ERROR_NOT_SUPPORTED,
6150 _("No application is registered as handling this file"));
6151 return NULL;
6156 #define GET_CONTENT_BLOCK_SIZE 8192
6159 * g_file_load_contents:
6160 * @file: input #GFile.
6161 * @cancellable: optional #GCancellable object, %NULL to ignore.
6162 * @contents: (out) (transfer full): a location to place the contents of the file.
6163 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6164 * or %NULL if the length is not needed
6165 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6166 * or %NULL if the entity tag is not needed
6167 * @error: a #GError, or %NULL
6169 * Loads the content of the file into memory. The data is always
6170 * zero-terminated, but this is not included in the resultant @length.
6171 * The returned @content should be freed with g_free() when no longer
6172 * needed.
6174 * If @cancellable is not %NULL, then the operation can be cancelled by
6175 * triggering the cancellable object from another thread. If the operation
6176 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6178 * Returns: %TRUE if the @file's contents were successfully loaded.
6179 * %FALSE if there were errors.
6181 gboolean
6182 g_file_load_contents (GFile *file,
6183 GCancellable *cancellable,
6184 char **contents,
6185 gsize *length,
6186 char **etag_out,
6187 GError **error)
6189 GFileInputStream *in;
6190 GByteArray *content;
6191 gsize pos;
6192 gssize res;
6193 GFileInfo *info;
6195 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6196 g_return_val_if_fail (contents != NULL, FALSE);
6198 in = g_file_read (file, cancellable, error);
6199 if (in == NULL)
6200 return FALSE;
6202 content = g_byte_array_new ();
6203 pos = 0;
6205 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6206 while ((res = g_input_stream_read (G_INPUT_STREAM (in),
6207 content->data + pos,
6208 GET_CONTENT_BLOCK_SIZE,
6209 cancellable, error)) > 0)
6211 pos += res;
6212 g_byte_array_set_size (content, pos + GET_CONTENT_BLOCK_SIZE + 1);
6215 if (etag_out)
6217 *etag_out = NULL;
6219 info = g_file_input_stream_query_info (in,
6220 G_FILE_ATTRIBUTE_ETAG_VALUE,
6221 cancellable,
6222 NULL);
6223 if (info)
6225 *etag_out = g_strdup (g_file_info_get_etag (info));
6226 g_object_unref (info);
6230 /* Ignore errors on close */
6231 g_input_stream_close (G_INPUT_STREAM (in), cancellable, NULL);
6232 g_object_unref (in);
6234 if (res < 0)
6236 /* error is set already */
6237 g_byte_array_free (content, TRUE);
6238 return FALSE;
6241 if (length)
6242 *length = pos;
6244 /* Zero terminate (we got an extra byte allocated for this */
6245 content->data[pos] = 0;
6247 *contents = (char *)g_byte_array_free (content, FALSE);
6249 return TRUE;
6252 typedef struct {
6253 GFile *file;
6254 GError *error;
6255 GCancellable *cancellable;
6256 GFileReadMoreCallback read_more_callback;
6257 GAsyncReadyCallback callback;
6258 gpointer user_data;
6259 GByteArray *content;
6260 gsize pos;
6261 char *etag;
6262 } LoadContentsData;
6265 static void
6266 load_contents_data_free (LoadContentsData *data)
6268 if (data->error)
6269 g_error_free (data->error);
6270 if (data->cancellable)
6271 g_object_unref (data->cancellable);
6272 if (data->content)
6273 g_byte_array_free (data->content, TRUE);
6274 g_free (data->etag);
6275 g_object_unref (data->file);
6276 g_free (data);
6279 static void
6280 load_contents_close_callback (GObject *obj,
6281 GAsyncResult *close_res,
6282 gpointer user_data)
6284 GInputStream *stream = G_INPUT_STREAM (obj);
6285 LoadContentsData *data = user_data;
6286 GSimpleAsyncResult *res;
6288 /* Ignore errors here, we're only reading anyway */
6289 g_input_stream_close_finish (stream, close_res, NULL);
6290 g_object_unref (stream);
6292 res = g_simple_async_result_new (G_OBJECT (data->file),
6293 data->callback,
6294 data->user_data,
6295 g_file_load_contents_async);
6296 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)load_contents_data_free);
6297 g_simple_async_result_complete (res);
6298 g_object_unref (res);
6301 static void
6302 load_contents_fstat_callback (GObject *obj,
6303 GAsyncResult *stat_res,
6304 gpointer user_data)
6306 GInputStream *stream = G_INPUT_STREAM (obj);
6307 LoadContentsData *data = user_data;
6308 GFileInfo *info;
6310 info = g_file_input_stream_query_info_finish (G_FILE_INPUT_STREAM (stream),
6311 stat_res, NULL);
6312 if (info)
6314 data->etag = g_strdup (g_file_info_get_etag (info));
6315 g_object_unref (info);
6318 g_input_stream_close_async (stream, 0,
6319 data->cancellable,
6320 load_contents_close_callback, data);
6323 static void
6324 load_contents_read_callback (GObject *obj,
6325 GAsyncResult *read_res,
6326 gpointer user_data)
6328 GInputStream *stream = G_INPUT_STREAM (obj);
6329 LoadContentsData *data = user_data;
6330 GError *error = NULL;
6331 gssize read_size;
6333 read_size = g_input_stream_read_finish (stream, read_res, &error);
6335 if (read_size < 0)
6337 /* Error or EOF, close the file */
6338 data->error = error;
6339 g_input_stream_close_async (stream, 0,
6340 data->cancellable,
6341 load_contents_close_callback, data);
6343 else if (read_size == 0)
6345 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6346 G_FILE_ATTRIBUTE_ETAG_VALUE,
6348 data->cancellable,
6349 load_contents_fstat_callback,
6350 data);
6352 else if (read_size > 0)
6354 data->pos += read_size;
6356 g_byte_array_set_size (data->content,
6357 data->pos + GET_CONTENT_BLOCK_SIZE);
6360 if (data->read_more_callback &&
6361 !data->read_more_callback ((char *)data->content->data, data->pos, data->user_data))
6362 g_file_input_stream_query_info_async (G_FILE_INPUT_STREAM (stream),
6363 G_FILE_ATTRIBUTE_ETAG_VALUE,
6365 data->cancellable,
6366 load_contents_fstat_callback,
6367 data);
6368 else
6369 g_input_stream_read_async (stream,
6370 data->content->data + data->pos,
6371 GET_CONTENT_BLOCK_SIZE,
6373 data->cancellable,
6374 load_contents_read_callback,
6375 data);
6379 static void
6380 load_contents_open_callback (GObject *obj,
6381 GAsyncResult *open_res,
6382 gpointer user_data)
6384 GFile *file = G_FILE (obj);
6385 GFileInputStream *stream;
6386 LoadContentsData *data = user_data;
6387 GError *error = NULL;
6388 GSimpleAsyncResult *res;
6390 stream = g_file_read_finish (file, open_res, &error);
6392 if (stream)
6394 g_byte_array_set_size (data->content,
6395 data->pos + GET_CONTENT_BLOCK_SIZE);
6396 g_input_stream_read_async (G_INPUT_STREAM (stream),
6397 data->content->data + data->pos,
6398 GET_CONTENT_BLOCK_SIZE,
6400 data->cancellable,
6401 load_contents_read_callback,
6402 data);
6405 else
6407 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6408 data->callback,
6409 data->user_data,
6410 error);
6411 g_simple_async_result_complete (res);
6412 g_error_free (error);
6413 load_contents_data_free (data);
6414 g_object_unref (res);
6419 * g_file_load_partial_contents_async:
6420 * @file: input #GFile.
6421 * @cancellable: optional #GCancellable object, %NULL to ignore.
6422 * @read_more_callback: a #GFileReadMoreCallback to receive partial data and to specify whether further data should be read.
6423 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6424 * @user_data: the data to pass to the callback functions.
6426 * Reads the partial contents of a file. A #GFileReadMoreCallback should be
6427 * used to stop reading from the file when appropriate, else this function
6428 * will behave exactly as g_file_load_contents_async(). This operation
6429 * can be finished by g_file_load_partial_contents_finish().
6431 * Users of this function should be aware that @user_data is passed to
6432 * both the @read_more_callback and the @callback.
6434 * If @cancellable is not %NULL, then the operation can be cancelled by
6435 * triggering the cancellable object from another thread. If the operation
6436 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6438 void
6439 g_file_load_partial_contents_async (GFile *file,
6440 GCancellable *cancellable,
6441 GFileReadMoreCallback read_more_callback,
6442 GAsyncReadyCallback callback,
6443 gpointer user_data)
6445 LoadContentsData *data;
6447 g_return_if_fail (G_IS_FILE (file));
6449 data = g_new0 (LoadContentsData, 1);
6451 if (cancellable)
6452 data->cancellable = g_object_ref (cancellable);
6453 data->read_more_callback = read_more_callback;
6454 data->callback = callback;
6455 data->user_data = user_data;
6456 data->content = g_byte_array_new ();
6457 data->file = g_object_ref (file);
6459 g_file_read_async (file,
6461 cancellable,
6462 load_contents_open_callback,
6463 data);
6467 * g_file_load_partial_contents_finish:
6468 * @file: input #GFile.
6469 * @res: a #GAsyncResult.
6470 * @contents: (out) (transfer full): a location to place the contents of the file.
6471 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6472 * or %NULL if the length is not needed
6473 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6474 * or %NULL if the entity tag is not needed
6475 * @error: a #GError, or %NULL
6477 * Finishes an asynchronous partial load operation that was started
6478 * with g_file_load_partial_contents_async(). The data is always
6479 * zero-terminated, but this is not included in the resultant @length.
6480 * The returned @content should be freed with g_free() when no longer
6481 * needed.
6483 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6484 * present, it will be set appropriately.
6486 gboolean
6487 g_file_load_partial_contents_finish (GFile *file,
6488 GAsyncResult *res,
6489 char **contents,
6490 gsize *length,
6491 char **etag_out,
6492 GError **error)
6494 GSimpleAsyncResult *simple;
6495 LoadContentsData *data;
6497 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6498 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6499 g_return_val_if_fail (contents != NULL, FALSE);
6501 simple = G_SIMPLE_ASYNC_RESULT (res);
6503 if (g_simple_async_result_propagate_error (simple, error))
6504 return FALSE;
6506 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_load_contents_async);
6508 data = g_simple_async_result_get_op_res_gpointer (simple);
6510 if (data->error)
6512 g_propagate_error (error, data->error);
6513 data->error = NULL;
6514 *contents = NULL;
6515 if (length)
6516 *length = 0;
6517 return FALSE;
6520 if (length)
6521 *length = data->pos;
6523 if (etag_out)
6525 *etag_out = data->etag;
6526 data->etag = NULL;
6529 /* Zero terminate */
6530 g_byte_array_set_size (data->content, data->pos + 1);
6531 data->content->data[data->pos] = 0;
6533 *contents = (char *)g_byte_array_free (data->content, FALSE);
6534 data->content = NULL;
6536 return TRUE;
6540 * g_file_load_contents_async:
6541 * @file: input #GFile.
6542 * @cancellable: optional #GCancellable object, %NULL to ignore.
6543 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6544 * @user_data: the data to pass to callback function
6546 * Starts an asynchronous load of the @file's contents.
6548 * For more details, see g_file_load_contents() which is
6549 * the synchronous version of this call.
6551 * When the load operation has completed, @callback will be called
6552 * with @user data. To finish the operation, call
6553 * g_file_load_contents_finish() with the #GAsyncResult returned by
6554 * the @callback.
6556 * If @cancellable is not %NULL, then the operation can be cancelled by
6557 * triggering the cancellable object from another thread. If the operation
6558 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6560 void
6561 g_file_load_contents_async (GFile *file,
6562 GCancellable *cancellable,
6563 GAsyncReadyCallback callback,
6564 gpointer user_data)
6566 g_file_load_partial_contents_async (file,
6567 cancellable,
6568 NULL,
6569 callback, user_data);
6573 * g_file_load_contents_finish:
6574 * @file: input #GFile.
6575 * @res: a #GAsyncResult.
6576 * @contents: (out) (transfer full): a location to place the contents of the file.
6577 * @length: (out) (allow-none): a location to place the length of the contents of the file,
6578 * or %NULL if the length is not needed
6579 * @etag_out: (out) (allow-none): a location to place the current entity tag for the file,
6580 * or %NULL if the entity tag is not needed
6581 * @error: a #GError, or %NULL
6583 * Finishes an asynchronous load of the @file's contents.
6584 * The contents are placed in @contents, and @length is set to the
6585 * size of the @contents string. The @content should be freed with
6586 * g_free() when no longer needed. If @etag_out is present, it will be
6587 * set to the new entity tag for the @file.
6589 * Returns: %TRUE if the load was successful. If %FALSE and @error is
6590 * present, it will be set appropriately.
6592 gboolean
6593 g_file_load_contents_finish (GFile *file,
6594 GAsyncResult *res,
6595 char **contents,
6596 gsize *length,
6597 char **etag_out,
6598 GError **error)
6600 return g_file_load_partial_contents_finish (file,
6601 res,
6602 contents,
6603 length,
6604 etag_out,
6605 error);
6609 * g_file_replace_contents:
6610 * @file: input #GFile.
6611 * @contents: a string containing the new contents for @file.
6612 * @length: the length of @contents in bytes.
6613 * @etag: (allow-none): the old <link linkend="gfile-etag">entity tag</link>
6614 * for the document, or %NULL
6615 * @make_backup: %TRUE if a backup should be created.
6616 * @flags: a set of #GFileCreateFlags.
6617 * @new_etag: a location to a new <link linkend="gfile-etag">entity tag</link>
6618 * for the document. This should be freed with g_free() when no longer
6619 * needed, or %NULL
6620 * @cancellable: optional #GCancellable object, %NULL to ignore.
6621 * @error: a #GError, or %NULL
6623 * Replaces the contents of @file with @contents of @length bytes.
6625 * If @etag is specified (not %NULL) any existing file must have that etag, or
6626 * the error %G_IO_ERROR_WRONG_ETAG will be returned.
6628 * If @make_backup is %TRUE, this function will attempt to make a backup of @file.
6630 * If @cancellable is not %NULL, then the operation can be cancelled by
6631 * triggering the cancellable object from another thread. If the operation
6632 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6634 * The returned @new_etag can be used to verify that the file hasn't changed the
6635 * next time it is saved over.
6637 * Returns: %TRUE if successful. If an error
6638 * has occurred, this function will return %FALSE and set @error
6639 * appropriately if present.
6641 gboolean
6642 g_file_replace_contents (GFile *file,
6643 const char *contents,
6644 gsize length,
6645 const char *etag,
6646 gboolean make_backup,
6647 GFileCreateFlags flags,
6648 char **new_etag,
6649 GCancellable *cancellable,
6650 GError **error)
6652 GFileOutputStream *out;
6653 gsize pos, remainder;
6654 gssize res;
6655 gboolean ret;
6657 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6658 g_return_val_if_fail (contents != NULL, FALSE);
6660 out = g_file_replace (file, etag, make_backup, flags, cancellable, error);
6661 if (out == NULL)
6662 return FALSE;
6664 pos = 0;
6665 remainder = length;
6666 while (remainder > 0 &&
6667 (res = g_output_stream_write (G_OUTPUT_STREAM (out),
6668 contents + pos,
6669 MIN (remainder, GET_CONTENT_BLOCK_SIZE),
6670 cancellable,
6671 error)) > 0)
6673 pos += res;
6674 remainder -= res;
6677 if (remainder > 0 && res < 0)
6679 /* Ignore errors on close */
6680 g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, NULL);
6681 g_object_unref (out);
6683 /* error is set already */
6684 return FALSE;
6687 ret = g_output_stream_close (G_OUTPUT_STREAM (out), cancellable, error);
6689 if (new_etag)
6690 *new_etag = g_file_output_stream_get_etag (out);
6692 g_object_unref (out);
6694 return ret;
6697 typedef struct {
6698 GFile *file;
6699 GError *error;
6700 GCancellable *cancellable;
6701 GAsyncReadyCallback callback;
6702 gpointer user_data;
6703 const char *content;
6704 gsize length;
6705 gsize pos;
6706 char *etag;
6707 } ReplaceContentsData;
6709 static void
6710 replace_contents_data_free (ReplaceContentsData *data)
6712 if (data->error)
6713 g_error_free (data->error);
6714 if (data->cancellable)
6715 g_object_unref (data->cancellable);
6716 g_object_unref (data->file);
6717 g_free (data->etag);
6718 g_free (data);
6721 static void
6722 replace_contents_close_callback (GObject *obj,
6723 GAsyncResult *close_res,
6724 gpointer user_data)
6726 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6727 ReplaceContentsData *data = user_data;
6728 GSimpleAsyncResult *res;
6730 /* Ignore errors here, we're only reading anyway */
6731 g_output_stream_close_finish (stream, close_res, NULL);
6732 g_object_unref (stream);
6734 data->etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (stream));
6736 res = g_simple_async_result_new (G_OBJECT (data->file),
6737 data->callback,
6738 data->user_data,
6739 g_file_replace_contents_async);
6740 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)replace_contents_data_free);
6741 g_simple_async_result_complete (res);
6742 g_object_unref (res);
6745 static void
6746 replace_contents_write_callback (GObject *obj,
6747 GAsyncResult *read_res,
6748 gpointer user_data)
6750 GOutputStream *stream = G_OUTPUT_STREAM (obj);
6751 ReplaceContentsData *data = user_data;
6752 GError *error = NULL;
6753 gssize write_size;
6755 write_size = g_output_stream_write_finish (stream, read_res, &error);
6757 if (write_size <= 0)
6759 /* Error or EOF, close the file */
6760 if (write_size < 0)
6761 data->error = error;
6762 g_output_stream_close_async (stream, 0,
6763 data->cancellable,
6764 replace_contents_close_callback, data);
6766 else if (write_size > 0)
6768 data->pos += write_size;
6770 if (data->pos >= data->length)
6771 g_output_stream_close_async (stream, 0,
6772 data->cancellable,
6773 replace_contents_close_callback, data);
6774 else
6775 g_output_stream_write_async (stream,
6776 data->content + data->pos,
6777 data->length - data->pos,
6779 data->cancellable,
6780 replace_contents_write_callback,
6781 data);
6785 static void
6786 replace_contents_open_callback (GObject *obj,
6787 GAsyncResult *open_res,
6788 gpointer user_data)
6790 GFile *file = G_FILE (obj);
6791 GFileOutputStream *stream;
6792 ReplaceContentsData *data = user_data;
6793 GError *error = NULL;
6794 GSimpleAsyncResult *res;
6796 stream = g_file_replace_finish (file, open_res, &error);
6798 if (stream)
6800 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
6801 data->content + data->pos,
6802 data->length - data->pos,
6804 data->cancellable,
6805 replace_contents_write_callback,
6806 data);
6809 else
6811 res = g_simple_async_result_new_from_error (G_OBJECT (data->file),
6812 data->callback,
6813 data->user_data,
6814 error);
6815 g_simple_async_result_complete (res);
6816 g_error_free (error);
6817 replace_contents_data_free (data);
6818 g_object_unref (res);
6823 * g_file_replace_contents_async:
6824 * @file: input #GFile.
6825 * @contents: string of contents to replace the file with.
6826 * @length: the length of @contents in bytes.
6827 * @etag: (allow-none): a new <link linkend="gfile-etag">entity tag</link> for the @file, or %NULL
6828 * @make_backup: %TRUE if a backup should be created.
6829 * @flags: a set of #GFileCreateFlags.
6830 * @cancellable: optional #GCancellable object, %NULL to ignore.
6831 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
6832 * @user_data: the data to pass to callback function
6834 * Starts an asynchronous replacement of @file with the given
6835 * @contents of @length bytes. @etag will replace the document's
6836 * current entity tag.
6838 * When this operation has completed, @callback will be called with
6839 * @user_user data, and the operation can be finalized with
6840 * g_file_replace_contents_finish().
6842 * If @cancellable is not %NULL, then the operation can be cancelled by
6843 * triggering the cancellable object from another thread. If the operation
6844 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6846 * If @make_backup is %TRUE, this function will attempt to
6847 * make a backup of @file.
6849 void
6850 g_file_replace_contents_async (GFile *file,
6851 const char *contents,
6852 gsize length,
6853 const char *etag,
6854 gboolean make_backup,
6855 GFileCreateFlags flags,
6856 GCancellable *cancellable,
6857 GAsyncReadyCallback callback,
6858 gpointer user_data)
6860 ReplaceContentsData *data;
6862 g_return_if_fail (G_IS_FILE (file));
6863 g_return_if_fail (contents != NULL);
6865 data = g_new0 (ReplaceContentsData, 1);
6867 if (cancellable)
6868 data->cancellable = g_object_ref (cancellable);
6869 data->callback = callback;
6870 data->user_data = user_data;
6871 data->content = contents;
6872 data->length = length;
6873 data->pos = 0;
6874 data->file = g_object_ref (file);
6876 g_file_replace_async (file,
6877 etag,
6878 make_backup,
6879 flags,
6881 cancellable,
6882 replace_contents_open_callback,
6883 data);
6887 * g_file_replace_contents_finish:
6888 * @file: input #GFile.
6889 * @res: a #GAsyncResult.
6890 * @new_etag: (out) (allow-none): a location of a new <link linkend="gfile-etag">entity tag</link>
6891 * for the document. This should be freed with g_free() when it is no
6892 * longer needed, or %NULL
6893 * @error: a #GError, or %NULL
6895 * Finishes an asynchronous replace of the given @file. See
6896 * g_file_replace_contents_async(). Sets @new_etag to the new entity
6897 * tag for the document, if present.
6899 * Returns: %TRUE on success, %FALSE on failure.
6901 gboolean
6902 g_file_replace_contents_finish (GFile *file,
6903 GAsyncResult *res,
6904 char **new_etag,
6905 GError **error)
6907 GSimpleAsyncResult *simple;
6908 ReplaceContentsData *data;
6910 g_return_val_if_fail (G_IS_FILE (file), FALSE);
6911 g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE);
6913 simple = G_SIMPLE_ASYNC_RESULT (res);
6915 if (g_simple_async_result_propagate_error (simple, error))
6916 return FALSE;
6918 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_replace_contents_async);
6920 data = g_simple_async_result_get_op_res_gpointer (simple);
6922 if (data->error)
6924 g_propagate_error (error, data->error);
6925 data->error = NULL;
6926 return FALSE;
6930 if (new_etag)
6932 *new_etag = data->etag;
6933 data->etag = NULL; /* Take ownership */
6936 return TRUE;
6940 * g_file_start_mountable:
6941 * @file: input #GFile.
6942 * @flags: flags affecting the operation
6943 * @start_operation: a #GMountOperation, or %NULL to avoid user interaction.
6944 * @cancellable: optional #GCancellable object, %NULL to ignore.
6945 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
6946 * @user_data: the data to pass to callback function
6948 * Starts a file of type G_FILE_TYPE_MOUNTABLE.
6949 * Using @start_operation, you can request callbacks when, for instance,
6950 * passwords are needed during authentication.
6952 * If @cancellable is not %NULL, then the operation can be cancelled by
6953 * triggering the cancellable object from another thread. If the operation
6954 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
6956 * When the operation is finished, @callback will be called. You can then call
6957 * g_file_mount_mountable_finish() to get the result of the operation.
6959 * Since: 2.22
6961 void
6962 g_file_start_mountable (GFile *file,
6963 GDriveStartFlags flags,
6964 GMountOperation *start_operation,
6965 GCancellable *cancellable,
6966 GAsyncReadyCallback callback,
6967 gpointer user_data)
6969 GFileIface *iface;
6971 g_return_if_fail (G_IS_FILE (file));
6973 iface = G_FILE_GET_IFACE (file);
6975 if (iface->start_mountable == NULL)
6977 g_simple_async_report_error_in_idle (G_OBJECT (file),
6978 callback,
6979 user_data,
6980 G_IO_ERROR,
6981 G_IO_ERROR_NOT_SUPPORTED,
6982 _("Operation not supported"));
6983 return;
6986 (* iface->start_mountable) (file,
6987 flags,
6988 start_operation,
6989 cancellable,
6990 callback,
6991 user_data);
6995 * g_file_start_mountable_finish:
6996 * @file: input #GFile.
6997 * @result: a #GAsyncResult.
6998 * @error: a #GError, or %NULL
7000 * Finishes a start operation. See g_file_start_mountable() for details.
7002 * Finish an asynchronous start operation that was started
7003 * with g_file_start_mountable().
7005 * Returns: %TRUE if the operation finished successfully. %FALSE
7006 * otherwise.
7008 * Since: 2.22
7010 gboolean
7011 g_file_start_mountable_finish (GFile *file,
7012 GAsyncResult *result,
7013 GError **error)
7015 GFileIface *iface;
7017 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7018 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7020 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7022 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7023 if (g_simple_async_result_propagate_error (simple, error))
7024 return FALSE;
7027 iface = G_FILE_GET_IFACE (file);
7028 return (* iface->start_mountable_finish) (file, result, error);
7032 * g_file_stop_mountable:
7033 * @file: input #GFile.
7034 * @flags: flags affecting the operation
7035 * @mount_operation: a #GMountOperation, or %NULL to avoid user interaction.
7036 * @cancellable: optional #GCancellable object, %NULL to ignore.
7037 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7038 * @user_data: the data to pass to callback function
7040 * Stops a file of type G_FILE_TYPE_MOUNTABLE.
7042 * If @cancellable is not %NULL, then the operation can be cancelled by
7043 * triggering the cancellable object from another thread. If the operation
7044 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7046 * When the operation is finished, @callback will be called. You can then call
7047 * g_file_stop_mountable_finish() to get the result of the operation.
7049 * Since: 2.22
7051 void
7052 g_file_stop_mountable (GFile *file,
7053 GMountUnmountFlags flags,
7054 GMountOperation *mount_operation,
7055 GCancellable *cancellable,
7056 GAsyncReadyCallback callback,
7057 gpointer user_data)
7059 GFileIface *iface;
7061 g_return_if_fail (G_IS_FILE (file));
7063 iface = G_FILE_GET_IFACE (file);
7065 if (iface->stop_mountable == NULL)
7067 g_simple_async_report_error_in_idle (G_OBJECT (file),
7068 callback,
7069 user_data,
7070 G_IO_ERROR,
7071 G_IO_ERROR_NOT_SUPPORTED,
7072 _("Operation not supported"));
7073 return;
7076 (* iface->stop_mountable) (file,
7077 flags,
7078 mount_operation,
7079 cancellable,
7080 callback,
7081 user_data);
7085 * g_file_stop_mountable_finish:
7086 * @file: input #GFile.
7087 * @result: a #GAsyncResult.
7088 * @error: a #GError, or %NULL
7090 * Finishes an stop operation, see g_file_stop_mountable() for details.
7092 * Finish an asynchronous stop operation that was started
7093 * with g_file_stop_mountable().
7095 * Returns: %TRUE if the operation finished successfully. %FALSE
7096 * otherwise.
7098 * Since: 2.22
7100 gboolean
7101 g_file_stop_mountable_finish (GFile *file,
7102 GAsyncResult *result,
7103 GError **error)
7105 GFileIface *iface;
7107 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7108 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7110 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7112 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7113 if (g_simple_async_result_propagate_error (simple, error))
7114 return FALSE;
7117 iface = G_FILE_GET_IFACE (file);
7118 return (* iface->stop_mountable_finish) (file, result, error);
7122 * g_file_poll_mountable:
7123 * @file: input #GFile.
7124 * @cancellable: optional #GCancellable object, %NULL to ignore.
7125 * @callback: a #GAsyncReadyCallback to call when the request is satisfied, or %NULL.
7126 * @user_data: the data to pass to callback function
7128 * Polls a file of type G_FILE_TYPE_MOUNTABLE.
7130 * If @cancellable is not %NULL, then the operation can be cancelled by
7131 * triggering the cancellable object from another thread. If the operation
7132 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
7134 * When the operation is finished, @callback will be called. You can then call
7135 * g_file_mount_mountable_finish() to get the result of the operation.
7137 * Since: 2.22
7139 void
7140 g_file_poll_mountable (GFile *file,
7141 GCancellable *cancellable,
7142 GAsyncReadyCallback callback,
7143 gpointer user_data)
7145 GFileIface *iface;
7147 g_return_if_fail (G_IS_FILE (file));
7149 iface = G_FILE_GET_IFACE (file);
7151 if (iface->poll_mountable == NULL)
7153 g_simple_async_report_error_in_idle (G_OBJECT (file),
7154 callback,
7155 user_data,
7156 G_IO_ERROR,
7157 G_IO_ERROR_NOT_SUPPORTED,
7158 _("Operation not supported"));
7159 return;
7162 (* iface->poll_mountable) (file,
7163 cancellable,
7164 callback,
7165 user_data);
7169 * g_file_poll_mountable_finish:
7170 * @file: input #GFile.
7171 * @result: a #GAsyncResult.
7172 * @error: a #GError, or %NULL
7174 * Finishes a poll operation. See g_file_poll_mountable() for details.
7176 * Finish an asynchronous poll operation that was polled
7177 * with g_file_poll_mountable().
7179 * Returns: %TRUE if the operation finished successfully. %FALSE
7180 * otherwise.
7182 * Since: 2.22
7184 gboolean
7185 g_file_poll_mountable_finish (GFile *file,
7186 GAsyncResult *result,
7187 GError **error)
7189 GFileIface *iface;
7191 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7192 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
7194 if (G_IS_SIMPLE_ASYNC_RESULT (result))
7196 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
7197 if (g_simple_async_result_propagate_error (simple, error))
7198 return FALSE;
7201 iface = G_FILE_GET_IFACE (file);
7202 return (* iface->poll_mountable_finish) (file, result, error);
7206 * g_file_supports_thread_contexts:
7207 * @file: a #GFile.
7209 * Checks if @file supports <link
7210 * linkend="g-main-context-push-thread-default-context">thread-default
7211 * contexts</link>. If this returns %FALSE, you cannot perform
7212 * asynchronous operations on @file in a thread that has a
7213 * thread-default context.
7215 * Returns: Whether or not @file supports thread-default contexts.
7217 * Since: 2.22
7219 gboolean
7220 g_file_supports_thread_contexts (GFile *file)
7222 GFileIface *iface;
7224 g_return_val_if_fail (G_IS_FILE (file), FALSE);
7226 iface = G_FILE_GET_IFACE (file);
7227 return iface->supports_thread_contexts;